Project

General

Profile

Download (19.8 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
/* ====================== hook implementations ====================== */
17

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

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

    
36

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

    
51

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

    
59
    $items[] = array(
60
      'path' => 'admin/settings/cdm_dataportal',
61
      'title' => t('CDM Dataportal'),
62
      'description' => t('Setting for the CDM Dataportal'),
63
      'access' => user_access('administer cdm_dataportal'),
64
      'callback' => 'drupal_get_form',
65
      'callback arguments' => 'cdm_dataportal_settings',
66
    );
67
    
68
    /*$items[] = array(
69
      'path' => 'admin/settings/cdm_dataportal/ws',
70
      'title' => t('Web Service'),
71
      'description' => t('Setting for the CDM Dataportal'),
72
      'access' => user_access('administer cdm_dataportal'),
73
      'callback' => 'drupal_get_form',
74
      'callback arguments' => 'cdm_dataportal_settings',
75
      'weight' => 1,
76
      'type' => MENU_DEFAULT_LOCAL_TASK,
77
    );*/
78
    
79
    $items[] = array(
80
	    'path' => 'cdm_dataportal/names',
81
	    'callback' => 'cdm_dataportal_view_names',
82
	    'access' => true,
83
	    'type' => MENU_CALLBACK, 
84
    );
85
    // optional callback arguments: page
86
     
87
    $items[] = array(
88
	    'path' => 'cdm_dataportal/taxon',
89
	    'callback' => 'cdm_dataportal_view_taxon',
90
	    'access' => true,
91
	    'type' => MENU_CALLBACK, 
92
    // expected callback arguments: name_uuid
93
    );
94
    
95
    $items[] = array(
96
      'path' => 'cdm_dataportal/search/taxon',
97
      'callback' => 'cdm_dataportal_view_search_taxon',
98
      'access' => true,
99
      'type' => MENU_CALLBACK, 
100
    );
101
    
102
    $items[] = array(
103
      'path' => 'cdm/xml2json',
104
      'callback' => 'cdm_view_xml2json',
105
      'access' => true,
106
      'type' => MENU_CALLBACK, 
107
    );
108
     
109
  }
110
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal.css');
111
  //drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
112
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_screen.css', 'screen');
113
  return $items;
114
  
115
}
116

    
117
/**
118
 * Implementation of hook_block()
119
 *
120
 * Provides the following blocks:
121
 *  0: list of links useful during development
122
 *
123
 * @param String $op
124
 * @param int $delta
125
 */
126
function cdm_dataportal_block($op='list', $delta=0) {
127
  // listing of blocks, such as on the admin/block page
128
  if ($op == "list") {
129
    $block[0]["info"] = t("CDM DataPortal DevLinks");
130
    $block[1]["info"] = t("CDM DataPortal Credits");
131
    $block[2]["info"] = t("CDM Search Taxa");
132
    $block[3]["info"] = t("CDM Filters");
133
    return $block;
134
  }
135
  else if ($op == 'view') {
136
    switch($delta){
137
      case 0:
138
        $block['subject'] = t('CDM DataPortal DevLinks');
139
        $block['content'] = '<ul>
140
        <li>'.l('A Taxon Page', cdm_dataportal_taxon_path('5000001-stub')).'</li>
141
        <li>'.l('Name List - page1 of A', 'cdm_dataportal/names/A/1')
142
        //.'</li><li>'.l('xml2json', 'cdm/xml2json/namelist;startwith=A.xml')
143
        .'</li>
144
        </ul>';
145
        return $block;
146
      case 1:
147
        $block['subject'] = t('Credits');
148
        $block['content'] = theme('cdm_credits');
149
        return $block;
150
      case 2:
151
        $block['subject'] = t('Search Taxa');
152
        $block['content'] = drupal_get_form('cdm_dataportal_search_taxon_form');
153
        return $block;
154
    }
155
  }
156
}
157

    
158

    
159
/**
160
 * Implementation of hook_validate()
161
 *
162
 * @param $element
163
 */
164
function cdm_dataportal_settings_validate($form_id, $form_values){
165
  
166
  if (!str_endsWith($form_values['cdm_webservice_url'], '/')) {
167
    form_set_error('cdm_webservice_url', t("The URL to the CDM Web Service must end with a slash: '/'."));
168
  }
169

    
170
  $cdm_webservice_url_changed = variable_get('cdm_webservice_url', '') != $form_values['cdm_webservice_url'];
171
  
172
  if ($cdm_webservice_url_changed) {
173
    $form_values['cdm_secUuid_default'] = '';
174
    //TODO does the currentSecUuid need to be stored?
175
    //   Yes - if switching to another concept is permanent until an other concept switch is used.
176
    //   NO  - if a user only 'visits' other concepts until this context is left by visiting a non taxon page
177
    //         , or explicitly when a concept switch is used.
178
    //         other concept could be activated permanently by personal preference settings
179
    //
180
    //  >> NO seems to be the desired solution <<
181
    //
182
    _cdm_dataportal_set_currentSecUuid(false);
183
    // reset all cdm related data stored in the session
184
    // cdm_dataportal_session_clear($form_values['cdm_webservice_url']); // cdm_webservice_url is not further stored here
185
    // clear the cdm webservice cache
186
    cache_clear_all(NULL, 'cache_cdm_ws');
187
    // better clear secref_cache since i can not be sure if the cache has not be used during this response
188
    cdm_api_secref_cache_clear();
189
  }
190
  if($form_values['cdm_webservice_cache'] != variable_get('cdm_webservice_cache', 1)){
191
    cache_clear_all(NULL, 'cache_cdm_ws');
192
    // better clear secref_cache since i can not be sure if the cache has not be used during this response
193
    cdm_api_secref_cache_clear();
194
  }
195
  
196
}
197

    
198
/* 
199
function cdm_dataportal_session_clear($cdm_ws_uri_update = false){
200
  $_SESSION['cdm'] = null;
201
  if(is_string($cdm_ws_uri_update)){
202
    $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
203
  }
204
}
205

    
206
function cdm_dataportal_session_validate(){
207

    
208
  if(!isset($_SESSION['cdm']['ws_uri'])){
209
    $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', false));
210
  } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', false)){
211
    cdm_dataportal_session_clear(variable_get('cdm_webservice_url', false));
212
  }
213
}
214
*/
215

    
216
function cdm_dataportal_search_taxon_form(){
217
  
218
  $url = 'cdm_dataportal/search/taxon';
219
  $form['#method'] = 'get';
220
  $form['#process'] = array('cdm_dataportal_search_process' => array());
221
  $form['#action'] = url($url, NULL, NULL, true);
222
  
223
  $form['queryString'] = array(
224
    '#type' => 'textfield',
225
    '#size' => 20,
226
    '#attributes' => array('title' => t('Enter the name or part of a name you wish to search for.')),
227
    '#default_value' => (isset($_SESSION['cdm']['search']['queryString']) ? $_SESSION['cdm']['search']['queryString'] : ''),
228
  );
229
  $form['mode'] =  array(
230
    '#type' => 'select',
231
    '#title' => t('Search Mode'),
232
    '#options' => array(
233
        'EXACT' => t('Exact'),
234
        'BEGINNING' => t('Beginning'),
235
        'ANYWHERE' => t('Anywhere')
236
        ),
237
    '#description'   => t('* can always be used as wildcard'),
238
    '#default_value' => (isset($_SESSION['cdm']['search']['mode']) ? $_SESSION['cdm']['search']['mode'] : 'start'),
239
  );
240
  
241
  /*$form['onlyAccepted'] = array(
242
    '#type' => 'checkbox',
243
    '#title' => t('Only Accepted'),
244
    '#attributes' => array('title' => t('Search only for taxa which are accepted by the current treatment.')),
245
    '#default_value' => isset($_SESSION['cdm']['search']['onlyAccepted']) && $_SESSION['cdm']['search']['onlyAccepted'],
246
  );
247
  */
248
  /*
249
  $form['vernacular'] = array(
250
    '#type' => 'checkbox',
251
    '#title' => t('Vernacular Names'),
252
    '#attributes' => array('title' => t('Search for vernacular names.')),
253
    '#default_value' => isset($_SESSION['cdm']['search']['vernacular']) && $_SESSION['cdm']['search']['vernacular'],
254
  );
255
  $form['language'] =  array(
256
    '#type' => 'select',
257
    '#title' => t('Language'),
258
    '#options' => array(
259
        '*' => t('ALL')
260
        ),
261
    '#description'   => t('The language of the vernacular name'),
262
    '#default_value' => (isset($_SESSION['cdm']['search']['language']) ? $_SESSION['cdm']['search']['language'] : ''),
263
  );
264
  */
265
  $form['page'] = array(
266
  '#type' => 'hidden',
267
  '#value' => '1',
268
  );
269
  
270
  $form['submit'] = array(
271
  '#type' => 'submit',
272
  '#name' => '', 
273
  '#value' => t('Search'),
274
  );
275
  
276
  // clean URL get forms breaks if we don't give it a 'q'.
277
  if (!(bool)variable_get('clean_url', '0')) {
278
    $form['q'] = array(
279
      '#type' => 'hidden',
280
      '#value' => $url,
281
      '#name' => 'q',
282
    );
283
  }
284
  return $form;
285
}
286

    
287
//TODO is this function REACHABLE and thus in use??
288
function cdm_dataportal_search_process($form) {
289
  unset($form['form_id']);
290
  unset($form['form_token']);
291
  return $form;
292
}
293

    
294
/**
295
 * Filters $_REQUEST by a list of valid request  parameters and also sets defaults if required.
296
 * returns the processed request parameters submitted by the search form.
297
 */
298
function cdm_dataportal_search_form_request(){
299
  // keys map $REQUEST Keys values are optional default parameters if not null
300
  static $search_form_params = array('queryString'=>null, 'onlyAccepted' =>'0', 'page'=>null,  'pagesize'=>15, 'mode'=>null);
301
  
302
  $form_params = array();
303
  foreach($search_form_params as $key=>$default ){
304
    $form_params[$key] = ( isset($_REQUEST[$key]) && $_REQUEST[$key] != '' ? $_REQUEST[$key] : $default);
305
  }
306
  $_SESSION['cdm']['search'] = $form_params;
307
  return $form_params;
308
}
309
  
310

    
311
/* UNREACHABLE since action of form directly links to view
312
function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
313
  
314
  $_SESSION['cdm']['search'] = $form_values;
315
  //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];      
316
  return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');  
317
  //$paramstr = compose_url_prameterstr($form_values);
318
  //return url('/cdm_dataportal/search/taxon/', $paramstr);
319
}
320
*/
321
/* ====================== menu callback functions ====================== */
322

    
323
/**
324
 * Generate main administration form.
325
 *
326
 * @return
327
 *   An array containing form items to place on the module settings page.
328
 */
329
function cdm_dataportal_settings(){
330

    
331
  $form = cdm_api_settings_form();
332

    
333
  //TODO: settings are still incomplete, compare with trunk/dataportal/inc/config_default.php.inc
334
  $form['cdm_dataportal'] = array(
335
      '#type' => 'fieldset',
336
      '#title' => t('CDM DataPortal'),
337
      '#collapsible' => FALSE,
338
      '#collapsed' => TRUE,
339
  );
340

    
341
  $form['cdm_dataportal']['secuuid_widget'] = array(
342
    '#type' => 'select_secuuid',
343
    '#title' => t('Default Concept Reference'),
344
    '#description'   => t('The default \'sensu\' reference to start the cdm_dataportal with. Per user choices are possible and may override this value.'),
345
    '#varname' => 'cdm_secUuid_default',
346
    '#multiple' => false
347
  );
348
  
349
  return system_settings_form($form);
350
}
351

    
352
/**
353
 * Implementation of hook_elements().
354
 */
355
function cdm_dataportal_elements() {
356
  $type['select_secuuid'] = array(
357
    '#input' => TRUE, 
358
    '#process' => array('cdm_dataportal_select_secuuid_expand' => array()),
359
    //'#validate' => array('cdm_dataportal_select_secuuid_validate' => array()),
360
    //'#default_value' => array(),
361
  );
362
  return $type;
363
}
364

    
365

    
366
/*
367
 * 
368
 */
369
function cdm_dataportal_select_secuuid_expand($element){
370
  
371
  $element['#tree'] = FALSE;
372
  
373
  // any value submitted?
374
  if(isset($element['#post'][$element['#varname']])){
375
    $selected_values = $element['#post'][$element['#varname']];
376
  } else {
377
    // use those store in drupal
378
    $selected_values = variable_get( $element['#varname'], array());
379
  }
380
  if(!is_array($selected_values)){
381
    $selected_values = array($selected_values);
382
  }
383
  $options = array();
384
  foreach($selected_values as $secUuid){
385
    $options[$secUuid] = cdm_taxontree_secRefTitle_for($secUuid);
386
  }
387

    
388
  $element[$element['#varname']] =  array(
389
    '#type' => 'select',
390
    '#options' => $options,
391
    '#default_value' => array_values($options),
392
    '#size' => $element['#multiple'] ? 12 : 2,
393
    '#multiple' => $element['#multiple'],
394
  );
395
  return $element;
396
}
397

    
398

    
399

    
400
/**
401
 * Displays a list of the known taxonomic names. Long lists are split up into multiple pages
402
 *
403
 * TODO: parameters are still preliminar
404
 * @param String $page page number to diplay defaults to page 1
405
 * @param boolean $hide_unaccepted whether to hide nams which are not accepted by the current view
406
 */
407
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = false ){
408

    
409
  $request_params  = array(
410
    'q' => $beginsWith, 
411
         //'sec' = '',
412
         //'higherTaxa' => getFiters(),
413
          // 'matchAnywhere' => false, // default is false
414
    'page' => $page, 
415
    'onlyAccepted' => $onlyAccepted, 
416
    'pagesize' => 20  /*$_SESSION['cdm']['namelist_pagesize'] */);
417
  
418
  $taxonSTOs = cdm_ws_get(CDM_WS_FIND_TAXA, $request_params);
419
  /*
420
   * FIXME the filter for accepted names will be a form element, thus this widget
421
   * should be generated via form api preferably as block
422
   */
423
  //$out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
424
  //$out .= theme('cdm_dataportal_widget_names_list', $names, $page);
425
  $out .= theme('cdm_listof_taxa', $taxonSTOs);
426
  return $out;
427
}
428

    
429

    
430
/**
431
 * The taxon page gives detailed information on a taxon, it shows:
432
 *  - Taxon name
433
 *  - Full list of synonyms homotypic synonyms on top, followed by the
434
 *    heterotypic and finally followed by misapplied names.
435
 *    The list is ordered historically.
436
 *  - All facts associated with the very taxon concept and taxon name.
437
 *
438
 * @param String $uuid the UUID of the taxon
439
 */
440
function cdm_dataportal_view_taxon($uuid, $arg2 = null){
441

    
442

    
443
  if(isset($arg2) && !is_numeric($arg2)){
444
   
445
    switch ($args[0]){
446
      case 'alternatives':
447
        $alternatives = cdm_ws_get(CDM_WS_TAXON_ALTERNATIVES, $uuid);
448
        return theme('cdm_alternative_taxa', $alternatives);
449
    }
450
    
451
  } else {
452
  
453
    // display the page for the taxon defined by $uuid
454
    $taxonTO = cdm_ws_get(CDM_WS_TAXON ,$uuid);
455
    if(!$taxonTO){
456
      drupal_set_title(t('Taxon does not exist'));
457
      return false;   
458
    }
459
    _cdm_dataportal_set_currentSecUuid($taxonTO->sec->uuid);
460
  
461
    drupal_set_title(theme('cdm_taxon', $taxonTO, true));
462
    //TODO retrieve complete synonymy and other data
463
  
464
    $out = theme('cdm_taxon_page', $taxonTO);
465
    return $out;
466
  }
467
}
468

    
469

    
470

    
471
/**
472
 * Expected URL request parameters:
473
 *  - text
474
 *  - language
475
 *  - vernacular
476
 * 
477
 * future extensions to meet palmweb mockup requirements:
478
 *  - habitat
479
 *  - uses
480
 *  - conservation status
481
 *  - locality / tdwg region
482
 */
483
function cdm_dataportal_view_search_taxon(){
484
  
485
  $request_params = cdm_dataportal_search_form_request();
486
  $sw_params = array_replace_key($request_params, array('queryString'=> 10 ));
487
  $resultPageSTO = cdm_ws_get(CDM_WS_FIND_TAXA, $sw_params);
488
  $resultPageSTO->pageNumber = $request_params['page'];
489
  return theme('cdm_search_results', $resultPageSTO, 'cdm_dataportal/search/taxon', $request_params);
490
}
491

    
492

    
493

    
494
function cdm_view_xml2json(){
495
  $file = arg(2);
496
  $datastr = get_content(variable_get('cdm_webservice_url', '').$file);
497
  return  xml2json::transformXmlStringToJson($datastr);
498

    
499
}
500

    
501
/**
502
 * Get a term for the current locale from the $localised_terms array.
503
 * Uses global variable $locale from drupal (see drupal functions locale_initialize() for details)
504
 * Function name is adapted from the drupal function t().
505
 *
506
 */
507
function cdm_dataportal_t($localised_terms){
508
  global $locale;  // drupal variable containing the current locale
509
  return cdm_get_localised_term($localised_terms, $locale);
510
}
511

    
512
/* ====================== other functions ====================== */
513

    
514
/**
515
 * Enter description here...
516
 *
517
 * @param String $uuid the UUID of the taxon
518
 * @return the URL
519
 */
520
function cdm_dataportal_taxon_path($uuid){
521
  return 'cdm_dataportal/taxon/'.$uuid;
522
}
523

    
524
/**
525
 * Creates a short taxonname by using the taggename field of NameSTO or NameTO instances.
526
 * If the taggename if empty the fullname will be returned.
527
 *
528
 * @param unknown_type $NameSTO a NameSTO, TreeNode or NameTO instance
529
 * @return string
530
 */
531
function cdm_dataportal_shortname_of($NameSTO){
532

    
533
  $name = '';
534
  // get all tagged text tokens of the scientific name
535
  foreach($NameSTO->taggedName as $tagtxt){
536
      if($tagtxt->type == 'name' || $tagtxt->type == 'rank'){
537
        $name .= ($name ? ' ' : '').$tagtxt->text;
538
      }
539
  }
540
  $name = trim($name);
541
  if($name){
542
    if(strpos($name, 'Incertae sedis') === false && $pos = stripos($name, ' ')){
543
      return substr($name, 0, 1).'. '.substr($name, $pos);
544
    } else {
545
      return $name;
546
    }
547
  } else {
548
    return $NameSTO->fullname;
549
  }
550
}
551

    
552
/**
553
 * @param UUID $secUuid
554
 */
555
function _cdm_dataportal_set_currentSecUuid($secUuid){
556

    
557
  if(is_array($secUuid)){
558
    $secUuid = $secUuid[0];
559
  }
560
  if( !isset($_SESSION['cdm']['currentSecRef']['uuid']) ||  $_SESSION['cdm']['currentSecRef']['uuid'] != $secUuid){
561
    if(!$secUuid){
562
      $_SESSION['cdm']['currentSecRef'] = null;
563
    } else {
564
      $secRef = cdm_ws_get(CDM_WS_SIMPLE_REFERENCE ,$secUuid);
565
      if(isset($secRef[0])){
566
        $_SESSION['cdm']['currentSecRef'] = (array)($secRef[0]);
567
      }
568
    }
569
  }
570
}
571

    
572
/**
573
 * returns the current secRef array from the users session. 
574
 * If the according session variable is not jet set the default
575
 * as configured in the setting is used otherwise null.
576
 * 
577
 * currentSecRef['uuid']
578
 * currentSecRef[....
579
 *
580
 * @return array
581
 */
582
function _cdm_dataportal_currentSecRef_array(){
583
  
584
  if( !isset($_SESSION['cdm']['currentSecRef']['uuid'])){
585
     $secUuid = variable_get('cdm_secUuid_default', null);
586
     _cdm_dataportal_set_currentSecUuid($secUuid);
587
  }
588
  return  $_SESSION['cdm']['currentSecRef'];
589
}
590

    
591
function _cdm_dataportal_acceptetByCurrentView($taxonTO){
592
  $current_secref = _cdm_dataportal_currentSecRef_array();
593
  
594
  return $current_secref->uuid == $taxonTO->secuuid && $taxonTO->accepted;
595
}
596

    
597
/**
598
 * @param $str the string to truncate
599
 * @param $len the maximun length
600
 * @param $appendix an optional appendix.
601
 *
602
 * @return the string truncated to the specified length or the original string as given as parameter.
603
 * if an appendix has been defined the resulting string
604
 * will have the specified length inculding the the appendix.
605
 */
606
function str_trunk(&$str, $len, $appendix=''){
607
  if(strlen($str) >= $len )
608
  return  substr($str, 0, $len - strlen($appendix)).$appendix;
609
  else
610
  return $str;
611
}
612

    
613
/**
614
 * @param string $str
615
 * @param string $sub
616
 * @return boolean
617
 */
618
function str_beginsWith( $str, $sub ) {
619
  return ( substr( $str, 0, strlen( $sub ) ) === $sub );
620
}
621

    
622
/**
623
 *
624
 * @param string $str
625
 * @param string $sub
626
 * @return boolean
627
 */
628
function str_endsWith( $str, $sub ) {
629
  return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
630
}
631

    
632

    
633
function array_replace_key($array, $replace_map){
634
  foreach($replace_map as $key=>$newkey){
635
    if(isset($array[$key])){
636
      $array[$newkey] = $array[$key];
637
      unset($array[$key]);
638
    }
639
  }
640
  return $array;
641
}
642

    
643

    
644
function compose_url_prameterstr($parameters = array()){
645
  $pstr = '';
646
  foreach($parameters as $key=>$value){
647
     $pstr .= ($pstr ? '&' :'').$key.'='.urlencode($value);
648
  }
649
  return $pstr;
650
}
651

    
(5-5/9)