search_api_autocomplete.entity.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. * - custom: An array of type-specific settings.
  41. *
  42. * @var array
  43. */
  44. public $options = array();
  45. // Inferred properties, for caching:
  46. /**
  47. * @var SearchApiIndex
  48. */
  49. protected $index;
  50. /**
  51. * @var SearchApiServer
  52. */
  53. protected $server;
  54. /**
  55. * Constructor.
  56. *
  57. * @param array $values
  58. * The entity properties.
  59. */
  60. public function __construct(array $values = array()) {
  61. parent::__construct($values, 'search_api_autocomplete_search');
  62. }
  63. /**
  64. * @return SearchApiIndex
  65. * The index this search belongs to.
  66. */
  67. public function index() {
  68. if (!isset($this->index)) {
  69. $this->index = search_api_index_load($this->index_id);
  70. if (!$this->index) {
  71. $this->index = FALSE;
  72. }
  73. }
  74. return $this->index;
  75. }
  76. /**
  77. * @return SearchApiServer
  78. * The server this search would at the moment be executed on.
  79. */
  80. public function server() {
  81. if (!isset($this->server)) {
  82. if (!$this->index() || !$this->index()->server) {
  83. $this->server = FALSE;
  84. }
  85. else {
  86. $this->server = $this->index()->server();
  87. if (!$this->server) {
  88. $this->server = FALSE;
  89. }
  90. }
  91. }
  92. return $this->server;
  93. }
  94. /**
  95. * @return boolean
  96. * TRUE if the server this search is currently associated with supports the
  97. * autocompletion feature; FALSE otherwise.
  98. */
  99. public function supportsAutocompletion() {
  100. return $this->server() && $this->server()->supportsFeature('search_api_autocomplete');
  101. }
  102. /**
  103. * Helper method for altering a textfield form element to use autocompletion.
  104. */
  105. public function alterElement(array &$element, array $fields = array()) {
  106. if (user_access('use search_api_autocomplete') && $this->supportsAutocompletion()) {
  107. $element['#attached']['css'][] = drupal_get_path('module', 'search_api_autocomplete') . '/search_api_autocomplete.css';
  108. $element['#autocomplete_path'] = 'search_api_autocomplete/' . $this->machine_name . '/' . implode(' ', $fields);
  109. }
  110. }
  111. /**
  112. * Split a string with search keywords into two parts.
  113. *
  114. * The first part consists of all words the user has typed completely, the
  115. * second one contains the beginning of the last, possibly incomplete word.
  116. *
  117. * @return array
  118. * An array with $keys split into exactly two parts, both of which may be
  119. * empty.
  120. */
  121. public function splitKeys($keys) {
  122. $keys = ltrim($keys);
  123. // If there is whitespace or a quote on the right, all words have been
  124. // completed.
  125. if (rtrim($keys, " \t\n\r\0\x0B\"") != $keys) {
  126. return array(rtrim($keys), '');
  127. }
  128. if (preg_match('/^(.*?)\s*"?([\S]*)$/', $keys, $m)) {
  129. return array($m[1], $m[2]);
  130. }
  131. return array('', $keys);
  132. }
  133. /**
  134. * Create the query that would be issued for this search for the complete keys.
  135. *
  136. * @param $complete
  137. * A string containing the complete search keys.
  138. * @param $incomplete
  139. * A string containing the incomplete last search key.
  140. *
  141. * @return SearchApiQueryInterface
  142. * The query that would normally be executed when only $complete was entered
  143. * as the search keys for this search.
  144. */
  145. public function getQuery($complete, $incomplete) {
  146. $info = search_api_autocomplete_get_types($this->type);
  147. if (empty($info['create query'])) {
  148. return NULL;
  149. }
  150. $query = $info['create query']($this, $complete, $incomplete);
  151. if ($complete && !$query->getKeys()) {
  152. $query->keys($complete);
  153. }
  154. return $query;
  155. }
  156. }