Project

General

Profile

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter_numeric.inc @ 4003efde

1
<?php
2

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

    
8
/**
9
 * Simple filter to handle greater than/less than filters
10
 *
11
 * @ingroup views_filter_handlers
12
 */
13
class views_handler_filter_numeric extends views_handler_filter {
14

    
15
  /**
16
   * Exposed filter options.
17
   */
18
  public $always_multiple = TRUE;
19

    
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function option_definition() {
24
    $options = parent::option_definition();
25

    
26
    $options['value'] = array(
27
      'contains' => array(
28
        'min' => array('default' => ''),
29
        'max' => array('default' => ''),
30
        'value' => array('default' => ''),
31
      ),
32
    );
33

    
34
    return $options;
35
  }
36

    
37
  /**
38
   * {@inheritdoc}
39
   */
40
  public function operators() {
41
    $operators = array(
42
      '<' => array(
43
        'title' => t('Is less than'),
44
        'method' => 'op_simple',
45
        'short' => t('<'),
46
        'values' => 1,
47
      ),
48
      '<=' => array(
49
        'title' => t('Is less than or equal to'),
50
        'method' => 'op_simple',
51
        'short' => t('<='),
52
        'values' => 1,
53
      ),
54
      '=' => array(
55
        'title' => t('Is equal to'),
56
        'method' => 'op_simple',
57
        'short' => t('='),
58
        'values' => 1,
59
      ),
60
      '!=' => array(
61
        'title' => t('Is not equal to'),
62
        'method' => 'op_simple',
63
        'short' => t('!='),
64
        'values' => 1,
65
      ),
66
      '>=' => array(
67
        'title' => t('Is greater than or equal to'),
68
        'method' => 'op_simple',
69
        'short' => t('>='),
70
        'values' => 1,
71
      ),
72
      '>' => array(
73
        'title' => t('Is greater than'),
74
        'method' => 'op_simple',
75
        'short' => t('>'),
76
        'values' => 1,
77
      ),
78
      'between' => array(
79
        'title' => t('Is between'),
80
        'method' => 'op_between',
81
        'short' => t('between'),
82
        'values' => 2,
83
      ),
84
      'not between' => array(
85
        'title' => t('Is not between'),
86
        'method' => 'op_between',
87
        'short' => t('not between'),
88
        'values' => 2,
89
      ),
90
    );
91

    
92
    // if the definition allows for the empty operator, add it.
93
    if (!empty($this->definition['allow empty'])) {
94
      $operators += array(
95
        'empty' => array(
96
          'title' => t('Is empty (NULL)'),
97
          'method' => 'op_empty',
98
          'short' => t('empty'),
99
          'values' => 0,
100
        ),
101
        'not empty' => array(
102
          'title' => t('Is not empty (NOT NULL)'),
103
          'method' => 'op_empty',
104
          'short' => t('not empty'),
105
          'values' => 0,
106
        ),
107
      );
108
    }
109

    
110
    // Add regexp support for MySQL.
111
    if (Database::getConnection()->databaseType() == 'mysql') {
112
      $operators += array(
113
        'regular_expression' => array(
114
          'title' => t('Regular expression'),
115
          'short' => t('regex'),
116
          'method' => 'op_regex',
117
          'values' => 1,
118
        ),
119
        'not_regular_expression' => array(
120
          'title' => t('Not regular expression'),
121
          'short' => t('not regex'),
122
          'method' => 'op_not_regex',
123
          'values' => 1,
124
        ),
125
      );
126
    }
127

    
128
    return $operators;
129
  }
130

    
131
  /**
132
   * Provide a list of all the numeric operators
133
   */
134
  public function operator_options($which = 'title') {
135
    $options = array();
136
    foreach ($this->operators() as $id => $info) {
137
      $options[$id] = $info[$which];
138
    }
139

    
140
    return $options;
141
  }
142

    
143
  /**
144
   * {@inheritdoc}
145
   */
146
  public function operator_values($values = 1) {
147
    $options = array();
148
    foreach ($this->operators() as $id => $info) {
149
      if ($info['values'] == $values) {
150
        $options[] = $id;
151
      }
152
    }
153

    
154
    return $options;
155
  }
156

    
157
  /**
158
   * Provide a simple textfield for equality
159
   */
160
  public function value_form(&$form, &$form_state) {
161
    $form['value']['#tree'] = TRUE;
162

    
163
    // We have to make some choices when creating this as an exposed
164
    // filter form. For example, if the operator is locked and thus
165
    // not rendered, we can't render dependencies; instead we only
166
    // render the form items we need.
167
    $which = 'all';
168
    $limit_operators = !empty($this->options['expose']['limit_operators']) && (count($this->options['expose']['available_operators']) > 0);
169
    $use_value = FALSE;
170
    $use_minmax = FALSE;
171

    
172
    if (!empty($form['operator'])) {
173
      $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
174
    }
175

    
176
    if (!empty($form_state['exposed'])) {
177
      $operator_values_with_1_values = $this->operator_values(1);
178
      $operator_values_with_2_values = $this->operator_values(2);
179
      if ($limit_operators) {
180
        // If limit operators is enabled, check that at least one operator
181
        // with two values is enabled to display the min max widgets
182
        foreach ($operator_values_with_2_values as $operator) {
183
          if (isset($this->options['expose']['available_operators'][$operator])) {
184
            $use_minmax = TRUE;
185
            break;
186
          }
187
        }
188
        // the same for operators with one value
189
        foreach ($operator_values_with_1_values as $operator) {
190
          if (isset($this->options['expose']['available_operators'][$operator])) {
191
            $use_value = TRUE;
192
            break;
193
          }
194
        }
195
      }
196
      else {
197
        $use_minmax = $use_value = TRUE;
198
      }
199
      $identifier = $this->options['expose']['identifier'];
200

    
201
      if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
202
        // exposed and locked.
203
        $which = in_array($this->operator, $operator_values_with_2_values) ? 'minmax' : 'value';
204
      }
205
      else {
206
        $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
207
      }
208
    }
209
    else {
210
      $use_minmax = $use_value = TRUE;
211
    }
212

    
213
    if ($use_value) {
214
      if ($which == 'all') {
215
        $form['value']['value'] = array(
216
          '#type' => 'textfield',
217
          '#title' => empty($form_state['exposed']) ? t('Value') : '',
218
          '#size' => 30,
219
          '#default_value' => $this->value['value'],
220
          '#dependency' => array($source => $this->operator_values(1)),
221
        );
222
        if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['value'])) {
223
          $form_state['input'][$identifier]['value'] = $this->value['value'];
224
        }
225
      }
226
      elseif ($which == 'value') {
227
        // When exposed we drop the value-value and just do value if
228
        // the operator is locked.
229
        $form['value'] = array(
230
          '#type' => 'textfield',
231
          '#title' => empty($form_state['exposed']) ? t('Value') : '',
232
          '#size' => 30,
233
          '#default_value' => $this->value['value'],
234
        );
235
        if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
236
          $form_state['input'][$identifier] = $this->value['value'];
237
        }
238
      }
239
    }
240

    
241
    if ($which == 'all' || $which == 'minmax') {
242
      if ($use_minmax) {
243
        $form['value']['min'] = array(
244
          '#type' => 'textfield',
245
          '#title' => empty($form_state['exposed']) ? t('Min') : '',
246
          '#size' => 30,
247
          '#default_value' => $this->value['min'],
248
        );
249
        $form['value']['max'] = array(
250
          '#type' => 'textfield',
251
          '#title' => empty($form_state['exposed']) ? t('And max') : t('And'),
252
          '#size' => 30,
253
          '#default_value' => $this->value['max'],
254
        );
255

    
256
        if ($which == 'all') {
257
          $dependency = array(
258
            '#dependency' => array($source => $this->operator_values(2)),
259
          );
260

    
261
          $form['value']['min'] += $dependency;
262
          $form['value']['max'] += $dependency;
263
        }
264
      }
265

    
266
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['min']) && $use_minmax) {
267
        $form_state['input'][$identifier]['min'] = $this->value['min'];
268
      }
269
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['max']) && $use_minmax) {
270
        $form_state['input'][$identifier]['max'] = $this->value['max'];
271
      }
272

    
273
      if (!isset($form['value'])) {
274
        // Ensure there is something in the 'value'.
275
        $form['value'] = array(
276
          '#type' => 'value',
277
          '#value' => NULL
278
        );
279
      }
280
    }
281
  }
282

    
283
  /**
284
   * {@inheritdoc}
285
   */
286
  public function query() {
287
    $this->ensure_my_table();
288
    $field = "$this->table_alias.$this->real_field";
289

    
290
    $info = $this->operators();
291
    if (!empty($info[$this->operator]['method'])) {
292
      $this->{$info[$this->operator]['method']}($field);
293
    }
294
  }
295

    
296
  /**
297
   * {@inheritdoc}
298
   */
299
  public function op_between($field) {
300
    if ($this->operator == 'between') {
301
      $this->query->add_where($this->options['group'], $field, array($this->value['min'], $this->value['max']), 'BETWEEN');
302
    }
303
    else {
304
      $this->query->add_where($this->options['group'], db_or()->condition($field, $this->value['min'], '<=')->condition($field, $this->value['max'], '>='));
305
    }
306
  }
307

    
308
  /**
309
   * {@inheritdoc}
310
   */
311
  public function op_simple($field) {
312
    $this->query->add_where($this->options['group'], $field, $this->value['value'], $this->operator);
313
  }
314

    
315
  /**
316
   * {@inheritdoc}
317
   */
318
  public function op_empty($field) {
319
    if ($this->operator == 'empty') {
320
      $operator = "IS NULL";
321
    }
322
    else {
323
      $operator = "IS NOT NULL";
324
    }
325

    
326
    $this->query->add_where($this->options['group'], $field, NULL, $operator);
327
  }
328

    
329
  /**
330
   * {@inheritdoc}
331
   */
332
  public function op_regex($field) {
333
    $this->query->add_where($this->options['group'], $field, $this->value['value'], 'RLIKE');
334
  }
335

    
336
  /**
337
   * {@inheritdoc}
338
   */
339
  public function op_not_regex($field) {
340
    $this->query->add_where($this->options['group'], $field, $this->value['value'], 'NOT RLIKE');
341
  }
342

    
343
  /**
344
   * {@inheritdoc}
345
   */
346
  public function admin_summary() {
347
    if ($this->is_a_group()) {
348
      return t('grouped');
349
    }
350
    if (!empty($this->options['exposed'])) {
351
      return t('exposed');
352
    }
353

    
354
    $options = $this->operator_options('short');
355
    $output = check_plain($options[$this->operator]);
356
    if (in_array($this->operator, $this->operator_values(2))) {
357
      $output .= ' ' . t('@min and @max', array('@min' => $this->value['min'], '@max' => $this->value['max']));
358
    }
359
    elseif (in_array($this->operator, $this->operator_values(1))) {
360
      $output .= ' ' . check_plain($this->value['value']);
361
    }
362
    return $output;
363
  }
364

    
365
  /**
366
   * Do some minor translation of the exposed input.
367
   */
368
  public function accept_exposed_input($input) {
369
    if (empty($this->options['exposed'])) {
370
      return TRUE;
371
    }
372

    
373
    // Rewrite the input value so that it's in the correct format so that the
374
    // parent gets the right data.
375
    if (!empty($this->options['expose']['identifier'])) {
376
      $value = &$input[$this->options['expose']['identifier']];
377
      if (!is_array($value)) {
378
        $value = array(
379
          'value' => $value,
380
        );
381
      }
382
      if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
383
        if ($input[$this->options['expose']['operator_id']] === 'empty' || $input[$this->options['expose']['operator_id']] === 'not empty') {
384
          // Parent method will expect a boolean value. We don't ask for a value
385
          // so we'll force "Yes".
386
          $value['value'] = 1;
387
        }
388
      }
389
    }
390

    
391
    $rc = parent::accept_exposed_input($input);
392

    
393
    if (empty($this->options['expose']['required'])) {
394
      // We have to do some of our own checking for non-required filters.
395
      $info = $this->operators();
396
      if (!empty($info[$this->operator]['values'])) {
397
        switch ($info[$this->operator]['values']) {
398
          case 1:
399
            if ($value['value'] === '') {
400
              return FALSE;
401
            }
402
            break;
403

    
404
          case 2:
405
            if ($value['min'] === '' && $value['max'] === '') {
406
              return FALSE;
407
            }
408
            break;
409
        }
410
      }
411
    }
412

    
413
    return $rc;
414
  }
415

    
416
}