Project

General

Profile

Download (15.2 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_FINDBY_DESCRIPTIONELEMENT_FULLTEXT => "cdm_dataportal/search/taxon_by_description",
26
  );
27
  return $form_ws_map[$ws_endpoint];
28
}
29

    
30
/**
31
 * Prepares a form array for a general purpose search form.
32
 *
33
 * The form is used for general purpose search functionality in the
34
 * dataportal. The form returned is populated with all nessecary fields
35
 * for internal processing and has the textfield element $form['query']
36
 * which holds the query term.
37
 * he sub tree array can be extended to contain additional search parameters.
38
 *
39
 * @param string $action_path
40
 *   The Drupal path to be put into the action url to which the form will
41
 *   be submitted.
42
 * @param string $search_webservice
43
 *   The cdm-remote webservice to be used, valid values are defined by
44
 *   the constants: FIXME.
45
 * @param string $query_field_default_value
46
 * @param string $query_field_description
47
 * @param string $process
48
 *   The value for #process, if NULL (default), 'cdm_dataportal_search_process'
49
 *   is used.
50
 *
51
 * @return array
52
 *   The prepared form array.
53
 */
54
function cdm_dataportal_search_form_prepare($action_path, $search_webservice, $query_field_default_value, $query_field_description, $process = NULL) {
55
  if ($process == NULL) {
56
    $process = 'cdm_dataportal_search_process';
57
  }
58
  $form['#method'] = 'get';
59
  /*
60
  $form['#process'] = array(
61
    $process => array(),
62
  );
63
  */
64
  $form['#action'] = url($action_path, array(
65
    'absolute' => TRUE,
66
  ));
67

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

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

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

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

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

    
113
  return $form;
114
}
115

    
116
/**
117
 * @todo document ths function.
118
 */
119
function cdm_dataportal_search_taxon_form($form, &$form_state, $advancedForm = FALSE) {
120
  global $theme_key;
121

    
122
  $tdwg_level_select = (isset($_SESSION['cdm']['search']['tdwg_level_select']) ? $_SESSION['cdm']['search']['tdwg_level_select'] : 2);
123
  $selected_areas = (isset($_SESSION['cdm']['search']['area']) ? $_SESSION['cdm']['search']['area'] : FALSE);
124

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

    
127
  $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);
128

    
129
  if (!$advancedForm) {
130
    $form['query']['#size'] = 20;
131
  }
132

    
133
  $form['search']['tree'] = array(
134
    '#weight' => -1,
135
    '#type' => 'hidden',
136
    '#value' => get_taxonomictree_uuid_selected(),
137
  );
138

    
139
  $form['search']['pageSize'] = array(
140
    '#weight' => -1,
141
    '#type' => 'hidden',
142
    '#value' => variable_get('cdm_dataportal_search_items_on_page', 25),
143
  );
144

    
145
  $form['search']['pageNumber'] = array(
146
    '#weight' => -1,
147
    '#type' => 'hidden',
148
    '#value' => 0,
149
  );
150

    
151
  if ($advancedForm) {
152

    
153
    // Get presets from settings.
154
    $preset_doTaxa = variable_get('cdm_search_doTaxa', 1);
155
    $preset_doSynonyms = variable_get('cdm_search_doSynonyms', 1);
156
    $preset_doTaxaByCommonNames = variable_get('cdm_search_doTaxaByCommonNames', 0);
157
    $preset_doMisappliedNames = variable_get('cdm_search_doMisappliedNames', 1);
158
    $preset_UseDefaults = variable_get('cdm_search_use_default_values', 1);
159

    
160
    // Overwrite presets by user choice stored in session.
161
    if (isset($_SESSION['cdm']['search']) && !$preset_UseDefaults) {
162
      $preset_doTaxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
163
      $preset_doSynonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
164
      $preset_doMisappliedNames = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
165
      $preset_doTaxaByCommonNames = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
166
    }
167
    // General search parameters.
168
    $form['search']['doTaxa'] = array(
169
      '#weight' => 2,
170
      '#type' => 'checkbox',
171
      '#title' => t('Search for accepted taxa'),
172
      '#value' => $preset_doTaxa,
173
    );
174
    $form['search']['doSynonyms'] = array(
175
      '#weight' => 3,
176
      '#type' => 'checkbox',
177
      '#title' => t('Search for synonyms'),
178
      '#value' => $preset_doSynonyms,
179
    );
180
    $form['search']['doMisappliedNames'] = array(
181
      '#weight' => 4,
182
      '#type' => 'checkbox',
183
      '#title' => t('Search for misapplied names'),
184
      '#value' => $preset_doMisappliedNames,
185
    );
186
    $form['search']['doTaxaByCommonNames'] = array(
187
      '#weight' => 5,
188
      '#type' => 'checkbox',
189
      '#title' => t('Search for common names'),
190
      '#value' => $preset_doTaxaByCommonNames,
191
    );
192

    
193
    // Geographic Range.
194
    $form['search']['geographic_range'] = array(
195
      '#type' => 'fieldset',
196
      '#weight' => 5,
197
      '#tree' => TRUE,
198
      '#title' => t('Geographic range'),
199
    );
200

    
201
    $form['search']['geographic_range']['tdwg_level_select'] = array(
202
      '#type' => 'radios',
203
      '#title' => t('Select a TDWG distribution level and code'),
204
      '#default_value' => $tdwg_level_select,
205
      '#options' => array(
206
        t('TDWG level-1, i.e. a continent'),
207
        t('TDWG level-2'),
208
        t('TDWG level-3, i.e. a country'),
209
        t('TDWG level-4'),
210
      ),
211
    );
212
    $tdwg[1] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '1');
213
    $tdwg[2] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '2');
214
    $tdwg[3] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '3');
215
    $tdwg[4] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '4');
216

    
217
    $tdwg_js = '';
218
    foreach ($tdwg as $key => $tdwg_level) {
219
      $tdwgOptions = array();
220
      $tdwgOptionsSelected = array();
221
      foreach ($tdwg_level as $area) {
222
        $representation = $area->representations[0];
223
        $tdwgOptions[$representation->abbreviatedLabel] = $area->representation_L10n;
224
        if (is_array($selected_areas) && in_array($representation->abbreviatedLabel, $selected_areas)) {
225
          // $area->uuid;
226
          $tdwgOptionsSelected[] = $representation->abbreviatedLabel;
227
        }
228
      }
229
      asort($tdwgOptions);
230
      $form['search']['geographic_range']['tdwg_level_' . $key] = array(
231
        '#type' => 'select',
232
        '#title' => t('TDWG level') . ' ' . $key,
233
        '#default_value' => $tdwgOptionsSelected,
234
        '#multiple' => TRUE,
235
        '#options' => $tdwgOptions,
236
      );
237
      $tdwg_js .= "$('#edit-search-geographic-range-tdwg-level-$key').parent()" . ($tdwg_level_select + 1 == $key ? '.show()' : '.hide()') . ";\n";
238
    }
239

    
240
    drupal_add_js(
241
    "jQuery(document).ready(function($){
242
      $(\"input[name='search[geographic_range][tdwg_level_select]']\").change(
243
        function(){
244
          var selectId = $(\"input[name='search[geographic_range][tdwg_level_select]']:checked\").val();
245
          var i;
246
          for(i = 0; i < 4; i++){
247
          
248
            if(selectId == i){
249
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1) ).parent().fadeIn('slow');
250
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
251
            } else {
252
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).parent().fadeOut('slow');
253
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
254
            }
255
          }
256
        }
257
      );
258

    
259
    $tdwg_js
260
    });",
261
    array('type' => 'inline'));
262
  }
263
  else {
264
    $preset_doTaxa = variable_get('cdm_search_doTaxa', 1);
265
    $preset_doSynonyms = variable_get('cdm_search_doSynonyms', 1);
266
    $preset_doTaxaByCommonNames = variable_get('cdm_search_doTaxaByCommonNames', 0);
267
    $preset_doMisappliedNames = variable_get('cdm_search_doMisappliedNames', 1);
268
    $preset_UseDefaults = variable_get('cdm_search_use_default_values', 1);
269

    
270
    // Overwrite presets by user choice stored in session.
271
    if (isset($_SESSION['cdm']['search']) && !$preset_UseDefaults) {
272
      $preset_doMisappliedNames = (isset($_SESSION['cdm']['search']['doMisappliedNames']) ? 1 : 0);
273
    }
274

    
275
    $form['search']['doTaxa'] = array(
276
      '#weight' => -2,
277
      '#type' => 'hidden',
278
      '#value' => $preset_doTaxa,
279
    );
280
    $form['search']['doSynonyms'] = array(
281
      '#weight' => -3,
282
      '#type' => 'hidden',
283
      '#value' => $preset_doSynonyms,
284
    );
285
    $form['search']['doMisappliedNames'] = array(
286
      '#weight' => -4,
287
      '#type' => 'checkbox',
288
      '#title' => t('Misapplied names'),
289
      '#value' => $preset_doMisappliedNames,
290
    );
291
    $form['search']['doTaxaByCommonNames'] = array(
292
      '#weight' => -5,
293
      '#type' => 'hidden',
294
      '#value' => $preset_doTaxaByCommonNames,
295
    );
296
  }
297

    
298
  return $form;
299
}
300
/**
301
 * @todo Please document this function.
302
 * @see http://drupal.org/node/1354
303
 */
304
function cdm_dataportal_search_taxon_form_advanced($form, &$form_state) {
305
  return cdm_dataportal_search_taxon_form($form, $form_state, TRUE);
306
}
307

    
308
/**
309
 * Search form for the searching taxa by the findByDescriptionElementFullText
310
 * rest service.
311
 */
312
function cdm_dataportal_search_taxon_by_description_form() {
313
  $query_field_default_value = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
314

    
315
  $form = cdm_dataportal_search_form_prepare(
316
      'cdm_dataportal/search/results/taxon',
317
      CDM_WS_PORTAL_TAXON_FINDBY_DESCRIPTIONELEMENT_FULLTEXT,
318
      $query_field_default_value,
319
      t("Enter the text you wish to search for. The asterisk character * can be
320
        used as wildcard. Terms can be combined with 'AND'. To search for a
321
        full phrase enclose the terms in parentheses. For more syntactial
322
        options please refer to the !link.", array(
323
          '!link' => l(t('Apache Lucene - Query Parser Syntax'), 'http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html', array(
324
             'attributes' => array('absolute' => TRUE, 'html' => TRUE))),
325
        )),
326
      NULL
327
      );
328

    
329
  $form['search']['tree'] = array(
330
    '#weight' => -1,
331
    '#type' => 'hidden',
332
    '#value' => get_taxonomictree_uuid_selected(),
333
  );
334

    
335
  $form['search']['hl'] = array(
336
    '#weight' => -1,
337
    '#type' => 'hidden',
338
    '#value' => 1,
339
  );
340

    
341
  // Only avaiable to admins:
342
  if (!isset($_SESSION['cdm']['search']['clazz'])) {
343
    $_SESSION['cdm']['search']['clazz'] = '';
344
  }
345
  if (module_exists("user") && user_access('administer')) {
346
    $form['search']['clazz'] = array(
347
      '#type' => 'select',
348
      '#title' => t('Limit to DescriptionElement type'),
349
      '#default_value' => $_SESSION['cdm']['search']['clazz'],
350
      '#options' => cdm_descriptionElementTypes_as_option(TRUE),
351
    );
352
  }
353

    
354
  /*
355
  see cdm_get_featureTrees_as_options() ... $treeRepresentation =
356
  $featureTree->titleCache; if(is_array($featureTree->root->children) &&
357
  count($featureTree->root->children) > 0){ // render the hierarchic tree
358
  structure $treeDetails = '<div class="featuretree_structure">'
359
  //._featureTree_elements_toString($featureTree->root)
360
  .theme('featureTree_hierarchy', $featureTree->uuid) .'</div>'; $form =
361
  array(); $form['featureTree-'.$featureTree->uuid] = array( '#type' =>
362
  'fieldset', '#title' => t('Show details'), '#collapsible' => TRUE,
363
  '#collapsed' => TRUE, );
364
  $form['featureTree-'.$featureTree->uuid]['details'] =
365
  array('#value'=>$treeDetails); $treeRepresentation .= drupal_render($form);
366
  */
367

    
368
  $profile_featureTree = get_profile_featureTree();
369
  $feature_options = _featureTree_nodes_as_feature_options($profile_featureTree->root);
370
  if (isset($_SESSION['cdm']['search']['features'])) {
371
    $form['search']['features'] = array(
372
      '#type' => 'checkboxes',
373
      '#title' => t('Limit to selected features'),
374
      '#default_value' => $_SESSION['cdm']['search']['features'],
375
      '#options' => $feature_options,
376
    );
377
  }
378
  else {
379
    $form['search']['features'] = array(
380
      '#type' => 'checkboxes',
381
      '#title' => t('Limit to selected features'),
382
      '#options' => $feature_options,
383
    );
384
  }
385
  return $form;
386
}
387

    
388
/**
389
 * Filters $_REQUEST by a list of valid request parameters and also sets
390
 * defaults if required.
391
 * returns the processed request parameters submitted by the search form and
392
 * also stores them
393
 * in $_SESSION['cdm']['search']
394
 */
395
function cdm_dataportal_search_form_request() {
396
  $form_params = array();
397
  if (is_array($_REQUEST['search'])) {
398
    array_deep_copy($_REQUEST['search'], $form_params);
399
  }
400
  $form_params['query'] = trim($_REQUEST['query']);
401

    
402
  // --- handle geographic range
403
  // Split of geographic range.
404
  if (isset($_REQUEST['search']['geographic_range']) && is_array($_REQUEST['search']['geographic_range'])) {
405
    $geographicRange = $_REQUEST['search']['geographic_range'];
406
    // Remove from form.
407
    unset($form_params['geographic_range']);
408
    $form_params['tdwg_level_select'] = $geographicRange['tdwg_level_select'];
409
    for ($i = 1; $i < 5; $i++) {
410
      if (isset($geographicRange['tdwg_level_' . $i])) {
411
        $form_params['area'] = $geographicRange['tdwg_level_' . $i];
412
      }
413
    }
414
  }
415

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

    
419
  return $form_params;
420
}
421

    
422
/**
423
 * Removes Drupal internal form elements from query.
424
 */
425
function cdm_dataportal_search_process($form, &$form_state) {
426
  unset($form['form_id']);
427
  unset($form['form_token']);
428
  return $form;
429
}
430

    
431
/**
432
 * @todo Please document this function.
433
 * @see http://drupal.org/node/1354
434
 */
435
function cdm_dataportal_search_execute() {
436

    
437
  // Store as last search in session.
438
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
439

    
440
  // Validate the search webservice parameter:
441
  if (!cdm_dataportal_search_form_path_for_ws($_REQUEST['ws'])) {// Check is ws.
442
    // Endpoint is unknown.
443
    drupal_set_message(t('Invalid search webservice parameter given'));
444
    return NULL;
445
  }
446

    
447
  $request_params = cdm_dataportal_search_form_request();
448
  $taxonPager = cdm_ws_get($_REQUEST['ws'], NULL, queryString($request_params));
449

    
450
  return $taxonPager;
451
}
(7-7/13)