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