Projet

Général

Profil

Paste
Télécharger (5,68 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_filter_combine.inc @ 651307cd

1
<?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
  /**
15
   * @var views_plugin_query_default
16
   */
17
  public $query;
18

    
19
  function option_definition() {
20
    $options = parent::option_definition();
21
    $options['fields'] = array('default' => array());
22

    
23
    return $options;
24
  }
25

    
26
  function options_form(&$form, &$form_state) {
27
    parent::options_form($form, $form_state);
28
    $this->view->init_style();
29

    
30
    // Allow to choose all fields as possible.
31
    if ($this->view->style_plugin->uses_fields()) {
32
      $options = array();
33
      foreach ($this->view->display_handler->get_handlers('field') as $name => $field) {
34
        $options[$name] = $field->ui_name(TRUE);
35
      }
36
      if ($options) {
37
        $form['fields'] = array(
38
          '#type' => 'select',
39
          '#title' => t('Choose fields to combine for filtering'),
40
          '#description' => t("This filter doesn't work for very special field handlers."),
41
          '#multiple' => TRUE,
42
          '#options' => $options,
43
          '#default_value' => $this->options['fields'],
44
        );
45
      }
46
      else {
47
        form_set_error('', t('You have to add some fields to be able to use this filter.'));
48
      }
49
    }
50
  }
51

    
52
  function query() {
53
    $this->view->_build('field');
54
    $fields = array();
55
    // Only add the fields if they have a proper field and table alias.
56
    foreach ($this->options['fields'] as $id) {
57
      // Field access checks may have removed this handler.
58
      if (!isset($this->view->field[$id])) {
59
        continue;
60
      }
61

    
62
      $field = $this->view->field[$id];
63
      // Always add the table of the selected fields to be sure a table alias
64
      // exists.
65
      $field->ensure_my_table();
66
      if (!empty($field->field_alias) && !empty($field->field_alias)) {
67
        $fields[] = "$field->table_alias.$field->real_field";
68
      }
69
    }
70
    if ($fields) {
71
      $count = count($fields);
72
      $separated_fields = array();
73
      foreach ($fields as $key => $field) {
74
        $separated_fields[] = $field;
75
        if ($key < $count - 1) {
76
          $separated_fields[] = "' '";
77
        }
78
      }
79
      $expression = implode(', ', $separated_fields);
80
      $expression = "CONCAT_WS(' ', $expression)";
81

    
82
      $info = $this->operators();
83
      if (!empty($info[$this->operator]['method'])) {
84
        $this->{$info[$this->operator]['method']}($expression);
85
      }
86
    }
87
  }
88

    
89
  // By default things like op_equal uses add_where, that doesn't support
90
  // complex expressions, so override all operators.
91
  function op_equal($field) {
92
    $placeholder = $this->placeholder();
93
    $operator = $this->operator();
94
    $this->query->add_where_expression($this->options['group'], "$field $operator $placeholder", array($placeholder => $this->value));
95
  }
96

    
97
  function op_contains($field) {
98
    $placeholder = $this->placeholder();
99
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
100
  }
101

    
102
  function op_word($field) {
103
    $where = $this->operator == 'word' ? db_or() : db_and();
104

    
105
    // Don't filter on empty strings.
106
    if (empty($this->value)) {
107
      return;
108
    }
109

    
110
    preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
111
    foreach ($matches as $match) {
112
      $phrase = FALSE;
113
      // Strip off phrase quotes.
114
      if ($match[2]{0} == '"') {
115
        $match[2] = substr($match[2], 1, -1);
116
        $phrase = TRUE;
117
      }
118
      $words = trim($match[2], ',?!();:-');
119
      $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
120
      $placeholder = $this->placeholder();
121
      foreach ($words as $word) {
122
        $where->where($field . " LIKE $placeholder", array($placeholder => '%' . db_like(trim($word, " ,!?")) . '%'));
123
      }
124
    }
125

    
126
    if (!$where) {
127
      return;
128
    }
129

    
130
    // Previously this was a call_user_func_array() but that's unnecessary
131
    // as views will unpack an array that is a single arg.
132
    $this->query->add_where($this->options['group'], $where);
133
  }
134

    
135
  function op_starts($field) {
136
    $placeholder = $this->placeholder();
137
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
138
  }
139

    
140
  function op_not_starts($field) {
141
    $placeholder = $this->placeholder();
142
    $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
143
  }
144

    
145
  function op_ends($field) {
146
    $placeholder = $this->placeholder();
147
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
148
  }
149

    
150
  function op_not_ends($field) {
151
    $placeholder = $this->placeholder();
152
    $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
153
  }
154

    
155
  function op_not($field) {
156
    $placeholder = $this->placeholder();
157
    $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
158
  }
159

    
160
  function op_regex($field) {
161
    $placeholder = $this->placeholder();
162
    $this->query->add_where_expression($this->options['group'], "$field RLIKE $placeholder", array($placeholder => $this->value));
163
  }
164

    
165
  function op_empty($field) {
166
    if ($this->operator == 'empty') {
167
      $operator = "IS NULL";
168
    }
169
    else {
170
      $operator = "IS NOT NULL";
171
    }
172

    
173
    $this->query->add_where_expression($this->options['group'], "$field $operator");
174
  }
175
}