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/renderhints.php');
30

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

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

    
52

    
53

    
54

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

    
60

    
61

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

    
66

    
67
/* ====================== hook implementations ====================== */
68

    
69
/**
70
 * Implementation of hook_help()
71
 *
72
 * Display help and module information
73
 * @param section which section of the site we're displaying help
74
 * @return help text for section
75
 */
76
function cdm_dataportal_help($section='') {
77

    
78
	$out = '';
79
	switch ($section) {
80
		case "admin/modules#description":
81
			$out = t("The dataportal publishes CDM data hosted in a CommunityStore on the web.");
82
			break;
83
	}
84
	return $out;
85
}
86

    
87

    
88
/**
89
 * Implementation of hook_perm()
90
 *
91
 * Valid permissions for this module
92
 * @return array An array of valid permissions for the portfolio module
93
 */
94
function cdm_dataportal_perm() {
95
	return array(
96
    	'administer cdm_dataportal',
97
      'cdm_dataportal view notes',
98
	//TODO which else permission are required? -> check the WP6 requirements document
99
	);
100
}
101

    
102

    
103
/**
104
 * Implementation of hook_menu()
105
 */
106
function cdm_dataportal_menu($may_cache) {
107
	$items = array();
108

    
109
	if ($may_cache) {
110

    
111
		$items[] = array(
112
      'path' => 'admin/settings/cdm_dataportal',
113
      'title' => t('CDM Dataportal'),
114
      'description' => t('Setting for the CDM DataPortal'),
115
      'access' => user_access('administer cdm_dataportal'),
116
      'callback' => 'drupal_get_form',
117
      'callback arguments' => 'cdm_dataportal_settings',
118
      'type' => MENU_NORMAL_ITEM,
119
		);
120

    
121
		$items[] = array(
122
      'path' => 'admin/settings/cdm_dataportal/general',
123
      'title' => t('General'),
124
      'description' => t('Setting for the CDM DataPortal'),
125
      'access' => user_access('administer cdm_dataportal'),
126
      'callback' => 'drupal_get_form',
127
      'callback arguments' => 'cdm_dataportal_settings',
128
	  'weight' => 0,
129
      'type' => MENU_LOCAL_TASK,
130
		);
131

    
132
		$items[] = array(
133
      'path' => 'admin/settings/cdm_dataportal/geo',
134
      'title' => t('Geo & Map'),
135
      'description' => t('Geo & Map'),
136
      'access' => user_access('administer cdm_dataportal'),
137
      'callback' => 'drupal_get_form',
138
      'callback arguments' => 'cdm_dataportal_settings_geo',
139
	  'weight' => 1,
140
      'type' => MENU_LOCAL_TASK,
141
		);
142

    
143
		$items[] = array(
144
      'path' => 'admin/settings/cdm_dataportal/layout',
145
      'title' => t('Layout'),
146
      'description' => t('Configure and adjust the layout of your DataPortal '),
147
      'access' => user_access('administer cdm_dataportal'),
148
      'callback' => 'drupal_get_form',
149
      'callback arguments' => 'cdm_dataportal_settings_layout',
150
      'weight' => 2,
151
      'type' => MENU_LOCAL_TASK,
152
		);
153

    
154
		$items[] = array(
155
      'path' => 'admin/settings/cdm_dataportal/cachesite',
156
      'title' => t('Cache'),
157
      'description' => t('Cache the whole CDM DataPortal site'),
158
      'access' => user_access('administer cdm_dataportal'),
159
      'callback' => 'cdm_dataportal_view_cache_site',
160
      'weight' => 10,
161
      'type' => MENU_LOCAL_TASK,
162
		);
163

    
164
		/*$items[] = array(
165
		 'path' => 'admin/settings/cdm_dataportal/ws',
166
		 'title' => t('Web Service'),
167
		 'description' => t('Setting for the CDM DataPortal'),
168
		 'access' => user_access('administer cdm_dataportal'),
169
		 'callback' => 'drupal_get_form',
170
		 'callback arguments' => 'cdm_dataportal_settings',
171
		 'weight' => 1,
172
		 'type' => MENU_DEFAULT_LOCAL_TASK,
173
		 );*/
174

    
175
		$items[] = array(
176
	    'path' => 'cdm_dataportal/names',
177
	    'callback' => 'cdm_dataportal_view_names',
178
	    'access' => true,
179
	    'type' => MENU_CALLBACK,
180
		);
181
		// optional callback arguments: page
182
			
183
		$items[] = array(
184
      'path' => 'cdm_dataportal/taxon',
185
      'callback' => 'cdm_dataportal_taxon_page_view',
186
      'access' => true,
187
      'type' => MENU_CALLBACK,
188
		// expected callback arguments: uuid
189
		);
190
		
191
		$items[] = array(
192
      'path' => 'cdm_dataportal/name',
193
      'callback' => 'cdm_dataportal_name_page_view',
194
      'access' => true,
195
      'type' => MENU_CALLBACK,
196
    // expected callback arguments: uuid
197
    );
198

    
199
		$items[] = array(
200
      'path' => 'cdm_dataportal/reference',
201
      'callback' => 'cdm_dataportal_view_reference',
202
      'access' => true,
203
      'type' => MENU_CALLBACK,
204
		// expected callback arguments: uuid
205
		);
206

    
207
		$items[] = array(
208
      'path' => 'cdm_dataportal/reference/list',
209
      'callback' => 'cdm_dataportal_view_reference_list',
210
      'access' => true,
211
      'type' => MENU_CALLBACK,
212
		// expected callback arguments: uuid
213
		);
214

    
215
		$items[] = array(
216
      'path' => 'cdm_dataportal/media',
217
      'callback' => 'cdm_dataportal_view_media',
218
      'access' => true,
219
      'type' => MENU_CALLBACK,
220
		// expected callback arguments: uuid, mediarepresentation_uuid, part_uuid or part#
221
		);
222

    
223
		$items[] = array(
224
      'path' => 'cdm_dataportal/search',
225
      'callback' => 'cdm_dataportal_view_search_advanced',
226
      'access' => true,
227
      'type' => MENU_CALLBACK,
228
		);
229

    
230
		$items[] = array(
231
      'path' => 'cdm_dataportal/search/taxon',
232
      'callback' => 'cdm_dataportal_view_search_taxon',
233
      'access' => true,
234
      'type' => MENU_CALLBACK,
235
		);
236
		
237
		$items[] = array(
238
      'path' => 'cdm/xml2json',
239
      'callback' => 'cdm_view_xml2json',
240
      'access' => true,
241
      'type' => MENU_CALLBACK,
242
		);
243

    
244
	} else {
245
		// may not cache
246
		// --- local tasks for Taxon
247
		$items[] = array(
248
      'path' => 'cdm_dataportal/name/'.arg(2),
249
      //'callback' => 'cdm_dataportal_view_name',
250
		  'callback' => 'cdm_dataportal_name_page_view',
251
      'callback arguments' => array(arg(2)),
252
      'access' => true,
253
      'type' => MENU_CALLBACK,
254
    );
255
    
256
		if(variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
257

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

    
269
			$items[] = array(
270
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/all',
271
      'title' => theme('cdm_taxonpage_tab', 'General'),
272
      'callback' => 'cdm_dataportal_taxon_page_view',
273
      'access' => true,
274
      'type' => MENU_CALLBACK,
275
      'weight' => 2,
276
      'callback arguments' => array(arg(2), "all")
277
			// expected callback arguments: name_uuid
278
			);
279

    
280
			$items[] = array(
281
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/description',
282
      'title' => theme('cdm_taxonpage_tab', 'General'),
283
      'callback' => 'cdm_dataportal_taxon_page_view',
284
      'access' => true,
285
      'type' => MENU_DEFAULT_LOCAL_TASK,
286
      'weight' => 2,
287
      'callback arguments' => array(arg(2), "description")
288
			// expected callback arguments: name_uuid
289
			);
290

    
291
			$items[] = array(
292
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/synonymy',
293
      'title' => theme('cdm_taxonpage_tab', 'Synonymy'),
294
      'callback' => 'cdm_dataportal_taxon_page_view',
295
      'access' => true,
296
      'type' => MENU_LOCAL_TASK,
297
      'weight' => 3,
298
      'callback arguments' => array(arg(2), "synonymy")
299
			// expected callback arguments: name_uuid
300
			);
301

    
302
			$items[] = array(
303
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/images',
304
      'title' => theme('cdm_taxonpage_tab', 'Images'),
305
      'callback' => 'cdm_dataportal_taxon_page_view',
306
      'access' => true,
307
      'type' => MENU_LOCAL_TASK,
308
      'weight' => 4,
309
      'callback arguments' => array(arg(2), "images")
310
			// expected callback arguments: name_uuid
311
			);
312
		}
313
	}
314

    
315
	drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal.css');
316
	//drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
317
	drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_screen.css', 'screen');
318

    
319
	return $items;
320

    
321
}
322

    
323
/**
324
 * Implementation of hook_block()
325
 *
326
 * Provides the following blocks:
327
 *  0: list of links useful during development
328
 *
329
 * @param String $op
330
 * @param int $delta
331
 */
332
function cdm_dataportal_block($op='list', $delta=0) {
333
	// listing of blocks, such as on the admin/block page
334
	if ($op == "list") {
335
		//$block[0]["info"] = t("CDM DataPortal DevLinks");
336
//		$block[1]["info"] = t("CDM DataPortal Credits");
337
		$block[2]["info"] = t("CDM Search Taxa");
338
		//$block[3]["info"] = t("CDM Filters");
339
		$block[4]["info"] = t("CDM Dataportal Print");
340
		return $block;
341
	}
342
	else if ($op == 'view') {
343
		switch($delta){
344
//			case 1:
345
//				$block['subject'] = t('Credits');
346
//				$block['content'] = theme('cdm_credits');
347
//				return $block;
348
			case 2:
349
				$block['subject'] = t('Search Taxa');
350
				$block['content'] = drupal_get_form('cdm_dataportal_search_taxon_form');
351
				$block['content'] .= '<div>'.l('Advanced Search', '/cdm_dataportal/search').'</div>';
352
				return $block;
353
			case 4:
354
				$block['subject'] = t('Print this page');
355
				$block['content'] = theme('cdm_print_button');;
356
				return $block;
357
		}
358
	}
359
}
360

    
361

    
362
/**
363
 * Implementation of hook_validate()
364
 *
365
 * @param $element
366
 */
367
function cdm_dataportal_settings_validate($form_id, $form_values){
368

    
369
	if (!str_endsWith($form_values['cdm_webservice_url'], '/')) {
370
		//form_set_error('cdm_webservice_url', t("The URL to the CDM Web Service must end with a slash: '/'."));
371
		$form_values['cdm_webservice_url'] .= '/';
372
	}
373

    
374
//	$cdm_webservice_url_changed = variable_get('cdm_webservice_url', '') != $form_values['cdm_webservice_url'];
375

    
376
//	if ($cdm_webservice_url_changed) {
377
//
378
//		//FIXME remove _cdm_dataportal_set_currentSecUuid($form_values['cdm_secUuid_default']);
379
//		// reset all cdm related data stored in the session
380
//		// cdm_dataportal_session_clear($form_values['cdm_webservice_url']); // cdm_webservice_url is not further stored here
381
//		// clear the cdm webservice cache
382
//		cache_clear_all(NULL, 'cache_cdm_ws');
383
//		// better clear secref_cache since i can not be sure if the cache has not be used during this response
384
//		cdm_api_secref_cache_clear();
385
//	}
386
	if($form_values['cdm_webservice_cache'] != variable_get('cdm_webservice_cache', 1)){
387
		cache_clear_all(NULL, 'cache_cdm_ws');
388
		// better clear secref_cache since i can not be sure if the cache has not be used during this response
389
		cdm_api_secref_cache_clear();
390
	}
391

    
392
}
393

    
394
/*
395
 function cdm_dataportal_session_clear($cdm_ws_uri_update = false){
396
 $_SESSION['cdm'] = null;
397
 if(is_string($cdm_ws_uri_update)){
398
 $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
399
 }
400
 }
401

    
402
 function cdm_dataportal_session_validate(){
403

    
404
 if(!isset($_SESSION['cdm']['ws_uri'])){
405
 $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', false));
406
 } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', false)){
407
 cdm_dataportal_session_clear(variable_get('cdm_webservice_url', false));
408
 }
409
 }
410
 */
411

    
412
function cdm_dataportal_search_taxon_form($advancedForm = false){
413

    
414
	$preset_query = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
415
	$preset_doTaxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
416
	$preset_doSynonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
417
	$preset_doTaxaByCommonNames = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
418
	$tdwg_level_select =  (isset($_SESSION['cdm']['search']['tdwg_level_select']) ? $_SESSION['cdm']['search']['tdwg_level_select'] : 2);
419
	$selected_areas =  (isset($_SESSION['cdm']['search']['area']) ? $_SESSION['cdm']['search']['area'] : false);
420

    
421
	$url = 'cdm_dataportal/search/taxon';
422
	$form['#method'] = 'get';
423
	$form['#process'] = array('cdm_dataportal_search_process' => array());
424
	$form['#action'] = url($url, NULL, NULL, true);
425

    
426
	$form['query'] = array(
427
    '#delta' => 0,
428
    '#type' => 'textfield',
429
    '#size' => 20,
430
    '#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')),
431
    '#value' => $preset_query,
432
	);
433

    
434
	$form['search'] = array(
435
      '#delta' => 1,
436
      '#tree' => true,
437
	//'#type' => $advancedForm ? 'fieldset': 'hidden',
438
      '#title' => t('Options')
439
	);
440

    
441
	$form['search']['tree'] = array(
442
    '#delta' => -1,
443
    '#type' => 'hidden',
444
    '#value' => variable_get('cdm_taxonomictree_uuid', false)
445
	);
446

    
447

    
448
	// clean URL get forms breaks if we don't give it a 'q'.
449
	if (!(bool)variable_get('clean_url', '0')) {
450
		$form['search']['q'] = array(
451
      '#delta' => -1,
452
      '#type' => 'hidden',
453
      '#value' => $url,
454
      '#name' => 'q',
455
		);
456
	}
457

    
458
	$form['search']['pageSize'] = array(
459
      '#delta' => -1,
460
      '#type' => 'hidden',
461
      '#value' => variable_get('cdm_dataportal_search_items_on_page', 25)
462
	);
463

    
464
	if($advancedForm){
465
		// general search parameters
466
		$form['search']['doTaxa'] = array(
467
      '#delta' => 2,
468
      '#type' => 'checkbox',
469
      '#title' => t('Search for accepted taxa'),
470
      '#value' => $preset_doTaxa
471
		);
472
		$form['search']['doSynonyms'] = array(
473
      '#delta' => 3,
474
      '#type' => 'checkbox',
475
      '#title' => t('Search for synonyms'),
476
      '#value' => $preset_doSynonyms
477
		);
478
		$form['search']['doTaxaByCommonNames'] = array(
479
      '#delta' => 4,
480
      '#type' => 'checkbox',
481
      '#title' => t('Search for common names'),
482
      '#value' => $preset_doTaxaByCommonNames
483
		);
484
			
485
		// Geographic Range
486
		$form['search']['geographic_range'] = array(
487
      '#type' => 'fieldset',
488
      '#delta' => 5,
489
      '#tree' => true,
490
      '#title' => t('Geographic range'),
491
		);
492

    
493
		$form['search']['geographic_range']['tdwg_level_select'] = array(
494
    '#type' => 'radios',
495
    '#title' => t('Select a TDWG distribution level and code'),
496
    '#default_value' => $tdwg_level_select,
497
    '#options' => array(
498
		t('TDWG level-1, i.e. a continent'),
499
		t('TDWG level-2'),
500
		t('TDWG level-3, i.e. a country'),
501
		t('TDWG level-4')
502
		)
503
		);
504
		$tdwg[1] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '1');
505
		$tdwg[2] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '2');
506
		$tdwg[3] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '3');
507
		$tdwg[4] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '4');
508

    
509
		$tdwg_js = '';
510
		foreach($tdwg as $key=>$tdwg_level){
511
			$tdwgOptions = array();
512
			$tdwgOptionsSelected = array();
513
			foreach($tdwg_level as $area){
514
				$representation = $area->representations[0];
515
				$tdwgOptions[$representation->abbreviatedLabel] = $area->representation_L10n;
516
				if(is_array($selected_areas) && in_array($representation->abbreviatedLabel, $selected_areas)){
517
					$tdwgOptionsSelected[] = $representation->abbreviatedLabel; //$area->uuid;
518
				}
519
			}
520
			asort($tdwgOptions);
521
			$form['search']['geographic_range']['tdwg_level_'.$key] = array(
522
        '#type' => 'select',
523
        '#title'         => t('TDWG level').' '.$key,
524
        '#default_value' => $tdwgOptionsSelected,
525
        '#multiple' => TRUE, 
526
        '#options' => $tdwgOptions
527
			);
528
			$tdwg_js .= "$('#edit-search-geographic-range-tdwg-level-$key').parent()".($tdwg_level_select + 1 == $key ?  '.show()' : '.hide()'). ";\n";
529
		}
530

    
531
		drupal_add_js(
532
    "$(document).ready(function(){
533
    
534
      $(\"input[@name='search[geographic_range][tdwg_level_select]']\").change( 
535
        function(event){
536
          var selectId = $(\"input[@name='search[geographic_range][tdwg_level_select]']:checked\").val();
537
          var i;
538
          for(i = 0; i < 4; i++){
539
            if(selectId == i){
540
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1) ).parent().fadeIn('slow');
541
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
542
            } else {
543
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).parent().fadeOut('slow');
544
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
545
            }
546
          }
547
        }
548
      );
549
      
550
      $tdwg_js
551
    });",
552
    'inline');
553

    
554
	} else {
555
		$form['search']['doTaxa'] = array(
556
      '#delta' => -2,
557
      '#type' => 'hidden',
558
      '#value' => 1
559
		);
560
		$form['search']['doSynonyms'] = array(
561
      '#delta' => -3,
562
      '#type' => 'hidden',
563
      '#value' => 1
564
		);
565
		$form['search']['doTaxaByCommonNames'] = array(
566
      '#delta' => -4,
567
      '#type' => 'hidden',
568
      '#value' => 1
569
		);
570
	}
571

    
572
	$form['submit'] = array(
573
  '#delta' => 9,
574
  '#type' => 'submit',
575
  '#name' => '',
576
  '#value' => t('Search')
577
	);
578

    
579
	return $form;
580
}
581

    
582
function cdm_dataportal_search_taxon_form_advanced(){
583
	return cdm_dataportal_search_taxon_form(true);
584
}
585

    
586
/**
587
 * Implementation #process method call, see form_builder()
588
 * <p>
589
 * Removes Drupal internal form elements from query
590
 * @param $form
591
 * @return unknown_type
592
 */
593
function cdm_dataportal_search_process($form) {
594
	unset($form['form_id']);
595
	unset($form['form_token']);
596
	return $form;
597
}
598

    
599
/**
600
 * Filters $_REQUEST by a list of valid request  parameters and also sets defaults if required.
601
 * returns the processed request parameters submitted by the search form.
602
 */
603
function cdm_dataportal_search_form_request(){
604

    
605

    
606
	$form_params = array();
607
	array_deep_copy($_REQUEST['search'], $form_params);
608
	$form_params['query'] =  trim($_REQUEST['query']);
609

    
610
	// split of  geographic range
611
	if(isset($_REQUEST['search']['geographic_range'])){
612
		$geographicRange = $_REQUEST['search']['geographic_range'];
613
		// remove
614
		unset($form_params['geographic_range']);
615
	}
616

    
617
	// add geographic range
618
	if($geographicRange){
619
		$form_params['tdwg_level_select'] = $geographicRange['tdwg_level_select'];
620
		for($i = 1; $i < 5; $i++){
621
			if(isset($geographicRange['tdwg_level_'.$i])){
622
				$form_params['area'] = $geographicRange['tdwg_level_'.$i];
623
			}
624
		}
625
	}
626

    
627
	// store in session
628
	$_SESSION['cdm']['search'] = $form_params;
629

    
630
	return $form_params;
631
}
632

    
633

    
634
/* UNREACHABLE since action of form directly links to view
635
 function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
636

    
637
 $_SESSION['cdm']['search'] = $form_values;
638
 //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
639
 return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
640
 //$paramstr = compose_url_prameterstr($form_values);
641
 //return url('/cdm_dataportal/search/taxon/', $paramstr);
642
 }
643
 */
644
/* ====================== menu callback functions ====================== */
645

    
646
/**
647
 * Generate main administration form.
648
 *
649
 * @return
650
 *   An array containing form items to place on the module settings page.
651
 */
652
function cdm_dataportal_settings(){
653

    
654
	$form = cdm_api_settings_form();
655

    
656
	//TODO: settings are still incomplete, compare with trunk/dataportal/inc/config_default.php.inc
657
	$form['cdm_dataportal'] = array(
658
      '#type' => 'fieldset',
659
      '#title' => t('CDM DataPortal'),
660
      '#collapsible' => FALSE,
661
      '#collapsed' => TRUE,
662
	);
663

    
664
	//  $form['cdm_dataportal']['secuuid_widget'] = array(
665
	//    '#type' => 'select_secuuid', // => will be themed by theme_select_secuuid(...)
666
	//    '#title' => t('Default Concept References'),
667
	//    '#description'   => t('The default \'sensu\' reference to start the CDM DataPortal with. Per user choices are possible and may override this value.'),
668
	//    '#varname' => 'cdm_secUuid_default',
669
	//    '#multiple' => false
670
	//  );
671

    
672
	$taxonTrees = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY);
673
	foreach($taxonTrees as $tree){
674
		$taxonomicTreeOptions[$tree->uuid] = $tree->titleCache;
675
	}
676
	$form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
677
    '#type' => 'select',
678
    '#title'         => t('Available taxonomic views'),
679
    '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
680
    '#options' => $taxonomicTreeOptions,
681
    '#description'   => t('Select a taxonomic view for this webservice.')
682
	);
683

    
684
	//  $form['cdm_dataportal']['DEBUG']= array(
685
	//    '#value' => '<pre>'.print_r($taxonTrees, true).'</pre>'
686
	//  );
687

    
688

    
689
	//------------------ FEATURE TREE --------------------//
690

    
691
	$form['cdm_dataportal']['feature_tree'] = array(
692
    '#type' => 'fieldset',
693
    '#title' => t('Feature Tree'),
694
    '#collapsible' => TRUE,
695
    '#collapsed' => TRUE,
696
	);
697

    
698
	$form['cdm_dataportal']['feature_tree']['cdm_dataportal_featuretree_uuid'] = array(
699
    '#type' => 'select',
700
    '#title'         => t('Available FeatureTrees'),
701
    '#default_value' => variable_get('cdm_dataportal_featuretree_uuid', UUID_DEFAULT_FEATURETREE),
702
	  '#options' => _get_feature_trees(),
703
    '#description'   => t('Select a FeatureTree for this webservice.'
704
    //.' If there is no applicable FeatureTree you can create a new one using the <a href="">FeatureTreeManager</a>'
705
	)
706
	);
707

    
708
	return system_settings_form($form);
709
}
710

    
711
function cdm_dataportal_form_alter($form_id, &$form) {
712
    
713
static $comment_node_disabled =  0;
714
static $comment_node_read_only =  1;
715
static $comment_node_read_write =  2;
716
   
717
    
718
  if ($form_id == 'node_type_form' 
719
    && isset($form['identity']['type']) 
720
    && array_key_exists($form['#node_type']->type, cdm_dataportal_get_nodetypes()) 
721
    ) {
722
    $form['workflow']['comment'] = array(
723
      '#type' => 'radios',
724
      '#title' => t('Default comment setting'),
725
      '#default_value' => variable_get('comment_'. $form['#node_type']->type, $comment_node_disabled),
726
      '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
727
      '#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
728
    );
729
  }
730
}
731

    
732
/**
733
 * LAYOUT settings
734
 * @return unknown_type
735
 */
736
function cdm_dataportal_settings_layout(){
737

    
738
	$form = array();
739

    
740

    
741
	$form['cdm_taxonname_type'] = array(
742
    '#type' => 'select',
743
    '#title'         => t('Taxon name type'),
744
    '#default_value' => variable_get('cdm_taxonname_type', 'BotanicalName'),
745
    '#options' => array( 'BotanicalName'=>t('BotanicalName'), 'ZoologicalName'=>t('ZoologicalName')),
746
    '#description'   => t('')
747
	);
748

    
749
	$form['cdm_dataportal_nomref_in_title'] = array(
750
    '#type' => 'checkbox',
751
    '#title' => t('Show full nomenclatural reference in title'),
752
    '#default_value' => variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE),
753
    '#description' => t('Uncheck this, if you want the title of a taxon page consisting of name and author plus publication year only.
754
                        If not checked, the first homotypic taxon is a repetition of the accepted taxon with
755
                        the full nomenclatural reference.')
756
	);
757

    
758
	$form[CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS] = array(
759
    '#type' => 'checkbox',
760
    '#title' => t('Show name relations of accepted taxa on taxon page'),
761
    '#default_value' => variable_get(CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_NAME_RELATIONSHIPS_DEFAULT),
762
    '#description' => t('')
763
	);
764
	
765
	$form[CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS] = array(
766
    '#type' => 'checkbox',
767
    '#title' => t('Show taxon relations of accepted taxa on taxon page'),
768
    '#default_value' => variable_get(CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT),
769
    '#description' => t('')
770
    );
771

    
772
	$form['cdm_dataportal_display_is_accepted_for'] = array(
773
    '#type' => 'checkbox',
774
    '#title' => t('Display <em>is accepted for ...</em> on taxon pages when coming from a synonym link.'),
775
    '#default_value' => variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR),
776
    '#description' => t('')
777
	);
778

    
779
	$form['cdm_dataportal_name_relations_skiptype_basionym'] = array(
780
    '#type' => 'checkbox',
781
    '#title' => t('Exclude the basionym relationship type from the taxon page'),
782
    '#default_value' => variable_get('cdm_dataportal_name_relations_skiptype_basionym', 1),
783
    '#description' => t('')
784
	);
785

    
786
	$form['cdm_dataportal_taxonpage_tabs'] = array(
787
    '#type' => 'checkbox',
788
    '#title' => t('Tabbed taxon page'),
789
    '#default_value' => variable_get('cdm_dataportal_taxonpage_tabs', 1),
790
    '#description' => t('Split the taxon page into individual tabs for description, images, synonymy')
791
	);
792

    
793
	$form['cdm_dataportal_search_items_on_page'] = array(
794
    '#type' => 'textfield',
795
    '#title' => t('Search Page Size'),
796
    '#default_value' => variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE),
797
    '#description' => t('Number of Names to display per page in search results.')
798
	);
799

    
800
	$form['cdm_dataportal_descriptions_separated'] = array(
801
    '#type' => 'checkbox',
802
    '#title' => t('Separate Descriptions'),
803
    '#default_value' => variable_get('cdm_dataportal_descriptions_separated', 0),
804
    '#description' => t('By default corresponding elements of different descriptions are joined together'
805
    .' into a common section per feature (i.e. type of description).'
806
    .' Check this box to allow displaying all descriptions separately.')
807
    );
808

    
809
    //---- IMAGES ----//
810
    $form['images'] = array(
811
    '#type' => 'fieldset',
812
    '#title' => t('Images'),
813
    '#collapsible' => TRUE,
814
    '#collapsed' => FALSE,
815
    );
816
    $options = cdm_rankVocabulary_as_option();
817
    array_unshift($options, '-- DISABLED --');
818
    $form['images']['image_hide_rank'] =  array(
819
    '#type'          => 'select',
820
    '#title'         => t('Hide Images for Taxa above'),
821
    '#default_value' => variable_get('image_hide_rank', '0'),
822
    '#options'       => $options,
823
    '#description'   => t(''),
824
    );
825
    //show media
826
    $selectShowMedia = array(0 => "Show only taxon media",
827
    1 => "Show taxon and child taxon media");
828
    $form['images']['cdm_dataportal_show_media'] = array(
829
    '#type' => 'select',
830
    '#title' => t('Available media files'),
831
    '#default_value' => variable_get('cdm_dataportal_show_media', false),
832
    '#options' => $selectShowMedia,
833
    '#description'   => t('Select if a taxon should show only his media or also child media.')
834
    );
835

    
836
    // --- SEARCH TAXA GALLERY ---- //
837
    $collapsed = TRUE;
838
    $form_name = CDM_DATAPORTAL_SEARCH_GALLERY_NAME;
839
    $form_tittle = 'Search Taxa';
840
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
841

    
842
    // --- FEATURE DESCRIPTION GALLERY ---- //
843
    $form_name = CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME;
844
    $form_tittle = 'Description elements gallery';
845
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
846

    
847
    // --- MEDIA GALLERY ---- //
848
    $form_name = CDM_DATAPORTAL_MEDIA_GALLERY_NAME;
849
    $form_tittle = 'Media gallery';
850
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
851

    
852
    return system_settings_form($form);
853
}
854

    
855
function cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed){
856

    
857
	$form[$form_name] = array(
858
    '#type' => 'fieldset',
859
    '#title' => t($form_tittle),
860
    '#collapsible' => TRUE,
861
    '#collapsed' => $collapsed,
862
    '#tree' => true,
863
	);
864

    
865
	$default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
866
	$gallery_settings = variable_get($form_name, $default_values);
867

    
868
  if($form_name != CDM_DATAPORTAL_MEDIA_GALLERY_NAME){
869
    $form[$form_name]['cdm_dataportal_show_taxon_thumbnails'] = array(
870
      '#type' => 'checkbox',
871
      '#title' => t('Show media thumbnails for accepted taxa'),
872
      '#default_value' => $gallery_settings['cdm_dataportal_show_taxon_thumbnails'],
873
      '#description' => t('')
874
    );
875

    
876
    $form[$form_name]['cdm_dataportal_show_synonym_thumbnails'] = array(
877
      '#type' => 'checkbox',
878
      '#title' => t('Show media thumbnails for synonyms'),
879
      '#default_value' => $gallery_settings['cdm_dataportal_show_synonym_thumbnails'],
880
      '#description' => t('')
881
    );
882
  }
883
  
884
	//$showCaption = variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 0);
885
	$form[$form_name]['cdm_dataportal_show_thumbnail_captions'] = array(
886
    '#type' => 'checkbox',
887
    '#title' => t('Show captions under thumbnails'),
888
    '#default_value' => $gallery_settings['cdm_dataportal_show_thumbnail_captions'],
889
    '#description' => t('')
890
	);
891

    
892
	$form[$form_name]['cdm_dataportal_media_maxextend'] = array(
893
    '#type' => 'textfield',
894
    '#title' => t('Maximum extend of Images'),
895
    '#default_value' => $gallery_settings['cdm_dataportal_media_maxextend'],
896
    '#description' => t('In the gallery neither width or height will exceed the maximum extend of images.')
897
	);
898

    
899
	$form[$form_name]['cdm_dataportal_media_cols'] = array(
900
    '#type' => 'textfield',
901
    '#title' => t('Number of columns'),
902
    '#default_value' => $gallery_settings['cdm_dataportal_media_cols'],
903
    '#description' => t('')
904
	);
905

    
906
  if($form_name != CDM_DATAPORTAL_MEDIA_GALLERY_NAME){
907
    $form[$form_name]['cdm_dataportal_media_maxRows'] = array(
908
      '#type' => 'textfield',
909
      '#title' => t('Maximum number of rows'),
910
      '#default_value' => $gallery_settings['cdm_dataportal_media_maxRows'],
911
      '#description' => t('If you want an unlimited number of rows please set to 0')
912
    );
913
  }
914

    
915
	return $form;
916
}
917

    
918
/**
919
 * GEOSERVICE and Map settings
920
 * @return unknown_type
921
 */
922
function cdm_dataportal_settings_geo(){
923

    
924
	$form = array();
925

    
926
	$form['cdm_dataportal_map_openlayers'] = array(
927
    '#type' => 'checkbox',
928
    '#title' => t('OpenLayers Viewer'),
929
    '#default_value' => variable_get('cdm_dataportal_map_openlayers', 1),
930
    '#description' => t('Display the maps in an interactive viewer which allows zooming and panning.')
931
	);
932

    
933
	$form['cdm_dataportal_geoservice_access_point'] = array(
934
    '#type' => 'textfield',
935
    '#title' => t('Geoservice Access Point'),
936
    '#default_value' => variable_get('cdm_dataportal_geoservice_access_point', ''),
937
    '#description' => t('Base URL of the geoservice to be used by this portal')
938
	);
939

    
940
	$form['cdm_dataportal_geoservice_display_width'] = array(
941
    '#type' => 'textfield',
942
    '#title' => t('Geoservice Display Width'),
943
    '#default_value' => variable_get('cdm_dataportal_geoservice_display_width', 390),
944
    '#description' => t('Width of the image generated by geoservice')
945
	);
946

    
947
	$form['cdm_dataportal_geoservice_bounding_box'] = array(
948
    '#type' => 'textfield',
949
    '#title' => t('Fixed Geoservice Bounding Box'),
950
    '#default_value' => variable_get('cdm_dataportal_geoservice_bounding_box', '-180,-90,180,90'),
951
    '#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.')
952
	);
953

    
954
	//cdm_dataportal_geoservice_labels_on
955
	$form['cdm_dataportal_geoservice_labels_on'] = array(
956
    '#type' => 'checkbox',
957
    '#title' => t('Display Country Labels'),
958
    '#default_value' => variable_get('cdm_dataportal_geoservice_labels_on', FALSE),
959
    '#description' => t('Check this if you like country names to be displayed in the maps. ')
960
	);
961

    
962
	//cdm_dataportal_geoservice_legend_on
963
	$form['cdm_dataportal_geoservice_map_caption'] = array(
964
    '#type' => 'textfield',
965
    '#title' => t('Map Caption'),
966
    '#default_value' => variable_get('cdm_dataportal_geoservice_map_caption', ''),
967
    '#description' => t('Define a caption for the map.')
968
	);
969

    
970
	/*
971
	 //cdm_dataportal_geoservice_legend_on
972
	 $form['cdm_dataportal_geoservice_legend_on'] = array(
973
	 '#type' => 'checkbox',
974
	 '#title' => t('Display a map legend'),
975
	 '#default_value' => variable_get('cdm_dataportal_geoservice_legend_on', TRUE),
976
	 '#description' => t('Check this if you like a legend to be displayed with the maps. ')
977
	 );
978
	 */
979

    
980
	//cdm_dataportal_geoservice_map_legend
981
	$form['cdm_dataportal_geoservice_map_legend'] = array(
982
      '#type' => 'fieldset',
983
      '#title' => t('Map Legend'),
984
      '#collapsible' => FALSE,
985
      '#collapsed' => TRUE,
986
	);
987

    
988
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_on'] = array(
989
    '#type' => 'checkbox',
990
    '#title' => t('Display a map legend'),
991
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_on', TRUE),
992
    '#description' => t('Check this if you like a legend to be displayed with the maps. ')
993
	);
994

    
995
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_size'] = array(
996
    '#type' => 'textfield',
997
    '#title' => t('Font size'),
998
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_size', 10),
999
    '#description' => t('Font size in pixels.')
1000
	);
1001

    
1002
	$fontStyles = array(0 => "plane", 1 => "italic");
1003
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_style'] = array(
1004
    '#type' => 'select',
1005
    '#title' => t('Available font styles'),
1006
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_style', false),
1007
    '#options' => $fontStyles,
1008
    '#description'   => t('Select a font style for the map legend.')
1009
	);
1010

    
1011
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_width'] = array(
1012
    '#type' => 'textfield',
1013
    '#title' => t('Icon width'),
1014
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_width', 35),
1015
    '#description' => t('Icon width in pixels.')
1016
	);
1017

    
1018
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_height'] = array(
1019
    '#type' => 'textfield',
1020
    '#title' => t('Icon height'),
1021
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_height', 15),
1022
    '#description' => t('Icon height in pixels.')
1023
	);
1024

    
1025
	/* variable_get('cdm_dataportal_findtaxa_media_maxextend', 120),
1026
	 *  $form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
1027
	 '#type' => 'select',
1028
	 '#title'         => t('Available taxonomic views'),
1029
	 '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
1030
	 '#options' => $taxonomicTreeOptions,
1031
	 '#description'   => t('Select a taxonomic view for this webservice.')
1032
	 );
1033

    
1034
	 $taxonTrees = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY);
1035
	 foreach($taxonTrees as $tree){
1036
	 $taxonomicTreeOptions[$tree->uuid] = $tree->titleCache;
1037
	 }
1038
	 $form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
1039
	 '#type' => 'select',
1040
	 '#title'         => t('Available taxonomic views'),
1041
	 '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
1042
	 '#options' => $taxonomicTreeOptions,
1043
	 '#description'   => t('Select a taxonomic view for this webservice.')
1044
	 );
1045
	 */
1046

    
1047
	return system_settings_form($form);
1048

    
1049
}
1050

    
1051

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

    
1057
	_add_js_progressbar();
1058

    
1059
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/cache_all_taxa.js');
1060

    
1061
	$out = '';
1062

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

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

    
1092
	return $out;
1093
}
1094

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

    
1108
/*
1109
 *
1110
 */
1111
function cdm_dataportal_select_secuuid_expand($element){
1112

    
1113
	$element['#tree'] = FALSE;
1114

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

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

    
1140

    
1141

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

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

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

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

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

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

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

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

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

    
1214
	$taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $uuid);
1215
	if(!$taxon){
1216
		drupal_set_title(t('Taxon does not exist'));
1217
		return false;
1218
	}
1219
	//FIXME remove _cdm_dataportal_set_currentSecUuid($taxon->sec->uuid);
1220

    
1221
	$taxonpage->title = theme('cdm_taxon_page_title', $taxon);
1222
	$taxonpage->content = theme('cdm_taxon_page_general', $taxon, $chapter);
1223

    
1224
	return $taxonpage;
1225
}
1226

    
1227
/**
1228
 * The function returns a name page as a drupal node ready to be renderized by drupal.
1229
 * The node page show the taxon name title and the list of taxon related with such taxon 
1230
 * name on the tree already in used. 
1231
 * @param $taxon_name_uuid A taxon name uuid
1232
 * @return The formatted name page as node
1233
 */
1234
function cdm_dataportal_name_page_view($taxon_name_uuid){
1235
  $taxonname_page = cdm_dataportal_name_view($taxon_name_uuid);
1236
  return cdm_node_show(NODETYPE_NAME, $taxon_name_uuid, $taxonname_page->title , $taxonname_page->content);
1237
}
1238

    
1239
/**
1240
 * The function genates a object ready to be transformated to a node 
1241
 * in order to show as a drupal node
1242
 * @param $taxon_name_uuid
1243
 * @return the object with the page content and title  
1244
 */
1245
function cdm_dataportal_name_view($taxon_name_uuid){
1246
	//getting the full taxonname object from the server  
1247
  $taxon_name = cdm_ws_get(CDM_WS_NAME, array($taxon_name_uuid));
1248
  if(!$taxon_name){
1249
    drupal_set_title(t('Taxon name does not exist'));
1250
    return false;
1251
  }
1252
  //searching for all the taxon connected with the taxon name on the tree in used
1253
  $name_cache = cdm_ws_get(CDM_WS_NAME_NAMECAHE, array($taxon_name_uuid));
1254
  $request_params = array();
1255
  $request_params['query'] = $name_cache;
1256
  $request_params['tree'] = variable_get('cdm_taxonomictree_uuid', false);
1257
  $request_params['doTaxa'] = 1;
1258
  $request_params['doSynonyms'] = 1;
1259
  $request_params['doTaxaByCommonNames'] = 0;
1260
  $request_params['matchMode'] = "EXACT";
1261
  //$taxon_pager = cdm_ws_get(CDM_WS_FIND_TAXA, null, queryString($request_params));
1262
  $taxon_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_FIND, null, queryString($request_params));
1263
  
1264
  if (sizeof($taxon_pager->records) == 1){
1265
  	drupal_goto('cdm_dataportal/taxon/' . $taxon_pager->records[0]->uuid);
1266
  }else{
1267
    $taxon_name_page->title = theme('cdm_name_page_title', $taxon_name);
1268
    if($taxon_pager->records){
1269
      $taxon_name_page->content = theme_cdm_list_of_taxa($taxon_pager->records, false);
1270
    }else{
1271
      $taxon_name_page->content = 'This name has no taxa';
1272
    }
1273
    return $taxon_name_page; 
1274
  }
1275
  
1276
  //_cdm_dataportal_set_currentSecUuid($taxon->sec->uuid);
1277
  
1278
  //generating the returned object
1279

    
1280
}
1281

    
1282
function cdm_dataportal_view_search_advanced(){
1283

    
1284
	drupal_set_title(t('Advanced Search'));
1285

    
1286
	$searchForm = cdm_dataportal_search_taxon_form(true);
1287

    
1288
	return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
1289

    
1290
}
1291

    
1292
/**
1293
 *
1294
 * future extensions to meet palmweb mockup requirements:
1295
 *  - habitat
1296
 *  - uses
1297
 *  - conservation status
1298
 *  - locality / tdwg region
1299
 */
1300
function cdm_dataportal_view_search_taxon(){
1301

    
1302
	$_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
1303

    
1304
	$request_params = cdm_dataportal_search_form_request();
1305
	$taxonPager = cdm_ws_get(CDM_WS_PORTAL_TAXON_FIND, null, queryString($request_params));
1306

    
1307
	$search_params = $_REQUEST;
1308
	unset($search_params['q']);
1309
	return theme('cdm_search_results', $taxonPager, 'cdm_dataportal/search/taxon', $search_params);
1310
}
1311

    
1312
///**
1313
// * 
1314
// * @return unknown_type
1315
// */
1316
//function cdm_dataportal_view_name($taxon_name_uuid){
1317
//
1318
//	$taxon_name = cdm_ws_get(CDM_WS_NAME, array($taxon_name_uuid));
1319
//	/*
1320
//	$name_cache = cdm_ws_get(CDM_WS_NAME_NAMECAHE, array($taxon_name_uuid));
1321
//	$records = array();
1322
//	foreach ($taxon_name->taxonBases as $taxon_base){
1323
//		$taxon = cdm_ws_get(CDM_WS_TAXON, array($taxon_base->uuid));
1324
//		$records[] = $taxon;
1325
//	}
1326
//	
1327
//	$out  = "Taxon name: " . $taxon_name->titleCache . "<br/>";
1328
//	$out .= theme_cdm_list_of_taxa($records, false);
1329
//	*/
1330
//	
1331
//	$request_params = array();
1332
//  $request_params['query'] = $name_cache;
1333
//  $request_params['tree'] = variable_get('cdm_taxonomictree_uuid', false);
1334
//  $request_params['doTaxa'] = 1;
1335
//  $request_params['doSynonyms'] = 1;
1336
//  $request_params['doTaxaByCommonNames'] = 0;
1337
//  //$request_params['matchMode'] = "EXACT";
1338
//  
1339
//  $taxon_pager = cdm_ws_get(CDM_WS_FIND_TAXA, null, queryString($request_params));
1340
//  //var_dump($taxon_pager->records);
1341
//  //$out = theme_cdm_taxonname_page_title($taxon_name);
1342
//  $out = theme_cdm_list_of_taxa($taxon_pager->records, false);
1343
//  
1344
///*
1345
//  $out  = "Taxon name: " . $taxon_name->titleCache . "<br/>";
1346
//  $out .= "Taxon name uuid: " . $taxon_name_uuid  . "<br/>";
1347
//  $out .= "Taxon name records: "  . "<br/>" . $records_names_out; 
1348
//*/
1349
//  //$out  = "Taxon name: " . $taxon_name->titleCache . "<br/>";
1350
//	return $out;
1351
//}
1352

    
1353
function cdm_view_xml2json(){
1354
	$file = arg(2);
1355
	$datastr = get_content(variable_get('cdm_webservice_url', '').$file);
1356
	return  xml2json::transformXmlStringToJson($datastr);
1357
}
1358

    
1359
/* ====================== other functions ====================== */
1360

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

    
1372
function path_to_reference($uuid){
1373
	if(!$uuid) return false;
1374
	return 'cdm_dataportal/reference/'.$uuid;
1375
}
1376

    
1377
function path_to_name($name_uuid){
1378
	$res = false;
1379
	if($name_uuid){
1380
	 $res = 'cdm_dataportal/name/'.$name_uuid;
1381
	}
1382
  return $res;
1383
}
1384

    
1385
function path_to_media($uuid, $representaion_uuid = false, $partId = false){
1386
	if(!$uuid) return false;
1387
	$out = 'cdm_dataportal/media/'.$uuid;
1388
	if($representaion_uuid){
1389
		$out .= '/'.$representaion_uuid;
1390
		if($partId !== false){
1391
			$out .= '/'.$partId;
1392
		}
1393
	}
1394
	return $out;
1395
}
1396

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

    
1421
function uri_to_synonym($synonymUuid, $acceptedUuid, $pagePart = null){
1422
	$acceptedPath = path_to_taxon($acceptedUuid);
1423
	return url($acceptedPath.($pagePart ? '/'.$pagePart : ''), 'highlite='.$synonymUuid.'&acceptedFor='.$synonymUuid);
1424
}
1425

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

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

    
1458
/**
1459
 * TODO Add Comments. I don't get what
1460
 *
1461
 * @param UUID $secUuid
1462
 */
1463
function _cdm_dataportal_set_currentSecUuid($secUuid){
1464

    
1465
	// do not save in session but in database
1466

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

    
1479
}
1480

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

    
1510
/**
1511
 * Check if a taxon is accepted by the current taxonomic tree
1512
 *
1513
 * @param Taxon $taxon
1514
 * @return true if $taxon is accepted, false otherwise
1515
 */
1516

    
1517
function _cdm_dataportal_acceptedByCurrentView($taxon){
1518

    
1519
	//FIXME $current_secref = _cdm_dataportal_currentSecRef_array();
1520
	$defaultTreeUuid = variable_get('cdm_taxonomictree_uuid', false);
1521
	if($taxon->class == "Taxon" && isset($taxon->taxonNodes)){
1522
		foreach($taxon->taxonNodes as $node){
1523
			if($node->taxonomicTree == $defaultTreeUuid) {
1524
				return true;
1525
			}
1526
		}
1527
	}
1528
	return false;
1529
}
1530

    
1531
/**@Deprecated
1532
 *
1533
 */
1534
function compose_url_prameterstr($parameters = array(), $parentPropertyName = false){
1535
	$pstr = '';
1536
	foreach($parameters as $key=>$value){
1537
		if(is_array($value)){
1538

    
1539
		} else {
1540
			$pstr .= ($pstr ? '&' :'').$key.'='.urlencode($value);
1541
		}
1542
	}
1543
	return $pstr;
1544
}
1545

    
1546
function _get_feature_trees(){
1547
	$feature_trees = array();
1548

    
1549
	// set tree that contains all features
1550
	$feature_trees[UUID_DEFAULT_FEATURETREE] = t('Default Featuretree (contains all features)');
1551

    
1552
	// get features from database
1553
	$persisted_trees = cdm_ws_get(CDM_WS_FEATURETREES);
1554
	if(is_array($persisted_trees)){
1555
		foreach($persisted_trees as $featureTree){
1556

    
1557
			// do not add the DEFAULT_FEATURETREE again
1558
			if($featureTree->uuid == UUID_DEFAULT_FEATURETREE){
1559
				continue;
1560
			}
1561
			$featureLabels = array();
1562
			foreach ($featureTree->root->children as $featureNode){
1563
				$featureLabels[] = $featureNode->feature->representation_L10n;
1564
			}
1565

    
1566
			$feature_trees[$featureTree->uuid] = implode(", ", $featureLabels);
1567

    
1568
		}
1569
	}
1570
	return $feature_trees;
1571
}
1572

    
1573

    
1574
function get_nameRenderTemplate($renderPath, $nameLink = NULL, $refenceLink = NULL){
1575
	//TODO implement admin user interface to replace swicth statement
1576
	//     preliminar solution: using themes
1577

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

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

    
1631
	if($nameLink && isset($template['namePart']['#uri'])){
1632
		$template['namePart']['#uri'] = $nameLink;
1633
	} else{
1634
		unset($template['namePart']['#uri']);
1635
	}
1636

    
1637
	if($refenceLink && isset($template['referencePart']['#uri'])){
1638
		$template['referencePart']['#uri'] = $refenceLink;
1639
	}else{
1640
		unset($template['referencePart']['#uri']);
1641
	}
1642
	
1643
	return $template;
1644
}
1645

    
1646
function get_partDefinition($taxonNameType){
1647
	//TODO implement admin user interface to allow specify the partdefinitions for any type
1648
	//     preliminar solution: using themes
1649

    
1650
	$partdef = theme('get_partDefinition', $taxonNameType);
1651

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

    
1718
/**
1719
 * The function read the metadata info such title or artist of a media file. The
1720
 * function tries at first to get all the info from the file metadata and if it is
1721
 * not avaible look at the media file info stored at the database.
1722
 * @param $media The media file
1723
 * @return array The array with the avilable specified metadata info.
1724
 * TODO rename to read_media_metadata() and move to *.module
1725
 */
1726
function cdm_read_media_metadata($media){
1727

    
1728
	$metadata_caption = array('title' => '',           //media_metadata and media
1729
                              'artist' => '',          //media_metadata and media
1730
                              'rights',                //media_metadata and media
1731
                              'location',              //media_metadata
1732
                              'filename' => '',        //media
1733
                              'mediacreated' => '',    //media
1734
                              'description' => '');    //media
1735

    
1736
	//getting the media metadata
1737
	$media_metadata = cdm_ws_get(CDM_WS_MEDIA_METADATA, array($media->uuid));
1738

    
1739
	//title
1740
	//if ($media_metadata->Headline) {
1741
	if ($media_metadata->ObjectName) {
1742
		$metadata_caption['title'] = $media_metadata->ObjectName;
1743
		//	} else if ($media->titleCache){
1744
		//		$metadata_caption['title'] = $media_metadata->ObjectName;
1745
	} else if ($media->titleCache){
1746
		$metadata_caption['title'] = $media->titleCache;
1747
		if ($media->description_L10n)
1748
		$metadata_caption['title'] .= ' - ' . $media->description_L10n;
1749
	}else{ //if there is no title on the db and metadata get file title
1750
    $aux = $media->representations[0]->parts[0]->uri;
1751
    $aux = explode("/", $aux);
1752
    $aux = end($aux);
1753
		$metadata_caption['title'] = $aux;		
1754
	}
1755

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

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

    
1790
	//location
1791
	$metadata_caption['location'] = array();
1792
	$metadata_caption['location']['sublocation'] = $media_metadata->Sublocation;
1793
	$metadata_caption['location']['city'] = $media_metadata->City;
1794
	$metadata_caption['location']['province'] = $media_metadata->Province;
1795
	$metadata_caption['location']['country'] = $media_metadata->Country;
1796

    
1797
	//filename
1798
	if(isset($media->representations[0]->parts[0]->uri)){
1799
		$fileUri = $media->representations[0]->parts[0]->uri;
1800
		$filename = substr($fileUri, strrpos($fileUri, "/")+1);
1801
		$metadata_caption['filename'] = $filename;
1802
	}
1803
	else{
1804
	 $metadata_caption['filename'] = '';
1805
	}
1806
	/*
1807
	 //creation date
1808
	 if($media_metadata["Modify Date"])
1809
	 $metadata_caption['mediacreated'] = $media_metadata["Modify Date"];
1810
	 else
1811
	 $metadata_caption['mediacreated'] = $media->created;
1812
	 */
1813
	//returned value
1814
	return $metadata_caption;
1815
}
1816

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

    
1838

    
(6-6/10)