Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / src / Plugin / views / filter / Combine.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\filter;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Database\Database;
7
8 /**
9  * Filter handler which allows to search on multiple fields.
10  *
11  * @ingroup views_field_handlers
12  *
13  * @ViewsFilter("combine")
14  */
15 class Combine extends StringFilter {
16
17   /**
18    * @var views_plugin_query_default
19    */
20   public $query;
21
22   protected function defineOptions() {
23     $options = parent::defineOptions();
24     $options['fields'] = ['default' => []];
25
26     return $options;
27   }
28
29   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
30     parent::buildOptionsForm($form, $form_state);
31     $this->view->initStyle();
32
33     // Allow to choose all fields as possible
34     if ($this->view->style_plugin->usesFields()) {
35       $options = [];
36       foreach ($this->view->display_handler->getHandlers('field') as $name => $field) {
37         // Only allow clickSortable fields. Fields without clickSorting will
38         // probably break in the Combine filter.
39         if ($field->clickSortable()) {
40           $options[$name] = $field->adminLabel(TRUE);
41         }
42       }
43       if ($options) {
44         $form['fields'] = [
45           '#type' => 'select',
46           '#title' => $this->t('Choose fields to combine for filtering'),
47           '#description' => $this->t("This filter doesn't work for very special field handlers."),
48           '#multiple' => TRUE,
49           '#options' => $options,
50           '#default_value' => $this->options['fields'],
51         ];
52       }
53       else {
54         $form_state->setErrorByName('', $this->t('You have to add some fields to be able to use this filter.'));
55       }
56     }
57   }
58
59   public function query() {
60     $this->view->_build('field');
61     $fields = [];
62     // Only add the fields if they have a proper field and table alias.
63     foreach ($this->options['fields'] as $id) {
64       // Overridden fields can lead to fields missing from a display that are
65       // still set in the non-overridden combined filter.
66       if (!isset($this->view->field[$id])) {
67         // If fields are no longer available that are needed to filter by, make
68         // sure no results are shown to prevent displaying more then intended.
69         $this->view->build_info['fail'] = TRUE;
70         continue;
71       }
72       $field = $this->view->field[$id];
73       // Always add the table of the selected fields to be sure a table alias exists.
74       $field->ensureMyTable();
75       if (!empty($field->field_alias) && !empty($field->field_alias)) {
76         $fields[] = "$field->tableAlias.$field->realField";
77       }
78     }
79     if ($fields) {
80       $count = count($fields);
81       $separated_fields = [];
82       foreach ($fields as $key => $field) {
83         $separated_fields[] = $field;
84         if ($key < $count - 1) {
85           $separated_fields[] = "' '";
86         }
87       }
88       $expression = implode(', ', $separated_fields);
89       $expression = "CONCAT_WS(' ', $expression)";
90
91       $info = $this->operators();
92       if (!empty($info[$this->operator]['method'])) {
93         $this->{$info[$this->operator]['method']}($expression);
94       }
95     }
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function validate() {
102     $errors = parent::validate();
103     if ($this->displayHandler->usesFields()) {
104       $fields = $this->displayHandler->getHandlers('field');
105       foreach ($this->options['fields'] as $id) {
106         if (!isset($fields[$id])) {
107           // Combined field filter only works with fields that are in the field
108           // settings.
109           $errors[] = $this->t('Field %field set in %filter is not set in display %display.', ['%field' => $id, '%filter' => $this->adminLabel(), '%display' => $this->displayHandler->display['display_title']]);
110           break;
111         }
112         elseif (!$fields[$id]->clickSortable()) {
113           // Combined field filter only works with simple fields. If the field
114           // is not click sortable we can assume it is not a simple field.
115           // @todo change this check to isComputed. See
116           // https://www.drupal.org/node/2349465
117           $errors[] = $this->t('Field %field set in %filter is not usable for this filter type. Combined field filter only works for simple fields.', ['%field' => $fields[$id]->adminLabel(), '%filter' => $this->adminLabel()]);
118         }
119       }
120     }
121     else {
122       $errors[] = $this->t('%display: %filter can only be used on displays that use fields. Set the style or row format for that display to one using fields to use the combine field filter.', ['%display' => $this->displayHandler->display['display_title'], '%filter' => $this->adminLabel()]);
123     }
124     return $errors;
125   }
126
127   /**
128    * By default things like opEqual uses add_where, that doesn't support
129    * complex expressions, so override opEqual (and all operators below).
130    */
131   public function opEqual($expression) {
132     $placeholder = $this->placeholder();
133     $operator = $this->operator();
134     $this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => $this->value]);
135   }
136
137   protected function opContains($expression) {
138     $placeholder = $this->placeholder();
139     $this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => '%' . db_like($this->value) . '%']);
140   }
141
142   /**
143    * Filters by one or more words.
144    *
145    * By default opContainsWord uses add_where, that doesn't support complex
146    * expressions.
147    *
148    * @param string $expression
149    */
150   protected function opContainsWord($expression) {
151     $placeholder = $this->placeholder();
152
153     // Don't filter on empty strings.
154     if (empty($this->value)) {
155       return;
156     }
157
158     // Match all words separated by spaces or sentences encapsulated by double
159     // quotes.
160     preg_match_all(static::WORDS_PATTERN, ' ' . $this->value, $matches, PREG_SET_ORDER);
161
162     // Switch between the 'word' and 'allwords' operator.
163     $type = $this->operator == 'word' ? 'OR' : 'AND';
164     $group = $this->query->setWhereGroup($type);
165     $operator = Database::getConnection()->mapConditionOperator('LIKE');
166     $operator = isset($operator['operator']) ? $operator['operator'] : 'LIKE';
167
168     foreach ($matches as $match_key => $match) {
169       $temp_placeholder = $placeholder . '_' . $match_key;
170       // Clean up the user input and remove the sentence delimiters.
171       $word = trim($match[2], ',?!();:-"');
172       $this->query->addWhereExpression($group, "$expression $operator $temp_placeholder", [$temp_placeholder => '%' . Database::getConnection()->escapeLike($word) . '%']);
173     }
174   }
175
176   protected function opStartsWith($expression) {
177     $placeholder = $this->placeholder();
178     $this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => db_like($this->value) . '%']);
179   }
180
181   protected function opNotStartsWith($expression) {
182     $placeholder = $this->placeholder();
183     $this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => db_like($this->value) . '%']);
184   }
185
186   protected function opEndsWith($expression) {
187     $placeholder = $this->placeholder();
188     $this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => '%' . db_like($this->value)]);
189   }
190
191   protected function opNotEndsWith($expression) {
192     $placeholder = $this->placeholder();
193     $this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => '%' . db_like($this->value)]);
194   }
195
196   protected function opNotLike($expression) {
197     $placeholder = $this->placeholder();
198     $this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => '%' . db_like($this->value) . '%']);
199   }
200
201   protected function opRegex($expression) {
202     $placeholder = $this->placeholder();
203     $this->query->addWhereExpression($this->options['group'], "$expression REGEXP $placeholder", [$placeholder => $this->value]);
204   }
205
206   protected function opEmpty($expression) {
207     if ($this->operator == 'empty') {
208       $operator = "IS NULL";
209     }
210     else {
211       $operator = "IS NOT NULL";
212     }
213
214     $this->query->addWhereExpression($this->options['group'], "$expression $operator");
215   }
216
217 }