views_handler_argument_search.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_argument_search.
  5. */
  6. /**
  7. * Argument that accepts query keys for search.
  8. *
  9. * @ingroup views_argument_handlers
  10. */
  11. class views_handler_argument_search extends views_handler_argument {
  12. /**
  13. * Take sure that parseSearchExpression is runned and everything is set up for it.
  14. *
  15. * @param $input
  16. * The search phrase which was input by the user.
  17. */
  18. function query_parse_search_expression($input) {
  19. if (!isset($this->search_query)) {
  20. $this->search_query = db_select('search_index', 'i', array('target' => 'slave'))->extend('viewsSearchQuery');
  21. $this->search_query->searchExpression($input, $this->view->base_table);
  22. $this->search_query->publicParseSearchExpression();
  23. }
  24. }
  25. /**
  26. * Add this argument to the query.
  27. */
  28. function query($group_by = FALSE) {
  29. $required = FALSE;
  30. $this->query_parse_search_expression($this->argument);
  31. if (!isset($this->search_query)) {
  32. $required = TRUE;
  33. }
  34. else {
  35. $words = $this->search_query->words();
  36. if (empty($words)) {
  37. $required = TRUE;
  38. }
  39. }
  40. if ($required) {
  41. if ($this->operator == 'required') {
  42. $this->query->add_where(0, 'FALSE');
  43. }
  44. }
  45. else {
  46. $search_index = $this->ensure_my_table();
  47. $search_condition = db_and();
  48. // Create a new join to relate the 'search_total' table to our current 'search_index' table.
  49. $join = new views_join;
  50. $join->construct('search_total', $search_index, 'word', 'word');
  51. $search_total = $this->query->add_relationship('search_total', $join, $search_index);
  52. $this->search_score = $this->query->add_field('', "$search_index.score * $search_total.count", 'score', array('aggregate' => TRUE, 'function' => 'sum'));
  53. if (empty($this->query->relationships[$this->relationship])) {
  54. $base_table = $this->query->base_table;
  55. }
  56. else {
  57. $base_table = $this->query->relationships[$this->relationship]['base'];
  58. }
  59. $search_condition->condition("$search_index.type", $base_table);
  60. if (!$this->search_query->simple()) {
  61. $search_dataset = $this->query->add_table('search_dataset');
  62. $conditions = $this->search_query->conditions();
  63. $condition_conditions =& $conditions->conditions();
  64. foreach ($condition_conditions as $key => &$condition) {
  65. // Take sure we just look at real conditions.
  66. if (is_numeric($key)) {
  67. // Replace the conditions with the table alias of views.
  68. $this->search_query->condition_replace_string('d.', "$search_dataset.", $condition);
  69. }
  70. }
  71. $search_conditions =& $search_condition->conditions();
  72. $search_conditions = array_merge($search_conditions, $condition_conditions);
  73. }
  74. else {
  75. // Stores each condition, so and/or on the filter level will still work.
  76. $or = db_or();
  77. foreach ($words as $word) {
  78. $or->condition("$search_index.word", $word);
  79. }
  80. $search_condition->condition($or);
  81. }
  82. $this->query->add_where(0, $search_condition);
  83. $this->query->add_groupby("$search_index.sid");
  84. $matches = $this->search_query->matches();
  85. $placeholder = $this->placeholder();
  86. $this->query->add_having_expression(0, "COUNT(*) >= $placeholder", array($placeholder => $matches));
  87. }
  88. }
  89. }