search.pages.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the search module.
  5. */
  6. /**
  7. * Menu callback; presents the search form and/or search results.
  8. *
  9. * @param $module
  10. * Search module to use for the search.
  11. * @param $keys
  12. * Keywords to use for the search.
  13. */
  14. function search_view($module = NULL, $keys = '') {
  15. $info = FALSE;
  16. $redirect = FALSE;
  17. $keys = trim($keys);
  18. // Also try to pull search keywords out of the $_REQUEST variable to
  19. // support old GET format of searches for existing links.
  20. if (!$keys && !empty($_REQUEST['keys'])) {
  21. $keys = trim($_REQUEST['keys']);
  22. }
  23. if (!empty($module)) {
  24. $active_module_info = search_get_info();
  25. if (isset($active_module_info[$module])) {
  26. $info = $active_module_info[$module];
  27. }
  28. }
  29. if (empty($info)) {
  30. // No path or invalid path: find the default module. Note that if there
  31. // are no enabled search modules, this function should never be called,
  32. // since hook_menu() would not have defined any search paths.
  33. $info = search_get_default_module_info();
  34. // Redirect from bare /search or an invalid path to the default search path.
  35. $path = 'search/' . $info['path'];
  36. if ($keys) {
  37. $path .= '/' . $keys;
  38. }
  39. drupal_goto($path);
  40. }
  41. // Default results output is an empty string.
  42. $results = array('#markup' => '');
  43. // Process the search form. Note that if there is $_POST data,
  44. // search_form_submit() will cause a redirect to search/[module path]/[keys],
  45. // which will get us back to this page callback. In other words, the search
  46. // form submits with POST but redirects to GET. This way we can keep
  47. // the search query URL clean as a whistle.
  48. if (empty($_POST['form_id']) || $_POST['form_id'] != 'search_form') {
  49. $conditions = NULL;
  50. if (isset($info['conditions_callback']) && function_exists($info['conditions_callback'])) {
  51. // Build an optional array of more search conditions.
  52. $conditions = call_user_func($info['conditions_callback'], $keys);
  53. }
  54. // Only search if there are keywords or non-empty conditions.
  55. if ($keys || !empty($conditions)) {
  56. // Log the search keys.
  57. watchdog('search', 'Searched %type for %keys.', array('%keys' => $keys, '%type' => $info['title']), WATCHDOG_NOTICE, l(t('results'), 'search/' . $info['path'] . '/' . $keys));
  58. // Collect the search results.
  59. $results = search_data($keys, $info['module'], $conditions);
  60. }
  61. }
  62. // The form may be altered based on whether the search was run.
  63. $build['search_form'] = drupal_get_form('search_form', NULL, $keys, $info['module']);
  64. $build['search_results'] = $results;
  65. return $build;
  66. }
  67. /**
  68. * Process variables for search-results.tpl.php.
  69. *
  70. * The $variables array contains the following arguments:
  71. * - $results: Search results array.
  72. * - $module: Module the search results came from (module implementing
  73. * hook_search_info()).
  74. *
  75. * @see search-results.tpl.php
  76. */
  77. function template_preprocess_search_results(&$variables) {
  78. $variables['search_results'] = '';
  79. if (!empty($variables['module'])) {
  80. $variables['module'] = check_plain($variables['module']);
  81. }
  82. foreach ($variables['results'] as $result) {
  83. $variables['search_results'] .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
  84. }
  85. $variables['pager'] = theme('pager', array('tags' => NULL));
  86. $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
  87. }
  88. /**
  89. * Process variables for search-result.tpl.php.
  90. *
  91. * The $variables array contains the following arguments:
  92. * - $result
  93. * - $module
  94. *
  95. * @see search-result.tpl.php
  96. */
  97. function template_preprocess_search_result(&$variables) {
  98. global $language;
  99. $result = $variables['result'];
  100. $variables['url'] = check_url($result['link']);
  101. $variables['title'] = check_plain($result['title']);
  102. if (isset($result['language']) && $result['language'] != $language->language && $result['language'] != LANGUAGE_NONE) {
  103. $variables['title_attributes_array']['xml:lang'] = $result['language'];
  104. $variables['content_attributes_array']['xml:lang'] = $result['language'];
  105. }
  106. $info = array();
  107. if (!empty($result['module'])) {
  108. $info['module'] = check_plain($result['module']);
  109. }
  110. if (!empty($result['user'])) {
  111. $info['user'] = $result['user'];
  112. }
  113. if (!empty($result['date'])) {
  114. $info['date'] = format_date($result['date'], 'short');
  115. }
  116. if (isset($result['extra']) && is_array($result['extra'])) {
  117. $info = array_merge($info, $result['extra']);
  118. }
  119. // Check for existence. User search does not include snippets.
  120. $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  121. // Provide separated and grouped meta information..
  122. $variables['info_split'] = $info;
  123. $variables['info'] = implode(' - ', $info);
  124. $variables['theme_hook_suggestions'][] = 'search_result__' . $variables['module'];
  125. }
  126. /**
  127. * As the search form collates keys from other modules hooked in via
  128. * hook_form_alter, the validation takes place in _submit.
  129. * search_form_validate() is used solely to set the 'processed_keys' form
  130. * value for the basic search form.
  131. */
  132. function search_form_validate($form, &$form_state) {
  133. form_set_value($form['basic']['processed_keys'], trim($form_state['values']['keys']), $form_state);
  134. }
  135. /**
  136. * Process a search form submission.
  137. */
  138. function search_form_submit($form, &$form_state) {
  139. $keys = $form_state['values']['processed_keys'];
  140. if ($keys == '') {
  141. form_set_error('keys', t('Please enter some keywords.'));
  142. // Fall through to the form redirect.
  143. }
  144. $form_state['redirect'] = $form_state['action'] . '/' . $keys;
  145. }