Stripping CVS keywords
[project/views.git] / modules / search / views_handler_filter_search.inc
1 <?php
2
3 /**
4 * Field handler to provide simple renderer that allows linking to a node.
5 */
6 class views_handler_filter_search extends views_handler_filter {
7 var $no_single = TRUE;
8 function option_definition() {
9 $options = parent::option_definition();
10
11 $options['operator']['default'] = 'optional';
12
13 return $options;
14 }
15
16 /**
17 * Provide simple equality operator
18 */
19 function operator_form(&$form, &$form_state) {
20 $form['operator'] = array(
21 '#type' => 'radios',
22 '#title' => t('On empty input'),
23 '#default_value' => $this->operator,
24 '#options' => array(
25 'optional' => t('Show All'),
26 'required' => t('Show None'),
27 ),
28 );
29 }
30
31 /**
32 * Provide a simple textfield for equality
33 */
34 function exposed_form(&$form, &$form_state) {
35 if (isset($this->options['expose']['identifier'])) {
36 $key = $this->options['expose']['identifier'];
37 $form[$key] = array(
38 '#type' => 'textfield',
39 '#size' => 15,
40 '#default_value' => $this->value,
41 '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
42 );
43 }
44 }
45
46 /**
47 * Validate the options form.
48 */
49 function exposed_validate($form, &$form_state) {
50 if (!isset($this->options['expose']['identifier'])) {
51 return;
52 }
53
54 $key = $this->options['expose']['identifier'];
55 if (!empty($form_state['values'][$key])) {
56 $this->search_query = search_parse_query($form_state['values'][$key]);
57
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))));
60 }
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>.'));
64 }
65 }
66 }
67 }
68
69 /**
70 * Add this filter to the query.
71 *
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.
75 */
76 function query() {
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');
80 }
81 }
82 else {
83 $search_index = $this->ensure_my_table();
84 $this->search_query[2] = str_replace('i.', "$search_index.", $this->search_query[2]);
85
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);
90
91 $this->search_score = $this->query->add_field('', "SUM($search_index.score * $search_total.count)", 'score', array('aggregate' => TRUE));
92
93 $this->query->add_where($this->options['group'], $this->search_query[2], $this->search_query[3]);
94
95 if (empty($this->query->relationships[$this->relationship])) {
96 $base_table = $this->query->base_table;
97 }
98 else {
99 $base_table = $this->query->relationships[$this->relationship]['base'];
100 }
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]);
106 }
107
108 $this->query->add_groupby("$search_index.sid");
109 $this->query->add_having($this->options['group'], 'COUNT(*) >= %d', $this->search_query[4]);
110 }
111 }
112 }