views_handler_filter_combine.inc

Definition of views_handler_filter_combine.

File

handlers/views_handler_filter_combine.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_filter_combine.
  5. */
  6. /**
  7. * Filter handler which allows to search on multiple fields.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class views_handler_filter_combine extends views_handler_filter_string {
  12. /**
  13. * @var views_plugin_query_default
  14. */
  15. public $query;
  16. function option_definition() {
  17. $options = parent::option_definition();
  18. $options['fields'] = array('default' => array());
  19. return $options;
  20. }
  21. function options_form(&$form, &$form_state) {
  22. parent::options_form($form, $form_state);
  23. $this->view->init_style();
  24. // Allow to choose all fields as possible.
  25. if ($this->view->style_plugin->uses_fields()) {
  26. $options = array();
  27. foreach ($this->view->display_handler->get_handlers('field') as $name => $field) {
  28. $options[$name] = $field->ui_name(TRUE);
  29. }
  30. if ($options) {
  31. $form['fields'] = array(
  32. '#type' => 'select',
  33. '#title' => t('Choose fields to combine for filtering'),
  34. '#description' => t("This filter doesn't work for very special field handlers."),
  35. '#multiple' => TRUE,
  36. '#options' => $options,
  37. '#default_value' => $this->options['fields'],
  38. );
  39. }
  40. else {
  41. form_set_error('', t('You have to add some fields to be able to use this filter.'));
  42. }
  43. }
  44. }
  45. function query() {
  46. $this->view->_build('field');
  47. $fields = array();
  48. // Only add the fields if they have a proper field and table alias.
  49. foreach ($this->options['fields'] as $id) {
  50. $field = $this->view->field[$id];
  51. // Always add the table of the selected fields to be sure a table alias
  52. // exists.
  53. $field->ensure_my_table();
  54. if (!empty($field->field_alias) && !empty($field->field_alias)) {
  55. $fields[] = "$field->table_alias.$field->real_field";
  56. }
  57. }
  58. if ($fields) {
  59. $count = count($fields);
  60. $separated_fields = array();
  61. foreach ($fields as $key => $field) {
  62. $separated_fields[] = $field;
  63. if ($key < $count - 1) {
  64. $separated_fields[] = "' '";
  65. }
  66. }
  67. $expression = implode(', ', $separated_fields);
  68. $expression = "CONCAT_WS(' ', $expression)";
  69. $info = $this->operators();
  70. if (!empty($info[$this->operator]['method'])) {
  71. $this->{$info[$this->operator]['method']}($expression);
  72. }
  73. }
  74. }
  75. // By default things like op_equal uses add_where, that doesn't support
  76. // complex expressions, so override all operators.
  77. function op_equal($field) {
  78. $placeholder = $this->placeholder();
  79. $operator = $this->operator();
  80. $this->query->add_where_expression($this->options['group'], "$field $operator $placeholder", array($placeholder => $this->value));
  81. }
  82. function op_contains($field) {
  83. $placeholder = $this->placeholder();
  84. $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
  85. }
  86. function op_word($field) {
  87. $where = $this->operator == 'word' ? db_or() : db_and();
  88. // Don't filter on empty strings.
  89. if (empty($this->value)) {
  90. return;
  91. }
  92. preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->value, $matches, PREG_SET_ORDER);
  93. foreach ($matches as $match) {
  94. $phrase = FALSE;
  95. // Strip off phrase quotes.
  96. if ($match[2]{0} == '"') {
  97. $match[2] = substr($match[2], 1, -1);
  98. $phrase = TRUE;
  99. }
  100. $words = trim($match[2], ',?!();:-');
  101. $words = $phrase ? array($words) : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
  102. $placeholder = $this->placeholder();
  103. foreach ($words as $word) {
  104. $where->where($field . " LIKE $placeholder", array($placeholder => '%' . db_like(trim($word, " ,!?")) . '%'));
  105. }
  106. }
  107. if (!$where) {
  108. return;
  109. }
  110. // Previously this was a call_user_func_array() but that's unnecessary
  111. // as views will unpack an array that is a single arg.
  112. $this->query->add_where($this->options['group'], $where);
  113. }
  114. function op_starts($field) {
  115. $placeholder = $this->placeholder();
  116. $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
  117. }
  118. function op_not_starts($field) {
  119. $placeholder = $this->placeholder();
  120. $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => db_like($this->value) . '%'));
  121. }
  122. function op_ends($field) {
  123. $placeholder = $this->placeholder();
  124. $this->query->add_where_expression($this->options['group'], "$field LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
  125. }
  126. function op_not_ends($field) {
  127. $placeholder = $this->placeholder();
  128. $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value)));
  129. }
  130. function op_not($field) {
  131. $placeholder = $this->placeholder();
  132. $this->query->add_where_expression($this->options['group'], "$field NOT LIKE $placeholder", array($placeholder => '%' . db_like($this->value) . '%'));
  133. }
  134. function op_regex($field) {
  135. $placeholder = $this->placeholder();
  136. $this->query->add_where_expression($this->options['group'], "$field RLIKE $placeholder", array($placeholder => $this->value));
  137. }
  138. function op_empty($field) {
  139. if ($this->operator == 'empty') {
  140. $operator = "IS NULL";
  141. }
  142. else {
  143. $operator = "IS NOT NULL";
  144. }
  145. $this->query->add_where_expression($this->options['group'], "$field $operator");
  146. }
  147. }