You are here

public function views_many_to_one_helper::add_filter in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 6.3 includes/handlers.inc \views_many_to_one_helper::add_filter()
  2. 6.2 includes/handlers.inc \views_many_to_one_helper::add_filter()

File

includes/handlers.inc, line 1058
Defines the various handler objects to help build and display views.

Class

views_many_to_one_helper
This many to one helper object is used on both arguments and filters.

Code

public function add_filter() {
  if (empty($this->handler->value)) {
    return;
  }
  $this->handler
    ->ensure_my_table();

  // Shorten some variables.
  $field = $this
    ->get_field();
  $options = $this->handler->options;
  $operator = $this->handler->operator;
  $formula = !empty($this->formula);
  $value = $this->handler->value;
  if (empty($options['group'])) {
    $options['group'] = 0;
  }

  // Determine whether a single expression is enough(FALSE) or the conditions
  // should be added via an db_or()/db_and() (TRUE).
  $add_condition = TRUE;
  if ($operator == 'or' && empty($options['reduce_duplicates'])) {
    if (count($value) > 1) {
      $operator = 'IN';
    }
    else {
      $value = is_array($value) ? array_pop($value) : $value;
      if (is_array($value) && count($value) > 1) {
        $operator = 'IN';
      }
      else {
        $operator = '=';
      }
    }
    $add_condition = FALSE;
  }
  if (!$add_condition) {
    if ($formula) {
      $placeholder = $this
        ->placeholder();
      if ($operator == 'IN') {
        $operator = "{$operator} IN({$placeholder})";
      }
      else {
        $operator = "{$operator} {$placeholder}";
      }
      $placeholders = array(
        $placeholder => $value,
      ) + $this->placeholders;
      $this->handler->query
        ->add_where_expression($options['group'], "{$field} {$operator}", $placeholders);
    }
    else {
      $this->handler->query
        ->add_where($options['group'], $field, $value, $operator);
    }
  }
  if ($add_condition) {
    $field = $this->handler->real_field;
    $clause = $operator == 'or' ? db_or() : db_and();
    foreach ($this->handler->table_aliases as $value => $alias) {
      if ($operator == 'not') {
        $value = NULL;
      }
      $clause
        ->condition("{$alias}.{$field}", $value);
    }

    // Implode on either AND or OR.
    $this->handler->query
      ->add_where($options['group'], $clause);
  }
}