Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
1 dd0767b6 Andreas Kohlbecker
<?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 fe064e2d Andreas Kohlbecker
const FOOTNOTE_KEY_SUFFIX_ANNOTATIONS = '-annotations';
19 dd0767b6 Andreas Kohlbecker
20
/**
21
 * Creates the footnotes for the given CDM instance.
22
 *
23
 * Footnotes are created for annotations and original sources whereas the resulting footnote keys depend on the
24
 * parameters $footnote_list_key_suggestion and $is_bibliography_aware, see parameter $footnote_list_key_suggestion
25
 * for more details.
26
 *
27
 * possible keys for
28
 *     - annotation footnotes:
29
 *       - $footnote_list_key_suggestion
30 fe064e2d Andreas Kohlbecker
 *       - RenderHints::getFootnoteListKey().FOOTNOTE_KEY_SUFFIX_ANNOTATIONS
31 dd0767b6 Andreas Kohlbecker
 *     - original source footnotes
32
 *       - "BIBLIOGRAPHY" (when !$is_bibliography_aware && bibliography_settings['enabled'] == 1 )
33
 *       - "BIBLIOGRAPHY-$footnote_list_key_suggestion" (when !$is_bibliography_aware && bibliography_settings['enabled'] == 0 )
34
 *       - $footnote_list_key_suggestion (when $is_bibliography_aware)
35
 *
36 fe064e2d Andreas Kohlbecker
 * @param $cdm_entity
37
 *   A CDM entity
38 dd0767b6 Andreas Kohlbecker
 * @param string $separator
39
 *   Optional parameter. The separator string to concatenate the footnote ids, default is ','
40
 * @param $footnote_list_key_suggestion string
41
 *    Optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be determined by the nested
42
 *    method calls by calling RenderHints::getFootnoteListKey(). NOTE: the footnote key for annotations will be set to
43
 *    RenderHints::getFootnoteListKey().'-annotations'.
44 51ace96f Andreas Kohlbecker
 *    For original sources the $footnote_list_key_suggestion will be overwritten by bibliography_footnote_list_key() when
45
 *    $is_bibliography_aware is set TRUE.
46 dd0767b6 Andreas Kohlbecker
 * @param bool $do_link_to_reference
47
 *    Create a link to the reference pages for sources when TRUE.
48
 * @param bool $do_link_to_name_used_in_source
49
 *    Create a link to the name pages for name in source when TRUE.
50
 * @param bool $is_bibliography_aware
51
 *    Put source references into the bibliography when this param is TRUE.
52 51ace96f Andreas Kohlbecker
 *    For original sources the $footnote_list_key_suggestion will be overwritten
53
 *    by bibliography_footnote_list_key() when
54
 *    $is_bibliography_aware is set TRUE.
55 dd0767b6 Andreas Kohlbecker
 * @return String
56 d8069342 Andreas Kohlbecker
 *   The foot note keys as markup
57 dd0767b6 Andreas Kohlbecker
 *
58 51ace96f Andreas Kohlbecker
 * NOTE: Only used in @see handle_annotations_and_sources()
59 dd0767b6 Andreas Kohlbecker
 */
60 d8069342 Andreas Kohlbecker
function render_entity_footnotes(
61 fe064e2d Andreas Kohlbecker
  $cdm_entity,
62 dd0767b6 Andreas Kohlbecker
  $separator = ',',
63
  $footnote_list_key_suggestion = null,
64
  $do_link_to_reference = FALSE,
65
  $do_link_to_name_used_in_source = FALSE,
66
  $is_bibliography_aware = FALSE
67
){
68
69 fe064e2d Andreas Kohlbecker
  $sources = cdm_entity_sources_sorted($cdm_entity);
70 dd0767b6 Andreas Kohlbecker
71
  // Annotations as footnotes.
72 fe064e2d Andreas Kohlbecker
  $footnote_keys = cdm_entity_annotations_as_footnote_keys($cdm_entity, $footnote_list_key_suggestion);
73 dd0767b6 Andreas Kohlbecker
74
  // Source references as footnotes.
75
  if($is_bibliography_aware){
76
    $bibliography_settings = get_bibliography_settings();
77
    $sources_footnote_list_key = bibliography_footnote_list_key($footnote_list_key_suggestion);
78
    $original_source_footnote_tag = $bibliography_settings['enabled'] == 1 ? 'div' : null; // null will cause bibliography_footnote_list_key to use the default
79
  } else {
80
    $sources_footnote_list_key = $footnote_list_key_suggestion;
81
    if(!$sources_footnote_list_key) {
82
      RenderHints::getFootnoteListKey();
83
    }
84
    $original_source_footnote_tag = NULL;
85
  }
86
87
  foreach ($sources as $source) {
88
    if (_is_original_source_type($source)) {
89
      $fn_key = FootnoteManager::addNewFootnote(
90
        $sources_footnote_list_key,
91
        render_original_source(
92
          $source,
93
          $do_link_to_reference,
94
          $do_link_to_name_used_in_source
95
        ),
96
        $original_source_footnote_tag
97
      );
98
      // Ensure uniqueness of the footnote keys.
99 fe064e2d Andreas Kohlbecker
      if(array_search($fn_key, $footnote_keys)=== false) {
100
        $footnote_keys[] = $fn_key;
101
      }
102 dd0767b6 Andreas Kohlbecker
    }
103
  }
104
  // Sort and render footnote keys.
105
  asort($footnote_keys);
106 d8069342 Andreas Kohlbecker
  return render_footnote_keys($footnote_keys, $separator);
107 dd0767b6 Andreas Kohlbecker
}
108
109
/**
110
 * Fetches the list of visible annotations for each of the cdm entities and returns the footnote keys for them.
111
 *
112
 * The footnotes are passed to the FootnoteManager in order to store the annotations and to create the footnote keys.
113
 *
114
 * @see cdm_fetch_visible_annotations()
115
 *
116
 * @param array $cdm_entities
117
 *   An array of CdmBase instances.
118
 * @param $footnote_list_key_suggestion string
119
 *    optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be determined be set to
120 fe064e2d Andreas Kohlbecker
 *    RenderHints::getFootnoteListKey().FOOTNOTE_KEY_SUFFIX_ANNOTATIONS otherwise the supplied key will be used.
121 dd0767b6 Andreas Kohlbecker
 *
122
 * @return array of footnote keys
123 51ace96f Andreas Kohlbecker
 *
124
 * @deprecated unused, needs to be removed
125 dd0767b6 Andreas Kohlbecker
 */
126 fe064e2d Andreas Kohlbecker
function cdm_entities_annotations_footnote_keys(array $cdm_entities, $footnote_list_key_suggestion = NULL) {
127 dd0767b6 Andreas Kohlbecker
128
  $foot_note_keys = [];
129
  foreach ($cdm_entities as $cdm_entity) {
130 fe064e2d Andreas Kohlbecker
    $foot_note_keys = array_merge($foot_note_keys, cdm_entity_annotations_as_footnote_keys($cdm_entity,$footnote_list_key_suggestion));
131 dd0767b6 Andreas Kohlbecker
  }
132
133
  return $foot_note_keys;
134
}
135
136
/**
137
 * Fetches the list of visible annotations for the cdm entity or for the comparable
138
 * object and returns the footnote keys.
139
 *
140
 * The footnotes are passed to the FootnoteManager in order to store the
141
 * annotations and to create the footnote keys.
142
143
 * @param stdClass $cdm_entity
144
 *   A single CdmBase instance ore comparable object.
145
 * @param $footnote_list_key_suggestion string
146
 *    optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be determined be set to
147
 *    RenderHints::getFootnoteListKey().'-annotations' otherwise the supplied key will be used.
148
 * @return array of footnote keys
149
 *
150
 * @see cdm_fetch_visible_annotations()
151
 */
152 fe064e2d Andreas Kohlbecker
function cdm_entity_annotations_as_footnote_keys(stdClass $cdm_entity, $footnote_list_key_suggestion = NULL) {
153 dd0767b6 Andreas Kohlbecker
154
  $foot_note_keys = [];
155
156 d8069342 Andreas Kohlbecker
  // Getting the key for the FootnoteManager.
157 dd0767b6 Andreas Kohlbecker
  if (isset($footnote_list_key_suggestion)) {
158
    $footnote_list_key = $footnote_list_key_suggestion;
159
  } else {
160 fe064e2d Andreas Kohlbecker
    $footnote_list_key = RenderHints::getFootnoteListKey() . FOOTNOTE_KEY_SUFFIX_ANNOTATIONS;
161 e31c69e3 Andreas Kohlbecker
    throw new Exception("FOOTNOTE_KEY_SUFFIX_ANNOTATIONS");
162 dd0767b6 Andreas Kohlbecker
  }
163
164
  // Adding the footnotes keys.
165
  $annotations = cdm_fetch_visible_annotations($cdm_entity);
166
  if (is_array($annotations)) {
167
    foreach ($annotations as $annotation) {
168
      $foot_note_keys[] = FootnoteManager::addNewFootnote($footnote_list_key, $annotation->text);
169
    }
170
  }
171
172
  return $foot_note_keys;
173
}
174
175 d8069342 Andreas Kohlbecker
/**
176
 * Created and registers footnotes in the FootnoteManager and returns the
177
 * footnote keys as markup.
178
 *
179
 * The following cdm cdm classes are annotatable:
180
 *
181
 * - DescriptionElementBase
182
 * - EventBase
183
 * - HomotypicalGroup
184
 * - IdentifiableEntity
185
 * - DescriptionBase
186
 * - IdentifiableMediaEntity
187
 * - Media
188
 * - Sequence
189
 * - TaxonBase
190
 * - TaxonName
191
 * - TaxonomicTree
192
 * - TermBase
193
 * - LanguageStringBase
194
 * - ReferencedEntityBase
195
 * - NomenclaturalStatus
196
 * - OriginalSourceBase
197
 * - RelationshipBase
198
 * - TypeDesignationBase
199
 * - TaxonNode
200
 * - WorkingSet
201
 *
202
 * @param array $cdm_entities
203
 *   An array of CdmBase instances.
204
 * @param $footnote_list_key_suggestion string
205
 *    optional parameter. If this parameter is left empty (null, 0, "") the
206
 *   footnote key will be determined be set to
207
 *   RenderHints::getFootnoteListKey().FOOTNOTE_KEY_SUFFIX_ANNOTATIONS otherwise the supplied
208
 *   key will be used.
209
 *
210
 * @return string
211
 *   The markup.
212 51ace96f Andreas Kohlbecker
 * @deprecated unused, needs to be removed
213 d8069342 Andreas Kohlbecker
 */
214
function render_entities_annotations_as_footnote_keys(array $cdm_entities, $footnote_list_key =null){
215
216
  // check if footnotes for annotations are disabled completely
217
  if (variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
218
    return '';
219
  }
220
  $markup = '';
221
  $footNoteKeys = cdm_entities_annotations_footnote_keys($cdm_entities, $footnote_list_key);
222
  foreach ($footNoteKeys as $a) {
223
    $markup .= render_footnote_key($a, ($markup ? ',' : ''));
224
  }
225
  return $markup;
226
}
227
228 dd0767b6 Andreas Kohlbecker
/**
229
 * Creates markup for an array of foot note keys
230
 *
231
 * @param array $footnote_keys
232
 * @param string $separator
233
 *
234
 * @return string
235
 */
236 d8069342 Andreas Kohlbecker
function render_footnote_keys(array $footnote_keys, $separator) {
237 dd0767b6 Andreas Kohlbecker
238
  $footnotes_markup = '';
239
  foreach ($footnote_keys as $foot_note_key) {
240
    try {
241 d8069342 Andreas Kohlbecker
      $footnotes_markup .= render_footnote_key($foot_note_key, ($footnotes_markup ? $separator : ''));
242 dd0767b6 Andreas Kohlbecker
    } catch (Exception $e) {
243
      drupal_set_message("Exception: " . $e->getMessage(), 'error');
244
    }
245
  }
246
  return $footnotes_markup;
247
}
248
249
/**
250
 * Create markup for the footnotes mapped to the $footnoteListKey.
251
 *
252
 * @param null $footnote_list_key
253
 *  The footnote list key, see RenderHints::getFootnoteListKey()
254
 * @param $element_tag
255
 *  The tag for the footnote element
256
 *
257
 * @return string
258
 */
259 d8069342 Andreas Kohlbecker
function render_annotation_footnotes($footnote_list_key = null, $element_tag = 'span') {
260 dd0767b6 Andreas Kohlbecker
  if (variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
261
    return '';
262
  }
263 d8069342 Andreas Kohlbecker
  return render_footnotes($footnote_list_key . FOOTNOTE_KEY_SUFFIX_ANNOTATIONS, $element_tag);
264 dd0767b6 Andreas Kohlbecker
}
265
266
/**
267
 * Creates markup for a foot note key
268
 *
269
 * @param null $footnoteKey
270
 * @param string $separator
271
 * @param bool $separator_off
272
 *
273
 * @return string
274
 *   The footnote key markup
275
 */
276 d8069342 Andreas Kohlbecker
function render_footnote_key($footnoteKey = null, $separator = '', $separator_off = false) {
277 dd0767b6 Andreas Kohlbecker
278
  if (!is_object($footnoteKey) or !isset($footnoteKey->footnoteListKey)) {
279
    return '';
280
  }
281
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
282
    return '';
283
  }
284
285
  if ($separator_off) {
286
    $separator = '';
287
  }
288
  $out = '<span class="footnote-key footnote-key-' . $footnoteKey->keyStr . ' member-of-footnotes-' . $footnoteKey->footnoteListKey . '">'
289
    . $separator . '<a href="#footnote-' . $footnoteKey->keyStr . '">' . $footnoteKey->keyStr . '</a>' . '</span>';
290
  return $out;
291
}
292
293
/**
294
 * @param null $footnoteKey
295
 * @param null $footnoteText
296
 * @param string $enclosing_tag
297
 *   default is 'span'
298
 *
299
 * @return string
300
 */
301 d8069342 Andreas Kohlbecker
function render_footnote($footnoteKey = null, $footnoteText = null, $enclosing_tag = 'span') {
302 dd0767b6 Andreas Kohlbecker
  _add_js_footnotes();
303
  if($enclosing_tag == null){
304
    $enclosing_tag = 'span';
305
  }
306
  return '<' . $enclosing_tag . ' class="footnote footnote-' . $footnoteKey . '">'
307
    . '<a name="footnote-' . $footnoteKey . '"></a>'
308
    . '<span class="footnote-anchor">' . $footnoteKey . '.</span>&nbsp;' . $footnoteText
309
    . '</' . $enclosing_tag . '>';
310
}
311
312
313
314
/**
315
 * Create markup for the footnotes mapped to the $footnoteListKey.
316
 *
317
 * @param null $footnote_list_key
318
 *  The footnote list key, see RenderHints::getFootnoteListKey()
319
 * @param $element_tag
320
 *  The tag for the footnote element
321
 *
322
 * @return string
323 3c4a5472 Andreas Kohlbecker
 *   The markup
324 dd0767b6 Andreas Kohlbecker
 */
325 d8069342 Andreas Kohlbecker
function render_footnotes($footnote_list_key = null, $element_tag = 'span') {
326 dd0767b6 Andreas Kohlbecker
327
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
328
    return '';
329
  }
330
331
  $out = '<' . $element_tag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">'
332
    . FootnoteManager::renderFootnoteList($footnote_list_key)
333
    . '</' . $element_tag . '>';
334
335
  FootnoteManager::removeFootnoteList($footnote_list_key);
336
  return $out;
337
}
338
339
/**
340
 * Renders the footnotes for annotations and sources, etc.
341
 *
342
 * @param string $footnote_list_key
343
 *    RenderHints::getFootnoteListKey() will be used if this parameter is undefined.
344
 * @param string $enclosingTag
345
 *    Default tag is 'span'
346
 *
347
 * @return string
348
 *    The markup string
349 fc6585f9 Andreas Kohlbecker
 * @deprecated to be replaced by render_footnotes() once FOOTNOTE_KEY_SUFFIX_ANNOTATIONS is no longer used
350 dd0767b6 Andreas Kohlbecker
 */
351 fc6585f9 Andreas Kohlbecker
function render_footnotes_annotation_and_sources($footnote_list_key = NULL, $enclosingTag = 'span'){
352 dd0767b6 Andreas Kohlbecker
353
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
354
    return '';
355
  }
356
  if(!$footnote_list_key){
357
    $footnote_list_key = RenderHints::getFootnoteListKey();
358
  }
359
360
  $out = '<' . $enclosingTag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">'
361 fe064e2d Andreas Kohlbecker
    . FootnoteManager::renderFootnoteList($footnote_list_key . FOOTNOTE_KEY_SUFFIX_ANNOTATIONS) . ' ' . FootnoteManager::renderFootnoteList($footnote_list_key)
362 dd0767b6 Andreas Kohlbecker
    . '</' . $enclosingTag . '>';
363 fe064e2d Andreas Kohlbecker
  FootnoteManager::removeFootnoteList($footnote_list_key . FOOTNOTE_KEY_SUFFIX_ANNOTATIONS);
364 dd0767b6 Andreas Kohlbecker
  FootnoteManager::removeFootnoteList($footnote_list_key);
365
  return $out;
366
}