Project

General

Profile

Download (16.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
    CDM_WS_PORTAL_TAXON_SEARCH => "cdm_dataportal/search",
26
    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
 * @param string $query_field_description
48
 * @param string $process
49
 *   The value for #process, if NULL (default), 'cdm_dataportal_search_process'
50
 *   is used.
51
 *
52
 * @return array
53
 *   The prepared form array.
54
 */
55
function cdm_dataportal_search_form_prepare($action_path, $search_webservice, $query_field_default_value, $query_field_description, $process = NULL) {
56
  if ($process == NULL) {
57
    $process = 'cdm_dataportal_search_process';
58
  }
59
  $form['#method'] = 'get';
60
  /*
61
  $form['#process'] = array(
62
    $process => array(),
63
  );
64
  */
65
  $form['#action'] = url($action_path, array(
66
    'absolute' => TRUE,
67
  ));
68

    
69
  $form['ws'] = array(
70
    '#type' => 'hidden',
71
    '#value' => $search_webservice,
72
    '#name' => 'ws',
73
  );
74

    
75
  $form['query'] = array(
76
    '#weight' => 0,
77
    '#type' => 'textfield',
78
    '#size' => 68,
79
  // Comment @WA: this causes the description to display also when hovering over
80
  // the textfield.
81
  // I think this should be removed, although it is currently there in
82
  // D5 portals.
83
    '#attributes' => array(
84
      'title' => $query_field_description,
85
    ),
86
    '#description' => $query_field_description,
87
    '#value' => $query_field_default_value,
88
    // '#description' => $query_field_description,
89
  );
90

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

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

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

    
114
  return $form;
115
}
116

    
117
/**
118
 * Creates a search form for searching on taxa.
119
 *
120
 * If advanced $advancedForm id TRUE the form will offer additinal choices
121
 *
122
 * @param unknown_type $form
123
 * @param unknown_type $form_state
124
 * @param bool $advancedForm
125
 *    default is FALSE
126
 * @param bool $classificationSelect
127
 *    set TRUE to offer a classifiaction selector in the form - default is FALSE
128
 *    if only available in the advanced mode
129
 *
130
 * @return
131
 *   the form array
132
 *
133
 */
134
function cdm_dataportal_search_taxon_form($form, &$form_state, $advancedForm = FALSE, $classificationSelect = TRUE) {
135
  global $theme_key;
136

    
137
  $tdwg_level_select = (isset($_SESSION['cdm']['search']['tdwg_level_select']) ? $_SESSION['cdm']['search']['tdwg_level_select'] : 2);
138
  $selected_areas = (isset($_SESSION['cdm']['search']['area']) ? $_SESSION['cdm']['search']['area'] : FALSE);
139
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
140

    
141

    
142

    
143
  if ($advancedForm) {
144
    $form = cdm_dataportal_search_form_prepare('cdm_dataportal/search/results/taxon', CDM_WS_PORTAL_TAXON_SEARCH, $query_field_default_value, t('Enter the name or part of a name you wish to search for. The asterisk  character * can always be used as wildcard.'), NULL);
145
  } else {
146
    // using CDM_WS_PORTAL_TAXON_SEARCH in all cases, for testing or the origial CDM_WS_PORTAL_TAXON_FIND for production
147
    $form = cdm_dataportal_search_form_prepare('cdm_dataportal/search/results/taxon', CDM_WS_PORTAL_TAXON_FIND, $query_field_default_value, t('Enter the name or part of a name you wish to search for. The asterisk  character * can always be used as wildcard.'), NULL);
148
//     $form = cdm_dataportal_search_form_prepare('cdm_dataportal/search/results/taxon', CDM_WS_PORTAL_TAXON_SEARCH, $query_field_default_value, t('Enter the name or part of a name you wish to search for. The asterisk  character * can always be used as wildcard.'), NULL);
149

    
150
    $form['query']['#size'] = 20;
151
  }
152

    
153
  $form['search']['pageSize'] = array(
154
    '#weight' => -1,
155
    '#type' => 'hidden',
156
    '#value' => variable_get('cdm_dataportal_search_items_on_page', 25),
157
  );
158

    
159
  $form['search']['pageNumber'] = array(
160
    '#weight' => -1,
161
    '#type' => 'hidden',
162
    '#value' => 0,
163
  );
164

    
165
  $search_taxa_mode_settings = get_array_variable_merged(CDM_SEARCH_TAXA_MODE, CDM_SEARCH_TAXA_MODE_DEFAULT);
166
  $preset_doTaxa = $search_taxa_mode_settings['doTaxa'] !== 0;
167
  $preset_doSynonyms = $search_taxa_mode_settings['doSynonyms'] !== 0;
168
  $preset_doTaxaByCommonNames = $search_taxa_mode_settings['doTaxaByCommonNames'] !== 0;
169
  $preset_doMisappliedNames = $search_taxa_mode_settings['doMisappliedNames'] !== 0;
170

    
171
  if ($advancedForm) {
172

    
173
    // --- ADVANCED SEARCH FORM ---
174
    //
175

    
176
    // Get presets from settings.
177
    $preset_classification_uuid = get_taxonomictree_uuid_selected();
178

    
179
    // Overwrite presets by user choice stored in session.
180
    if (isset($_SESSION['cdm']['search'])) {
181
      $preset_doTaxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
182
      $preset_doSynonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
183
      $preset_doMisappliedNames = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
184
      $preset_doTaxaByCommonNames = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
185
      if (isset($_SESSION['cdm']['search']['tree'])) {
186
        $preset_classification_uuid = $_SESSION['cdm']['search']['tree'];
187
      }
188
    }
189

    
190

    
191
   if ($classificationSelect === TRUE) {
192
      $form['search']['tree'] = array(
193
        '#title' => t('Classification'),
194
        '#weight' => 1,
195
        '#type' => 'select',
196
        '#default_value' => get_taxonomictree_uuid_selected(),
197
        '#options' => cdm_get_taxontrees_as_options(TRUE),
198
        '#description' => t('A filter to limit the search to a specific classification.')
199
      );
200
   }
201

    
202
    // General search parameters.
203
    $form['search']['doTaxa'] = array(
204
      '#weight' => 2,
205
      '#type' => 'checkbox',
206
      '#title' => t('Search for accepted taxa'),
207
      '#value' => $preset_doTaxa,
208
    );
209
    $form['search']['doSynonyms'] = array(
210
      '#weight' => 3,
211
      '#type' => 'checkbox',
212
      '#title' => t('Search for synonyms'),
213
      '#value' => $preset_doSynonyms,
214
    );
215
    $form['search']['doMisappliedNames'] = array(
216
      '#weight' => 4,
217
      '#type' => 'checkbox',
218
      '#title' => t('Search for misapplied names'),
219
      '#value' => $preset_doMisappliedNames,
220
    );
221
    $form['search']['doTaxaByCommonNames'] = array(
222
      '#weight' => 5,
223
      '#type' => 'checkbox',
224
      '#title' => t('Search for common names'),
225
      '#value' => $preset_doTaxaByCommonNames
226
    );
227

    
228
    $areas_options = cdm_terms_as_options(cdm_ws_fetch_all(CDM_WS_DESCRIPTION_NAMEDAREAS_IN_USE));
229
    $areas_defaults = array();
230
    if(isset($_SESSION['cdm']['search']['area'])){
231
      $areas_defaults = explode(',', $_SESSION['cdm']['search']['area']);
232
    }
233
    $form['search']['area'] = array(
234
      '#type' => 'checkboxes',
235
      '#title' => t('Filter by distribution areas'),
236
      '#default_value' =>  $areas_defaults,
237
      '#options' => $areas_options,
238
      '#description' => t('Check one or multiple areas to filter by distribution.')
239
    );
240

    
241
  } else {
242
    // --- SIMPLE SEARCH FORM ---
243
    //
244

    
245
    // Overwrite presets by user choice stored in session.
246
    if (isset($_SESSION['cdm']['search'])) {
247
      $preset_doMisappliedNames = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
248
    }
249

    
250
    $form['search']['doTaxa'] = array(
251
      '#weight' => -2,
252
      '#type' => 'hidden',
253
      '#value' => $preset_doTaxa,
254
    );
255
    $form['search']['doSynonyms'] = array(
256
      '#weight' => -3,
257
      '#type' => 'hidden',
258
      '#value' => $preset_doSynonyms,
259
    );
260
    $form['search']['doMisappliedNames'] = array(
261
      '#weight' => -4,
262
      '#type' => 'checkbox',
263
      '#title' => t('Misapplied names'),
264
      '#value' => $preset_doMisappliedNames,
265
    );
266
    $form['search']['doTaxaByCommonNames'] = array(
267
      '#weight' => -5,
268
      '#type' => 'hidden',
269
      '#value' => $preset_doTaxaByCommonNames,
270
    );
271
  }
272

    
273
  return $form;
274
}
275
/**
276
 * @todo Please document this function.
277
 * @see http://drupal.org/node/1354
278
 */
279
function cdm_dataportal_search_taxon_form_advanced($form, &$form_state) {
280
  return cdm_dataportal_search_taxon_form($form, $form_state, TRUE);
281
}
282

    
283
/**
284
 * Search form for the searching taxa by the findByDescriptionElementFullText
285
 * rest service.
286
 */
287
function cdm_dataportal_search_taxon_by_description_form() {
288
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
289

    
290
  $form = cdm_dataportal_search_form_prepare(
291
      'cdm_dataportal/search/results/taxon',
292
      CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT,
293
      $query_field_default_value,
294
      t("Enter the text you wish to search for. The asterisk character * can be
295
        used as wildcard. Terms can be combined with 'AND'. To search for a
296
        full phrase enclose the terms in parentheses. For more syntactial
297
        options please refer to the !link.", array(
298
          '!link' => l(t('Apache Lucene - Query Parser Syntax'), 'http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html', array(
299
             'attributes' => array('absolute' => TRUE, 'html' => TRUE))),
300
        )),
301
      NULL
302
      );
303

    
304
  $form['search']['tree'] = array(
305
    '#weight' => -1,
306
    '#type' => 'hidden',
307
    '#value' => get_taxonomictree_uuid_selected(),
308
  );
309

    
310
  $form['search']['hl'] = array(
311
    '#weight' => -1,
312
    '#type' => 'hidden',
313
    '#value' => 1,
314
  );
315

    
316
  // Only avaiable to admins:
317
  if (!isset($_SESSION['cdm']['search']['clazz'])) {
318
    $_SESSION['cdm']['search']['clazz'] = '';
319
  }
320
  if (module_exists("user") && user_access('administer')) {
321
    $form['search']['clazz'] = array(
322
      '#type' => 'select',
323
      '#title' => t('Limit to DescriptionElement type'),
324
      '#default_value' => $_SESSION['cdm']['search']['clazz'],
325
      '#options' => cdm_descriptionElementTypes_as_option(TRUE),
326
    );
327
  }
328

    
329
  /*
330
  see cdm_get_featureTrees_as_options() ... $treeRepresentation =
331
  $featureTree->titleCache; if(is_array($featureTree->root->childNodes) &&
332
  count($featureTree->root->childNodes) > 0){ // render the hierarchic tree
333
  structure $treeDetails = '<div class="featuretree_structure">'
334
  //.cdm_featureTree_elements_toString($featureTree->root)
335
  .theme('featureTree_hierarchy', $featureTree->uuid) .'</div>'; $form =
336
  array(); $form['featureTree-'.$featureTree->uuid] = array( '#type' =>
337
  'fieldset', '#title' => t('Show details'), '#collapsible' => TRUE,
338
  '#collapsed' => TRUE, );
339
  $form['featureTree-'.$featureTree->uuid]['details'] =
340
  array('#value'=>$treeDetails); $treeRepresentation .= drupal_render($form);
341
  */
342

    
343
  $profile_featureTree = get_profile_featureTree();
344
  $feature_options = _featureTree_nodes_as_feature_options($profile_featureTree->root);
345
  if (isset($_SESSION['cdm']['search']['features'])) {
346
    $form['search']['features'] = array(
347
      '#type' => 'checkboxes',
348
      '#title' => t('Limit to selected features'),
349
      '#default_value' => $_SESSION['cdm']['search']['features'],
350
      '#options' => $feature_options,
351
    );
352
  }
353
  else {
354
    $form['search']['features'] = array(
355
      '#type' => 'checkboxes',
356
      '#title' => t('Limit to selected features'),
357
      '#options' => $feature_options,
358
    );
359
  }
360
  return $form;
361
}
362

    
363
/**
364
 * Reads the query parameters from $_REQUEST and modifies and adds additional query parameters if nessecary.
365
 *
366
 *  - Filters $_REQUEST by a list of valid request parameters
367
 *  - modifies geographic_range parameters
368
 *  - adds taxon tree uuid if it is missing and if it should not be
369
 *    ignored (parameter value = 'IGNORE')
370
 *  - and more
371
 *
372
 *
373
 * @return
374
 *   the processed request parameters submitted by the search form and
375
 *   also stores them in $_SESSION['cdm']['search']
376
 */
377
function cdm_dataportal_search_form_request() {
378

    
379
  $form_params = array();
380

    
381
  if (isset($_REQUEST['search']) && is_array($_REQUEST['search'])) {
382
    array_deep_copy($_REQUEST['search'], $form_params);
383
  }
384

    
385
  if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
386
    $form_params = array_merge($form_params, $_REQUEST['pager']);
387
  }
388

    
389
  $form_params['query'] = trim($_REQUEST['query']);
390

    
391
  // --- handle geographic range
392
  // Split of geographic range.
393
  unset($form_params['area']);
394
  if (isset($_REQUEST['search']['area']) && is_array($_REQUEST['search']['area'])) {
395
    $form_params['area'] = implode(',', $_REQUEST['search']['area']);
396
  }
397

    
398
  // simple search will not submit a 'tree' query parameter, so we add it here from
399
  // what is stored in the session unless 'simple_search_ignore_classification'
400
  // is checked in the settings
401
  if (!isset($form_params['tree']) && !variable_get('simple_search_ignore_classification', 1)) {
402
    $form_params['tree'] = get_taxonomictree_uuid_selected();
403
  }
404
  // if the 'NONE' classification has been chosen (adanced search) delete the tree information
405
  // to avoid unknown uuid exceptions in the cdm service
406
  if (isset($form_params['tree']) && ($form_params['tree'] == 'NONE' || ! is_uuid($form_params['tree']))) {
407
//     $form_params['ignore_classification'] =  TRUE;
408
    unset($form_params['tree']);
409
  }
410
//   else {
411
//     $form_params['ignore_classification'] =  NULL;
412
//   }
413

    
414
  // Store in session.
415
  $_SESSION['cdm']['search'] = $form_params;
416

    
417
  return $form_params;
418
}
419

    
420
/**
421
 * Provides the classification the last search has been run on if any.
422
 *
423
 * This function should only be used after the cdm_dataportal_search_execute() handler has been run,
424
 * otherwise it will return the infomation from the last search executed. The information is retrieved from
425
 * the $_SESSION variable:  $_SESSION['cdm']['search']['tree']
426
 *
427
 * @return
428
 *    the CDM classification instance which has been used a filter for the last processed search
429
 *    or NULL, it it was on all classifications
430
 */
431
function cdm_dataportal_searched_in_classification() {
432

    
433
  $classification = &drupal_static(__FUNCTION__);
434

    
435
  if (!isset($classification)) {
436
    if (isset($_SESSION['cdm']['search']['tree'])) {
437
      $classification = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY, ($_SESSION['cdm']['search']['tree']));
438
    } else {
439
      $classification = FALSE;
440
    }
441
  }
442

    
443
  return $classification !== FALSE ?  $classification : NULL;
444
}
445

    
446
/**
447
 * Removes Drupal internal form elements from query.
448
 */
449
function cdm_dataportal_search_process($form, &$form_state) {
450
  unset($form['form_id']);
451
  unset($form['form_token']);
452
  return $form;
453
}
454

    
455
/**
456
 * Sends a search request at the cdm web server.
457
 *
458
 * The parameters to build the query are taken obtained by calling
459
 * cdm_dataportal_search_form_request() which reads the query parameters
460
 * from $_REQUEST and add additional query parameters if nessecary.
461
 *
462
 * @see cdm_dataportal_search_form_request()
463
 *
464
 */
465
function cdm_dataportal_search_execute() {
466

    
467
  // Store as last search in session.
468
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
469

    
470
  // Validate the search webservice parameter:
471
  if (!isset($_REQUEST['ws'])) {// Check is ws.
472
    // Endpoint is unknown.
473
    drupal_set_message(t('webservice parameter \'ws\' missing'), 'warning');
474
    return NULL;
475
  }
476
  if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {// Check is ws.
477
    // Endpoint is unknown.
478
    drupal_set_message(t('Invalid search webservice parameter  \'ws\' given'), 'warning');
479
    return NULL;
480
  }
481

    
482
  // read the query parameters from $_REQUEST and add additional query parameters if nessecary.
483
  $request_params = cdm_dataportal_search_form_request();
484

    
485
  $taxonPager = cdm_ws_get($_REQUEST['ws'], NULL, queryString($request_params));
486

    
487
  return $taxonPager;
488
}
(8-8/14)