handler_argument_fulltext.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiViewsHandlerArgumentFulltext.
  5. */
  6. /**
  7. * Views argument handler class for handling fulltext fields.
  8. */
  9. class SearchApiViewsHandlerArgumentFulltext extends SearchApiViewsHandlerArgument {
  10. /**
  11. * Specify the options this filter uses.
  12. */
  13. public function option_definition() {
  14. $options = parent::option_definition();
  15. $options['fields'] = array('default' => array());
  16. $options['conjunction'] = array('default' => 'AND');
  17. return $options;
  18. }
  19. /**
  20. * Extend the options form a bit.
  21. */
  22. public function options_form(&$form, &$form_state) {
  23. parent::options_form($form, $form_state);
  24. $form['help']['#markup'] = t('Note: You can change how search keys are parsed under "Advanced" > "Query settings".');
  25. $fields = $this->getFulltextFields();
  26. if (!empty($fields)) {
  27. $form['fields'] = array(
  28. '#type' => 'select',
  29. '#title' => t('Searched fields'),
  30. '#description' => t('Select the fields that will be searched. If no fields are selected, all available fulltext fields will be searched.'),
  31. '#options' => $fields,
  32. '#size' => min(4, count($fields)),
  33. '#multiple' => TRUE,
  34. '#default_value' => $this->options['fields'],
  35. );
  36. $form['conjunction'] = array(
  37. '#title' => t('Operator'),
  38. '#description' => t('Determines how multiple keywords entered for the search will be combined.'),
  39. '#type' => 'radios',
  40. '#options' => array(
  41. 'AND' => t('Contains all of these words'),
  42. 'OR' => t('Contains any of these words'),
  43. ),
  44. '#default_value' => $this->options['conjunction'],
  45. );
  46. }
  47. else {
  48. $form['fields'] = array(
  49. '#type' => 'value',
  50. '#value' => array(),
  51. );
  52. }
  53. }
  54. /**
  55. * Set up the query for this argument.
  56. *
  57. * The argument sent may be found at $this->argument.
  58. */
  59. public function query($group_by = FALSE) {
  60. if ($this->options['fields']) {
  61. try {
  62. $this->query->fields($this->options['fields']);
  63. }
  64. catch (SearchApiException $e) {
  65. $this->query->abort($e->getMessage());
  66. return;
  67. }
  68. }
  69. if ($this->options['conjunction'] != 'AND') {
  70. $this->query->setOption('conjunction', $this->options['conjunction']);
  71. }
  72. $old = $this->query->getOriginalKeys();
  73. $this->query->keys($this->argument);
  74. if ($old) {
  75. $keys = &$this->query->getKeys();
  76. if (is_array($keys)) {
  77. $keys[] = $old;
  78. }
  79. elseif (is_array($old)) {
  80. // We don't support such nonsense.
  81. }
  82. else {
  83. $keys = "($old) ($keys)";
  84. }
  85. }
  86. }
  87. /**
  88. * Helper method to get an option list of all available fulltext fields.
  89. */
  90. protected function getFulltextFields() {
  91. $ret = array();
  92. $index = search_api_index_load(substr($this->table, 17));
  93. if (!empty($index->options['fields'])) {
  94. $fields = $index->getFields();
  95. foreach ($index->getFulltextFields() as $field) {
  96. $ret[$field] = $fields[$field]['name'];
  97. }
  98. }
  99. return $ret;
  100. }
  101. }