Project

General

Profile

Download (21.6 KB) Statistics
| Branch: | Tag: | Revision:
1 6657531f Andreas Kohlbecker
<?php
2
/**
3
 * @file
4
 * Search related functions.
5
 */
6
7
/**
8
 * Returns a Drupal path to a search form for a CDM webservice.
9
 *
10
 * For a given CDM webservice end-point, the drupal page path to the
11
 * according search form is returned.
12
 * cdm webservice end points are defined in constant variables like:
13
 * <code>CDM_WS_PORTAL_TAXON_FIND</code> and
14
 * <code>CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT</code>
15
 *
16
 * @param string $ws_endpoint
17
 *   The cdm webservice endpoint for which to find the search form path.
18
 *
19
 * @return string
20
 *   The Drupal path found.
21
 */
22
function cdm_dataportal_search_form_path_for_ws($ws_endpoint) {
23
  static $form_ws_map = array(
24
    CDM_WS_PORTAL_TAXON_FIND => "cdm_dataportal/search",
25 dc435632 Andreas Kohlbecker
    CDM_WS_PORTAL_TAXON_SEARCH => "cdm_dataportal/search",
26 6657531f Andreas Kohlbecker
    CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT => "cdm_dataportal/search/taxon_by_description",
27
  );
28
  return $form_ws_map[$ws_endpoint];
29
}
30
31
/**
32
 * Prepares a form array for a general purpose search form.
33
 *
34
 * The form is used for general purpose search functionality in the
35
 * dataportal. The form returned is populated with all nessecary fields
36
 * for internal processing and has the textfield element $form['query']
37
 * which holds the query term.
38
 * he sub tree array can be extended to contain additional search parameters.
39
 *
40
 * @param string $action_path
41
 *   The Drupal path to be put into the action url to which the form will
42
 *   be submitted.
43
 * @param string $search_webservice
44
 *   The cdm-remote webservice to be used, valid values are defined by
45
 *   the constants: FIXME.
46
 * @param string $query_field_default_value
47 632a697a Andreas Kohlbecker
 *   A default text for the query field
48 6657531f Andreas Kohlbecker
 * @param string $query_field_description
49 632a697a Andreas Kohlbecker
 *   The description text for the query field
50 6657531f Andreas Kohlbecker
 * @param string $process
51
 *   The value for #process, if NULL (default), 'cdm_dataportal_search_process'
52
 *   is used.
53
 *
54
 * @return array
55
 *   The prepared form array.
56
 */
57
function cdm_dataportal_search_form_prepare($action_path, $search_webservice, $query_field_default_value, $query_field_description, $process = NULL) {
58 2d0d855a Andreas Kohlbecker
59 6657531f Andreas Kohlbecker
  if ($process == NULL) {
60
    $process = 'cdm_dataportal_search_process';
61
  }
62 2d0d855a Andreas Kohlbecker
63 6657531f Andreas Kohlbecker
  $form['#method'] = 'get';
64 632a697a Andreas Kohlbecker
  //
65
  //  $form['#process'] = array(
66
  //  $process => array(),
67
  //  );
68
  //
69 6657531f Andreas Kohlbecker
  $form['#action'] = url($action_path, array(
70
    'absolute' => TRUE,
71
  ));
72
73
  $form['ws'] = array(
74
    '#type' => 'hidden',
75
    '#value' => $search_webservice,
76
    '#name' => 'ws',
77
  );
78
79
  $form['query'] = array(
80
    '#weight' => 0,
81
    '#type' => 'textfield',
82
    '#size' => 68,
83 632a697a Andreas Kohlbecker
    // This causes the description to display also when hovering over
84 2d0d855a Andreas Kohlbecker
    // the textfield.
85
    // This is wanted behaviour for the simple seach but could
86 632a697a Andreas Kohlbecker
    // be disabled for the advances search.
87 6657531f Andreas Kohlbecker
    '#attributes' => array(
88
      'title' => $query_field_description,
89
    ),
90
    '#description' => $query_field_description,
91
    '#value' => $query_field_default_value,
92
    // '#description' => $query_field_description,
93 93ae67cc Patrick Plitzner
    '#autocomplete_path' => 'cdm_dataportal/taxonSearch/autocomplete'
94 6657531f Andreas Kohlbecker
  );
95
96 93ae67cc Patrick Plitzner
    $form['search'] = array(
97 6657531f Andreas Kohlbecker
    '#weight' => 3,
98
    '#tree' => TRUE,
99 7b4c053d Andreas Kohlbecker
    // '#type' => $advanced_form ? 'fieldset': 'hidden',
100 6657531f Andreas Kohlbecker
    '#title' => t('Options'),
101
  );
102
103
  // Clean URL get forms breaks if we don't give it a 'q'.
104
  if (!(bool) variable_get('clean_url', '0')) {
105
    $form['search']['q'] = array(
106
      '#type' => 'hidden',
107
      '#value' => $action_path,
108
      '#name' => 'q',
109
    );
110
  }
111
112
  $form['submit'] = array(
113
    '#weight' => 5,
114
    '#type' => 'submit',
115
    '#name' => '',
116
    '#value' => t('Search'),
117
  );
118
119
  return $form;
120
}
121
122 6e842c77 Patrick Plitzner
function cdm_dataportal_taxon_search_autocomplete($string) {
123 93ae67cc Patrick Plitzner
  $matches = array();
124
125
  $queryParams = array();
126
  $queryParams['query'] = $string."*";
127
  $queryParams['pageNumber'] = '0';
128
  $queryParams['pageSize'] = '100';
129
  $queryParams['doTaxa'] = true;
130
  $queryParams['doSynonyms'] = true;
131
  $queryParams['doMisappliedNames'] = true;
132
  $queryParams['doTaxaByCommonNames'] = true;
133
134
  $search_results = cdm_ws_get(CDM_WS_TAXON_SEARCH, NULL, queryString($queryParams));
135
  foreach($search_results->records as $record){
136
      $titleCache = $record->entity->titleCache;
137 01fff74a Andreas Kohlbecker
      preg_match('/(.*) sec.*/', $titleCache, $trimmedTitle); //remove sec reference
138 93ae67cc Patrick Plitzner
      $trimmedTitle = trim($trimmedTitle[1]);
139
      $matches[$trimmedTitle] = $trimmedTitle;
140
  }
141
  drupal_json_output($matches);
142
}
143
144
145
  /**
146 dbf4b8a8 Andreas Kohlbecker
 * Creates a search form for searching on taxa.
147
 *
148 7b4c053d Andreas Kohlbecker
 * If advanced $advanced_form id TRUE the form will offer additional choices
149 dbf4b8a8 Andreas Kohlbecker
 *
150 73874c48 Andreas Kohlbecker
 * @param array $form
151 7b4c053d Andreas Kohlbecker
 *   A drupal form array
152 73874c48 Andreas Kohlbecker
 * @param array $form_state
153 7b4c053d Andreas Kohlbecker
 *   The drupal form state passed as reference
154
 * @param bool $advanced_form
155 73874c48 Andreas Kohlbecker
 *   default is FALSE
156 7b4c053d Andreas Kohlbecker
 * @param bool $classification_select
157 01fff74a Andreas Kohlbecker
 *   set TRUE to offer a classification selector in the form - default is FALSE
158 73874c48 Andreas Kohlbecker
 *   if only available in the advanced mode
159 dbf4b8a8 Andreas Kohlbecker
 *
160 7b4c053d Andreas Kohlbecker
 * @return array
161 dbf4b8a8 Andreas Kohlbecker
 *   the form array
162 6657531f Andreas Kohlbecker
 */
163 7b4c053d Andreas Kohlbecker
function cdm_dataportal_search_taxon_form($form, &$form_state, $advanced_form = FALSE, $classification_select = TRUE) {
164 6657531f Andreas Kohlbecker
165
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
166
167 7b4c053d Andreas Kohlbecker
  if ($advanced_form || variable_get(SIMPLE_SEARCH_USE_LUCENE_BACKEND, FALSE)) {
168 2d0d855a Andreas Kohlbecker
    $search_service_endpoint = CDM_WS_PORTAL_TAXON_SEARCH;
169 7b4c053d Andreas Kohlbecker
  }
170
  else {
171 2d0d855a Andreas Kohlbecker
    $search_service_endpoint = CDM_WS_PORTAL_TAXON_FIND;
172
  }
173
174
  $form = cdm_dataportal_search_form_prepare(
175
    'cdm_dataportal/search/results/taxon',
176
    $search_service_endpoint,
177
    $query_field_default_value,
178
    t('Enter the name or part of a name you wish to search for.
179 20624f5a Andreas Kohlbecker
      The asterisk  character * can be used as wildcard, but must not be used as first character.'),
180 73874c48 Andreas Kohlbecker
      NULL
181 2d0d855a Andreas Kohlbecker
  );
182 cd64df61 Andreas Kohlbecker
183 7b4c053d Andreas Kohlbecker
  if (!$advanced_form) {
184 6657531f Andreas Kohlbecker
    $form['query']['#size'] = 20;
185
  }
186
187
  $form['search']['pageSize'] = array(
188
    '#weight' => -1,
189
    '#type' => 'hidden',
190
    '#value' => variable_get('cdm_dataportal_search_items_on_page', 25),
191
  );
192
193
  $form['search']['pageNumber'] = array(
194
    '#weight' => -1,
195
    '#type' => 'hidden',
196
    '#value' => 0,
197
  );
198
199 7b4c053d Andreas Kohlbecker
  $search_taxa_mode_settings = get_array_variable_merged(
200
    CDM_SEARCH_TAXA_MODE,
201
    CDM_SEARCH_TAXA_MODE_DEFAULT
202
  );
203
  $preset_do_taxa = $search_taxa_mode_settings['doTaxa'] !== 0;
204
  $preset_do_synonyms = $search_taxa_mode_settings['doSynonyms'] !== 0;
205
  $preset_do_taxa_by_common_names = $search_taxa_mode_settings['doTaxaByCommonNames'] !== 0;
206
  $preset_do_misapplied_names = $search_taxa_mode_settings['doMisappliedNames'] !== 0;
207 6657531f Andreas Kohlbecker
208 7b4c053d Andreas Kohlbecker
  if ($advanced_form) {
209 642b323b Andreas Kohlbecker
210 6280e639 Andreas Kohlbecker
    // --- ADVANCED SEARCH FORM ---
211 61b6ee11 Andreas Kohlbecker
    //
212 dbf4b8a8 Andreas Kohlbecker
213 6657531f Andreas Kohlbecker
    // Get presets from settings.
214 7663cd0b Andreas Kohlbecker
    $preset_classification_uuid = get_current_classification_uuid();
215 6657531f Andreas Kohlbecker
216
    // Overwrite presets by user choice stored in session.
217 6280e639 Andreas Kohlbecker
    if (isset($_SESSION['cdm']['search'])) {
218 7b4c053d Andreas Kohlbecker
      $preset_do_taxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
219
      $preset_do_synonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
220
      $preset_do_misapplied_names = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
221
      $preset_do_taxa_by_common_names = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
222 61b6ee11 Andreas Kohlbecker
      if (isset($_SESSION['cdm']['search']['tree'])) {
223
        $preset_classification_uuid = $_SESSION['cdm']['search']['tree'];
224
      }
225 6657531f Andreas Kohlbecker
    }
226 dbf4b8a8 Andreas Kohlbecker
227 7b4c053d Andreas Kohlbecker
    if ($classification_select === TRUE) {
228 f56b1626 Andreas Kohlbecker
      $form['search']['tree'] = array(
229 642b323b Andreas Kohlbecker
        '#title' => t('Classification'),
230
        '#weight' => 1,
231
        '#type' => 'select',
232 7663cd0b Andreas Kohlbecker
        '#default_value' => get_current_classification_uuid(),
233 f56b1626 Andreas Kohlbecker
        '#options' => cdm_get_taxontrees_as_options(TRUE),
234 8ae3cfe3 Andreas Kohlbecker
        '#description' => t('A filter to limit the search to a specific classification. Choosing <em>--- ALL ---</em> will disable this filter.'),
235 dbf4b8a8 Andreas Kohlbecker
      );
236 632a697a Andreas Kohlbecker
    }
237 dbf4b8a8 Andreas Kohlbecker
238 6657531f Andreas Kohlbecker
    // General search parameters.
239
    $form['search']['doTaxa'] = array(
240
      '#weight' => 2,
241
      '#type' => 'checkbox',
242 2f564f8e Andreas Kohlbecker
      '#title' => t('Search for') . ' ' . t('accepted taxa'),
243 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_taxa,
244 6657531f Andreas Kohlbecker
    );
245
    $form['search']['doSynonyms'] = array(
246
      '#weight' => 3,
247
      '#type' => 'checkbox',
248 2f564f8e Andreas Kohlbecker
      '#title' => t('Search for') . ' ' . t('synonyms'),
249 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_synonyms,
250 6657531f Andreas Kohlbecker
    );
251
    $form['search']['doMisappliedNames'] = array(
252
      '#weight' => 4,
253
      '#type' => 'checkbox',
254 2f564f8e Andreas Kohlbecker
      '#title' => t('Search for') . ' ' . t('misapplied names'),
255 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_misapplied_names,
256 6657531f Andreas Kohlbecker
    );
257
    $form['search']['doTaxaByCommonNames'] = array(
258
      '#weight' => 5,
259
      '#type' => 'checkbox',
260 2f564f8e Andreas Kohlbecker
      '#title' => t('Search for') . ' ' . t('common names'),
261 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_taxa_by_common_names,
262 6657531f Andreas Kohlbecker
    );
263
264 7b4c053d Andreas Kohlbecker
    $area_term_dtos = cdm_ws_fetch_all(
265
      CDM_WS_DESCRIPTION_NAMEDAREAS_IN_USE,
266
      array('includeAllParents' => 'true')
267
    );
268 446cffc5 Andreas Kohlbecker
269 86424a78 Andreas Kohlbecker
    // create map: term_uuid => term
270 41c6dd13 Andreas Kohlbecker
    $term_map = array();
271 7b4c053d Andreas Kohlbecker
    foreach ($area_term_dtos as $term_dto) {
272 41c6dd13 Andreas Kohlbecker
      $term_map[$term_dto->uuid] = $term_dto;
273 632a697a Andreas Kohlbecker
    }
274 41c6dd13 Andreas Kohlbecker
275 632a697a Andreas Kohlbecker
    $term_tree = array();
276 41c6dd13 Andreas Kohlbecker
    // mixed_vocabularies will contain the uuid vocabularies which
277 86424a78 Andreas Kohlbecker
    // also contain terms of foreign vocabularies due to the term
278 41c6dd13 Andreas Kohlbecker
    // hierarchy
279
    $mixed_vocabularies = array();
280
281
    // Build hierarchy of the terms regardless of the vocabulary.
282 a783afbc Andreas Kohlbecker
    foreach ($term_map as $term_dto) {
283
      if (!empty($term_dto->partOfUuid)) {
284
        // Children.
285
        $parent =& $term_map[$term_dto->partOfUuid];
286
        if ($parent) {
287
          if (!isset($parent->children)) {
288
            $parent->children = array();
289 446cffc5 Andreas Kohlbecker
          }
290 a783afbc Andreas Kohlbecker
          $parent->children[$term_dto->uuid] = $term_dto;
291
          if ($parent->vocabularyUuid != $term_dto->vocabularyUuid) {
292
            $mixed_vocabularies[$parent->vocabularyUuid] = $parent->vocabularyUuid;
293 41c6dd13 Andreas Kohlbecker
          }
294 632a697a Andreas Kohlbecker
        }
295 446cffc5 Andreas Kohlbecker
      }
296 a783afbc Andreas Kohlbecker
      else {
297
        // group root nodes by vocabulary
298
        if (!isset($term_tree[$term_dto->vocabularyUuid])) {
299
          $term_tree[$term_dto->vocabularyUuid] = array();
300
        }
301
        $term_tree[$term_dto->vocabularyUuid][$term_dto->uuid] = $term_dto;
302
      }
303
    }
304
305 072122ee Andreas Kohlbecker
    $show_area_filter = ! variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '');
306 446cffc5 Andreas Kohlbecker
307 072122ee Andreas Kohlbecker
    if($show_area_filter){
308
      drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/search_area_filter.js');
309
310
      drupal_add_js('jQuery(document).ready(function() {
311 446cffc5 Andreas Kohlbecker
        jQuery(\'#edit-search-areas\').search_area_filter(\'#edit-search-areas-areas-filter\');
312
      });
313
      ', array('type' => 'inline'));
314
315 072122ee Andreas Kohlbecker
      $form['search']['areas'] = array(
316
        '#type' => 'fieldset',
317
        '#title' => t('Filter by distribution areas'),
318
        '#description' => t('The search will return taxa having distribution
319 722b1688 Andreas Kohlbecker
        information for at least one of the selected areas.') . ' '
320 072122ee Andreas Kohlbecker
          .(count($term_tree) > 1 ? t('The areas are grouped
321 722b1688 Andreas Kohlbecker
        by the vocabularies to which the highest level areas belong.') : ''),
322 632a697a Andreas Kohlbecker
      );
323 072122ee Andreas Kohlbecker
      $form['search']['areas']['areas_filter'] = array(
324
        '#type' => 'textfield',
325
        '#description' => t('Type to filter the areas listed below.'),
326
      );
327
      $vocab_cnt = 0;
328
      $areas_defaults = array();
329
      if (isset($_SESSION['cdm']['search']['area'])) {
330
        $areas_defaults = explode(',', $_SESSION['cdm']['search']['area']);
331
      }
332
      foreach ($term_tree as $vocab_uuid => $term_dto_tree) {
333
        $vocabulary = cdm_ws_get(CDM_WS_TERMVOCABULARY, array($vocab_uuid));
334
        $areas_options = term_tree_as_options($term_dto_tree);
335
        $form['search']['areas']['area'][$vocab_cnt++] = array(
336
          '#prefix' => '<strong>' . $vocabulary->representation_L10n
337
            . (isset($mixed_vocabularies[$vocab_uuid]) ? ' <span title="Contains terms of at least one other area vocabulary.">(' . t('mixed') . ')</span>': '')
338
            . '</strong>',
339
          '#type' => 'checkboxes',
340
          '#default_value' => $areas_defaults,
341
          '#options' => $areas_options,
342
        );
343
      }
344 632a697a Andreas Kohlbecker
    }
345 446cffc5 Andreas Kohlbecker
346 7b4c053d Andreas Kohlbecker
  }
347
  else {
348 61b6ee11 Andreas Kohlbecker
    // --- SIMPLE SEARCH FORM ---
349
    //
350
351 6657531f Andreas Kohlbecker
    // Overwrite presets by user choice stored in session.
352 6280e639 Andreas Kohlbecker
    if (isset($_SESSION['cdm']['search'])) {
353 7b4c053d Andreas Kohlbecker
      $preset_do_misapplied_names = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
354 6657531f Andreas Kohlbecker
    }
355
356
    $form['search']['doTaxa'] = array(
357
      '#weight' => -2,
358
      '#type' => 'hidden',
359 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_taxa,
360 6657531f Andreas Kohlbecker
    );
361
    $form['search']['doSynonyms'] = array(
362
      '#weight' => -3,
363
      '#type' => 'hidden',
364 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_synonyms,
365 6657531f Andreas Kohlbecker
    );
366
    $form['search']['doMisappliedNames'] = array(
367
      '#weight' => -4,
368
      '#type' => 'checkbox',
369
      '#title' => t('Misapplied names'),
370 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_misapplied_names,
371 6657531f Andreas Kohlbecker
    );
372
    $form['search']['doTaxaByCommonNames'] = array(
373
      '#weight' => -5,
374
      '#type' => 'hidden',
375 7b4c053d Andreas Kohlbecker
      '#value' => $preset_do_taxa_by_common_names,
376 6657531f Andreas Kohlbecker
    );
377
  }
378
379
  return $form;
380
}
381 632a697a Andreas Kohlbecker
382 6657531f Andreas Kohlbecker
/**
383 7b4c053d Andreas Kohlbecker
 * Wrapper function for cdm_dataportal_search_taxon_form().
384
 *
385
 * This function makes ot possible possible to just pass the
386
 * correct $form_id 'cdm_dataportal_search_taxon_form_advanced' to
387
 * drupal_get_form like:
388
 * drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
389
 *
390
 * @param array $form
391
 *   A drupal form array
392
 * @param array $form_state
393
 *   The drupal form state passed as reference
394
 *
395
 * @return array
396
 *   The form array
397 6657531f Andreas Kohlbecker
 */
398
function cdm_dataportal_search_taxon_form_advanced($form, &$form_state) {
399
  return cdm_dataportal_search_taxon_form($form, $form_state, TRUE);
400
}
401
402
/**
403 7b4c053d Andreas Kohlbecker
 * Form for searching taxa by the findByDescriptionElementFullText rest service.
404 6657531f Andreas Kohlbecker
 */
405
function cdm_dataportal_search_taxon_by_description_form() {
406
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
407
408
  $form = cdm_dataportal_search_form_prepare(
409 632a697a Andreas Kohlbecker
    'cdm_dataportal/search/results/taxon',
410
    CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT,
411
    $query_field_default_value,
412
    t("Enter the text you wish to search for. The asterisk character * can be
413 20624f5a Andreas Kohlbecker
        used as wildcard, but must not be used as first character. Terms can be combined with 'AND'. To search for a
414 36af8b60 Andreas Kohlbecker
        full phrase enclose the terms in parentheses. For more syntactical
415 7b4c053d Andreas Kohlbecker
        options please refer to the !link.",
416
      array(
417
        '!link' => l(
418
          t('Apache Lucene - Query Parser Syntax'),
419
          'http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html', array(
420
            'attributes' => array(
421
              'absolute' => TRUE,
422
              'html' => TRUE),
423
          )
424
        ),
425
      )
426
    ),
427 632a697a Andreas Kohlbecker
    NULL
428
  );
429 6657531f Andreas Kohlbecker
430
  $form['search']['tree'] = array(
431
    '#weight' => -1,
432
    '#type' => 'hidden',
433 7663cd0b Andreas Kohlbecker
    '#value' => get_current_classification_uuid(),
434 6657531f Andreas Kohlbecker
  );
435
436
  $form['search']['hl'] = array(
437
    '#weight' => -1,
438
    '#type' => 'hidden',
439
    '#value' => 1,
440
  );
441
442 b58fcc1f Andreas Kohlbecker
  // Only available to admins:
443 6657531f Andreas Kohlbecker
  if (!isset($_SESSION['cdm']['search']['clazz'])) {
444
    $_SESSION['cdm']['search']['clazz'] = '';
445
  }
446
  if (module_exists("user") && user_access('administer')) {
447
    $form['search']['clazz'] = array(
448
      '#type' => 'select',
449 b58fcc1f Andreas Kohlbecker
      '#title' => t('Limit to description item type'),
450 6657531f Andreas Kohlbecker
      '#default_value' => $_SESSION['cdm']['search']['clazz'],
451
      '#options' => cdm_descriptionElementTypes_as_option(TRUE),
452
    );
453
  }
454
455 7b4c053d Andreas Kohlbecker
  $profile_feature_tree = get_profile_feature_tree();
456
  $feature_options = _featureTree_nodes_as_feature_options($profile_feature_tree->root);
457 6657531f Andreas Kohlbecker
  if (isset($_SESSION['cdm']['search']['features'])) {
458
    $form['search']['features'] = array(
459
      '#type' => 'checkboxes',
460
      '#title' => t('Limit to selected features'),
461
      '#default_value' => $_SESSION['cdm']['search']['features'],
462
      '#options' => $feature_options,
463
    );
464
  }
465
  else {
466
    $form['search']['features'] = array(
467
      '#type' => 'checkboxes',
468
      '#title' => t('Limit to selected features'),
469
      '#options' => $feature_options,
470
    );
471
  }
472
  return $form;
473
}
474
475
/**
476 7b4c053d Andreas Kohlbecker
 * Processes the query parameters of the search form.
477
 *
478
 * Reads the query parameters from $_REQUEST and modifies and adds additional
479
 * query parameters if nessecary.
480 dbf4b8a8 Andreas Kohlbecker
 *
481
 *  - Filters $_REQUEST by a list of valid request parameters
482
 *  - modifies geographic_range parameters
483
 *  - adds taxon tree uuid if it is missing and if it should not be
484
 *    ignored (parameter value = 'IGNORE')
485
 *  - and more
486
 *
487
 *
488 7b4c053d Andreas Kohlbecker
 * @return array
489 dbf4b8a8 Andreas Kohlbecker
 *   the processed request parameters submitted by the search form and
490
 *   also stores them in $_SESSION['cdm']['search']
491 6657531f Andreas Kohlbecker
 */
492 f48cede0 Andreas Kohlbecker
function cdm_dataportal_search_form_request()
493
{
494 61b6ee11 Andreas Kohlbecker
495 6657531f Andreas Kohlbecker
  $form_params = array();
496 61b6ee11 Andreas Kohlbecker
497 34dd7be9 Andreas Kohlbecker
  if (isset($_REQUEST['search']) && is_array($_REQUEST['search'])) {
498 6657531f Andreas Kohlbecker
    array_deep_copy($_REQUEST['search'], $form_params);
499
  }
500 34dd7be9 Andreas Kohlbecker
501
  if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
502
    $form_params = array_merge($form_params, $_REQUEST['pager']);
503
  }
504
505 6657531f Andreas Kohlbecker
  $form_params['query'] = trim($_REQUEST['query']);
506
507
  // --- handle geographic range
508
  // Split of geographic range.
509 44756fcd Andreas Kohlbecker
  unset($form_params['areas']);
510 072122ee Andreas Kohlbecker
511 9bbe3bcd Andreas Kohlbecker
  $area_filter_preset = null;
512 f48cede0 Andreas Kohlbecker
  if (variable_get(CDM_SEARCH_AREA_FILTER_PRESET, '')) {
513
    $area_filter_preset = explode(',', variable_get(CDM_SEARCH_AREA_FILTER_PRESET, ''));
514
  }
515 072122ee Andreas Kohlbecker
516 30399ffa Andreas Kohlbecker
  $area_uuids = array();
517 072122ee Andreas Kohlbecker
  if($area_filter_preset){
518
    $area_uuids = $area_filter_preset;
519
  }
520
  elseif (isset($_REQUEST['search']['areas']['area']) && is_array($_REQUEST['search']['areas']['area'])) {
521 7b4c053d Andreas Kohlbecker
    foreach ($_REQUEST['search']['areas']['area'] as $areas) {
522
      $area_uuids = array_merge($area_uuids, $areas);
523
    }
524 072122ee Andreas Kohlbecker
  }
525 30399ffa Andreas Kohlbecker
  if(count($area_uuids) > 0){
526 7b4c053d Andreas Kohlbecker
    $form_params['area'] = implode(',', $area_uuids);
527 6657531f Andreas Kohlbecker
  }
528
529 7b4c053d Andreas Kohlbecker
  // Simple search will not submit a 'tree' query parameter,
530
  // so we add it here from what is stored in the session unless
531 90a6166e Andreas Kohlbecker
  // SIMPLE_SEARCH_IGNORE_CLASSIFICATION is checked in the settings.
532
  if (!isset($form_params['tree']) && !variable_get(SIMPLE_SEARCH_IGNORE_CLASSIFICATION, 0)) {
533 7663cd0b Andreas Kohlbecker
    $form_params['tree'] = get_current_classification_uuid();
534 61b6ee11 Andreas Kohlbecker
  }
535 7b4c053d Andreas Kohlbecker
  // If the 'NONE' classification has been chosen (adanced search)
536
  // delete the tree information to avoid unknown uuid exceptions in the
537
  // cdm service.
538
  if (isset($form_params['tree'])
539
    && ($form_params['tree'] == 'NONE' || !is_uuid($form_params['tree']))
540
  ) {
541
    // $form_params['ignore_classification'] =  TRUE;
542 dbf4b8a8 Andreas Kohlbecker
    unset($form_params['tree']);
543
  }
544 7b4c053d Andreas Kohlbecker
  // else {
545
  //   $form_params['ignore_classification'] =  NULL;
546
  // }
547 dbf4b8a8 Andreas Kohlbecker
548 6657531f Andreas Kohlbecker
  // Store in session.
549
  $_SESSION['cdm']['search'] = $form_params;
550
551
  return $form_params;
552
}
553
554 61b6ee11 Andreas Kohlbecker
/**
555 44756fcd Andreas Kohlbecker
 * Provides the classification to which the last search has been limited to..
556 61b6ee11 Andreas Kohlbecker
 *
557 7b4c053d Andreas Kohlbecker
 * This function should only be used after the cdm_dataportal_search_execute()
558
 * handler has been run, otherwise it will return the infomation from the last
559
 * search executed. The information is retrieved from
560 61b6ee11 Andreas Kohlbecker
 * the $_SESSION variable:  $_SESSION['cdm']['search']['tree']
561
 *
562 7b4c053d Andreas Kohlbecker
 * @return object
563
 *   the CDM classification instance which has been used a filter for the
564
 *   last processed search
565
 *   or NULL, it it was on all classifications
566 61b6ee11 Andreas Kohlbecker
 */
567
function cdm_dataportal_searched_in_classification() {
568
569
  $classification = &drupal_static(__FUNCTION__);
570
571
  if (!isset($classification)) {
572
    if (isset($_SESSION['cdm']['search']['tree'])) {
573
      $classification = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY, ($_SESSION['cdm']['search']['tree']));
574 7b4c053d Andreas Kohlbecker
    }
575
    else {
576 61b6ee11 Andreas Kohlbecker
      $classification = FALSE;
577
    }
578
  }
579
580 7b4c053d Andreas Kohlbecker
  return $classification !== FALSE ? $classification : NULL;
581 61b6ee11 Andreas Kohlbecker
}
582
583 6657531f Andreas Kohlbecker
/**
584
 * Removes Drupal internal form elements from query.
585
 */
586
function cdm_dataportal_search_process($form, &$form_state) {
587
  unset($form['form_id']);
588
  unset($form['form_token']);
589
  return $form;
590
}
591
592
/**
593 dbf4b8a8 Andreas Kohlbecker
 * Sends a search request at the cdm web server.
594
 *
595
 * The parameters to build the query are taken obtained by calling
596
 * cdm_dataportal_search_form_request() which reads the query parameters
597
 * from $_REQUEST and add additional query parameters if nessecary.
598
 *
599
 * @see cdm_dataportal_search_form_request()
600 6657531f Andreas Kohlbecker
 */
601
function cdm_dataportal_search_execute() {
602
603
  // Store as last search in session.
604
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
605
606
  // Validate the search webservice parameter:
607 7b4c053d Andreas Kohlbecker
  if (!isset($_REQUEST['ws'])) {
608
    drupal_set_message(
609 78ec4159 Andreas Kohlbecker
      t("Invalid search webservice parameter 'ws' given"), 'warning'
610 7b4c053d Andreas Kohlbecker
    );
611 61b6ee11 Andreas Kohlbecker
    return NULL;
612
  }
613 7b4c053d Andreas Kohlbecker
  if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {
614 6657531f Andreas Kohlbecker
    // Endpoint is unknown.
615 7b4c053d Andreas Kohlbecker
    drupal_set_message(
616 78ec4159 Andreas Kohlbecker
      t("Invalid search webservice parameter 'ws' given"), 'warning'
617 7b4c053d Andreas Kohlbecker
    );
618 6657531f Andreas Kohlbecker
    return NULL;
619
  }
620
621 7b4c053d Andreas Kohlbecker
  // Read the query parameters from $_REQUEST and add additional query
622
  // parameters if necessary.
623 6657531f Andreas Kohlbecker
  $request_params = cdm_dataportal_search_form_request();
624 dbf4b8a8 Andreas Kohlbecker
625 446cffc5 Andreas Kohlbecker
  $taxon_pager = cdm_ws_get($_REQUEST['ws'], NULL, queryString($request_params));
626
627
  return $taxon_pager;
628
}
629
630 7b4c053d Andreas Kohlbecker
/**
631
 * Transforms the termDTO tree into options array.
632
 *
633
 *   TermDto:
634
 *      - partOfUuid:
635
 *      - representation_L10n:
636
 *      - representation_L10n_abbreviatedLabel:
637
 *      - uuid:
638
 *      - vocabularyUuid:
639
 *      - children: array of TermDto
640
 *
641
 * The options array is suitable for drupal form API elements that
642
 * allow multiple choices.
643
 * @see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#options
644
 *
645
 * @param array $term_dto_tree
646
 *   a hierarchic array of CDM TermDto instances, with additional
647
 * 'children' field:
648
 * @param array $options
649
 *   Internally used for recursive calls
650
 * @param string $prefix
651
 *   Internally used for recursive calls
652
 *
653
 * @return array
654
 *   the terms in an array as options for a form element that allows
655
 *   multiple choices.
656
 */
657
function term_tree_as_options($term_dto_tree, &$options = array(), $prefix = '') {
658
659
  foreach ($term_dto_tree as $uuid => $dto) {
660
    $label = $prefix . '<span class="child-label">'
661
      .  $dto->representation_L10n
662 35619936 Andreas Kohlbecker
      . '</span><span class="child-label-abbreviated"> (' . $dto->representation_L10n_abbreviatedLabel . ')</span>';
663 7b4c053d Andreas Kohlbecker
    $options[$uuid] = $label;
664
    if (isset($dto->children) && is_array($dto->children)) {
665 0d2c9bab Andreas Kohlbecker
      uasort($dto->children, 'compare_terms_by_representationL10n');
666 7b4c053d Andreas Kohlbecker
      term_tree_as_options(
667
        $dto->children,
668
        $options, $prefix
669
          . '<span data-cdm-parent="' . $uuid . '" class="parent"></span>');
670 446cffc5 Andreas Kohlbecker
    }
671 7b4c053d Andreas Kohlbecker
  }
672 6657531f Andreas Kohlbecker
673 7b4c053d Andreas Kohlbecker
  return $options;
674 6657531f Andreas Kohlbecker
}