Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / 7.x / modules / cdm_dataportal / cdm_dataportal.search.php @ 72032ea6

History | View | Annotate | Download (18.6 KB)

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_FINDBY_DESCRIPTIONELEMENT_FULLTEXT => "cdm_dataportal/search/taxon_by_description",
26
  );
27
  return $form_ws_map[$ws_endpoint];
28
}
29

    
30
/**
31
 * Prepares a form array for a general purpose search form.
32
 *
33
 * The form is used for general purpose search functionality in the
34
 * dataportal. The form returned is populated with all nessecary fields
35
 * for internal processing and has the textfield element $form['query']
36
 * which holds the query term.
37
 * he sub tree array can be extended to contain additional search parameters.
38
 *
39
 * @param string $action_path
40
 *   The Drupal path to be put into the action url to which the form will
41
 *   be submitted.
42
 * @param string $search_webservice
43
 *   The cdm-remote webservice to be used, valid values are defined by
44
 *   the constants: FIXME.
45
 * @param string $query_field_default_value
46
 * @param string $query_field_description
47
 * @param string $process
48
 *   The value for #process, if NULL (default), 'cdm_dataportal_search_process'
49
 *   is used.
50
 *
51
 * @return array
52
 *   The prepared form array.
53
 */
54
function cdm_dataportal_search_form_prepare($action_path, $search_webservice, $query_field_default_value, $query_field_description, $process = NULL) {
55
  if ($process == NULL) {
56
    $process = 'cdm_dataportal_search_process';
57
  }
58
  $form['#method'] = 'get';
59
  /*
60
  $form['#process'] = array(
61
    $process => array(),
62
  );
63
  */
64
  $form['#action'] = url($action_path, array(
65
    'absolute' => TRUE,
66
  ));
67

    
68
  $form['ws'] = array(
69
    '#type' => 'hidden',
70
    '#value' => $search_webservice,
71
    '#name' => 'ws',
72
  );
73

    
74
  $form['query'] = array(
75
    '#weight' => 0,
76
    '#type' => 'textfield',
77
    '#size' => 68,
78
  // Comment @WA: this causes the description to display also when hovering over
79
  // the textfield.
80
  // I think this should be removed, although it is currently there in
81
  // D5 portals.
82
    '#attributes' => array(
83
      'title' => $query_field_description,
84
    ),
85
    '#description' => $query_field_description,
86
    '#value' => $query_field_default_value,
87
    // '#description' => $query_field_description,
88
  );
89

    
90
  $form['search'] = array(
91
    '#weight' => 3,
92
    '#tree' => TRUE,
93
    // '#type' => $advancedForm ? 'fieldset': 'hidden',
94
    '#title' => t('Options'),
95
  );
96

    
97
  // Clean URL get forms breaks if we don't give it a 'q'.
98
  if (!(bool) variable_get('clean_url', '0')) {
99
    $form['search']['q'] = array(
100
      '#type' => 'hidden',
101
      '#value' => $action_path,
102
      '#name' => 'q',
103
    );
104
  }
105

    
106
  $form['submit'] = array(
107
    '#weight' => 5,
108
    '#type' => 'submit',
109
    '#name' => '',
110
    '#value' => t('Search'),
111
  );
112

    
113
  return $form;
114
}
115

    
116
/**
117
 * Creates a search form for searching on taxa.
118
 *
119
 * If advanced $advancedForm id TRUE the form will offer additinal choices
120
 *
121
 * @param unknown_type $form
122
 * @param unknown_type $form_state
123
 * @param bool $advancedForm
124
 *    default is FALSE
125
 * @param bool $classificationSelect
126
 *    set TRUE to offer a classifiaction selector in the form - default is FALSE
127
 *    if only available in the advanced mode
128
 *
129
 * @return
130
 *   the form array
131
 *
132
 */
133
function cdm_dataportal_search_taxon_form($form, &$form_state, $advancedForm = FALSE, $classificationSelect = TRUE) {
134
  global $theme_key;
135

    
136
  $tdwg_level_select = (isset($_SESSION['cdm']['search']['tdwg_level_select']) ? $_SESSION['cdm']['search']['tdwg_level_select'] : 2);
137
  $selected_areas = (isset($_SESSION['cdm']['search']['area']) ? $_SESSION['cdm']['search']['area'] : FALSE);
138
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
139

    
140
  $form = cdm_dataportal_search_form_prepare('cdm_dataportal/search/results/taxon', CDM_WS_PORTAL_TAXON_FIND, $query_field_default_value, t('Enter the name or part of a name you wish to search for. The asterisk  character * can always be used as wildcard.'), NULL);
141

    
142
  if (!$advancedForm) {
143
    $form['query']['#size'] = 20;
144
  }
145

    
146
  $form['search']['pageSize'] = array(
147
    '#weight' => -1,
148
    '#type' => 'hidden',
149
    '#value' => variable_get('cdm_dataportal_search_items_on_page', 25),
150
  );
151

    
152
  $form['search']['pageNumber'] = array(
153
    '#weight' => -1,
154
    '#type' => 'hidden',
155
    '#value' => 0,
156
  );
157

    
158
  if ($advancedForm) {
159

    
160
    // --- ADVANCED SEARCH FORM ---
161
    //
162

    
163
    // Get presets from settings.
164
    $preset_doTaxa = variable_get('cdm_search_doTaxa', 1);
165
    $preset_doSynonyms = variable_get('cdm_search_doSynonyms', 1);
166
    $preset_doTaxaByCommonNames = variable_get('cdm_search_doTaxaByCommonNames', 0);
167
    $preset_doMisappliedNames = variable_get('cdm_search_doMisappliedNames', 1);
168
    $preset_UseDefaults = variable_get('cdm_search_use_default_values', 1);
169
    $preset_classification_uuid = get_taxonomictree_uuid_selected();
170

    
171
    // Overwrite presets by user choice stored in session.
172
    if (isset($_SESSION['cdm']['search']) && !$preset_UseDefaults) {
173
      $preset_doTaxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
174
      $preset_doSynonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
175
      $preset_doMisappliedNames = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
176
      $preset_doTaxaByCommonNames = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
177
      if (isset($_SESSION['cdm']['search']['tree'])) {
178
        $preset_classification_uuid = $_SESSION['cdm']['search']['tree'];
179
      }
180
    }
181

    
182

    
183
   if ($classificationSelect === TRUE) {
184
      $form['search']['tree'] = array(
185
        '#weight' => 1,
186
        '#type' => 'select',
187
        '#default_value' => get_taxonomictree_uuid_selected(),
188
        '#options' => cdm_get_taxontrees_as_options(TRUE),
189
      );
190
   }
191

    
192
    // General search parameters.
193
    $form['search']['doTaxa'] = array(
194
      '#weight' => 2,
195
      '#type' => 'checkbox',
196
      '#title' => t('Search for accepted taxa'),
197
      '#value' => $preset_doTaxa,
198
    );
199
    $form['search']['doSynonyms'] = array(
200
      '#weight' => 3,
201
      '#type' => 'checkbox',
202
      '#title' => t('Search for synonyms'),
203
      '#value' => $preset_doSynonyms,
204
    );
205
    $form['search']['doMisappliedNames'] = array(
206
      '#weight' => 4,
207
      '#type' => 'checkbox',
208
      '#title' => t('Search for misapplied names'),
209
      '#value' => $preset_doMisappliedNames,
210
    );
211
    $form['search']['doTaxaByCommonNames'] = array(
212
      '#weight' => 5,
213
      '#type' => 'checkbox',
214
      '#title' => t('Search for common names'),
215
      '#value' => $preset_doTaxaByCommonNames
216
    );
217

    
218
    // Geographic Range.
219
    $form['search']['geographic_range'] = array(
220
      '#type' => 'fieldset',
221
      '#weight' => 5,
222
      '#tree' => TRUE,
223
      '#title' => t('Geographic range'),
224
    );
225

    
226
    $form['search']['geographic_range']['tdwg_level_select'] = array(
227
      '#type' => 'radios',
228
      '#title' => t('Select a TDWG distribution level and code'),
229
      '#default_value' => $tdwg_level_select,
230
      '#options' => array(
231
        t('TDWG level-1, i.e. a continent'),
232
        t('TDWG level-2'),
233
        t('TDWG level-3, i.e. a country'),
234
        t('TDWG level-4'),
235
      ),
236
    );
237
    $tdwg[1] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '1');
238
    $tdwg[2] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '2');
239
    $tdwg[3] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '3');
240
    $tdwg[4] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '4');
241

    
242
    $tdwg_js = '';
243
    foreach ($tdwg as $key => $tdwg_level) {
244
      $tdwgOptions = array();
245
      $tdwgOptionsSelected = array();
246
      foreach ($tdwg_level as $area) {
247
        $representation = $area->representations[0];
248
        $tdwgOptions[$representation->abbreviatedLabel] = $area->representation_L10n;
249
        if (is_array($selected_areas) && in_array($representation->abbreviatedLabel, $selected_areas)) {
250
          // $area->uuid;
251
          $tdwgOptionsSelected[] = $representation->abbreviatedLabel;
252
        }
253
      }
254
      asort($tdwgOptions);
255
      $form['search']['geographic_range']['tdwg_level_' . $key] = array(
256
        '#type' => 'select',
257
        '#title' => t('TDWG level') . ' ' . $key,
258
        '#default_value' => $tdwgOptionsSelected,
259
        '#multiple' => TRUE,
260
        '#options' => $tdwgOptions,
261
      );
262
      $tdwg_js .= "$('#edit-search-geographic-range-tdwg-level-$key').parent()" . ($tdwg_level_select + 1 == $key ? '.show()' : '.hide()') . ";\n";
263
    }
264

    
265
    drupal_add_js(
266
    "jQuery(document).ready(function($){
267
      $(\"input[name='search[geographic_range][tdwg_level_select]']\").change(
268
        function(){
269
          var selectId = $(\"input[name='search[geographic_range][tdwg_level_select]']:checked\").val();
270
          var i;
271
          for(i = 0; i < 4; i++){
272

273
            if(selectId == i){
274
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1) ).parent().fadeIn('slow');
275
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
276
            } else {
277
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).parent().fadeOut('slow');
278
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
279
            }
280
          }
281
        }
282
      );
283

284
    $tdwg_js
285
    });",
286
    array('type' => 'inline'));
287
  }
288
  else {
289
    // --- SIMPLE SEARCH FORM ---
290
    //
291

    
292
    $preset_doTaxa = variable_get('cdm_search_doTaxa', 1);
293
    $preset_doSynonyms = variable_get('cdm_search_doSynonyms', 1);
294
    $preset_doTaxaByCommonNames = variable_get('cdm_search_doTaxaByCommonNames', 0);
295
    $preset_doMisappliedNames = variable_get('cdm_search_doMisappliedNames', 1);
296
    $preset_UseDefaults = variable_get('cdm_search_use_default_values', 1);
297

    
298
    // Overwrite presets by user choice stored in session.
299
    if (isset($_SESSION['cdm']['search']) && !$preset_UseDefaults) {
300
      $preset_doMisappliedNames = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
301
    }
302

    
303
    $form['search']['doTaxa'] = array(
304
      '#weight' => -2,
305
      '#type' => 'hidden',
306
      '#value' => $preset_doTaxa,
307
    );
308
    $form['search']['doSynonyms'] = array(
309
      '#weight' => -3,
310
      '#type' => 'hidden',
311
      '#value' => $preset_doSynonyms,
312
    );
313
    $form['search']['doMisappliedNames'] = array(
314
      '#weight' => -4,
315
      '#type' => 'checkbox',
316
      '#title' => t('Misapplied names'),
317
      '#value' => $preset_doMisappliedNames,
318
    );
319
    $form['search']['doTaxaByCommonNames'] = array(
320
      '#weight' => -5,
321
      '#type' => 'hidden',
322
      '#value' => $preset_doTaxaByCommonNames,
323
    );
324
  }
325

    
326
  return $form;
327
}
328
/**
329
 * @todo Please document this function.
330
 * @see http://drupal.org/node/1354
331
 */
332
function cdm_dataportal_search_taxon_form_advanced($form, &$form_state) {
333
  return cdm_dataportal_search_taxon_form($form, $form_state, TRUE);
334
}
335

    
336
/**
337
 * Search form for the searching taxa by the findByDescriptionElementFullText
338
 * rest service.
339
 */
340
function cdm_dataportal_search_taxon_by_description_form() {
341
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
342

    
343
  $form = cdm_dataportal_search_form_prepare(
344
      'cdm_dataportal/search/results/taxon',
345
      CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT,
346
      $query_field_default_value,
347
      t("Enter the text you wish to search for. The asterisk character * can be
348
        used as wildcard. Terms can be combined with 'AND'. To search for a
349
        full phrase enclose the terms in parentheses. For more syntactial
350
        options please refer to the !link.", array(
351
          '!link' => l(t('Apache Lucene - Query Parser Syntax'), 'http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html', array(
352
             'attributes' => array('absolute' => TRUE, 'html' => TRUE))),
353
        )),
354
      NULL
355
      );
356

    
357
  $form['search']['tree'] = array(
358
    '#weight' => -1,
359
    '#type' => 'hidden',
360
    '#value' => get_taxonomictree_uuid_selected(),
361
  );
362

    
363
  $form['search']['hl'] = array(
364
    '#weight' => -1,
365
    '#type' => 'hidden',
366
    '#value' => 1,
367
  );
368

    
369
  // Only avaiable to admins:
370
  if (!isset($_SESSION['cdm']['search']['clazz'])) {
371
    $_SESSION['cdm']['search']['clazz'] = '';
372
  }
373
  if (module_exists("user") && user_access('administer')) {
374
    $form['search']['clazz'] = array(
375
      '#type' => 'select',
376
      '#title' => t('Limit to DescriptionElement type'),
377
      '#default_value' => $_SESSION['cdm']['search']['clazz'],
378
      '#options' => cdm_descriptionElementTypes_as_option(TRUE),
379
    );
380
  }
381

    
382
  /*
383
  see cdm_get_featureTrees_as_options() ... $treeRepresentation =
384
  $featureTree->titleCache; if(is_array($featureTree->root->children) &&
385
  count($featureTree->root->children) > 0){ // render the hierarchic tree
386
  structure $treeDetails = '<div class="featuretree_structure">'
387
  //.cdm_featureTree_elements_toString($featureTree->root)
388
  .theme('featureTree_hierarchy', $featureTree->uuid) .'</div>'; $form =
389
  array(); $form['featureTree-'.$featureTree->uuid] = array( '#type' =>
390
  'fieldset', '#title' => t('Show details'), '#collapsible' => TRUE,
391
  '#collapsed' => TRUE, );
392
  $form['featureTree-'.$featureTree->uuid]['details'] =
393
  array('#value'=>$treeDetails); $treeRepresentation .= drupal_render($form);
394
  */
395

    
396
  $profile_featureTree = get_profile_featureTree();
397
  $feature_options = _featureTree_nodes_as_feature_options($profile_featureTree->root);
398
  if (isset($_SESSION['cdm']['search']['features'])) {
399
    $form['search']['features'] = array(
400
      '#type' => 'checkboxes',
401
      '#title' => t('Limit to selected features'),
402
      '#default_value' => $_SESSION['cdm']['search']['features'],
403
      '#options' => $feature_options,
404
    );
405
  }
406
  else {
407
    $form['search']['features'] = array(
408
      '#type' => 'checkboxes',
409
      '#title' => t('Limit to selected features'),
410
      '#options' => $feature_options,
411
    );
412
  }
413
  return $form;
414
}
415

    
416
/**
417
 * Reads the query parameters from $_REQUEST and modifies and adds additional query parameters if nessecary.
418
 *
419
 *  - Filters $_REQUEST by a list of valid request parameters
420
 *  - modifies geographic_range parameters
421
 *  - adds taxon tree uuid if it is missing and if it should not be
422
 *    ignored (parameter value = 'IGNORE')
423
 *  - and more
424
 *
425
 *
426
 * @return
427
 *   the processed request parameters submitted by the search form and
428
 *   also stores them in $_SESSION['cdm']['search']
429
 */
430
function cdm_dataportal_search_form_request() {
431

    
432
  $form_params = array();
433

    
434
  if (isset($_REQUEST['search']) && is_array($_REQUEST['search'])) {
435
    array_deep_copy($_REQUEST['search'], $form_params);
436
  }
437

    
438
  if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
439
    $form_params = array_merge($form_params, $_REQUEST['pager']);
440
  }
441

    
442
  $form_params['query'] = trim($_REQUEST['query']);
443

    
444
  // --- handle geographic range
445
  // Split of geographic range.
446
  if (isset($_REQUEST['search']['geographic_range']) && is_array($_REQUEST['search']['geographic_range'])) {
447
    $geographicRange = $_REQUEST['search']['geographic_range'];
448
    // Remove from form.
449
    unset($form_params['geographic_range']);
450
    $form_params['tdwg_level_select'] = $geographicRange['tdwg_level_select'];
451
    for ($i = 1; $i < 5; $i++) {
452
      if (isset($geographicRange['tdwg_level_' . $i])) {
453
        $form_params['area'] = $geographicRange['tdwg_level_' . $i];
454
      }
455
    }
456
  }
457

    
458
  // simple search will not submit a 'tree' query parameter, so we add it here from
459
  // what is stored in the session unless 'simple_search_ignore_classification'
460
  // is checked in the settings
461
  if (!isset($form_params['tree']) && !variable_get('simple_search_ignore_classification', 1)) {
462
    $form_params['tree'] = get_taxonomictree_uuid_selected();
463
  }
464
  // if the 'NONE' classification has been chosen (adanced search) delete the tree information
465
  // to avoid unknown uuid exceptions in the cdm service
466
  if (isset($form_params['tree']) && $form_params['tree'] == 'NONE') {
467
//     $form_params['ignore_classification'] =  TRUE;
468
    unset($form_params['tree']);
469
  }
470
//   else {
471
//     $form_params['ignore_classification'] =  NULL;
472
//   }
473

    
474
  // Store in session.
475
  $_SESSION['cdm']['search'] = $form_params;
476

    
477
  return $form_params;
478
}
479

    
480
/**
481
 * Provides the classification the last search has been run on if any.
482
 *
483
 * This function should only be used after the cdm_dataportal_search_execute() handler has been run,
484
 * otherwise it will return the infomation from the last search executed. The information is retrieved from
485
 * the $_SESSION variable:  $_SESSION['cdm']['search']['tree']
486
 *
487
 * @return bool
488
 *    the classification the last processed search has been run on or NULL, it it was on all classifications
489
 */
490
function cdm_dataportal_searched_in_classification() {
491

    
492
  $classification = &drupal_static(__FUNCTION__);
493

    
494
  if (!isset($classification)) {
495
    if (isset($_SESSION['cdm']['search']['tree'])) {
496
      $classification = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY, ($_SESSION['cdm']['search']['tree']));
497
    } else {
498
      $classification = FALSE;
499
    }
500
  }
501

    
502
  return $classification !== FALSE ?  $classification : NULL;
503
}
504

    
505
/**
506
 * Removes Drupal internal form elements from query.
507
 */
508
function cdm_dataportal_search_process($form, &$form_state) {
509
  unset($form['form_id']);
510
  unset($form['form_token']);
511
  return $form;
512
}
513

    
514
/**
515
 * Sends a search request at the cdm web server.
516
 *
517
 * The parameters to build the query are taken obtained by calling
518
 * cdm_dataportal_search_form_request() which reads the query parameters
519
 * from $_REQUEST and add additional query parameters if nessecary.
520
 *
521
 * @see cdm_dataportal_search_form_request()
522
 *
523
 */
524
function cdm_dataportal_search_execute() {
525

    
526
  // Store as last search in session.
527
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
528

    
529
  // Validate the search webservice parameter:
530
  if (!isset($_REQUEST['ws'])) {// Check is ws.
531
    // Endpoint is unknown.
532
    drupal_set_message(t('webservice parameter \'ws\' missing'), 'warning');
533
    return NULL;
534
  }
535
  if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {// Check is ws.
536
    // Endpoint is unknown.
537
    drupal_set_message(t('Invalid search webservice parameter  \'ws\' given'), 'warning');
538
    return NULL;
539
  }
540

    
541
  // read the query parameters from $_REQUEST and add additional query parameters if nessecary.
542
  $request_params = cdm_dataportal_search_form_request();
543

    
544
  $taxonPager = cdm_ws_get($_REQUEST['ws'], NULL, queryString($request_params));
545

    
546
  return $taxonPager;
547
}
Add picture from clipboard (Maximum size: 40 MB)