handler_argument_fulltext.inc 2.8 KB

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