Project

General

Profile

Download (21.5 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
    '#autocomplete_path' => 'cdm_dataportal/taxonSearch/autocomplete'
94
  );
95

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

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

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

    
119
  return $form;
120
}
121

    
122
function cdm_dataportal_taxaon_search_autocomplete($string) {
123
  $matches = array();
124

    
125
  $queryParams = array();
126
  $queryParams['query'] = $string."*";
127
  $queryParams['pageNumber'] = '0';
128
  $queryParams['pageSize'] = '100';
129
  $queryParams['doTaxa'] = true;
130
  $queryParams['doSynonyms'] = true;
131
  $queryParams['doMisappliedNames'] = true;
132
  $queryParams['doTaxaByCommonNames'] = true;
133

    
134
  $search_results = cdm_ws_get(CDM_WS_TAXON_SEARCH, NULL, queryString($queryParams));
135
  foreach($search_results->records as $record){
136
      $titleCache = $record->entity->titleCache;
137
      preg_match('/(.*) sec.*/', $titleCache, $trimmedTitle);//remove sec reference
138
      $trimmedTitle = trim($trimmedTitle[1]);
139
      $matches[$trimmedTitle] = $trimmedTitle;
140
  }
141
  drupal_json_output($matches);
142
}
143

    
144

    
145
  /**
146
 * Creates a search form for searching on taxa.
147
 *
148
 * If advanced $advanced_form id TRUE the form will offer additional choices
149
 *
150
 * @param array $form
151
 *   A drupal form array
152
 * @param array $form_state
153
 *   The drupal form state passed as reference
154
 * @param bool $advanced_form
155
 *   default is FALSE
156
 * @param bool $classification_select
157
 *   set TRUE to offer a classifiaction selector in the form - default is FALSE
158
 *   if only available in the advanced mode
159
 *
160
 * @return array
161
 *   the form array
162
 */
163
function cdm_dataportal_search_taxon_form($form, &$form_state, $advanced_form = FALSE, $classification_select = TRUE) {
164

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

    
167
  if ($advanced_form || variable_get(SIMPLE_SEARCH_USE_LUCENE_BACKEND, FALSE)) {
168
    $search_service_endpoint = CDM_WS_PORTAL_TAXON_SEARCH;
169
  }
170
  else {
171
    $search_service_endpoint = CDM_WS_PORTAL_TAXON_FIND;
172
  }
173

    
174
  $form = cdm_dataportal_search_form_prepare(
175
    'cdm_dataportal/search/results/taxon',
176
    $search_service_endpoint,
177
    $query_field_default_value,
178
    t('Enter the name or part of a name you wish to search for.
179
      The asterisk  character * can be used as wildcard, but must not be used as first character.'),
180
      NULL
181
  );
182

    
183
  if (!$advanced_form) {
184
    $form['query']['#size'] = 20;
185
  }
186

    
187
  $form['search']['pageSize'] = array(
188
    '#weight' => -1,
189
    '#type' => 'hidden',
190
    '#value' => variable_get('cdm_dataportal_search_items_on_page', 25),
191
  );
192

    
193
  $form['search']['pageNumber'] = array(
194
    '#weight' => -1,
195
    '#type' => 'hidden',
196
    '#value' => 0,
197
  );
198

    
199
  $search_taxa_mode_settings = get_array_variable_merged(
200
    CDM_SEARCH_TAXA_MODE,
201
    CDM_SEARCH_TAXA_MODE_DEFAULT
202
  );
203
  $preset_do_taxa = $search_taxa_mode_settings['doTaxa'] !== 0;
204
  $preset_do_synonyms = $search_taxa_mode_settings['doSynonyms'] !== 0;
205
  $preset_do_taxa_by_common_names = $search_taxa_mode_settings['doTaxaByCommonNames'] !== 0;
206
  $preset_do_misapplied_names = $search_taxa_mode_settings['doMisappliedNames'] !== 0;
207

    
208
  if ($advanced_form) {
209

    
210
    // --- ADVANCED SEARCH FORM ---
211
    //
212

    
213
    // Get presets from settings.
214
    $preset_classification_uuid = get_current_classification_uuid();
215

    
216
    // Overwrite presets by user choice stored in session.
217
    if (isset($_SESSION['cdm']['search'])) {
218
      $preset_do_taxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
219
      $preset_do_synonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
220
      $preset_do_misapplied_names = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
221
      $preset_do_taxa_by_common_names = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
222
      if (isset($_SESSION['cdm']['search']['tree'])) {
223
        $preset_classification_uuid = $_SESSION['cdm']['search']['tree'];
224
      }
225
    }
226

    
227
    if ($classification_select === TRUE) {
228
      $form['search']['tree'] = array(
229
        '#title' => t('Classification'),
230
        '#weight' => 1,
231
        '#type' => 'select',
232
        '#default_value' => get_current_classification_uuid(),
233
        '#options' => cdm_get_taxontrees_as_options(TRUE),
234
        '#description' => t('A filter to limit the search to a specific classification. Choosing <em>--- ALL ---</em> will disable this filter.'),
235
      );
236
    }
237

    
238
    // General search parameters.
239
    $form['search']['doTaxa'] = array(
240
      '#weight' => 2,
241
      '#type' => 'checkbox',
242
      '#title' => t('Search for') . ' ' . t('accepted taxa'),
243
      '#value' => $preset_do_taxa,
244
    );
245
    $form['search']['doSynonyms'] = array(
246
      '#weight' => 3,
247
      '#type' => 'checkbox',
248
      '#title' => t('Search for') . ' ' . t('synonyms'),
249
      '#value' => $preset_do_synonyms,
250
    );
251
    $form['search']['doMisappliedNames'] = array(
252
      '#weight' => 4,
253
      '#type' => 'checkbox',
254
      '#title' => t('Search for') . ' ' . t('misapplied names'),
255
      '#value' => $preset_do_misapplied_names,
256
    );
257
    $form['search']['doTaxaByCommonNames'] = array(
258
      '#weight' => 5,
259
      '#type' => 'checkbox',
260
      '#title' => t('Search for') . ' ' . t('common names'),
261
      '#value' => $preset_do_taxa_by_common_names,
262
    );
263

    
264
    $area_term_dtos = cdm_ws_fetch_all(
265
      CDM_WS_DESCRIPTION_NAMEDAREAS_IN_USE,
266
      array('includeAllParents' => 'true')
267
    );
268

    
269
    // create map: term_uuid => term
270
    $term_map = array();
271
    foreach ($area_term_dtos as $term_dto) {
272
      $term_map[$term_dto->uuid] = $term_dto;
273
    }
274

    
275
    $term_tree = array();
276
    // mixed_vocabularies will contain the uuid vocabularies which
277
    // also contain terms of foreign vocabularies due to the term
278
    // hierarchy
279
    $mixed_vocabularies = array();
280

    
281
    // Build hierarchy of the terms regardless of the vocabulary.
282
    foreach ($term_map as $term_dto) {
283
      if (!empty($term_dto->partOfUuid)) {
284
        // Children.
285
        $parent =& $term_map[$term_dto->partOfUuid];
286
        if ($parent) {
287
          if (!isset($parent->children)) {
288
            $parent->children = array();
289
          }
290
          $parent->children[$term_dto->uuid] = $term_dto;
291
          if ($parent->vocabularyUuid != $term_dto->vocabularyUuid) {
292
            $mixed_vocabularies[$parent->vocabularyUuid] = $parent->vocabularyUuid;
293
          }
294
        }
295
      }
296
      else {
297
        // group root nodes by vocabulary
298
        if (!isset($term_tree[$term_dto->vocabularyUuid])) {
299
          $term_tree[$term_dto->vocabularyUuid] = array();
300
        }
301
        $term_tree[$term_dto->vocabularyUuid][$term_dto->uuid] = $term_dto;
302
      }
303
    }
304

    
305
    $show_area_filter = ! variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '');
306

    
307
    if($show_area_filter){
308
      drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/search_area_filter.js');
309

    
310
      drupal_add_js('jQuery(document).ready(function() {
311
        jQuery(\'#edit-search-areas\').search_area_filter(\'#edit-search-areas-areas-filter\');
312
      });
313
      ', array('type' => 'inline'));
314

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

    
346
  }
347
  else {
348
    // --- SIMPLE SEARCH FORM ---
349
    //
350

    
351
    // Overwrite presets by user choice stored in session.
352
    if (isset($_SESSION['cdm']['search'])) {
353
      $preset_do_misapplied_names = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
354
    }
355

    
356
    $form['search']['doTaxa'] = array(
357
      '#weight' => -2,
358
      '#type' => 'hidden',
359
      '#value' => $preset_do_taxa,
360
    );
361
    $form['search']['doSynonyms'] = array(
362
      '#weight' => -3,
363
      '#type' => 'hidden',
364
      '#value' => $preset_do_synonyms,
365
    );
366
    $form['search']['doMisappliedNames'] = array(
367
      '#weight' => -4,
368
      '#type' => 'checkbox',
369
      '#title' => t('Misapplied names'),
370
      '#value' => $preset_do_misapplied_names,
371
    );
372
    $form['search']['doTaxaByCommonNames'] = array(
373
      '#weight' => -5,
374
      '#type' => 'hidden',
375
      '#value' => $preset_do_taxa_by_common_names,
376
    );
377
  }
378

    
379
  return $form;
380
}
381

    
382
/**
383
 * Wrapper function for cdm_dataportal_search_taxon_form().
384
 *
385
 * This function makes ot possible possible to just pass the
386
 * correct $form_id 'cdm_dataportal_search_taxon_form_advanced' to
387
 * drupal_get_form like:
388
 * drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
389
 *
390
 * @param array $form
391
 *   A drupal form array
392
 * @param array $form_state
393
 *   The drupal form state passed as reference
394
 *
395
 * @return array
396
 *   The form array
397
 */
398
function cdm_dataportal_search_taxon_form_advanced($form, &$form_state) {
399
  return cdm_dataportal_search_taxon_form($form, $form_state, TRUE);
400
}
401

    
402
/**
403
 * Form for searching taxa by the findByDescriptionElementFullText rest service.
404
 */
405
function cdm_dataportal_search_taxon_by_description_form() {
406
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
407

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

    
430
  $form['search']['tree'] = array(
431
    '#weight' => -1,
432
    '#type' => 'hidden',
433
    '#value' => get_current_classification_uuid(),
434
  );
435

    
436
  $form['search']['hl'] = array(
437
    '#weight' => -1,
438
    '#type' => 'hidden',
439
    '#value' => 1,
440
  );
441

    
442
  // Only available to admins:
443
  if (!isset($_SESSION['cdm']['search']['clazz'])) {
444
    $_SESSION['cdm']['search']['clazz'] = '';
445
  }
446
  if (module_exists("user") && user_access('administer')) {
447
    $form['search']['clazz'] = array(
448
      '#type' => 'select',
449
      '#title' => t('Limit to description item type'),
450
      '#default_value' => $_SESSION['cdm']['search']['clazz'],
451
      '#options' => cdm_descriptionElementTypes_as_option(TRUE),
452
    );
453
  }
454

    
455
  $profile_feature_tree = get_profile_feature_tree();
456
  $feature_options = _featureTree_nodes_as_feature_options($profile_feature_tree->root);
457
  if (isset($_SESSION['cdm']['search']['features'])) {
458
    $form['search']['features'] = array(
459
      '#type' => 'checkboxes',
460
      '#title' => t('Limit to selected features'),
461
      '#default_value' => $_SESSION['cdm']['search']['features'],
462
      '#options' => $feature_options,
463
    );
464
  }
465
  else {
466
    $form['search']['features'] = array(
467
      '#type' => 'checkboxes',
468
      '#title' => t('Limit to selected features'),
469
      '#options' => $feature_options,
470
    );
471
  }
472
  return $form;
473
}
474

    
475
/**
476
 * Processes the query parameters of the search form.
477
 *
478
 * Reads the query parameters from $_REQUEST and modifies and adds additional
479
 * query parameters if nessecary.
480
 *
481
 *  - Filters $_REQUEST by a list of valid request parameters
482
 *  - modifies geographic_range parameters
483
 *  - adds taxon tree uuid if it is missing and if it should not be
484
 *    ignored (parameter value = 'IGNORE')
485
 *  - and more
486
 *
487
 *
488
 * @return array
489
 *   the processed request parameters submitted by the search form and
490
 *   also stores them in $_SESSION['cdm']['search']
491
 */
492
function cdm_dataportal_search_form_request()
493
{
494

    
495
  $form_params = array();
496

    
497
  if (isset($_REQUEST['search']) && is_array($_REQUEST['search'])) {
498
    array_deep_copy($_REQUEST['search'], $form_params);
499
  }
500

    
501
  if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
502
    $form_params = array_merge($form_params, $_REQUEST['pager']);
503
  }
504

    
505
  $form_params['query'] = trim($_REQUEST['query']);
506

    
507
  // --- handle geographic range
508
  // Split of geographic range.
509
  unset($form_params['areas']);
510

    
511
  if (variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '')) {
512
    $area_filter_preset = explode(',', variable_get(CDM_SEARCH_AREA_FILTER_PRESET, ''));
513
  }
514

    
515
  if($area_filter_preset){
516
    $area_uuids = $area_filter_preset;
517
  }
518
  elseif (isset($_REQUEST['search']['areas']['area']) && is_array($_REQUEST['search']['areas']['area'])) {
519
    $area_uuids = array();
520
    foreach ($_REQUEST['search']['areas']['area'] as $areas) {
521
      $area_uuids = array_merge($area_uuids, $areas);
522
    }
523
  }
524
  if(isset($area_uuids[0])){
525
    $form_params['area'] = implode(',', $area_uuids);
526
  }
527

    
528
  // Simple search will not submit a 'tree' query parameter,
529
  // so we add it here from what is stored in the session unless
530
  // SIMPLE_SEARCH_IGNORE_CLASSIFICATION is checked in the settings.
531
  if (!isset($form_params['tree']) && !variable_get(SIMPLE_SEARCH_IGNORE_CLASSIFICATION, 0)) {
532
    $form_params['tree'] = get_current_classification_uuid();
533
  }
534
  // If the 'NONE' classification has been chosen (adanced search)
535
  // delete the tree information to avoid unknown uuid exceptions in the
536
  // cdm service.
537
  if (isset($form_params['tree'])
538
    && ($form_params['tree'] == 'NONE' || !is_uuid($form_params['tree']))
539
  ) {
540
    // $form_params['ignore_classification'] =  TRUE;
541
    unset($form_params['tree']);
542
  }
543
  // else {
544
  //   $form_params['ignore_classification'] =  NULL;
545
  // }
546

    
547
  // Store in session.
548
  $_SESSION['cdm']['search'] = $form_params;
549

    
550
  return $form_params;
551
}
552

    
553
/**
554
 * Provides the classification to which the last search has been limited to..
555
 *
556
 * This function should only be used after the cdm_dataportal_search_execute()
557
 * handler has been run, otherwise it will return the infomation from the last
558
 * search executed. The information is retrieved from
559
 * the $_SESSION variable:  $_SESSION['cdm']['search']['tree']
560
 *
561
 * @return object
562
 *   the CDM classification instance which has been used a filter for the
563
 *   last processed search
564
 *   or NULL, it it was on all classifications
565
 */
566
function cdm_dataportal_searched_in_classification() {
567

    
568
  $classification = &drupal_static(__FUNCTION__);
569

    
570
  if (!isset($classification)) {
571
    if (isset($_SESSION['cdm']['search']['tree'])) {
572
      $classification = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY, ($_SESSION['cdm']['search']['tree']));
573
    }
574
    else {
575
      $classification = FALSE;
576
    }
577
  }
578

    
579
  return $classification !== FALSE ? $classification : NULL;
580
}
581

    
582
/**
583
 * Removes Drupal internal form elements from query.
584
 */
585
function cdm_dataportal_search_process($form, &$form_state) {
586
  unset($form['form_id']);
587
  unset($form['form_token']);
588
  return $form;
589
}
590

    
591
/**
592
 * Sends a search request at the cdm web server.
593
 *
594
 * The parameters to build the query are taken obtained by calling
595
 * cdm_dataportal_search_form_request() which reads the query parameters
596
 * from $_REQUEST and add additional query parameters if nessecary.
597
 *
598
 * @see cdm_dataportal_search_form_request()
599
 */
600
function cdm_dataportal_search_execute() {
601

    
602
  // Store as last search in session.
603
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
604

    
605
  // Validate the search webservice parameter:
606
  if (!isset($_REQUEST['ws'])) {
607
    drupal_set_message(
608
      t("Invalid search webservice parameter 'ws' given"), 'warning'
609
    );
610
    return NULL;
611
  }
612
  if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {
613
    // Endpoint is unknown.
614
    drupal_set_message(
615
      t("Invalid search webservice parameter 'ws' given"), 'warning'
616
    );
617
    return NULL;
618
  }
619

    
620
  // Read the query parameters from $_REQUEST and add additional query
621
  // parameters if necessary.
622
  $request_params = cdm_dataportal_search_form_request();
623

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

    
626
  return $taxon_pager;
627
}
628

    
629
/**
630
 * Transforms the termDTO tree into options array.
631
 *
632
 *   TermDto:
633
 *      - partOfUuid:
634
 *      - representation_L10n:
635
 *      - representation_L10n_abbreviatedLabel:
636
 *      - uuid:
637
 *      - vocabularyUuid:
638
 *      - children: array of TermDto
639
 *
640
 * The options array is suitable for drupal form API elements that
641
 * allow multiple choices.
642
 * @see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#options
643
 *
644
 * @param array $term_dto_tree
645
 *   a hierarchic array of CDM TermDto instances, with additional
646
 * 'children' field:
647
 * @param array $options
648
 *   Internally used for recursive calls
649
 * @param string $prefix
650
 *   Internally used for recursive calls
651
 *
652
 * @return array
653
 *   the terms in an array as options for a form element that allows
654
 *   multiple choices.
655
 */
656
function term_tree_as_options($term_dto_tree, &$options = array(), $prefix = '') {
657

    
658
  foreach ($term_dto_tree as $uuid => $dto) {
659
    $label = $prefix . '<span class="child-label">'
660
      .  $dto->representation_L10n
661
      . '</span><span class="child-label-abbreviated"> (' . $dto->representation_L10n_abbreviatedLabel . ')</span>';
662
    $options[$uuid] = $label;
663
    if (isset($dto->children) && is_array($dto->children)) {
664
      uasort($dto->children, 'compare_terms_by_representationL10n');
665
      term_tree_as_options(
666
        $dto->children,
667
        $options, $prefix
668
          . '<span data-cdm-parent="' . $uuid . '" class="parent"></span>');
669
    }
670
  }
671

    
672
  return $options;
673
}
(11-11/16)