Projet

Général

Profil

Paste
Télécharger (9,48 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter_string.inc @ 6f57d8c7

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_filter_string.
6
 */
7

    
8
/**
9
 * Basic textfield filter to handle string filtering commands
10
 * including equality, like, not like, etc.
11
 *
12
 * @ingroup views_filter_handlers
13
 */
14
class views_handler_filter_string extends views_handler_filter {
15
  // exposed filter options
16
  var $always_multiple = TRUE;
17

    
18
  function option_definition() {
19
    $options = parent::option_definition();
20

    
21
    $options['expose']['contains']['required'] = array('default' => FALSE, 'bool' => TRUE);
22

    
23
    return $options;
24
  }
25

    
26
  /**
27
   * This kind of construct makes it relatively easy for a child class
28
   * to add or remove functionality by overriding this function and
29
   * adding/removing items from this array.
30
   */
31
  function operators() {
32
    $operators = array(
33
      '=' => array(
34
        'title' => t('Is equal to'),
35
        'short' => t('='),
36
        'method' => 'op_equal',
37
        'values' => 1,
38
      ),
39
      '!=' => array(
40
        'title' => t('Is not equal to'),
41
        'short' => t('!='),
42
        'method' => 'op_equal',
43
        'values' => 1,
44
      ),
45
      'contains' => array(
46
        'title' => t('Contains'),
47
        'short' => t('contains'),
48
        'method' => 'op_contains',
49
        'values' => 1,
50
      ),
51
      'word' => array(
52
        'title' => t('Contains any word'),
53
        'short' => t('has word'),
54
        'method' => 'op_word',
55
        'values' => 1,
56
      ),
57
      'allwords' => array(
58
        'title' => t('Contains all words'),
59
        'short' => t('has all'),
60
        'method' => 'op_word',
61
        'values' => 1,
62
      ),
63
      'starts' => array(
64
        'title' => t('Starts with'),
65
        'short' => t('begins'),
66
        'method' => 'op_starts',
67
        'values' => 1,
68
      ),
69
      'not_starts' => array(
70
        'title' => t('Does not start with'),
71
        'short' => t('not_begins'),
72
        'method' => 'op_not_starts',
73
        'values' => 1,
74
      ),
75
      'ends' => array(
76
        'title' => t('Ends with'),
77
        'short' => t('ends'),
78
        'method' => 'op_ends',
79
        'values' => 1,
80
      ),
81
      'not_ends' => array(
82
        'title' => t('Does not end with'),
83
        'short' => t('not_ends'),
84
        'method' => 'op_not_ends',
85
        'values' => 1,
86
      ),
87
      'not' => array(
88
        'title' => t('Does not contain'),
89
        'short' => t('!has'),
90
        'method' => 'op_not',
91
        'values' => 1,
92
      ),
93
      'shorterthan' => array(
94
        'title' => t('Length is shorter than'),
95
        'short' => t('shorter than'),
96
        'method' => 'op_shorter',
97
        'values' => 1,
98
      ),
99
      'longerthan' => array(
100
        'title' => t('Length is longer than'),
101
        'short' => t('longer than'),
102
        'method' => 'op_longer',
103
        'values' => 1,
104
      ),
105
    );
106
    // if the definition allows for the empty operator, add it.
107
    if (!empty($this->definition['allow empty'])) {
108
      $operators += array(
109
        'empty' => array(
110
          'title' => t('Is empty (NULL)'),
111
          'method' => 'op_empty',
112
          'short' => t('empty'),
113
          'values' => 0,
114
        ),
115
        'not empty' => array(
116
          'title' => t('Is not empty (NOT NULL)'),
117
          'method' => 'op_empty',
118
          'short' => t('not empty'),
119
          'values' => 0,
120
        ),
121
      );
122
    }
123
    // Add regexp support for MySQL.
124
    if (Database::getConnection()->databaseType() == 'mysql') {
125
      $operators += array(
126
        'regular_expression' => array(
127
          'title' => t('Regular expression'),
128
          'short' => t('regex'),
129
          'method' => 'op_regex',
130
          'values' => 1,
131
        ),
132
      );
133
    }
134

    
135
    return $operators;
136
  }
137

    
138
  /**
139
   * Build strings from the operators() for 'select' options
140
   */
141
  function operator_options($which = 'title') {
142
    $options = array();
143
    foreach ($this->operators() as $id => $info) {
144
      $options[$id] = $info[$which];
145
    }
146

    
147
    return $options;
148
  }
149

    
150
  function admin_summary() {
151
    if ($this->is_a_group()) {
152
      return t('grouped');
153
    }
154
    if (!empty($this->options['exposed'])) {
155
      return t('exposed');
156
    }
157

    
158
    $options = $this->operator_options('short');
159
    $output = '';
160
    if(!empty($options[$this->operator])) {
161
      $output = check_plain($options[$this->operator]);
162
    }
163
    if (in_array($this->operator, $this->operator_values(1))) {
164
      $output .= ' ' . check_plain($this->value);
165
    }
166
    return $output;
167
  }
168

    
169
  function operator_values($values = 1) {
170
    $options = array();
171
    foreach ($this->operators() as $id => $info) {
172
      if (isset($info['values']) && $info['values'] == $values) {
173
        $options[] = $id;
174
      }
175
    }
176

    
177
    return $options;
178
  }
179

    
180
  /**
181
   * Provide a simple textfield for equality
182
   */
183
  function value_form(&$form, &$form_state) {
184
    // We have to make some choices when creating this as an exposed
185
    // filter form. For example, if the operator is locked and thus
186
    // not rendered, we can't render dependencies; instead we only
187
    // render the form items we need.
188
    $which = 'all';
189
    if (!empty($form['operator'])) {
190
      $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
191
    }
192
    if (!empty($form_state['exposed'])) {
193
      $identifier = $this->options['expose']['identifier'];
194

    
195
      if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
196
        // exposed and locked.
197
        $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
198
      }
199
      else {
200
        $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
201
      }
202
    }
203

    
204
    if ($which == 'all' || $which == 'value') {
205
      $form['value'] = array(
206
        '#type' => 'textfield',
207
        '#title' => t('Value'),
208
        '#size' => 30,
209
        '#default_value' => $this->value,
210
      );
211
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
212
        $form_state['input'][$identifier] = $this->value;
213
      }
214

    
215
      if ($which == 'all') {
216
        $form['value'] += array(
217
          '#dependency' => array($source => $this->operator_values(1)),
218
        );
219
      }
220
    }
221

    
222
    if (!isset($form['value'])) {
223
      // Ensure there is something in the 'value'.
224
      $form['value'] = array(
225
        '#type' => 'value',
226
        '#value' => NULL
227
      );
228
    }
229
  }
230

    
231
  function operator() {
232
    return $this->operator == '=' ? 'LIKE' : 'NOT LIKE';
233
  }
234

    
235
  /**
236
   * Add this filter to the query.
237
   *
238
   * Due to the nature of fapi, the value and the operator have an unintended
239
   * level of indirection. You will find them in $this->operator
240
   * and $this->value respectively.
241
   */
242
  function query() {
243
    $this->ensure_my_table();
244
    $field = "$this->table_alias.$this->real_field";
245

    
246
    $info = $this->operators();
247
    if (!empty($info[$this->operator]['method'])) {
248
      $this->{$info[$this->operator]['method']}($field);
249
    }
250
  }
251

    
252
  function op_equal($field) {
253
    $this->query->add_where($this->options['group'], $field, $this->value, $this->operator());
254
  }
255

    
256
  function op_contains($field) {
257
    $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'LIKE');
258
  }
259

    
260
  function op_word($field) {
261
    $where = $this->operator == 'word' ? db_or() : db_and();
262

    
263
    // Don't filter on empty strings.
264
    if (empty($this->value)) {
265
      return;
266
    }
267

    
268
    preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
269
    foreach ($matches as $match) {
270
      $phrase = false;
271
      // Strip off phrase quotes
272
      if ($match[2]{0} == '"') {
273
        $match[2] = substr($match[2], 1, -1);
274
        $phrase = true;
275
      }
276
      $words = trim($match[2], ',?!();:-');
277
      $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
278
      foreach ($words as $word) {
279
        $placeholder = $this->placeholder();
280
        $where->condition($field, '%' . db_like(trim($word, " ,!?")) . '%', 'LIKE');
281
      }
282
    }
283

    
284
    if (!$where) {
285
      return;
286
    }
287

    
288
    // previously this was a call_user_func_array but that's unnecessary
289
    // as views will unpack an array that is a single arg.
290
    $this->query->add_where($this->options['group'], $where);
291
  }
292

    
293
  function op_starts($field) {
294
    $this->query->add_where($this->options['group'], $field, db_like($this->value) . '%', 'LIKE');
295
  }
296

    
297
  function op_not_starts($field) {
298
    $this->query->add_where($this->options['group'], $field, db_like($this->value) . '%', 'NOT LIKE');
299
  }
300

    
301
  function op_ends($field) {
302
    $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value), 'LIKE');
303
  }
304

    
305
  function op_not_ends($field) {
306
    $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value), 'NOT LIKE');
307
  }
308

    
309
  function op_not($field) {
310
    $this->query->add_where($this->options['group'], $field, '%' . db_like($this->value) . '%', 'NOT LIKE');
311
  }
312

    
313
  function op_shorter($field) {
314
    $placeholder = $this->placeholder();
315
    $this->query->add_where_expression($this->options['group'], "LENGTH($field) < $placeholder", array($placeholder => $this->value));
316
  }
317

    
318
  function op_longer($field) {
319
    $placeholder = $this->placeholder();
320
    $this->query->add_where_expression($this->options['group'], "LENGTH($field) > $placeholder", array($placeholder => $this->value));
321
  }
322

    
323
  function op_regex($field) {
324
    $this->query->add_where($this->options['group'], $field, $this->value, 'RLIKE');
325
  }
326

    
327
  function op_empty($field) {
328
    if ($this->operator == 'empty') {
329
      $operator = "IS NULL";
330
    }
331
    else {
332
      $operator = "IS NOT NULL";
333
    }
334

    
335
    $this->query->add_where($this->options['group'], $field, NULL, $operator);
336
  }
337

    
338
}