You are here

function og_subgroups_get_reverse_hierarchy_tree_perm in Subgroups for Organic groups 7

Check if a user has access permission in one of the ancestors groups and return the tree structure of the group hierarchy.

Parameters

$entity_groups: Array of group id to check and start moving up in the hierarchy.

$account: Optional; The account to check

$string: Optional; The permission string, if empty return the full tree structure in $structure, otherwise stops when the permission is grant.

$structure: Optional; This is the array that you should send by refeference to get back all the tree structure of the given groups. Array contain 2 keys ['gid'] - group id, and ['level'] the depth of the parent.

$level: Optional; Level of tree to start counting from, normally there is no need to change this. Default to 0 being the entity given.

@return TRUE if user has access grant with the given perm to one of the enstertors groups.

4 calls to og_subgroups_get_reverse_hierarchy_tree_perm()
OgSubgroupsAccessFromDescendanceTestCase::testOgSubgroupsGetReverseHierarchyTreePerm in ./og_subgroups.test
Use cases:
og_subgroups_graph_content_type_render in plugins/content_types/graph/graph.inc
og_subgroups_node_access_records_alter in ./og_subgroups.module
Implements hook_node_access_records_alter().
og_subgroups_og_user_access_alter in ./og_subgroups.module
Implements hook_og_user_access_alter()

File

./og_subgroups.module, line 45
Enable defining hierarchy of groups for organic groups.

Code

function og_subgroups_get_reverse_hierarchy_tree_perm($entity_groups, $string = '', $account = NULL, &$structure = array(), &$graph = FALSE, $level = 0) {

  // Check if user has the permission in a parent group.
  foreach ($entity_groups as $gid) {

    // Save the hierarchy structure.
    $structure[$gid] = array();
    $structure[$gid]['gid'] = $gid;
    $structure[$gid]['level'] = $level;

    // Build a graph with graph api
    if (is_array($graph) && module_exists('graphapi')) {
      $group = og_load($gid);
      graphapi_set_node_title($graph, $gid, $group->label);
    }

    // Check access permission.
    if ($string) {
      if (og_user_access($gid, $string, $account, TRUE)) {
        return TRUE;
      }
    }
  }

  // Get all groups that are content of user_groups (as an array of group ids).
  $groups = og_load_multiple($entity_groups);
  $parent_groups = array();
  foreach ($groups as $group) {

    // Load the entity associated with the group.
    $entity = og_load_entity_from_group($group->gid);

    // Get all groups that are associated with passed group.
    $parents = og_get_entity_groups($group->entity_type, $entity);
    $parent_groups += $parents;

    // Build a graph path with graph api
    if (is_array($graph) && module_exists('graphapi')) {
      foreach ($parents as $parent) {
        graphapi_set_node_title($graph, $parent, $parent);
        graphapi_set_link_data($graph, $group->gid, $parent, array(
          'color' => '#018FE2',
        ));
      }
    }
  }
  if ($parent_groups) {

    // Recurssion call of the function.
    return og_subgroups_get_reverse_hierarchy_tree_perm($parent_groups, $string, $account, $structure, $graph, ++$level);
  }
  else {

    // Reached a dead end, return false.
    return FALSE;
  }
}