Projet

Général

Profil

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

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

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

    
77
      $info = $this->operators();
78
      if (!empty($info[$this->operator]['method'])) {
79
        $this->{$info[$this->operator]['method']}($expression);
80
      }
81
    }
82
  }
83

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

    
92
  function op_contains($field) {
93
    $placeholder = $this->placeholder();
94
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
95
  }
96

    
97
  function op_word($field) {
98
    $where = $this->operator == 'word' ? db_or() : db_and();
99

    
100
    // Don't filter on empty strings.
101
    if (empty($this->value)) {
102
      return;
103
    }
104

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

    
121
    if (!$where) {
122
      return;
123
    }
124

    
125
    // Previously this was a call_user_func_array() but that's unnecessary
126
    // as views will unpack an array that is a single arg.
127
    $this->query->add_where($this->options['group'], $where);
128
  }
129

    
130
  function op_starts($field) {
131
    $placeholder = $this->placeholder();
132
    $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
133
  }
134

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

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

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

    
150
  function op_not($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_regex($field) {
156
    $placeholder = $this->placeholder();
157
    $this->query->add_where_expression($this->options['group'], "$field RLIKE $placeholder", array($placeholder => $this->value));
158
  }
159

    
160
  function op_empty($field) {
161
    if ($this->operator == 'empty') {
162
      $operator = "IS NULL";
163
    }
164
    else {
165
      $operator = "IS NOT NULL";
166
    }
167

    
168
    $this->query->add_where_expression($this->options['group'], "$field $operator");
169
  }
170
}