You are here

public function SearchApiDate::query in Search API 8

Set up the query for this argument.

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

Overrides SearchApiStandard::query

File

src/Plugin/views/argument/SearchApiDate.php, line 63

Class

SearchApiDate
Defines a contextual filter for conditions on date fields.

Namespace

Drupal\search_api\Plugin\views\argument

Code

public function query($group_by = FALSE) {
  $this
    ->fillValue();
  if ($this->value === FALSE) {
    $this
      ->abort();
    return;
  }
  $outer_conjunction = strtoupper($this->operator);
  if (empty($this->options['not'])) {
    $condition_operator = '=';
    $inner_conjunction = 'OR';
  }
  else {
    $condition_operator = '<>';
    $inner_conjunction = 'AND';
  }
  if (!empty($this->value)) {
    $outer_conditions = $this->query
      ->createConditionGroup($outer_conjunction);

    // @todo Refactor to use only a single nested filter, and only if
    //   necessary. $value_conditions will currently only ever contain a
    //   single child – a condition or a nested filter with two conditions.
    foreach ($this->value as $value) {
      $value_conditions = $this->query
        ->createConditionGroup($inner_conjunction);
      $values = explode(';', $value);
      $values = array_map([
        $this,
        'getTimestamp',
      ], $values);
      if (in_array(FALSE, $values, TRUE)) {
        $this
          ->abort();
        return;
      }
      $is_range = count($values) > 1;
      $inner_conditions = $is_range ? $this->query
        ->createConditionGroup('AND') : $value_conditions;
      $range_op = empty($this->options['not']) ? '>=' : '<';
      $inner_conditions
        ->addCondition($this->realField, $values[0], $is_range ? $range_op : $condition_operator);
      if ($is_range) {
        $range_op = empty($this->options['not']) ? '<=' : '>';
        $inner_conditions
          ->addCondition($this->realField, $values[1], $range_op);
        $value_conditions
          ->addConditionGroup($inner_conditions);
      }
      $outer_conditions
        ->addConditionGroup($value_conditions);
    }
    $this->query
      ->addConditionGroup($outer_conditions);
  }
}