| 1 |
<?php
|
| 2 |
// $Id: search.pages.inc,v 1.11 2009/08/25 10:27:14 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* User page callbacks for the search module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Menu callback; presents the search form and/or search results.
|
| 11 |
*/
|
| 12 |
function search_view($type = 'node') {
|
| 13 |
// Search form submits with POST but redirects to GET. This way we can keep
|
| 14 |
// the search query URL clean as a whistle:
|
| 15 |
// search/type/keyword+keyword
|
| 16 |
if (!isset($_POST['form_id'])) {
|
| 17 |
if ($type == '') {
|
| 18 |
// Note: search/node can not be a default tab because it would take on the
|
| 19 |
// path of its parent (search). It would prevent remembering keywords when
|
| 20 |
// switching tabs. This is why we drupal_goto to it from the parent instead.
|
| 21 |
drupal_goto('search/node');
|
| 22 |
}
|
| 23 |
|
| 24 |
$keys = search_get_keys();
|
| 25 |
// Only perform search if there is non-whitespace search term:
|
| 26 |
$results = '';
|
| 27 |
if (trim($keys)) {
|
| 28 |
// Log the search keys:
|
| 29 |
watchdog('search', '%keys (@type).', array('%keys' => $keys, '@type' => module_invoke($type, 'search', 'name')), WATCHDOG_NOTICE, l(t('results'), 'search/' . $type . '/' . $keys));
|
| 30 |
|
| 31 |
// Collect the search results:
|
| 32 |
$results = search_data($keys, $type);
|
| 33 |
|
| 34 |
// Construct the search form.
|
| 35 |
$build['search_form'] = drupal_get_form('search_form', NULL, $keys, $type);
|
| 36 |
$build['search_results'] = array(
|
| 37 |
'#theme' => 'search_results_listing',
|
| 38 |
'#title' => empty($results) ? t('Your search yielded no results') : t('Search results'),
|
| 39 |
'#content' => empty($results) ? search_help('search#noresults', drupal_help_arg()) : $results,
|
| 40 |
);
|
| 41 |
|
| 42 |
return $build;
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
return drupal_get_form('search_form', NULL, empty($keys) ? '' : $keys, $type);
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Theme the listing of search results
|
| 51 |
*
|
| 52 |
* @param $variables
|
| 53 |
* An associative array containing:
|
| 54 |
* - title: The subject of the listing.
|
| 55 |
* - content: The content of the listing.
|
| 56 |
*
|
| 57 |
* @return
|
| 58 |
* A string containing the listing output.
|
| 59 |
*/
|
| 60 |
function theme_search_results_listing($variables) {
|
| 61 |
$output = '<h2 class="title">' . $variables['title'] . '</h2><div>' . $variables['content'] . '</div>';
|
| 62 |
return $output;
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Process variables for search-results.tpl.php.
|
| 67 |
*
|
| 68 |
* The $variables array contains the following arguments:
|
| 69 |
* - $results
|
| 70 |
* - $type
|
| 71 |
*
|
| 72 |
* @see search-results.tpl.php
|
| 73 |
*/
|
| 74 |
function template_preprocess_search_results(&$variables) {
|
| 75 |
$variables['search_results'] = '';
|
| 76 |
foreach ($variables['results'] as $result) {
|
| 77 |
$variables['search_results'] .= theme('search_result', array('result' => $result, 'type' => $variables['type']));
|
| 78 |
}
|
| 79 |
$variables['pager'] = theme('pager', array('tags' => NULL));
|
| 80 |
// Provide alternate search results template.
|
| 81 |
$variables['template_files'][] = 'search-results-' . $variables['type'];
|
| 82 |
}
|
| 83 |
|
| 84 |
/**
|
| 85 |
* Process variables for search-result.tpl.php.
|
| 86 |
*
|
| 87 |
* The $variables array contains the following arguments:
|
| 88 |
* - $result
|
| 89 |
* - $type
|
| 90 |
*
|
| 91 |
* @see search-result.tpl.php
|
| 92 |
*/
|
| 93 |
function template_preprocess_search_result(&$variables) {
|
| 94 |
$result = $variables['result'];
|
| 95 |
$variables['url'] = check_url($result['link']);
|
| 96 |
$variables['title'] = check_plain($result['title']);
|
| 97 |
|
| 98 |
$info = array();
|
| 99 |
if (!empty($result['type'])) {
|
| 100 |
$info['type'] = check_plain($result['type']);
|
| 101 |
}
|
| 102 |
if (!empty($result['user'])) {
|
| 103 |
$info['user'] = $result['user'];
|
| 104 |
}
|
| 105 |
if (!empty($result['date'])) {
|
| 106 |
$info['date'] = format_date($result['date'], 'short');
|
| 107 |
}
|
| 108 |
if (isset($result['extra']) && is_array($result['extra'])) {
|
| 109 |
$info = array_merge($info, $result['extra']);
|
| 110 |
}
|
| 111 |
// Check for existence. User search does not include snippets.
|
| 112 |
$variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
|
| 113 |
// Provide separated and grouped meta information..
|
| 114 |
$variables['info_split'] = $info;
|
| 115 |
$variables['info'] = implode(' - ', $info);
|
| 116 |
// Provide alternate search result template.
|
| 117 |
$variables['template_files'][] = 'search-result-' . $variables['type'];
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* As the search form collates keys from other modules hooked in via
|
| 122 |
* hook_form_alter, the validation takes place in _submit.
|
| 123 |
* search_form_validate() is used solely to set the 'processed_keys' form
|
| 124 |
* value for the basic search form.
|
| 125 |
*/
|
| 126 |
function search_form_validate($form, &$form_state) {
|
| 127 |
form_set_value($form['basic']['inline']['processed_keys'], trim($form_state['values']['keys']), $form_state);
|
| 128 |
}
|
| 129 |
|
| 130 |
/**
|
| 131 |
* Process a search form submission.
|
| 132 |
*/
|
| 133 |
function search_form_submit($form, &$form_state) {
|
| 134 |
$keys = $form_state['values']['processed_keys'];
|
| 135 |
if ($keys == '') {
|
| 136 |
form_set_error('keys', t('Please enter some keywords.'));
|
| 137 |
// Fall through to the drupal_goto() call.
|
| 138 |
}
|
| 139 |
|
| 140 |
$type = $form_state['values']['module'] ? $form_state['values']['module'] : 'node';
|
| 141 |
$form_state['redirect'] = 'search/' . $type . '/' . $keys;
|
| 142 |
return;
|
| 143 |
}
|