Project

General

Profile

Download (42.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Search related functions.
5
 */
6

    
7
const SESSION_KEY_SEARCH_REGISTRATION_FILTER = "SESSION_KEY_SEARCH_REGISTRATION_FILTER";
8
const SESSION_KEY_SEARCH_TAXONGRAPH_FOR_REGISTRATION_FILTER = 'SESSION_KEY_SEARCH_TAXONGRAPH_FOR_REGISTRATION_FILTER';
9
const SESSION_KEY_SEARCH_AGENT_FILTER = 'SEARCH_AGENT_FILTER';
10

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

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

    
59

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

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

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

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

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

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

    
113
  return $form;
114
}
115

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

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

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

    
150

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

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

    
176

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

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

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

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

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

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

    
218
  if ($advanced_form) {
219

    
220
    // --- ADVANCED SEARCH FORM ---
221
    //
222

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
391
  return $form;
392
}
393

    
394
/**
395
 * Creates a search form for searching on taxa.
396
 *
397
 * If advanced $advanced_form id TRUE the form will offer additional choices
398
 *
399
 * @param array $form
400
 *   A drupal form array
401
 * @param array $form_state
402
 *   The drupal form state passed as reference
403
 * @param bool $advanced_form
404
 *   default is FALSE
405
 * @param bool $classification_select
406
 *   set TRUE to offer a classification selector in the form - default is FALSE
407
 *   if only available in the advanced mode
408
 *
409
 * @return array
410
 *   the form array
411
 */
412
function cdm_dataportal_search_blast_form($form, &$form_state) {
413

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

    
416
    $search_service_endpoint = CDM_SEARCH_BLAST_SERVICE_URI;
417

    
418

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

    
426

    
427

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

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

    
440

    
441

    
442

    
443

    
444
    return $form;
445
}
446

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

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

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

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

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

    
510

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
691
  $form_params = array();
692

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

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

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

    
703

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

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

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

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

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

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

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

    
768

    
769
  return $form_params;
770
}
771

    
772
/**
773
 * Processes the query parameters of the blast search form.
774
 *
775
 * Reads the query parameters from $_REQUEST and modifies and adds additional
776
 * query parameters if necessary.
777
 *
778
 *  - Filters $_REQUEST by a list of valid request parameters
779
 *
780
 *
781
 * @param $search_endpoint string
782
 *    The web service endpoint which will be used for executing the search.
783
 *
784
 * @return array
785
 *   the processed request parameters submitted by the search form and
786
 *   also stores them in $_SESSION['cdm']['search']
787
 */
788
function cdm_dataportal_blast_search_request($search_endpoint)
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
    $form_params['data'] = formatWSParams($_REQUEST['search']);
796
    $form_params['query']= trim($_REQUEST['query']).$form_params['data'];
797
    // Store in session.
798
    $_SESSION['cdm']['search'] = $form_params;
799
    return $form_params;
800
}
801

    
802
/**
803
 * Provides the classification to which the last search has been limited to..
804
 *
805
 * This function should only be used after the cdm_dataportal_search_taxon_execute()
806
 * handler has been run, otherwise it will return the information from the last
807
 * search executed. The information is retrieved from
808
 * the $_SESSION variable:  $_SESSION['cdm']['search']['tree']
809
 *
810
 * @return object
811
 *   the CDM classification instance which has been used a filter for the
812
 *   last processed search
813
 *   or NULL, it it was on all classifications
814
 */
815
function cdm_dataportal_searched_in_classification() {
816

    
817
  $classification = &drupal_static(__FUNCTION__);
818

    
819
  if (!isset($classification)) {
820
    if (isset($_SESSION['cdm']['search']['tree'])) {
821
      $classification = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY, ($_SESSION['cdm']['search']['tree']));
822
    }
823
    else {
824
      $classification = FALSE;
825
    }
826
  }
827

    
828
  return $classification !== FALSE ? $classification : NULL;
829
}
830

    
831
/**
832
 * Removed the drupal internal form parameters 'form_id', 'form_token', 'form_build_id' from the request array.
833
 *
834
 * @param $request array
835
 *   Pass $_REQUEST as paramter
836
 * @return array
837
 *  The $request array without drupal internal form parameters
838
 */
839
function remove_drupal_form_params($request) {
840

    
841
  static $exclude_keys = array('form_id', 'form_token', 'form_build_id');
842
  $request_sanitized = array();
843
  foreach ($request as $key => $value) {
844
    if(!array_search($key, $exclude_keys)){
845
      $request_sanitized[$key] = $value;
846
    }
847
  }
848

    
849
  return $request_sanitized;
850
}
851

    
852
/**
853
 * Sends a search request to the cdm server.
854
 *
855
 * The parameters to build the query are taken obtained by calling
856
 * cdm_dataportal_search_request() which reads the query parameters
857
 * from $_REQUEST and add additional query parameters if nessecary.
858
 *
859
 * @see cdm_dataportal_search_request()
860
 */
861
function cdm_dataportal_search_taxon_execute() {
862

    
863
  // Store as last search in session.
864
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
865

    
866
  // Validate the search webservice parameter:
867
  if (!isset($_REQUEST['ws'])) {
868
    drupal_set_message(
869
      t("Invalid search, webservice parameter 'ws' is missing"), 'warning'
870
    );
871
    return NULL;
872
  }
873
  if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {
874
    // Endpoint is unknown.
875
    drupal_set_message(
876
      t("Invalid search webservice parameter 'ws' given"), 'warning'
877
    );
878
    return NULL;
879
  }
880

    
881
  // Read the query parameters from $_REQUEST and add additional query
882
  // parameters if necessary.
883
  $request_params = cdm_dataportal_search_request($_REQUEST['ws']);
884

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

    
887
  return $taxon_pager;
888
}
889

    
890
/**
891
 * Sends a search request to the cdm server.
892
 *
893
 * The parameters to build the query are taken obtained by calling
894
 * cdm_dataportal_search_request() which reads the query parameters
895
 * from $_REQUEST and add additional query parameters if nessecary.
896
 *
897
 * @see cdm_dataportal_search_request()
898
 */
899
function cdm_dataportal_search_blast_execute() {
900

    
901
    // Store as last search in session.
902
    $_SESSION['cdm']['last_blast_search'] = $_SERVER['REQUEST_URI'];
903

    
904
    // Validate the search webservice parameter:
905
    if (!isset($_REQUEST['ws'])) {
906
        drupal_set_message(
907
            t("Invalid search, webservice parameter 'ws' is missing"), 'warning'
908
        );
909
        return NULL;
910
    }
911
//    if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {
912
//        // Endpoint is unknown.
913
//        drupal_set_message(
914
//            t("Invalid search webservice parameter 'ws' given"), 'warning'
915
//        );
916
//        return NULL;
917
//    }
918

    
919
    // Read the query parameters from $_REQUEST and add additional query
920
    // parameters if necessary.
921
    $request_params = cdm_dataportal_blast_search_request($_REQUEST['ws']);
922
   // $url = drupal_http_build_query($_REQUEST['ws'], $request_params);
923
    $request_params['timeout'] = 200;
924
    $taxon_pager = drupal_http_request($_REQUEST['ws'].'?sequence='.$request_params['query'], $request_params);
925

    
926
    return $taxon_pager;
927
}
928

    
929

    
930

    
931
/**
932
 * Sends a request for a registrations filter search to the cdm server.
933
 */
934
function cdm_dataportal_search_registrations_filter_execute()
935
{
936

    
937
  static $query_param_map = array(
938
    'identifier' => 'identifierFilterPattern',
939
    'taxon_name'=> 'taxonNameFilterPattern',
940
    'reference_citation' => 'referenceFilterPattern',
941
    'type_designation_status_uuids' => 'typeDesignationStatusUuids',
942
  );
943

    
944
  $session_key = SESSION_KEY_SEARCH_REGISTRATION_FILTER;
945
  $request_params = cdm_dataportal_search_request_params($session_key, $query_param_map);
946

    
947
  // cleanup
948
  if(isset($request_params['typeDesignationStatusUuids'])){
949
    if(!$request_params['typeDesignationStatusUuids']
950
      || $request_params['typeDesignationStatusUuids'] == "0"
951
      || (isset($request_params['typeDesignationStatusUuids'][0]) && !$request_params['typeDesignationStatusUuids'][0])){
952
      unset($request_params['typeDesignationStatusUuids']);
953
    }
954
  }
955
  if(isset($request_params['taxonNameFilterPattern'])){
956
    // trim and remove empty taxon name query strings
957
    $request_params['taxonNameFilterPattern'] = trim($request_params['taxonNameFilterPattern']);
958
    if(!$request_params['taxonNameFilterPattern']){
959
      unset($request_params['taxonNameFilterPattern']);
960
    }
961
  }
962
  // reference_citation
963
  if(isset($request_params['referenceFilterPattern'])){
964
    // trim and remove empty taxon name query strings
965
    $request_params['referenceFilterPattern'] = trim($request_params['referenceFilterPattern']);
966
    if(!$request_params['referenceFilterPattern']){
967
      unset($request_params['referenceFilterPattern']);
968
    }
969
  }
970

    
971
  $registration_pager = cdm_ws_get('registrationDTO/find', NULL, queryString($request_params));
972

    
973
  return $registration_pager;
974
}
975

    
976
/**
977
 * Sends a request for a registrations taxongraph search to the cdm server.
978
 */
979
function cdm_dataportal_search_registrations_taxongraph_execute()
980
{
981

    
982
  static $query_param_map = array(
983
    'taxon_name'=> 'taxonNameFilter'
984
  );
985

    
986
  $session_key = SESSION_KEY_SEARCH_TAXONGRAPH_FOR_REGISTRATION_FILTER;
987
  $request_params = cdm_dataportal_search_request_params($session_key, $query_param_map);
988

    
989
  // cleanup
990
  if(isset($request_params['taxonNameFilter'])){
991
    // trim and remove empty taxon name query strings
992
    $request_params['taxonNameFilter'] = trim($request_params['taxonNameFilter']);
993
    if(!$request_params['taxonNameFilter']){
994
      unset($request_params['taxonNameFilter']);
995
    }
996
  }
997

    
998
  $registration_pager = cdm_ws_get('registrationDTO/findInTaxonGraph', NULL, queryString($request_params));
999

    
1000
  return $registration_pager;
1001
}
1002

    
1003
/**
1004
 * @param $session_key
1005
 *   The key to be used for storing the search params in $_SESSION['cdm'][]
1006
 * @param $query_param_map
1007
 *    An array which maps the filter_key to the web service query parameter.
1008
 *    The filter key may be used as form element name or as drupal url
1009
 *    query parameter.
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
    'reference_citation'=> null,
1099
    'type_designation_status_uuids' => null
1100
  );
1101

    
1102
  _add_font_awesome_font();
1103

    
1104
  if(isset($_REQUEST['q']) && ($_REQUEST['q'] == 'cdm_dataportal/registration-search/filter' || $_REQUEST['q'] == 'cdm_dataportal/registration-search')){
1105
    // read the $request_params only if it was send from this form
1106
    $request_params = remove_drupal_form_params($_REQUEST);
1107
  } else {
1108
    $request_params = array();
1109
  }
1110
  $filter_presets = (isset($_SESSION['cdm'][SESSION_KEY_SEARCH_REGISTRATION_FILTER]) ? $_SESSION['cdm'][SESSION_KEY_SEARCH_REGISTRATION_FILTER] : array());
1111
  $filter_presets = array_merge($filter_presets_empty, $filter_presets, $request_params);
1112
  $form['#action'] =  url('/cdm_dataportal/registration-search/filter');
1113
  $form['#method'] = 'get';
1114
  $form['#attributes'] = array('class' => array('search-filter'));
1115
  $form['identifier'] = array(
1116
    '#type' => 'textfield',
1117
    '#title' => t('Identifier'),
1118
    '#default_value' => $filter_presets['identifier'],
1119
    '#size' => 20,
1120
    '#maxlength' => 128
1121
  );
1122
  $form['taxon_name'] = array(
1123
    '#type' => 'textfield',
1124
    '#title' => t('Scientific name'),
1125
    '#default_value' => $filter_presets['taxon_name'],
1126
    '#size' => 20,
1127
    '#maxlength' => 128
1128
  );
1129
  $form['reference_citation'] = array(
1130
    '#type' => 'textfield',
1131
    '#title' => t('Publication'),
1132
    '#default_value' => $filter_presets['reference_citation'],
1133
    '#size' => 20,
1134
    '#maxlength' => 128
1135
  );
1136
  $form['type_designation_status_uuids'] = array(
1137
    '#type' => 'select',
1138
    '#title' => t('Type designation status'),
1139
    '#multiple' => true,
1140
    '#options' => cdm_type_designation_status_filter_terms_as_options('- none -'),
1141
    '#default_value' => $filter_presets['type_designation_status_uuids'],
1142
    "#description" => '<i>' . t('Ctrl + Click to unselect') . '</i>'
1143
  );
1144

    
1145
  $form['submit'] = array(
1146
    '#markup' => '<button type="submit" title="Search" class="form-submit">' . font_awesome_icon_markup('fa-search') . '</button>'
1147
//    '#prefix' => "<div class=\"form-item\"><label>&nbsp</label>",
1148
//    '#suffix' => "</div>"
1149

    
1150
  );
1151
  return $form;
1152
}
1153

    
1154

    
1155
function cdm_dataportal_search_registration_taxongraph_form($form, &$form_state) {
1156

    
1157
  static $filter_presets_empty = array(
1158
    'taxon_name'=> null
1159
  );
1160

    
1161
  _add_font_awesome_font();
1162

    
1163
  if(isset($_REQUEST['q']) && $_REQUEST['q']  == 'cdm_dataportal/registration-search/taxongraph'){
1164
    // read the $request_params only if it was send from this form
1165
    $request_params = remove_drupal_form_params($_REQUEST);
1166
  } else {
1167
    $request_params = array();
1168
  }
1169
  $filter_presets = (isset($_SESSION['cdm'][SESSION_KEY_SEARCH_TAXONGRAPH_FOR_REGISTRATION_FILTER]) ? $_SESSION['cdm'][SESSION_KEY_SEARCH_TAXONGRAPH_FOR_REGISTRATION_FILTER] : array());
1170
  $filter_presets = array_merge($filter_presets_empty, $filter_presets, $request_params);
1171

    
1172
  $form['#action'] =  url('/cdm_dataportal/registration-search/taxongraph');
1173
  $form['#method'] = 'get';
1174
  $form['#attributes'] = array('class' => array('search-filter'));
1175
  $form['taxon_name'] = array(
1176
    '#type' => 'textfield',
1177
    '#title' => t('Scientific name'),
1178
    '#default_value' => $filter_presets['taxon_name'],
1179
    '#size' => 20,
1180
    '#maxlength' => 128
1181
  );
1182

    
1183
  $form['submit'] = array(
1184
    '#type' => 'submit',
1185
    '#attributes' => array('class' => array('fa-icon'), 'title' => t('Search')),
1186
    '#value' => decode_entities('&#xf002;'), // fontawesome search icon
1187
//    '#prefix' => "<div class=\"form-item\"><label>&nbsp</label>",
1188
//    '#suffix' => "</div>"
1189

    
1190
  );
1191
  return $form;
1192
}
1193

    
1194
/**
1195
 * Compose the result set of a registration search from a pager object
1196
 *
1197
 * @param $cdm_item_pager
1198
 *    The pager containing registration objects
1199
 *
1200
 * @return
1201
 *   A drupal render array.
1202
 *
1203
 * @ingroup compose
1204
 *
1205
 * TODO compose function into search.inc ?
1206
 */
1207
function compose_search_results($cdm_item_pager, ItemComposeHandler $item_compose_handler){
1208

    
1209
  $render_array = array();
1210
  $render_array['pre'] = markup_to_render_array("<div class=\"cdm-item-list\">");
1211

    
1212
  if($cdm_item_pager != null && count($cdm_item_pager->records) > 0){
1213
    $items_render_array = array();
1214
    foreach($cdm_item_pager->records as $registration_dto) {
1215

    
1216
      $items_render_array[]  = array(
1217
        '#prefix' => "<div class=\"item\"><div class=\"" . $item_compose_handler->getClassAttributes($registration_dto) . "\">",
1218
         'item_data' => $item_compose_handler->composeItem($registration_dto),
1219
        '#suffix' => "</div></div>"
1220
        );
1221
      ;
1222
    }
1223

    
1224
    $render_array['items'] = $items_render_array;
1225
    $render_array['pager'] =  markup_to_render_array(theme('cdm_pager', array(
1226
          'pager' => $cdm_item_pager,
1227
          'path' => $_REQUEST['q'], // stay on same page
1228
          'parameters' => $_REQUEST,
1229
        )));
1230

    
1231
  } else {
1232
    if($cdm_item_pager != null && $cdm_item_pager->count > 0 && count($cdm_item_pager->records) == 0){
1233
      $render_array['items'] = markup_to_render_array("<div id=\"no_results\">Result page out of range.</div>");
1234
    } else {
1235
      $render_array['items'] = markup_to_render_array("<div id=\"no_results\">No results found.</div>");
1236
    }
1237
  }
1238
  $render_array['post'] = markup_to_render_array("</div>");
1239

    
1240
  return $render_array;
1241

    
1242
}
1243

    
1244

    
1245
/**
1246
 * Sends a search request for agents to the cdm server.
1247
 */
1248
function cdm_dataportal_search_agent_execute()
1249
{
1250

    
1251
  static $query_param_map = array(
1252
    'markerType' => 'markerType',
1253
    'cdmType' => 'class'
1254
  );
1255

    
1256
  $session_key = SESSION_KEY_SEARCH_AGENT_FILTER;
1257
  $request_params = cdm_dataportal_search_request_params($session_key, $query_param_map);
1258

    
1259
  // cleanup
1260
  if(isset($request_params['taxonNameFilter'])){
1261
    // trim and remove empty taxon name query strings
1262
    $request_params['taxonNameFilter'] = trim($request_params['taxonNameFilter']);
1263
    if(!$request_params['taxonNameFilter']){
1264
      unset($request_params['taxonNameFilter']);
1265
    }
1266
  }
1267
  $restrictions = [];
1268
  if( isset($request_params['markerType'])){
1269
    $restrictions = array(new Restriction("markers.markerType.uuid","EXACT", array($request_params['markerType']), 'AND'));
1270
  }
1271
  $init_strategy = array(
1272
    "$",
1273
    "titleCache",
1274
    "extensions.$",
1275
    "identifiers.type"
1276
  );
1277

    
1278
  $type_restriction = null;
1279
  if(isset($query_param_map['class']) && ($query_param_map['class'] == 'Team' || $query_param_map['class'] == 'Person')){
1280
    $type_restriction = $query_param_map['class'];
1281
  }
1282
  $page_index = 0;
1283
  if(isset($_REQUEST['pager']['pageNumber'])){
1284
    $page_index = $_REQUEST['pager']['pageNumber'];
1285
  }
1286
  $pager = cdm_ws_page_by_restriction('AgentBase', $type_restriction, $restrictions, $init_strategy, 50, $page_index);
1287

    
1288
  return $pager;
1289
}
1290

    
(11-11/18)