Projet

Général

Profil

Paste
Télécharger (51,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter.inc @ 5d12d676

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
 *  You can set some specific behavior by setting up the following flags on
19
 *  your custom class.
20
 *
21
 * - always_multiple:
22
 *    Disable the possibility to force a single value.
23
 * - no_operator:
24
 *    Disable the possibility to use operators.
25
 * - always_required:
26
 *    Disable the possibility to allow a exposed input to be optional.
27
 */
28

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
172
    return $options;
173
  }
174

    
175
  /**
176
   * Display the filter on the administrative summary.
177
   */
178
  public function admin_summary() {
179
    return check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
180
  }
181

    
182
  /**
183
   * Determine if a filter can be exposed.
184
   */
185
  public function can_expose() {
186
    return TRUE;
187
  }
188

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

    
198
  /**
199
   * Returns TRUE if the exposed filter works like a grouped filter.
200
   */
201
  public function is_a_group() {
202
    return $this->is_exposed() && !empty($this->options['is_grouped']);
203
  }
204

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

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

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

    
272
    if (!$this->is_a_group()) {
273
      $this->operator_submit($form, $form_state);
274
      $this->value_submit($form, $form_state);
275
    }
276
    if (!empty($this->options['exposed'])) {
277
      $this->expose_submit($form, $form_state);
278
    }
279
    if ($this->is_a_group()) {
280
      $this->build_group_submit($form, $form_state);
281
    }
282
  }
283

    
284
  /**
285
   * Shortcut to display the operator form.
286
   */
287
  public function show_operator_form(&$form, &$form_state) {
288
    $this->operator_form($form, $form_state);
289
    $form['operator']['#prefix'] = '<div class="views-group-box views-left-30">';
290
    $form['operator']['#suffix'] = '</div>';
291
  }
292

    
293
  /**
294
   * Options form subform for setting the operator.
295
   *
296
   * This may be overridden by child classes, and it must
297
   * define $form['operator'];
298
   *
299
   * @see options_form()
300
   */
301
  public function operator_form(&$form, &$form_state) {
302
    $options = $this->operator_options();
303
    if (!empty($options)) {
304
      $form['operator'] = array(
305
        '#type' => count($options) < 10 ? 'radios' : 'select',
306
        '#title' => t('Operator'),
307
        '#default_value' => $this->operator,
308
        '#options' => $options,
309
      );
310
    }
311
  }
312

    
313
  /**
314
   * Provide a list of options for the default operator form.
315
   *
316
   * Should be overridden by classes that don't override operator_form
317
   */
318
  public function operator_options() {
319
    return array();
320
  }
321

    
322
  /**
323
   * Validate the operator form.
324
   */
325
  public function operator_validate($form, &$form_state) {
326
  }
327

    
328
  /**
329
   * Perform any necessary changes to the form values prior to storage.
330
   *
331
   * There is no need for this function to actually store the data.
332
   */
333
  public function operator_submit($form, &$form_state) {
334
  }
335

    
336
  /**
337
   * Shortcut to display the value form.
338
   */
339
  public function show_value_form(&$form, &$form_state) {
340
    $this->value_form($form, $form_state);
341
    if (empty($this->no_operator)) {
342
      $form['value']['#prefix'] = '<div class="views-group-box views-right-70">' . (isset($form['value']['#prefix']) ? $form['value']['#prefix'] : '');
343
      $form['value']['#suffix'] = (isset($form['value']['#suffix']) ? $form['value']['#suffix'] : '') . '</div>';
344
    }
345
  }
346

    
347
  /**
348
   * Options form subform for setting options.
349
   *
350
   * This should be overridden by all child classes and it must define
351
   * $form['value'].
352
   *
353
   * @see options_form()
354
   */
355
  public function value_form(&$form, &$form_state) {
356
    $form['value'] = array();
357
  }
358

    
359
  /**
360
   * Validate the options form.
361
   */
362
  public function value_validate($form, &$form_state) {
363
  }
364

    
365
  /**
366
   * Perform any necessary changes to the form values prior to storage.
367
   *
368
   * There is no need for this function to actually store the data.
369
   */
370
  public function value_submit($form, &$form_state) {
371
  }
372

    
373
  /**
374
   * Shortcut to display the exposed options form.
375
   */
376
  public function show_build_group_form(&$form, &$form_state) {
377
    if (empty($this->options['is_grouped'])) {
378
      return;
379
    }
380

    
381
    $this->build_group_form($form, $form_state);
382

    
383
    // When we click the expose button, we add new gadgets to the form but they
384
    // have no data in $_POST so their defaults get wiped out. This prevents
385
    // these defaults from getting wiped out. This setting will only be TRUE
386
    // during a 2nd pass rerender.
387
    if (!empty($form_state['force_build_group_options'])) {
388
      foreach (element_children($form['group_info']) as $id) {
389
        if (isset($form['group_info'][$id]['#default_value']) && !isset($form['group_info'][$id]['#value'])) {
390
          $form['group_info'][$id]['#value'] = $form['group_info'][$id]['#default_value'];
391
        }
392
      }
393
    }
394
  }
395

    
396
  /**
397
   * Shortcut to display the build_group/hide button.
398
   */
399
  public function show_build_group_button(&$form, &$form_state) {
400
    $form['group_button'] = array(
401
      '#prefix' => '<div class="views-grouped clearfix">',
402
      '#suffix' => '</div>',
403
      // Should always come after the description and the relationship.
404
      '#weight' => -190,
405
    );
406

    
407
    $grouped_description = t('Grouped filters allow a choice between predefined operator|value pairs.');
408
    $form['group_button']['radios'] = array(
409
      '#theme_wrappers' => array('container'),
410
      '#attributes' => array('class' => array('js-only')),
411
    );
412
    $form['group_button']['radios']['radios'] = array(
413
      '#title' => t('Filter type to expose'),
414
      '#description' => $grouped_description,
415
      '#type' => 'radios',
416
      '#options' => array(
417
        t('Single filter'),
418
        t('Grouped filters'),
419
      ),
420
    );
421

    
422
    if (empty($this->options['is_grouped'])) {
423
      $form['group_button']['markup'] = array(
424
        '#markup' => '<div class="description grouped-description">' . $grouped_description . '</div>',
425
      );
426
      $form['group_button']['button'] = array(
427
        '#limit_validation_errors' => array(),
428
        '#type' => 'submit',
429
        '#value' => t('Grouped filters'),
430
        '#submit' => array('views_ui_config_item_form_build_group'),
431
      );
432
      $form['group_button']['radios']['radios']['#default_value'] = 0;
433
    }
434
    else {
435
      $form['group_button']['button'] = array(
436
        '#limit_validation_errors' => array(),
437
        '#type' => 'submit',
438
        '#value' => t('Single filter'),
439
        '#submit' => array('views_ui_config_item_form_build_group'),
440
      );
441
      $form['group_button']['radios']['radios']['#default_value'] = 1;
442
    }
443
  }
444

    
445
  /**
446
   * Shortcut to display the expose/hide button.
447
   */
448
  public function show_expose_button(&$form, &$form_state) {
449
    $form['expose_button'] = array(
450
      '#prefix' => '<div class="views-expose clearfix">',
451
      '#suffix' => '</div>',
452
      // Should always come after the description and the relationship.
453
      '#weight' => -200,
454
    );
455

    
456
    // Add a checkbox for JS users, which will have behavior attached to it so
457
    // it can replace the button.
458
    $form['expose_button']['checkbox'] = array(
459
      '#theme_wrappers' => array('container'),
460
      '#attributes' => array('class' => array('js-only')),
461
    );
462
    $form['expose_button']['checkbox']['checkbox'] = array(
463
      '#title' => t('Expose this filter to visitors, to allow them to change it'),
464
      '#type' => 'checkbox',
465
    );
466

    
467
    // Then add the button itself.
468
    if (empty($this->options['exposed'])) {
469
      $form['expose_button']['markup'] = array(
470
        '#markup' => '<div class="description exposed-description">' . t('This filter is not exposed. Expose it to allow the users to change it.') . '</div>',
471
      );
472
      $form['expose_button']['button'] = array(
473
        '#limit_validation_errors' => array(),
474
        '#type' => 'submit',
475
        '#value' => t('Expose filter'),
476
        '#submit' => array('views_ui_config_item_form_expose'),
477
      );
478
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
479
    }
480
    else {
481
      $form['expose_button']['markup'] = array(
482
        '#markup' => '<div class="description exposed-description">' . t('This filter is exposed. If you hide it, users will not be able to change it.') . '</div>',
483
      );
484
      $form['expose_button']['button'] = array(
485
        '#limit_validation_errors' => array(),
486
        '#type' => 'submit',
487
        '#value' => t('Hide filter'),
488
        '#submit' => array('views_ui_config_item_form_expose'),
489
      );
490
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
491
    }
492
  }
493

    
494
  /**
495
   * Options form subform for exposed filter options.
496
   *
497
   * @see options_form()
498
   */
499
  public function expose_form(&$form, &$form_state) {
500
    $form['#theme'] = 'views_ui_expose_filter_form';
501
    // #flatten will move everything from $form['expose'][$key] to $form[$key]
502
    // prior to rendering. That's why the pre_render for it needs to run first,
503
    // so that when the next pre_render (the one for fieldsets) runs, it gets
504
    // the flattened data.
505
    array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
506
    $form['expose']['#flatten'] = TRUE;
507

    
508
    if (empty($this->always_required)) {
509
      $form['expose']['required'] = array(
510
        '#type' => 'checkbox',
511
        '#title' => t('Required'),
512
        '#default_value' => $this->options['expose']['required'],
513
      );
514
    }
515
    else {
516
      $form['expose']['required'] = array(
517
        '#type' => 'value',
518
        '#value' => TRUE,
519
      );
520
    }
521
    $form['expose']['label'] = array(
522
      '#type' => 'textfield',
523
      '#default_value' => $this->options['expose']['label'],
524
      '#title' => t('Label'),
525
      '#size' => 40,
526
    );
527

    
528
    $form['expose']['description'] = array(
529
      '#type' => 'textfield',
530
      '#default_value' => $this->options['expose']['description'],
531
      '#title' => t('Description'),
532
      '#size' => 60,
533
    );
534

    
535
    if (!empty($form['operator']['#type'])) {
536
       // Increase the width of the left (operator) column.
537
      $form['operator']['#prefix'] = '<div class="views-group-box views-left-40">';
538
      $form['operator']['#suffix'] = '</div>';
539
      $form['value']['#prefix'] = '<div class="views-group-box views-right-60">';
540
      $form['value']['#suffix'] = '</div>';
541

    
542
      $form['expose']['use_operator'] = array(
543
        '#type' => 'checkbox',
544
        '#title' => t('Expose operator'),
545
        '#description' => t('Allow the user to choose the operator.'),
546
        '#default_value' => !empty($this->options['expose']['use_operator']),
547
      );
548
      $form['expose']['operator_label'] = array(
549
        '#type' => 'textfield',
550
        '#default_value' => $this->options['expose']['operator_label'],
551
        '#title' => t('Operator label'),
552
        '#size' => 40,
553
        '#description' => t('This will appear before your operator select field.'),
554
        '#dependency' => array(
555
          'edit-options-expose-use-operator' => array(1)
556
        ),
557
      );
558
      $form['expose']['operator_id'] = array(
559
        '#type' => 'textfield',
560
        '#default_value' => $this->options['expose']['operator_id'],
561
        '#title' => t('Operator identifier'),
562
        '#size' => 40,
563
        '#description' => t('This will appear in the URL after the ? to identify this operator.'),
564
        '#dependency' => array(
565
          'edit-options-expose-use-operator' => array(1)
566
        ),
567
        '#fieldset' => 'more',
568
      );
569
    }
570
    else {
571
      $form['expose']['operator_id'] = array(
572
        '#type' => 'value',
573
        '#value' => '',
574
      );
575
    }
576

    
577
    if (empty($this->always_multiple)) {
578
      $form['expose']['multiple'] = array(
579
        '#type' => 'checkbox',
580
        '#title' => t('Allow multiple selections'),
581
        '#description' => t('Enable to allow users to select multiple items.'),
582
        '#default_value' => $this->options['expose']['multiple'],
583
      );
584
    }
585
    $form['expose']['remember'] = array(
586
      '#type' => 'checkbox',
587
      '#title' => t('Remember the last selection'),
588
      '#description' => t('Enable to remember the last selection made by the user.'),
589
      '#default_value' => $this->options['expose']['remember'],
590
    );
591

    
592
    $role_options = array_map('check_plain', user_roles());
593
    $form['expose']['remember_roles'] = array(
594
      '#type' => 'checkboxes',
595
      '#title' => t('User roles'),
596
      '#description' => t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
597
      '#default_value' => $this->options['expose']['remember_roles'],
598
      '#options' => $role_options,
599
      '#dependency' => array(
600
        'edit-options-expose-remember' => array(1),
601
      ),
602
    );
603

    
604
    $form['expose']['identifier'] = array(
605
      '#type' => 'textfield',
606
      '#default_value' => $this->options['expose']['identifier'],
607
      '#title' => t('Filter identifier'),
608
      '#size' => 40,
609
      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
610
      '#fieldset' => 'more',
611
    );
612
  }
613

    
614
  /**
615
   * Validate the options form.
616
   */
617
  public function expose_validate($form, &$form_state) {
618
    if (empty($form_state['values']['options']['expose']['identifier'])) {
619
      form_error($form['expose']['identifier'], t('The identifier is required if the filter is exposed.'));
620
    }
621

    
622
    if (!empty($form_state['values']['options']['expose']['identifier'])) {
623
      $illegal_identifiers = array('value', 'q');
624
      if (in_array($form_state['values']['options']['expose']['identifier'], $illegal_identifiers)) {
625
        form_error($form['expose']['identifier'], t('This identifier is not allowed.'));
626
      }
627
    }
628

    
629
    if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['expose']['identifier'])) {
630
      form_error($form['expose']['identifier'], t('This identifier is used by another handler.'));
631
    }
632
  }
633

    
634
  /**
635
   * Validate the build group options form.
636
   */
637
  public function build_group_validate($form, &$form_state) {
638
    if (!empty($form_state['values']['options']['group_info'])) {
639
      if (empty($form_state['values']['options']['group_info']['identifier'])) {
640
        form_error($form['group_info']['identifier'], t('The identifier is required if the filter is exposed.'));
641
      }
642

    
643
      if (!empty($form_state['values']['options']['group_info']['identifier'])) {
644
        $illegal_identifiers = array('value', 'q');
645
        if (in_array($form_state['values']['options']['group_info']['identifier'], $illegal_identifiers)) {
646
          form_error($form['group_info']['identifier'], t('This identifier is not allowed.'));
647
        }
648
      }
649

    
650
      if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['group_info']['identifier'])) {
651
        form_error($form['group_info']['identifier'], t('This identifier is used by another handler.'));
652
      }
653
    }
654

    
655
    if (!empty($form_state['values']['options']['group_info']['group_items'])) {
656
      foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
657
        if (empty($group['remove'])) {
658
          // Check if the title is defined but value wasn't defined.
659
          if (!empty($group['title'])) {
660
            if ((!is_array($group['value']) && trim($group['value']) == "") ||
661
                (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) == 0)) {
662
              form_error($form['group_info']['group_items'][$id]['value'],
663
                t('The value is required if title for this item is defined.'));
664
            }
665
          }
666

    
667
          // Check if the value is defined but title wasn't defined.
668
          if ((!is_array($group['value']) && trim($group['value']) != "") ||
669
              (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) > 0)) {
670
            if (empty($group['title'])) {
671
              form_error($form['group_info']['group_items'][$id]['title'],
672
                t('The title is required if value for this item is defined.'));
673
            }
674
          }
675
        }
676
      }
677
    }
678
  }
679

    
680
  /**
681
   * Save new group items, re-enumerates and remove groups marked to delete.
682
   */
683
  public function build_group_submit($form, &$form_state) {
684
    $groups = array();
685
    uasort($form_state['values']['options']['group_info']['group_items'], 'drupal_sort_weight');
686

    
687
    // Filter out removed items.
688
    // Start from 1 to avoid problems with #default_value in the widget.
689
    $new_id = 1;
690
    $new_default = 'All';
691
    foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
692
      if (empty($group['remove'])) {
693
        // Don't store this.
694
        unset($group['remove']);
695
        unset($group['weight']);
696
        $groups[$new_id] = $group;
697

    
698
        if ($form_state['values']['options']['group_info']['default_group'] === $id) {
699
          $new_default = $new_id;
700
        }
701
      }
702
      $new_id++;
703
    }
704
    if ($new_default != 'All') {
705
      $form_state['values']['options']['group_info']['default_group'] = $new_default;
706
    }
707
    $filter_default_multiple = array_filter($form_state['values']['options']['group_info']['default_group_multiple']);
708
    $form_state['values']['options']['group_info']['default_group_multiple'] = $filter_default_multiple;
709

    
710
    $form_state['values']['options']['group_info']['group_items'] = $groups;
711
  }
712

    
713
  /**
714
   * Provide default options for exposed filters.
715
   */
716
  public function expose_options() {
717
    $this->options['expose'] = array(
718
      'use_operator' => FALSE,
719
      'operator' => $this->options['id'] . '_op',
720
      'identifier' => $this->options['id'],
721
      'label' => $this->definition['title'],
722
      'description' => NULL,
723
      'remember' => FALSE,
724
      'multiple' => FALSE,
725
      'required' => FALSE,
726
    );
727
  }
728

    
729
  /**
730
   * Provide default options for exposed filters.
731
   */
732
  public function build_group_options() {
733
    $this->options['group_info'] = array(
734
      'label' => $this->definition['title'],
735
      'description' => NULL,
736
      'identifier' => $this->options['id'],
737
      'optional' => TRUE,
738
      'widget' => 'select',
739
      'multiple' => FALSE,
740
      'remember' => FALSE,
741
      'default_group' => 'All',
742
      'default_group_multiple' => array(),
743
      'group_items' => array(),
744
    );
745
  }
746

    
747
  /**
748
   * Build a form with a group of operator | values to apply as a single filter.
749
   */
750
  public function group_form(&$form, &$form_state) {
751
    if (!empty($this->options['group_info']['optional']) && !$this->multiple_exposed_input()) {
752
      $old_any = $this->options['group_info']['widget'] == 'select' ? '<Any>' : '&lt;Any&gt;';
753
      $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? $old_any : t('- Any -');
754
      $groups = array('All' => $any_label);
755
    }
756
    foreach ($this->options['group_info']['group_items'] as $id => $group) {
757
      if (!empty($group['title'])) {
758
        $groups[$id] = $id != 'All' ? t($group['title']) : $group['title'];
759
      }
760
    }
761

    
762
    if (count($groups)) {
763
      $value = $this->options['group_info']['identifier'];
764

    
765
      $form[$value] = array(
766
        '#type' => $this->options['group_info']['widget'],
767
        '#default_value' => $this->group_info,
768
        '#options' => $groups,
769
      );
770
      if (!empty($this->options['group_info']['multiple'])) {
771
        if (count($groups) < 5) {
772
          $form[$value]['#type'] = 'checkboxes';
773
        }
774
        else {
775
          $form[$value]['#type'] = 'select';
776
          $form[$value]['#size'] = 5;
777
          $form[$value]['#multiple'] = TRUE;
778
        }
779
        unset($form[$value]['#default_value']);
780
        if (empty($form_state['input'][$value])) {
781
          $form_state['input'][$value] = $this->group_info;
782
        }
783
      }
784

    
785
      $this->options['expose']['label'] = '';
786
    }
787
  }
788

    
789

    
790
  /**
791
   * Render our chunk of the exposed filter form when selecting
792
   *
793
   * You can override this if it doesn't do what you expect.
794
   */
795
  public function exposed_form(&$form, &$form_state) {
796
    if (empty($this->options['exposed'])) {
797
      return;
798
    }
799

    
800
    // Build the exposed form, when its based on an operator.
801
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
802
      $operator = $this->options['expose']['operator_id'];
803
      $this->operator_form($form, $form_state);
804
      $form[$operator] = $form['operator'];
805
      $form[$operator]['#title'] =  $this->options['expose']['operator_label'];
806
      $form[$operator]['#title_display'] = 'invisible';
807

    
808
      $this->exposed_translate($form[$operator], 'operator');
809

    
810
      unset($form['operator']);
811
    }
812

    
813
    // Build the form and set the value based on the identifier.
814
    if (!empty($this->options['expose']['identifier'])) {
815
      $value = $this->options['expose']['identifier'];
816
      $this->value_form($form, $form_state);
817
      $form[$value] = $form['value'];
818

    
819
      if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
820
        unset($form[$value]['#title']);
821
      }
822

    
823
      $this->exposed_translate($form[$value], 'value');
824

    
825
      if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
826
        unset($form[$value]['#default_value']);
827
      }
828

    
829
      if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
830
        $form[$value]['#default_value'] = 'All';
831
      }
832

    
833
      if ($value != 'value') {
834
        unset($form['value']);
835
      }
836
    }
837
  }
838

    
839
  /**
840
   * Build the form to let users create the group of exposed filters.
841
   *
842
   * This form is displayed when users click on button 'Build group'
843
   */
844
  public function build_group_form(&$form, &$form_state) {
845
    if (empty($this->options['exposed']) || empty($this->options['is_grouped'])) {
846
      return;
847
    }
848
    $form['#theme'] = 'views_ui_build_group_filter_form';
849

    
850
    // #flatten will move everything from $form['group_info'][$key] to
851
    // $form[$key] prior to rendering. That's why the pre_render for it needs
852
    // to run first, so that when the next pre_render (the one for fieldsets)
853
    // runs, it gets the flattened data.
854
    array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
855
    $form['group_info']['#flatten'] = TRUE;
856

    
857
    if (!empty($this->options['group_info']['identifier'])) {
858
      $identifier = $this->options['group_info']['identifier'];
859
    }
860
    else {
861
      $identifier = 'group_' . $this->options['expose']['identifier'];
862
    }
863
    $form['group_info']['identifier'] = array(
864
      '#type' => 'textfield',
865
      '#default_value' => $identifier,
866
      '#title' => t('Filter identifier'),
867
      '#size' => 40,
868
      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
869
      '#fieldset' => 'more',
870
    );
871
    $form['group_info']['label'] = array(
872
      '#type' => 'textfield',
873
      '#default_value' => $this->options['group_info']['label'],
874
      '#title' => t('Label'),
875
      '#size' => 40,
876
    );
877

    
878
    $form['group_info']['optional'] = array(
879
      '#type' => 'checkbox',
880
      '#title' => t('Optional'),
881
      '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
882
      '#default_value' => $this->options['group_info']['optional'],
883
    );
884
    $form['group_info']['multiple'] = array(
885
      '#type' => 'checkbox',
886
      '#title' => t('Allow multiple selections'),
887
      '#description' => t('Enable to allow users to select multiple items.'),
888
      '#default_value' => $this->options['group_info']['multiple'],
889
    );
890
    $form['group_info']['widget'] = array(
891
      '#type' => 'radios',
892
      '#default_value' => $this->options['group_info']['widget'],
893
      '#title' => t('Widget type'),
894
      '#options' => array(
895
        'radios' => t('Radios'),
896
        'select' => t('Select'),
897
      ),
898
      '#description' => t('Select which kind of widget will be used to render the group of filters'),
899
    );
900
    $form['group_info']['remember'] = array(
901
      '#type' => 'checkbox',
902
      '#title' => t('Remember'),
903
      '#description' => t('Remember the last setting the user gave this filter.'),
904
      '#default_value' => $this->options['group_info']['remember'],
905
    );
906

    
907
    if (!empty($this->options['group_info']['identifier'])) {
908
      $identifier = $this->options['group_info']['identifier'];
909
    }
910
    else {
911
      $identifier = 'group_' . $this->options['expose']['identifier'];
912
    }
913
    $form['group_info']['identifier'] = array(
914
      '#type' => 'textfield',
915
      '#default_value' => $identifier,
916
      '#title' => t('Filter identifier'),
917
      '#size' => 40,
918
      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
919
      '#fieldset' => 'more',
920
    );
921
    $form['group_info']['label'] = array(
922
      '#type' => 'textfield',
923
      '#default_value' => $this->options['group_info']['label'],
924
      '#title' => t('Label'),
925
      '#size' => 40,
926
    );
927
    $form['group_info']['description'] = array(
928
      '#type' => 'textfield',
929
      '#default_value' => $this->options['group_info']['description'],
930
      '#title' => t('Description'),
931
      '#size' => 60,
932
    );
933
    $form['group_info']['optional'] = array(
934
      '#type' => 'checkbox',
935
      '#title' => t('Optional'),
936
      '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
937
      '#default_value' => $this->options['group_info']['optional'],
938
    );
939
    $form['group_info']['widget'] = array(
940
      '#type' => 'radios',
941
      '#default_value' => $this->options['group_info']['widget'],
942
      '#title' => t('Widget type'),
943
      '#options' => array(
944
        'radios' => t('Radios'),
945
        'select' => t('Select'),
946
      ),
947
      '#description' => t('Select which kind of widget will be used to render the group of filters'),
948
    );
949
    $form['group_info']['remember'] = array(
950
      '#type' => 'checkbox',
951
      '#title' => t('Remember'),
952
      '#description' => t('Remember the last setting the user gave this filter.'),
953
      '#default_value' => $this->options['group_info']['remember'],
954
    );
955

    
956
    // The string '- Any -' will not be rendered.
957
    // @see theme_views_ui_build_group_filter_form()
958
    $groups = array('All' => '- Any -'); 
959

    
960
    // Provide 3 options to start when we are in a new group.
961
    if (count($this->options['group_info']['group_items']) == 0) {
962
      $this->options['group_info']['group_items'] = array_fill(1, 3, array());
963
    }
964

    
965
    // After the general settings, comes a table with all the existent groups.
966
    $default_weight = 0;
967
    foreach ($this->options['group_info']['group_items'] as $item_id => $item) {
968
      if (!empty($form_state['values']['options']['group_info']['group_items'][$item_id]['remove'])) {
969
        continue;
970
      }
971
      // Each rows contains three widgets:
972
      // a) The title, where users define how they identify a pair of operator
973
      //    | value.
974
      // b) The operator.
975
      // c) The value (or values) to use in the filter with the selected
976
      //    operator.
977
      // In each row, we have to display the operator form and the value from
978
      // $row acts as a fake form to render each widget in a row.
979
      $row = array();
980
      $groups[$item_id] = '';
981
      $this->operator_form($row, $form_state);
982
      // Force the operator form to be a select box. Some handlers uses
983
      // radios and they occupy a lot of space in a table row.
984
      $row['operator']['#type'] = 'select';
985
      $row['operator']['#title'] = '';
986
      $this->value_form($row, $form_state);
987

    
988
      // Fix the dependencies to update value forms when operators changes.
989
      // This is needed because forms are inside a new form and their ids
990
      // changes. Dependencies are used when operator changes from to
991
      // 'Between', 'Not Between', etc, and two or more widgets are displayed.
992
      $without_children = TRUE;
993
      foreach (element_children($row['value']) as $children) {
994
        if (isset($row['value'][$children]['#dependency']['edit-options-operator'])) {
995
          $row['value'][$children]['#dependency']["edit-options-group-info-group-items-$item_id-operator"] = $row['value'][$children]['#dependency']['edit-options-operator'];
996
          unset($row['value'][$children]['#dependency']['edit-options-operator']);
997
          $row['value'][$children]['#title'] = '';
998

    
999
          if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$children])) {
1000
            $row['value'][$children]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$children];
1001
          }
1002
        }
1003
        $without_children = FALSE;
1004
      }
1005

    
1006
      if ($without_children) {
1007
        if (!empty($this->options['group_info']['group_items'][$item_id]['value'])) {
1008
          $row['value']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'];
1009
        }
1010
      }
1011

    
1012
      if (!empty($this->options['group_info']['group_items'][$item_id]['operator'])) {
1013
        $row['operator']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['operator'];
1014
      }
1015

    
1016
      $default_title = '';
1017
      if (!empty($this->options['group_info']['group_items'][$item_id]['title'])) {
1018
        $default_title = $this->options['group_info']['group_items'][$item_id]['title'];
1019
      }
1020

    
1021
      // Per item group, we have a title that identifies it.
1022
      $form['group_info']['group_items'][$item_id] = array(
1023
        'title' => array(
1024
          '#type' => 'textfield',
1025
          '#size' => 20,
1026
          '#default_value' => $default_title,
1027
        ),
1028
        'operator' => $row['operator'],
1029
        'value' => $row['value'],
1030
        'remove' => array(
1031
          '#type' => 'checkbox',
1032
          '#id' => 'views-removed-' . $item_id,
1033
          '#attributes' => array('class' => array('views-remove-checkbox')),
1034
          '#default_value' => 0,
1035
        ),
1036
        'weight' => array(
1037
          '#type' => 'weight',
1038
          '#delta' => count($this->options['group_info']['group_items']),
1039
          '#default_value' => $default_weight++,
1040
          '#attributes' => array('class' => array('weight')),
1041
        ),
1042
      );
1043
    }
1044
    // From all groups, let chose which is the default.
1045
    $form['group_info']['default_group'] = array(
1046
      '#type' => 'radios',
1047
      '#options' => $groups,
1048
      '#default_value' => $this->options['group_info']['default_group'],
1049
      '#required' => TRUE,
1050
      '#attributes' => array(
1051
        'class' => array('default-radios'),
1052
      )
1053
    );
1054
    // From all groups, let chose which is the default.
1055
    $form['group_info']['default_group_multiple'] = array(
1056
      '#type' => 'checkboxes',
1057
      '#options' => $groups,
1058
      '#default_value' => $this->options['group_info']['default_group_multiple'],
1059
      '#attributes' => array(
1060
        'class' => array('default-checkboxes'),
1061
      )
1062
    );
1063

    
1064
    $form['group_info']['add_group'] = array(
1065
      '#prefix' => '<div class="views-build-group clear-block">',
1066
      '#suffix' => '</div>',
1067
      '#type' => 'submit',
1068
      '#value' => t('Add another item'),
1069
      '#submit' => array('views_ui_config_item_form_add_group'),
1070
    );
1071

    
1072
    $js = array();
1073
    $js['tableDrag']['views-filter-groups']['weight'][0] = array(
1074
      'target' => 'weight',
1075
      'source' => NULL,
1076
      'relationship' => 'sibling',
1077
      'action' => 'order',
1078
      'hidden' => TRUE,
1079
      'limit' => 0,
1080
    );
1081
    if (!empty($form_state['js settings']) && is_array($js)) {
1082
      $form_state['js settings'] = array_merge($form_state['js settings'], $js);
1083
    }
1084
    else {
1085
      $form_state['js settings'] = $js;
1086
    }
1087
  }
1088

    
1089

    
1090
  /**
1091
   * Make some translations to a form item to make it more suitable to exposing.
1092
   */
1093
  public function exposed_translate(&$form, $type) {
1094
    if (!isset($form['#type'])) {
1095
      return;
1096
    }
1097

    
1098
    if ($form['#type'] == 'radios') {
1099
      $form['#type'] = 'select';
1100
    }
1101
    // Checkboxes don't work so well in exposed forms due to GET conversions.
1102
    if ($form['#type'] == 'checkboxes') {
1103
      if (empty($form['#no_convert']) || empty($this->options['expose']['multiple'])) {
1104
        $form['#type'] = 'select';
1105
      }
1106
      if (!empty($this->options['expose']['multiple'])) {
1107
        $form['#multiple'] = TRUE;
1108
      }
1109
    }
1110
    if (empty($this->options['expose']['multiple']) && isset($form['#multiple'])) {
1111
      unset($form['#multiple']);
1112
      $form['#size'] = NULL;
1113
    }
1114

    
1115
    // Cleanup in case the translated element's (radios or checkboxes) display
1116
    // value contains html.
1117
    if ($form['#type'] == 'select') {
1118
      $this->prepare_filter_select_options($form['#options']);
1119
    }
1120

    
1121
    if ($type == 'value' && empty($this->always_required) && empty($this->options['expose']['required']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
1122
      $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -');
1123
      $form['#options'] = array('All' => $any_label) + $form['#options'];
1124
      $form['#default_value'] = 'All';
1125
    }
1126

    
1127
    if (!empty($this->options['expose']['required'])) {
1128
      $form['#required'] = TRUE;
1129
    }
1130
  }
1131

    
1132
  /**
1133
   * Sanitizes the HTML select element's options.
1134
   *
1135
   * The function is recursive to support optgroups.
1136
   */
1137
  public function prepare_filter_select_options(&$options) {
1138
    foreach ($options as $value => $label) {
1139
      // Recurse for optgroups.
1140
      if (is_array($label)) {
1141
        $this->prepare_filter_select_options($options[$value]);
1142
      }
1143
      // FAPI has some special value to allow hierarchy.
1144
      // @see _form_options_flatten()
1145
      elseif (is_object($label)) {
1146
        $this->prepare_filter_select_options($options[$value]->option);
1147
      }
1148
      else {
1149
        $options[$value] = strip_tags(decode_entities($label));
1150
      }
1151
    }
1152
  }
1153

    
1154
  /**
1155
   * Tell the renderer about our exposed form.
1156
   *
1157
   * This only needs to be overridden for particularly complex forms. And maybe
1158
   * not even then.
1159
   *
1160
   * @return array|null
1161
   *   For standard exposed filters. An array with the following keys:
1162
   *   - operator: The $form key of the operator. Set to NULL if no operator.
1163
   *   - value: The $form key of the value. Set to NULL if no value.
1164
   *   - label: The label to use for this piece.
1165
   *   For grouped exposed filters. An array with the following keys:
1166
   *   - value: The $form key of the value. Set to NULL if no value.
1167
   *   - label: The label to use for this piece.
1168
   */
1169
  public function exposed_info() {
1170
    if (empty($this->options['exposed'])) {
1171
      return;
1172
    }
1173

    
1174
    if ($this->is_a_group()) {
1175
      return array(
1176
        'value' => $this->options['group_info']['identifier'],
1177
        'label' => $this->options['group_info']['label'],
1178
        'description' => $this->options['group_info']['description'],
1179
      );
1180
    }
1181

    
1182
    return array(
1183
      'operator' => $this->options['expose']['operator_id'],
1184
      'value' => $this->options['expose']['identifier'],
1185
      'label' => $this->options['expose']['label'],
1186
      'description' => $this->options['expose']['description'],
1187
    );
1188
  }
1189

    
1190
  /**
1191
   * Transform the input from a grouped filter into a standard filter.
1192
   *
1193
   * When a filter is a group, find the set of operator and values
1194
   * that the choosed item represents, and inform views that a normal
1195
   * filter was submitted by telling the operator and the value selected.
1196
   *
1197
   * The param $selected_group_id is only passed when the filter uses the
1198
   * checkboxes widget, and this function will be called for each item
1199
   * choosed in the checkboxes.
1200
   */
1201
  public function convert_exposed_input(&$input, $selected_group_id = NULL) {
1202
    if ($this->is_a_group()) {
1203
      // If it is already defined the selected group, use it. Only valid when
1204
      // the filter uses checkboxes for widget.
1205
      if (!empty($selected_group_id)) {
1206
        $selected_group = $selected_group_id;
1207
      }
1208
      else {
1209
        $selected_group = $input[$this->options['group_info']['identifier']];
1210
      }
1211
      if ($selected_group == 'All' && !empty($this->options['group_info']['optional'])) {
1212
        return NULL;
1213
      }
1214
      if ($selected_group != 'All' && empty($this->options['group_info']['group_items'][$selected_group])) {
1215
        return FALSE;
1216
      }
1217
      if (isset($selected_group) && isset($this->options['group_info']['group_items'][$selected_group])) {
1218
        $input[$this->options['expose']['operator']] = $this->options['group_info']['group_items'][$selected_group]['operator'];
1219

    
1220
        // Value can be optional, For example for 'empty' and 'not empty'
1221
        // filters.
1222
        if (!empty($this->options['group_info']['group_items'][$selected_group]['value'])) {
1223
          $input[$this->options['expose']['identifier']] = $this->options['group_info']['group_items'][$selected_group]['value'];
1224
        }
1225
        $this->options['expose']['use_operator'] = TRUE;
1226

    
1227
        $this->group_info = $input[$this->options['group_info']['identifier']];
1228
        return TRUE;
1229
      }
1230
      else {
1231
        return FALSE;
1232
      }
1233
    }
1234
  }
1235

    
1236
  /**
1237
   * Returns the options available for a grouped filter that users checkboxes
1238
   * as widget, and therefore has to be applied several times, one per
1239
   * item selected.
1240
   */
1241
  public function group_multiple_exposed_input(&$input) {
1242
    if (!empty($input[$this->options['group_info']['identifier']])) {
1243
      return array_filter($input[$this->options['group_info']['identifier']]);
1244
    }
1245
    return array();
1246
  }
1247

    
1248
  /**
1249
   *
1250
   *
1251
   * @return bool
1252
   *   TRUE if users can select multiple groups items of a grouped exposed
1253
   *   filter.
1254
   */
1255
  public function multiple_exposed_input() {
1256
    return $this->is_a_group() && !empty($this->options['group_info']['multiple']);
1257
  }
1258

    
1259
  /**
1260
   * If set to remember exposed input in the session, store it there.
1261
   * This function is similar to store_exposed_input but modified to
1262
   * work properly when the filter is a group.
1263
   */
1264
  public function store_group_input($input, $status) {
1265
    if (!$this->is_a_group() || empty($this->options['group_info']['identifier'])) {
1266
      return TRUE;
1267
    }
1268

    
1269
    if (empty($this->options['group_info']['remember'])) {
1270
      return;
1271
    }
1272

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

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

    
1282
      if (isset($session[$this->options['group_info']['identifier']])) {
1283
        unset($session[$this->options['group_info']['identifier']]);
1284
      }
1285
    }
1286

    
1287
    if ($status !== FALSE) {
1288
      if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
1289
        $_SESSION['views'][$this->view->name][$display_id] = array();
1290
      }
1291

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

    
1294
      $session[$this->options['group_info']['identifier']] = $input[$this->options['group_info']['identifier']];
1295
    }
1296
  }
1297

    
1298
  /**
1299
   * Check to see if input from the exposed filters should change the behavior.
1300
   */
1301
  public function accept_exposed_input($input) {
1302
    if (empty($this->options['exposed'])) {
1303
      return TRUE;
1304
    }
1305

    
1306
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
1307
      $this->operator = $input[$this->options['expose']['operator_id']];
1308
    }
1309

    
1310
    if (!empty($this->options['expose']['identifier'])) {
1311
      $value = $input[$this->options['expose']['identifier']];
1312

    
1313
      // Various ways to check for the absence of non-required input.
1314
      if (empty($this->options['expose']['required'])) {
1315
        if (($this->operator == 'empty' || $this->operator == 'not empty') && $value === '') {
1316
          $value = ' ';
1317
        }
1318

    
1319
        if ($this->operator != 'empty' && $this->operator != 'not empty') {
1320
          if ($value == 'All' || $value === array()) {
1321
            return FALSE;
1322
          }
1323
        }
1324

    
1325
        if (!empty($this->always_multiple) && $value === '') {
1326
          return FALSE;
1327
        }
1328
      }
1329

    
1330
      if (isset($value)) {
1331
        $this->value = $value;
1332
        if (empty($this->always_multiple) && empty($this->options['expose']['multiple'])) {
1333
          $this->value = array($value);
1334
        }
1335
      }
1336
      else {
1337
        return FALSE;
1338
      }
1339
    }
1340

    
1341
    return TRUE;
1342
  }
1343

    
1344
  /**
1345
   *
1346
   */
1347
  public function store_exposed_input($input, $status) {
1348
    if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
1349
      return TRUE;
1350
    }
1351

    
1352
    if (empty($this->options['expose']['remember'])) {
1353
      return;
1354
    }
1355

    
1356
    // Check if we store exposed value for current user.
1357
    global $user;
1358
    $allowed_rids = empty($this->options['expose']['remember_roles']) ? array() : array_filter($this->options['expose']['remember_roles']);
1359
    $intersect_rids = array_intersect_key($allowed_rids, $user->roles);
1360
    if (empty($intersect_rids)) {
1361
      return;
1362
    }
1363

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

    
1368
    // Shortcut test.
1369
    $operator = !empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']);
1370

    
1371
    // False means that we got a setting that means to recuse ourselves, so we
1372
    // should erase whatever happened to be there.
1373
    if (!$status && isset($_SESSION['views'][$this->view->name][$display_id])) {
1374
      $session = &$_SESSION['views'][$this->view->name][$display_id];
1375
      if ($operator && isset($session[$this->options['expose']['operator_id']])) {
1376
        unset($session[$this->options['expose']['operator_id']]);
1377
      }
1378

    
1379
      if (isset($session[$this->options['expose']['identifier']])) {
1380
        unset($session[$this->options['expose']['identifier']]);
1381
      }
1382
    }
1383

    
1384
    if ($status) {
1385
      if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
1386
        $_SESSION['views'][$this->view->name][$display_id] = array();
1387
      }
1388

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

    
1391
      if ($operator && isset($input[$this->options['expose']['operator_id']])) {
1392
        $session[$this->options['expose']['operator_id']] = $input[$this->options['expose']['operator_id']];
1393
      }
1394

    
1395
      $session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];
1396
    }
1397
  }
1398

    
1399
  /**
1400
   * Add this filter to the query.
1401
   *
1402
   * Due to the nature of fapi, the value and the operator have an unintended
1403
   * level of indirection. You will find them in $this->operator and
1404
   * $this->value respectively.
1405
   */
1406
  public function query() {
1407
    $this->ensure_my_table();
1408
    $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $this->value, $this->operator);
1409
  }
1410

    
1411
  /**
1412
   * Can this filter be used in OR groups?
1413
   *
1414
   * Some filters have complicated where clauses that cannot be easily used with
1415
   * OR groups. Some filters must also use HAVING which also makes them not
1416
   * groupable. These filters will end up in a special group if OR grouping is
1417
   * in use.
1418
   *
1419
   * @return bool
1420
   *   Whether the filter can be used in OR groups.
1421
   */
1422
   public function can_group() {
1423
     return TRUE;
1424
   }
1425

    
1426
}
1427

    
1428

    
1429
/**
1430
 * A special handler to take the place of missing or broken handlers.
1431
 *
1432
 * @ingroup views_filter_handlers
1433
 */
1434
class views_handler_filter_broken extends views_handler_filter {
1435

    
1436
  /**
1437
   * {@inheritdoc}
1438
   */
1439
  public function ui_name($short = FALSE) {
1440
    return t('Broken/missing handler');
1441
  }
1442

    
1443
  /**
1444
   * {@inheritdoc}
1445
   */
1446
  public function ensure_my_table() {
1447
    // No table to ensure!
1448
  }
1449

    
1450
  /**
1451
   * {@inheritdoc}
1452
   */
1453
  public function query($group_by = FALSE) {
1454
    // No query to run.
1455
  }
1456

    
1457
  /**
1458
   * {@inheritdoc}
1459
   */
1460
  public function options_form(&$form, &$form_state) {
1461
    $form['markup'] = array(
1462
      '#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>',
1463
    );
1464
  }
1465

    
1466
  /**
1467
   * {@inheritdoc}
1468
   */
1469
  public function broken() {
1470
    return TRUE;
1471
  }
1472

    
1473
}
1474

    
1475
/**
1476
 * Filter by no empty values, though allow to use "0".
1477
 *
1478
 * @param string $var
1479
 *
1480
 * @return bool
1481
 */
1482
function _views_array_filter_zero($var) {
1483
  return trim($var) != "";
1484
}
1485

    
1486

    
1487
/**
1488
 * @}
1489
 */