Projet

Général

Profil

Paste
Télécharger (7,49 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / modules / search / views_handler_filter_search.inc @ 5d12d676

1
<?php
2

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

    
8
/**
9
 * Field handler to provide simple renderer that allows linking to a node.
10
 *
11
 * @ingroup views_filter_handlers
12
 */
13
class views_handler_filter_search extends views_handler_filter {
14

    
15
  /**
16
   *
17
   */
18
  public $always_multiple = TRUE;
19

    
20
  /**
21
   * Stores a viewsSearchQuery object to be able to use the search.module "api".
22
   *
23
   * @var viewsSearchQuery
24
   */
25
  public $search_query = NULL;
26

    
27
  /**
28
   * Checks if the search query has been parsed.
29
   */
30
  public $parsed = FALSE;
31

    
32
  /**
33
   * {@inheritdoc}
34
   */
35
  public function option_definition() {
36
    $options = parent::option_definition();
37

    
38
    $options['operator']['default'] = 'optional';
39
    $options['remove_score'] = array('default' => FALSE, 'bool' => TRUE);
40

    
41
    return $options;
42
  }
43

    
44
  /**
45
   * {@inheritdoc}
46
   */
47
  public function options_form(&$form, &$form_state) {
48
    parent::options_form($form, $form_state);
49

    
50
    // Add an option to remove search scores from the query.
51
    $form['remove_score'] = array(
52
      '#type' => 'checkbox',
53
      '#title' => t('Remove search score'),
54
      '#description' => t('Check this box to remove the search score from the query. This can help reduce duplicate search results when using this filter.'),
55
      '#default_value' => $this->options['remove_score'],
56
    );
57
  }
58

    
59
  /**
60
   * Provide simple equality operator.
61
   */
62
  public function operator_form(&$form, &$form_state) {
63
    $form['operator'] = array(
64
      '#type' => 'radios',
65
      '#title' => t('On empty input'),
66
      '#default_value' => $this->operator,
67
      '#options' => array(
68
        'optional' => t('Show All'),
69
        'required' => t('Show None'),
70
      ),
71
    );
72
  }
73

    
74
  /**
75
   * Provide a simple textfield for equality.
76
   */
77
  public function value_form(&$form, &$form_state) {
78
    $form['value'] = array(
79
      '#type' => 'textfield',
80
      '#size' => 15,
81
      '#default_value' => $this->value,
82
      '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
83
      '#title' => empty($form_state['exposed']) ? t('Value') : '',
84
    );
85
  }
86

    
87
  /**
88
   * Validate the options form.
89
   */
90
  public function exposed_validate(&$form, &$form_state) {
91
    if (!isset($this->options['expose']['identifier'])) {
92
      return;
93
    }
94

    
95
    $key = $this->options['expose']['identifier'];
96
    if (!empty($form_state['values'][$key])) {
97
      $this->query_parse_search_expression($form_state['values'][$key]);
98
      if (count($this->search_query->words()) == 0) {
99
        form_set_error($key, format_plural(variable_get('minimum_word_size', 3), 'You must include at least one positive keyword with 1 character or more.', 'You must include at least one positive keyword with @count characters or more.'));
100
      }
101
    }
102
  }
103

    
104
  /**
105
   * Make sure that parseSearchExpression is run and everything is set up.
106
   *
107
   * @param string $input
108
   *    The search phrase which was input by the user.
109
   */
110
  public function query_parse_search_expression($input) {
111
    if (!isset($this->search_query)) {
112
      $this->parsed = TRUE;
113
      $this->search_query = db_select('search_index', 'i', array('target' => 'slave'))->extend('viewsSearchQuery');
114
      $this->search_query->searchExpression($input, $this->view->base_table);
115
      $this->search_query->publicParseSearchExpression();
116
    }
117
  }
118

    
119
  /**
120
   * Add this filter to the query.
121
   *
122
   * Due to the nature of fapi, the value and the operator have an unintended
123
   * level of indirection. You will find them in $this->operator
124
   * and $this->value respectively.
125
   */
126
  public function query() {
127
    // Since attachment views don't validate the exposed input, parse the search
128
    // expression if required.
129
    if (!$this->parsed) {
130
      $this->query_parse_search_expression($this->value);
131
    }
132
    $required = FALSE;
133
    if (!isset($this->search_query)) {
134
      $required = TRUE;
135
    }
136
    else {
137
      $words = $this->search_query->words();
138
      if (empty($words)) {
139
        $required = TRUE;
140
      }
141
    }
142
    if ($required) {
143
      if ($this->operator == 'required') {
144
        $this->query->add_where($this->options['group'], 'FALSE');
145
      }
146
    }
147
    else {
148
      $search_index = $this->ensure_my_table();
149

    
150
      $search_condition = db_and();
151

    
152
      if (!$this->options['remove_score']) {
153
        // Create a new join to relate the 'serach_total' table to our current
154
        // 'search_index' table.
155
        $join = new views_join();
156
        $join->construct('search_total', $search_index, 'word', 'word');
157
        $search_total = $this->query->add_relationship('search_total', $join, $search_index);
158

    
159
        $this->search_score = $this->query->add_field('', "$search_index.score * $search_total.count", 'score', array('aggregate' => TRUE, 'function' => 'sum'));
160
      }
161

    
162
      if (empty($this->query->relationships[$this->relationship])) {
163
        $base_table = $this->query->base_table;
164
      }
165
      else {
166
        $base_table = $this->query->relationships[$this->relationship]['base'];
167
      }
168
      $search_condition->condition("$search_index.type", $base_table);
169
      if (!$this->search_query->simple()) {
170
        $search_dataset = $this->query->add_table('search_dataset');
171
        $conditions = $this->search_query->conditions();
172
        $condition_conditions =& $conditions->conditions();
173
        foreach ($condition_conditions as $key => &$condition) {
174
          // Take sure we just look at real conditions.
175
          if (is_numeric($key)) {
176
            // Replace the conditions with the table alias of views.
177
            $this->search_query->condition_replace_string('d.', "$search_dataset.", $condition);
178
          }
179
        }
180
        $search_conditions =& $search_condition->conditions();
181
        $search_conditions = array_merge($search_conditions, $condition_conditions);
182
      }
183
      else {
184
        // Stores each condition, so and/or on the filter level will still work.
185
        $or = db_or();
186
        foreach ($words as $word) {
187
          $or->condition("$search_index.word", $word);
188
        }
189

    
190
        $search_condition->condition($or);
191
      }
192

    
193
      $this->query->add_where($this->options['group'], $search_condition);
194
      $this->query->add_groupby("$search_index.sid");
195
      $matches = $this->search_query->matches();
196
      $placeholder = $this->placeholder();
197
      $this->query->add_having_expression($this->options['group'], "COUNT(*) >= $placeholder", array($placeholder => $matches));
198
    }
199
    // Set to NULL to prevent PDO exception when views object is cached.
200
    $this->search_query = NULL;
201
  }
202

    
203
}
204

    
205
/**
206
 * Extends the core SearchQuery.
207
 */
208
class viewsSearchQuery extends SearchQuery {
209

    
210
  /**
211
   * {@inheritdoc}
212
   */
213
  public function &conditions() {
214
    return $this->conditions;
215
  }
216

    
217
  /**
218
   * {@inheritdoc}
219
   */
220
  public function words() {
221
    return $this->words;
222
  }
223

    
224
  /**
225
   * {@inheritdoc}
226
   */
227
  public function simple() {
228
    return $this->simple;
229
  }
230

    
231
  /**
232
   * {@inheritdoc}
233
   */
234
  public function matches() {
235
    return $this->matches;
236
  }
237

    
238
  /**
239
   * {@inheritdoc}
240
   */
241
  public function publicParseSearchExpression() {
242
    return $this->parseSearchExpression();
243
  }
244

    
245
  /**
246
   * {@inheritdoc}
247
   */
248
  public function condition_replace_string($search, $replace, &$condition) {
249
    if ($condition['field'] instanceof DatabaseCondition) {
250
      $conditions =& $condition['field']->conditions();
251
      foreach ($conditions as $key => &$subcondition) {
252
        if (is_numeric($key)) {
253
          $this->condition_replace_string($search, $replace, $subcondition);
254
        }
255
      }
256
    }
257
    else {
258
      $condition['field'] = str_replace($search, $replace, $condition['field']);
259
    }
260
  }
261

    
262
}