search_api_page.pages.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the Search pages module.
  5. */
  6. /**
  7. * Displays a search page.
  8. *
  9. * @param $id
  10. * The search page's machine name.
  11. * @param $keys
  12. * The keys to search for.
  13. */
  14. function search_api_page_view($id, $keys = NULL) {
  15. $page = search_api_page_load($id);
  16. if (!$page) {
  17. return MENU_NOT_FOUND;
  18. }
  19. // Override per_page setting with GET parameter.
  20. if (!empty($_GET['per_page']) && !empty($page->options['get_per_page']) && ((int) $_GET['per_page']) > 0) {
  21. // Remember and later restore the true setting value so we don't
  22. // accidentally permanently save the altered one.
  23. $page->options['original_per_page'] = $page->options['per_page'];
  24. $page->options['per_page'] = (int) $_GET['per_page'];
  25. }
  26. if (!isset($page->options['result_page_search_form']) || $page->options['result_page_search_form']) {
  27. $ret['form'] = drupal_get_form('search_api_page_search_form', $page, $keys);
  28. }
  29. if ($keys) {
  30. try {
  31. $results = search_api_page_search_execute($page, $keys);
  32. }
  33. catch (SearchApiException $e) {
  34. $ret['message'] = t('An error occurred while executing the search. Please try again or contact the site administrator if the problem persists.');
  35. watchdog('search_api_page', 'An error occurred while executing a search: !msg.', array('!msg' => $e->getMessage()), WATCHDOG_ERROR, l(t('search page'), $_GET['q']));
  36. }
  37. // If spellcheck results are returned then add them to the render array.
  38. if (isset($results['search_api_spellcheck'])) {
  39. $ret['search_api_spellcheck']['#theme'] = 'search_api_spellcheck';
  40. $ret['search_api_spellcheck']['#spellcheck'] = $results['search_api_spellcheck'];
  41. // Let the theme function know where the key is stored by passing its arg
  42. // number. We can work this out from the number of args in the page path.
  43. $ret['search_api_spellcheck']['#options'] = array(
  44. 'arg' => array(count(arg(NULL, $page->path))),
  45. );
  46. }
  47. $ret['results']['#theme'] = 'search_api_page_results';
  48. $ret['results']['#index'] = search_api_index_load($page->index_id);
  49. $ret['results']['#results'] = $results;
  50. $ret['results']['#view_mode'] = isset($page->options['view_mode']) ? $page->options['view_mode'] : 'search_api_page_result';
  51. $ret['results']['#keys'] = $keys;
  52. if ($results['result count'] > $page->options['per_page']) {
  53. pager_default_initialize($results['result count'], $page->options['per_page']);
  54. $ret['pager']['#theme'] = 'pager';
  55. $ret['pager']['#quantity'] = 9;
  56. }
  57. if (!empty($results['ignored'])) {
  58. drupal_set_message(t('The following search keys are too short or too common and were therefore ignored: "@list".', array('@list' => implode(t('", "'), $results['ignored']))), 'warning');
  59. }
  60. if (!empty($results['warnings'])) {
  61. foreach ($results['warnings'] as $warning) {
  62. drupal_set_message(check_plain($warning), 'warning');
  63. }
  64. }
  65. }
  66. if (isset($page->options['original_per_page'])) {
  67. $page->options['per_page'] = $page->options['original_per_page'];
  68. unset($page->options['original_per_page']);
  69. }
  70. return $ret;
  71. }
  72. /**
  73. * Executes a search.
  74. *
  75. * @param Entity $page
  76. * The page for which a search should be executed.
  77. * @param $keys
  78. * The keywords to search for.
  79. *
  80. * @return array
  81. * The search results as returned by SearchApiQueryInterface::execute().
  82. */
  83. function search_api_page_search_execute(Entity $page, $keys) {
  84. $limit = $page->options['per_page'];
  85. $offset = pager_find_page() * $limit;
  86. $options = array(
  87. 'search id' => 'search_api_page:' . $page->path,
  88. 'parse mode' => $page->options['mode'],
  89. );
  90. if (!empty($page->options['search_api_spellcheck'])) {
  91. $options['search_api_spellcheck'] = TRUE;
  92. }
  93. $query = search_api_query($page->index_id, $options)
  94. ->keys($keys)
  95. ->range($offset, $limit);
  96. if (!empty($page->options['fields'])) {
  97. $query->fields($page->options['fields']);
  98. }
  99. return $query->execute();
  100. }
  101. /**
  102. * Function for preprocessing the variables for the search_api_page_results
  103. * theme.
  104. *
  105. * @param array $variables
  106. * An associative array containing:
  107. * - index: The index this search was executed on.
  108. * - results: An array of search results, as returned by
  109. * SearchApiQueryInterface::execute().
  110. * - keys: The keywords of the executed search.
  111. */
  112. function template_preprocess_search_api_page_results(array &$variables) {
  113. if (!empty($variables['results']['results'])) {
  114. $variables['items'] = $variables['index']->loadItems(array_keys($variables['results']['results']));
  115. }
  116. }
  117. /**
  118. * Theme function for displaying search results.
  119. *
  120. * @param array $variables
  121. * An associative array containing:
  122. * - index: The index this search was executed on.
  123. * - results: An array of search results, as returned by
  124. * SearchApiQueryInterface::execute().
  125. * - items: The loaded items for all results, in an array keyed by ID.
  126. * - view_mode: The view mode to use for displaying the individual results,
  127. * or the special mode "search_api_page_result" to use the theme function
  128. * of the same name.
  129. * - keys: The keywords of the executed search.
  130. */
  131. function theme_search_api_page_results(array $variables) {
  132. drupal_add_css(drupal_get_path('module', 'search_api_page') . '/search_api_page.css');
  133. $index = $variables['index'];
  134. $results = $variables['results'];
  135. $items = $variables['items'];
  136. $keys = $variables['keys'];
  137. $output = '<p class="search-performance">' . format_plural($results['result count'],
  138. 'The search found 1 result in @sec seconds.',
  139. 'The search found @count results in @sec seconds.',
  140. array('@sec' => round($results['performance']['complete'], 3))) . '</p>';
  141. if (!$results['result count']) {
  142. $output .= "\n<h2>" . t('Your search yielded no results') . "</h2>\n";
  143. return $output;
  144. }
  145. $output .= "\n<h2>" . t('Search results') . "</h2>\n";
  146. if ($variables['view_mode'] == 'search_api_page_result') {
  147. $output .= '<ol class="search-results">';
  148. foreach ($results['results'] as $item) {
  149. $output .= '<li class="search-result">' . theme('search_api_page_result', array('index' => $index, 'result' => $item, 'item' => isset($items[$item['id']]) ? $items[$item['id']] : NULL, 'keys' => $keys)) . '</li>';
  150. }
  151. $output .= '</ol>';
  152. }
  153. else {
  154. // This option can only be set when the items are entities.
  155. $output .= '<div class="search-results">';
  156. $render = entity_view($index->item_type, $items, $variables['view_mode']);
  157. $output .= render($render);
  158. $output .= '</div>';
  159. }
  160. return $output;
  161. }
  162. /**
  163. * Theme function for displaying search results.
  164. *
  165. * @param array $variables
  166. * An associative array containing:
  167. * - index: The index this search was executed on.
  168. * - result: One item of the search results, an array containing the keys
  169. * 'id' and 'score'.
  170. * - item: The loaded item corresponding to the result.
  171. * - keys: The keywords of the executed search.
  172. */
  173. function theme_search_api_page_result(array $variables) {
  174. $index = $variables['index'];
  175. $id = $variables['result']['id'];
  176. $item = $variables['item'];
  177. $wrapper = $index->entityWrapper($item, FALSE);
  178. $url = $index->datasource()->getItemUrl($item);
  179. $name = $index->datasource()->getItemLabel($item);
  180. if (!empty($variables['result']['excerpt'])) {
  181. $text = $variables['result']['excerpt'];
  182. }
  183. else {
  184. $fields = $index->options['fields'];
  185. $fields = array_intersect_key($fields, drupal_map_assoc($index->getFulltextFields()));
  186. $fields = search_api_extract_fields($wrapper, $fields);
  187. $text = '';
  188. $length = 0;
  189. foreach ($fields as $field_name => $field) {
  190. if (search_api_is_list_type($field['type']) || !isset($field['value'])) {
  191. continue;
  192. }
  193. $val_length = drupal_strlen($field['value']);
  194. if ($val_length > $length) {
  195. $text = $field['value'];
  196. $length = $val_length;
  197. $format = NULL;
  198. if (($pos = strrpos($field_name, ':')) && substr($field_name, $pos + 1) == 'value') {
  199. $tmp = $wrapper;
  200. try {
  201. foreach (explode(':', substr($field_name, 0, $pos)) as $part) {
  202. if (!isset($tmp->$part)) {
  203. $tmp = NULL;
  204. }
  205. $tmp = $tmp->$part;
  206. }
  207. }
  208. catch (EntityMetadataWrapperException $e) {
  209. $tmp = NULL;
  210. }
  211. if ($tmp && $tmp->type() == 'text_formatted' && isset($tmp->format)) {
  212. $format = $tmp->format->value();
  213. }
  214. }
  215. }
  216. }
  217. if ($text && function_exists('text_summary')) {
  218. $text = text_summary($text, $format);
  219. }
  220. }
  221. $output = '<h3>' . ($url ? l($name, $url['path'], $url['options']) : check_plain($name)) . "</h3>\n";
  222. if ($text) {
  223. $output .= $text;
  224. }
  225. return $output;
  226. }