4 * Field handler to provide simple renderer that allows linking to a node.
6 class views_handler_filter_search
extends views_handler_filter
{
8 function option_definition() {
9 $options = parent
::option_definition();
11 $options['operator']['default'] = 'optional';
17 * Provide simple equality operator
19 function operator_form(&$form, &$form_state) {
20 $form['operator'] = array(
22 '#title' => t('On empty input'),
23 '#default_value' => $this->operator
,
25 'optional' => t('Show All'),
26 'required' => t('Show None'),
32 * Provide a simple textfield for equality
34 function exposed_form(&$form, &$form_state) {
35 if (isset($this->options
['expose']['identifier'])) {
36 $key = $this->options
['expose']['identifier'];
38 '#type' => 'textfield',
40 '#default_value' => $this->value
,
41 '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
47 * Validate the options form.
49 function exposed_validate($form, &$form_state) {
50 if (!isset($this->options
['expose']['identifier'])) {
54 $key = $this->options
['expose']['identifier'];
55 if (!empty($form_state['values'][$key])) {
56 $this->search_query
= search_parse_query($form_state['values'][$key]);
58 if ($this->search_query
[2] == '') {
59 form_set_error($key, t('You must include at least one positive keyword with @count characters or more.', array('@count' => variable_get('minimum_word_size', 3))));
61 if ($this->search_query
[6]) {
62 if ($this->search_query
[6] == 'or') {
63 drupal_set_message(t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'));
70 * Add this filter to the query.
72 * Due to the nature of fapi, the value and the operator have an unintended
73 * level of indirection. You will find them in $this->operator
74 * and $this->value respectively.
77 if (!isset($this->search_query
) || empty($this->search_query
[3])) {
78 if ($this->operator
== 'required') {
79 $this->query
->add_where($this->options
['group'], '0');
83 $search_index = $this->ensure_my_table();
84 $this->search_query
[2] = str_replace('i.', "$search_index.", $this->search_query
[2]);
86 // Create a new join to relate the 'serach_total' table to our current 'search_index' table.
87 $join = new views_join
;
88 $join->construct('search_total', $search_index, 'word', 'word');
89 $search_total = $this->query
->add_relationship('search_total', $join, $search_index);
91 $this->search_score
= $this->query
->add_field('', "SUM($search_index.score * $search_total.count)", 'score', array('aggregate' => TRUE
));
93 $this->query
->add_where($this->options
['group'], $this->search_query
[2], $this->search_query
[3]);
95 if (empty($this->query
->relationships
[$this->relationship
])) {
96 $base_table = $this->query
->base_table
;
99 $base_table = $this->query
->relationships
[$this->relationship
]['base'];
101 $this->query
->add_where($this->options
['group'], "$search_index.type = '%s'", $base_table);
102 if (!$this->search_query
[5]) {
103 $search_dataset = $this->query
->add_table('search_dataset');
104 $this->search_query
[0] = str_replace('d.', "$search_dataset.", $this->search_query
[0]);
105 $this->query
->add_where($this->options
['group'], $this->search_query
[0], $this->search_query
[1]);
108 $this->query
->add_groupby("$search_index.sid");
109 $this->query
->add_having($this->options
['group'], 'COUNT(*) >= %d', $this->search_query
[4]);