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

File

modules/search/views_handler_filter_search.inc
View source
  1. <?php
  2. /**
  3. * Field handler to provide simple renderer that allows linking to a node.
  4. */
  5. class views_handler_filter_search extends views_handler_filter {
  6. var $no_single = TRUE;
  7. function option_definition() {
  8. $options = parent::option_definition();
  9. $options['operator']['default'] = 'optional';
  10. return $options;
  11. }
  12. /**
  13. * Provide simple equality operator
  14. */
  15. function operator_form(&$form, &$form_state) {
  16. $form['operator'] = array(
  17. '#type' => 'radios',
  18. '#title' => t('On empty input'),
  19. '#default_value' => $this->operator,
  20. '#options' => array(
  21. 'optional' => t('Show All'),
  22. 'required' => t('Show None'),
  23. ),
  24. );
  25. }
  26. /**
  27. * Provide a simple textfield for equality
  28. */
  29. function exposed_form(&$form, &$form_state) {
  30. if (isset($this->options['expose']['identifier'])) {
  31. $key = $this->options['expose']['identifier'];
  32. $form[$key] = array(
  33. '#type' => 'textfield',
  34. '#size' => 15,
  35. '#default_value' => $this->value,
  36. '#attributes' => array('title' => t('Enter the terms you wish to search for.')),
  37. );
  38. }
  39. }
  40. /**
  41. * Validate the options form.
  42. */
  43. function exposed_validate($form, &$form_state) {
  44. if (!isset($this->options['expose']['identifier'])) {
  45. return;
  46. }
  47. $key = $this->options['expose']['identifier'];
  48. if (!empty($form_state['values'][$key])) {
  49. $this->search_query = search_parse_query($form_state['values'][$key]);
  50. if ($this->search_query[2] == '') {
  51. 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))));
  52. }
  53. if ($this->search_query[6]) {
  54. if ($this->search_query[6] == 'or') {
  55. drupal_set_message(t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'));
  56. }
  57. }
  58. }
  59. }
  60. /**
  61. * Add this filter to the query.
  62. *
  63. * Due to the nature of fapi, the value and the operator have an unintended
  64. * level of indirection. You will find them in $this->operator
  65. * and $this->value respectively.
  66. */
  67. function query() {
  68. if (!isset($this->search_query) || empty($this->search_query[3])) {
  69. if ($this->operator == 'required') {
  70. $this->query->add_where($this->options['group'], 'FALSE');
  71. }
  72. }
  73. else {
  74. $search_index = $this->ensure_my_table();
  75. $this->search_query[2] = str_replace('i.', "$search_index.", $this->search_query[2]);
  76. // Create a new join to relate the 'serach_total' table to our current 'search_index' table.
  77. $join = new views_join;
  78. $join->construct('search_total', $search_index, 'word', 'word');
  79. $search_total = $this->query->add_relationship('search_total', $join, $search_index);
  80. $this->search_score = $this->query->add_field('', "SUM($search_index.score * $search_total.count)", 'score', array('aggregate' => TRUE));
  81. $this->query->add_where($this->options['group'], $this->search_query[2], $this->search_query[3]);
  82. if (empty($this->query->relationships[$this->relationship])) {
  83. $base_table = $this->query->base_table;
  84. }
  85. else {
  86. $base_table = $this->query->relationships[$this->relationship]['base'];
  87. }
  88. $this->query->add_where($this->options['group'], "$search_index.type = '%s'", $base_table);
  89. if (!$this->search_query[5]) {
  90. $search_dataset = $this->query->add_table('search_dataset');
  91. $this->search_query[0] = str_replace('d.', "$search_dataset.", $this->search_query[0]);
  92. $this->query->add_where($this->options['group'], $this->search_query[0], $this->search_query[1]);
  93. }
  94. $this->query->add_groupby("$search_index.sid");
  95. $this->query->add_having($this->options['group'], 'COUNT(*) >= %d', $this->search_query[4]);
  96. }
  97. }
  98. }