Project

General

Profile

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