views_handler_filter_search.inc

  1. 3.x modules/search/views_handler_filter_search.inc
  2. 2.x modules/search/views_handler_filter_search.inc

Contains a search filter handler.

File

modules/search/views_handler_filter_search.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains a search filter handler.
  5. */
  6. /**
  7. * Field handler to provide simple renderer that allows linking to a node.
  8. *
  9. * @ingroup views_filter_handlers
  10. */
  11. class views_handler_filter_search extends views_handler_filter {
  12. var $always_multiple = TRUE;
  13. /**
  14. * Stores a viewsSearchQuery object to be able to use the search.module "api".
  15. *
  16. * @var viewsSearchQuery
  17. */
  18. var $search_query = NULL;
  19. /**
  20. * Checks if the search query has been parsed.
  21. */
  22. var $parsed = FALSE;
  23. function option_definition() {
  24. $options = parent::option_definition();
  25. $options['operator']['default'] = 'optional';
  26. $options['remove_score'] = array('default' => FALSE, 'bool' => TRUE);
  27. return $options;
  28. }
  29. /**
  30. * Overrides views_handler_filter::options_form().
  31. *
  32. * Add an option to remove search scores from the query.
  33. */
  34. function options_form(&$form, &$form_state) {
  35. parent::options_form($form, $form_state);
  36. $form['remove_score'] = array(
  37. '#type' => 'checkbox',
  38. '#title' => t('Remove search score'),
  39. '#description' => t('Check this box to remove the search score from the query. This can help reduce help reduce duplicate search results when using this filter.'),
  40. '#default_value' => $this->options['remove_score'],
  41. );
  42. }
  43. /**
  44. * Provide simple equality operator
  45. */
  46. function operator_form(&$form, &$form_state) {
  47. $form['operator'] = array(
  48. '#type' => 'radios',
  49. '#title' => t('On empty input'),
  50. '#default_value' => $this->operator,
  51. '#options' => array(
  52. 'optional' => t('Show All'),
  53. 'required' => t('Show None'),
  54. ),
  55. );
  56. }
  57. /**
  58. * Provide a simple textfield for equality
  59. */
  60. function value_form(&$form, &$form_state) {
  61. $form['value'] = array(
  62. '#type' => 'textfield',
  63. '#size' => 15,
  64. '#default_value' => $this->value,
  65. '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
  66. '#title' => empty($form_state['exposed']) ? t('Value') : '',
  67. );
  68. }
  69. /**
  70. * Validate the options form.
  71. */
  72. function exposed_validate(&$form, &$form_state) {
  73. if (!isset($this->options['expose']['identifier'])) {
  74. return;
  75. }
  76. $key = $this->options['expose']['identifier'];
  77. if (!empty($form_state['values'][$key])) {
  78. $this->query_parse_search_expression($form_state['values'][$key]);
  79. if (count($this->search_query->words()) == 0) {
  80. 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.'));
  81. }
  82. }
  83. }
  84. /**
  85. * Take sure that parseSearchExpression is runned and everything is set up for it.
  86. *
  87. * @param $input
  88. * The search phrase which was input by the user.
  89. */
  90. function query_parse_search_expression($input) {
  91. if (!isset($this->search_query)) {
  92. $this->parsed = TRUE;
  93. $this->search_query = db_select('search_index', 'i', array('target' => 'slave'))->extend('viewsSearchQuery');
  94. $this->search_query->searchExpression($input, $this->view->base_table);
  95. $this->search_query->publicParseSearchExpression();
  96. }
  97. }
  98. /**
  99. * Add this filter to the query.
  100. *
  101. * Due to the nature of fapi, the value and the operator have an unintended
  102. * level of indirection. You will find them in $this->operator
  103. * and $this->value respectively.
  104. */
  105. function query() {
  106. // Since attachment views don't validate the exposed input, parse the search
  107. // expression if required.
  108. if (!$this->parsed) {
  109. $this->query_parse_search_expression($this->value);
  110. }
  111. $required = FALSE;
  112. if (!isset($this->search_query)) {
  113. $required = TRUE;
  114. }
  115. else {
  116. $words = $this->search_query->words();
  117. if (empty($words)) {
  118. $required = TRUE;
  119. }
  120. }
  121. if ($required) {
  122. if ($this->operator == 'required') {
  123. $this->query->add_where($this->options['group'], 'FALSE');
  124. }
  125. }
  126. else {
  127. $search_index = $this->ensure_my_table();
  128. $search_condition = db_and();
  129. if (!$this->options['remove_score']) {
  130. // Create a new join to relate the 'serach_total' table to our current 'search_index' table.
  131. $join = new views_join;
  132. $join->construct('search_total', $search_index, 'word', 'word');
  133. $search_total = $this->query->add_relationship('search_total', $join, $search_index);
  134. $this->search_score = $this->query->add_field('', "SUM($search_index.score * $search_total.count)", 'score', array('aggregate' => TRUE));
  135. }
  136. if (empty($this->query->relationships[$this->relationship])) {
  137. $base_table = $this->query->base_table;
  138. }
  139. else {
  140. $base_table = $this->query->relationships[$this->relationship]['base'];
  141. }
  142. $search_condition->condition("$search_index.type", $base_table);
  143. if (!$this->search_query->simple()) {
  144. $search_dataset = $this->query->add_table('search_dataset');
  145. $conditions = $this->search_query->conditions();
  146. $condition_conditions =& $conditions->conditions();
  147. foreach ($condition_conditions as $key => &$condition) {
  148. // Take sure we just look at real conditions.
  149. if (is_numeric($key)) {
  150. // Replace the conditions with the table alias of views.
  151. $this->search_query->condition_replace_string('d.', "$search_dataset.", $condition);
  152. }
  153. }
  154. $search_conditions =& $search_condition->conditions();
  155. $search_conditions = array_merge($search_conditions, $condition_conditions);
  156. }
  157. else {
  158. // Stores each condition, so and/or on the filter level will still work.
  159. $or = db_or();
  160. foreach ($words as $word) {
  161. $or->condition("$search_index.word", $word);
  162. }
  163. $search_condition->condition($or);
  164. }
  165. $this->query->add_where($this->options['group'], $search_condition);
  166. $this->query->add_groupby("$search_index.sid");
  167. $matches = $this->search_query->matches();
  168. $placeholder = $this->placeholder();
  169. $this->query->add_having_expression($this->options['group'], "COUNT(*) >= $placeholder", array($placeholder => $matches));
  170. }
  171. // Set to NULL to prevent PDO exception when views object is cached.
  172. $this->search_query = NULL;
  173. }
  174. }
  175. /**
  176. * Extends the core SearchQuery.
  177. */
  178. class viewsSearchQuery extends SearchQuery {
  179. public function &conditions() {
  180. return $this->conditions;
  181. }
  182. public function words() {
  183. return $this->words;
  184. }
  185. public function simple() {
  186. return $this->simple;
  187. }
  188. public function matches() {
  189. return $this->matches;
  190. }
  191. public function publicParseSearchExpression() {
  192. return $this->parseSearchExpression();
  193. }
  194. function condition_replace_string($search, $replace, &$condition) {
  195. if ($condition['field'] instanceof DatabaseCondition) {
  196. $conditions =& $condition['field']->conditions();
  197. foreach ($conditions as $key => &$subcondition) {
  198. if (is_numeric($key)) {
  199. $this->condition_replace_string($search, $replace, $subcondition);
  200. }
  201. }
  202. }
  203. else {
  204. $condition['field'] = str_replace($search, $replace, $condition['field']);
  205. }
  206. }
  207. }