search.pages.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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') {
  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. // Log the search keys.
  56. watchdog('search', 'Searched %type for %keys.', array('%keys' => $keys, '%type' => $info['title']), WATCHDOG_NOTICE, l(t('results'), 'search/' . $info['path'] . '/' . $keys));
  57. // Collect the search results.
  58. $results = search_data($keys, $info['module'], $conditions);
  59. }
  60. }
  61. // The form may be altered based on whether the search was run.
  62. $build['search_form'] = drupal_get_form('search_form', NULL, $keys, $info['module']);
  63. $build['search_results'] = $results;
  64. return $build;
  65. }
  66. /**
  67. * Process variables for search-results.tpl.php.
  68. *
  69. * The $variables array contains the following arguments:
  70. * - $results: Search results array.
  71. * - $module: Module the search results came from (module implementing
  72. * hook_search_info()).
  73. *
  74. * @see search-results.tpl.php
  75. */
  76. function template_preprocess_search_results(&$variables) {
  77. $variables['search_results'] = '';
  78. if (!empty($variables['module'])) {
  79. $variables['module'] = check_plain($variables['module']);
  80. }
  81. foreach ($variables['results'] as $result) {
  82. $variables['search_results'] .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
  83. }
  84. $variables['pager'] = theme('pager', array('tags' => NULL));
  85. $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
  86. }
  87. /**
  88. * Process variables for search-result.tpl.php.
  89. *
  90. * The $variables array contains the following arguments:
  91. * - $result
  92. * - $module
  93. *
  94. * @see search-result.tpl.php
  95. */
  96. function template_preprocess_search_result(&$variables) {
  97. global $language;
  98. $result = $variables['result'];
  99. $variables['url'] = check_url($result['link']);
  100. $variables['title'] = check_plain($result['title']);
  101. if (isset($result['language']) && $result['language'] != $language->language && $result['language'] != LANGUAGE_NONE) {
  102. $variables['title_attributes_array']['xml:lang'] = $result['language'];
  103. $variables['content_attributes_array']['xml:lang'] = $result['language'];
  104. }
  105. $info = array();
  106. if (!empty($result['module'])) {
  107. $info['module'] = check_plain($result['module']);
  108. }
  109. if (!empty($result['user'])) {
  110. $info['user'] = $result['user'];
  111. }
  112. if (!empty($result['date'])) {
  113. $info['date'] = format_date($result['date'], 'short');
  114. }
  115. if (isset($result['extra']) && is_array($result['extra'])) {
  116. $info = array_merge($info, $result['extra']);
  117. }
  118. // Check for existence. User search does not include snippets.
  119. $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  120. // Provide separated and grouped meta information..
  121. $variables['info_split'] = $info;
  122. $variables['info'] = implode(' - ', $info);
  123. $variables['theme_hook_suggestions'][] = 'search_result__' . $variables['module'];
  124. }
  125. /**
  126. * As the search form collates keys from other modules hooked in via
  127. * hook_form_alter, the validation takes place in _submit.
  128. * search_form_validate() is used solely to set the 'processed_keys' form
  129. * value for the basic search form.
  130. */
  131. function search_form_validate($form, &$form_state) {
  132. form_set_value($form['basic']['processed_keys'], trim($form_state['values']['keys']), $form_state);
  133. }
  134. /**
  135. * Process a search form submission.
  136. */
  137. function search_form_submit($form, &$form_state) {
  138. $keys = $form_state['values']['processed_keys'];
  139. if ($keys == '') {
  140. form_set_error('keys', t('Please enter some keywords.'));
  141. // Fall through to the form redirect.
  142. }
  143. $form_state['redirect'] = $form_state['action'] . '/' . $keys;
  144. }