Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Plugin / views / cache / Time.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\cache;
4
5 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Cache\Cache;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\HttpFoundation\Request;
10
11 /**
12  * Simple caching of query results for Views displays.
13  *
14  * @ingroup views_cache_plugins
15  *
16  * @ViewsCache(
17  *   id = "time",
18  *   title = @Translation("Time-based"),
19  *   help = @Translation("Simple time-based caching of data.")
20  * )
21  */
22 class Time extends CachePluginBase {
23
24   /**
25    * {@inheritdoc}
26    */
27   protected $usesOptions = TRUE;
28
29   /**
30    * The date formatter service.
31    *
32    * @var \Drupal\Core\Datetime\DateFormatterInterface
33    */
34   protected $dateFormatter;
35
36   /**
37    * The current request.
38    *
39    * @var \Symfony\Component\HttpFoundation\Request
40    */
41   protected $request;
42
43   /**
44    * Constructs a Time cache plugin object.
45    *
46    * @param array $configuration
47    *   A configuration array containing information about the plugin instance.
48    * @param string $plugin_id
49    *   The plugin_id for the plugin instance.
50    * @param mixed $plugin_definition
51    *   The plugin implementation definition.
52    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
53    *   The date formatter service.
54    * @param \Symfony\Component\HttpFoundation\Request $request
55    *   The current request.
56    */
57   public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, Request $request) {
58     $this->dateFormatter = $date_formatter;
59     $this->request = $request;
60
61     parent::__construct($configuration, $plugin_id, $plugin_definition);
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
68     return new static(
69       $configuration,
70       $plugin_id,
71       $plugin_definition,
72       $container->get('date.formatter'),
73       $container->get('request_stack')->getCurrentRequest()
74     );
75   }
76
77   protected function defineOptions() {
78     $options = parent::defineOptions();
79     $options['results_lifespan'] = ['default' => 3600];
80     $options['results_lifespan_custom'] = ['default' => 0];
81     $options['output_lifespan'] = ['default' => 3600];
82     $options['output_lifespan_custom'] = ['default' => 0];
83
84     return $options;
85   }
86
87   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
88     parent::buildOptionsForm($form, $form_state);
89     $options = [60, 300, 1800, 3600, 21600, 518400];
90     $options = array_map([$this->dateFormatter, 'formatInterval'], array_combine($options, $options));
91     $options = [0 => $this->t('Never cache')] + $options + ['custom' => $this->t('Custom')];
92
93     $form['results_lifespan'] = [
94       '#type' => 'select',
95       '#title' => $this->t('Query results'),
96       '#description' => $this->t('The length of time raw query results should be cached.'),
97       '#options' => $options,
98       '#default_value' => $this->options['results_lifespan'],
99     ];
100     $form['results_lifespan_custom'] = [
101       '#type' => 'textfield',
102       '#title' => $this->t('Seconds'),
103       '#size' => '25',
104       '#maxlength' => '30',
105       '#description' => $this->t('Length of time in seconds raw query results should be cached.'),
106       '#default_value' => $this->options['results_lifespan_custom'],
107       '#states' => [
108         'visible' => [
109           ':input[name="cache_options[results_lifespan]"]' => ['value' => 'custom'],
110         ],
111       ],
112     ];
113     $form['output_lifespan'] = [
114       '#type' => 'select',
115       '#title' => $this->t('Rendered output'),
116       '#description' => $this->t('The length of time rendered HTML output should be cached.'),
117       '#options' => $options,
118       '#default_value' => $this->options['output_lifespan'],
119     ];
120     $form['output_lifespan_custom'] = [
121       '#type' => 'textfield',
122       '#title' => $this->t('Seconds'),
123       '#size' => '25',
124       '#maxlength' => '30',
125       '#description' => $this->t('Length of time in seconds rendered HTML output should be cached.'),
126       '#default_value' => $this->options['output_lifespan_custom'],
127       '#states' => [
128         'visible' => [
129           ':input[name="cache_options[output_lifespan]"]' => ['value' => 'custom'],
130         ],
131       ],
132     ];
133   }
134
135   public function validateOptionsForm(&$form, FormStateInterface $form_state) {
136     $custom_fields = ['output_lifespan', 'results_lifespan'];
137     foreach ($custom_fields as $field) {
138       $cache_options = $form_state->getValue('cache_options');
139       if ($cache_options[$field] == 'custom' && !is_numeric($cache_options[$field . '_custom'])) {
140         $form_state->setError($form[$field . '_custom'], $this->t('Custom time values must be numeric.'));
141       }
142     }
143   }
144
145   public function summaryTitle() {
146     $results_lifespan = $this->getLifespan('results');
147     $output_lifespan = $this->getLifespan('output');
148     return $this->dateFormatter->formatInterval($results_lifespan, 1) . '/' . $this->dateFormatter->formatInterval($output_lifespan, 1);
149   }
150
151   protected function getLifespan($type) {
152     $lifespan = $this->options[$type . '_lifespan'] == 'custom' ? $this->options[$type . '_lifespan_custom'] : $this->options[$type . '_lifespan'];
153     return $lifespan;
154   }
155
156   protected function cacheExpire($type) {
157     $lifespan = $this->getLifespan($type);
158     if ($lifespan) {
159       $cutoff = REQUEST_TIME - $lifespan;
160       return $cutoff;
161     }
162     else {
163       return FALSE;
164     }
165   }
166
167   /**
168    * {@inheritdoc}
169    */
170   protected function cacheSetMaxAge($type) {
171     $lifespan = $this->getLifespan($type);
172     if ($lifespan) {
173       return $lifespan;
174     }
175     else {
176       return Cache::PERMANENT;
177     }
178   }
179
180   /**
181    * {@inheritdoc}
182    */
183   protected function getDefaultCacheMaxAge() {
184     // The max age, unless overridden by some other piece of the rendered code
185     // is determined by the output time setting.
186     return (int) $this->cacheSetMaxAge('output');
187   }
188
189 }