You are here

function views_many_to_one_helper::add_filter in Views (for Drupal 7) 6.3

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

File

includes/handlers.inc, line 980
handlers.inc 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

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;
  if (empty($options['group'])) {
    $options['group'] = 0;
  }
  $placeholder = !empty($this->handler->definition['numeric']) ? '%d' : "'%s'";
  if ($operator == 'not') {
    $this->handler->query
      ->add_where($options['group'], "{$field} IS NULL");
  }
  else {
    if ($operator == 'or' && empty($options['reduce_duplicates'])) {
      if (count($this->handler->value) > 1) {
        $replace = array_fill(0, sizeof($this->handler->value), $placeholder);
        $in = '(' . implode(", ", $replace) . ')';
        $this->handler->query
          ->add_where($options['group'], "{$field} IN {$in}", $this->handler->value);
      }
      else {
        $this->handler->query
          ->add_where($options['group'], "{$field} = {$placeholder}", $this->handler->value);
      }
    }
    else {
      $field = $this->handler->real_field;
      $clauses = array();
      foreach ($this->handler->table_aliases as $value => $alias) {
        $clauses[] = "{$alias}.{$field} = {$placeholder}";
      }
      $group = empty($options['group']) ? 0 : $options['group'];

      // implode on either AND or OR.
      $this->handler->query
        ->add_where($group, implode(' ' . strtoupper($operator) . ' ', $clauses), $this->handler->value);
    }
  }
}