source: sipes/modules_contrib/views/handlers/views_handler_filter_string.inc @ 65dadeb

stableversion-3.0
Last change on this file since 65dadeb was 59029b2, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se actualizo la version del modulo views

  • Propiedad mode establecida a 100644
File size: 9.3 KB
Línea 
1<?php
2
3/**
4 * Basic textfield filter to handle string filtering commands
5 * including equality, like, not like, etc.
6 *
7 * @ingroup views_filter_handlers
8 */
9class views_handler_filter_string extends views_handler_filter {
10  // exposed filter options
11  var $no_single = TRUE;
12
13  function option_definition() {
14    $options = parent::option_definition();
15
16    $options['expose']['contains']['optional'] = array('default' => TRUE);
17    $options['case'] = array('default' => TRUE);
18
19    return $options;
20  }
21
22  /**
23   * This kind of construct makes it relatively easy for a child class
24   * to add or remove functionality by overriding this function and
25   * adding/removing items from this array.
26   */
27  function operators() {
28    $operators = array(
29      '=' => array(
30        'title' => t('Is equal to'),
31        'short' => t('='),
32        'method' => 'op_equal',
33        'values' => 1,
34      ),
35      '!=' => array(
36        'title' => t('Is not equal to'),
37        'short' => t('!='),
38        'method' => 'op_equal',
39        'values' => 1,
40      ),
41      'contains' => array(
42        'title' => t('Contains'),
43        'short' => t('contains'),
44        'method' => 'op_contains',
45        'values' => 1,
46      ),
47      'word' => array(
48        'title' => t('Contains any word'),
49        'short' => t('has word'),
50        'method' => 'op_word',
51        'values' => 1,
52      ),
53      'allwords' => array(
54        'title' => t('Contains all words'),
55        'short' => t('has all'),
56        'method' => 'op_word',
57        'values' => 1,
58      ),
59      'starts' => array(
60        'title' => t('Starts with'),
61        'short' => t('begins'),
62        'method' => 'op_starts',
63        'values' => 1,
64      ),
65      'not_starts' => array(
66        'title' => t('Does not start with'),
67        'short' => t('not_begins'),
68        'method' => 'op_not_starts',
69        'values' => 1,
70      ),
71      'ends' => array(
72        'title' => t('Ends with'),
73        'short' => t('ends'),
74        'method' => 'op_ends',
75        'values' => 1,
76      ),
77      'not_ends' => array(
78        'title' => t('Does not end with'),
79        'short' => t('not_ends'),
80        'method' => 'op_not_ends',
81        'values' => 1,
82      ),
83      'not' => array(
84        'title' => t('Does not contain'),
85        'short' => t('!has'),
86        'method' => 'op_not',
87        'values' => 1,
88      ),
89      'shorterthan' => array(
90        'title' => t('Length is shorter than'),
91        'short' => t('shorter than'),
92        'method' => 'op_shorter',
93        'values' => 1,
94      ),
95      'longerthan' => array(
96        'title' => t('Length is longer than'),
97        'short' => t('longer than'),
98        'method' => 'op_longer',
99        'values' => 1,
100      ),
101    );
102    // if the definition allows for the empty operator, add it.
103    if (!empty($this->definition['allow empty'])) {
104      $operators += array(
105        'empty' => array(
106          'title' => t('Is empty (NULL)'),
107          'method' => 'op_empty',
108          'short' => t('empty'),
109          'values' => 0,
110        ),
111        'not empty' => array(
112          'title' => t('Is not empty (NOT NULL)'),
113          'method' => 'op_empty',
114          'short' => t('not empty'),
115          'values' => 0,
116        ),
117      );
118    }
119
120    return $operators;
121  }
122
123  /**
124   * Build strings from the operators() for 'select' options
125   */
126  function operator_options($which = 'title') {
127    $options = array();
128    foreach ($this->operators() as $id => $info) {
129      $options[$id] = $info[$which];
130    }
131
132    return $options;
133  }
134
135  function admin_summary() {
136    if (!empty($this->options['exposed'])) {
137      return t('exposed');
138    }
139
140    $options = $this->operator_options('short');
141    $output = check_plain($options[$this->operator]);
142    if (in_array($this->operator, $this->operator_values(1))) {
143      $output .= ' ' . check_plain($this->value);
144    }
145    return $output;
146  }
147
148  function options_form(&$form, &$form_state) {
149    parent::options_form($form, $form_state);
150    $form['case'] = array(
151      '#type' => 'checkbox',
152      '#title' => t('Case sensitive'),
153      '#default_value' => $this->options['case'],
154      '#description' => t('Case sensitive filters may be faster. MySQL might ignore case sensitivity.'),
155    );
156  }
157
158  function operator_values($values = 1) {
159    $options = array();
160    foreach ($this->operators() as $id => $info) {
161      if (isset($info['values']) && $info['values'] == $values) {
162        $options[] = $id;
163      }
164    }
165
166    return $options;
167  }
168
169  /**
170   * Provide a simple textfield for equality
171   */
172  function value_form(&$form, &$form_state) {
173    // We have to make some choices when creating this as an exposed
174    // filter form. For example, if the operator is locked and thus
175    // not rendered, we can't render dependencies; instead we only
176    // render the form items we need.
177    $which = 'all';
178    if (!empty($form['operator'])) {
179      $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
180    }
181    if (!empty($form_state['exposed'])) {
182      $identifier = $this->options['expose']['identifier'];
183
184      if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator'])) {
185        // exposed and locked.
186        $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
187      }
188      else {
189        $source = 'edit-' . form_clean_id($this->options['expose']['operator']);
190      }
191    }
192
193    if ($which == 'all' || $which == 'value') {
194      $form['value'] = array(
195        '#type' => 'textfield',
196        '#title' => t('Value'),
197        '#size' => 30,
198        '#default_value' => $this->value,
199      );
200      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
201        $form_state['input'][$identifier] = $this->value;
202      }
203
204      if ($which == 'all') {
205        $form['value'] += array(
206          '#process' => array('views_process_dependency'),
207          '#dependency' => array($source => $this->operator_values(1)),
208        );
209      }
210    }
211
212    if (!isset($form['value'])) {
213      // Ensure there is something in the 'value'.
214      $form['value'] = array(
215        '#type' => 'value',
216        '#value' => NULL
217      );
218    }
219  }
220
221  function case_transform() {
222    return !empty($this->options['case']) ? '' : 'UPPER';
223  }
224
225  /**
226   * Add this filter to the query.
227   *
228   * Due to the nature of fapi, the value and the operator have an unintended
229   * level of indirection. You will find them in $this->operator
230   * and $this->value respectively.
231   */
232  function query() {
233    $this->ensure_my_table();
234    $field = "$this->table_alias.$this->real_field";
235    $upper = $this->case_transform();
236
237    $info = $this->operators();
238    if (!empty($info[$this->operator]['method'])) {
239      $this->{$info[$this->operator]['method']}($field, $upper);
240    }
241  }
242
243  function op_equal($field, $upper) {
244    // operator is either = or !=
245    $this->query->add_where($this->options['group'], "$upper($field) $this->operator $upper('%s')", $this->value);
246  }
247
248  function op_contains($field, $upper) {
249    $this->query->add_where($this->options['group'], "$upper($field) LIKE $upper('%%%s%%')", $this->value);
250  }
251
252  function op_word($field, $upper) {
253    $where = array();
254    preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
255    foreach ($matches as $match) {
256      $phrase = false;
257      // Strip off phrase quotes
258      if ($match[2]{0} == '"') {
259        $match[2] = substr($match[2], 1, -1);
260        $phrase = true;
261      }
262      $words = trim($match[2], ',?!();:-');
263      $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
264      foreach ($words as $word) {
265        $where[] = "$upper($field) LIKE $upper('%%%s%%')";
266        $values[] = trim($word, " ,!?");
267      }
268    }
269
270    if (!$where) {
271      return;
272    }
273
274    if ($this->operator == 'word') {
275      $where = '(' . implode(' OR ', $where) . ')';
276    }
277    else {
278      $where = implode(' AND ', $where);
279    }
280    // previously this was a call_user_func_array but that's unnecessary
281    // as views will unpack an array that is a single arg.
282    $this->query->add_where($this->options['group'], $where, $values);
283  }
284
285  function op_starts($field, $upper) {
286    $this->query->add_where($this->options['group'], "$upper($field) LIKE $upper('%s%%')", $this->value);
287  }
288
289  function op_not_starts($field, $upper) {
290    $this->query->add_where($this->options['group'], "$upper($field) NOT LIKE $upper('%s%%')", $this->value);
291  }
292
293  function op_ends($field, $upper) {
294    $this->query->add_where($this->options['group'], "$upper($field) LIKE $upper('%%%s')", $this->value);
295  }
296
297  function op_not_ends($field, $upper) {
298    $this->query->add_where($this->options['group'], "$upper($field) NOT LIKE $upper('%%%s')", $this->value);
299  }
300
301  function op_not($field, $upper) {
302    $this->query->add_where($this->options['group'], "$upper($field) NOT LIKE $upper('%%%s%%')", $this->value);
303  }
304
305  function op_shorter($field, $upper) {
306    $this->query->add_where($this->options['group'], "LENGTH($upper($field)) < %d", $this->value);
307  }
308
309  function op_longer($field, $upper) {
310    $this->query->add_where($this->options['group'], "LENGTH($upper($field)) > %d", $this->value);
311  }
312
313  function op_empty($field) {
314    if ($this->operator == 'empty') {
315      $operator = "IS NULL";
316    }
317    else {
318      $operator = "IS NOT NULL";
319    }
320
321    $this->query->add_where($this->options['group'], "$field $operator");
322  }
323
324}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.