Project

General

Profile

Download (45.7 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: 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: 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: uuid
112
    );
113
    
114
    $items[] = array(
115
      'path' => 'cdm_dataportal/media',
116
      'callback' => 'cdm_dataportal_view_media',
117
      'access' => true,
118
      'type' => MENU_CALLBACK,
119
    // expected callback arguments: uuid, mediarepresentation_uuid, part_uuid or part#
120
    );
121
    
122
    $items[] = array(
123
      'path' => 'cdm_dataportal/search',
124
      'callback' => 'cdm_dataportal_view_search_advanced',
125
      'access' => true,
126
      'type' => MENU_CALLBACK,
127
    );
128
    
129
    $items[] = array(
130
      'path' => 'cdm_dataportal/search/taxon',
131
      'callback' => 'cdm_dataportal_view_search_taxon',
132
      'access' => true,
133
      'type' => MENU_CALLBACK,
134
    );
135
    
136
    $items[] = array(
137
      'path' => 'cdm/xml2json',
138
      'callback' => 'cdm_view_xml2json',
139
      'access' => true,
140
      'type' => MENU_CALLBACK,
141
    );
142
    
143
    
144
  } else {
145
    // may not cache
146
        // --- local tasks for Taxon
147
    if(variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
148
      
149
       $items[] = array(
150
      'path' => 'cdm_dataportal/taxon/'.arg(2),
151
      'title' => theme('cdm_taxonpage_tab', 'General'),
152
      'callback' => 'cdm_dataportal_view_taxon',
153
      'access' => true,
154
      'type' => MENU_CALLBACK,
155
      'weight' => 1,
156
      'callback arguments' => array(arg(2), "description")
157
      // expected callback arguments: name_uuid
158
      );
159
      
160
      $items[] = array(
161
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/description',
162
      'title' => theme('cdm_taxonpage_tab', 'General'),
163
      'callback' => 'cdm_dataportal_view_taxon',
164
      'access' => true,
165
      'type' => MENU_DEFAULT_LOCAL_TASK,
166
      'weight' => 2,
167
      'callback arguments' => array(arg(2), "description")
168
      // expected callback arguments: name_uuid
169
      );
170
      
171
      $items[] = array(
172
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/synonymy',
173
      'title' => theme('cdm_taxonpage_tab', 'Synonymy'),
174
      'callback' => 'cdm_dataportal_view_taxon',
175
      'access' => true,
176
      'type' => MENU_LOCAL_TASK,
177
      'weight' => 3,
178
      'callback arguments' => array(arg(2), "synonymy")
179
      // expected callback arguments: name_uuid
180
      );
181
      
182
      $items[] = array(
183
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/images',
184
      'title' => theme('cdm_taxonpage_tab', 'Images'),
185
      'callback' => 'cdm_dataportal_view_taxon',
186
      'access' => true,
187
      'type' => MENU_LOCAL_TASK,
188
      'weight' => 4,
189
      'callback arguments' => array(arg(2), "images")
190
      // expected callback arguments: name_uuid
191
      );
192
    } 
193
  }
194
  
195
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal.css');
196
  //drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
197
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_screen.css', 'screen');
198
  
199
  return $items;
200
  
201
}
202

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

    
241

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

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

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

    
283
function cdm_dataportal_session_validate(){
284

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

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

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

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

    
463
function cdm_dataportal_search_taxon_form_advanced(){
464
  return cdm_dataportal_search_taxon_form(true);
465
}
466

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

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

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

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

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

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

    
535
  $form = cdm_api_settings_form();
536

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

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

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

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

    
825

    
826

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

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

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

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

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

    
871
/**
872
 * The taxon page gives detailed information on a taxon, it shows:
873
 *  - Taxon name
874
 *  - Full list of synonyms homotypic synonyms on top, followed by the
875
 *    heterotypic and finally followed by misapplied names.
876
 *    The list is ordered historically.
877
 *  - All facts associated with the very taxon concept and taxon name.
878
 *
879
 * @param String $uuid the UUID of the taxon
880
 */
881
function cdm_dataportal_view_taxon($uuid, $chapter = null){
882
  
883
    // display the page for the taxon defined by $uuid
884
    $taxon = cdm_ws_get(CDM_WS_TAXON, $uuid);
885
    if(!$taxon){
886
      drupal_set_title(t('Taxon does not exist'));
887
      return false;
888
    }
889
    _cdm_dataportal_set_currentSecUuid($taxon->sec->uuid);
890
    
891
    drupal_set_title(theme('cdm_taxon_page_title', $taxon));
892
    $out = theme('cdm_taxon_page_general', $taxon, $chapter);
893
    
894
    return $out;
895
  /*}*/
896
}
897

    
898
function cdm_dataportal_view_search_advanced(){
899
  
900
  drupal_set_title(t('Advanced Search'));
901
  
902
  $searchForm = cdm_dataportal_search_taxon_form(true);
903
    
904
  return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
905
  
906
}
907

    
908
/**
909
 *
910
 * future extensions to meet palmweb mockup requirements:
911
 *  - habitat
912
 *  - uses
913
 *  - conservation status
914
 *  - locality / tdwg region
915
 */
916
function cdm_dataportal_view_search_taxon(){
917
  
918
  $_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
919

    
920
  $request_params = cdm_dataportal_search_form_request();
921
  $taxonPager = cdm_ws_get(CDM_WS_FIND_TAXA, null, queryString($request_params));
922
  
923
  $search_params = $_REQUEST;
924
  unset($search_params['q']);
925
  return theme('cdm_search_results', $taxonPager, 'cdm_dataportal/search/taxon', $search_params);
926
}
927

    
928
function cdm_view_xml2json(){
929
  $file = arg(2);
930
  $datastr = get_content(variable_get('cdm_webservice_url', '').$file);
931
  return  xml2json::transformXmlStringToJson($datastr);
932
}
933

    
934
/* ====================== other functions ====================== */
935

    
936
/**
937
 * Enter description here...
938
 *
939
 * @param String $uuid the UUID of the taxon
940
 * @return the URL
941
 */
942
function path_to_taxon($uuid){
943
  if(!$uuid) return false;
944
  return 'cdm_dataportal/taxon/'.$uuid;
945
}
946

    
947
function path_to_reference($uuid){
948
  if(!$uuid) return false;
949
  return 'cdm_dataportal/reference/'.$uuid;
950
}
951

    
952
function path_to_media($uuid, $representaion_uuid = false, $partId = false){
953
  if(!$uuid) return false;
954
  $out = 'cdm_dataportal/media/'.$uuid;
955
  if($representaion_uuid){
956
    $out .= '/'.$representaion_uuid;
957
    if($partId !== false){
958
      $out .= '/'.$partId;
959
    }
960
  }
961
  return $out;
962
}
963

    
964
/**
965
 * Compares thisRank with thatRank.
966
 * Returns a negative integer, zero, or a positive integer 
967
 * as the of thisRank is higher than, equal to, or lower than thatRank.
968
 * e.g: 
969
 * <ul>
970
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
971
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
972
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
973
 * </ul>
974
 * <p>
975
 * This compare logic of the underlying webservice is the
976
 * <b>inverse logic</b> of the the one implemented in 
977
 * java.lang.Comparable#compareTo(java.lang.Object)
978
 * @param $thisRankUuid
979
 * @param $thatRankUuid
980
 * @return  a negative integer, zero, or a positive integer 
981
 * as the thisRank is lower than, equal to, or higher than thatRank 
982
 */
983
function rank_compare($thisRankUuid, $thatRankUuid){
984
    $result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
985
    return $result->Integer;
986
}
987

    
988
function uri_to_synonym($synonymUuid, $acceptedUuid, $pagePart = null){
989
   $acceptedPath = path_to_taxon($acceptedUuid);
990
   return url($acceptedPath.($pagePart ? '/'.$pagePart : ''), 'highlite='.$synonymUuid.'&acceptedFor='.$synonymUuid);
991
}
992

    
993
/**
994
 * Creates a short taxonname by using the taggename field of NameSTO or NameTO instances.
995
 * If the taggename if empty the fullname will be returned.
996
 *
997
 * @param unknown_type $Name or TreeNode
998
 * @return string
999
 */
1000
function cdm_dataportal_shortname_of($name){
1001

    
1002
  $nameStr = '';
1003
  // get all tagged text tokens of the scientific name
1004
  foreach($name->taggedTitle as $tagtxt){
1005
      if($tagtxt->type == 'name' || $tagtxt->type == 'rank'){
1006
        $nameStr .= ($nameStr ? ' ' : '').$tagtxt->text;
1007
      }
1008
  }
1009
  $nameStr = trim($nameStr);
1010
  if($nameStr){
1011
    // do not return short names for these
1012
    if($nameStr == 'Incertae sedis' || $nameStr ==  'Nomina excludenda'){
1013
      return $nameStr;
1014
    }
1015
    if($pos = stripos($nameStr, ' ')){
1016
      return substr($nameStr, 0, 1).'. '.substr($nameStr, $pos);
1017
    } else {
1018
      return $nameStr;
1019
    }
1020
  } else {
1021
    return $name->titleCache;
1022
  }
1023
}
1024

    
1025
/**
1026
 * TODO Add Comments. I don't get what
1027
 *
1028
 * @param UUID $secUuid
1029
 */
1030
function _cdm_dataportal_set_currentSecUuid($secUuid){
1031
 
1032
  // do not save in session but in database
1033
  
1034
  if(is_array($secUuid)){
1035
    $secUuid = $secUuid[0];
1036
  }
1037
  if(!$secUuid){
1038
    variable_set('cdm_currentSecRef', null);
1039
  } else {
1040
    $secRef = cdm_ws_get(CDM_WS_REFERENCE, $secUuid);
1041
    if(isset($secRef)){
1042
      variable_set('cdm_currentSecRef', (array)($secRef));
1043
    }
1044
  }
1045
  
1046
}
1047

    
1048
/**
1049
 * TODO
1050
 * This will not work with multiple instances of the dataportal running
1051
 * under the same host as there is only one session for all instances.
1052
 * In case you switch instances with the same client, you will definitely
1053
 * run into trouble.
1054
 * Also this is not handling multiple secs.
1055
 *
1056
 *
1057
 * returns the current secRef array.
1058
 * If the according session variable is not jet set the default
1059
 * as configured in the setting is used otherwise null.
1060
 *
1061
 * currentSecRef['uuid']
1062
 * currentSecRef[....
1063
 *
1064
 * @return array
1065
 */
1066
function _cdm_dataportal_currentSecRef_array(){
1067

    
1068
  // do not look in session but in database
1069
  if( variable_get('cdm_currentSecRef', null) == null){
1070
     $secUuid = variable_get('cdm_secUuid_default', null);
1071
     _cdm_dataportal_set_currentSecUuid($secUuid);
1072
  }
1073
  return variable_get('cdm_currentSecRef', null);
1074
  
1075
}
1076

    
1077
/**
1078
 * Check if a taxon is accepted by the current taxonomic tree
1079
 *
1080
 * @param Taxon $taxon
1081
 * @return true if $taxon is accepted, false otherwise
1082
 */
1083

    
1084
function _cdm_dataportal_acceptedByCurrentView($taxon){
1085
  
1086
  //$current_secref = _cdm_dataportal_currentSecRef_array();
1087
  $defaultTreeUuid = variable_get('cdm_taxonomictree_uuid', false);
1088
  if($taxon->class == "Taxon" && isset($taxon->taxonNodes)){
1089
    foreach($taxon->taxonNodes as $node){
1090
      if($node->taxonomicTree == $defaultTreeUuid) {
1091
        return true;
1092
      }
1093
    }
1094
  }
1095
  return false;
1096
}
1097

    
1098
/**@Deprecated
1099
 * 
1100
 */
1101
function compose_url_prameterstr($parameters = array(), $parentPropertyName = false){
1102
  $pstr = '';
1103
  foreach($parameters as $key=>$value){
1104
    if(is_array($value)){
1105
      
1106
    } else {
1107
       $pstr .= ($pstr ? '&' :'').$key.'='.urlencode($value);    
1108
    }
1109
  }
1110
  return $pstr;
1111
}
1112

    
1113
function _get_feature_trees(){
1114
   $feature_trees = array();
1115
   
1116
   // set tree that contains all features
1117
   $feature_trees['0'] = 'No FeatureTree';
1118
   
1119
   // get features from database
1120
   $persisted_trees = cdm_ws_get(CDM_WS_FEATURETREES);
1121
   if(is_array($persisted_trees)){
1122
     foreach($persisted_trees as $featureTree){
1123
       
1124
       $featureLabels = array();
1125
       foreach ($featureTree->root->children as $featureNode){
1126
         $featureLabels[] = $featureNode->feature->representation_L10n;
1127
       }
1128
       
1129
       $feature_trees[$featureTree->uuid] = implode(", ", $featureLabels);
1130
       
1131
     }
1132
   }
1133
   return $feature_trees;
1134
}
1135

    
1136

    
1137
function get_nameRenderTemplate($renderPath, $nameLink = NULL, $refenceLink = NULL){
1138
  //TODO implement admin user interface to replace swicth statement 
1139
  //     preliminar solution: using themes 
1140
  
1141
  $template = theme('get_nameRenderTemplate', $renderPath);
1142
  
1143
  if(!is_array($template)){
1144
    switch($renderPath){
1145
      case 'list_of_taxa':
1146
      case  'acceptedFor':
1147
      case 'taxon_page_synonymy':
1148
      case 'typedesignations':
1149
      case 'taxon_page_title':
1150
      case 'na': $template = array(
1151
          'namePart' => array('#uri'=>true),
1152
          //'authorshipPart' => true,
1153
        );
1154
        break;
1155
      case 'nar': $template = array(
1156
          'namePart' => array('#uri'=>true),
1157
          //'authorshipPart' => true,
1158
          'referencePart' => array('#uri'=>true),
1159
          'microreferencePart' => true,
1160
        );
1161
        break;
1162
      default: $template = array(
1163
          'namePart' => array('#uri'=>true),
1164
          'authorshipPart' => true,
1165
          'referencePart' => array('#uri'=>true),
1166
          'microreferencePart' => true,
1167
          'statusPart' => true,
1168
          'descriptionPart' => true
1169
        );
1170
    }
1171
  }
1172
  
1173
  if($nameLink && isset($template['nameAuthorPart']['#uri'])){
1174
    $template['nameAuthorPart']['#uri'] = $nameLink;
1175
  } else{
1176
    unset($template['nameAuthorPart']['#uri']);
1177
  }
1178
  
1179
  if($nameLink && isset($template['namePart']['#uri'])){
1180
    $template['namePart']['#uri'] = $nameLink;
1181
  } else{
1182
    unset($template['namePart']['#uri']);
1183
  }
1184
  
1185
  if($refenceLink && isset($template['referencePart']['#uri'])){
1186
    $template['referencePart']['#uri'] = $refenceLink;
1187
  }else{
1188
    unset($template['referencePart']['#uri']);
1189
  }
1190
  
1191
  return $template;
1192
}
1193

    
1194
function get_partDefinition($taxonNameType){
1195
  //TODO implement admin user interface to allow specify the partdefinitions for any type
1196
  //     preliminar solution: using themes 
1197
  
1198
  $partdef = theme('get_partDefinition', $taxonNameType);
1199
  
1200
  if(!is_array($partdef)){  
1201
    switch($taxonNameType){
1202
      case 'ZoologicalName': $partdef = array(
1203
        'namePart' => array(
1204
          'name' => true,
1205
        ),
1206
        'referencePart' => array(
1207
          'authorTeam' => true
1208
        ),
1209
        'microreferencePart' => array(
1210
          'microreference' => true,
1211
        ),
1212
        'statusPart' => array(
1213
          'status' => true,
1214
        ),
1215
        'descriptionPart' => array(
1216
          'description' => true,
1217
        ),
1218
      );
1219
      break;
1220
      case 'BotanicalName': $partdef = array(
1221
        'namePart' => array(
1222
          'name' => true
1223
        ),
1224
        'authorTeamPart' => array(
1225
          'authorTeam' => true,   
1226
        ),
1227
        'referencePart' => array(
1228
          'reference' => true      
1229
        ),
1230
        'microreferencePart' => array(
1231
          'microreference' => true,
1232
        ),
1233
        'statusPart' => array(
1234
          'status' => true,
1235
        ),
1236
        'descriptionPart' => array(
1237
          'description' => true,
1238
        ),
1239
      );
1240
      break;
1241
      default: $partdef = array(
1242
        'namePart' => array(
1243
          'name' => true,
1244
        ),
1245
        'authorTeamPart' => array(
1246
          'authorTeam' => true,   
1247
        ),
1248
        'referencePart' => array(
1249
          'reference' => true      
1250
        ),
1251
        'microreferencePart' => array(
1252
          'microreference' => true,
1253
        ),
1254
        'statusPart' => array(
1255
          'status' => true,
1256
        ),
1257
        'descriptionPart' => array(
1258
          'description' => true,
1259
        ),
1260
      );
1261
    }
1262
  }
1263
  return $partdef;
1264
}
1265

    
1266
/**
1267
 * 
1268
 * @param $media
1269
 * @param $priority
1270
 * @return unknown_type
1271
 * TODO rename to read_media_metadata() and move to *.module
1272
 */
1273
function cdm_read_media_metadata($media, $priority = 1){
1274

    
1275
    $metadata_caption = array('title' => '', 
1276
                              'artist' => '', 
1277
                              'rights', 
1278
                              'location', 
1279
                              'filename' => '', 
1280
                              'mediacreated' => '', 
1281
                              'description' => '');
1282

    
1283
    //getting the media metadata
1284
    $media_metadata = cdm_ws_get(CDM_WS_MEDIA_METADATA, array($media->uuid));
1285

    
1286
    //filling the title, artist, copyright depending on the priority 1=>files 2=>CDM
1287
    switch($priority){
1288
        case 1:
1289
            //filling the title
1290
            if ($media_metadata->Headline)
1291
            $metadata_caption['title'] = $media_metadata->Headline;
1292
            elseif ($media->titleCache){
1293
                $metadata_caption['title'] = $media->titleCache;
1294
                if ($media->description_L10n)
1295
                $metadata_caption['title'] .= ' - ' . $media->description_L10n;
1296
            }
1297
            
1298
            //filling the artist
1299
            if ($media_metadata->Artist)
1300
                 $metadata_caption['artist'] = ($media_metadata->Artist ? ''.$media_metadata->Artist : '');
1301
            elseif ($media->artist->titleCache)
1302
                 $metadata_caption['artist'] = $media->artist->titleCache;
1303
    
1304
            //filling out with the copyright info
1305
            $metadata_caption['rights'] = array('copyright' => array('agentNames' => array()),
1306
                                                'license' => array('agentNames' => array(), 'types' => array(), 'abbreviatedTexts' => array(), 'uris' => array()));
1307
            if ($media_metadata->Copyright)
1308
                 $metadata_caption['rights']['copyright']['agentNames'][] = $media_metadata->Copyright;
1309
            elseif ($media->rights){
1310
                foreach($media->rights as $right){
1311
                    switch($right->term->uuid){
1312
                        case UUID_RIGHTS_LICENCE:
1313
                            $metadata_caption['rights']['license']['agentNames'][] = ($right->agent ? ''.$right->agent->firstname.' '.$right->agent->lastname : '');
1314
                            $metadata_caption['rights']['license']['types'][] = ($right->representation_L10n ? ''.$right->representation_L10n : '');
1315
                            $metadata_caption['rights']['license']['abbreviatedTexts'][] = ($right->abbreviatedText ? ''.$right->abbreviatedText : '');
1316
                            $metadata_caption['rights']['license']['uris'][] = ($right->uri ? ''.$right->uri : '');
1317
                            break;
1318
                        case UUID_RIGHTS_COPYRIGHT:
1319
                            $metadata_caption['rights']['copyright']['agentNames'][] = $right->agent->firstname . ' ' . $right->agent->lastname;
1320
                            break;
1321
                    }
1322
                }
1323
            }
1324
            else
1325
            $metadata_caption['rights']['agentNames'][] = '';
1326

    
1327
            break;
1328

    
1329
        case 2:
1330
            //filling the title
1331
            if ($media->titleCache){
1332
                $metadata_caption['title'] = $media->titleCache;
1333
                if ($media->description_L10n)
1334
                $metadata_caption['title'] .= ' - ' . $media->description_L10n;
1335
            }
1336
            elseif ($media_metadata->Headline)
1337
                $metadata_caption['title'] = $media_metadata->Headline;
1338
            break;
1339
    }
1340
    //filling the description (though there is no description in the db???)
1341
    $metadata_caption['description'] = $media->description_L10n;
1342

    
1343
    //filling the location
1344
    $metadata_caption['location'] = array();
1345
    $metadata_caption['location']['sublocation'] = $media_metadata->Sublocation;
1346
    $metadata_caption['location']['city'] = $media_metadata->City;
1347
    $metadata_caption['location']['province'] = $media_metadata->Province;
1348
    $metadata_caption['location']['country'] = $media_metadata->Country;
1349

    
1350

    
1351
    //filling the filename
1352
    if(isset($media->representations[0]->parts[0]->uri)){
1353
        $fileUri = $media->representations[0]->parts[0]->uri;
1354
        $filename = substr($fileUri, strrpos($fileUri, "/")+1);
1355
        $metadata_caption['filename'] = $filename;
1356
    }
1357
    else
1358
    $metadata_caption['filename'] = '';
1359
    
1360
    //filling the creation date
1361
    $metadata_caption['mediacreated'] = $media->created;
1362

    
1363
    //returned value
1364
    return $metadata_caption;
1365

    
1366
}
(5-5/9)