handler_filter_fulltext.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiViewsHandlerFilterFulltext.
  5. */
  6. /**
  7. * Views filter handler class for handling fulltext fields.
  8. */
  9. class SearchApiViewsHandlerFilterFulltext extends SearchApiViewsHandlerFilterText {
  10. /**
  11. * Displays the operator form, adding a description.
  12. */
  13. public function show_operator_form(&$form, &$form_state) {
  14. $this->operator_form($form, $form_state);
  15. $form['operator']['#description'] = t('This operator is only useful when using \'Search keys\'.');
  16. }
  17. /**
  18. * Provide a list of options for the operator form.
  19. */
  20. public function operator_options() {
  21. return array(
  22. 'AND' => t('Contains all of these words'),
  23. 'OR' => t('Contains any of these words'),
  24. 'NOT' => t('Contains none of these words'),
  25. );
  26. }
  27. /**
  28. * Specify the options this filter uses.
  29. */
  30. public function option_definition() {
  31. $options = parent::option_definition();
  32. $options['operator']['default'] = 'AND';
  33. $options['mode'] = array('default' => 'keys');
  34. $options['min_length'] = array('default' => '');
  35. $options['fields'] = array('default' => array());
  36. return $options;
  37. }
  38. /**
  39. * Extend the options form a bit.
  40. */
  41. public function options_form(&$form, &$form_state) {
  42. parent::options_form($form, $form_state);
  43. $form['mode'] = array(
  44. '#title' => t('Use as'),
  45. '#type' => 'radios',
  46. '#options' => array(
  47. 'keys' => t('Search keys – multiple words will be split and the filter will influence relevance. You can change how search keys are parsed under "Advanced" > "Query settings".'),
  48. 'filter' => t("Search filter – use as a single phrase that restricts the result set but doesn't influence relevance."),
  49. ),
  50. '#default_value' => $this->options['mode'],
  51. );
  52. $fields = $this->getFulltextFields();
  53. if (!empty($fields)) {
  54. $form['fields'] = array(
  55. '#type' => 'select',
  56. '#title' => t('Searched fields'),
  57. '#description' => t('Select the fields that will be searched. If no fields are selected, all available fulltext fields will be searched.'),
  58. '#options' => $fields,
  59. '#size' => min(4, count($fields)),
  60. '#multiple' => TRUE,
  61. '#default_value' => $this->options['fields'],
  62. );
  63. }
  64. else {
  65. $form['fields'] = array(
  66. '#type' => 'value',
  67. '#value' => array(),
  68. );
  69. }
  70. if (isset($form['expose'])) {
  71. $form['expose']['#weight'] = -5;
  72. }
  73. $form['min_length'] = array(
  74. '#title' => t('Minimum keyword length'),
  75. '#description' => t('Minimum length of each word in the search keys. Leave empty to allow all words.'),
  76. '#type' => 'textfield',
  77. '#element_validate' => array('element_validate_integer_positive'),
  78. '#default_value' => $this->options['min_length'],
  79. );
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function exposed_validate(&$form, &$form_state) {
  85. // Only validate exposed input.
  86. if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
  87. return;
  88. }
  89. // We only need to validate if there is a minimum word length set.
  90. if ($this->options['min_length'] < 2) {
  91. return;
  92. }
  93. $identifier = $this->options['expose']['identifier'];
  94. $input = &$form_state['values'][$identifier];
  95. if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) {
  96. $this->operator = $this->options['group_info']['group_items'][$input]['operator'];
  97. $input = &$this->options['group_info']['group_items'][$input]['value'];
  98. }
  99. // If there is no input, we're fine.
  100. if (!trim($input)) {
  101. return;
  102. }
  103. $words = preg_split('/\s+/', $input);
  104. foreach ($words as $i => $word) {
  105. if (drupal_strlen($word) < $this->options['min_length']) {
  106. unset($words[$i]);
  107. }
  108. }
  109. if (!$words) {
  110. $vars['@count'] = $this->options['min_length'];
  111. $msg = t('You must include at least one positive keyword with @count characters or more.', $vars);
  112. form_error($form[$identifier], $msg);
  113. }
  114. $input = implode(' ', $words);
  115. }
  116. /**
  117. * Add this filter to the query.
  118. */
  119. public function query() {
  120. while (is_array($this->value)) {
  121. $this->value = $this->value ? reset($this->value) : '';
  122. }
  123. // Catch empty strings entered by the user, but not "0".
  124. if ($this->value === '') {
  125. return;
  126. }
  127. $fields = $this->options['fields'];
  128. $fields = $fields ? $fields : array_keys($this->getFulltextFields());
  129. // If something already specifically set different fields, we silently fall
  130. // back to mere filtering.
  131. $filter = $this->options['mode'] == 'filter';
  132. if (!$filter) {
  133. $old = $this->query->getFields();
  134. $filter = $old && (array_diff($old, $fields) || array_diff($fields, $old));
  135. }
  136. if ($filter) {
  137. $filter = $this->query->createFilter('OR');
  138. foreach ($fields as $field) {
  139. $filter->condition($field, $this->value, $this->operator);
  140. }
  141. $this->query->filter($filter);
  142. return;
  143. }
  144. // If the operator was set to OR or NOT, set OR as the conjunction. (It is
  145. // also set for NOT since otherwise it would be "not all of these words".)
  146. if ($this->operator != 'AND') {
  147. $this->query->setOption('conjunction', $this->operator);
  148. }
  149. $this->query->fields($fields);
  150. $old = $this->query->getOriginalKeys();
  151. $this->query->keys($this->value);
  152. if ($this->operator == 'NOT') {
  153. $keys = &$this->query->getKeys();
  154. if (is_array($keys)) {
  155. $keys['#negation'] = TRUE;
  156. }
  157. else {
  158. // We can't know how negation is expressed in the server's syntax.
  159. }
  160. }
  161. if ($old) {
  162. $keys = &$this->query->getKeys();
  163. if (is_array($keys)) {
  164. $keys[] = $old;
  165. }
  166. elseif (is_array($old)) {
  167. // We don't support such nonsense.
  168. }
  169. else {
  170. $keys = "($old) ($keys)";
  171. }
  172. }
  173. }
  174. /**
  175. * Helper method to get an option list of all available fulltext fields.
  176. */
  177. protected function getFulltextFields() {
  178. $fields = array();
  179. $index = search_api_index_load(substr($this->table, 17));
  180. if (!empty($index->options['fields'])) {
  181. $f = $index->getFields();
  182. foreach ($index->getFulltextFields() as $name) {
  183. $fields[$name] = $f[$name]['name'];
  184. }
  185. }
  186. return $fields;
  187. }
  188. }