You are here

function og_get_group_ids in Organic groups 7

Get group IDs by the entity type and entity IDs.

Parameters

$entity_type: The group entity type. Defaults to "group", that will return the group ID.

$etids: Array with the entity IDs that should be loaded. If FALSE, all groups IDs that belong to the entity will be returned.

$states: Array of states the group must be in. Values can be OG_STATE_ACTIVE, OG_STATE_PENDING or OG_STATE_BLOCKED. Defaults to OG_STATE_ACTIVE.

$soft_reset: A boolean indicating that the internal cache should be "soft" reset (i.e. only the cached values of the specific entity type). For a "hard" reset use

drupal_static_reset('og_get_group_ids');

Return value

Array keyed with the entity ID and the group ID as the value.

4 calls to og_get_group_ids()
OgGroupApi::testOgGetGroupIds in ./og.test
Test og_get_group_ids().
og_get_context_by_url in ./og.module
Select groups if they were passed in the URL.
og_get_group in ./og.module
Return a loaded group entity if exists or create a new one.
og_register_get_groups in og_register/og_register.module
Get all the groups node IDs that should appear in the user registration.
3 string references to 'og_get_group_ids'
OgGroupApi::testOgCrud in ./og.test
Test CRUD of group entities.
OgGroupApi::testOgGetGroupIds in ./og.test
Test og_get_group_ids().
og_invalidate_cache in ./og.module
Invalidate cache.

File

./og.module, line 1455
Enable users to create and manage groups with roles and permissions.

Code

function og_get_group_ids($entity_type = 'group', $etids = FALSE, $states = array(
  OG_STATE_ACTIVE,
), $soft_reset = FALSE) {
  $gids =& drupal_static(__FUNCTION__, array());
  if ($soft_reset || empty($gids[$entity_type]) || !empty($gids['__info'][$entity_type]['states']) && array_diff($gids['__info'][$entity_type]['states'], $states)) {
    $gids[$entity_type] = array();

    // Make sure the cached values are according to the states we are looking
    // for.
    $gids['__info'][$entity_type]['states'] = $states;
    $gids['__info'][$entity_type]['query all'] = FALSE;
  }
  $query_etids = $etids;

  // Check we don't already have the group IDs, and if we have them, return them
  // for the cache.
  if (!empty($gids[$entity_type])) {
    if ($query_etids !== FALSE) {
      $query_etids = array_diff($query_etids, array_flip($gids[$entity_type]));
      if (!$query_etids) {
        return array_intersect_key($gids[$entity_type], drupal_map_assoc($etids));
      }
    }
  }

  // Check if we need to query all group enteties, and if this was already
  // cached.
  if ($query_etids === FALSE && $gids['__info'][$entity_type]['query all']) {
    return $gids[$entity_type];
  }

  // Don't query if we have already queried all.
  if (empty($gids['__info'][$entity_type]['query all'])) {
    if (!empty($query_etids) && $entity_type == 'node' && module_exists('translation')) {

      // If a node is a translation of another node that is a group, we should
      // mark it as a group as-well.
      $query = db_select('node', 'node');
      $query
        ->fields('node', array(
        'tnid',
        'tnid',
      ))
        ->condition('nid', $query_etids, 'IN');

      // Add the real group node IDs to the entity IDs that will be queried
      // against the {og} table.
      if ($result = $query
        ->execute()
        ->fetchAllKeyed()) {
        $query_etids = array_merge($query_etids, $result);

        // Add it to the original IDs, as they are needed to later on intersect
        // with the results from the cache.
        $etids = array_merge($etids, $result);
      }
    }

    // We can't use EntityFieldQuery as it return only the group ID, but in
    // order to cache the results we need to maintain a relation between the
    // entity ID and the group ID.
    $query = db_select('og', 'og');
    if ($entity_type == 'group') {
      $query
        ->fields('og', array(
        'gid',
        'gid',
      ));
      if (!empty($query_etids)) {
        $query
          ->condition('gid', $query_etids, 'IN');
      }
    }
    else {
      $query
        ->fields('og', array(
        'etid',
        'gid',
      ));
      $query
        ->condition('entity_type', $entity_type);
      if (!empty($query_etids)) {
        $query
          ->condition('etid', $query_etids, 'IN');
      }
    }
    if (!empty($states)) {
      $query
        ->condition('state', $states, 'IN');
    }
    $gids[$entity_type] += $query
      ->execute()
      ->fetchAllKeyed();
  }

  // Make sure we return only the ids we were asked for, or if no specific IDs
  // were asked, then return all of them.
  if ($query_etids !== FALSE) {
    $return = array_intersect_key($gids[$entity_type], drupal_map_assoc($etids));
  }
  else {
    $return = $gids[$entity_type];

    // Let the cache know we queried all IDs of this entity type.
    $gids['__info'][$entity_type]['query all'] = TRUE;
  }
  return $return;
}