You are here

function _nodehierarchy_tree_data in Node Hierarchy 6.2

Same name and namespace in other branches
  1. 6.3 nodehierarchy.module \_nodehierarchy_tree_data()
  2. 7.2 nodehierarchy.module \_nodehierarchy_tree_data()

Recursive helper function to build the data representing a menu tree.

The function is a bit complex because the rendering of an item depends on the next menu item. So we are always rendering the element previously processed not the current one.

1 call to _nodehierarchy_tree_data()
nodehierarchy_tree_data in ./nodehierarchy.module
Build the data representing a menu tree.

File

./nodehierarchy.module, line 1586
A module to make nodes hierarchical.

Code

function _nodehierarchy_tree_data($result, $exclude = NULL, $allowed_types, $depth, $previous_element = array()) {
  $remnant = NULL;
  $tree = array();
  $enabled_tree = TRUE;
  $exclude = NULL;
  while ($item = db_fetch_array($result)) {
    if ($exclude !== $item['nid']) {
      $item['format'] = FILTER_FORMAT_DEFAULT;
      $item['disabled'] = in_array($item['type'], $allowed_types) ? FALSE : TRUE;
      $item['disabled'] = $item['disabled'] || !node_access('update', $item) && !user_access('create child of any parent');
      $enabled_tree = $enabled_tree || empty($item['disabled']) || isset($previous_element['disabled']) && empty($previous_element['disabled']);

      // The current item is the first in a new submenu.
      if ($item['depth'] > $depth) {

        // _menu_tree returns an item and the menu tree structure.
        list($item, $below) = _nodehierarchy_tree_data($result, $exclude, $allowed_types, $item['depth'], $item);
        if ($previous_element && ($below || !$previous_element['disabled'])) {
          $tree[_nodehierarchy_get_parent_selector_value($previous_element)] = $previous_element;
        }
        $tree += $below;

        // We need to fall back one level.
        if (!isset($item) || $item['depth'] < $depth) {
          return $enabled_tree ? array(
            $item,
            $tree,
          ) : array(
            $item,
            array(),
          );
        }

        // This will be the link to be output in the next iteration.
        $previous_element = $item;
      }
      elseif ($item['depth'] == $depth) {
        if ($previous_element && !$previous_element['disabled']) {

          // Only the first time.
          $tree[_nodehierarchy_get_parent_selector_value($previous_element)] = $previous_element;
        }

        // This will be the link to be output in the next iteration.
        $previous_element = $item;
      }
      else {
        $remnant = $item;
        break;
      }
    }
  }
  if ($previous_element && !$previous_element['disabled']) {

    // We have one more link dangling.
    $tree[_nodehierarchy_get_parent_selector_value($previous_element)] = $previous_element;
  }
  return $enabled_tree ? array(
    $remnant,
    $tree,
  ) : array(
    $remnant,
    array(),
  );
}