Projet

Général

Profil

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

root / htmltest / sites / all / modules / views / handlers / views_handler_filter_numeric.inc @ 4543c6c7

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
  var $always_multiple = TRUE;
15
  function option_definition() {
16
    $options = parent::option_definition();
17

    
18
    $options['value'] = array(
19
      'contains' => array(
20
        'min' => array('default' => ''),
21
        'max' => array('default' => ''),
22
        'value' => array('default' => ''),
23
      ),
24
    );
25

    
26
    return $options;
27
  }
28

    
29
  function operators() {
30
    $operators = array(
31
      '<' => array(
32
        'title' => t('Is less than'),
33
        'method' => 'op_simple',
34
        'short' => t('<'),
35
        'values' => 1,
36
      ),
37
      '<=' => array(
38
        'title' => t('Is less than or equal to'),
39
        'method' => 'op_simple',
40
        'short' => t('<='),
41
        'values' => 1,
42
      ),
43
      '=' => array(
44
        'title' => t('Is equal to'),
45
        'method' => 'op_simple',
46
        'short' => t('='),
47
        'values' => 1,
48
      ),
49
      '!=' => array(
50
        'title' => t('Is not equal to'),
51
        'method' => 'op_simple',
52
        'short' => t('!='),
53
        'values' => 1,
54
      ),
55
      '>=' => array(
56
        'title' => t('Is greater than or equal to'),
57
        'method' => 'op_simple',
58
        'short' => t('>='),
59
        'values' => 1,
60
      ),
61
      '>' => array(
62
        'title' => t('Is greater than'),
63
        'method' => 'op_simple',
64
        'short' => t('>'),
65
        'values' => 1,
66
      ),
67
      'between' => array(
68
        'title' => t('Is between'),
69
        'method' => 'op_between',
70
        'short' => t('between'),
71
        'values' => 2,
72
      ),
73
      'not between' => array(
74
        'title' => t('Is not between'),
75
        'method' => 'op_between',
76
        'short' => t('not between'),
77
        'values' => 2,
78
      ),
79
    );
80

    
81
    // if the definition allows for the empty operator, add it.
82
    if (!empty($this->definition['allow empty'])) {
83
      $operators += array(
84
        'empty' => array(
85
          'title' => t('Is empty (NULL)'),
86
          'method' => 'op_empty',
87
          'short' => t('empty'),
88
          'values' => 0,
89
        ),
90
        'not empty' => array(
91
          'title' => t('Is not empty (NOT NULL)'),
92
          'method' => 'op_empty',
93
          'short' => t('not empty'),
94
          'values' => 0,
95
        ),
96
      );
97
    }
98

    
99
    // Add regexp support for MySQL.
100
    if (Database::getConnection()->databaseType() == 'mysql') {
101
      $operators += array(
102
        'regular_expression' => array(
103
          'title' => t('Regular expression'),
104
          'short' => t('regex'),
105
          'method' => 'op_regex',
106
          'values' => 1,
107
        ),
108
      );
109
    }
110

    
111
    return $operators;
112
  }
113

    
114
  /**
115
   * Provide a list of all the numeric operators
116
   */
117
  function operator_options($which = 'title') {
118
    $options = array();
119
    foreach ($this->operators() as $id => $info) {
120
      $options[$id] = $info[$which];
121
    }
122

    
123
    return $options;
124
  }
125

    
126
  function operator_values($values = 1) {
127
    $options = array();
128
    foreach ($this->operators() as $id => $info) {
129
      if ($info['values'] == $values) {
130
        $options[] = $id;
131
      }
132
    }
133

    
134
    return $options;
135
  }
136
  /**
137
   * Provide a simple textfield for equality
138
   */
139
  function value_form(&$form, &$form_state) {
140
    $form['value']['#tree'] = TRUE;
141

    
142
    // We have to make some choices when creating this as an exposed
143
    // filter form. For example, if the operator is locked and thus
144
    // not rendered, we can't render dependencies; instead we only
145
    // render the form items we need.
146
    $which = 'all';
147
    if (!empty($form['operator'])) {
148
      $source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
149
    }
150

    
151
    if (!empty($form_state['exposed'])) {
152
      $identifier = $this->options['expose']['identifier'];
153

    
154
      if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
155
        // exposed and locked.
156
        $which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
157
      }
158
      else {
159
        $source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
160
      }
161
    }
162

    
163
    if ($which == 'all') {
164
      $form['value']['value'] = array(
165
        '#type' => 'textfield',
166
        '#title' => empty($form_state['exposed']) ? t('Value') : '',
167
        '#size' => 30,
168
        '#default_value' => $this->value['value'],
169
        '#dependency' => array($source => $this->operator_values(1)),
170
      );
171
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['value'])) {
172
        $form_state['input'][$identifier]['value'] = $this->value['value'];
173
      }
174
    }
175
    elseif ($which == 'value') {
176
      // When exposed we drop the value-value and just do value if
177
      // the operator is locked.
178
      $form['value'] = array(
179
        '#type' => 'textfield',
180
        '#title' => empty($form_state['exposed']) ? t('Value') : '',
181
        '#size' => 30,
182
        '#default_value' => $this->value['value'],
183
      );
184
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier])) {
185
        $form_state['input'][$identifier] = $this->value['value'];
186
      }
187
    }
188

    
189
    if ($which == 'all' || $which == 'minmax') {
190
      $form['value']['min'] = array(
191
        '#type' => 'textfield',
192
        '#title' => empty($form_state['exposed']) ? t('Min') : '',
193
        '#size' => 30,
194
        '#default_value' => $this->value['min'],
195
      );
196
      $form['value']['max'] = array(
197
        '#type' => 'textfield',
198
        '#title' => empty($form_state['exposed']) ? t('And max') : t('And'),
199
        '#size' => 30,
200
        '#default_value' => $this->value['max'],
201
      );
202
      if ($which == 'all') {
203
        $dependency = array(
204
          '#dependency' => array($source => $this->operator_values(2)),
205
        );
206
        $form['value']['min'] += $dependency;
207
        $form['value']['max'] += $dependency;
208
      }
209
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['min'])) {
210
        $form_state['input'][$identifier]['min'] = $this->value['min'];
211
      }
212
      if (!empty($form_state['exposed']) && !isset($form_state['input'][$identifier]['max'])) {
213
        $form_state['input'][$identifier]['max'] = $this->value['max'];
214
      }
215

    
216
      if (!isset($form['value'])) {
217
        // Ensure there is something in the 'value'.
218
        $form['value'] = array(
219
          '#type' => 'value',
220
          '#value' => NULL
221
        );
222
      }
223
    }
224
  }
225

    
226
  function query() {
227
    $this->ensure_my_table();
228
    $field = "$this->table_alias.$this->real_field";
229

    
230
    $info = $this->operators();
231
    if (!empty($info[$this->operator]['method'])) {
232
      $this->{$info[$this->operator]['method']}($field);
233
    }
234
  }
235

    
236
  function op_between($field) {
237
    if ($this->operator == 'between') {
238
      $this->query->add_where($this->options['group'], $field, array($this->value['min'], $this->value['max']), 'BETWEEN');
239
    }
240
    else {
241
      $this->query->add_where($this->options['group'], db_or()->condition($field, $this->value['min'], '<=')->condition($field, $this->value['max'], '>='));
242
    }
243
  }
244

    
245
  function op_simple($field) {
246
    $this->query->add_where($this->options['group'], $field, $this->value['value'], $this->operator);
247
  }
248

    
249
  function op_empty($field) {
250
    if ($this->operator == 'empty') {
251
      $operator = "IS NULL";
252
    }
253
    else {
254
      $operator = "IS NOT NULL";
255
    }
256

    
257
    $this->query->add_where($this->options['group'], $field, NULL, $operator);
258
  }
259

    
260
  function op_regex($field) {
261
    $this->query->add_where($this->options['group'], $field, $this->value['value'], 'RLIKE');
262
  }
263

    
264
  function admin_summary() {
265
    if ($this->is_a_group()) {
266
      return t('grouped');
267
    }
268
    if (!empty($this->options['exposed'])) {
269
      return t('exposed');
270
    }
271

    
272
    $options = $this->operator_options('short');
273
    $output = check_plain($options[$this->operator]);
274
    if (in_array($this->operator, $this->operator_values(2))) {
275
      $output .= ' ' . t('@min and @max', array('@min' => $this->value['min'], '@max' => $this->value['max']));
276
    }
277
    elseif (in_array($this->operator, $this->operator_values(1))) {
278
      $output .= ' ' . check_plain($this->value['value']);
279
    }
280
    return $output;
281
  }
282

    
283
  /**
284
   * Do some minor translation of the exposed input
285
   */
286
  function accept_exposed_input($input) {
287
    if (empty($this->options['exposed'])) {
288
      return TRUE;
289
    }
290

    
291
    // rewrite the input value so that it's in the correct format so that
292
    // the parent gets the right data.
293
    if (!empty($this->options['expose']['identifier'])) {
294
      $value = &$input[$this->options['expose']['identifier']];
295
      if (!is_array($value)) {
296
        $value = array(
297
          'value' => $value,
298
        );
299
      }
300
    }
301

    
302
    $rc = parent::accept_exposed_input($input);
303

    
304
    if (empty($this->options['expose']['required'])) {
305
      // We have to do some of our own checking for non-required filters.
306
      $info = $this->operators();
307
      if (!empty($info[$this->operator]['values'])) {
308
        switch ($info[$this->operator]['values']) {
309
          case 1:
310
            if ($value['value'] === '') {
311
              return FALSE;
312
            }
313
            break;
314
          case 2:
315
            if ($value['min'] === '' && $value['max'] === '') {
316
              return FALSE;
317
            }
318
            break;
319
        }
320
      }
321
    }
322

    
323
    return $rc;
324
  }
325
}