You are here

function og_query_og_membership_alter in Organic groups 7.2

Implements hook_query_TAG_alter().

Join the {og_membership} table and alter the query.

File

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

Code

function og_query_og_membership_alter(QueryAlterableInterface $query) {
  $tables =& $query
    ->getTables();
  $fields =& $query
    ->getFields();
  $conditions =& $query
    ->conditions();

  // Find the group-audience fields.
  $field_names = array();
  foreach ($query->alterMetaData['entity_field_query']->fieldConditions as $values) {
    $field_name = $values['field']['field_name'];
    if (og_is_group_audience_field($field_name)) {
      $field_names[] = $field_name;
    }
  }
  $aliases = array();
  $base_table = FALSE;
  $base_table_alias = '';
  foreach ($tables as $alias => $values) {
    if (!$base_table_alias && empty($values['join type'])) {
      $base_table_alias = $alias;
    }
    if (strpos($alias, 'field_data') !== 0) {
      continue;
    }
    $field_name = substr($values['table'], 11);
    if (!in_array($field_name, $field_names)) {
      continue;
    }
    if (empty($values['join type'])) {

      // This is the base table, so remove it in favor of OG membership.
      $base_table = TRUE;
    }
    unset($tables[$alias]);
    $aliases[$alias] = $field_name;
  }
  foreach ($aliases as $alias => $field_name) {
    foreach ($tables as $key => $values) {
      $condition = str_replace("{$alias}.entity_type", 'ogm.entity_type', $values['condition']);
      $condition = str_replace("{$alias}.entity_id", 'ogm.etid', $condition);
      $tables[$key]['condition'] = $condition;
    }
  }
  $entity_type = $query->alterMetaData['entity_field_query']->entityConditions['entity_type']['value'];
  $entity_type = is_array($entity_type) ? $entity_type[0] : $entity_type;
  $entity_info = entity_get_info($entity_type);
  $id = $entity_info['entity keys']['id'];
  if ($base_table) {

    // If the table of the base entity does not exist (e.g. there is no
    // property condition), we need to add it, as we don't have the
    // revision ID and bundle in {og_membership} table.
    $base_table = $entity_info['base table'];
    if (strpos($base_table_alias, 'field_data') === 0) {

      // Check if the entity base table already exists.
      $base_table_alias = FALSE;
      foreach ($tables as $table) {
        if ($table['table'] == $base_table) {
          $base_table_alias = $table['alias'];
          break;
        }
      }
      if (!$base_table_alias) {
        $base_table_alias = $query
          ->innerJoin($base_table, NULL, "{$base_table}.{$id} = ogm.etid");
      }
    }

    // Point the revision ID and bundle to the base entity.
    $fields['revision_id']['table'] = $base_table;

    // If there is no revision table, use the bundle.
    if (!empty($entity_info['entity keys']['revision'])) {

      // Entity doesn't support revisions.
      $fields['revision_id']['field'] = $entity_info['entity keys']['revision'];
    }
    elseif (!empty($entity_info['entity keys']['bundle'])) {
      $fields['revision_id']['field'] = $entity_info['entity keys']['bundle'];
    }
    else {

      // Entity doesn't have bundles (e.g. user).
      $fields['revision_id']['field'] = $id;
    }
    $fields['bundle']['table'] = $base_table;
    $fields['bundle']['field'] = !empty($entity_info['entity keys']['bundle']) ? $entity_info['entity keys']['bundle'] : $id;
    $fields['entity_type']['table'] = 'ogm';
    $fields['entity_type']['field'] = 'entity_type';
    $fields['entity_id']['table'] = 'ogm';
    $fields['entity_id']['field'] = 'etid';

    // Populate the alias key, as it might be empty on COUNT queries.
    foreach (array_keys($fields) as $key) {
      if (empty($fields[$key]['alias'])) {
        $fields[$key]['alias'] = $key;
      }
    }
    $ogm = array(
      'join type' => NULL,
      'table' => 'og_membership',
      'alias' => 'ogm',
      'condition' => '',
      'arguments' => array(),
    );
    $tables = array_merge(array(
      'ogm' => $ogm,
    ), $tables);
  }
  else {

    // If the original EntityFieldQuery has an entity type entityCondition,
    // restrict the join by this. Otherwise, we would bring in the IDs of
    // entities of other types if they happen to match on the base table entity
    // ID.
    $query_base_entity_type = $query->alterMetaData['entity_field_query']->entityConditions['entity_type']['value'];
    if (empty($query_base_entity_type)) {

      // It's possible to have no entity type specified: join without it.
      $query
        ->join('og_membership', 'ogm', "ogm.etid = {$base_table_alias}.entity_id");
    }
    else {
      if (is_array($query_base_entity_type)) {

        // It's also possible for the entity type to be multiple.
        $query
          ->join('og_membership', 'ogm', "ogm.etid = {$base_table_alias}.entity_id AND ogm.entity_type IN (:entity_type)", array(
          ':entity_type' => $query_base_entity_type,
        ));
      }
      else {
        $query
          ->join('og_membership', 'ogm', "ogm.etid = {$base_table_alias}.entity_id AND ogm.entity_type = :entity_type", array(
          ':entity_type' => $query_base_entity_type,
        ));
      }
    }
  }
  _og_query_og_membership_alter_conditions($conditions, $aliases, $base_table_alias, $entity_info);
}