You are here

function oa_core_views_query_alter in Open Atrium Core 7.2

Implements hook_views_query_alter().

File

./oa_core.module, line 1848

Code

function oa_core_views_query_alter(&$view, &$query) {

  // only show space types allowed to create.
  if ($view->name == 'oa_core_space_types') {
    $tids = oa_core_get_allowed_space_terms();
    if (isset($tids)) {
      $tids = array_keys($tids);
      $query
        ->add_where(1, 'taxonomy_term_data.tid', $tids, 'IN');
    }
  }

  // We only modify views that have 'og_group_ref_target_id' as an exposed
  // filter and only when that filter has a value. We are adding support for
  // the 'Visible in other Spaces' (oa_other_spaces_ref) field.
  if (!empty($view->exposed_input['og_group_ref_target_id']) && is_numeric($view->exposed_input['og_group_ref_target_id']) && !empty($view->filter['og_group_ref_target_id']->options['exposed'])) {

    // Ensure that we've joined against 'field_data_oa_other_spaces_ref' table.
    $join = new views_join();
    $join
      ->construct('field_data_oa_other_spaces_ref', 'node', 'nid', 'entity_id', array(
      array(
        'field' => 'entity_type',
        'value' => 'node',
      ),
      array(
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ),
    ));
    $alias = $query
      ->ensure_table('field_data_oa_other_spaces_ref', 'node', $join);
    if ($alias) {

      // Only change the query if the join to node table worked
      //
      // Add a new 'where group' which is an OR and move 'og_group_ref_target_id'
      // to it (and grab it's value).
      $extra_groups = array();
      $group = $query
        ->set_where_group('OR');
      foreach ($query->where[1]['conditions'] as $key => $condition) {

        // If the field is 'og_membership.gid' or an alias for the same column.
        $column = 'og_membership';
        if (!empty($condition['field']) && is_string($condition['field']) && ($condition['field'] === $column || $condition['field'] === $column . ".gid" || substr($condition['field'], -strlen($column) - 2) === "__{$column}")) {

          // Move the condition to our new where group.
          // Also need to modify the condition to only match the og_membership.gid field
          // if the "other_spaces" field is empty, or has a delta of zero
          // This prevents duplicate entries when the node is visible in multiple
          // other spaces and is faster than making the query distinct.
          $sub_condition = db_or()
            ->condition("{$alias}.delta", NULL, 'IS NULL')
            ->condition("{$alias}.delta", 0);
          $new_condition = db_and()
            ->condition($condition['field'], $condition['value'], $condition['operator'])
            ->condition($sub_condition);
          $query
            ->add_where($group, $new_condition);
          $extra_groups[] = $condition['value'];
          unset($query->where[1]['conditions'][$key]);
          break;
        }
      }

      // Finally, we filter against 'field_data_oa_other_spaces_ref' with the
      // same value as was given to 'oa_group_ref_target_id'.
      if (!empty($extra_groups)) {
        $query
          ->add_where($group, "{$alias}.oa_other_spaces_ref_target_id", $extra_groups, 'IN');

        // Next, check if the section filter condition needs to be updated
        if (module_exists('oa_subspaces') && !empty($view->exposed_input['oa_section_ref_target_id']) && is_numeric($view->exposed_input['oa_section_ref_target_id']) && !empty($view->filter['oa_section_ref_target_id']->options['exposed'])) {
          $columns = array(
            'field_data_oa_section_ref.oa_section_ref_target_id',
          );

          // get a list of matching section names in the other spaces
          $extra_sections = oa_subspaces_matching_sections($view->exposed_input['oa_section_ref_target_id'], array(), $extra_groups);
          oa_core_view_query_modify_condition($query, $columns, $extra_sections);
        }
      }
    }
  }
}