source: sipes/modules_contrib/views/handlers/views_handler_filter_in_operator.inc @ 59029b2

stableversion-3.0
Last change on this file since 59029b2 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: 14.1 KB
Línea 
1<?php
2/**
3 * Simple filter to handle matching of multiple options selectable via checkboxes
4 *
5 * Definition items:
6 * - numeric: If set to true, this item will use numeric operators instead of string.
7 * - options callback: The function to call in order to generate the value options. If omitted, the options 'Yes' and 'No' will be used.
8 * - options arguments: An array of arguments to pass to the options callback.
9 *
10 * @ingroup views_filter_handlers
11 */
12class views_handler_filter_in_operator extends views_handler_filter {
13  var $value_form_type = 'checkboxes';
14
15  /**
16   * @var array
17   * Stores all operations which are available on the form.
18   */
19  var $value_options = NULL;
20
21  function construct() {
22    parent::construct();
23    $this->value_title = t('Options');
24    $this->value_options = NULL;
25  }
26
27  /**
28   * Child classes should be used to override this function and set the
29   * 'value options', unless 'options callback' is defined as a valid function
30   * or static public method to generate these values.
31   *
32   * This can use a guard to be used to reduce database hits as much as
33   * possible.
34   *
35   * @return
36   *   Return the stored values in $this->value_options if someone expects it.
37   */
38  function get_value_options() {
39    if (isset($this->value_options)) {
40      return;
41    }
42
43    if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
44      if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
45        $this->value_options = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
46      }
47      else {
48        $this->value_options = call_user_func($this->definition['options callback']);
49      }
50    }
51    else {
52      $this->value_options = array(t('Yes'), t('No'));
53    }
54
55    return $this->value_options;
56  }
57
58  function expose_options() {
59    parent::expose_options();
60    $this->options['expose']['reduce'] = FALSE;
61  }
62
63  function expose_form_right(&$form, &$form_state) {
64    parent::expose_form_right($form, $form_state);
65    $form['expose']['reduce'] = array(
66      '#type' => 'checkbox',
67      '#title' => t('Limit list to selected items'),
68      '#description' => t('If checked, the only items presented to the user will be the ones selected here.'),
69      '#default_value' => !empty($this->options['expose']['reduce']), // safety
70    );
71  }
72
73  function option_definition() {
74    $options = parent::option_definition();
75
76    $options['operator']['default'] = 'in';
77    $options['value']['default'] = array();
78    $options['expose']['contains']['reduce'] = array('default' => FALSE);
79
80    return $options;
81  }
82
83  /**
84   * This kind of construct makes it relatively easy for a child class
85   * to add or remove functionality by overriding this function and
86   * adding/removing items from this array.
87   */
88  function operators() {
89    $operators = array(
90      'in' => array(
91        'title' => t('Is one of'),
92        'short' => t('in'),
93        'short_single' => t('='),
94        'method' => 'op_simple',
95        'values' => 1,
96      ),
97      'not in' => array(
98        'title' => t('Is not one of'),
99        'short' => t('not in'),
100        'short_single' => t('<>'),
101        'method' => 'op_simple',
102        'values' => 1,
103      ),
104    );
105    // if the definition allows for the empty operator, add it.
106    if (!empty($this->definition['allow empty'])) {
107      $operators += array(
108        'empty' => array(
109          'title' => t('Is empty (NULL)'),
110          'method' => 'op_empty',
111          'short' => t('empty'),
112          'values' => 0,
113        ),
114        'not empty' => array(
115          'title' => t('Is not empty (NOT NULL)'),
116          'method' => 'op_empty',
117          'short' => t('not empty'),
118          'values' => 0,
119        ),
120      );
121    }
122
123    return $operators;
124  }
125
126  /**
127   * Build strings from the operators() for 'select' options
128   */
129  function operator_options($which = 'title') {
130    $options = array();
131    foreach ($this->operators() as $id => $info) {
132      $options[$id] = $info[$which];
133    }
134
135    return $options;
136  }
137
138  function operator_values($values = 1) {
139    $options = array();
140    foreach ($this->operators() as $id => $info) {
141      if (isset($info['values']) && $info['values'] == $values) {
142        $options[] = $id;
143      }
144    }
145
146    return $options;
147  }
148
149  function value_form(&$form, &$form_state) {
150    $form['value'] = array();
151
152    $this->get_value_options();
153    $options = $this->value_options;
154    $default_value = (array) $this->value;
155
156    $which = 'all';
157    if (!empty($form['operator'])) {
158      $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
159    }
160    if (!empty($form_state['exposed'])) {
161      $identifier = $this->options['expose']['identifier'];
162
163      if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator'])) {
164        // exposed and locked.
165        $which = in_array($this->operator, $this->operator_values(1)) ? 'value' : 'none';
166      }
167      else {
168        $source = 'edit-' . form_clean_id($this->options['expose']['operator']);
169      }
170
171      if (!empty($this->options['expose']['reduce'])) {
172        $options = $this->reduce_value_options();
173
174        if (empty($this->options['expose']['single']) && !empty($this->options['expose']['optional'])) {
175          $default_value = array();
176        }
177      }
178
179      if (!empty($this->options['expose']['single'])) {
180        if (!empty($this->options['expose']['optional']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
181          $default_value = 'All';
182        }
183        else if (empty($default_value)) {
184          $keys = array_keys($options);
185          $default_value = array_shift($keys);
186        }
187        else {
188          $copy = $default_value;
189          $default_value = array_shift($copy);
190        }
191      }
192    }
193
194    if ($which == 'all' || $which == 'value') {
195      $form['value'] = array(
196        '#type' => $this->value_form_type,
197        '#title' => $this->value_title,
198        '#options' => $options,
199        '#default_value' => $default_value,
200        // These are only valid for 'select' type, but do no harm to checkboxes.
201        '#multiple' => TRUE,
202        '#size' => count($options) > 8 ? 8 : count($options),
203      );
204      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
205        $form_state['input'][$identifier] = $default_value;
206      }
207
208      $process = array();
209      if ($this->value_form_type == 'checkboxes') {
210        // If this form element will use checkboxes in the UI, we need to
211        // check_plain() all the options ourselves since FAPI is inconsistent
212        // about this. However, instead of directly doing that to the #options
213        // right now, we define a #process callback since we might change our
214        // mind later and convert this into a 'select' form element, which
215        // would lead to double-escaping the options.
216        $process[] = 'views_process_check_options';
217      }
218      if ($which == 'all') {
219        if (empty($form_state['exposed']) && ($this->value_form_type == 'checkboxes' || $this->value_form_type == 'radios')) {
220          $process[] = "expand_$this->value_form_type";
221          $form['value']['#prefix'] = '<div id="edit-options-value-wrapper">';
222          $form['value']['#suffix'] = '</div>';
223        }
224        $process[] = 'views_process_dependency';
225        $form['value']['#dependency'] = array($source => $this->operator_values(1));
226      }
227      if (!empty($process)) {
228        $form['value']['#process'] = $process;
229      }
230    }
231  }
232
233  /**
234   * When using exposed filters, we may be required to reduce the set.
235   */
236  function reduce_value_options($input = NULL) {
237    if (!isset($input)) {
238      $input = $this->value_options;
239    }
240
241    // Because options may be an array of strings, or an array of mixed arrays
242    // and strings (optgroups) or an array of objects, we have to
243    // step through and handle each one individually.
244    $options = array();
245    foreach ($input as $id => $option) {
246      if (is_array($option)) {
247        $options[$id] = $this->reduce_value_options($option);
248        continue;
249      }
250      else if (is_object($option)) {
251        $keys = array_keys($option->option);
252        $key = array_shift($keys);
253        if (isset($this->options['value'][$key])) {
254          $options[$id] = $option;
255        }
256      }
257      else if (isset($this->options['value'][$id])) {
258        $options[$id] = $option;
259      }
260    }
261    return $options;
262  }
263
264  function accept_exposed_input($input) {
265    // A very special override because the All state for this type of
266    // filter could have a default:
267    if (empty($this->options['exposed'])) {
268      return TRUE;
269    }
270
271    // If this is single and optional, this says that yes this filter will
272    // participate, but using the default settings, *if* 'limit is true.
273    if (!empty($this->options['expose']['single']) && !empty($this->options['expose']['optional']) && !empty($this->options['expose']['limit'])) {
274      $identifier = $this->options['expose']['identifier'];
275      if ($input[$identifier] == 'All') {
276        return TRUE;
277      }
278    }
279
280    return parent::accept_exposed_input($input);
281  }
282
283  function value_submit($form, &$form_state) {
284    // Drupal's FAPI system automatically puts '0' in for any checkbox that
285    // was not set, and the key to the checkbox if it is set.
286    // Unfortunately, this means that if the key to that checkbox is 0,
287    // we are unable to tell if that checkbox was set or not.
288
289    // Luckily, the '#value' on the checkboxes form actually contains
290    // *only* a list of checkboxes that were set, and we can use that
291    // instead.
292
293    $form_state['values']['options']['value'] = $form['value']['#value'];
294  }
295
296  function admin_summary() {
297    if (!empty($this->options['exposed'])) {
298      return t('exposed');
299    }
300    $info = $this->operators();
301
302    $this->get_value_options();
303
304    if (!is_array($this->value)) {
305      return;
306    }
307
308    $operator = check_plain($info[$this->operator]['short']);
309    $values = '';
310    if (in_array($this->operator, $this->operator_values(1))) {
311      // Remove every element which is not known.
312      foreach ($this->value as $value) {
313        if (!isset($this->value_options[$value])) {
314          unset($this->value[$value]);
315        }
316      }
317      // Choose different kind of ouput for 0, a single and multiple values.
318      if (count($this->value) == 0) {
319        $values = t('Unknown');
320      }
321      else if (count($this->value) == 1) {
322        // If any, use the 'single' short name of the operator instead.
323        if (isset($info[$this->operator]['short_single'])) {
324          $operator = check_plain($info[$this->operator]['short_single']);
325        }
326
327        $keys = $this->value;
328        $value = array_shift($keys);
329        if (isset($this->value_options[$value])) {
330          $values = check_plain($this->value_options[$value]);
331        }
332        else {
333          $values = '';
334        }
335      }
336      else {
337        foreach ($this->value as $value) {
338          if ($values !== '') {
339            $values .= ', ';
340          }
341          if (strlen($values) > 8) {
342            $values .= '...';
343            break;
344          }
345          if (isset($this->value_options[$value])) {
346            $values .= check_plain($this->value_options[$value]);
347          }
348        }
349      }
350    }
351
352    return $operator . (($values !== '') ? ' ' . $values : '');
353  }
354
355  function query() {
356    $info = $this->operators();
357    if (!empty($info[$this->operator]['method'])) {
358      $this->{$info[$this->operator]['method']}();
359    }
360  }
361
362  function op_simple() {
363    if (empty($this->value)) {
364      return;
365    }
366    $this->ensure_my_table();
367    $placeholder = !empty($this->definition['numeric']) ? '%d' : "'%s'";
368
369    $replace = array_fill(0, sizeof($this->value), $placeholder);
370    $in = ' (' . implode(", ", $replace) . ')';
371
372    // We use array_values() because the checkboxes keep keys and that can cause
373    // array addition problems.
374    $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field " . $this->operator . $in, array_values($this->value));
375  }
376
377  function op_empty() {
378    $this->ensure_my_table();
379    $field = "$this->table_alias.$this->real_field";
380
381    if ($this->operator == 'empty') {
382      $operator = "IS NULL";
383    }
384    else {
385      $operator = "IS NOT NULL";
386    }
387
388    $this->query->add_where($this->options['group'], "$field $operator");
389  }
390
391  function validate() {
392    $this->get_value_options();
393    $errors = array();
394
395    // If the operator is an operator which doesn't require a value, there is
396    // no need for additional validation.
397    if (in_array($this->operator, $this->operator_values(0))) {
398      return array();
399    }
400
401    if (!in_array($this->operator, $this->operator_values(1))) {
402      $errors[] = t('The operator is invalid on filter: @filter.', array('@filter' => $this->ui_name(TRUE)));
403    }
404    if (is_array($this->value)) {
405      if (!isset($this->value_options)) {
406        // Don't validate if there are none value options provided, for example for special handlers.
407        return $errors;
408      }
409      if ($this->options['exposed'] && !$this->options['expose']['required'] && empty($this->value)) {
410        // Don't validate if the field is exposed and no default value is provided.
411        return $errors;
412      }
413
414      // Some filter_in_operator usage uses optgroups forms, so flatten it.
415      $flat_options = form_options_flatten($this->value_options, TRUE);
416
417      // Remove every element which is not known.
418      foreach ($this->value as $value) {
419        if (!isset($flat_options[$value])) {
420          unset($this->value[$value]);
421        }
422      }
423      // Choose different kind of ouput for 0, a single and multiple values.
424      if (count($this->value) == 0) {
425        $errors[] = t('No valid values found on filter: @filter.', array('@filter' => $this->ui_name(TRUE)));
426      }
427    }
428    elseif (!empty($this->value) && ($this->operator == 'in' || $this->operator == 'not in')) {
429      $errors[] = t('The value @value is not an array for @operator on filter: @filter', array('@value' => views_var_export($this->value), '@operator' => $this->operator, '@filter' => $this->ui_name(TRUE)));
430    }
431    return $errors;
432  }
433}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.