1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Search related functions.
|
5
|
*/
|
6
|
|
7
|
/**
|
8
|
* Returns a Drupal path to a search form for a CDM webservice.
|
9
|
*
|
10
|
* For a given CDM webservice end-point, the drupal page path to the
|
11
|
* according search form is returned.
|
12
|
* cdm webservice end points are defined in constant variables like:
|
13
|
* <code>CDM_WS_PORTAL_TAXON_FIND</code> and
|
14
|
* <code>CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT</code>
|
15
|
*
|
16
|
* @param string $ws_endpoint
|
17
|
* The cdm webservice endpoint for which to find the search form path.
|
18
|
*
|
19
|
* @return string
|
20
|
* The Drupal path found.
|
21
|
*/
|
22
|
function cdm_dataportal_search_form_path_for_ws($ws_endpoint) {
|
23
|
static $form_ws_map = array(
|
24
|
CDM_WS_PORTAL_TAXON_FIND => "cdm_dataportal/search",
|
25
|
CDM_WS_PORTAL_TAXON_SEARCH => "cdm_dataportal/search",
|
26
|
CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT => "cdm_dataportal/search/taxon_by_description",
|
27
|
);
|
28
|
return $form_ws_map[$ws_endpoint];
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* Prepares a form array for a general purpose search form.
|
33
|
*
|
34
|
* The form is used for general purpose search functionality in the
|
35
|
* dataportal. The form returned is populated with all nessecary fields
|
36
|
* for internal processing and has the textfield element $form['query']
|
37
|
* which holds the query term.
|
38
|
* he sub tree array can be extended to contain additional search parameters.
|
39
|
*
|
40
|
* @param string $action_path
|
41
|
* The Drupal path to be put into the action url to which the form will
|
42
|
* be submitted.
|
43
|
* @param string $search_webservice
|
44
|
* The cdm-remote webservice to be used, valid values are defined by
|
45
|
* the constants: FIXME.
|
46
|
* @param string $query_field_default_value
|
47
|
* @param string $query_field_description
|
48
|
* @param string $process
|
49
|
* The value for #process, if NULL (default), 'cdm_dataportal_search_process'
|
50
|
* is used.
|
51
|
*
|
52
|
* @return array
|
53
|
* The prepared form array.
|
54
|
*/
|
55
|
function cdm_dataportal_search_form_prepare($action_path, $search_webservice, $query_field_default_value, $query_field_description, $process = NULL) {
|
56
|
|
57
|
if ($process == NULL) {
|
58
|
$process = 'cdm_dataportal_search_process';
|
59
|
}
|
60
|
|
61
|
$form['#method'] = 'get';
|
62
|
/*
|
63
|
$form['#process'] = array(
|
64
|
$process => array(),
|
65
|
);
|
66
|
*/
|
67
|
$form['#action'] = url($action_path, array(
|
68
|
'absolute' => TRUE,
|
69
|
));
|
70
|
|
71
|
$form['ws'] = array(
|
72
|
'#type' => 'hidden',
|
73
|
'#value' => $search_webservice,
|
74
|
'#name' => 'ws',
|
75
|
);
|
76
|
|
77
|
$form['query'] = array(
|
78
|
'#weight' => 0,
|
79
|
'#type' => 'textfield',
|
80
|
'#size' => 68,
|
81
|
// this causes the description to display also when hovering over
|
82
|
// the textfield.
|
83
|
// This is wanted behaviour for the simple seach but could
|
84
|
// be disabled for the advances search
|
85
|
'#attributes' => array(
|
86
|
'title' => $query_field_description,
|
87
|
),
|
88
|
'#description' => $query_field_description,
|
89
|
'#value' => $query_field_default_value,
|
90
|
// '#description' => $query_field_description,
|
91
|
);
|
92
|
|
93
|
$form['search'] = array(
|
94
|
'#weight' => 3,
|
95
|
'#tree' => TRUE,
|
96
|
// '#type' => $advancedForm ? 'fieldset': 'hidden',
|
97
|
'#title' => t('Options'),
|
98
|
);
|
99
|
|
100
|
// Clean URL get forms breaks if we don't give it a 'q'.
|
101
|
if (!(bool) variable_get('clean_url', '0')) {
|
102
|
$form['search']['q'] = array(
|
103
|
'#type' => 'hidden',
|
104
|
'#value' => $action_path,
|
105
|
'#name' => 'q',
|
106
|
);
|
107
|
}
|
108
|
|
109
|
$form['submit'] = array(
|
110
|
'#weight' => 5,
|
111
|
'#type' => 'submit',
|
112
|
'#name' => '',
|
113
|
'#value' => t('Search'),
|
114
|
);
|
115
|
|
116
|
return $form;
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Creates a search form for searching on taxa.
|
121
|
*
|
122
|
* If advanced $advancedForm id TRUE the form will offer additional choices
|
123
|
*
|
124
|
* @param array $form
|
125
|
* @param array $form_state
|
126
|
* @param bool $advancedForm
|
127
|
* default is FALSE
|
128
|
* @param bool $classificationSelect
|
129
|
* set TRUE to offer a classifiaction selector in the form - default is FALSE
|
130
|
* if only available in the advanced mode
|
131
|
*
|
132
|
* @return
|
133
|
* the form array
|
134
|
*
|
135
|
*/
|
136
|
function cdm_dataportal_search_taxon_form($form, &$form_state, $advancedForm = FALSE, $classificationSelect = TRUE) {
|
137
|
|
138
|
$query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
|
139
|
|
140
|
if ($advancedForm || variable_get(SIMPLE_SEARCH_USE_LUCENE_BACKEND, FALSE)) {
|
141
|
$search_service_endpoint = CDM_WS_PORTAL_TAXON_SEARCH;
|
142
|
} else {
|
143
|
$search_service_endpoint = CDM_WS_PORTAL_TAXON_FIND;
|
144
|
}
|
145
|
|
146
|
$form = cdm_dataportal_search_form_prepare(
|
147
|
'cdm_dataportal/search/results/taxon',
|
148
|
$search_service_endpoint,
|
149
|
$query_field_default_value,
|
150
|
t('Enter the name or part of a name you wish to search for.
|
151
|
The asterisk character * can always be used as wildcard.'),
|
152
|
NULL
|
153
|
);
|
154
|
|
155
|
if (!$advancedForm){
|
156
|
$form['query']['#size'] = 20;
|
157
|
}
|
158
|
|
159
|
$form['search']['pageSize'] = array(
|
160
|
'#weight' => -1,
|
161
|
'#type' => 'hidden',
|
162
|
'#value' => variable_get('cdm_dataportal_search_items_on_page', 25),
|
163
|
);
|
164
|
|
165
|
$form['search']['pageNumber'] = array(
|
166
|
'#weight' => -1,
|
167
|
'#type' => 'hidden',
|
168
|
'#value' => 0,
|
169
|
);
|
170
|
|
171
|
$search_taxa_mode_settings = get_array_variable_merged(CDM_SEARCH_TAXA_MODE, CDM_SEARCH_TAXA_MODE_DEFAULT);
|
172
|
$preset_doTaxa = $search_taxa_mode_settings['doTaxa'] !== 0;
|
173
|
$preset_doSynonyms = $search_taxa_mode_settings['doSynonyms'] !== 0;
|
174
|
$preset_doTaxaByCommonNames = $search_taxa_mode_settings['doTaxaByCommonNames'] !== 0;
|
175
|
$preset_doMisappliedNames = $search_taxa_mode_settings['doMisappliedNames'] !== 0;
|
176
|
|
177
|
if ($advancedForm) {
|
178
|
|
179
|
// --- ADVANCED SEARCH FORM ---
|
180
|
//
|
181
|
|
182
|
// Get presets from settings.
|
183
|
$preset_classification_uuid = get_taxonomictree_uuid_selected();
|
184
|
|
185
|
// Overwrite presets by user choice stored in session.
|
186
|
if (isset($_SESSION['cdm']['search'])) {
|
187
|
$preset_doTaxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
|
188
|
$preset_doSynonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
|
189
|
$preset_doMisappliedNames = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
|
190
|
$preset_doTaxaByCommonNames = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
|
191
|
if (isset($_SESSION['cdm']['search']['tree'])) {
|
192
|
$preset_classification_uuid = $_SESSION['cdm']['search']['tree'];
|
193
|
}
|
194
|
}
|
195
|
|
196
|
|
197
|
if ($classificationSelect === TRUE) {
|
198
|
$form['search']['tree'] = array(
|
199
|
'#title' => t('Classification'),
|
200
|
'#weight' => 1,
|
201
|
'#type' => 'select',
|
202
|
'#default_value' => get_taxonomictree_uuid_selected(),
|
203
|
'#options' => cdm_get_taxontrees_as_options(TRUE),
|
204
|
'#description' => t('A filter to limit the search to a specific classification. Choosing <em>-- None --</em> will disable this filter.'),
|
205
|
);
|
206
|
}
|
207
|
|
208
|
// General search parameters.
|
209
|
$form['search']['doTaxa'] = array(
|
210
|
'#weight' => 2,
|
211
|
'#type' => 'checkbox',
|
212
|
'#title' => t('Search for accepted taxa'),
|
213
|
'#value' => $preset_doTaxa,
|
214
|
);
|
215
|
$form['search']['doSynonyms'] = array(
|
216
|
'#weight' => 3,
|
217
|
'#type' => 'checkbox',
|
218
|
'#title' => t('Search for synonyms'),
|
219
|
'#value' => $preset_doSynonyms,
|
220
|
);
|
221
|
$form['search']['doMisappliedNames'] = array(
|
222
|
'#weight' => 4,
|
223
|
'#type' => 'checkbox',
|
224
|
'#title' => t('Search for misapplied names'),
|
225
|
'#value' => $preset_doMisappliedNames,
|
226
|
);
|
227
|
$form['search']['doTaxaByCommonNames'] = array(
|
228
|
'#weight' => 5,
|
229
|
'#type' => 'checkbox',
|
230
|
'#title' => t('Search for common names'),
|
231
|
'#value' => $preset_doTaxaByCommonNames,
|
232
|
);
|
233
|
|
234
|
$areas_options = cdm_terms_as_options(cdm_ws_fetch_all(CDM_WS_DESCRIPTION_NAMEDAREAS_IN_USE));
|
235
|
$areas_defaults = array();
|
236
|
if(isset($_SESSION['cdm']['search']['area'])){
|
237
|
$areas_defaults = explode(',', $_SESSION['cdm']['search']['area']);
|
238
|
}
|
239
|
$form['search']['area'] = array(
|
240
|
'#type' => 'checkboxes',
|
241
|
'#title' => t('Filter by distribution areas'),
|
242
|
'#default_value' => $areas_defaults,
|
243
|
'#options' => $areas_options,
|
244
|
'#description' => t('Check one or multiple areas to filter by distribution.'),
|
245
|
);
|
246
|
|
247
|
} else {
|
248
|
// --- SIMPLE SEARCH FORM ---
|
249
|
//
|
250
|
|
251
|
// Overwrite presets by user choice stored in session.
|
252
|
if (isset($_SESSION['cdm']['search'])) {
|
253
|
$preset_doMisappliedNames = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
|
254
|
}
|
255
|
|
256
|
$form['search']['doTaxa'] = array(
|
257
|
'#weight' => -2,
|
258
|
'#type' => 'hidden',
|
259
|
'#value' => $preset_doTaxa,
|
260
|
);
|
261
|
$form['search']['doSynonyms'] = array(
|
262
|
'#weight' => -3,
|
263
|
'#type' => 'hidden',
|
264
|
'#value' => $preset_doSynonyms,
|
265
|
);
|
266
|
$form['search']['doMisappliedNames'] = array(
|
267
|
'#weight' => -4,
|
268
|
'#type' => 'checkbox',
|
269
|
'#title' => t('Misapplied names'),
|
270
|
'#value' => $preset_doMisappliedNames,
|
271
|
);
|
272
|
$form['search']['doTaxaByCommonNames'] = array(
|
273
|
'#weight' => -5,
|
274
|
'#type' => 'hidden',
|
275
|
'#value' => $preset_doTaxaByCommonNames,
|
276
|
);
|
277
|
}
|
278
|
|
279
|
return $form;
|
280
|
}
|
281
|
/**
|
282
|
* @todo Please document this function.
|
283
|
* @see http://drupal.org/node/1354
|
284
|
*/
|
285
|
function cdm_dataportal_search_taxon_form_advanced($form, &$form_state) {
|
286
|
return cdm_dataportal_search_taxon_form($form, $form_state, TRUE);
|
287
|
}
|
288
|
|
289
|
/**
|
290
|
* Search form for the searching taxa by the findByDescriptionElementFullText
|
291
|
* rest service.
|
292
|
*/
|
293
|
function cdm_dataportal_search_taxon_by_description_form() {
|
294
|
$query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
|
295
|
|
296
|
$form = cdm_dataportal_search_form_prepare(
|
297
|
'cdm_dataportal/search/results/taxon',
|
298
|
CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT,
|
299
|
$query_field_default_value,
|
300
|
t("Enter the text you wish to search for. The asterisk character * can be
|
301
|
used as wildcard. Terms can be combined with 'AND'. To search for a
|
302
|
full phrase enclose the terms in parentheses. For more syntactial
|
303
|
options please refer to the !link.", array(
|
304
|
'!link' => l(t('Apache Lucene - Query Parser Syntax'), 'http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html', array(
|
305
|
'attributes' => array('absolute' => TRUE, 'html' => TRUE))),
|
306
|
)),
|
307
|
NULL
|
308
|
);
|
309
|
|
310
|
$form['search']['tree'] = array(
|
311
|
'#weight' => -1,
|
312
|
'#type' => 'hidden',
|
313
|
'#value' => get_taxonomictree_uuid_selected(),
|
314
|
);
|
315
|
|
316
|
$form['search']['hl'] = array(
|
317
|
'#weight' => -1,
|
318
|
'#type' => 'hidden',
|
319
|
'#value' => 1,
|
320
|
);
|
321
|
|
322
|
// Only avaiable to admins:
|
323
|
if (!isset($_SESSION['cdm']['search']['clazz'])) {
|
324
|
$_SESSION['cdm']['search']['clazz'] = '';
|
325
|
}
|
326
|
if (module_exists("user") && user_access('administer')) {
|
327
|
$form['search']['clazz'] = array(
|
328
|
'#type' => 'select',
|
329
|
'#title' => t('Limit to DescriptionElement type'),
|
330
|
'#default_value' => $_SESSION['cdm']['search']['clazz'],
|
331
|
'#options' => cdm_descriptionElementTypes_as_option(TRUE),
|
332
|
);
|
333
|
}
|
334
|
|
335
|
/*
|
336
|
see cdm_get_featureTrees_as_options() ... $treeRepresentation =
|
337
|
$featureTree->titleCache; if(is_array($featureTree->root->childNodes) &&
|
338
|
count($featureTree->root->childNodes) > 0){ // render the hierarchic tree
|
339
|
structure $treeDetails = '<div class="featuretree_structure">'
|
340
|
//.cdm_featureTree_elements_toString($featureTree->root)
|
341
|
.theme('featureTree_hierarchy', $featureTree->uuid) .'</div>'; $form =
|
342
|
array(); $form['featureTree-'.$featureTree->uuid] = array( '#type' =>
|
343
|
'fieldset', '#title' => t('Show details'), '#collapsible' => TRUE,
|
344
|
'#collapsed' => TRUE, );
|
345
|
$form['featureTree-'.$featureTree->uuid]['details'] =
|
346
|
array('#value'=>$treeDetails); $treeRepresentation .= drupal_render($form);
|
347
|
*/
|
348
|
|
349
|
$profile_featureTree = get_profile_feature_tree();
|
350
|
$feature_options = _featureTree_nodes_as_feature_options($profile_featureTree->root);
|
351
|
if (isset($_SESSION['cdm']['search']['features'])) {
|
352
|
$form['search']['features'] = array(
|
353
|
'#type' => 'checkboxes',
|
354
|
'#title' => t('Limit to selected features'),
|
355
|
'#default_value' => $_SESSION['cdm']['search']['features'],
|
356
|
'#options' => $feature_options,
|
357
|
);
|
358
|
}
|
359
|
else {
|
360
|
$form['search']['features'] = array(
|
361
|
'#type' => 'checkboxes',
|
362
|
'#title' => t('Limit to selected features'),
|
363
|
'#options' => $feature_options,
|
364
|
);
|
365
|
}
|
366
|
return $form;
|
367
|
}
|
368
|
|
369
|
/**
|
370
|
* Reads the query parameters from $_REQUEST and modifies and adds additional query parameters if nessecary.
|
371
|
*
|
372
|
* - Filters $_REQUEST by a list of valid request parameters
|
373
|
* - modifies geographic_range parameters
|
374
|
* - adds taxon tree uuid if it is missing and if it should not be
|
375
|
* ignored (parameter value = 'IGNORE')
|
376
|
* - and more
|
377
|
*
|
378
|
*
|
379
|
* @return
|
380
|
* the processed request parameters submitted by the search form and
|
381
|
* also stores them in $_SESSION['cdm']['search']
|
382
|
*/
|
383
|
function cdm_dataportal_search_form_request() {
|
384
|
|
385
|
$form_params = array();
|
386
|
|
387
|
if (isset($_REQUEST['search']) && is_array($_REQUEST['search'])) {
|
388
|
array_deep_copy($_REQUEST['search'], $form_params);
|
389
|
}
|
390
|
|
391
|
if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
|
392
|
$form_params = array_merge($form_params, $_REQUEST['pager']);
|
393
|
}
|
394
|
|
395
|
$form_params['query'] = trim($_REQUEST['query']);
|
396
|
|
397
|
// --- handle geographic range
|
398
|
// Split of geographic range.
|
399
|
unset($form_params['area']);
|
400
|
if (isset($_REQUEST['search']['area']) && is_array($_REQUEST['search']['area'])) {
|
401
|
$form_params['area'] = implode(',', $_REQUEST['search']['area']);
|
402
|
}
|
403
|
|
404
|
// simple search will not submit a 'tree' query parameter, so we add it here from
|
405
|
// what is stored in the session unless 'simple_search_ignore_classification'
|
406
|
// is checked in the settings
|
407
|
if (!isset($form_params['tree']) && !variable_get('simple_search_ignore_classification', 1)) {
|
408
|
$form_params['tree'] = get_taxonomictree_uuid_selected();
|
409
|
}
|
410
|
// if the 'NONE' classification has been chosen (adanced search) delete the tree information
|
411
|
// to avoid unknown uuid exceptions in the cdm service
|
412
|
if (isset($form_params['tree']) && ($form_params['tree'] == 'NONE' || ! is_uuid($form_params['tree']))) {
|
413
|
// $form_params['ignore_classification'] = TRUE;
|
414
|
unset($form_params['tree']);
|
415
|
}
|
416
|
// else {
|
417
|
// $form_params['ignore_classification'] = NULL;
|
418
|
// }
|
419
|
|
420
|
// Store in session.
|
421
|
$_SESSION['cdm']['search'] = $form_params;
|
422
|
|
423
|
return $form_params;
|
424
|
}
|
425
|
|
426
|
/**
|
427
|
* Provides the classification ti which the last search has been limited to..
|
428
|
*
|
429
|
* This function should only be used after the cdm_dataportal_search_execute() handler has been run,
|
430
|
* otherwise it will return the infomation from the last search executed. The information is retrieved from
|
431
|
* the $_SESSION variable: $_SESSION['cdm']['search']['tree']
|
432
|
*
|
433
|
* @return
|
434
|
* the CDM classification instance which has been used a filter for the last processed search
|
435
|
* or NULL, it it was on all classifications
|
436
|
*/
|
437
|
function cdm_dataportal_searched_in_classification() {
|
438
|
|
439
|
$classification = &drupal_static(__FUNCTION__);
|
440
|
|
441
|
if (!isset($classification)) {
|
442
|
if (isset($_SESSION['cdm']['search']['tree'])) {
|
443
|
$classification = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY, ($_SESSION['cdm']['search']['tree']));
|
444
|
} else {
|
445
|
$classification = FALSE;
|
446
|
}
|
447
|
}
|
448
|
|
449
|
return $classification !== FALSE ? $classification : NULL;
|
450
|
}
|
451
|
|
452
|
/**
|
453
|
* Removes Drupal internal form elements from query.
|
454
|
*/
|
455
|
function cdm_dataportal_search_process($form, &$form_state) {
|
456
|
unset($form['form_id']);
|
457
|
unset($form['form_token']);
|
458
|
return $form;
|
459
|
}
|
460
|
|
461
|
/**
|
462
|
* Sends a search request at the cdm web server.
|
463
|
*
|
464
|
* The parameters to build the query are taken obtained by calling
|
465
|
* cdm_dataportal_search_form_request() which reads the query parameters
|
466
|
* from $_REQUEST and add additional query parameters if nessecary.
|
467
|
*
|
468
|
* @see cdm_dataportal_search_form_request()
|
469
|
*
|
470
|
*/
|
471
|
function cdm_dataportal_search_execute() {
|
472
|
|
473
|
// Store as last search in session.
|
474
|
$_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
|
475
|
|
476
|
// Validate the search webservice parameter:
|
477
|
if (!isset($_REQUEST['ws'])) {// Check is ws.
|
478
|
// Endpoint is unknown.
|
479
|
drupal_set_message(t('webservice parameter \'ws\' missing'), 'warning');
|
480
|
return NULL;
|
481
|
}
|
482
|
if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {// Check is ws.
|
483
|
// Endpoint is unknown.
|
484
|
drupal_set_message(t('Invalid search webservice parameter \'ws\' given'), 'warning');
|
485
|
return NULL;
|
486
|
}
|
487
|
|
488
|
// read the query parameters from $_REQUEST and add additional query parameters if nessecary.
|
489
|
$request_params = cdm_dataportal_search_form_request();
|
490
|
|
491
|
$taxonPager = cdm_ws_get($_REQUEST['ws'], NULL, queryString($request_params));
|
492
|
|
493
|
return $taxonPager;
|
494
|
}
|