handler_argument_fulltext.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. $this->query->fields($this->options['fields']);
  62. }
  63. if ($this->options['conjunction'] != 'AND') {
  64. $this->query->setOption('conjunction', $this->options['conjunction']);
  65. }
  66. $old = $this->query->getOriginalKeys();
  67. $this->query->keys($this->argument);
  68. if ($old) {
  69. $keys = &$this->query->getKeys();
  70. if (is_array($keys)) {
  71. $keys[] = $old;
  72. }
  73. elseif (is_array($old)) {
  74. // We don't support such nonsense.
  75. }
  76. else {
  77. $keys = "($old) ($keys)";
  78. }
  79. }
  80. }
  81. /**
  82. * Helper method to get an option list of all available fulltext fields.
  83. */
  84. protected function getFulltextFields() {
  85. $ret = array();
  86. $index = search_api_index_load(substr($this->table, 17));
  87. if (!empty($index->options['fields'])) {
  88. $fields = $index->getFields();
  89. foreach ($index->getFulltextFields() as $field) {
  90. $ret[$field] = $fields[$field]['name'];
  91. }
  92. }
  93. return $ret;
  94. }
  95. }