Project

General

Profile

Download (11.7 KB) Statistics
| Branch: | Tag: | Revision:
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
const MEMBER_OF_FOOTNOTES = ' member-of-footnotes-';
19

    
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 and $is_bibliography_aware, see parameter $footnote_list_key
25
 * for more details.
26
 *
27
 * possible keys for annotation and source footnotes:
28
 *       - $footnote_list_key
29
 *       - RenderHints::getFootnoteListKey()
30
 *     - original source footnotes
31
 *       - "BIBLIOGRAPHY" (when !$is_bibliography_aware && bibliography_settings['enabled'] == 1 )
32
 *       - "BIBLIOGRAPHY-$footnote_list_key" (when !$is_bibliography_aware && bibliography_settings['enabled'] == 0 )
33
 *       - $footnote_list_key (when $is_bibliography_aware)
34
 *
35
 * @param $cdm_entity
36
 *   A CDM entity
37
 * @param string $separator
38
 *   Optional parameter. The separator string to concatenate the footnote ids, default is ','
39
 * @param $footnote_list_key string
40
 *    Optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be determined by the nested
41
 *    method calls by calling RenderHints::getFootnoteListKey().
42
 *    For original sources the $footnote_list_key will be overwritten by bibliography_footnote_list_key() when
43
 *    $is_bibliography_aware is set TRUE.
44
 * @param bool $do_link_to_reference
45
 *    Create a link to the reference pages for sources when TRUE.
46
 * @param bool $do_link_to_name_used_in_source
47
 *    Create a link to the name pages for name in source when TRUE.
48
 * @param bool $is_bibliography_aware
49
 *    Put source references into the bibliography when this param is TRUE.
50
 *    For original sources the $footnote_list_key will be overwritten
51
 *    by bibliography_footnote_list_key() when
52
 *    $is_bibliography_aware is set TRUE.
53
 * @return String
54
 *   The foot note keys as markup
55
 *
56
 * NOTE: Only used in @see handle_annotations_and_sources()
57
 */
58
function render_entity_footnotes(
59
  $cdm_entity,
60
  $separator = ',',
61
  $footnote_list_key = null,
62
  $do_link_to_reference = FALSE,
63
  $do_link_to_name_used_in_source = FALSE,
64
  $is_bibliography_aware = FALSE
65
){
66

    
67
  $sources = cdm_entity_sources_sorted($cdm_entity);
68

    
69
  if (!isset($footnote_list_key) || !$footnote_list_key) {
70
    $footnote_list_key = RenderHints::getFootnoteListKey();
71
  }
72

    
73
  // Annotations as footnotes.
74
  $footnote_keys = cdm_entity_annotations_as_footnote_keys($cdm_entity, $footnote_list_key);
75

    
76
  // Source references as footnotes.
77
  if($is_bibliography_aware){
78
    $bibliography_settings = get_bibliography_settings();
79
    $sources_footnote_list_key = bibliography_footnote_list_key($footnote_list_key);
80
    $original_source_footnote_tag = $bibliography_settings['enabled'] == 1 ? 'div' : null; // null will cause bibliography_footnote_list_key to use the default
81
  } else {
82
    $sources_footnote_list_key = $footnote_list_key;
83
    $original_source_footnote_tag = NULL;
84
  }
85

    
86
  foreach ($sources as $source) {
87
    if (_is_original_source_type($source)) {
88
      $fn_key = FootnoteManager::addNewFootnote(
89
        $sources_footnote_list_key,
90
        render_original_source(
91
          $source,
92
          $do_link_to_reference,
93
          $do_link_to_name_used_in_source
94
        ),
95
        $original_source_footnote_tag
96
      );
97
      // Ensure uniqueness of the footnote keys.
98
      if(array_search($fn_key, $footnote_keys)=== false) {
99
        $footnote_keys[] = $fn_key;
100
      }
101
    }
102
  }
103
  // Sort and render footnote keys.
104
  asort($footnote_keys);
105
  return render_footnote_keys($footnote_keys, $separator);
106
}
107

    
108
/**
109
 * Fetches the list of visible annotations for the cdm entity or for the comparable
110
 * object and returns the footnote keys.
111
 *
112
 * The footnotes are passed to the FootnoteManager in order to store the
113
 * annotations and to create the footnote keys.
114

    
115
 * @param stdClass $cdm_entity
116
 *   A single CdmBase instance ore comparable object.
117
 * @param $footnote_list_key string
118
 *    optional parameter. If this parameter is left empty (null, 0, "") the
119
 *    footnote key will be set to RenderHints::getFootnoteListKey()
120
 *    otherwise the supplied $footnote_list_key will be used.
121
 * @return array of footnote keys
122
 *
123
 * @see cdm_fetch_visible_annotations()
124
 */
125
function cdm_entity_annotations_as_footnote_keys(stdClass $cdm_entity, $footnote_list_key = NULL) {
126

    
127
  $foot_note_keys = [];
128

    
129
  if (!isset($footnote_list_key) || !$footnote_list_key) {
130
    $footnote_list_key = RenderHints::getFootnoteListKey();
131
  }
132

    
133
  // Adding the footnotes keys.
134
  $annotations = cdm_fetch_visible_annotations($cdm_entity);
135
  if (is_array($annotations)) {
136
    foreach ($annotations as $annotation) {
137
      $foot_note_keys[] = FootnoteManager::addNewFootnote($footnote_list_key, $annotation->text);
138
    }
139
  }
140

    
141
  return $foot_note_keys;
142
}
143

    
144
/**
145
 * Creates markup for an array of foot note keys
146
 *
147
 * @param array $footnote_keys
148
 * @param string $separator
149
 *
150
 * @return string
151
 */
152
function render_footnote_keys(array $footnote_keys, $separator) {
153

    
154
  $footnotes_markup = '';
155
  foreach ($footnote_keys as $foot_note_key) {
156
    try {
157
      $footnotes_markup .= render_footnote_key($foot_note_key, ($footnotes_markup ? $separator : ''));
158
    } catch (Exception $e) {
159
      drupal_set_message("Exception: " . $e->getMessage(), 'error');
160
    }
161
  }
162
  return $footnotes_markup;
163
}
164

    
165
/**
166
 * Creates markup for a foot note key
167
 *
168
 * @param null $footnoteKey
169
 * @param string $separator
170
 * @param bool $separator_off
171
 *
172
 * @return string
173
 *   The footnote key markup
174
 */
175
function render_footnote_key($footnoteKey = null, $separator = '', $separator_off = false) {
176

    
177
  if (!is_object($footnoteKey) or !isset($footnoteKey->footnoteListKey)) {
178
    return '';
179
  }
180
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
181
    return '';
182
  }
183

    
184
  if ($separator_off) {
185
    $separator = '';
186
  }
187
  $out = '<span class="footnote-key footnote-key-' . $footnoteKey->keyStr . MEMBER_OF_FOOTNOTES . $footnoteKey->footnoteListKey . '">'
188
    . $separator . '<a href="#footnote-' . $footnoteKey->keyStr . '">' . $footnoteKey->keyStr . '</a>' . '</span>';
189
  return $out;
190
}
191

    
192
/**
193
 * Create the markup for a footnote. This method is used in {@link Footnote::doRender()}
194
 * @param null $footnoteKey
195
 * @param null $footnoteText
196
 * @param string $enclosing_tag
197
 *   default is 'span'
198
 *
199
 * @return string
200
 *
201
 */
202
function render_footnote($footnoteKey = null, $footnoteText = null, $enclosing_tag = 'span', $footnote_list_key = null) {
203
  _add_js_footnotes();
204
  if($enclosing_tag == null){
205
    $enclosing_tag = 'span';
206
  }
207
  $class_attribute_member_of = '';
208
  if($footnote_list_key){
209
    $class_attribute_member_of = MEMBER_OF_FOOTNOTES . $footnote_list_key;
210
  }
211
  return '<' . $enclosing_tag . ' class="footnote footnote-' . $footnoteKey . $class_attribute_member_of . '">'
212
    . '<a name="footnote-' . $footnoteKey . '"></a>'
213
    . '<span class="footnote-anchor">' . $footnoteKey . '.</span>&nbsp;' . $footnoteText
214
    . '</' . $enclosing_tag . '>';
215
}
216

    
217

    
218

    
219
/**
220
 * Create markup for the footnotes mapped to the $footnoteListKey.
221
 *
222
 * @param null $footnote_list_key
223
 *  The footnote list key, see RenderHints::getFootnoteListKey()
224
 * @param $element_tag
225
 *  The tag for the footnote element
226
 *
227
 * @return string
228
 *   The markup
229
 */
230
function render_footnotes($footnote_list_key = null, $element_tag = 'span') {
231

    
232
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
233
    return '';
234
  }
235

    
236
  if (!isset($footnote_list_key) || !$footnote_list_key) {
237
    $footnote_list_key = RenderHints::getFootnoteListKey();
238
  }
239

    
240
  $out = '<' . $element_tag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">'
241
    . FootnoteManager::renderFootnoteList($footnote_list_key)
242
    . '</' . $element_tag . '>';
243

    
244
  FootnoteManager::removeFootnoteList($footnote_list_key);
245
  return $out;
246
}
247

    
248
/**
249
 * This method determines the footnote key for original sources to be shown in the bibliography block
250
 *
251
 * The footnote key depends on the value of the 'enabled' value of the bibliography_settings
252
 *    - enabled == 1 -> "BIBLIOGRAPHY"
253
 *    - enabled == 0 -> "BIBLIOGRAPHY-$key_suggestion"
254
 *
255
 * @see get_bibliography_settings() and @see constant BIBLIOGRAPHY_FOOTNOTE_KEY
256
 *
257
 * @param $key_suggestion string
258
 *    optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be retrieved by
259
 *    calling RenderHints::getFootnoteListKey().
260

    
261
 *
262
 * @return string
263
 *  the footnote_list_key
264
 */
265
function bibliography_footnote_list_key($key_suggestion = null) {
266
  if(!$key_suggestion){
267
    $key_suggestion = RenderHints::getFootnoteListKey();
268
  }
269
  $bibliography_settings = get_bibliography_settings();
270
  $footnote_list_key = $bibliography_settings['enabled'] == 1 ? BIBLIOGRAPHY_FOOTNOTE_KEY : BIBLIOGRAPHY_FOOTNOTE_KEY . '-' . $key_suggestion;
271
  return $footnote_list_key;
272
}
273

    
274

    
275
/**
276
 * Creates footnote markup for the name relationship and
277
 * registers it in the {@link \FootnoteManager}. The resulting foonote
278
 * key is returned as markup.
279
 *
280
 * @param $name_rel
281
 *   The cdm name relationship
282
 * @return string
283
 *  The markup for the footnote key
284
 */
285
function handle_name_relationship_as_footnote($name_rel)
286
{
287
  $footnote_markup = '';
288
  $footnote_key_markup = '';
289
  if (isset($name_rel->ruleConsidered) && $name_rel->ruleConsidered) {
290
    $footnote_markup = '<span class="rule_considered">' . $name_rel->ruleConsidered . '</span> ';
291
  }
292
  if (isset($name_rel->source->citation)) {
293
    $footnote_markup .= '<span class="reference">' . $name_rel->source->citation->titleCache . '</span>';
294
  }
295
  if (isset($name_rel->source->citationMicroReference) && $name_rel->source->citationMicroReference) {
296
    $footnote_markup .= (isset($name_rel->source->citation) ? ':' : '') . ' <span class="reference_detail">' . $name_rel->source->citationMicroReference . '</span>';
297
  }
298
  if ($footnote_markup) {
299
    $fnkey = FootnoteManager::addNewFootnote(RenderHints::getFootnoteListKey(), $footnote_markup);
300
    $footnote_key_markup = render_footnote_key($fnkey,',',TRUE);
301
  }
302
  return $footnote_key_markup;
303
}
304

    
305
/**
306
 * Creates footnote markup for nomenclatural status and
307
 * registers it in the {@link \FootnoteManager}. The resulting foonote
308
 * key is returned as markup.
309
 *
310
 * @param $nom_status
311
 * @return string
312
 *  The markup for the footnote key
313
 */
314
function handle_nomenclatural_status_as_footnote($nom_status)
315
{
316
  // NomenclaturalStatus is a subclass of ReferencedEntityBase
317
  // and has the same structure as TaxonNameRelationship
318
  return handle_name_relationship_as_footnote($nom_status);
319
}
320

    
321
/**
322
 * Creates footnote markup for nomenclatural reference of the name and
323
 * registers it in the {@link \FootnoteManager}. The resulting foonote
324
 * key is returned as markup.
325
 *
326
 * @param $name
327
 * The name whose nomenclatural reference is to be shown as footnote
328
 * @return string
329
 *  The markup for the footnote key
330
 */
331
function handle_nomenclatural_reference_as_footnote($name)
332
{
333
  $footnote_markup = '';
334
  $footnote_key_markup = '';
335
  if (isset($name->nomenclaturalSource->citation) && $name->nomenclaturalSource->citation) {
336
    $footnote_markup .= '<span class="reference">' . $name->nomenclaturalSource->citation->titleCache . '</span>';
337
  }
338
  if (isset($name->nomenclaturalSource->citationMicroReference)) {
339
    $footnote_markup .= ($footnote_key_markup ? ':' : '') . '<span class="reference_detail">' . $name->nomenclaturalSource->citationMicroReference . '</span>';
340
  }
341
  if ($footnote_markup) {
342
    $fnkey = FootnoteManager::addNewFootnote(RenderHints::getFootnoteListKey(), $footnote_markup);
343
    $footnote_key_markup = render_footnote_key($fnkey, ',',TRUE);
344
  }
345
  return $footnote_key_markup;
346
}
347

    
348

    
(4-4/14)