View source
<?php
namespace Drupal\datetime\Plugin\views\filter;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\views\FieldAPIHandlerTrait;
use Drupal\views\Plugin\views\filter\Date as NumericDate;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class Date extends NumericDate implements ContainerFactoryPluginInterface {
use FieldAPIHandlerTrait;
protected $dateFormatter;
protected $dateFormat = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
protected $calculateOffset = TRUE;
protected $requestStack;
public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, RequestStack $request_stack) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->dateFormatter = $date_formatter;
$this->requestStack = $request_stack;
$definition = $this
->getFieldStorageDefinition();
if ($definition
->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
$this->dateFormat = DateTimeItemInterface::DATE_STORAGE_FORMAT;
$this->calculateOffset = FALSE;
}
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('date.formatter'), $container
->get('request_stack'));
}
protected function opBetween($field) {
$timezone = $this
->getTimezone();
$origin_offset = $this
->getOffset($this->value['min'], $timezone);
$min = !empty($this->value['min']) ? $this->value['min'] : '@0';
$a = new DateTimePlus($min, new \DateTimeZone($timezone));
$a = $this->query
->getDateFormat($this->query
->getDateField("'" . $this->dateFormatter
->format($a
->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
$b = new DateTimePlus($this->value['max'], new \DateTimeZone($timezone));
$b = $this->query
->getDateFormat($this->query
->getDateField("'" . $this->dateFormatter
->format($b
->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
$operator = strtoupper($this->operator);
$field = $this->query
->getDateFormat($this->query
->getDateField($field, TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
$this->query
->addWhereExpression($this->options['group'], "{$field} {$operator} {$a} AND {$b}");
}
protected function opSimple($field) {
$timezone = $this
->getTimezone();
$origin_offset = $this
->getOffset($this->value['value'], $timezone);
$value = new DateTimePlus($this->value['value'], new \DateTimeZone($timezone));
$value = $this->query
->getDateFormat($this->query
->getDateField("'" . $this->dateFormatter
->format($value
->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
$field = $this->query
->getDateFormat($this->query
->getDateField($field, TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
$this->query
->addWhereExpression($this->options['group'], "{$field} {$this->operator} {$value}");
}
protected function getTimezone() {
return $this->dateFormat === DateTimeItemInterface::DATE_STORAGE_FORMAT ? DateTimeItemInterface::STORAGE_TIMEZONE : date_default_timezone_get();
}
protected function getOffset($time, $timezone) {
$origin_offset = 0;
if ($this->dateFormat === DateTimeItemInterface::DATE_STORAGE_FORMAT && $this->value['type'] === 'offset') {
$origin_offset = $origin_offset + timezone_offset_get(new \DateTimeZone(date_default_timezone_get()), new \DateTime($time, new \DateTimeZone($timezone)));
}
return $origin_offset;
}
}