Project

General

Profile

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter.inc @ 8be7bf84

1
<?php
2

    
3
/**
4
 * @file
5
 * Definitions of views_handler_filter and views_handler_filter_broken.
6
 */
7

    
8
/**
9
 * @defgroup views_filter_handlers Views filter handlers
10
 * @{
11
 * Handlers to tell Views how to filter queries.
12
 *
13
 * Definition items:
14
 * - allow empty: If true, the 'IS NULL' and 'IS NOT NULL' operators become
15
 *   available as standard operators.
16
 *
17
 * Object flags:
18
 *   It's possible to set specific behavior using the following flags on the
19
 *   custom class:
20
 *   - always_multiple:
21
 *     Disable the possibility to force a single value.
22
 *   - no_operator:
23
 *     Disable the possibility to use operators.
24
 *   - always_required:
25
 *     Disable the possibility to allow a exposed input to be optional.
26
 */
27

    
28
/**
29
 * Base class for filters.
30
 *
31
 * @ingroup views_filter_handlers
32
 */
33
class views_handler_filter extends views_handler {
34

    
35
  /**
36
   * Contains the actual value of the field.
37
   *
38
   * This will be either configured in the views UI or entered in the exposed
39
   * filters.
40
   *
41
   * @var mixed
42
   */
43
  public $value = NULL;
44

    
45
  /**
46
   * Contains the operator which is used on the query.
47
   *
48
   * @var string
49
   */
50
  public $operator = '=';
51

    
52
  /**
53
   * Contains the information of the selected item in a gruped filter.
54
   *
55
   * @var array|null
56
   */
57
  public $group_info = NULL;
58

    
59
  /**
60
   * Disable the possibility to force a single value.
61
   *
62
   * @var bool
63
   */
64
  public $always_multiple = FALSE;
65

    
66
  /**
67
   * Disable the possibility to use operators.
68
   *
69
   * @var bool
70
   */
71
  public $no_operator = FALSE;
72

    
73
  /**
74
   * Disable the possibility to allow a exposed input to be optional.
75
   *
76
   * @var bool
77
   */
78
  public $always_required = FALSE;
79

    
80
  /**
81
   * Provide some extra help to get the operator/value easier to use.
82
   *
83
   * This likely has to be overridden by filters which are more complex
84
   * than simple operator/value.
85
   */
86
  public function init(&$view, &$options) {
87
    parent::init($view, $options);
88

    
89
    $this->operator = $this->options['operator'];
90
    $this->value = $this->options['value'];
91
    $this->group_info = $this->options['group_info']['default_group'];
92

    
93
    // Compatibility: The new UI changed several settings.
94
    if (!empty($options['exposed']) && !empty($options['expose']['optional']) && !isset($options['expose']['required'])) {
95
      $this->options['expose']['required'] = !$options['expose']['optional'];
96
    }
97
    if (!empty($options['exposed']) && !empty($options['expose']['single']) && !isset($options['expose']['multiple'])) {
98
      $this->options['expose']['multiple'] = !$options['expose']['single'];
99
    }
100
    if (!empty($options['exposed']) && !empty($options['expose']['operator']) && !isset($options['expose']['operator_id'])) {
101
      $this->options['expose']['operator_id'] = $options['expose']['operator_id'] = $options['expose']['operator'];
102
    }
103

    
104
    if ($this->multiple_exposed_input()) {
105
      $this->group_info = NULL;
106
      if (!empty($options['group_info']['default_group_multiple'])) {
107
        $this->group_info = array_filter($options['group_info']['default_group_multiple']);
108
      }
109

    
110
      $this->options['expose']['multiple'] = TRUE;
111
    }
112

    
113
    // If there are relationships in the view, allow empty should be true
114
    // so that we can do IS NULL checks on items. Not all filters respect
115
    // allow empty, but string and numeric do and that covers enough.
116
    if ($this->view->display_handler->get_option('relationships')) {
117
      $this->definition['allow empty'] = TRUE;
118
    }
119
  }
120

    
121
  /**
122
   * {@inheritdoc}
123
   */
124
  public function option_definition() {
125
    $options = parent::option_definition();
126

    
127
    $options['operator'] = array('default' => '=');
128
    $options['value'] = array('default' => '');
129
    $options['group'] = array('default' => '1');
130
    $options['exposed'] = array('default' => FALSE, 'bool' => TRUE);
131
    $options['expose'] = array(
132
      'contains' => array(
133
        'operator_id' => array('default' => FALSE),
134
        'label' => array('default' => '', 'translatable' => TRUE),
135
        'description' => array('default' => '', 'translatable' => TRUE),
136
        'use_operator' => array('default' => FALSE, 'bool' => TRUE),
137
        'operator_label' => array('default' => '', 'translatable' => TRUE),
138
        'operator' => array('default' => ''),
139
        'limit_operators' => array('default' => FALSE, 'bool' => TRUE),
140
        'available_operators' => array('default' => array()),
141
        'identifier' => array('default' => ''),
142
        'required' => array('default' => FALSE, 'bool' => TRUE),
143
        'remember' => array('default' => FALSE, 'bool' => TRUE),
144
        'multiple' => array('default' => FALSE, 'bool' => TRUE),
145
        'remember_roles' => array(
146
          'default' => array(
147
            DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
148
          ),
149
        ),
150
      ),
151
    );
152

    
153
    // A group is a combination of a filter, an operator and a value
154
    // operating like a single filter.
155
    // Users can choose from a select box which group they want to apply.
156
    // Views will filter the view according to the defined values.
157
    // Because it acts as a standard filter, we have to define
158
    // an identifier and other settings like the widget and the label.
159
    // This settings are saved in another array to allow users to switch
160
    // between a normal filter and a group of filters with a single click.
161
    $options['is_grouped'] = array('default' => FALSE, 'bool' => TRUE);
162
    $options['group_info'] = array(
163
      'contains' => array(
164
        'label' => array('default' => '', 'translatable' => TRUE),
165
        'description' => array('default' => '', 'translatable' => TRUE),
166
        'identifier' => array('default' => ''),
167
        'optional' => array('default' => TRUE, 'bool' => TRUE),
168
        'widget' => array('default' => 'select'),
169
        'multiple' => array('default' => FALSE, 'bool' => TRUE),
170
        'remember' => array('default' => 0),
171
        'default_group' => array('default' => 'All'),
172
        'default_group_multiple' => array('default' => array()),
173
        'group_items' => array('default' => array()),
174
      ),
175
    );
176

    
177
    return $options;
178
  }
179

    
180
  /**
181
   * Display the filter on the administrative summary.
182
   */
183
  public function admin_summary() {
184
    return check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
185
  }
186

    
187
  /**
188
   * Determine if a filter can be exposed.
189
   */
190
  public function can_expose() {
191
    return TRUE;
192
  }
193

    
194
  /**
195
   * Determine if a filter can be converted into a group.
196
   *
197
   * Only exposed filters with operators available can be converted into groups.
198
   */
199
  public function can_build_group() {
200
    return $this->is_exposed() && (count($this->operator_options()) > 0);
201
  }
202

    
203
  /**
204
   * Returns TRUE if the exposed filter works like a grouped filter.
205
   */
206
  public function is_a_group() {
207
    return $this->is_exposed() && !empty($this->options['is_grouped']);
208
  }
209

    
210
  /**
211
   * Provide the basic form which calls through to subforms.
212
   *
213
   * If overridden, it is best to call through to the parent, or to at least
214
   * make sure all of the functions in this form are called.
215
   */
216
  public function options_form(&$form, &$form_state) {
217
    parent::options_form($form, $form_state);
218
    if ($this->can_expose()) {
219
      $this->show_expose_button($form, $form_state);
220
    }
221
    if ($this->can_build_group()) {
222
      $this->show_build_group_button($form, $form_state);
223
    }
224
    $form['clear_markup_start'] = array(
225
      '#markup' => '<div class="clearfix">',
226
    );
227
    if ($this->is_a_group()) {
228
      if ($this->can_build_group()) {
229
        $form['clear_markup_start'] = array(
230
          '#markup' => '<div class="clearfix">',
231
        );
232
        // Render the build group form.
233
        $this->show_build_group_form($form, $form_state);
234
        $form['clear_markup_end'] = array(
235
          '#markup' => '</div>',
236
        );
237
      }
238
    }
239
    else {
240
      // Add the subform from operator_form().
241
      $this->show_operator_form($form, $form_state);
242
      // Add the subform from value_form().
243
      $this->show_value_form($form, $form_state);
244
      $form['clear_markup_end'] = array(
245
        '#markup' => '</div>',
246
      );
247
      if ($this->can_expose()) {
248
        // Add the subform from expose_form().
249
        $this->show_expose_form($form, $form_state);
250
      }
251
    }
252
  }
253

    
254
  /**
255
   * Simple validate handler.
256
   */
257
  public function options_validate(&$form, &$form_state) {
258
    $this->operator_validate($form, $form_state);
259
    $this->value_validate($form, $form_state);
260
    if (!empty($this->options['exposed']) && !$this->is_a_group()) {
261
      $this->expose_validate($form, $form_state);
262
    }
263
    if ($this->is_a_group()) {
264
      $this->build_group_validate($form, $form_state);
265
    }
266
  }
267

    
268
  /**
269
   * Simple submit handler.
270
   */
271
  public function options_submit(&$form, &$form_state) {
272
    // Don't store these.
273
    unset($form_state['values']['expose_button']);
274
    unset($form_state['values']['group_button']);
275

    
276
    if (!$this->is_a_group()) {
277
      $this->operator_submit($form, $form_state);
278
      $this->value_submit($form, $form_state);
279
    }
280
    if (!empty($this->options['exposed'])) {
281
      $options = &$form_state['values']['options']['expose'];
282
      $options['available_operators'] = (!empty($options['use_operator']) && !empty($options['limit_operators'])) ? array_filter($options['available_operators']) : array();
283
      $this->expose_submit($form, $form_state);
284
    }
285
    if ($this->is_a_group()) {
286
      $this->build_group_submit($form, $form_state);
287
    }
288
  }
289

    
290
  /**
291
   * Shortcut to display the operator form.
292
   */
293
  public function show_operator_form(&$form, &$form_state) {
294
    $this->operator_form($form, $form_state);
295
    $form['operator']['#prefix'] = '<div class="views-group-box views-left-30">';
296
    $form['operator']['#suffix'] = '</div>';
297
  }
298

    
299
  /**
300
   * Options form subform for setting the operator.
301
   *
302
   * This may be overridden by child classes, and it must define
303
   * $form['operator'].
304
   *
305
   * @see options_form()
306
   */
307
  public function operator_form(&$form, &$form_state) {
308
    $options = $this->operator_options();
309
    if (!empty($options)) {
310
      $available = $this->options['expose']['available_operators'];
311
      if ($this->options['expose']['limit_operators'] && count($available)) {
312
        foreach ($options as $key => $value) {
313
          if (!isset($available[$key])) {
314
            unset($options[$key]);
315
          }
316
        }
317

    
318
        // Make sure we have a valid default value if the current one is
319
        // excluded.
320
        if (!isset($options[$this->operator])) {
321
          // Just choose the first.
322
          $this->operator = key($options);
323
        }
324
      }
325
      $form['operator'] = array(
326
        '#type' => count($options) < 10 ? 'radios' : 'select',
327
        '#title' => t('Operator'),
328
        '#default_value' => $this->operator,
329
        '#options' => $options,
330
      );
331
    }
332
  }
333

    
334
  /**
335
   * Provide a list of options for the default operator form.
336
   *
337
   * Should be overridden by classes that don't override operator_form.
338
   */
339
  public function operator_options() {
340
    return array();
341
  }
342

    
343
  /**
344
   * Validate the operator form.
345
   */
346
  public function operator_validate($form, &$form_state) {
347
  }
348

    
349
  /**
350
   * Perform any necessary changes to the form values prior to storage.
351
   *
352
   * There is no need for this function to actually store the data.
353
   */
354
  public function operator_submit($form, &$form_state) {
355
  }
356

    
357
  /**
358
   * Shortcut to display the value form.
359
   */
360
  public function show_value_form(&$form, &$form_state) {
361
    $this->value_form($form, $form_state);
362
    if (empty($this->no_operator)) {
363
      $form['value']['#prefix'] = '<div class="views-group-box views-right-70">' . (isset($form['value']['#prefix']) ? $form['value']['#prefix'] : '');
364
      $form['value']['#suffix'] = (isset($form['value']['#suffix']) ? $form['value']['#suffix'] : '') . '</div>';
365
    }
366
  }
367

    
368
  /**
369
   * Options form subform for setting options.
370
   *
371
   * This should be overridden by all child classes and it must define
372
   * $form['value'].
373
   *
374
   * @see options_form()
375
   */
376
  public function value_form(&$form, &$form_state) {
377
    $form['value'] = array();
378
  }
379

    
380
  /**
381
   * Validate the options form.
382
   */
383
  public function value_validate($form, &$form_state) {
384
  }
385

    
386
  /**
387
   * Perform any necessary changes to the form values prior to storage.
388
   *
389
   * There is no need for this function to actually store the data.
390
   */
391
  public function value_submit($form, &$form_state) {
392
  }
393

    
394
  /**
395
   * Shortcut to display the exposed options form.
396
   */
397
  public function show_build_group_form(&$form, &$form_state) {
398
    if (empty($this->options['is_grouped'])) {
399
      return;
400
    }
401

    
402
    $this->build_group_form($form, $form_state);
403

    
404
    // When we click the expose button, we add new gadgets to the form but they
405
    // have no data in $_POST so their defaults get wiped out. This prevents
406
    // these defaults from getting wiped out. This setting will only be TRUE
407
    // during a 2nd pass rerender.
408
    if (!empty($form_state['force_build_group_options'])) {
409
      foreach (element_children($form['group_info']) as $id) {
410
        if (isset($form['group_info'][$id]['#default_value']) && !isset($form['group_info'][$id]['#value'])) {
411
          $form['group_info'][$id]['#value'] = $form['group_info'][$id]['#default_value'];
412
        }
413
      }
414
    }
415
  }
416

    
417
  /**
418
   * Shortcut to display the build_group/hide button.
419
   */
420
  public function show_build_group_button(&$form, &$form_state) {
421
    $form['group_button'] = array(
422
      '#prefix' => '<div class="views-grouped clearfix">',
423
      '#suffix' => '</div>',
424
      // Should always come after the description and the relationship.
425
      '#weight' => -190,
426
    );
427

    
428
    $grouped_description = t('Grouped filters allow a choice between predefined operator|value pairs.');
429
    $form['group_button']['radios'] = array(
430
      '#theme_wrappers' => array('container'),
431
      '#attributes' => array('class' => array('js-only')),
432
    );
433
    $form['group_button']['radios']['radios'] = array(
434
      '#title' => t('Filter type to expose'),
435
      '#description' => $grouped_description,
436
      '#type' => 'radios',
437
      '#options' => array(
438
        t('Single filter'),
439
        t('Grouped filters'),
440
      ),
441
    );
442

    
443
    if (empty($this->options['is_grouped'])) {
444
      $form['group_button']['markup'] = array(
445
        '#markup' => '<div class="description grouped-description">' . $grouped_description . '</div>',
446
      );
447
      $form['group_button']['button'] = array(
448
        '#limit_validation_errors' => array(),
449
        '#type' => 'submit',
450
        '#value' => t('Grouped filters'),
451
        '#submit' => array('views_ui_config_item_form_build_group'),
452
      );
453
      $form['group_button']['radios']['radios']['#default_value'] = 0;
454
    }
455
    else {
456
      $form['group_button']['button'] = array(
457
        '#limit_validation_errors' => array(),
458
        '#type' => 'submit',
459
        '#value' => t('Single filter'),
460
        '#submit' => array('views_ui_config_item_form_build_group'),
461
      );
462
      $form['group_button']['radios']['radios']['#default_value'] = 1;
463
    }
464
  }
465

    
466
  /**
467
   * Shortcut to display the expose/hide button.
468
   */
469
  public function show_expose_button(&$form, &$form_state) {
470
    $form['expose_button'] = array(
471
      '#prefix' => '<div class="views-expose clearfix">',
472
      '#suffix' => '</div>',
473
      // Should always come after the description and the relationship.
474
      '#weight' => -200,
475
    );
476

    
477
    // Add a checkbox for JS users, which will have behavior attached to it so
478
    // it can replace the button.
479
    $form['expose_button']['checkbox'] = array(
480
      '#theme_wrappers' => array('container'),
481
      '#attributes' => array('class' => array('js-only')),
482
    );
483
    $form['expose_button']['checkbox']['checkbox'] = array(
484
      '#title' => t('Expose this filter to visitors, to allow them to change it'),
485
      '#type' => 'checkbox',
486
    );
487

    
488
    // Then add the button itself.
489
    if (empty($this->options['exposed'])) {
490
      $form['expose_button']['markup'] = array(
491
        '#markup' => '<div class="description exposed-description">' . t('This filter is not exposed. Expose it to allow the users to change it.') . '</div>',
492
      );
493
      $form['expose_button']['button'] = array(
494
        '#limit_validation_errors' => array(),
495
        '#type' => 'submit',
496
        '#value' => t('Expose filter'),
497
        '#submit' => array('views_ui_config_item_form_expose'),
498
      );
499
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
500
    }
501
    else {
502
      $form['expose_button']['markup'] = array(
503
        '#markup' => '<div class="description exposed-description">' . t('This filter is exposed. If you hide it, users will not be able to change it.') . '</div>',
504
      );
505
      $form['expose_button']['button'] = array(
506
        '#limit_validation_errors' => array(),
507
        '#type' => 'submit',
508
        '#value' => t('Hide filter'),
509
        '#submit' => array('views_ui_config_item_form_expose'),
510
      );
511
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
512
    }
513
  }
514

    
515
  /**
516
   * Options form subform for exposed filter options.
517
   *
518
   * @see options_form()
519
   */
520
  public function expose_form(&$form, &$form_state) {
521
    $form['#theme'] = 'views_ui_expose_filter_form';
522
    // #flatten will move everything from $form['expose'][$key] to $form[$key]
523
    // prior to rendering. That's why the pre_render for it needs to run first,
524
    // so that when the next pre_render (the one for fieldsets) runs, it gets
525
    // the flattened data.
526
    array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
527
    $form['expose']['#flatten'] = TRUE;
528

    
529
    if (empty($this->always_required)) {
530
      $form['expose']['required'] = array(
531
        '#type' => 'checkbox',
532
        '#title' => t('Required'),
533
        '#default_value' => $this->options['expose']['required'],
534
      );
535

    
536
      $operator_options = $this->operator_options();
537
      if (count($operator_options)) {
538
        $form['expose']['limit_operators'] = array(
539
          '#type' => 'checkbox',
540
          '#title' => t('Limit operators'),
541
          '#description' => t('When checked, the operator will be exposed to the user'),
542
          '#default_value' => !empty($this->options['expose']['limit_operators']),
543
          '#dependency' => array(
544
            'edit-options-expose-use-operator' => array(1),
545
          ),
546
          '#description' => t('Restrict which operators will be available to select in the exposed operator form.'),
547
        );
548

    
549
        $form['expose']['available_operators'] = array(
550
          '#type' => 'checkboxes',
551
          '#title' => t('Limit the exposed operators'),
552
          '#default_value' => $this->options['expose']['available_operators'],
553
          '#prefix' => '<div id="edit-options-expose-available-operators-wrapper"><div id="edit-options-expose-available-operators">',
554
          '#suffix' => '</div></div>',
555
          '#description' => t('Select which operators will be available to select in the exposed operator form. If none are selected, all the operators listed here will be used.'),
556
          '#options' => $operator_options,
557
          '#dependency' => array(
558
            'edit-options-expose-limit-operators' => array(1),
559
          ),
560
        );
561
      }
562
    }
563
    else {
564
      $form['expose']['required'] = array(
565
        '#type' => 'value',
566
        '#value' => TRUE,
567
      );
568
    }
569
    $form['expose']['label'] = array(
570
      '#type' => 'textfield',
571
      '#default_value' => $this->options['expose']['label'],
572
      '#title' => t('Label'),
573
      '#size' => 40,
574
    );
575

    
576
    $form['expose']['description'] = array(
577
      '#type' => 'textfield',
578
      '#default_value' => $this->options['expose']['description'],
579
      '#title' => t('Description'),
580
      '#size' => 60,
581
    );
582

    
583
    if (!empty($form['operator']['#type'])) {
584
      // Increase the width of the left (operator) column.
585
      $form['operator']['#prefix'] = '<div class="views-group-box views-left-40">';
586
      $form['operator']['#suffix'] = '</div>';
587
      $form['value']['#prefix'] = '<div class="views-group-box views-right-60">';
588
      $form['value']['#suffix'] = '</div>';
589

    
590
      $form['expose']['use_operator'] = array(
591
        '#type' => 'checkbox',
592
        '#title' => t('Expose operator'),
593
        '#description' => t('Allow the user to choose the operator.'),
594
        '#default_value' => !empty($this->options['expose']['use_operator']),
595
      );
596
      $form['expose']['operator_label'] = array(
597
        '#type' => 'textfield',
598
        '#default_value' => $this->options['expose']['operator_label'],
599
        '#title' => t('Operator label'),
600
        '#size' => 40,
601
        '#description' => t('This will appear before your operator select field.'),
602
        '#dependency' => array(
603
          'edit-options-expose-use-operator' => array(1),
604
        ),
605
      );
606
      $form['expose']['operator_id'] = array(
607
        '#type' => 'textfield',
608
        '#default_value' => $this->options['expose']['operator_id'],
609
        '#title' => t('Operator identifier'),
610
        '#size' => 40,
611
        '#description' => t('This will appear in the URL after the ? to identify this operator.'),
612
        '#dependency' => array(
613
          'edit-options-expose-use-operator' => array(1),
614
        ),
615
        '#fieldset' => 'more',
616
      );
617
    }
618
    else {
619
      $form['expose']['operator_id'] = array(
620
        '#type' => 'value',
621
        '#value' => '',
622
      );
623
    }
624

    
625
    if (empty($this->always_multiple)) {
626
      $form['expose']['multiple'] = array(
627
        '#type' => 'checkbox',
628
        '#title' => t('Allow multiple selections'),
629
        '#description' => t('Enable to allow users to select multiple items.'),
630
        '#default_value' => $this->options['expose']['multiple'],
631
      );
632
    }
633
    $form['expose']['remember'] = array(
634
      '#type' => 'checkbox',
635
      '#title' => t('Remember the last selection'),
636
      '#description' => t('Enable to remember the last selection made by the user.'),
637
      '#default_value' => $this->options['expose']['remember'],
638
    );
639

    
640
    $role_options = array_map('check_plain', user_roles());
641
    $form['expose']['remember_roles'] = array(
642
      '#type' => 'checkboxes',
643
      '#title' => t('User roles'),
644
      '#description' => t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
645
      '#default_value' => $this->options['expose']['remember_roles'],
646
      '#options' => $role_options,
647
      '#dependency' => array(
648
        'edit-options-expose-remember' => array(1),
649
      ),
650
    );
651

    
652
    $form['expose']['identifier'] = array(
653
      '#type' => 'textfield',
654
      '#default_value' => $this->options['expose']['identifier'],
655
      '#title' => t('Filter identifier'),
656
      '#size' => 40,
657
      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
658
      '#fieldset' => 'more',
659
    );
660
  }
661

    
662
  /**
663
   * Validate the options form.
664
   */
665
  public function expose_validate($form, &$form_state) {
666
    if (empty($form_state['values']['options']['expose']['identifier'])) {
667
      form_error($form['expose']['identifier'], t('The identifier is required if the filter is exposed.'));
668
    }
669

    
670
    if (!empty($form_state['values']['options']['expose']['identifier'])) {
671
      $illegal_identifiers = array('value', 'q');
672
      if (in_array($form_state['values']['options']['expose']['identifier'], $illegal_identifiers)) {
673
        form_error($form['expose']['identifier'], t('This identifier is not allowed.'));
674
      }
675
    }
676

    
677
    if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['expose']['identifier'])) {
678
      form_error($form['expose']['identifier'], t('This identifier is used by another handler.'));
679
    }
680

    
681
    // Filter out roles which weren't selected, so that they aren't exported.
682
    // This is purely cosmetic.
683
    if (!empty($form_state['values']['options']['expose']['remember_roles'])) {
684
      $form_state['values']['options']['expose']['remember_roles'] = array_filter($form_state['values']['options']['expose']['remember_roles']);
685
    }
686
  }
687

    
688
  /**
689
   * Validate the build group options form.
690
   */
691
  public function build_group_validate($form, &$form_state) {
692
    if (!empty($form_state['values']['options']['group_info'])) {
693
      if (empty($form_state['values']['options']['group_info']['identifier'])) {
694
        form_error($form['group_info']['identifier'], t('The identifier is required if the filter is exposed.'));
695
      }
696

    
697
      if (!empty($form_state['values']['options']['group_info']['identifier'])) {
698
        $illegal_identifiers = array('value', 'q');
699
        if (in_array($form_state['values']['options']['group_info']['identifier'], $illegal_identifiers)) {
700
          form_error($form['group_info']['identifier'], t('This identifier is not allowed.'));
701
        }
702
      }
703

    
704
      if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['group_info']['identifier'])) {
705
        form_error($form['group_info']['identifier'], t('This identifier is used by another handler.'));
706
      }
707
    }
708

    
709
    if (!empty($form_state['values']['options']['group_info']['group_items'])) {
710
      foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
711
        if (empty($group['remove'])) {
712
          // Check if the title is defined but value wasn't defined.
713
          if (!empty($group['title'])) {
714
            // No value is needed for 'empty' and 'not empty' operator.
715
            if (!in_array($group['operator'], array('empty', 'not empty'))) {
716
              if ((!is_array($group['value']) && trim($group['value']) == "") ||
717
                  (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) == 0)) {
718
                form_error($form['group_info']['group_items'][$id]['value'],
719
                  t('The value is required if title for this item is defined.'));
720
              }
721
            }
722
          }
723

    
724
          // Check if the value is defined but title wasn't defined.
725
          if ((!is_array($group['value']) && trim($group['value']) != "") ||
726
              (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) > 0)) {
727
            if (empty($group['title'])) {
728
              form_error($form['group_info']['group_items'][$id]['title'],
729
                t('The title is required if value for this item is defined.'));
730
            }
731
          }
732
        }
733
      }
734
    }
735
  }
736

    
737
  /**
738
   * Save new group items, re-enumerates and remove groups marked to delete.
739
   */
740
  public function build_group_submit($form, &$form_state) {
741
    $groups = array();
742
    uasort($form_state['values']['options']['group_info']['group_items'], 'drupal_sort_weight');
743

    
744
    // Filter out removed items.
745
    // Start from 1 to avoid problems with #default_value in the widget.
746
    $new_id = 1;
747
    $new_default = 'All';
748
    foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
749
      if (empty($group['remove'])) {
750
        // Don't store this.
751
        unset($group['remove']);
752
        unset($group['weight']);
753
        $groups[$new_id] = $group;
754

    
755
        if ($form_state['values']['options']['group_info']['default_group'] === $id) {
756
          $new_default = $new_id;
757
        }
758
      }
759
      $new_id++;
760
    }
761
    if ($new_default != 'All') {
762
      $form_state['values']['options']['group_info']['default_group'] = $new_default;
763
    }
764
    $filter_default_multiple = array_filter($form_state['values']['options']['group_info']['default_group_multiple']);
765
    $form_state['values']['options']['group_info']['default_group_multiple'] = $filter_default_multiple;
766

    
767
    $form_state['values']['options']['group_info']['group_items'] = $groups;
768
  }
769

    
770
  /**
771
   * Provide default options for exposed filters.
772
   */
773
  public function expose_options() {
774
    $this->options['expose'] = array(
775
      'use_operator' => FALSE,
776
      'operator' => $this->options['id'] . '_op',
777
      'identifier' => $this->options['id'],
778
      'label' => $this->definition['title'],
779
      'description' => NULL,
780
      'remember' => FALSE,
781
      'multiple' => FALSE,
782
      'required' => FALSE,
783
    );
784
  }
785

    
786
  /**
787
   * Provide default options for exposed filters.
788
   */
789
  public function build_group_options() {
790
    $this->options['group_info'] = array(
791
      'label' => $this->definition['title'],
792
      'description' => NULL,
793
      'identifier' => $this->options['id'],
794
      'optional' => TRUE,
795
      'widget' => 'select',
796
      'multiple' => FALSE,
797
      'remember' => FALSE,
798
      'default_group' => 'All',
799
      'default_group_multiple' => array(),
800
      'group_items' => array(),
801
    );
802
  }
803

    
804
  /**
805
   * Build a form with a group of operator | values to apply as a single filter.
806
   */
807
  public function group_form(&$form, &$form_state) {
808
    if (!empty($this->options['group_info']['optional']) && !$this->multiple_exposed_input()) {
809
      $old_any = $this->options['group_info']['widget'] == 'select' ? '<Any>' : '&lt;Any&gt;';
810
      $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? $old_any : t('- Any -');
811
      $groups = array('All' => $any_label);
812
    }
813
    foreach ($this->options['group_info']['group_items'] as $id => $group) {
814
      if (!empty($group['title'])) {
815
        $groups[$id] = $id != 'All' ? t($group['title']) : $group['title'];
816
      }
817
    }
818

    
819
    if (count($groups)) {
820
      $value = $this->options['group_info']['identifier'];
821

    
822
      $form[$value] = array(
823
        '#type' => $this->options['group_info']['widget'],
824
        '#default_value' => $this->group_info,
825
        '#options' => $groups,
826
      );
827
      if (!empty($this->options['group_info']['multiple'])) {
828
        if (count($groups) < 5) {
829
          $form[$value]['#type'] = 'checkboxes';
830
        }
831
        else {
832
          $form[$value]['#type'] = 'select';
833
          $form[$value]['#size'] = 5;
834
          $form[$value]['#multiple'] = TRUE;
835
        }
836
        unset($form[$value]['#default_value']);
837
        if (empty($form_state['input'][$value])) {
838
          $form_state['input'][$value] = $this->group_info;
839
        }
840
      }
841

    
842
      $this->options['expose']['label'] = '';
843
    }
844
  }
845

    
846
  /**
847
   * Render our chunk of the exposed filter form when selecting.
848
   *
849
   * You can override this if it doesn't do what you expect.
850
   */
851
  public function exposed_form(&$form, &$form_state) {
852
    if (empty($this->options['exposed'])) {
853
      return;
854
    }
855

    
856
    // Build the exposed form, when its based on an operator.
857
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
858
      $operator = $this->options['expose']['operator_id'];
859
      $this->operator_form($form, $form_state);
860
      $form[$operator] = $form['operator'];
861
      $form[$operator]['#title'] = $this->options['expose']['operator_label'];
862
      $form[$operator]['#title_display'] = 'invisible';
863

    
864
      $this->exposed_translate($form[$operator], 'operator');
865

    
866
      unset($form['operator']);
867
    }
868

    
869
    // Build the form and set the value based on the identifier.
870
    if (!empty($this->options['expose']['identifier'])) {
871
      $value = $this->options['expose']['identifier'];
872
      if ($this->operator == 'empty' || $this->operator == 'not empty') {
873
        $boolean = new views_handler_filter_boolean_operator();
874
        $boolean->value = $this->value = 'All';
875
        $boolean->value_value = $this->value_value = '';
876
        $boolean->value_options = $this->value_options = array(
877
          1 => t('Yes'),
878
          0 => t('No'),
879
        );
880
        $boolean->value_form($form, $form_state);
881
      }
882
      else {
883
        $this->value_form($form, $form_state);
884
      }
885
      $form[$value] = $form['value'];
886

    
887
      if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
888
        unset($form[$value]['#title']);
889
      }
890

    
891
      $this->exposed_translate($form[$value], 'value');
892

    
893
      if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
894
        unset($form[$value]['#default_value']);
895
      }
896

    
897
      if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
898
        $form[$value]['#default_value'] = 'All';
899
      }
900

    
901
      if ($value != 'value') {
902
        unset($form['value']);
903
      }
904
    }
905
  }
906

    
907
  /**
908
   * Build the form to let users create the group of exposed filters.
909
   *
910
   * This form is displayed when users click on button 'Build group'
911
   */
912
  public function build_group_form(&$form, &$form_state) {
913
    if (empty($this->options['exposed']) || empty($this->options['is_grouped'])) {
914
      return;
915
    }
916
    $form['#theme'] = 'views_ui_build_group_filter_form';
917

    
918
    // #flatten will move everything from $form['group_info'][$key] to
919
    // $form[$key] prior to rendering. That's why the pre_render for it needs
920
    // to run first, so that when the next pre_render (the one for fieldsets)
921
    // runs, it gets the flattened data.
922
    array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
923
    $form['group_info']['#flatten'] = TRUE;
924

    
925
    if (!empty($this->options['group_info']['identifier'])) {
926
      $identifier = $this->options['group_info']['identifier'];
927
    }
928
    else {
929
      $identifier = 'group_' . $this->options['expose']['identifier'];
930
    }
931
    $form['group_info']['identifier'] = array(
932
      '#type' => 'textfield',
933
      '#default_value' => $identifier,
934
      '#title' => t('Filter identifier'),
935
      '#size' => 40,
936
      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
937
      '#fieldset' => 'more',
938
    );
939
    $form['group_info']['label'] = array(
940
      '#type' => 'textfield',
941
      '#default_value' => $this->options['group_info']['label'],
942
      '#title' => t('Label'),
943
      '#size' => 40,
944
    );
945

    
946
    $form['group_info']['optional'] = array(
947
      '#type' => 'checkbox',
948
      '#title' => t('Optional'),
949
      '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
950
      '#default_value' => $this->options['group_info']['optional'],
951
    );
952
    $form['group_info']['multiple'] = array(
953
      '#type' => 'checkbox',
954
      '#title' => t('Allow multiple selections'),
955
      '#description' => t('Enable to allow users to select multiple items.'),
956
      '#default_value' => $this->options['group_info']['multiple'],
957
    );
958
    $form['group_info']['widget'] = array(
959
      '#type' => 'radios',
960
      '#default_value' => $this->options['group_info']['widget'],
961
      '#title' => t('Widget type'),
962
      '#options' => array(
963
        'radios' => t('Radios'),
964
        'select' => t('Select'),
965
      ),
966
      '#description' => t('Select which kind of widget will be used to render the group of filters'),
967
    );
968
    $form['group_info']['remember'] = array(
969
      '#type' => 'checkbox',
970
      '#title' => t('Remember'),
971
      '#description' => t('Remember the last setting the user gave this filter.'),
972
      '#default_value' => $this->options['group_info']['remember'],
973
    );
974

    
975
    if (!empty($this->options['group_info']['identifier'])) {
976
      $identifier = $this->options['group_info']['identifier'];
977
    }
978
    else {
979
      $identifier = 'group_' . $this->options['expose']['identifier'];
980
    }
981
    $form['group_info']['identifier'] = array(
982
      '#type' => 'textfield',
983
      '#default_value' => $identifier,
984
      '#title' => t('Filter identifier'),
985
      '#size' => 40,
986
      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
987
      '#fieldset' => 'more',
988
    );
989
    $form['group_info']['label'] = array(
990
      '#type' => 'textfield',
991
      '#default_value' => $this->options['group_info']['label'],
992
      '#title' => t('Label'),
993
      '#size' => 40,
994
    );
995
    $form['group_info']['description'] = array(
996
      '#type' => 'textfield',
997
      '#default_value' => $this->options['group_info']['description'],
998
      '#title' => t('Description'),
999
      '#size' => 60,
1000
    );
1001
    $form['group_info']['optional'] = array(
1002
      '#type' => 'checkbox',
1003
      '#title' => t('Optional'),
1004
      '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
1005
      '#default_value' => $this->options['group_info']['optional'],
1006
    );
1007
    $form['group_info']['widget'] = array(
1008
      '#type' => 'radios',
1009
      '#default_value' => $this->options['group_info']['widget'],
1010
      '#title' => t('Widget type'),
1011
      '#options' => array(
1012
        'radios' => t('Radios'),
1013
        'select' => t('Select'),
1014
      ),
1015
      '#description' => t('Select which kind of widget will be used to render the group of filters'),
1016
    );
1017
    $form['group_info']['remember'] = array(
1018
      '#type' => 'checkbox',
1019
      '#title' => t('Remember'),
1020
      '#description' => t('Remember the last setting the user gave this filter.'),
1021
      '#default_value' => $this->options['group_info']['remember'],
1022
    );
1023

    
1024
    // The string '- Any -' will not be rendered.
1025
    // @see theme_views_ui_build_group_filter_form()
1026
    $groups = array('All' => '- Any -');
1027

    
1028
    // Provide 3 options to start when we are in a new group.
1029
    if (count($this->options['group_info']['group_items']) == 0) {
1030
      $this->options['group_info']['group_items'] = array_fill(1, 3, array());
1031
    }
1032

    
1033
    // After the general settings, comes a table with all the existent groups.
1034
    $default_weight = 0;
1035
    foreach ($this->options['group_info']['group_items'] as $item_id => $item) {
1036
      if (!empty($form_state['values']['options']['group_info']['group_items'][$item_id]['remove'])) {
1037
        continue;
1038
      }
1039

    
1040
      // Each rows contains three widgets:
1041
      // a) The title, where users define how they identify a pair of operator
1042
      //    | value.
1043
      // b) The operator.
1044
      // c) The value (or values) to use in the filter with the selected
1045
      //    operator.
1046
      // In each row, we have to display the operator form and the value from
1047
      // $row acts as a fake form to render each widget in a row.
1048
      $row = array();
1049
      $groups[$item_id] = '';
1050
      $this->operator_form($row, $form_state);
1051
      // Force the operator form to be a select box. Some handlers uses radios
1052
      // and they occupy a lot of space in a table row.
1053
      $row['operator']['#type'] = 'select';
1054
      $row['operator']['#title'] = '';
1055
      $this->value_form($row, $form_state);
1056

    
1057
      // Fix the dependencies to update value forms when operators changes.
1058
      // This is needed because forms are inside a new form and their IDs
1059
      // changes. Dependencies are used when operator changes from to
1060
      // 'Between', 'Not Between', etc, and two or more widgets are displayed.
1061
      $without_children = TRUE;
1062
      foreach (element_children($row['value']) as $children) {
1063
        if (isset($row['value'][$children]['#dependency']['edit-options-operator'])) {
1064
          $row['value'][$children]['#dependency']["edit-options-group-info-group-items-$item_id-operator"] = $row['value'][$children]['#dependency']['edit-options-operator'];
1065
          unset($row['value'][$children]['#dependency']['edit-options-operator']);
1066
          $row['value'][$children]['#title'] = '';
1067

    
1068
          if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$children])) {
1069
            $row['value'][$children]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$children];
1070
          }
1071
        }
1072
        $without_children = FALSE;
1073
      }
1074

    
1075
      if ($without_children) {
1076
        if (!empty($this->options['group_info']['group_items'][$item_id]['value'])) {
1077
          $row['value']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'];
1078
        }
1079
      }
1080

    
1081
      if (!empty($this->options['group_info']['group_items'][$item_id]['operator'])) {
1082
        $row['operator']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['operator'];
1083
      }
1084

    
1085
      $default_title = '';
1086
      if (!empty($this->options['group_info']['group_items'][$item_id]['title'])) {
1087
        $default_title = $this->options['group_info']['group_items'][$item_id]['title'];
1088
      }
1089

    
1090
      // Per item group, we have a title that identifies it.
1091
      $form['group_info']['group_items'][$item_id] = array(
1092
        'title' => array(
1093
          '#type' => 'textfield',
1094
          '#size' => 20,
1095
          '#default_value' => $default_title,
1096
        ),
1097
        'operator' => $row['operator'],
1098
        'value' => $row['value'],
1099
        'remove' => array(
1100
          '#type' => 'checkbox',
1101
          '#id' => 'views-removed-' . $item_id,
1102
          '#attributes' => array('class' => array('views-remove-checkbox')),
1103
          '#default_value' => 0,
1104
        ),
1105
        'weight' => array(
1106
          '#type' => 'weight',
1107
          '#delta' => count($this->options['group_info']['group_items']),
1108
          '#default_value' => $default_weight++,
1109
          '#attributes' => array('class' => array('weight')),
1110
        ),
1111
      );
1112
    }
1113

    
1114
    // From all groups, let chose which is the default.
1115
    $form['group_info']['default_group'] = array(
1116
      '#type' => 'radios',
1117
      '#options' => $groups,
1118
      '#default_value' => $this->options['group_info']['default_group'],
1119
      '#required' => TRUE,
1120
      '#attributes' => array(
1121
        'class' => array('default-radios'),
1122
      ),
1123
    );
1124

    
1125
    // From all groups, let chose which is the default.
1126
    $form['group_info']['default_group_multiple'] = array(
1127
      '#type' => 'checkboxes',
1128
      '#options' => $groups,
1129
      '#default_value' => $this->options['group_info']['default_group_multiple'],
1130
      '#attributes' => array(
1131
        'class' => array('default-checkboxes'),
1132
      ),
1133
    );
1134

    
1135
    $form['group_info']['add_group'] = array(
1136
      '#prefix' => '<div class="views-build-group clear-block">',
1137
      '#suffix' => '</div>',
1138
      '#type' => 'submit',
1139
      '#value' => t('Add another item'),
1140
      '#submit' => array('views_ui_config_item_form_add_group'),
1141
    );
1142

    
1143
    $js = array();
1144
    $js['tableDrag']['views-filter-groups']['weight'][0] = array(
1145
      'target' => 'weight',
1146
      'source' => NULL,
1147
      'relationship' => 'sibling',
1148
      'action' => 'order',
1149
      'hidden' => TRUE,
1150
      'limit' => 0,
1151
    );
1152
    if (!empty($form_state['js settings']) && is_array($js)) {
1153
      $form_state['js settings'] = array_merge($form_state['js settings'], $js);
1154
    }
1155
    else {
1156
      $form_state['js settings'] = $js;
1157
    }
1158
  }
1159

    
1160
  /**
1161
   * Make some translations to a form item to make it more suitable to exposing.
1162
   */
1163
  public function exposed_translate(&$form, $type) {
1164
    if (!isset($form['#type'])) {
1165
      return;
1166
    }
1167

    
1168
    if ($form['#type'] == 'radios') {
1169
      $form['#type'] = 'select';
1170
    }
1171
    // Checkboxes don't work so well in exposed forms due to GET conversions.
1172
    if ($form['#type'] == 'checkboxes') {
1173
      if (empty($form['#no_convert']) || empty($this->options['expose']['multiple'])) {
1174
        $form['#type'] = 'select';
1175
      }
1176
      if (!empty($this->options['expose']['multiple'])) {
1177
        $form['#multiple'] = TRUE;
1178
      }
1179
    }
1180
    if (empty($this->options['expose']['multiple']) && isset($form['#multiple'])) {
1181
      unset($form['#multiple']);
1182
      $form['#size'] = NULL;
1183
    }
1184

    
1185
    // Cleanup in case the translated element's (radios or checkboxes) display
1186
    // value contains html.
1187
    if ($form['#type'] == 'select') {
1188
      $this->prepare_filter_select_options($form['#options']);
1189
    }
1190

    
1191
    if ($type == 'value' && empty($this->always_required) && empty($this->options['expose']['required']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
1192
      $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -');
1193
      $form['#options'] = array('All' => $any_label) + $form['#options'];
1194
      $form['#default_value'] = 'All';
1195
    }
1196

    
1197
    if (!empty($this->options['expose']['required'])) {
1198
      $form['#required'] = TRUE;
1199
    }
1200
  }
1201

    
1202
  /**
1203
   * Sanitizes the HTML select element's options.
1204
   *
1205
   * The function is recursive to support optgroups.
1206
   */
1207
  public function prepare_filter_select_options(&$options) {
1208
    foreach ($options as $value => $label) {
1209
      // Recurse for optgroups.
1210
      if (is_array($label)) {
1211
        $this->prepare_filter_select_options($options[$value]);
1212
      }
1213
      // FAPI has some special value to allow hierarchy.
1214
      // @see _form_options_flatten()
1215
      elseif (is_object($label)) {
1216
        $this->prepare_filter_select_options($options[$value]->option);
1217
      }
1218
      else {
1219
        $options[$value] = strip_tags(decode_entities($label));
1220
      }
1221
    }
1222
  }
1223

    
1224
  /**
1225
   * Tell the renderer about our exposed form.
1226
   *
1227
   * This only needs to be overridden for particularly complex forms. And maybe
1228
   * not even then.
1229
   *
1230
   * @return array|null
1231
   *   For standard exposed filters. An array with the following keys:
1232
   *   - operator: The $form key of the operator. Set to NULL if no operator.
1233
   *   - value: The $form key of the value. Set to NULL if no value.
1234
   *   - label: The label to use for this piece.
1235
   *   For grouped exposed filters. An array with the following keys:
1236
   *   - value: The $form key of the value. Set to NULL if no value.
1237
   *   - label: The label to use for this piece.
1238
   */
1239
  public function exposed_info() {
1240
    if (empty($this->options['exposed'])) {
1241
      return;
1242
    }
1243

    
1244
    if ($this->is_a_group()) {
1245
      return array(
1246
        'value' => $this->options['group_info']['identifier'],
1247
        'label' => $this->options['group_info']['label'],
1248
        'description' => $this->options['group_info']['description'],
1249
      );
1250
    }
1251

    
1252
    return array(
1253
      'operator' => $this->options['expose']['operator_id'],
1254
      'value' => $this->options['expose']['identifier'],
1255
      'label' => $this->options['expose']['label'],
1256
      'description' => $this->options['expose']['description'],
1257
    );
1258
  }
1259

    
1260
  /**
1261
   * Transform the input from a grouped filter into a standard filter.
1262
   *
1263
   * When a filter is a group, find the set of operator and values that the
1264
   * choosen item represents, and inform views that a normal filter was
1265
   * submitted by telling the operator and the value selected.
1266
   *
1267
   * The param $selected_group_id is only passed when the filter uses the
1268
   * checkboxes widget, and this function will be called for each item choosen
1269
   * in the checkboxes.
1270
   */
1271
  public function convert_exposed_input(&$input, $selected_group_id = NULL) {
1272
    if ($this->is_a_group()) {
1273
      // If it is already defined the selected group, use it. Only valid when
1274
      // the filter uses checkboxes for widget.
1275
      if (!empty($selected_group_id)) {
1276
        $selected_group = $selected_group_id;
1277
      }
1278
      else {
1279
        $selected_group = $input[$this->options['group_info']['identifier']];
1280
      }
1281
      if ($selected_group == 'All' && !empty($this->options['group_info']['optional'])) {
1282
        return NULL;
1283
      }
1284
      if ($selected_group != 'All' && empty($this->options['group_info']['group_items'][$selected_group])) {
1285
        return FALSE;
1286
      }
1287
      if (isset($selected_group) && isset($this->options['group_info']['group_items'][$selected_group])) {
1288
        $input[$this->options['expose']['operator']] = $this->options['group_info']['group_items'][$selected_group]['operator'];
1289

    
1290
        // Value can be optional, For example for 'empty' and 'not empty'
1291
        // filters.
1292
        if (!empty($this->options['group_info']['group_items'][$selected_group]['value'])) {
1293
          $input[$this->options['expose']['identifier']] = $this->options['group_info']['group_items'][$selected_group]['value'];
1294
        }
1295
        $this->options['expose']['use_operator'] = TRUE;
1296

    
1297
        $this->group_info = $input[$this->options['group_info']['identifier']];
1298
        return TRUE;
1299
      }
1300
      else {
1301
        return FALSE;
1302
      }
1303
    }
1304
  }
1305

    
1306
  /**
1307
   * Options available for a grouped filter which uses checkboxes.
1308
   *
1309
   * Note: has to be applied several times, one per item selected.
1310
   *
1311
   * @return array
1312
   *   The options available for a grouped filter.
1313
   */
1314
  public function group_multiple_exposed_input(&$input) {
1315
    if (!empty($input[$this->options['group_info']['identifier']])) {
1316
      return array_filter($input[$this->options['group_info']['identifier']]);
1317
    }
1318
    return array();
1319
  }
1320

    
1321
  /**
1322
   * Indicate whether users can select multiple group items.
1323
   *
1324
   * @return bool
1325
   *   TRUE if users can select multiple groups items of a grouped exposed
1326
   *   filter.
1327
   */
1328
  public function multiple_exposed_input() {
1329
    return $this->is_a_group() && !empty($this->options['group_info']['multiple']);
1330
  }
1331

    
1332
  /**
1333
   * If set to remember exposed input in the session, store it there.
1334
   *
1335
   * This function is similar to store_exposed_input but modified to work
1336
   * properly when the filter is a group.
1337
   */
1338
  public function store_group_input($input, $status) {
1339
    if (!$this->is_a_group() || empty($this->options['group_info']['identifier'])) {
1340
      return TRUE;
1341
    }
1342

    
1343
    if (empty($this->options['group_info']['remember'])) {
1344
      return;
1345
    }
1346

    
1347
    // Figure out which display id is responsible for the filters, so we know
1348
    // where to look for session stored values.
1349
    $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
1350

    
1351
    // False means that we got a setting that means to recuse ourselves, so we
1352
    // should erase whatever happened to be there.
1353
    if ($status === FALSE && isset($_SESSION['views'][$this->view->name][$display_id])) {
1354
      $session = &$_SESSION['views'][$this->view->name][$display_id];
1355

    
1356
      if (isset($session[$this->options['group_info']['identifier']])) {
1357
        unset($session[$this->options['group_info']['identifier']]);
1358
      }
1359
    }
1360

    
1361
    if ($status !== FALSE) {
1362
      if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
1363
        $_SESSION['views'][$this->view->name][$display_id] = array();
1364
      }
1365

    
1366
      $session = &$_SESSION['views'][$this->view->name][$display_id];
1367

    
1368
      $session[$this->options['group_info']['identifier']] = $input[$this->options['group_info']['identifier']];
1369
    }
1370
  }
1371

    
1372
  /**
1373
   * Check to see if input from the exposed filters should change the behavior.
1374
   */
1375
  public function accept_exposed_input($input) {
1376
    if (empty($this->options['exposed'])) {
1377
      return TRUE;
1378
    }
1379

    
1380
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
1381
      $this->operator = $input[$this->options['expose']['operator_id']];
1382
    }
1383

    
1384
    if (!empty($this->options['expose']['identifier'])) {
1385
      $value = $input[$this->options['expose']['identifier']];
1386

    
1387
      // Various ways to check for the absence of non-required input.
1388
      if (empty($this->options['expose']['required'])) {
1389
        if ($this->operator == 'empty' || $this->operator == 'not empty') {
1390
          $value = is_array($value) ? $value['value'] : $value;
1391
          $this->operator = ($this->operator == 'empty' && empty($value)) || ($this->operator == 'not empty' && !empty($value)) ? 'not empty' : 'empty';
1392
        }
1393

    
1394
        if ($value == 'All' || $value === array()) {
1395
          return FALSE;
1396
        }
1397

    
1398
        if (!empty($this->always_multiple) && $value === '') {
1399
          return FALSE;
1400
        }
1401
      }
1402

    
1403
      if (isset($value)) {
1404
        $this->value = $value;
1405
        if (empty($this->always_multiple) && empty($this->options['expose']['multiple'])) {
1406
          $this->value = array($value);
1407
        }
1408
      }
1409
      else {
1410
        return FALSE;
1411
      }
1412
    }
1413

    
1414
    return TRUE;
1415
  }
1416

    
1417
  /**
1418
   * Store the exposed input for processing later.
1419
   */
1420
  public function store_exposed_input($input, $status) {
1421
    if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
1422
      return TRUE;
1423
    }
1424

    
1425
    if (empty($this->options['expose']['remember'])) {
1426
      return;
1427
    }
1428

    
1429
    // Check if we store exposed value for current user.
1430
    global $user;
1431
    $allowed_rids = empty($this->options['expose']['remember_roles']) ? array() : array_filter($this->options['expose']['remember_roles']);
1432
    $intersect_rids = array_intersect_key($allowed_rids, $user->roles);
1433
    if (empty($intersect_rids)) {
1434
      return;
1435
    }
1436

    
1437
    // Figure out which display id is responsible for the filters, so we know
1438
    // where to look for session stored values.
1439
    $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
1440

    
1441
    // Shortcut test.
1442
    $operator = !empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']);
1443

    
1444
    // False means that we got a setting that means to recuse ourselves, so we
1445
    // should erase whatever happened to be there.
1446
    if (!$status && isset($_SESSION['views'][$this->view->name][$display_id])) {
1447
      $session = &$_SESSION['views'][$this->view->name][$display_id];
1448
      if ($operator && isset($session[$this->options['expose']['operator_id']])) {
1449
        unset($session[$this->options['expose']['operator_id']]);
1450
      }
1451

    
1452
      if (isset($session[$this->options['expose']['identifier']])) {
1453
        unset($session[$this->options['expose']['identifier']]);
1454
      }
1455
    }
1456

    
1457
    if ($status) {
1458
      if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
1459
        $_SESSION['views'][$this->view->name][$display_id] = array();
1460
      }
1461

    
1462
      $session = &$_SESSION['views'][$this->view->name][$display_id];
1463

    
1464
      if ($operator && isset($input[$this->options['expose']['operator_id']])) {
1465
        $session[$this->options['expose']['operator_id']] = $input[$this->options['expose']['operator_id']];
1466
      }
1467

    
1468
      if (isset($input[$this->options['expose']['identifier']])) {
1469
        $session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];
1470
      }
1471
    }
1472
  }
1473

    
1474
  /**
1475
   * Add this filter to the query.
1476
   *
1477
   * Due to the nature of fapi, the value and the operator have an unintended
1478
   * level of indirection. You will find them in $this->operator and
1479
   * $this->value respectively.
1480
   */
1481
  public function query() {
1482
    $this->ensure_my_table();
1483
    $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $this->value, $this->operator);
1484
  }
1485

    
1486
  /**
1487
   * Can this filter be used in OR groups?
1488
   *
1489
   * Some filters have complicated where clauses that cannot be easily used with
1490
   * OR groups. Some filters must also use HAVING which also makes them not
1491
   * groupable. These filters will end up in a special group if OR grouping is
1492
   * in use.
1493
   *
1494
   * @return bool
1495
   *   Whether the filter can be used in OR groups.
1496
   */
1497
  public function can_group() {
1498
    return TRUE;
1499
  }
1500

    
1501
}
1502

    
1503

    
1504
/**
1505
 * A special handler to take the place of missing or broken handlers.
1506
 *
1507
 * @ingroup views_filter_handlers
1508
 */
1509
class views_handler_filter_broken extends views_handler_filter {
1510

    
1511
  /**
1512
   * {@inheritdoc}
1513
   */
1514
  public function ui_name($short = FALSE) {
1515
    return t('Broken/missing handler');
1516
  }
1517

    
1518
  /**
1519
   * {@inheritdoc}
1520
   */
1521
  public function ensure_my_table() {
1522
    // No table to ensure!
1523
  }
1524

    
1525
  /**
1526
   * {@inheritdoc}
1527
   */
1528
  public function query($group_by = FALSE) {
1529
    // No query to run.
1530
  }
1531

    
1532
  /**
1533
   * {@inheritdoc}
1534
   */
1535
  public function options_form(&$form, &$form_state) {
1536
    $form['markup'] = array(
1537
      '#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
1538
    );
1539
  }
1540

    
1541
  /**
1542
   * {@inheritdoc}
1543
   */
1544
  public function broken() {
1545
    return TRUE;
1546
  }
1547

    
1548
}
1549

    
1550
/**
1551
 * Filter by no empty values, though allow to use "0".
1552
 *
1553
 * @param string $var
1554
 *   The string to check.
1555
 *
1556
 * @return bool
1557
 *   Indicates if the argument is an empty string.
1558
 */
1559
function _views_array_filter_zero($var) {
1560
  return trim($var) != '';
1561
}
1562

    
1563
/**
1564
 * @}
1565
 */