Project

General

Profile

Download (21.8 KB) Statistics
| Branch: | Tag: | Revision:
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
 *   A default text for the query field
48
 * @param string $query_field_description
49
 *   The description text for the query field
50
 * @param string $process
51
 *   The value for #process, if NULL (default), 'cdm_dataportal_search_process'
52
 *   is used.
53
 *
54
 * @return array
55
 *   The prepared form array.
56
 */
57
function cdm_dataportal_search_form_prepare($action_path, $search_webservice, $query_field_default_value, $query_field_description, $process = NULL) {
58

    
59
  if ($process == NULL) {
60
    $process = 'cdm_dataportal_search_process';
61
  }
62

    
63
  $form['#method'] = 'get';
64
  //
65
  //  $form['#process'] = array(
66
  //  $process => array(),
67
  //  );
68
  //
69
  $form['#action'] = url($action_path, array(
70
    'absolute' => TRUE,
71
  ));
72

    
73
  $form['ws'] = array(
74
    '#type' => 'hidden',
75
    '#value' => $search_webservice,
76
    '#name' => 'ws',
77
  );
78

    
79
  $form['query'] = array(
80
    '#weight' => 0,
81
    '#type' => 'textfield',
82
    '#size' => 68,
83
    // This causes the description to display also when hovering over
84
    // the textfield.
85
    // This is wanted behaviour for the simple seach but could
86
    // be disabled for the advances search.
87
    '#attributes' => array(
88
      'title' => $query_field_description,
89
    ),
90
    '#description' => $query_field_description,
91
    '#value' => $query_field_default_value,
92
    // '#description' => $query_field_description,
93
  );
94
  if(variable_get('cdm_dataportal_taxon_auto_suggest')){
95
      $form['query']['#autocomplete_path'] = 'cdm_dataportal/taxon/autosuggest';
96
  }
97

    
98
    $form['search'] = array(
99
    '#weight' => 3,
100
    '#tree' => TRUE,
101
    // '#type' => $advanced_form ? 'fieldset': 'hidden',
102
    '#title' => t('Options'),
103
  );
104

    
105
  // Clean URL get forms breaks if we don't give it a 'q'.
106
  if (!(bool) variable_get('clean_url', '0')) {
107
    $form['search']['q'] = array(
108
      '#type' => 'hidden',
109
      '#value' => $action_path,
110
      '#name' => 'q',
111
    );
112
  }
113

    
114
  $form['submit'] = array(
115
    '#weight' => 5,
116
    '#type' => 'submit',
117
    '#name' => '',
118
    '#value' => t('Search'),
119
  );
120

    
121
  return $form;
122
}
123

    
124
function cdm_dataportal_taxon_autosuggest($string, $treeUuid = NULL, $areaUuid = NULL, $status = NULL) {
125
  $matches = array();
126

    
127
  $queryParams = array();
128
  $queryParams['query'] = $string.'*';
129
  if($treeUuid){
130
    $queryParams['tree'] = $treeUuid;
131
  }
132
  if($areaUuid){
133
    $queryParams['area'] = $areaUuid;
134
  }
135
  if($status){
136
    $queryParams['status'] = $status ;
137
  }
138
  $queryParams['pageNumber'] = '0';
139
  $queryParams['pageSize'] = '10';
140
  $queryParams['doTaxa'] = true;
141
  $queryParams['doSynonyms'] = true;
142
  $queryParams['doMisappliedNames'] = true;
143
  $queryParams['doTaxaByCommonNames'] = true;
144

    
145
  $search_results = cdm_ws_get(CDM_WS_TAXON_SEARCH, NULL, queryString($queryParams));
146
  foreach($search_results->records as $record){
147
      $titleCache = $record->entity->titleCache;
148
      preg_match('/(.*) sec.*/', $titleCache, $trimmedTitle); //remove sec reference
149
      $trimmedTitle = trim($trimmedTitle[1]);
150
      $matches[$trimmedTitle] = $trimmedTitle;
151
  }
152
  drupal_json_output($matches);
153
}
154

    
155

    
156
  /**
157
 * Creates a search form for searching on taxa.
158
 *
159
 * If advanced $advanced_form id TRUE the form will offer additional choices
160
 *
161
 * @param array $form
162
 *   A drupal form array
163
 * @param array $form_state
164
 *   The drupal form state passed as reference
165
 * @param bool $advanced_form
166
 *   default is FALSE
167
 * @param bool $classification_select
168
 *   set TRUE to offer a classification selector in the form - default is FALSE
169
 *   if only available in the advanced mode
170
 *
171
 * @return array
172
 *   the form array
173
 */
174
function cdm_dataportal_search_taxon_form($form, &$form_state, $advanced_form = FALSE, $classification_select = TRUE) {
175

    
176
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
177

    
178
  if ($advanced_form || variable_get(SIMPLE_SEARCH_USE_LUCENE_BACKEND, FALSE)) {
179
    $search_service_endpoint = CDM_WS_PORTAL_TAXON_SEARCH;
180
  }
181
  else {
182
    $search_service_endpoint = CDM_WS_PORTAL_TAXON_FIND;
183
  }
184

    
185
  $form = cdm_dataportal_search_form_prepare(
186
    'cdm_dataportal/search/results/taxon',
187
    $search_service_endpoint,
188
    $query_field_default_value,
189
    t('Enter the name or part of a name you wish to search for.
190
      The asterisk  character * can be used as wildcard, but must not be used as first character.'),
191
      NULL
192
  );
193

    
194
  if (!$advanced_form) {
195
    $form['query']['#size'] = 20;
196
  }
197

    
198
  $form['search']['pageSize'] = array(
199
    '#weight' => -1,
200
    '#type' => 'hidden',
201
    '#value' => variable_get('cdm_dataportal_search_items_on_page', 25),
202
  );
203

    
204
  $form['search']['pageNumber'] = array(
205
    '#weight' => -1,
206
    '#type' => 'hidden',
207
    '#value' => 0,
208
  );
209

    
210
  $search_taxa_mode_settings = get_array_variable_merged(
211
    CDM_SEARCH_TAXA_MODE,
212
    CDM_SEARCH_TAXA_MODE_DEFAULT
213
  );
214
  $preset_do_taxa = $search_taxa_mode_settings['doTaxa'] !== 0;
215
  $preset_do_synonyms = $search_taxa_mode_settings['doSynonyms'] !== 0;
216
  $preset_do_taxa_by_common_names = $search_taxa_mode_settings['doTaxaByCommonNames'] !== 0;
217
  $preset_do_misapplied_names = $search_taxa_mode_settings['doMisappliedNames'] !== 0;
218

    
219
  if ($advanced_form) {
220

    
221
    // --- ADVANCED SEARCH FORM ---
222
    //
223

    
224
    // Get presets from settings.
225
    $preset_classification_uuid = get_current_classification_uuid();
226

    
227
    // Overwrite presets by user choice stored in session.
228
    if (isset($_SESSION['cdm']['search'])) {
229
      $preset_do_taxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
230
      $preset_do_synonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
231
      $preset_do_misapplied_names = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
232
      $preset_do_taxa_by_common_names = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
233
      if (isset($_SESSION['cdm']['search']['tree'])) {
234
        $preset_classification_uuid = $_SESSION['cdm']['search']['tree'];
235
      }
236
    }
237

    
238
    if ($classification_select === TRUE) {
239
      $form['search']['tree'] = array(
240
        '#title' => t('Classification'),
241
        '#weight' => 1,
242
        '#type' => 'select',
243
        '#default_value' => get_current_classification_uuid(),
244
        '#options' => cdm_get_taxontrees_as_options(TRUE),
245
        '#description' => t('A filter to limit the search to a specific classification. Choosing <em>--- ALL ---</em> will disable this filter.'),
246
      );
247
    }
248

    
249
    // General search parameters.
250
    $form['search']['doTaxa'] = array(
251
      '#weight' => 2,
252
      '#type' => 'checkbox',
253
      '#title' => t('Include') . ' ' . t('accepted taxa'),
254
      '#value' => $preset_do_taxa,
255
    );
256
    $form['search']['doSynonyms'] = array(
257
      '#weight' => 3,
258
      '#type' => 'checkbox',
259
      '#title' => t('Include') . ' ' . t('synonyms'),
260
      '#value' => $preset_do_synonyms,
261
    );
262
    $form['search']['doMisappliedNames'] = array(
263
      '#weight' => 4,
264
      '#type' => 'checkbox',
265
      '#title' => t('Include') . ' ' . t('misapplied names'),
266
      '#value' => $preset_do_misapplied_names,
267
    );
268
    $form['search']['doTaxaByCommonNames'] = array(
269
      '#weight' => 5,
270
      '#type' => 'checkbox',
271
      '#title' => t('Include') . ' ' . t('common names'),
272
      '#value' => $preset_do_taxa_by_common_names,
273
    );
274

    
275
    $area_term_dtos = cdm_ws_fetch_all(
276
      CDM_WS_DESCRIPTION_NAMEDAREAS_IN_USE,
277
      array('includeAllParents' => 'true')
278
    );
279

    
280
    // create map: term_uuid => term
281
    $term_map = array();
282
    foreach ($area_term_dtos as $term_dto) {
283
      $term_map[$term_dto->uuid] = $term_dto;
284
    }
285

    
286
    $term_tree = array();
287
    // mixed_vocabularies will contain the uuid vocabularies which
288
    // also contain terms of foreign vocabularies due to the term
289
    // hierarchy
290
    $mixed_vocabularies = array();
291

    
292
    // Build hierarchy of the terms regardless of the vocabulary.
293
    foreach ($term_map as $term_dto) {
294
      if (!empty($term_dto->partOfUuid)) {
295
        // Children.
296
        $parent =& $term_map[$term_dto->partOfUuid];
297
        if ($parent) {
298
          if (!isset($parent->children)) {
299
            $parent->children = array();
300
          }
301
          $parent->children[$term_dto->uuid] = $term_dto;
302
          if ($parent->vocabularyUuid != $term_dto->vocabularyUuid) {
303
            $mixed_vocabularies[$parent->vocabularyUuid] = $parent->vocabularyUuid;
304
          }
305
        }
306
      }
307
      else {
308
        // group root nodes by vocabulary
309
        if (!isset($term_tree[$term_dto->vocabularyUuid])) {
310
          $term_tree[$term_dto->vocabularyUuid] = array();
311
        }
312
        $term_tree[$term_dto->vocabularyUuid][$term_dto->uuid] = $term_dto;
313
      }
314
    }
315

    
316
    $show_area_filter = ! variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '');
317

    
318
    if($show_area_filter){
319
      drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/search_area_filter.js');
320

    
321
      drupal_add_js('jQuery(document).ready(function() {
322
        jQuery(\'#edit-search-areas\').search_area_filter(\'#edit-search-areas-areas-filter\');
323
      });
324
      ', array('type' => 'inline'));
325

    
326
      $form['search']['areas'] = array(
327
        '#type' => 'fieldset',
328
        '#title' => t('Filter by distribution areas'),
329
        '#description' => t('The search will return taxa having distribution
330
        information for at least one of the selected areas.') . ' '
331
          .(count($term_tree) > 1 ? t('The areas are grouped
332
        by the vocabularies to which the highest level areas belong.') : ''),
333
      );
334
      $form['search']['areas']['areas_filter'] = array(
335
        '#type' => 'textfield',
336
        '#description' => t('Type to filter the areas listed below.'),
337
      );
338
      $vocab_cnt = 0;
339
      $areas_defaults = array();
340
      if (isset($_SESSION['cdm']['search']['area'])) {
341
        $areas_defaults = explode(',', $_SESSION['cdm']['search']['area']);
342
      }
343
      foreach ($term_tree as $vocab_uuid => $term_dto_tree) {
344
        $vocabulary = cdm_ws_get(CDM_WS_TERMVOCABULARY, array($vocab_uuid));
345
        $areas_options = term_tree_as_options($term_dto_tree);
346
        $form['search']['areas']['area'][$vocab_cnt++] = array(
347
          '#prefix' => '<strong>' . $vocabulary->representation_L10n
348
            . (isset($mixed_vocabularies[$vocab_uuid]) ? ' <span title="Contains terms of at least one other area vocabulary.">(' . t('mixed') . ')</span>': '')
349
            . '</strong>',
350
          '#type' => 'checkboxes',
351
          '#default_value' => $areas_defaults,
352
          '#options' => $areas_options,
353
        );
354
      }
355
    }
356

    
357
  }
358
  else {
359
    // --- SIMPLE SEARCH FORM ---
360
    //
361

    
362
    // Overwrite presets by user choice stored in session.
363
    if (isset($_SESSION['cdm']['search'])) {
364
      $preset_do_misapplied_names = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
365
    }
366

    
367
    $form['search']['doTaxa'] = array(
368
      '#weight' => -2,
369
      '#type' => 'hidden',
370
      '#value' => $preset_do_taxa,
371
    );
372
    $form['search']['doSynonyms'] = array(
373
      '#weight' => -3,
374
      '#type' => 'hidden',
375
      '#value' => $preset_do_synonyms,
376
    );
377
    $form['search']['doMisappliedNames'] = array(
378
      '#weight' => -4,
379
      '#type' => 'checkbox',
380
      '#title' => t('Misapplied names'),
381
      '#value' => $preset_do_misapplied_names,
382
    );
383
    $form['search']['doTaxaByCommonNames'] = array(
384
      '#weight' => -5,
385
      '#type' => 'hidden',
386
      '#value' => $preset_do_taxa_by_common_names,
387
    );
388
  }
389

    
390
  return $form;
391
}
392

    
393
/**
394
 * Wrapper function for cdm_dataportal_search_taxon_form().
395
 *
396
 * This function makes ot possible possible to just pass the
397
 * correct $form_id 'cdm_dataportal_search_taxon_form_advanced' to
398
 * drupal_get_form like:
399
 * drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
400
 *
401
 * @param array $form
402
 *   A drupal form array
403
 * @param array $form_state
404
 *   The drupal form state passed as reference
405
 *
406
 * @return array
407
 *   The form array
408
 */
409
function cdm_dataportal_search_taxon_form_advanced($form, &$form_state) {
410
  return cdm_dataportal_search_taxon_form($form, $form_state, TRUE);
411
}
412

    
413
/**
414
 * Form for searching taxa by the findByDescriptionElementFullText rest service.
415
 */
416
function cdm_dataportal_search_taxon_by_description_form() {
417
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
418

    
419
  $form = cdm_dataportal_search_form_prepare(
420
    'cdm_dataportal/search/results/taxon',
421
    CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT,
422
    $query_field_default_value,
423
    t("Enter the text you wish to search for. The asterisk character * can be
424
        used as wildcard, but must not be used as first character. Terms can be combined with 'AND'. To search for a
425
        full phrase enclose the terms in parentheses. For more syntactical
426
        options please refer to the !link.",
427
      array(
428
        '!link' => l(
429
          t('Apache Lucene - Query Parser Syntax'),
430
          'http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html', array(
431
            'attributes' => array(
432
              'absolute' => TRUE,
433
              'html' => TRUE),
434
          )
435
        ),
436
      )
437
    ),
438
    NULL
439
  );
440

    
441
  $form['search']['tree'] = array(
442
    '#weight' => -1,
443
    '#type' => 'hidden',
444
    '#value' => get_current_classification_uuid(),
445
  );
446

    
447
  $form['search']['hl'] = array(
448
    '#weight' => -1,
449
    '#type' => 'hidden',
450
    '#value' => 1,
451
  );
452

    
453
  // Only available to admins:
454
  if (!isset($_SESSION['cdm']['search']['clazz'])) {
455
    $_SESSION['cdm']['search']['clazz'] = '';
456
  }
457
  if (module_exists("user") && user_access('administer')) {
458
    $form['search']['clazz'] = array(
459
      '#type' => 'select',
460
      '#title' => t('Limit to description item type'),
461
      '#default_value' => $_SESSION['cdm']['search']['clazz'],
462
      '#options' => cdm_descriptionElementTypes_as_option(TRUE),
463
    );
464
  }
465

    
466
  $profile_feature_tree = get_profile_feature_tree();
467
  $feature_options = _featureTree_nodes_as_feature_options($profile_feature_tree->root);
468
  if (isset($_SESSION['cdm']['search']['features'])) {
469
    $form['search']['features'] = array(
470
      '#type' => 'checkboxes',
471
      '#title' => t('Limit to selected features'),
472
      '#default_value' => $_SESSION['cdm']['search']['features'],
473
      '#options' => $feature_options,
474
    );
475
  }
476
  else {
477
    $form['search']['features'] = array(
478
      '#type' => 'checkboxes',
479
      '#title' => t('Limit to selected features'),
480
      '#options' => $feature_options,
481
    );
482
  }
483
  return $form;
484
}
485

    
486
/**
487
 * Processes the query parameters of the search form.
488
 *
489
 * Reads the query parameters from $_REQUEST and modifies and adds additional
490
 * query parameters if nessecary.
491
 *
492
 *  - Filters $_REQUEST by a list of valid request parameters
493
 *  - modifies geographic_range parameters
494
 *  - adds taxon tree uuid if it is missing and if it should not be
495
 *    ignored (parameter value = 'IGNORE')
496
 *  - and more
497
 *
498
 *
499
 * @return array
500
 *   the processed request parameters submitted by the search form and
501
 *   also stores them in $_SESSION['cdm']['search']
502
 */
503
function cdm_dataportal_search_form_request()
504
{
505

    
506
  $form_params = array();
507

    
508
  if (isset($_REQUEST['search']) && is_array($_REQUEST['search'])) {
509
    array_deep_copy($_REQUEST['search'], $form_params);
510
  }
511

    
512
  if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
513
    $form_params = array_merge($form_params, $_REQUEST['pager']);
514
  }
515

    
516
  $form_params['query'] = trim($_REQUEST['query']);
517

    
518
  // --- handle geographic range
519
  // Split of geographic range.
520
  unset($form_params['areas']);
521

    
522
  $area_filter_preset = null;
523
  if (variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '')) {
524
    $area_filter_preset = explode(',', variable_get(CDM_SEARCH_AREA_FILTER_PRESET, ''));
525
  }
526

    
527
  $area_uuids = array();
528
  if($area_filter_preset){
529
    $area_uuids = $area_filter_preset;
530
  }
531
  elseif (isset($_REQUEST['search']['areas']['area']) && is_array($_REQUEST['search']['areas']['area'])) {
532
    foreach ($_REQUEST['search']['areas']['area'] as $areas) {
533
      $area_uuids = array_merge($area_uuids, $areas);
534
    }
535
  }
536
  if(count($area_uuids) > 0){
537
    $form_params['area'] = implode(',', $area_uuids);
538
  }
539

    
540
  // Simple search will not submit a 'tree' query parameter,
541
  // so we add it here from what is stored in the session unless
542
  // SIMPLE_SEARCH_IGNORE_CLASSIFICATION is checked in the settings.
543
  if (!isset($form_params['tree']) && !variable_get(SIMPLE_SEARCH_IGNORE_CLASSIFICATION, 0)) {
544
    $form_params['tree'] = get_current_classification_uuid();
545
  }
546
  // If the 'NONE' classification has been chosen (adanced search)
547
  // delete the tree information to avoid unknown uuid exceptions in the
548
  // cdm service.
549
  if (isset($form_params['tree'])
550
    && ($form_params['tree'] == 'NONE' || !is_uuid($form_params['tree']))
551
  ) {
552
    // $form_params['ignore_classification'] =  TRUE;
553
    unset($form_params['tree']);
554
  }
555
  // else {
556
  //   $form_params['ignore_classification'] =  NULL;
557
  // }
558

    
559
  // Store in session.
560
  $_SESSION['cdm']['search'] = $form_params;
561

    
562
  return $form_params;
563
}
564

    
565
/**
566
 * Provides the classification to which the last search has been limited to..
567
 *
568
 * This function should only be used after the cdm_dataportal_search_execute()
569
 * handler has been run, otherwise it will return the infomation from the last
570
 * search executed. The information is retrieved from
571
 * the $_SESSION variable:  $_SESSION['cdm']['search']['tree']
572
 *
573
 * @return object
574
 *   the CDM classification instance which has been used a filter for the
575
 *   last processed search
576
 *   or NULL, it it was on all classifications
577
 */
578
function cdm_dataportal_searched_in_classification() {
579

    
580
  $classification = &drupal_static(__FUNCTION__);
581

    
582
  if (!isset($classification)) {
583
    if (isset($_SESSION['cdm']['search']['tree'])) {
584
      $classification = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY, ($_SESSION['cdm']['search']['tree']));
585
    }
586
    else {
587
      $classification = FALSE;
588
    }
589
  }
590

    
591
  return $classification !== FALSE ? $classification : NULL;
592
}
593

    
594
/**
595
 * Removes Drupal internal form elements from query.
596
 */
597
function cdm_dataportal_search_process($form, &$form_state) {
598
  unset($form['form_id']);
599
  unset($form['form_token']);
600
  return $form;
601
}
602

    
603
/**
604
 * Sends a search request at the cdm web server.
605
 *
606
 * The parameters to build the query are taken obtained by calling
607
 * cdm_dataportal_search_form_request() which reads the query parameters
608
 * from $_REQUEST and add additional query parameters if nessecary.
609
 *
610
 * @see cdm_dataportal_search_form_request()
611
 */
612
function cdm_dataportal_search_execute() {
613

    
614
  // Store as last search in session.
615
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
616

    
617
  // Validate the search webservice parameter:
618
  if (!isset($_REQUEST['ws'])) {
619
    drupal_set_message(
620
      t("Invalid search webservice parameter 'ws' given"), 'warning'
621
    );
622
    return NULL;
623
  }
624
  if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {
625
    // Endpoint is unknown.
626
    drupal_set_message(
627
      t("Invalid search webservice parameter 'ws' given"), 'warning'
628
    );
629
    return NULL;
630
  }
631

    
632
  // Read the query parameters from $_REQUEST and add additional query
633
  // parameters if necessary.
634
  $request_params = cdm_dataportal_search_form_request();
635

    
636
  $taxon_pager = cdm_ws_get($_REQUEST['ws'], NULL, queryString($request_params));
637

    
638
  return $taxon_pager;
639
}
640

    
641
/**
642
 * Transforms the termDTO tree into options array.
643
 *
644
 *   TermDto:
645
 *      - partOfUuid:
646
 *      - representation_L10n:
647
 *      - representation_L10n_abbreviatedLabel:
648
 *      - uuid:
649
 *      - vocabularyUuid:
650
 *      - children: array of TermDto
651
 *
652
 * The options array is suitable for drupal form API elements that
653
 * allow multiple choices.
654
 * @see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#options
655
 *
656
 * @param array $term_dto_tree
657
 *   a hierarchic array of CDM TermDto instances, with additional
658
 * 'children' field:
659
 * @param array $options
660
 *   Internally used for recursive calls
661
 * @param string $prefix
662
 *   Internally used for recursive calls
663
 *
664
 * @return array
665
 *   the terms in an array as options for a form element that allows
666
 *   multiple choices.
667
 */
668
function term_tree_as_options($term_dto_tree, &$options = array(), $prefix = '') {
669

    
670
  foreach ($term_dto_tree as $uuid => $dto) {
671
    $label = $prefix . '<span class="child-label">'
672
      .  $dto->representation_L10n
673
      . '</span><span class="child-label-abbreviated"> (' . $dto->representation_L10n_abbreviatedLabel . ')</span>';
674
    $options[$uuid] = $label;
675
    if (isset($dto->children) && is_array($dto->children)) {
676
      uasort($dto->children, 'compare_terms_by_representationL10n');
677
      term_tree_as_options(
678
        $dto->children,
679
        $options, $prefix
680
          . '<span data-cdm-parent="' . $uuid . '" class="parent"></span>');
681
    }
682
  }
683

    
684
  return $options;
685
}
(11-11/16)