Project

General

Profile

Download (7.65 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 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
  // Annotations as footnotes.
69
  $footnote_keys = cdm_entity_annotations_as_footnote_keys($cdm_entity, $footnote_list_key_suggestion);
70

    
71
  // Source references as footnotes.
72
  if($is_bibliography_aware){
73
    $bibliography_settings = get_bibliography_settings();
74
    $sources_footnote_list_key = bibliography_footnote_list_key($footnote_list_key_suggestion);
75
    $original_source_footnote_tag = $bibliography_settings['enabled'] == 1 ? 'div' : null; // null will cause bibliography_footnote_list_key to use the default
76
  } else {
77
    $sources_footnote_list_key = $footnote_list_key_suggestion;
78
    if(!$sources_footnote_list_key) {
79
      RenderHints::getFootnoteListKey();
80
    }
81
    $original_source_footnote_tag = NULL;
82
  }
83

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

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

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

    
125
  $foot_note_keys = [];
126

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

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

    
139
  return $foot_note_keys;
140
}
141

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

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

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

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

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

    
190
/**
191
 * @param null $footnoteKey
192
 * @param null $footnoteText
193
 * @param string $enclosing_tag
194
 *   default is 'span'
195
 *
196
 * @return string
197
 */
198
function render_footnote($footnoteKey = null, $footnoteText = null, $enclosing_tag = 'span') {
199
  _add_js_footnotes();
200
  if($enclosing_tag == null){
201
    $enclosing_tag = 'span';
202
  }
203
  return '<' . $enclosing_tag . ' class="footnote footnote-' . $footnoteKey . '">'
204
    . '<a name="footnote-' . $footnoteKey . '"></a>'
205
    . '<span class="footnote-anchor">' . $footnoteKey . '.</span>&nbsp;' . $footnoteText
206
    . '</' . $enclosing_tag . '>';
207
}
208

    
209

    
210

    
211
/**
212
 * Create markup for the footnotes mapped to the $footnoteListKey.
213
 *
214
 * @param null $footnote_list_key
215
 *  The footnote list key, see RenderHints::getFootnoteListKey()
216
 * @param $element_tag
217
 *  The tag for the footnote element
218
 *
219
 * @return string
220
 *   The markup
221
 */
222
function render_footnotes($footnote_list_key = null, $element_tag = 'span') {
223

    
224
  if (variable_get('cdm_dataportal_all_footnotes', CDM_DATAPORTAL_ALL_FOOTNOTES)) {
225
    return '';
226
  }
227

    
228
  $out = '<' . $element_tag . ' class="footnotes footnotes-' . $footnote_list_key . ' ">'
229
    . FootnoteManager::renderFootnoteList($footnote_list_key)
230
    . '</' . $element_tag . '>';
231

    
232
  FootnoteManager::removeFootnoteList($footnote_list_key);
233
  return $out;
234
}
235

    
(4-4/14)