Project

General

Profile

Paste
Download (14 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter_in_operator.inc @ 4003efde

1
<?php
2

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

    
8
/**
9
 * Simple filter to handle matching of multiple options using checkboxes.
10
 *
11
 * Definition items:
12
 * - options callback: The function to call in order to generate the value
13
 *   options. If omitted, the options 'Yes' and 'No' will be used.
14
 * - options arguments: An array of arguments to pass to the options callback.
15
 *
16
 * @ingroup views_filter_handlers
17
 */
18
class views_handler_filter_in_operator extends views_handler_filter {
19

    
20
  /**
21
   *
22
   */
23
  public $value_form_type = 'checkboxes';
24

    
25
  /**
26
   * @var array
27
   * Stores all operations which are available on the form.
28
   */
29
  public $value_options = NULL;
30

    
31
  /**
32
   * {@inheritdoc}
33
   */
34
  public function construct() {
35
    parent::construct();
36
    $this->value_title = t('Options');
37
    $this->value_options = NULL;
38
  }
39

    
40
  /**
41
   * Child classes should be used to override this function and set the
42
   * 'value options', unless 'options callback' is defined as a valid function
43
   * or static public method to generate these values.
44
   *
45
   * This can use a guard to be used to reduce database hits as much as
46
   * possible.
47
   *
48
   * @return
49
   *   Return the stored values in $this->value_options if someone expects it.
50
   */
51
  public function get_value_options() {
52
    if (isset($this->value_options)) {
53
      return;
54
    }
55

    
56
    if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
57
      if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
58
        $this->value_options = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
59
      }
60
      else {
61
        $this->value_options = call_user_func($this->definition['options callback']);
62
      }
63
    }
64
    else {
65
      $this->value_options = array(t('Yes'), t('No'));
66
    }
67

    
68
    return $this->value_options;
69
  }
70

    
71
  /**
72
   * {@inheritdoc}
73
   */
74
  public function expose_options() {
75
    parent::expose_options();
76
    $this->options['expose']['reduce'] = FALSE;
77
  }
78

    
79
  /**
80
   * {@inheritdoc}
81
   */
82
  public function expose_form(&$form, &$form_state) {
83
    parent::expose_form($form, $form_state);
84
    $form['expose']['reduce'] = array(
85
      '#type' => 'checkbox',
86
      '#title' => t('Limit list to selected items'),
87
      '#description' => t('If checked, the only items presented to the user will be the ones selected here.'),
88
      // Safety.
89
      '#default_value' => !empty($this->options['expose']['reduce']),
90
    );
91
  }
92

    
93
  /**
94
   * {@inheritdoc}
95
   */
96
  public function option_definition() {
97
    $options = parent::option_definition();
98

    
99
    $options['operator']['default'] = 'in';
100
    $options['value']['default'] = array();
101
    $options['expose']['contains']['reduce'] = array('default' => FALSE, 'bool' => TRUE);
102

    
103
    return $options;
104
  }
105

    
106
  /**
107
   * This kind of construct makes it relatively easy for a child class to add or
108
   * remove functionality by overriding this function and adding/removing items
109
   * from this array.
110
   */
111
  public function operators() {
112
    $operators = array(
113
      'in' => array(
114
        'title' => t('Is one of'),
115
        'short' => t('in'),
116
        'short_single' => t('='),
117
        'method' => 'op_simple',
118
        'values' => 1,
119
      ),
120
      'not in' => array(
121
        'title' => t('Is not one of'),
122
        'short' => t('not in'),
123
        'short_single' => t('<>'),
124
        'method' => 'op_simple',
125
        'values' => 1,
126
      ),
127
    );
128
    // if the definition allows for the empty operator, add it.
129
    if (!empty($this->definition['allow empty'])) {
130
      $operators += array(
131
        'empty' => array(
132
          'title' => t('Is empty (NULL)'),
133
          'method' => 'op_empty',
134
          'short' => t('empty'),
135
          'values' => 0,
136
        ),
137
        'not empty' => array(
138
          'title' => t('Is not empty (NOT NULL)'),
139
          'method' => 'op_empty',
140
          'short' => t('not empty'),
141
          'values' => 0,
142
        ),
143
      );
144
    }
145

    
146
    return $operators;
147
  }
148

    
149
  /**
150
   * Build strings from the operators() for 'select' options.
151
   */
152
  public function operator_options($which = 'title') {
153
    $options = array();
154
    foreach ($this->operators() as $id => $info) {
155
      $options[$id] = $info[$which];
156
    }
157

    
158
    return $options;
159
  }
160

    
161
  /**
162
   * {@inheritdoc}
163
   */
164
  public function operator_values($values = 1) {
165
    $options = array();
166
    foreach ($this->operators() as $id => $info) {
167
      if (isset($info['values']) && $info['values'] == $values) {
168
        $options[] = $id;
169
      }
170
    }
171

    
172
    return $options;
173
  }
174

    
175
  /**
176
   * {@inheritdoc}
177
   */
178
  public function value_form(&$form, &$form_state) {
179
    $form['value'] = array();
180
    $options = array();
181

    
182
    if (empty($form_state['exposed'])) {
183
      // Add a select all option to the value form.
184
      $options = array('all' => t('Select all'));
185
    }
186

    
187
    $this->get_value_options();
188
    $options += $this->value_options;
189
    $default_value = (array) $this->value;
190

    
191
    $which = 'all';
192
    if (!empty($form['operator'])) {
193
      $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
194
    }
195
    if (!empty($form_state['exposed'])) {
196
      $identifier = $this->options['expose']['identifier'];
197

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

    
206
      if (!empty($this->options['expose']['reduce'])) {
207
        $options = $this->reduce_value_options();
208

    
209
        if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
210
          $default_value = array();
211
        }
212
      }
213

    
214
      if (empty($this->options['expose']['multiple'])) {
215
        if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
216
          $default_value = 'All';
217
        }
218
        elseif (empty($default_value)) {
219
          $keys = array_keys($options);
220
          $default_value = array_shift($keys);
221
        }
222
        else {
223
          $copy = $default_value;
224
          $default_value = array_shift($copy);
225
        }
226
      }
227
    }
228

    
229
    if ($which == 'all' || $which == 'value') {
230
      $form['value'] = array(
231
        '#type' => $this->value_form_type,
232
        '#title' => $this->value_title,
233
        '#options' => $options,
234
        '#default_value' => $default_value,
235
        // These are only valid for 'select' type, but do no harm to checkboxes.
236
        '#multiple' => TRUE,
237
        '#size' => count($options) > 8 ? 8 : count($options),
238
      );
239
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
240
        $form_state['input'][$identifier] = $default_value;
241
      }
242

    
243
      if ($which == 'all') {
244
        if (empty($form_state['exposed']) && (in_array($this->value_form_type, array('checkbox', 'checkboxes', 'radios', 'select')))) {
245
          $form['value']['#prefix'] = '<div id="edit-options-value-wrapper">';
246
          $form['value']['#suffix'] = '</div>';
247
        }
248
        $form['value']['#dependency'] = array($source => $this->operator_values(1));
249
      }
250
    }
251
  }
252

    
253
  /**
254
   * When using exposed filters, we may be required to reduce the set.
255
   */
256
  public function reduce_value_options($input = NULL) {
257
    if (!isset($input)) {
258
      $input = $this->value_options;
259
    }
260

    
261
    // Because options may be an array of strings, or an array of mixed arrays
262
    // and strings (optgroups) or an array of objects, we have to
263
    // step through and handle each one individually.
264
    $options = array();
265
    foreach ($input as $id => $option) {
266
      if (is_array($option)) {
267
        $options[$id] = $this->reduce_value_options($option);
268
        continue;
269
      }
270
      elseif (is_object($option)) {
271
        $keys = array_keys($option->option);
272
        $key = array_shift($keys);
273
        if (isset($this->options['value'][$key])) {
274
          $options[$id] = $option;
275
        }
276
      }
277
      elseif (isset($this->options['value'][$id])) {
278
        $options[$id] = $option;
279
      }
280
    }
281
    return $options;
282
  }
283

    
284
  /**
285
   * {@inheritdoc}
286
   */
287
  public function accept_exposed_input($input) {
288
    // A very special override because the All state for this type of
289
    // filter could have a default.
290
    if (empty($this->options['exposed'])) {
291
      return TRUE;
292
    }
293

    
294
    // If this is non-multiple and non-required, then this filter will
295
    // participate, but using the default settings, *if* 'limit is true.
296
    if (empty($this->options['expose']['multiple']) && empty($this->options['expose']['required']) && !empty($this->options['expose']['limit'])) {
297
      $identifier = $this->options['expose']['identifier'];
298
      if ($input[$identifier] == 'All') {
299
        return TRUE;
300
      }
301
    }
302

    
303
    return parent::accept_exposed_input($input);
304
  }
305

    
306
  /**
307
   * {@inheritdoc}
308
   */
309
  public function value_submit($form, &$form_state) {
310
    // Drupal's FAPI system automatically puts '0' in for any checkbox that
311
    // was not set, and the key to the checkbox if it is set.
312
    // Unfortunately, this means that if the key to that checkbox is 0,
313
    // we are unable to tell if that checkbox was set or not.
314

    
315
    // Luckily, the '#value' on the checkboxes form actually contains
316
    // *only* a list of checkboxes that were set, and we can use that
317
    // instead.
318

    
319
    $form_state['values']['options']['value'] = $form['value']['#value'];
320
  }
321

    
322
  /**
323
   * {@inheritdoc}
324
   */
325
  public function admin_summary() {
326
    if ($this->is_a_group()) {
327
      return t('grouped');
328
    }
329
    if (!empty($this->options['exposed'])) {
330
      return t('exposed');
331
    }
332
    $info = $this->operators();
333

    
334
    $this->get_value_options();
335

    
336
    if (!is_array($this->value)) {
337
      return;
338
    }
339

    
340
    $operator = check_plain($info[$this->operator]['short']);
341
    $values = '';
342
    if (in_array($this->operator, $this->operator_values(1))) {
343
      // Remove every element which is not known.
344
      foreach ($this->value as $value) {
345
        if (!isset($this->value_options[$value])) {
346
          unset($this->value[$value]);
347
        }
348
      }
349
      // Choose different kind of ouput for 0, a single and multiple values.
350
      if (count($this->value) == 0) {
351
        $values = t('Unknown');
352
      }
353
      elseif (count($this->value) == 1) {
354
        // If any, use the 'single' short name of the operator instead.
355
        if (isset($info[$this->operator]['short_single'])) {
356
          $operator = check_plain($info[$this->operator]['short_single']);
357
        }
358

    
359
        $keys = $this->value;
360
        $value = array_shift($keys);
361
        if (isset($this->value_options[$value])) {
362
          $values = check_plain($this->value_options[$value]);
363
        }
364
        else {
365
          $values = '';
366
        }
367
      }
368
      else {
369
        foreach ($this->value as $value) {
370
          if ($values !== '') {
371
            $values .= ', ';
372
          }
373
          if (drupal_strlen($values) > 8) {
374
            $values .= '...';
375
            break;
376
          }
377
          if (isset($this->value_options[$value])) {
378
            $values .= check_plain($this->value_options[$value]);
379
          }
380
        }
381
      }
382
    }
383

    
384
    return $operator . (($values !== '') ? ' ' . $values : '');
385
  }
386

    
387
  /**
388
   * {@inheritdoc}
389
   */
390
  public function query() {
391
    $info = $this->operators();
392
    if (!empty($info[$this->operator]['method'])) {
393
      $this->{$info[$this->operator]['method']}();
394
    }
395
  }
396

    
397
  /**
398
   * {@inheritdoc}
399
   */
400
  public function op_simple() {
401
    if (empty($this->value)) {
402
      return;
403
    }
404
    $this->ensure_my_table();
405

    
406
    // We use array_values() because the checkboxes keep keys and that can cause
407
    // array addition problems.
408
    $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", array_values($this->value), $this->operator);
409
  }
410

    
411
  /**
412
   * {@inheritdoc}
413
   */
414
  public function op_empty() {
415
    $this->ensure_my_table();
416
    if ($this->operator == 'empty') {
417
      $operator = "IS NULL";
418
    }
419
    else {
420
      $operator = "IS NOT NULL";
421
    }
422

    
423
    $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", NULL, $operator);
424
  }
425

    
426
  /**
427
   * {@inheritdoc}
428
   */
429
  public function validate() {
430
    $this->get_value_options();
431
    $errors = array();
432

    
433
    // If the operator is an operator which doesn't require a value, there is
434
    // no need for additional validation.
435
    if (in_array($this->operator, $this->operator_values(0))) {
436
      return array();
437
    }
438

    
439
    if (!in_array($this->operator, $this->operator_values(1))) {
440
      $errors[] = t('The operator is invalid on filter: @filter.', array('@filter' => $this->ui_name(TRUE)));
441
    }
442
    if (is_array($this->value)) {
443
      if (!isset($this->value_options)) {
444
        // Don't validate if there are none value options provided, for example
445
        // for special handlers.
446
        return $errors;
447
      }
448
      if ($this->options['exposed'] && !$this->options['expose']['required'] && empty($this->value)) {
449
        // Don't validate if the field is exposed and no default value is
450
        // provided.
451
        return $errors;
452
      }
453

    
454
      // Some filter_in_operator usage uses optgroups forms, so flatten it.
455
      $flat_options = form_options_flatten($this->value_options, TRUE);
456

    
457
      // Remove every element which is not known.
458
      foreach ($this->value as $value) {
459
        if (!isset($flat_options[$value])) {
460
          unset($this->value[$value]);
461
        }
462
      }
463
      // Choose different kind of ouput for 0, a single and multiple values.
464
      if (count($this->value) == 0) {
465
        $errors[] = t('No valid values found on filter: @filter.', array('@filter' => $this->ui_name(TRUE)));
466
      }
467
    }
468
    elseif (!empty($this->value) && ($this->operator == 'in' || $this->operator == 'not in')) {
469
      $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)));
470
    }
471
    return $errors;
472
  }
473

    
474
}