Project

General

Profile

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

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

    
14
require_once('node_types.php');
15
require_once('cdm_dataportal.theme.php');
16
require_once('classes/footnotemanager.php');
17
require_once('classes/footnote.php');
18

    
19
define('CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE', 25);
20
define('CDM_DATAPORTAL_NOMREF_IN_TITLE', 1);
21
define('CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR', 0);
22

    
23
/* gallery variables */
24
$gallery_settings = array(
25
    "cdm_dataportal_show_taxon_thumbnails" => 1,
26
    "cdm_dataportal_show_synonym_thumbnails" => 0,
27
    "cdm_dataportal_show_thumbnail_captions" => 1,
28
    "cdm_dataportal_media_maxextend" => 120,
29
    "cdm_dataportal_media_cols" => 3,
30
    "cdm_dataportal_media_maxRows" => 1);
31
define('CDM_DATAPORTAL_GALLERY_SETTINGS', serialize($gallery_settings));
32
define('CDM_DATAPORTAL_DESCRIPTION_GALLERY_FORM_NAME', "description_gallery");
33
define('CDM_DATAPORTAL_MEDIA_GALLERY_FORM_NAME', "media_gallery");
34
define('CDM_DATAPORTAL_SEARCH_GALLERY_FORM_NAME', "search_gallery");
35

    
36

    
37

    
38
function _add_js_progressbar(){
39
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/jquery.progressbar/js/jquery.progressbar.js');
40
}
41

    
42

    
43
/* ====================== hook implementations ====================== */
44

    
45
/**
46
 * Implementation of hook_help()
47
 *
48
 * Display help and module information
49
 * @param section which section of the site we're displaying help
50
 * @return help text for section
51
 */
52
function cdm_dataportal_help($section='') {
53

    
54
	$out = '';
55
	switch ($section) {
56
		case "admin/modules#description":
57
			$out = t("The dataportal publishes CDM data hosted in a CommunityStore on the web.");
58
			break;
59
	}
60
	return $out;
61
}
62

    
63

    
64
/**
65
 * Implementation of hook_perm()
66
 *
67
 * Valid permissions for this module
68
 * @return array An array of valid permissions for the portfolio module
69
 */
70
function cdm_dataportal_perm() {
71
	return array(
72
    	'administer cdm_dataportal',
73
      'cdm_dataportal view notes',
74
	//TODO which else permission are required? -> check the WP6 requirements document
75
	);
76
}
77

    
78

    
79
/**
80
 * Implementation of hook_menu()
81
 */
82
function cdm_dataportal_menu($may_cache) {
83
	$items = array();
84

    
85
	if ($may_cache) {
86

    
87
		$items[] = array(
88
      'path' => 'admin/settings/cdm_dataportal',
89
      'title' => t('CDM Dataportal'),
90
      'description' => t('Setting for the CDM DataPortal'),
91
      'access' => user_access('administer cdm_dataportal'),
92
      'callback' => 'drupal_get_form',
93
      'callback arguments' => 'cdm_dataportal_settings',
94
      'type' => MENU_NORMAL_ITEM,
95
		);
96

    
97
		$items[] = array(
98
      'path' => 'admin/settings/cdm_dataportal/general',
99
      'title' => t('General'),
100
      'description' => t('Setting for the CDM DataPortal'),
101
      'access' => user_access('administer cdm_dataportal'),
102
      'callback' => 'drupal_get_form',
103
      'callback arguments' => 'cdm_dataportal_settings',
104
      'type' => MENU_LOCAL_TASK,
105
		);
106

    
107
		$items[] = array(
108
      'path' => 'admin/settings/cdm_dataportal/geo',
109
      'title' => t('Geo & Map'),
110
      'description' => t('Geo & Map'),
111
      'access' => user_access('administer cdm_dataportal'),
112
      'callback' => 'drupal_get_form',
113
      'callback arguments' => 'cdm_dataportal_settings_geo',
114
      'type' => MENU_LOCAL_TASK,
115
		);
116

    
117
		$items[] = array(
118
      'path' => 'admin/settings/cdm_dataportal/layout',
119
      'title' => t('Layout'),
120
      'description' => t('Configure and adjust the layout of your DataPortal '),
121
      'access' => user_access('administer cdm_dataportal'),
122
      'callback' => 'drupal_get_form',
123
      'callback arguments' => 'cdm_dataportal_settings_layout',
124
      'type' => MENU_LOCAL_TASK,
125
		);
126

    
127
		$items[] = array(
128
      'path' => 'admin/settings/cdm_dataportal/cachesite',
129
      'title' => t('Cache Site'),
130
      'description' => t('Cache the whole CDM DataPortal site'),
131
      'access' => user_access('administer cdm_dataportal'),
132
      'callback' => 'cdm_dataportal_view_cache_site',
133
      'type' => MENU_LOCAL_TASK,
134
		);
135

    
136
		/*$items[] = array(
137
		 'path' => 'admin/settings/cdm_dataportal/ws',
138
		 'title' => t('Web Service'),
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' => 1,
144
		 'type' => MENU_DEFAULT_LOCAL_TASK,
145
		 );*/
146

    
147
		$items[] = array(
148
	    'path' => 'cdm_dataportal/names',
149
	    'callback' => 'cdm_dataportal_view_names',
150
	    'access' => true,
151
	    'type' => MENU_CALLBACK,
152
		);
153
		// optional callback arguments: page
154
			
155
		$items[] = array(
156
      'path' => 'cdm_dataportal/taxon',
157
      'callback' => 'cdm_dataportal_taxon_page_view',
158
      'access' => true,
159
      'type' => MENU_CALLBACK,
160
		// expected callback arguments: uuid
161
		);
162

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

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

    
179
		$items[] = array(
180
      'path' => 'cdm_dataportal/media',
181
      'callback' => 'cdm_dataportal_view_media',
182
      'access' => true,
183
      'type' => MENU_CALLBACK,
184
		// expected callback arguments: uuid, mediarepresentation_uuid, part_uuid or part#
185
		);
186

    
187
		$items[] = array(
188
      'path' => 'cdm_dataportal/search',
189
      'callback' => 'cdm_dataportal_view_search_advanced',
190
      'access' => true,
191
      'type' => MENU_CALLBACK,
192
		);
193

    
194
		$items[] = array(
195
      'path' => 'cdm_dataportal/search/taxon',
196
      'callback' => 'cdm_dataportal_view_search_taxon',
197
      'access' => true,
198
      'type' => MENU_CALLBACK,
199
		);
200

    
201
		$items[] = array(
202
      'path' => 'cdm/xml2json',
203
      'callback' => 'cdm_view_xml2json',
204
      'access' => true,
205
      'type' => MENU_CALLBACK,
206
		);
207

    
208

    
209
	} else {
210
		// may not cache
211
		// --- local tasks for Taxon
212
		if(variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
213

    
214
			$items[] = array(
215
      'path' => 'cdm_dataportal/taxon/'.arg(2),
216
      'title' => theme('cdm_taxonpage_tab', 'General'),
217
      'callback' => 'cdm_dataportal_taxon_page_view',
218
      'access' => true,
219
      'type' => MENU_CALLBACK,
220
      'weight' => 1,
221
      'callback arguments' => array(arg(2), "description")
222
			// expected callback arguments: name_uuid
223
			);
224

    
225
			$items[] = array(
226
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/all',
227
      'title' => theme('cdm_taxonpage_tab', 'General'),
228
      'callback' => 'cdm_dataportal_taxon_page_view',
229
      'access' => true,
230
      'type' => MENU_CALLBACK,
231
      'weight' => 2,
232
      'callback arguments' => array(arg(2), "all")
233
			// expected callback arguments: name_uuid
234
			);
235

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

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

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

    
271
	drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal.css');
272
	//drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
273
	drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_screen.css', 'screen');
274

    
275
	return $items;
276

    
277
}
278

    
279
/**
280
 * Implementation of hook_block()
281
 *
282
 * Provides the following blocks:
283
 *  0: list of links useful during development
284
 *
285
 * @param String $op
286
 * @param int $delta
287
 */
288
function cdm_dataportal_block($op='list', $delta=0) {
289
	// listing of blocks, such as on the admin/block page
290
	if ($op == "list") {
291
		//$block[0]["info"] = t("CDM DataPortal DevLinks");
292
		$block[1]["info"] = t("CDM DataPortal Credits");
293
		$block[2]["info"] = t("CDM Search Taxa");
294
		//$block[3]["info"] = t("CDM Filters");
295
		$block[4]["info"] = t("CDM Dataportal Print");
296
		return $block;
297
	}
298
	else if ($op == 'view') {
299
		switch($delta){
300
			case 1:
301
				$block['subject'] = t('Credits');
302
				$block['content'] = theme('cdm_credits');
303
				return $block;
304
			case 2:
305
				$block['subject'] = t('Search Taxa');
306
				$block['content'] = drupal_get_form('cdm_dataportal_search_taxon_form');
307
				$block['content'] .= '<div>'.l('Advanced Search', '/cdm_dataportal/search').'</div>';
308
				return $block;
309
			case 4:
310
				$block['subject'] = t('Print this page');
311
				$block['content'] = theme('cdm_print_button');;
312
				return $block;
313
		}
314
	}
315
}
316

    
317

    
318
/**
319
 * Implementation of hook_validate()
320
 *
321
 * @param $element
322
 */
323
function cdm_dataportal_settings_validate($form_id, $form_values){
324

    
325
	if (!str_endsWith($form_values['cdm_webservice_url'], '/')) {
326
		form_set_error('cdm_webservice_url', t("The URL to the CDM Web Service must end with a slash: '/'."));
327
	}
328

    
329
	$cdm_webservice_url_changed = variable_get('cdm_webservice_url', '') != $form_values['cdm_webservice_url'];
330

    
331
	$cdm_secUuid_default_changed = variable_get('cdm_secUuid_default', '') != $form_values['cdm_secUuid_default'];
332

    
333
	if ($cdm_webservice_url_changed || $cdm_secUuid_default_changed) {
334

    
335
		_cdm_dataportal_set_currentSecUuid($form_values['cdm_secUuid_default']);
336
		// reset all cdm related data stored in the session
337
		// cdm_dataportal_session_clear($form_values['cdm_webservice_url']); // cdm_webservice_url is not further stored here
338
		// clear the cdm webservice cache
339
		cache_clear_all(NULL, 'cache_cdm_ws');
340
		// better clear secref_cache since i can not be sure if the cache has not be used during this response
341
		cdm_api_secref_cache_clear();
342
	}
343
	if($form_values['cdm_webservice_cache'] != variable_get('cdm_webservice_cache', 1)){
344
		cache_clear_all(NULL, 'cache_cdm_ws');
345
		// better clear secref_cache since i can not be sure if the cache has not be used during this response
346
		cdm_api_secref_cache_clear();
347
	}
348

    
349
}
350

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

    
359
 function cdm_dataportal_session_validate(){
360

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

    
369
function cdm_dataportal_search_taxon_form($advancedForm = false){
370

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

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

    
383
	$form['query'] = array(
384
    '#delta' => 0,
385
    '#type' => 'textfield',
386
    '#size' => 20,
387
    '#attributes' => array('title' => t('Enter the name or part of a name you wish to search for. The asterisk  character * can always be used as wildcard')),
388
    '#value' => $preset_query,
389
	);
390

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

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

    
404

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

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

    
421
	if($advancedForm){
422
		// general search parameters
423
		$form['search']['doTaxa'] = array(
424
      '#delta' => 2,
425
      '#type' => 'checkbox',
426
      '#title' => t('Search for accepted taxa'),
427
      '#value' => $preset_doTaxa
428
		);
429
		$form['search']['doSynonyms'] = array(
430
      '#delta' => 3,
431
      '#type' => 'checkbox',
432
      '#title' => t('Search for synonyms'),
433
      '#value' => $preset_doSynonyms
434
		);
435
		$form['search']['doTaxaByCommonNames'] = array(
436
      '#delta' => 4,
437
      '#type' => 'checkbox',
438
      '#title' => t('Search for common names'),
439
      '#value' => $preset_doTaxaByCommonNames
440
		);
441
			
442
		// Geographic Range
443
		$form['search']['geographic_range'] = array(
444
      '#type' => 'fieldset',
445
      '#delta' => 5,
446
      '#tree' => true,
447
      '#title' => t('Geographic range'),
448
		);
449

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

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

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

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

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

    
536
	return $form;
537
}
538

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

    
543
/**
544
 * Implementation #process method call, see form_builder()
545
 * <p>
546
 * Removes Drupal internal form elements from query
547
 * @param $form
548
 * @return unknown_type
549
 */
550
function cdm_dataportal_search_process($form) {
551
	unset($form['form_id']);
552
	unset($form['form_token']);
553
	return $form;
554
}
555

    
556
/**
557
 * Filters $_REQUEST by a list of valid request  parameters and also sets defaults if required.
558
 * returns the processed request parameters submitted by the search form.
559
 */
560
function cdm_dataportal_search_form_request(){
561

    
562

    
563
	$form_params = array();
564
	array_deep_copy($_REQUEST['search'], $form_params);
565
	$form_params['query'] =  trim($_REQUEST['query']);
566

    
567
	// split of  geographic range
568
	if(isset($_REQUEST['search']['geographic_range'])){
569
		$geographicRange = $_REQUEST['search']['geographic_range'];
570
		// remove
571
		unset($form_params['geographic_range']);
572
	}
573

    
574
	// add geographic range
575
	if($geographicRange){
576
		$form_params['tdwg_level_select'] = $geographicRange['tdwg_level_select'];
577
		for($i = 1; $i < 5; $i++){
578
			if(isset($geographicRange['tdwg_level_'.$i])){
579
				$form_params['area'] = $geographicRange['tdwg_level_'.$i];
580
			}
581
		}
582
	}
583

    
584
	// store in session
585
	$_SESSION['cdm']['search'] = $form_params;
586

    
587
	return $form_params;
588
}
589

    
590

    
591
/* UNREACHABLE since action of form directly links to view
592
 function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
593

    
594
 $_SESSION['cdm']['search'] = $form_values;
595
 //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
596
 return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
597
 //$paramstr = compose_url_prameterstr($form_values);
598
 //return url('/cdm_dataportal/search/taxon/', $paramstr);
599
 }
600
 */
601
/* ====================== menu callback functions ====================== */
602

    
603
/**
604
 * Generate main administration form.
605
 *
606
 * @return
607
 *   An array containing form items to place on the module settings page.
608
 */
609
function cdm_dataportal_settings(){
610

    
611
	$form = cdm_api_settings_form();
612

    
613
	//TODO: settings are still incomplete, compare with trunk/dataportal/inc/config_default.php.inc
614
	$form['cdm_dataportal'] = array(
615
      '#type' => 'fieldset',
616
      '#title' => t('CDM DataPortal'),
617
      '#collapsible' => FALSE,
618
      '#collapsed' => TRUE,
619
	);
620

    
621
	//  $form['cdm_dataportal']['secuuid_widget'] = array(
622
	//    '#type' => 'select_secuuid', // => will be themed by theme_select_secuuid(...)
623
	//    '#title' => t('Default Concept References'),
624
	//    '#description'   => t('The default \'sensu\' reference to start the CDM DataPortal with. Per user choices are possible and may override this value.'),
625
	//    '#varname' => 'cdm_secUuid_default',
626
	//    '#multiple' => false
627
	//  );
628

    
629
	$taxonTrees = cdm_ws_get(CDM_WS_TAXONOMY);
630
	foreach($taxonTrees as $tree){
631
		$taxonomicTreeOptions[$tree->uuid] = $tree->titleCache;
632
	}
633
	$form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
634
    '#type' => 'select',
635
    '#title'         => t('Available taxonomic views'),
636
    '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
637
    '#options' => $taxonomicTreeOptions,
638
    '#description'   => t('Select a taxonomic view for this webservice.')
639
	);
640

    
641
	//  $form['cdm_dataportal']['DEBUG']= array(
642
	//    '#value' => '<pre>'.print_r($taxonTrees, true).'</pre>'
643
	//  );
644

    
645

    
646
	//------------------ FEATURE TREE --------------------//
647

    
648
	$form['cdm_dataportal']['feature_tree'] = array(
649
    '#type' => 'fieldset',
650
    '#title' => t('Feature Tree'),
651
    '#collapsible' => TRUE,
652
    '#collapsed' => TRUE,
653
	);
654

    
655
	$form['cdm_dataportal']['feature_tree']['cdm_dataportal_featuretree_uuid'] = array(
656
    '#type' => 'select',
657
    '#title'         => t('Available FeatureTrees'),
658
    '#default_value' => variable_get('cdm_dataportal_featuretree_uuid', UUID_DEFAULT_FEATURETREE),
659
	  '#options' => _get_feature_trees(),
660
    '#description'   => t('Select a FeatureTree for this webservice.'
661
    //.' If there is no applicable FeatureTree you can create a new one using the <a href="">FeatureTreeManager</a>'
662
	)
663
	);
664

    
665
	return system_settings_form($form);
666
}
667

    
668
/**
669
 * LAYOUT settings
670
 * @return unknown_type
671
 */
672
function cdm_dataportal_settings_layout(){
673

    
674
	$form = array();
675

    
676

    
677
	$form['cdm_taxonname_type'] = array(
678
    '#type' => 'select',
679
    '#title'         => t('Taxon name type'),
680
    '#default_value' => variable_get('cdm_taxonname_type', 'BotanicalName'),
681
    '#options' => array( 'BotanicalName'=>t('BotanicalName'), 'ZoologicalName'=>t('ZoologicalName')),
682
    '#description'   => t('')
683
	);
684

    
685
	$form['cdm_dataportal_nomref_in_title'] = array(
686
    '#type' => 'checkbox',
687
    '#title' => t('Show full nomenclatural reference in title'),
688
    '#default_value' => variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE),
689
    '#description' => t('Uncheck this, if you want the title of a taxon page consisting of name and author plus publication year only.
690
                        If not checked, the first homotypic taxon is a repetition of the accepted taxon with
691
                        the full nomenclatural reference.')
692
	);
693

    
694
	$form['cdm_dataportal_display_name_relations'] = array(
695
    '#type' => 'checkbox',
696
    '#title' => t('Show name relations of accepted taxa on taxon page'),
697
    '#default_value' => variable_get('cdm_dataportal_display_name_relations', 1),
698
    '#description' => t('')
699
	);
700

    
701
	$form['cdm_dataportal_display_is_accepted_for'] = array(
702
    '#type' => 'checkbox',
703
    '#title' => t('Display <em>is accepted for ...</em> on taxon pages when coming from a synonym link.'),
704
    '#default_value' => variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR),
705
    '#description' => t('')
706
	);
707

    
708
	$form['cdm_dataportal_name_relations_skiptype_basionym'] = array(
709
    '#type' => 'checkbox',
710
    '#title' => t('Exclude the basionym relationship type from the taxon page'),
711
    '#default_value' => variable_get('cdm_dataportal_name_relations_skiptype_basionym', 1),
712
    '#description' => t('')
713
	);
714

    
715
	$form['cdm_dataportal_taxonpage_tabs'] = array(
716
    '#type' => 'checkbox',
717
    '#title' => t('Tabbed taxon page'),
718
    '#default_value' => variable_get('cdm_dataportal_taxonpage_tabs', 1),
719
    '#description' => t('Split the taxon page into individual tabs for description, images, synonymy')
720
	);
721

    
722
	$form['cdm_dataportal_search_items_on_page'] = array(
723
    '#type' => 'textfield',
724
    '#title' => t('Search Page Size'),
725
    '#default_value' => variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE),
726
    '#description' => t('Number of Names to display per page in search results.')
727
	);
728

    
729
	$form['cdm_dataportal_descriptions_separated'] = array(
730
    '#type' => 'checkbox',
731
    '#title' => t('Separate Descriptions'),
732
    '#default_value' => variable_get('cdm_dataportal_descriptions_separated', 0),
733
    '#description' => t('By default corresponding elements of different descriptions are joined together'
734
    .' into a common section per feature (i.e. type of description).'
735
    .' Check this box to allow displaying all descriptions separately.')
736
    );
737

    
738
    //---- IMAGES ----//
739
    $form['images'] = array(
740
    '#type' => 'fieldset',
741
    '#title' => t('Images'),
742
    '#collapsible' => TRUE,
743
    '#collapsed' => FALSE,
744
    );
745
    $options = cdm_rankVocabulary_as_option();
746
    array_unshift($options, '-- DISABLED --');
747
    $form['images']['image_hide_rank'] =  array(
748
    '#type'          => 'select',
749
    '#title'         => t('Hide Images for Taxa above'),
750
    '#default_value' => variable_get('image_hide_rank', '0'),
751
    '#options'       => $options,
752
    '#description'   => t(''),
753
    );
754
    //show media
755
    $selectShowMedia = array(0 => "Show only taxon media",
756
    1 => "Show taxon and child taxon media");
757
    $form['images']['cdm_dataportal_show_media'] = array(
758
    '#type' => 'select',
759
    '#title' => t('Available media files'),
760
    '#default_value' => variable_get('cdm_dataportal_show_media', false),
761
    '#options' => $selectShowMedia,
762
    '#description'   => t('Select if a taxon should show only his media or also child media.')
763
    );
764
/*
765
    // --- SEARCH TAXA GALLERY ---- //
766
    $form['findtaxa'] = array(
767
    '#type' => 'fieldset',
768
    '#title' => t('Search Taxa'),
769
    '#collapsible' => TRUE,
770
    '#collapsed' => FALSE,
771
    );
772

    
773
    //variable_get('', 1);
774
    $form['findtaxa']['cdm_dataportal_findtaxa_show_taxon_thumbnails'] = array(
775
    '#type' => 'checkbox',
776
    '#title' => t('Show media thumbnails for accepted taxa'),
777
    '#default_value' => variable_get('cdm_dataportal_findtaxa_show_taxon_thumbnails', 1),
778
    '#description' => t('')
779
    );
780

    
781
    $form['findtaxa']['cdm_dataportal_findtaxa_show_synonym_thumbnails'] = array(
782
    '#type' => 'checkbox',
783
    '#title' => t('Show media thumbnails for synonyms'),
784
    '#default_value' => variable_get('cdm_dataportal_findtaxa_show_synonym_thumbnails', 0),
785
    '#description' => t('')
786
    );
787

    
788
    //   $showCaption = variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 0);
789
    $form['findtaxa']['cdm_dataportal_findtaxa_show_thumbnail_captions'] = array(
790
    '#type' => 'checkbox',
791
    '#title' => t('Show captions under thumbnails'),
792
    '#default_value' => variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 1),
793
    '#description' => t('')
794
    );
795

    
796
    $form['findtaxa']['cdm_dataportal_findtaxa_media_maxextend'] = array(
797
    '#type' => 'textfield',
798
    '#title' => t('Maximum extend of Images'),
799
    '#default_value' => variable_get('cdm_dataportal_findtaxa_media_maxextend', 120),
800
    '#description' => t('')
801
    );
802

    
803
    $form['findtaxa']['cdm_dataportal_findtaxa_media_cols'] = array(
804
    '#type' => 'textfield',
805
    '#title' => t('Number of columns'),
806
    '#default_value' => variable_get('cdm_dataportal_findtaxa_media_cols', 3),
807
    '#description' => t('')
808
    );
809

    
810
    $form['findtaxa']['cdm_dataportal_findtaxa_media_maxRows'] = array(
811
    '#type' => 'textfield',
812
    '#title' => t('Maximum number of rows'),
813
    '#default_value' => variable_get('cdm_dataportal_findtaxa_media_maxRows', 1),
814
    '#description' => t('')
815
    );
816
*/
817
    // --- SEARCH TAXA GALLERY ---- //
818
    $collapsed = TRUE;
819
    $form_name = CDM_DATAPORTAL_SEARCH_GALLERY_FORM_NAME;
820
    $form_tittle = 'Search Taxa';
821
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
822

    
823
    // --- FEATURE DESCRIPTION GALLERY ---- //
824
    $form_name = CDM_DATAPORTAL_DESCRIPTION_GALLERY_FORM_NAME;
825
    $form_tittle = 'Description elements gallery';
826
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
827

    
828
    // --- MEDIA GALLERY ---- //
829
    $form_name = CDM_DATAPORTAL_MEDIA_GALLERY_FORM_NAME;
830
    $form_tittle = 'Media gallery';
831
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
832

    
833
    return system_settings_form($form);
834
}
835

    
836
function cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed){
837

    
838
	$form[$form_name] = array(
839
    '#type' => 'fieldset',
840
    '#title' => t($form_tittle),
841
    '#collapsible' => TRUE,
842
    '#collapsed' => $collapsed,
843
    '#tree' => true,
844
	);
845

    
846
	$default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
847
	$gallery_settings = variable_get($form_name, $default_values);
848
	//variable_get('', 1);
849
	$form[$form_name]['cdm_dataportal_show_taxon_thumbnails'] = array(
850
    '#type' => 'checkbox',
851
    '#title' => t('Show media thumbnails for accepted taxa'),
852
    '#default_value' => $gallery_settings['cdm_dataportal_show_taxon_thumbnails'],
853
    '#description' => t('')
854
	);
855

    
856
	$form[$form_name]['cdm_dataportal_show_synonym_thumbnails'] = array(
857
    '#type' => 'checkbox',
858
    '#title' => t('Show media thumbnails for synonyms'),
859
    '#default_value' => $gallery_settings['cdm_dataportal_show_synonym_thumbnails'],
860
    '#description' => t('')
861
	);
862

    
863
	//$showCaption = variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 0);
864
	$form[$form_name]['cdm_dataportal_show_thumbnail_captions'] = array(
865
    '#type' => 'checkbox',
866
    '#title' => t('Show captions under thumbnails'),
867
    '#default_value' => $gallery_settings['cdm_dataportal_show_thumbnail_captions'],
868
    '#description' => t('')
869
	);
870

    
871
	$form[$form_name]['cdm_dataportal_media_maxextend'] = array(
872
    '#type' => 'textfield',
873
    '#title' => t('Maximum extend of Images'),
874
    '#default_value' => $gallery_settings['cdm_dataportal_media_maxextend'],
875
    '#description' => t('')
876
	);
877

    
878
	$form[$form_name]['cdm_dataportal_media_cols'] = array(
879
    '#type' => 'textfield',
880
    '#title' => t('Number of columns'),
881
    '#default_value' => $gallery_settings['cdm_dataportal_media_cols'],
882
    '#description' => t('')
883
	);
884

    
885
	$form[$form_name]['cdm_dataportal_media_maxRows'] = array(
886
    '#type' => 'textfield',
887
    '#title' => t('Maximum number of rows'),
888
    '#default_value' => $gallery_settings['cdm_dataportal_media_maxRows'],
889
    '#description' => t('')
890
	);
891

    
892
	return $form;
893
}
894

    
895
/**
896
 * GEOSERVICE and Map settings
897
 * @return unknown_type
898
 */
899
function cdm_dataportal_settings_geo(){
900

    
901
	$form = array();
902

    
903
	$form['cdm_dataportal_map_openlayers'] = array(
904
    '#type' => 'checkbox',
905
    '#title' => t('OpenLayers Viewer'),
906
    '#default_value' => variable_get('cdm_dataportal_map_openlayers', 1),
907
    '#description' => t('Display the maps in an interactive viewer which allows zooming and panning.')
908
	);
909

    
910
	$form['cdm_dataportal_geoservice_access_point'] = array(
911
    '#type' => 'textfield',
912
    '#title' => t('Geoservice Access Point'),
913
    '#default_value' => variable_get('cdm_dataportal_geoservice_access_point', ''),
914
    '#description' => t('Base URL of the geoservice to be used by this portal')
915
	);
916

    
917
	$form['cdm_dataportal_geoservice_display_width'] = array(
918
    '#type' => 'textfield',
919
    '#title' => t('Geoservice Display Width'),
920
    '#default_value' => variable_get('cdm_dataportal_geoservice_display_width', 390),
921
    '#description' => t('Width of the image generated by geoservice')
922
	);
923

    
924
	$form['cdm_dataportal_geoservice_bounding_box'] = array(
925
    '#type' => 'textfield',
926
    '#title' => t('Fixed Geoservice Bounding Box'),
927
    '#default_value' => variable_get('cdm_dataportal_geoservice_bounding_box', '-180,-90,180,90'),
928
    '#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.')
929
	);
930

    
931
	//cdm_dataportal_geoservice_labels_on
932
	$form['cdm_dataportal_geoservice_labels_on'] = array(
933
    '#type' => 'checkbox',
934
    '#title' => t('Display Country Labels'),
935
    '#default_value' => variable_get('cdm_dataportal_geoservice_labels_on', FALSE),
936
    '#description' => t('Check this if you like country names to be displayed in the maps. ')
937
	);
938

    
939
	//cdm_dataportal_geoservice_legend_on
940
	$form['cdm_dataportal_geoservice_map_caption'] = array(
941
    '#type' => 'textfield',
942
    '#title' => t('Map Caption'),
943
    '#default_value' => variable_get('cdm_dataportal_geoservice_map_caption', ''),
944
    '#description' => t('Define a caption for the map.')
945
	);
946

    
947
	/*
948
	 //cdm_dataportal_geoservice_legend_on
949
	 $form['cdm_dataportal_geoservice_legend_on'] = array(
950
	 '#type' => 'checkbox',
951
	 '#title' => t('Display a map legend'),
952
	 '#default_value' => variable_get('cdm_dataportal_geoservice_legend_on', TRUE),
953
	 '#description' => t('Check this if you like a legend to be displayed with the maps. ')
954
	 );
955
	 */
956

    
957
	//cdm_dataportal_geoservice_map_legend
958
	$form['cdm_dataportal_geoservice_map_legend'] = array(
959
      '#type' => 'fieldset',
960
      '#title' => t('Map Legend'),
961
      '#collapsible' => FALSE,
962
      '#collapsed' => TRUE,
963
	);
964

    
965
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_on'] = array(
966
    '#type' => 'checkbox',
967
    '#title' => t('Display a map legend'),
968
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_on', TRUE),
969
    '#description' => t('Check this if you like a legend to be displayed with the maps. ')
970
	);
971

    
972
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_size'] = array(
973
    '#type' => 'textfield',
974
    '#title' => t('Font size'),
975
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_size', 10),
976
    '#description' => t('Font size in pixels.')
977
	);
978

    
979
	$fontStyles = array(0 => "plane", 1 => "italic");
980
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_style'] = array(
981
    '#type' => 'select',
982
    '#title' => t('Available font styles'),
983
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_style', false),
984
    '#options' => $fontStyles,
985
    '#description'   => t('Select a font style for the map legend.')
986
	);
987

    
988
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_width'] = array(
989
    '#type' => 'textfield',
990
    '#title' => t('Icon width'),
991
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_width', 35),
992
    '#description' => t('Icon width in pixels.')
993
	);
994

    
995
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_height'] = array(
996
    '#type' => 'textfield',
997
    '#title' => t('Icon height'),
998
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_height', 15),
999
    '#description' => t('Icon height in pixels.')
1000
	);
1001

    
1002
	/* variable_get('cdm_dataportal_findtaxa_media_maxextend', 120),
1003
	 *  $form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
1004
	 '#type' => 'select',
1005
	 '#title'         => t('Available taxonomic views'),
1006
	 '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
1007
	 '#options' => $taxonomicTreeOptions,
1008
	 '#description'   => t('Select a taxonomic view for this webservice.')
1009
	 );
1010

    
1011
	 $taxonTrees = cdm_ws_get(CDM_WS_TAXONOMY);
1012
	 foreach($taxonTrees as $tree){
1013
	 $taxonomicTreeOptions[$tree->uuid] = $tree->titleCache;
1014
	 }
1015
	 $form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
1016
	 '#type' => 'select',
1017
	 '#title'         => t('Available taxonomic views'),
1018
	 '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
1019
	 '#options' => $taxonomicTreeOptions,
1020
	 '#description'   => t('Select a taxonomic view for this webservice.')
1021
	 );
1022
	 */
1023

    
1024
	return system_settings_form($form);
1025

    
1026
}
1027

    
1028

    
1029
/**
1030
 * @return walk and cache all taxon pages
1031
 */
1032
function cdm_dataportal_view_cache_site(){
1033

    
1034
	_add_js_progressbar();
1035

    
1036
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/cache_all_taxa.js');
1037

    
1038
	$out = '';
1039

    
1040
	$request_params = array();
1041
	$request_params['query'] = '%';
1042
	$request_params['tree'] = variable_get('cdm_taxonomictree_uuid', false);
1043
	$request_params['doTaxa'] = 1;
1044
	$request_params['doSynonyms'] = 0;
1045
	$request_params['doTaxaByCommonNames'] = 0;
1046
	$search_url = cdm_compose_url(CDM_WS_FIND_TAXA, null, queryString($request_params));
1047
	$search_url = uri_uriByProxy($search_url);
1048
	$taxon_page_url = url('cdm_dataportal/taxon/');
1049

    
1050
	$out .= '<form id="cache_site">';
1051
	$out .= '<br /><h4>'
1052
	.t('Cache all taxon pages')
1053
	.'</h4>';
1054
	$out .= '<div>'.t('This caching process may take long time and could cause heavy load on your server').'</div>';
1055
	$out .= '<div id="progress"></div>';
1056
	$out .= '<input type="hidden" name="searchTaxaUrl" value="'.$search_url.'"/>';
1057
	$out .= '<input type="hidden" name="taxonPageUrl" value="'.$taxon_page_url.'"/>';
1058
	$out .= '<input type="button" name="start" value="'.t('Start').'"/>';
1059
	$out .= '<input type="button" name="stop" value="'.t('Stop').'"/>';
1060
	$out .= '</form>';
1061
	$out .= '</div>';
1062
	//  foreach($taxonPager->records as $taxon){
1063
	//    cdm_dataportal_taxon_view($uuid);
1064
	//  }
1065

    
1066
	return $out;
1067
}
1068

    
1069
/**
1070
 * Implementation of hook_elements().
1071
 */
1072
function cdm_dataportal_elements() {
1073
	$type['select_secuuid'] = array(
1074
    '#input' => TRUE,
1075
    '#process' => array('cdm_dataportal_select_secuuid_expand' => array()),
1076
	//'#validate' => array('cdm_dataportal_select_secuuid_validate' => array()),
1077
	//'#default_value' => array(),
1078
	);
1079
	return $type;
1080
}
1081

    
1082
/*
1083
 *
1084
 */
1085
function cdm_dataportal_select_secuuid_expand($element){
1086

    
1087
	$element['#tree'] = FALSE;
1088

    
1089
	// any value submitted?
1090
	if(isset($element['#post'][$element['#varname']])){
1091
		$selected_values = $element['#post'][$element['#varname']];
1092
	} else {
1093
		// use those store in drupal
1094
		$selected_values = variable_get( $element['#varname'], array());
1095
	}
1096
	if(!is_array($selected_values)){
1097
		$selected_values = array($selected_values);
1098
	}
1099
	$options = array();
1100
	foreach($selected_values as $secUuid){
1101
		$options[$secUuid] = cdm_taxontree_secRefTitle_for($secUuid);
1102
	}
1103

    
1104
	$element[$element['#varname']] =  array(
1105
    '#type' => 'select',
1106
    '#options' => $options,
1107
    '#default_value' => array_values($options),
1108
    '#size' => $element['#multiple'] ? 12 : 2,
1109
    '#multiple' => $element['#multiple'],
1110
	);
1111
	return $element;
1112
}
1113

    
1114

    
1115

    
1116
/**
1117
 * Displays a list of the known taxonomic names. Long lists are split up into multiple pages
1118
 *
1119
 * TODO: parameters are still preliminar
1120
 * @param String $page page number to diplay defaults to page 1
1121
 * @param boolean $hide_unaccepted whether to hide nams which are not accepted by the current view
1122
 */
1123
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = false ){
1124

    
1125
	$request_params  = array(
1126
    'q' => $beginsWith,
1127
	//'sec' = '',
1128
	//'higherTaxa' => getFiters(),
1129
	// 'matchAnywhere' => false, // default is false
1130
    'page' => $page,
1131
    'onlyAccepted' => $onlyAccepted,
1132
    'pagesize' => 20  /*$_SESSION['cdm']['namelist_pagesize'] */);
1133

    
1134
	$taxonPager = cdm_ws_find(CDM_WS_FIND_TAXA, $request_params);
1135
	/*
1136
	 * FIXME the filter for accepted names will be a form element, thus this widget
1137
	 * should be generated via form api preferably as block
1138
	 */
1139
	//$out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
1140
	//$out .= theme('cdm_dataportal_widget_names_list', $names, $page);
1141
	$out .= theme('cdm_listof_taxa', $taxonPager);
1142
	return $out;
1143
}
1144

    
1145
function cdm_dataportal_view_reference($uuid, $arg2 = null){
1146
	$reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
1147
	return theme('cdm_reference_page', $reference);
1148
}
1149

    
1150
function cdm_dataportal_view_reference_list($pageNumber){
1151
	$referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE), $pageNumber);
1152
	return theme('cdm_reference_pager', $referencePager, 'cdm_dataportal/reference/list/');
1153
}
1154

    
1155
function cdm_dataportal_view_media($mediaUuid, $mediarepresentation_uuid = false, $part = 0){
1156
	$media = cdm_ws_get(CDM_WS_MEDIA, $mediaUuid);
1157
	return theme('cdm_media_page', $media, $mediarepresentation_uuid, $part);
1158
}
1159

    
1160
/**
1161
 * The taxon page gives detailed information on a taxon, it shows:
1162
 *  - Taxon name
1163
 *  - Full list of synonyms homotypic synonyms on top, followed by the
1164
 *    heterotypic and finally followed by misapplied names.
1165
 *    The list is ordered historically.
1166
 *  - All description associated with the taxon.
1167
 *
1168
 * @param $uuid
1169
 * @param $chapter name of the part to display,
1170
 *         valid values are: 'description', 'images', 'synonymy', 'all'
1171
 * @return unknown_type
1172
 */
1173
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all'){
1174

    
1175
	// display the page for the taxon defined by $uuid
1176
	$taxonpage = cdm_dataportal_taxon_view($uuid, $chapter);
1177
	return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title , $taxonpage->content);
1178
}
1179

    
1180
/**
1181
 * @param $uuid
1182
 * @param $chapter name of the part to display,
1183
 *         valid values are: 'description', 'images', 'synonymy', 'all'
1184
 * @return unknown_type
1185
 */
1186
function cdm_dataportal_taxon_view($uuid, $chapter = 'all'){
1187

    
1188
	$taxon = cdm_ws_get(CDM_WS_TAXON, $uuid);
1189
	if(!$taxon){
1190
		drupal_set_title(t('Taxon does not exist'));
1191
		return false;
1192
	}
1193
	_cdm_dataportal_set_currentSecUuid($taxon->sec->uuid);
1194

    
1195
	$taxonpage->title = theme('cdm_taxon_page_title', $taxon);
1196
	$taxonpage->content = theme('cdm_taxon_page_general', $taxon, $chapter);
1197

    
1198
	return $taxonpage;
1199
}
1200

    
1201

    
1202
function cdm_dataportal_view_search_advanced(){
1203

    
1204
	drupal_set_title(t('Advanced Search'));
1205

    
1206
	$searchForm = cdm_dataportal_search_taxon_form(true);
1207

    
1208
	return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
1209

    
1210
}
1211

    
1212
/**
1213
 *
1214
 * future extensions to meet palmweb mockup requirements:
1215
 *  - habitat
1216
 *  - uses
1217
 *  - conservation status
1218
 *  - locality / tdwg region
1219
 */
1220
function cdm_dataportal_view_search_taxon(){
1221

    
1222
	$_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
1223

    
1224
	$request_params = cdm_dataportal_search_form_request();
1225
	$taxonPager = cdm_ws_get(CDM_WS_FIND_TAXA, null, queryString($request_params));
1226

    
1227
	$search_params = $_REQUEST;
1228
	unset($search_params['q']);
1229
	return theme('cdm_search_results', $taxonPager, 'cdm_dataportal/search/taxon', $search_params);
1230
}
1231

    
1232
function cdm_view_xml2json(){
1233
	$file = arg(2);
1234
	$datastr = get_content(variable_get('cdm_webservice_url', '').$file);
1235
	return  xml2json::transformXmlStringToJson($datastr);
1236
}
1237

    
1238
/* ====================== other functions ====================== */
1239

    
1240
/**
1241
 * Enter description here...
1242
 *
1243
 * @param String $uuid the UUID of the taxon
1244
 * @return the URL
1245
 */
1246
function path_to_taxon($uuid){
1247
	if(!$uuid) return false;
1248
	return 'cdm_dataportal/taxon/'.$uuid;
1249
}
1250

    
1251
function path_to_reference($uuid){
1252
	if(!$uuid) return false;
1253
	return 'cdm_dataportal/reference/'.$uuid;
1254
}
1255

    
1256
function path_to_media($uuid, $representaion_uuid = false, $partId = false){
1257
	if(!$uuid) return false;
1258
	$out = 'cdm_dataportal/media/'.$uuid;
1259
	if($representaion_uuid){
1260
		$out .= '/'.$representaion_uuid;
1261
		if($partId !== false){
1262
			$out .= '/'.$partId;
1263
		}
1264
	}
1265
	return $out;
1266
}
1267

    
1268
/**
1269
 * Compares thisRank with thatRank.
1270
 * Returns a negative integer, zero, or a positive integer
1271
 * as the of thisRank is higher than, equal to, or lower than thatRank.
1272
 * e.g:
1273
 * <ul>
1274
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
1275
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
1276
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
1277
 * </ul>
1278
 * <p>
1279
 * This compare logic of the underlying webservice is the
1280
 * <b>inverse logic</b> of the the one implemented in
1281
 * java.lang.Comparable#compareTo(java.lang.Object)
1282
 * @param $thisRankUuid
1283
 * @param $thatRankUuid
1284
 * @return  a negative integer, zero, or a positive integer
1285
 * as the thisRank is lower than, equal to, or higher than thatRank
1286
 */
1287
function rank_compare($thisRankUuid, $thatRankUuid){
1288
	$result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
1289
	return $result->Integer;
1290
}
1291

    
1292
function uri_to_synonym($synonymUuid, $acceptedUuid, $pagePart = null){
1293
	$acceptedPath = path_to_taxon($acceptedUuid);
1294
	return url($acceptedPath.($pagePart ? '/'.$pagePart : ''), 'highlite='.$synonymUuid.'&acceptedFor='.$synonymUuid);
1295
}
1296

    
1297
/**
1298
 * Creates a short taxonname by using the taggename field of NameSTO or NameTO instances.
1299
 * If the taggename if empty the fullname will be returned.
1300
 *
1301
 * @param unknown_type $Name or TreeNode
1302
 * @return string
1303
 */
1304
function cdm_dataportal_shortname_of($name){
1305

    
1306
	$nameStr = '';
1307
	// get all tagged text tokens of the scientific name
1308
	foreach($name->taggedTitle as $tagtxt){
1309
		if($tagtxt->type == 'name' || $tagtxt->type == 'rank'){
1310
			$nameStr .= ($nameStr ? ' ' : '').$tagtxt->text;
1311
		}
1312
	}
1313
	$nameStr = trim($nameStr);
1314
	if($nameStr){
1315
		// do not return short names for these
1316
		if($nameStr == 'Incertae sedis' || $nameStr ==  'Nomina excludenda'){
1317
			return $nameStr;
1318
		}
1319
		if($pos = stripos($nameStr, ' ')){
1320
			return substr($nameStr, 0, 1).'. '.substr($nameStr, $pos);
1321
		} else {
1322
			return $nameStr;
1323
		}
1324
	} else {
1325
		return $name->titleCache;
1326
	}
1327
}
1328

    
1329
/**
1330
 * TODO Add Comments. I don't get what
1331
 *
1332
 * @param UUID $secUuid
1333
 */
1334
function _cdm_dataportal_set_currentSecUuid($secUuid){
1335

    
1336
	// do not save in session but in database
1337

    
1338
	if(is_array($secUuid)){
1339
		$secUuid = $secUuid[0];
1340
	}
1341
	if(!$secUuid){
1342
		variable_set('cdm_currentSecRef', null);
1343
	} else {
1344
		$secRef = cdm_ws_get(CDM_WS_REFERENCE, $secUuid);
1345
		if(isset($secRef)){
1346
			variable_set('cdm_currentSecRef', (array)($secRef));
1347
		}
1348
	}
1349

    
1350
}
1351

    
1352
/**
1353
 * TODO
1354
 * This will not work with multiple instances of the dataportal running
1355
 * under the same host as there is only one session for all instances.
1356
 * In case you switch instances with the same client, you will definitely
1357
 * run into trouble.
1358
 * Also this is not handling multiple secs.
1359
 *
1360
 *
1361
 * returns the current secRef array.
1362
 * If the according session variable is not jet set the default
1363
 * as configured in the setting is used otherwise null.
1364
 *
1365
 * currentSecRef['uuid']
1366
 * currentSecRef[....
1367
 *
1368
 * @return array
1369
 */
1370
function _cdm_dataportal_currentSecRef_array(){
1371

    
1372
	// do not look in session but in database
1373
	if( variable_get('cdm_currentSecRef', null) == null){
1374
		$secUuid = variable_get('cdm_secUuid_default', null);
1375
		_cdm_dataportal_set_currentSecUuid($secUuid);
1376
	}
1377
	return variable_get('cdm_currentSecRef', null);
1378

    
1379
}
1380

    
1381
/**
1382
 * Check if a taxon is accepted by the current taxonomic tree
1383
 *
1384
 * @param Taxon $taxon
1385
 * @return true if $taxon is accepted, false otherwise
1386
 */
1387

    
1388
function _cdm_dataportal_acceptedByCurrentView($taxon){
1389

    
1390
	//$current_secref = _cdm_dataportal_currentSecRef_array();
1391
	$defaultTreeUuid = variable_get('cdm_taxonomictree_uuid', false);
1392
	if($taxon->class == "Taxon" && isset($taxon->taxonNodes)){
1393
		foreach($taxon->taxonNodes as $node){
1394
			if($node->taxonomicTree == $defaultTreeUuid) {
1395
				return true;
1396
			}
1397
		}
1398
	}
1399
	return false;
1400
}
1401

    
1402
/**@Deprecated
1403
 *
1404
 */
1405
function compose_url_prameterstr($parameters = array(), $parentPropertyName = false){
1406
	$pstr = '';
1407
	foreach($parameters as $key=>$value){
1408
		if(is_array($value)){
1409

    
1410
		} else {
1411
			$pstr .= ($pstr ? '&' :'').$key.'='.urlencode($value);
1412
		}
1413
	}
1414
	return $pstr;
1415
}
1416

    
1417
function _get_feature_trees(){
1418
	$feature_trees = array();
1419

    
1420
	// set tree that contains all features
1421
	$feature_trees[UUID_DEFAULT_FEATURETREE] = t('Default Featuretree (contains all features)');
1422

    
1423
	// get features from database
1424
	$persisted_trees = cdm_ws_get(CDM_WS_FEATURETREES);
1425
	if(is_array($persisted_trees)){
1426
		foreach($persisted_trees as $featureTree){
1427

    
1428
			// do not add the DEFAULT_FEATURETREE again
1429
			if($featureTree->uuid == UUID_DEFAULT_FEATURETREE){
1430
				continue;
1431
			}
1432
			$featureLabels = array();
1433
			foreach ($featureTree->root->children as $featureNode){
1434
				$featureLabels[] = $featureNode->feature->representation_L10n;
1435
			}
1436

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

    
1439
		}
1440
	}
1441
	return $feature_trees;
1442
}
1443

    
1444

    
1445
function get_nameRenderTemplate($renderPath, $nameLink = NULL, $refenceLink = NULL){
1446
	//TODO implement admin user interface to replace swicth statement
1447
	//     preliminar solution: using themes
1448

    
1449
	$template = theme('get_nameRenderTemplate', $renderPath);
1450

    
1451
	if(!is_array($template)){
1452
		switch($renderPath){
1453
			case 'list_of_taxa':
1454
			case  'acceptedFor':
1455
			case 'taxon_page_synonymy':
1456
			case 'typedesignations':
1457
			case 'taxon_page_title':
1458
			case 'na': $template = array(
1459
          'namePart' => array('#uri'=>true),
1460
			//'authorshipPart' => true,
1461
			);
1462
			break;
1463
			case 'nar': $template = array(
1464
          'namePart' => array('#uri'=>true),
1465
			//'authorshipPart' => true,
1466
          'referencePart' => array('#uri'=>true),
1467
          'microreferencePart' => true,
1468
			);
1469
			break;
1470
			default: $template = array(
1471
          'namePart' => array('#uri'=>true),
1472
          'authorshipPart' => true,
1473
          'referencePart' => array('#uri'=>true),
1474
          'microreferencePart' => true,
1475
          'statusPart' => true,
1476
          'descriptionPart' => true
1477
			);
1478
		}
1479
	}
1480

    
1481
	if($nameLink && isset($template['nameAuthorPart']['#uri'])){
1482
		$template['nameAuthorPart']['#uri'] = $nameLink;
1483
	} else{
1484
		unset($template['nameAuthorPart']['#uri']);
1485
	}
1486

    
1487
	if($nameLink && isset($template['namePart']['#uri'])){
1488
		$template['namePart']['#uri'] = $nameLink;
1489
	} else{
1490
		unset($template['namePart']['#uri']);
1491
	}
1492

    
1493
	if($refenceLink && isset($template['referencePart']['#uri'])){
1494
		$template['referencePart']['#uri'] = $refenceLink;
1495
	}else{
1496
		unset($template['referencePart']['#uri']);
1497
	}
1498

    
1499
	return $template;
1500
}
1501

    
1502
function get_partDefinition($taxonNameType){
1503
	//TODO implement admin user interface to allow specify the partdefinitions for any type
1504
	//     preliminar solution: using themes
1505

    
1506
	$partdef = theme('get_partDefinition', $taxonNameType);
1507

    
1508
	if(!is_array($partdef)){
1509
		switch($taxonNameType){
1510
			case 'ZoologicalName': $partdef = array(
1511
        'namePart' => array(
1512
          'name' => true,
1513
			),
1514
        'referencePart' => array(
1515
          'authorTeam' => true
1516
			),
1517
        'microreferencePart' => array(
1518
          'microreference' => true,
1519
			),
1520
        'statusPart' => array(
1521
          'status' => true,
1522
			),
1523
        'descriptionPart' => array(
1524
          'description' => true,
1525
			),
1526
			);
1527
			break;
1528
			case 'BotanicalName': $partdef = array(
1529
        'namePart' => array(
1530
          'name' => true
1531
			),
1532
        'authorTeamPart' => array(
1533
          'authorTeam' => true,   
1534
			),
1535
        'referencePart' => array(
1536
          'reference' => true      
1537
			),
1538
        'microreferencePart' => array(
1539
          'microreference' => true,
1540
			),
1541
        'statusPart' => array(
1542
          'status' => true,
1543
			),
1544
        'descriptionPart' => array(
1545
          'description' => true,
1546
			),
1547
			);
1548
			break;
1549
			default: $partdef = array(
1550
        'namePart' => array(
1551
          'name' => true,
1552
			),
1553
        'authorTeamPart' => array(
1554
          'authorTeam' => true,   
1555
			),
1556
        'referencePart' => array(
1557
          'reference' => true      
1558
			),
1559
        'microreferencePart' => array(
1560
          'microreference' => true,
1561
			),
1562
        'statusPart' => array(
1563
          'status' => true,
1564
			),
1565
        'descriptionPart' => array(
1566
          'description' => true,
1567
			),
1568
			);
1569
		}
1570
	}
1571
	return $partdef;
1572
}
1573

    
1574
/**
1575
 * The function read the metadata info such title or artist of a media file. The
1576
 * function tries at first to get all the info from the file metadata and if it is
1577
 * not avaible look at the media file info stored at the database.
1578
 * @param $media The media file
1579
 * @return array The array with the avilable specified metadata info.
1580
 * TODO rename to read_media_metadata() and move to *.module
1581
 */
1582
function cdm_read_media_metadata($media){
1583

    
1584
	$metadata_caption = array('title' => '',           //media_metadata and media
1585
                              'artist' => '',          //media_metadata and media
1586
                              'rights',                //media_metadata and media
1587
                              'location',              //media_metadata
1588
                              'filename' => '',        //media
1589
                              'mediacreated' => '',    //media
1590
                              'description' => '');    //media
1591

    
1592
	//getting the media metadata
1593
	$media_metadata = cdm_ws_get(CDM_WS_MEDIA_METADATA, array($media->uuid));
1594

    
1595
	//title
1596
	//if ($media_metadata->Headline) {
1597
	if ($media_metadata->ObjectName) {
1598
		$metadata_caption['title'] = $media_metadata->ObjectName;
1599
		//	} else if ($media->titleCache){
1600
		//		$metadata_caption['title'] = $media_metadata->ObjectName;
1601
	} else if ($media->titleCache){
1602
		$metadata_caption['title'] = $media->titleCache;
1603
		if ($media->description_L10n)
1604
		$metadata_caption['title'] .= ' - ' . $media->description_L10n;
1605
	}else{ //if there is no title on the db and metadata get file title
1606
    $aux = $media->representations[0]->parts[0]->uri;
1607
    $aux = explode("/", $aux);
1608
    $aux = end($aux);
1609
		$metadata_caption['title'] = $aux;		
1610
	}
1611

    
1612
	//artist
1613
	if ($media_metadata->Artist){
1614
	 $metadata_caption['artist'] = ($media_metadata->Artist ? ''.$media_metadata->Artist : '');
1615
	}
1616
	elseif ($media->artist->titleCache){
1617
	 $metadata_caption['artist'] = $media->artist->titleCache;
1618
	}
1619
	
1620
	//copyright
1621
	$metadata_caption['rights'] = array('copyright' => array('agentNames' => array()),
1622
                                      'license' => array('agentNames' => array(), 'types' => array(), 'abbreviatedTexts' => array(), 'uris' => array()));
1623
	if ($media_metadata->Copyright)
1624
	$metadata_caption['rights']['copyright']['agentNames'][] = $media_metadata->Copyright;
1625
	elseif ($media->rights){
1626
		foreach($media->rights as $right){
1627
			switch($right->term->uuid){
1628
				case UUID_RIGHTS_LICENCE:
1629
					$metadata_caption['rights']['license']['agentNames'][] = ($right->agent ? ''.$right->agent->firstname.' '.$right->agent->lastname : '');
1630
					$metadata_caption['rights']['license']['types'][] = ($right->representation_L10n ? ''.$right->representation_L10n : '');
1631
					$metadata_caption['rights']['license']['abbreviatedTexts'][] = ($right->abbreviatedText ? ''.$right->abbreviatedText : '');
1632
					$metadata_caption['rights']['license']['uris'][] = ($right->uri ? ''.$right->uri : '');
1633
					break;
1634
				case UUID_RIGHTS_COPYRIGHT:
1635
					$metadata_caption['rights']['copyright']['agentNames'][] = $right->agent->firstname . ' ' . $right->agent->lastname;
1636
					break;
1637
			}
1638
		}
1639
	}
1640
	else
1641
	$metadata_caption['rights']['agentNames'][] = '';
1642

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

    
1646
	//location
1647
	$metadata_caption['location'] = array();
1648
	$metadata_caption['location']['sublocation'] = $media_metadata->Sublocation;
1649
	$metadata_caption['location']['city'] = $media_metadata->City;
1650
	$metadata_caption['location']['province'] = $media_metadata->Province;
1651
	$metadata_caption['location']['country'] = $media_metadata->Country;
1652

    
1653
	//filename
1654
	if(isset($media->representations[0]->parts[0]->uri)){
1655
		$fileUri = $media->representations[0]->parts[0]->uri;
1656
		$filename = substr($fileUri, strrpos($fileUri, "/")+1);
1657
		$metadata_caption['filename'] = $filename;
1658
	}
1659
	else{
1660
	 $metadata_caption['filename'] = '';
1661
	}
1662
	/*
1663
	 //creation date
1664
	 if($media_metadata["Modify Date"])
1665
	 $metadata_caption['mediacreated'] = $media_metadata["Modify Date"];
1666
	 else
1667
	 $metadata_caption['mediacreated'] = $media->created;
1668
	 */
1669
	//returned value
1670
	return $metadata_caption;
1671
}
(5-5/10)