search.pages.inc

  1. 7.x drupal-7.x/modules/search/search.pages.inc
  2. 6.x drupal-6.x/modules/search/search.pages.inc

User page callbacks for the search module.

File

drupal-6.x/modules/search/search.pages.inc
View source
  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. function search_view($type = 'node') {
  10. // Search form submits with POST but redirects to GET. This way we can keep
  11. // the search query URL clean as a whistle:
  12. // search/type/keyword+keyword
  13. if (!isset($_POST['form_id'])) {
  14. if ($type == '') {
  15. // Note: search/node can not be a default tab because it would take on the
  16. // path of its parent (search). It would prevent remembering keywords when
  17. // switching tabs. This is why we drupal_goto to it from the parent instead.
  18. drupal_goto('search/node');
  19. }
  20. $keys = search_get_keys();
  21. // Only perform search if there is non-whitespace search term:
  22. $results = '';
  23. if (trim($keys)) {
  24. // Log the search keys:
  25. watchdog('search', '%keys (@type).', array('%keys' => $keys, '@type' => module_invoke($type, 'search', 'name')), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys));
  26. // Collect the search results:
  27. $results = search_data($keys, $type);
  28. if ($results) {
  29. $results = theme('box', t('Search results'), $results);
  30. }
  31. else {
  32. $results = theme('box', t('Your search yielded no results'), search_help('search#noresults', drupal_help_arg()));
  33. }
  34. }
  35. // Construct the search form.
  36. $output = drupal_get_form('search_form', NULL, $keys, $type);
  37. $output .= $results;
  38. return $output;
  39. }
  40. return drupal_get_form('search_form', NULL, empty($keys) ? '' : $keys, $type);
  41. }
  42. /**
  43. * Process variables for search-results.tpl.php.
  44. *
  45. * The $variables array contains the following arguments:
  46. * - $results
  47. * - $type
  48. *
  49. * @see search-results.tpl.php
  50. */
  51. function template_preprocess_search_results(&$variables) {
  52. $variables['search_results'] = '';
  53. foreach ($variables['results'] as $result) {
  54. $variables['search_results'] .= theme('search_result', $result, $variables['type']);
  55. }
  56. $variables['pager'] = theme('pager', NULL, 10, 0);
  57. // Provide alternate search results template.
  58. $variables['template_files'][] = 'search-results-'. $variables['type'];
  59. }
  60. /**
  61. * Process variables for search-result.tpl.php.
  62. *
  63. * The $variables array contains the following arguments:
  64. * - $result
  65. * - $type
  66. *
  67. * @see search-result.tpl.php
  68. */
  69. function template_preprocess_search_result(&$variables) {
  70. $result = $variables['result'];
  71. $variables['url'] = check_url($result['link']);
  72. $variables['title'] = check_plain($result['title']);
  73. $info = array();
  74. if (!empty($result['type'])) {
  75. $info['type'] = check_plain($result['type']);
  76. }
  77. if (!empty($result['user'])) {
  78. $info['user'] = $result['user'];
  79. }
  80. if (!empty($result['date'])) {
  81. $info['date'] = format_date($result['date'], 'small');
  82. }
  83. if (isset($result['extra']) && is_array($result['extra'])) {
  84. $info = array_merge($info, $result['extra']);
  85. }
  86. // Check for existence. User search does not include snippets.
  87. $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  88. // Provide separated and grouped meta information..
  89. $variables['info_split'] = $info;
  90. $variables['info'] = implode(' - ', $info);
  91. // Provide alternate search result template.
  92. $variables['template_files'][] = 'search-result-'. $variables['type'];
  93. }
  94. /**
  95. * As the search form collates keys from other modules hooked in via
  96. * hook_form_alter, the validation takes place in _submit.
  97. * search_form_validate() is used solely to set the 'processed_keys' form
  98. * value for the basic search form.
  99. */
  100. function search_form_validate($form, &$form_state) {
  101. form_set_value($form['basic']['inline']['processed_keys'], trim($form_state['values']['keys']), $form_state);
  102. }
  103. /**
  104. * Process a search form submission.
  105. */
  106. function search_form_submit($form, &$form_state) {
  107. $keys = $form_state['values']['processed_keys'];
  108. if ($keys == '') {
  109. form_set_error('keys', t('Please enter some keywords.'));
  110. // Fall through to the drupal_goto() call.
  111. }
  112. $type = $form_state['values']['module'] ? $form_state['values']['module'] : 'node';
  113. $form_state['redirect'] = 'search/'. $type .'/'. $keys;
  114. return;
  115. }