search_api_autocomplete.pages.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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');
  20. if ($fields) {
  21. $fields = explode(' ', $fields);
  22. $query->fields($fields);
  23. }
  24. $query->preExecute();
  25. $suggestions = $server->getAutocompleteSuggestions($query, $search, $incomplete, $keys);
  26. if ($suggestions) {
  27. foreach ($suggestions as $suggestion) {
  28. // Convert suggestion strings into an array.
  29. if (is_string($suggestion)) {
  30. $pos = strpos($suggestion, $keys);
  31. if ($pos === FALSE) {
  32. $suggestion = array(
  33. 'user_input' => '',
  34. 'suggestion_suffix' => $suggestion,
  35. );
  36. }
  37. else {
  38. $suggestion = array(
  39. 'suggestion_prefix' => substr($suggestion, 0, $pos),
  40. 'user_input' => $keys,
  41. 'suggestion_suffix' => substr($suggestion, $pos + strlen($keys)),
  42. );
  43. }
  44. }
  45. // Add defaults.
  46. $suggestion += array(
  47. 'prefix' => NULL,
  48. 'suggestion_prefix' => '',
  49. 'user_input' => $keys,
  50. 'suggestion_suffix' => '',
  51. 'results' => NULL,
  52. );
  53. if (empty($search->options['results'])) {
  54. unset($suggestion['results']);
  55. }
  56. $key = $suggestion['suggestion_prefix'] . $suggestion['user_input'] . $suggestion['suggestion_suffix'];
  57. if (!isset($ret[$key])) {
  58. $ret[$key] = theme('search_api_autocomplete_suggestion', $suggestion);
  59. }
  60. }
  61. }
  62. }
  63. }
  64. drupal_json_output($ret);
  65. }
  66. /**
  67. *
  68. *
  69. * @param array $variables
  70. * An associative array containing:
  71. * - prefix: For special suggestions, some kind of prefix describing them.
  72. * - suggestion_prefix: A suggested prefix for the entered input.
  73. * - user_input: The input entered by the user.
  74. * - suggestion_suffix: A suggested suffix for the entered input.
  75. * - results: If available, the estimated number of results for these keys.
  76. */
  77. function theme_search_api_autocomplete_suggestion(array $variables) {
  78. extract($variables);
  79. $output = '';
  80. if ($prefix) {
  81. $output .= "<span class=\"autocomplete-suggestion-note\">$prefix</span> ";
  82. }
  83. if ($suggestion_prefix) {
  84. $output .= "<span class=\"autocomplete-suggestion-prefix\">$suggestion_prefix</span>";
  85. }
  86. if ($user_input) {
  87. $output .= "<span class=\"autocomplete-user-input\">$user_input</span>";
  88. }
  89. if ($suggestion_suffix) {
  90. $output .= "<span class=\"autocomplete-suggestion-suffix\">$suggestion_suffix</span>";
  91. }
  92. if ($results) {
  93. $output .= " <span class=\"autocomplete-suggestion-results\">$results</span>";
  94. }
  95. return "<div class=\"search-api-autocomplete-suggestion\">$output</div>";
  96. }