cdm-dataportal / modules / cdm_dataportal / includes / footnotes.inc @ 1c30a02b
History | View | Annotate | Download (7.84 KB)
1 |
<?php |
---|---|
2 |
/** |
3 |
* @file |
4 |
* footnote functions. |
5 |
* |
6 |
* @copyright |
7 |
* (C) 2007-2020 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 |
* @author |
16 |
* - Andreas Kohlbecker <a.kohlbecker@BGBM.org> |
17 |
*/ |
18 |
|
19 |
/** |
20 |
* Creates the footnotes for the given CDM instance. |
21 |
* |
22 |
* Footnotes are created for annotations and original sources whereas the resulting footnote keys depend on the |
23 |
* parameters $footnote_list_key_suggestion and $is_bibliography_aware, see parameter $footnote_list_key_suggestion |
24 |
* for more details. |
25 |
* |
26 |
* possible keys for annotation and source footnotes: |
27 |
* - $footnote_list_key_suggestion |
28 |
* - RenderHints::getFootnoteListKey() |
29 |
* - original source footnotes |
30 |
* - "BIBLIOGRAPHY" (when !$is_bibliography_aware && bibliography_settings['enabled'] == 1 ) |
31 |
* - "BIBLIOGRAPHY-$footnote_list_key_suggestion" (when !$is_bibliography_aware && bibliography_settings['enabled'] == 0 ) |
32 |
* - $footnote_list_key_suggestion (when $is_bibliography_aware) |
33 |
* |
34 |
* @param $cdm_entity |
35 |
* A CDM entity |
36 |
* @param string $separator |
37 |
* Optional parameter. The separator string to concatenate the footnote ids, default is ',' |
38 |
* @param $footnote_list_key_suggestion string |
39 |
* Optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be determined by the nested |
40 |
* method calls by calling RenderHints::getFootnoteListKey(). |
41 |
* For original sources the $footnote_list_key_suggestion will be overwritten by bibliography_footnote_list_key() when |
42 |
* $is_bibliography_aware is set TRUE. |
43 |
* @param bool $do_link_to_reference |
44 |
* Create a link to the reference pages for sources when TRUE. |
45 |
* @param bool $do_link_to_name_used_in_source |
46 |
* Create a link to the name pages for name in source when TRUE. |
47 |
* @param bool $is_bibliography_aware |
48 |
* Put source references into the bibliography when this param is TRUE. |
49 |
* For original sources the $footnote_list_key_suggestion will be overwritten |
50 |
* by bibliography_footnote_list_key() when |
51 |
* $is_bibliography_aware is set TRUE. |
52 |
* @return String |
53 |
* The foot note keys as markup |
54 |
* |
55 |
* NOTE: Only used in @see handle_annotations_and_sources() |
56 |
*/ |
57 |
function render_entity_footnotes( |
58 |
$cdm_entity, |
59 |
$separator = ',', |
60 |
$footnote_list_key_suggestion = null, |
61 |
$do_link_to_reference = FALSE, |
62 |
$do_link_to_name_used_in_source = FALSE, |
63 |
$is_bibliography_aware = FALSE |
64 |
){ |
65 |
|
66 |
$sources = cdm_entity_sources_sorted($cdm_entity); |
67 |
|
68 |
if (!isset($footnote_list_key_suggestion) || !$footnote_list_key_suggestion) { |
69 |
$footnote_list_key_suggestion = RenderHints::getFootnoteListKey(); |
70 |
} |
71 |
|
72 |
// Annotations as footnotes. |
73 |
$footnote_keys = cdm_entity_annotations_as_footnote_keys($cdm_entity, $footnote_list_key_suggestion); |
74 |
|
75 |
// Source references as footnotes. |
76 |
if($is_bibliography_aware){ |
77 |
$bibliography_settings = get_bibliography_settings(); |
78 |
$sources_footnote_list_key = bibliography_footnote_list_key($footnote_list_key_suggestion); |
79 |
$original_source_footnote_tag = $bibliography_settings['enabled'] == 1 ? 'div' : null; // null will cause bibliography_footnote_list_key to use the default |
80 |
} else { |
81 |
$sources_footnote_list_key = $footnote_list_key_suggestion; |
82 |
$original_source_footnote_tag = NULL; |
83 |
} |
84 |
|
85 |
foreach ($sources as $source) { |
86 |
if (_is_original_source_type($source)) { |
87 |
$fn_key = FootnoteManager::addNewFootnote( |
88 |
$sources_footnote_list_key, |
89 |
render_original_source( |
90 |
$source, |
91 |
$do_link_to_reference, |
92 |
$do_link_to_name_used_in_source |
93 |
), |
94 |
$original_source_footnote_tag |
95 |
); |
96 |
// Ensure uniqueness of the footnote keys. |
97 |
if(array_search($fn_key, $footnote_keys)=== false) { |
98 |
$footnote_keys[] = $fn_key; |
99 |
} |
100 |
} |
101 |
} |
102 |
// Sort and render footnote keys. |
103 |
asort($footnote_keys); |
104 |
return render_footnote_keys($footnote_keys, $separator); |
105 |
} |
106 |
|
107 |
/** |
108 |
* Fetches the list of visible annotations for the cdm entity or for the comparable |
109 |
* object and returns the footnote keys. |
110 |
* |
111 |
* The footnotes are passed to the FootnoteManager in order to store the |
112 |
* annotations and to create the footnote keys. |
113 |
|
114 |
* @param stdClass $cdm_entity |
115 |
* A single CdmBase instance ore comparable object. |
116 |
* @param $footnote_list_key string |
117 |
* optional parameter. If this parameter is left empty (null, 0, "") the |
118 |
* footnote key will be set to RenderHints::getFootnoteListKey() |
119 |
* otherwise the supplied $footnote_list_key will be used. |
120 |
* @return array of footnote keys |
121 |
* |
122 |
* @see cdm_fetch_visible_annotations() |
123 |
*/ |
124 |
function cdm_entity_annotations_as_footnote_keys(stdClass $cdm_entity, $footnote_list_key = NULL) { |
125 |
|
126 |
$foot_note_keys = []; |
127 |
|
128 |
if (!isset($footnote_list_key) || !$footnote_list_key) { |
129 |
$footnote_list_key = RenderHints::getFootnoteListKey(); |
130 |
} |
131 |
|
132 |
// Adding the footnotes keys. |
133 |
$annotations = cdm_fetch_visible_annotations($cdm_entity); |
134 |
if (is_array($annotations)) { |
135 |
foreach ($annotations as $annotation) { |
136 |
$foot_note_keys[] = FootnoteManager::addNewFootnote($footnote_list_key, $annotation->text); |
137 |
} |
138 |
} |
139 |
|
140 |
return $foot_note_keys; |
141 |
} |
142 |
|
143 |
/** |
144 |
* Creates markup for an array of foot note keys |
145 |
* |
146 |
* @param array $footnote_keys |
147 |
* @param string $separator |
148 |
* |
149 |
* @return string |
150 |
*/ |
151 |
function render_footnote_keys(array $footnote_keys, $separator) { |
152 |
|
153 |
$footnotes_markup = ''; |
154 |
foreach ($footnote_keys as $foot_note_key) { |
155 |
try { |
156 |
$footnotes_markup .= render_footnote_key($foot_note_key, ($footnotes_markup ? $separator : '')); |
157 |
} catch (Exception $e) { |
158 |
drupal_set_message("Exception: " . $e->getMessage(), 'error'); |
159 |
} |
160 |
} |
161 |
return $footnotes_markup; |
162 |
} |
163 |
|
164 |
/** |
165 |
* Creates markup for a foot note key |
166 |
* |
167 |
* @param null $footnoteKey |
168 |
* @param string $separator |
169 |
* @param bool $separator_off |
170 |
* |
171 |
* @return string |
172 |
* The footnote key markup |
173 |
*/ |
174 |
function render_footnote_key($footnoteKey = null, $separator = '', $separator_off = false) { |
175 |
|
176 |
if (!is_object($footnoteKey) or !isset($footnoteKey->footnoteListKey)) { |
177 |
return ''; |
178 |
} |
179 |
if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) { |
180 |
return ''; |
181 |
} |
182 |
|
183 |
if ($separator_off) { |
184 |
$separator = ''; |
185 |
} |
186 |
$out = '<span class="footnote-key footnote-key-' . $footnoteKey->keyStr . ' member-of-footnotes-' . $footnoteKey->footnoteListKey . '">' |
187 |
. $separator . '<a href="#footnote-' . $footnoteKey->keyStr . '">' . $footnoteKey->keyStr . '</a>' . '</span>'; |
188 |
return $out; |
189 |
} |
190 |
|
191 |
/** |
192 |
* @param null $footnoteKey |
193 |
* @param null $footnoteText |
194 |
* @param string $enclosing_tag |
195 |
* default is 'span' |
196 |
* |
197 |
* @return string |
198 |
*/ |
199 |
function render_footnote($footnoteKey = null, $footnoteText = null, $enclosing_tag = 'span') { |
200 |
_add_js_footnotes(); |
201 |
if($enclosing_tag == null){ |
202 |
$enclosing_tag = 'span'; |
203 |
} |
204 |
return '<' . $enclosing_tag . ' class="footnote footnote-' . $footnoteKey . '">' |
205 |
. '<a name="footnote-' . $footnoteKey . '"></a>' |
206 |
. '<span class="footnote-anchor">' . $footnoteKey . '.</span> ' . $footnoteText |
207 |
. '</' . $enclosing_tag . '>'; |
208 |
} |
209 |
|
210 |
|
211 |
|
212 |
/** |
213 |
* Create markup for the footnotes mapped to the $footnoteListKey. |
214 |
* |
215 |
* @param null $footnote_list_key |
216 |
* The footnote list key, see RenderHints::getFootnoteListKey() |
217 |
* @param $element_tag |
218 |
* The tag for the footnote element |
219 |
* |
220 |
* @return string |
221 |
* The markup |
222 |
*/ |
223 |
function render_footnotes($footnote_list_key = null, $element_tag = 'span') { |
224 |
|
225 |
if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) { |
226 |
return ''; |
227 |
} |
228 |
|
229 |
if (!isset($footnote_list_key) || !$footnote_list_key) { |
230 |
$footnote_list_key = RenderHints::getFootnoteListKey(); |
231 |
} |
232 |
|
233 |
$out = '<' . $element_tag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">' |
234 |
. FootnoteManager::renderFootnoteList($footnote_list_key) |
235 |
. '</' . $element_tag . '>'; |
236 |
|
237 |
FootnoteManager::removeFootnoteList($footnote_list_key); |
238 |
return $out; |
239 |
} |
240 |
|