1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Class to manage footnotes.
|
5
|
*
|
6
|
* @copyright
|
7
|
* (C) 2007-2012 EDIT
|
8
|
* European Distributed Institute of Taxonomy
|
9
|
* http://www.e-taxonomy.eu
|
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
|
|
16
|
/**
|
17
|
* Manages footnotes in multiple list. Each of these footnote lists is
|
18
|
* identified by a footnoteListKey.
|
19
|
*
|
20
|
* The $footnoteListKey for the current page part that should be stored in the
|
21
|
* the RenderHints class by calling @see RenderHints::setFootnoteListKey($footnoteListKey)
|
22
|
*/
|
23
|
class FootnoteManager {
|
24
|
private static $fnstore = [];
|
25
|
private static $blockedFootnoteLitsKeys = [];
|
26
|
/*
|
27
|
* An associative array holding information on
|
28
|
* special footnote sets which are to be handle separately
|
29
|
*
|
30
|
* The key of the map is the $footnoteListKey which is also the
|
31
|
* key of the $fnstore map
|
32
|
*
|
33
|
* The values are associative arrays with the following optional elements:
|
34
|
* - enclosing_tag: the enclosing tag to be used for rendering of the footnote,
|
35
|
* @see theme_cdm_footnote()
|
36
|
* - key_format: 'latin', 'ROMAN', 'roman', 'ALPHA', 'alpha'
|
37
|
* and one required element:
|
38
|
* - key_index: the set specific counter, to replace the
|
39
|
* default $footnote_key_index
|
40
|
*/
|
41
|
private static $fn_sets = array();
|
42
|
private static $default_set_definition = array(
|
43
|
'key_index' => 1,
|
44
|
'enclosing_tag' => null,
|
45
|
'key_format' => 'numeric'
|
46
|
);
|
47
|
|
48
|
/**
|
49
|
* Private constructor.
|
50
|
*/
|
51
|
private function __construct() {}
|
52
|
|
53
|
/**
|
54
|
* Get a list of footnotes.
|
55
|
*
|
56
|
* @param string $footnoteListKey
|
57
|
* A string as key to the list of footnotes.
|
58
|
*
|
59
|
* @return array
|
60
|
* An array of footnotes objects.
|
61
|
*/
|
62
|
public static function getFootnoteList($footnoteListKey) {
|
63
|
return array_key_exists($footnoteListKey, self::$fnstore) ? self::$fnstore[$footnoteListKey] : NULL;
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* Remove a list of footnotes.
|
68
|
*
|
69
|
* @param string $footnoteListKey
|
70
|
* A string as key to the list of footnotes.
|
71
|
*
|
72
|
* @return void
|
73
|
*/
|
74
|
public static function removeFootnoteList($footnoteListKey) {
|
75
|
if (array_key_exists($footnoteListKey, self::$fnstore)) {
|
76
|
unset(self::$fnstore[$footnoteListKey]);
|
77
|
}
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Render a footnote list
|
82
|
*
|
83
|
* @param string $footnoteListKey
|
84
|
* @param string $separator
|
85
|
*
|
86
|
* @return string
|
87
|
* The rendered footnotelist or an empty string if the $footnoteListKey is
|
88
|
* blocked
|
89
|
*/
|
90
|
public static function renderFootnoteList($footnoteListKey, $separator = ', ') {
|
91
|
|
92
|
if(self::isBlockedFootnoteListKey($footnoteListKey)){
|
93
|
// the footnote key is blocked, don't render it!
|
94
|
return '';
|
95
|
}
|
96
|
$out = '';
|
97
|
if (array_key_exists($footnoteListKey, self::$fnstore)) {
|
98
|
foreach (self::$fnstore[$footnoteListKey] as $fn) {
|
99
|
$out .= $fn->doRender($footnoteListKey) . $separator;
|
100
|
}
|
101
|
$out = substr($out, 0, strlen($out) - strlen($separator));
|
102
|
}
|
103
|
return $out;
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Add a new footnote.
|
108
|
*
|
109
|
* @param $footnoteListKey
|
110
|
* @param $object
|
111
|
*
|
112
|
* @return FootnoteKey
|
113
|
*/
|
114
|
public static function addNewFootnote($footnoteListKey, $object = NULL) {
|
115
|
if (!$object || !$footnoteListKey || self::isBlockedFootnoteListKey($footnoteListKey)) {
|
116
|
return null;
|
117
|
}
|
118
|
if (!array_key_exists($footnoteListKey, self::$fnstore)) {
|
119
|
self::$fnstore[$footnoteListKey] = array();
|
120
|
}
|
121
|
|
122
|
$key_label = NULL;
|
123
|
if (!($key_label = self::footnoteExists($footnoteListKey, $object))) {
|
124
|
|
125
|
$set_def = &self::matchFootnoteSetDefinition($footnoteListKey);
|
126
|
|
127
|
$fn_index = $set_def['key_index']++;
|
128
|
|
129
|
// see http://php.net/manual/de/function.base-convert.php
|
130
|
switch($set_def['key_format']) {
|
131
|
case 'ROMAN':
|
132
|
$key_label = roman_numerals($fn_index);
|
133
|
break;
|
134
|
case 'roman':
|
135
|
$key_label = strtolower(roman_numerals($fn_index));
|
136
|
break;
|
137
|
case 'ALPHA':
|
138
|
$key_label = num2alpha($fn_index - 1);
|
139
|
break;
|
140
|
case 'alpha':
|
141
|
$key_label = strtolower(num2alpha($fn_index - 1));
|
142
|
break;
|
143
|
case 'latin':
|
144
|
default:
|
145
|
$key_label = $fn_index;
|
146
|
}
|
147
|
|
148
|
$fn = new Footnote($key_label, $object, $set_def['enclosing_tag']);
|
149
|
self::$fnstore[$footnoteListKey][$key_label] = $fn;
|
150
|
}
|
151
|
|
152
|
return new FootnoteKey($key_label, $footnoteListKey);
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* Check if a footnote exists.
|
157
|
*
|
158
|
* @param $footnoteListKey
|
159
|
* @param $object
|
160
|
*
|
161
|
* @return bool|int|string
|
162
|
*/
|
163
|
private static function footnoteExists($footnoteListKey, $object) {
|
164
|
foreach (self::$fnstore[$footnoteListKey] as $key => $fn) {
|
165
|
/*
|
166
|
When using the comparison operator (==), object variables are compared
|
167
|
in a simple manner, namely:
|
168
|
Two object instances are equal if they have the same attributes and
|
169
|
values, and are instances of the same class.
|
170
|
*/
|
171
|
if ($object == $fn->object) {
|
172
|
return $key;
|
173
|
}
|
174
|
}
|
175
|
return FALSE;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Register special footnote set for the given $footnoteListKey which is to be handle separately
|
180
|
* @param $footnoteListKey
|
181
|
* @param $enclosing_tag: the enclosing tag to be used for rendering of the footnote, @see theme_cdm_footnote()
|
182
|
* @param $key_format: 'latin', 'ROMAN', 'roman', 'ALPHA', 'alpha'
|
183
|
*/
|
184
|
public static function registerFootnoteSet($footnoteListKey, $enclosing_tag = null, $key_format = null){
|
185
|
|
186
|
$set_def = array('key_index' => 1);
|
187
|
if($enclosing_tag){
|
188
|
$set_def['enclosing_tag'] = $enclosing_tag;
|
189
|
} else {
|
190
|
$set_def['enclosing_tag'] = self::$default_set_definition['enclosing_tag'];
|
191
|
}
|
192
|
if($key_format){
|
193
|
$set_def['key_format'] = $key_format;
|
194
|
} else {
|
195
|
$set_def['key_format'] = self::$default_set_definition['key_format'];
|
196
|
}
|
197
|
self::$fn_sets[$footnoteListKey] = $set_def;
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Returns the footnote set definition which has been
|
202
|
* registered with a key which matches the given $footnoteListKey.
|
203
|
* A match is the first key which is equal to or which starts with $footnoteListKey
|
204
|
* If no match is found the default is returned.
|
205
|
*
|
206
|
* @see registerFootnoteSet()
|
207
|
*
|
208
|
* @param $footnoteListKey
|
209
|
* @return array
|
210
|
*/
|
211
|
public static function &matchFootnoteSetDefinition($footnoteListKey){
|
212
|
|
213
|
foreach(self::$fn_sets as $fn_set_key => &$fn_set_def){
|
214
|
if($footnoteListKey == $fn_set_key || str_beginsWith($footnoteListKey, $fn_set_key)){
|
215
|
return $fn_set_def;
|
216
|
}
|
217
|
}
|
218
|
return self::$default_set_definition;
|
219
|
}
|
220
|
|
221
|
public static function blockFootnotesFor($footnoteListKey) {
|
222
|
if(array_search($footnoteListKey, self::$blockedFootnoteLitsKeys) === false){
|
223
|
self::$blockedFootnoteLitsKeys[] = $footnoteListKey;
|
224
|
}
|
225
|
}
|
226
|
public static function unblockFootnotesFor($footnoteListKey) {
|
227
|
$index = array_search($footnoteListKey, self::$blockedFootnoteLitsKeys);
|
228
|
if($index !== false){
|
229
|
unset(self::$blockedFootnoteLitsKeys[$index]);
|
230
|
}
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* @param $footnoteListKey
|
235
|
*
|
236
|
* @return bool
|
237
|
*/
|
238
|
public static function isBlockedFootnoteListKey($footnoteListKey) {
|
239
|
return array_search($footnoteListKey, self::$blockedFootnoteLitsKeys) !== false;
|
240
|
}
|
241
|
|
242
|
/**
|
243
|
* Stop users from cloning.
|
244
|
*/
|
245
|
public function __clone() {
|
246
|
trigger_error('Cloning instances of the singleton class FootNoteManager is prohibited', E_USER_ERROR);
|
247
|
}
|
248
|
}
|