Project

General

Profile

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

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

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

    
26
  /**
27
   * Exposed filter options.
28
   */
29
  public $always_multiple = TRUE;
30

    
31
  /**
32
   * Don't display empty space where the operator would be.
33
   */
34
  public $no_operator = TRUE;
35

    
36
  /**
37
   * Whether to accept NULL as a false value or not.
38
   */
39
  public $accept_null = FALSE;
40

    
41
  /**
42
   * {@inheritdoc}
43
   */
44
  public function construct() {
45
    $this->value_value = t('True');
46
    if (isset($this->definition['label'])) {
47
      $this->value_value = $this->definition['label'];
48
    }
49
    if (isset($this->definition['accept null'])) {
50
      $this->accept_null = (bool) $this->definition['accept null'];
51
    }
52
    elseif (isset($this->definition['accept_null'])) {
53
      $this->accept_null = (bool) $this->definition['accept_null'];
54
    }
55
    $this->value_options = NULL;
56
    parent::construct();
57
  }
58

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

    
84
    // Provide a fallback if the above didn't set anything.
85
    if (!isset($this->value_options)) {
86
      $this->value_options = array(1 => t('True'), 0 => t('False'));
87
    }
88
  }
89

    
90
  /**
91
   * {@inheritdoc}
92
   */
93
  public function option_definition() {
94
    $options = parent::option_definition();
95

    
96
    $options['value']['default'] = FALSE;
97

    
98
    return $options;
99
  }
100

    
101
  /**
102
   * {@inheritdoc}
103
   */
104
  public function operator_form(&$form, &$form_state) {
105
    $form['operator'] = array();
106
  }
107

    
108
  /**
109
   * {@inheritdoc}
110
   */
111
  public function value_form(&$form, &$form_state) {
112
    if (empty($this->value_options)) {
113
      // Initialize the array of possible values for this filter.
114
      $this->get_value_options();
115
    }
116
    if (!empty($form_state['exposed'])) {
117
      // Exposed filter: use a select box to save space.
118
      $filter_form_type = 'select';
119
    }
120
    else {
121
      // Configuring a filter: use radios for clarity.
122
      $filter_form_type = 'radios';
123
    }
124
    $form['value'] = array(
125
      '#type' => $filter_form_type,
126
      '#title' => $this->value_value,
127
      '#options' => $this->value_options,
128
      '#default_value' => $this->value,
129
    );
130
    if (!empty($this->options['exposed'])) {
131
      $identifier = $this->options['expose']['identifier'];
132
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
133
        $form_state['input'][$identifier] = $this->value;
134
      }
135
      // If we're configuring an exposed filter, add an <Any> option.
136
      if (empty($form_state['exposed']) || empty($this->options['expose']['required'])) {
137
        $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? '<Any>' : t('- Any -');
138
        if ($form['value']['#type'] != 'select') {
139
          $any_label = check_plain($any_label);
140
        }
141
        $form['value']['#options'] = array('All' => $any_label) + $form['value']['#options'];
142
      }
143
    }
144
  }
145

    
146
  /**
147
   * {@inheritdoc}
148
   */
149
  public function value_validate($form, &$form_state) {
150
    if ($form_state['values']['options']['value'] == 'All' && !empty($form_state['values']['options']['expose']['required'])) {
151
      form_set_error('value', t('You must select a value unless this is an non-required exposed filter.'));
152
    }
153
  }
154

    
155
  /**
156
   * {@inheritdoc}
157
   */
158
  public function admin_summary() {
159
    if ($this->is_a_group()) {
160
      return t('grouped');
161
    }
162
    if (!empty($this->options['exposed'])) {
163
      return t('exposed');
164
    }
165
    if (empty($this->value_options)) {
166
      $this->get_value_options();
167
    }
168
    // Now that we have the valid options for this filter, just return the
169
    // human-readable label based on the current value.  The value_options
170
    // array is keyed with either 0 or 1, so if the current value is not
171
    // empty, use the label for 1, and if it's empty, use the label for 0.
172
    return $this->value_options[!empty($this->value)];
173
  }
174

    
175
  /**
176
   * {@inheritdoc}
177
   */
178
  public function expose_options() {
179
    parent::expose_options();
180
    $this->options['expose']['operator_id'] = '';
181
    $this->options['expose']['label'] = $this->value_value;
182
    $this->options['expose']['required'] = TRUE;
183
  }
184

    
185
  /**
186
   * {@inheritdoc}
187
   */
188
  public function query() {
189
    $this->ensure_my_table();
190
    $field = "$this->table_alias.$this->real_field";
191

    
192
    if (empty($this->value)) {
193
      if ($this->accept_null) {
194
        $or = db_or()
195
          ->condition($field, 0, '=')
196
          ->condition($field, NULL, 'IS NULL');
197
        $this->query->add_where($this->options['group'], $or);
198
      }
199
      else {
200
        $this->query->add_where($this->options['group'], $field, 0, '=');
201
      }
202
    }
203
    else {
204
      if (!empty($this->definition['use equal'])) {
205
        $this->query->add_where($this->options['group'], $field, 1, '=');
206
      }
207
      else {
208
        $this->query->add_where($this->options['group'], $field, 0, '<>');
209
      }
210
    }
211
  }
212

    
213
}