92a57991ca7bfc7aa6c491542a61efeb1972b20f
[yaffs-website] / web / core / modules / history / src / Plugin / views / filter / HistoryUserTimestamp.php
1 <?php
2
3 namespace Drupal\history\Plugin\views\filter;
4
5 use Drupal\Core\Cache\UncacheableDependencyTrait;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Plugin\views\filter\FilterPluginBase;
8
9 /**
10  * Filter for new content.
11  *
12  * The handler is named history_user, because of compatibility reasons, the
13  * table is history.
14  *
15  * @ingroup views_filter_handlers
16  *
17  * @ViewsFilter("history_user_timestamp")
18  */
19 class HistoryUserTimestamp extends FilterPluginBase {
20
21   use UncacheableDependencyTrait;
22
23   // Don't display empty space where the operator would be.
24   public $no_operator = TRUE;
25
26   /**
27    * {@inheritdoc}
28    */
29   public function usesGroupBy() {
30     return FALSE;
31   }
32
33   public function buildExposeForm(&$form, FormStateInterface $form_state) {
34     parent::buildExposeForm($form, $form_state);
35     // @todo There are better ways of excluding required and multiple (object flags)
36     unset($form['expose']['required']);
37     unset($form['expose']['multiple']);
38     unset($form['expose']['remember']);
39   }
40
41   protected function valueForm(&$form, FormStateInterface $form_state) {
42     // Only present a checkbox for the exposed filter itself. There's no way
43     // to tell the difference between not checked and the default value, so
44     // specifying the default value via the views UI is meaningless.
45     if ($form_state->get('exposed')) {
46       if (isset($this->options['expose']['label'])) {
47         $label = $this->options['expose']['label'];
48       }
49       else {
50         $label = $this->t('Has new content');
51       }
52       $form['value'] = [
53         '#type' => 'checkbox',
54         '#title' => $label,
55         '#default_value' => $this->value,
56       ];
57     }
58   }
59
60   public function query() {
61     // This can only work if we're authenticated in.
62     if (!\Drupal::currentUser()->isAuthenticated()) {
63       return;
64     }
65
66     // Don't filter if we're exposed and the checkbox isn't selected.
67     if ((!empty($this->options['exposed'])) && empty($this->value)) {
68       return;
69     }
70
71     // Hey, Drupal kills old history, so nodes that haven't been updated
72     // since HISTORY_READ_LIMIT are bzzzzzzzt outta here!
73
74     $limit = REQUEST_TIME - HISTORY_READ_LIMIT;
75
76     $this->ensureMyTable();
77     $field = "$this->tableAlias.$this->realField";
78     $node = $this->query->ensureTable('node_field_data', $this->relationship);
79
80     $clause = '';
81     $clause2 = '';
82     if ($ces = $this->query->ensureTable('comment_entity_statistics', $this->relationship)) {
83       $clause = ("OR $ces.last_comment_timestamp > (***CURRENT_TIME*** - $limit)");
84       $clause2 = "OR $field < $ces.last_comment_timestamp";
85     }
86
87     // NULL means a history record doesn't exist. That's clearly new content.
88     // Unless it's very very old content. Everything in the query is already
89     // type safe cause none of it is coming from outside here.
90     $this->query->addWhereExpression($this->options['group'], "($field IS NULL AND ($node.changed > (***CURRENT_TIME*** - $limit) $clause)) OR $field < $node.changed $clause2");
91   }
92
93   public function adminSummary() {
94     if (!empty($this->options['exposed'])) {
95       return $this->t('exposed');
96     }
97   }
98
99 }