You are here

function og_subgroups_parents_load_multiple in Subgroups for Organic groups 7.2

Same as og_subgroups_parents_load() but takes in an array of groups.

1 call to og_subgroups_parents_load_multiple()
og_subgroups_parents_load in ./og_subgroups.common.inc
Return an array of all the parent groups, optionally filtered including only the groups that allow for inheritance.

File

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

Code

function og_subgroups_parents_load_multiple($groups, $filter = TRUE, $fetch_all = TRUE, $include_current = TRUE, $reset = FALSE) {
  $group_cache =& drupal_static(__FUNCTION__, array());
  $return = array();
  foreach ($groups as $group_type => $group_ids) {

    // Reprocess all if resetting.
    if ($reset) {
      $process = $group_ids;
    }
    else {

      // Only find group_ids we haven't looked up yet.
      $process = array();
      foreach ($group_ids as $group_id) {
        $cid = _og_subgroups_load_multiple_cid('parents', $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) {
      foreach ($process as $group_id) {
        $key = array_search($group_id, $process);
        unset($process[$key]);
        $cid = _og_subgroups_load_multiple_cid('parents', $group_type, $group_id, $filter);
        $group_cache[$cid] = array();
        if ($parent_groups = og_get_entity_groups($group_type, $group_id)) {
          foreach ($parent_groups as $parent_group_type => $parent_group_ids) {

            // If not filtering, than all ids are valid.
            if (!$filter) {
              $group_cache[$cid][$parent_group_type] = drupal_map_assoc($parent_group_ids);
            }
            else {
              foreach (_og_subgroups_get_field_matching($parent_group_type, $parent_group_ids, OG_USER_INHERITANCE_FIELD, 1) as $parent_group_id) {
                $group_cache[$cid][$parent_group_type][$parent_group_id] = $parent_group_id;
              }
            }
          }
        }
        cache_set($cid, $group_cache[$cid]);
      }

      // Any leftovers are from a failed entity_load, so just set blank arrays
      // for them.
      foreach ($process as $group_id) {
        $cid = _og_subgroups_load_multiple_cid('parents', $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('parents', $group_type, $group_id, $filter);
      $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])) {
        $return = og_subgroups_merge_groups($return, og_subgroups_parents_load_multiple($group_cache[$cid], $filter, $fetch_all, TRUE, $reset));
      }

      // Include current will be set for all child calls, which should generally prevent recurssion.
      if ($include_current) {
        $return[$group_type][$group_id] = $group_id;
      }
    }
  }
  return $return;
}