Project

General

Profile

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