search_api_autocomplete.pages.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @file
  4. * Contains page callbacks and theme functions for the frontend UI.
  5. */
  6. /**
  7. * Page callback for getting autocomplete suggestions.
  8. */
  9. function search_api_autocomplete_autocomplete(SearchApiAutocompleteSearch $search, $fields, $keys = '') {
  10. $ret = array();
  11. if ($search->supportsAutocompletion()) {
  12. $server = $search->server();
  13. list($complete, $incomplete) = $search->splitKeys($keys);
  14. $keys = preg_replace('/\s+/', ' ', trim($keys));
  15. $query = $search->getQuery($complete, $incomplete);
  16. if ($query) {
  17. // @todo Maybe make range configurable?
  18. $query->range(0, 10);
  19. $query->setOption('search id', 'search_api_autocomplete:' . $search->machine_name);
  20. if (!empty($search->options['fields'])) {
  21. $query->fields($search->options['fields']);
  22. }
  23. elseif (trim($fields)) {
  24. $fields = explode(' ', $fields);
  25. $query->fields($fields);
  26. }
  27. $query->preExecute();
  28. $suggestions = $server->getAutocompleteSuggestions($query, $search, $incomplete, $keys);
  29. if ($suggestions) {
  30. foreach ($suggestions as $suggestion) {
  31. // Convert suggestion strings into an array.
  32. if (is_string($suggestion)) {
  33. $pos = strpos($suggestion, $keys);
  34. if ($pos === FALSE) {
  35. $suggestion = array(
  36. 'user_input' => '',
  37. 'suggestion_suffix' => $suggestion,
  38. );
  39. }
  40. else {
  41. $suggestion = array(
  42. 'suggestion_prefix' => substr($suggestion, 0, $pos),
  43. 'user_input' => $keys,
  44. 'suggestion_suffix' => substr($suggestion, $pos + strlen($keys)),
  45. );
  46. }
  47. }
  48. // Add defaults.
  49. $suggestion += array(
  50. 'prefix' => NULL,
  51. 'suggestion_prefix' => '',
  52. 'user_input' => $keys,
  53. 'suggestion_suffix' => '',
  54. 'results' => NULL,
  55. );
  56. if (empty($search->options['results'])) {
  57. unset($suggestion['results']);
  58. }
  59. $key = $suggestion['suggestion_prefix'] . $suggestion['user_input'] . $suggestion['suggestion_suffix'];
  60. if (!isset($ret[$key])) {
  61. $ret[$key] = theme('search_api_autocomplete_suggestion', $suggestion);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. drupal_json_output($ret);
  68. }
  69. /**
  70. *
  71. *
  72. * @param array $variables
  73. * An associative array containing:
  74. * - prefix: For special suggestions, some kind of prefix describing them.
  75. * - suggestion_prefix: A suggested prefix for the entered input.
  76. * - user_input: The input entered by the user.
  77. * - suggestion_suffix: A suggested suffix for the entered input.
  78. * - results: If available, the estimated number of results for these keys.
  79. */
  80. function theme_search_api_autocomplete_suggestion(array $variables) {
  81. extract($variables);
  82. $output = '';
  83. if ($prefix) {
  84. $output .= "<span class=\"autocomplete-suggestion-note\">$prefix</span> ";
  85. }
  86. if ($suggestion_prefix) {
  87. $output .= "<span class=\"autocomplete-suggestion-prefix\">$suggestion_prefix</span>";
  88. }
  89. if ($user_input) {
  90. $output .= "<span class=\"autocomplete-user-input\">$user_input</span>";
  91. }
  92. if ($suggestion_suffix) {
  93. $output .= "<span class=\"autocomplete-suggestion-suffix\">$suggestion_suffix</span>";
  94. }
  95. if ($results) {
  96. $output .= " <span class=\"autocomplete-suggestion-results\">$results</span>";
  97. }
  98. return "<div class=\"search-api-autocomplete-suggestion\">$output</div>";
  99. }