Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter.inc @ 6eb8d15f

1
<?php
2

    
3
/**
4
 * @file
5
 * @todo.
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
   * Contains the actual value of the field,either configured in the views ui
37
   * or entered in the exposed filters.
38
   */
39
  var $value = NULL;
40

    
41
  /**
42
   * Contains the operator which is used on the query.
43
   */
44
  var $operator = '=';
45

    
46
  /**
47
   * Contains the information of the selected item in a gruped filter.
48
   */
49
  var $group_info = NULL;
50

    
51
  /**
52
   * @var bool
53
   * Disable the possibility to force a single value.
54
   */
55
  var $always_multiple = FALSE;
56

    
57
  /**
58
   * @var bool
59
   * Disable the possibility to use operators.
60
   */
61
  var $no_operator = FALSE;
62

    
63
  /**
64
   * @var bool
65
   * Disable the possibility to allow a exposed input to be optional.
66
   */
67
  var $always_required = FALSE;
68

    
69
  /**
70
   * Provide some extra help to get the operator/value easier to use.
71
   *
72
   * This likely has to be overridden by filters which are more complex
73
   * than simple operator/value.
74
   */
75
  function init(&$view, &$options) {
76
    parent::init($view, $options);
77

    
78
    $this->operator = $this->options['operator'];
79
    $this->value = $this->options['value'];
80
    $this->group_info = $this->options['group_info']['default_group'];
81

    
82
    // Compatibility: The new UI changed several settings.
83
    if (!empty($options['exposed']) && !empty($options['expose']['optional']) && !isset($options['expose']['required'])) {
84
      $this->options['expose']['required'] = !$options['expose']['optional'];
85
    }
86
    if (!empty($options['exposed']) && !empty($options['expose']['single']) && !isset($options['expose']['multiple'])) {
87
      $this->options['expose']['multiple'] = !$options['expose']['single'];
88
    }
89
    if (!empty($options['exposed']) && !empty($options['expose']['operator']) && !isset($options['expose']['operator_id'])) {
90
      $this->options['expose']['operator_id'] = $options['expose']['operator_id'] = $options['expose']['operator'];
91
    }
92

    
93
    if ($this->multiple_exposed_input()) {
94
      $this->group_info = NULL;
95
      if (!empty($options['group_info']['default_group_multiple'])) {
96
        $this->group_info = array_filter($options['group_info']['default_group_multiple']);
97
      }
98

    
99
      $this->options['expose']['multiple'] = TRUE;
100
    }
101

    
102
    // If there are relationships in the view, allow empty should be true
103
    // so that we can do IS NULL checks on items. Not all filters respect
104
    // allow empty, but string and numeric do and that covers enough.
105
    if ($this->view->display_handler->get_option('relationships')) {
106
      $this->definition['allow empty'] = TRUE;
107
    }
108
  }
109

    
110
  function option_definition() {
111
    $options = parent::option_definition();
112

    
113
    $options['operator'] = array('default' => '=');
114
    $options['value'] = array('default' => '');
115
    $options['group'] = array('default' => '1');
116
    $options['exposed'] = array('default' => FALSE, 'bool' => TRUE);
117
    $options['expose'] = array(
118
      'contains' => array(
119
        'operator_id' => array('default' => FALSE),
120
        'label' => array('default' => '', 'translatable' => TRUE),
121
        'description' => array('default' => '', 'translatable' => TRUE),
122
        'use_operator' => array('default' => FALSE, 'bool' => TRUE),
123
        'operator_label' => array('default' => '', 'translatable' => TRUE),
124
        'operator' => array('default' => ''),
125
        'identifier' => array('default' => ''),
126
        'required' => array('default' => FALSE, 'bool' => TRUE),
127
        'remember' => array('default' => FALSE, 'bool' => TRUE),
128
        'multiple' => array('default' => FALSE, 'bool' => TRUE),
129
        'remember_roles' => array('default' => array(
130
          DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
131
        )),
132
      ),
133
    );
134

    
135
    // A group is a combination of a filter, an operator and a value
136
    // operating like a single filter.
137
    // Users can choose from a select box which group they want to apply.
138
    // Views will filter the view according to the defined values.
139
    // Because it acts as a standard filter, we have to define
140
    // an identifier and other settings like the widget and the label.
141
    // This settings are saved in another array to allow users to switch
142
    // between a normal filter and a group of filters with a single click.
143
    $options['is_grouped'] = array('default' => FALSE, 'bool' => TRUE);
144
    $options['group_info'] = array(
145
      'contains' => array(
146
        'label' => array('default' => '', 'translatable' => TRUE),
147
        'description' => array('default' => '', 'translatable' => TRUE),
148
        'identifier' => array('default' => ''),
149
        'optional' => array('default' => TRUE, 'bool' => TRUE),
150
        'widget' => array('default' => 'select'),
151
        'multiple' => array('default' => FALSE, 'bool' => TRUE),
152
        'remember' => array('default' => 0),
153
        'default_group' => array('default' => 'All'),
154
        'default_group_multiple' => array('default' => array()),
155
        'group_items' => array('default' => array()),
156
      ),
157
    );
158

    
159
    return $options;
160
  }
161

    
162
  /**
163
   * Display the filter on the administrative summary
164
   */
165
  function admin_summary() {
166
    return check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
167
  }
168

    
169
  /**
170
   * Determine if a filter can be exposed.
171
   */
172
  function can_expose() { return TRUE; }
173

    
174
  /**
175
   * Determine if a filter can be converted into a group.
176
   * Only exposed filters with operators available can be converted into groups.
177
   */
178
  function can_build_group() {
179
    return $this->is_exposed() && (count($this->operator_options()) > 0);
180
  }
181

    
182
  /**
183
   * Returns TRUE if the exposed filter works like a grouped filter.
184
   */
185
  function is_a_group() {
186
    return $this->is_exposed() && !empty($this->options['is_grouped']);
187
  }
188

    
189
  /**
190
   * Provide the basic form which calls through to subforms.
191
   * If overridden, it is best to call through to the parent,
192
   * or to at least make sure all of the functions in this form
193
   * are called.
194
   */
195
  function options_form(&$form, &$form_state) {
196
    parent::options_form($form, $form_state);
197
    if ($this->can_expose()) {
198
      $this->show_expose_button($form, $form_state);
199
    }
200
    if ($this->can_build_group()) {
201
      $this->show_build_group_button($form, $form_state);
202
    }
203
    $form['clear_markup_start'] = array(
204
      '#markup' => '<div class="clearfix">',
205
    );
206
    if ($this->is_a_group()) {
207
      if ($this->can_build_group()) {
208
        $form['clear_markup_start'] = array(
209
          '#markup' => '<div class="clearfix">',
210
        );
211
        // Render the build group form.
212
        $this->show_build_group_form($form, $form_state);
213
        $form['clear_markup_end'] = array(
214
          '#markup' => '</div>',
215
        );
216
      }
217
    }
218
    else {
219
      // Add the subform from operator_form().
220
      $this->show_operator_form($form, $form_state);
221
      // Add the subform from value_form().
222
      $this->show_value_form($form, $form_state);
223
      $form['clear_markup_end'] = array(
224
        '#markup' => '</div>',
225
      );
226
      if ($this->can_expose()) {
227
        // Add the subform from expose_form().
228
        $this->show_expose_form($form, $form_state);
229
      }
230
    }
231
  }
232

    
233
  /**
234
   * Simple validate handler
235
   */
236
  function options_validate(&$form, &$form_state) {
237
    $this->operator_validate($form, $form_state);
238
    $this->value_validate($form, $form_state);
239
    if (!empty($this->options['exposed']) && !$this->is_a_group()) {
240
      $this->expose_validate($form, $form_state);
241
    }
242
    if ($this->is_a_group()) {
243
      $this->build_group_validate($form, $form_state);
244
    }
245
  }
246

    
247
  /**
248
   * Simple submit handler
249
   */
250
  function options_submit(&$form, &$form_state) {
251
    unset($form_state['values']['expose_button']); // don't store this.
252
    unset($form_state['values']['group_button']); // don't store this.
253
    if (!$this->is_a_group()) {
254
      $this->operator_submit($form, $form_state);
255
      $this->value_submit($form, $form_state);
256
    }
257
    if (!empty($this->options['exposed'])) {
258
      $this->expose_submit($form, $form_state);
259
    }
260
    if ($this->is_a_group()) {
261
      $this->build_group_submit($form, $form_state);
262
    }
263
  }
264

    
265
  /**
266
   * Shortcut to display the operator form.
267
   */
268
  function show_operator_form(&$form, &$form_state) {
269
    $this->operator_form($form, $form_state);
270
    $form['operator']['#prefix'] = '<div class="views-group-box views-left-30">';
271
    $form['operator']['#suffix'] = '</div>';
272
  }
273

    
274
  /**
275
   * Options form subform for setting the operator.
276
   *
277
   * This may be overridden by child classes, and it must
278
   * define $form['operator'];
279
   *
280
   * @see options_form()
281
   */
282
  function operator_form(&$form, &$form_state) {
283
    $options = $this->operator_options();
284
    if (!empty($options)) {
285
      $form['operator'] = array(
286
        '#type' => count($options) < 10 ? 'radios' : 'select',
287
        '#title' => t('Operator'),
288
        '#default_value' => $this->operator,
289
        '#options' => $options,
290
      );
291
    }
292
  }
293

    
294
  /**
295
   * Provide a list of options for the default operator form.
296
   * Should be overridden by classes that don't override operator_form
297
   */
298
  function operator_options() { return array(); }
299

    
300
  /**
301
   * Validate the operator form.
302
   */
303
  function operator_validate($form, &$form_state) { }
304

    
305
  /**
306
   * Perform any necessary changes to the form values prior to storage.
307
   * There is no need for this function to actually store the data.
308
   */
309
  function operator_submit($form, &$form_state) { }
310

    
311
  /**
312
   * Shortcut to display the value form.
313
   */
314
  function show_value_form(&$form, &$form_state) {
315
    $this->value_form($form, $form_state);
316
    if (empty($this->no_operator)) {
317
      $form['value']['#prefix'] = '<div class="views-group-box views-right-70">' . (isset($form['value']['#prefix']) ? $form['value']['#prefix'] : '');
318
      $form['value']['#suffix'] = (isset($form['value']['#suffix']) ? $form['value']['#suffix'] : '') . '</div>';
319
    }
320
  }
321

    
322
  /**
323
   * Options form subform for setting options.
324
   *
325
   * This should be overridden by all child classes and it must
326
   * define $form['value']
327
   *
328
   * @see options_form()
329
   */
330
  function value_form(&$form, &$form_state) { $form['value'] = array(); }
331

    
332
  /**
333
   * Validate the options form.
334
   */
335
  function value_validate($form, &$form_state) { }
336

    
337
  /**
338
   * Perform any necessary changes to the form values prior to storage.
339
   * There is no need for this function to actually store the data.
340
   */
341
  function value_submit($form, &$form_state) { }
342

    
343
  /**
344
   * Shortcut to display the exposed options form.
345
   */
346
  function show_build_group_form(&$form, &$form_state) {
347
    if (empty($this->options['is_grouped'])) {
348
      return;
349
    }
350

    
351
    $this->build_group_form($form, $form_state);
352

    
353
    // When we click the expose button, we add new gadgets to the form but they
354
    // have no data in $_POST so their defaults get wiped out. This prevents
355
    // these defaults from getting wiped out. This setting will only be TRUE
356
    // during a 2nd pass rerender.
357
    if (!empty($form_state['force_build_group_options'])) {
358
      foreach (element_children($form['group_info']) as $id) {
359
        if (isset($form['group_info'][$id]['#default_value']) && !isset($form['group_info'][$id]['#value'])) {
360
          $form['group_info'][$id]['#value'] = $form['group_info'][$id]['#default_value'];
361
        }
362
      }
363
    }
364
  }
365

    
366
  /**
367
   * Shortcut to display the build_group/hide button.
368
   */
369
  function show_build_group_button(&$form, &$form_state) {
370

    
371
    $form['group_button'] = array(
372
      '#prefix' => '<div class="views-grouped clearfix">',
373
      '#suffix' => '</div>',
374
      // Should always come after the description and the relationship.
375
      '#weight' => -190,
376
    );
377

    
378
    $grouped_description = t('Grouped filters allow a choice between predefined operator|value pairs.');
379
    $form['group_button']['radios'] = array(
380
      '#theme_wrappers' => array('container'),
381
      '#attributes' => array('class' => array('js-only')),
382
    );
383
    $form['group_button']['radios']['radios'] = array(
384
      '#title' => t('Filter type to expose'),
385
      '#description' => $grouped_description,
386
      '#type' => 'radios',
387
      '#options' => array(
388
        t('Single filter'),
389
        t('Grouped filters'),
390
      ),
391
    );
392

    
393
    if (empty($this->options['is_grouped'])) {
394
      $form['group_button']['markup'] = array(
395
        '#markup' => '<div class="description grouped-description">' . $grouped_description . '</div>',
396
      );
397
      $form['group_button']['button'] = array(
398
        '#limit_validation_errors' => array(),
399
        '#type' => 'submit',
400
        '#value' => t('Grouped filters'),
401
        '#submit' => array('views_ui_config_item_form_build_group'),
402
      );
403
      $form['group_button']['radios']['radios']['#default_value'] = 0;
404
    }
405
    else {
406
      $form['group_button']['button'] = array(
407
        '#limit_validation_errors' => array(),
408
        '#type' => 'submit',
409
        '#value' => t('Single filter'),
410
        '#submit' => array('views_ui_config_item_form_build_group'),
411
      );
412
      $form['group_button']['radios']['radios']['#default_value'] = 1;
413
    }
414
  }
415
  /**
416
   * Shortcut to display the expose/hide button.
417
   */
418
  function show_expose_button(&$form, &$form_state) {
419
    $form['expose_button'] = array(
420
      '#prefix' => '<div class="views-expose clearfix">',
421
      '#suffix' => '</div>',
422
      // Should always come after the description and the relationship.
423
      '#weight' => -200,
424
    );
425

    
426
    // Add a checkbox for JS users, which will have behavior attached to it
427
    // so it can replace the button.
428
    $form['expose_button']['checkbox'] = array(
429
      '#theme_wrappers' => array('container'),
430
      '#attributes' => array('class' => array('js-only')),
431
    );
432
    $form['expose_button']['checkbox']['checkbox'] = array(
433
      '#title' => t('Expose this filter to visitors, to allow them to change it'),
434
      '#type' => 'checkbox',
435
    );
436

    
437
    // Then add the button itself.
438
    if (empty($this->options['exposed'])) {
439
      $form['expose_button']['markup'] = array(
440
        '#markup' => '<div class="description exposed-description">' . t('This filter is not exposed. Expose it to allow the users to change it.') . '</div>',
441
      );
442
      $form['expose_button']['button'] = array(
443
        '#limit_validation_errors' => array(),
444
        '#type' => 'submit',
445
        '#value' => t('Expose filter'),
446
        '#submit' => array('views_ui_config_item_form_expose'),
447
      );
448
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
449
    }
450
    else {
451
      $form['expose_button']['markup'] = array(
452
        '#markup' => '<div class="description exposed-description">' . t('This filter is exposed. If you hide it, users will not be able to change it.') . '</div>',
453
      );
454
      $form['expose_button']['button'] = array(
455
        '#limit_validation_errors' => array(),
456
        '#type' => 'submit',
457
        '#value' => t('Hide filter'),
458
        '#submit' => array('views_ui_config_item_form_expose'),
459
      );
460
      $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
461
    }
462
  }
463

    
464
  /**
465
   * Options form subform for exposed filter options.
466
   *
467
   * @see options_form()
468
   */
469
  function expose_form(&$form, &$form_state) {
470
    $form['#theme'] = 'views_ui_expose_filter_form';
471
    // #flatten will move everything from $form['expose'][$key] to $form[$key]
472
    // prior to rendering. That's why the pre_render for it needs to run first,
473
    // so that when the next pre_render (the one for fieldsets) runs, it gets
474
    // the flattened data.
475
    array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
476
    $form['expose']['#flatten'] = TRUE;
477

    
478
    if (empty($this->always_required)) {
479
      $form['expose']['required'] = array(
480
        '#type' => 'checkbox',
481
        '#title' => t('Required'),
482
        '#default_value' => $this->options['expose']['required'],
483
      );
484
    }
485
    else {
486
      $form['expose']['required'] = array(
487
        '#type' => 'value',
488
        '#value' => TRUE,
489
      );
490
    }
491
    $form['expose']['label'] = array(
492
      '#type' => 'textfield',
493
      '#default_value' => $this->options['expose']['label'],
494
      '#title' => t('Label'),
495
      '#size' => 40,
496
    );
497

    
498
    $form['expose']['description'] = array(
499
      '#type' => 'textfield',
500
      '#default_value' => $this->options['expose']['description'],
501
      '#title' => t('Description'),
502
      '#size' => 60,
503
    );
504

    
505
    if (!empty($form['operator']['#type'])) {
506
       // Increase the width of the left (operator) column.
507
      $form['operator']['#prefix'] = '<div class="views-group-box views-left-40">';
508
      $form['operator']['#suffix'] = '</div>';
509
      $form['value']['#prefix'] = '<div class="views-group-box views-right-60">';
510
      $form['value']['#suffix'] = '</div>';
511

    
512
      $form['expose']['use_operator'] = array(
513
        '#type' => 'checkbox',
514
        '#title' => t('Expose operator'),
515
        '#description' => t('Allow the user to choose the operator.'),
516
        '#default_value' => !empty($this->options['expose']['use_operator']),
517
      );
518
      $form['expose']['operator_label'] = array(
519
        '#type' => 'textfield',
520
        '#default_value' => $this->options['expose']['operator_label'],
521
        '#title' => t('Operator label'),
522
        '#size' => 40,
523
        '#description' => t('This will appear before your operator select field.'),
524
        '#dependency' => array(
525
          'edit-options-expose-use-operator' => array(1)
526
        ),
527
      );
528
      $form['expose']['operator_id'] = array(
529
        '#type' => 'textfield',
530
        '#default_value' => $this->options['expose']['operator_id'],
531
        '#title' => t('Operator identifier'),
532
        '#size' => 40,
533
        '#description' => t('This will appear in the URL after the ? to identify this operator.'),
534
        '#dependency' => array(
535
          'edit-options-expose-use-operator' => array(1)
536
        ),
537
        '#fieldset' => 'more',
538
      );
539
    }
540
    else {
541
      $form['expose']['operator_id'] = array(
542
        '#type' => 'value',
543
        '#value' => '',
544
      );
545
    }
546

    
547
    if (empty($this->always_multiple)) {
548
      $form['expose']['multiple'] = array(
549
        '#type' => 'checkbox',
550
        '#title' => t('Allow multiple selections'),
551
        '#description' => t('Enable to allow users to select multiple items.'),
552
        '#default_value' => $this->options['expose']['multiple'],
553
      );
554
    }
555
    $form['expose']['remember'] = array(
556
      '#type' => 'checkbox',
557
      '#title' => t('Remember the last selection'),
558
      '#description' => t('Enable to remember the last selection made by the user.'),
559
      '#default_value' => $this->options['expose']['remember'],
560
    );
561

    
562
    $role_options = array_map('check_plain', user_roles());
563
    $form['expose']['remember_roles'] = array(
564
      '#type' => 'checkboxes',
565
      '#title' => t('User roles'),
566
      '#description' => t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
567
      '#default_value' => $this->options['expose']['remember_roles'],
568
      '#options' => $role_options,
569
      '#dependency' =>  array(
570
        'edit-options-expose-remember' => array(1),
571
      ),
572
    );
573

    
574
    $form['expose']['identifier'] = array(
575
      '#type' => 'textfield',
576
      '#default_value' => $this->options['expose']['identifier'],
577
      '#title' => t('Filter identifier'),
578
      '#size' => 40,
579
      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
580
      '#fieldset' => 'more',
581
    );
582
  }
583

    
584
  /**
585
   * Validate the options form.
586
   */
587
  function expose_validate($form, &$form_state) {
588
    if (empty($form_state['values']['options']['expose']['identifier'])) {
589
      form_error($form['expose']['identifier'], t('The identifier is required if the filter is exposed.'));
590
    }
591

    
592
    if (!empty($form_state['values']['options']['expose']['identifier']) && $form_state['values']['options']['expose']['identifier'] == 'value') {
593
      form_error($form['expose']['identifier'], t('This identifier is not allowed.'));
594
    }
595

    
596
    if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['expose']['identifier'])) {
597
      form_error($form['expose']['identifier'], t('This identifier is used by another handler.'));
598
    }
599
  }
600

    
601
   /**
602
   * Validate the build group options form.
603
   */
604
  function build_group_validate($form, &$form_state) {
605
    if (!empty($form_state['values']['options']['group_info'])) {
606
      if (empty($form_state['values']['options']['group_info']['identifier'])) {
607
        form_error($form['group_info']['identifier'], t('The identifier is required if the filter is exposed.'));
608
      }
609

    
610
      if (!empty($form_state['values']['options']['group_info']['identifier']) && $form_state['values']['options']['group_info']['identifier'] == 'value') {
611
        form_error($form['group_info']['identifier'], t('This identifier is not allowed.'));
612
      }
613

    
614
      if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['group_info']['identifier'])) {
615
        form_error($form['group_info']['identifier'], t('This identifier is used by another handler.'));
616
      }
617
    }
618

    
619
    if (!empty($form_state['values']['options']['group_info']['group_items'])) {
620
      foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
621
        if (empty($group['remove'])) {
622

    
623
          // Check if the title is defined but value wasn't defined.
624
          if (!empty($group['title'])) {
625
            if ((!is_array($group['value']) && trim($group['value']) == "") ||
626
                (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) == 0)) {
627
              form_error($form['group_info']['group_items'][$id]['value'],
628
                         t('The value is required if title for this item is defined.'));
629
            }
630
          }
631

    
632
          // Check if the value is defined but title wasn't defined.
633
          if ((!is_array($group['value']) && trim($group['value']) != "") ||
634
              (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) > 0)) {
635
            if (empty($group['title'])) {
636
              form_error($form['group_info']['group_items'][$id]['title'],
637
                         t('The title is required if value for this item is defined.'));
638
            }
639
          }
640
        }
641
      }
642
    }
643
  }
644

    
645
  /**
646
   * Save new group items, re-enumerates and remove groups marked to delete.
647
   */
648
  function build_group_submit($form, &$form_state) {
649
    $groups = array();
650
    uasort($form_state['values']['options']['group_info']['group_items'], 'drupal_sort_weight');
651
    // Filter out removed items.
652

    
653
    // Start from 1 to avoid problems with #default_value in the widget.
654
    $new_id = 1;
655
    $new_default = 'All';
656
    foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
657
      if (empty($group['remove'])) {
658
        // Don't store this.
659
        unset($group['remove']);
660
        unset($group['weight']);
661
        $groups[$new_id] = $group;
662

    
663
        if ($form_state['values']['options']['group_info']['default_group'] === $id) {
664
          $new_default = $new_id;
665
        }
666
      }
667
      $new_id++;
668
    }
669
    if ($new_default != 'All') {
670
      $form_state['values']['options']['group_info']['default_group'] = $new_default;
671
    }
672
    $filter_default_multiple = array_filter($form_state['values']['options']['group_info']['default_group_multiple']);
673
    $form_state['values']['options']['group_info']['default_group_multiple'] = $filter_default_multiple;
674

    
675
    $form_state['values']['options']['group_info']['group_items'] = $groups;
676
  }
677

    
678
  /**
679
   * Provide default options for exposed filters.
680
   */
681
  function expose_options() {
682
    $this->options['expose'] = array(
683
      'use_operator' => FALSE,
684
      'operator' => $this->options['id'] . '_op',
685
      'identifier' => $this->options['id'],
686
      'label' => $this->definition['title'],
687
      'description' => NULL,
688
      'remember' => FALSE,
689
      'multiple' => FALSE,
690
      'required' => FALSE,
691
    );
692
  }
693

    
694
   /**
695
   * Provide default options for exposed filters.
696
   */
697
  function build_group_options() {
698
    $this->options['group_info'] = array(
699
      'label' => $this->definition['title'],
700
      'description' => NULL,
701
      'identifier' => $this->options['id'],
702
      'optional' => TRUE,
703
      'widget' => 'select',
704
      'multiple' => FALSE,
705
      'remember' => FALSE,
706
      'default_group' => 'All',
707
      'default_group_multiple' => array(),
708
      'group_items' => array(),
709
    );
710
  }
711

    
712
  /**
713
   * Build a form containing a group of operator | values to apply as a
714
   * single filter.
715
   */
716
  function group_form(&$form, &$form_state) {
717
    if (!empty($this->options['group_info']['optional']) && !$this->multiple_exposed_input()) {
718

    
719
      $old_any = $this->options['group_info']['widget'] == 'select' ? '<Any>' : '&lt;Any&gt;';
720
      $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? $old_any : t('- Any -');
721
      $groups = array('All' => $any_label);
722
    }
723
    foreach ($this->options['group_info']['group_items'] as $id => $group) {
724
      if (!empty($group['title'])) {
725
        $groups[$id] = $id != 'All' ? t($group['title']) : $group['title'];
726
      }
727
    }
728

    
729
    if (count($groups)) {
730
      $value = $this->options['group_info']['identifier'];
731

    
732
      $form[$value] = array(
733
        '#type' => $this->options['group_info']['widget'],
734
        '#default_value' => $this->group_info,
735
        '#options' => $groups,
736
      );
737
      if (!empty($this->options['group_info']['multiple'])) {
738
        if (count($groups) < 5) {
739
          $form[$value]['#type'] = 'checkboxes';
740
        }
741
        else {
742
          $form[$value]['#type'] = 'select';
743
          $form[$value]['#size'] = 5;
744
          $form[$value]['#multiple'] = TRUE;
745
        }
746
        unset($form[$value]['#default_value']);
747
        if (empty($form_state['input'])) {
748
          $form_state['input'][$value] = $this->group_info;
749
        }
750
      }
751

    
752
      $this->options['expose']['label'] = '';
753
    }
754
  }
755

    
756

    
757
  /**
758
   * Render our chunk of the exposed filter form when selecting
759
   *
760
   * You can override this if it doesn't do what you expect.
761
   */
762
  function exposed_form(&$form, &$form_state) {
763
    if (empty($this->options['exposed'])) {
764
      return;
765
    }
766

    
767
    // Build the exposed form, when its based on an operator.
768
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
769
      $operator = $this->options['expose']['operator_id'];
770
      $this->operator_form($form, $form_state);
771
      $form[$operator] = $form['operator'];
772
      $form[$operator]['#title'] =  $this->options['expose']['operator_label'];
773
      $form[$operator]['#title_display'] = 'invisible';
774

    
775
      $this->exposed_translate($form[$operator], 'operator');
776

    
777
      unset($form['operator']);
778
    }
779

    
780
    // Build the form and set the value based on the identifier.
781
    if (!empty($this->options['expose']['identifier'])) {
782
      $value = $this->options['expose']['identifier'];
783
      $this->value_form($form, $form_state);
784
      $form[$value] = $form['value'];
785

    
786
      if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
787
        unset($form[$value]['#title']);
788
      }
789

    
790
      $this->exposed_translate($form[$value], 'value');
791

    
792
      if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
793
        unset($form[$value]['#default_value']);
794
      }
795

    
796
      if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
797
        $form[$value]['#default_value'] = 'All';
798
      }
799

    
800
      if ($value != 'value') {
801
        unset($form['value']);
802
      }
803
    }
804
  }
805

    
806
  /**
807
   * Build the form to let users create the group of exposed filters.
808
   * This form is displayed when users click on button 'Build group'
809
   */
810
  function build_group_form(&$form, &$form_state) {
811
    if (empty($this->options['exposed']) || empty($this->options['is_grouped'])) {
812
      return;
813
    }
814
    $form['#theme'] = 'views_ui_build_group_filter_form';
815

    
816
    // #flatten will move everything from $form['group_info'][$key] to $form[$key]
817
    // prior to rendering. That's why the pre_render for it needs to run first,
818
    // so that when the next pre_render (the one for fieldsets) runs, it gets
819
    // the flattened data.
820
    array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
821
    $form['group_info']['#flatten'] = TRUE;
822

    
823
    if (!empty($this->options['group_info']['identifier'])) {
824
      $identifier = $this->options['group_info']['identifier'];
825
    }
826
    else {
827
      $identifier = 'group_' . $this->options['expose']['identifier'];
828
    }
829
    $form['group_info']['identifier'] = array(
830
      '#type' => 'textfield',
831
      '#default_value' => $identifier,
832
      '#title' => t('Filter identifier'),
833
      '#size' => 40,
834
      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
835
      '#fieldset' => 'more',
836
    );
837
    $form['group_info']['label'] = array(
838
      '#type' => 'textfield',
839
      '#default_value' => $this->options['group_info']['label'],
840
      '#title' => t('Label'),
841
      '#size' => 40,
842
    );
843

    
844
    $form['group_info']['optional'] = array(
845
      '#type' => 'checkbox',
846
      '#title' => t('Optional'),
847
      '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
848
      '#default_value' => $this->options['group_info']['optional'],
849
    );
850
    $form['group_info']['multiple'] = array(
851
      '#type' => 'checkbox',
852
      '#title' => t('Allow multiple selections'),
853
      '#description' => t('Enable to allow users to select multiple items.'),
854
      '#default_value' => $this->options['group_info']['multiple'],
855
    );
856
    $form['group_info']['widget'] = array(
857
      '#type' => 'radios',
858
      '#default_value' => $this->options['group_info']['widget'],
859
      '#title' => t('Widget type'),
860
      '#options' => array(
861
        'radios' => t('Radios'),
862
        'select' => t('Select'),
863
      ),
864
      '#description' => t('Select which kind of widget will be used to render the group of filters'),
865
    );
866
    $form['group_info']['remember'] = array(
867
      '#type' => 'checkbox',
868
      '#title' => t('Remember'),
869
      '#description' => t('Remember the last setting the user gave this filter.'),
870
      '#default_value' => $this->options['group_info']['remember'],
871
    );
872

    
873
    if (!empty($this->options['group_info']['identifier'])) {
874
      $identifier = $this->options['group_info']['identifier'];
875
    }
876
    else {
877
      $identifier = 'group_' . $this->options['expose']['identifier'];
878
    }
879
    $form['group_info']['identifier'] = array(
880
      '#type' => 'textfield',
881
      '#default_value' => $identifier,
882
      '#title' => t('Filter identifier'),
883
      '#size' => 40,
884
      '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
885
      '#fieldset' => 'more',
886
    );
887
    $form['group_info']['label'] = array(
888
      '#type' => 'textfield',
889
      '#default_value' => $this->options['group_info']['label'],
890
      '#title' => t('Label'),
891
      '#size' => 40,
892
    );
893
    $form['group_info']['description'] = array(
894
      '#type' => 'textfield',
895
      '#default_value' => $this->options['group_info']['description'],
896
      '#title' => t('Description'),
897
      '#size' => 60,
898
    );
899
    $form['group_info']['optional'] = array(
900
      '#type' => 'checkbox',
901
      '#title' => t('Optional'),
902
      '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
903
      '#default_value' => $this->options['group_info']['optional'],
904
    );
905
    $form['group_info']['widget'] = array(
906
      '#type' => 'radios',
907
      '#default_value' => $this->options['group_info']['widget'],
908
      '#title' => t('Widget type'),
909
      '#options' => array(
910
        'radios' => t('Radios'),
911
        'select' => t('Select'),
912
      ),
913
      '#description' => t('Select which kind of widget will be used to render the group of filters'),
914
    );
915
    $form['group_info']['remember'] = array(
916
      '#type' => 'checkbox',
917
      '#title' => t('Remember'),
918
      '#description' => t('Remember the last setting the user gave this filter.'),
919
      '#default_value' => $this->options['group_info']['remember'],
920
    );
921

    
922
    $groups = array('All' => '- Any -'); // The string '- Any -' will not be rendered see @theme_views_ui_build_group_filter_form
923

    
924
    // Provide 3 options to start when we are in a new group.
925
    if (count($this->options['group_info']['group_items']) == 0) {
926
      $this->options['group_info']['group_items'] = array_fill(1, 3, array());
927
    }
928

    
929
    // After the general settings, comes a table with all the existent groups.
930
    $default_weight = 0;
931
    foreach ($this->options['group_info']['group_items'] as $item_id => $item) {
932
      if (!empty($form_state['values']['options']['group_info']['group_items'][$item_id]['remove'])) {
933
        continue;
934
      }
935
      // Each rows contains three widgets:
936
      // a) The title, where users define how they identify a pair of operator | value
937
      // b) The operator
938
      // c) The value (or values) to use in the filter with the selected operator
939

    
940
      // In each row, we have to display the operator form and the value from
941
      // $row acts as a fake form to render each widget in a row.
942
      $row = array();
943
      $groups[$item_id] = '';
944
      $this->operator_form($row, $form_state);
945
      // Force the operator form to be a select box. Some handlers uses
946
      // radios and they occupy a lot of space in a table row.
947
      $row['operator']['#type'] = 'select';
948
      $row['operator']['#title'] = '';
949
      $this->value_form($row, $form_state);
950

    
951
      // Fix the dependencies to update value forms when operators
952
      // changes. This is needed because forms are inside a new form and
953
      // their ids changes. Dependencies are used when operator changes
954
      // from to 'Between', 'Not Between', etc, and two or more widgets
955
      // are displayed.
956
      $without_children = TRUE;
957
      foreach (element_children($row['value']) as $children) {
958
        if (isset($row['value'][$children]['#dependency']['edit-options-operator'])) {
959
          $row['value'][$children]['#dependency']["edit-options-group-info-group-items-$item_id-operator"] = $row['value'][$children]['#dependency']['edit-options-operator'];
960
          unset($row['value'][$children]['#dependency']['edit-options-operator']);
961
          $row['value'][$children]['#title'] = '';
962

    
963
          if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$children])) {
964
            $row['value'][$children]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$children];
965
          }
966
        }
967
        $without_children = FALSE;
968
      }
969

    
970
      if ($without_children) {
971
        if (!empty($this->options['group_info']['group_items'][$item_id]['value'])) {
972
          $row['value']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'];
973
        }
974
      }
975

    
976
      if (!empty($this->options['group_info']['group_items'][$item_id]['operator'])) {
977
        $row['operator']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['operator'];
978
      }
979

    
980
      $default_title = '';
981
      if (!empty($this->options['group_info']['group_items'][$item_id]['title'])) {
982
        $default_title = $this->options['group_info']['group_items'][$item_id]['title'];
983
      }
984

    
985
      // Per item group, we have a title that identifies it.
986
      $form['group_info']['group_items'][$item_id] = array(
987
        'title' => array(
988
          '#type' => 'textfield',
989
          '#size' => 20,
990
          '#default_value' => $default_title,
991
        ),
992
        'operator' => $row['operator'],
993
        'value' => $row['value'],
994
        'remove' => array(
995
          '#type' => 'checkbox',
996
          '#id' => 'views-removed-' . $item_id,
997
          '#attributes' => array('class' => array('views-remove-checkbox')),
998
          '#default_value' => 0,
999
        ),
1000
        'weight' => array(
1001
          '#type' => 'weight',
1002
          '#delta' => 10,
1003
          '#default_value' => $default_weight++,
1004
          '#attributes' => array('class' => array('weight')),
1005
        ),
1006
      );
1007
    }
1008
    // From all groups, let chose which is the default.
1009
    $form['group_info']['default_group'] = array(
1010
      '#type' => 'radios',
1011
      '#options' => $groups,
1012
      '#default_value' => $this->options['group_info']['default_group'],
1013
      '#required' => TRUE,
1014
      '#attributes' => array(
1015
        'class' => array('default-radios'),
1016
      )
1017
    );
1018
    // From all groups, let chose which is the default.
1019
    $form['group_info']['default_group_multiple'] = array(
1020
      '#type' => 'checkboxes',
1021
      '#options' => $groups,
1022
      '#default_value' => $this->options['group_info']['default_group_multiple'],
1023
      '#attributes' => array(
1024
        'class' => array('default-checkboxes'),
1025
      )
1026
    );
1027

    
1028
    $form['group_info']['add_group'] = array(
1029
      '#prefix' => '<div class="views-build-group clear-block">',
1030
      '#suffix' => '</div>',
1031
      '#type' => 'submit',
1032
      '#value' => t('Add another item'),
1033
      '#submit' => array('views_ui_config_item_form_add_group'),
1034
    );
1035

    
1036
    $js = array();
1037
    $js['tableDrag']['views-filter-groups']['weight'][0] = array(
1038
      'target' => 'weight',
1039
      'source' => NULL,
1040
      'relationship' => 'sibling',
1041
      'action' => 'order',
1042
      'hidden' => TRUE,
1043
      'limit' => 0,
1044
    );
1045
    if (!empty($form_state['js settings']) && is_array($js)) {
1046
      $form_state['js settings'] = array_merge($form_state['js settings'], $js);
1047
    }
1048
    else {
1049
      $form_state['js settings'] = $js;
1050
    }
1051
  }
1052

    
1053

    
1054
  /**
1055
   * Make some translations to a form item to make it more suitable to
1056
   * exposing.
1057
   */
1058
  function exposed_translate(&$form, $type) {
1059
    if (!isset($form['#type'])) {
1060
      return;
1061
    }
1062

    
1063
    if ($form['#type'] == 'radios') {
1064
      $form['#type'] = 'select';
1065
    }
1066
    // Checkboxes don't work so well in exposed forms due to GET conversions.
1067
    if ($form['#type'] == 'checkboxes') {
1068
      if (empty($form['#no_convert']) || empty($this->options['expose']['multiple'])) {
1069
        $form['#type'] = 'select';
1070
      }
1071
      if (!empty($this->options['expose']['multiple'])) {
1072
        $form['#multiple'] = TRUE;
1073
      }
1074
    }
1075
    if (empty($this->options['expose']['multiple']) && isset($form['#multiple'])) {
1076
      unset($form['#multiple']);
1077
      $form['#size'] = NULL;
1078
    }
1079

    
1080
    // Cleanup in case the translated element's (radios or checkboxes) display value contains html.
1081
    if ($form['#type'] == 'select') {
1082
      $this->prepare_filter_select_options($form['#options']);
1083
    }
1084

    
1085
    if ($type == 'value' && empty($this->always_required) && empty($this->options['expose']['required']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
1086
      $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -');
1087
      $form['#options'] = array('All' => $any_label) + $form['#options'];
1088
      $form['#default_value'] = 'All';
1089
    }
1090

    
1091
    if (!empty($this->options['expose']['required'])) {
1092
      $form['#required'] = TRUE;
1093
    }
1094
  }
1095

    
1096

    
1097

    
1098
  /**
1099
   * Sanitizes the HTML select element's options.
1100
   *
1101
   * The function is recursive to support optgroups.
1102
   */
1103
  function prepare_filter_select_options(&$options) {
1104
    foreach ($options as $value => $label) {
1105
      // Recurse for optgroups.
1106
      if (is_array($label)) {
1107
        $this->prepare_filter_select_options($options[$value]);
1108
      }
1109
      // FAPI has some special value to allow hierarchy.
1110
      // @see _form_options_flatten
1111
      elseif (is_object($label)) {
1112
        $this->prepare_filter_select_options($options[$value]->option);
1113
      }
1114
      else {
1115
        $options[$value] = strip_tags(decode_entities($label));
1116
      }
1117
    }
1118
  }
1119

    
1120
  /**
1121
   * Tell the renderer about our exposed form. This only needs to be
1122
   * overridden for particularly complex forms. And maybe not even then.
1123
   *
1124
   * @return array|null
1125
   *   For standard exposed filters. An array with the following keys:
1126
   *   - operator: The $form key of the operator. Set to NULL if no operator.
1127
   *   - value: The $form key of the value. Set to NULL if no value.
1128
   *   - label: The label to use for this piece.
1129
   *   For grouped exposed filters. An array with the following keys:
1130
   *   - value: The $form key of the value. Set to NULL if no value.
1131
   *   - label: The label to use for this piece.
1132
   */
1133
  function exposed_info() {
1134
    if (empty($this->options['exposed'])) {
1135
      return;
1136
    }
1137

    
1138
    if ($this->is_a_group()) {
1139
      return array(
1140
        'value' => $this->options['group_info']['identifier'],
1141
        'label' => $this->options['group_info']['label'],
1142
        'description' => $this->options['group_info']['description'],
1143
      );
1144
    }
1145

    
1146
    return array(
1147
      'operator' => $this->options['expose']['operator_id'],
1148
      'value' => $this->options['expose']['identifier'],
1149
      'label' => $this->options['expose']['label'],
1150
      'description' => $this->options['expose']['description'],
1151
    );
1152
  }
1153

    
1154
  /*
1155
   * Transform the input from a grouped filter into a standard filter.
1156
   *
1157
   * When a filter is a group, find the set of operator and values
1158
   * that the choosed item represents, and inform views that a normal
1159
   * filter was submitted by telling the operator and the value selected.
1160
   *
1161
   * The param $selected_group_id is only passed when the filter uses the
1162
   * checkboxes widget, and this function will be called for each item
1163
   * choosed in the checkboxes.
1164
   */
1165
  function convert_exposed_input(&$input, $selected_group_id = NULL) {
1166
    if ($this->is_a_group()) {
1167
      // If it is already defined the selected group, use it. Only valid
1168
      // when the filter uses checkboxes for widget.
1169
      if (!empty($selected_group_id)) {
1170
        $selected_group = $selected_group_id;
1171
      }
1172
      else {
1173
        $selected_group = $input[$this->options['group_info']['identifier']];
1174
      }
1175
      if ($selected_group == 'All' && !empty($this->options['group_info']['optional'])) {
1176
        return NULL;
1177
      }
1178
      if ($selected_group != 'All' && empty($this->options['group_info']['group_items'][$selected_group])) {
1179
        return FALSE;
1180
      }
1181
      if (isset($selected_group) && isset($this->options['group_info']['group_items'][$selected_group])) {
1182
        $input[$this->options['expose']['operator']] = $this->options['group_info']['group_items'][$selected_group]['operator'];
1183

    
1184
        // Value can be optional, For example for 'empty' and 'not empty' filters.
1185
        if (!empty($this->options['group_info']['group_items'][$selected_group]['value'])) {
1186
          $input[$this->options['expose']['identifier']] = $this->options['group_info']['group_items'][$selected_group]['value'];
1187
        }
1188
        $this->options['expose']['use_operator'] = TRUE;
1189

    
1190
        $this->group_info = $input[$this->options['group_info']['identifier']];
1191
        return TRUE;
1192
      }
1193
      else {
1194
        return FALSE;
1195
      }
1196
    }
1197
  }
1198

    
1199
  /**
1200
   * Returns the options available for a grouped filter that users checkboxes
1201
   * as widget, and therefore has to be applied several times, one per
1202
   * item selected.
1203
   */
1204
  function group_multiple_exposed_input(&$input) {
1205
    if (!empty($input[$this->options['group_info']['identifier']])) {
1206
      return array_filter($input[$this->options['group_info']['identifier']]);
1207
    }
1208
    return array();
1209
  }
1210

    
1211
  /**
1212
   * Returns TRUE if users can select multiple groups items of a
1213
   * grouped exposed filter.
1214
   */
1215
  function multiple_exposed_input() {
1216
    return $this->is_a_group() && !empty($this->options['group_info']['multiple']);
1217
  }
1218

    
1219
  /**
1220
   * If set to remember exposed input in the session, store it there.
1221
   * This function is similar to store_exposed_input but modified to
1222
   * work properly when the filter is a group.
1223
   */
1224
  function store_group_input($input, $status) {
1225
    if (!$this->is_a_group() || empty($this->options['group_info']['identifier'])) {
1226
      return TRUE;
1227
    }
1228

    
1229
    if (empty($this->options['group_info']['remember'])) {
1230
      return;
1231
    }
1232

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

    
1237
    // false means that we got a setting that means to recuse ourselves,
1238
    // so we should erase whatever happened to be there.
1239
    if ($status === FALSE && isset($_SESSION['views'][$this->view->name][$display_id])) {
1240
      $session = &$_SESSION['views'][$this->view->name][$display_id];
1241

    
1242
      if (isset($session[$this->options['group_info']['identifier']])) {
1243
        unset($session[$this->options['group_info']['identifier']]);
1244
      }
1245
    }
1246

    
1247
    if ($status !== FALSE) {
1248
      if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
1249
        $_SESSION['views'][$this->view->name][$display_id] = array();
1250
      }
1251

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

    
1254
      $session[$this->options['group_info']['identifier']] = $input[$this->options['group_info']['identifier']];
1255
    }
1256
  }
1257

    
1258
  /**
1259
   * Check to see if input from the exposed filters should change
1260
   * the behavior of this filter.
1261
   */
1262
  function accept_exposed_input($input) {
1263
    if (empty($this->options['exposed'])) {
1264
      return TRUE;
1265
    }
1266

    
1267

    
1268
    if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
1269
      $this->operator = $input[$this->options['expose']['operator_id']];
1270
    }
1271

    
1272
    if (!empty($this->options['expose']['identifier'])) {
1273
      $value = $input[$this->options['expose']['identifier']];
1274

    
1275
      // Various ways to check for the absence of non-required input.
1276
      if (empty($this->options['expose']['required'])) {
1277
        if (($this->operator == 'empty' || $this->operator == 'not empty') && $value === '') {
1278
          $value = ' ';
1279
        }
1280

    
1281
        if ($this->operator != 'empty' && $this->operator != 'not empty') {
1282
          if ($value == 'All' || $value === array()) {
1283
            return FALSE;
1284
          }
1285
        }
1286

    
1287
        if (!empty($this->always_multiple) && $value === '') {
1288
          return FALSE;
1289
        }
1290
      }
1291

    
1292

    
1293
      if (isset($value)) {
1294
        $this->value = $value;
1295
        if (empty($this->always_multiple) && empty($this->options['expose']['multiple'])) {
1296
          $this->value = array($value);
1297
        }
1298
      }
1299
      else {
1300
        return FALSE;
1301
      }
1302
    }
1303

    
1304
    return TRUE;
1305
  }
1306

    
1307
  function store_exposed_input($input, $status) {
1308
    if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
1309
      return TRUE;
1310
    }
1311

    
1312
    if (empty($this->options['expose']['remember'])) {
1313
      return;
1314
    }
1315

    
1316
    // Check if we store exposed value for current user.
1317
    global $user;
1318
    $allowed_rids = empty($this->options['expose']['remember_roles']) ? array() : array_filter($this->options['expose']['remember_roles']);
1319
    $intersect_rids = array_intersect_key($allowed_rids, $user->roles);
1320
    if (empty($intersect_rids)) {
1321
      return;
1322
    }
1323

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

    
1328
    // shortcut test.
1329
    $operator = !empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']);
1330

    
1331
    // false means that we got a setting that means to recuse ourselves,
1332
    // so we should erase whatever happened to be there.
1333
    if (!$status && isset($_SESSION['views'][$this->view->name][$display_id])) {
1334
      $session = &$_SESSION['views'][$this->view->name][$display_id];
1335
      if ($operator && isset($session[$this->options['expose']['operator_id']])) {
1336
        unset($session[$this->options['expose']['operator_id']]);
1337
      }
1338

    
1339
      if (isset($session[$this->options['expose']['identifier']])) {
1340
        unset($session[$this->options['expose']['identifier']]);
1341
      }
1342
    }
1343

    
1344
    if ($status) {
1345
      if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
1346
        $_SESSION['views'][$this->view->name][$display_id] = array();
1347
      }
1348

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

    
1351
      if ($operator && isset($input[$this->options['expose']['operator_id']])) {
1352
        $session[$this->options['expose']['operator_id']] = $input[$this->options['expose']['operator_id']];
1353
      }
1354

    
1355
      $session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];
1356
    }
1357
  }
1358

    
1359
  /**
1360
   * Add this filter to the query.
1361
   *
1362
   * Due to the nature of fapi, the value and the operator have an unintended
1363
   * level of indirection. You will find them in $this->operator
1364
   * and $this->value respectively.
1365
   */
1366
  function query() {
1367
    $this->ensure_my_table();
1368
    $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $this->value, $this->operator);
1369
  }
1370

    
1371
  /**
1372
   * Can this filter be used in OR groups?
1373
   *
1374
   * Some filters have complicated where clauses that cannot be easily used
1375
   * with OR groups. Some filters must also use HAVING which also makes
1376
   * them not groupable. These filters will end up in a special group
1377
   * if OR grouping is in use.
1378
   *
1379
   * @return bool
1380
   */
1381
   function can_group() {
1382
     return TRUE;
1383
   }
1384
}
1385

    
1386

    
1387
/**
1388
 * A special handler to take the place of missing or broken handlers.
1389
 *
1390
 * @ingroup views_filter_handlers
1391
 */
1392
class views_handler_filter_broken extends views_handler_filter {
1393
  function ui_name($short = FALSE) {
1394
    return t('Broken/missing handler');
1395
  }
1396

    
1397
  function ensure_my_table() { /* No table to ensure! */ }
1398
  function query($group_by = FALSE) { /* No query to run */ }
1399
  function options_form(&$form, &$form_state) {
1400
    $form['markup'] = array(
1401
      '#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>',
1402
    );
1403
  }
1404

    
1405
  /**
1406
   * Determine if the handler is considered 'broken'
1407
   */
1408
  function broken() { return TRUE; }
1409
}
1410

    
1411
/**
1412
 * Filter by no empty values, though allow to use "0".
1413
 * @param $var
1414
 * @return bool
1415
 */
1416
function _views_array_filter_zero($var) {
1417
  return trim($var) != "";
1418
}
1419

    
1420

    
1421
/**
1422
 * @}
1423
 */