Projet

Général

Profil

Paste
Télécharger (6,25 ko) Statistiques
| Branche: | Révision:

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

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Definition of views_handler_filter_combine.
6
 */
7
8
/**
9
 * Filter handler which allows to search on multiple fields.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class views_handler_filter_combine extends views_handler_filter_string {
14 5d12d676 Assos Assos
15 85ad3d82 Assos Assos
  /**
16
   * @var views_plugin_query_default
17
   */
18
  public $query;
19
20 5d12d676 Assos Assos
  /**
21
   * {@inheritdoc}
22
   */
23
  public function option_definition() {
24 85ad3d82 Assos Assos
    $options = parent::option_definition();
25
    $options['fields'] = array('default' => array());
26
27
    return $options;
28
  }
29
30 5d12d676 Assos Assos
  /**
31
   * {@inheritdoc}
32
   */
33
  public function options_form(&$form, &$form_state) {
34 85ad3d82 Assos Assos
    parent::options_form($form, $form_state);
35
    $this->view->init_style();
36
37
    // Allow to choose all fields as possible.
38
    if ($this->view->style_plugin->uses_fields()) {
39
      $options = array();
40
      foreach ($this->view->display_handler->get_handlers('field') as $name => $field) {
41
        $options[$name] = $field->ui_name(TRUE);
42
      }
43
      if ($options) {
44
        $form['fields'] = array(
45
          '#type' => 'select',
46
          '#title' => t('Choose fields to combine for filtering'),
47
          '#description' => t("This filter doesn't work for very special field handlers."),
48
          '#multiple' => TRUE,
49
          '#options' => $options,
50
          '#default_value' => $this->options['fields'],
51
        );
52
      }
53
      else {
54
        form_set_error('', t('You have to add some fields to be able to use this filter.'));
55
      }
56
    }
57
  }
58
59 5d12d676 Assos Assos
  /**
60
   * {@inheritdoc}
61
   */
62
  public function query() {
63 85ad3d82 Assos Assos
    $this->view->_build('field');
64
    $fields = array();
65
    // Only add the fields if they have a proper field and table alias.
66
    foreach ($this->options['fields'] as $id) {
67 d719f12f Assos Assos
      // Field access checks may have removed this handler.
68
      if (!isset($this->view->field[$id])) {
69
        continue;
70
      }
71
72 85ad3d82 Assos Assos
      $field = $this->view->field[$id];
73
      // Always add the table of the selected fields to be sure a table alias
74
      // exists.
75
      $field->ensure_my_table();
76 8be7bf84 Assos Assos
      if (!empty($field->table_alias) && !empty($field->real_field)) {
77 85ad3d82 Assos Assos
        $fields[] = "$field->table_alias.$field->real_field";
78
      }
79
    }
80
    if ($fields) {
81
      $count = count($fields);
82
      $separated_fields = array();
83
      foreach ($fields as $key => $field) {
84
        $separated_fields[] = $field;
85
        if ($key < $count - 1) {
86
          $separated_fields[] = "' '";
87
        }
88
      }
89
      $expression = implode(', ', $separated_fields);
90
      $expression = "CONCAT_WS(' ', $expression)";
91
92
      $info = $this->operators();
93
      if (!empty($info[$this->operator]['method'])) {
94
        $this->{$info[$this->operator]['method']}($expression);
95
      }
96
    }
97
  }
98
99 5d12d676 Assos Assos
  /**
100
   * By default things like op_equal uses add_where, that doesn't support
101
   * complex expressions, so override all operators.
102
   */
103
  public function op_equal($field) {
104 85ad3d82 Assos Assos
    $placeholder = $this->placeholder();
105
    $operator = $this->operator();
106
    $this->query->add_where_expression($this->options['group'], "$field $operator $placeholder", array($placeholder => $this->value));
107
  }
108
109 5d12d676 Assos Assos
  /**
110
   *
111
   */
112
  public function op_contains($field) {
113 85ad3d82 Assos Assos
    $placeholder = $this->placeholder();
114
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
115
  }
116
117 5d12d676 Assos Assos
  /**
118
   *
119
   */
120
  public function op_word($field) {
121 85ad3d82 Assos Assos
    $where = $this->operator == 'word' ? db_or() : db_and();
122
123
    // Don't filter on empty strings.
124
    if (empty($this->value)) {
125
      return;
126
    }
127
128
    preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
129
    foreach ($matches as $match) {
130
      $phrase = FALSE;
131
      // Strip off phrase quotes.
132 4003efde Assos Assos
      if ($match[2][0] == '"') {
133 85ad3d82 Assos Assos
        $match[2] = substr($match[2], 1, -1);
134
        $phrase = TRUE;
135
      }
136
      $words = trim($match[2], ',?!();:-');
137
      $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
138
      $placeholder = $this->placeholder();
139
      foreach ($words as $word) {
140
        $where->where($field . " LIKE $placeholder", array($placeholder => '%' . db_like(trim($word, " ,!?")) . '%'));
141
      }
142
    }
143
144
    if (!$where) {
145
      return;
146
    }
147
148
    // Previously this was a call_user_func_array() but that's unnecessary
149
    // as views will unpack an array that is a single arg.
150
    $this->query->add_where($this->options['group'], $where);
151
  }
152
153 5d12d676 Assos Assos
  /**
154
   *
155
   */
156
  public function op_starts($field) {
157 85ad3d82 Assos Assos
    $placeholder = $this->placeholder();
158
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
159
  }
160
161 5d12d676 Assos Assos
  /**
162
   *
163
   */
164
  public function op_not_starts($field) {
165 85ad3d82 Assos Assos
    $placeholder = $this->placeholder();
166
    $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
167
  }
168
169 5d12d676 Assos Assos
  /**
170
   *
171
   */
172
  public function op_ends($field) {
173 85ad3d82 Assos Assos
    $placeholder = $this->placeholder();
174
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
175
  }
176
177 5d12d676 Assos Assos
  /**
178
   *
179
   */
180
  public function op_not_ends($field) {
181 85ad3d82 Assos Assos
    $placeholder = $this->placeholder();
182
    $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
183
  }
184
185 5d12d676 Assos Assos
  /**
186
   *
187
   */
188
  public function op_not($field) {
189 85ad3d82 Assos Assos
    $placeholder = $this->placeholder();
190
    $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
191
  }
192
193 5d12d676 Assos Assos
  /**
194
   *
195
   */
196
  public function op_regex($field) {
197 85ad3d82 Assos Assos
    $placeholder = $this->placeholder();
198
    $this->query->add_where_expression($this->options['group'], "$field RLIKE $placeholder", array($placeholder => $this->value));
199
  }
200
201 8be7bf84 Assos Assos
  /**
202
   *
203
   */
204
  public function op_not_regex($field) {
205
    $placeholder = $this->placeholder();
206
    $this->query->add_where_expression($this->options['group'], "$field NOT RLIKE $placeholder", array($placeholder => $this->value));
207
  }
208
209 5d12d676 Assos Assos
  /**
210
   *
211
   */
212
  public function op_empty($field) {
213 85ad3d82 Assos Assos
    if ($this->operator == 'empty') {
214
      $operator = "IS NULL";
215
    }
216
    else {
217
      $operator = "IS NOT NULL";
218
    }
219
220
    $this->query->add_where_expression($this->options['group'], "$field $operator");
221
  }
222 5d12d676 Assos Assos
223 85ad3d82 Assos Assos
}