0358054587671b445b23534f65554ab0d30e8f3e
[yaffs-website] / src / Plugin / views / filter / Date.php
1 <?php
2
3 namespace Drupal\datetime\Plugin\views\filter;
4
5 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
8 use Drupal\views\FieldAPIHandlerTrait;
9 use Drupal\views\Plugin\views\filter\Date as NumericDate;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\RequestStack;
12
13 /**
14  * Date/time views filter.
15  *
16  * Even thought dates are stored as strings, the numeric filter is extended
17  * because it provides more sensible operators.
18  *
19  * @ingroup views_filter_handlers
20  *
21  * @ViewsFilter("datetime")
22  */
23 class Date extends NumericDate implements ContainerFactoryPluginInterface {
24
25   use FieldAPIHandlerTrait;
26
27   /**
28    * The date formatter service.
29    *
30    * @var \Drupal\Core\Datetime\DateFormatterInterface
31    */
32   protected $dateFormatter;
33
34   /**
35    * Date format for SQL conversion.
36    *
37    * @var string
38    *
39    * @see \Drupal\views\Plugin\views\query\Sql::getDateFormat()
40    */
41   protected $dateFormat = DATETIME_DATETIME_STORAGE_FORMAT;
42
43   /**
44    * The request stack used to determin current time.
45    *
46    * @var \Symfony\Component\HttpFoundation\RequestStack
47    */
48   protected $requestStack;
49
50   /**
51    * Constructs a new Date handler.
52    *
53    * @param array $configuration
54    *   A configuration array containing information about the plugin instance.
55    * @param string $plugin_id
56    *   The plugin ID for the plugin instance.
57    * @param mixed $plugin_definition
58    *   The plugin implementation definition.
59    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
60    *   The date formatter service.
61    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
62    *   The request stack used to determine the current time.
63    */
64   public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, RequestStack $request_stack) {
65     parent::__construct($configuration, $plugin_id, $plugin_definition);
66     $this->dateFormatter = $date_formatter;
67     $this->requestStack = $request_stack;
68
69     // Date format depends on field storage format.
70     $definition = $this->getFieldStorageDefinition();
71     if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
72       $this->dateFormat = DATETIME_DATE_STORAGE_FORMAT;
73     }
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
80     return new static(
81       $configuration,
82       $plugin_id,
83       $plugin_definition,
84       $container->get('date.formatter'),
85       $container->get('request_stack')
86     );
87   }
88
89   /**
90    * Override parent method, which deals with dates as integers.
91    */
92   protected function opBetween($field) {
93     $origin = ($this->value['type'] == 'offset') ? $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') : 0;
94     $a = intval(strtotime($this->value['min'], $origin));
95     $b = intval(strtotime($this->value['max'], $origin));
96
97     // Formatting will vary on date storage.
98
99     // Convert to ISO format and format for query. UTC timezone is used since
100     // dates are stored in UTC.
101     $a = $this->query->getDateFormat("'" . $this->dateFormatter->format($a, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
102     $b = $this->query->getDateFormat("'" . $this->dateFormatter->format($b, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
103
104     // This is safe because we are manually scrubbing the values.
105     $operator = strtoupper($this->operator);
106     $field = $this->query->getDateFormat($field, $this->dateFormat, TRUE);
107     $this->query->addWhereExpression($this->options['group'], "$field $operator $a AND $b");
108   }
109
110   /**
111    * Override parent method, which deals with dates as integers.
112    */
113   protected function opSimple($field) {
114     $origin = (!empty($this->value['type']) && $this->value['type'] == 'offset') ? $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') : 0;
115     $value = intval(strtotime($this->value['value'], $origin));
116
117     // Convert to ISO. UTC is used since dates are stored in UTC.
118     $value = $this->query->getDateFormat("'" . $this->dateFormatter->format($value, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
119
120     // This is safe because we are manually scrubbing the value.
121     $field = $this->query->getDateFormat($field, $this->dateFormat, TRUE);
122     $this->query->addWhereExpression($this->options['group'], "$field $this->operator $value");
123   }
124
125 }