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