Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / modules / cdm_dataportal / cdm_dataportal.module @ 806baeb2

History | View | Annotate | Download (54.2 KB)

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

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

    
34
/* gallery variables */
35
$gallery_settings = array(
36
    "cdm_dataportal_show_taxon_thumbnails" => 1,
37
    "cdm_dataportal_show_synonym_thumbnails" => 0,
38
    "cdm_dataportal_show_thumbnail_captions" => 1,
39
    "cdm_dataportal_media_maxextend" => 120,
40
    "cdm_dataportal_media_cols" => 3,
41
    "cdm_dataportal_media_maxRows" => 0);
42
define('CDM_DATAPORTAL_GALLERY_SETTINGS', serialize($gallery_settings));
43
define('CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME', "description_gallery");
44
define('CDM_DATAPORTAL_MEDIA_GALLERY_NAME', "media_gallery");
45
define('CDM_DATAPORTAL_SEARCH_GALLERY_NAME', "search_gallery");
46

    
47

    
48

    
49

    
50
function getGallerySettings($gallery_config_form_name){
51
	$default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
52
  	return variable_get($gallery_config_form_name, $default_values);
53
}
54

    
55

    
56

    
57
function _add_js_progressbar(){
58
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/jquery.progressbar/js/jquery.progressbar.js');
59
}
60

    
61

    
62
/* ====================== hook implementations ====================== */
63

    
64
/**
65
 * Implementation of hook_help()
66
 *
67
 * Display help and module information
68
 * @param section which section of the site we're displaying help
69
 * @return help text for section
70
 */
71
function cdm_dataportal_help($section='') {
72

    
73
	$out = '';
74
	switch ($section) {
75
		case "admin/modules#description":
76
			$out = t("The dataportal publishes CDM data hosted in a CommunityStore on the web.");
77
			break;
78
	}
79
	return $out;
80
}
81

    
82

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

    
97

    
98
/**
99
 * Implementation of hook_menu()
100
 */
101
function cdm_dataportal_menu($may_cache) {
102
	$items = array();
103

    
104
	if ($may_cache) {
105

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

    
116
		$items[] = array(
117
      'path' => 'admin/settings/cdm_dataportal/general',
118
      'title' => t('General'),
119
      'description' => t('Setting for the CDM DataPortal'),
120
      'access' => user_access('administer cdm_dataportal'),
121
      'callback' => 'drupal_get_form',
122
      'callback arguments' => 'cdm_dataportal_settings',
123
      'type' => MENU_LOCAL_TASK,
124
		);
125

    
126
		$items[] = array(
127
      'path' => 'admin/settings/cdm_dataportal/geo',
128
      'title' => t('Geo & Map'),
129
      'description' => t('Geo & Map'),
130
      'access' => user_access('administer cdm_dataportal'),
131
      'callback' => 'drupal_get_form',
132
      'callback arguments' => 'cdm_dataportal_settings_geo',
133
      'type' => MENU_LOCAL_TASK,
134
		);
135

    
136
		$items[] = array(
137
      'path' => 'admin/settings/cdm_dataportal/layout',
138
      'title' => t('Layout'),
139
      'description' => t('Configure and adjust the layout of your DataPortal '),
140
      'access' => user_access('administer cdm_dataportal'),
141
      'callback' => 'drupal_get_form',
142
      'callback arguments' => 'cdm_dataportal_settings_layout',
143
      'type' => MENU_LOCAL_TASK,
144
		);
145

    
146
		$items[] = array(
147
      'path' => 'admin/settings/cdm_dataportal/cachesite',
148
      'title' => t('Cache Site'),
149
      'description' => t('Cache the whole CDM DataPortal site'),
150
      'access' => user_access('administer cdm_dataportal'),
151
      'callback' => 'cdm_dataportal_view_cache_site',
152
      'type' => MENU_LOCAL_TASK,
153
		);
154

    
155
		/*$items[] = array(
156
		 'path' => 'admin/settings/cdm_dataportal/ws',
157
		 'title' => t('Web Service'),
158
		 'description' => t('Setting for the CDM DataPortal'),
159
		 'access' => user_access('administer cdm_dataportal'),
160
		 'callback' => 'drupal_get_form',
161
		 'callback arguments' => 'cdm_dataportal_settings',
162
		 'weight' => 1,
163
		 'type' => MENU_DEFAULT_LOCAL_TASK,
164
		 );*/
165

    
166
		$items[] = array(
167
	    'path' => 'cdm_dataportal/names',
168
	    'callback' => 'cdm_dataportal_view_names',
169
	    'access' => true,
170
	    'type' => MENU_CALLBACK,
171
		);
172
		// optional callback arguments: page
173
			
174
		$items[] = array(
175
      'path' => 'cdm_dataportal/taxon',
176
      'callback' => 'cdm_dataportal_taxon_page_view',
177
      'access' => true,
178
      'type' => MENU_CALLBACK,
179
		// expected callback arguments: uuid
180
		);
181

    
182
		$items[] = array(
183
      'path' => 'cdm_dataportal/reference',
184
      'callback' => 'cdm_dataportal_view_reference',
185
      'access' => true,
186
      'type' => MENU_CALLBACK,
187
		// expected callback arguments: uuid
188
		);
189

    
190
		$items[] = array(
191
      'path' => 'cdm_dataportal/reference/list',
192
      'callback' => 'cdm_dataportal_view_reference_list',
193
      'access' => true,
194
      'type' => MENU_CALLBACK,
195
		// expected callback arguments: uuid
196
		);
197

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

    
206
		$items[] = array(
207
      'path' => 'cdm_dataportal/search',
208
      'callback' => 'cdm_dataportal_view_search_advanced',
209
      'access' => true,
210
      'type' => MENU_CALLBACK,
211
		);
212

    
213
		$items[] = array(
214
      'path' => 'cdm_dataportal/search/taxon',
215
      'callback' => 'cdm_dataportal_view_search_taxon',
216
      'access' => true,
217
      'type' => MENU_CALLBACK,
218
		);
219

    
220
		$items[] = array(
221
      'path' => 'cdm/xml2json',
222
      'callback' => 'cdm_view_xml2json',
223
      'access' => true,
224
      'type' => MENU_CALLBACK,
225
		);
226

    
227

    
228
	} else {
229
		// may not cache
230
		// --- local tasks for Taxon
231
		if(variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
232

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

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

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

    
266
			$items[] = array(
267
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/synonymy',
268
      'title' => theme('cdm_taxonpage_tab', 'Synonymy'),
269
      'callback' => 'cdm_dataportal_taxon_page_view',
270
      'access' => true,
271
      'type' => MENU_LOCAL_TASK,
272
      'weight' => 3,
273
      'callback arguments' => array(arg(2), "synonymy")
274
			// expected callback arguments: name_uuid
275
			);
276

    
277
			$items[] = array(
278
      'path' => 'cdm_dataportal/taxon/'.arg(2).'/images',
279
      'title' => theme('cdm_taxonpage_tab', 'Images'),
280
      'callback' => 'cdm_dataportal_taxon_page_view',
281
      'access' => true,
282
      'type' => MENU_LOCAL_TASK,
283
      'weight' => 4,
284
      'callback arguments' => array(arg(2), "images")
285
			// expected callback arguments: name_uuid
286
			);
287
		}
288
	}
289

    
290
	drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal.css');
291
	//drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
292
	drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_screen.css', 'screen');
293

    
294
	return $items;
295

    
296
}
297

    
298
/**
299
 * Implementation of hook_block()
300
 *
301
 * Provides the following blocks:
302
 *  0: list of links useful during development
303
 *
304
 * @param String $op
305
 * @param int $delta
306
 */
307
function cdm_dataportal_block($op='list', $delta=0) {
308
	// listing of blocks, such as on the admin/block page
309
	if ($op == "list") {
310
		//$block[0]["info"] = t("CDM DataPortal DevLinks");
311
		$block[1]["info"] = t("CDM DataPortal Credits");
312
		$block[2]["info"] = t("CDM Search Taxa");
313
		//$block[3]["info"] = t("CDM Filters");
314
		$block[4]["info"] = t("CDM Dataportal Print");
315
		return $block;
316
	}
317
	else if ($op == 'view') {
318
		switch($delta){
319
			case 1:
320
				$block['subject'] = t('Credits');
321
				$block['content'] = theme('cdm_credits');
322
				return $block;
323
			case 2:
324
				$block['subject'] = t('Search Taxa');
325
				$block['content'] = drupal_get_form('cdm_dataportal_search_taxon_form');
326
				$block['content'] .= '<div>'.l('Advanced Search', '/cdm_dataportal/search').'</div>';
327
				return $block;
328
			case 4:
329
				$block['subject'] = t('Print this page');
330
				$block['content'] = theme('cdm_print_button');;
331
				return $block;
332
		}
333
	}
334
}
335

    
336

    
337
/**
338
 * Implementation of hook_validate()
339
 *
340
 * @param $element
341
 */
342
function cdm_dataportal_settings_validate($form_id, $form_values){
343

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

    
348
	$cdm_webservice_url_changed = variable_get('cdm_webservice_url', '') != $form_values['cdm_webservice_url'];
349

    
350
	$cdm_secUuid_default_changed = variable_get('cdm_secUuid_default', '') != $form_values['cdm_secUuid_default'];
351

    
352
	if ($cdm_webservice_url_changed || $cdm_secUuid_default_changed) {
353

    
354
		_cdm_dataportal_set_currentSecUuid($form_values['cdm_secUuid_default']);
355
		// reset all cdm related data stored in the session
356
		// cdm_dataportal_session_clear($form_values['cdm_webservice_url']); // cdm_webservice_url is not further stored here
357
		// clear the cdm webservice cache
358
		cache_clear_all(NULL, 'cache_cdm_ws');
359
		// better clear secref_cache since i can not be sure if the cache has not be used during this response
360
		cdm_api_secref_cache_clear();
361
	}
362
	if($form_values['cdm_webservice_cache'] != variable_get('cdm_webservice_cache', 1)){
363
		cache_clear_all(NULL, 'cache_cdm_ws');
364
		// better clear secref_cache since i can not be sure if the cache has not be used during this response
365
		cdm_api_secref_cache_clear();
366
	}
367

    
368
}
369

    
370
/*
371
 function cdm_dataportal_session_clear($cdm_ws_uri_update = false){
372
 $_SESSION['cdm'] = null;
373
 if(is_string($cdm_ws_uri_update)){
374
 $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
375
 }
376
 }
377

    
378
 function cdm_dataportal_session_validate(){
379

    
380
 if(!isset($_SESSION['cdm']['ws_uri'])){
381
 $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', false));
382
 } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', false)){
383
 cdm_dataportal_session_clear(variable_get('cdm_webservice_url', false));
384
 }
385
 }
386
 */
387

    
388
function cdm_dataportal_search_taxon_form($advancedForm = false){
389

    
390
	$preset_query = (isset($_SESSION['cdm']['search']['query']) ? $_SESSION['cdm']['search']['query'] : '');
391
	$preset_doTaxa = (isset($_SESSION['cdm']['search']['doTaxa']) ? 1 : 0);
392
	$preset_doSynonyms = (isset($_SESSION['cdm']['search']['doSynonyms']) ? 1 : 0);
393
	$preset_doTaxaByCommonNames = (isset($_SESSION['cdm']['search']['doTaxaByCommonNames']) ? 1 : 0);
394
	$tdwg_level_select =  (isset($_SESSION['cdm']['search']['tdwg_level_select']) ? $_SESSION['cdm']['search']['tdwg_level_select'] : 2);
395
	$selected_areas =  (isset($_SESSION['cdm']['search']['area']) ? $_SESSION['cdm']['search']['area'] : false);
396

    
397
	$url = 'cdm_dataportal/search/taxon';
398
	$form['#method'] = 'get';
399
	$form['#process'] = array('cdm_dataportal_search_process' => array());
400
	$form['#action'] = url($url, NULL, NULL, true);
401

    
402
	$form['query'] = array(
403
    '#delta' => 0,
404
    '#type' => 'textfield',
405
    '#size' => 20,
406
    '#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')),
407
    '#value' => $preset_query,
408
	);
409

    
410
	$form['search'] = array(
411
      '#delta' => 1,
412
      '#tree' => true,
413
	//'#type' => $advancedForm ? 'fieldset': 'hidden',
414
      '#title' => t('Options')
415
	);
416

    
417
	$form['search']['tree'] = array(
418
    '#delta' => -1,
419
    '#type' => 'hidden',
420
    '#value' => variable_get('cdm_taxonomictree_uuid', false)
421
	);
422

    
423

    
424
	// clean URL get forms breaks if we don't give it a 'q'.
425
	if (!(bool)variable_get('clean_url', '0')) {
426
		$form['search']['q'] = array(
427
      '#delta' => -1,
428
      '#type' => 'hidden',
429
      '#value' => $url,
430
      '#name' => 'q',
431
		);
432
	}
433

    
434
	$form['search']['pageSize'] = array(
435
      '#delta' => -1,
436
      '#type' => 'hidden',
437
      '#value' => variable_get('cdm_dataportal_search_items_on_page', 25)
438
	);
439

    
440
	if($advancedForm){
441
		// general search parameters
442
		$form['search']['doTaxa'] = array(
443
      '#delta' => 2,
444
      '#type' => 'checkbox',
445
      '#title' => t('Search for accepted taxa'),
446
      '#value' => $preset_doTaxa
447
		);
448
		$form['search']['doSynonyms'] = array(
449
      '#delta' => 3,
450
      '#type' => 'checkbox',
451
      '#title' => t('Search for synonyms'),
452
      '#value' => $preset_doSynonyms
453
		);
454
		$form['search']['doTaxaByCommonNames'] = array(
455
      '#delta' => 4,
456
      '#type' => 'checkbox',
457
      '#title' => t('Search for common names'),
458
      '#value' => $preset_doTaxaByCommonNames
459
		);
460
			
461
		// Geographic Range
462
		$form['search']['geographic_range'] = array(
463
      '#type' => 'fieldset',
464
      '#delta' => 5,
465
      '#tree' => true,
466
      '#title' => t('Geographic range'),
467
		);
468

    
469
		$form['search']['geographic_range']['tdwg_level_select'] = array(
470
    '#type' => 'radios',
471
    '#title' => t('Select a TDWG distribution level and code'),
472
    '#default_value' => $tdwg_level_select,
473
    '#options' => array(
474
		t('TDWG level-1, i.e. a continent'),
475
		t('TDWG level-2'),
476
		t('TDWG level-3, i.e. a country'),
477
		t('TDWG level-4')
478
		)
479
		);
480
		$tdwg[1] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '1');
481
		$tdwg[2] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '2');
482
		$tdwg[3] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '3');
483
		$tdwg[4] = cdm_ws_get(CDM_WS_TDWG_LEVEL, '4');
484

    
485
		$tdwg_js = '';
486
		foreach($tdwg as $key=>$tdwg_level){
487
			$tdwgOptions = array();
488
			$tdwgOptionsSelected = array();
489
			foreach($tdwg_level as $area){
490
				$representation = $area->representations[0];
491
				$tdwgOptions[$representation->abbreviatedLabel] = $area->representation_L10n;
492
				if(is_array($selected_areas) && in_array($representation->abbreviatedLabel, $selected_areas)){
493
					$tdwgOptionsSelected[] = $representation->abbreviatedLabel; //$area->uuid;
494
				}
495
			}
496
			asort($tdwgOptions);
497
			$form['search']['geographic_range']['tdwg_level_'.$key] = array(
498
        '#type' => 'select',
499
        '#title'         => t('TDWG level').' '.$key,
500
        '#default_value' => $tdwgOptionsSelected,
501
        '#multiple' => TRUE, 
502
        '#options' => $tdwgOptions
503
			);
504
			$tdwg_js .= "$('#edit-search-geographic-range-tdwg-level-$key').parent()".($tdwg_level_select + 1 == $key ?  '.show()' : '.hide()'). ";\n";
505
		}
506

    
507
		drupal_add_js(
508
    "$(document).ready(function(){
509
    
510
      $(\"input[@name='search[geographic_range][tdwg_level_select]']\").change( 
511
        function(event){
512
          var selectId = $(\"input[@name='search[geographic_range][tdwg_level_select]']:checked\").val();
513
          var i;
514
          for(i = 0; i < 4; i++){
515
            if(selectId == i){
516
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1) ).parent().fadeIn('slow');
517
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
518
            } else {
519
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).parent().fadeOut('slow');
520
              $('#edit-search-geographic-range-tdwg-level-' + (i + 1)).children().removeAttr('selected');
521
            }
522
          }
523
        }
524
      );
525
      
526
      $tdwg_js
527
    });",
528
    'inline');
529

    
530
	} else {
531
		$form['search']['doTaxa'] = array(
532
      '#delta' => -2,
533
      '#type' => 'hidden',
534
      '#value' => 1
535
		);
536
		$form['search']['doSynonyms'] = array(
537
      '#delta' => -3,
538
      '#type' => 'hidden',
539
      '#value' => 1
540
		);
541
		$form['search']['doTaxaByCommonNames'] = array(
542
      '#delta' => -4,
543
      '#type' => 'hidden',
544
      '#value' => 1
545
		);
546
	}
547

    
548
	$form['submit'] = array(
549
  '#delta' => 9,
550
  '#type' => 'submit',
551
  '#name' => '',
552
  '#value' => t('Search')
553
	);
554

    
555
	return $form;
556
}
557

    
558
function cdm_dataportal_search_taxon_form_advanced(){
559
	return cdm_dataportal_search_taxon_form(true);
560
}
561

    
562
/**
563
 * Implementation #process method call, see form_builder()
564
 * <p>
565
 * Removes Drupal internal form elements from query
566
 * @param $form
567
 * @return unknown_type
568
 */
569
function cdm_dataportal_search_process($form) {
570
	unset($form['form_id']);
571
	unset($form['form_token']);
572
	return $form;
573
}
574

    
575
/**
576
 * Filters $_REQUEST by a list of valid request  parameters and also sets defaults if required.
577
 * returns the processed request parameters submitted by the search form.
578
 */
579
function cdm_dataportal_search_form_request(){
580

    
581

    
582
	$form_params = array();
583
	array_deep_copy($_REQUEST['search'], $form_params);
584
	$form_params['query'] =  trim($_REQUEST['query']);
585

    
586
	// split of  geographic range
587
	if(isset($_REQUEST['search']['geographic_range'])){
588
		$geographicRange = $_REQUEST['search']['geographic_range'];
589
		// remove
590
		unset($form_params['geographic_range']);
591
	}
592

    
593
	// add geographic range
594
	if($geographicRange){
595
		$form_params['tdwg_level_select'] = $geographicRange['tdwg_level_select'];
596
		for($i = 1; $i < 5; $i++){
597
			if(isset($geographicRange['tdwg_level_'.$i])){
598
				$form_params['area'] = $geographicRange['tdwg_level_'.$i];
599
			}
600
		}
601
	}
602

    
603
	// store in session
604
	$_SESSION['cdm']['search'] = $form_params;
605

    
606
	return $form_params;
607
}
608

    
609

    
610
/* UNREACHABLE since action of form directly links to view
611
 function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
612

    
613
 $_SESSION['cdm']['search'] = $form_values;
614
 //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
615
 return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
616
 //$paramstr = compose_url_prameterstr($form_values);
617
 //return url('/cdm_dataportal/search/taxon/', $paramstr);
618
 }
619
 */
620
/* ====================== menu callback functions ====================== */
621

    
622
/**
623
 * Generate main administration form.
624
 *
625
 * @return
626
 *   An array containing form items to place on the module settings page.
627
 */
628
function cdm_dataportal_settings(){
629

    
630
	$form = cdm_api_settings_form();
631

    
632
	//TODO: settings are still incomplete, compare with trunk/dataportal/inc/config_default.php.inc
633
	$form['cdm_dataportal'] = array(
634
      '#type' => 'fieldset',
635
      '#title' => t('CDM DataPortal'),
636
      '#collapsible' => FALSE,
637
      '#collapsed' => TRUE,
638
	);
639

    
640
	//  $form['cdm_dataportal']['secuuid_widget'] = array(
641
	//    '#type' => 'select_secuuid', // => will be themed by theme_select_secuuid(...)
642
	//    '#title' => t('Default Concept References'),
643
	//    '#description'   => t('The default \'sensu\' reference to start the CDM DataPortal with. Per user choices are possible and may override this value.'),
644
	//    '#varname' => 'cdm_secUuid_default',
645
	//    '#multiple' => false
646
	//  );
647

    
648
	$taxonTrees = cdm_ws_get(CDM_WS_TAXONOMY);
649
	foreach($taxonTrees as $tree){
650
		$taxonomicTreeOptions[$tree->uuid] = $tree->titleCache;
651
	}
652
	$form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
653
    '#type' => 'select',
654
    '#title'         => t('Available taxonomic views'),
655
    '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
656
    '#options' => $taxonomicTreeOptions,
657
    '#description'   => t('Select a taxonomic view for this webservice.')
658
	);
659

    
660
	//  $form['cdm_dataportal']['DEBUG']= array(
661
	//    '#value' => '<pre>'.print_r($taxonTrees, true).'</pre>'
662
	//  );
663

    
664

    
665
	//------------------ FEATURE TREE --------------------//
666

    
667
	$form['cdm_dataportal']['feature_tree'] = array(
668
    '#type' => 'fieldset',
669
    '#title' => t('Feature Tree'),
670
    '#collapsible' => TRUE,
671
    '#collapsed' => TRUE,
672
	);
673

    
674
	$form['cdm_dataportal']['feature_tree']['cdm_dataportal_featuretree_uuid'] = array(
675
    '#type' => 'select',
676
    '#title'         => t('Available FeatureTrees'),
677
    '#default_value' => variable_get('cdm_dataportal_featuretree_uuid', UUID_DEFAULT_FEATURETREE),
678
	  '#options' => _get_feature_trees(),
679
    '#description'   => t('Select a FeatureTree for this webservice.'
680
    //.' If there is no applicable FeatureTree you can create a new one using the <a href="">FeatureTreeManager</a>'
681
	)
682
	);
683

    
684
	return system_settings_form($form);
685
}
686

    
687
/**
688
 * LAYOUT settings
689
 * @return unknown_type
690
 */
691
function cdm_dataportal_settings_layout(){
692

    
693
	$form = array();
694

    
695

    
696
	$form['cdm_taxonname_type'] = array(
697
    '#type' => 'select',
698
    '#title'         => t('Taxon name type'),
699
    '#default_value' => variable_get('cdm_taxonname_type', 'BotanicalName'),
700
    '#options' => array( 'BotanicalName'=>t('BotanicalName'), 'ZoologicalName'=>t('ZoologicalName')),
701
    '#description'   => t('')
702
	);
703

    
704
	$form['cdm_dataportal_nomref_in_title'] = array(
705
    '#type' => 'checkbox',
706
    '#title' => t('Show full nomenclatural reference in title'),
707
    '#default_value' => variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE),
708
    '#description' => t('Uncheck this, if you want the title of a taxon page consisting of name and author plus publication year only.
709
                        If not checked, the first homotypic taxon is a repetition of the accepted taxon with
710
                        the full nomenclatural reference.')
711
	);
712

    
713
	$form['cdm_dataportal_display_name_relations'] = array(
714
    '#type' => 'checkbox',
715
    '#title' => t('Show name relations of accepted taxa on taxon page'),
716
    '#default_value' => variable_get('cdm_dataportal_display_name_relations', 1),
717
    '#description' => t('')
718
	);
719

    
720
	$form['cdm_dataportal_display_is_accepted_for'] = array(
721
    '#type' => 'checkbox',
722
    '#title' => t('Display <em>is accepted for ...</em> on taxon pages when coming from a synonym link.'),
723
    '#default_value' => variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR),
724
    '#description' => t('')
725
	);
726

    
727
	$form['cdm_dataportal_name_relations_skiptype_basionym'] = array(
728
    '#type' => 'checkbox',
729
    '#title' => t('Exclude the basionym relationship type from the taxon page'),
730
    '#default_value' => variable_get('cdm_dataportal_name_relations_skiptype_basionym', 1),
731
    '#description' => t('')
732
	);
733

    
734
	$form['cdm_dataportal_taxonpage_tabs'] = array(
735
    '#type' => 'checkbox',
736
    '#title' => t('Tabbed taxon page'),
737
    '#default_value' => variable_get('cdm_dataportal_taxonpage_tabs', 1),
738
    '#description' => t('Split the taxon page into individual tabs for description, images, synonymy')
739
	);
740

    
741
	$form['cdm_dataportal_search_items_on_page'] = array(
742
    '#type' => 'textfield',
743
    '#title' => t('Search Page Size'),
744
    '#default_value' => variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE),
745
    '#description' => t('Number of Names to display per page in search results.')
746
	);
747

    
748
	$form['cdm_dataportal_descriptions_separated'] = array(
749
    '#type' => 'checkbox',
750
    '#title' => t('Separate Descriptions'),
751
    '#default_value' => variable_get('cdm_dataportal_descriptions_separated', 0),
752
    '#description' => t('By default corresponding elements of different descriptions are joined together'
753
    .' into a common section per feature (i.e. type of description).'
754
    .' Check this box to allow displaying all descriptions separately.')
755
    );
756

    
757
    //---- IMAGES ----//
758
    $form['images'] = array(
759
    '#type' => 'fieldset',
760
    '#title' => t('Images'),
761
    '#collapsible' => TRUE,
762
    '#collapsed' => FALSE,
763
    );
764
    $options = cdm_rankVocabulary_as_option();
765
    array_unshift($options, '-- DISABLED --');
766
    $form['images']['image_hide_rank'] =  array(
767
    '#type'          => 'select',
768
    '#title'         => t('Hide Images for Taxa above'),
769
    '#default_value' => variable_get('image_hide_rank', '0'),
770
    '#options'       => $options,
771
    '#description'   => t(''),
772
    );
773
    //show media
774
    $selectShowMedia = array(0 => "Show only taxon media",
775
    1 => "Show taxon and child taxon media");
776
    $form['images']['cdm_dataportal_show_media'] = array(
777
    '#type' => 'select',
778
    '#title' => t('Available media files'),
779
    '#default_value' => variable_get('cdm_dataportal_show_media', false),
780
    '#options' => $selectShowMedia,
781
    '#description'   => t('Select if a taxon should show only his media or also child media.')
782
    );
783

    
784
    // --- SEARCH TAXA GALLERY ---- //
785
    $collapsed = TRUE;
786
    $form_name = CDM_DATAPORTAL_SEARCH_GALLERY_NAME;
787
    $form_tittle = 'Search Taxa';
788
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
789

    
790
    // --- FEATURE DESCRIPTION GALLERY ---- //
791
    $form_name = CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME;
792
    $form_tittle = 'Description elements gallery';
793
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
794

    
795
    // --- MEDIA GALLERY ---- //
796
    $form_name = CDM_DATAPORTAL_MEDIA_GALLERY_NAME;
797
    $form_tittle = 'Media gallery';
798
    $form[] = cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed);
799

    
800
    return system_settings_form($form);
801
}
802

    
803
function cdm_dataportal_create_gallery_settings_form($form_name, $form_tittle, $collapsed){
804

    
805
	$form[$form_name] = array(
806
    '#type' => 'fieldset',
807
    '#title' => t($form_tittle),
808
    '#collapsible' => TRUE,
809
    '#collapsed' => $collapsed,
810
    '#tree' => true,
811
	);
812

    
813
	$default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
814
	$gallery_settings = variable_get($form_name, $default_values);
815

    
816
	$form[$form_name]['cdm_dataportal_show_taxon_thumbnails'] = array(
817
    '#type' => 'checkbox',
818
    '#title' => t('Show media thumbnails for accepted taxa'),
819
    '#default_value' => $gallery_settings['cdm_dataportal_show_taxon_thumbnails'],
820
    '#description' => t('')
821
	);
822

    
823
	$form[$form_name]['cdm_dataportal_show_synonym_thumbnails'] = array(
824
    '#type' => 'checkbox',
825
    '#title' => t('Show media thumbnails for synonyms'),
826
    '#default_value' => $gallery_settings['cdm_dataportal_show_synonym_thumbnails'],
827
    '#description' => t('')
828
	);
829

    
830
	//$showCaption = variable_get('cdm_dataportal_findtaxa_show_thumbnail_captions', 0);
831
	$form[$form_name]['cdm_dataportal_show_thumbnail_captions'] = array(
832
    '#type' => 'checkbox',
833
    '#title' => t('Show captions under thumbnails'),
834
    '#default_value' => $gallery_settings['cdm_dataportal_show_thumbnail_captions'],
835
    '#description' => t('')
836
	);
837

    
838
	$form[$form_name]['cdm_dataportal_media_maxextend'] = array(
839
    '#type' => 'textfield',
840
    '#title' => t('Maximum extend of Images'),
841
    '#default_value' => $gallery_settings['cdm_dataportal_media_maxextend'],
842
    '#description' => t('In the gallery neither width or height will exceed the maximum extend of images.')
843
	);
844

    
845
	$form[$form_name]['cdm_dataportal_media_cols'] = array(
846
    '#type' => 'textfield',
847
    '#title' => t('Number of columns'),
848
    '#default_value' => $gallery_settings['cdm_dataportal_media_cols'],
849
    '#description' => t('')
850
	);
851

    
852
	$form[$form_name]['cdm_dataportal_media_maxRows'] = array(
853
    '#type' => 'textfield',
854
    '#title' => t('Maximum number of rows'),
855
    '#default_value' => $gallery_settings['cdm_dataportal_media_maxRows'],
856
    '#description' => t('If you want an unlimited number of rows please set to 0')
857
	);
858

    
859
	return $form;
860
}
861

    
862
/**
863
 * GEOSERVICE and Map settings
864
 * @return unknown_type
865
 */
866
function cdm_dataportal_settings_geo(){
867

    
868
	$form = array();
869

    
870
	$form['cdm_dataportal_map_openlayers'] = array(
871
    '#type' => 'checkbox',
872
    '#title' => t('OpenLayers Viewer'),
873
    '#default_value' => variable_get('cdm_dataportal_map_openlayers', 1),
874
    '#description' => t('Display the maps in an interactive viewer which allows zooming and panning.')
875
	);
876

    
877
	$form['cdm_dataportal_geoservice_access_point'] = array(
878
    '#type' => 'textfield',
879
    '#title' => t('Geoservice Access Point'),
880
    '#default_value' => variable_get('cdm_dataportal_geoservice_access_point', ''),
881
    '#description' => t('Base URL of the geoservice to be used by this portal')
882
	);
883

    
884
	$form['cdm_dataportal_geoservice_display_width'] = array(
885
    '#type' => 'textfield',
886
    '#title' => t('Geoservice Display Width'),
887
    '#default_value' => variable_get('cdm_dataportal_geoservice_display_width', 390),
888
    '#description' => t('Width of the image generated by geoservice')
889
	);
890

    
891
	$form['cdm_dataportal_geoservice_bounding_box'] = array(
892
    '#type' => 'textfield',
893
    '#title' => t('Fixed Geoservice Bounding Box'),
894
    '#default_value' => variable_get('cdm_dataportal_geoservice_bounding_box', '-180,-90,180,90'),
895
    '#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.')
896
	);
897

    
898
	//cdm_dataportal_geoservice_labels_on
899
	$form['cdm_dataportal_geoservice_labels_on'] = array(
900
    '#type' => 'checkbox',
901
    '#title' => t('Display Country Labels'),
902
    '#default_value' => variable_get('cdm_dataportal_geoservice_labels_on', FALSE),
903
    '#description' => t('Check this if you like country names to be displayed in the maps. ')
904
	);
905

    
906
	//cdm_dataportal_geoservice_legend_on
907
	$form['cdm_dataportal_geoservice_map_caption'] = array(
908
    '#type' => 'textfield',
909
    '#title' => t('Map Caption'),
910
    '#default_value' => variable_get('cdm_dataportal_geoservice_map_caption', ''),
911
    '#description' => t('Define a caption for the map.')
912
	);
913

    
914
	/*
915
	 //cdm_dataportal_geoservice_legend_on
916
	 $form['cdm_dataportal_geoservice_legend_on'] = array(
917
	 '#type' => 'checkbox',
918
	 '#title' => t('Display a map legend'),
919
	 '#default_value' => variable_get('cdm_dataportal_geoservice_legend_on', TRUE),
920
	 '#description' => t('Check this if you like a legend to be displayed with the maps. ')
921
	 );
922
	 */
923

    
924
	//cdm_dataportal_geoservice_map_legend
925
	$form['cdm_dataportal_geoservice_map_legend'] = array(
926
      '#type' => 'fieldset',
927
      '#title' => t('Map Legend'),
928
      '#collapsible' => FALSE,
929
      '#collapsed' => TRUE,
930
	);
931

    
932
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_on'] = array(
933
    '#type' => 'checkbox',
934
    '#title' => t('Display a map legend'),
935
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_on', TRUE),
936
    '#description' => t('Check this if you like a legend to be displayed with the maps. ')
937
	);
938

    
939
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_size'] = array(
940
    '#type' => 'textfield',
941
    '#title' => t('Font size'),
942
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_size', 10),
943
    '#description' => t('Font size in pixels.')
944
	);
945

    
946
	$fontStyles = array(0 => "plane", 1 => "italic");
947
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_font_style'] = array(
948
    '#type' => 'select',
949
    '#title' => t('Available font styles'),
950
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_font_style', false),
951
    '#options' => $fontStyles,
952
    '#description'   => t('Select a font style for the map legend.')
953
	);
954

    
955
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_width'] = array(
956
    '#type' => 'textfield',
957
    '#title' => t('Icon width'),
958
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_width', 35),
959
    '#description' => t('Icon width in pixels.')
960
	);
961

    
962
	$form['cdm_dataportal_geoservice_map_legend']['cdm_dataportal_geoservice_legend_icon_height'] = array(
963
    '#type' => 'textfield',
964
    '#title' => t('Icon height'),
965
    '#default_value' => variable_get('cdm_dataportal_geoservice_legend_icon_height', 15),
966
    '#description' => t('Icon height in pixels.')
967
	);
968

    
969
	/* variable_get('cdm_dataportal_findtaxa_media_maxextend', 120),
970
	 *  $form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
971
	 '#type' => 'select',
972
	 '#title'         => t('Available taxonomic views'),
973
	 '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
974
	 '#options' => $taxonomicTreeOptions,
975
	 '#description'   => t('Select a taxonomic view for this webservice.')
976
	 );
977

    
978
	 $taxonTrees = cdm_ws_get(CDM_WS_TAXONOMY);
979
	 foreach($taxonTrees as $tree){
980
	 $taxonomicTreeOptions[$tree->uuid] = $tree->titleCache;
981
	 }
982
	 $form['cdm_dataportal']['cdm_taxonomictree_uuid'] = array(
983
	 '#type' => 'select',
984
	 '#title'         => t('Available taxonomic views'),
985
	 '#default_value' => variable_get('cdm_taxonomictree_uuid', false),
986
	 '#options' => $taxonomicTreeOptions,
987
	 '#description'   => t('Select a taxonomic view for this webservice.')
988
	 );
989
	 */
990

    
991
	return system_settings_form($form);
992

    
993
}
994

    
995

    
996
/**
997
 * @return walk and cache all taxon pages
998
 */
999
function cdm_dataportal_view_cache_site(){
1000

    
1001
	_add_js_progressbar();
1002

    
1003
	drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/cache_all_taxa.js');
1004

    
1005
	$out = '';
1006

    
1007
	$request_params = array();
1008
	$request_params['query'] = '%';
1009
	$request_params['tree'] = variable_get('cdm_taxonomictree_uuid', false);
1010
	$request_params['doTaxa'] = 1;
1011
	$request_params['doSynonyms'] = 0;
1012
	$request_params['doTaxaByCommonNames'] = 0;
1013
	$search_url = cdm_compose_url(CDM_WS_FIND_TAXA, null, queryString($request_params));
1014
	$search_url = uri_uriByProxy($search_url);
1015
	$taxon_page_url = url('cdm_dataportal/taxon/');
1016

    
1017
	$out .= '<form id="cache_site">';
1018
	$out .= '<br /><h4>'
1019
	.t('Cache all taxon pages')
1020
	.'</h4>';
1021
	$out .= '<div>'.t('This caching process may take long time and could cause heavy load on your server').'</div>';
1022
	$out .= '<div id="progress"></div>';
1023
	$out .= '<input type="hidden" name="searchTaxaUrl" value="'.$search_url.'"/>';
1024
	$out .= '<input type="hidden" name="taxonPageUrl" value="'.$taxon_page_url.'"/>';
1025
	$out .= '<input type="button" name="start" value="'.t('Start').'"/>';
1026
	$out .= '<input type="button" name="stop" value="'.t('Stop').'"/>';
1027
	$out .= '</form>';
1028
	$out .= '</div>';
1029
	//  foreach($taxonPager->records as $taxon){
1030
	//    cdm_dataportal_taxon_view($uuid);
1031
	//  }
1032

    
1033
	return $out;
1034
}
1035

    
1036
/**
1037
 * Implementation of hook_elements().
1038
 */
1039
function cdm_dataportal_elements() {
1040
	$type['select_secuuid'] = array(
1041
    '#input' => TRUE,
1042
    '#process' => array('cdm_dataportal_select_secuuid_expand' => array()),
1043
	//'#validate' => array('cdm_dataportal_select_secuuid_validate' => array()),
1044
	//'#default_value' => array(),
1045
	);
1046
	return $type;
1047
}
1048

    
1049
/*
1050
 *
1051
 */
1052
function cdm_dataportal_select_secuuid_expand($element){
1053

    
1054
	$element['#tree'] = FALSE;
1055

    
1056
	// any value submitted?
1057
	if(isset($element['#post'][$element['#varname']])){
1058
		$selected_values = $element['#post'][$element['#varname']];
1059
	} else {
1060
		// use those store in drupal
1061
		$selected_values = variable_get( $element['#varname'], array());
1062
	}
1063
	if(!is_array($selected_values)){
1064
		$selected_values = array($selected_values);
1065
	}
1066
	$options = array();
1067
	foreach($selected_values as $secUuid){
1068
		$options[$secUuid] = cdm_taxontree_secRefTitle_for($secUuid);
1069
	}
1070

    
1071
	$element[$element['#varname']] =  array(
1072
    '#type' => 'select',
1073
    '#options' => $options,
1074
    '#default_value' => array_values($options),
1075
    '#size' => $element['#multiple'] ? 12 : 2,
1076
    '#multiple' => $element['#multiple'],
1077
	);
1078
	return $element;
1079
}
1080

    
1081

    
1082

    
1083
/**
1084
 * Displays a list of the known taxonomic names. Long lists are split up into multiple pages
1085
 *
1086
 * TODO: parameters are still preliminar
1087
 * @param String $page page number to diplay defaults to page 1
1088
 * @param boolean $hide_unaccepted whether to hide nams which are not accepted by the current view
1089
 */
1090
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = false ){
1091

    
1092
	$request_params  = array(
1093
    'q' => $beginsWith,
1094
	//'sec' = '',
1095
	//'higherTaxa' => getFiters(),
1096
	// 'matchAnywhere' => false, // default is false
1097
    'page' => $page,
1098
    'onlyAccepted' => $onlyAccepted,
1099
    'pagesize' => 20  /*$_SESSION['cdm']['namelist_pagesize'] */);
1100

    
1101
	$taxonPager = cdm_ws_find(CDM_WS_FIND_TAXA, $request_params);
1102
	/*
1103
	 * FIXME the filter for accepted names will be a form element, thus this widget
1104
	 * should be generated via form api preferably as block
1105
	 */
1106
	//$out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
1107
	//$out .= theme('cdm_dataportal_widget_names_list', $names, $page);
1108
	$out .= theme('cdm_listof_taxa', $taxonPager);
1109
	return $out;
1110
}
1111

    
1112
function cdm_dataportal_view_reference($uuid, $arg2 = null){
1113
	$reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
1114
	return theme('cdm_reference_page', $reference);
1115
}
1116

    
1117
function cdm_dataportal_view_reference_list($pageNumber){
1118
	$referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE), $pageNumber);
1119
	return theme('cdm_reference_pager', $referencePager, 'cdm_dataportal/reference/list/');
1120
}
1121

    
1122
function cdm_dataportal_view_media($mediaUuid, $mediarepresentation_uuid = false, $part = 0){
1123
	$media = cdm_ws_get(CDM_WS_MEDIA, $mediaUuid);
1124
	return theme('cdm_media_page', $media, $mediarepresentation_uuid, $part);
1125
}
1126

    
1127
/**
1128
 * The taxon page gives detailed information on a taxon, it shows:
1129
 *  - Taxon name
1130
 *  - Full list of synonyms homotypic synonyms on top, followed by the
1131
 *    heterotypic and finally followed by misapplied names.
1132
 *    The list is ordered historically.
1133
 *  - All description associated with the taxon.
1134
 *
1135
 * @param $uuid
1136
 * @param $chapter name of the part to display,
1137
 *         valid values are: 'description', 'images', 'synonymy', 'all'
1138
 * @return unknown_type
1139
 */
1140
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all'){
1141

    
1142
	// display the page for the taxon defined by $uuid
1143
	$taxonpage = cdm_dataportal_taxon_view($uuid, $chapter);
1144
	return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title , $taxonpage->content);
1145
}
1146

    
1147
/**
1148
 * @param $uuid
1149
 * @param $chapter name of the part to display,
1150
 *         valid values are: 'description', 'images', 'synonymy', 'all'
1151
 * @return unknown_type
1152
 */
1153
function cdm_dataportal_taxon_view($uuid, $chapter = 'all'){
1154

    
1155
	$taxon = cdm_ws_get(CDM_WS_TAXON, $uuid);
1156
	if(!$taxon){
1157
		drupal_set_title(t('Taxon does not exist'));
1158
		return false;
1159
	}
1160
	_cdm_dataportal_set_currentSecUuid($taxon->sec->uuid);
1161

    
1162
	$taxonpage->title = theme('cdm_taxon_page_title', $taxon);
1163
	$taxonpage->content = theme('cdm_taxon_page_general', $taxon, $chapter);
1164

    
1165
	return $taxonpage;
1166
}
1167

    
1168

    
1169
function cdm_dataportal_view_search_advanced(){
1170

    
1171
	drupal_set_title(t('Advanced Search'));
1172

    
1173
	$searchForm = cdm_dataportal_search_taxon_form(true);
1174

    
1175
	return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
1176

    
1177
}
1178

    
1179
/**
1180
 *
1181
 * future extensions to meet palmweb mockup requirements:
1182
 *  - habitat
1183
 *  - uses
1184
 *  - conservation status
1185
 *  - locality / tdwg region
1186
 */
1187
function cdm_dataportal_view_search_taxon(){
1188

    
1189
	$_SESSION['cdm']['last_search'] = $_SERVER['REQUEST_URI'];
1190

    
1191
	$request_params = cdm_dataportal_search_form_request();
1192
	$taxonPager = cdm_ws_get(CDM_WS_FIND_TAXA, null, queryString($request_params));
1193

    
1194
	$search_params = $_REQUEST;
1195
	unset($search_params['q']);
1196
	return theme('cdm_search_results', $taxonPager, 'cdm_dataportal/search/taxon', $search_params);
1197
}
1198

    
1199
function cdm_view_xml2json(){
1200
	$file = arg(2);
1201
	$datastr = get_content(variable_get('cdm_webservice_url', '').$file);
1202
	return  xml2json::transformXmlStringToJson($datastr);
1203
}
1204

    
1205
/* ====================== other functions ====================== */
1206

    
1207
/**
1208
 * Enter description here...
1209
 *
1210
 * @param String $uuid the UUID of the taxon
1211
 * @return the URL
1212
 */
1213
function path_to_taxon($uuid){
1214
	if(!$uuid) return false;
1215
	return 'cdm_dataportal/taxon/'.$uuid;
1216
}
1217

    
1218
function path_to_reference($uuid){
1219
	if(!$uuid) return false;
1220
	return 'cdm_dataportal/reference/'.$uuid;
1221
}
1222

    
1223
function path_to_media($uuid, $representaion_uuid = false, $partId = false){
1224
	if(!$uuid) return false;
1225
	$out = 'cdm_dataportal/media/'.$uuid;
1226
	if($representaion_uuid){
1227
		$out .= '/'.$representaion_uuid;
1228
		if($partId !== false){
1229
			$out .= '/'.$partId;
1230
		}
1231
	}
1232
	return $out;
1233
}
1234

    
1235
/**
1236
 * Compares thisRank with thatRank.
1237
 * Returns a negative integer, zero, or a positive integer
1238
 * as the of thisRank is higher than, equal to, or lower than thatRank.
1239
 * e.g:
1240
 * <ul>
1241
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
1242
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
1243
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
1244
 * </ul>
1245
 * <p>
1246
 * This compare logic of the underlying webservice is the
1247
 * <b>inverse logic</b> of the the one implemented in
1248
 * java.lang.Comparable#compareTo(java.lang.Object)
1249
 * @param $thisRankUuid
1250
 * @param $thatRankUuid
1251
 * @return  a negative integer, zero, or a positive integer
1252
 * as the thisRank is lower than, equal to, or higher than thatRank
1253
 */
1254
function rank_compare($thisRankUuid, $thatRankUuid){
1255
	$result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
1256
	return $result->Integer;
1257
}
1258

    
1259
function uri_to_synonym($synonymUuid, $acceptedUuid, $pagePart = null){
1260
	$acceptedPath = path_to_taxon($acceptedUuid);
1261
	return url($acceptedPath.($pagePart ? '/'.$pagePart : ''), 'highlite='.$synonymUuid.'&acceptedFor='.$synonymUuid);
1262
}
1263

    
1264
/**
1265
 * Creates a short taxonname by using the taggename field of NameSTO or NameTO instances.
1266
 * If the taggename if empty the fullname will be returned.
1267
 *
1268
 * @param unknown_type $Name or TreeNode
1269
 * @return string
1270
 */
1271
function cdm_dataportal_shortname_of($name){
1272

    
1273
	$nameStr = '';
1274
	// get all tagged text tokens of the scientific name
1275
	foreach($name->taggedTitle as $tagtxt){
1276
		if($tagtxt->type == 'name' || $tagtxt->type == 'rank'){
1277
			$nameStr .= ($nameStr ? ' ' : '').$tagtxt->text;
1278
		}
1279
	}
1280
	$nameStr = trim($nameStr);
1281
	if($nameStr){
1282
		// do not return short names for these
1283
		if($nameStr == 'Incertae sedis' || $nameStr ==  'Nomina excludenda'){
1284
			return $nameStr;
1285
		}
1286
		if($pos = stripos($nameStr, ' ')){
1287
			return substr($nameStr, 0, 1).'. '.substr($nameStr, $pos);
1288
		} else {
1289
			return $nameStr;
1290
		}
1291
	} else {
1292
		return $name->titleCache;
1293
	}
1294
}
1295

    
1296
/**
1297
 * TODO Add Comments. I don't get what
1298
 *
1299
 * @param UUID $secUuid
1300
 */
1301
function _cdm_dataportal_set_currentSecUuid($secUuid){
1302

    
1303
	// do not save in session but in database
1304

    
1305
	if(is_array($secUuid)){
1306
		$secUuid = $secUuid[0];
1307
	}
1308
	if(!$secUuid){
1309
		variable_set('cdm_currentSecRef', null);
1310
	} else {
1311
		$secRef = cdm_ws_get(CDM_WS_REFERENCE, $secUuid);
1312
		if(isset($secRef)){
1313
			variable_set('cdm_currentSecRef', (array)($secRef));
1314
		}
1315
	}
1316

    
1317
}
1318

    
1319
/**
1320
 * TODO
1321
 * This will not work with multiple instances of the dataportal running
1322
 * under the same host as there is only one session for all instances.
1323
 * In case you switch instances with the same client, you will definitely
1324
 * run into trouble.
1325
 * Also this is not handling multiple secs.
1326
 *
1327
 *
1328
 * returns the current secRef array.
1329
 * If the according session variable is not jet set the default
1330
 * as configured in the setting is used otherwise null.
1331
 *
1332
 * currentSecRef['uuid']
1333
 * currentSecRef[....
1334
 *
1335
 * @return array
1336
 */
1337
function _cdm_dataportal_currentSecRef_array(){
1338

    
1339
	// do not look in session but in database
1340
	if( variable_get('cdm_currentSecRef', null) == null){
1341
		$secUuid = variable_get('cdm_secUuid_default', null);
1342
		_cdm_dataportal_set_currentSecUuid($secUuid);
1343
	}
1344
	return variable_get('cdm_currentSecRef', null);
1345

    
1346
}
1347

    
1348
/**
1349
 * Check if a taxon is accepted by the current taxonomic tree
1350
 *
1351
 * @param Taxon $taxon
1352
 * @return true if $taxon is accepted, false otherwise
1353
 */
1354

    
1355
function _cdm_dataportal_acceptedByCurrentView($taxon){
1356

    
1357
	//$current_secref = _cdm_dataportal_currentSecRef_array();
1358
	$defaultTreeUuid = variable_get('cdm_taxonomictree_uuid', false);
1359
	if($taxon->class == "Taxon" && isset($taxon->taxonNodes)){
1360
		foreach($taxon->taxonNodes as $node){
1361
			if($node->taxonomicTree == $defaultTreeUuid) {
1362
				return true;
1363
			}
1364
		}
1365
	}
1366
	return false;
1367
}
1368

    
1369
/**@Deprecated
1370
 *
1371
 */
1372
function compose_url_prameterstr($parameters = array(), $parentPropertyName = false){
1373
	$pstr = '';
1374
	foreach($parameters as $key=>$value){
1375
		if(is_array($value)){
1376

    
1377
		} else {
1378
			$pstr .= ($pstr ? '&' :'').$key.'='.urlencode($value);
1379
		}
1380
	}
1381
	return $pstr;
1382
}
1383

    
1384
function _get_feature_trees(){
1385
	$feature_trees = array();
1386

    
1387
	// set tree that contains all features
1388
	$feature_trees[UUID_DEFAULT_FEATURETREE] = t('Default Featuretree (contains all features)');
1389

    
1390
	// get features from database
1391
	$persisted_trees = cdm_ws_get(CDM_WS_FEATURETREES);
1392
	if(is_array($persisted_trees)){
1393
		foreach($persisted_trees as $featureTree){
1394

    
1395
			// do not add the DEFAULT_FEATURETREE again
1396
			if($featureTree->uuid == UUID_DEFAULT_FEATURETREE){
1397
				continue;
1398
			}
1399
			$featureLabels = array();
1400
			foreach ($featureTree->root->children as $featureNode){
1401
				$featureLabels[] = $featureNode->feature->representation_L10n;
1402
			}
1403

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

    
1406
		}
1407
	}
1408
	return $feature_trees;
1409
}
1410

    
1411

    
1412
function get_nameRenderTemplate($renderPath, $nameLink = NULL, $refenceLink = NULL){
1413
	//TODO implement admin user interface to replace swicth statement
1414
	//     preliminar solution: using themes
1415

    
1416
	$template = theme('get_nameRenderTemplate', $renderPath);
1417

    
1418
	if(!is_array($template)){
1419
		switch($renderPath){
1420
			case 'list_of_taxa':
1421
			case  'acceptedFor':
1422
			case 'taxon_page_synonymy':
1423
			case 'typedesignations':
1424
			case 'taxon_page_title':
1425
			case 'na': $template = array(
1426
          'namePart' => array('#uri'=>true),
1427
			//'authorshipPart' => true,
1428
			);
1429
			break;
1430
			case 'nar': $template = array(
1431
          'namePart' => array('#uri'=>true),
1432
			//'authorshipPart' => true,
1433
          'referencePart' => array('#uri'=>true),
1434
          'microreferencePart' => true,
1435
			);
1436
			break;
1437
			default: $template = array(
1438
          'namePart' => array('#uri'=>true),
1439
          'authorshipPart' => true,
1440
          'referencePart' => array('#uri'=>true),
1441
          'microreferencePart' => true,
1442
          'statusPart' => true,
1443
          'descriptionPart' => true
1444
			);
1445
		}
1446
	}
1447

    
1448
	if($nameLink && isset($template['nameAuthorPart']['#uri'])){
1449
		$template['nameAuthorPart']['#uri'] = $nameLink;
1450
	} else{
1451
		unset($template['nameAuthorPart']['#uri']);
1452
	}
1453

    
1454
	if($nameLink && isset($template['namePart']['#uri'])){
1455
		$template['namePart']['#uri'] = $nameLink;
1456
	} else{
1457
		unset($template['namePart']['#uri']);
1458
	}
1459

    
1460
	if($refenceLink && isset($template['referencePart']['#uri'])){
1461
		$template['referencePart']['#uri'] = $refenceLink;
1462
	}else{
1463
		unset($template['referencePart']['#uri']);
1464
	}
1465

    
1466
	return $template;
1467
}
1468

    
1469
function get_partDefinition($taxonNameType){
1470
	//TODO implement admin user interface to allow specify the partdefinitions for any type
1471
	//     preliminar solution: using themes
1472

    
1473
	$partdef = theme('get_partDefinition', $taxonNameType);
1474

    
1475
	if(!is_array($partdef)){
1476
		switch($taxonNameType){
1477
			case 'ZoologicalName': $partdef = array(
1478
        'namePart' => array(
1479
          'name' => true,
1480
			),
1481
        'referencePart' => array(
1482
          'authorTeam' => true
1483
			),
1484
        'microreferencePart' => array(
1485
          'microreference' => true,
1486
			),
1487
        'statusPart' => array(
1488
          'status' => true,
1489
			),
1490
        'descriptionPart' => array(
1491
          'description' => true,
1492
			),
1493
			);
1494
			break;
1495
			case 'BotanicalName': $partdef = array(
1496
        'namePart' => array(
1497
          'name' => true
1498
			),
1499
        'authorTeamPart' => array(
1500
          'authorTeam' => true,   
1501
			),
1502
        'referencePart' => array(
1503
          'reference' => true      
1504
			),
1505
        'microreferencePart' => array(
1506
          'microreference' => true,
1507
			),
1508
        'statusPart' => array(
1509
          'status' => true,
1510
			),
1511
        'descriptionPart' => array(
1512
          'description' => true,
1513
			),
1514
			);
1515
			break;
1516
			default: $partdef = array(
1517
        'namePart' => array(
1518
          'name' => true,
1519
			),
1520
        'authorTeamPart' => array(
1521
          'authorTeam' => true,   
1522
			),
1523
        'referencePart' => array(
1524
          'reference' => true      
1525
			),
1526
        'microreferencePart' => array(
1527
          'microreference' => true,
1528
			),
1529
        'statusPart' => array(
1530
          'status' => true,
1531
			),
1532
        'descriptionPart' => array(
1533
          'description' => true,
1534
			),
1535
			);
1536
		}
1537
	}
1538
	return $partdef;
1539
}
1540

    
1541
/**
1542
 * The function read the metadata info such title or artist of a media file. The
1543
 * function tries at first to get all the info from the file metadata and if it is
1544
 * not avaible look at the media file info stored at the database.
1545
 * @param $media The media file
1546
 * @return array The array with the avilable specified metadata info.
1547
 * TODO rename to read_media_metadata() and move to *.module
1548
 */
1549
function cdm_read_media_metadata($media){
1550

    
1551
	$metadata_caption = array('title' => '',           //media_metadata and media
1552
                              'artist' => '',          //media_metadata and media
1553
                              'rights',                //media_metadata and media
1554
                              'location',              //media_metadata
1555
                              'filename' => '',        //media
1556
                              'mediacreated' => '',    //media
1557
                              'description' => '');    //media
1558

    
1559
	//getting the media metadata
1560
	$media_metadata = cdm_ws_get(CDM_WS_MEDIA_METADATA, array($media->uuid));
1561

    
1562
	//title
1563
	//if ($media_metadata->Headline) {
1564
	if ($media_metadata->ObjectName) {
1565
		$metadata_caption['title'] = $media_metadata->ObjectName;
1566
		//	} else if ($media->titleCache){
1567
		//		$metadata_caption['title'] = $media_metadata->ObjectName;
1568
	} else if ($media->titleCache){
1569
		$metadata_caption['title'] = $media->titleCache;
1570
		if ($media->description_L10n)
1571
		$metadata_caption['title'] .= ' - ' . $media->description_L10n;
1572
	}else{ //if there is no title on the db and metadata get file title
1573
    $aux = $media->representations[0]->parts[0]->uri;
1574
    $aux = explode("/", $aux);
1575
    $aux = end($aux);
1576
		$metadata_caption['title'] = $aux;		
1577
	}
1578

    
1579
	//artist
1580
	if ($media_metadata->Artist){
1581
	 $metadata_caption['artist'] = ($media_metadata->Artist ? ''.$media_metadata->Artist : '');
1582
	}
1583
	elseif ($media->artist->titleCache){
1584
	 $metadata_caption['artist'] = $media->artist->titleCache;
1585
	}
1586
	
1587
	//copyright
1588
	$metadata_caption['rights'] = array('copyright' => array('agentNames' => array()),
1589
                                      'license' => array('agentNames' => array(), 'types' => array(), 'abbreviatedTexts' => array(), 'uris' => array()));
1590
	if ($media_metadata->Copyright)
1591
	$metadata_caption['rights']['copyright']['agentNames'][] = $media_metadata->Copyright;
1592
	elseif ($media->rights){
1593
		foreach($media->rights as $right){
1594
			switch($right->term->uuid){
1595
				case UUID_RIGHTS_LICENCE:
1596
					$metadata_caption['rights']['license']['agentNames'][] = ($right->agent ? ''.$right->agent->firstname.' '.$right->agent->lastname : '');
1597
					$metadata_caption['rights']['license']['types'][] = ($right->representation_L10n ? ''.$right->representation_L10n : '');
1598
					$metadata_caption['rights']['license']['abbreviatedTexts'][] = ($right->abbreviatedText ? ''.$right->abbreviatedText : '');
1599
					$metadata_caption['rights']['license']['uris'][] = ($right->uri ? ''.$right->uri : '');
1600
					break;
1601
				case UUID_RIGHTS_COPYRIGHT:
1602
					$metadata_caption['rights']['copyright']['agentNames'][] = $right->agent->firstname . ' ' . $right->agent->lastname;
1603
					break;
1604
			}
1605
		}
1606
	}
1607
	else
1608
	$metadata_caption['rights']['agentNames'][] = '';
1609

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

    
1613
	//location
1614
	$metadata_caption['location'] = array();
1615
	$metadata_caption['location']['sublocation'] = $media_metadata->Sublocation;
1616
	$metadata_caption['location']['city'] = $media_metadata->City;
1617
	$metadata_caption['location']['province'] = $media_metadata->Province;
1618
	$metadata_caption['location']['country'] = $media_metadata->Country;
1619

    
1620
	//filename
1621
	if(isset($media->representations[0]->parts[0]->uri)){
1622
		$fileUri = $media->representations[0]->parts[0]->uri;
1623
		$filename = substr($fileUri, strrpos($fileUri, "/")+1);
1624
		$metadata_caption['filename'] = $filename;
1625
	}
1626
	else{
1627
	 $metadata_caption['filename'] = '';
1628
	}
1629
	/*
1630
	 //creation date
1631
	 if($media_metadata["Modify Date"])
1632
	 $metadata_caption['mediacreated'] = $media_metadata["Modify Date"];
1633
	 else
1634
	 $metadata_caption['mediacreated'] = $media->created;
1635
	 */
1636
	//returned value
1637
	return $metadata_caption;
1638
}
1639

    
1640
/**
1641
 * This function collects all the media from a list of description elements
1642
 * and return them as an Array.
1643
 *
1644
 * @param $descriptionElementes The description elements
1645
 * @return Array The output with all the media
1646
 */
1647
function cdm_dataportal_media_from_descriptionElements($descriptionElements){
1648
	//variables
1649
	$outArrayOfMedia = array(); //return value
1650
	//implementation
1651
	foreach($descriptionElements as $descriptionElement){
1652
		foreach($descriptionElement->media as $media){
1653
			if(isset($media)){
1654
			 $outArrayOfMedia[] = $media;
1655
			}
1656
	 }
1657
	}
1658
	return $outArrayOfMedia;
1659
}
1660

    
1661

    
Add picture from clipboard (Maximum size: 40 MB)