search_api_page.pages.inc.orig 8.6 KB

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