You are here

function og_subgroups_children_load_multiple in Subgroups for Organic groups 7.2

Get the inheriented groups + current group.

2 calls to og_subgroups_children_load_multiple()
og_subgroups_children_load in ./og_subgroups.common.inc
Gets the children of the given group including current group.
og_subgroup_user_groups_load in ./og_subgroups.common.inc
Return all the groups a user belongs to.

File

./og_subgroups.common.inc, line 451
Common functions used in og_subgroups.

Code

function og_subgroups_children_load_multiple($groups, $filter = TRUE, $fetch_all = TRUE, $include_current = TRUE, $reset = FALSE, $main_level = TRUE) {
  $group_cache =& drupal_static(__FUNCTION__, array());
  $return = array();

  // Deep trees make the regular caching less then effective, so allow
  // performance improvements for sites that have deep trees that may
  // not be best for flatter sites.
  $deep_tree_performance = variable_get('og_subgroups_deep_tree_performance', FALSE);
  $group_cache_all =& drupal_static(__FUNCTION__ . '_all', array());

  // Recursion protection protects both from infinite recursion and from
  // reprocessing the same children if a parent is the child of two groups.
  if ($main_level && $deep_tree_performance) {
    $all_cid = print_r($groups, 1) . ':' . ($filter ? 1 : 0) . ':' . ($fetch_all ? 1 : 0) . ':' . ($include_current ? 1 : 0);
    if ($deep_tree_performance && isset($group_cache_all[$all_cid])) {
      return $group_cache_all[$all_cid];
    }
  }
  foreach ($groups as $group_type => $group_ids) {

    // Reprocess all if resetting.
    if ($reset) {
      $process = $group_ids;
    }
    else {
      $process = array();
      foreach ($group_ids as $group_id) {
        $cid = _og_subgroups_load_multiple_cid('children', $group_type, $group_id, $filter);

        // Fetch from cache if possible.
        if (!isset($group_cache[$cid])) {
          if ($cache = cache_get($cid)) {
            $group_cache[$cid] = $cache->data;
          }
          else {
            $process[] = $group_id;
          }
        }
      }
    }
    if ($process) {
      $inheritated_groups = _og_subgroups_get_field_matching($group_type, $process, OG_USER_INHERITANCE_FIELD, 1);
      foreach ($process as $group_id) {
        $key = array_search($group_id, $process);
        unset($process[$key]);
        $cid = _og_subgroups_load_multiple_cid('children', $group_type, $group_id, $filter);

        // If group does not inherient then no child groups.
        if ($filter && !in_array($group_id, $inheritated_groups)) {
          $group_cache[$cid] = array();
        }
        else {
          $group_cache[$cid] = array();
          foreach (og_subgroups_get_associated_entities($group_type, $group_id, array(), TRUE) as $entity_type => $entity_ids) {
            $group_cache[$cid][$entity_type] = drupal_map_assoc($entity_ids);
          }
          cache_set($cid, $group_cache[$cid]);
        }
      }

      // Any leftovers are from a failed entity_load, so just set blank arrays
      // for them.
      foreach ($process as $key => $group_id) {
        unset($process[$key]);
        $cid = _og_subgroups_load_multiple_cid('children', $group_type, $group_id, $filter);
        $group_cache[$cid][$group_type] = array();
        cache_set($cid, $group_cache[$cid]);
      }
    }

    // Add them to return array.
    foreach ($group_ids as $group_id) {
      $cid = _og_subgroups_load_multiple_cid('children', $group_type, $group_id, $filter);
      $current_return = $return;
      $return = og_subgroups_merge_groups($return, $group_cache[$cid]);

      // If current group has not been processed, process it's children.
      if ($fetch_all && !isset($return[$group_type][$group_id]) && !empty($group_cache[$cid])) {

        // We want to find children that we haven't processed yet and that could be parents.
        $process_children = array();
        foreach ($group_cache[$cid] as $new_group_type => $children) {

          // If it's in return before current children added, it's already been processed.
          $not_processed = !empty($current_return[$group_type]) ? array_diff($children, $current_return[$group_type]) : $children;
          unset($children[$group_type][$group_id]);

          // If performing for big trees, remove any ones that don't have any children.
          if ($deep_tree_performance && ($potentional_parents = og_subgroups_get_potentional_parents($group_type))) {
            $process_children[$new_group_type] = array_intersect($not_processed, $potentional_parents);
          }
          else {
            $process_children[$new_group_type] = $not_processed;
          }
        }

        // If we're optomizing for large tree, remove any children that aren't parents of anything.
        if (array_filter($process_children)) {
          $return = og_subgroups_merge_groups($return, og_subgroups_children_load_multiple($process_children, $filter, $fetch_all, TRUE, $reset, FALSE));
        }
      }

      // Include current will generally be set for all but the originall caller.
      // which should prevent recurssion.
      if ($include_current) {
        $return[$group_type][$group_id] = $group_id;
      }
    }
  }
  if (isset($all_cid) && $deep_tree_performance) {
    $group_cache_all[$all_cid] = $return;
  }
  return $return;
}