Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / modules / cdm_dataportal / cdm_dataportal.search.php @ 103a1463

History | View | Annotate | Download (22.5 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_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 necessary fields
36
 * for internal processing and has the textfield element $form['query']
37
 * which holds the query term.
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
 *   A default text for the query field
47
 * @param string $query_field_description
48
 *   The description text for the query field
49
 * @param string $process
50
 *   The value for #process, if NULL (default), 'cdm_dataportal_search_process'
51
 *   is used.
52
 *
53
 * @return array
54
 *   The prepared form array.
55
 */
56
function cdm_dataportal_search_form_prepare($action_path, $search_webservice, $query_field_default_value, $query_field_description, $process = NULL) {
57

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

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

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

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

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

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

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

    
120
  return $form;
121
}
122

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

    
126
  $queryParams = array();
127
  $queryParams['query'] = $string.'*';
128
  if((is_null($classificationUuid) || $classificationUuid=='') && isset($_SESSION['cdm']['taxonomictree_uuid'])){
129
    $classificationUuid = $_SESSION['cdm']['taxonomictree_uuid'];// if no classification uuid is set use the current one
130
  }
131
  if($classificationUuid){
132
    $queryParams['classificationUuid'] = $classificationUuid;
133
  }
134
  if($areaUuid){
135
    $queryParams['area'] = $areaUuid;
136
  }
137
  if($status){
138
    $queryParams['status'] = $status ;
139
  }
140
  $queryParams['pageNumber'] = '0';
141
  $queryParams['pageSize'] = '10';
142
  $queryParams['doTaxa'] = true;
143
  $queryParams['doSynonyms'] = true;
144
  $queryParams['doMisappliedNames'] = true;
145
  $queryParams['doTaxaByCommonNames'] = true;
146

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

    
157

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

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

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

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

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

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

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

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

    
221
  if ($advanced_form) {
222

    
223
    // --- ADVANCED SEARCH FORM ---
224
    //
225

    
226
    // Get presets from settings.
227
    $preset_classification_uuid = get_current_classification_uuid();
228

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

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

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

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

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

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

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

    
318
    $show_area_filter = ! variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '');
319

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

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

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

    
361
  }
362
  else {
363
    // --- SIMPLE SEARCH FORM ---
364
    //
365

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

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

    
394
  return $form;
395
}
396

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

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

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

    
445
  $form['search']['tree'] = array(
446
    '#weight' => -1,
447
    '#type' => 'hidden',
448
    '#value' => get_current_classification_uuid(),
449
  );
450

    
451
  $form['search']['hl'] = array(
452
    '#weight' => -1,
453
    '#type' => 'hidden',
454
    '#value' => 1,
455
  );
456

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

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

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

    
510
  $form_params = array();
511

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

    
516
  if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
517
    $form_params = array_merge($form_params, $_REQUEST['pager']);
518
  }
519

    
520
  $form_params['query'] = trim($_REQUEST['query']);
521

    
522
  // --- handle geographic range
523
  // Split of geographic range.
524
  unset($form_params['areas']);
525

    
526
  $area_filter_preset = null;
527
  if (variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '')) {
528
    $area_filter_preset = explode(',', variable_get(CDM_SEARCH_AREA_FILTER_PRESET, ''));
529
  }
530

    
531
  $area_uuids = array();
532
  if($area_filter_preset){
533
    $area_uuids = $area_filter_preset;
534
  }
535
  elseif (isset($_REQUEST['search']['areas']['area']) && is_array($_REQUEST['search']['areas']['area'])) {
536
    foreach ($_REQUEST['search']['areas']['area'] as $areas) {
537
      $area_uuids = array_merge($area_uuids, $areas);
538
    }
539
    // The area filter is limited to areas with non absent distribution status
540
    $presence_terms_options = cdm_vocabulary_as_option(UUID_PRESENCE_ABSENCE_TERM, null, FALSE, array('absenceTerm' => '/false/'));
541
    $presence_term_uuids = array_keys($presence_terms_options);
542
    $form_params['status'] = $presence_term_uuids;
543
  }
544
  if(count($area_uuids) > 0){
545
    $form_params['area'] = implode(',', $area_uuids);
546
  }
547

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

    
567
  // Store in session.
568
  $_SESSION['cdm']['search'] = $form_params;
569

    
570
  return $form_params;
571
}
572

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

    
588
  $classification = &drupal_static(__FUNCTION__);
589

    
590
  if (!isset($classification)) {
591
    if (isset($_SESSION['cdm']['search']['tree'])) {
592
      $classification = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY, ($_SESSION['cdm']['search']['tree']));
593
    }
594
    else {
595
      $classification = FALSE;
596
    }
597
  }
598

    
599
  return $classification !== FALSE ? $classification : NULL;
600
}
601

    
602
/**
603
 * Removes Drupal internal form elements from query.
604
 */
605
function cdm_dataportal_search_process($form, &$form_state) {
606
  unset($form['form_id']);
607
  unset($form['form_token']);
608
  return $form;
609
}
610

    
611
/**
612
 * Sends a search request to the cdm server.
613
 *
614
 * The parameters to build the query are taken obtained by calling
615
 * cdm_dataportal_search_request() which reads the query parameters
616
 * from $_REQUEST and add additional query parameters if nessecary.
617
 *
618
 * @see cdm_dataportal_search_request()
619
 */
620
function cdm_dataportal_search_execute() {
621

    
622
  // Store as last search in session.
623
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
624

    
625
  // Validate the search webservice parameter:
626
  if (!isset($_REQUEST['ws'])) {
627
    drupal_set_message(
628
      t("Invalid search, webservice parameter 'ws' is missing"), 'warning'
629
    );
630
    return NULL;
631
  }
632
  if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {
633
    // Endpoint is unknown.
634
    drupal_set_message(
635
      t("Invalid search webservice parameter 'ws' given"), 'warning'
636
    );
637
    return NULL;
638
  }
639

    
640
  // Read the query parameters from $_REQUEST and add additional query
641
  // parameters if necessary.
642
  $request_params = cdm_dataportal_search_request();
643

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

    
646
  return $taxon_pager;
647
}
648

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

    
678
  uasort($term_dto_tree, 'compare_terms_by_order_index');
679
  foreach ($term_dto_tree as $uuid => $dto) {
680
    $label = $prefix . '<span class="child-label">'
681
      .  $dto->representation_L10n
682
      . '</span><span class="child-label-abbreviated"> (' . $dto->representation_L10n_abbreviatedLabel . ')</span>';
683
    $options[$uuid] = $label;
684
    if (isset($dto->children) && is_array($dto->children)) {
685
      term_tree_as_options(
686
        $dto->children,
687
        $options, $prefix
688
          . '<span data-cdm-parent="' . $uuid . '" class="parent"></span>'
689
      );
690
    }
691
  }
692

    
693
  return $options;
694
}
Add picture from clipboard (Maximum size: 40 MB)