1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Provides tokens for taxa, names, references, etc.
|
5
|
*
|
6
|
* @copyright
|
7
|
* (C) 2021 EDIT
|
8
|
* European Distributed Institute of Taxonomy
|
9
|
* http://www.cybertaxonopmy.org
|
10
|
*
|
11
|
* The contents of this module are subject to the Mozilla
|
12
|
* Public License Version 1.1.
|
13
|
* @see http://www.mozilla.org/MPL/MPL-1.1.html
|
14
|
*
|
15
|
* @author
|
16
|
* - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
|
17
|
*/
|
18
|
|
19
|
/**
|
20
|
* Implements hook_token_info().
|
21
|
*/
|
22
|
function cdm_tokens_token_info() {
|
23
|
$info = array();
|
24
|
|
25
|
$info['tokens']['cdm']['taxon_name'] = t('The taxon name consisting of all name parts and sub species rank. (Only present on a cdm_dataportal/taxon/ or cdm_dataportal/name/* page)');
|
26
|
$info['tokens']['cdm']['taxon_name:uninomial'] = t('The taxon names uninomial or genus name. (Only present on a cdm_dataportal/taxon/ or cdm_dataportal/name/* page)');
|
27
|
$info['tokens']['cdm']['taxon_name:epithet'] = t('The taxon names specific epithet. (Only present on a cdm_dataportal/taxon/ or cdm_dataportal/name/* page');
|
28
|
$info['tokens']['cdm']['taxon_name:infraspecific_epithet'] = t('The taxon names specific epithet. (Only present on a cdm_dataportal/taxon/ or cdm_dataportal/name/* page');
|
29
|
$info['tokens']['cdm']['taxon_name:reference'] = t('The taxon name nomenclatural reference. (Only present on a cdm_dataportal/taxon/ or cdm_dataportal/name/* page');
|
30
|
// $info['tokens']['cdm']['taxon_name:reference:doi'] = t('The taxon name nomenclatural reference. (Only present on a cdm_dataportal/taxon/ or cdm_dataportal/name/* page');
|
31
|
$info['tokens']['cdm']['taxon_name:authorship'] = t('The author(s) of taxon name. (Only present on a cdm_dataportal/taxon/ or cdm_dataportal/name/* page');
|
32
|
$info['tokens']['cdm']['taxon_name:rank'] = t('The names rank. (Only present on a cdm_dataportal/taxon/ or cdm_dataportal/name/* page');
|
33
|
|
34
|
return $info;
|
35
|
}
|
36
|
|
37
|
/**
|
38
|
* Implements hook_tokens().
|
39
|
*/
|
40
|
function cdm_tokens_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
41
|
$replacements = array();
|
42
|
|
43
|
if ($type == 'cdm') {
|
44
|
$token_values = _cdm_tokens_taxon_or_name();
|
45
|
foreach ($tokens as $name => $original) {
|
46
|
// Find the desired token by name
|
47
|
switch ($name) {
|
48
|
case 'taxon_name':
|
49
|
if($token_values['taxon_name']){
|
50
|
$replacements[$original] = $token_values['taxon_name'];
|
51
|
}
|
52
|
break;
|
53
|
case 'taxon_name:uninomial':
|
54
|
case 'taxon_name:epithet':
|
55
|
case 'taxon_name:infraspecific_epithet':
|
56
|
case 'taxon_name:rank':
|
57
|
case 'taxon_name:reference':
|
58
|
case 'taxon_name:authorship':
|
59
|
if($token_values[$name]){
|
60
|
$replacements[$original] = $token_values[$name];
|
61
|
}
|
62
|
break;
|
63
|
}
|
64
|
}
|
65
|
}
|
66
|
|
67
|
// Return the replacements.
|
68
|
return $replacements;
|
69
|
}
|
70
|
|
71
|
|
72
|
function _cdm_tokens_taxon_or_name(){
|
73
|
static $current_token_values;
|
74
|
if(!isset($current_token_values)){
|
75
|
$current_token_values = [
|
76
|
'taxon_name' => null,
|
77
|
'uninomial' => null,
|
78
|
'epithet' => null,
|
79
|
'infraspecific_epithet' => null,
|
80
|
'rank' => null,
|
81
|
'reference' => null,
|
82
|
// 'reference:doi' => null,
|
83
|
'authorship' => null
|
84
|
];
|
85
|
$taxon_name = null;
|
86
|
|
87
|
$taxon_uuid = get_current_taxon_uuid();
|
88
|
if($taxon_uuid){
|
89
|
// this is request is made anyway. Practically we only query the L1 cache here.
|
90
|
$taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, [$taxon_uuid]);
|
91
|
if(isset_not_empty($taxon)){
|
92
|
$taxon_name = $taxon->name;
|
93
|
}
|
94
|
} else {
|
95
|
$name_uuid = get_current_name_uuid();
|
96
|
$taxon_name = cdm_ws_get(CDM_WS_PORTAL_NAME, [$name_uuid]);
|
97
|
}
|
98
|
|
99
|
if(isset_not_empty($taxon_name)){
|
100
|
$current_token_values['taxon_name'] = $taxon_name->nameCache;
|
101
|
$current_token_values['uninomial'] = $taxon_name->genusOrUninomial;
|
102
|
$current_token_values['epithet'] = $taxon_name->specificEpithet;
|
103
|
$current_token_values['infraspecific_epithet'] = $taxon_name->infraSpecificEpithet;
|
104
|
if(isset_not_empty($taxon_name->rank->representation_L10n)) {
|
105
|
$current_token_values['rank'] = $taxon_name->rank->representation_L10n;
|
106
|
}
|
107
|
if(isset_not_empty($taxon_name->nomenclaturalSource->citation->titleCache)) {
|
108
|
$current_token_values['reference'] = $taxon_name->nomenclaturalSource->citation->titleCache;
|
109
|
}
|
110
|
if(isset_not_empty($taxon_name->nomenclaturalSource->citation->authorship->titleCache)) {
|
111
|
$current_token_values['authorship'] = $taxon_name->nomenclaturalSource->citation->authorship->titleCache;
|
112
|
}
|
113
|
}
|
114
|
}
|
115
|
|
116
|
return $current_token_values;
|
117
|
}
|
118
|
|