Projet

Général

Profil

Paste
Télécharger (6,04 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter_boolean_operator.inc @ 7547bb19

1
<?php
2

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

    
8
/**
9
 * Simple filter to handle matching of boolean values
10
 *
11
 * Definition items:
12
 * - label: (REQUIRED) The label for the checkbox.
13
 * - type: For basic 'true false' types, an item can specify the following:
14
 *    - true-false: True/false (this is the default)
15
 *    - yes-no: Yes/No
16
 *    - on-off: On/Off
17
 *    - enabled-disabled: Enabled/Disabled
18
 * - accept null: Treat a NULL value as false.
19
 * - use equal: If you use this flag the query will use = 1 instead of <> 0.
20
 *   This might be helpful for performance reasons.
21
 *
22
 * @ingroup views_filter_handlers
23
 */
24
class views_handler_filter_boolean_operator extends views_handler_filter {
25
  // exposed filter options
26
  var $always_multiple = TRUE;
27
  // Don't display empty space where the operator would be.
28
  var $no_operator = TRUE;
29
  // Whether to accept NULL as a false value or not
30
  var $accept_null = FALSE;
31

    
32
  function construct() {
33
    $this->value_value = t('True');
34
    if (isset($this->definition['label'])) {
35
      $this->value_value = $this->definition['label'];
36
    }
37
    if (isset($this->definition['accept null'])) {
38
      $this->accept_null = (bool) $this->definition['accept null'];
39
    }
40
    else if (isset($this->definition['accept_null'])) {
41
      $this->accept_null = (bool) $this->definition['accept_null'];
42
    }
43
    $this->value_options = NULL;
44
    parent::construct();
45
  }
46

    
47
  /**
48
   * Return the possible options for this filter.
49
   *
50
   * Child classes should override this function to set the possible values
51
   * for the filter.  Since this is a boolean filter, the array should have
52
   * two possible keys: 1 for "True" and 0 for "False", although the labels
53
   * can be whatever makes sense for the filter.  These values are used for
54
   * configuring the filter, when the filter is exposed, and in the admin
55
   * summary of the filter.  Normally, this should be static data, but if it's
56
   * dynamic for some reason, child classes should use a guard to reduce
57
   * database hits as much as possible.
58
   */
59
  function get_value_options() {
60
    if (isset($this->definition['type'])) {
61
      if ($this->definition['type'] == 'yes-no') {
62
        $this->value_options = array(1 => t('Yes'), 0 => t('No'));
63
      }
64
      if ($this->definition['type'] == 'on-off') {
65
        $this->value_options = array(1 => t('On'), 0 => t('Off'));
66
      }
67
      if ($this->definition['type'] == 'enabled-disabled') {
68
        $this->value_options = array(1 => t('Enabled'), 0 => t('Disabled'));
69
      }
70
    }
71

    
72
    // Provide a fallback if the above didn't set anything.
73
    if (!isset($this->value_options)) {
74
      $this->value_options = array(1 => t('True'), 0 => t('False'));
75
    }
76
  }
77

    
78
  function option_definition() {
79
    $options = parent::option_definition();
80

    
81
    $options['value']['default'] = FALSE;
82

    
83
    return $options;
84
  }
85

    
86
  function operator_form(&$form, &$form_state) {
87
    $form['operator'] = array();
88
  }
89

    
90
  function value_form(&$form, &$form_state) {
91
    if (empty($this->value_options)) {
92
      // Initialize the array of possible values for this filter.
93
      $this->get_value_options();
94
    }
95
    if (!empty($form_state['exposed'])) {
96
      // Exposed filter: use a select box to save space.
97
      $filter_form_type = 'select';
98
    }
99
    else {
100
      // Configuring a filter: use radios for clarity.
101
      $filter_form_type = 'radios';
102
    }
103
    $form['value'] = array(
104
      '#type' => $filter_form_type,
105
      '#title' => $this->value_value,
106
      '#options' => $this->value_options,
107
      '#default_value' => $this->value,
108
    );
109
    if (!empty($this->options['exposed'])) {
110
      $identifier = $this->options['expose']['identifier'];
111
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
112
        $form_state['input'][$identifier] = $this->value;
113
      }
114
      // If we're configuring an exposed filter, add an <Any> option.
115
      if (empty($form_state['exposed']) || empty($this->options['expose']['required'])) {
116
        $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? '<Any>' : t('- Any -');
117
        if ($form['value']['#type'] != 'select') {
118
          $any_label = check_plain($any_label);
119
        }
120
        $form['value']['#options'] = array('All' => $any_label) + $form['value']['#options'];
121
      }
122
    }
123
  }
124

    
125
  function value_validate($form, &$form_state) {
126
    if ($form_state['values']['options']['value'] == 'All' && !empty($form_state['values']['options']['expose']['required'])) {
127
      form_set_error('value', t('You must select a value unless this is an non-required exposed filter.'));
128
    }
129
  }
130

    
131
  function admin_summary() {
132
    if ($this->is_a_group()) {
133
      return t('grouped');
134
    }
135
    if (!empty($this->options['exposed'])) {
136
      return t('exposed');
137
    }
138
    if (empty($this->value_options)) {
139
      $this->get_value_options();
140
    }
141
    // Now that we have the valid options for this filter, just return the
142
    // human-readable label based on the current value.  The value_options
143
    // array is keyed with either 0 or 1, so if the current value is not
144
    // empty, use the label for 1, and if it's empty, use the label for 0.
145
    return $this->value_options[!empty($this->value)];
146
  }
147

    
148
  function expose_options() {
149
    parent::expose_options();
150
    $this->options['expose']['operator_id'] = '';
151
    $this->options['expose']['label'] = $this->value_value;
152
    $this->options['expose']['required'] = TRUE;
153
  }
154

    
155
  function query() {
156
    $this->ensure_my_table();
157
    $field = "$this->table_alias.$this->real_field";
158

    
159
    if (empty($this->value)) {
160
      if ($this->accept_null) {
161
        $or = db_or()
162
          ->condition($field, 0, '=')
163
          ->condition($field, NULL, 'IS NULL');
164
        $this->query->add_where($this->options['group'], $or);
165
      }
166
      else {
167
        $this->query->add_where($this->options['group'], $field, 0, '=');
168
      }
169
    }
170
    else {
171
      if (!empty($this->definition['use equal'])) {
172
        $this->query->add_where($this->options['group'], $field, 1, '=');
173
      }
174
      else {
175
        $this->query->add_where($this->options['group'], $field, 0, '<>');
176
      }
177
    }
178
  }
179
}