Project

General

Profile

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

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

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

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

    
32
define('CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE', 25);
33
define('CDM_DATAPORTAL_NOMREF_IN_TITLE', 1);
34
define('CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR', 0);
35

    
36
/* gallery variables */
37
$gallery_settings = array(
38
    "cdm_dataportal_show_taxon_thumbnails" => 1,
39
    "cdm_dataportal_show_synonym_thumbnails" => 0,
40
    "cdm_dataportal_show_thumbnail_captions" => 1,
41
    "cdm_dataportal_media_maxextend" => 120,
42
    "cdm_dataportal_media_cols" => 3,
43
    "cdm_dataportal_media_maxRows" => 1);
44
define('CDM_DATAPORTAL_GALLERY_SETTINGS', serialize($gallery_settings));
45
define('CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME', "description_gallery");
46
define('CDM_DATAPORTAL_MEDIA_GALLERY_NAME', "media_gallery");
47
define('CDM_DATAPORTAL_SEARCH_GALLERY_NAME', "search_gallery");
48
define('CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS', 'cdm_dataportal_display_taxon_relationships');
49
define('CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS', 'cdm_dataportal_display_name_relations');
50
define('CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT', 1);
51
define('CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS_DEFAULT', 1);
52
define('CDM_DATAPORTAL_DEFAULT_FEATURETREE_UUID', 'cdm_dataportal_featuretree_uuid');
53

    
54

    
55

    
56

    
57
function getGallerySettings($gallery_config_form_name){
58
	$default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
59
  	return variable_get($gallery_config_form_name, $default_values);
60
}
61

    
62

    
63

    
64
function _add_js_progressbar(){
65
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/jquery.progressbar/js/jquery.progressbar.js');
66
}
67

    
68
function _add_js_treeselector(){
69
    //drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/treeselector.js');
70
	drupal_add_js("
71
		if (Drupal.jsEnabled) {
72
		  $(document).ready(function() {
73
		      $('#cdm-taxonomictree-selector-form #edit-val').change(function () {
74
		          $('#cdm-taxonomictree-selector-form').submit();
75
		      });
76
		      
77
		  });
78
		}", 'inline');
79
}
80

    
81

    
82
/* ====================== hook implementations ====================== */
83

    
84
/**
85
 * Implementation of hook_help()
86
 *
87
 * Display help and module information
88
 * @param section which section of the site we're displaying help
89
 * @return help text for section
90
 */
91
function cdm_dataportal_help($section='') {
92

    
93
	$out = '';
94
	switch ($section) {
95
		case "admin/modules#description":
96
			$out = t("The dataportal publishes CDM data hosted in a CommunityStore on the web.");
97
			break;
98
	}
99
	return $out;
100
}
101

    
102

    
103
/**
104
 * Implementation of hook_perm()
105
 *
106
 * Valid permissions for this module
107
 * @return array An array of valid permissions for the portfolio module
108
 */
109
function cdm_dataportal_perm() {
110
	return array(
111
    	'administer cdm_dataportal',
112
      'cdm_dataportal view notes',
113
	//TODO which else permission are required? -> check the WP6 requirements document
114
	);
115
}
116

    
117

    
118
/**
119
 * Implementation of hook_menu()
120
 */
121
function cdm_dataportal_menu($may_cache) {
122
	$items = array();
123

    
124
	if ($may_cache) {
125

    
126
		$items[] = array(
127
      'path' => 'admin/settings/cdm_dataportal',
128
      'title' => t('CDM Dataportal'),
129
      'description' => t('Setting for the CDM DataPortal'),
130
      'access' => user_access('administer cdm_dataportal'),
131
      'callback' => 'drupal_get_form',
132
      'callback arguments' => 'cdm_dataportal_settings',
133
      'type' => MENU_NORMAL_ITEM,
134
		);
135

    
136
		$items[] = array(
137
      'path' => 'admin/settings/cdm_dataportal/general',
138
      'title' => t('General'),
139
      'description' => t('Setting for the CDM DataPortal'),
140
      'access' => user_access('administer cdm_dataportal'),
141
      'callback' => 'drupal_get_form',
142
      'callback arguments' => 'cdm_dataportal_settings',
143
	  'weight' => 0,
144
      'type' => MENU_LOCAL_TASK,
145
		);
146

    
147
		$items[] = array(
148
      'path' => 'admin/settings/cdm_dataportal/geo',
149
      'title' => t('Geo & Map'),
150
      'description' => t('Geo & Map'),
151
      'access' => user_access('administer cdm_dataportal'),
152
      'callback' => 'drupal_get_form',
153
      'callback arguments' => 'cdm_dataportal_settings_geo',
154
	  'weight' => 1,
155
      'type' => MENU_LOCAL_TASK,
156
		);
157

    
158
		$items[] = array(
159
      'path' => 'admin/settings/cdm_dataportal/layout',
160
      'title' => t('Layout'),
161
      'description' => t('Configure and adjust the layout of your DataPortal '),
162
      'access' => user_access('administer cdm_dataportal'),
163
      'callback' => 'drupal_get_form',
164
      'callback arguments' => 'cdm_dataportal_settings_layout',
165
      'weight' => 2,
166
      'type' => MENU_LOCAL_TASK,
167
		);
168

    
169
		$items[] = array(
170
      'path' => 'admin/settings/cdm_dataportal/cachesite',
171
      'title' => t('Cache'),
172
      'description' => t('Cache the whole CDM DataPortal site'),
173
      'access' => user_access('administer cdm_dataportal'),
174
      'callback' => 'cdm_dataportal_view_cache_site',
175
      'weight' => 10,
176
      'type' => MENU_LOCAL_TASK,
177
		);
178

    
179
		/*$items[] = array(
180
		 'path' => 'admin/settings/cdm_dataportal/ws',
181
		 'title' => t('Web Service'),
182
		 'description' => t('Setting for the CDM DataPortal'),
183
		 'access' => user_access('administer cdm_dataportal'),
184
		 'callback' => 'drupal_get_form',
185
		 'callback arguments' => 'cdm_dataportal_settings',
186
		 'weight' => 1,
187
		 'type' => MENU_DEFAULT_LOCAL_TASK,
188
		 );*/
189

    
190
		$items[] = array(
191
	    'path' => 'cdm_dataportal/names',
192
	    'callback' => 'cdm_dataportal_view_names',
193
	    'access' => true,
194
	    'type' => MENU_CALLBACK,
195
		);
196
		// optional callback arguments: page
197
			
198
		$items[] = array(
199
      'path' => 'cdm_dataportal/taxon',
200
      'callback' => 'cdm_dataportal_taxon_page_view',
201
      'access' => true,
202
      'type' => MENU_CALLBACK,
203
		// expected callback arguments: uuid
204
		);
205
		
206
		$items[] = array(
207
      'path' => 'cdm_dataportal/name',
208
      'callback' => 'cdm_dataportal_name_page_view',
209
      'access' => true,
210
      'type' => MENU_CALLBACK,
211
    // expected callback arguments: uuid
212
    );
213

    
214
		$items[] = array(
215
      'path' => 'cdm_dataportal/reference',
216
      'callback' => 'cdm_dataportal_view_reference',
217
      'access' => true,
218
      'type' => MENU_CALLBACK,
219
		// expected callback arguments: uuid
220
		);
221

    
222
		$items[] = array(
223
      'path' => 'cdm_dataportal/reference/list',
224
      'callback' => 'cdm_dataportal_view_reference_list',
225
      'access' => true,
226
      'type' => MENU_CALLBACK,
227
		// expected callback arguments: uuid
228
		);
229

    
230
		$items[] = array(
231
      'path' => 'cdm_dataportal/media',
232
      'callback' => 'cdm_dataportal_view_media',
233
      'access' => true,
234
      'type' => MENU_CALLBACK,
235
		// expected callback arguments: uuid, mediarepresentation_uuid, part_uuid or part#
236
		);
237

    
238
		$items[] = array(
239
      'path' => 'cdm_dataportal/search',
240
      'callback' => 'cdm_dataportal_view_search_advanced',
241
      'access' => true,
242
      'type' => MENU_CALLBACK,
243
		);
244

    
245
		$items[] = array(
246
      'path' => 'cdm_dataportal/search/taxon',
247
      'callback' => 'cdm_dataportal_view_search_taxon',
248
      'access' => true,
249
      'type' => MENU_CALLBACK,
250
		);
251
		
252
		$items[] = array(
253
      'path' => 'cdm/xml2json',
254
      'callback' => 'cdm_view_xml2json',
255
      'access' => true,
256
      'type' => MENU_CALLBACK,
257
		);
258

    
259
	} else {
260
		// may not cache
261
		// --- local tasks for Taxon
262
		$items[] = array(
263
      'path' => 'cdm_dataportal/name/'.arg(2),
264
      //'callback' => 'cdm_dataportal_view_name',
265
		  'callback' => 'cdm_dataportal_name_page_view',
266
      'callback arguments' => array(arg(2), arg(3)),
267
      'access' => true,
268
      'type' => MENU_CALLBACK,
269
    );
270
    
271
		if(variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
272

    
273
			$items[] = array(
274
      'path' => 'cdm_dataportal/taxon/'.arg(2),
275
      'title' => theme('cdm_taxonpage_tab', 'General'),
276
      'callback' => 'cdm_dataportal_taxon_page_view',
277
      'access' => true,
278
      'type' => MENU_CALLBACK,
279
      'weight' => 1,
280
      'callback arguments' => array(arg(2), "description")
281
			// expected callback arguments: name_uuid
282
			);
283

    
284
			$items[] = array(
285
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/all',
286
      'title' => theme('cdm_taxonpage_tab', 'General'),
287
      'callback' => 'cdm_dataportal_taxon_page_view',
288
      'access' => true,
289
      'type' => MENU_CALLBACK,
290
      'weight' => 2,
291
      'callback arguments' => array(arg(2), "all")
292
			// expected callback arguments: name_uuid
293
			);
294

    
295
			$items[] = array(
296
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/description',
297
      'title' => theme('cdm_taxonpage_tab', 'General'),
298
      'callback' => 'cdm_dataportal_taxon_page_view',
299
      'access' => true,
300
      'type' => MENU_DEFAULT_LOCAL_TASK,
301
      'weight' => 2,
302
      'callback arguments' => array(arg(2), "description")
303
			// expected callback arguments: name_uuid
304
			);
305

    
306
			$items[] = array(
307
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/synonymy',
308
      'title' => theme('cdm_taxonpage_tab', 'Synonymy'),
309
      'callback' => 'cdm_dataportal_taxon_page_view',
310
      'access' => true,
311
      'type' => MENU_LOCAL_TASK,
312
      'weight' => 3,
313
      'callback arguments' => array(arg(2), "synonymy")
314
			// expected callback arguments: name_uuid
315
			);
316

    
317
			$items[] = array(
318
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/images',
319
      'title' => theme('cdm_taxonpage_tab', 'Images'),
320
      'callback' => 'cdm_dataportal_taxon_page_view',
321
      'access' => true,
322
      'type' => MENU_LOCAL_TASK,
323
      'weight' => 4,
324
      'callback arguments' => array(arg(2), "images")
325
			// expected callback arguments: name_uuid
326
			);
327
		}
328
	}
329

    
330
	drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal.css');
331
	//drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
332
	drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_screen.css', 'screen');
333

    
334
	return $items;
335

    
336
}
337

    
338
/**
339
 * Implementation of hook_block()
340
 *
341
 * Provides the following blocks:
342
 *  0: list of links useful during development
343
 *
344
 * @param String $op
345
 * @param int $delta
346
 */
347
function cdm_dataportal_block($op='list', $delta=0) {
348
	// listing of blocks, such as on the admin/block page
349
	if ($op == "list") {
350
		//$block[0]["info"] = t("CDM DataPortal DevLinks");
351
//		$block[1]["info"] = t("CDM DataPortal Credits");
352
		$block[2]["info"] = t("CDM Search Taxa");
353
		//$block[3]["info"] = t("CDM Filters");
354
		$block[4]["info"] = t("CDM Dataportal Print");
355
		return $block;
356
	}
357
	else if ($op == 'view') {
358
		switch($delta){
359
//			case 1:
360
//				$block['subject'] = t('Credits');
361
//				$block['content'] = theme('cdm_credits');
362
//				return $block;
363
			case 2:
364
				$block['subject'] = t('Search taxa');
365
				$block['content'] = drupal_get_form('cdm_dataportal_search_taxon_form');
366
				$block['content'] .= '<div>'.l('Advanced Search', '/cdm_dataportal/search').'</div>';
367
				return $block;
368
			case 4:
369
				$block['subject'] = t('Print this page');
370
				$block['content'] = theme('cdm_print_button');;
371
				return $block;
372
		}
373
	}
374
}
375

    
376

    
377
/**
378
 * Implementation of hook_validate()
379
 *
380
 * @param $element
381
 */
382
function cdm_dataportal_settings_validate($form_id, $form_values){
383

    
384
	if (!str_endsWith($form_values['cdm_webservice_url'], '/')) {
385
		//form_set_error('cdm_webservice_url', t("The URL to the CDM Web Service must end with a slash: '/'."));
386
		$form_values['cdm_webservice_url'] .= '/';
387
	}
388

    
389
//	$cdm_webservice_url_changed = variable_get('cdm_webservice_url', '') != $form_values['cdm_webservice_url'];
390

    
391
//	if ($cdm_webservice_url_changed) {
392
//
393
//		//FIXME remove _cdm_dataportal_set_currentSecUuid($form_values['cdm_secUuid_default']);
394
//		// reset all cdm related data stored in the session
395
//		// cdm_dataportal_session_clear($form_values['cdm_webservice_url']); // cdm_webservice_url is not further stored here
396
//		// clear the cdm webservice cache
397
//		cache_clear_all(NULL, 'cache_cdm_ws');
398
//		// better clear secref_cache since i can not be sure if the cache has not be used during this response
399
//		cdm_api_secref_cache_clear();
400
//	}
401
	if($form_values['cdm_webservice_cache'] != variable_get('cdm_webservice_cache', 1)){
402
		cache_clear_all(NULL, 'cache_cdm_ws');
403
		// better clear secref_cache since i can not be sure if the cache has not be used during this response
404
		cdm_api_secref_cache_clear();
405
	}
406

    
407
}
408

    
409
/*
410
 function cdm_dataportal_session_clear($cdm_ws_uri_update = false){
411
 $_SESSION['cdm'] = null;
412
 if(is_string($cdm_ws_uri_update)){
413
 $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
414
 }
415
 }
416

    
417
 function cdm_dataportal_session_validate(){
418

    
419
 if(!isset($_SESSION['cdm']['ws_uri'])){
420
 $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', false));
421
 } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', false)){
422
 cdm_dataportal_session_clear(variable_get('cdm_webservice_url', false));
423
 }
424
 }
425
 */
426

    
427
function cdm_dataportal_search_taxon_form($advancedForm = false){
428

    
429
	$preset_query = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
430
	$preset_doTaxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
431
	$preset_doSynonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
432
	$preset_doTaxaByCommonNames = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
433
	$tdwg_level_select =  (isset($_SESSION['cdm']['search']['tdwg_level_select']) ? $_SESSION['cdm']['search']['tdwg_level_select'] : 2);
434
	$selected_areas =  (isset($_SESSION['cdm']['search']['area']) ? $_SESSION['cdm']['search']['area'] : false);
435

    
436
	$url = 'cdm_dataportal/search/taxon';
437
	$form['#method'] = 'get';
438
	$form['#process'] = array('cdm_dataportal_search_process' => array());
439
	$form['#action'] = url($url, NULL, NULL, true);
440

    
441
	$form['query'] = array(
442
    '#delta' => 0,
443
    '#type' => 'textfield',
444
    '#size' => 20,
445
    '#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')),
446
    '#value' => $preset_query,
447
	);
448

    
449
	$form['search'] = array(
450
      '#delta' => 1,
451
      '#tree' => true,
452
	//'#type' => $advancedForm ? 'fieldset': 'hidden',
453
      '#title' => t('Options')
454
	);
455

    
456
	$form['search']['tree'] = array(
457
    '#delta' => -1,
458
    '#type' => 'hidden',
459
    '#value' => get_taxonomictree_uuid_selected()
460
	);
461

    
462

    
463
	// clean URL get forms breaks if we don't give it a 'q'.
464
	if (!(bool)variable_get('clean_url', '0')) {
465
		$form['search']['q'] = array(
466
      '#delta' => -1,
467
      '#type' => 'hidden',
468
      '#value' => $url,
469
      '#name' => 'q',
470
		);
471
	}
472

    
473
	$form['search']['pageSize'] = array(
474
      '#delta' => -1,
475
      '#type' => 'hidden',
476
      '#value' => variable_get('cdm_dataportal_search_items_on_page', 25)
477
	);
478

    
479
	if($advancedForm){
480
		// general search parameters
481
		$form['search']['doTaxa'] = array(
482
      '#delta' => 2,
483
      '#type' => 'checkbox',
484
      '#title' => t('Search for accepted taxa'),
485
      '#value' => $preset_doTaxa
486
		);
487
		$form['search']['doSynonyms'] = array(
488
      '#delta' => 3,
489
      '#type' => 'checkbox',
490
      '#title' => t('Search for synonyms'),
491
      '#value' => $preset_doSynonyms
492
		);
493
		$form['search']['doTaxaByCommonNames'] = array(
494
      '#delta' => 4,
495
      '#type' => 'checkbox',
496
      '#title' => t('Search for common names'),
497
      '#value' => $preset_doTaxaByCommonNames
498
		);
499
			
500
		// Geographic Range
501
		$form['search']['geographic_range'] = array(
502
      '#type' => 'fieldset',
503
      '#delta' => 5,
504
      '#tree' => true,
505
      '#title' => t('Geographic range'),
506
		);
507

    
508
		$form['search']['geographic_range']['tdwg_level_select'] = array(
509
    '#type' => 'radios',
510
    '#title' => t('Select a TDWG distribution level and code'),
511
    '#default_value' => $tdwg_level_select,
512
    '#options' => array(
513
		t('TDWG level-1, i.e. a continent'),
514
		t('TDWG level-2'),
515
		t('TDWG level-3, i.e. a country'),
516
		t('TDWG level-4')
517
		)
518
		);
519
		$tdwg[1] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '1');
520
		$tdwg[2] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '2');
521
		$tdwg[3] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '3');
522
		$tdwg[4] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '4');
523

    
524
		$tdwg_js = '';
525
		foreach($tdwg as $key=>$tdwg_level){
526
			$tdwgOptions = array();
527
			$tdwgOptionsSelected = array();
528
			foreach($tdwg_level as $area){
529
				$representation = $area->representations[0];
530
				$tdwgOptions[$representation->abbreviatedLabel] = $area->representation_L10n;
531
				if(is_array($selected_areas) && in_array($representation->abbreviatedLabel, $selected_areas)){
532
					$tdwgOptionsSelected[] = $representation->abbreviatedLabel; //$area->uuid;
533
				}
534
			}
535
			asort($tdwgOptions);
536
			$form['search']['geographic_range']['tdwg_level_'.$key] = array(
537
        '#type' => 'select',
538
        '#title'         => t('TDWG level').' '.$key,
539
        '#default_value' => $tdwgOptionsSelected,
540
        '#multiple' => TRUE, 
541
        '#options' => $tdwgOptions
542
			);
543
			$tdwg_js .= "$('#edit-search-geographic-range-tdwg-level-$key').parent()".($tdwg_level_select + 1 == $key ?  '.show()' : '.hide()'). ";\n";
544
		}
545

    
546
		drupal_add_js(
547
    "$(document).ready(function(){
548
    
549
      $(\"input[@name='search[geographic_range][tdwg_level_select]']\").change( 
550
        function(event){
551
          var selectId = $(\"input[@name='search[geographic_range][tdwg_level_select]']:checked\").val();
552
          var i;
553
          for(i = 0; i < 4; i++){
554
            if(selectId == i){
555
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1) ).parent().fadeIn('slow');
556
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
557
            } else {
558
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).parent().fadeOut('slow');
559
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
560
            }
561
          }
562
        }
563
      );
564
      
565
      $tdwg_js
566
    });",
567
    'inline');
568

    
569
	} else {
570
		$form['search']['doTaxa'] = array(
571
      '#delta' => -2,
572
      '#type' => 'hidden',
573
      '#value' => 1
574
		);
575
		$form['search']['doSynonyms'] = array(
576
      '#delta' => -3,
577
      '#type' => 'hidden',
578
      '#value' => 1
579
		);
580
		$form['search']['doTaxaByCommonNames'] = array(
581
      '#delta' => -4,
582
      '#type' => 'hidden',
583
      '#value' => 1
584
		);
585
	}
586

    
587
	$form['submit'] = array(
588
  '#delta' => 9,
589
  '#type' => 'submit',
590
  '#name' => '',
591
  '#value' => t('Search')
592
	);
593

    
594
	return $form;
595
}
596

    
597
function cdm_dataportal_search_taxon_form_advanced(){
598
	return cdm_dataportal_search_taxon_form(true);
599
}
600

    
601
function cdm_taxonomictree_selector(){
602
	_add_js_treeselector();
603
    
604
    $out = drupal_get_form('cdm_taxonomictree_selector_form');
605
    
606
    return $out;
607
}
608

    
609

    
610
function cdm_taxonomictree_selector_form(){
611
	
612
	$url = url('cdm_api/setvalue/session', null);
613
    $form['#action'] = $url;
614
    
615
    $form['var'] = array(
616
        '#delta' => -3,
617
        '#type' => 'hidden',
618
        '#value' => '[cdm][taxonomictree_uuid]'    
619
    );
620
    
621
    $form['destination'] = array(
622
        '#delta' => -3,
623
        '#type' => 'hidden',
624
        '#value' => substr(drupal_get_destination(), strlen('destination='))
625
    );
626
    
627
    $form['val'] = array(
628
	    '#type' => 'select',
629
	    '#title'         => t('Available classifications'),
630
	    '#default_value' => get_taxonomictree_uuid_selected(),
631
	    '#options' => cdm_get_taxontrees_as_options()
632
    );
633
    
634
    return $form;
635
	
636
}
637

    
638
/**
639
 * Implementation #process method call, see form_builder()
640
 * <p>
641
 * Removes Drupal internal form elements from query
642
 * @param $form
643
 * @return unknown_type
644
 */
645
function cdm_dataportal_search_process($form) {
646
	unset($form['form_id']);
647
	unset($form['form_token']);
648
	return $form;
649
}
650

    
651
/**
652
 * Filters $_REQUEST by a list of valid request  parameters and also sets defaults if required.
653
 * returns the processed request parameters submitted by the search form.
654
 */
655
function cdm_dataportal_search_form_request(){
656

    
657

    
658
	$form_params = array();
659
	array_deep_copy($_REQUEST['search'], $form_params);
660
	$form_params['query'] =  trim($_REQUEST['query']);
661

    
662
	// split of  geographic range
663
	if(isset($_REQUEST['search']['geographic_range'])){
664
		$geographicRange = $_REQUEST['search']['geographic_range'];
665
		// remove
666
		unset($form_params['geographic_range']);
667
	}
668

    
669
	// add geographic range
670
	if($geographicRange){
671
		$form_params['tdwg_level_select'] = $geographicRange['tdwg_level_select'];
672
		for($i = 1; $i < 5; $i++){
673
			if(isset($geographicRange['tdwg_level_'.$i])){
674
				$form_params['area'] = $geographicRange['tdwg_level_'.$i];
675
			}
676
		}
677
	}
678

    
679
	// store in session
680
	$_SESSION['cdm']['search'] = $form_params;
681

    
682
	return $form_params;
683
}
684

    
685

    
686
/* UNREACHABLE since action of form directly links to view
687
 function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
688

    
689
 $_SESSION['cdm']['search'] = $form_values;
690
 //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
691
 return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
692
 //$paramstr = compose_url_prameterstr($form_values);
693
 //return url('/cdm_dataportal/search/taxon/', $paramstr);
694
 }
695
 */
696
/* ====================== menu callback functions ====================== */
697

    
698
/**
699
 * Generate main administration form.
700
 *
701
 * @return
702
 *   An array containing form items to place on the module settings page.
703
 */
704
function cdm_dataportal_settings(){
705

    
706
	$form = cdm_api_settings_form();
707

    
708
	//TODO: settings are still incomplete, compare with trunk/dataportal/inc/config_default.php.inc
709
	$form['cdm_dataportal'] = array(
710
      '#type' => 'fieldset',
711
      '#title' => t('CDM DataPortal'),
712
      '#collapsible' => FALSE,
713
      '#collapsed' => TRUE,
714
	);
715

    
716
	$form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
717
    '#type' => 'select',
718
    '#title'         => t('Available classifications'),
719
    '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
720
    '#options' => cdm_get_taxontrees_as_options(),
721
    '#description'   => t('Select the default classification to be used.')
722
	);
723

    
724
	//------------------ FEATURE TREE --------------------//
725

    
726
	$form['cdm_dataportal']['feature_tree'] = array(
727
    '#type' => 'fieldset',
728
    '#title' => t('Feature Tree'),
729
    '#collapsible' => TRUE,
730
    '#collapsed' => TRUE,
731
	);
732

    
733
	$form['cdm_dataportal']['feature_tree'][CDM_DATAPORTAL_DEFAULT_FEATURETREE_UUID] = array(
734
    '#type' => 'select',
735
    '#title'         => t('Available FeatureTrees'),                
736
    '#default_value' => variable_get(CDM_DATAPORTAL_FEATURETREE_UUID, UUID_DEFAULT_FEATURETREE),
737
	  '#options' => cdm_get_featureTrees_as_options(),
738
    '#description'   => t('Select a FeatureTree for this webservice.'
739
    //.' If there is no applicable FeatureTree you can create a new one using the <a href="">FeatureTreeManager</a>'
740
	)
741
	);
742

    
743
	return system_settings_form($form);
744
}
745

    
746
function cdm_dataportal_form_alter($form_id, &$form) {
747
    
748
static $comment_node_disabled =  0;
749
static $comment_node_read_only =  1;
750
static $comment_node_read_write =  2;
751
   
752
    
753
  if ($form_id == 'node_type_form' 
754
    && isset($form['identity']['type']) 
755
    && array_key_exists($form['#node_type']->type, cdm_dataportal_get_nodetypes()) 
756
    ) {
757
    $form['workflow']['comment'] = array(
758
      '#type' => 'radios',
759
      '#title' => t('Default comment setting'),
760
      '#default_value' => variable_get('comment_'. $form['#node_type']->type, $comment_node_disabled),
761
      '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
762
      '#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
763
    );
764
  }
765
}
766

    
767
/**
768
 * LAYOUT settings
769
 * @return unknown_type
770
 */
771
function cdm_dataportal_settings_layout(){
772

    
773
	$form = array();
774

    
775

    
776
	$form['cdm_taxonname_type'] = array(
777
    '#type' => 'select',
778
    '#title'         => t('Taxon name type'),
779
    '#default_value' => variable_get('cdm_taxonname_type', 'BotanicalName'),
780
    '#options' => array( 'BotanicalName'=>t('BotanicalName'), 'ZoologicalName'=>t('ZoologicalName')),
781
    '#description'   => t('')
782
	);
783

    
784
	$form['cdm_dataportal_nomref_in_title'] = array(
785
    '#type' => 'checkbox',
786
    '#title' => t('Show full nomenclatural reference in title'),
787
    '#default_value' => variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE),
788
    '#description' => t('Uncheck this, if you want the title of a taxon page consisting of name and author plus publication year only.
789
                        If not checked, the first homotypic taxon is a repetition of the accepted taxon with
790
                        the full nomenclatural reference.')
791
	);
792

    
793
	$form[CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS] = array(
794
    '#type' => 'checkbox',
795
    '#title' => t('Show name relations of accepted taxa on taxon page'),
796
    '#default_value' => variable_get(CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS_DEFAULT),
797
    '#description' => t('')
798
	);
799
	
800
	$form[CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS] = array(
801
    '#type' => 'checkbox',
802
    '#title' => t('Show taxon relations of accepted taxa on taxon page'),
803
    '#default_value' => variable_get(CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT),
804
    '#description' => t('')
805
    );
806

    
807
	$form['cdm_dataportal_display_is_accepted_for'] = array(
808
    '#type' => 'checkbox',
809
    '#title' => t('Display <em>is accepted for ...</em> on taxon pages when coming from a synonym link.'),
810
    '#default_value' => variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR),
811
    '#description' => t('')
812
	);
813

    
814
	$form['cdm_dataportal_name_relations_skiptype_basionym'] = array(
815
    '#type' => 'checkbox',
816
    '#title' => t('Exclude the basionym relationship type from the taxon page'),
817
    '#default_value' => variable_get('cdm_dataportal_name_relations_skiptype_basionym', 1),
818
    '#description' => t('')
819
	);
820

    
821
	$form['cdm_dataportal_taxonpage_tabs'] = array(
822
    '#type' => 'checkbox',
823
    '#title' => t('Tabbed taxon page'),
824
    '#default_value' => variable_get('cdm_dataportal_taxonpage_tabs', 1),
825
    '#description' => t('Split the taxon page into individual tabs for description, images, synonymy')
826
	);
827

    
828
	$form['cdm_dataportal_search_items_on_page'] = array(
829
    '#type' => 'textfield',
830
    '#title' => t('Search Page Size'),
831
    '#default_value' => variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE),
832
    '#description' => t('Number of Names to display per page in search results.')
833
	);
834

    
835
	$form['cdm_dataportal_descriptions_separated'] = array(
836
    '#type' => 'checkbox',
837
    '#title' => t('Separate Descriptions'),
838
    '#default_value' => variable_get('cdm_dataportal_descriptions_separated', 0),
839
    '#description' => t('By default corresponding elements of different descriptions are joined together'
840
    .' into a common section per feature (i.e. type of description).'
841
    .' Check this box to allow displaying all descriptions separately.')
842
    );
843

    
844
    //---- IMAGES ----//
845
    $form['images'] = array(
846
    '#type' => 'fieldset',
847
    '#title' => t('Images'),
848
    '#collapsible' => TRUE,
849
    '#collapsed' => FALSE,
850
    );
851
    $options = cdm_rankVocabulary_as_option();
852
    array_unshift($options, '-- DISABLED --');
853
    $form['images']['image_hide_rank'] =  array(
854
    '#type'          => 'select',
855
    '#title'         => t('Hide Images for Taxa above'),
856
    '#default_value' => variable_get('image_hide_rank', '0'),
857
    '#options'       => $options,
858
    '#description'   => t(''),
859
    );
860
    //show media
861
    $selectShowMedia = array(0 => "Show only taxon media",
862
    1 => "Show taxon and child taxon media");
863
    $form['images']['cdm_dataportal_show_media'] = array(
864
    '#type' => 'select',
865
    '#title' => t('Available media files'),
866
    '#default_value' => variable_get('cdm_dataportal_show_media', false),
867
    '#options' => $selectShowMedia,
868
    '#description'   => t('Select if a taxon should show only his media or also child media.')
869
    );
870

    
871
    // --- SEARCH TAXA GALLERY ---- //
872
    $collapsed = TRUE;
873
    $form_name = CDM_DATAPORTAL_SEARCH_GALLERY_NAME;
874
    $form_tittle = 'Search Taxa';
875
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
876

    
877
    // --- FEATURE DESCRIPTION GALLERY ---- //
878
    $form_name = CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME;
879
    $form_tittle = 'Description elements gallery';
880
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
881

    
882
    // --- MEDIA GALLERY ---- //
883
    $form_name = CDM_DATAPORTAL_MEDIA_GALLERY_NAME;
884
    $form_tittle = 'Media gallery';
885
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
886

    
887
    return system_settings_form($form);
888
}
889

    
890
function cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed){
891

    
892
	$form[$form_name] = array(
893
    '#type' => 'fieldset',
894
    '#title' => t($form_tittle),
895
    '#collapsible' => TRUE,
896
    '#collapsed' => $collapsed,
897
    '#tree' => true,
898
	);
899

    
900
	$default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
901
	$gallery_settings = variable_get($form_name, $default_values);
902

    
903
  if($form_name == CDM_DATAPORTAL_SEARCH_GALLERY_NAME){
904
    $form[$form_name]['cdm_dataportal_show_taxon_thumbnails'] = array(
905
      '#type' => 'checkbox',
906
      '#title' => t('Show media thumbnails for accepted taxa'),
907
      '#default_value' => $gallery_settings['cdm_dataportal_show_taxon_thumbnails'],
908
      '#description' => t('')
909
    );
910

    
911
    $form[$form_name]['cdm_dataportal_show_synonym_thumbnails'] = array(
912
      '#type' => 'checkbox',
913
      '#title' => t('Show media thumbnails for synonyms'),
914
      '#default_value' => $gallery_settings['cdm_dataportal_show_synonym_thumbnails'],
915
      '#description' => t('')
916
    );
917
  }
918
  
919
	//$showCaption = variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 0);
920
	$form[$form_name]['cdm_dataportal_show_thumbnail_captions'] = array(
921
    '#type' => 'checkbox',
922
    '#title' => t('Show captions under thumbnails'),
923
    '#default_value' => $gallery_settings['cdm_dataportal_show_thumbnail_captions'],
924
    '#description' => t('')
925
	);
926

    
927
	$form[$form_name]['cdm_dataportal_media_maxextend'] = array(
928
    '#type' => 'textfield',
929
    '#title' => t('Maximum extend of Images'),
930
    '#default_value' => $gallery_settings['cdm_dataportal_media_maxextend'],
931
    '#description' => t('In the gallery neither width or height will exceed the maximum extend of images.')
932
	);
933

    
934
	$form[$form_name]['cdm_dataportal_media_cols'] = array(
935
    '#type' => 'textfield',
936
    '#title' => t('Number of columns'),
937
    '#default_value' => $gallery_settings['cdm_dataportal_media_cols'],
938
    '#description' => t('')
939
	);
940

    
941
  if($form_name == CDM_DATAPORTAL_SEARCH_GALLERY_NAME){
942
    $form[$form_name]['cdm_dataportal_media_maxRows'] = array(
943
      '#type' => 'textfield',
944
      '#title' => t('Maximum number of rows'),
945
      '#default_value' => $gallery_settings['cdm_dataportal_media_maxRows'],
946
      '#description' => t('If you want an unlimited number of rows please set to 0')
947
    );
948
  }
949

    
950
	return $form;
951
}
952

    
953
/**
954
 * GEOSERVICE and Map settings
955
 * @return unknown_type
956
 */
957
function cdm_dataportal_settings_geo(){
958

    
959
	$form = array();
960

    
961
	$form['cdm_dataportal_map_openlayers'] = array(
962
    '#type' => 'checkbox',
963
    '#title' => t('OpenLayers Viewer'),
964
    '#default_value' => variable_get('cdm_dataportal_map_openlayers', 1),
965
    '#description' => t('Display the maps in an interactive viewer which allows zooming and panning.')
966
	);
967

    
968
	$form['cdm_dataportal_geoservice_access_point'] = array(
969
    '#type' => 'textfield',
970
    '#title' => t('Geoservice Access Point'),
971
    '#default_value' => variable_get('cdm_dataportal_geoservice_access_point', ''),
972
    '#description' => t('Base URL of the geoservice to be used by this portal')
973
	);
974

    
975
	$form['cdm_dataportal_geoservice_display_width'] = array(
976
    '#type' => 'textfield',
977
    '#title' => t('Geoservice Display Width'),
978
    '#default_value' => variable_get('cdm_dataportal_geoservice_display_width', 390),
979
    '#description' => t('Width of the image generated by geoservice')
980
	);
981

    
982
	$form['cdm_dataportal_geoservice_bounding_box'] = array(
983
    '#type' => 'textfield',
984
    '#title' => t('Fixed Geoservice Bounding Box'),
985
    '#default_value' => variable_get('cdm_dataportal_geoservice_bounding_box', '-180,-90,180,90'),
986
    '#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.')
987
	);
988

    
989
	//cdm_dataportal_geoservice_labels_on
990
	$form['cdm_dataportal_geoservice_labels_on'] = array(
991
    '#type' => 'checkbox',
992
    '#title' => t('Display Country Labels'),
993
    '#default_value' => variable_get('cdm_dataportal_geoservice_labels_on', FALSE),
994
    '#description' => t('Check this if you like country names to be displayed in the maps. ')
995
	);
996

    
997
	//cdm_dataportal_geoservice_legend_on
998
	$form['cdm_dataportal_geoservice_map_caption'] = array(
999
    '#type' => 'textfield',
1000
    '#title' => t('Map Caption'),
1001
    '#default_value' => variable_get('cdm_dataportal_geoservice_map_caption', ''),
1002
    '#description' => t('Define a caption for the map.')
1003
	);
1004

    
1005
	//cdm_dataportal_geoservice_map_legend
1006
	$form['cdm_dataportal_geoservice_map_legend'] = array(
1007
      '#type' => 'fieldset',
1008
      '#title' => t('Map Legend'),
1009
      '#collapsible' => FALSE,
1010
      '#collapsed' => TRUE,
1011
	);
1012

    
1013
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_on'] = array(
1014
    '#type' => 'checkbox',
1015
    '#title' => t('Display a map legend'),
1016
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_on', TRUE),
1017
    '#description' => t('Check this if you like a legend to be displayed with the maps. ')
1018
	);
1019

    
1020
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_size'] = array(
1021
    '#type' => 'textfield',
1022
    '#title' => t('Font size'),
1023
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_size', 10),
1024
    '#description' => t('Font size in pixels.')
1025
	);
1026

    
1027
	$fontStyles = array(0 => "plane", 1 => "italic");
1028
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_style'] = array(
1029
    '#type' => 'select',
1030
    '#title' => t('Available font styles'),
1031
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_style', false),
1032
    '#options' => $fontStyles,
1033
    '#description'   => t('Select a font style for the map legend.')
1034
	);
1035

    
1036
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_width'] = array(
1037
    '#type' => 'textfield',
1038
    '#title' => t('Icon width'),
1039
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_width', 35),
1040
    '#description' => t('Icon width in pixels.')
1041
	);
1042

    
1043
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_height'] = array(
1044
    '#type' => 'textfield',
1045
    '#title' => t('Icon height'),
1046
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_height', 15),
1047
    '#description' => t('Icon height in pixels.')
1048
	);
1049

    
1050
	return system_settings_form($form);
1051
}
1052

    
1053

    
1054
/**
1055
 * @return walk and cache all taxon pages
1056
 */
1057
function cdm_dataportal_view_cache_site(){
1058

    
1059
	_add_js_progressbar();
1060

    
1061
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/cache_all_taxa.js');
1062

    
1063
	$out = '';
1064

    
1065
	$request_params = array();
1066
	$request_params['query'] = '%';
1067
	$request_params['tree'] = variable_get('cdm_taxonomictree_uuid', false); //cache only the dafault classification
1068
	$request_params['doTaxa'] = 1;
1069
	$request_params['doSynonyms'] = 0;
1070
	$request_params['doTaxaByCommonNames'] = 0;
1071
	$search_url = cdm_compose_url(CDM_WS_PORTAL_TAXON_FIND, null, queryString($request_params));
1072
	$search_url = uri_uriByProxy($search_url);
1073
	$taxon_page_url = url('cdm_dataportal/taxon/');
1074

    
1075
	$out .= '<form id="cache_site">';
1076
	$out .= '<br /><h4>'.t('Cache all taxon pages').'</h4>';
1077
	$out .= 'Before  running the cache bot you have to empty the cache manually:<ul>' 
1078
            .'<li>Navigate to "Site Configuration -> General"</li>'
1079
            .'<li>Uncheck "Enable Caching" checkbox</li>'
1080
            .'<li>Check "Enable Caching" checkbox</li>'
1081
	        .'</ul>';
1082
	$out .= '<div>'.t('This caching process may take long time and could cause heavy load on your server').'</div>';
1083
	$out .= '<div id="progress"></div>';
1084
	$out .= '<input type="hidden" name="searchTaxaUrl" value="'.$search_url.'"/>';
1085
	$out .= '<input type="hidden" name="taxonPageUrl" value="'.$taxon_page_url.'"/>';
1086
	$out .= '<input type="button" name="start" value="'.t('Start').'"/>';
1087
	$out .= '<input type="button" name="stop" value="'.t('Stop').'"/>';
1088
	$out .= '</form>';
1089
	$out .= '</div>';
1090
	//  foreach($taxonPager->records as $taxon){
1091
	//    cdm_dataportal_taxon_view($uuid);
1092
	//  }
1093

    
1094
	return $out;
1095
}
1096

    
1097
/**
1098
 * Implementation of hook_elements().
1099
 */
1100
function cdm_dataportal_elements() {
1101
	$type['select_secuuid'] = array(
1102
    '#input' => TRUE,
1103
    '#process' => array('cdm_dataportal_select_secuuid_expand' => array()),
1104
	//'#validate' => array('cdm_dataportal_select_secuuid_validate' => array()),
1105
	//'#default_value' => array(),
1106
	);
1107
	return $type;
1108
}
1109

    
1110
/*
1111
 *
1112
 */
1113
function cdm_dataportal_select_secuuid_expand($element){
1114

    
1115
	$element['#tree'] = FALSE;
1116

    
1117
	// any value submitted?
1118
	if(isset($element['#post'][$element['#varname']])){
1119
		$selected_values = $element['#post'][$element['#varname']];
1120
	} else {
1121
		// use those store in drupal
1122
		$selected_values = variable_get( $element['#varname'], array());
1123
	}
1124
	if(!is_array($selected_values)){
1125
		$selected_values = array($selected_values);
1126
	}
1127
	$options = array();
1128
	foreach($selected_values as $secUuid){
1129
		$options[$secUuid] = cdm_taxontree_secRefTitle_for($secUuid);
1130
	}
1131

    
1132
	$element[$element['#varname']] =  array(
1133
    '#type' => 'select',
1134
    '#options' => $options,
1135
    '#default_value' => array_values($options),
1136
    '#size' => $element['#multiple'] ? 12 : 2,
1137
    '#multiple' => $element['#multiple'],
1138
	);
1139
	return $element;
1140
}
1141

    
1142

    
1143

    
1144
/**
1145
 * Displays a list of the known taxonomic names. Long lists are split up into multiple pages
1146
 *
1147
 * TODO: parameters are still preliminar
1148
 * @param String $page page number to diplay defaults to page 1
1149
 * @param boolean $hide_unaccepted whether to hide nams which are not accepted by the current view
1150
 */
1151
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = false ){
1152

    
1153
	$request_params  = array(
1154
    'q' => $beginsWith,
1155
	//'sec' = '',
1156
	//'higherTaxa' => getFiters(),
1157
	// 'matchAnywhere' => false, // default is false
1158
    'page' => $page,
1159
    'onlyAccepted' => $onlyAccepted,
1160
    'pagesize' => 20  /*$_SESSION['cdm']['namelist_pagesize'] */);
1161

    
1162
	$taxonPager = cdm_ws_find(CDM_WS_PORTAL_TAXON_FIND, $request_params);
1163
	/*
1164
	 * FIXME the filter for accepted names will be a form element, thus this widget
1165
	 * should be generated via form api preferably as block
1166
	 */
1167
	//$out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
1168
	//$out .= theme('cdm_dataportal_widget_names_list', $names, $page);
1169
	$out .= theme('cdm_listof_taxa', $taxonPager);
1170
	return $out;
1171
}
1172

    
1173
function cdm_dataportal_view_reference($uuid, $arg2 = null){
1174
	$reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
1175
	return theme('cdm_reference_page', $reference);
1176
}
1177

    
1178
function cdm_dataportal_view_reference_list($pageNumber){
1179
	$referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE), $pageNumber);
1180
	return theme('cdm_reference_pager', $referencePager, 'cdm_dataportal/reference/list/');
1181
}
1182

    
1183
function cdm_dataportal_view_media($mediaUuid, $mediarepresentation_uuid = false, $part = 0){
1184
	$media = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $mediaUuid);
1185
	return theme('cdm_media_page', $media, $mediarepresentation_uuid, $part);
1186
}
1187

    
1188
/**
1189
 * The taxon page gives detailed information on a taxon, it shows:
1190
 *  - Taxon name
1191
 *  - Full list of synonyms homotypic synonyms on top, followed by the
1192
 *    heterotypic and finally followed by misapplied names.
1193
 *    The list is ordered historically.
1194
 *  - All description associated with the taxon.
1195
 *
1196
 * @param $uuid
1197
 * @param $chapter name of the part to display,
1198
 *         valid values are: 'description', 'images', 'synonymy', 'all'
1199
 * @return unknown_type
1200
 */
1201
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all'){
1202

    
1203
	// display the page for the taxon defined by $uuid
1204
	$taxonpage = cdm_dataportal_taxon_view($uuid, $chapter);
1205
	return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title , $taxonpage->content);
1206
}
1207

    
1208
/**
1209
 * @param $uuid
1210
 * @param $chapter name of the part to display,
1211
 *         valid values are: 'description', 'images', 'synonymy', 'all'
1212
 * @return unknown_type
1213
 */
1214
function cdm_dataportal_taxon_view($uuid, $chapter = 'all'){
1215

    
1216
	$taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $uuid);
1217
	if(!$taxon){
1218
		drupal_set_title(t('Taxon does not exist'));
1219
		return false;
1220
	}
1221
	$taxonpage->title = theme('cdm_taxon_page_title', $taxon);
1222
	
1223
	// check if the taxon id contained in the currently selected tree
1224
	$taxon_nodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, $uuid);
1225
	$taxon_in_current_tree = false;
1226
	foreach($taxon_nodes as $node){	
1227
	   if(get_taxonomictree_uuid_selected() == $node->taxonomicTree->uuid){
1228
	       $taxon_in_current_tree = true;
1229
	       break;
1230
	   }
1231
	}
1232
	if(!$taxon_in_current_tree){
1233
		if(count($taxon_nodes) == 0){
1234
			drupal_set_message('This concept of the taxon '.theme('cdm_taxonName', $taxon->name).' is not contained any classification.'
1235
			,'warning');
1236
		} else {
1237
			$trees = '';
1238
			foreach($taxon_nodes as $node){ 
1239
				$trees .= ($trees?', ':'').'<strong>'.$node->taxonomicTree->titleCache.'</strong>';
1240
				
1241
			} 
1242
			drupal_set_message('This concept of the taxon '.theme('cdm_taxonName', $taxon->name).' is not contained in the currently chosen classification, but in '
1243
			.(count($taxon_nodes) > 1? ' one of these: ' : ' this one: ') . $trees
1244
			, 'warning');
1245
		}
1246
	}
1247

    
1248
	// render the taxon page
1249
	$taxonpage->content = theme('cdm_taxon_page_general', $taxon, $chapter);
1250

    
1251
	return $taxonpage;
1252
}
1253

    
1254
/**
1255
 * The function returns a name page as a drupal node ready to be renderized by drupal.
1256
 * The node page show the taxon name title and the list of taxon related with such taxon 
1257
 * name on the tree already in used. 
1258
 * @param $taxon_name_uuid A taxon name uuid
1259
 * @return The formatted name page as node
1260
 */
1261
function cdm_dataportal_name_page_view($taxon_name_uuid, $taxon_to_hide_uuid){
1262
  $taxonname_page = cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid);
1263
  return cdm_node_show(NODETYPE_NAME, $taxon_name_uuid, $taxonname_page->title , $taxonname_page->content);
1264
}
1265

    
1266
/**
1267
 * The function genates a object ready to be transformated to a node 
1268
 * in order to show as a drupal node
1269
 * @param $taxon_name_uuid
1270
 * @return the object with the page content and title  
1271
 */
1272
function cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid){
1273
	//getting the full taxonname object from the server  
1274
  $taxon_name = cdm_ws_get(CDM_WS_NAME, array($taxon_name_uuid));
1275
  if(!$taxon_name){
1276
    drupal_set_title(t('Taxon name does not exist'));
1277
    return false;
1278
  }
1279
  //searching for all the taxon connected with the taxon name on the tree in used
1280
  $name_cache = cdm_ws_get(CDM_WS_NAME_NAMECAHE, array($taxon_name_uuid));
1281
  $request_params = array();
1282
  $request_params['query'] = $name_cache;
1283
  $request_params['tree'] = get_taxonomictree_uuid_selected();
1284
  $request_params['doTaxa'] = 1;
1285
  $request_params['doSynonyms'] = 1;
1286
  $request_params['doTaxaByCommonNames'] = 0;
1287
  $request_params['matchMode'] = "EXACT";
1288
  $taxon_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_FIND, null, queryString($request_params));
1289
  
1290
  //removing the name where we come from and 
1291
  foreach($taxon_pager->records as $k=>&$taxon){
1292
  	if($taxon->uuid == $taxon_to_hide_uuid){
1293
  		unset($taxon_pager->records[$k]);
1294
  	}
1295
  }
1296
 
1297
  //show the taxa list or go to the singular taxon
1298
  if (sizeof($taxon_pager->records) == 1){ //sigle taxon case
1299
  	reset($taxon_pager->records);
1300
  	$first_key = key($taxon_pager->records);
1301
    if($taxon_pager->records[$first_key]->class != "Taxon"){
1302
        $taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, $taxon_pager->records[$first_key]->uuid);
1303
        drupal_goto('cdm_dataportal/taxon/' . $taxon[0]->uuid);
1304
    }else{
1305
  	   drupal_goto('cdm_dataportal/taxon/' . $taxon_pager->records[$first_key]->uuid);
1306
    }
1307
  }else{ //more than one taxa case
1308
    $taxon_name_page->title = theme('cdm_name_page_title', $taxon_name);
1309
    if($taxon_pager->records){
1310
      $taxon_name_page->content = theme_cdm_list_of_taxa($taxon_pager->records, false);
1311
    }else{
1312
      $taxon_name_page->content = 'This name has no taxa';
1313
    }
1314
    return $taxon_name_page; 
1315
  }
1316
}
1317

    
1318
function cdm_dataportal_view_search_advanced(){
1319

    
1320
	drupal_set_title(t('Advanced search'));
1321

    
1322
	$searchForm = cdm_dataportal_search_taxon_form(true);
1323

    
1324
	return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
1325

    
1326
}
1327

    
1328
/**
1329
 *
1330
 * future extensions to meet palmweb mockup requirements:
1331
 *  - habitat
1332
 *  - uses
1333
 *  - conservation status
1334
 *  - locality / tdwg region
1335
 */
1336
function cdm_dataportal_view_search_taxon(){
1337

    
1338
	$_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
1339

    
1340
	$request_params = cdm_dataportal_search_form_request();
1341
	$taxonPager = cdm_ws_get(CDM_WS_PORTAL_TAXON_FIND, null, queryString($request_params));
1342

    
1343
	$search_params = $_REQUEST;
1344
	unset($search_params['q']);
1345
	return theme('cdm_search_results', $taxonPager, 'cdm_dataportal/search/taxon', $search_params);
1346
}
1347

    
1348

    
1349
function cdm_view_xml2json(){
1350
	$file = arg(2);
1351
	$datastr = get_content(variable_get('cdm_webservice_url', '').$file);
1352
	return  xml2json::transformXmlStringToJson($datastr);
1353
}
1354

    
1355
/* ====================== other functions ====================== */
1356

    
1357
/**
1358
 * Enter description here...
1359
 *
1360
 * @param String $uuid the UUID of the taxon
1361
 * @return the URL
1362
 */
1363
function path_to_taxon($uuid){
1364
	if(!$uuid) return false;
1365
	return 'cdm_dataportal/taxon/'.$uuid;
1366
}
1367

    
1368
function path_to_reference($uuid){
1369
	if(!$uuid) return false;
1370
	return 'cdm_dataportal/reference/'.$uuid;
1371
}
1372

    
1373
function path_to_name($name_uuid){
1374
	$res = false;
1375
	if($name_uuid){
1376
	 $res = 'cdm_dataportal/name/'.$name_uuid;
1377
	}
1378
  return $res;
1379
}
1380

    
1381
function path_to_media($uuid, $representaion_uuid = false, $partId = false){
1382
	if(!$uuid) return false;
1383
	$out = 'cdm_dataportal/media/'.$uuid;
1384
	if($representaion_uuid){
1385
		$out .= '/'.$representaion_uuid;
1386
		if($partId !== false){
1387
			$out .= '/'.$partId;
1388
		}
1389
	}
1390
	return $out;
1391
}
1392

    
1393
/**
1394
 * Compares thisRank with thatRank.
1395
 * Returns a negative integer, zero, or a positive integer
1396
 * as the of thisRank is higher than, equal to, or lower than thatRank.
1397
 * e.g:
1398
 * <ul>
1399
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
1400
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
1401
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
1402
 * </ul>
1403
 * <p>
1404
 * This compare logic of the underlying webservice is the
1405
 * <b>inverse logic</b> of the the one implemented in
1406
 * java.lang.Comparable#compareTo(java.lang.Object)
1407
 * @param $thisRankUuid
1408
 * @param $thatRankUuid
1409
 * @return  a negative integer, zero, or a positive integer
1410
 * as the thisRank is lower than, equal to, or higher than thatRank
1411
 */
1412
function rank_compare($thisRankUuid, $thatRankUuid){
1413
	$result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
1414
	return $result->Integer;
1415
}
1416

    
1417
function uri_to_synonym($synonymUuid, $acceptedUuid, $pagePart = null){
1418
	$acceptedPath = path_to_taxon($acceptedUuid);
1419
	return url($acceptedPath.($pagePart ? '/'.$pagePart : ''), 'highlite='.$synonymUuid.'&acceptedFor='.$synonymUuid);
1420
}
1421

    
1422
/**
1423
 * Creates a short taxonname by using the taggename field of NameSTO or NameTO instances.
1424
 * If the taggename if empty the fullname will be returned.
1425
 *
1426
 * @param unknown_type $Name or TreeNode
1427
 * @return string
1428
 */
1429
function cdm_dataportal_shortname_of($name){
1430

    
1431
	$nameStr = '';
1432
	// get all tagged text tokens of the scientific name
1433
	foreach($name->taggedTitle as $tagtxt){
1434
		if($tagtxt->type == 'name' || $tagtxt->type == 'rank'){
1435
			$nameStr .= ($nameStr ? ' ' : '').$tagtxt->text;
1436
		}
1437
	}
1438
	$nameStr = trim($nameStr);
1439
	if($nameStr){
1440
		// do not return short names for these
1441
		if($nameStr == 'Incertae sedis' || $nameStr ==  'Nomina excludenda'){
1442
			return $nameStr;
1443
		}
1444
		if($pos = stripos($nameStr, ' ')){
1445
			return substr($nameStr, 0, 1).'. '.substr($nameStr, $pos);
1446
		} else {
1447
			return $nameStr;
1448
		}
1449
	} else {
1450
		return $name->titleCache;
1451
	}
1452
}
1453

    
1454
/**
1455
 * TODO Add Comments. I don't get what
1456
 *
1457
 * @param UUID $secUuid
1458
 */
1459
function _cdm_dataportal_set_currentSecUuid($secUuid){
1460

    
1461
	// do not save in session but in database
1462

    
1463
	if(is_array($secUuid)){
1464
		$secUuid = $secUuid[0];
1465
	}
1466
	if(!$secUuid){
1467
		variable_set('cdm_currentSecRef', null);
1468
	} else {
1469
		$secRef = cdm_ws_get(CDM_WS_REFERENCE, $secUuid);
1470
		if(isset($secRef)){
1471
			variable_set('cdm_currentSecRef', (array)($secRef));
1472
		}
1473
	}
1474

    
1475
}
1476

    
1477
/**
1478
 * TODO
1479
 * This will not work with multiple instances of the dataportal running
1480
 * under the same host as there is only one session for all instances.
1481
 * In case you switch instances with the same client, you will definitely
1482
 * run into trouble.
1483
 * Also this is not handling multiple secs.
1484
 *
1485
 *
1486
 * returns the current secRef array.
1487
 * If the according session variable is not jet set the default
1488
 * as configured in the setting is used otherwise null.
1489
 *
1490
 * currentSecRef['uuid']
1491
 * currentSecRef[....
1492
 *
1493
 * @return array
1494
 */
1495
//FIXME function _cdm_dataportal_currentSecRef_array(){
1496
//
1497
//	// do not look in session but in database
1498
//	if( variable_get('cdm_currentSecRef', null) == null){
1499
//		$secUuid = variable_get('cdm_secUuid_default', null);
1500
//		_cdm_dataportal_set_currentSecUuid($secUuid);
1501
//	}
1502
//	return variable_get('cdm_currentSecRef', null);
1503
//
1504
//}
1505

    
1506
/**
1507
 * Check if a taxon is accepted by the current taxonomic tree
1508
 *
1509
 * @param Taxon $taxon
1510
 * @return true if $taxon is accepted, false otherwise
1511
 */
1512

    
1513
function _cdm_dataportal_acceptedByCurrentView($taxon){
1514

    
1515
	$defaultTreeUuid = get_taxonomictree_uuid_selected();
1516
	if($taxon->class == "Taxon" && isset($taxon->taxonNodes)){
1517
		foreach($taxon->taxonNodes as $node){
1518
			if($node->taxonomicTree == $defaultTreeUuid) {
1519
				return true;
1520
			}
1521
		}
1522
	}
1523
	return false;
1524
}
1525

    
1526
/**@Deprecated
1527
 *
1528
 */
1529
function compose_url_prameterstr($parameters = array(), $parentPropertyName = false){
1530
	$pstr = '';
1531
	foreach($parameters as $key=>$value){
1532
		if(is_array($value)){
1533

    
1534
		} else {
1535
			$pstr .= ($pstr ? '&' :'').$key.'='.urlencode($value);
1536
		}
1537
	}
1538
	return $pstr;
1539
}
1540

    
1541
/**
1542
 * workaround for missing original source type, idNamespace is always set in these cases
1543
 * @param unknown_type $source
1544
 * @return unknown_type
1545
 */
1546
function _is_original_source_type($source){
1547
	return !$source->idNamespace && strlen($source->idNamespace) == 0;
1548
}
1549

    
1550
function get_taxonomictree_uuid_selected(){
1551
	if(is_uuid($_SESSION['cdm']['taxonomictree_uuid']) ){
1552
		return $_SESSION['cdm']['taxonomictree_uuid'];
1553
	} else {
1554
		return variable_get('cdm_taxonomictree_uuid', false);
1555
	}
1556
}
1557

    
1558
function switch_to_taxonomictree_uuid($taxonomictree_uuid){
1559
    $_SESSION['cdm']['taxonomictree_uuid'] = $taxonomictree_uuid;
1560
}
1561

    
1562
function reset_taxonomictree_uuid($taxonomictree_uuid){
1563
    unset($_SESSION['cdm']['taxonomictree_uuid']);
1564
}
1565

    
1566

    
1567
function get_nameRenderTemplate($renderPath, $nameLink = NULL, $refenceLink = NULL){
1568
	//TODO implement admin user interface to replace switch statement
1569
	//     preliminar solution: using themes
1570

    
1571
	$template = null;
1572
	// find best matching default RenderTemplate in theme
1573
	//echo "<br/><br/>RENDER PATH:  ".$renderPath;
1574
	//var_dump('RENDER PATH: ' . $renderPath);
1575
	while(!is_array($template) && strlen($renderPath) > 0){
1576
	   $template = theme('get_nameRenderTemplate', $renderPath);
1577
	   $renderPath = substr($renderPath, 0, strrpos($renderPath, '.'));
1578
//	   if(!is_array($template)){
1579
//	       echo "<br/>->".$renderPath;
1580
//	   }
1581
	}
1582
	
1583
	// otherwise get default RenderTemplate from theme 
1584
	if(!is_array($template)){
1585
	   $template = theme('get_nameRenderTemplate', '#DEFAULT');
1586
	}
1587
	// otherwise get system default RenderTemplate
1588
	if(!is_array($template)){
1589
		switch($renderPath){
1590
			case 'list_of_taxa':
1591
			case  'acceptedFor':
1592
			case 'taxon_page_synonymy':
1593
			case 'typedesignations':
1594
			case 'taxon_page_title':
1595
			case 'na': $template = array(
1596
			        'namePart' => array('#uri'=>true),
1597
			        //'authorshipPart' => true,
1598
				);
1599
				break;
1600
			case 'nar': $template = array(
1601
		          	'namePart' => array('#uri'=>true),
1602
					 //'authorshipPart' => true,
1603
		          	'referencePart' => array('#uri'=>true),
1604
		          	'microreferencePart' => true,
1605
				);
1606
				break;
1607
			default: $template = array(
1608
          'namePart' => array('#uri'=>true),
1609
          'authorshipPart' => true,
1610
          'referencePart' => array('#uri'=>true),
1611
          'microreferencePart' => true,
1612
          'statusPart' => true,
1613
          'descriptionPart' => true
1614
			);
1615
		}
1616
	}
1617

    
1618
	if($nameLink && isset($template['nameAuthorPart']['#uri'])){
1619
		$template['nameAuthorPart']['#uri'] = $nameLink;
1620
	} else{
1621
		unset($template['nameAuthorPart']['#uri']);
1622
	}
1623

    
1624
	if($nameLink && isset($template['namePart']['#uri'])){
1625
		$template['namePart']['#uri'] = $nameLink;
1626
	} else{
1627
		unset($template['namePart']['#uri']);
1628
	}
1629

    
1630
	if($refenceLink && isset($template['referencePart']['#uri'])){
1631
		$template['referencePart']['#uri'] = $refenceLink;
1632
	}else{
1633
		unset($template['referencePart']['#uri']);
1634
	}
1635
	
1636
	return $template;
1637
}
1638

    
1639
function get_partDefinition($taxonNameType){
1640
	//TODO implement admin user interface to allow specify the partdefinitions for any type
1641
	//     preliminar solution: using themes
1642

    
1643
	$partdef = theme('get_partDefinition', $taxonNameType);
1644

    
1645
	if(!is_array($partdef)){
1646
		switch($taxonNameType){
1647
			case 'ZoologicalName': $partdef = array(
1648
        'namePart' => array(
1649
          'name' => true,
1650
			),
1651
        'referencePart' => array(
1652
          'authorTeam' => true
1653
			),
1654
        'microreferencePart' => array(
1655
          'microreference' => true,
1656
			),
1657
        'statusPart' => array(
1658
          'status' => true,
1659
			),
1660
        'descriptionPart' => array(
1661
          'description' => true,
1662
			),
1663
			);
1664
			break;
1665
			case 'BotanicalName': $partdef = array(
1666
        'namePart' => array(
1667
          'name' => true
1668
			),
1669
        'authorTeamPart' => array(
1670
          'authorTeam' => true,   
1671
			),
1672
        'referencePart' => array(
1673
          'reference' => true      
1674
			),
1675
        'microreferencePart' => array(
1676
          'microreference' => true,
1677
			),
1678
        'statusPart' => array(
1679
          'status' => true,
1680
			),
1681
        'descriptionPart' => array(
1682
          'description' => true,
1683
			),
1684
			);
1685
			break;
1686
			default: $partdef = array(
1687
        'namePart' => array(
1688
          'name' => true,
1689
			),
1690
        'authorTeamPart' => array(
1691
          'authorTeam' => true,   
1692
			),
1693
        'referencePart' => array(
1694
          'reference' => true      
1695
			),
1696
        'microreferencePart' => array(
1697
          'microreference' => true,
1698
			),
1699
        'statusPart' => array(
1700
          'status' => true,
1701
			),
1702
        'descriptionPart' => array(
1703
          'description' => true,
1704
			),
1705
			);
1706
		}
1707
	}
1708
	return $partdef;
1709
}
1710

    
1711
/**
1712
 * The function read the metadata info such title or artist of a media file. The
1713
 * function tries at first to get all the info from the file metadata and if it is
1714
 * not avaible look at the media file info stored at the database.
1715
 * @param $media The media file
1716
 * @return array The array with the avilable specified metadata info.
1717
 * TODO rename to read_media_metadata() and move to *.module
1718
 */
1719
function cdm_read_media_metadata($media){
1720

    
1721
	$metadata_caption = array('title' => '',           //media_metadata and media
1722
                              'artist' => '',          //media_metadata and media
1723
                              'rights',                //media_metadata and media
1724
                              'location',              //media_metadata
1725
                              'filename' => '',        //media
1726
                              'mediacreated' => '',    //media
1727
                              'description' => '');    //media
1728

    
1729
	//getting the media metadata
1730
	$media_metadata = cdm_ws_get(CDM_WS_MEDIA_METADATA, array($media->uuid));
1731

    
1732
	//title
1733
	//if ($media_metadata->Headline) {
1734
	if ($media_metadata->ObjectName) {
1735
		$metadata_caption['title'] = $media_metadata->ObjectName;
1736
		//	} else if ($media->titleCache){
1737
		//		$metadata_caption['title'] = $media_metadata->ObjectName;
1738
	} else if ($media->titleCache){
1739
		$metadata_caption['title'] = $media->titleCache;
1740
		if ($media->description_L10n)
1741
		$metadata_caption['title'] .= ' - ' . $media->description_L10n;
1742
	}else{ //if there is no title on the db and metadata get file title
1743
    $aux = $media->representations[0]->parts[0]->uri;
1744
    $aux = explode("/", $aux);
1745
    $aux = end($aux);
1746
		$metadata_caption['title'] = $aux;		
1747
	}
1748

    
1749
	//artist
1750
	if ($media_metadata->Artist){
1751
	 $metadata_caption['artist'] = ($media_metadata->Artist ? ''.$media_metadata->Artist : '');
1752
	}
1753
	elseif ($media->artist->titleCache){
1754
	 $metadata_caption['artist'] = $media->artist->titleCache;
1755
	}
1756
	
1757
	//copyright
1758
	$metadata_caption['rights'] = array('copyright' => array('agentNames' => array()),
1759
                                      'license' => array('agentNames' => array(), 'types' => array(), 'abbreviatedTexts' => array(), 'uris' => array()));
1760
	if ($media_metadata->Copyright)
1761
	$metadata_caption['rights']['copyright']['agentNames'][] = $media_metadata->Copyright;
1762
	elseif ($media->rights){
1763
		foreach($media->rights as $right){
1764
			switch($right->term->uuid){
1765
				case UUID_RIGHTS_LICENCE:
1766
					$metadata_caption['rights']['license']['agentNames'][] = ($right->agent ? ''.$right->agent->firstname.' '.$right->agent->lastname : '');
1767
					$metadata_caption['rights']['license']['types'][] = ($right->representation_L10n ? ''.$right->representation_L10n : '');
1768
					$metadata_caption['rights']['license']['abbreviatedTexts'][] = ($right->abbreviatedText ? ''.$right->abbreviatedText : '');
1769
					$metadata_caption['rights']['license']['uris'][] = ($right->uri ? ''.$right->uri : '');
1770
					break;
1771
				case UUID_RIGHTS_COPYRIGHT:
1772
					$metadata_caption['rights']['copyright']['agentNames'][] = $right->agent->firstname . ' ' . $right->agent->lastname;
1773
					break;
1774
			}
1775
		}
1776
	}
1777
	else
1778
	$metadata_caption['rights']['agentNames'][] = '';
1779

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

    
1783
	//location
1784
	$metadata_caption['location'] = array();
1785
	$metadata_caption['location']['sublocation'] = $media_metadata->Sublocation;
1786
	$metadata_caption['location']['city'] = $media_metadata->City;
1787
	$metadata_caption['location']['province'] = $media_metadata->Province;
1788
	$metadata_caption['location']['country'] = $media_metadata->Country;
1789

    
1790
	//filename
1791
	if(isset($media->representations[0]->parts[0]->uri)){
1792
		$fileUri = $media->representations[0]->parts[0]->uri;
1793
		$filename = substr($fileUri, strrpos($fileUri, "/")+1);
1794
		$metadata_caption['filename'] = $filename;
1795
	}
1796
	else{
1797
	 $metadata_caption['filename'] = '';
1798
	}
1799
	/*
1800
	 //creation date
1801
	 if($media_metadata["Modify Date"])
1802
	 $metadata_caption['mediacreated'] = $media_metadata["Modify Date"];
1803
	 else
1804
	 $metadata_caption['mediacreated'] = $media->created;
1805
	 */
1806
	//returned value
1807
	return $metadata_caption;
1808
}
1809

    
1810
/**
1811
 * This function collects all the media from a list of description elements
1812
 * and return them as an Array.
1813
 *
1814
 * @param $descriptionElementes The description elements
1815
 * @return Array The output with all the media
1816
 */
1817
function cdm_dataportal_media_from_descriptionElements($descriptionElements){
1818
	//variables
1819
	$outArrayOfMedia = array(); //return value
1820
	//implementation
1821
	foreach($descriptionElements as $descriptionElement){
1822
		foreach($descriptionElement->media as $media){
1823
			if(isset($media)){
1824
			 $outArrayOfMedia[] = $media;
1825
			}
1826
	 }
1827
	}
1828
	return $outArrayOfMedia;
1829
}
1830

    
1831
function cdm_annotations_as_footnotekeys($cdmBase){
1832
    
1833
	$footNoteKeys = array();
1834
    $footnoteListKey = RenderHints::getFootnoteListKey() . '-annotations';
1835
    $annotationUrl = cdm_compose_annotations_url($cdmBase);
1836
    if($annotationUrl){
1837
        $annotationPager = cdm_ws_get($annotationUrl, null, null, null, true);
1838
        if($annotationPager->count > 0){
1839
            foreach($annotationPager->records as $annotation){
1840
                $footNoteKeys[] = FootnoteManager::addNewFootnote($footnoteListKey, $annotation->text);
1841
            }   
1842
        }
1843
    }
1844
    return $footNoteKeys;
1845
}
1846

    
1847

    
1848

    
(6-6/10)