Project

General

Profile

Download (42.1 KB) Statistics
| Branch: | Tag: | Revision:
1 6657531f Andreas Kohlbecker
<?php
2
/**
3
 * @file
4
 * Search related functions.
5
 */
6
7 e2617c7e Andreas Kohlbecker
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 a6ae799b Andreas Kohlbecker
11 6657531f Andreas Kohlbecker
/**
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 dc435632 Andreas Kohlbecker
    CDM_WS_PORTAL_TAXON_SEARCH => "cdm_dataportal/search",
30 6657531f Andreas Kohlbecker
    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 103a1463 Andreas Kohlbecker
 * dataportal. The form returned is populated with all necessary fields
40 6657531f Andreas Kohlbecker
 * 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 632a697a Andreas Kohlbecker
 *   A default text for the query field
51 6657531f Andreas Kohlbecker
 * @param string $query_field_description
52 632a697a Andreas Kohlbecker
 *   The description text for the query field
53 6657531f Andreas Kohlbecker
 *
54
 * @return array
55
 *   The prepared form array.
56
 */
57 24797d7f Andreas Kohlbecker
function cdm_dataportal_search_form_prepare($action_path, $search_webservice, $query_field_default_value, $query_field_description) {
58 2d0d855a Andreas Kohlbecker
59
60 6657531f Andreas Kohlbecker
  $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 632a697a Andreas Kohlbecker
    // This causes the description to display also when hovering over
76 2d0d855a Andreas Kohlbecker
    // the textfield.
77
    // This is wanted behaviour for the simple seach but could
78 632a697a Andreas Kohlbecker
    // be disabled for the advances search.
79 6657531f Andreas Kohlbecker
    '#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 023c4a03 Patrick Plitzner
  if(variable_get(SIMPLE_SEARCH_AUTO_SUGGEST)){
87 24f57e45 Patrick Plitzner
      $form['query']['#autocomplete_path'] = 'cdm_dataportal/taxon/autosuggest////';
88 35bea66c Patrick Plitzner
  }
89 6657531f Andreas Kohlbecker
90 93ae67cc Patrick Plitzner
    $form['search'] = array(
91 6657531f Andreas Kohlbecker
    '#weight' => 3,
92
    '#tree' => TRUE,
93 7b4c053d Andreas Kohlbecker
    // '#type' => $advanced_form ? 'fieldset': 'hidden',
94 6657531f Andreas Kohlbecker
    '#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 24f57e45 Patrick Plitzner
function cdm_dataportal_taxon_autosuggest($classificationUuid = NULL, $areaUuid = NULL, $status = NULL, $string) {
117 93ae67cc Patrick Plitzner
  $matches = array();
118
119
  $queryParams = array();
120 04a1d647 Patrick Plitzner
  $queryParams['query'] = $string.'*';
121 24f57e45 Patrick Plitzner
  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 04a1d647 Patrick Plitzner
  }
127
  if($areaUuid){
128
    $queryParams['area'] = $areaUuid;
129
  }
130
  if($status){
131
    $queryParams['status'] = $status ;
132
  }
133 93ae67cc Patrick Plitzner
  $queryParams['pageNumber'] = '0';
134 04a1d647 Patrick Plitzner
  $queryParams['pageSize'] = '10';
135 93ae67cc Patrick Plitzner
  $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 01fff74a Andreas Kohlbecker
      preg_match('/(.*) sec.*/', $titleCache, $trimmedTitle); //remove sec reference
144 93ae67cc Patrick Plitzner
      $trimmedTitle = trim($trimmedTitle[1]);
145 f35d3f4a Patrick Plitzner
      $matches[$trimmedTitle] = check_plain($trimmedTitle);
146 93ae67cc Patrick Plitzner
  }
147
  drupal_json_output($matches);
148
}
149
150
151
  /**
152 dbf4b8a8 Andreas Kohlbecker
 * Creates a search form for searching on taxa.
153
 *
154 7b4c053d Andreas Kohlbecker
 * If advanced $advanced_form id TRUE the form will offer additional choices
155 dbf4b8a8 Andreas Kohlbecker
 *
156 73874c48 Andreas Kohlbecker
 * @param array $form
157 7b4c053d Andreas Kohlbecker
 *   A drupal form array
158 73874c48 Andreas Kohlbecker
 * @param array $form_state
159 7b4c053d Andreas Kohlbecker
 *   The drupal form state passed as reference
160
 * @param bool $advanced_form
161 73874c48 Andreas Kohlbecker
 *   default is FALSE
162 7b4c053d Andreas Kohlbecker
 * @param bool $classification_select
163 01fff74a Andreas Kohlbecker
 *   set TRUE to offer a classification selector in the form - default is FALSE
164 73874c48 Andreas Kohlbecker
 *   if only available in the advanced mode
165 dbf4b8a8 Andreas Kohlbecker
 *
166 7b4c053d Andreas Kohlbecker
 * @return array
167 dbf4b8a8 Andreas Kohlbecker
 *   the form array
168 6657531f Andreas Kohlbecker
 */
169 6eaec849 Katja Luther
function cdm_dataportal_search_taxon_form($form, &$form_state, $advanced_form = FALSE, $classification_select = TRUE)
170
{
171 6657531f Andreas Kohlbecker
172 6eaec849 Katja Luther
    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 6657531f Andreas Kohlbecker
176 2d0d855a Andreas Kohlbecker
177 6eaec849 Katja Luther
        $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 cd64df61 Andreas Kohlbecker
185 6eaec849 Katja Luther
        $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 7b4c053d Andreas Kohlbecker
  if (!$advanced_form) {
194 6657531f Andreas Kohlbecker
    $form['query']['#size'] = 20;
195
  }
196
197
  $form['search']['pageSize'] = array(
198
    '#weight' => -1,
199
    '#type' => 'hidden',
200 44458b04 Andreas Kohlbecker
    '#value' => variable_get(CDM_SEARCH_RESULT_PAGE_SIZE, CDM_SEARCH_RESULT_PAGE_SIZE_DEFAULT),
201 6657531f Andreas Kohlbecker
  );
202
203
  $form['search']['pageNumber'] = array(
204
    '#weight' => -1,
205
    '#type' => 'hidden',
206
    '#value' => 0,
207
  );
208
209 7b4c053d Andreas Kohlbecker
  $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 6657531f Andreas Kohlbecker
218 7b4c053d Andreas Kohlbecker
  if ($advanced_form) {
219 642b323b Andreas Kohlbecker
220 6280e639 Andreas Kohlbecker
    // --- ADVANCED SEARCH FORM ---
221 61b6ee11 Andreas Kohlbecker
    //
222 dbf4b8a8 Andreas Kohlbecker
223 6657531f Andreas Kohlbecker
    // Get presets from settings.
224 7663cd0b Andreas Kohlbecker
    $preset_classification_uuid = get_current_classification_uuid();
225 6657531f Andreas Kohlbecker
226
    // Overwrite presets by user choice stored in session.
227 6280e639 Andreas Kohlbecker
    if (isset($_SESSION['cdm']['search'])) {
228 7b4c053d Andreas Kohlbecker
      $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 61b6ee11 Andreas Kohlbecker
      if (isset($_SESSION['cdm']['search']['tree'])) {
233
        $preset_classification_uuid = $_SESSION['cdm']['search']['tree'];
234
      }
235 6657531f Andreas Kohlbecker
    }
236 dbf4b8a8 Andreas Kohlbecker
237 7b4c053d Andreas Kohlbecker
    if ($classification_select === TRUE) {
238 f56b1626 Andreas Kohlbecker
      $form['search']['tree'] = array(
239 642b323b Andreas Kohlbecker
        '#title' => t('Classification'),
240
        '#weight' => 1,
241
        '#type' => 'select',
242 7d42c393 Andreas Kohlbecker
        '#default_value' => $preset_classification_uuid,
243 f56b1626 Andreas Kohlbecker
        '#options' => cdm_get_taxontrees_as_options(TRUE),
244 8ae3cfe3 Andreas Kohlbecker
        '#description' => t('A filter to limit the search to a specific classification. Choosing <em>--- ALL ---</em> will disable this filter.'),
245 dbf4b8a8 Andreas Kohlbecker
      );
246 632a697a Andreas Kohlbecker
    }
247 dbf4b8a8 Andreas Kohlbecker
248 6657531f Andreas Kohlbecker
    // General search parameters.
249
    $form['search']['doTaxa'] = array(
250
      '#weight' => 2,
251
      '#type' => 'checkbox',
252 51384c70 Andreas Kohlbecker
      '#title' => t('Include') . ' ' . t('accepted taxa'),
253 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_taxa,
254 6657531f Andreas Kohlbecker
    );
255
    $form['search']['doSynonyms'] = array(
256
      '#weight' => 3,
257
      '#type' => 'checkbox',
258 51384c70 Andreas Kohlbecker
      '#title' => t('Include') . ' ' . t('synonyms'),
259 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_synonyms,
260 6657531f Andreas Kohlbecker
    );
261
    $form['search']['doMisappliedNames'] = array(
262
      '#weight' => 4,
263
      '#type' => 'checkbox',
264 51384c70 Andreas Kohlbecker
      '#title' => t('Include') . ' ' . t('misapplied names'),
265 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_misapplied_names,
266 6657531f Andreas Kohlbecker
    );
267
    $form['search']['doTaxaByCommonNames'] = array(
268
      '#weight' => 5,
269
      '#type' => 'checkbox',
270 51384c70 Andreas Kohlbecker
      '#title' => t('Include') . ' ' . t('common names'),
271 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_taxa_by_common_names,
272 6657531f Andreas Kohlbecker
    );
273
274 7b4c053d Andreas Kohlbecker
    $area_term_dtos = cdm_ws_fetch_all(
275
      CDM_WS_DESCRIPTION_NAMEDAREAS_IN_USE,
276
      array('includeAllParents' => 'true')
277
    );
278 446cffc5 Andreas Kohlbecker
279 86424a78 Andreas Kohlbecker
    // create map: term_uuid => term
280 41c6dd13 Andreas Kohlbecker
    $term_map = array();
281 7b4c053d Andreas Kohlbecker
    foreach ($area_term_dtos as $term_dto) {
282 41c6dd13 Andreas Kohlbecker
      $term_map[$term_dto->uuid] = $term_dto;
283 632a697a Andreas Kohlbecker
    }
284 41c6dd13 Andreas Kohlbecker
285 632a697a Andreas Kohlbecker
    $term_tree = array();
286 41c6dd13 Andreas Kohlbecker
    // mixed_vocabularies will contain the uuid vocabularies which
287 86424a78 Andreas Kohlbecker
    // also contain terms of foreign vocabularies due to the term
288 41c6dd13 Andreas Kohlbecker
    // hierarchy
289
    $mixed_vocabularies = array();
290
291
    // Build hierarchy of the terms regardless of the vocabulary.
292 a783afbc Andreas Kohlbecker
    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 446cffc5 Andreas Kohlbecker
          }
300 a783afbc Andreas Kohlbecker
          $parent->children[$term_dto->uuid] = $term_dto;
301
          if ($parent->vocabularyUuid != $term_dto->vocabularyUuid) {
302
            $mixed_vocabularies[$parent->vocabularyUuid] = $parent->vocabularyUuid;
303 41c6dd13 Andreas Kohlbecker
          }
304 632a697a Andreas Kohlbecker
        }
305 446cffc5 Andreas Kohlbecker
      }
306 a783afbc Andreas Kohlbecker
      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 072122ee Andreas Kohlbecker
    $show_area_filter = ! variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '');
316 446cffc5 Andreas Kohlbecker
317 072122ee Andreas Kohlbecker
    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 446cffc5 Andreas Kohlbecker
        jQuery(\'#edit-search-areas\').search_area_filter(\'#edit-search-areas-areas-filter\');
322
      });
323
      ', array('type' => 'inline'));
324
325 072122ee Andreas Kohlbecker
      $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 722b1688 Andreas Kohlbecker
        information for at least one of the selected areas.') . ' '
330 072122ee Andreas Kohlbecker
          .(count($term_tree) > 1 ? t('The areas are grouped
331 722b1688 Andreas Kohlbecker
        by the vocabularies to which the highest level areas belong.') : ''),
332 632a697a Andreas Kohlbecker
      );
333 072122ee Andreas Kohlbecker
      $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 6b605d3b Andreas Kohlbecker
      _add_js_resizable_element('.resizable-box', true);
343 072122ee Andreas Kohlbecker
      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 6b605d3b Andreas Kohlbecker
            . '</strong><div class="resizable-container"><div class="resizable-box">',
350 072122ee Andreas Kohlbecker
          '#type' => 'checkboxes',
351
          '#default_value' => $areas_defaults,
352
          '#options' => $areas_options,
353 6b605d3b Andreas Kohlbecker
          '#suffix' => '</div></div>'
354 072122ee Andreas Kohlbecker
        );
355
      }
356 632a697a Andreas Kohlbecker
    }
357 446cffc5 Andreas Kohlbecker
358 7b4c053d Andreas Kohlbecker
  }
359
  else {
360 61b6ee11 Andreas Kohlbecker
    // --- SIMPLE SEARCH FORM ---
361
    //
362
363 6657531f Andreas Kohlbecker
    // Overwrite presets by user choice stored in session.
364 6280e639 Andreas Kohlbecker
    if (isset($_SESSION['cdm']['search'])) {
365 7b4c053d Andreas Kohlbecker
      $preset_do_misapplied_names = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
366 6657531f Andreas Kohlbecker
    }
367
368
    $form['search']['doTaxa'] = array(
369
      '#weight' => -2,
370
      '#type' => 'hidden',
371 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_taxa,
372 6657531f Andreas Kohlbecker
    );
373
    $form['search']['doSynonyms'] = array(
374
      '#weight' => -3,
375
      '#type' => 'hidden',
376 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_synonyms,
377 6657531f Andreas Kohlbecker
    );
378
    $form['search']['doMisappliedNames'] = array(
379
      '#weight' => -4,
380
      '#type' => 'checkbox',
381
      '#title' => t('Misapplied names'),
382 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_misapplied_names,
383 6657531f Andreas Kohlbecker
    );
384
    $form['search']['doTaxaByCommonNames'] = array(
385
      '#weight' => -5,
386
      '#type' => 'hidden',
387 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_taxa_by_common_names,
388 6657531f Andreas Kohlbecker
    );
389
  }
390
391
  return $form;
392
}
393 632a697a Andreas Kohlbecker
394 6eaec849 Katja Luther
/**
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 2f30124c Andreas Kohlbecker
    $search_service_endpoint = CDM_SEARCH_BLAST_SERVICE_URI;
417 6eaec849 Katja Luther
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 44458b04 Andreas Kohlbecker
        '#value' => variable_get(CDM_SEARCH_RESULT_PAGE_SIZE, CDM_SEARCH_RESULT_PAGE_SIZE_DEFAULT),
432 6eaec849 Katja Luther
    );
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 6657531f Andreas Kohlbecker
/**
577 7b4c053d Andreas Kohlbecker
 * 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 6657531f Andreas Kohlbecker
 */
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 7b4c053d Andreas Kohlbecker
 * Form for searching taxa by the findByDescriptionElementFullText rest service.
598 6657531f Andreas Kohlbecker
 */
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 632a697a Andreas Kohlbecker
    '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 20624f5a Andreas Kohlbecker
        used as wildcard, but must not be used as first character. Terms can be combined with 'AND'. To search for a
608 36af8b60 Andreas Kohlbecker
        full phrase enclose the terms in parentheses. For more syntactical
609 7b4c053d Andreas Kohlbecker
        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 a6ae799b Andreas Kohlbecker
    )
621 632a697a Andreas Kohlbecker
  );
622 6657531f Andreas Kohlbecker
623
  $form['search']['tree'] = array(
624
    '#weight' => -1,
625
    '#type' => 'hidden',
626 7663cd0b Andreas Kohlbecker
    '#value' => get_current_classification_uuid(),
627 6657531f Andreas Kohlbecker
  );
628
629
  $form['search']['hl'] = array(
630
    '#weight' => -1,
631
    '#type' => 'hidden',
632
    '#value' => 1,
633
  );
634
635 b58fcc1f Andreas Kohlbecker
  // Only available to admins:
636 6657531f Andreas Kohlbecker
  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 b58fcc1f Andreas Kohlbecker
      '#title' => t('Limit to description item type'),
643 6657531f Andreas Kohlbecker
      '#default_value' => $_SESSION['cdm']['search']['clazz'],
644
      '#options' => cdm_descriptionElementTypes_as_option(TRUE),
645
    );
646
  }
647
648 7b4c053d Andreas Kohlbecker
  $profile_feature_tree = get_profile_feature_tree();
649
  $feature_options = _featureTree_nodes_as_feature_options($profile_feature_tree->root);
650 6657531f Andreas Kohlbecker
  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 7b4c053d Andreas Kohlbecker
 * Processes the query parameters of the search form.
670
 *
671
 * Reads the query parameters from $_REQUEST and modifies and adds additional
672 103a1463 Andreas Kohlbecker
 * query parameters if necessary.
673 dbf4b8a8 Andreas Kohlbecker
 *
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 b3d49e6e Andreas Kohlbecker
 * @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 7b4c053d Andreas Kohlbecker
 * @return array
685 dbf4b8a8 Andreas Kohlbecker
 *   the processed request parameters submitted by the search form and
686
 *   also stores them in $_SESSION['cdm']['search']
687 6657531f Andreas Kohlbecker
 */
688 b3d49e6e Andreas Kohlbecker
function cdm_dataportal_search_request($search_endpoint)
689 f48cede0 Andreas Kohlbecker
{
690 61b6ee11 Andreas Kohlbecker
691 6657531f Andreas Kohlbecker
  $form_params = array();
692 61b6ee11 Andreas Kohlbecker
693 34dd7be9 Andreas Kohlbecker
  if (isset($_REQUEST['search']) && is_array($_REQUEST['search'])) {
694 6657531f Andreas Kohlbecker
    array_deep_copy($_REQUEST['search'], $form_params);
695
  }
696 34dd7be9 Andreas Kohlbecker
697
  if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
698
    $form_params = array_merge($form_params, $_REQUEST['pager']);
699
  }
700
701 6657531f Andreas Kohlbecker
  $form_params['query'] = trim($_REQUEST['query']);
702
703 b3d49e6e Andreas Kohlbecker
704 6657531f Andreas Kohlbecker
  // --- handle geographic range
705
  // Split of geographic range.
706 44756fcd Andreas Kohlbecker
  unset($form_params['areas']);
707 072122ee Andreas Kohlbecker
708 9bbe3bcd Andreas Kohlbecker
  $area_filter_preset = null;
709 f48cede0 Andreas Kohlbecker
  if (variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '')) {
710
    $area_filter_preset = explode(',', variable_get(CDM_SEARCH_AREA_FILTER_PRESET, ''));
711
  }
712 072122ee Andreas Kohlbecker
713 30399ffa Andreas Kohlbecker
  $area_uuids = array();
714 072122ee Andreas Kohlbecker
  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 7b4c053d Andreas Kohlbecker
    foreach ($_REQUEST['search']['areas']['area'] as $areas) {
719
      $area_uuids = array_merge($area_uuids, $areas);
720
    }
721 efa372e9 Andreas Kohlbecker
    // 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 072122ee Andreas Kohlbecker
  }
726 30399ffa Andreas Kohlbecker
  if(count($area_uuids) > 0){
727 7b4c053d Andreas Kohlbecker
    $form_params['area'] = implode(',', $area_uuids);
728 6657531f Andreas Kohlbecker
  }
729
730 7b4c053d Andreas Kohlbecker
  // Simple search will not submit a 'tree' query parameter,
731
  // so we add it here from what is stored in the session unless
732 90a6166e Andreas Kohlbecker
  // SIMPLE_SEARCH_IGNORE_CLASSIFICATION is checked in the settings.
733
  if (!isset($form_params['tree']) && !variable_get(SIMPLE_SEARCH_IGNORE_CLASSIFICATION, 0)) {
734 7663cd0b Andreas Kohlbecker
    $form_params['tree'] = get_current_classification_uuid();
735 61b6ee11 Andreas Kohlbecker
  }
736 38753115 Andreas Kohlbecker
  // Store in session.
737
  $_SESSION['cdm']['search'] = $form_params;
738
739
  // ----------- further processing that must not be store in the session --------- //
740
741 67a5cafa Andreas Kohlbecker
  if($search_endpoint == CDM_WS_PORTAL_TAXON_SEARCH){
742 a15edbb0 Andreas Kohlbecker
    // HACK to allow using dot characters
743
    $form_params['query'] = str_replace('.', '*', $form_params['query']);
744 a0557101 Andreas Kohlbecker
    // 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 67a5cafa Andreas Kohlbecker
    }
753
  }
754
755 efa372e9 Andreas Kohlbecker
  // If the 'NONE' classification has been chosen (advanced search)
756 7b4c053d Andreas Kohlbecker
  // 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 dbf4b8a8 Andreas Kohlbecker
    unset($form_params['tree']);
763
  }
764 7b4c053d Andreas Kohlbecker
  // else {
765
  //   $form_params['ignore_classification'] =  NULL;
766
  // }
767 dbf4b8a8 Andreas Kohlbecker
768 6657531f Andreas Kohlbecker
769
  return $form_params;
770
}
771
772 6eaec849 Katja Luther
/**
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 72d57201 Katja Luther
    $form_params['data'] = formatWSParams($_REQUEST['search']);
796
    $form_params['query']= trim($_REQUEST['query']).$form_params['data'];
797 6eaec849 Katja Luther
    // Store in session.
798
    $_SESSION['cdm']['search'] = $form_params;
799
    return $form_params;
800
}
801
802 61b6ee11 Andreas Kohlbecker
/**
803 44756fcd Andreas Kohlbecker
 * Provides the classification to which the last search has been limited to..
804 61b6ee11 Andreas Kohlbecker
 *
805 24797d7f Andreas Kohlbecker
 * This function should only be used after the cdm_dataportal_search_taxon_execute()
806 efa372e9 Andreas Kohlbecker
 * handler has been run, otherwise it will return the information from the last
807 7b4c053d Andreas Kohlbecker
 * search executed. The information is retrieved from
808 61b6ee11 Andreas Kohlbecker
 * the $_SESSION variable:  $_SESSION['cdm']['search']['tree']
809
 *
810 7b4c053d Andreas Kohlbecker
 * @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 61b6ee11 Andreas Kohlbecker
 */
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 7b4c053d Andreas Kohlbecker
    }
823
    else {
824 61b6ee11 Andreas Kohlbecker
      $classification = FALSE;
825
    }
826
  }
827
828 7b4c053d Andreas Kohlbecker
  return $classification !== FALSE ? $classification : NULL;
829 61b6ee11 Andreas Kohlbecker
}
830
831 a6ae799b Andreas Kohlbecker
/**
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 6657531f Andreas Kohlbecker
/**
853 103a1463 Andreas Kohlbecker
 * Sends a search request to the cdm server.
854 dbf4b8a8 Andreas Kohlbecker
 *
855
 * The parameters to build the query are taken obtained by calling
856 efa372e9 Andreas Kohlbecker
 * cdm_dataportal_search_request() which reads the query parameters
857 dbf4b8a8 Andreas Kohlbecker
 * from $_REQUEST and add additional query parameters if nessecary.
858
 *
859 efa372e9 Andreas Kohlbecker
 * @see cdm_dataportal_search_request()
860 6657531f Andreas Kohlbecker
 */
861 24797d7f Andreas Kohlbecker
function cdm_dataportal_search_taxon_execute() {
862 6657531f Andreas Kohlbecker
863
  // Store as last search in session.
864
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
865
866
  // Validate the search webservice parameter:
867 7b4c053d Andreas Kohlbecker
  if (!isset($_REQUEST['ws'])) {
868
    drupal_set_message(
869 103a1463 Andreas Kohlbecker
      t("Invalid search, webservice parameter 'ws' is missing"), 'warning'
870 7b4c053d Andreas Kohlbecker
    );
871 61b6ee11 Andreas Kohlbecker
    return NULL;
872
  }
873 7b4c053d Andreas Kohlbecker
  if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {
874 6657531f Andreas Kohlbecker
    // Endpoint is unknown.
875 7b4c053d Andreas Kohlbecker
    drupal_set_message(
876 78ec4159 Andreas Kohlbecker
      t("Invalid search webservice parameter 'ws' given"), 'warning'
877 7b4c053d Andreas Kohlbecker
    );
878 6657531f Andreas Kohlbecker
    return NULL;
879
  }
880
881 7b4c053d Andreas Kohlbecker
  // Read the query parameters from $_REQUEST and add additional query
882
  // parameters if necessary.
883 b3d49e6e Andreas Kohlbecker
  $request_params = cdm_dataportal_search_request($_REQUEST['ws']);
884 dbf4b8a8 Andreas Kohlbecker
885 446cffc5 Andreas Kohlbecker
  $taxon_pager = cdm_ws_get($_REQUEST['ws'], NULL, queryString($request_params));
886
887
  return $taxon_pager;
888
}
889
890 6eaec849 Katja Luther
/**
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 72d57201 Katja Luther
    $request_params['timeout'] = 200;
924 6eaec849 Katja Luther
    $taxon_pager = drupal_http_request($_REQUEST['ws'].'?sequence='.$request_params['query'], $request_params);
925
926
    return $taxon_pager;
927
}
928
929
930 a6ae799b Andreas Kohlbecker
931
/**
932 efbff4b3 Andreas Kohlbecker
 * Sends a request for a registrations filter search to the cdm server.
933 a6ae799b Andreas Kohlbecker
 */
934 efbff4b3 Andreas Kohlbecker
function cdm_dataportal_search_registrations_filter_execute()
935 a6ae799b Andreas Kohlbecker
{
936
937
  static $query_param_map = array(
938
    'identifier' => 'identifierFilterPattern',
939
    'taxon_name'=> 'taxonNameFilterPattern',
940 c5089cec Andreas Kohlbecker
    'reference_citation' => 'referenceFilterPattern',
941 dc58a71b Andreas Kohlbecker
    'type_designation_status_uuids' => 'typeDesignationStatusUuids',
942 a6ae799b Andreas Kohlbecker
  );
943
944 efbff4b3 Andreas Kohlbecker
  $session_key = SESSION_KEY_SEARCH_REGISTRATION_FILTER;
945
  $request_params = cdm_dataportal_search_request_params($session_key, $query_param_map);
946 a6ae799b Andreas Kohlbecker
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 bb177c82 Andreas Kohlbecker
  }
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 a6ae799b Andreas Kohlbecker
  }
962 c5089cec Andreas Kohlbecker
  // 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 a6ae799b Andreas Kohlbecker
971
  $registration_pager = cdm_ws_get('registrationDTO/find', NULL, queryString($request_params));
972
973
  return $registration_pager;
974
}
975
976 efbff4b3 Andreas Kohlbecker
/**
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 e2617c7e Andreas Kohlbecker
 *   The key to be used for storing the search params in $_SESSION['cdm'][]
1006 efbff4b3 Andreas Kohlbecker
 * @param $query_param_map
1007 e2617c7e Andreas Kohlbecker
 *    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 efbff4b3 Andreas Kohlbecker
 * @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 7b4c053d Andreas Kohlbecker
/**
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 c079337e Andreas Kohlbecker
  uasort($term_dto_tree, 'compare_terms_by_order_index');
1075 7b4c053d Andreas Kohlbecker
  foreach ($term_dto_tree as $uuid => $dto) {
1076
    $label = $prefix . '<span class="child-label">'
1077
      .  $dto->representation_L10n
1078 35619936 Andreas Kohlbecker
      . '</span><span class="child-label-abbreviated"> (' . $dto->representation_L10n_abbreviatedLabel . ')</span>';
1079 7b4c053d Andreas Kohlbecker
    $options[$uuid] = $label;
1080
    if (isset($dto->children) && is_array($dto->children)) {
1081
      term_tree_as_options(
1082
        $dto->children,
1083
        $options, $prefix
1084 fdf52d6a Andreas Kohlbecker
          . '<span data-cdm-parent="' . $uuid . '" class="parent"></span>'
1085
      );
1086 446cffc5 Andreas Kohlbecker
    }
1087 7b4c053d Andreas Kohlbecker
  }
1088 6657531f Andreas Kohlbecker
1089 7b4c053d Andreas Kohlbecker
  return $options;
1090 6657531f Andreas Kohlbecker
}
1091 a6ae799b Andreas Kohlbecker
1092
1093 efbff4b3 Andreas Kohlbecker
function cdm_dataportal_search_registration_filter_form($form, &$form_state) {
1094 a6ae799b Andreas Kohlbecker
1095 d68a30b0 Andreas Kohlbecker
  static $filter_presets_empty = array(
1096
    'identifier'=> null,
1097
    'taxon_name'=> null,
1098 c5089cec Andreas Kohlbecker
    'reference_citation'=> null,
1099 dc58a71b Andreas Kohlbecker
    'type_designation_status_uuids' => null
1100 d68a30b0 Andreas Kohlbecker
  );
1101
1102 6d9071b7 Andreas Kohlbecker
  _add_font_awesome_font();
1103 bb177c82 Andreas Kohlbecker
1104 5195ddf2 Andreas Kohlbecker
  if(isset($_REQUEST['q']) && ($_REQUEST['q'] == 'cdm_dataportal/registration-search/filter' || $_REQUEST['q'] == 'cdm_dataportal/registration-search')){
1105 efbff4b3 Andreas Kohlbecker
    // 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 a6ae799b Andreas Kohlbecker
  $filter_presets = (isset($_SESSION['cdm'][SESSION_KEY_SEARCH_REGISTRATION_FILTER]) ? $_SESSION['cdm'][SESSION_KEY_SEARCH_REGISTRATION_FILTER] : array());
1111 efbff4b3 Andreas Kohlbecker
  $filter_presets = array_merge($filter_presets_empty, $filter_presets, $request_params);
1112
  $form['#action'] =  url('/cdm_dataportal/registration-search/filter');
1113 a6ae799b Andreas Kohlbecker
  $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 c5089cec Andreas Kohlbecker
  $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 dc58a71b Andreas Kohlbecker
  $form['type_designation_status_uuids'] = array(
1137 a6ae799b Andreas Kohlbecker
    '#type' => 'select',
1138
    '#title' => t('Type designation status'),
1139 88131480 Andreas Kohlbecker
    '#multiple' => true,
1140 dc58a71b Andreas Kohlbecker
    '#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 a6ae799b Andreas Kohlbecker
  );
1144
1145
  $form['submit'] = array(
1146 c8624e9a Andreas Kohlbecker
    '#markup' => '<button type="submit" title="Search" class="form-submit">' . font_awesome_icon_markup('fa-search') . '</button>'
1147 098b6358 Andreas Kohlbecker
//    '#prefix' => "<div class=\"form-item\"><label>&nbsp</label>",
1148
//    '#suffix' => "</div>"
1149 a6ae799b Andreas Kohlbecker
1150
  );
1151
  return $form;
1152
}
1153
1154
1155 efbff4b3 Andreas Kohlbecker
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 5195ddf2 Andreas Kohlbecker
  if(isset($_REQUEST['q']) && $_REQUEST['q']  == 'cdm_dataportal/registration-search/taxongraph'){
1164 efbff4b3 Andreas Kohlbecker
    // 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 a6ae799b Andreas Kohlbecker
/**
1195
 * Compose the result set of a registration search from a pager object
1196
 *
1197 e2617c7e Andreas Kohlbecker
 * @param $cdm_item_pager
1198 a6ae799b Andreas Kohlbecker
 *    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 e2617c7e Andreas Kohlbecker
function compose_search_results($cdm_item_pager, ItemComposeHandler $item_compose_handler){
1208 a6ae799b Andreas Kohlbecker
1209
  $render_array = array();
1210 6789eff8 Andreas Kohlbecker
  $render_array['pre'] = markup_to_render_array("<div class=\"cdm-item-list\">");
1211 0b494565 Andreas Kohlbecker
1212 e2617c7e Andreas Kohlbecker
  if($cdm_item_pager != null && count($cdm_item_pager->records) > 0){
1213 a6ae799b Andreas Kohlbecker
    $items_render_array = array();
1214 e2617c7e Andreas Kohlbecker
    foreach($cdm_item_pager->records as $registration_dto) {
1215 26b8a1bc Andreas Kohlbecker
1216
      $items_render_array[]  = array(
1217 e2617c7e Andreas Kohlbecker
        '#prefix' => "<div class=\"item\"><div class=\"" . $item_compose_handler->getClassAttributes($registration_dto) . "\">",
1218
         'item_data' => $item_compose_handler->composeItem($registration_dto),
1219 26b8a1bc Andreas Kohlbecker
        '#suffix' => "</div></div>"
1220 a6ae799b Andreas Kohlbecker
        );
1221
      ;
1222
    }
1223 efbff4b3 Andreas Kohlbecker
1224 a6ae799b Andreas Kohlbecker
    $render_array['items'] = $items_render_array;
1225
    $render_array['pager'] =  markup_to_render_array(theme('cdm_pager', array(
1226 e2617c7e Andreas Kohlbecker
          'pager' => $cdm_item_pager,
1227 efbff4b3 Andreas Kohlbecker
          'path' => $_REQUEST['q'], // stay on same page
1228 a6ae799b Andreas Kohlbecker
          'parameters' => $_REQUEST,
1229
        )));
1230
1231
  } else {
1232 e2617c7e Andreas Kohlbecker
    if($cdm_item_pager != null && $cdm_item_pager->count > 0 && count($cdm_item_pager->records) == 0){
1233 0b494565 Andreas Kohlbecker
      $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 a6ae799b Andreas Kohlbecker
  }
1238
  $render_array['post'] = markup_to_render_array("</div>");
1239
1240
  return $render_array;
1241
1242 e2617c7e Andreas Kohlbecker
}
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 77451b2a Andreas Kohlbecker
  $restrictions = [];
1268
  if( isset($request_params['markerType'])){
1269
    $restrictions = array(new Restriction("markers.markerType.uuid","EXACT", array($request_params['markerType']), 'AND'));
1270
  }
1271 e2617c7e Andreas Kohlbecker
  $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 77451b2a Andreas Kohlbecker
  $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 e2617c7e Andreas Kohlbecker
1288
  return $pager;
1289
}