Project

General

Profile

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