Project

General

Profile

Download (46.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
 * The contents of this file are subject to the Mozilla Public License Version 1.1
13
 * See http://www.mozilla.org/MPL/MPL-1.1.html for the full license terms.
14
 */
15

    
16
require_once('node_types.php');
17
require_once('settings.php');
18
require_once('help.php');
19

    
20
require_once('theme/cdm_dataportal.common.theme');
21
require_once('theme/cdm_dataportal.descriptions.theme');
22
require_once('theme/cdm_dataportal.media.theme');
23
require_once('theme/cdm_dataportal.occurrence.theme');
24
require_once('theme/cdm_dataportal.page.theme');
25
require_once('theme/cdm_dataportal.taxon.theme');
26
require_once('theme/cdm_dataportal.name.theme');
27
require_once('theme/cdm_dataportal.references.theme');
28

    
29
require_once('classes/footnotemanager.php');
30
require_once('classes/footnote.php');
31
require_once('classes/footnotekey.php');
32
require_once('classes/renderhints.php');
33

    
34
function _add_js_progressbar(){
35
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/jquery.progressbar/js/jquery.progressbar.js');
36
}
37

    
38
function _add_js_treeselector(){
39
	//drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/treeselector.js');
40
	drupal_add_js("
41
		if (Drupal.jsEnabled) {
42
		  $(document).ready(function() {
43
		      $('#cdm-taxonomictree-selector-form #edit-val').change(function () {
44
		          $('#cdm-taxonomictree-selector-form').submit();
45
		      });
46

    
47
		  });
48
		}", 'inline');
49
}
50

    
51
function _add_js_openlayers_map(){
52

    
53
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/OpenLayers/OpenLayers.js', 'core', 'header');
54
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/openlayers_map.js');
55

    
56
	$gmap_api_key = variable_get('gmap_api_key', 'ABQIAAAAFho6eHAcUOTHLmH9IYHAeBRi_j0U6kJrkFvY4-OX2XYmEAa76BTsyMmEq-tn6nFNtD2UdEGvfhvoCQ');
57

    
58
	$baseLayers = variable_get('baselayers', array('metacarta_vmap0', 'PREFERRED' => 'metacarta_vmap0'));
59
	$layerNames = '';
60
	foreach($baseLayers as $layerName => $layerLabel){
61
		if($layerName == 'PREFERRED'){
62
			$preferredLayer = $baseLayers['PREFERRED'];
63
		} else {
64
			$layerNames .= ($layerNames ? ', ': '') . "'$layerName'";
65
		}
66
	}
67

    
68
	if( isset($baseLayers['gmap']) || isset($baseLayers['gsat']) || isset($baseLayers['ghyb']) ){
69
	  drupal_set_html_head('<script src=http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key='.$gmap_api_key.'" type="text/javascript"></script>');
70
	  drupal_set_html_head('<script src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.1" type="text/javascript"></script>');
71
	}
72

    
73
	drupal_add_js("
74
        if (Drupal.jsEnabled) {
75
          $(document).ready(function() {
76
              $('#openlayers_map').cdm_openlayers_map('"
77
              .getEDITMapServiceURI()."', {
78
                legendPosition: 3,
79
                displayWidth: '" . variable_get('cdm_dataportal_geoservice_display_width', false) . "',
80
                boundingBox: '" . variable_get('cdm_dataportal_geoservice_bounding_box', false) . "',
81
                distributionOpacity: '" . variable_get('cdm_dataportal_geoservice_distributionOpacity', '0.5') . "',
82
                legendOpacity: '" . variable_get('cdm_dataportal_geoservice_legendOpacity', '0.5') . "',
83
                showLayerSwitcher: " . (variable_get('cdm_dataportal_geoservice_showLayerSwitcher', TRUE) ? 'true':'false') . ",
84
                baseLayerNames: [".$layerNames."],
85
                defaultBaseLayerName: '".$preferredLayer."',
86
            });
87
          });
88
        }", 'inline');
89

    
90
}
91

    
92

    
93
function get_openlayers_map($width, $occurrenceQuery = false, $distributionQuery = false, $legendFormatQuery = false, $map_caption = false ){
94

    
95
	_add_js_openlayers_map();
96

    
97
	$out = '<div id="openlayers">';
98
	$out .= '<div id="openlayers_map" class="smallmap"';
99
	$out .= ' style="width: ' . $width . 'px; height:'.( $width / 2).'px"';
100
	if($occurrenceQuery){
101

    
102
		//fix $occurrenceQuery
103
		$occurrenceQuery .= '&ms=400,350';
104
		$occurrenceQuery .= '&l=v%3Aatbi%2Ce_w_0';
105
		$occurrenceQuery .= '&legend=0'; //TODO add to cdm service?
106

    
107
		$out .= ' occurrenceQuery="'.$occurrenceQuery.'"';
108
	}
109
	if($distributionQuery){
110
		$out .= ' distributionQuery="'.$distributionQuery.'"';
111
	}
112
	if($legendFormatQuery){
113
		$out .= ' legendFormatQuery="'.$legendFormatQuery.'"';
114
	}
115
	$out .= '></div></div>';
116

    
117
	// showing map caption
118
	if($map_caption){
119
		$out .= '<div class="distribution_map_caption">' . $map_caption . '</div>' . '<br />'; //FIXME: replace <br> by according css style
120
		$out .= '</div>';
121
	}
122
	return $out;
123
}
124

    
125
/* ====================== hook implementations ====================== */
126

    
127

    
128
/**
129
 * Implementation of hook_perm()
130
 *
131
 * Valid permissions for this module
132
 * @return array An array of valid permissions for the portfolio module
133
 */
134
function cdm_dataportal_perm() {
135
	return array(
136
    	'administer cdm_dataportal',
137
      'cdm_dataportal view notes',
138
	//TODO which else permission are required? -> check the WP6 requirements document
139
	);
140
}
141

    
142

    
143
/**
144
 * Implementation of hook_menu()
145
 */
146
function cdm_dataportal_menu($may_cache) {
147
	$items = array();
148

    
149
	cdm_dataportal_menu_admin($may_cache, $items);
150
	cdm_dataportal_menu_help($may_cache, $items);
151

    
152
	if ($may_cache) {
153

    
154
	 $items[] = array(
155
	    'path' => 'cdm_dataportal/names',
156
	    'callback' => 'cdm_dataportal_view_names',
157
	    'access' => true,
158
	    'type' => MENU_CALLBACK,
159
		);
160
		// optional callback arguments: page
161

    
162
		$items[] = array(
163
      'path' => 'cdm_dataportal/taxon',
164
      'callback' => 'cdm_dataportal_taxon_page_view',
165
      'access' => true,
166
      'type' => MENU_CALLBACK,
167
		// expected callback arguments: uuid
168
		);
169

    
170
		$items[] = array(
171
      'path' => 'cdm_dataportal/name',
172
      'callback' => 'cdm_dataportal_name_page_view',
173
      'access' => true,
174
      'type' => MENU_CALLBACK,
175
		// expected callback arguments: uuid
176
		);
177

    
178
		$items[] = array(
179
      'path' => 'cdm_dataportal/reference',
180
      'callback' => 'cdm_dataportal_view_reference',
181
      'access' => true,
182
      'type' => MENU_CALLBACK,
183
		// expected callback arguments: uuid
184
		);
185

    
186
		$items[] = array(
187
      'path' => 'cdm_dataportal/reference/list',
188
      'callback' => 'cdm_dataportal_view_reference_list',
189
      'access' => true,
190
      'type' => MENU_CALLBACK,
191
		// expected callback arguments: uuid
192
		);
193

    
194
		$items[] = array(
195
      'path' => 'cdm_dataportal/media',
196
      'callback' => 'cdm_dataportal_view_media',
197
      'access' => true,
198
      'type' => MENU_CALLBACK,
199
		// expected callback arguments: uuid, mediarepresentation_uuid, part_uuid or part#
200
		);
201

    
202
		$items[] = array(
203
      'path' => 'cdm_dataportal/search',
204
      'callback' => 'cdm_dataportal_view_search_advanced',
205
      'access' => true,
206
      'type' => MENU_CALLBACK,
207
		);
208

    
209
		$items[] = array(
210
      'path' => 'cdm_dataportal/search/taxon',
211
      'callback' => 'cdm_dataportal_view_search_taxon',
212
      'access' => true,
213
      'type' => MENU_CALLBACK,
214
		);
215

    
216
		$items[] = array(
217
      'path' => 'cdm/xml2json',
218
      'callback' => 'cdm_view_xml2json',
219
      'access' => true,
220
      'type' => MENU_CALLBACK,
221
		);
222

    
223
	} else {
224
		// may not cache
225
		// --- local tasks for Taxon
226
		$items[] = array(
227
      'path' => 'cdm_dataportal/name/'.arg(2),
228
		//'callback' => 'cdm_dataportal_view_name',
229
		  'callback' => 'cdm_dataportal_name_page_view',
230
      'callback arguments' => array(arg(2), arg(3), arg(4)),
231
      'access' => true,
232
      'type' => MENU_CALLBACK,
233
		);
234

    
235
		if(variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
236

    
237
			$items[] = array(
238
      'path' => 'cdm_dataportal/taxon/'.arg(2),
239
      'title' => theme('cdm_taxonpage_tab', 'General'),
240
      'callback' => 'cdm_dataportal_taxon_page_view',
241
      'access' => true,
242
      'type' => MENU_CALLBACK,
243
      'weight' => 1,
244
      'callback arguments' => array(arg(2), "description")
245
			// expected callback arguments: name_uuid
246
			);
247

    
248
			$items[] = array(
249
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/all',
250
      'title' => theme('cdm_taxonpage_tab', 'General'),
251
      'callback' => 'cdm_dataportal_taxon_page_view',
252
      'access' => true,
253
      'type' => MENU_CALLBACK,
254
      'weight' => 2,
255
      'callback arguments' => array(arg(2), "all")
256
			// expected callback arguments: name_uuid
257
			);
258

    
259
			$items[] = array(
260
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/description',
261
      'title' => theme('cdm_taxonpage_tab', 'General'),
262
      'callback' => 'cdm_dataportal_taxon_page_view',
263
      'access' => true,
264
      'type' => MENU_DEFAULT_LOCAL_TASK,
265
      'weight' => 2,
266
      'callback arguments' => array(arg(2), "description")
267
			// expected callback arguments: name_uuid
268
			);
269

    
270
			$items[] = array(
271
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/synonymy',
272
      'title' => theme('cdm_taxonpage_tab', 'Synonymy'),
273
      'callback' => 'cdm_dataportal_taxon_page_view',
274
      'access' => true,
275
      'type' => MENU_LOCAL_TASK,
276
      'weight' => 4,
277
      'callback arguments' => array(arg(2), "synonymy", arg(4))
278
			// expected callback arguments: name_uuid
279
			);
280
			$items[] = array(
281
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/images',
282
      'title' => theme('cdm_taxonpage_tab', 'Images'),
283
      'callback' => 'cdm_dataportal_taxon_page_view',
284
      'access' => true,
285
      'type' => MENU_LOCAL_TASK,
286
      'weight' => 5,
287
      'callback arguments' => array(arg(2), "images")
288
			// expected callback arguments: name_uuid
289
			);
290
		 $items[] = array(
291
		 'path' => 'cdm_dataportal/taxon/'.arg(2).'/specimens',
292
		 'title' => theme('cdm_taxonpage_tab', 'Specimens'),
293
		 'callback' => 'cdm_dataportal_taxon_page_view',
294
		 'access' => true,
295
		 'type' => MENU_LOCAL_TASK,
296
		 'weight' => 6,
297
		 'callback arguments' => array(arg(2), "specimens")
298
		 // expected callback arguments: name_uuid
299
		 );
300
		}
301
	}
302

    
303
	drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal.css');
304
	//drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
305
	drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_screen.css', 'screen');
306

    
307
	return $items;
308

    
309
}
310

    
311
/**
312
 * Implementation of hook_block()
313
 *
314
 * Provides the following blocks:
315
 *  0: list of links useful during development
316
 *
317
 * @param String $op
318
 * @param int $delta
319
 */
320
function cdm_dataportal_block($op='list', $delta=0) {
321
	// listing of blocks, such as on the admin/block page
322
	if ($op == "list") {
323
		//$block[0]["info"] = t("CDM DataPortal DevLinks");
324
		//		$block[1]["info"] = t("CDM DataPortal Credits");
325
		$block[2]["info"] = t("CDM Search Taxa");
326
		//$block[3]["info"] = t("CDM Filters");
327
		$block[4]["info"] = t("CDM Dataportal Print");
328
		return $block;
329
	}
330
	else if ($op == 'view') {
331
		switch($delta){
332
			//			case 1:
333
			//				$block['subject'] = t('Credits');
334
			//				$block['content'] = theme('cdm_credits');
335
			//				return $block;
336
			case 2:
337
				$block['subject'] = t('Search taxa');
338
				$block['content'] = drupal_get_form('cdm_dataportal_search_taxon_form');
339
				$block['content'] .= '<div>'.l('Advanced Search', '/cdm_dataportal/search').'</div>';
340
				return $block;
341
			case 4:
342
				$block['subject'] = t('<none>');
343
				$block['content'] = theme('cdm_print_button');;
344
				return $block;
345
		}
346
	}
347
}
348

    
349

    
350

    
351
/*
352
 function cdm_dataportal_session_clear($cdm_ws_uri_update = false){
353
 $_SESSION['cdm'] = null;
354
 if(is_string($cdm_ws_uri_update)){
355
 $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
356
 }
357
 }
358

    
359
 function cdm_dataportal_session_validate(){
360

    
361
 if(!isset($_SESSION['cdm']['ws_uri'])){
362
 $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', false));
363
 } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', false)){
364
 cdm_dataportal_session_clear(variable_get('cdm_webservice_url', false));
365
 }
366
 }
367
 */
368

    
369
function cdm_dataportal_search_taxon_form($advancedForm = false){
370

    
371
	$preset_query = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
372
	$preset_doTaxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
373
	$preset_doSynonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
374
	$preset_doTaxaByCommonNames = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
375
	$tdwg_level_select =  (isset($_SESSION['cdm']['search']['tdwg_level_select']) ? $_SESSION['cdm']['search']['tdwg_level_select'] : 2);
376
	$selected_areas =  (isset($_SESSION['cdm']['search']['area']) ? $_SESSION['cdm']['search']['area'] : false);
377

    
378
	$url = 'cdm_dataportal/search/taxon';
379
	$form['#method'] = 'get';
380
	$form['#process'] = array('cdm_dataportal_search_process' => array());
381
	$form['#action'] = url($url, NULL, NULL, true);
382

    
383
	$form['query'] = array(
384
    '#delta' => 0,
385
    '#type' => 'textfield',
386
    '#size' => 20,
387
    '#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')),
388
    '#value' => $preset_query,
389
	);
390

    
391
	$form['search'] = array(
392
      '#delta' => 1,
393
      '#tree' => true,
394
	//'#type' => $advancedForm ? 'fieldset': 'hidden',
395
      '#title' => t('Options')
396
	);
397

    
398
	$form['search']['tree'] = array(
399
    '#delta' => -1,
400
    '#type' => 'hidden',
401
    '#value' => get_taxonomictree_uuid_selected()
402
	);
403

    
404

    
405
	// clean URL get forms breaks if we don't give it a 'q'.
406
	if (!(bool)variable_get('clean_url', '0')) {
407
		$form['search']['q'] = array(
408
      '#delta' => -1,
409
      '#type' => 'hidden',
410
      '#value' => $url,
411
      '#name' => 'q',
412
		);
413
	}
414

    
415
	$form['search']['pageSize'] = array(
416
      '#delta' => -1,
417
      '#type' => 'hidden',
418
      '#value' => variable_get('cdm_dataportal_search_items_on_page', 25)
419
	);
420

    
421
	if($advancedForm){
422
		// general search parameters
423
		$form['search']['doTaxa'] = array(
424
      '#delta' => 2,
425
      '#type' => 'checkbox',
426
      '#title' => t('Search for accepted taxa'),
427
      '#value' => $preset_doTaxa
428
		);
429
		$form['search']['doSynonyms'] = array(
430
      '#delta' => 3,
431
      '#type' => 'checkbox',
432
      '#title' => t('Search for synonyms'),
433
      '#value' => $preset_doSynonyms
434
		);
435
		$form['search']['doTaxaByCommonNames'] = array(
436
      '#delta' => 4,
437
      '#type' => 'checkbox',
438
      '#title' => t('Search for common names'),
439
      '#value' => $preset_doTaxaByCommonNames
440
		);
441

    
442
		// Geographic Range
443
		$form['search']['geographic_range'] = array(
444
      '#type' => 'fieldset',
445
      '#delta' => 5,
446
      '#tree' => true,
447
      '#title' => t('Geographic range'),
448
		);
449

    
450
		$form['search']['geographic_range']['tdwg_level_select'] = array(
451
    '#type' => 'radios',
452
    '#title' => t('Select a TDWG distribution level and code'),
453
    '#default_value' => $tdwg_level_select,
454
    '#options' => array(
455
		t('TDWG level-1, i.e. a continent'),
456
		t('TDWG level-2'),
457
		t('TDWG level-3, i.e. a country'),
458
		t('TDWG level-4')
459
		)
460
		);
461
		$tdwg[1] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '1');
462
		$tdwg[2] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '2');
463
		$tdwg[3] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '3');
464
		$tdwg[4] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '4');
465

    
466
		$tdwg_js = '';
467
		foreach($tdwg as $key=>$tdwg_level){
468
			$tdwgOptions = array();
469
			$tdwgOptionsSelected = array();
470
			foreach($tdwg_level as $area){
471
				$representation = $area->representations[0];
472
				$tdwgOptions[$representation->abbreviatedLabel] = $area->representation_L10n;
473
				if(is_array($selected_areas) && in_array($representation->abbreviatedLabel, $selected_areas)){
474
					$tdwgOptionsSelected[] = $representation->abbreviatedLabel; //$area->uuid;
475
				}
476
			}
477
			asort($tdwgOptions);
478
			$form['search']['geographic_range']['tdwg_level_'.$key] = array(
479
        '#type' => 'select',
480
        '#title'         => t('TDWG level').' '.$key,
481
        '#default_value' => $tdwgOptionsSelected,
482
        '#multiple' => TRUE,
483
        '#options' => $tdwgOptions
484
			);
485
			$tdwg_js .= "$('#edit-search-geographic-range-tdwg-level-$key').parent()".($tdwg_level_select + 1 == $key ?  '.show()' : '.hide()'). ";\n";
486
		}
487

    
488
		drupal_add_js(
489
    "$(document).ready(function(){
490

    
491
      $(\"input[@name='search[geographic_range][tdwg_level_select]']\").change(
492
        function(event){
493
          var selectId = $(\"input[@name='search[geographic_range][tdwg_level_select]']:checked\").val();
494
          var i;
495
          for(i = 0; i < 4; i++){
496
            if(selectId == i){
497
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1) ).parent().fadeIn('slow');
498
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
499
            } else {
500
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).parent().fadeOut('slow');
501
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
502
            }
503
          }
504
        }
505
      );
506

    
507
      $tdwg_js
508
    });",
509
    'inline');
510

    
511
	} else {
512
		$form['search']['doTaxa'] = array(
513
	      '#delta' => -2,
514
	      '#type' => 'hidden',
515
	      '#value' => 1
516
		);
517
		$form['search']['doSynonyms'] = array(
518
	      '#delta' => -3,
519
	      '#type' => 'hidden',
520
	      '#value' => 1
521
		);
522
		$form['search']['doTaxaByCommonNames'] = array(
523
	      '#delta' => -4,
524
	      '#type' => 'hidden',
525
	      '#value' => 0
526
		);
527
	}
528

    
529
	$form['submit'] = array(
530
	  '#delta' => 9,
531
	  '#type' => 'submit',
532
	  '#name' => '',
533
	  '#value' => t('Search')
534
	);
535

    
536
	return $form;
537
}
538

    
539
function cdm_dataportal_search_taxon_form_advanced(){
540
	return cdm_dataportal_search_taxon_form(true);
541
}
542

    
543
function cdm_taxonomictree_selector(){
544
	_add_js_treeselector();
545

    
546
	$out = drupal_get_form('cdm_taxonomictree_selector_form');
547

    
548
	return $out;
549
}
550

    
551

    
552
function cdm_taxonomictree_selector_form(){
553

    
554
	$url = url('cdm_api/setvalue/session', null);
555
	$form['#action'] = $url;
556

    
557
	$form['var'] = array(
558
        '#delta' => -3,
559
        '#type' => 'hidden',
560
        '#value' => '[cdm][taxonomictree_uuid]'
561
        );
562

    
563
        $form['destination'] = array(
564
	   '#delta' => -3,
565
	   '#type' => 'hidden',
566
	   '#value' => substr(drupal_get_destination(), strlen('destination='))
567
        );
568

    
569
        $form['val'] = array(
570
      '#type' => 'select',
571
      '#title'         => t('Available classifications'),
572
      '#default_value' => get_taxonomictree_uuid_selected(),
573
      '#options' => cdm_get_taxontrees_as_options()
574
        );
575

    
576
        return $form;
577

    
578
}
579

    
580
/**
581
 * Implementation #process method call, see form_builder()
582
 * <p>
583
 * Removes Drupal internal form elements from query
584
 * @param $form
585
 * @return unknown_type
586
 */
587
function cdm_dataportal_search_process($form) {
588
	unset($form['form_id']);
589
	unset($form['form_token']);
590
	return $form;
591
}
592

    
593
/**
594
 * Filters $_REQUEST by a list of valid request  parameters and also sets defaults if required.
595
 * returns the processed request parameters submitted by the search form.
596
 */
597
function cdm_dataportal_search_form_request(){
598

    
599

    
600
	$form_params = array();
601
	array_deep_copy($_REQUEST['search'], $form_params);
602
	$form_params['query'] =  trim($_REQUEST['query']);
603

    
604
	// split of  geographic range
605
	if(isset($_REQUEST['search']['geographic_range'])){
606
		$geographicRange = $_REQUEST['search']['geographic_range'];
607
		// remove
608
		unset($form_params['geographic_range']);
609
	}
610

    
611
	// add geographic range
612
	if($geographicRange){
613
		$form_params['tdwg_level_select'] = $geographicRange['tdwg_level_select'];
614
		for($i = 1; $i < 5; $i++){
615
			if(isset($geographicRange['tdwg_level_'.$i])){
616
				$form_params['area'] = $geographicRange['tdwg_level_'.$i];
617
			}
618
		}
619
	}
620

    
621
	// store in session
622
	$_SESSION['cdm']['search'] = $form_params;
623

    
624
	return $form_params;
625
}
626

    
627

    
628
/* UNREACHABLE since action of form directly links to view
629
 function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
630

    
631
 $_SESSION['cdm']['search'] = $form_values;
632
 //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
633
 return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
634
 //$paramstr = compose_url_prameterstr($form_values);
635
 //return url('/cdm_dataportal/search/taxon/', $paramstr);
636
 }
637
 */
638
/* ====================== menu callback functions ====================== */
639

    
640

    
641

    
642
function cdm_dataportal_form_alter($form_id, &$form) {
643

    
644
	static $comment_node_disabled =  0;
645
	static $comment_node_read_only =  1;
646
	static $comment_node_read_write =  2;
647

    
648

    
649
	if ($form_id == 'node_type_form'
650
	&& isset($form['identity']['type'])
651
	&& array_key_exists($form['#node_type']->type, cdm_get_nodetypes())
652
	) {
653
		$form['workflow']['comment'] = array(
654
      '#type' => 'radios',
655
      '#title' => t('Default comment setting'),
656
      '#default_value' => variable_get('comment_'. $form['#node_type']->type, $comment_node_disabled),
657
      '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
658
      '#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
659
		);
660
	}
661
}
662

    
663

    
664

    
665
function cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed){
666

    
667
	$form[$form_name] = array(
668
    '#type' => 'fieldset',
669
    '#title' => t($form_tittle),
670
    '#collapsible' => TRUE,
671
    '#collapsed' => $collapsed,
672
    '#tree' => true,
673
	);
674

    
675
	$default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
676
	$gallery_settings = variable_get($form_name, $default_values);
677
	//$test = variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE);
678

    
679
	if($form_name == CDM_DATAPORTAL_SEARCH_GALLERY_NAME){
680
		/* TODO: why cdm_dataportal_search_items_on_page does not save the value on $test???
681
		 $form[$form_name]['cdm_dataportal_search_items_on_page'] = array(
682
		 '#type' => 'textfield',
683
		 '#title' => t('Search Page Size'),
684
		 '#default_value' => $test,
685
		 '#description' => t('Number of Names to display per page in search results.')
686
		 );
687
		 */
688
		$form[$form_name]['cdm_dataportal_show_taxon_thumbnails'] = array(
689
      '#type' => 'checkbox',
690
      '#title' => t('Show media thumbnails for accepted taxa'),
691
      '#default_value' => $gallery_settings['cdm_dataportal_show_taxon_thumbnails'],
692
      '#description' => t('')
693
		);
694

    
695
		$form[$form_name]['cdm_dataportal_show_synonym_thumbnails'] = array(
696
      '#type' => 'checkbox',
697
      '#title' => t('Show media thumbnails for synonyms'),
698
      '#default_value' => $gallery_settings['cdm_dataportal_show_synonym_thumbnails'],
699
      '#description' => t('')
700
		);
701
	}
702

    
703
	//$showCaption = variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 0);
704
	$form[$form_name]['cdm_dataportal_show_thumbnail_captions'] = array(
705
    '#type' => 'checkbox',
706
    '#title' => t('Show captions under thumbnails'),
707
    '#default_value' => $gallery_settings['cdm_dataportal_show_thumbnail_captions'],
708
    '#description' => t('')
709
	);
710

    
711
	$form[$form_name]['cdm_dataportal_media_maxextend'] = array(
712
    '#type' => 'textfield',
713
    '#title' => t('Maximum extend of Images'),
714
    '#default_value' => $gallery_settings['cdm_dataportal_media_maxextend'],
715
    '#description' => t('In the gallery neither width or height will exceed the maximum extend of images.')
716
	);
717

    
718
	$form[$form_name]['cdm_dataportal_media_cols'] = array(
719
    '#type' => 'textfield',
720
    '#title' => t('Number of columns'),
721
    '#default_value' => $gallery_settings['cdm_dataportal_media_cols'],
722
    '#description' => t('')
723
	);
724

    
725
	if($form_name == CDM_DATAPORTAL_SEARCH_GALLERY_NAME){
726
		$form[$form_name]['cdm_dataportal_media_maxRows'] = array(
727
      '#type' => 'textfield',
728
      '#title' => t('Maximum number of rows'),
729
      '#default_value' => $gallery_settings['cdm_dataportal_media_maxRows'],
730
      '#description' => t('If you want an unlimited number of rows please set to 0')
731
		);
732
	}
733

    
734
	return $form;
735
}
736

    
737

    
738
/**
739
 * Displays a list of the known taxonomic names. Long lists are split up into multiple pages
740
 *
741
 * TODO: parameters are still preliminar
742
 * @param String $page page number to diplay defaults to page 1
743
 * @param boolean $hide_unaccepted whether to hide nams which are not accepted by the current view
744
 */
745
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = false ){
746

    
747
	$request_params  = array(
748
    'q' => $beginsWith,
749
	//'sec' = '',
750
	//'higherTaxa' => getFiters(),
751
	// 'matchAnywhere' => false, // default is false
752
    'page' => $page,
753
    'onlyAccepted' => $onlyAccepted,
754
    'pagesize' => 20  /*$_SESSION['cdm']['namelist_pagesize'] */);
755

    
756
	$taxonPager = cdm_ws_find(CDM_WS_PORTAL_TAXON_FIND, $request_params);
757
	/*
758
	 * FIXME the filter for accepted names will be a form element, thus this widget
759
	 * should be generated via form api preferably as block
760
	 */
761
	//$out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
762
	//$out .= theme('cdm_dataportal_widget_names_list', $names, $page);
763
	$out .= theme('cdm_listof_taxa', $taxonPager);
764
	return $out;
765
}
766

    
767
function cdm_dataportal_view_reference($uuid, $arg2 = null){
768
	$reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
769
	return theme('cdm_reference_page', $reference);
770
}
771

    
772
function cdm_dataportal_view_reference_list($pageNumber){
773
	$referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE), $pageNumber);
774
	return theme('cdm_reference_pager', $referencePager, 'cdm_dataportal/reference/list/');
775
}
776

    
777
function cdm_dataportal_view_media($mediaUuid, $mediarepresentation_uuid = false, $part = 0){
778
	$media = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $mediaUuid);
779
	return theme('cdm_media_page', $media, $mediarepresentation_uuid, $part);
780
}
781

    
782
/**
783
 * The taxon page gives detailed information on a taxon, it shows:
784
 *  - Taxon name
785
 *  - Full list of synonyms homotypic synonyms on top, followed by the
786
 *    heterotypic and finally followed by misapplied names.
787
 *    The list is ordered historically.
788
 *  - All description associated with the taxon.
789
 *
790
 * @param $uuid
791
 * @param $chapter name of the part to display,
792
 *         valid values are: 'description', 'images', 'synonymy', 'specimens', 'all'
793
 * @return unknown_type
794
 */
795
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all', $synonym_uuid = null){
796
	// display the page for the taxon defined by $uuid
797
	set_last_taxon_page_tab(arg(3));
798
	$taxonpage = cdm_dataportal_taxon_view($uuid, $chapter, $synonym_uuid);
799
	return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title , $taxonpage->content);
800
}
801

    
802
/**
803
 * @param $uuid
804
 * @param $chapter name of the part to display,
805
 *         valid values are: 'description', 'images', 'synonymy', 'all'
806
 * @return unknown_type
807
 */
808
function cdm_dataportal_taxon_view($uuid, $chapter = 'all', $synonym_uuid = null){
809
	$taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $uuid);
810
	if(!$taxon){
811
		drupal_set_title(t('Taxon does not exist'));
812
		return false;
813
	}
814
	
815
	$taxonpage->title = theme('cdm_taxon_page_title', $taxon, $uuid, $synonym_uuid);
816

    
817
	// check if the taxon id contained in the currently selected tree
818
	$taxon_in_current_tree = taxon_in_current_tree($uuid);
819
	$taxon_nodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, $uuid);
820
	if(!$taxon_in_current_tree){
821
		if(count($taxon_nodes) == 0){
822
			drupal_set_message('This concept of the taxon '.theme('cdm_taxonName', $taxon->name).' is not contained any classification.'
823
			,'warning');
824
		} else {
825
			$trees = '';
826
			foreach($taxon_nodes as $node){
827
				$trees .= ($trees?', ':'').'<strong>'.$node->taxonomicTree->titleCache.'</strong>';
828

    
829
			}
830
			drupal_set_message('This concept of the taxon '.theme('cdm_taxonName', $taxon->name).' is not contained in the currently chosen classification, but in '
831
			.(count($taxon_nodes) > 1? ' one of these: ' : ' this one: ') . $trees
832
			, 'warning');
833
		}
834
	}
835

    
836
	// render the taxon page
837
	$taxonpage->content = theme('cdm_taxon_page_general', $taxon, $chapter);
838

    
839
	return $taxonpage;
840
}
841

    
842
/**
843
 * The function returns a name page as a drupal node ready to be renderized by drupal.
844
 * The node page show the taxon name title and the list of taxon related with such taxon
845
 * name on the tree already in used.
846
 * @param $taxon_name_uuid A taxon name uuid
847
 * @return The formatted name page as node
848
 */
849
function cdm_dataportal_name_page_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid = null){
850
	$taxonname_page = cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid);
851
	return cdm_node_show(NODETYPE_NAME, $taxon_name_uuid, $taxonname_page->title , $taxonname_page->content);
852
}
853

    
854
/**
855
 * The function genates a object ready to be transformated to a node
856
 * in order to show as a drupal node
857
 * @param $taxon_name_uuid
858
 * @return the object with the page content and title
859
 */
860
function cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid = null){
861
	//getting the full taxonname object from the server
862
	$taxon_name = cdm_ws_get(CDM_WS_NAME, array($taxon_name_uuid));
863
	if(!$taxon_name){
864
		drupal_set_title(t('Taxon name does not exist'));
865
		return false;
866
	}
867
	//searching for all the taxa connected with the taxon name on the tree in used
868
	$name_cache = cdm_ws_get(CDM_WS_NAME_NAMECAHE, array($taxon_name_uuid));
869
	$request_params = array();
870
	$request_params['query'] = $name_cache;
871
	$request_params['tree'] = get_taxonomictree_uuid_selected();
872
	$request_params['doTaxa'] = 1;
873
	$request_params['doSynonyms'] = 1;
874
	$request_params['doTaxaByCommonNames'] = 0;
875
	$request_params['matchMode'] = "EXACT";
876
	$taxon_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_FIND, null, queryString($request_params));
877

    
878
	//removing the name where we come from and
879
	foreach($taxon_pager->records as $k=>&$taxon){
880
		if($taxon->uuid == $taxon_to_hide_uuid){
881
			unset($taxon_pager->records[$k]);
882
		}
883
	}
884
	//show the taxa list or go to the singular taxon
885
	if (sizeof($taxon_pager->records) == 1){ //sigle taxon case
886
		reset($taxon_pager->records);
887
		$first_key = key($taxon_pager->records);
888
		if($taxon_pager->records[$first_key]->class != "Taxon"){
889
			$taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, $taxon_pager->records[$first_key]->uuid);
890
			if($synonym_uuid){
891
				drupal_goto('cdm_dataportal/taxon/' . $taxon[0]->uuid . '/synonymy', 'highlite=' . $synonym_uuid);
892
			}else{
893
				drupal_goto('cdm_dataportal/taxon/' . $taxon[0]->uuid);
894
			}
895
		}else{
896
			if($synonym_uuid){
897
				drupal_goto('cdm_dataportal/taxon/' . $taxon_pager->records[$first_key]->uuid . '/synonymy',
898
                      'highlite=' . $synonym_uuid);
899
			}else{
900
				drupal_goto('cdm_dataportal/taxon/' . $taxon_pager->records[$first_key]->uuid);
901
			}
902
			//drupal_goto('cdm_dataportal/taxon/' . $taxon_pager->records[$first_key]->uuid);
903
		}
904
	}else{ //more than one taxa case
905
		$taxon_name_page->title = theme('cdm_name_page_title', $taxon_name);
906
		if($taxon_pager->records){
907
			$taxon_name_page->content = theme_cdm_list_of_taxa($taxon_pager->records, false);
908
		}else{
909
			$taxon_name_page->content = 'This name has no taxa';
910
		}
911
		return $taxon_name_page;
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_PORTAL_TAXON_FIND, 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

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

    
952
/* ====================== other functions ====================== */
953

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

    
969
function path_to_reference($uuid){
970
	if(!$uuid) return false;
971
	return 'cdm_dataportal/reference/'.$uuid;
972
}
973

    
974
function path_to_name($name_uuid){
975
	$res = false;
976
	if($name_uuid){
977
	 $res = 'cdm_dataportal/name/'.$name_uuid;
978
	}
979
	return $res;
980
}
981

    
982
function path_to_media($uuid, $representaion_uuid = false, $partId = false){
983
	if(!$uuid) return false;
984
	$out = 'cdm_dataportal/media/'.$uuid;
985
	if($representaion_uuid){
986
		$out .= '/'.$representaion_uuid;
987
		if($partId !== false){
988
			$out .= '/'.$partId;
989
		}
990
	}
991
	return $out;
992
}
993

    
994
/**
995
 * Compares thisRank with thatRank.
996
 * Returns a negative integer, zero, or a positive integer
997
 * as the of thisRank is higher than, equal to, or lower than thatRank.
998
 * e.g:
999
 * <ul>
1000
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
1001
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
1002
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
1003
 * </ul>
1004
 * <p>
1005
 * This compare logic of the underlying webservice is the
1006
 * <b>inverse logic</b> of the the one implemented in
1007
 * java.lang.Comparable#compareTo(java.lang.Object)
1008
 * @param $thisRankUuid
1009
 * @param $thatRankUuid
1010
 * @return  a negative integer, zero, or a positive integer
1011
 * as the thisRank is lower than, equal to, or higher than thatRank
1012
 */
1013
function rank_compare($thisRankUuid, $thatRankUuid){
1014
	$result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
1015
	return $result->Integer;
1016
}
1017

    
1018
function theme_cdm_uri_to_synonym($synonymUuid, $acceptedUuid, $pagePart = null){
1019
	$acceptedPath = path_to_taxon($acceptedUuid);
1020
	return url($acceptedPath.($pagePart ? '/'.$pagePart : ''), 'highlite='.$synonymUuid.'&acceptedFor='.$synonymUuid, $synonymUuid);
1021
}
1022

    
1023
/**
1024
 * Creates a short taxonname by using the taggename field of NameSTO or NameTO instances.
1025
 * If the taggename if empty the fullname will be returned.
1026
 *
1027
 * @param unknown_type $Name or TreeNode
1028
 * @return string
1029
 */
1030
function cdm_dataportal_shortname_of($name){
1031
	$nameStr = '';
1032
	// get all tagged text tokens of the scientific name
1033
	foreach($name->taggedTitle as $tagtxt){
1034
		if($tagtxt->type == 'name' || $tagtxt->type == 'rank'){
1035
			$nameStr .= ($nameStr ? ' ' : '').$tagtxt->text;
1036
		}
1037
	}
1038
	$nameStr = trim($nameStr);
1039
	if($nameStr){
1040
		// do not return short names for these
1041
		//if (stristr(strtolower($nameStr), 'incertae sedis') !== FALSE ||
1042
		//	stristr(strtolower($nameStr), 'nomina excludenda') !== FALSE) {
1043
		if ($name->unplaced || $name->excluded){
1044
			return $nameStr;
1045
		}
1046
		if($pos = stripos($nameStr, ' ')){
1047
			return substr($nameStr, 0, 1).'. '.substr($nameStr, $pos);
1048
		} else {
1049
			return $nameStr;
1050
		}
1051
	} else {
1052
		return $name->titleCache;
1053
	}
1054
}
1055

    
1056

    
1057
/**
1058
 * Check if a taxon is accepted by the current taxonomic tree
1059
 *
1060
 * @param Taxon $taxon
1061
 * @return true if $taxon is accepted, false otherwise
1062
 */
1063

    
1064
function _cdm_dataportal_acceptedByCurrentView($taxon){
1065

    
1066
	$defaultTreeUuid = get_taxonomictree_uuid_selected();
1067
	if($taxon->class == "Taxon" && isset($taxon->taxonNodes)){
1068
		foreach($taxon->taxonNodes as $node){
1069
			if($node->taxonomicTree == $defaultTreeUuid) {
1070
				return true;
1071
			}
1072
		}
1073
	}
1074
	return false;
1075
}
1076

    
1077
/**@Deprecated
1078
 *
1079
 */
1080
function compose_url_prameterstr($parameters = array(), $parentPropertyName = false){
1081
	$pstr = '';
1082
	foreach($parameters as $key=>$value){
1083
		if(is_array($value)){
1084

    
1085
		} else {
1086
			$pstr .= ($pstr ? '&' :'').$key.'='.urlencode($value);
1087
		}
1088
	}
1089
	return $pstr;
1090
}
1091

    
1092
/**
1093
 * workaround for missing original source type, idNamespace is always set in these cases
1094
 * @param unknown_type $source
1095
 * @return unknown_type
1096
 */
1097
function _is_original_source_type($source){
1098
	return !$source->idNamespace && strlen($source->idNamespace) == 0;
1099
}
1100

    
1101

    
1102
function get_nameRenderTemplate($renderPath, $nameLink = NULL, $refenceLink = NULL){
1103
	//TODO implement admin user interface to replace switch statement
1104
	//     preliminar solution: using themes
1105

    
1106
	$template = null;
1107
	// find best matching default RenderTemplate in theme
1108
	//echo "<br/><br/>RENDER PATH:  ".$renderPath;
1109
	//var_dump('RENDER PATH: ' . $renderPath);
1110
	while(!is_array($template) && strlen($renderPath) > 0){
1111
		$template = theme('get_nameRenderTemplate', $renderPath);
1112
		$renderPath = substr($renderPath, 0, strrpos($renderPath, '.'));
1113
		//	   if(!is_array($template)){
1114
		//	       echo "<br/>->".$renderPath;
1115
		//	   }
1116
	}
1117

    
1118
	// otherwise get default RenderTemplate from theme
1119
	if(!is_array($template)){
1120
		$template = theme('get_nameRenderTemplate', '#DEFAULT');
1121
	}
1122
	// otherwise get system default RenderTemplate
1123
	if(!is_array($template)){
1124
		switch($renderPath){
1125
			case 'list_of_taxa':
1126
			case  'acceptedFor':
1127
			case 'taxon_page_synonymy':
1128
			case 'typedesignations':
1129
			case 'taxon_page_title':
1130
			case 'na': $template = array(
1131
			        'namePart' => array('#uri'=>true),
1132
			//'authorshipPart' => true,
1133
			);
1134
			break;
1135
			case 'nar': $template = array(
1136
		          	'namePart' => array('#uri'=>true),
1137
			//'authorshipPart' => true,
1138
		          	'referencePart' => array('#uri'=>true),
1139
		          	'microreferencePart' => true,
1140
			);
1141
			break;
1142
			default: $template = array(
1143
          'namePart' => array('#uri'=>true),
1144
          'authorshipPart' => true,
1145
          'referencePart' => array('#uri'=>true),
1146
          'microreferencePart' => true,
1147
          'statusPart' => true,
1148
          'descriptionPart' => true
1149
			);
1150
		}
1151
	}
1152

    
1153
	if($nameLink && isset($template['nameAuthorPart']['#uri'])){
1154
		$template['nameAuthorPart']['#uri'] = $nameLink;
1155
	} else{
1156
		unset($template['nameAuthorPart']['#uri']);
1157
	}
1158

    
1159
	if($nameLink && isset($template['namePart']['#uri'])){
1160
		$template['namePart']['#uri'] = $nameLink;
1161
	} else{
1162
		unset($template['namePart']['#uri']);
1163
	}
1164

    
1165
	if($refenceLink && isset($template['referencePart']['#uri'])){
1166
		$template['referencePart']['#uri'] = $refenceLink;
1167
	}else{
1168
		unset($template['referencePart']['#uri']);
1169
	}
1170

    
1171
	return $template;
1172
}
1173

    
1174
function get_partDefinition($taxonNameType){
1175
	//TODO implement admin user interface to allow specify the partdefinitions for any type
1176
	//     preliminar solution: using themes
1177

    
1178
	$partdef = theme('get_partDefinition', $taxonNameType);
1179

    
1180
	if(!is_array($partdef)){
1181
		switch($taxonNameType){
1182
			case 'ZoologicalName': $partdef = array(
1183
        'namePart' => array(
1184
          'name' => true,
1185
			),
1186
        'referencePart' => array(
1187
          'authorTeam' => true
1188
			),
1189
        'microreferencePart' => array(
1190
          'microreference' => true,
1191
			),
1192
        'statusPart' => array(
1193
          'status' => true,
1194
			),
1195
        'descriptionPart' => array(
1196
          'description' => true,
1197
			),
1198
			);
1199
			break;
1200
			case 'BotanicalName': $partdef = array(
1201
        'namePart' => array(
1202
          'name' => true
1203
			),
1204
        'authorTeamPart' => array(
1205
          'authorTeam' => true,
1206
			),
1207
        'referencePart' => array(
1208
          'reference' => true
1209
			),
1210
        'microreferencePart' => array(
1211
          'microreference' => true,
1212
			),
1213
        'statusPart' => array(
1214
          'status' => true,
1215
			),
1216
        'descriptionPart' => array(
1217
          'description' => true,
1218
			),
1219
			);
1220
			break;
1221
			default: $partdef = array(
1222
        'namePart' => array(
1223
          'name' => true,
1224
			),
1225
        'authorTeamPart' => array(
1226
          'authorTeam' => true,
1227
			),
1228
        'referencePart' => array(
1229
          'reference' => true
1230
			),
1231
        'microreferencePart' => array(
1232
          'microreference' => true,
1233
			),
1234
        'statusPart' => array(
1235
          'status' => true,
1236
			),
1237
        'descriptionPart' => array(
1238
          'description' => true,
1239
			),
1240
			);
1241
		}
1242
	}
1243
	return $partdef;
1244
}
1245

    
1246
/**
1247
 * The function read the metadata info such title or artist of a media file. The
1248
 * function tries at first to get all the info from the file metadata and if it is
1249
 * not avaible look at the media file info stored at the database.
1250
 * @param $media The media file
1251
 * @return array The array with the avilable specified metadata info.
1252
 * TODO rename to read_media_metadata() and move to *.module
1253
 */
1254
function cdm_read_media_metadata($media){
1255

    
1256
	$metadata_caption = array('title' => '',           //media_metadata and media
1257
                              'artist' => '',          //media_metadata and media
1258
                              'rights',                //media_metadata and media
1259
                              'location',              //media_metadata
1260
                              'filename' => '',        //media
1261
                              'mediacreated' => '',    //media
1262
                              'description' => '');    //media
1263

    
1264
	//getting the media metadata
1265
	$media_metadata = cdm_ws_get(CDM_WS_MEDIA_METADATA, array($media->uuid));
1266

    
1267
	//title
1268
	//if ($media_metadata->Headline) {
1269
	if ($media_metadata->ObjectName) {
1270
		$metadata_caption['title'] = $media_metadata->ObjectName;
1271
		//	} else if ($media->titleCache){
1272
		//		$metadata_caption['title'] = $media_metadata->ObjectName;
1273
	} else if ($media->titleCache){
1274
		$metadata_caption['title'] = $media->titleCache;
1275
		if ($media->description_L10n)
1276
		$metadata_caption['title'] .= ' - ' . $media->description_L10n;
1277
	}else{ //if there is no title on the db and metadata get file title
1278
		$aux = $media->representations[0]->parts[0]->uri;
1279
		$aux = explode("/", $aux);
1280
		$aux = end($aux);
1281
		$metadata_caption['title'] = $aux;
1282
	}
1283

    
1284
	//artist
1285
	if ($media_metadata->Artist){
1286
	 $metadata_caption['artist'] = ($media_metadata->Artist ? ''.$media_metadata->Artist : '');
1287
	}
1288
	elseif ($media->artist->titleCache){
1289
	 $metadata_caption['artist'] = $media->artist->titleCache;
1290
	}
1291

    
1292
	//copyright
1293
	$metadata_caption['rights'] = array('copyright' => array('agentNames' => array()),
1294
                                      'license' => array('agentNames' => array(), 'types' => array(), 'abbreviatedTexts' => array(), 'uris' => array()));
1295
	if ($media_metadata->Copyright)
1296
	$metadata_caption['rights']['copyright']['agentNames'][] = $media_metadata->Copyright;
1297
	elseif ($media->rights){
1298
		foreach($media->rights as $right){
1299
			switch($right->term->uuid){
1300
				case UUID_RIGHTS_LICENCE:
1301
					$metadata_caption['rights']['license']['agentNames'][] = ($right->agent ? ''.$right->agent->firstname.' '.$right->agent->lastname : '');
1302
					$metadata_caption['rights']['license']['types'][] = ($right->representation_L10n ? ''.$right->representation_L10n : '');
1303
					$metadata_caption['rights']['license']['abbreviatedTexts'][] = ($right->abbreviatedText ? ''.$right->abbreviatedText : '');
1304
					$metadata_caption['rights']['license']['uris'][] = ($right->uri ? ''.$right->uri : '');
1305
					break;
1306
				case UUID_RIGHTS_COPYRIGHT:
1307
					$metadata_caption['rights']['copyright']['agentNames'][] = $right->agent->firstname . ' ' . $right->agent->lastname;
1308
					break;
1309
			}
1310
		}
1311
	}
1312
	else
1313
	$metadata_caption['rights']['agentNames'][] = '';
1314

    
1315
	//filling the description (though there is no description in the db???)
1316
	//$metadata_caption['description'] = $media->description_L10n;
1317

    
1318
	//location
1319
	$metadata_caption['location'] = array();
1320
	$metadata_caption['location']['sublocation'] = $media_metadata->Sublocation;
1321
	$metadata_caption['location']['city'] = $media_metadata->City;
1322
	$metadata_caption['location']['province'] = $media_metadata->Province;
1323
	$metadata_caption['location']['country'] = $media_metadata->Country;
1324

    
1325
	//filename
1326
	if(isset($media->representations[0]->parts[0]->uri)){
1327
		$fileUri = $media->representations[0]->parts[0]->uri;
1328
		$filename = substr($fileUri, strrpos($fileUri, "/")+1);
1329
		$metadata_caption['filename'] = $filename;
1330
	}
1331
	else{
1332
	 $metadata_caption['filename'] = '';
1333
	}
1334
	/*
1335
	 //creation date
1336
	 if($media_metadata["Modify Date"])
1337
	 $metadata_caption['mediacreated'] = $media_metadata["Modify Date"];
1338
	 else
1339
	 $metadata_caption['mediacreated'] = $media->created;
1340
	 */
1341
	//returned value
1342
	return $metadata_caption;
1343
}
1344

    
1345
/**
1346
 * This function collects all the media from a list of description elements
1347
 * and return them as an Array.
1348
 *
1349
 * @param $descriptionElementes The description elements
1350
 * @return Array The output with all the media
1351
 */
1352
function cdm_dataportal_media_from_descriptionElements($descriptionElements){
1353
	//variables
1354
	$outArrayOfMedia = array(); //return value
1355
	//implementation
1356
	if(is_array($descriptionElements)){//avoiding warning box in drupal for flora malesiana
1357
		foreach($descriptionElements as $descriptionElement){
1358
			if(is_array($descriptionElement->media)){
1359
				foreach($descriptionElement->media as $media){
1360
					if(isset($media)){
1361
						$outArrayOfMedia[] = $media;
1362
					}
1363
				}
1364
			}
1365
		}
1366
	}
1367
	return $outArrayOfMedia;
1368
}
1369

    
1370
function cdm_annotations_as_footnotekeys($cdmBase, $footnote_list_key = array()){
1371
	$footNoteKeys = array();
1372
	//is argument cdmBase an array?
1373
	if (!is_array($cdmBase)){
1374
		$cdmBase_array = array();
1375
		$cdmBase_array[] = $cdmBase;
1376
	}else{
1377
		$cdmBase_array = $cdmBase;
1378
	}
1379
	//getting the key for the footnotemanager
1380
	if((bool) $footnote_list_key){
1381
		$footnoteListKey = $footnote_list_key;
1382
	}else{
1383
		$footnoteListKey = RenderHints::getFootnoteListKey() . '-annotations';
1384
	}
1385
	//adding the footnotes keys
1386
	foreach($cdmBase_array as $cdmBase_element){
1387
		$annotationUrl = cdm_compose_annotations_url($cdmBase_element);
1388
		if($annotationUrl){
1389
			$annotationPager = cdm_ws_get($annotationUrl, null, null, null, true);
1390
			if($annotationPager->count > 0){
1391
				foreach($annotationPager->records as $annotation){
1392
					$footNoteKeys[] = FootnoteManager::addNewFootnote($footnoteListKey, $annotation->text);
1393
				}
1394
			}
1395
		}
1396
	}
1397
	return $footNoteKeys;
1398
	/*
1399
	 $footNoteKeys = array();
1400
	 $footnoteListKey = RenderHints::getFootnoteListKey() . '-annotations';
1401
	 $annotationUrl = cdm_compose_annotations_url($cdmBase);
1402
	 if($annotationUrl){
1403
		$annotationPager = cdm_ws_get($annotationUrl, null, null, null, true);
1404
		if($annotationPager->count > 0){
1405
		foreach($annotationPager->records as $annotation){
1406
		$footNoteKeys[] = FootnoteManager::addNewFootnote($footnoteListKey, $annotation->text);
1407
		}
1408
		}
1409
		}
1410
		return $footNoteKeys;
1411
		*/
1412
}
1413

    
1414
function cdm_dynabox($label, $content_url, $theme, $link_alt_text, $enclosingtags = array('li', 'ul')){
1415

    
1416
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/cdm_dynabox.js');
1417

    
1418
	$cdm_proxy_url = url('cdm_api/proxy/'.urlencode($content_url)."/$theme");
1419
	$out .= '<'. $enclosingtags[0]. ' class="dynabox"><a href="'.$content_url.'" class="label" alt="'.t($link_alt_text).'">'.$label.'</a>';
1420
	$out .= '<'. $enclosingtags[1]. ' class="dynabox_content" title="'.$cdm_proxy_url.'"><'. $enclosingtags[0]. '><img class="loading" src="'.drupal_get_path('module', 'cdm_dataportal').'/images/loading_circle_grey_16.gif" style="display:none;"></'. $enclosingtags[0]. '></'. $enclosingtags[1]. '>';
1421
	$out .= '</'. $enclosingtags[0]. '>';
1422
	return $out;
1423
}
1424

    
(6-6/12)