Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / modules / cdm_dataportal / cdm_dataportal.search.php @ 6eaec849

History | View | Annotate | Download (39.8 KB)

1
<?php
2
/**
3
 * @file
4
 * Search related functions.
5
 */
6

    
7
define("SESSION_KEY_SEARCH_REGISTRATION_FILTER", "SESSION_KEY_SEARCH_REGISTRATION_FILTER");
8
define('SESSION_KEY_SEARCH_TAXONGRAPH_FOR_REGISTRATION_FILTER', 'SESSION_KEY_SEARCH_TAXONGRAPH_FOR_REGISTRATION_FILTER');
9

    
10
/**
11
 * Returns a Drupal path to a search form for a CDM webservice.
12
 *
13
 * For a given CDM webservice end-point, the drupal page path to the
14
 * according search form is returned.
15
 * cdm webservice end points are defined in constant variables like:
16
 * <code>CDM_WS_PORTAL_TAXON_FIND</code> and
17
 * <code>CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT</code>
18
 *
19
 * @param string $ws_endpoint
20
 *   The cdm webservice endpoint for which to find the search form path.
21
 *
22
 * @return string
23
 *   The Drupal path found.
24
 */
25
function cdm_dataportal_search_form_path_for_ws($ws_endpoint) {
26
  static $form_ws_map = array(
27
    CDM_WS_PORTAL_TAXON_FIND => "cdm_dataportal/search",
28
    CDM_WS_PORTAL_TAXON_SEARCH => "cdm_dataportal/search",
29
    CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT => "cdm_dataportal/search/taxon_by_description",
30
  );
31
  return $form_ws_map[$ws_endpoint];
32
}
33

    
34
/**
35
 * Prepares a form array for a general purpose search form.
36
 *
37
 * The form is used for general purpose search functionality in the
38
 * dataportal. The form returned is populated with all necessary fields
39
 * for internal processing and has the textfield element $form['query']
40
 * which holds the query term.
41
 *
42
 * @param string $action_path
43
 *   The Drupal path to be put into the action url to which the form will
44
 *   be submitted.
45
 * @param string $search_webservice
46
 *   The cdm-remote webservice to be used, valid values are defined by
47
 *   the constants: FIXME.
48
 * @param string $query_field_default_value
49
 *   A default text for the query field
50
 * @param string $query_field_description
51
 *   The description text for the query field
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) {
57

    
58

    
59
  $form['#method'] = 'get';
60
  $form['#action'] = url($action_path, array(
61
    'absolute' => TRUE,
62
  ));
63

    
64
  $form['ws'] = array(
65
    '#type' => 'hidden',
66
    '#value' => $search_webservice,
67
    '#name' => 'ws',
68
  );
69

    
70
  $form['query'] = array(
71
    '#weight' => 0,
72
    '#type' => 'textfield',
73
    '#size' => 68,
74
    // This causes the description to display also when hovering over
75
    // the textfield.
76
    // This is wanted behaviour for the simple seach but could
77
    // be disabled for the advances search.
78
    '#attributes' => array(
79
      'title' => $query_field_description,
80
    ),
81
    '#description' => $query_field_description,
82
    '#value' => $query_field_default_value,
83
    // '#description' => $query_field_description,
84
  );
85
  if(variable_get(SIMPLE_SEARCH_AUTO_SUGGEST)){
86
      $form['query']['#autocomplete_path'] = 'cdm_dataportal/taxon/autosuggest////';
87
  }
88

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

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

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

    
112
  return $form;
113
}
114

    
115
function cdm_dataportal_taxon_autosuggest($classificationUuid = NULL, $areaUuid = NULL, $status = NULL, $string) {
116
  $matches = array();
117

    
118
  $queryParams = array();
119
  $queryParams['query'] = $string.'*';
120
  if((is_null($classificationUuid) || $classificationUuid=='') && isset($_SESSION['cdm']['taxonomictree_uuid'])){
121
    $classificationUuid = $_SESSION['cdm']['taxonomictree_uuid'];// if no classification uuid is set use the current one
122
  }
123
  if($classificationUuid){
124
    $queryParams['classificationUuid'] = $classificationUuid;
125
  }
126
  if($areaUuid){
127
    $queryParams['area'] = $areaUuid;
128
  }
129
  if($status){
130
    $queryParams['status'] = $status ;
131
  }
132
  $queryParams['pageNumber'] = '0';
133
  $queryParams['pageSize'] = '10';
134
  $queryParams['doTaxa'] = true;
135
  $queryParams['doSynonyms'] = true;
136
  $queryParams['doMisappliedNames'] = true;
137
  $queryParams['doTaxaByCommonNames'] = true;
138

    
139
  $search_results = cdm_ws_get(CDM_WS_TAXON_SEARCH, NULL, queryString($queryParams));
140
  foreach($search_results->records as $record){
141
      $titleCache = $record->entity->titleCache;
142
      preg_match('/(.*) sec.*/', $titleCache, $trimmedTitle); //remove sec reference
143
      $trimmedTitle = trim($trimmedTitle[1]);
144
      $matches[$trimmedTitle] = check_plain($trimmedTitle);
145
  }
146
  drupal_json_output($matches);
147
}
148

    
149

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

    
171
    if ($form_state['build_info']['form_id'] == 'cdm_dataportal_search_blast_form') {
172
        $form = cdm_dataportal_search_blast_form($form, $form_state);
173
    } else {
174

    
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
        } else {
181
            $search_service_endpoint = CDM_WS_PORTAL_TAXON_FIND;
182
        }
183

    
184
        $form = cdm_dataportal_search_form_prepare(
185
            'cdm_dataportal/search/results/taxon',
186
            $search_service_endpoint,
187
            $query_field_default_value,
188
            t('Enter the name or part of a name you wish to search for.
189
          The asterisk  character * can be used as wildcard, but must not be used as first character.')
190
        );
191
    }
192
  if (!$advanced_form) {
193
    $form['query']['#size'] = 20;
194
  }
195

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

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

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

    
217
  if ($advanced_form) {
218

    
219
    // --- ADVANCED SEARCH FORM ---
220
    //
221

    
222
    // Get presets from settings.
223
    $preset_classification_uuid = get_current_classification_uuid();
224

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

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

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

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

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

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

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

    
314
    $show_area_filter = ! variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '');
315

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

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

    
324
      $form['search']['areas'] = array(
325
        '#type' => 'fieldset',
326
        '#title' => t('Filter by distribution areas'),
327
        '#description' => t('The search will return taxa having distribution
328
        information for at least one of the selected areas.') . ' '
329
          .(count($term_tree) > 1 ? t('The areas are grouped
330
        by the vocabularies to which the highest level areas belong.') : ''),
331
      );
332
      $form['search']['areas']['areas_filter'] = array(
333
        '#type' => 'textfield',
334
        '#description' => t('Type to filter the areas listed below.'),
335
      );
336
      $vocab_cnt = 0;
337
      $areas_defaults = array();
338
      if (isset($_SESSION['cdm']['search']['area'])) {
339
        $areas_defaults = explode(',', $_SESSION['cdm']['search']['area']);
340
      }
341
      _add_js_resizable_element('.resizable-box', true);
342
      foreach ($term_tree as $vocab_uuid => $term_dto_tree) {
343
        $vocabulary = cdm_ws_get(CDM_WS_TERMVOCABULARY, array($vocab_uuid));
344
        $areas_options = term_tree_as_options($term_dto_tree);
345
        $form['search']['areas']['area'][$vocab_cnt++] = array(
346
          '#prefix' => '<strong>' . $vocabulary->representation_L10n
347
            . (isset($mixed_vocabularies[$vocab_uuid]) ? ' <span title="Contains terms of at least one other area vocabulary.">(' . t('mixed') . ')</span>': '')
348
            . '</strong><div class="resizable-container"><div class="resizable-box">',
349
          '#type' => 'checkboxes',
350
          '#default_value' => $areas_defaults,
351
          '#options' => $areas_options,
352
          '#suffix' => '</div></div>'
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
 * Creates a search form for searching on taxa.
395
 *
396
 * If advanced $advanced_form id TRUE the form will offer additional choices
397
 *
398
 * @param array $form
399
 *   A drupal form array
400
 * @param array $form_state
401
 *   The drupal form state passed as reference
402
 * @param bool $advanced_form
403
 *   default is FALSE
404
 * @param bool $classification_select
405
 *   set TRUE to offer a classification selector in the form - default is FALSE
406
 *   if only available in the advanced mode
407
 *
408
 * @return array
409
 *   the form array
410
 */
411
function cdm_dataportal_search_blast_form($form, &$form_state) {
412

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

    
415
    $search_service_endpoint = CDM_WS_PORTAL_BLAST;
416

    
417

    
418
    $form = cdm_dataportal_search_blast_form_prepare(
419
        'cdm_dataportal/search/results/specimen',
420
        $search_service_endpoint,
421
        $query_field_default_value,
422
        t('Enter the sequence or part of a sequence you wish to search for.')
423
    );
424

    
425

    
426

    
427
    $form['search']['pageSize'] = array(
428
        '#weight' => -1,
429
        '#type' => 'hidden',
430
        '#value' => variable_get('cdm_dataportal_search_items_on_page', 25),
431
    );
432

    
433
    $form['search']['pageNumber'] = array(
434
        '#weight' => -1,
435
        '#type' => 'hidden',
436
        '#value' => 0,
437
    );
438

    
439

    
440

    
441

    
442

    
443
    return $form;
444
}
445

    
446
/**
447
 * Prepares a form array for a general purpose search form.
448
 *
449
 * The form is used for general purpose search functionality in the
450
 * dataportal. The form returned is populated with all necessary fields
451
 * for internal processing and has the textfield element $form['query']
452
 * which holds the query term.
453
 *
454
 * @param string $action_path
455
 *   The Drupal path to be put into the action url to which the form will
456
 *   be submitted.
457
 * @param string $search_webservice
458
 *   The cdm-remote webservice to be used, valid values are defined by
459
 *   the constants: FIXME.
460
 * @param string $query_field_default_value
461
 *   A default text for the query field
462
 * @param string $query_field_description
463
 *   The description text for the query field
464
 * @param string $process
465
 *   The value for #process, if NULL (default), 'cdm_dataportal_search_process'
466
 *   is used.
467
 *
468
 * @return array
469
 *   The prepared form array.
470
 */
471
function cdm_dataportal_search_blast_form_prepare($action_path, $search_webservice, $query_field_default_value, $query_field_description, $process = NULL) {
472

    
473
    if ($process == NULL) {
474
        $process = 'cdm_dataportal_search_process';
475
    }
476

    
477
    $form['#method'] = 'get';
478
    //
479
    //  $form['#process'] = array(
480
    //  $process => array(),
481
    //  );
482
    //
483
    $form['#action'] = url($action_path, array(
484
        'absolute' => TRUE,
485
    ));
486

    
487
    $form['ws'] = array(
488
        '#type' => 'hidden',
489
        '#value' => $search_webservice,
490
        '#name' => 'ws',
491
    );
492

    
493
    $form['query'] = array(
494
        '#weight' => 0,
495
        '#type' => 'textarea',
496
        '#size' => 68,
497
        // This causes the description to display also when hovering over
498
        // the textfield.
499
        // This is wanted behaviour for the simple seach but could
500
        // be disabled for the advances search.
501
        '#attributes' => array(
502
            'title' => $query_field_description,
503
        ),
504
        '#description' => $query_field_description,
505
       // '#value' => $query_field_default_value,
506
        // '#description' => $query_field_description,
507
    );
508

    
509

    
510
    $form['search'] = array(
511
        '#weight' => 3,
512
        '#tree' => TRUE,
513
        // '#type' => $advanced_form ? 'fieldset': 'hidden',
514
        '#title' => t('Options'),
515
    );
516

    
517
    // Clean URL get forms breaks if we don't give it a 'q'.
518
    if (!(bool) variable_get('clean_url', '0')) {
519
        $form['search']['q'] = array(
520
            '#type' => 'hidden',
521
            '#value' => $action_path,
522
            '#name' => 'q',
523
        );
524
    }
525

    
526
    $form['search']['word_size'] = array(
527
        '#weight' => 1,
528
        '#type' => 'textfield',
529
        '#title' => t('Word size'),
530
        '#default_value' => 7,
531
        '#description' => t('Length of initial exact match'),
532
    );
533

    
534
    $form['search']['reward'] = array(
535
        '#weight' => 2,
536
        '#type' => 'textfield',
537
        '#title' => t('Reward'),
538
        '#default_value' => 1,
539
        '#description' => t('Reward for Matching'),
540
    );
541

    
542
    $form['search']['penalty'] = array(
543
        '#weight' => 3,
544
        '#type' => 'textfield',
545
        '#title' => t('Penalty'),
546
        '#default_value' => -2,
547
        '#description' => t('Penalty for mismatching'),
548
    );
549

    
550
    $form['search']['gap_open'] = array(
551
        '#weight' => 4,
552
        '#type' => 'textfield',
553
        '#title' => t('Gap open'),
554
        '#default_value' => 5,
555
        '#description' => t('Cost to open a gap'),
556
    );
557

    
558
    $form['search']['gap_extend'] = array(
559
        '#weight' => 5,
560
        '#type' => 'textfield',
561
        '#title' => t('Gap extend'),
562
        '#default_value' => -2,
563
        '#description' => t('Cost for extend a gap'),
564
    );
565

    
566
    $form['submit'] = array(
567
        '#weight' => 5,
568
        '#type' => 'submit',
569
        '#name' => '',
570
        '#value' => t('Search'),
571
    );
572

    
573
    return $form;
574
}
575
/**
576
 * Wrapper function for cdm_dataportal_search_taxon_form().
577
 *
578
 * This function makes ot possible possible to just pass the
579
 * correct $form_id 'cdm_dataportal_search_taxon_form_advanced' to
580
 * drupal_get_form like:
581
 * drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
582
 *
583
 * @param array $form
584
 *   A drupal form array
585
 * @param array $form_state
586
 *   The drupal form state passed as reference
587
 *
588
 * @return array
589
 *   The form array
590
 */
591
function cdm_dataportal_search_taxon_form_advanced($form, &$form_state) {
592
  return cdm_dataportal_search_taxon_form($form, $form_state, TRUE);
593
}
594

    
595
/**
596
 * Form for searching taxa by the findByDescriptionElementFullText rest service.
597
 */
598
function cdm_dataportal_search_taxon_by_description_form() {
599
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
600

    
601
  $form = cdm_dataportal_search_form_prepare(
602
    'cdm_dataportal/search/results/taxon',
603
    CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT,
604
    $query_field_default_value,
605
    t("Enter the text you wish to search for. The asterisk character * can be
606
        used as wildcard, but must not be used as first character. Terms can be combined with 'AND'. To search for a
607
        full phrase enclose the terms in parentheses. For more syntactical
608
        options please refer to the !link.",
609
      array(
610
        '!link' => l(
611
          t('Apache Lucene - Query Parser Syntax'),
612
          'http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html', array(
613
            'attributes' => array(
614
              'absolute' => TRUE,
615
              'html' => TRUE),
616
          )
617
        ),
618
      )
619
    )
620
  );
621

    
622
  $form['search']['tree'] = array(
623
    '#weight' => -1,
624
    '#type' => 'hidden',
625
    '#value' => get_current_classification_uuid(),
626
  );
627

    
628
  $form['search']['hl'] = array(
629
    '#weight' => -1,
630
    '#type' => 'hidden',
631
    '#value' => 1,
632
  );
633

    
634
  // Only available to admins:
635
  if (!isset($_SESSION['cdm']['search']['clazz'])) {
636
    $_SESSION['cdm']['search']['clazz'] = '';
637
  }
638
  if (module_exists("user") && user_access('administer')) {
639
    $form['search']['clazz'] = array(
640
      '#type' => 'select',
641
      '#title' => t('Limit to description item type'),
642
      '#default_value' => $_SESSION['cdm']['search']['clazz'],
643
      '#options' => cdm_descriptionElementTypes_as_option(TRUE),
644
    );
645
  }
646

    
647
  $profile_feature_tree = get_profile_feature_tree();
648
  $feature_options = _featureTree_nodes_as_feature_options($profile_feature_tree->root);
649
  if (isset($_SESSION['cdm']['search']['features'])) {
650
    $form['search']['features'] = array(
651
      '#type' => 'checkboxes',
652
      '#title' => t('Limit to selected features'),
653
      '#default_value' => $_SESSION['cdm']['search']['features'],
654
      '#options' => $feature_options,
655
    );
656
  }
657
  else {
658
    $form['search']['features'] = array(
659
      '#type' => 'checkboxes',
660
      '#title' => t('Limit to selected features'),
661
      '#options' => $feature_options,
662
    );
663
  }
664
  return $form;
665
}
666

    
667
/**
668
 * Processes the query parameters of the search form.
669
 *
670
 * Reads the query parameters from $_REQUEST and modifies and adds additional
671
 * query parameters if necessary.
672
 *
673
 *  - Filters $_REQUEST by a list of valid request parameters
674
 *  - modifies geographic_range parameters
675
 *  - adds taxon tree uuid if it is missing and if it should not be
676
 *    ignored (parameter value = 'IGNORE')
677
 *  - and more
678
 *
679
 * @param $search_endpoint string
680
 *    The web service endpoint which will be used for executing the search.
681
 *    Usually one of CDM_WS_PORTAL_TAXON_SEARCH, CDM_WS_PORTAL_TAXON_FIND,
682
 *    CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT.
683
 * @return array
684
 *   the processed request parameters submitted by the search form and
685
 *   also stores them in $_SESSION['cdm']['search']
686
 */
687
function cdm_dataportal_search_request($search_endpoint)
688
{
689

    
690
  $form_params = array();
691

    
692
  if (isset($_REQUEST['search']) && is_array($_REQUEST['search'])) {
693
    array_deep_copy($_REQUEST['search'], $form_params);
694
  }
695

    
696
  if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
697
    $form_params = array_merge($form_params, $_REQUEST['pager']);
698
  }
699

    
700
  $form_params['query'] = trim($_REQUEST['query']);
701

    
702

    
703
  // --- handle geographic range
704
  // Split of geographic range.
705
  unset($form_params['areas']);
706

    
707
  $area_filter_preset = null;
708
  if (variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '')) {
709
    $area_filter_preset = explode(',', variable_get(CDM_SEARCH_AREA_FILTER_PRESET, ''));
710
  }
711

    
712
  $area_uuids = array();
713
  if($area_filter_preset){
714
    $area_uuids = $area_filter_preset;
715
  }
716
  elseif (isset($_REQUEST['search']['areas']['area']) && is_array($_REQUEST['search']['areas']['area'])) {
717
    foreach ($_REQUEST['search']['areas']['area'] as $areas) {
718
      $area_uuids = array_merge($area_uuids, $areas);
719
    }
720
    // The area filter is limited to areas with non absent distribution status
721
    $presence_terms_options = cdm_vocabulary_as_option(UUID_PRESENCE_ABSENCE_TERM, null, FALSE, array('absenceTerm' => '/false/'));
722
    $presence_term_uuids = array_keys($presence_terms_options);
723
    $form_params['status'] = $presence_term_uuids;
724
  }
725
  if(count($area_uuids) > 0){
726
    $form_params['area'] = implode(',', $area_uuids);
727
  }
728

    
729
  // Simple search will not submit a 'tree' query parameter,
730
  // so we add it here from what is stored in the session unless
731
  // SIMPLE_SEARCH_IGNORE_CLASSIFICATION is checked in the settings.
732
  if (!isset($form_params['tree']) && !variable_get(SIMPLE_SEARCH_IGNORE_CLASSIFICATION, 0)) {
733
    $form_params['tree'] = get_current_classification_uuid();
734
  }
735
  // Store in session.
736
  $_SESSION['cdm']['search'] = $form_params;
737

    
738
  // ----------- further processing that must not be store in the session --------- //
739

    
740
  if($search_endpoint == CDM_WS_PORTAL_TAXON_SEARCH){
741
    // HACK to allow using dot characters
742
    $form_params['query'] = str_replace('.', '*', $form_params['query']);
743
    // lucene based taxon search always as phrase search if the query string contains a whitespace --> enclose it in "
744
    if(preg_match("/\s+/", $form_params['query'])){
745
      if(!str_beginsWith($form_params['query'], '"')){
746
        $form_params['query'] = '"' . $form_params['query'];
747
      }
748
      if(!str_endsWith($form_params['query'], '"')){
749
        $form_params['query'] = $form_params['query'] . '"' ;
750
      }
751
    }
752
  }
753

    
754
  // If the 'NONE' classification has been chosen (advanced search)
755
  // delete the tree information to avoid unknown uuid exceptions in the
756
  // cdm service.
757
  if (isset($form_params['tree'])
758
    && ($form_params['tree'] == 'NONE' || !is_uuid($form_params['tree']))
759
  ) {
760
    // $form_params['ignore_classification'] =  TRUE;
761
    unset($form_params['tree']);
762
  }
763
  // else {
764
  //   $form_params['ignore_classification'] =  NULL;
765
  // }
766

    
767

    
768
  return $form_params;
769
}
770

    
771
/**
772
 * Processes the query parameters of the blast search form.
773
 *
774
 * Reads the query parameters from $_REQUEST and modifies and adds additional
775
 * query parameters if necessary.
776
 *
777
 *  - Filters $_REQUEST by a list of valid request parameters
778
 *
779
 *
780
 * @param $search_endpoint string
781
 *    The web service endpoint which will be used for executing the search.
782
 *
783
 * @return array
784
 *   the processed request parameters submitted by the search form and
785
 *   also stores them in $_SESSION['cdm']['search']
786
 */
787
function cdm_dataportal_blast_search_request($search_endpoint)
788
{
789

    
790
    $form_params = array();
791

    
792
    if (isset($_REQUEST['search']) && is_array($_REQUEST['search'])) {
793
        array_deep_copy($_REQUEST['search'], $form_params['data']);
794
    }
795

    
796

    
797
    //$query = trim($_REQUEST['query']);
798
    $form_params['data'] = formatParams($_REQUEST['search']);
799

    
800
    /*if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
801
        $form_params = array_merge($form_params, $_REQUEST['pager']);
802
    }*/
803

    
804
    $form_params['query']= trim($_REQUEST['query']);
805

    
806

    
807
    // Store in session.
808
    $_SESSION['cdm']['search'] = $form_params;
809

    
810
    return $form_params;
811
}
812

    
813

    
814

    
815
/**
816
 * Provides the classification to which the last search has been limited to..
817
 *
818
 * This function should only be used after the cdm_dataportal_search_taxon_execute()
819
 * handler has been run, otherwise it will return the information from the last
820
 * search executed. The information is retrieved from
821
 * the $_SESSION variable:  $_SESSION['cdm']['search']['tree']
822
 *
823
 * @return object
824
 *   the CDM classification instance which has been used a filter for the
825
 *   last processed search
826
 *   or NULL, it it was on all classifications
827
 */
828
function cdm_dataportal_searched_in_classification() {
829

    
830
  $classification = &drupal_static(__FUNCTION__);
831

    
832
  if (!isset($classification)) {
833
    if (isset($_SESSION['cdm']['search']['tree'])) {
834
      $classification = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY, ($_SESSION['cdm']['search']['tree']));
835
    }
836
    else {
837
      $classification = FALSE;
838
    }
839
  }
840

    
841
  return $classification !== FALSE ? $classification : NULL;
842
}
843

    
844
/**
845
 * Removed the drupal internal form parameters 'form_id', 'form_token', 'form_build_id' from the request array.
846
 *
847
 * @param $request array
848
 *   Pass $_REQUEST as paramter
849
 * @return array
850
 *  The $request array without drupal internal form parameters
851
 */
852
function remove_drupal_form_params($request) {
853

    
854
  static $exclude_keys = array('form_id', 'form_token', 'form_build_id');
855
  $request_sanitized = array();
856
  foreach ($request as $key => $value) {
857
    if(!array_search($key, $exclude_keys)){
858
      $request_sanitized[$key] = $value;
859
    }
860
  }
861

    
862
  return $request_sanitized;
863
}
864

    
865
/**
866
 * Sends a search request to the cdm server.
867
 *
868
 * The parameters to build the query are taken obtained by calling
869
 * cdm_dataportal_search_request() which reads the query parameters
870
 * from $_REQUEST and add additional query parameters if nessecary.
871
 *
872
 * @see cdm_dataportal_search_request()
873
 */
874
function cdm_dataportal_search_taxon_execute() {
875

    
876
  // Store as last search in session.
877
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
878

    
879
  // Validate the search webservice parameter:
880
  if (!isset($_REQUEST['ws'])) {
881
    drupal_set_message(
882
      t("Invalid search, webservice parameter 'ws' is missing"), 'warning'
883
    );
884
    return NULL;
885
  }
886
  if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {
887
    // Endpoint is unknown.
888
    drupal_set_message(
889
      t("Invalid search webservice parameter 'ws' given"), 'warning'
890
    );
891
    return NULL;
892
  }
893

    
894
  // Read the query parameters from $_REQUEST and add additional query
895
  // parameters if necessary.
896
  $request_params = cdm_dataportal_search_request($_REQUEST['ws']);
897

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

    
900
  return $taxon_pager;
901
}
902

    
903
/**
904
 * Sends a search request to the cdm server.
905
 *
906
 * The parameters to build the query are taken obtained by calling
907
 * cdm_dataportal_search_request() which reads the query parameters
908
 * from $_REQUEST and add additional query parameters if nessecary.
909
 *
910
 * @see cdm_dataportal_search_request()
911
 */
912
function cdm_dataportal_search_blast_execute() {
913

    
914
    // Store as last search in session.
915
    $_SESSION['cdm']['last_blast_search'] = $_SERVER['REQUEST_URI'];
916

    
917
    // Validate the search webservice parameter:
918
    if (!isset($_REQUEST['ws'])) {
919
        drupal_set_message(
920
            t("Invalid search, webservice parameter 'ws' is missing"), 'warning'
921
        );
922
        return NULL;
923
    }
924
//    if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {
925
//        // Endpoint is unknown.
926
//        drupal_set_message(
927
//            t("Invalid search webservice parameter 'ws' given"), 'warning'
928
//        );
929
//        return NULL;
930
//    }
931

    
932
    // Read the query parameters from $_REQUEST and add additional query
933
    // parameters if necessary.
934
    $request_params = cdm_dataportal_blast_search_request($_REQUEST['ws']);
935
   // $url = drupal_http_build_query($_REQUEST['ws'], $request_params);
936

    
937
    $taxon_pager = drupal_http_request($_REQUEST['ws'].'?sequence='.$request_params['query'], $request_params);
938

    
939
    return $taxon_pager;
940
}
941

    
942

    
943

    
944
/**
945
 * Sends a request for a registrations filter search to the cdm server.
946
 */
947
function cdm_dataportal_search_registrations_filter_execute()
948
{
949

    
950
  static $query_param_map = array(
951
    'identifier' => 'identifierFilterPattern',
952
    'taxon_name'=> 'taxonNameFilterPattern',
953
    'type_designation_status' => 'typeDesignationStatusUuids',
954
  );
955

    
956
  $session_key = SESSION_KEY_SEARCH_REGISTRATION_FILTER;
957
  $request_params = cdm_dataportal_search_request_params($session_key, $query_param_map);
958

    
959
  // cleanup
960
  if(isset($request_params['typeDesignationStatusUuids'])){
961
    if(!$request_params['typeDesignationStatusUuids']
962
      || $request_params['typeDesignationStatusUuids'] == "0"
963
      || (isset($request_params['typeDesignationStatusUuids'][0]) && !$request_params['typeDesignationStatusUuids'][0])){
964
      unset($request_params['typeDesignationStatusUuids']);
965
    }
966
  }
967
  if(isset($request_params['taxonNameFilterPattern'])){
968
    // trim and remove empty taxon name query strings
969
    $request_params['taxonNameFilterPattern'] = trim($request_params['taxonNameFilterPattern']);
970
    if(!$request_params['taxonNameFilterPattern']){
971
      unset($request_params['taxonNameFilterPattern']);
972
    }
973
  }
974

    
975
  $registration_pager = cdm_ws_get('registrationDTO/find', NULL, queryString($request_params));
976

    
977
  return $registration_pager;
978
}
979

    
980
/**
981
 * Sends a request for a registrations taxongraph search to the cdm server.
982
 */
983
function cdm_dataportal_search_registrations_taxongraph_execute()
984
{
985

    
986
  static $query_param_map = array(
987
    'taxon_name'=> 'taxonNameFilter'
988
  );
989

    
990
  $session_key = SESSION_KEY_SEARCH_TAXONGRAPH_FOR_REGISTRATION_FILTER;
991
  $request_params = cdm_dataportal_search_request_params($session_key, $query_param_map);
992

    
993
  // cleanup
994
  if(isset($request_params['taxonNameFilter'])){
995
    // trim and remove empty taxon name query strings
996
    $request_params['taxonNameFilter'] = trim($request_params['taxonNameFilter']);
997
    if(!$request_params['taxonNameFilter']){
998
      unset($request_params['taxonNameFilter']);
999
    }
1000
  }
1001

    
1002
  $registration_pager = cdm_ws_get('registrationDTO/findInTaxonGraph', NULL, queryString($request_params));
1003

    
1004
  return $registration_pager;
1005
}
1006

    
1007
/**
1008
 * @param $session_key
1009
 * @param $query_param_map
1010
 * @return array
1011
 */
1012
function cdm_dataportal_search_request_params($session_key, $query_param_map)
1013
{
1014
  // Read the query parameters from $_REQUEST and add additional query
1015
  // parameters if necessary.
1016
  $request_params = array();
1017

    
1018
  $request = remove_drupal_form_params($_REQUEST);
1019

    
1020
  if (count($request) > 0) {
1021
    $_SESSION['cdm'][$session_key] = $request;
1022
    foreach ($query_param_map as $filter_key => $query_param) {
1023
      if (isset($request[$filter_key])) {
1024
        $request_params[$query_param] = $request[$filter_key];
1025
      }
1026
    }
1027
    if (isset($request['pager']['pageNumber'])) {
1028
      $request_params['pageNumber'] = $request['pager']['pageNumber'];
1029
    }
1030
  }
1031

    
1032
  if (count($request_params) == 0 && isset($_SESSION['cdm'][$session_key])) {
1033
    foreach ($query_param_map as $filter_key => $query_param) {
1034
      if (isset($_SESSION['cdm'][$session_key][$filter_key])) {
1035
        $request_params[$query_param] = $_SESSION['cdm'][$session_key][$filter_key];
1036
      }
1037
    }
1038
    if (isset($_SESSION['cdm'][$session_key]['pager']['pageNumber'])) {
1039
      $request_params['pageNumber'] = $_SESSION['cdm'][$session_key]['pager']['pageNumber'];
1040
    }
1041
  }
1042
  return $request_params;
1043
}
1044

    
1045
/**
1046
 * Transforms the termDTO tree into options array.
1047
 *
1048
 *   TermDto:
1049
 *      - partOfUuid:
1050
 *      - representation_L10n:
1051
 *      - representation_L10n_abbreviatedLabel:
1052
 *      - uuid:
1053
 *      - vocabularyUuid:
1054
 *      - children: array of TermDto
1055
 *
1056
 * The options array is suitable for drupal form API elements that
1057
 * allow multiple choices.
1058
 * @see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#options
1059
 *
1060
 * @param array $term_dto_tree
1061
 *   a hierarchic array of CDM TermDto instances, with additional
1062
 * 'children' field:
1063
 * @param array $options
1064
 *   Internally used for recursive calls
1065
 * @param string $prefix
1066
 *   Internally used for recursive calls
1067
 *
1068
 * @return array
1069
 *   the terms in an array as options for a form element that allows
1070
 *   multiple choices.
1071
 */
1072
function term_tree_as_options($term_dto_tree, &$options = array(), $prefix = '') {
1073

    
1074
  uasort($term_dto_tree, 'compare_terms_by_order_index');
1075
  foreach ($term_dto_tree as $uuid => $dto) {
1076
    $label = $prefix . '<span class="child-label">'
1077
      .  $dto->representation_L10n
1078
      . '</span><span class="child-label-abbreviated"> (' . $dto->representation_L10n_abbreviatedLabel . ')</span>';
1079
    $options[$uuid] = $label;
1080
    if (isset($dto->children) && is_array($dto->children)) {
1081
      term_tree_as_options(
1082
        $dto->children,
1083
        $options, $prefix
1084
          . '<span data-cdm-parent="' . $uuid . '" class="parent"></span>'
1085
      );
1086
    }
1087
  }
1088

    
1089
  return $options;
1090
}
1091

    
1092

    
1093
function cdm_dataportal_search_registration_filter_form($form, &$form_state) {
1094

    
1095
  static $filter_presets_empty = array(
1096
    'identifier'=> null,
1097
    'taxon_name'=> null,
1098
    'type_designation_status' => null
1099
  );
1100

    
1101
  _add_font_awesome_font();
1102

    
1103
  if(isset($_REQUEST['q']) && ($_REQUEST['q'] == 'cdm_dataportal/registration-search/filter' || $_REQUEST['q'] == 'cdm_dataportal/registration-search')){
1104
    // read the $request_params only if it was send from this form
1105
    $request_params = remove_drupal_form_params($_REQUEST);
1106
  } else {
1107
    $request_params = array();
1108
  }
1109
  $filter_presets = (isset($_SESSION['cdm'][SESSION_KEY_SEARCH_REGISTRATION_FILTER]) ? $_SESSION['cdm'][SESSION_KEY_SEARCH_REGISTRATION_FILTER] : array());
1110
  $filter_presets = array_merge($filter_presets_empty, $filter_presets, $request_params);
1111
  $form['#action'] =  url('/cdm_dataportal/registration-search/filter');
1112
  $form['#method'] = 'get';
1113
  $form['#attributes'] = array('class' => array('search-filter'));
1114
  $form['identifier'] = array(
1115
    '#type' => 'textfield',
1116
    '#title' => t('Identifier'),
1117
    '#default_value' => $filter_presets['identifier'],
1118
    '#size' => 20,
1119
    '#maxlength' => 128
1120
  );
1121
  $form['taxon_name'] = array(
1122
    '#type' => 'textfield',
1123
    '#title' => t('Scientific name'),
1124
    '#default_value' => $filter_presets['taxon_name'],
1125
    '#size' => 20,
1126
    '#maxlength' => 128
1127
  );
1128
  $form['type_designation_status'] = array(
1129
    '#type' => 'select',
1130
    '#title' => t('Type designation status'),
1131
    '#multiple' => true,
1132
    '#options' => cdm_terms_by_type_as_option('TypeDesignationStatusBase', null, null, TRUE),
1133
    '#default_value' => $filter_presets['type_designation_status']
1134
  );
1135

    
1136
  $form['submit'] = array(
1137
    '#type' => 'submit',
1138
    '#attributes' => array('class' => array('fa-icon'), 'title' => t('Search')),
1139
    '#value' => decode_entities('&#xf002;'), // fontawesome search icon
1140
//    '#prefix' => "<div class=\"form-item\"><label>&nbsp</label>",
1141
//    '#suffix' => "</div>"
1142

    
1143
  );
1144
  return $form;
1145
}
1146

    
1147

    
1148
function cdm_dataportal_search_registration_taxongraph_form($form, &$form_state) {
1149

    
1150
  static $filter_presets_empty = array(
1151
    'taxon_name'=> null
1152
  );
1153

    
1154
  _add_font_awesome_font();
1155

    
1156
  if(isset($_REQUEST['q']) && $_REQUEST['q']  == 'cdm_dataportal/registration-search/taxongraph'){
1157
    // read the $request_params only if it was send from this form
1158
    $request_params = remove_drupal_form_params($_REQUEST);
1159
  } else {
1160
    $request_params = array();
1161
  }
1162
  $filter_presets = (isset($_SESSION['cdm'][SESSION_KEY_SEARCH_TAXONGRAPH_FOR_REGISTRATION_FILTER]) ? $_SESSION['cdm'][SESSION_KEY_SEARCH_TAXONGRAPH_FOR_REGISTRATION_FILTER] : array());
1163
  $filter_presets = array_merge($filter_presets_empty, $filter_presets, $request_params);
1164

    
1165
  $form['#action'] =  url('/cdm_dataportal/registration-search/taxongraph');
1166
  $form['#method'] = 'get';
1167
  $form['#attributes'] = array('class' => array('search-filter'));
1168
  $form['taxon_name'] = array(
1169
    '#type' => 'textfield',
1170
    '#title' => t('Scientific name'),
1171
    '#default_value' => $filter_presets['taxon_name'],
1172
    '#size' => 20,
1173
    '#maxlength' => 128
1174
  );
1175

    
1176
  $form['submit'] = array(
1177
    '#type' => 'submit',
1178
    '#attributes' => array('class' => array('fa-icon'), 'title' => t('Search')),
1179
    '#value' => decode_entities('&#xf002;'), // fontawesome search icon
1180
//    '#prefix' => "<div class=\"form-item\"><label>&nbsp</label>",
1181
//    '#suffix' => "</div>"
1182

    
1183
  );
1184
  return $form;
1185
}
1186

    
1187
/**
1188
 * Compose the result set of a registration search from a pager object
1189
 *
1190
 * @param $registration_pager
1191
 *    The pager containing registration objects
1192
 *
1193
 * @return
1194
 *   A drupal render array.
1195
 *
1196
 * @ingroup compose
1197
 *
1198
 * TODO compose function into search.inc ?
1199
 */
1200
function compose_registrations_search_results($registration_pager){
1201

    
1202
  $render_array = array();
1203
  $render_array['pre'] = markup_to_render_array("<div class=\"cdm-item-list\">");
1204

    
1205
  if($registration_pager != null && count($registration_pager->records) > 0){
1206
    $items_render_array = array();
1207
    foreach($registration_pager->records as $registration_dto) {
1208

    
1209
      $items_render_array[]  = array(
1210
        '#prefix' => "<div class=\"item\"><div class=\"" . html_class_attribute_ref(new TypedEntityReference("Registration", $registration_dto->uuid)) . "\">",
1211
         'item_data' => compose_registration_dto_compact($registration_dto, 'item-style', 'div'),
1212
        '#suffix' => "</div></div>"
1213
        );
1214
      ;
1215
    }
1216

    
1217
    $render_array['items'] = $items_render_array;
1218
    $render_array['pager'] =  markup_to_render_array(theme('cdm_pager', array(
1219
          'pager' => $registration_pager,
1220
          'path' => $_REQUEST['q'], // stay on same page
1221
          'parameters' => $_REQUEST,
1222
        )));
1223

    
1224
  } else {
1225
    if($registration_pager != null && $registration_pager->count > 0 && count($registration_pager->records) == 0){
1226
      $render_array['items'] = markup_to_render_array("<div id=\"no_results\">Result page out of range.</div>");
1227
    } else {
1228
      $render_array['items'] = markup_to_render_array("<div id=\"no_results\">No results found.</div>");
1229
    }
1230
  }
1231
  $render_array['post'] = markup_to_render_array("</div>");
1232

    
1233
  return $render_array;
1234

    
1235
}
Add picture from clipboard (Maximum size: 40 MB)