You are here

public function SearchApiViewsHandlerArgumentDate::query in Search API 7

Set up the query for this argument.

The argument sent may be found at $this->argument.

Overrides SearchApiViewsHandlerArgument::query

File

contrib/search_api_views/includes/handler_argument_date.inc, line 16
Contains the SearchApiViewsHandlerArgumentDate class.

Class

SearchApiViewsHandlerArgumentDate
Defines a contextual filter searching for a date or date range.

Code

public function query($group_by = FALSE) {
  if (empty($this->value)) {
    $this
      ->fillValue();
    if ($this->value === FALSE) {
      $this
        ->abort();
      return;
    }
  }
  $outer_conjunction = strtoupper($this->operator);
  if (empty($this->options['not'])) {
    $operator = '=';
    $inner_conjunction = 'OR';
  }
  else {
    $operator = '<>';
    $inner_conjunction = 'AND';
  }
  if (!empty($this->value)) {
    if (!empty($this->value)) {
      $outer_filter = $this->query
        ->createFilter($outer_conjunction);
      foreach ($this->value as $value) {
        $value_filter = $this->query
          ->createFilter($inner_conjunction);
        $values = explode(';', $value);
        $values = array_map(array(
          $this,
          'getTimestamp',
        ), $values);
        if (in_array(FALSE, $values, TRUE)) {
          $this
            ->abort();
          return;
        }
        $is_range = count($values) > 1;
        $inner_filter = $is_range ? $this->query
          ->createFilter('AND') : $value_filter;
        $range_op = empty($this->options['not']) ? '>=' : '<';
        $inner_filter
          ->condition($this->real_field, $values[0], $is_range ? $range_op : $operator);
        if ($is_range) {
          $range_op = empty($this->options['not']) ? '<=' : '>';
          $inner_filter
            ->condition($this->real_field, $values[1], $range_op);
          $value_filter
            ->filter($inner_filter);
        }
        $outer_filter
          ->filter($value_filter);
      }
      $this->query
        ->filter($outer_filter);
    }
  }
}