Projet

Général

Profil

Paste
Télécharger (9,69 ko) Statistiques
| Branche: | Révision:

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

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
      );
120
    }
121

    
122
    return $operators;
123
  }
124

    
125
  /**
126
   * Provide a list of all the numeric operators
127
   */
128
  public function operator_options($which = 'title') {
129
    $options = array();
130
    foreach ($this->operators() as $id => $info) {
131
      $options[$id] = $info[$which];
132
    }
133

    
134
    return $options;
135
  }
136

    
137
  /**
138
   * {@inheritdoc}
139
   */
140
  public function operator_values($values = 1) {
141
    $options = array();
142
    foreach ($this->operators() as $id => $info) {
143
      if ($info['values'] == $values) {
144
        $options[] = $id;
145
      }
146
    }
147

    
148
    return $options;
149
  }
150

    
151
  /**
152
   * Provide a simple textfield for equality
153
   */
154
  public function value_form(&$form, &$form_state) {
155
    $form['value']['#tree'] = TRUE;
156

    
157
    // We have to make some choices when creating this as an exposed
158
    // filter form. For example, if the operator is locked and thus
159
    // not rendered, we can't render dependencies; instead we only
160
    // render the form items we need.
161
    $which = 'all';
162
    if (!empty($form['operator'])) {
163
      $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
164
    }
165

    
166
    if (!empty($form_state['exposed'])) {
167
      $identifier = $this->options['expose']['identifier'];
168

    
169
      if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
170
        // exposed and locked.
171
        $which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
172
      }
173
      else {
174
        $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
175
      }
176
    }
177

    
178
    if ($which == 'all') {
179
      $form['value']['value'] = array(
180
        '#type' => 'textfield',
181
        '#title' => empty($form_state['exposed']) ? t('Value') : '',
182
        '#size' => 30,
183
        '#default_value' => $this->value['value'],
184
        '#dependency' => array($source => $this->operator_values(1)),
185
      );
186
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['value'])) {
187
        $form_state['input'][$identifier]['value'] = $this->value['value'];
188
      }
189
    }
190
    elseif ($which == 'value') {
191
      // When exposed we drop the value-value and just do value if
192
      // the operator is locked.
193
      $form['value'] = array(
194
        '#type' => 'textfield',
195
        '#title' => empty($form_state['exposed']) ? t('Value') : '',
196
        '#size' => 30,
197
        '#default_value' => $this->value['value'],
198
      );
199
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
200
        $form_state['input'][$identifier] = $this->value['value'];
201
      }
202
    }
203

    
204
    if ($which == 'all' || $which == 'minmax') {
205
      $form['value']['min'] = array(
206
        '#type' => 'textfield',
207
        '#title' => empty($form_state['exposed']) ? t('Min') : '',
208
        '#size' => 30,
209
        '#default_value' => $this->value['min'],
210
      );
211
      $form['value']['max'] = array(
212
        '#type' => 'textfield',
213
        '#title' => empty($form_state['exposed']) ? t('And max') : t('And'),
214
        '#size' => 30,
215
        '#default_value' => $this->value['max'],
216
      );
217
      if ($which == 'all') {
218
        $dependency = array(
219
          '#dependency' => array($source => $this->operator_values(2)),
220
        );
221
        $form['value']['min'] += $dependency;
222
        $form['value']['max'] += $dependency;
223
      }
224
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['min'])) {
225
        $form_state['input'][$identifier]['min'] = $this->value['min'];
226
      }
227
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['max'])) {
228
        $form_state['input'][$identifier]['max'] = $this->value['max'];
229
      }
230

    
231
      if (!isset($form['value'])) {
232
        // Ensure there is something in the 'value'.
233
        $form['value'] = array(
234
          '#type' => 'value',
235
          '#value' => NULL
236
        );
237
      }
238
    }
239
  }
240

    
241
  /**
242
   * {@inheritdoc}
243
   */
244
  public function query() {
245
    $this->ensure_my_table();
246
    $field = "$this->table_alias.$this->real_field";
247

    
248
    $info = $this->operators();
249
    if (!empty($info[$this->operator]['method'])) {
250
      $this->{$info[$this->operator]['method']}($field);
251
    }
252
  }
253

    
254
  /**
255
   * {@inheritdoc}
256
   */
257
  public function op_between($field) {
258
    if ($this->operator == 'between') {
259
      $this->query->add_where($this->options['group'], $field, array($this->value['min'], $this->value['max']), 'BETWEEN');
260
    }
261
    else {
262
      $this->query->add_where($this->options['group'], db_or()->condition($field, $this->value['min'], '<=')->condition($field, $this->value['max'], '>='));
263
    }
264
  }
265

    
266
  /**
267
   * {@inheritdoc}
268
   */
269
  public function op_simple($field) {
270
    $this->query->add_where($this->options['group'], $field, $this->value['value'], $this->operator);
271
  }
272

    
273
  /**
274
   * {@inheritdoc}
275
   */
276
  public function op_empty($field) {
277
    if ($this->operator == 'empty') {
278
      $operator = "IS NULL";
279
    }
280
    else {
281
      $operator = "IS NOT NULL";
282
    }
283

    
284
    $this->query->add_where($this->options['group'], $field, NULL, $operator);
285
  }
286

    
287
  /**
288
   * {@inheritdoc}
289
   */
290
  public function op_regex($field) {
291
    $this->query->add_where($this->options['group'], $field, $this->value['value'], 'RLIKE');
292
  }
293

    
294
  /**
295
   * {@inheritdoc}
296
   */
297
  public function admin_summary() {
298
    if ($this->is_a_group()) {
299
      return t('grouped');
300
    }
301
    if (!empty($this->options['exposed'])) {
302
      return t('exposed');
303
    }
304

    
305
    $options = $this->operator_options('short');
306
    $output = check_plain($options[$this->operator]);
307
    if (in_array($this->operator, $this->operator_values(2))) {
308
      $output .= ' ' . t('@min and @max', array('@min' => $this->value['min'], '@max' => $this->value['max']));
309
    }
310
    elseif (in_array($this->operator, $this->operator_values(1))) {
311
      $output .= ' ' . check_plain($this->value['value']);
312
    }
313
    return $output;
314
  }
315

    
316
  /**
317
   * Do some minor translation of the exposed input.
318
   */
319
  public function accept_exposed_input($input) {
320
    if (empty($this->options['exposed'])) {
321
      return TRUE;
322
    }
323

    
324
    // Rewrite the input value so that it's in the correct format so that the
325
    // parent gets the right data.
326
    if (!empty($this->options['expose']['identifier'])) {
327
      $value = &$input[$this->options['expose']['identifier']];
328
      if (!is_array($value)) {
329
        $value = array(
330
          'value' => $value,
331
        );
332
      }
333
    }
334

    
335
    $rc = parent::accept_exposed_input($input);
336

    
337
    if (empty($this->options['expose']['required'])) {
338
      // We have to do some of our own checking for non-required filters.
339
      $info = $this->operators();
340
      if (!empty($info[$this->operator]['values'])) {
341
        switch ($info[$this->operator]['values']) {
342
          case 1:
343
            if ($value['value'] === '') {
344
              return FALSE;
345
            }
346
            break;
347

    
348
          case 2:
349
            if ($value['min'] === '' && $value['max'] === '') {
350
              return FALSE;
351
            }
352
            break;
353
        }
354
      }
355
    }
356

    
357
    return $rc;
358
  }
359

    
360
}