You are here

protected function DatexViewsDate::opSimple in Datex 8

@inheritDoc

Overrides Date::opSimple

File

src/Plugin/views/filter/DatexViewsDate.php, line 31

Class

DatexViewsDate
Filter to handle dates stored as a timestamp.

Namespace

Drupal\datex\Plugin\views\filter

Code

protected function opSimple($field) {
  $this->value['value'] = DatexArgHandlerTrait::translate($this->value['value']);
  $cal = datex_factory();
  if (!$cal) {
    parent::opSimple($field);
    return;
  }
  if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
    $v = $this->value['value'];
    $ts = strtotime($v);
    if ($ts === FALSE) {
      $this->query
        ->addWhereExpression($this->options['group'], '1 = 2');
      return;
    }
    $cal
      ->setTimestamp($ts);
    if (strpos($v, 'second') !== FALSE) {
      $min = $ts;
      $max = $ts;
    }
    elseif (strpos($v, 'minute') != FALSE) {
      $cal
        ->setTime($cal
        ->format('H'), $cal
        ->format('i'), 0);
      $min = $cal
        ->getTimestamp();
      $cal
        ->setTime($cal
        ->format('H'), $cal
        ->format('i'), 59);
      $max = $cal
        ->getTimestamp();
    }
    elseif (strpos($v, 'hour') != FALSE) {
      $cal
        ->setTime($cal
        ->format('H'), 0, 0);
      $min = $cal
        ->getTimestamp();
      $cal
        ->setTime($cal
        ->format('H'), 59, 59);
      $max = $cal
        ->getTimestamp();
    }
    elseif (strpos($v, 'day') != FALSE) {
      $cal
        ->setTime(0, 0, 0);
      $min = $cal
        ->getTimestamp();
      $cal
        ->setTime(23, 59, 59);
      $max = $cal
        ->getTimestamp();
    }
    elseif (strpos($v, 'month') != FALSE) {
      $cal
        ->setTime(0, 0, 0);
      $cal
        ->setDateLocale($cal
        ->format('Y'), $cal
        ->format('m'), 1);
      $min = $cal
        ->getTimestamp();
      $cal
        ->setDateLocale($cal
        ->format('Y'), $cal
        ->format('m'), $cal
        ->format('t'));
      $max = $cal
        ->getTimestamp();
    }
    elseif (strpos($v, 'year') != FALSE) {
      $cal
        ->setTime(0, 0, 0);
      $cal
        ->setDateLocale($cal
        ->format('Y'), 0, 1);
      $min = $cal
        ->getTimestamp();

      // Set month to last month, then use t.
      $cal
        ->setDateLocale($cal
        ->format('Y'), 12, 1);
      $cal
        ->setDateLocale($cal
        ->format('Y'), 12, $cal
        ->format('t'));
      $max = $cal
        ->getTimestamp();
    }
    elseif (strpos($v, 'week' === FALSE)) {
      $this->query
        ->addWhereExpression($this->options['group'], '1 = 2');
      return;
    }
  }
  else {
    if (!$cal
      ->parse($this->value['value'] . ' 00:00:00', 'Y-m-d H:i:s')) {
      $this->query
        ->addWhereExpression($this->options['group'], '1 = 2');
      return;
    }
    $min = $cal
      ->getTimestamp();
    if (!$cal
      ->parse($this->value['value'] . ' 23:59:59', 'Y-m-d H:i:s')) {
      $this->query
        ->addWhereExpression($this->options['group'], '1 = 2');
      return;
    }
    $max = $cal
      ->getTimestamp();
  }
  switch ($this->operator) {
    case '=':
      $this->query
        ->addWhereExpression($this->options['group'], "{$field} BETWEEN {$min} AND {$max}");
      break;
    case '!=':
      $this->query
        ->addWhereExpression($this->options['group'], "{$field} < {$min} OR {$field} > {$max}");
      break;
    case '<':
      $this->query
        ->addWhereExpression($this->options['group'], "{$field} < {$min}");
      break;
    case '>':
      $this->query
        ->addWhereExpression($this->options['group'], "{$field} > {$max}");
      break;
    case '>=':
      $this->query
        ->addWhereExpression($this->options['group'], "{$field} >= {$min}");
      break;
    case '<=':
      $this->query
        ->addWhereExpression($this->options['group'], "{$field} <= {$max}");
      break;
    default:
      $this->query
        ->addWhereExpression($this->options['group'], '1 = 2');
      return;
  }
}