You are here

function workbench_access_build_tree in Workbench Access 7

Build a hierarchy defined by an access control schema.

Note that unlike taxonomy_build_tree() and similar, the child items are expressly listed as an array of the parent, for easier checking later.

Parameters

&$tree: The hierarchy array, passed by reference.

$sections: An optional array of sections to limit the return set.

$depth: Internal depth marker, used for recursive array processing. Do not use.

Return value

An array of items within the given access scheme.

4 calls to workbench_access_build_tree()
workbench_access_get_active_tree in ./workbench_access.module
Load the active tree.
workbench_access_get_user_tree in ./workbench_access.module
Build an access tree for a user account.
workbench_access_handler_filter_access::query in includes/workbench_access_handler_filter_access.inc
Add this filter to the query.
workbench_access_node_operations in ./workbench_access.module
Implements hook_node_operations().

File

./workbench_access.module, line 1239
Workbench Access module file.

Code

function workbench_access_build_tree(&$tree, $sections = NULL, $depth = -1) {
  static $max_depth;
  if (!isset($max_depth)) {
    $max_depth = 0;
  }
  if ($depth > $max_depth) {
    if (empty($sections)) {

      // Reset max_depth static in case we get a new function call.
      $max_depth = 0;
      return;
    }
    else {
      $new_tree = array();
      $sections = array_flip($sections);

      // Find all the children of the active sections.
      foreach ($tree as $access_id => $data) {
        if (isset($sections[$access_id])) {
          $new_tree[$access_id] = $tree[$access_id];
          if (isset($tree[$access_id]['children'])) {
            foreach ($tree[$access_id]['children'] as $id) {
              $new_tree[$id] = $tree[$id];
            }
          }
        }
      }

      // Remove sections that should not be shown.
      foreach ($tree as $access_id => $data) {
        if (!isset($new_tree[$access_id])) {
          unset($tree[$access_id]);
        }
      }

      // Reset max_depth static in case we get a new function call.
      $max_depth = 0;
      return;
    }
  }
  $depth++;
  foreach ($tree as $id => $item) {
    if ($item['depth'] > $max_depth) {
      $max_depth = $item['depth'];
    }

    // Set the parentage of this item.
    if ($depth == 0 && !empty($item['parent']) && isset($tree[$item['parent']])) {
      $tree[$item['parent']]['children'][$id] = $id;
    }
    elseif ($item['depth'] > 0 && !empty($item['children']) && isset($tree[$item['parent']]['children'])) {
      foreach ($item['children'] as $child_id) {
        if (!isset($child_id, $tree[$item['parent']]['children'][$child_id])) {
          $tree[$item['parent']]['children'][$child_id] = $child_id;
        }
      }
    }
  }
  workbench_access_build_tree($tree, $sections, $depth);
}