Numeric.php 9.74 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\filter\Numeric.
6 7
 */

8
namespace Drupal\views\Plugin\views\filter;
9

10
use Drupal\Component\Utility\String as UtilityString;
11
use Drupal\Core\Database\Database;
12
use Drupal\Core\Form\FormStateInterface;
13

14 15 16 17
/**
 * Simple filter to handle greater than/less than filters
 *
 * @ingroup views_filter_handlers
18
 *
19
 * @ViewsFilter("numeric")
20
 */
21
class Numeric extends FilterPluginBase {
22

23
  protected $alwaysMultiple = TRUE;
24

25 26
  protected function defineOptions() {
    $options = parent::defineOptions();
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

    $options['value'] = array(
      'contains' => array(
        'min' => array('default' => ''),
        'max' => array('default' => ''),
        'value' => array('default' => ''),
      ),
    );

    return $options;
  }

  function operators() {
    $operators = array(
      '<' => array(
        'title' => t('Is less than'),
43
        'method' => 'opSimple',
44 45 46 47 48
        'short' => t('<'),
        'values' => 1,
      ),
      '<=' => array(
        'title' => t('Is less than or equal to'),
49
        'method' => 'opSimple',
50 51 52 53 54
        'short' => t('<='),
        'values' => 1,
      ),
      '=' => array(
        'title' => t('Is equal to'),
55
        'method' => 'opSimple',
56 57 58 59 60
        'short' => t('='),
        'values' => 1,
      ),
      '!=' => array(
        'title' => t('Is not equal to'),
61
        'method' => 'opSimple',
62 63 64 65 66
        'short' => t('!='),
        'values' => 1,
      ),
      '>=' => array(
        'title' => t('Is greater than or equal to'),
67
        'method' => 'opSimple',
68 69 70 71 72
        'short' => t('>='),
        'values' => 1,
      ),
      '>' => array(
        'title' => t('Is greater than'),
73
        'method' => 'opSimple',
74 75 76 77 78
        'short' => t('>'),
        'values' => 1,
      ),
      'between' => array(
        'title' => t('Is between'),
79
        'method' => 'opBetween',
80 81 82 83 84
        'short' => t('between'),
        'values' => 2,
      ),
      'not between' => array(
        'title' => t('Is not between'),
85
        'method' => 'opBetween',
86 87 88
        'short' => t('not between'),
        'values' => 2,
      ),
89 90 91 92 93 94
      'regular_expression' => array(
        'title' => t('Regular expression'),
        'short' => t('regex'),
        'method' => 'op_regex',
        'values' => 1,
      ),
95 96 97 98 99 100 101
    );

    // if the definition allows for the empty operator, add it.
    if (!empty($this->definition['allow empty'])) {
      $operators += array(
        'empty' => array(
          'title' => t('Is empty (NULL)'),
102
          'method' => 'opEmpty',
103 104 105 106 107
          'short' => t('empty'),
          'values' => 0,
        ),
        'not empty' => array(
          'title' => t('Is not empty (NOT NULL)'),
108
          'method' => 'opEmpty',
109 110 111 112 113 114 115 116 117 118 119 120
          'short' => t('not empty'),
          'values' => 0,
        ),
      );
    }

    return $operators;
  }

  /**
   * Provide a list of all the numeric operators
   */
121
  public function operatorOptions($which = 'title') {
122 123 124 125 126 127 128 129
    $options = array();
    foreach ($this->operators() as $id => $info) {
      $options[$id] = $info[$which];
    }

    return $options;
  }

130
  protected function operatorValues($values = 1) {
131 132 133 134 135 136 137 138 139 140 141 142
    $options = array();
    foreach ($this->operators() as $id => $info) {
      if ($info['values'] == $values) {
        $options[] = $id;
      }
    }

    return $options;
  }
  /**
   * Provide a simple textfield for equality
   */
143
  protected function valueForm(&$form, FormStateInterface $form_state) {
144 145 146 147 148 149 150 151
    $form['value']['#tree'] = TRUE;

    // We have to make some choices when creating this as an exposed
    // filter form. For example, if the operator is locked and thus
    // not rendered, we can't render dependencies; instead we only
    // render the form items we need.
    $which = 'all';
    if (!empty($form['operator'])) {
152
      $source = ':input[name="options[operator]"]';
153 154
    }

155
    if ($exposed = $form_state->get('exposed')) {
156 157 158 159
      $identifier = $this->options['expose']['identifier'];

      if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
        // exposed and locked.
160
        $which = in_array($this->operator, $this->operatorValues(2)) ? 'minmax' : 'value';
161 162
      }
      else {
163
        $source = ':input[name="' . $this->options['expose']['operator_id'] . '"]';
164 165 166
      }
    }

167
    $user_input = $form_state->getUserInput();
168 169 170
    if ($which == 'all') {
      $form['value']['value'] = array(
        '#type' => 'textfield',
171
        '#title' => !$exposed ? t('Value') : '',
172 173 174
        '#size' => 30,
        '#default_value' => $this->value['value'],
      );
175
      // Setup #states for all operators with one value.
176
      foreach ($this->operatorValues(1) as $operator) {
177 178 179 180
        $form['value']['value']['#states']['visible'][] = array(
          $source => array('value' => $operator),
        );
      }
181
      if ($exposed && !isset($user_input[$identifier]['value'])) {
182 183
        $user_input[$identifier]['value'] = $this->value['value'];
        $form_state->setUserInput($user_input);
184 185 186 187 188 189 190
      }
    }
    elseif ($which == 'value') {
      // When exposed we drop the value-value and just do value if
      // the operator is locked.
      $form['value'] = array(
        '#type' => 'textfield',
191
        '#title' => !$exposed ? t('Value') : '',
192 193 194
        '#size' => 30,
        '#default_value' => $this->value['value'],
      );
195
      if ($exposed && !isset($user_input[$identifier])) {
196 197
        $user_input[$identifier] = $this->value['value'];
        $form_state->setUserInput($user_input);
198 199 200 201 202 203
      }
    }

    if ($which == 'all' || $which == 'minmax') {
      $form['value']['min'] = array(
        '#type' => 'textfield',
204
        '#title' => !$exposed ? t('Min') : '',
205 206 207 208 209
        '#size' => 30,
        '#default_value' => $this->value['min'],
      );
      $form['value']['max'] = array(
        '#type' => 'textfield',
210
        '#title' => !$exposed ? t('And max') : t('And'),
211 212 213 214
        '#size' => 30,
        '#default_value' => $this->value['max'],
      );
      if ($which == 'all') {
215 216
        $states = array();
        // Setup #states for all operators with two values.
217
        foreach ($this->operatorValues(2) as $operator) {
218 219 220 221 222 223
          $states['#states']['visible'][] = array(
            $source => array('value' => $operator),
          );
        }
        $form['value']['min'] += $states;
        $form['value']['max'] += $states;
224
      }
225
      if ($exposed && !isset($user_input[$identifier]['min'])) {
226
        $user_input[$identifier]['min'] = $this->value['min'];
227
      }
228
      if ($exposed && !isset($user_input[$identifier]['max'])) {
229
        $user_input[$identifier]['max'] = $this->value['max'];
230 231 232 233 234 235 236 237 238 239 240 241
      }

      if (!isset($form['value'])) {
        // Ensure there is something in the 'value'.
        $form['value'] = array(
          '#type' => 'value',
          '#value' => NULL
        );
      }
    }
  }

242
  public function query() {
243
    $this->ensureMyTable();
244
    $field = "$this->tableAlias.$this->realField";
245 246 247 248 249 250 251

    $info = $this->operators();
    if (!empty($info[$this->operator]['method'])) {
      $this->{$info[$this->operator]['method']}($field);
    }
  }

252
  protected function opBetween($field) {
253
    if ($this->operator == 'between') {
254
      $this->query->addWhere($this->options['group'], $field, array($this->value['min'], $this->value['max']), 'BETWEEN');
255 256
    }
    else {
257
      $this->query->addWhere($this->options['group'], db_or()->condition($field, $this->value['min'], '<=')->condition($field, $this->value['max'], '>='));
258 259 260
    }
  }

261
  protected function opSimple($field) {
262
    $this->query->addWhere($this->options['group'], $field, $this->value['value'], $this->operator);
263 264
  }

265
  protected function opEmpty($field) {
266 267 268 269 270 271 272
    if ($this->operator == 'empty') {
      $operator = "IS NULL";
    }
    else {
      $operator = "IS NOT NULL";
    }

273
    $this->query->addWhere($this->options['group'], $field, NULL, $operator);
274 275
  }

276 277 278 279 280 281
  /**
   * Filters by a regular expression.
   *
   * @param string $field
   *   The expression pointing to the queries field, for example "foo.bar".
   */
282
  protected function opRegex($field) {
283
    $this->query->addWhere($this->options['group'], $field, $this->value, 'REGEXP');
284 285
  }

286 287
  public function adminSummary() {
    if ($this->isAGroup()) {
288 289
      return t('grouped');
    }
290 291 292 293
    if (!empty($this->options['exposed'])) {
      return t('exposed');
    }

294
    $options = $this->operatorOptions('short');
295
    $output = UtilityString::checkPlain($options[$this->operator]);
296
    if (in_array($this->operator, $this->operatorValues(2))) {
297 298
      $output .= ' ' . t('@min and @max', array('@min' => $this->value['min'], '@max' => $this->value['max']));
    }
299
    elseif (in_array($this->operator, $this->operatorValues(1))) {
300
      $output .= ' ' . UtilityString::checkPlain($this->value['value']);
301 302 303 304 305 306 307
    }
    return $output;
  }

  /**
   * Do some minor translation of the exposed input
   */
308
  public function acceptExposedInput($input) {
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    if (empty($this->options['exposed'])) {
      return TRUE;
    }

    // rewrite the input value so that it's in the correct format so that
    // the parent gets the right data.
    if (!empty($this->options['expose']['identifier'])) {
      $value = &$input[$this->options['expose']['identifier']];
      if (!is_array($value)) {
        $value = array(
          'value' => $value,
        );
      }
    }

324
    $rc = parent::acceptExposedInput($input);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

    if (empty($this->options['expose']['required'])) {
      // We have to do some of our own checking for non-required filters.
      $info = $this->operators();
      if (!empty($info[$this->operator]['values'])) {
        switch ($info[$this->operator]['values']) {
          case 1:
            if ($value['value'] === '') {
              return FALSE;
            }
            break;
          case 2:
            if ($value['min'] === '' && $value['max'] === '') {
              return FALSE;
            }
            break;
        }
      }
    }

    return $rc;
  }
347

348
}