search_api_autocomplete.entity.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiAutocompleteSearch class.
  5. */
  6. /**
  7. * Class describing the settings for a certain search for which autocompletion
  8. * is available.
  9. */
  10. class SearchApiAutocompleteSearch extends Entity {
  11. // Entity properties, loaded from the database:
  12. /**
  13. * @var integer
  14. */
  15. public $id;
  16. /**
  17. * @var string
  18. */
  19. public $machine_name;
  20. /**
  21. * @var string
  22. */
  23. public $name;
  24. /**
  25. * @var integer
  26. */
  27. public $index_id;
  28. /**
  29. * @var string
  30. */
  31. public $type;
  32. /**
  33. * @var boolean
  34. */
  35. public $enabled;
  36. /**
  37. * An array of options for this search, containing any of the following:
  38. * - results: Boolean indicating whether to also list the estimated number of
  39. * results for each suggestion (if possible).
  40. * - fields: Array containing the fulltext fields to use for autocompletion.
  41. * - custom: An array of type-specific settings.
  42. *
  43. * @var array
  44. */
  45. public $options = array();
  46. // Inferred properties, for caching:
  47. /**
  48. * @var SearchApiIndex
  49. */
  50. protected $index;
  51. /**
  52. * @var SearchApiServer
  53. */
  54. protected $server;
  55. /**
  56. * Constructor.
  57. *
  58. * @param array $values
  59. * The entity properties.
  60. */
  61. public function __construct(array $values = array()) {
  62. parent::__construct($values, 'search_api_autocomplete_search');
  63. }
  64. /**
  65. * @return SearchApiIndex
  66. * The index this search belongs to.
  67. */
  68. public function index() {
  69. if (!isset($this->index)) {
  70. $this->index = search_api_index_load($this->index_id);
  71. if (!$this->index) {
  72. $this->index = FALSE;
  73. }
  74. }
  75. return $this->index;
  76. }
  77. /**
  78. * @return SearchApiServer
  79. * The server this search would at the moment be executed on.
  80. */
  81. public function server() {
  82. if (!isset($this->server)) {
  83. if (!$this->index() || !$this->index()->server) {
  84. $this->server = FALSE;
  85. }
  86. else {
  87. $this->server = $this->index()->server();
  88. if (!$this->server) {
  89. $this->server = FALSE;
  90. }
  91. }
  92. }
  93. return $this->server;
  94. }
  95. /**
  96. * @return boolean
  97. * TRUE if the server this search is currently associated with supports the
  98. * autocompletion feature; FALSE otherwise.
  99. */
  100. public function supportsAutocompletion() {
  101. return $this->server() && $this->server()->supportsFeature('search_api_autocomplete');
  102. }
  103. /**
  104. * Helper method for altering a textfield form element to use autocompletion.
  105. */
  106. public function alterElement(array &$element, array $fields = array()) {
  107. if (search_api_autocomplete_access($this)) {
  108. $fields_string = $fields ? implode(' ', $fields) : ' ';
  109. $element['#attached']['css'][] = drupal_get_path('module', 'search_api_autocomplete') . '/search_api_autocomplete.css';
  110. $element['#autocomplete_path'] = 'search_api_autocomplete/' . $this->machine_name . '/' . $fields_string;
  111. }
  112. }
  113. /**
  114. * Split a string with search keywords into two parts.
  115. *
  116. * The first part consists of all words the user has typed completely, the
  117. * second one contains the beginning of the last, possibly incomplete word.
  118. *
  119. * @return array
  120. * An array with $keys split into exactly two parts, both of which may be
  121. * empty.
  122. */
  123. public function splitKeys($keys) {
  124. $keys = ltrim($keys);
  125. // If there is whitespace or a quote on the right, all words have been
  126. // completed.
  127. if (rtrim($keys, " \t\n\r\0\x0B\"") != $keys) {
  128. return array(rtrim($keys), '');
  129. }
  130. if (preg_match('/^(.*?)\s*"?([\S]*)$/', $keys, $m)) {
  131. return array($m[1], $m[2]);
  132. }
  133. return array('', $keys);
  134. }
  135. /**
  136. * Create the query that would be issued for this search for the complete keys.
  137. *
  138. * @param $complete
  139. * A string containing the complete search keys.
  140. * @param $incomplete
  141. * A string containing the incomplete last search key.
  142. *
  143. * @return SearchApiQueryInterface
  144. * The query that would normally be executed when only $complete was entered
  145. * as the search keys for this search.
  146. */
  147. public function getQuery($complete, $incomplete) {
  148. $info = search_api_autocomplete_get_types($this->type);
  149. if (empty($info['create query'])) {
  150. return NULL;
  151. }
  152. $query = $info['create query']($this, $complete, $incomplete);
  153. if ($complete && !$query->getKeys()) {
  154. $query->keys($complete);
  155. }
  156. return $query;
  157. }
  158. }