Project

General

Profile

Download (46.2 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('node_types.php');
15
require_once('cdm_dataportal.theme.php');
16

    
17

    
18
define(CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE, 25);
19

    
20
/* ====================== hook implementations ====================== */
21

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

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

    
40

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

    
55

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

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

    
205
/**
206
 * Implementation of hook_block()
207
 *
208
 * Provides the following blocks:
209
 *  0: list of links useful during development
210
 *
211
 * @param String $op
212
 * @param int $delta
213
 */
214
function cdm_dataportal_block($op='list', $delta=0) {
215
  // listing of blocks, such as on the admin/block page
216
  if ($op == "list") {
217
    //$block[0]["info"] = t("CDM DataPortal DevLinks");
218
    $block[1]["info"] = t("CDM DataPortal Credits");
219
    $block[2]["info"] = t("CDM Search Taxa");
220
    //$block[3]["info"] = t("CDM Filters");
221
    $block[4]["info"] = t("CDM Dataportal Print");
222
    return $block;
223
  }
224
  else if ($op == 'view') {
225
    switch($delta){
226
      case 1:
227
        $block['subject'] = t('Credits');
228
        $block['content'] = theme('cdm_credits');
229
        return $block;
230
      case 2:
231
        $block['subject'] = t('Search Taxa');
232
        $block['content'] = drupal_get_form('cdm_dataportal_search_taxon_form');
233
        $block['content'] .= '<div>'.l('Advanced Search', '/cdm_dataportal/search').'</div>';
234
        return $block;
235
      case 4:
236
      	$block['subject'] = t('Print this page');
237
      	$block['content'] = theme('cdm_print_button');;
238
      	return $block;   
239
     }
240
  }
241
}
242

    
243

    
244
/**
245
 * Implementation of hook_validate()
246
 *
247
 * @param $element
248
 */
249
function cdm_dataportal_settings_validate($form_id, $form_values){
250
  
251
  if (!str_endsWith($form_values['cdm_webservice_url'], '/')) {
252
    form_set_error('cdm_webservice_url', t("The URL to the CDM Web Service must end with a slash: '/'."));
253
  }
254

    
255
  $cdm_webservice_url_changed = variable_get('cdm_webservice_url', '') != $form_values['cdm_webservice_url'];
256
  
257
  $cdm_secUuid_default_changed = variable_get('cdm_secUuid_default', '') != $form_values['cdm_secUuid_default'];
258
  
259
  if ($cdm_webservice_url_changed || $cdm_secUuid_default_changed) {
260
    
261
    _cdm_dataportal_set_currentSecUuid($form_values['cdm_secUuid_default']);
262
    // reset all cdm related data stored in the session
263
    // cdm_dataportal_session_clear($form_values['cdm_webservice_url']); // cdm_webservice_url is not further stored here
264
    // clear the cdm webservice cache
265
    cache_clear_all(NULL, 'cache_cdm_ws');
266
    // better clear secref_cache since i can not be sure if the cache has not be used during this response
267
    cdm_api_secref_cache_clear();
268
  }
269
  if($form_values['cdm_webservice_cache'] != variable_get('cdm_webservice_cache', 1)){
270
    cache_clear_all(NULL, 'cache_cdm_ws');
271
    // better clear secref_cache since i can not be sure if the cache has not be used during this response
272
    cdm_api_secref_cache_clear();
273
  }
274
  
275
}
276

    
277
/*
278
function cdm_dataportal_session_clear($cdm_ws_uri_update = false){
279
  $_SESSION['cdm'] = null;
280
  if(is_string($cdm_ws_uri_update)){
281
    $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
282
  }
283
}
284

    
285
function cdm_dataportal_session_validate(){
286

    
287
  if(!isset($_SESSION['cdm']['ws_uri'])){
288
    $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', false));
289
  } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', false)){
290
    cdm_dataportal_session_clear(variable_get('cdm_webservice_url', false));
291
  }
292
}
293
*/
294

    
295
function cdm_dataportal_search_taxon_form($advancedForm = false){
296
  
297
  $preset_query = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
298
  $preset_doTaxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
299
  $preset_doSynonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
300
  $preset_doTaxaByCommonNames = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
301
  $tdwg_level_select =  (isset($_SESSION['cdm']['search']['tdwg_level_select']) ? $_SESSION['cdm']['search']['tdwg_level_select'] : 2);
302
  $selected_areas =  (isset($_SESSION['cdm']['search']['area']) ? $_SESSION['cdm']['search']['area'] : false);
303
  
304
  $url = 'cdm_dataportal/search/taxon';
305
  $form['#method'] = 'get';
306
  $form['#process'] = array('cdm_dataportal_search_process' => array());
307
  $form['#action'] = url($url, NULL, NULL, true);
308
  
309
  $form['query'] = array(
310
    '#delta' => 0,
311
    '#type' => 'textfield',
312
    '#size' => 20,
313
    '#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')),
314
    '#value' => $preset_query,
315
  );
316
  
317
  $form['search'] = array(
318
      '#delta' => 1,
319
      '#tree' => true,
320
      //'#type' => $advancedForm ? 'fieldset': 'hidden',
321
      '#title' => t('Options')
322
    );
323
    
324
   $form['search']['tree'] = array(
325
    '#delta' => -1,
326
    '#type' => 'hidden',
327
    '#value' => variable_get('cdm_taxonomictree_uuid', false)
328
  );
329
  
330
  
331
  // clean URL get forms breaks if we don't give it a 'q'.
332
  if (!(bool)variable_get('clean_url', '0')) {
333
    $form['search']['q'] = array(
334
      '#delta' => -1,
335
      '#type' => 'hidden',
336
      '#value' => $url,
337
      '#name' => 'q',
338
    );
339
  }
340
  
341
  $form['search']['pageSize'] = array(
342
      '#delta' => -1,
343
      '#type' => 'hidden',
344
      '#value' => variable_get('cdm_dataportal_search_items_on_page', 25)
345
    );
346

    
347
  if($advancedForm){
348
    // general search parameters
349
    $form['search']['doTaxa'] = array(
350
      '#delta' => 2,
351
      '#type' => 'checkbox',
352
      '#title' => t('Search for accepted taxa'),
353
      '#value' => $preset_doTaxa
354
    );
355
    $form['search']['doSynonyms'] = array(
356
      '#delta' => 3,
357
      '#type' => 'checkbox',
358
      '#title' => t('Search for synonyms'),
359
      '#value' => $preset_doSynonyms
360
    );
361
    $form['search']['doTaxaByCommonNames'] = array(
362
      '#delta' => 4,
363
      '#type' => 'checkbox',
364
      '#title' => t('Search for common names'),
365
      '#value' => $preset_doTaxaByCommonNames
366
    );
367
     
368
    // Geographic Range
369
    $form['search']['geographic_range'] = array(
370
      '#type' => 'fieldset',
371
      '#delta' => 5,
372
      '#tree' => true,
373
      '#title' => t('Geographic range'),
374
    );
375
    
376
    $form['search']['geographic_range']['tdwg_level_select'] = array(
377
    '#type' => 'radios',
378
    '#title' => t('Select a TDWG distribution level and code'),
379
    '#default_value' => $tdwg_level_select,
380
    '#options' => array(
381
      t('TDWG level-1, i.e. a continent'),
382
      t('TDWG level-2'),
383
      t('TDWG level-3, i.e. a country'),
384
      t('TDWG level-4')
385
      )     
386
    );
387
    $tdwg[1] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '1');
388
    $tdwg[2] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '2');
389
    $tdwg[3] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '3');
390
    $tdwg[4] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '4');
391
    
392
    $tdwg_js = '';
393
    foreach($tdwg as $key=>$tdwg_level){
394
      $tdwgOptions = array();
395
      $tdwgOptionsSelected = array();
396
      foreach($tdwg_level as $area){
397
        $representation = $area->representations[0];
398
        $tdwgOptions[$representation->abbreviatedLabel] = $area->representation_L10n;
399
        if(is_array($selected_areas) && in_array($representation->abbreviatedLabel, $selected_areas)){
400
          $tdwgOptionsSelected[] = $representation->abbreviatedLabel; //$area->uuid;
401
        }
402
      }
403
      asort($tdwgOptions);
404
      $form['search']['geographic_range']['tdwg_level_'.$key] = array(
405
        '#type' => 'select',
406
        '#title'         => t('TDWG level').' '.$key,
407
        '#default_value' => $tdwgOptionsSelected,
408
        '#multiple' => TRUE, 
409
        '#options' => $tdwgOptions
410
      );   
411
      $tdwg_js .= "$('#edit-search-geographic-range-tdwg-level-$key').parent()".($tdwg_level_select + 1 == $key ?  '.show()' : '.hide()'). ";\n";  
412
    }
413

    
414
    drupal_add_js(
415
    "$(document).ready(function(){
416
    
417
      $(\"input[@name='search[geographic_range][tdwg_level_select]']\").change( 
418
        function(event){
419
          var selectId = $(\"input[@name='search[geographic_range][tdwg_level_select]']:checked\").val();
420
          var i;
421
          for(i = 0; i < 4; i++){
422
            if(selectId == i){
423
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1) ).parent().fadeIn('slow');
424
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
425
            } else {
426
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).parent().fadeOut('slow');
427
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
428
            }
429
          }
430
        }
431
      );
432
      
433
     $tdwg_js
434
    });",
435
    'inline');
436
      
437
  } else {
438
    $form['search']['doTaxa'] = array(
439
      '#delta' => -2,
440
      '#type' => 'hidden',
441
      '#value' => 1
442
     );
443
      $form['search']['doSynonyms'] = array(
444
      '#delta' => -3,
445
      '#type' => 'hidden',
446
      '#value' => 1
447
     );
448
     $form['search']['doTaxaByCommonNames'] = array(
449
      '#delta' => -4,
450
      '#type' => 'hidden',
451
      '#value' => 1
452
     );
453
  }
454
  
455
   $form['submit'] = array(
456
  '#delta' => 9,
457
  '#type' => 'submit',
458
  '#name' => '',
459
  '#value' => t('Search')
460
  );
461
  
462
  return $form;
463
}
464

    
465
function cdm_dataportal_search_taxon_form_advanced(){
466
  return cdm_dataportal_search_taxon_form(true);
467
}
468

    
469
/**
470
 * Implementation #process method call, see form_builder()
471
 * <p>
472
 * Removes Drupal internal form elements from query
473
 * @param $form
474
 * @return unknown_type
475
 */
476
function cdm_dataportal_search_process($form) {
477
  unset($form['form_id']);
478
  unset($form['form_token']);
479
  return $form;
480
}
481

    
482
/**
483
 * Filters $_REQUEST by a list of valid request  parameters and also sets defaults if required.
484
 * returns the processed request parameters submitted by the search form.
485
 */
486
function cdm_dataportal_search_form_request(){
487

    
488
    
489
  $form_params = array();
490
  array_deep_copy($_REQUEST['search'], $form_params);
491
  $form_params['query'] =  trim($_REQUEST['query']);
492

    
493
  // split of  geographic range
494
  if(isset($_REQUEST['search']['geographic_range'])){
495
    $geographicRange = $_REQUEST['search']['geographic_range'];
496
    // remove 
497
    unset($form_params['geographic_range']);
498
  }
499
  
500
  // add geographic range
501
  if($geographicRange){
502
    $form_params['tdwg_level_select'] = $geographicRange['tdwg_level_select'];
503
    for($i = 1; $i < 5; $i++){
504
      if(isset($geographicRange['tdwg_level_'.$i])){
505
        $form_params['area'] = $geographicRange['tdwg_level_'.$i];
506
      }
507
    }
508
  }
509
  
510
  // store in session
511
  $_SESSION['cdm']['search'] = $form_params;
512
  
513
  return $form_params;
514
}
515
  
516

    
517
/* UNREACHABLE since action of form directly links to view
518
function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
519
  
520
  $_SESSION['cdm']['search'] = $form_values;
521
  //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
522
  return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
523
  //$paramstr = compose_url_prameterstr($form_values);
524
  //return url('/cdm_dataportal/search/taxon/', $paramstr);
525
}
526
*/
527
/* ====================== menu callback functions ====================== */
528

    
529
/**
530
 * Generate main administration form.
531
 *
532
 * @return
533
 *   An array containing form items to place on the module settings page.
534
 */
535
function cdm_dataportal_settings(){
536

    
537
  $form = cdm_api_settings_form();
538

    
539
  //TODO: settings are still incomplete, compare with trunk/dataportal/inc/config_default.php.inc
540
  $form['cdm_dataportal'] = array(
541
      '#type' => 'fieldset',
542
      '#title' => t('CDM DataPortal'),
543
      '#collapsible' => FALSE,
544
      '#collapsed' => TRUE,
545
  );
546
  
547
//  $form['cdm_dataportal']['secuuid_widget'] = array(
548
//    '#type' => 'select_secuuid', // => will be themed by theme_select_secuuid(...)
549
//    '#title' => t('Default Concept References'),
550
//    '#description'   => t('The default \'sensu\' reference to start the CDM DataPortal with. Per user choices are possible and may override this value.'),
551
//    '#varname' => 'cdm_secUuid_default',
552
//    '#multiple' => false
553
//  );
554
  
555
  $taxonTrees = cdm_ws_get(CDM_WS_TAXONOMY);
556
  foreach($taxonTrees as $tree){
557
    $taxonomicTreeOptions[$tree->uuid] = $tree->titleCache;  
558
  }
559
  $form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
560
    '#type' => 'select',
561
    '#title'         => t('Available taxonomic views'),
562
    '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
563
    '#options' => $taxonomicTreeOptions,
564
    '#description'   => t('Select a taxonomic view for this webservice.')
565
  );
566
  
567
//  $form['cdm_dataportal']['DEBUG']= array(
568
//    '#value' => '<pre>'.print_r($taxonTrees, true).'</pre>'
569
//  );
570
  
571
    
572
  //------------------ FEATURE TREE --------------------//
573
  
574
  $form['cdm_dataportal']['feature_tree'] = array(
575
    '#type' => 'fieldset',
576
    '#title' => t('Feature Tree'),
577
    '#collapsible' => TRUE,
578
    '#collapsed' => TRUE,
579
  );
580
  
581
    $form['cdm_dataportal']['feature_tree']['cdm_dataportal_featuretree_uuid'] = array(
582
    '#type' => 'select',
583
    '#title'         => t('Available FeatureTrees'),
584
    '#default_value' => variable_get('cdm_dataportal_featuretree_uuid', false),
585
	  '#options' => _get_feature_trees(),
586
    '#description'   => t('Select a FeatureTree for this webservice.'
587
          //.' If there is no applicable FeatureTree you can create a new one using the <a href="">FeatureTreeManager</a>'
588
          )
589
  );
590
  
591
  //------------------ LAYOUT --------------------//
592
  
593
  $form['cdm_dataportal']['layout'] = array(
594
    '#type' => 'fieldset',
595
    '#title' => t('Layout'),
596
    '#collapsible' => TRUE,
597
    '#collapsed' => TRUE,
598
  );
599
  
600
  $form['cdm_dataportal']['layout']['cdm_taxonname_type'] = array(
601
    '#type' => 'select',
602
    '#title'         => t('Taxon name type'),
603
    '#default_value' => variable_get('cdm_taxonname_type', 'BotanicalName'),
604
    '#options' => array( 'BotanicalName'=>t('BotanicalName'), 'ZoologicalName'=>t('ZoologicalName')),
605
    '#description'   => t('')
606
  );
607
  
608
  $form['cdm_dataportal']['layout']['cdm_dataportal_nomref_in_title'] = array(
609
    '#type' => 'checkbox',
610
    '#title' => t('Hide full nomenclatural reference in title'),
611
    '#default_value' => variable_get('cdm_dataportal_nomref_in_title', 1),
612
    '#description' => t('Uncheck this, if you want the title of a taxon page consisting of name and author plus publication year only.
613
    					If not checked, the first homotypic taxon is a repetition of the accepted taxon with
614
    					the full nomenclatural reference.')
615
  );
616
  
617
  $form['cdm_dataportal']['layout']['cdm_dataportal_display_name_relations'] = array(
618
    '#type' => 'checkbox',
619
    '#title' => t('Show name relations of accepted taxa on taxon page'),
620
    '#default_value' => variable_get('cdm_dataportal_display_name_relations', 1),
621
    '#description' => t('')
622
  );
623
  
624
  $form['cdm_dataportal']['layout']['cdm_dataportal_name_relations_skiptype_basionym'] = array(
625
    '#type' => 'checkbox',
626
    '#title' => t('Exclude the basionym relationship type from the taxon page'),
627
    '#default_value' => variable_get('cdm_dataportal_name_relations_skiptype_basionym', 1),
628
    '#description' => t('')
629
  );
630
  
631
  $form['cdm_dataportal']['layout']['cdm_dataportal_taxonpage_tabs'] = array(
632
    '#type' => 'checkbox',
633
    '#title' => t('Tabbed taxon page'),
634
    '#default_value' => variable_get('cdm_dataportal_taxonpage_tabs', 1),
635
    '#description' => t('Split the taxon page into individual tabs for description, images, synonymy')
636
  );
637
  
638
  $form['cdm_dataportal']['layout']['cdm_dataportal_search_items_on_page'] = array(
639
    '#type' => 'textfield',
640
    '#title' => t('Search Page Size'),
641
    '#default_value' => variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE),
642
    '#description' => t('Number of Names to display per page in search results.')
643
  );
644
  
645
  $form['cdm_dataportal']['layout']['cdm_dataportal_descriptions_separated'] = array(
646
    '#type' => 'checkbox',
647
    '#title' => t('Separate Desriptions'),
648
    '#default_value' => variable_get('cdm_dataportal_descriptions_separated', 0),
649
    '#description' => t('By default corresponding elements of different descriptions are joined together'
650
                        .' into a common section per feature (i.e. type of description).'
651
                        .' Check this box to allow displaying all descriptions separately.')
652
  );
653
  
654
  $options = cdm_rankVocabulary_as_option();
655
  array_unshift($options, '-- DISABLED --');
656
  $form['cdm_dataportal']['layout']['image_hide_rank'] =  array(
657
    '#type'          => 'select',
658
    '#title'         => t('Hide Images for Taxa above'),
659
    '#default_value' => variable_get('image_hide_rank', '0'),
660
    '#options'       => $options,
661
    '#description'   => t(''),
662
  );
663
  
664
  // --- find taxa ---- //
665
   $form['cdm_dataportal']['layout']['findtaxa'] = array(
666
    '#type' => 'fieldset',
667
    '#title' => t('Search Taxa'),
668
    '#collapsible' => TRUE,
669
    '#collapsed' => TRUE,
670
  );
671
  
672
  //variable_get('', 1);
673
  $form['cdm_dataportal']['layout']['findtaxa']['cdm_dataportal_findtaxa_show_taxon_thumbnails'] = array(
674
    '#type' => 'checkbox',
675
    '#title' => t('Show media thumbnails for accepted taxa'),
676
    '#default_value' => variable_get('cdm_dataportal_findtaxa_show_taxon_thumbnails', 1),
677
    '#description' => t('')
678
  );
679
  
680
  $form['cdm_dataportal']['layout']['findtaxa']['cdm_dataportal_findtaxa_show_synonym_thumbnails'] = array(
681
    '#type' => 'checkbox',
682
    '#title' => t('Show media thumbnails for synonyms'),
683
    '#default_value' => variable_get('cdm_dataportal_findtaxa_show_synonym_thumbnails', 0),
684
    '#description' => t('')
685
  );
686
  
687
  //   $showCaption = variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 0);
688
  $form['cdm_dataportal']['layout']['findtaxa']['cdm_dataportal_findtaxa_show_thumbnail_captions'] = array(
689
    '#type' => 'checkbox',
690
    '#title' => t('Show captions under thumbnails'),
691
    '#default_value' => variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 1),
692
    '#description' => t('')
693
  );
694
  
695
   $form['cdm_dataportal']['layout']['findtaxa']['cdm_dataportal_findtaxa_media_maxextend'] = array(
696
    '#type' => 'textfield',
697
    '#title' => t('Maximum extend of Images'),
698
    '#default_value' => variable_get('cdm_dataportal_findtaxa_media_maxextend', 120),
699
    '#description' => t('')
700
  );
701
  
702
  $form['cdm_dataportal']['layout']['findtaxa']['cdm_dataportal_findtaxa_media_cols'] = array(
703
    '#type' => 'textfield',
704
    '#title' => t('Number of columns'),
705
    '#default_value' => variable_get('cdm_dataportal_findtaxa_media_cols', 3),
706
    '#description' => t('')
707
  );
708
  
709
  $form['cdm_dataportal']['layout']['findtaxa']['cdm_dataportal_findtaxa_media_maxRows'] = array(
710
    '#type' => 'textfield',
711
    '#title' => t('Maximum number of rows'),
712
    '#default_value' => variable_get('cdm_dataportal_findtaxa_media_maxRows', 1),
713
    '#description' => t('')
714
  );
715
  
716
  
717
 
718
  //---------------- GEOSERVICE ----------------//
719
  
720
  $form['cdm_dataportal']['geoservice'] = array(
721
    '#type' => 'fieldset',
722
    '#title' => t('Geoservice'),
723
    '#collapsible' => TRUE,
724
    '#collapsed' => TRUE,
725
  );
726
  
727
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_map_openlayers'] = array(
728
    '#type' => 'checkbox',
729
    '#title' => t('OpenLayers Viewer'),
730
    '#default_value' => variable_get('cdm_dataportal_map_openlayers', 1),
731
    '#description' => t('Display the maps in an interactive viewer which allows zooming and panning.')
732
  );
733
  
734
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_geoservice_access_point'] = array(
735
    '#type' => 'textfield',
736
    '#title' => t('Geoservice Access Point'),
737
    '#default_value' => variable_get('cdm_dataportal_geoservice_access_point', ''),
738
    '#description' => t('Base URL of the geoservice to be used by this portal')
739
  );
740
  
741
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_geoservice_display_width'] = array(
742
    '#type' => 'textfield',
743
    '#title' => t('Geoservice Display Width'),
744
    '#default_value' => variable_get('cdm_dataportal_geoservice_display_width', 390),
745
    '#description' => t('Width of the image generated by geoservice')
746
  );
747
  
748
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_geoservice_bounding_box'] = array(
749
    '#type' => 'textfield',
750
    '#title' => t('Fixed Geoservice Bounding Box'),
751
    '#default_value' => variable_get('cdm_dataportal_geoservice_bounding_box', '-180,-90,180,90'),
752
    '#description' => t('Define urrounding of area to be displayed in maps. Use "-180,-90,180,90" for the whole world. Leave <strong>empty</strong> to let the map <strong>automatically zoom</strong> to the distribution area.')
753
  );
754
  
755
   //cdm_dataportal_geoservice_labels_on
756
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_geoservice_labels_on'] = array(
757
    '#type' => 'checkbox',
758
    '#title' => t('Display Country Labels'),
759
    '#default_value' => variable_get('cdm_dataportal_geoservice_labels_on', FALSE),
760
    '#description' => t('Check this if you like country names to be displayed in the maps. ')
761
  );
762
  
763
   //cdm_dataportal_geoservice_legend_on
764
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_geoservice_legend_on'] = array(
765
    '#type' => 'checkbox',
766
    '#title' => t('Display a map legend'),
767
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_on', TRUE),
768
    '#description' => t('Check this if you like a legend to be displayed with the maps. ')
769
  );
770
  
771
   //cdm_dataportal_geoservice_legend_on
772
  $form['cdm_dataportal']['geoservice']['cdm_dataportal_geoservice_map_caption'] = array(
773
    '#type' => 'textfield',
774
    '#title' => t('Map Caption'),
775
    '#default_value' => variable_get('cdm_dataportal_geoservice_map_caption', ''),
776
    '#description' => t('Define a caption for the map.')
777
  );
778
  
779
  return system_settings_form($form);
780
}
781

    
782
/**
783
 * Implementation of hook_elements().
784
 */
785
function cdm_dataportal_elements() {
786
  $type['select_secuuid'] = array(
787
    '#input' => TRUE,
788
    '#process' => array('cdm_dataportal_select_secuuid_expand' => array()),
789
    //'#validate' => array('cdm_dataportal_select_secuuid_validate' => array()),
790
    //'#default_value' => array(),
791
  );
792
  return $type;
793
}
794

    
795
/*
796
 *
797
 */
798
function cdm_dataportal_select_secuuid_expand($element){
799
  
800
  $element['#tree'] = FALSE;
801
  
802
  // any value submitted?
803
  if(isset($element['#post'][$element['#varname']])){
804
    $selected_values = $element['#post'][$element['#varname']];
805
  } else {
806
    // use those store in drupal
807
    $selected_values = variable_get( $element['#varname'], array());
808
  }
809
  if(!is_array($selected_values)){
810
    $selected_values = array($selected_values);
811
  }
812
  $options = array();
813
  foreach($selected_values as $secUuid){
814
    $options[$secUuid] = cdm_taxontree_secRefTitle_for($secUuid);
815
  }
816

    
817
  $element[$element['#varname']] =  array(
818
    '#type' => 'select',
819
    '#options' => $options,
820
    '#default_value' => array_values($options),
821
    '#size' => $element['#multiple'] ? 12 : 2,
822
    '#multiple' => $element['#multiple'],
823
  );
824
  return $element;
825
}
826

    
827

    
828

    
829
/**
830
 * Displays a list of the known taxonomic names. Long lists are split up into multiple pages
831
 *
832
 * TODO: parameters are still preliminar
833
 * @param String $page page number to diplay defaults to page 1
834
 * @param boolean $hide_unaccepted whether to hide nams which are not accepted by the current view
835
 */
836
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = false ){
837

    
838
  $request_params  = array(
839
    'q' => $beginsWith,
840
         //'sec' = '',
841
         //'higherTaxa' => getFiters(),
842
          // 'matchAnywhere' => false, // default is false
843
    'page' => $page,
844
    'onlyAccepted' => $onlyAccepted,
845
    'pagesize' => 20  /*$_SESSION['cdm']['namelist_pagesize'] */);
846
  
847
  $taxonPager = cdm_ws_find(CDM_WS_FIND_TAXA, $request_params);
848
  /*
849
   * FIXME the filter for accepted names will be a form element, thus this widget
850
   * should be generated via form api preferably as block
851
   */
852
  //$out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
853
  //$out .= theme('cdm_dataportal_widget_names_list', $names, $page);
854
  $out .= theme('cdm_listof_taxa', $taxonPager);
855
  return $out;
856
}
857

    
858
function cdm_dataportal_view_reference($uuid, $arg2 = null){
859
    $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
860
    return theme('cdm_reference_page', $reference);
861
}
862

    
863
function cdm_dataportal_view_reference_list($pageNumber){
864
    $referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE), $pageNumber);
865
    return theme('cdm_reference_pager', $referencePager, 'cdm_dataportal/reference/list/');
866
}
867

    
868
function cdm_dataportal_view_media($mediaUuid, $mediarepresentation_uuid = false, $part = 0){
869
   	$media = cdm_ws_get(CDM_WS_MEDIA, $mediaUuid);
870
   return theme('cdm_media_page', $media, $mediarepresentation_uuid, $part);
871
}
872

    
873
/**
874
 * The taxon page gives detailed information on a taxon, it shows:
875
 *  - Taxon name
876
 *  - Full list of synonyms homotypic synonyms on top, followed by the
877
 *    heterotypic and finally followed by misapplied names.
878
 *    The list is ordered historically.
879
 *  - All description associated with the taxon.
880
 *
881
 * @param $uuid
882
 * @param $chapter name of the part to display,
883
 *         valid values are: 'description', 'images', 'synonymy', 'all'
884
 * @return unknown_type
885
 */
886
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all'){
887
  
888
    // display the page for the taxon defined by $uuid
889
    $taxonpage = cdm_dataportal_taxon_view($uuid, $chapter); 
890
    return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title , $taxonpage->content);
891
}
892

    
893
/**
894
 * @param $uuid
895
 * @param $chapter name of the part to display,
896
 *         valid values are: 'description', 'images', 'synonymy', 'all'
897
 * @return unknown_type
898
 */
899
function cdm_dataportal_taxon_view($uuid, $chapter = 'all'){
900
  
901
    $taxon = cdm_ws_get(CDM_WS_TAXON, $uuid);
902
    if(!$taxon){
903
      drupal_set_title(t('Taxon does not exist'));
904
      return false;
905
    }
906
    _cdm_dataportal_set_currentSecUuid($taxon->sec->uuid);
907
    
908
    $taxonpage->title = theme('cdm_taxon_page_title', $taxon);
909
    $taxonpage->content = theme('cdm_taxon_page_general', $taxon, $chapter);
910

    
911
    return $taxonpage;
912
}
913

    
914

    
915
function cdm_dataportal_view_search_advanced(){
916
  
917
  drupal_set_title(t('Advanced Search'));
918
  
919
  $searchForm = cdm_dataportal_search_taxon_form(true);
920
    
921
  return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
922
  
923
}
924

    
925
/**
926
 *
927
 * future extensions to meet palmweb mockup requirements:
928
 *  - habitat
929
 *  - uses
930
 *  - conservation status
931
 *  - locality / tdwg region
932
 */
933
function cdm_dataportal_view_search_taxon(){
934
  
935
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
936

    
937
  $request_params = cdm_dataportal_search_form_request();
938
  $taxonPager = cdm_ws_get(CDM_WS_FIND_TAXA, null, queryString($request_params));
939
  
940
  $search_params = $_REQUEST;
941
  unset($search_params['q']);
942
  return theme('cdm_search_results', $taxonPager, 'cdm_dataportal/search/taxon', $search_params);
943
}
944

    
945
function cdm_view_xml2json(){
946
  $file = arg(2);
947
  $datastr = get_content(variable_get('cdm_webservice_url', '').$file);
948
  return  xml2json::transformXmlStringToJson($datastr);
949
}
950

    
951
/* ====================== other functions ====================== */
952

    
953
/**
954
 * Enter description here...
955
 *
956
 * @param String $uuid the UUID of the taxon
957
 * @return the URL
958
 */
959
function path_to_taxon($uuid){
960
  if(!$uuid) return false;
961
  return 'cdm_dataportal/taxon/'.$uuid;
962
}
963

    
964
function path_to_reference($uuid){
965
  if(!$uuid) return false;
966
  return 'cdm_dataportal/reference/'.$uuid;
967
}
968

    
969
function path_to_media($uuid, $representaion_uuid = false, $partId = false){
970
  if(!$uuid) return false;
971
  $out = 'cdm_dataportal/media/'.$uuid;
972
  if($representaion_uuid){
973
    $out .= '/'.$representaion_uuid;
974
    if($partId !== false){
975
      $out .= '/'.$partId;
976
    }
977
  }
978
  return $out;
979
}
980

    
981
/**
982
 * Compares thisRank with thatRank.
983
 * Returns a negative integer, zero, or a positive integer 
984
 * as the of thisRank is higher than, equal to, or lower than thatRank.
985
 * e.g: 
986
 * <ul>
987
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
988
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
989
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
990
 * </ul>
991
 * <p>
992
 * This compare logic of the underlying webservice is the
993
 * <b>inverse logic</b> of the the one implemented in 
994
 * java.lang.Comparable#compareTo(java.lang.Object)
995
 * @param $thisRankUuid
996
 * @param $thatRankUuid
997
 * @return  a negative integer, zero, or a positive integer 
998
 * as the thisRank is lower than, equal to, or higher than thatRank 
999
 */
1000
function rank_compare($thisRankUuid, $thatRankUuid){
1001
    $result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
1002
    return $result->Integer;
1003
}
1004

    
1005
function uri_to_synonym($synonymUuid, $acceptedUuid, $pagePart = null){
1006
   $acceptedPath = path_to_taxon($acceptedUuid);
1007
   return url($acceptedPath.($pagePart ? '/'.$pagePart : ''), 'highlite='.$synonymUuid.'&acceptedFor='.$synonymUuid);
1008
}
1009

    
1010
/**
1011
 * Creates a short taxonname by using the taggename field of NameSTO or NameTO instances.
1012
 * If the taggename if empty the fullname will be returned.
1013
 *
1014
 * @param unknown_type $Name or TreeNode
1015
 * @return string
1016
 */
1017
function cdm_dataportal_shortname_of($name){
1018

    
1019
  $nameStr = '';
1020
  // get all tagged text tokens of the scientific name
1021
  foreach($name->taggedTitle as $tagtxt){
1022
      if($tagtxt->type == 'name' || $tagtxt->type == 'rank'){
1023
        $nameStr .= ($nameStr ? ' ' : '').$tagtxt->text;
1024
      }
1025
  }
1026
  $nameStr = trim($nameStr);
1027
  if($nameStr){
1028
    // do not return short names for these
1029
    if($nameStr == 'Incertae sedis' || $nameStr ==  'Nomina excludenda'){
1030
      return $nameStr;
1031
    }
1032
    if($pos = stripos($nameStr, ' ')){
1033
      return substr($nameStr, 0, 1).'. '.substr($nameStr, $pos);
1034
    } else {
1035
      return $nameStr;
1036
    }
1037
  } else {
1038
    return $name->titleCache;
1039
  }
1040
}
1041

    
1042
/**
1043
 * TODO Add Comments. I don't get what
1044
 *
1045
 * @param UUID $secUuid
1046
 */
1047
function _cdm_dataportal_set_currentSecUuid($secUuid){
1048
 
1049
  // do not save in session but in database
1050
  
1051
  if(is_array($secUuid)){
1052
    $secUuid = $secUuid[0];
1053
  }
1054
  if(!$secUuid){
1055
    variable_set('cdm_currentSecRef', null);
1056
  } else {
1057
    $secRef = cdm_ws_get(CDM_WS_REFERENCE, $secUuid);
1058
    if(isset($secRef)){
1059
      variable_set('cdm_currentSecRef', (array)($secRef));
1060
    }
1061
  }
1062
  
1063
}
1064

    
1065
/**
1066
 * TODO
1067
 * This will not work with multiple instances of the dataportal running
1068
 * under the same host as there is only one session for all instances.
1069
 * In case you switch instances with the same client, you will definitely
1070
 * run into trouble.
1071
 * Also this is not handling multiple secs.
1072
 *
1073
 *
1074
 * returns the current secRef array.
1075
 * If the according session variable is not jet set the default
1076
 * as configured in the setting is used otherwise null.
1077
 *
1078
 * currentSecRef['uuid']
1079
 * currentSecRef[....
1080
 *
1081
 * @return array
1082
 */
1083
function _cdm_dataportal_currentSecRef_array(){
1084

    
1085
  // do not look in session but in database
1086
  if( variable_get('cdm_currentSecRef', null) == null){
1087
     $secUuid = variable_get('cdm_secUuid_default', null);
1088
     _cdm_dataportal_set_currentSecUuid($secUuid);
1089
  }
1090
  return variable_get('cdm_currentSecRef', null);
1091
  
1092
}
1093

    
1094
/**
1095
 * Check if a taxon is accepted by the current taxonomic tree
1096
 *
1097
 * @param Taxon $taxon
1098
 * @return true if $taxon is accepted, false otherwise
1099
 */
1100

    
1101
function _cdm_dataportal_acceptedByCurrentView($taxon){
1102
  
1103
  //$current_secref = _cdm_dataportal_currentSecRef_array();
1104
  $defaultTreeUuid = variable_get('cdm_taxonomictree_uuid', false);
1105
  if($taxon->class == "Taxon" && isset($taxon->taxonNodes)){
1106
    foreach($taxon->taxonNodes as $node){
1107
      if($node->taxonomicTree == $defaultTreeUuid) {
1108
        return true;
1109
      }
1110
    }
1111
  }
1112
  return false;
1113
}
1114

    
1115
/**@Deprecated
1116
 * 
1117
 */
1118
function compose_url_prameterstr($parameters = array(), $parentPropertyName = false){
1119
  $pstr = '';
1120
  foreach($parameters as $key=>$value){
1121
    if(is_array($value)){
1122
      
1123
    } else {
1124
       $pstr .= ($pstr ? '&' :'').$key.'='.urlencode($value);    
1125
    }
1126
  }
1127
  return $pstr;
1128
}
1129

    
1130
function _get_feature_trees(){
1131
   $feature_trees = array();
1132
   
1133
   // set tree that contains all features
1134
   $feature_trees['0'] = 'No FeatureTree';
1135
   
1136
   // get features from database
1137
   $persisted_trees = cdm_ws_get(CDM_WS_FEATURETREES);
1138
   if(is_array($persisted_trees)){
1139
     foreach($persisted_trees as $featureTree){
1140
       
1141
       $featureLabels = array();
1142
       foreach ($featureTree->root->children as $featureNode){
1143
         $featureLabels[] = $featureNode->feature->representation_L10n;
1144
       }
1145
       
1146
       $feature_trees[$featureTree->uuid] = implode(", ", $featureLabels);
1147
       
1148
     }
1149
   }
1150
   return $feature_trees;
1151
}
1152

    
1153

    
1154
function get_nameRenderTemplate($renderPath, $nameLink = NULL, $refenceLink = NULL){
1155
  //TODO implement admin user interface to replace swicth statement 
1156
  //     preliminar solution: using themes 
1157
  
1158
  $template = theme('get_nameRenderTemplate', $renderPath);
1159
  
1160
  if(!is_array($template)){
1161
    switch($renderPath){
1162
      case 'list_of_taxa':
1163
      case  'acceptedFor':
1164
      case 'taxon_page_synonymy':
1165
      case 'typedesignations':
1166
      case 'taxon_page_title':
1167
      case 'na': $template = array(
1168
          'namePart' => array('#uri'=>true),
1169
          //'authorshipPart' => true,
1170
        );
1171
        break;
1172
      case 'nar': $template = array(
1173
          'namePart' => array('#uri'=>true),
1174
          //'authorshipPart' => true,
1175
          'referencePart' => array('#uri'=>true),
1176
          'microreferencePart' => true,
1177
        );
1178
        break;
1179
      default: $template = array(
1180
          'namePart' => array('#uri'=>true),
1181
          'authorshipPart' => true,
1182
          'referencePart' => array('#uri'=>true),
1183
          'microreferencePart' => true,
1184
          'statusPart' => true,
1185
          'descriptionPart' => true
1186
        );
1187
    }
1188
  }
1189
  
1190
  if($nameLink && isset($template['nameAuthorPart']['#uri'])){
1191
    $template['nameAuthorPart']['#uri'] = $nameLink;
1192
  } else{
1193
    unset($template['nameAuthorPart']['#uri']);
1194
  }
1195
  
1196
  if($nameLink && isset($template['namePart']['#uri'])){
1197
    $template['namePart']['#uri'] = $nameLink;
1198
  } else{
1199
    unset($template['namePart']['#uri']);
1200
  }
1201
  
1202
  if($refenceLink && isset($template['referencePart']['#uri'])){
1203
    $template['referencePart']['#uri'] = $refenceLink;
1204
  }else{
1205
    unset($template['referencePart']['#uri']);
1206
  }
1207
  
1208
  return $template;
1209
}
1210

    
1211
function get_partDefinition($taxonNameType){
1212
  //TODO implement admin user interface to allow specify the partdefinitions for any type
1213
  //     preliminar solution: using themes 
1214
  
1215
  $partdef = theme('get_partDefinition', $taxonNameType);
1216
  
1217
  if(!is_array($partdef)){  
1218
    switch($taxonNameType){
1219
      case 'ZoologicalName': $partdef = array(
1220
        'namePart' => array(
1221
          'name' => true,
1222
        ),
1223
        'referencePart' => array(
1224
          'authorTeam' => true
1225
        ),
1226
        'microreferencePart' => array(
1227
          'microreference' => true,
1228
        ),
1229
        'statusPart' => array(
1230
          'status' => true,
1231
        ),
1232
        'descriptionPart' => array(
1233
          'description' => true,
1234
        ),
1235
      );
1236
      break;
1237
      case 'BotanicalName': $partdef = array(
1238
        'namePart' => array(
1239
          'name' => true
1240
        ),
1241
        'authorTeamPart' => array(
1242
          'authorTeam' => true,   
1243
        ),
1244
        'referencePart' => array(
1245
          'reference' => true      
1246
        ),
1247
        'microreferencePart' => array(
1248
          'microreference' => true,
1249
        ),
1250
        'statusPart' => array(
1251
          'status' => true,
1252
        ),
1253
        'descriptionPart' => array(
1254
          'description' => true,
1255
        ),
1256
      );
1257
      break;
1258
      default: $partdef = array(
1259
        'namePart' => array(
1260
          'name' => true,
1261
        ),
1262
        'authorTeamPart' => array(
1263
          'authorTeam' => true,   
1264
        ),
1265
        'referencePart' => array(
1266
          'reference' => true      
1267
        ),
1268
        'microreferencePart' => array(
1269
          'microreference' => true,
1270
        ),
1271
        'statusPart' => array(
1272
          'status' => true,
1273
        ),
1274
        'descriptionPart' => array(
1275
          'description' => true,
1276
        ),
1277
      );
1278
    }
1279
  }
1280
  return $partdef;
1281
}
1282

    
1283
/**
1284
 * 
1285
 * @param $media
1286
 * @param $priority
1287
 * @return unknown_type
1288
 * TODO rename to read_media_metadata() and move to *.module
1289
 */
1290
function cdm_read_media_metadata($media, $priority = 1){
1291

    
1292
    $metadata_caption = array('title' => '', 
1293
                              'artist' => '', 
1294
                              'rights', 
1295
                              'location', 
1296
                              'filename' => '', 
1297
                              'mediacreated' => '', 
1298
                              'description' => '');
1299

    
1300
    //getting the media metadata
1301
    $media_metadata = cdm_ws_get(CDM_WS_MEDIA_METADATA, array($media->uuid));
1302

    
1303
    //filling the title, artist, copyright depending on the priority 1=>files 2=>CDM
1304
    switch($priority){
1305
        case 1:
1306
            //filling the title
1307
            if ($media_metadata->Headline)
1308
            $metadata_caption['title'] = $media_metadata->Headline;
1309
            elseif ($media->titleCache){
1310
                $metadata_caption['title'] = $media->titleCache;
1311
                if ($media->description_L10n)
1312
                $metadata_caption['title'] .= ' - ' . $media->description_L10n;
1313
            }
1314
            
1315
            //filling the artist
1316
            if ($media_metadata->Artist)
1317
                 $metadata_caption['artist'] = ($media_metadata->Artist ? ''.$media_metadata->Artist : '');
1318
            elseif ($media->artist->titleCache)
1319
                 $metadata_caption['artist'] = $media->artist->titleCache;
1320
    
1321
            //filling out with the copyright info
1322
            $metadata_caption['rights'] = array('copyright' => array('agentNames' => array()),
1323
                                                'license' => array('agentNames' => array(), 'types' => array(), 'abbreviatedTexts' => array(), 'uris' => array()));
1324
            if ($media_metadata->Copyright)
1325
                 $metadata_caption['rights']['copyright']['agentNames'][] = $media_metadata->Copyright;
1326
            elseif ($media->rights){
1327
                foreach($media->rights as $right){
1328
                    switch($right->term->uuid){
1329
                        case UUID_RIGHTS_LICENCE:
1330
                            $metadata_caption['rights']['license']['agentNames'][] = ($right->agent ? ''.$right->agent->firstname.' '.$right->agent->lastname : '');
1331
                            $metadata_caption['rights']['license']['types'][] = ($right->representation_L10n ? ''.$right->representation_L10n : '');
1332
                            $metadata_caption['rights']['license']['abbreviatedTexts'][] = ($right->abbreviatedText ? ''.$right->abbreviatedText : '');
1333
                            $metadata_caption['rights']['license']['uris'][] = ($right->uri ? ''.$right->uri : '');
1334
                            break;
1335
                        case UUID_RIGHTS_COPYRIGHT:
1336
                            $metadata_caption['rights']['copyright']['agentNames'][] = $right->agent->firstname . ' ' . $right->agent->lastname;
1337
                            break;
1338
                    }
1339
                }
1340
            }
1341
            else
1342
            $metadata_caption['rights']['agentNames'][] = '';
1343

    
1344
            break;
1345

    
1346
        case 2:
1347
            //filling the title
1348
            if ($media->titleCache){
1349
                $metadata_caption['title'] = $media->titleCache;
1350
                if ($media->description_L10n)
1351
                $metadata_caption['title'] .= ' - ' . $media->description_L10n;
1352
            }
1353
            elseif ($media_metadata->Headline)
1354
                $metadata_caption['title'] = $media_metadata->Headline;
1355
            break;
1356
    }
1357
    //filling the description (though there is no description in the db???)
1358
    $metadata_caption['description'] = $media->description_L10n;
1359

    
1360
    //filling the location
1361
    $metadata_caption['location'] = array();
1362
    $metadata_caption['location']['sublocation'] = $media_metadata->Sublocation;
1363
    $metadata_caption['location']['city'] = $media_metadata->City;
1364
    $metadata_caption['location']['province'] = $media_metadata->Province;
1365
    $metadata_caption['location']['country'] = $media_metadata->Country;
1366

    
1367

    
1368
    //filling the filename
1369
    if(isset($media->representations[0]->parts[0]->uri)){
1370
        $fileUri = $media->representations[0]->parts[0]->uri;
1371
        $filename = substr($fileUri, strrpos($fileUri, "/")+1);
1372
        $metadata_caption['filename'] = $filename;
1373
    }
1374
    else
1375
    $metadata_caption['filename'] = '';
1376
    
1377
    //filling the creation date
1378
    $metadata_caption['mediacreated'] = $media->created;
1379

    
1380
    //returned value
1381
    return $metadata_caption;
1382
}
(5-5/10)