Project

General

Profile

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

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

1
<?php
2

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

    
8
/**
9
 * Basic argument handler for arguments that are numeric.
10
 *
11
 * Incorporates break_phrase.
12
 *
13
 * @ingroup views_argument_handlers
14
 */
15
class views_handler_argument_numeric extends views_handler_argument {
16

    
17
  /**
18
   * The operator used for the query: or|and.
19
   * @var string
20
   */
21
  public $operator;
22

    
23
  /**
24
   * The actual value which is used for querying.
25
   * @var array
26
   */
27
  public $value;
28

    
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function option_definition() {
33
    $options = parent::option_definition();
34

    
35
    $options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
36
    $options['not'] = array('default' => FALSE, 'bool' => TRUE);
37

    
38
    return $options;
39
  }
40

    
41
  /**
42
   * {@inheritdoc}
43
   */
44
  public function options_form(&$form, &$form_state) {
45
    parent::options_form($form, $form_state);
46

    
47
    // allow + for or, , for and
48
    $form['break_phrase'] = array(
49
      '#type' => 'checkbox',
50
      '#title' => t('Allow multiple values'),
51
      '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
52
      '#default_value' => !empty($this->options['break_phrase']),
53
      '#fieldset' => 'more',
54
    );
55

    
56
    $form['not'] = array(
57
      '#type' => 'checkbox',
58
      '#title' => t('Exclude'),
59
      '#description' => t('If selected, the numbers entered for the filter will be excluded rather than limiting the view.'),
60
      '#default_value' => !empty($this->options['not']),
61
      '#fieldset' => 'more',
62
    );
63
  }
64

    
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public function title() {
69
    if (!$this->argument) {
70
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
71
    }
72

    
73
    if (!empty($this->options['break_phrase'])) {
74
      views_break_phrase($this->argument, $this);
75
    }
76
    else {
77
      $this->value = array($this->argument);
78
      $this->operator = 'or';
79
    }
80

    
81
    if (empty($this->value)) {
82
      return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
83
    }
84

    
85
    if ($this->value === array(-1)) {
86
      return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
87
    }
88

    
89
    return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
90
  }
91

    
92
  /**
93
   * Override for specific title lookups.
94
   *
95
   * @return array
96
   *    Returns all titles, if it's just one title it's an array with one entry.
97
   */
98
  public function title_query() {
99
    return $this->value;
100
  }
101

    
102
  /**
103
   * {@inheritdoc}
104
   */
105
  public function query($group_by = FALSE) {
106
    $this->ensure_my_table();
107

    
108
    if (!empty($this->options['break_phrase'])) {
109
      views_break_phrase($this->argument, $this);
110
    }
111
    else {
112
      $this->value = array($this->argument);
113
    }
114

    
115
    $placeholder = $this->placeholder();
116
    $null_check = empty($this->options['not']) ? '' : "OR $this->table_alias.$this->real_field IS NULL";
117

    
118
    if (count($this->value) > 1) {
119
      $operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
120
      $this->query->add_where_expression(0, "$this->table_alias.$this->real_field $operator($placeholder) $null_check", array($placeholder => $this->value));
121
    }
122
    else {
123
      $operator = empty($this->options['not']) ? '=' : '!=';
124
      $this->query->add_where_expression(0, "$this->table_alias.$this->real_field $operator $placeholder $null_check", array($placeholder => $this->argument));
125
    }
126
  }
127

    
128
  /**
129
   * {@inheritdoc}
130
   */
131
  public function get_sort_name() {
132
    return t('Numerical', array(), array('context' => 'Sort order'));
133
  }
134

    
135
}