Project

General

Profile

Download (10.9 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

    
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
27
 *     - annotation footnotes:
28
 *       - $footnote_list_key_suggestion
29
 *       - RenderHints::getFootnoteListKey().'-annotations'
30
 *     - original source footnotes
31
 *       - "BIBLIOGRAPHY" (when !$is_bibliography_aware && bibliography_settings['enabled'] == 1 )
32
 *       - "BIBLIOGRAPHY-$footnote_list_key_suggestion" (when !$is_bibliography_aware && bibliography_settings['enabled'] == 0 )
33
 *       - $footnote_list_key_suggestion (when $is_bibliography_aware)
34
 *
35
 * @param $description_element
36
 *   A CDM DescriptionElement instance
37
 * @param string $separator
38
 *   Optional parameter. The separator string to concatenate the footnote ids, default is ','
39
 * @param $footnote_list_key_suggestion 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(). NOTE: the footnote key for annotations will be set to
42
 *    RenderHints::getFootnoteListKey().'-annotations'.
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
 *
50
 * @return String
51
 *   The foot note keys
52
 *
53
 * @throws \Exception re-throw exception from theme()
54
 * @see cdm_entities_annotations_footnotekeys()
55
 *    For original sources the $footnote_list_key_suggestion will be overwritten by bibliography_footnote_list_key() when
56
 *    $is_bibliography_aware is set TRUE.
57
 * @$original_source_footnote_tag
58
 *    null will cause bibliography_footnote_list_key to use the default
59
 */
60
function cdm_create_footnotes(
61
  $description_element,
62
  $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
  $sources = cdm_entity_sources_sorted($description_element);
70

    
71
  // Annotations as footnotes.
72
  $footnote_keys = cdm_entity_annotations_as_footnotekeys($description_element, $footnote_list_key_suggestion);
73

    
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
      cdm_add_footnote_to_array($footnote_keys, $fn_key);
100
    }
101
  }
102
  // Sort and render footnote keys.
103
  asort($footnote_keys);
104
  return footnote_keys_to_markup($footnote_keys, $separator);
105
}
106

    
107
/**
108
 * Fetches the list of visible annotations for each of the cdm entities and returns the footnote keys for them.
109
 *
110
 * The footnotes are passed to the FootnoteManager in order to store the annotations and to create the footnote keys.
111
 *
112
 * @see cdm_fetch_visible_annotations()
113
 *
114
 * @param array $cdm_entities
115
 *   An array of CdmBase instances.
116
 * @param $footnote_list_key_suggestion string
117
 *    optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be determined be set to
118
 *    RenderHints::getFootnoteListKey().'-annotations' otherwise the supplied key will be used.
119
 *
120
 * @return array of footnote keys
121
 */
122
function cdm_entities_annotations_footnotekeys(array $cdm_entities, $footnote_list_key_suggestion = NULL) {
123

    
124
  $foot_note_keys = [];
125
  foreach ($cdm_entities as $cdm_entity) {
126
    $foot_note_keys = array_merge($foot_note_keys, cdm_entity_annotations_as_footnotekeys($cdm_entity,$footnote_list_key_suggestion));
127
  }
128

    
129
  return $foot_note_keys;
130
}
131

    
132
/**
133
 * Fetches the list of visible annotations for the cdm entity or for the comparable
134
 * object and returns the footnote keys.
135
 *
136
 * The footnotes are passed to the FootnoteManager in order to store the
137
 * annotations and to create the footnote keys.
138

    
139
 * @param stdClass $cdm_entity
140
 *   A single CdmBase instance ore comparable object.
141
 * @param $footnote_list_key_suggestion string
142
 *    optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be determined be set to
143
 *    RenderHints::getFootnoteListKey().'-annotations' otherwise the supplied key will be used.
144
 * @return array of footnote keys
145
 *
146
 * @see cdm_fetch_visible_annotations()
147
 */
148
function cdm_entity_annotations_as_footnotekeys(stdClass $cdm_entity, $footnote_list_key_suggestion = NULL) {
149

    
150
  $foot_note_keys = [];
151

    
152
  // Getting the key for the footnotemanager.
153
  if (isset($footnote_list_key_suggestion)) {
154
    $footnote_list_key = $footnote_list_key_suggestion;
155
  } else {
156
    $footnote_list_key = RenderHints::getFootnoteListKey() . '-annotations';
157
  }
158

    
159
  // Adding the footnotes keys.
160
  $annotations = cdm_fetch_visible_annotations($cdm_entity);
161
  if (is_array($annotations)) {
162
    foreach ($annotations as $annotation) {
163
      $foot_note_keys[] = FootnoteManager::addNewFootnote($footnote_list_key, $annotation->text);
164
    }
165
  }
166

    
167
  return $foot_note_keys;
168
}
169

    
170
/**
171
 * Creates markup for an array of foot note keys
172
 *
173
 * @param array $footnote_keys
174
 * @param string $separator
175
 *
176
 * @return string
177
 */
178
function footnote_keys_to_markup(array $footnote_keys, $separator) {
179

    
180
  $footnotes_markup = '';
181
  foreach ($footnote_keys as $foot_note_key) {
182
    try {
183
      $footnotes_markup .= cdm_footnote_key($foot_note_key, ($footnotes_markup ? $separator : ''));
184
    } catch (Exception $e) {
185
      drupal_set_message("Exception: " . $e->getMessage(), 'error');
186
    }
187
  }
188
  return $footnotes_markup;
189
}
190

    
191
/**
192
 * @todo Please document this function.
193
 * @see http://drupal.org/node/1354
194
 */
195
function cdm_add_footnote_to_array(&$footnote_list, $footnote) {
196
  if (!cdm_exist_footnote($footnote_list, $footnote)) {
197
    $footnote_list[] = $footnote;
198
  }
199
}
200

    
201

    
202
/**
203
 * Create markup for the footnotes mapped to the $footnoteListKey.
204
 *
205
 * @param null $footnote_list_key
206
 *  The footnote list key, see RenderHints::getFootnoteListKey()
207
 * @param $element_tag
208
 *  The tag for the footnote element
209
 *
210
 * @return string
211
 * @throws \Exception
212
 */
213
function cdm_annotation_footnotes($footnote_list_key = null, $element_tag = 'span') {
214
  if (variable_get('cdm_dataportal_annotations_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
215
    return '';
216
  }
217
  return cdm_footnotes($footnote_list_key . '-annotations', $element_tag);
218
}
219

    
220
/**
221
 * Creates markup for a foot note key
222
 *
223
 * @param null $footnoteKey
224
 * @param string $separator
225
 * @param bool $separator_off
226
 *
227
 * @return string
228
 *   The footnote key markup
229
 */
230
function cdm_footnote_key($footnoteKey = null, $separator = '', $separator_off = false) {
231

    
232
  if (!is_object($footnoteKey) or !isset($footnoteKey->footnoteListKey)) {
233
    return '';
234
  }
235
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
236
    return '';
237
  }
238

    
239
  if ($separator_off) {
240
    $separator = '';
241
  }
242
  $out = '<span class="footnote-key footnote-key-' . $footnoteKey->keyStr . ' member-of-footnotes-' . $footnoteKey->footnoteListKey . '">'
243
    . $separator . '<a href="#footnote-' . $footnoteKey->keyStr . '">' . $footnoteKey->keyStr . '</a>' . '</span>';
244
  return $out;
245
}
246

    
247
/**
248
 * @param null $footnoteKey
249
 * @param null $footnoteText
250
 * @param string $enclosing_tag
251
 *   default is 'span'
252
 *
253
 * @return string
254
 */
255
function footnote_markup($footnoteKey = null, $footnoteText = null, $enclosing_tag = 'span') {
256
  _add_js_footnotes();
257
  if($enclosing_tag == null){
258
    $enclosing_tag = 'span';
259
  }
260
  return '<' . $enclosing_tag . ' class="footnote footnote-' . $footnoteKey . '">'
261
    . '<a name="footnote-' . $footnoteKey . '"></a>'
262
    . '<span class="footnote-anchor">' . $footnoteKey . '.</span>&nbsp;' . $footnoteText
263
    . '</' . $enclosing_tag . '>';
264
}
265

    
266

    
267

    
268
/**
269
 * Create markup for the footnotes mapped to the $footnoteListKey.
270
 *
271
 * @param null $footnote_list_key
272
 *  The footnote list key, see RenderHints::getFootnoteListKey()
273
 * @param $element_tag
274
 *  The tag for the footnote element
275
 *
276
 * @return string
277
 */
278
function cdm_footnotes($footnote_list_key = null, $element_tag = 'span') {
279

    
280
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
281
    return '';
282
  }
283

    
284
  $out = '<' . $element_tag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">'
285
    . FootnoteManager::renderFootnoteList($footnote_list_key)
286
    . '</' . $element_tag . '>';
287

    
288
  FootnoteManager::removeFootnoteList($footnote_list_key);
289
  return $out;
290
}
291

    
292
/**
293
 * Renders the footnotes for annotations and sources, etc.
294
 *
295
 * @param string $footnote_list_key
296
 *    RenderHints::getFootnoteListKey() will be used if this parameter is undefined.
297
 * @param string $enclosingTag
298
 *    Default tag is 'span'
299
 *
300
 * @return string
301
 *    The markup string
302
 *
303
 */
304
function render_cdm_footnotes($footnote_list_key = NULL, $enclosingTag = 'span'){
305

    
306
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
307
    return '';
308
  }
309
  if(!$footnote_list_key){
310
    $footnote_list_key = RenderHints::getFootnoteListKey();
311
  }
312

    
313
  $out = '<' . $enclosingTag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">'
314
    . FootnoteManager::renderFootnoteList($footnote_list_key . '-annotations') . ' ' . FootnoteManager::renderFootnoteList($footnote_list_key)
315
    . '</' . $enclosingTag . '>';
316
  FootnoteManager::removeFootnoteList($footnote_list_key . '-annotations');
317
  FootnoteManager::removeFootnoteList($footnote_list_key);
318
  return $out;
319
}
320

    
321
/**
322
 * @todo Please document this function.
323
 * @see http://drupal.org/node/1354
324
 */
325
function cdm_exist_footnote($footnote_list, $footnote) {
326
  $result = FALSE;
327
  if (is_array($footnote_list)) {
328
    foreach ($footnote_list as $element) {
329
      if ($element == $footnote) {
330
        $result = TRUE;
331
      }
332
    }
333
  }
334
  return $result;
335
}
(4-4/14)