0
|
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 solrsearch_view($keys = '') {
|
|
17
|
|
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
|
|
26
|
|
27 // Default results output is an empty string.
|
|
28 $results = array('#markup' => '');
|
|
29 // Process the search form. Note that if there is $_POST data,
|
|
30 // search_form_submit() will cause a redirect to search/[module path]/[keys],
|
|
31 // which will get us back to this page callback. In other words, the search
|
|
32 // form submits with POST but redirects to GET. This way we can keep
|
|
33 // the search query URL clean as a whistle.
|
|
34
|
|
35 if (empty($_POST['form_id']) || $_POST['form_id'] != 'solrsearch_form') {
|
|
36
|
|
37 $conditions = NULL;
|
|
38
|
|
39 $conditions = solrsearch_search_conditions($keys);
|
|
40
|
|
41 // Only search if there are keywords or non-empty conditions.
|
|
42 if ($keys || !empty($conditions)) {
|
|
43
|
|
44 // Log the search keys.
|
|
45 //watchdog('search', 'Searched %type for %keys.', array('%keys' => $keys, '%type' => $info['title']), WATCHDOG_NOTICE, l(t('results'), 'search/' . $info['path'] . '/' . $keys));
|
|
46
|
|
47 // Collect the search results.
|
|
48 $results = solrsearch_data($keys, 'solrsearch_search', $conditions);
|
|
49
|
|
50 }
|
|
51 }
|
|
52 // The form may be altered based on whether the search was run.
|
|
53
|
|
54 $wrapper = array(
|
|
55 '#type' => 'container',
|
|
56 '#attributes' => array(
|
|
57 'class' => array('tool','box'),
|
|
58 ),
|
|
59 );
|
|
60
|
|
61
|
|
62
|
|
63 $wrapper['form'] = drupal_get_form('solrsearch_form', NULL, $keys);
|
|
64
|
|
65 $build['search_form'] = $wrapper;
|
|
66
|
|
67
|
|
68 $build['search_results'] = $results;
|
|
69
|
|
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_solrsearch_results(&$variables) {
|
|
85 $variables['search_results'] = '';
|
|
86 if (!empty($variables['module'])) {
|
|
87 $variables['module'] = check_plain($variables['module']);
|
|
88 }
|
|
89
|
|
90
|
|
91
|
|
92
|
|
93 if (user_access("manage private collections")){
|
|
94 $variables['digitalobjects_items_select']=base_path()."digitalcollections/manageCurrent";
|
|
95 $variables['search_results'] .='<tr><td colspan="5"><input type="submit" value="add selected to current collection">';
|
|
96 $variables['search_results'] .='<input type="hidden" name="redirect" value="' . urlencode(current_path() .'?' . $_SERVER['QUERY_STRING']) . '"></td></tr>';
|
|
97 }
|
|
98
|
|
99 foreach ($variables['results'] as $result) {
|
|
100
|
|
101
|
|
102 /* add checkboxes for the item */
|
|
103
|
|
104 $variables['search_results'] .= '<tr><td colspan="5">';
|
|
105
|
|
106 if (user_access("manage private collections")){
|
|
107 $variables['search_results'] .= '<input type="checkbox" name="digitalobjects_items_select[]" value="'.$result['mpiwg-dri'].'"/>';
|
|
108 }
|
|
109
|
|
110 /* add tools for the item */
|
|
111 $variables['search_results'] .= theme('digitalobjects_item_tools', array('objid' => $result['mpiwg-dri'])) . "</td></tr>";
|
|
112
|
|
113
|
|
114 $variables['search_results'] .= theme('solrsearch_result', array('result' => $result, 'module' => $variables['module']));
|
|
115
|
|
116 }
|
|
117 $variables['pager'] = theme('pager', array('tags' => array('<<','<','...','>','>>'),'quantity' => 5));
|
|
118
|
|
119 $variables['description'] = '';
|
|
120 $variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
|
|
121 }
|
|
122
|
|
123
|
|
124 function check_array_plain($result,$field){
|
|
125 if (!isset($result[$field])){
|
|
126 return null;
|
|
127 }
|
|
128
|
|
129 $string = $result[$field];
|
|
130
|
|
131 if (is_array($string)){
|
|
132 return check_plain(implode($string));
|
|
133 } else {
|
|
134 return check_plain($string);
|
|
135 }
|
|
136 }
|
|
137 /**
|
|
138 * Process variables for search-result.tpl.php.
|
|
139 *
|
|
140 * The $variables array contains the following arguments:
|
|
141 * - $result
|
|
142 * - $module
|
|
143 *
|
|
144 * @see search-result.tpl.php
|
|
145 */
|
|
146 function template_preprocess_solrsearch_result(&$variables) {
|
|
147 global $language;
|
|
148
|
|
149 $result = $variables['result'];
|
|
150
|
|
151 $variables['url'] = create_url_from_dri($result['mpiwg-dri'],$result['access-type']);
|
|
152 $variables['access_type'] = $result['access-type']!="free" ? "restricted" :$result['access-type'];
|
|
153 $variables['title'] = check_plain($result['title']);
|
|
154 $variables['author'] = check_plain($result['author']);
|
|
155 $variables['date'] = check_array_plain($result,'date');
|
|
156 $variables['signature'] = check_array_plain($result,'signature');
|
|
157 $variables['author'] = check_plain($result['author']);
|
|
158 $variables['year'] = is_array($result['year']) ? implode($result['year']) :$result['year'];
|
|
159 $variables['thumburl'] = create_thumburl_from_dri($result['mpiwg-dri']);
|
|
160 // Check for existence. User search does not include snippets.
|
|
161 $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
|
|
162
|
|
163 $variables['theme_hook_suggestions'][] = 'search_result__' . $variables['module'];
|
|
164 }
|
|
165
|
|
166 /**
|
|
167 * As the search form collates keys from other modules hooked in via
|
|
168 * hook_form_alter, the validation takes place in _submit.
|
|
169 * search_form_validate() is used solely to set the 'processed_keys' form
|
|
170 * value for the basic search form.
|
|
171 */
|
|
172 function solrsearch_form_validate($form, &$form_state) {
|
|
173 form_set_value($form['basic']['processed_keys'], trim($form_state['values']['keys']), $form_state);
|
|
174 }
|
|
175
|
|
176 /**
|
|
177 * Process a search form submission.
|
|
178 */
|
|
179 function solrsearch_form_submit($form, &$form_state) {
|
|
180
|
|
181
|
|
182 $keys = $form_state['values']['processed_keys'];
|
|
183 if ($keys == '') {
|
|
184 form_set_error('keys', t('Please enter some keywords.'));
|
|
185 // Fall through to the form redirect.
|
|
186 }
|
|
187
|
|
188 $exact = $form_state['values']['exact'];
|
|
189 if ($exact==0) { #nicht genaue suche dann trunkiere das wort
|
|
190 $keys .="*";
|
|
191 }
|
|
192 $form_state['redirect'] = $form_state['action'] . '/' . $keys;
|
|
193 }
|
|
194
|
|
195
|
|
196 /**
|
|
197 * Builds a search form.
|
|
198 *
|
|
199 * @param $action
|
|
200 * Form action. Defaults to "search/$path", where $path is the search path
|
|
201 * associated with the module in its hook_search_info(). This will be
|
|
202 * run through url().
|
|
203 * @param $keys
|
|
204 * The search string entered by the user, containing keywords for the search.
|
|
205 * @param $module
|
|
206 * The search module to render the form for: a module that implements
|
|
207 * hook_search_info(). If not supplied, the default search module is used.
|
|
208 * @param $prompt
|
|
209 * Label for the keywords field. Defaults to t('Enter your keywords') if NULL.
|
|
210 * Supply '' to omit.
|
|
211 *
|
|
212 * @return
|
|
213 * A Form API array for the search form.
|
|
214 */
|
|
215 function solrsearch_form($form, &$form_state, $action = '', $keys = '', $module = NULL, $prompt = NULL) {
|
|
216 $module_info = FALSE;
|
|
217
|
|
218 if (!$action) {
|
|
219 $action = 'solrsearch';
|
|
220 }
|
|
221 if (!isset($prompt)) {
|
|
222 $prompt = t('Enter your keywords');
|
|
223
|
|
224 }
|
|
225
|
|
226 if ($keys == ''){
|
|
227 $keys=$prompt;
|
|
228 }
|
|
229
|
|
230 $form['#action'] = url($action);
|
|
231
|
|
232 // Record the $action for later use in redirecting.
|
|
233 $form_state['action'] = $action;
|
|
234 $form['#attributes']['class'][] = 'search-form';
|
|
235 $form['module'] = array('#type' => 'value', '#value' => $module);
|
|
236 $form['basic'] = array('#type' => 'container', '#attributes' => array('class' => array('container-inline','searchbox')));
|
|
237
|
|
238 $form['basic']['keys'] = array(
|
|
239 '#type' => 'textfield',
|
|
240 '#size' => $prompt ? 40 : 20,
|
|
241 '#attributes' =>array('placeholder' => t($keys),'class' => array('text')),
|
|
242 '#maxlength' => 255,
|
|
243 );
|
|
244 // processed_keys is used to coordinate keyword passing between other forms
|
|
245 // that hook into the basic search form.
|
|
246 $form['basic']['processed_keys'] = array('#type' => 'value', '#value' => '');
|
|
247
|
|
248
|
|
249 $form['basic']['submit'] = array(
|
|
250 '#type' => 'submit',
|
|
251 '#value' => t('>'),
|
|
252 '#attributes' =>array('class' => array('submit')),
|
|
253
|
|
254 );
|
|
255
|
|
256 $form['basic']['exact'] = array(
|
|
257 '#type' => 'checkbox',
|
|
258 '#title' => 'whole word',
|
|
259
|
|
260
|
|
261
|
|
262 );
|
|
263
|
|
264
|
|
265 return $form;
|
|
266 }
|
|
267
|
|
268
|
|
269
|