handler_argument_fulltext.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Views argument handler class for handling fulltext fields.
  4. */
  5. class SearchApiViewsHandlerArgumentFulltext extends SearchApiViewsHandlerArgumentText {
  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. return $options;
  13. }
  14. /**
  15. * Extend the options form a bit.
  16. */
  17. public function options_form(&$form, &$form_state) {
  18. parent::options_form($form, $form_state);
  19. $fields = $this->getFulltextFields();
  20. if (!empty($fields)) {
  21. $form['fields'] = array(
  22. '#type' => 'select',
  23. '#title' => t('Searched fields'),
  24. '#description' => t('Select the fields that will be searched. If no fields are selected, all available fulltext fields will be searched.'),
  25. '#options' => $fields,
  26. '#size' => min(4, count($fields)),
  27. '#multiple' => TRUE,
  28. '#default_value' => $this->options['fields'],
  29. );
  30. }
  31. else {
  32. $form['fields'] = array(
  33. '#type' => 'value',
  34. '#value' => array(),
  35. );
  36. }
  37. }
  38. /**
  39. * Set up the query for this argument.
  40. *
  41. * The argument sent may be found at $this->argument.
  42. */
  43. public function query($group_by = FALSE) {
  44. if ($this->options['fields']) {
  45. $this->query->fields($this->options['fields']);
  46. }
  47. $old = $this->query->getOriginalKeys();
  48. $this->query->keys($this->argument);
  49. if ($old) {
  50. $keys = &$this->query->getKeys();
  51. if (is_array($keys)) {
  52. $keys[] = $old;
  53. }
  54. elseif (is_array($old)) {
  55. // We don't support such nonsense.
  56. }
  57. else {
  58. $keys = "($old) ($keys)";
  59. }
  60. }
  61. }
  62. /**
  63. * Helper method to get an option list of all available fulltext fields.
  64. */
  65. protected function getFulltextFields() {
  66. $ret = array();
  67. $index = search_api_index_load(substr($this->table, 17));
  68. if (!empty($index->options['fields'])) {
  69. $fields = $index->getFields();
  70. foreach ($index->getFulltextFields() as $field) {
  71. $ret[$field] = $fields[$field]['name'];
  72. }
  73. }
  74. return $ret;
  75. }
  76. }