You are here

public function fivestar_views_handler_filter_in_operator::op_simple in Fivestar 7.2

Operator callback.

Overrides views_handler_filter_in_operator::op_simple

File

includes/fivestar_views_handler_filter_in_operator.inc, line 50
Provides the view handling functions for fivestar.

Class

fivestar_views_handler_filter_in_operator

Code

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

  // Zero needs special handling.
  $has_nul = in_array(0, $this->value);
  $or_statement = db_or();
  $and_statement = db_and();

  // Add regular condition if we have any values other than zero.
  if (count($this->value) > ($has_nul ? 1 : 0)) {
    $or_statement
      ->condition("{$this->table_alias}.{$this->real_field}", array_values($this->value), $this->operator);
  }
  $selected_val_count = count(array_values($this->value));
  $first_element = reset($this->value);

  // 'IN' Condition.
  if ($this->operator == 'in') {
    if ($selected_val_count == 1) {
      if ($first_element == 0) {
        $nul_operator = $has_nul == 0 ? 'IS NOT NULL' : 'IS NULL';
        $or_statement
          ->condition("{$this->table_alias}.{$this->real_field}", NULL, $nul_operator);
        $this->query
          ->add_where($this->options['group'], $or_statement);
      }
      elseif ($first_element != 0) {
        $operator = 'IN';
        $and_statement
          ->condition("{$this->table_alias}.{$this->real_field}", array_values($this->value), $operator);
        $this->query
          ->add_where($this->options['group'], $and_statement);
      }
    }
    elseif ($selected_val_count > 1) {
      if ($first_element == 0) {
        $nul_operator = $has_nul == 0 ? 'IS NOT NULL' : 'IS NULL';
        $or_statement
          ->condition("{$this->table_alias}.{$this->real_field}", NULL, $nul_operator);
        $this->query
          ->add_where($this->options['group'], $or_statement);
      }
      elseif ($first_element != 0) {
        $operator = 'IN';
        $and_statement
          ->condition("{$this->table_alias}.{$this->real_field}", array_values($this->value), $operator);
        $this->query
          ->add_where($this->options['group'], $and_statement);
      }
    }
  }
  elseif ($this->operator == 'not in') {
    if ($has_nul == 1 && $first_element == 0) {
      if ($selected_val_count == 1) {
        $nul_operator = 'IS NOT NULL';
        $and_statement
          ->condition("{$this->table_alias}.{$this->real_field}", array_values($this->value), $nul_operator);
        $this->query
          ->add_where($this->options['group'], $and_statement);
      }
      elseif ($selected_val_count > 1) {
        $operator = 'NOT IN';
        $nul_operator = 'IS NOT NULL';
        $and_statement
          ->condition("{$this->table_alias}.{$this->real_field}", array_values($this->value), $operator);
        $and_statement
          ->condition("{$this->table_alias}.{$this->real_field}", NULL, $nul_operator);
        $this->query
          ->add_where($this->options['group'], $and_statement);
      }
    }
    elseif ($has_nul == 0) {
      $nul_operator = 'IS NULL';
      $or_statement
        ->condition("{$this->table_alias}.{$this->real_field}", NULL, $nul_operator);
      $this->query
        ->add_where($this->options['group'], $or_statement);
    }
  }
}