Project

General

Profile

Download (28.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
// $Id$
3

    
4
/*
5
 * @file
6
 * cdm_dataportal
7
 *
8
 * Copyright (C) 2007 EDIT
9
 * European Distributed Institute of Taxonomy
10
 * http://www.e-taxonomy.eu
11
 *
12
 */
13

    
14
require_once('cdm_dataportal.theme.php');
15

    
16
define(CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE, 25);
17

    
18
/* ====================== hook implementations ====================== */
19

    
20
/**
21
 * Implementation of hook_help()
22
 *
23
 * Display help and module information
24
 * @param section which section of the site we're displaying help
25
 * @return help text for section
26
 */
27
function cdm_dataportal_help($section='') {
28

    
29
  $out = '';
30
  switch ($section) {
31
    case "admin/modules#description":
32
      $out = t("The dataportal publishes CDM data hosted in a CommunityStore on the web.");
33
      break;
34
  }
35
  return $out;
36
}
37

    
38

    
39
/**
40
 * Implementation of hook_perm()
41
 *
42
 * Valid permissions for this module
43
 * @return array An array of valid permissions for the portfolio module
44
 */
45
function cdm_dataportal_perm() {
46
  return array(
47
    	'administer cdm_dataportal',
48
        'cdm_dataportal view notes',
49
  //TODO which else permission are required? -> check the WP6 requirements document
50
  );
51
}
52

    
53

    
54
/**
55
 * Implementation of hook_menu()
56
 */
57
function cdm_dataportal_menu($may_cache) {
58
  $items = array();
59
  
60
  if ($may_cache) {
61

    
62
    $items[] = array(
63
      'path' => 'admin/settings/cdm_dataportal',
64
      'title' => t('CDM Dataportal'),
65
      'description' => t('Setting for the CDM Dataportal'),
66
      'access' => user_access('administer cdm_dataportal'),
67
      'callback' => 'drupal_get_form',
68
      'callback arguments' => 'cdm_dataportal_settings',
69
    );
70
    
71
    /*$items[] = array(
72
      'path' => 'admin/settings/cdm_dataportal/ws',
73
      'title' => t('Web Service'),
74
      'description' => t('Setting for the CDM Dataportal'),
75
      'access' => user_access('administer cdm_dataportal'),
76
      'callback' => 'drupal_get_form',
77
      'callback arguments' => 'cdm_dataportal_settings',
78
      'weight' => 1,
79
      'type' => MENU_DEFAULT_LOCAL_TASK,
80
    );*/
81
    
82
    $items[] = array(
83
	    'path' => 'cdm_dataportal/names',
84
	    'callback' => 'cdm_dataportal_view_names',
85
	    'access' => true,
86
	    'type' => MENU_CALLBACK,
87
    );
88
    // optional callback arguments: page
89
   
90
    $items[] = array(
91
      'path' => 'cdm_dataportal/taxon',
92
      'callback' => 'cdm_dataportal_view_taxon',
93
      'access' => true,
94
      'type' => MENU_CALLBACK,
95
    // expected callback arguments: name_uuid
96
    );
97
    
98
    $items[] = array(
99
      'path' => 'cdm_dataportal/reference',
100
      'callback' => 'cdm_dataportal_view_reference',
101
      'access' => true,
102
      'type' => MENU_CALLBACK,
103
    // expected callback arguments: name_uuid
104
    );
105
    
106
    $items[] = array(
107
      'path' => 'cdm_dataportal/reference/list',
108
      'callback' => 'cdm_dataportal_view_reference_list',
109
      'access' => true,
110
      'type' => MENU_CALLBACK,
111
    // expected callback arguments: name_uuid
112
    );
113
    
114
    $items[] = array(
115
      'path' => 'cdm_dataportal/search/taxon',
116
      'callback' => 'cdm_dataportal_view_search_taxon',
117
      'access' => true,
118
      'type' => MENU_CALLBACK,
119
    );
120
    
121
    $items[] = array(
122
      'path' => 'cdm/xml2json',
123
      'callback' => 'cdm_view_xml2json',
124
      'access' => true,
125
      'type' => MENU_CALLBACK,
126
    );
127
    
128
    
129
  } else {
130
    // may not cache
131
        // --- local tasks for Taxon
132
    if(variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
133
      
134
       $items[] = array(
135
      'path' => 'cdm_dataportal/taxon/'.arg(2),
136
      'title' => t('General'),
137
      'callback' => 'cdm_dataportal_view_taxon',
138
      'access' => true,
139
      'type' => MENU_CALLBACK,
140
      'weight' => 1,
141
      'callback arguments' => array(arg(2))
142
      // expected callback arguments: name_uuid
143
      );
144
      
145
      $items[] = array(
146
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/description',
147
      'title' => t('General'),
148
      'callback' => 'cdm_dataportal_view_taxon',
149
      'access' => true,
150
      'type' => MENU_DEFAULT_LOCAL_TASK,
151
      'weight' => 2,
152
      'callback arguments' => array(arg(2), "description")
153
      // expected callback arguments: name_uuid
154
      );
155
      
156
      $items[] = array(
157
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/synonymy',
158
      'title' => t('Synonymy'),
159
      'callback' => 'cdm_dataportal_view_taxon',
160
      'access' => true,
161
      'type' => MENU_LOCAL_TASK,
162
      'weight' => 3,
163
      'callback arguments' => array(arg(2), "synonymy")
164
      // expected callback arguments: name_uuid
165
      );
166
      
167
      $items[] = array(
168
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/images',
169
      'title' => t('Images'),
170
      'callback' => 'cdm_dataportal_view_taxon',
171
      'access' => true,
172
      'type' => MENU_LOCAL_TASK,
173
      'weight' => 4,
174
      'callback arguments' => array(arg(2), "images")
175
      // expected callback arguments: name_uuid
176
      );
177
    } 
178
  }
179
  
180
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal.css');
181
  //drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
182
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_screen.css', 'screen');
183
  
184
  return $items;
185
  
186
}
187

    
188
/**
189
 * Implementation of hook_block()
190
 *
191
 * Provides the following blocks:
192
 *  0: list of links useful during development
193
 *
194
 * @param String $op
195
 * @param int $delta
196
 */
197
function cdm_dataportal_block($op='list', $delta=0) {
198
  // listing of blocks, such as on the admin/block page
199
  if ($op == "list") {
200
    $block[0]["info"] = t("CDM DataPortal DevLinks");
201
    $block[1]["info"] = t("CDM DataPortal Credits");
202
    $block[2]["info"] = t("CDM Search Taxa");
203
    $block[3]["info"] = t("CDM Filters");
204
    return $block;
205
  }
206
  else if ($op == 'view') {
207
    switch($delta){
208
      case 0:
209
        $block['subject'] = t('CDM DataPortal DevLinks');
210
        $block['content'] = '<ul>
211
        <li>'.l('A Taxon Page', cdm_dataportal_taxon_path('5000001-stub')).'</li>
212
        <li>'.l('Name List - page1 of A', 'cdm_dataportal/names/A/1')
213
        //.'</li><li>'.l('xml2json', 'cdm/xml2json/namelist;startwith=A.xml')
214
        .'</li>
215
        </ul>';
216
        return $block;
217
      case 1:
218
        $block['subject'] = t('Credits');
219
        $block['content'] = theme('cdm_credits');
220
        return $block;
221
      case 2:
222
        $block['subject'] = t('Search Taxa');
223
        $block['content'] = drupal_get_form('cdm_dataportal_search_taxon_form');
224
        return $block;
225
    }
226
  }
227
}
228

    
229

    
230
/**
231
 * Implementation of hook_validate()
232
 *
233
 * @param $element
234
 */
235
function cdm_dataportal_settings_validate($form_id, $form_values){
236
  
237
  if (!str_endsWith($form_values['cdm_webservice_url'], '/')) {
238
    form_set_error('cdm_webservice_url', t("The URL to the CDM Web Service must end with a slash: '/'."));
239
  }
240

    
241
  $cdm_webservice_url_changed = variable_get('cdm_webservice_url', '') != $form_values['cdm_webservice_url'];
242
  
243
  if ($cdm_webservice_url_changed) {
244
    $form_values['cdm_secUuid_default'] = '';
245
    //TODO does the currentSecUuid need to be stored?
246
    //   Yes - if switching to another concept is permanent until an other concept switch is used.
247
    //   NO  - if a user only 'visits' other concepts until this context is left by visiting a non taxon page
248
    //         , or explicitly when a concept switch is used.
249
    //         other concept could be activated permanently by personal preference settings
250
    //
251
    //  >> NO seems to be the desired solution <<
252
    //
253
    _cdm_dataportal_set_currentSecUuid(false);
254
    // reset all cdm related data stored in the session
255
    // cdm_dataportal_session_clear($form_values['cdm_webservice_url']); // cdm_webservice_url is not further stored here
256
    // clear the cdm webservice cache
257
    cache_clear_all(NULL, 'cache_cdm_ws');
258
    // better clear secref_cache since i can not be sure if the cache has not be used during this response
259
    cdm_api_secref_cache_clear();
260
  }
261
  if($form_values['cdm_webservice_cache'] != variable_get('cdm_webservice_cache', 1)){
262
    cache_clear_all(NULL, 'cache_cdm_ws');
263
    // better clear secref_cache since i can not be sure if the cache has not be used during this response
264
    cdm_api_secref_cache_clear();
265
  }
266
  
267
}
268

    
269
/*
270
function cdm_dataportal_session_clear($cdm_ws_uri_update = false){
271
  $_SESSION['cdm'] = null;
272
  if(is_string($cdm_ws_uri_update)){
273
    $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
274
  }
275
}
276

    
277
function cdm_dataportal_session_validate(){
278

    
279
  if(!isset($_SESSION['cdm']['ws_uri'])){
280
    $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', false));
281
  } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', false)){
282
    cdm_dataportal_session_clear(variable_get('cdm_webservice_url', false));
283
  }
284
}
285
*/
286

    
287
function cdm_dataportal_search_taxon_form(){
288
  
289
  $url = 'cdm_dataportal/search/taxon';
290
  $form['#method'] = 'get';
291
  $form['#process'] = array('cdm_dataportal_search_process' => array());
292
  $form['#action'] = url($url, NULL, NULL, true);
293
  
294
  $form['queryString'] = array(
295
    '#type' => 'textfield',
296
    '#size' => 20,
297
    '#attributes' => array('title' => t('Enter the name or part of a name you wish to search for. The asterisk  character * can always be used as wildcard')),
298
    '#default_value' => (isset($_SESSION['cdm']['search']['queryString']) ? $_SESSION['cdm']['search']['queryString'] : ''),
299
  );
300
  $form['mode'] =  array(
301
    '#type' => 'hidden',
302
    '#value' => 'BEGINNING'
303
    );
304
    
305
  $form['onlyAccepted'] =  array(
306
    '#type' => 'hidden',
307
    '#value' => '0',
308
    );
309
  
310
 /* $form['onlyAccepted'] = array(
311
    '#type' => 'checkbox',
312
    '#title' => t('Only Accepted'),
313
    '#attributes' => array('title' => t('Search only for accepted taxa.')),
314
    '#default_value' => isset($_SESSION['cdm']['search']['onlyAccepted']) && $_SESSION['cdm']['search']['onlyAccepted'],
315
  );*/
316
  
317
  /*
318
  $form['vernacular'] = array(
319
    '#type' => 'checkbox',
320
    '#title' => t('Vernacular Names'),
321
    '#attributes' => array('title' => t('Search for vernacular names.')),
322
    '#default_value' => isset($_SESSION['cdm']['search']['vernacular']) && $_SESSION['cdm']['search']['vernacular'],
323
  );
324
  $form['language'] =  array(
325
    '#type' => 'select',
326
    '#title' => t('Language'),
327
    '#options' => array(
328
        '*' => t('ALL')
329
        ),
330
    '#description'   => t('The language of the vernacular name'),
331
    '#default_value' => (isset($_SESSION['cdm']['search']['language']) ? $_SESSION['cdm']['search']['language'] : ''),
332
  );
333
  */
334
  $form['page'] = array(
335
  '#type' => 'hidden',
336
  '#value' => '1',
337
  );
338
  
339
  $form['submit'] = array(
340
  '#type' => 'submit',
341
  '#name' => '',
342
  '#value' => t('Search'),
343
  );
344
  
345
  // clean URL get forms breaks if we don't give it a 'q'.
346
  if (!(bool)variable_get('clean_url', '0')) {
347
    $form['q'] = array(
348
      '#type' => 'hidden',
349
      '#value' => $url,
350
      '#name' => 'q',
351
    );
352
  }
353
  return $form;
354
}
355

    
356
//TODO is this function REACHABLE and thus in use??
357
function cdm_dataportal_search_process($form) {
358
  unset($form['form_id']);
359
  unset($form['form_token']);
360
  return $form;
361
}
362

    
363
/**
364
 * Filters $_REQUEST by a list of valid request  parameters and also sets defaults if required.
365
 * returns the processed request parameters submitted by the search form.
366
 */
367
function cdm_dataportal_search_form_request(){
368
  $currentSecRefArray = _cdm_dataportal_currentSecRef_array();
369
  // keys map $REQUEST Keys values are optional default parameters if not null
370
  $search_form_params = array( 	'queryString' => null,
371
  										'onlyAccepted' => '0',
372
  										'page' => null,
373
  										'pagesize' => variable_get('cdm_dataportal_search_items_on_page', 25),
374
  										'mode' => null,
375
                      'sec' => $currentSecRefArray['uuid']);
376
  
377
  
378
  $form_params = array();
379
  foreach($search_form_params as $key=>$default ){
380
    $form_params[$key] = ( isset($_REQUEST[$key]) && $_REQUEST[$key] != '' ? $_REQUEST[$key] : $default);
381
  }
382
  $_SESSION['cdm']['search'] = $form_params;
383
  return $form_params;
384
}
385
  
386

    
387
/* UNREACHABLE since action of form directly links to view
388
function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
389
  
390
  $_SESSION['cdm']['search'] = $form_values;
391
  //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
392
  return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
393
  //$paramstr = compose_url_prameterstr($form_values);
394
  //return url('/cdm_dataportal/search/taxon/', $paramstr);
395
}
396
*/
397
/* ====================== menu callback functions ====================== */
398

    
399
/**
400
 * Generate main administration form.
401
 *
402
 * @return
403
 *   An array containing form items to place on the module settings page.
404
 */
405
function cdm_dataportal_settings(){
406

    
407
  $form = cdm_api_settings_form();
408

    
409
  //TODO: settings are still incomplete, compare with trunk/dataportal/inc/config_default.php.inc
410
  $form['cdm_dataportal'] = array(
411
      '#type' => 'fieldset',
412
      '#title' => t('CDM DataPortal'),
413
      '#collapsible' => FALSE,
414
      '#collapsed' => TRUE,
415
  );
416
  
417
  $form['cdm_dataportal']['secuuid_widget'] = array(
418
    '#type' => 'select_secuuid', // => will be themed by theme_select_secuuid(...)
419
    '#title' => t('Default Concept References'),
420
    '#description'   => t('The default \'sensu\' reference to start the CDM DataPortal with. Per user choices are possible and may override this value.'),
421
    '#varname' => 'cdm_secUuid_default',
422
    '#multiple' => false
423
  );
424
  
425
    
426
  //------------------ FEATURE TREE --------------------//
427
  
428
  $form['cdm_dataportal']['feature_tree'] = array(
429
    '#type' => 'fieldset',
430
    '#title' => t('Feature Tree'),
431
    '#collapsible' => TRUE,
432
    '#collapsed' => TRUE,
433
  );
434
  
435
    $form['cdm_dataportal']['feature_tree']['cdm_dataportal_feature_tree_active'] = array(
436
    '#type' => 'select',
437
    '#title'         => t('Available FeatureTrees'),
438
    '#default_value' => variable_get('cdm_dataportal_feature_tree_active', 'ae9615b8-bc60-4ed0-ad96-897f9226d568'),
439
	'#options' => _get_feature_trees(),
440
    '#description'   => t('Select a FeatureTree for this webservice. If there is no applicable FeatureTree you can create a new one using the <a href="">FeatureTreeManager</a>')
441
  );
442
  
443
  //------------------ LAYOUT --------------------//
444
  
445
  $form['cdm_dataportal']['layout'] = array(
446
    '#type' => 'fieldset',
447
    '#title' => t('Layout'),
448
    '#collapsible' => TRUE,
449
    '#collapsed' => TRUE,
450
  );
451
  
452
   $form['cdm_dataportal']['layout']['cdm_taxonname_type'] = array(
453
    '#type' => 'select',
454
    '#title'         => t('Taxon name type'),
455
    '#default_value' => variable_get('cdm_taxonname_type', 'BotanicalName'),
456
    '#options' => array( 'BotanicalName'=>t('BotanicalName'), 'ZoologicalName'=>t('ZoologicalName')),
457
    '#description'   => t('')
458
  );
459
  
460
  $form['cdm_dataportal']['layout']['cdm_dataportal_nomref_in_title'] = array(
461
    '#type' => 'checkbox',
462
    '#title' => t('Hide full nomenclatural reference in title'),
463
    '#default_value' => variable_get('cdm_dataportal_nomref_in_title', 1),
464
    '#description' => t('Uncheck this, if you want the title of a taxon page consisting of name and author plus publication year only.
465
    					If not checked, the first homotypic taxon is a repetition of the accepted taxon with
466
    					the full nomenclatural reference.')
467
  );
468
  
469
  $form['cdm_dataportal']['layout']['cdm_dataportal_display_name_relations'] = array(
470
    '#type' => 'checkbox',
471
    '#title' => t('Show name relations of accepted taxa on taxon page'),
472
    '#default_value' => variable_get('cdm_dataportal_display_name_relations', 1),
473
    '#description' => t('')
474
  );
475
  
476
  $form['cdm_dataportal']['layout']['cdm_dataportal_taxonpage_tabs'] = array(
477
    '#type' => 'checkbox',
478
    '#title' => t('Tabbed taxon page'),
479
    '#default_value' => variable_get('cdm_dataportal_taxonpage_tabs', 1),
480
    '#description' => t('Split the taxon page into individual tabs for description, images, synonymy')
481
  );
482
  
483
  $form['cdm_dataportal']['layout']['cdm_dataportal_search_items_on_page'] = array(
484
    '#type' => 'textfield',
485
    '#title' => t('Search Page Size'),
486
    '#default_value' => variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE),
487
    '#description' => t('Number of Names to display per page in search results.')
488
  );
489
  
490
 
491
  //---------------- GEOSERVICE ----------------//
492
  
493
  $form['cdm_dataportal']['geoservice'] = array(
494
    '#type' => 'fieldset',
495
    '#title' => t('Geoservice'),
496
    '#collapsible' => TRUE,
497
    '#collapsed' => TRUE,
498
  );
499
  
500
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_geoservice_access_point'] = array(
501
    '#type' => 'textfield',
502
    '#title' => t('Geoservice Access Point'),
503
    '#default_value' => variable_get('cdm_dataportal_geoservice_access_point', ''),
504
    '#description' => t('Base URL of the geoservice to be used by this portal')
505
  );
506
  
507
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_geoservice_display_width'] = array(
508
    '#type' => 'textfield',
509
    '#title' => t('Geoservice Display Width'),
510
    '#default_value' => variable_get('cdm_dataportal_geoservice_display_width', 390),
511
    '#description' => t('Width of the image generated by geoservice')
512
  );
513
  
514
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_geoservice_bounding_box'] = array(
515
    '#type' => 'textfield',
516
    '#title' => t('Geoservice Bounding Box'),
517
    '#default_value' => variable_get('cdm_dataportal_geoservice_bounding_box', '-180,-90,180,90'),
518
    '#description' => t('Define the surrounding area using latitude and longitude. Leave as is for the whole world.')
519
  );
520
  
521
   //cdm_dataportal_geoservice_labels_on
522
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_geoservice_labels_on'] = array(
523
    '#type' => 'checkbox',
524
    '#title' => t('Display Country Labels'),
525
    '#default_value' => variable_get('cdm_dataportal_geoservice_labels_on', FALSE),
526
    '#description' => t('Check this if you like country names to be displayed in the maps. ')
527
  );
528
  
529
  return system_settings_form($form);
530
}
531

    
532
/**
533
 * Implementation of hook_elements().
534
 */
535
function cdm_dataportal_elements() {
536
  $type['select_secuuid'] = array(
537
    '#input' => TRUE,
538
    '#process' => array('cdm_dataportal_select_secuuid_expand' => array()),
539
    //'#validate' => array('cdm_dataportal_select_secuuid_validate' => array()),
540
    //'#default_value' => array(),
541
  );
542
  return $type;
543
}
544

    
545
/*
546
 *
547
 */
548
function cdm_dataportal_select_secuuid_expand($element){
549
  
550
  $element['#tree'] = FALSE;
551
  
552
  // any value submitted?
553
  if(isset($element['#post'][$element['#varname']])){
554
    $selected_values = $element['#post'][$element['#varname']];
555
  } else {
556
    // use those store in drupal
557
    $selected_values = variable_get( $element['#varname'], array());
558
  }
559
  if(!is_array($selected_values)){
560
    $selected_values = array($selected_values);
561
  }
562
  $options = array();
563
  foreach($selected_values as $secUuid){
564
    $options[$secUuid] = cdm_taxontree_secRefTitle_for($secUuid);
565
  }
566

    
567
  $element[$element['#varname']] =  array(
568
    '#type' => 'select',
569
    '#options' => $options,
570
    '#default_value' => array_values($options),
571
    '#size' => $element['#multiple'] ? 12 : 2,
572
    '#multiple' => $element['#multiple'],
573
  );
574
  return $element;
575
}
576

    
577

    
578

    
579
/**
580
 * Displays a list of the known taxonomic names. Long lists are split up into multiple pages
581
 *
582
 * TODO: parameters are still preliminar
583
 * @param String $page page number to diplay defaults to page 1
584
 * @param boolean $hide_unaccepted whether to hide nams which are not accepted by the current view
585
 */
586
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = false ){
587

    
588
  $request_params  = array(
589
    'q' => $beginsWith,
590
         //'sec' = '',
591
         //'higherTaxa' => getFiters(),
592
          // 'matchAnywhere' => false, // default is false
593
    'page' => $page,
594
    'onlyAccepted' => $onlyAccepted,
595
    'pagesize' => 20  /*$_SESSION['cdm']['namelist_pagesize'] */);
596
  
597
  $taxonPager = cdm_ws_find(CDM_WS_FIND_TAXA, $request_params);
598
  /*
599
   * FIXME the filter for accepted names will be a form element, thus this widget
600
   * should be generated via form api preferably as block
601
   */
602
  //$out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
603
  //$out .= theme('cdm_dataportal_widget_names_list', $names, $page);
604
  $out .= theme('cdm_listof_taxa', $taxonPager);
605
  return $out;
606
}
607

    
608
function cdm_dataportal_view_reference($uuid, $arg2 = null){
609
    $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
610
    return theme('cdm_reference_page', $reference);
611
}
612

    
613
function cdm_dataportal_view_reference_list($pageNumber){
614
    $referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE), $pageNumber);
615
    return theme('cdm_reference_pager', $referencePager, 'cdm_dataportal/reference/list/');
616
}
617

    
618
/**
619
 * The taxon page gives detailed information on a taxon, it shows:
620
 *  - Taxon name
621
 *  - Full list of synonyms homotypic synonyms on top, followed by the
622
 *    heterotypic and finally followed by misapplied names.
623
 *    The list is ordered historically.
624
 *  - All facts associated with the very taxon concept and taxon name.
625
 *
626
 * @param String $uuid the UUID of the taxon
627
 */
628
function cdm_dataportal_view_taxon($uuid, $chapter){
629
  
630
    // display the page for the taxon defined by $uuid
631
    $taxon = cdm_ws_get(CDM_WS_TAXON, $uuid);
632
    if(!$taxon){
633
      drupal_set_title(t('Taxon does not exist'));
634
      return false;
635
    }
636
    _cdm_dataportal_set_currentSecUuid($taxon->sec->uuid);
637
    
638
    drupal_set_title(theme('cdm_taxon_page_title', $taxon->name));
639
    $out = theme('cdm_taxon_page_general', $taxon, $chapter);
640
    
641
    return $out;
642
  /*}*/
643
}
644

    
645
/**
646
 * Expected URL request parameters:
647
 *  - text
648
 *  - language
649
 *  - vernacular
650
 *
651
 * future extensions to meet palmweb mockup requirements:
652
 *  - habitat
653
 *  - uses
654
 *  - conservation status
655
 *  - locality / tdwg region
656
 */
657
function cdm_dataportal_view_search_taxon(){
658
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
659
  $request_params = cdm_dataportal_search_form_request();
660
  $sw_params = array_replace_key($request_params, array('queryString'=> 10 ));
661
  $taxonPager = cdm_ws_find(CDM_WS_TAXON, $sw_params);
662
  //$taxonPager->pageNumber = $request_params['page'];
663
  return theme('cdm_search_results', $taxonPager, 'cdm_dataportal/search/taxon', $request_params);
664
}
665

    
666
function cdm_view_xml2json(){
667
  $file = arg(2);
668
  $datastr = get_content(variable_get('cdm_webservice_url', '').$file);
669
  return  xml2json::transformXmlStringToJson($datastr);
670
}
671

    
672
/* ====================== other functions ====================== */
673

    
674
/**
675
 * Enter description here...
676
 *
677
 * @param String $uuid the UUID of the taxon
678
 * @return the URL
679
 */
680
function cdm_dataportal_taxon_path($uuid){
681
  return 'cdm_dataportal/taxon/'.$uuid;
682
}
683

    
684
/**
685
 * Creates a short taxonname by using the taggename field of NameSTO or NameTO instances.
686
 * If the taggename if empty the fullname will be returned.
687
 *
688
 * @param unknown_type $Name or TreeNode
689
 * @return string
690
 */
691
function cdm_dataportal_shortname_of($name){
692

    
693
  $nameStr = '';
694
  // get all tagged text tokens of the scientific name
695
  foreach($name->taggedTitle as $tagtxt){
696
      if($tagtxt->type == 'name' || $tagtxt->type == 'rank'){
697
        $nameStr .= ($nameStr ? ' ' : '').$tagtxt->text;
698
      }
699
  }
700
  $nameStr = trim($nameStr);
701
  if($nameStr){
702
    // do not return short names for these
703
    if($nameStr == 'Incertae sedis' || $nameStr ==  'Nomina excludenda'){
704
      return $nameStr;
705
    }
706
    if($pos = stripos($nameStr, ' ')){
707
      return substr($nameStr, 0, 1).'. '.substr($nameStr, $pos);
708
    } else {
709
      return $nameStr;
710
    }
711
  } else {
712
    return $name->titleCache;
713
  }
714
}
715

    
716
/**
717
 * TODO Add Comments. I don't get what
718
 *
719
 * @param UUID $secUuid
720
 */
721
function _cdm_dataportal_set_currentSecUuid($secUuid){
722
 
723
// do not save in session but in database
724
  
725
  //TODO setting the currentSecUuid causes problems in cichorieae in taraxacum, 
726
  //     thus it is temporary disabled
727
  return;
728
  
729
  if(is_array($secUuid)){
730
    $secUuid = $secUuid[0];
731
  }
732
  if(!$secUuid){
733
    variable_set('cdm_currentSecRef', null);
734
  } else {
735
    $secRef = cdm_ws_get(CDM_WS_REFERENCE, $secUuid);
736
    if(isset($secRef[0])){
737
      variable_set('cdm_currentSecRef', (array)($secRef[0]));
738
    }
739
  }
740
  
741
}
742

    
743
/**
744
 * TODO
745
 * This will not work with multiple instances of the dataportal running
746
 * under the same host as there is only one session for all instances.
747
 * In case you switch instances with the same client, you will definitely
748
 * run into trouble.
749
 * Also this is not handling multiple secs.
750
 *
751
 *
752
 * returns the current secRef array from the users session.
753
 * If the according session variable is not jet set the default
754
 * as configured in the setting is used otherwise null.
755
 *
756
 * currentSecRef['uuid']
757
 * currentSecRef[....
758
 *
759
 * @return array
760
 */
761
function _cdm_dataportal_currentSecRef_array(){
762
    /*
763
  if( !isset($_SESSION['cdm']['currentSecRef']['uuid'])){
764
     $secUuid = variable_get('cdm_secUuid_default', null);
765
     _cdm_dataportal_set_currentSecUuid($secUuid);
766
  }
767
  return  $_SESSION['cdm']['currentSecRef'];
768
  */
769
  
770
  //TODO setting the currentSecUuid causes problems in cichorieae in taraxacum, 
771
  //     thus it is temporary DISABLED
772
  // do not look in session but in database
773
  if( "DISABLED" != false || variable_get('cdm_currentSecRef', null) == null){
774
     $secUuid = variable_get('cdm_secUuid_default', null);
775
     _cdm_dataportal_set_currentSecUuid($secUuid);
776
  }
777
  return variable_get('cdm_currentSecRef', null);
778
  
779
}
780

    
781
/**
782
 * Check if a taxon is accepted by the current secundum
783
 *
784
 * @param TaxonTO $taxonTO
785
 * @return true if $taxonTo is accepted, false otherwise
786
 */
787
function _cdm_dataportal_acceptedByCurrentView($taxon){
788
  $current_secref = _cdm_dataportal_currentSecRef_array();
789
  
790
  return $taxon->class == "Taxon" && $current_secref->uuid == $taxon->sec->uuid;
791
}
792

    
793
/**
794
 * Truncates a $string to the specified $length. 
795
 * If the supplied string is equal to or shorter than the $legth the original is returend.
796
 * if an $appendix is defined the resulting string will have the specified $length including the $appendix.
797
 *
798
 * @param String $string	the string to truncate
799
 * @param Number $length	the maximun length
800
 * @param String $appendix	an optional appendix.
801
 *
802
 * @return the string
803
 */
804
function str_trunk(&$string, $length, $appendix=''){
805
  if(strlen($string) >= $length ){
806
    return  substr($string, 0, $length - strlen($appendix)).$appendix;
807
  } else {
808
    return $string;
809
  }
810
}
811

    
812
/**
813
 * @param string $str
814
 * @param string $sub
815
 * @return boolean
816
 */
817
function str_beginsWith( $str, $sub ) {
818
  return ( substr( $str, 0, strlen( $sub ) ) === $sub );
819
}
820

    
821
/**
822
 *
823
 * @param string $str
824
 * @param string $sub
825
 * @return boolean
826
 */
827
function str_endsWith( $str, $sub ) {
828
  return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
829
}
830

    
831

    
832
function array_replace_key($array, $replace_map){
833
  foreach($replace_map as $key=>$newkey){
834
    if(isset($array[$key])){
835
      $array[$newkey] = $array[$key];
836
      unset($array[$key]);
837
    }
838
  }
839
  return $array;
840
}
841

    
842
/**
843
 * Replaces all occurrences of space characters with 
844
 * an underscore and transforms the given
845
 * string to lowercase.
846
 *
847
 * @param String $string
848
 * @return the transformed string
849
 */
850
function generalizeString($string){
851
  return str_replace(' ', '_', strtolower($string));
852
}
853

    
854

    
855
function compose_url_prameterstr($parameters = array()){
856
  $pstr = '';
857
  foreach($parameters as $key=>$value){
858
     $pstr .= ($pstr ? '&' :'').$key.'='.urlencode($value);
859
  }
860
  return $pstr;
861
}
862

    
863
function _get_feature_trees(){
864
   $feature_trees = array();
865
   
866
   // set tree that contains all features
867
   $feature_trees['0'] = 'No FeatureTree';
868
   
869
   // get features from database
870
   $pager = cdm_ws_page(CDM_WS_FEATURETREE, null, null);
871
   $persisted_trees = $pager->records;
872
   if(is_array($persisted_trees)){
873
     foreach($persisted_trees as $featureTree){
874
       
875
       $featureLabels = array();
876
       foreach ($featureTree->children as $featureNode){
877
         $featureLabels[] = $featureNode->feature->representation_L10n;
878
       }
879
       
880
       $feature_trees[$featureTree->uuid] = implode(", ", $featureLabels);
881
       
882
     }
883
   }
884
   return $feature_trees;
885
}
886

    
(5-5/9)