You are here

function _search_autocomplete_get_ordered_list in Search Autocomplete 7.4

Same name and namespace in other branches
  1. 6.4 search_autocomplete.form.treelist.inc \_search_autocomplete_get_ordered_list()
  2. 6.2 search_autocomplete.form.treelist.inc \_search_autocomplete_get_ordered_list()
  3. 7.2 search_autocomplete.form.treelist.inc \_search_autocomplete_get_ordered_list()
  4. 7.3 search_autocomplete.form.treelist.inc \_search_autocomplete_get_ordered_list()

HELPER function to order a list.

Returns a tree list of all items in the $items array that are children of the supplied parent, ordered appropriately.

Return value

array the ordered tree

1 call to _search_autocomplete_get_ordered_list()
_search_autocomplete_get_items in ./search_autocomplete.form.treelist.inc
Helper function: get the forms from database and render them hierarchically.

File

./search_autocomplete.form.treelist.inc, line 219
Search Autocomplete Display the list of form to autocomplete and theme it as a draggable table.

Code

function _search_autocomplete_get_ordered_list($parent, $items, $depth = 0) {
  $remnant = array();
  $children = array();

  // Insert direct children in $children
  // and remaining in $remnant.
  foreach ($items as $item) {
    if ($item->parent_fid == $parent) {
      $item->depth = $depth;
      $children[] = $item;
    }
    else {
      $remnant[] = $item;
    }
  }

  // Sort the direct children by weight.
  usort($children, '_search_autocomplete_sort_by_weight');
  $ancestors = array();
  foreach ($children as $child) {

    // Order the child ancestors iteratively.
    $child_children = _search_autocomplete_get_ordered_list($child->fid, $remnant, $depth + 1);

    // Push the results into the main array below the child.
    $ancestors[] = $child;

    // Merge it if needed.
    if (count($child_children)) {
      $ancestors = array_merge($ancestors, $child_children);
    }
  }
  return $ancestors;
}