Project

General

Profile

Download (88.6 KB) Statistics
| Branch: | Tag: | Revision:
1 aa7c7290 Andreas Kohlbecker
<?php
2 c3a41ddb Andreas Kohlbecker
3
const BIBLIOGRAPHY_FOOTNOTE_KEY = PSEUDO_FEATURE_BIBLIOGRAPHY;
4
5 aa7c7290 Andreas Kohlbecker
6 ce29c528 Andreas Kohlbecker
/**
7
 * Returns the localized representations of the modifiers hold by the
8
 * supplied cdm instance concatenated into one string.
9
 *
10 a6d7907d Andreas Kohlbecker
 * @param object $is_modifieable
11 ce29c528 Andreas Kohlbecker
 *   cdm instance of an class implementing the interface IModifieable:
12
 *   DescriptionElementBase, StateDate, State
13
 *
14
 * @return String
15
 *   localized representations of the modifiers hold by the
16
 *   supplied cdm instance concatenated into one string
17
 */
18 a6d7907d Andreas Kohlbecker
function cdm_modifiers_representations($is_modifieable, $glue = ', ') {
19 ce29c528 Andreas Kohlbecker
  $modifiers_strings = array();
20 a6d7907d Andreas Kohlbecker
  if (isset($is_modifieable->modifiers)) {
21
    foreach ($is_modifieable->modifiers as $modifier) {
22 ce29c528 Andreas Kohlbecker
      $modifiers_strings[] = cdm_term_representation($modifier);
23 f19f47fa Andreas Kohlbecker
    }
24 aa7c7290 Andreas Kohlbecker
  }
25 ce29c528 Andreas Kohlbecker
  return implode(', ', $modifiers_strings);
26
}
27 bf97c2e8 Andreas Kohlbecker
28 ce29c528 Andreas Kohlbecker
/**
29
 * Filters the given set of description elements and prefers computed elements over others.
30
 *
31
 * Computed description elements
32
 * are identified by the MarkerType.COMPUTED()
33
 *
34
 * If the given set contains at least one computed element only
35
 * the computed elements are returned.
36
 *
37
 * @param array $description_elements
38
 *   An array of CDM DescriptionElementBase instances
39
 *
40
 * @return array
41
 *   only the computed description elements otherwise all others.
42
 *
43
 * @deprecated this is replaced by the cdmlib DistributionUtil class!!!
44
 */
45
function cdm_description_elements_prefer_computed($description_elements){
46 19e097a3 Andreas Kohlbecker
47 ce29c528 Andreas Kohlbecker
  $computed_elements = array();
48
  $other_elements = array();
49 19e097a3 Andreas Kohlbecker
50 ce29c528 Andreas Kohlbecker
  if (!empty($description_elements)) {
51
    foreach ($description_elements as $element) {
52
      if (cdm_entity_has_marker($element, UUID_MARKERTYPE_COMPUTED)) {
53
        $computed_elements[] = $element;
54
      }
55
      else {
56
        $other_elements[] = $element;
57 19e097a3 Andreas Kohlbecker
      }
58
    }
59 ce29c528 Andreas Kohlbecker
  }
60 19e097a3 Andreas Kohlbecker
61 ce29c528 Andreas Kohlbecker
  if (count($computed_elements) > 0) {
62
    return $computed_elements;
63
  }
64
  else {
65
    return $other_elements;
66 19e097a3 Andreas Kohlbecker
  }
67 ce29c528 Andreas Kohlbecker
}
68 ae0a5060 Andreas Kohlbecker
69 ce29c528 Andreas Kohlbecker
/**
70
 * Creates a query parameter array based on the setting stored in the drupal variable CDM_DISTRIBUTION_FILTER.
71
 *
72
 * @return array
73
 *   An array with distribution filter query parameters
74
 */
75
function cdm_distribution_filter_query() {
76
  $cdm_distribution_filter = get_array_variable_merged(CDM_DISTRIBUTION_FILTER, CDM_DISTRIBUTION_FILTER_DEFAULT);
77
  $query = array();
78 aa63dfb4 Andreas Kohlbecker
79 3eccfdb9 Andreas Kohlbecker
  $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
80
  if($feature_block_settings['sort_elements'] == SORT_HIERARCHICAL){
81
    $query['distributionOrder'] = 'AREA_ORDER';
82
  }
83 ec2cb73b Andreas Kohlbecker
84 846c0606 Andreas Kohlbecker
  $query['recipe'] = variable_get(DISTRIBUTION_CONDENSED_RECIPE, DISTRIBUTION_CONDENSED_RECIPE_DEFAULT);
85
86 3eccfdb9 Andreas Kohlbecker
87
88 ce29c528 Andreas Kohlbecker
  if ($cdm_distribution_filter['filter_rules']['statusOrderPreference']) {
89
    $query['statusOrderPreference'] = 1;
90
  }
91
  if ($cdm_distribution_filter['filter_rules']['subAreaPreference']) {
92
    $query['subAreaPreference'] = 1;
93
  }
94 6fbf1bd3 Andreas Kohlbecker
  if (is_array($cdm_distribution_filter['hiddenAreaMarkerType']) && count($cdm_distribution_filter['hiddenAreaMarkerType']) > 0) {
95
    $query['hiddenAreaMarkerType'] = '';
96
    foreach ($cdm_distribution_filter['hiddenAreaMarkerType'] as $marker_type => $enabled) {
97 ce29c528 Andreas Kohlbecker
      if ($enabled) {
98 6fbf1bd3 Andreas Kohlbecker
        $query['hiddenAreaMarkerType'] .= ($query['hiddenAreaMarkerType'] ? ',' : '') . $marker_type;
99 ce29c528 Andreas Kohlbecker
      }
100 dd1c109a Andreas Kohlbecker
    }
101 aa63dfb4 Andreas Kohlbecker
  }
102
103 ce29c528 Andreas Kohlbecker
  return $query;
104
}
105 aa63dfb4 Andreas Kohlbecker
106 ce29c528 Andreas Kohlbecker
/**
107
 * Merge the fields 'annotations', 'markers', 'sources', 'media' from the source CDM DescriptionElement into  the target.
108
 *
109
 * @param object $target
110
 *   The source CDM DescriptionElement
111
 * @param object $source
112
 *   The target CDM DescriptionElement
113
 */
114 a6d7907d Andreas Kohlbecker
function cdm_merge_description_elements($target, $source) {
115 ce29c528 Andreas Kohlbecker
  static $fields_to_merge = array('annotations', 'markers', 'sources', 'media');
116
117
  foreach ($fields_to_merge as $field) {
118
    if (is_array($source->$field)) {
119
      if (!is_array($target->$field)) {
120
        $target->$field = $source->$field;
121
      }
122
      else {
123
        $target->$field = array_merge($target->$field, $source->$field);
124 ae0a5060 Andreas Kohlbecker
      }
125
    }
126
  }
127 ce29c528 Andreas Kohlbecker
}
128 bf97c2e8 Andreas Kohlbecker
129 ce29c528 Andreas Kohlbecker
/**
130
 * Adds an entry to the end of the table of content items list
131
 *
132
 * The  table of content items are crated internally by calling
133
 * toc_list_item() the resulting item is added to the statically cached
134
 * list of toc elements
135
 *
136
 * @param string $label
137
 *   The label of toc entry
138
 * @param $class_attribute_suffix
139 a6d7907d Andreas Kohlbecker
 *   The suffix to be appended to the class attribute prefix:
140
 *   "feature-toc-item-"
141 ce29c528 Andreas Kohlbecker
 * @param string $fragment
142
 *   Optional parameter to define a url fragment different from the $label,
143
 *   if the $fragment is not defined the $label will be used
144 c3a41ddb Andreas Kohlbecker
 * @param boolean $as_first_element
145 a6d7907d Andreas Kohlbecker
 *   Place the new item as fist one. This parameter is ignored when $weight is
146
 *   being set
147 c3a41ddb Andreas Kohlbecker
 * @param integer $weight
148
 *   Defines the weight for the ordering the item in the toc list.
149
 *   The item will be placed at the end if weigth is NULL.
150 a6d7907d Andreas Kohlbecker
 *
151
 * @throws \Exception re-throws exception from theme()
152 ce29c528 Andreas Kohlbecker
 */
153 c3a41ddb Andreas Kohlbecker
function cdm_toc_list_add_item($label, $class_attribute_suffix, $fragment = NULL, $as_first_element = FALSE, $weight = null) {
154
155 ce29c528 Andreas Kohlbecker
  $toc_list_items = &cdm_toc_list();
156 dd1c109a Andreas Kohlbecker
157 ce29c528 Andreas Kohlbecker
  if (!$fragment) {
158
    $fragment = $label;
159
  }
160
  $fragment = generalizeString($fragment);
161 dd1c109a Andreas Kohlbecker
162 ce29c528 Andreas Kohlbecker
  $class_attributes = 'feature-toc-item-' . $class_attribute_suffix;
163 dd1c109a Andreas Kohlbecker
164 ce29c528 Andreas Kohlbecker
  $new_item = toc_list_item(
165
    theme(
166
      'cdm_feature_name',
167
      array('feature_name' => $label)),
168 dd1c109a Andreas Kohlbecker
      array('class' => $class_attributes),
169
      $fragment
170
    );
171
172 c3a41ddb Andreas Kohlbecker
  if ($weight === null && $as_first_element) {
173
    reset($toc_list_items);
174
    $weight = key($toc_list_items); // returns null for empty arrays
175
    $weight = $weight !== null ? $weight - FEATURE_BLOCK_WEIGHT_INCREMENT : 0;
176 ce29c528 Andreas Kohlbecker
  }
177 c3a41ddb Andreas Kohlbecker
  else if($weight === null) {
178
      end($toc_list_items);
179
      $weight = key($toc_list_items); // returns null for empty arrays
180
      $weight = $weight !== null ? $weight + FEATURE_BLOCK_WEIGHT_INCREMENT : 0;
181 bf97c2e8 Andreas Kohlbecker
  }
182 c3a41ddb Andreas Kohlbecker
  $toc_list_items[$weight] = $new_item;
183
  ksort($toc_list_items); // sort so that the last element has always the highest weight
184 bf97c2e8 Andreas Kohlbecker
185 ce29c528 Andreas Kohlbecker
}
186 bf97c2e8 Andreas Kohlbecker
187 ce29c528 Andreas Kohlbecker
/**
188
 * Returns the statically cached table of content items as render array.
189
 *
190
 * @see cdm_toc_list_add_item()
191
 *
192
 * @return array
193
 *   a render array of table of content items suitable for theme_item_list()
194
 */
195
function &cdm_toc_list(){
196 a6d7907d Andreas Kohlbecker
197 ce29c528 Andreas Kohlbecker
  $toc_list_items = &drupal_static('toc_list_items', array());
198
199
  return $toc_list_items;
200
}
201 1f11e206 Andreas Kohlbecker
202 f19f47fa Andreas Kohlbecker
/**
203 a6d7907d Andreas Kohlbecker
 * Prepares an empty Drupal block for displaying description elements of a
204
 * specific CDM Feature.
205 f19f47fa Andreas Kohlbecker
 *
206 a6d7907d Andreas Kohlbecker
 * The block can also be used for pseudo Features like a bibliography. Pseudo
207
 * features are derived from other data on the fly and so not exist as such in
208
 * the cdm data. For pseudo features the $feature can be created using
209
 * make_pseudo_feature().
210 f19f47fa Andreas Kohlbecker
 *
211
 * @param $feature_name
212 a6d7907d Andreas Kohlbecker
 *   A label describing the feature, usually the localized feature
213
 *   representation.
214 ce29c528 Andreas Kohlbecker
 * @param object $feature
215 fec3fd41 Andreas Kohlbecker
 *   The CDM Feature for which the block is created.
216 a6d7907d Andreas Kohlbecker
 *
217 f19f47fa Andreas Kohlbecker
 * @return object
218 ce29c528 Andreas Kohlbecker
 *   A Drupal block object
219 a6d7907d Andreas Kohlbecker
 *
220
 * @throws \Exception re-throws exception from theme()
221 f19f47fa Andreas Kohlbecker
 */
222 fec3fd41 Andreas Kohlbecker
function feature_block($feature_name, $feature) {
223
  $block = new stdclass();
224 f19f47fa Andreas Kohlbecker
  $block->module = 'cdm_dataportal';
225 ce29c528 Andreas Kohlbecker
  $block->region = NULL;
226 fec3fd41 Andreas Kohlbecker
  $class_attribute = html_class_attribute_ref($feature);
227 d43a3486 Andreas Kohlbecker
  $block_delta = $feature->class !== 'PseudoFeature' ? $feature_name : $feature->label;
228 fec3fd41 Andreas Kohlbecker
  $block->delta = generalizeString($block_delta);
229 28ec5a3e Andreas Kohlbecker
  $block->subject = '<a name="' . $block->delta . '"></a><span class="' . $class_attribute . '">'
230 f19f47fa Andreas Kohlbecker
    . theme('cdm_feature_name', array('feature_name' => $feature_name))
231
    . '</span>';
232
  $block->module = "cdm_dataportal-feature";
233
  $block->content = '';
234
  return $block;
235
}
236
237 dd1c109a Andreas Kohlbecker
238 ce29c528 Andreas Kohlbecker
/**
239
 * Returns a list of a specific type of IdentificationKeys.
240
 *
241
 * The list can be restricted by a taxon.
242
 *
243
 * @param string $type
244
 *   The simple name of the cdm class implementing the interface
245
 *   IdentificationKey, valid values are:
246
 *   PolytomousKey, MediaKey, MultiAccessKey.
247
 * @param string $taxonUuid
248
 *   If given this parameter restrict the listed keys to those which have
249
 *   the taxon identified be this uuid in scope.
250
 *
251
 * @return array
252
 *   List with identification keys.
253
 */
254
function _list_IdentificationKeys($type, $taxonUuid = NULL, $pageSize = NULL, $pageNumber = NULL) {
255
  if (!$type) {
256
    drupal_set_message(t('Type parameter is missing'), 'error');
257
    return;
258
  }
259 a6d7907d Andreas Kohlbecker
  $cdm_ws_base_path = NULL;
260 ce29c528 Andreas Kohlbecker
  switch ($type) {
261
    case "PolytomousKey":
262 a6d7907d Andreas Kohlbecker
      $cdm_ws_base_path = CDM_WS_POLYTOMOUSKEY;
263 ce29c528 Andreas Kohlbecker
      break;
264
265
    case "MediaKey":
266 a6d7907d Andreas Kohlbecker
      $cdm_ws_base_path = CDM_WS_MEDIAKEY;
267 ce29c528 Andreas Kohlbecker
      break;
268 dd1c109a Andreas Kohlbecker
269 ce29c528 Andreas Kohlbecker
    case "MultiAccessKey":
270 a6d7907d Andreas Kohlbecker
      $cdm_ws_base_path = CDM_WS_MULTIACCESSKEY;
271 ce29c528 Andreas Kohlbecker
      break;
272
273
  }
274
275 a6d7907d Andreas Kohlbecker
  if (!$cdm_ws_base_path) {
276 ce29c528 Andreas Kohlbecker
    drupal_set_message(t('Type parameter is not valid: ') . $type, 'error');
277
  }
278
279
  $queryParameters = '';
280
  if (is_numeric($pageSize)) {
281
    $queryParameters = "pageSize=" . $pageSize;
282
  }
283
  else {
284
    $queryParameters = "pageSize=0";
285
  }
286
287
  if (is_numeric($pageNumber)) {
288
    $queryParameters = "pageNumber=" . $pageNumber;
289
  }
290
  else {
291
    $queryParameters = "pageNumber=0";
292
  }
293
  $queryParameters = NULL;
294
  if ($taxonUuid) {
295
    $queryParameters = "findByTaxonomicScope=$taxonUuid";
296
  }
297 a6d7907d Andreas Kohlbecker
  $pager = cdm_ws_get($cdm_ws_base_path, NULL, $queryParameters);
298 ce29c528 Andreas Kohlbecker
299
  if (!$pager || $pager->count == 0) {
300
    return array();
301
  }
302
  return $pager->records;
303
}
304
305
306
/**
307
 * Creates a list item for a table of content, suitable as data element for a themed list
308
 *
309
 * @see theme_list()
310
 *
311
 * @param $label
312
 * @param $http_request_params
313
 * @param $attributes
314
 * @return array
315
 */
316
function toc_list_item($label, $attributes = array(), $fragment = null) {
317
318
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
319
  $http_request_params = drupal_static('http_request_params', drupal_get_query_parameters());
320
321
  $item =  array(
322
    'data' => l(
323
      $label,
324
      $_GET['q'],
325
      array(
326
        'attributes' => array('class' => array('toc')),
327
        'fragment' => generalizeString($label),
328
        'query' => $http_request_params,
329 f19f47fa Andreas Kohlbecker
      )
330 ce29c528 Andreas Kohlbecker
    ),
331
  );
332
  $item['attributes'] = $attributes;
333
  return $item;
334
}
335
336 0c2b9b9d Andreas Kohlbecker
/**
337
 * Creates the footnotes for the given CDM instance.
338
 *
339 f814b2cd Andreas Kohlbecker
 * Footnotes are created for annotations and original sources whereas the resulting footnote keys depend on the
340
 * parameters $footnote_list_key_suggestion and $is_bibliography_aware, see parameter $footnote_list_key_suggestion
341
 * for more details.
342
 *
343
 * possible keys for
344
 *     - annotation footnotes:
345
 *       - $footnote_list_key_suggestion
346
 *       - RenderHints::getFootnoteListKey().'-annotations'
347
 *     - original source footnotes
348
 *       - "BIBLIOGRAPHY" (when !$is_bibliography_aware && bibliography_settings['enabled'] == 1 )
349
 *       - "BIBLIOGRAPHY-$footnote_list_key_suggestion" (when !$is_bibliography_aware && bibliography_settings['enabled'] == 0 )
350
 *       - $footnote_list_key_suggestion (when $is_bibliography_aware)
351 0c2b9b9d Andreas Kohlbecker
 *
352 a6d7907d Andreas Kohlbecker
 * @param $description_element
353 0c2b9b9d Andreas Kohlbecker
 *   A CDM DescriptionElement instance
354 a6d7907d Andreas Kohlbecker
 * @param string $separator
355 0c2b9b9d Andreas Kohlbecker
 *   Optional parameter. The separator string to concatenate the footnote ids, default is ','
356 f814b2cd Andreas Kohlbecker
 * @param $footnote_list_key_suggestion string
357
 *    Optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be determined by the nested
358
 *    method calls by calling RenderHints::getFootnoteListKey(). NOTE: the footnote key for annotations will be set to
359 0e617798 Andreas Kohlbecker
 *    RenderHints::getFootnoteListKey().'-annotations'.
360 a6d7907d Andreas Kohlbecker
 * @param bool $do_link_to_reference
361
 *    Create a link to the reference pages for sources when TRUE.
362
 * @param bool $do_link_to_name_used_in_source
363
 *    Create a link to the name pages for name in source when TRUE.
364
 * @param bool $is_bibliography_aware
365
 *    Put source references into the bibliography when this param is TRUE.
366 0e617798 Andreas Kohlbecker
 *
367
 * @return String
368 2fc5c836 Andreas Kohlbecker
 *   The foot note keys
369
 *
370 a6d7907d Andreas Kohlbecker
 * @throws \Exception re-throw exception from theme()
371 2fc5c836 Andreas Kohlbecker
 * @see cdm_entities_annotations_as_footnotekeys()
372 9b8eb609 Andreas Kohlbecker
 *    For original sources the $footnote_list_key_suggestion will be overwritten by bibliography_footnote_list_key() when
373 f814b2cd Andreas Kohlbecker
 *    $is_bibliography_aware is set TRUE.
374 0c2b9b9d Andreas Kohlbecker
 * @$original_source_footnote_tag
375 9b8eb609 Andreas Kohlbecker
 *    null will cause bibliography_footnote_list_key to use the default
376 0c2b9b9d Andreas Kohlbecker
 */
377
function cdm_create_footnotes(
378
    $description_element,
379
    $separator = ',',
380 a1493182 Andreas Kohlbecker
    $footnote_list_key_suggestion = null,
381 0c2b9b9d Andreas Kohlbecker
    $do_link_to_reference = FALSE,
382
    $do_link_to_name_used_in_source = FALSE,
383 a1493182 Andreas Kohlbecker
    $is_bibliography_aware = FALSE
384 0c2b9b9d Andreas Kohlbecker
  ){
385 ce29c528 Andreas Kohlbecker
386 a6c4c53c Andreas Kohlbecker
  $sources = cdm_entity_sources_sorted($description_element);
387
388 ce29c528 Andreas Kohlbecker
  // Annotations as footnotes.
389 0e617798 Andreas Kohlbecker
  $footnote_keys = cdm_entity_annotations_as_footnotekeys($description_element, $footnote_list_key_suggestion);
390 ce29c528 Andreas Kohlbecker
391
  // Source references as footnotes.
392 a1493182 Andreas Kohlbecker
  if($is_bibliography_aware){
393
    $bibliography_settings = get_bibliography_settings();
394 9b8eb609 Andreas Kohlbecker
    $sources_footnote_list_key = bibliography_footnote_list_key($footnote_list_key_suggestion);
395
    $original_source_footnote_tag = $bibliography_settings['enabled'] == 1 ? 'div' : null; // null will cause bibliography_footnote_list_key to use the default
396 a1493182 Andreas Kohlbecker
  } else {
397 9b8eb609 Andreas Kohlbecker
    $sources_footnote_list_key = $footnote_list_key_suggestion;
398
    if(!$sources_footnote_list_key) {
399
      RenderHints::getFootnoteListKey();
400
    }
401 a1493182 Andreas Kohlbecker
    $original_source_footnote_tag = NULL;
402
  }
403
404 a6c4c53c Andreas Kohlbecker
  foreach ($sources as $source) {
405 ce29c528 Andreas Kohlbecker
    if (_is_original_source_type($source)) {
406
      $fn_key = FootnoteManager::addNewFootnote(
407 a1493182 Andreas Kohlbecker
        $sources_footnote_list_key,
408 fe3a5674 Andreas Kohlbecker
        render_original_source(
409
          $source,
410 bb93d5d1 Andreas Kohlbecker
          $do_link_to_reference,
411
          $do_link_to_name_used_in_source
412
        ),
413 ce29c528 Andreas Kohlbecker
        $original_source_footnote_tag
414
      );
415
      // Ensure uniqueness of the footnote keys.
416 0e617798 Andreas Kohlbecker
      cdm_add_footnote_to_array($footnote_keys, $fn_key);
417 ce29c528 Andreas Kohlbecker
    }
418
  }
419
  // Sort and render footnote keys.
420 0e617798 Andreas Kohlbecker
  asort($footnote_keys);
421 a6d7907d Andreas Kohlbecker
  return footnote_keys_to_markup($footnote_keys, $separator);
422 0e617798 Andreas Kohlbecker
}
423
424
/**
425
 * Creates markup for an array of foot note keys
426
 *
427
 * @param array $footnote_keys
428
 * @param string $separator
429
 *
430
 * @return string
431
 */
432 a6d7907d Andreas Kohlbecker
function footnote_keys_to_markup(array $footnote_keys, $separator) {
433 0e617798 Andreas Kohlbecker
434
  $footnotes_markup = '';
435
  foreach ($footnote_keys as $foot_note_key) {
436
    try {
437
      $footnotes_markup .= theme('cdm_footnote_key',
438 f814b2cd Andreas Kohlbecker
        array(
439 0e617798 Andreas Kohlbecker
          'footnoteKey' => $foot_note_key,
440
          'separator' => ($footnotes_markup ? $separator : '')
441
        ));
442 f814b2cd Andreas Kohlbecker
    } catch (Exception $e) {
443 0e617798 Andreas Kohlbecker
      drupal_set_message("Exception: " . $e->getMessage(), 'error');
444 f814b2cd Andreas Kohlbecker
    }
445 dd1c109a Andreas Kohlbecker
  }
446 0e617798 Andreas Kohlbecker
  return $footnotes_markup;
447 ce29c528 Andreas Kohlbecker
}
448
449 f19f47fa Andreas Kohlbecker
450 f23ae4e3 Andreas Kohlbecker
/**
451 a6d7907d Andreas Kohlbecker
 * Compare callback to be used in usort() to sort render arrays produced by
452
 * compose_description_element().
453 f23ae4e3 Andreas Kohlbecker
 *
454
 * @param $a
455
 * @param $b
456 a6d7907d Andreas Kohlbecker
 *
457
 * @return int Returns < 0 if $a['#value'].$a['#value-suffix'] from is less than
458
 * $b['#value'].$b['#value-suffix'], > 0 if it is greater than $b['#value'].$b['#value-suffix'],
459
 * and 0 if they are equal.
460 f23ae4e3 Andreas Kohlbecker
 */
461
function compare_description_element_render_arrays($a, $b){
462
  if ($a['#value'].$a['#value-suffix'] == $b['#value'].$b['#value-suffix']) {
463
    return 0;
464
  }
465
  return ($a['#value'].$a['#value-suffix'] < $b['#value'].$b['#value-suffix']) ? -1 : 1;
466
467
}
468
469 dfc49afb Andreas Kohlbecker
470
/**
471
 * @param $element
472
 * @param $feature_block_settings
473
 * @param $element_markup
474
 * @param $footnote_list_key_suggestion
475 a6d7907d Andreas Kohlbecker
 * @param bool $prepend_feature_label
476
 *
477
 * @return array|null
478 dfc49afb Andreas Kohlbecker
 */
479 c651d3b1 Andreas Kohlbecker
function compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label = FALSE)
480 dfc49afb Andreas Kohlbecker
{
481 0ef9a709 Andreas Kohlbecker
482
  $render_array = array(
483
    '#type' => 'html_tag',
484
    '#tag' => cdm_feature_block_element_tag_name($feature_block_settings),
485
486
    '#attributes' => array(
487 f23ae4e3 Andreas Kohlbecker
      'class' => array(
488
        'DescriptionElement',
489
        'DescriptionElement-' . $element->class,
490
        html_class_attribute_ref($element)
491
      )
492 0ef9a709 Andreas Kohlbecker
    ),
493
494
    '#value' => '',
495
    '#value_suffix' => NULL
496
497
  );
498
499 0c2b9b9d Andreas Kohlbecker
  $annotations_and_sources = handle_annotations_and_sources(
500
    $element,
501
    handle_annotations_and_sources_config($feature_block_settings),
502
    $element_markup,
503
    $footnote_list_key_suggestion
504
  );
505 dfc49afb Andreas Kohlbecker
506 90425089 Andreas Kohlbecker
  $timescope_markup = '';
507
  if(isset($element->timeperiod)){
508 ef6a4ce9 Andreas Kohlbecker
    $timescope_markup = ' ' . timePeriodToString($element->timeperiod, true);
509 90425089 Andreas Kohlbecker
  }
510
511 0ef9a709 Andreas Kohlbecker
  // handle the special case were the TextData is used as container for original source with name
512
  // used in source information without any text stored in it.
513 dfc49afb Andreas Kohlbecker
  $names_used_in_source_markup = '';
514
  if (!empty($annotations_and_sources['names_used_in_source']) && empty($element_markup)) {
515
    $names_used_in_source_markup = join(', ', $annotations_and_sources['names_used_in_source']) . ': ';
516 72c12d45 Andreas Kohlbecker
    // remove all <span class="nameUsedInSource">...</span> from all source_references
517
    // these are expected to be at the end of the strings
518
    $pattern = '/ <span class="nameUsedInSource">.*$/';
519
    foreach( $annotations_and_sources['source_references'] as &$source_reference){
520
      $source_reference = preg_replace($pattern , '', $source_reference);
521
    }
522 dfc49afb Andreas Kohlbecker
  }
523
524
  $source_references_markup = '';
525
  if (!empty($annotations_and_sources['source_references'])) {
526
    $source_references_markup = '<span class="sources">' . join(' ', $annotations_and_sources['source_references']) . '<span>';
527
  }
528
529 c651d3b1 Andreas Kohlbecker
  $feature_label = '';
530
  if ($prepend_feature_label) {
531
    $feature_label = '<span class="nested-feature-tree-feature-label">' . $element->feature->representation_L10n . ':</span> ';
532
  }
533 90425089 Andreas Kohlbecker
  $content_markup = $names_used_in_source_markup . $element_markup . $timescope_markup . $source_references_markup;
534 0ef9a709 Andreas Kohlbecker
535 caab0bd2 Andreas Kohlbecker
  if(!$content_markup && $feature_block_settings['sources_as_content'] !== 1){
536
    // no textual content? So skip this element completely, even if there could be an footnote key
537 7a751db4 Andreas Kohlbecker
    // see #4379
538
    return null;
539
  }
540
541
    $render_array['#value'] = $feature_label . $content_markup;
542
    $render_array['#value_suffix'] = $annotations_and_sources['foot_note_keys'];
543 0ef9a709 Andreas Kohlbecker
  return $render_array;
544 dfc49afb Andreas Kohlbecker
}
545
546 0c2b9b9d Andreas Kohlbecker
/**
547
 * Creates a handle_annotations_and_sources configuration array from feature_block_settings.
548
 *
549
 * The handle_annotations_and_sources configuration array is meant to be used for the
550
 * method handle_annotations_and_sources().
551
 *
552
 * @param $feature_block_settings array
553
 *
554
 * @return array
555
 *   The configuration array for handle_annotations_and_sources()
556
 */
557
function handle_annotations_and_sources_config($feature_block_settings){
558
559
  $config = $feature_block_settings;
560
  unset($config['sources_as_content_to_bibliography']);
561 f79d32d6 Andreas Kohlbecker
  $config['add_footnote_keys'] = 0;
562 0c2b9b9d Andreas Kohlbecker
  if($feature_block_settings['sources_as_content'] !== 1 || $feature_block_settings['sources_as_content_to_bibliography'] == 1) {
563
    $config['add_footnote_keys'] = 1;
564
  }
565
  $config['bibliography_aware'] = 1;
566
567
  return $config;
568
}
569 dfc49afb Andreas Kohlbecker
570 a6d7907d Andreas Kohlbecker
/**
571
 * @param $entity
572
 * @param $config array
573
 *   An associative array to configure the display of the annotations and
574
 *   sources. The array has the following keys
575
 *   - sources_as_content
576
 *   - link_to_name_used_in_source
577
 *   - link_to_reference
578
 *   - add_footnote_keys
579
 *   - bibliography_aware
580
 *   Valid values are 1 or 0.
581
 * @param $inline_text_prefix
582
 *   Only used to decide if the source references should be enclosed in
583
 *   brackets or not when displayed inline. This text will not be included into
584
 *   the response.
585
 * @param $footnote_list_key_suggestion string
586
 *    optional parameter. If this paramter is left empty (null, 0, "") the
587
 *   footnote key will be determined by the nested method calls by calling
588
 *   RenderHints::getFootnoteListKey(). NOTE: the footnore key for annotations
589
 *   will be set to RenderHints::getFootnoteListKey().'-annotations'. @return
590
 *   array an associative array with the following elements:
591
 *   - foot_note_keys: all footnote keys as markup
592
 *   - source_references: an array of the source references citations
593
 *   - names used in source: an associative array of the names in source,
594
 *        the name in source strings are de-duplicated
595
 *        !!!NOTE!!!!: this field will most probably be removed soon (TODO)
596
 *
597
 * @throws \Exception
598
 *
599
 * @see cdm_entities_annotations_as_footnotekeys()
600
 */
601 0c2b9b9d Andreas Kohlbecker
  function handle_annotations_and_sources($entity, $config, $inline_text_prefix, $footnote_list_key_suggestion) {
602
603 ce29c528 Andreas Kohlbecker
    $annotations_and_sources = array(
604
      'foot_note_keys' => NULL,
605 0e617798 Andreas Kohlbecker
      'source_references' => [],
606
      'names_used_in_source' => []
607 ce29c528 Andreas Kohlbecker
    );
608 dd1c109a Andreas Kohlbecker
609 95e00758 Andreas Kohlbecker
    // some entity types only have single sources:
610 a6c4c53c Andreas Kohlbecker
    $sources = cdm_entity_sources_sorted($entity);
611 9e2aa1ff Andreas Kohlbecker
612 0c2b9b9d Andreas Kohlbecker
    if ($config['sources_as_content'] == 1) {
613 95e00758 Andreas Kohlbecker
      foreach ($sources as $source) {
614 2a7eb586 Andreas Kohlbecker
        if (_is_original_source_type($source)) {
615 bb93d5d1 Andreas Kohlbecker
          $reference_citation = render_original_source(
616 fe3a5674 Andreas Kohlbecker
            $source,
617 9078b9c6 Andreas Kohlbecker
            $config['link_to_reference'] == 1,
618
            $config['link_to_name_used_in_source'] == 1
619 2a7eb586 Andreas Kohlbecker
          );
620
621
          if ($reference_citation) {
622
            if (empty($inline_text_prefix)) {
623
              $annotations_and_sources['source_references'][] = $reference_citation;
624
            } else {
625
              $annotations_and_sources['source_references'][] = ' (' . $reference_citation . ')';
626
            }
627 ce29c528 Andreas Kohlbecker
          }
628 bb3188cb Andreas Kohlbecker
629 27bceb70 Andreas Kohlbecker
          // also put the name in source into the array, these are already included in the $reference_citation but are
630
          // still required to be available separately in some contexts.
631 2a7eb586 Andreas Kohlbecker
          $name_in_source_render_array = compose_name_in_source(
632
            $source,
633
            $config['link_to_name_used_in_source'] == 1
634
          );
635 ce29c528 Andreas Kohlbecker
636 2a7eb586 Andreas Kohlbecker
          if (!empty($name_in_source_render_array)) {
637
            $annotations_and_sources['names_used_in_source'][$name_in_source_render_array['#_plaintext']] = drupal_render($name_in_source_render_array);
638
          }
639 ce29c528 Andreas Kohlbecker
        }
640
      } // END of loop over sources
641
642 0e617798 Andreas Kohlbecker
      // annotations footnotes separate from sources
643
      $annotations_and_sources['foot_note_keys'] = footnote_keys_to_markup(
644
        cdm_entity_annotations_as_footnotekeys($entity, $footnote_list_key_suggestion), ', '
645 c797d7db Andreas Kohlbecker
      );
646 2d95e99f Andreas Kohlbecker
647 85ac09e7 Andreas Kohlbecker
    } // END of references inline
648
649 0fd965ee Andreas Kohlbecker
    // footnotes for sources and annotations or put into into bibliography if requested ...
650 0c2b9b9d Andreas Kohlbecker
    if ($config['add_footnote_keys'] == 1) {
651 85ac09e7 Andreas Kohlbecker
        $annotations_and_sources['foot_note_keys'] = cdm_create_footnotes(
652 0c2b9b9d Andreas Kohlbecker
          $entity, ',',
653
          $footnote_list_key_suggestion,
654
          $config['link_to_reference'] == 1,
655 0fd965ee Andreas Kohlbecker
          $config['link_to_name_used_in_source'] == 1,
656
          !empty($config['bibliography_aware'])
657 0c2b9b9d Andreas Kohlbecker
        );
658 dd1c109a Andreas Kohlbecker
    }
659 ce29c528 Andreas Kohlbecker
660
    return $annotations_and_sources;
661 1f11e206 Andreas Kohlbecker
  }
662 aa63dfb4 Andreas Kohlbecker
663 a6c4c53c Andreas Kohlbecker
/**
664
 * Get the source or the sources from a cdm entity and return them ordered by see compare_original_sources()
665
 * (Some entity types only have single sources)
666
 * @param $entity
667
 *
668
 * @return array
669
 */
670
function cdm_entity_sources_sorted($entity) {
671
  if (isset($entity->source) && is_object($entity->source)) {
672
    $sources = [$entity->source];
673
  }
674
  else if (isset($entity->sources)) {
675
    $sources = $entity->sources;
676
  }
677
  else {
678
    $sources = [];
679
  }
680
  usort($sources, 'compare_original_sources');
681
  return $sources;
682
}
683 ce29c528 Andreas Kohlbecker
684 a6c4c53c Andreas Kohlbecker
685
/**
686 f814b2cd Andreas Kohlbecker
   * This method determines the footnote key for original sources to be shown in the bibliography block
687
   *
688
   * The footnote key depends on the value of the 'enabled' value of the bibliography_settings
689
   *    - enabled == 1 -> "BIBLIOGRAPHY"
690
   *    - enabled == 0 -> "BIBLIOGRAPHY-$key_suggestion"
691 dd1c109a Andreas Kohlbecker
   *
692 f814b2cd Andreas Kohlbecker
   * @see get_bibliography_settings() and @see constant BIBLIOGRAPHY_FOOTNOTE_KEY
693
   *
694
   * @param $key_suggestion string
695
   *    optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be retrieved by
696
   *    calling RenderHints::getFootnoteListKey().
697
698 57d513cb Andreas Kohlbecker
   *
699
   * @return string
700
   *  the footnote_list_key
701 dd1c109a Andreas Kohlbecker
   */
702 9b8eb609 Andreas Kohlbecker
  function bibliography_footnote_list_key($key_suggestion = null) {
703 dd1c109a Andreas Kohlbecker
    if(!$key_suggestion){
704
      $key_suggestion = RenderHints::getFootnoteListKey();
705
    }
706
    $bibliography_settings = get_bibliography_settings();
707 f814b2cd Andreas Kohlbecker
    $footnote_list_key = $bibliography_settings['enabled'] == 1 ? BIBLIOGRAPHY_FOOTNOTE_KEY : BIBLIOGRAPHY_FOOTNOTE_KEY . '-' . $key_suggestion;
708 dd1c109a Andreas Kohlbecker
    return $footnote_list_key;
709 f19f47fa Andreas Kohlbecker
  }
710
711 a6d7907d Andreas Kohlbecker
/**
712
 * Provides the according tag name for the description element markup which
713
 * fits the  $feature_block_settings['as_list'] value
714
 *
715
 * @param $feature_block_settings
716
 *   A feature_block_settings array, for details, please see
717
 *   get_feature_block_settings($feature_uuid = 'DEFAULT')
718
 *
719
 * @return mixed|string
720
 */
721 08a339ec Andreas Kohlbecker
  function cdm_feature_block_element_tag_name($feature_block_settings){
722
    switch ($feature_block_settings['as_list']){
723
      case 'ul':
724
      case 'ol':
725
        return 'li';
726
      case 'div':
727 f2e44165 Andreas Kohlbecker
        if(isset($feature_block_settings['element_tag'])){
728
          return $feature_block_settings['element_tag'];
729
        }
730 08a339ec Andreas Kohlbecker
        return 'span';
731
      case 'dl':
732
        return 'dd';
733
      default:
734
        return 'div'; // should never happen, throw error instead?
735
    }
736
  }
737 ce29c528 Andreas Kohlbecker
738
739
/* ==================== COMPOSE FUNCTIONS =============== */
740
741
  /**
742
   * Returns a set of feature blocks for a taxon profile from the $mergedFeatureNodes of a given $taxon.
743
   *
744
   * The taxon profile consists of drupal block elements, one for the description elements
745
   * of a specific feature. The structure is defined by specific FeatureTree.
746
   * The chosen FeatureTree is merged with the list of description elements prior to using this method.
747
   *
748
   * The merged nodes can be obtained by making use of the
749
   * function cdm_ws_descriptions_by_featuretree().
750
   *
751
   * @see cdm_ws_descriptions_by_featuretree()
752
   *
753
   * @param $mergedFeatureNodes
754
   *
755
   * @param $taxon
756
   *
757
   * @return array
758
   *  A Drupal render array containing feature blocks and the table of content
759
   *
760
   * @ingroup compose
761
   */
762 1b756c5f Andreas Kohlbecker
  function make_feature_block_list($mergedFeatureNodes, $taxon) {
763 ce29c528 Andreas Kohlbecker
764
    $block_list = array();
765
766
    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME);
767
768 4feafea8 Andreas Kohlbecker
    $use_description_features = array(UUID_USE);
769
770 ddc8c754 Andreas Kohlbecker
771
    RenderHints::pushToRenderStack('feature_block');
772 ce29c528 Andreas Kohlbecker
    // Create a drupal block for each feature
773 c3a41ddb Andreas Kohlbecker
    $block_weight = - FEATURE_BLOCK_WEIGHT_INCREMENT;
774 b011743c Andreas Kohlbecker
    foreach ($mergedFeatureNodes as $feature_node) {
775 ce29c528 Andreas Kohlbecker
776 c3a41ddb Andreas Kohlbecker
      $block_weight = $block_weight + FEATURE_BLOCK_WEIGHT_INCREMENT;
777
778 b011743c Andreas Kohlbecker
      if ((isset($feature_node->descriptionElements['#type']) ||
779
          has_feature_node_description_elements($feature_node)) && $feature_node->term->uuid != UUID_IMAGE) { // skip empty or suppressed features
780 ce29c528 Andreas Kohlbecker
781 b011743c Andreas Kohlbecker
        RenderHints::pushToRenderStack($feature_node->term->uuid);
782 ddc8c754 Andreas Kohlbecker
          
783 b011743c Andreas Kohlbecker
        $feature_name = cdm_term_representation($feature_node->term, 'Unnamed Feature');
784
        $feature_block_settings = get_feature_block_settings($feature_node->term->uuid);
785 ddc8c754 Andreas Kohlbecker
        
786 ce29c528 Andreas Kohlbecker
787 b011743c Andreas Kohlbecker
        $block = feature_block($feature_name, $feature_node->term);
788 ce29c528 Andreas Kohlbecker
        $block->content = array();
789
        $block_content_is_empty = TRUE;
790
791 b011743c Andreas Kohlbecker
        if(array_search($feature_node->term->uuid, $use_description_features) !== false) {
792 4feafea8 Andreas Kohlbecker
          // do not show features which belong to the UseDescriptions, these are
793 1b756c5f Andreas Kohlbecker
          // handled by compose_feature_block_items_use_records() where the according descriptions are
794 4feafea8 Andreas Kohlbecker
          // fetched again separately.
795 1b756c5f Andreas Kohlbecker
          // UseDescriptions are a special feature introduced for palmweb
796 4feafea8 Andreas Kohlbecker
          continue;
797
        }
798
799 ce29c528 Andreas Kohlbecker
        /*
800
         * Content/DISTRIBUTION.
801
         */
802 b011743c Andreas Kohlbecker
        if ($feature_node->term->uuid == UUID_DISTRIBUTION) {
803
          $block = compose_feature_block_distribution($taxon, $feature_node->descriptionElements, $feature_node->term);
804 ce29c528 Andreas Kohlbecker
          $block_content_is_empty = FALSE;
805
        }
806 4feafea8 Andreas Kohlbecker
807 ce29c528 Andreas Kohlbecker
        /*
808
         * Content/COMMON_NAME.
809
         */
810 b011743c Andreas Kohlbecker
        else if ($feature_node->term->uuid == UUID_COMMON_NAME) {
811
          $common_names_render_array = compose_feature_block_items_feature_common_name($feature_node->descriptionElements, $feature_node->term);
812 ce29c528 Andreas Kohlbecker
          $block->content[] = $common_names_render_array;
813
          $block_content_is_empty = FALSE;
814
        }
815
816 4feafea8 Andreas Kohlbecker
        /*
817 1b756c5f Andreas Kohlbecker
         * Content/Use Description (Use + UseRecord)
818 4feafea8 Andreas Kohlbecker
         */
819 b011743c Andreas Kohlbecker
        else if ($feature_node->term->uuid == UUID_USE_RECORD) {
820
          $block->content[] = cdm_block_use_description_content($taxon->uuid, $feature_node->term);
821 ce29c528 Andreas Kohlbecker
          $block_content_is_empty = FALSE;
822
        }
823
824
        /*
825
         * Content/ALL OTHER FEATURES.
826
         */
827
        else {
828
829
          $media_list = array();
830 c651d3b1 Andreas Kohlbecker
          $elements_render_array = array();
831
          $child_elements_render_array = null;
832 ce29c528 Andreas Kohlbecker
833 b011743c Andreas Kohlbecker
          if (isset($feature_node->descriptionElements[0])) {
834
            $elements_render_array = compose_feature_block_items_generic($feature_node->descriptionElements, $feature_node->term);
835 ce29c528 Andreas Kohlbecker
          }
836
837
          // Content/ALL OTHER FEATURES/Subordinate Features
838
          // subordinate features are printed inline in one floating text,
839
          // it is expected hat subordinate features can "contain" TextData,
840 c651d3b1 Andreas Kohlbecker
          // Qualitative- and Qualitative- DescriptionElements
841 b011743c Andreas Kohlbecker
          if (isset($feature_node->childNodes[0])) {
842
            $child_elements_render_array = compose_feature_block_items_nested($feature_node, $media_list, $feature_block_settings);
843 c651d3b1 Andreas Kohlbecker
            $elements_render_array = array_merge($elements_render_array, $child_elements_render_array);
844
          }
845
          $block_content_is_empty = $block_content_is_empty && empty($media_list) && empty($elements_render_array);
846
          if(!$block_content_is_empty){
847 b011743c Andreas Kohlbecker
            $block->content[] = compose_feature_block_wrap_elements($elements_render_array, $feature_node->term, $feature_block_settings['glue']);
848
            $block->content[] = compose_feature_media_gallery($feature_node, $media_list, $gallery_settings);
849 c651d3b1 Andreas Kohlbecker
            /*
850
             * Footnotes for the feature block
851
             */
852 b810c46f Andreas Kohlbecker
            $block->content[] = markup_to_render_array(cdm_footnotes(PSEUDO_FEATURE_BIBLIOGRAPHY . '-' . $feature_node->term->uuid));
853
            $block->content[] = markup_to_render_array(cdm_footnotes($feature_node->term->uuid));
854 f95bf1b1 Andreas Kohlbecker
            $block->content[] = markup_to_render_array(cdm_annotation_footnotes($feature_node->term->uuid));
855 ce29c528 Andreas Kohlbecker
          }
856
        } // END all other features
857
858 bdd382be Andreas Kohlbecker
        // allows modifying the block contents via a the hook_cdm_feature_node_block_content_alter
859 b011743c Andreas Kohlbecker
        drupal_alter('cdm_feature_node_block_content', $block->content, $feature_node->term, $feature_node->descriptionElements);
860 bdd382be Andreas Kohlbecker
861 ce29c528 Andreas Kohlbecker
        if(!$block_content_is_empty){ // skip empty block content
862 c3a41ddb Andreas Kohlbecker
          $block_list[$block_weight] = $block;
863
          cdm_toc_list_add_item(cdm_term_representation($feature_node->term), $feature_node->term->uuid, null, FALSE, $block_weight);
864 ce29c528 Andreas Kohlbecker
        } // END: skip empty block content
865 c651d3b1 Andreas Kohlbecker
      } // END: skip empty or suppressed features
866 ddc8c754 Andreas Kohlbecker
      RenderHints::popFromRenderStack();
867 ce29c528 Andreas Kohlbecker
    } // END: creating a block per feature
868
869 ddc8c754 Andreas Kohlbecker
    RenderHints::popFromRenderStack();
870
871 ce29c528 Andreas Kohlbecker
    drupal_alter('cdm_feature_node_blocks', $block_list, $taxon);
872 6421984d Andreas Kohlbecker
873 c3a41ddb Andreas Kohlbecker
    ksort($block_list);
874
875 1b756c5f Andreas Kohlbecker
    return  $block_list;
876 ce29c528 Andreas Kohlbecker
  }
877
878 c651d3b1 Andreas Kohlbecker
/**
879
 * Creates a render array of description elements  held by child nodes of the given feature node.
880
 *
881
 * This function is called recursively!
882
 *
883 b011743c Andreas Kohlbecker
 * @param $feature_node
884 c651d3b1 Andreas Kohlbecker
 *   The feature node.
885
 * @param array $media_list
886
 *   List of CDM Media entities. All media of subordinate elements should be passed to the main feature.ä
887
 * @param $feature_block_settings
888
 *   The feature block settings.
889
 * @param $main_feature
890
 *  Only used internally in recursive calls.
891
 *
892
 * @return array
893
 *  A Drupal render array
894
 *
895
 * @ingroup compose
896
 */
897 b011743c Andreas Kohlbecker
function compose_feature_block_items_nested($feature_node, &$media_list, $feature_block_settings, $main_feature = NULL)
898 c651d3b1 Andreas Kohlbecker
{
899
900
  if(!$main_feature){
901 b011743c Andreas Kohlbecker
    $main_feature = $feature_node->term;
902 c651d3b1 Andreas Kohlbecker
  }
903
  /*
904
   * TODO should be configurable, options; YES, NO, AUTOMATIC
905
   * (automatic will only place the label if the first word of the description element text is not the same)
906
   */
907
  $prepend_feature_label = false;
908
909
  $render_arrays = array();
910 b011743c Andreas Kohlbecker
  foreach ($feature_node->childNodes as $child_node) {
911 c651d3b1 Andreas Kohlbecker
    if (isset($child_node->descriptionElements[0])) {
912
      foreach ($child_node->descriptionElements as $element) {
913
914
        if (isset($element->media[0])) {
915
          // Append media of subordinate elements to list of main
916
          // feature.
917
          $media_list = array_merge($media_list, $element->media);
918
        }
919
920
        $child_node_element = null;
921
        switch ($element->class) {
922
          case 'TextData':
923
            $child_node_element = compose_description_element_text_data($element, $element->feature->uuid, $feature_block_settings, $prepend_feature_label);
924
            break;
925
          case 'CategoricalData':
926
            $child_node_element = compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label);
927
            break;
928
          case 'QuantitativeData':
929
            $child_node_element = compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label);
930
931
        }
932
        if (is_array($child_node_element)) {
933
          $render_arrays[] = $child_node_element;
934
        }
935
      }
936
    }
937
938
    if(isset($child_node->childNodes[0])){
939
      $render_arrays = array_merge($render_arrays, compose_feature_block_items_nested($child_node, $media_list, $feature_block_settings, $main_feature ));
940
    }
941
  }
942
943
  return $render_arrays;
944
}
945
946 ce29c528 Andreas Kohlbecker
  /**
947 c651d3b1 Andreas Kohlbecker
   *
948 b011743c Andreas Kohlbecker
   * @param $feature_node
949 c651d3b1 Andreas Kohlbecker
   *  The merged feature three node which potentially contains media in its description elements.
950 ce29c528 Andreas Kohlbecker
   * @param $media_list
951 c651d3b1 Andreas Kohlbecker
   *    Additional media to be merged witht the media contained in the nodes description elements
952 ce29c528 Andreas Kohlbecker
   * @param $gallery_settings
953
   * @return array
954 c651d3b1 Andreas Kohlbecker
   *
955
   * @ingroup compose
956 ce29c528 Andreas Kohlbecker
   */
957 b011743c Andreas Kohlbecker
  function compose_feature_media_gallery($feature_node, $media_list, $gallery_settings) {
958 ce29c528 Andreas Kohlbecker
959 b011743c Andreas Kohlbecker
    if (isset($feature_node->descriptionElements)) {
960
      $media_list = array_merge($media_list, cdm_dataportal_media_from_descriptionElements($feature_node->descriptionElements));
961 ce29c528 Andreas Kohlbecker
    }
962
963
    $captionElements = array('title', 'rights');
964
965
    if (isset($media_list[0]) && isset($gallery_settings['cdm_dataportal_media_maxextend']) && isset($gallery_settings['cdm_dataportal_media_cols'])) {
966 a9815578 Andreas Kohlbecker
      $gallery = compose_cdm_media_gallerie(array(
967 ce29c528 Andreas Kohlbecker
        'mediaList' => $media_list,
968 b011743c Andreas Kohlbecker
        'galleryName' => CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME . '_' . $feature_node->term->uuid,
969 ce29c528 Andreas Kohlbecker
        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
970
        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
971
        'captionElements' => $captionElements,
972
      ));
973
      return markup_to_render_array($gallery);
974
    }
975
976
    return markup_to_render_array('');
977
  }
978
979
  /**
980 4a236db6 Andreas Kohlbecker
   * Composes the distribution feature block for a taxon
981
   *
982 ce29c528 Andreas Kohlbecker
   * @param $taxon
983
   * @param $descriptionElements
984
   *   an associative array with two elements:
985
   *   - '#type': must be 'DTO'
986
   *   - 'DistributionInfoDTO': a CDM DistributionInfoDTO object as returned by the DistributionInfo web service
987
   * @param $feature
988
   *
989 18727898 Andreas Kohlbecker
   * @return array
990
   *  A drupal render array
991
   *
992 ce29c528 Andreas Kohlbecker
   * @ingroup compose
993
   */
994
  function compose_feature_block_distribution($taxon, $descriptionElements, $feature) {
995
    $text_data_glue = '';
996
    $text_data_sortOutArray = FALSE;
997
    $text_data_enclosingTag = 'ul';
998
    $text_data_out_array = array();
999
1000
    $distributionElements = NULL;
1001
    $distribution_info_dto = NULL;
1002
    $distribution_sortOutArray = FALSE;
1003
1004
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
1005
1006 284fb36d Andreas Kohlbecker
    // TODO use feature_block_settings instead of DISTRIBUTION_ORDER_MODE
1007 97ac3d38 Andreas Kohlbecker
    if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) != 'TREE') {
1008 ce29c528 Andreas Kohlbecker
      $distribution_glue = '';
1009
      $distribution_enclosingTag = 'dl';
1010
    } else {
1011
      $distribution_glue = '';
1012
      $distribution_enclosingTag = 'ul';
1013
    }
1014
1015
    if (!isset($descriptionElements['#type']) || !$descriptionElements['#type'] == 'DTO') {
1016
      // skip the DISTRIBUTION section if there is no DTO type element
1017
      return array(); // FIXME is it ok to return an empty array?
1018
    }
1019
1020
    $block = feature_block(
1021
      cdm_term_representation($feature, 'Unnamed Feature'),
1022
      $feature
1023
    );
1024 f3baa35e Andreas Kohlbecker
    $block->content = array();
1025 ce29c528 Andreas Kohlbecker
1026
    // $$descriptionElements['TextData'] is added to to the feature node in merged_taxon_feature_tree()
1027
    if (isset($descriptionElements['TextData'])) {
1028
      // --- TextData
1029
      foreach ($descriptionElements['TextData'] as $text_data_element) {
1030 0ef9a709 Andreas Kohlbecker
        $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
1031 ce29c528 Andreas Kohlbecker
        $repr = drupal_render($text_data_render_array);
1032
1033
        if (!array_search($repr, $text_data_out_array)) { // de-duplication !!
1034
          $text_data_out_array[] = $repr;
1035 c651d3b1 Andreas Kohlbecker
          // TODO HINT: sorting in compose_feature_block_wrap_elements will
1036 ce29c528 Andreas Kohlbecker
          // not work since this array contains html attributes with uuids
1037
          // and what is about cases like the bibliography where
1038
          // any content can be prefixed with some foot-note anchors?
1039
          $text_data_sortOutArray = TRUE;
1040
          $text_data_glue = '<br/> ';
1041
          $text_data_enclosingTag = 'p';
1042
        }
1043
      }
1044
    }
1045
1046
1047
    if ($text_data_out_array && variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
1048 c651d3b1 Andreas Kohlbecker
      $block->content[] = compose_feature_block_wrap_elements(
1049 dfc49afb Andreas Kohlbecker
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
1050 ce29c528 Andreas Kohlbecker
      );
1051
    }
1052
1053
    // --- Distribution map
1054
    $distribution_map_query_parameters = NULL;
1055 378bc1ce Andreas Kohlbecker
1056 e0613197 Andreas Kohlbecker
    $map_visibility = variable_get(DISTRIBUTION_MAP_VISIBILITY, DISTRIBUTION_MAP_VISIBILITY_DEFAULT);
1057
    if(variable_get(DISTRIBUTION_MAP_VISIBILITY, DISTRIBUTION_MAP_VISIBILITY_DEFAULT) == 'always' ||
1058
        $map_visibility == 'automatic' && isset($descriptionElements['DistributionInfoDTO']->mapUriParams)
1059 378bc1ce Andreas Kohlbecker
      )
1060
    {
1061 ce29c528 Andreas Kohlbecker
      $distribution_map_query_parameters = $descriptionElements['DistributionInfoDTO']->mapUriParams;
1062
    }
1063 653e9c6b Andreas Kohlbecker
    $map_render_element = compose_distribution_map($distribution_map_query_parameters);
1064 ce29c528 Andreas Kohlbecker
    $block->content[] = $map_render_element;
1065
1066
    $dto_out_array = array();
1067 bda17f32 Andreas Kohlbecker
1068
    // --- Condensed Distribution
1069
    if(variable_get(DISTRIBUTION_CONDENSED) && isset($descriptionElements['DistributionInfoDTO']->condensedDistribution)){
1070
      $condensed_distribution_markup = '<p class="condensed_distribution">';
1071
1072
      $isFirst = true;
1073
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous[0])){
1074
        foreach($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous as $cdItem){
1075
          if(!$isFirst){
1076
            $condensed_distribution_markup .= ' ';
1077
          }
1078
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->idInVocabulary . '">'
1079
          . $cdItem->areaStatusLabel . '</span>';
1080
          $isFirst = false;
1081
        }
1082
      }
1083
1084
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign[0])) {
1085
        if(!$isFirst){
1086
          $condensed_distribution_markup .= ' ';
1087
        }
1088
        $isFirst = TRUE;
1089
        $condensed_distribution_markup .= '[';
1090
        foreach ($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign as $cdItem) {
1091
          if (!$isFirst) {
1092
            $condensed_distribution_markup .= ' ';
1093
          }
1094
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->titleCache . '">'
1095
            . $cdItem->areaStatusLabel . '</span>';
1096
          $isFirst = false;
1097
        }
1098
        $condensed_distribution_markup .= ']';
1099
      }
1100
1101 423486b0 Andreas Kohlbecker
      $condensed_distribution_markup .= '&nbsp;' . l(
1102
          font_awesome_icon_markup(
1103
            'fa-info-circle',
1104
            array(
1105
              'alt'=>'help',
1106
              'class' => array('superscript')
1107
            )
1108
          ),
1109 6858b474 Andreas Kohlbecker
          variable_get(DISTRIBUTION_CONDENSED_INFO_PATH, DISTRIBUTION_CONDENSED_INFO_PATH_DEFAULT) ,
1110 423486b0 Andreas Kohlbecker
          array('html' => TRUE));
1111 bda17f32 Andreas Kohlbecker
      $condensed_distribution_markup .= '</p>';
1112
      $dto_out_array[] = $condensed_distribution_markup;
1113
    }
1114
1115
    // --- tree or list
1116 ce29c528 Andreas Kohlbecker
    if (isset($descriptionElements['DistributionInfoDTO'])) {
1117
      $distribution_info_dto = $descriptionElements['DistributionInfoDTO'];
1118
1119
      // --- tree
1120
      if (is_object($distribution_info_dto->tree)) {
1121 97ac3d38 Andreas Kohlbecker
        $distribution_tree_render_array = compose_distribution_hierarchy($distribution_info_dto->tree, $feature_block_settings);
1122 f23ae4e3 Andreas Kohlbecker
        $dto_out_array[] = $distribution_tree_render_array;
1123 ce29c528 Andreas Kohlbecker
      }
1124
1125
      // --- sorted element list
1126
      if (is_array($distribution_info_dto->elements) && count($distribution_info_dto->elements) > 0) {
1127
        foreach ($distribution_info_dto->elements as $descriptionElement) {
1128
          if (is_object($descriptionElement->area)) {
1129
            $sortKey = $descriptionElement->area->representation_L10n;
1130
            $distributionElements[$sortKey] = $descriptionElement;
1131
          }
1132
        }
1133
        ksort($distributionElements);
1134 63740051 Andreas Kohlbecker
        $distribution_element_render_array = compose_description_elements_distribution($distributionElements);
1135 f23ae4e3 Andreas Kohlbecker
        $dto_out_array[] = $distribution_element_render_array;
1136 ce29c528 Andreas Kohlbecker
1137
      }
1138
      //
1139 c651d3b1 Andreas Kohlbecker
      $block->content[] = compose_feature_block_wrap_elements(
1140 dfc49afb Andreas Kohlbecker
        $dto_out_array, $feature, $distribution_glue, $distribution_sortOutArray
1141 ce29c528 Andreas Kohlbecker
      );
1142
    }
1143
1144
    // --- TextData at the bottom
1145
    if ($text_data_out_array && !variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
1146 c651d3b1 Andreas Kohlbecker
      $block->content[] = compose_feature_block_wrap_elements(
1147 dfc49afb Andreas Kohlbecker
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
1148 ce29c528 Andreas Kohlbecker
      );
1149
    }
1150
1151 b810c46f Andreas Kohlbecker
    $block->content[] = markup_to_render_array(cdm_footnotes('BIBLIOGRAPHY-' . UUID_DISTRIBUTION));
1152
    $block->content[] = markup_to_render_array(cdm_footnotes(UUID_DISTRIBUTION));
1153 f95bf1b1 Andreas Kohlbecker
    $block->content[] = markup_to_render_array(cdm_annotation_footnotes(UUID_DISTRIBUTION));
1154 ce29c528 Andreas Kohlbecker
1155
    return $block;
1156
  }
1157
1158
1159
  /**
1160 0ebb6efc Andreas Kohlbecker
   * Composes a drupal render array for single CDM TextData description element.
1161 ce29c528 Andreas Kohlbecker
   *
1162
   * @param $element
1163 0ef9a709 Andreas Kohlbecker
   *    The CDM TextData description element.
1164 ce29c528 Andreas Kohlbecker
   *  @param $feature_uuid
1165 c651d3b1 Andreas Kohlbecker
   * @param bool $prepend_feature_label
1166
   *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1167 ce29c528 Andreas Kohlbecker
   *
1168
   * @return array
1169
   *   A drupal render array with the following elements being used:
1170
   *    - #tag: either 'div', 'li', ...
1171
   *    ⁻ #attributes: class attributes
1172
   *    - #value_prefix: (optionally) contains footnote anchors
1173
   *    - #value: contains the textual content
1174
   *    - #value_suffix: (optionally) contains footnote keys
1175
   *
1176
   * @ingroup compose
1177
   */
1178 c651d3b1 Andreas Kohlbecker
  function compose_description_element_text_data($element, $feature_uuid, $feature_block_settings, $prepend_feature_label = FALSE) {
1179 ce29c528 Andreas Kohlbecker
1180
    $footnote_list_key_suggestion = $feature_uuid;
1181
1182 0ef9a709 Andreas Kohlbecker
    $element_markup = '';
1183 ce29c528 Andreas Kohlbecker
    if (isset($element->multilanguageText_L10n->text)) {
1184
      // TODO replacement of \n by <br> should be configurable
1185 0ef9a709 Andreas Kohlbecker
      $element_markup = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
1186 ce29c528 Andreas Kohlbecker
    }
1187
1188 c651d3b1 Andreas Kohlbecker
    $render_array = compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label);
1189 ce29c528 Andreas Kohlbecker
1190 dfc49afb Andreas Kohlbecker
    return $render_array;
1191
  }
1192 ce29c528 Andreas Kohlbecker
1193
1194 e413f218 Andreas Kohlbecker
/**
1195
 * Theme function to render CDM DescriptionElements of the type TaxonInteraction.
1196
 *
1197
 * @param $element
1198
 *  The CDM TaxonInteraction entity
1199
 *
1200
 * @return
1201
 *  A drupal render array
1202
 *
1203
 * @ingroup compose
1204
 */
1205
function compose_description_element_taxon_interaction($element, $feature_block_settings) {
1206
1207
  $out = '';
1208 57991ddf Andreas Kohlbecker
1209 e413f218 Andreas Kohlbecker
1210
  if (isset($element->description_L10n)) {
1211
    $out .=  ' ' . $element->description_L10n;
1212
  }
1213
1214
  if(isset($element->taxon2)){
1215
    $out = render_taxon_or_name($element->taxon2, url(path_to_taxon($element->taxon2->uuid)));
1216
  }
1217
1218 f23ae4e3 Andreas Kohlbecker
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1219 e413f218 Andreas Kohlbecker
1220 f23ae4e3 Andreas Kohlbecker
  return $render_array;
1221 e413f218 Andreas Kohlbecker
}
1222
1223
1224 dfc49afb Andreas Kohlbecker
/**
1225
 * Renders a single instance of the type IndividualsAssociations.
1226
 *
1227
 * @param $element
1228
 *   The CDM IndividualsAssociations entity.
1229
 * @param $feature_block_settings
1230
 *
1231
 * @return array
1232
 *   Drupal render array
1233
 *
1234
 * @ingroup compose
1235
 */
1236
function compose_description_element_individuals_association($element, $feature_block_settings) {
1237
1238
  $out = '';
1239 ce29c528 Andreas Kohlbecker
1240 bd814ea2 Andreas Kohlbecker
  $render_array = compose_cdm_specimen_or_observation($element->associatedSpecimenOrObservation);
1241 dfc49afb Andreas Kohlbecker
1242
  if (isset($element->description_L10n)) {
1243
    $out .=  ' ' . $element->description_L10n;
1244 ce29c528 Andreas Kohlbecker
  }
1245
1246 dfc49afb Andreas Kohlbecker
  $out .= drupal_render($render_array);
1247 ce29c528 Andreas Kohlbecker
1248 f23ae4e3 Andreas Kohlbecker
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1249 dfc49afb Andreas Kohlbecker
1250 f23ae4e3 Andreas Kohlbecker
  return $render_array;
1251 dfc49afb Andreas Kohlbecker
}
1252
1253 529c47ff Andreas Kohlbecker
/**
1254
 * Renders a single instance of the type CategoricalData.
1255
 *
1256
 * @param $element
1257 c651d3b1 Andreas Kohlbecker
 *  The CDM CategoricalData entity
1258
 *
1259 4f8e61ad Andreas Kohlbecker
 * @param $feature_block_settings
1260 529c47ff Andreas Kohlbecker
 *
1261 c651d3b1 Andreas Kohlbecker
 * @param bool $prepend_feature_label
1262
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1263
 *
1264 55236f7f Andreas Kohlbecker
 * @return array
1265
 *   a drupal render array for given CategoricalData element
1266 529c47ff Andreas Kohlbecker
 *
1267
 * @ingroup compose
1268
 */
1269 c651d3b1 Andreas Kohlbecker
function compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1270 529c47ff Andreas Kohlbecker
1271 f63479ae Andreas Kohlbecker
  $state_data_markup = render_state_data($element);
1272 e170a53e Patrick Plitzner
1273 f63479ae Andreas Kohlbecker
  $render_array = compose_description_element($element, $feature_block_settings, $state_data_markup, $element->feature->uuid, $prepend_feature_label);
1274 e170a53e Patrick Plitzner
1275
  return $render_array;
1276
}
1277
1278
/**
1279
 * @param $element
1280
 *
1281
 * @return string
1282 f63479ae Andreas Kohlbecker
 * the markup
1283 e170a53e Patrick Plitzner
 */
1284 f63479ae Andreas Kohlbecker
function render_state_data($element) {
1285 882e2c02 Patrick Plitzner
1286 f63479ae Andreas Kohlbecker
  $state_data_items = [];
1287 882e2c02 Patrick Plitzner
1288 f146528a Andreas Kohlbecker
  $out = '';
1289
1290 529c47ff Andreas Kohlbecker
  if (isset($element->stateData)) {
1291
    foreach ($element->stateData as $state_data) {
1292
1293 e170a53e Patrick Plitzner
      $state = NULL;
1294 529c47ff Andreas Kohlbecker
1295
      if (isset($state_data->state)) {
1296
        $state = cdm_term_representation($state_data->state);
1297
1298 fec3fd41 Andreas Kohlbecker
          $sample_count = 0;
1299 4b428921 Patrick Plitzner
          if (isset($state_data->count)) {
1300 fec3fd41 Andreas Kohlbecker
            $sample_count = $state_data->count;
1301 f63479ae Andreas Kohlbecker
            $state .= ' [' . $state_data->count . ']';
1302 4b428921 Patrick Plitzner
          }
1303
    
1304
          if (isset($state_data->modifyingText_L10n)) {
1305
            $state .= ' ' . $state_data->modifyingText_L10n;
1306
          }
1307
    
1308 a6d7907d Andreas Kohlbecker
          $modifiers_strings = cdm_modifiers_representations($state_data);
1309 4c9d7e32 Andreas Kohlbecker
          $state_data_markup = $state . ($modifiers_strings ? ' ' . $modifiers_strings : '');
1310
          // we could use strip_tags() to reduce the markup to text for the key but this is expensive
1311 fec3fd41 Andreas Kohlbecker
          $sort_key = str_pad($sample_count, 6, '0', STR_PAD_LEFT) . '_' . $state_data_markup;
1312 f63479ae Andreas Kohlbecker
          $state_data_items[$sort_key] = $state_data_markup;
1313 529c47ff Andreas Kohlbecker
      }
1314
1315
    }
1316 f63479ae Andreas Kohlbecker
    krsort($state_data_items);
1317
    $out = '<span class="' . html_class_attribute_ref($element) . '">' . join(', ', $state_data_items) .  '</span>';
1318 529c47ff Andreas Kohlbecker
  }
1319 f63479ae Andreas Kohlbecker
  return $out;
1320 4f8e61ad Andreas Kohlbecker
}
1321
1322
1323
/**
1324
 * Theme function to render CDM DescriptionElements of the type QuantitativeData.
1325
 *
1326
 * The function renders the statisticalValues contained in the QuantitativeData
1327
 * entity according to the following scheme:
1328
 *
1329
 * (ExtremeMin)-Min-Average-Max-(ExtremeMax)
1330
 *
1331
 * All modifiers of these values are appended.
1332
 *
1333
 * If the QuantitativeData is containing more statisticalValues with further
1334
 * statisticalValue types, these additional measures will be appended to the
1335
 * above string separated by whitespace.
1336
 *
1337
 * Special cases;
1338
 * 1. Min==Max: this will be interpreted as Average
1339
 *
1340 c651d3b1 Andreas Kohlbecker
 * @param $element
1341
 *  The CDM QuantitativeData entity
1342
 *
1343
 * @param $feature_block_settings
1344
 *
1345
 * @param bool $prepend_feature_label
1346
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1347
 *
1348 4f8e61ad Andreas Kohlbecker
 *
1349 55236f7f Andreas Kohlbecker
 * @return array
1350
 *   drupal render array for the given QuantitativeData element
1351 4f8e61ad Andreas Kohlbecker
 *
1352 55236f7f Andreas Kohlbecker
 * @ingroup compose
1353 4f8e61ad Andreas Kohlbecker
 */
1354 c651d3b1 Andreas Kohlbecker
function compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1355 4f8e61ad Andreas Kohlbecker
  /*
1356
   * - statisticalValues
1357
   *   - value
1358
   *   - modifiers
1359
   *   - type
1360
   * - unit->representation_L10n
1361
   * - modifyingText
1362
   * - modifiers
1363
   * - sources
1364
   */
1365
1366 fec3fd41 Andreas Kohlbecker
  $out = render_quantitative_statistics($element);
1367 e170a53e Patrick Plitzner
1368
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid, $prepend_feature_label);
1369
1370
  return $render_array;
1371
}
1372
1373
/**
1374
 * Composes the HTML for quantitative statistics
1375
 * @param $element
1376
 *
1377
 * @return string
1378
 */
1379 fec3fd41 Andreas Kohlbecker
function render_quantitative_statistics($element) {
1380 ad7fa57c Andreas Kohlbecker
1381 4f8e61ad Andreas Kohlbecker
  $out = '';
1382
  $type_representation = NULL;
1383 2f4646e0 Andreas Kohlbecker
  $min_max = statistical_values_array();
1384 ad7fa57c Andreas Kohlbecker
  $sample_size_markup = null;
1385 4f8e61ad Andreas Kohlbecker
1386
  if (isset($element->statisticalValues)) {
1387 fec3fd41 Andreas Kohlbecker
    $out = '<span class=\"' . html_class_attribute_ref($element) . '\">';
1388 e170a53e Patrick Plitzner
    $other_values_markup = [];
1389 4f8e61ad Andreas Kohlbecker
    foreach ($element->statisticalValues as $statistical_val) {
1390
1391
      // compile the full value string which also may contain modifiers
1392
      if (isset($statistical_val->value)) {
1393
        $statistical_val->_value = $statistical_val->value;
1394
      }
1395 a6d7907d Andreas Kohlbecker
      $val_modifiers_strings = cdm_modifiers_representations($statistical_val);
1396 4f8e61ad Andreas Kohlbecker
      if ($val_modifiers_strings) {
1397
        $statistical_val->_value = ' ' . $val_modifiers_strings . ' ' . $statistical_val->_value;
1398
      }
1399
1400
      // either put into min max array or into $other_values
1401
      // for generic output to be appended to 'min-max' string
1402 f63479ae Andreas Kohlbecker
      if (array_key_exists(statistical_measure_term2min_max_key($statistical_val->type), $min_max)) {
1403
        $min_max[statistical_measure_term2min_max_key($statistical_val->type)] = $statistical_val;
1404 4f8e61ad Andreas Kohlbecker
      }
1405
      else {
1406 7f36e33a Andreas Kohlbecker
        drupal_set_message("Unsupported statistical value type: " . $statistical_val->type->uuid, "error");
1407 4f8e61ad Andreas Kohlbecker
      }
1408
    } // end of loop over statisticalValues
1409
1410
    // create markup
1411 7f36e33a Andreas Kohlbecker
    $unit = null;
1412
    if (isset($element->unit)) {
1413
      $unit = ' <span class="unit" title="'
1414
        . cdm_term_representation($element->unit) . '">'
1415
        . cdm_term_representation_abbreviated($element->unit)
1416
        . '</span>';
1417 4f8e61ad Andreas Kohlbecker
    }
1418 7f36e33a Andreas Kohlbecker
    $min_max_markup = statistical_values($min_max, $unit);
1419
    $out .= $min_max_markup . '</span>';
1420 4f8e61ad Andreas Kohlbecker
  }
1421
1422 ad7fa57c Andreas Kohlbecker
  if($sample_size_markup){
1423
    $out .= ' ' . $sample_size_markup;
1424
1425
  }
1426
1427 4f8e61ad Andreas Kohlbecker
  // modifiers of the description element itself
1428 a6d7907d Andreas Kohlbecker
  $modifier_string = cdm_modifiers_representations($element);
1429 4f8e61ad Andreas Kohlbecker
  $out .= ($modifier_string ? ' ' . $modifier_string : '');
1430
  if (isset($element->modifyingText_L10n)) {
1431
    $out = $element->modifyingText_L10n . ' ' . $out;
1432
  }
1433 e170a53e Patrick Plitzner
  return $out;
1434 529c47ff Andreas Kohlbecker
}
1435
1436 f63479ae Andreas Kohlbecker
function statistical_measure_term2min_max_key($term){
1437
  static $uuid2key = [
1438
    UUID_STATISTICALMEASURE_MIN => 'Min',
1439
    UUID_STATISTICALMEASURE_MAX => 'Max',
1440
    UUID_STATISTICALMEASURE_AVERAGE => 'Average',
1441
    UUID_STATISTICALMEASURE_SAMPLESIZE => 'SampleSize',
1442
    UUID_STATISTICALMEASURE_VARIANCE => 'Variance',
1443
    UUID_STATISTICALMEASURE_TYPICALLOWERBOUNDARY => 'TypicalLowerBoundary',
1444
    UUID_STATISTICALMEASURE_TYPICALUPPERBOUNDARY => 'TypicalUpperBoundary',
1445
    UUID_STATISTICALMEASURE_STANDARDDEVIATION => 'StandardDeviation',
1446
    UUID_STATISTICALMEASURE_EXACTVALUE => 'ExactValue',
1447
    UUID_STATISTICALMEASURE_STATISTICALMEASUREUNKNOWNDATA => 'StatisticalMeasureUnknownData'
1448
  ];
1449
  return $uuid2key[$term->uuid];
1450
}
1451
1452 dfc49afb Andreas Kohlbecker
1453
/**
1454 c651d3b1 Andreas Kohlbecker
 * Wraps the render array for the given feature into an enclosing html tag.
1455 dfc49afb Andreas Kohlbecker
 *
1456 c651d3b1 Andreas Kohlbecker
 * Optionally the elements can be sorted and glued together by a separator string.
1457
 *
1458
 * @param array $description_element_render_arrays
1459 f23ae4e3 Andreas Kohlbecker
 *   An list of render arrays. Which are usually are produced by the compose_description_element()
1460
 *   function. The render arrays should be of #type=html_tag, so that they can understand the #attribute property.
1461 dfc49afb Andreas Kohlbecker
 * @param  $feature :
1462
 *  The feature to which the elements given in $elements are belonging to.
1463
 * @param string $glue :
1464
 *  Defaults to empty string.
1465
 * @param bool $sort
1466
 *   Boolean Whether to sort the $elements alphabetically, default is FALSE
1467
 *
1468 c651d3b1 Andreas Kohlbecker
 * @return array
1469
 *    A Drupal render array
1470 dfc49afb Andreas Kohlbecker
 *
1471
 * @ingroup compose
1472
 */
1473 c651d3b1 Andreas Kohlbecker
  function compose_feature_block_wrap_elements(array $description_element_render_arrays, $feature, $glue = '', $sort = FALSE)
1474 dfc49afb Andreas Kohlbecker
  {
1475 ce29c528 Andreas Kohlbecker
1476
    $feature_block_settings = get_feature_block_settings($feature->uuid);
1477 0ef9a709 Andreas Kohlbecker
    $enclosing_tag = $feature_block_settings['as_list'];
1478 ce29c528 Andreas Kohlbecker
1479 f23ae4e3 Andreas Kohlbecker
    if ($sort) { // TODO remove parameter and replace by $feature_block_settings['sort_elements']
1480 c651d3b1 Andreas Kohlbecker
      usort($description_element_render_arrays, 'compare_description_element_render_arrays');
1481 f23ae4e3 Andreas Kohlbecker
    }
1482 ce29c528 Andreas Kohlbecker
1483 f23ae4e3 Andreas Kohlbecker
    $is_first = true;
1484 c651d3b1 Andreas Kohlbecker
    foreach($description_element_render_arrays as &$element_render_array){
1485 f23ae4e3 Andreas Kohlbecker
      if(!is_array($element_render_array)){
1486
        $element_render_array = markup_to_render_array($element_render_array);
1487
      }
1488
      $element_render_array['#attributes']['class'][] = "feature-block-element";
1489
1490
      // add the glue!
1491
      if(!$is_first) {
1492
        if (isset($element_render_array['#value'])) {
1493
          $element_render_array['#value'] = $glue . $element_render_array['#value'];
1494
        } elseif (isset($element_render_array['#markup'])) {
1495
          $element_render_array['#markup'] = $glue . $element_render_array['#markup'];
1496
        }
1497
      }
1498
      $is_first = false;
1499 ce29c528 Andreas Kohlbecker
    }
1500
1501 c651d3b1 Andreas Kohlbecker
    $render_array['elements']['children'] = $description_element_render_arrays;
1502 ce29c528 Andreas Kohlbecker
1503 f23ae4e3 Andreas Kohlbecker
    $render_array['elements']['#prefix'] = '<' . $enclosing_tag . ' class="feature-block-elements" id="' . $feature->representation_L10n . '">';
1504
    $render_array['elements']['#suffix'] = '</' . $enclosing_tag . '>';
1505
1506
    return $render_array;
1507 ce29c528 Andreas Kohlbecker
  }
1508
1509
1510
  /* compose nameInSource or originalNameString as markup
1511
   *
1512
   * @param $source
1513
   * @param $do_link_to_name_used_in_source
1514
   * @param $suppress_for_shown_taxon
1515
   *    the nameInSource will be suppressed when it has the same name as the accepted taxon
1516
   *    for which the taxon page is being created, Defaults to TRUE
1517
   *
1518
   * @return array
1519
   *    A Drupal render array with an additional element, the render array is empty
1520
   *    if the source had no name in source information
1521
   *    - #_plaintext: contains the plaintext version of the name (custom element)
1522
   *
1523
   * @ingroup compose
1524
   */
1525
  function compose_name_in_source($source, $do_link_to_name_used_in_source, $suppress_for_shown_taxon = TRUE) {
1526
1527
    $plaintext = NULL;
1528
    $markup = NULL;
1529
    $name_in_source_render_array = array();
1530
1531
    static $taxon_page_accepted_name = '';
1532 0af3ce28 Andreas Kohlbecker
    $taxon_uuid = get_current_taxon_uuid();
1533
    if($suppress_for_shown_taxon && $taxon_uuid && empty($taxon_page_accepted_name)){
1534 ce29c528 Andreas Kohlbecker
1535 0af3ce28 Andreas Kohlbecker
      $current_taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $taxon_uuid);
1536 ce29c528 Andreas Kohlbecker
      $taxon_page_accepted_name = $current_taxon->name->titleCache;
1537
    }
1538
1539
    if (isset($source->nameUsedInSource->uuid) && isset($source->nameUsedInSource->titleCache)) {
1540
      // it is a DescriptionElementSource !
1541
      $plaintext = $source->nameUsedInSource->titleCache;
1542
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1543
        return $name_in_source_render_array; // SKIP this name
1544
      }
1545 63c3ac73 Andreas Kohlbecker
      $markup = render_taxon_or_name($source->nameUsedInSource);
1546 ce29c528 Andreas Kohlbecker
      if ($do_link_to_name_used_in_source) {
1547
        $markup = l(
1548
          $markup,
1549
          path_to_name($source->nameUsedInSource->uuid),
1550
          array(
1551
            'attributes' => array(),
1552
            'absolute' => TRUE,
1553
            'html' => TRUE,
1554
          ));
1555
      }
1556
    }
1557
    else if (isset($source->originalNameString) && !empty($source->originalNameString)) {
1558
      // the name used in source can not be expressed as valid taxon name,
1559
      // so the editor has chosen to put the freetext name into ReferencedEntityBase.originalNameString
1560
      // field
1561
      // using the originalNameString as key to avoid duplicate entries
1562
      $plaintext = $source->originalNameString;
1563
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1564
        return $name_in_source_render_array; // SKIP this name
1565
      }
1566
      $markup = $source->originalNameString;
1567
    }
1568
1569
    if ($plaintext) { // checks if we have any content
1570
      $name_in_source_render_array = markup_to_render_array($markup);
1571
      $name_in_source_render_array['#_plaintext'] = $plaintext;
1572
    }
1573
1574
    return $name_in_source_render_array;
1575
  }
1576
1577
1578
1579
  /**
1580
   * Return HTML for a list of description elements.
1581
   *
1582
   * Usually these are of a specific feature type.
1583
   *
1584 c651d3b1 Andreas Kohlbecker
   * @param $description_elements
1585 ce29c528 Andreas Kohlbecker
   *   array of descriptionElements which belong to the same feature.
1586
   *   These descriptions elements of a Description must be ordered by the chosen feature tree by
1587
   *   calling the function _mergeFeatureTreeDescriptions().
1588
   *   @see _mergeFeatureTreeDescriptions()
1589
   *
1590
   * @param  $feature_uuid
1591
   *
1592
   * @return
1593
   *    A drupal render array for the $descriptionElements, may be an empty array if the textual content was empty.
1594
   *    Footnote key or anchors are not considered to be textual content.
1595
   *
1596
   * @ingroup compose
1597
   */
1598 c651d3b1 Andreas Kohlbecker
  function compose_feature_block_items_generic($description_elements, $feature) {
1599 ce29c528 Andreas Kohlbecker
1600
    $elements_out_array = array();
1601
    $distribution_tree = null;
1602
1603
    /*
1604
     * $feature_block_has_content will be set true if at least one of the
1605
     * $descriptionElements contains some text which makes up some content
1606
     * for the feature block. Footnote keys are not considered
1607
     * to be content in this sense.
1608
     */
1609
    $feature_block_has_content = false;
1610
1611 c651d3b1 Andreas Kohlbecker
    if (is_array($description_elements)) {
1612
      foreach ($description_elements as $description_element) {
1613 ce29c528 Andreas Kohlbecker
          /* decide based on the description element class
1614
           *
1615
           * Features handled here:
1616
           * all except DISTRIBUTION, COMMON_NAME, USES, IMAGES,
1617
           *
1618
           * TODO provide api_hook as extension point for this?
1619
           */
1620 c651d3b1 Andreas Kohlbecker
        $feature_block_settings = get_feature_block_settings($description_element->feature->uuid);
1621
        switch ($description_element->class) {
1622 f23ae4e3 Andreas Kohlbecker
          case 'TextData':
1623 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_text_data($description_element, $description_element->feature->uuid, $feature_block_settings);
1624 f23ae4e3 Andreas Kohlbecker
            break;
1625
          case 'CategoricalData':
1626 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_categorical_data($description_element, $feature_block_settings);
1627 f23ae4e3 Andreas Kohlbecker
            break;
1628
          case 'QuantitativeData':
1629 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_quantitative_data($description_element, $feature_block_settings);
1630 f23ae4e3 Andreas Kohlbecker
            break;
1631
          case 'IndividualsAssociation':
1632 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_individuals_association($description_element, $feature_block_settings);
1633 f23ae4e3 Andreas Kohlbecker
            break;
1634
          case 'TaxonInteraction':
1635 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_taxon_interaction($description_element, $feature_block_settings);
1636 f23ae4e3 Andreas Kohlbecker
            break;
1637 57991ddf Andreas Kohlbecker
          case 'CommonTaxonName':
1638
            $elements_out_array[] = compose_description_element_common_taxon_name($description_element, $feature_block_settings);
1639
            break;
1640 f23ae4e3 Andreas Kohlbecker
          case 'Uses':
1641 1b756c5f Andreas Kohlbecker
            /* IGNORE Uses classes, these are handled completely in compose_feature_block_items_use_records()  */
1642 f23ae4e3 Andreas Kohlbecker
            break;
1643
          default:
1644
            $feature_block_has_content = true;
1645 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = markup_to_render_array('<li>No method for rendering unknown description class: ' . $description_element->class . '</li>');
1646 f23ae4e3 Andreas Kohlbecker
        }
1647
        $elements_out_array_last_item = $elements_out_array[count($elements_out_array) - 1];
1648
        // considering not empty as long as the last item added is a render array
1649
        $feature_block_has_content = $feature_block_has_content || !empty($elements_out_array_last_item['#type']);
1650
      }
1651 ce29c528 Andreas Kohlbecker
1652
      // If feature = CITATION sort the list of sources.
1653
      // This is ONLY for FLORA MALESIANA and FLORE d'AFRIQUE CENTRALE.
1654 c651d3b1 Andreas Kohlbecker
      if (isset($description_element) && $description_element->feature->uuid == UUID_CITATION) {
1655 ce29c528 Andreas Kohlbecker
        sort($elements_out_array);
1656
      }
1657 aa26f9f0 Andreas Kohlbecker
    }
1658 ce29c528 Andreas Kohlbecker
1659 7a751db4 Andreas Kohlbecker
    // sanitize: remove empty and NULL items from the render array
1660
    $tmp_out_array = $elements_out_array;
1661
    $elements_out_array = array();
1662
    foreach($tmp_out_array as $item){
1663 6a0ca989 Andreas Kohlbecker
      if(is_array($item) && count($item) > 0){
1664 7a751db4 Andreas Kohlbecker
        $elements_out_array[] = $item;
1665
      }
1666
    }
1667
1668 c651d3b1 Andreas Kohlbecker
    return $elements_out_array;
1669 ce29c528 Andreas Kohlbecker
  }
1670
1671 4a236db6 Andreas Kohlbecker
/**
1672
 * Composes block of common names for the given DescriptionElements $elements which must be of the feature CommonName
1673
 *
1674
 * @parameter $elements
1675
 *  an array of CDM DescriptionElements either of type CommonName or TextData
1676
 * @parameter $feature
1677
 *  the common feature of all $elements, must be CommonName
1678
 *
1679
 * @return
1680
 *   A drupal render array
1681
 *
1682
 * @ingroup compose
1683
 */
1684 0ebb6efc Andreas Kohlbecker
function compose_feature_block_items_feature_common_name($elements, $feature, $weight = FALSE) {
1685 4a236db6 Andreas Kohlbecker
1686
  $common_name_out = '';
1687
  $common_name_feature_elements = array();
1688
  $textData_commonNames = array();
1689
1690
  $footnote_key_suggestion = 'common-names-feature-block';
1691
1692
  $feature_block_settings = get_feature_block_settings(UUID_COMMON_NAME);
1693
1694
  if (is_array($elements)) {
1695
    foreach ($elements as $element) {
1696
1697
      if ($element->class == 'CommonTaxonName') {
1698
1699
        // common name without a language or area, should not happen but is possible
1700
        $language_area_key = '';
1701
        if (isset($element->language->representation_L10n)) {
1702 a20ddbc4 Andreas Kohlbecker
          $language_area_key .= $element->language->representation_L10n ;
1703 4a236db6 Andreas Kohlbecker
        }
1704
        if(isset($element->area->titleCache) && strlen($element->area->titleCache) > 0){
1705
          $language_area_key .= ($language_area_key ? ' '  : '') . '(' . $element->area->titleCache . ')';
1706
        }
1707 a20ddbc4 Andreas Kohlbecker
        if($language_area_key){
1708
          $language_area_key = '<span class="language-area-label">' . $language_area_key . '<span class="separator">: </span></span>';
1709
        }
1710 4a236db6 Andreas Kohlbecker
1711
        if(isset($common_names[$language_area_key][$element->name])) {
1712 f23ae4e3 Andreas Kohlbecker
          // same name already exists for language and area combination, se we merge the description elements
1713 4a236db6 Andreas Kohlbecker
          cdm_merge_description_elements($common_names[$language_area_key][$element->name], $element);
1714
        } else{
1715
          // otherwise add as new entry
1716
          $common_names[$language_area_key][$element->name] = $element;
1717
        }
1718
1719
      }
1720
      elseif ($element->class == 'TextData') {
1721
        $textData_commonNames[] = $element;
1722
      }
1723
    }
1724
  }
1725
  // Handling common names.
1726
  if (isset($common_names) && count($common_names) > 0) {
1727
    // Sorting the array based on the key (language, + area if set).
1728
    // Comment @WA there are common names without a language, so this sorting
1729
    // can give strange results.
1730
    ksort($common_names);
1731
1732
    // loop over set of elements per language area
1733
    foreach ($common_names as $language_area_key => $elements) {
1734
      ksort($elements); // sort names alphabetically
1735
      $per_language_area_out = array();
1736 57991ddf Andreas Kohlbecker
1737 4a236db6 Andreas Kohlbecker
      foreach ($elements as $element) {
1738 57991ddf Andreas Kohlbecker
        $common_name_render_array = compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion);
1739
        $common_name_markup = drupal_render($common_name_render_array);
1740
        // IMPORTANT!
1741
        // during the above drupal_render the theme_html_tag function is executed, which adds a "\n" character to the end of the markup
1742
        // this is an error and the trailing whitespace needs to be removed
1743
        if(str_endsWith($common_name_markup, "\n")){
1744
          $common_name_markup = substr($common_name_markup, 0, strlen($common_name_markup) - 1);
1745 4a236db6 Andreas Kohlbecker
        }
1746 57991ddf Andreas Kohlbecker
        $per_language_area_out[] = $common_name_markup;
1747
      }
1748
1749 a20ddbc4 Andreas Kohlbecker
      $common_name_feature_elements[] = $language_area_key . join(', ', $per_language_area_out);
1750 4a236db6 Andreas Kohlbecker
    } // End of loop over set of elements per language area
1751
1752
1753 c651d3b1 Andreas Kohlbecker
    $common_name_feature_elements_render_array = compose_feature_block_wrap_elements(
1754 dfc49afb Andreas Kohlbecker
      $common_name_feature_elements, $feature, '; ', FALSE
1755 4a236db6 Andreas Kohlbecker
    );
1756 f23ae4e3 Andreas Kohlbecker
    $common_name_out .= drupal_render($common_name_feature_elements_render_array); // FIXME should this be a render array instead?
1757 4a236db6 Andreas Kohlbecker
1758
  }
1759
1760
  // Handling commons names as text data.
1761
  $text_data_out = array();
1762
1763
  foreach ($textData_commonNames as $text_data_element) {
1764 0ef9a709 Andreas Kohlbecker
    /* footnotes are not handled correctly in compose_description_element_text_data,
1765 4a236db6 Andreas Kohlbecker
       need to set 'common-names-feature-block' as $footnote_key_suggestion */
1766
    RenderHints::setFootnoteListKey($footnote_key_suggestion);
1767 0ef9a709 Andreas Kohlbecker
    $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
1768 4a236db6 Andreas Kohlbecker
    $text_data_out[] = drupal_render($text_data_render_array);
1769
  }
1770
1771 c651d3b1 Andreas Kohlbecker
  $common_name_out_text_data = compose_feature_block_wrap_elements(
1772 dfc49afb Andreas Kohlbecker
    $text_data_out, $feature
1773 4a236db6 Andreas Kohlbecker
  );
1774
1775 b810c46f Andreas Kohlbecker
  $footnotes = cdm_footnotes('BIBLIOGRAPHY-' . $footnote_key_suggestion);
1776
  $footnotes .= cdm_footnotes($footnote_key_suggestion); // FIXME is this needed at all?
1777 f95bf1b1 Andreas Kohlbecker
  $footnotes .= cdm_annotation_footnotes($footnote_key_suggestion);
1778 4a236db6 Andreas Kohlbecker
1779 f23ae4e3 Andreas Kohlbecker
  return  markup_to_render_array(  // FIXME markup_to_render_array should no longer be needed
1780 57991ddf Andreas Kohlbecker
    '<div class="common-taxon-name">' . $common_name_out . '</div>'
1781
    .'<div class="text-data">' . drupal_render($common_name_out_text_data) . '</div>'
1782 4a236db6 Andreas Kohlbecker
    .$footnotes,
1783
    $weight
1784
  );
1785
}
1786 ce29c528 Andreas Kohlbecker
1787 57991ddf Andreas Kohlbecker
/**
1788
 * Renders a single instance of the type CommonTaxonName.
1789
 *
1790
 * @param $element
1791
 *   The CDM CommonTaxonName entity.
1792
 * @param $feature_block_settings
1793
 *
1794
 * @param $footnote_key_suggestion
1795
 *
1796
 * @param $element_tag_name
1797
 *
1798
 * @return array
1799
 *   Drupal render array
1800
 *
1801
 * @ingroup compose
1802
 */
1803
function compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion = NULL)
1804
{
1805 ce29c528 Andreas Kohlbecker
1806 57991ddf Andreas Kohlbecker
  if(!$footnote_key_suggestion) {
1807
    $footnote_key_suggestion = $element->feature->uuid;
1808
  }
1809 ce29c528 Andreas Kohlbecker
1810 57991ddf Andreas Kohlbecker
  $name = '';
1811
  if(isset($element->name)){
1812
    $name = $element->name;
1813
  }
1814 ce29c528 Andreas Kohlbecker
1815 18727898 Andreas Kohlbecker
1816 57991ddf Andreas Kohlbecker
  return compose_description_element($element, $feature_block_settings, $name, $footnote_key_suggestion);
1817
}
1818 18727898 Andreas Kohlbecker
1819 57991ddf Andreas Kohlbecker
/**
1820
 * Composes the render array for a CDM Distribution description element
1821
 *
1822
 * @param array $description_elements
1823
 *   Array of CDM Distribution instances
1824
 * @param $enclosingTag
1825
 *   The html tag to be use for the enclosing element
1826
 *
1827
 * @return array
1828
 *   A Drupal render array
1829
 *
1830
 * @ingroup compose
1831
 */
1832
function compose_description_elements_distribution($description_elements){
1833 ce29c528 Andreas Kohlbecker
1834 bf4b3dfa Andreas Kohlbecker
  $markup_array = array();
1835 57991ddf Andreas Kohlbecker
  RenderHints::pushToRenderStack('descriptionElementDistribution');
1836
  RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1837
1838
  $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
1839
  $enclosingTag = cdm_feature_block_element_tag_name($feature_block_settings);
1840
1841
  foreach ($description_elements as $description_element) {
1842
    $annotations_and_sources = handle_annotations_and_sources(
1843
      $description_element,
1844 0c2b9b9d Andreas Kohlbecker
      handle_annotations_and_sources_config($feature_block_settings),
1845 57991ddf Andreas Kohlbecker
      $description_element->area->representation_L10n,
1846
      UUID_DISTRIBUTION
1847
    );
1848
1849
1850 29857cb0 Andreas Kohlbecker
    $status = distribution_status_label_and_markup([$description_element]);
1851 57991ddf Andreas Kohlbecker
1852 bf4b3dfa Andreas Kohlbecker
    $out = '';
1853 57991ddf Andreas Kohlbecker
    $out .= '<' . $enclosingTag . ' class="descriptionElement descriptionElement-' . $description_element->uuid
1854 c797d7db Andreas Kohlbecker
      . ' " title="' . $status['label']. '">'
1855 57991ddf Andreas Kohlbecker
      . $description_element->area->representation_L10n
1856 c797d7db Andreas Kohlbecker
      . $status['markup'];
1857 57991ddf Andreas Kohlbecker
    if(!empty($annotations_and_sources['source_references'])){
1858
      $out .= ' ' . join(' ', $annotations_and_sources['source_references'] );
1859
    }
1860 bf4b3dfa Andreas Kohlbecker
    $out .= $annotations_and_sources['foot_note_keys']   . '</' . $enclosingTag . '>';
1861
    $markup_array[] = $out;
1862 ce29c528 Andreas Kohlbecker
  }
1863
1864 57991ddf Andreas Kohlbecker
  RenderHints::popFromRenderStack();
1865 bf4b3dfa Andreas Kohlbecker
  return markup_to_render_array(join('<span class="separator">' . $feature_block_settings['glue'] . '</span>', $markup_array));
1866 57991ddf Andreas Kohlbecker
}
1867
1868 65345976 Andreas Kohlbecker
  /**
1869 29857cb0 Andreas Kohlbecker
   * @param array $distribution_status
1870 c797d7db Andreas Kohlbecker
   * @return array an array with following keys
1871
   *   - 'label': the plain text status label
1872
   *   - 'markup': markup for the status
1873 65345976 Andreas Kohlbecker
   */
1874 29857cb0 Andreas Kohlbecker
  function distribution_status_label_and_markup(array $distribution_status, $status_glue = '&#8210; ') {
1875
1876 65345976 Andreas Kohlbecker
    $status_markup = '';
1877
    $status_label = '';
1878
1879 29857cb0 Andreas Kohlbecker
    foreach($distribution_status as $status) {
1880
      $status_label .= ($status_label ? $status_glue : '') . $status->representation_L10n;
1881
      $status_markup .=  '<span class="distributionStatus"> '
1882
        . ($status_markup ? $status_glue : '')
1883
        . '<span class="distributionStatus-' . $status->idInVocabulary . '">'
1884
        .  $status->representation_L10n
1885 3de12c0d Andreas Kohlbecker
        . '</span></span>';
1886 65345976 Andreas Kohlbecker
1887
    };
1888 c797d7db Andreas Kohlbecker
    return ['label' => $status_label, 'markup' => $status_markup];
1889 65345976 Andreas Kohlbecker
  }
1890
1891 ce29c528 Andreas Kohlbecker
1892 7508605c Andreas Kohlbecker
  /**
1893
   * Provides the merged feature tree for a taxon profile page.
1894
   *
1895 4feafea8 Andreas Kohlbecker
   * The merging of the profile feature tree is actually done in
1896 7508605c Andreas Kohlbecker
   * _mergeFeatureTreeDescriptions(). See this method  for details
1897
   * on the structure of the merged tree.
1898
   *
1899 4feafea8 Andreas Kohlbecker
   * This method provides a hook which can be used to modify the
1900 7508605c Andreas Kohlbecker
   * merged feature tree after it has been created, see
1901
   * hook_merged_taxon_feature_tree_alter()
1902
   *
1903
   * @param $taxon
1904 6527c2f5 Andreas Kohlbecker
   *    A CDM Taxon instance
1905 7508605c Andreas Kohlbecker
   *
1906
   * @return object
1907 6527c2f5 Andreas Kohlbecker
   *    The merged feature tree
1908 7508605c Andreas Kohlbecker
   *
1909
   */
1910
  function merged_taxon_feature_tree($taxon) {
1911
1912
    // 1. fetch descriptions_by_featuretree but exclude the distribution feature
1913
    $merged_tree = cdm_ws_descriptions_by_featuretree(get_profile_feature_tree(), $taxon->uuid, array(UUID_DISTRIBUTION));
1914
1915
1916
    // 2. find the distribution feature node
1917
    $distribution_node =& cdm_feature_tree_find_node($merged_tree->root->childNodes, UUID_DISTRIBUTION);
1918
1919
    if ($distribution_node) {
1920
      // 3. get the distributionInfoDTO
1921
      $query_parameters = cdm_distribution_filter_query();
1922
      $query_parameters['part'] = array('mapUriParams');
1923
      if(variable_get(DISTRIBUTION_CONDENSED)){
1924
        $query_parameters['part'][] = 'condensedDistribution';
1925
      }
1926 97ac3d38 Andreas Kohlbecker
      if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) == 'TREE') {
1927 7508605c Andreas Kohlbecker
        $query_parameters['part'][] = 'tree';
1928
      }
1929
      else {
1930
        $query_parameters['part'][] = 'elements';
1931
      }
1932 284fb36d Andreas Kohlbecker
      $query_parameters['omitLevels'] = array();
1933
      foreach(variable_get(DISTRIBUTION_TREE_OMIT_LEVELS, array()) as $uuid){
1934
        if(is_uuid($uuid)){
1935
          $query_parameters['omitLevels'][] = $uuid;
1936
        }
1937
      }
1938 7508605c Andreas Kohlbecker
      $customStatusColorsJson = variable_get(DISTRIBUTION_STATUS_COLORS, NULL);
1939
      if ($customStatusColorsJson) {
1940
        $query_parameters['statusColors'] = $customStatusColorsJson;
1941
      }
1942
1943
      $distribution_info_dto = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION_DISTRIBUTION_INFO_FOR, $taxon->uuid, queryString($query_parameters));
1944
      // 4. get distribution TextData is there are any
1945
      $distribution_text_data = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON,
1946
        array(
1947
          'taxon' => $taxon->uuid,
1948
          'type' => 'TextData',
1949
          'features' => UUID_DISTRIBUTION
1950
        )
1951
      );
1952
1953
      // 5. put all distribution data into the distribution feature node
1954
      if ($distribution_text_data //if text data exists
1955
        || ($distribution_info_dto && isset($distribution_info_dto->tree) && $distribution_info_dto->tree->rootElement->numberOfChildren > 0) // OR if tree element has distribution elements
1956
        || ($distribution_info_dto && !empty($distribution_info_dto->elements))
1957
      ) { // OR if DTO has distribution elements
1958
        $distribution_node->descriptionElements = array('#type' => 'DTO');
1959
        if ($distribution_text_data) {
1960
          $distribution_node->descriptionElements['TextData'] = $distribution_text_data;
1961
        }
1962
        if ($distribution_info_dto) {
1963
          $distribution_node->descriptionElements['DistributionInfoDTO'] = $distribution_info_dto;
1964
        }
1965
      }
1966
    }
1967
1968
    // allows modifying the merged tree via a the hook_cdm_feature_node_block_content_alter
1969
    drupal_alter('merged_taxon_feature_tree', $taxon, $merged_tree);
1970
1971
    return $merged_tree;
1972
  }
1973
1974 29857cb0 Andreas Kohlbecker
  /**
1975
   * @param $distribution_tree
1976
   *  A tree cdm TreeNode items. A TreeNode item has a NamedArea as nodeId
1977
   *  and Distribution items as data array. Per data array some Distributions may
1978
   *  be with status information, others only with sources, others with both.
1979
   *  Each node may also have subordinate node items in the children field.
1980
   *  TreeNode:
1981
   *   - array data
1982
   *   - array children
1983
   *   - int numberOfChildren
1984
   *   - stdClass nodeId
1985
   *
1986
   * @param $feature_block_settings
1987
   *
1988
   * @return array
1989
   * @throws \Exception
1990
   */
1991 65345976 Andreas Kohlbecker
  function compose_distribution_hierarchy($distribution_tree, $feature_block_settings){
1992
1993
    static $hierarchy_style;
1994 38dd933d Andreas Kohlbecker
    // TODO expose $hierarchy_style to administration or provide a hook
1995 65345976 Andreas Kohlbecker
    if( !isset($hierarchy_style)){
1996 38dd933d Andreas Kohlbecker
      $hierarchy_style = get_array_variable_merged(DISTRIBUTION_HIERARCHY_STYLE, DISTRIBUTION_HIERARCHY_STYLE_DEFAULT);
1997 65345976 Andreas Kohlbecker
    }
1998
1999
    $render_array = array();
2000
2001
    RenderHints::pushToRenderStack('descriptionElementDistribution');
2002
    RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
2003
2004
    // Returning NULL if there are no description elements.
2005
    if ($distribution_tree == null) {
2006
      return $render_array;
2007
    }
2008
    // for now we are not using a render array internally to avoid performance problems
2009
    $markup = '';
2010
    if (isset($distribution_tree->rootElement->children)) {
2011
      $tree_nodes = $distribution_tree->rootElement->children;
2012
      _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, $markup, $hierarchy_style);
2013
    }
2014
2015
    $render_array['distribution_hierarchy'] = markup_to_render_array(
2016
      $markup,
2017
      0,
2018
      '<div id="distribution_hierarchy" class="distribution_hierarchy">',
2019
      '</div>'
2020
    );
2021
2022 71acbd65 Andreas Kohlbecker
    RenderHints::popFromRenderStack();
2023
2024 65345976 Andreas Kohlbecker
    return $render_array;
2025
  }
2026
2027 29857cb0 Andreas Kohlbecker
/**
2028
 * this function should produce markup as the
2029
 * compose_description_elements_distribution() function.
2030
 *
2031
 * @param array $tree_nodes
2032
 *  An array of cdm TreeNode items. A TreeNode item has a NamedArea as nodeId
2033
 *  and Distribution items as data array. Per data array some Distributions may
2034
 *  be with status information, others only with sources, others with both.
2035
 *  TreeNode:
2036
 *   - array data
2037
 *   - array children
2038
 *   - int numberOfChildren
2039
 *   - stdClass nodeId
2040
 * @param array $feature_block_settings
2041
 * @param $markup
2042
 * @param $hierarchy_style
2043
 * @param int $level_index
2044
 *
2045
 * @throws \Exception
2046
 *
2047
 * @see compose_description_elements_distribution()
2048
 * @see compose_distribution_hierarchy()
2049
 *
2050
 */
2051
  function _compose_distribution_hierarchy(array $tree_nodes, array $feature_block_settings, &$markup, $hierarchy_style, $level_index = -1){
2052 65345976 Andreas Kohlbecker
2053
    $level_index++;
2054
    static $enclosingTag = "span";
2055
2056 38dd933d Andreas Kohlbecker
    $level_style = array_shift($hierarchy_style);
2057 330d08aa Andreas Kohlbecker
    if(count($hierarchy_style) == 0){
2058
      // lowest defined level style will be reused for all following levels
2059
      $hierarchy_style[] = $level_style;
2060
    }
2061 65345976 Andreas Kohlbecker
2062
    $node_index = -1;
2063
    $per_node_markup = array();
2064 29857cb0 Andreas Kohlbecker
2065 65345976 Andreas Kohlbecker
    foreach ($tree_nodes as $node){
2066
2067
      $per_node_markup[++$node_index] = '';
2068
      $label = $node->nodeId->representation_L10n;
2069
2070
      $distributions = $node->data;
2071
      $distribution_uuids = array();
2072
      $distribution_aggregate = NULL;
2073 29857cb0 Andreas Kohlbecker
      $status = ['label' => '', 'markup' => ''];
2074
2075
      foreach($distributions as $distribution){
2076
        $distribution_uuids[] = $distribution->uuid;
2077
        // if there is more than one distribution we aggregate the sources and
2078
        // annotations into a synthetic distribution so that the footnote keys
2079
        // can be rendered consistently
2080
        if(!$distribution_aggregate) {
2081
          $distribution_aggregate = $distribution;
2082
          if(isset($distribution->status)){
2083
            $distribution_aggregate->status = [$distribution->status];
2084 65345976 Andreas Kohlbecker
          } else {
2085 29857cb0 Andreas Kohlbecker
            $distribution_aggregate->status = [];
2086
          }
2087
          if(!isset($distribution_aggregate->sources[0])){
2088
            $distribution_aggregate->sources = array();
2089
          }
2090
          if(!isset($distribution_aggregate->annotations[0])){
2091
            $distribution_aggregate->annotations = array();
2092
          }
2093
        } else {
2094
          if(isset($distribution->status)){
2095
            $distribution_aggregate->status[] = $distribution->status;
2096
          }
2097
          if(isset($distribution->sources[0])) {
2098
            $distribution_aggregate->sources = array_merge($distribution_aggregate->sources,
2099
              $distribution->sources);
2100
          }
2101
          if(isset($distribution->annotations[0])) {
2102
            $distribution_aggregate->annotations = array_merge($distribution_aggregate->annotations,
2103
              $distribution->annotations);
2104 65345976 Andreas Kohlbecker
          }
2105
        }
2106 29857cb0 Andreas Kohlbecker
      }
2107 65345976 Andreas Kohlbecker
2108 9e2aa1ff Andreas Kohlbecker
      $annotations_and_sources =  null;
2109 65345976 Andreas Kohlbecker
      if($distribution_aggregate) {
2110
        $annotations_and_sources = handle_annotations_and_sources(
2111
          $distribution_aggregate,
2112 0c2b9b9d Andreas Kohlbecker
          handle_annotations_and_sources_config($feature_block_settings),
2113 65345976 Andreas Kohlbecker
          $label,
2114
          UUID_DISTRIBUTION
2115
        );
2116
2117 29857cb0 Andreas Kohlbecker
        $status = distribution_status_label_and_markup($distribution_aggregate->status, $level_style['status_glue']);
2118
      }
2119 65345976 Andreas Kohlbecker
2120
      $per_node_markup[$node_index] .= '<' . $enclosingTag . ' class="descriptionElement'
2121
        . join(' descriptionElement-', $distribution_uuids)
2122
        . ' level_index_' . $level_index
2123 c797d7db Andreas Kohlbecker
        . ' " title="' . $status['label'] . '">'
2124 65345976 Andreas Kohlbecker
        . '<span class="area_label">' . $label
2125 38dd933d Andreas Kohlbecker
        . $level_style['label_suffix'] . '</span>'
2126 c797d7db Andreas Kohlbecker
        . $status['markup']
2127 65345976 Andreas Kohlbecker
      ;
2128
2129 97ac3d38 Andreas Kohlbecker
      if(isset($annotations_and_sources)){
2130 65345976 Andreas Kohlbecker
        if(!empty($annotations_and_sources['source_references'])){
2131 97ac3d38 Andreas Kohlbecker
          $per_node_markup[$node_index] .= ' ' . join(', ' , $annotations_and_sources['source_references']);
2132 65345976 Andreas Kohlbecker
        }
2133
        if($annotations_and_sources['foot_note_keys']) {
2134
          $per_node_markup[$node_index] .= $annotations_and_sources['foot_note_keys'];
2135
        }
2136
      }
2137
2138
      if(isset($node->children[0])){
2139
        _compose_distribution_hierarchy(
2140
          $node->children,
2141
          $feature_block_settings,
2142
          $per_node_markup[$node_index],
2143
          $hierarchy_style,
2144
          $level_index
2145
        );
2146
      }
2147
2148
      $per_node_markup[$node_index] .= '</' . $enclosingTag . '>';
2149
    }
2150 38dd933d Andreas Kohlbecker
    $markup .= $level_style['item_group_prefix']  . join( $level_style['item_glue'], $per_node_markup) . $level_style['item_group_postfix'];
2151 65345976 Andreas Kohlbecker
  }
2152
2153
2154 4295d33d Andreas Kohlbecker
/**
2155
 * Provides the content for a block of Uses Descriptions for a given taxon.
2156
 *
2157
 * Fetches the list of TaxonDescriptions tagged with the MARKERTYPE_USE
2158
 * and passes them to the theme function theme_cdm_UseDescription().
2159
 *
2160
 * @param string $taxon_uuid
2161
 *   The uuid of the Taxon
2162
 *
2163
 * @return array
2164
 *   A drupal render array
2165
 */
2166 1b756c5f Andreas Kohlbecker
function cdm_block_use_description_content($taxon_uuid, $feature) {
2167 4295d33d Andreas Kohlbecker
2168 1b756c5f Andreas Kohlbecker
  $use_description_content = array();
2169 4295d33d Andreas Kohlbecker
2170
  if (is_uuid($taxon_uuid )) {
2171
    $markerTypes = array();
2172
    $markerTypes['markerTypes'] = UUID_MARKERTYPE_USE;
2173
    $useDescriptions = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXON . '/' . $taxon_uuid . '/descriptions', $markerTypes);
2174
    if (!empty($useDescriptions)) {
2175 1b756c5f Andreas Kohlbecker
      $use_description_content = compose_feature_block_items_use_records($useDescriptions, $taxon_uuid, $feature);
2176 4295d33d Andreas Kohlbecker
    }
2177
  }
2178
2179 1b756c5f Andreas Kohlbecker
  return $use_description_content;
2180
}
2181
2182
/**
2183
 * Creates a trunk of a feature object which can be used to build pseudo feature blocks like the Bibliography.
2184
 *
2185
 * @param $representation_L10n
2186
 * @param String $pseudo_feature_key
2187
 *    Will be set as uuid but should be one of 'BIBLIOGRAPHY', ... more to come. See also get_feature_block_settings()
2188
 *
2189
 * @return object
2190
 *  The feature object
2191
 */
2192
function make_pseudo_feature($representation_L10n, $pseudo_feature_key = null){
2193
  $feature = new stdClass;
2194
  $feature->representation_L10n = $representation_L10n;
2195 d43a3486 Andreas Kohlbecker
  $feature->uuid = NULL; // $pseudo_feature_key;
2196
  $feature->label = $pseudo_feature_key;
2197 9e7ae433 Andreas Kohlbecker
  $feature->class = 'PseudoFeature';
2198 1b756c5f Andreas Kohlbecker
2199
  return $feature;
2200
2201 4295d33d Andreas Kohlbecker
}
2202
2203 9b434f6a Andreas Kohlbecker
/**
2204 2527a83c Andreas Kohlbecker
 * @param $root_nodes, for obtaining the  root nodes from a description you can
2205
 * use the function get_root_nodes_for_dataset($description);
2206 9b434f6a Andreas Kohlbecker
 *
2207
 * @return string
2208
 */
2209 2527a83c Andreas Kohlbecker
function render_description_string($root_nodes, &$item_cnt = 0) {
2210 caf1ed73 Andreas Kohlbecker
2211
  $out = '';
2212
2213
  $description_strings= [];
2214 9b434f6a Andreas Kohlbecker
  if (!empty($root_nodes)) {
2215
    foreach ($root_nodes as $root_node) {
2216 06d7ad52 Patrick Plitzner
      if(isset($root_node->descriptionElements)) {
2217
        foreach ($root_node->descriptionElements as $element) {
2218 2bc2b13e Andreas Kohlbecker
          $feature_label = $element->feature->representation_L10n;
2219
          if($item_cnt == 0){
2220
            $feature_label = ucfirst($feature_label);
2221
          }
2222 06d7ad52 Patrick Plitzner
          switch ($element->class) {
2223
            case 'CategoricalData':
2224 f63479ae Andreas Kohlbecker
              $state_data = render_state_data($element);
2225 06d7ad52 Patrick Plitzner
              if (!empty($state_data)) {
2226 834f548a Andreas Kohlbecker
                if(is_suppress_state_present_display($element, $root_node)){
2227
                  $description_strings[] = '<span class="'. html_class_attribute_ref($element) .'"><span class="label">' . $feature_label . '</span>: '  . '</span>';
2228
                } else {
2229
                  $description_strings[] = '<span class="'. html_class_attribute_ref($element) .'"><span class="label">' . $feature_label . '</span>: ' . $state_data . '</span>;' ;
2230
                }
2231 06d7ad52 Patrick Plitzner
              }
2232
              break;
2233
            case 'QuantitativeData':
2234 834f548a Andreas Kohlbecker
              $description_strings[] = '<span class="'. html_class_attribute_ref($element) .'"><span class="label">' . $feature_label . '</span>: ' . render_quantitative_statistics($element) . '</span>;';
2235 06d7ad52 Patrick Plitzner
              break;
2236
          }
2237 9b434f6a Andreas Kohlbecker
        }
2238 2bc2b13e Andreas Kohlbecker
        $item_cnt++;
2239 9b434f6a Andreas Kohlbecker
      }
2240 caf1ed73 Andreas Kohlbecker
2241 9b434f6a Andreas Kohlbecker
      // recurse child nodes
2242 2527a83c Andreas Kohlbecker
      $child_markup = render_description_string($root_node->childNodes, $item_cnt);
2243 caf1ed73 Andreas Kohlbecker
      if($child_markup){
2244 16ab19f1 Andreas Kohlbecker
        $description_strings[] = $child_markup;
2245 caf1ed73 Andreas Kohlbecker
      }
2246 9b434f6a Andreas Kohlbecker
    }
2247 834f548a Andreas Kohlbecker
    if(count($description_strings) > 0){
2248
      // remove last semicolon
2249
      $description_strings[count($description_strings) - 1] = preg_replace('/;$/', '', $description_strings[count($description_strings) - 1]);
2250
    }
2251
    $out  = join($description_strings,  ' ');
2252 9b434f6a Andreas Kohlbecker
  }
2253 caf1ed73 Andreas Kohlbecker
  return $out;
2254 9b434f6a Andreas Kohlbecker
}
2255
2256 c7d8a579 Andreas Kohlbecker
/**
2257
 * Compose a description as a table of Feature<->State
2258
 *
2259
 * @param $description_uuid
2260
 *
2261
 * @return array
2262
 *    The drupal render array for the page
2263
 *
2264
 * @ingroup compose
2265
 */
2266 20bfe15b Andreas Kohlbecker
function  compose_description_table($description_uuid, $descriptive_dataset_uuid = NULL) {
2267
2268 c7d8a579 Andreas Kohlbecker
  RenderHints::pushToRenderStack('description_table');
2269
2270
  $render_array = [];
2271
2272
  $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, [$description_uuid]);
2273
  $dataSet = NULL;
2274
  // dataset passed as parameter
2275
  if ($descriptive_dataset_uuid != NULL) {
2276
    foreach ($description->descriptiveDataSets as $set) {
2277
      if ($set->uuid == $descriptive_dataset_uuid) {
2278
        $dataSet = $set;
2279
        break;
2280
      }
2281
    }
2282
  }
2283 20bfe15b Andreas Kohlbecker
2284 c7d8a579 Andreas Kohlbecker
  if(!empty($description->descriptiveDataSets)) {
2285
    // only one dataset present
2286
    if (!isset($dataSet) && sizeof($description->descriptiveDataSets) == 1) {
2287
      foreach ($description->descriptiveDataSets as $dataSet) {
2288
        break;
2289
      }
2290
    }
2291 20bfe15b Andreas Kohlbecker
2292
    // generate description title
2293
    RenderHints::pushToRenderStack('title');
2294 c7d8a579 Andreas Kohlbecker
    if (isset($dataSet)) {
2295 20bfe15b Andreas Kohlbecker
2296
      $described_entity_title = NULL;
2297
      if(isset($description->describedSpecimenOrObservation)){
2298
        $described_entity_title = $description->describedSpecimenOrObservation->titleCache;
2299
      } else if($description->taxon) {
2300
          $described_entity_title = render_taxon_or_name($description->taxon);
2301
      }
2302
      $title = 'Descriptive Data ' . $dataSet->titleCache .
2303
        ($described_entity_title ? ' for ' . $described_entity_title : '');
2304 753a8083 Andreas Kohlbecker
    }
2305
    $render_array['title'] = markup_to_render_array($title, null, '<h3 class="title">', '</h3>');
2306 20bfe15b Andreas Kohlbecker
    RenderHints::popFromRenderStack();
2307 753a8083 Andreas Kohlbecker
    // END of --- generate description title
2308 20bfe15b Andreas Kohlbecker
2309 753a8083 Andreas Kohlbecker
    if (isset($description->types)) {
2310
      foreach ($description->types as $type) {
2311
        if ($type == 'CLONE_FOR_SOURCE') {
2312
          $render_array['source'] = markup_to_render_array("Aggregation source from " . $description->created, null, '<div class="date-created">', '</div>');
2313
          break;
2314 c7d8a579 Andreas Kohlbecker
        }
2315
      }
2316
    }
2317 753a8083 Andreas Kohlbecker
  }
2318
  // multiple datasets present see #8714 "Show multiple datasets per description as list of links"
2319
  else {
2320
    $items = [];
2321
    foreach ($description->descriptiveDataSets as $dataSet) {
2322
      $path = path_to_description($description->uuid, $dataSet->uuid);
2323
      $attributes['class'][] = html_class_attribute_ref($description);
2324
      $items[] = [
2325
        'data' => $dataSet->titleCache . icon_link($path),
2326 c7d8a579 Andreas Kohlbecker
      ];
2327
    }
2328 753a8083 Andreas Kohlbecker
    $render_array['description_elements'] = [
2329
      '#title' => 'Available data sets for description',
2330
      '#theme' => 'item_list',
2331
      '#type' => 'ul',
2332
      '#items' => $items,
2333
    ];
2334
  }
2335 c7d8a579 Andreas Kohlbecker
2336 3db61e83 Andreas Kohlbecker
  $described_entities = [];
2337 c7d8a579 Andreas Kohlbecker
  if (isset($description->describedSpecimenOrObservation)) {
2338 3db61e83 Andreas Kohlbecker
    $decr_entitiy = '<span class="label">Specimen:</span> ' . render_cdm_specimen_link($description->describedSpecimenOrObservation);
2339
    $described_entities['specimen'] = markup_to_render_array($decr_entitiy, null, '<div>', '</div>');
2340 c7d8a579 Andreas Kohlbecker
  }
2341
  if (isset($description->taxon)) {
2342 3db61e83 Andreas Kohlbecker
    $decr_entitiy = '<span class="label">Taxon:</span> ' . render_taxon_or_name($description->taxon, url(path_to_taxon($description->taxon->uuid)));
2343
    $described_entities['taxon'] = markup_to_render_array($decr_entitiy, null, '<div>', '</div>');
2344
  }
2345
2346
  if(count($described_entities)){
2347
    $render_array['described_entities'] = $described_entities;
2348
    $render_array['described_entities']['#prefix'] = '<div class="described-entities">';
2349
    $render_array['described_entities']['#suffix'] = '</div>';
2350 c7d8a579 Andreas Kohlbecker
  }
2351
2352 3db61e83 Andreas Kohlbecker
2353 c7d8a579 Andreas Kohlbecker
  $root_nodes = get_root_nodes_for_dataset($description);
2354
2355
2356
  $rows = [];
2357 209feb13 Andreas Kohlbecker
  $rows = description_element_table_rows($root_nodes, $rows);
2358 c7d8a579 Andreas Kohlbecker
2359 0e414cc1 Andreas Kohlbecker
  // --- create headers
2360 dfd71699 Andreas Kohlbecker
  $header = [0 => [], 1 => []];
2361 0e414cc1 Andreas Kohlbecker
2362
  foreach($rows as $row) {
2363 dfd71699 Andreas Kohlbecker
    if(array_search('Character', $row['class']) && array_search('Character', $header[0]) === false){
2364
      $header[0][] = 'Character';
2365
    } elseif (array_search('Feature', $row['class']) && array_search('Feature', $header[0]) === false){
2366
      $header[0][] = 'Feature';
2367 0e414cc1 Andreas Kohlbecker
    }
2368 dfd71699 Andreas Kohlbecker
    if(array_search('has_state', $row['class']) && array_search('States', $header[1]) === false){
2369
      $header[1][] = 'States';
2370
    } elseif (array_search('has_values', $row['class']) && array_search('Values', $header[1]) === false){
2371
      $header[1][] = 'Values';
2372 0e414cc1 Andreas Kohlbecker
    }
2373
  }
2374 dfd71699 Andreas Kohlbecker
  asort($header[0]);
2375 0e414cc1 Andreas Kohlbecker
  asort($header[1]);
2376 dfd71699 Andreas Kohlbecker
  $header[0] = join('/', $header[0]);
2377 0e414cc1 Andreas Kohlbecker
  $header[1] = join('/', $header[1]);
2378
2379
  // ---
2380
2381 c7d8a579 Andreas Kohlbecker
  if (!empty($rows)) {
2382 3db61e83 Andreas Kohlbecker
    $render_array['table'] = markup_to_render_array(theme('table', [
2383 c7d8a579 Andreas Kohlbecker
      'header' => $header,
2384
      'rows' => $rows,
2385 cd3ea2e8 Andreas Kohlbecker
      'caption' => statistical_values_explanation(),
2386 6722d9a5 Andreas Kohlbecker
      'title' => "Table"
2387 c7d8a579 Andreas Kohlbecker
    ]));
2388
  }
2389
2390 753a8083 Andreas Kohlbecker
  // --- sources
2391 c7d8a579 Andreas Kohlbecker
  if (isset($description->sources) and !empty($description->sources)) {
2392
    $items = [];
2393
    foreach ($description->sources as $source) {
2394
      if ($source->type == 'Aggregation' and isset($source->cdmSource)){
2395 fec3fd41 Andreas Kohlbecker
        $cdm_source_entity = $source->cdmSource;
2396
        switch($cdm_source_entity->class){
2397
          case 'Taxon':
2398 cbe9b085 Andreas Kohlbecker
            $source_link_markup = render_taxon_or_name($cdm_source_entity) . icon_link(path_to_taxon($cdm_source_entity->uuid, false));
2399 fec3fd41 Andreas Kohlbecker
            break;
2400
          case 'TaxonDescription':
2401
          case 'NameDescription':
2402 3db61e83 Andreas Kohlbecker
          case 'SpecimenDescription':
2403 92e522db Andreas Kohlbecker
            $source_link_markup = render_cdm_entity_link($cdm_source_entity);
2404 fec3fd41 Andreas Kohlbecker
            break;
2405
          default:
2406 2d78d276 Andreas Kohlbecker
            $source_link_markup = '<span class="error">Unhandled CdmSource</span>';
2407 fec3fd41 Andreas Kohlbecker
        }
2408 92e522db Andreas Kohlbecker
        $items[$cdm_source_entity->titleCache] = [
2409 fec3fd41 Andreas Kohlbecker
          'data' => $source_link_markup
2410 c7d8a579 Andreas Kohlbecker
        ];
2411
      }
2412
    }
2413 92e522db Andreas Kohlbecker
    ksort($items);
2414 3db61e83 Andreas Kohlbecker
    $render_array['sources'] = [
2415 c7d8a579 Andreas Kohlbecker
      '#title' => 'Sources',
2416
      '#theme' => 'item_list',
2417
      '#type' => 'ul',
2418
      '#items' => $items,
2419 3db61e83 Andreas Kohlbecker
      '#attributes' => ['class' => 'sources']
2420 c7d8a579 Andreas Kohlbecker
    ];
2421 3db61e83 Andreas Kohlbecker
    $render_array['#prefix'] = '<div class="description-table">';
2422
    $render_array['#suffix'] = '</div>';
2423 c7d8a579 Andreas Kohlbecker
  }
2424
2425
  RenderHints::popFromRenderStack();
2426 20bfe15b Andreas Kohlbecker
2427 c7d8a579 Andreas Kohlbecker
  return $render_array;
2428
}
2429
2430
/**
2431
 * For a given description returns the root nodes according to the
2432
 *corresponding term tree. The term tree is determined as follow:
2433
 * 1. If description is part of a descriptive data set the term tree of that
2434
 *    data set is used (FIXME handle multiple term trees)
2435
 * 2. Otherwise the portal taxon profile tree is used
2436
 * @param $description
2437
 *
2438
 * @return array
2439
 */
2440
function get_root_nodes_for_dataset($description) {
2441
  if (!empty($description->descriptiveDataSets)) {
2442
    foreach ($description->descriptiveDataSets as $dataSet) {
2443
      break;// FIXME handle multiple term trees
2444
    }
2445
    $tree = cdm_ws_get(CDM_WS_TERMTREE, $dataSet->descriptiveSystem->uuid);
2446
    $root_nodes = _mergeFeatureTreeDescriptions($tree->root->childNodes, $description->elements);
2447
  }
2448
  else {
2449
    $root_nodes = _mergeFeatureTreeDescriptions(get_profile_feature_tree()->root->childNodes, $description->elements);
2450
  }
2451
  return $root_nodes;
2452
}
2453
2454
/**
2455 0e414cc1 Andreas Kohlbecker
 * Recursively creates an array of row items to be used in theme_table.
2456
 *
2457
 * The array items will have am element 'class' with information on the
2458
 * nature of the DescriptionElement ('has_values' | 'has_state') and on the
2459
 * type of the FeatureNode ('Feature' | 'Character')
2460 209feb13 Andreas Kohlbecker
 *
2461
 * @param array $root_nodes
2462
 * @param array $row_items
2463
 * @param int $level
2464
 *     the depth in the hierarchy
2465 c7d8a579 Andreas Kohlbecker
 *
2466
 * @return array
2467 209feb13 Andreas Kohlbecker
 *  An array of row items to be used in theme_table
2468
 *
2469
 *
2470 c7d8a579 Andreas Kohlbecker
 */
2471 209feb13 Andreas Kohlbecker
function description_element_table_rows($root_nodes, $row_items, $level = 0) {
2472 0e414cc1 Andreas Kohlbecker
2473 209feb13 Andreas Kohlbecker
  $indent_string = '&nbsp;&nbsp;&nbsp;';
2474 c7d8a579 Andreas Kohlbecker
  foreach ($root_nodes as $root_node) {
2475
    if(isset($root_node->descriptionElements)) {
2476
      foreach ($root_node->descriptionElements as $element) {
2477 209feb13 Andreas Kohlbecker
        $level_indent = str_pad('', $level * strlen($indent_string), $indent_string);
2478 c7d8a579 Andreas Kohlbecker
        switch ($element->class) {
2479 0e414cc1 Andreas Kohlbecker
          case 'QuantitativeData':
2480
            $row_items[] = [
2481
              'data' => [
2482
                [
2483
                  'data' => markup_to_render_array($level_indent . $element->feature->representation_L10n),
2484
                  'class' => ['level_' . $level]
2485
                ],
2486
                render_quantitative_statistics($element)
2487
              ],
2488
              'class' => ['level_' . $level, 'has_values', $element->feature->class]
2489
            ];
2490
            break;
2491 c7d8a579 Andreas Kohlbecker
          case 'CategoricalData':
2492 0e414cc1 Andreas Kohlbecker
            default:
2493 c7d8a579 Andreas Kohlbecker
            if (!empty($element->stateData)) {
2494 834f548a Andreas Kohlbecker
              $supress_state_display = is_suppress_state_present_display($element, $root_node);
2495 358e0b7f Andreas Kohlbecker
              if(!$supress_state_display){
2496
                $state_cell = render_state_data($element);
2497
              } else {
2498
                $state_cell = "<span> </span>";
2499
              }
2500 209feb13 Andreas Kohlbecker
              $row_items[] = [
2501
                'data' => [
2502
                  [
2503
                    'data' => markup_to_render_array($level_indent . $element->feature->representation_L10n),
2504 88e622d2 Andreas Kohlbecker
                    'class' => ['level_' . $level]
2505 209feb13 Andreas Kohlbecker
                  ],
2506 358e0b7f Andreas Kohlbecker
                  $state_cell,
2507 209feb13 Andreas Kohlbecker
                ],
2508 0e414cc1 Andreas Kohlbecker
                'class' => ['level_' . $level, 'has_state', $element->feature->class]
2509 c7d8a579 Andreas Kohlbecker
              ];
2510
            }
2511
            break;
2512
        }
2513
      }
2514
    }
2515
    // recurse child nodes
2516 209feb13 Andreas Kohlbecker
    $row_items = description_element_table_rows($root_node->childNodes, $row_items, $level + 1);
2517 c7d8a579 Andreas Kohlbecker
  }
2518 209feb13 Andreas Kohlbecker
  return $row_items;
2519 c7d8a579 Andreas Kohlbecker
}
2520
2521 834f548a Andreas Kohlbecker
/**
2522
 * @param $element
2523
 * @param $root_node
2524
 *
2525
 * @return bool
2526
 */
2527
function is_suppress_state_present_display($element, $root_node) {
2528
  return count($element->stateData) == 1 & $element->stateData[0]->state->representation_L10n == 'present' && is_array($root_node->childNodes);
2529
}
2530
2531 c7d8a579 Andreas Kohlbecker