Project

General

Profile

Download (86.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
const BIBLIOGRAPHY_FOOTNOTE_KEY = PSEUDO_FEATURE_BIBLIOGRAPHY;
4

    
5

    
6
/**
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
    }
24
  }
25
  return implode(', ', $modifiers_strings);
26
}
27

    
28
/**
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

    
47
  $computed_elements = array();
48
  $other_elements = array();
49

    
50
  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
      }
58
    }
59
  }
60

    
61
  if (count($computed_elements) > 0) {
62
    return $computed_elements;
63
  }
64
  else {
65
    return $other_elements;
66
  }
67
}
68

    
69
/**
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

    
79
  $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

    
84
  $query['recipe'] = variable_get(DISTRIBUTION_CONDENSED_RECIPE, DISTRIBUTION_CONDENSED_RECIPE_DEFAULT);
85

    
86

    
87

    
88
  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
  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
      if ($enabled) {
98
        $query['hiddenAreaMarkerType'] .= ($query['hiddenAreaMarkerType'] ? ',' : '') . $marker_type;
99
      }
100
    }
101
  }
102

    
103
  return $query;
104
}
105

    
106
/**
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
      }
125
    }
126
  }
127
}
128

    
129
/**
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
 * @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
 */
149
function cdm_toc_list_add_item($label, $class_attribute_suffix, $fragment = NULL, $as_first_element = FALSE, $weight = null) {
150

    
151
  $toc_list_items = &cdm_toc_list();
152

    
153
  if (!$fragment) {
154
    $fragment = $label;
155
  }
156
  $fragment = generalizeString($fragment);
157

    
158
  $class_attributes = 'feature-toc-item-' . $class_attribute_suffix;
159

    
160
  $new_item = toc_list_item(
161
    theme(
162
      'cdm_feature_name',
163
      array('feature_name' => $label)),
164
      array('class' => $class_attributes),
165
      $fragment
166
    );
167

    
168
  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
  }
173
  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
  }
178
  $toc_list_items[$weight] = $new_item;
179
  ksort($toc_list_items); // sort so that the last element has always the highest weight
180

    
181
}
182

    
183
/**
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

    
197
/**
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
 * 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
 *
204
 * @param $feature_name
205
 *   A label describing the feature, usually the localized feature representation.
206
 * @param object $feature
207
 *   The CDM Feature for which the block is created.
208
 * @return object
209
 *   A Drupal block object
210
 */
211
function feature_block($feature_name, $feature) {
212
  $block = new stdclass();
213
  $block->module = 'cdm_dataportal';
214
  $block->region = NULL;
215
  $class_attribute = html_class_attribute_ref($feature);
216
  $block_delta = $feature->class !== 'PseudoFeature' ? $feature_name : $feature->label;
217
  $block->delta = generalizeString($block_delta);
218
  $block->subject = '<a name="' . $block->delta . '"></a><span class="' . $class_attribute . '">'
219
    . 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

    
227
/**
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

    
258
    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
      )
319
    ),
320
  );
321
  $item['attributes'] = $attributes;
322
  return $item;
323
}
324

    
325
/**
326
 * Creates the footnotes for the given CDM instance.
327
 *
328
 * 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
 *
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
 * @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
 * @$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
    $footnote_list_key_suggestion = null,
361
    $do_link_to_reference = FALSE,
362
    $do_link_to_name_used_in_source = FALSE,
363
    $is_bibliography_aware = FALSE
364
  ){
365

    
366
  // Annotations as footnotes.
367
  $footNoteKeys = cdm_annotations_as_footnotekeys($description_element, $footnote_list_key_suggestion);
368

    
369
  // Source references as footnotes.
370

    
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
  usort($description_element->sources, 'compare_original_sources');
381
  foreach ($description_element->sources as $source) {
382
    if (_is_original_source_type($source)) {
383
      $fn_key = FootnoteManager::addNewFootnote(
384
        $sources_footnote_list_key,
385
        render_original_source(
386
          $source,
387
          $do_link_to_reference,
388
          $do_link_to_name_used_in_source
389
        ),
390
        $original_source_footnote_tag
391
      );
392
      // Ensure uniqueness of the footnote keys.
393
      cdm_add_footnote_to_array($footNoteKeys, $fn_key);
394
    }
395
  }
396
  // Sort and render footnote keys.
397
  $footnoteKeyListStr = '';
398
  asort($footNoteKeys);
399
  foreach ($footNoteKeys as $footNoteKey) {
400
    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
  }
409
  return $footnoteKeyListStr;
410
}
411

    
412

    
413
/**
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

    
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
function compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label = FALSE)
436
{
437

    
438
  $render_array = array(
439
    '#type' => 'html_tag',
440
    '#tag' => cdm_feature_block_element_tag_name($feature_block_settings),
441

    
442
    '#attributes' => array(
443
      'class' => array(
444
        'DescriptionElement',
445
        'DescriptionElement-' . $element->class,
446
        html_class_attribute_ref($element)
447
      )
448
    ),
449

    
450
    '#value' => '',
451
    '#value_suffix' => NULL
452

    
453
  );
454

    
455
  $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

    
462
  $timescope_markup = '';
463
  if(isset($element->timeperiod)){
464
    $timescope_markup = ' ' . timePeriodToString($element->timeperiod, true);
465
  }
466

    
467
  // 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
  $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
    // 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
  }
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
  $feature_label = '';
486
  if ($prepend_feature_label) {
487
    $feature_label = '<span class="nested-feature-tree-feature-label">' . $element->feature->representation_L10n . ':</span> ';
488
  }
489
  $content_markup = $names_used_in_source_markup . $element_markup . $timescope_markup . $source_references_markup;
490

    
491
  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
    // 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
  return $render_array;
500
}
501

    
502
/**
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
  $config['add_footnote_keys'] = 0;
518
  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

    
526
  /**
527
   * @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
   * @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
   *
545
   * @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
   */
553
  function handle_annotations_and_sources($entity, $config, $inline_text_prefix, $footnote_list_key_suggestion) {
554

    
555
    $annotations_and_sources = array(
556
      'foot_note_keys' => NULL,
557
      'source_references' => array(),
558
      'names_used_in_source' => array()
559
    );
560

    
561
    // some entity types only have single sources:
562
    if(isset($entity->source) && is_object($entity->source)){
563
      $sources = [$entity->source];
564
    } else if(isset($entity->sources)) {
565
      $sources = $entity->sources;
566
    } else {
567
      $sources = [];
568
    }
569
    usort($sources, 'compare_original_sources');
570

    
571
    if ($config['sources_as_content'] == 1) {
572
      foreach ($sources as $source) {
573
        if (_is_original_source_type($source)) {
574
          $reference_citation = render_original_source(
575
            $source,
576
            $config['link_to_reference'] == 1,
577
            $config['link_to_name_used_in_source'] == 1
578
          );
579

    
580
          if ($reference_citation) {
581
            if (empty($inline_text_prefix)) {
582
              $annotations_and_sources['source_references'][] = $reference_citation;
583
            } else {
584
              $annotations_and_sources['source_references'][] = ' (' . $reference_citation . ')';
585
            }
586
          }
587

    
588
          // also put the name in source into the array, these are already included in the $reference_citation but are
589
          // still required to be available separately in some contexts.
590
          $name_in_source_render_array = compose_name_in_source(
591
            $source,
592
            $config['link_to_name_used_in_source'] == 1
593
          );
594

    
595
          if (!empty($name_in_source_render_array)) {
596
            $annotations_and_sources['names_used_in_source'][$name_in_source_render_array['#_plaintext']] = drupal_render($name_in_source_render_array);
597
          }
598
        }
599
      } // END of loop over sources
600

    
601
    // annotations footnotes separate.
602
    $annotations_and_sources['foot_note_keys'] = theme('cdm_annotations_as_footnotekeys',
603
      array(
604
        'cdmBase_list' => $entity,
605
        'footnote_list_key' => $footnote_list_key_suggestion,
606
      )
607
    );
608

    
609
    } // END of references inline
610

    
611
    // footnotes for sources and annotations or put into into bibliography if requested ...
612
    if ($config['add_footnote_keys'] == 1) {
613
        $annotations_and_sources['foot_note_keys'] = cdm_create_footnotes(
614
          $entity, ',',
615
          $footnote_list_key_suggestion,
616
          $config['link_to_reference'] == 1,
617
          $config['link_to_name_used_in_source'] == 1,
618
          !empty($config['bibliography_aware'])
619
        );
620
    }
621

    
622
    return $annotations_and_sources;
623
  }
624

    
625

    
626
  /**
627
   * This method determines the footnote key for original sources to be shown in the bibliography block
628
   *
629
   * The footnote key depends on the value of the 'enabled' value of the bibliography_settings
630
   *    - enabled == 1 -> "BIBLIOGRAPHY"
631
   *    - enabled == 0 -> "BIBLIOGRAPHY-$key_suggestion"
632
   *
633
   * @see get_bibliography_settings() and @see constant BIBLIOGRAPHY_FOOTNOTE_KEY
634
   *
635
   * @param $key_suggestion string
636
   *    optional parameter. If this parameter is left empty (null, 0, "") the footnote key will be retrieved by
637
   *    calling RenderHints::getFootnoteListKey().
638

    
639
   *
640
   * @return string
641
   *  the footnote_list_key
642
   */
643
  function original_source_footnote_list_key($key_suggestion = null) {
644
    if(!$key_suggestion){
645
      $key_suggestion = RenderHints::getFootnoteListKey();
646
    }
647
    $bibliography_settings = get_bibliography_settings();
648
    $footnote_list_key = $bibliography_settings['enabled'] == 1 ? BIBLIOGRAPHY_FOOTNOTE_KEY : BIBLIOGRAPHY_FOOTNOTE_KEY . '-' . $key_suggestion;
649
    return $footnote_list_key;
650
  }
651

    
652
  /**
653
   * Provides the according tag name for the description element markup which fits the  $feature_block_settings['as_list'] value
654
   *
655
   * @param $feature_block_settings
656
   *   A feature_block_settings array, for details, please see get_feature_block_settings($feature_uuid = 'DEFAULT')
657
   */
658
  function cdm_feature_block_element_tag_name($feature_block_settings){
659
    switch ($feature_block_settings['as_list']){
660
      case 'ul':
661
      case 'ol':
662
        return 'li';
663
      case 'div':
664
        if(isset($feature_block_settings['element_tag'])){
665
          return $feature_block_settings['element_tag'];
666
        }
667
        return 'span';
668
      case 'dl':
669
        return 'dd';
670
      default:
671
        return 'div'; // should never happen, throw error instead?
672
    }
673
  }
674

    
675

    
676
/* ==================== COMPOSE FUNCTIONS =============== */
677

    
678
  /**
679
   * Returns a set of feature blocks for a taxon profile from the $mergedFeatureNodes of a given $taxon.
680
   *
681
   * The taxon profile consists of drupal block elements, one for the description elements
682
   * of a specific feature. The structure is defined by specific FeatureTree.
683
   * The chosen FeatureTree is merged with the list of description elements prior to using this method.
684
   *
685
   * The merged nodes can be obtained by making use of the
686
   * function cdm_ws_descriptions_by_featuretree().
687
   *
688
   * @see cdm_ws_descriptions_by_featuretree()
689
   *
690
   * @param $mergedFeatureNodes
691
   *
692
   * @param $taxon
693
   *
694
   * @return array
695
   *  A Drupal render array containing feature blocks and the table of content
696
   *
697
   * @ingroup compose
698
   */
699
  function make_feature_block_list($mergedFeatureNodes, $taxon) {
700

    
701
    $block_list = array();
702

    
703
    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME);
704

    
705
    $use_description_features = array(UUID_USE);
706

    
707

    
708
    RenderHints::pushToRenderStack('feature_block');
709
    // Create a drupal block for each feature
710
    $block_weight = - FEATURE_BLOCK_WEIGHT_INCREMENT;
711
    foreach ($mergedFeatureNodes as $feature_node) {
712

    
713
      $block_weight = $block_weight + FEATURE_BLOCK_WEIGHT_INCREMENT;
714

    
715
      if ((isset($feature_node->descriptionElements['#type']) ||
716
          has_feature_node_description_elements($feature_node)) && $feature_node->term->uuid != UUID_IMAGE) { // skip empty or suppressed features
717

    
718
        RenderHints::pushToRenderStack($feature_node->term->uuid);
719
          
720
        $feature_name = cdm_term_representation($feature_node->term, 'Unnamed Feature');
721
        $feature_block_settings = get_feature_block_settings($feature_node->term->uuid);
722
        
723

    
724
        $block = feature_block($feature_name, $feature_node->term);
725
        $block->content = array();
726
        $block_content_is_empty = TRUE;
727

    
728
        if(array_search($feature_node->term->uuid, $use_description_features) !== false) {
729
          // do not show features which belong to the UseDescriptions, these are
730
          // handled by compose_feature_block_items_use_records() where the according descriptions are
731
          // fetched again separately.
732
          // UseDescriptions are a special feature introduced for palmweb
733
          continue;
734
        }
735

    
736
        /*
737
         * Content/DISTRIBUTION.
738
         */
739
        if ($feature_node->term->uuid == UUID_DISTRIBUTION) {
740
          $block = compose_feature_block_distribution($taxon, $feature_node->descriptionElements, $feature_node->term);
741
          $block_content_is_empty = FALSE;
742
        }
743

    
744
        /*
745
         * Content/COMMON_NAME.
746
         */
747
        else if ($feature_node->term->uuid == UUID_COMMON_NAME) {
748
          $common_names_render_array = compose_feature_block_items_feature_common_name($feature_node->descriptionElements, $feature_node->term);
749
          $block->content[] = $common_names_render_array;
750
          $block_content_is_empty = FALSE;
751
        }
752

    
753
        /*
754
         * Content/Use Description (Use + UseRecord)
755
         */
756
        else if ($feature_node->term->uuid == UUID_USE_RECORD) {
757
          $block->content[] = cdm_block_use_description_content($taxon->uuid, $feature_node->term);
758
          $block_content_is_empty = FALSE;
759
        }
760

    
761
        /*
762
         * Content/ALL OTHER FEATURES.
763
         */
764
        else {
765

    
766
          $media_list = array();
767
          $elements_render_array = array();
768
          $child_elements_render_array = null;
769

    
770
          if (isset($feature_node->descriptionElements[0])) {
771
            $elements_render_array = compose_feature_block_items_generic($feature_node->descriptionElements, $feature_node->term);
772
          }
773

    
774
          // Content/ALL OTHER FEATURES/Subordinate Features
775
          // subordinate features are printed inline in one floating text,
776
          // it is expected hat subordinate features can "contain" TextData,
777
          // Qualitative- and Qualitative- DescriptionElements
778
          if (isset($feature_node->childNodes[0])) {
779
            $child_elements_render_array = compose_feature_block_items_nested($feature_node, $media_list, $feature_block_settings);
780
            $elements_render_array = array_merge($elements_render_array, $child_elements_render_array);
781
          }
782
          $block_content_is_empty = $block_content_is_empty && empty($media_list) && empty($elements_render_array);
783
          if(!$block_content_is_empty){
784
            $block->content[] = compose_feature_block_wrap_elements($elements_render_array, $feature_node->term, $feature_block_settings['glue']);
785
            $block->content[] = compose_feature_media_gallery($feature_node, $media_list, $gallery_settings);
786
            /*
787
             * Footnotes for the feature block
788
             */
789
            $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => PSEUDO_FEATURE_BIBLIOGRAPHY . '-' . $feature_node->term->uuid)));
790
            $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => $feature_node->term->uuid)));
791
            $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => $feature_node->term->uuid)));
792
          }
793
        } // END all other features
794

    
795
        // allows modifying the block contents via a the hook_cdm_feature_node_block_content_alter
796
        drupal_alter('cdm_feature_node_block_content', $block->content, $feature_node->term, $feature_node->descriptionElements);
797

    
798
        if(!$block_content_is_empty){ // skip empty block content
799
          $block_list[$block_weight] = $block;
800
          cdm_toc_list_add_item(cdm_term_representation($feature_node->term), $feature_node->term->uuid, null, FALSE, $block_weight);
801
        } // END: skip empty block content
802
      } // END: skip empty or suppressed features
803
      RenderHints::popFromRenderStack();
804
    } // END: creating a block per feature
805

    
806
    RenderHints::popFromRenderStack();
807

    
808
    drupal_alter('cdm_feature_node_blocks', $block_list, $taxon);
809

    
810
    ksort($block_list);
811

    
812
    return  $block_list;
813
  }
814

    
815
/**
816
 * Creates a render array of description elements  held by child nodes of the given feature node.
817
 *
818
 * This function is called recursively!
819
 *
820
 * @param $feature_node
821
 *   The feature node.
822
 * @param array $media_list
823
 *   List of CDM Media entities. All media of subordinate elements should be passed to the main feature.ä
824
 * @param $feature_block_settings
825
 *   The feature block settings.
826
 * @param $main_feature
827
 *  Only used internally in recursive calls.
828
 *
829
 * @return array
830
 *  A Drupal render array
831
 *
832
 * @ingroup compose
833
 */
834
function compose_feature_block_items_nested($feature_node, &$media_list, $feature_block_settings, $main_feature = NULL)
835
{
836

    
837
  if(!$main_feature){
838
    $main_feature = $feature_node->term;
839
  }
840
  /*
841
   * TODO should be configurable, options; YES, NO, AUTOMATIC
842
   * (automatic will only place the label if the first word of the description element text is not the same)
843
   */
844
  $prepend_feature_label = false;
845

    
846
  $render_arrays = array();
847
  foreach ($feature_node->childNodes as $child_node) {
848
    if (isset($child_node->descriptionElements[0])) {
849
      foreach ($child_node->descriptionElements as $element) {
850

    
851
        if (isset($element->media[0])) {
852
          // Append media of subordinate elements to list of main
853
          // feature.
854
          $media_list = array_merge($media_list, $element->media);
855
        }
856

    
857
        $child_node_element = null;
858
        switch ($element->class) {
859
          case 'TextData':
860
            $child_node_element = compose_description_element_text_data($element, $element->feature->uuid, $feature_block_settings, $prepend_feature_label);
861
            break;
862
          case 'CategoricalData':
863
            $child_node_element = compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label);
864
            break;
865
          case 'QuantitativeData':
866
            $child_node_element = compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label);
867

    
868
        }
869
        if (is_array($child_node_element)) {
870
          $render_arrays[] = $child_node_element;
871
        }
872
      }
873
    }
874

    
875
    if(isset($child_node->childNodes[0])){
876
      $render_arrays = array_merge($render_arrays, compose_feature_block_items_nested($child_node, $media_list, $feature_block_settings, $main_feature ));
877
    }
878
  }
879

    
880
  return $render_arrays;
881
}
882

    
883
  /**
884
   *
885
   * @param $feature_node
886
   *  The merged feature three node which potentially contains media in its description elements.
887
   * @param $media_list
888
   *    Additional media to be merged witht the media contained in the nodes description elements
889
   * @param $gallery_settings
890
   * @return array
891
   *
892
   * @ingroup compose
893
   */
894
  function compose_feature_media_gallery($feature_node, $media_list, $gallery_settings) {
895

    
896
    if (isset($feature_node->descriptionElements)) {
897
      $media_list = array_merge($media_list, cdm_dataportal_media_from_descriptionElements($feature_node->descriptionElements));
898
    }
899

    
900
    $captionElements = array('title', 'rights');
901

    
902
    if (isset($media_list[0]) && isset($gallery_settings['cdm_dataportal_media_maxextend']) && isset($gallery_settings['cdm_dataportal_media_cols'])) {
903
      $gallery = compose_cdm_media_gallerie(array(
904
        'mediaList' => $media_list,
905
        'galleryName' => CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME . '_' . $feature_node->term->uuid,
906
        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
907
        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
908
        'captionElements' => $captionElements,
909
      ));
910
      return markup_to_render_array($gallery);
911
    }
912

    
913
    return markup_to_render_array('');
914
  }
915

    
916
  /**
917
   * Composes the distribution feature block for a taxon
918
   *
919
   * @param $taxon
920
   * @param $descriptionElements
921
   *   an associative array with two elements:
922
   *   - '#type': must be 'DTO'
923
   *   - 'DistributionInfoDTO': a CDM DistributionInfoDTO object as returned by the DistributionInfo web service
924
   * @param $feature
925
   *
926
   * @return array
927
   *  A drupal render array
928
   *
929
   * @ingroup compose
930
   */
931
  function compose_feature_block_distribution($taxon, $descriptionElements, $feature) {
932
    $text_data_glue = '';
933
    $text_data_sortOutArray = FALSE;
934
    $text_data_enclosingTag = 'ul';
935
    $text_data_out_array = array();
936

    
937
    $distributionElements = NULL;
938
    $distribution_info_dto = NULL;
939
    $distribution_sortOutArray = FALSE;
940

    
941
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
942

    
943
    // TODO use feature_block_settings instead of DISTRIBUTION_ORDER_MODE
944
    if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) != 'TREE') {
945
      $distribution_glue = '';
946
      $distribution_enclosingTag = 'dl';
947
    } else {
948
      $distribution_glue = '';
949
      $distribution_enclosingTag = 'ul';
950
    }
951

    
952
    if (!isset($descriptionElements['#type']) || !$descriptionElements['#type'] == 'DTO') {
953
      // skip the DISTRIBUTION section if there is no DTO type element
954
      return array(); // FIXME is it ok to return an empty array?
955
    }
956

    
957
    $block = feature_block(
958
      cdm_term_representation($feature, 'Unnamed Feature'),
959
      $feature
960
    );
961
    $block->content = array();
962

    
963
    // $$descriptionElements['TextData'] is added to to the feature node in merged_taxon_feature_tree()
964
    if (isset($descriptionElements['TextData'])) {
965
      // --- TextData
966
      foreach ($descriptionElements['TextData'] as $text_data_element) {
967
        $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
968
        $repr = drupal_render($text_data_render_array);
969

    
970
        if (!array_search($repr, $text_data_out_array)) { // de-duplication !!
971
          $text_data_out_array[] = $repr;
972
          // TODO HINT: sorting in compose_feature_block_wrap_elements will
973
          // not work since this array contains html attributes with uuids
974
          // and what is about cases like the bibliography where
975
          // any content can be prefixed with some foot-note anchors?
976
          $text_data_sortOutArray = TRUE;
977
          $text_data_glue = '<br/> ';
978
          $text_data_enclosingTag = 'p';
979
        }
980
      }
981
    }
982

    
983

    
984
    if ($text_data_out_array && variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
985
      $block->content[] = compose_feature_block_wrap_elements(
986
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
987
      );
988
    }
989

    
990
    // --- Distribution map
991
    $distribution_map_query_parameters = NULL;
992

    
993
    $map_visibility = variable_get(DISTRIBUTION_MAP_VISIBILITY, DISTRIBUTION_MAP_VISIBILITY_DEFAULT);
994
    if(variable_get(DISTRIBUTION_MAP_VISIBILITY, DISTRIBUTION_MAP_VISIBILITY_DEFAULT) == 'always' ||
995
        $map_visibility == 'automatic' && isset($descriptionElements['DistributionInfoDTO']->mapUriParams)
996
      )
997
    {
998
      $distribution_map_query_parameters = $descriptionElements['DistributionInfoDTO']->mapUriParams;
999
    }
1000
    $map_render_element = compose_distribution_map($distribution_map_query_parameters);
1001
    $block->content[] = $map_render_element;
1002

    
1003
    $dto_out_array = array();
1004

    
1005
    // --- Condensed Distribution
1006
    if(variable_get(DISTRIBUTION_CONDENSED) && isset($descriptionElements['DistributionInfoDTO']->condensedDistribution)){
1007
      $condensed_distribution_markup = '<p class="condensed_distribution">';
1008

    
1009
      $isFirst = true;
1010
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous[0])){
1011
        foreach($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous as $cdItem){
1012
          if(!$isFirst){
1013
            $condensed_distribution_markup .= ' ';
1014
          }
1015
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->idInVocabulary . '">'
1016
          . $cdItem->areaStatusLabel . '</span>';
1017
          $isFirst = false;
1018
        }
1019
      }
1020

    
1021
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign[0])) {
1022
        if(!$isFirst){
1023
          $condensed_distribution_markup .= ' ';
1024
        }
1025
        $isFirst = TRUE;
1026
        $condensed_distribution_markup .= '[';
1027
        foreach ($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign as $cdItem) {
1028
          if (!$isFirst) {
1029
            $condensed_distribution_markup .= ' ';
1030
          }
1031
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->titleCache . '">'
1032
            . $cdItem->areaStatusLabel . '</span>';
1033
          $isFirst = false;
1034
        }
1035
        $condensed_distribution_markup .= ']';
1036
      }
1037

    
1038
      $condensed_distribution_markup .= '&nbsp;' . l(
1039
          font_awesome_icon_markup(
1040
            'fa-info-circle',
1041
            array(
1042
              'alt'=>'help',
1043
              'class' => array('superscript')
1044
            )
1045
          ),
1046
          variable_get(DISTRIBUTION_CONDENSED_INFO_PATH, DISTRIBUTION_CONDENSED_INFO_PATH_DEFAULT) ,
1047
          array('html' => TRUE));
1048
      $condensed_distribution_markup .= '</p>';
1049
      $dto_out_array[] = $condensed_distribution_markup;
1050
    }
1051

    
1052
    // --- tree or list
1053
    if (isset($descriptionElements['DistributionInfoDTO'])) {
1054
      $distribution_info_dto = $descriptionElements['DistributionInfoDTO'];
1055

    
1056
      // --- tree
1057
      if (is_object($distribution_info_dto->tree)) {
1058
        $distribution_tree_render_array = compose_distribution_hierarchy($distribution_info_dto->tree, $feature_block_settings);
1059
        $dto_out_array[] = $distribution_tree_render_array;
1060
      }
1061

    
1062
      // --- sorted element list
1063
      if (is_array($distribution_info_dto->elements) && count($distribution_info_dto->elements) > 0) {
1064
        foreach ($distribution_info_dto->elements as $descriptionElement) {
1065
          if (is_object($descriptionElement->area)) {
1066
            $sortKey = $descriptionElement->area->representation_L10n;
1067
            $distributionElements[$sortKey] = $descriptionElement;
1068
          }
1069
        }
1070
        ksort($distributionElements);
1071
        $distribution_element_render_array = compose_description_elements_distribution($distributionElements);
1072
        $dto_out_array[] = $distribution_element_render_array;
1073

    
1074
      }
1075
      //
1076
      $block->content[] = compose_feature_block_wrap_elements(
1077
        $dto_out_array, $feature, $distribution_glue, $distribution_sortOutArray
1078
      );
1079
    }
1080

    
1081
    // --- TextData at the bottom
1082
    if ($text_data_out_array && !variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
1083
      $block->content[] = compose_feature_block_wrap_elements(
1084
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
1085
      );
1086
    }
1087

    
1088
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . UUID_DISTRIBUTION)));
1089
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
1090
    $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
1091

    
1092
    return $block;
1093
  }
1094

    
1095

    
1096
  /**
1097
   * Composes a drupal render array for single CDM TextData description element.
1098
   *
1099
   * @param $element
1100
   *    The CDM TextData description element.
1101
   *  @param $feature_uuid
1102
   * @param bool $prepend_feature_label
1103
   *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1104
   *
1105
   * @return array
1106
   *   A drupal render array with the following elements being used:
1107
   *    - #tag: either 'div', 'li', ...
1108
   *    ⁻ #attributes: class attributes
1109
   *    - #value_prefix: (optionally) contains footnote anchors
1110
   *    - #value: contains the textual content
1111
   *    - #value_suffix: (optionally) contains footnote keys
1112
   *
1113
   * @ingroup compose
1114
   */
1115
  function compose_description_element_text_data($element, $feature_uuid, $feature_block_settings, $prepend_feature_label = FALSE) {
1116

    
1117
    $footnote_list_key_suggestion = $feature_uuid;
1118

    
1119
    $element_markup = '';
1120
    if (isset($element->multilanguageText_L10n->text)) {
1121
      // TODO replacement of \n by <br> should be configurable
1122
      $element_markup = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
1123
    }
1124

    
1125
    $render_array = compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label);
1126

    
1127
    return $render_array;
1128
  }
1129

    
1130

    
1131
/**
1132
 * Theme function to render CDM DescriptionElements of the type TaxonInteraction.
1133
 *
1134
 * @param $element
1135
 *  The CDM TaxonInteraction entity
1136
 *
1137
 * @return
1138
 *  A drupal render array
1139
 *
1140
 * @ingroup compose
1141
 */
1142
function compose_description_element_taxon_interaction($element, $feature_block_settings) {
1143

    
1144
  $out = '';
1145

    
1146

    
1147
  if (isset($element->description_L10n)) {
1148
    $out .=  ' ' . $element->description_L10n;
1149
  }
1150

    
1151
  if(isset($element->taxon2)){
1152
    $out = render_taxon_or_name($element->taxon2, url(path_to_taxon($element->taxon2->uuid)));
1153
  }
1154

    
1155
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1156

    
1157
  return $render_array;
1158
}
1159

    
1160

    
1161
/**
1162
 * Renders a single instance of the type IndividualsAssociations.
1163
 *
1164
 * @param $element
1165
 *   The CDM IndividualsAssociations entity.
1166
 * @param $feature_block_settings
1167
 *
1168
 * @return array
1169
 *   Drupal render array
1170
 *
1171
 * @ingroup compose
1172
 */
1173
function compose_description_element_individuals_association($element, $feature_block_settings) {
1174

    
1175
  $out = '';
1176

    
1177
  $render_array = compose_cdm_specimen_or_observation($element->associatedSpecimenOrObservation);
1178

    
1179
  if (isset($element->description_L10n)) {
1180
    $out .=  ' ' . $element->description_L10n;
1181
  }
1182

    
1183
  $out .= drupal_render($render_array);
1184

    
1185
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1186

    
1187
  return $render_array;
1188
}
1189

    
1190
/**
1191
 * Renders a single instance of the type CategoricalData.
1192
 *
1193
 * @param $element
1194
 *  The CDM CategoricalData entity
1195
 *
1196
 * @param $feature_block_settings
1197
 *
1198
 * @param bool $prepend_feature_label
1199
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1200
 *
1201
 * @return array
1202
 *   a drupal render array for given CategoricalData element
1203
 *
1204
 * @ingroup compose
1205
 */
1206
function compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1207

    
1208
  $state_data_markup = render_state_data($element);
1209

    
1210
  $render_array = compose_description_element($element, $feature_block_settings, $state_data_markup, $element->feature->uuid, $prepend_feature_label);
1211

    
1212
  return $render_array;
1213
}
1214

    
1215
/**
1216
 * @param $element
1217
 *
1218
 * @return string
1219
 * the markup
1220
 */
1221
function render_state_data($element) {
1222

    
1223
  $state_data_items = [];
1224

    
1225
  if (isset($element->stateData)) {
1226
    foreach ($element->stateData as $state_data) {
1227

    
1228
      $state = NULL;
1229

    
1230
      if (isset($state_data->state)) {
1231
        $state = cdm_term_representation($state_data->state);
1232

    
1233
          $sample_count = 0;
1234
          if (isset($state_data->count)) {
1235
            $sample_count = $state_data->count;
1236
            $state .= ' [' . $state_data->count . ']';
1237
          }
1238
    
1239
          if (isset($state_data->modifyingText_L10n)) {
1240
            $state .= ' ' . $state_data->modifyingText_L10n;
1241
          }
1242
    
1243
          $modifiers_strings = cdm_modifers_representations($state_data);
1244
          $state_data_markup = $state . ($modifiers_strings ? ' ' . $modifiers_strings : '');
1245
          // we could use strip_tags() to reduce the markup to text for the key but this is expensive
1246
          $sort_key = str_pad($sample_count, 6, '0', STR_PAD_LEFT) . '_' . $state_data_markup;
1247
          $state_data_items[$sort_key] = $state_data_markup;
1248
      }
1249

    
1250
    }
1251
    krsort($state_data_items);
1252
    $out = '<span class="' . html_class_attribute_ref($element) . '">' . join(', ', $state_data_items) .  '</span>';
1253
  }
1254
  return $out;
1255
}
1256

    
1257

    
1258
/**
1259
 * Theme function to render CDM DescriptionElements of the type QuantitativeData.
1260
 *
1261
 * The function renders the statisticalValues contained in the QuantitativeData
1262
 * entity according to the following scheme:
1263
 *
1264
 * (ExtremeMin)-Min-Average-Max-(ExtremeMax)
1265
 *
1266
 * All modifiers of these values are appended.
1267
 *
1268
 * If the QuantitativeData is containing more statisticalValues with further
1269
 * statisticalValue types, these additional measures will be appended to the
1270
 * above string separated by whitespace.
1271
 *
1272
 * Special cases;
1273
 * 1. Min==Max: this will be interpreted as Average
1274
 *
1275
 * @param $element
1276
 *  The CDM QuantitativeData entity
1277
 *
1278
 * @param $feature_block_settings
1279
 *
1280
 * @param bool $prepend_feature_label
1281
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1282
 *
1283
 *
1284
 * @return array
1285
 *   drupal render array for the given QuantitativeData element
1286
 *
1287
 * @ingroup compose
1288
 */
1289
function compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1290
  /*
1291
   * - statisticalValues
1292
   *   - value
1293
   *   - modifiers
1294
   *   - type
1295
   * - unit->representation_L10n
1296
   * - modifyingText
1297
   * - modifiers
1298
   * - sources
1299
   */
1300

    
1301
  $out = render_quantitative_statistics($element);
1302

    
1303
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid, $prepend_feature_label);
1304

    
1305
  return $render_array;
1306
}
1307

    
1308
/**
1309
 * Composes the HTML for quantitative statistics
1310
 * @param $element
1311
 *
1312
 * @return string
1313
 */
1314
function render_quantitative_statistics($element) {
1315

    
1316
  $out = '';
1317
  $type_representation = NULL;
1318
  $min_max = statistical_values_array();
1319
  $sample_size_markup = null;
1320

    
1321
  if (isset($element->statisticalValues)) {
1322
    $out = '<span class=\"' . html_class_attribute_ref($element) . '\">';
1323
    $other_values_markup = [];
1324
    foreach ($element->statisticalValues as $statistical_val) {
1325

    
1326
      // compile the full value string which also may contain modifiers
1327
      if (isset($statistical_val->value)) {
1328
        $statistical_val->_value = $statistical_val->value;
1329
      }
1330
      $val_modifiers_strings = cdm_modifers_representations($statistical_val);
1331
      if ($val_modifiers_strings) {
1332
        $statistical_val->_value = ' ' . $val_modifiers_strings . ' ' . $statistical_val->_value;
1333
      }
1334

    
1335
      // either put into min max array or into $other_values
1336
      // for generic output to be appended to 'min-max' string
1337
      if (array_key_exists(statistical_measure_term2min_max_key($statistical_val->type), $min_max)) {
1338
        $min_max[statistical_measure_term2min_max_key($statistical_val->type)] = $statistical_val;
1339
      }
1340
      else {
1341
        drupal_set_message("Unsupported statistical value type: " . $statistical_val->type->uuid, "error");
1342
      }
1343
    } // end of loop over statisticalValues
1344

    
1345
    // create markup
1346
    $unit = null;
1347
    if (isset($element->unit)) {
1348
      $unit = ' <span class="unit" title="'
1349
        . cdm_term_representation($element->unit) . '">'
1350
        . cdm_term_representation_abbreviated($element->unit)
1351
        . '</span>';
1352
    }
1353
    $min_max_markup = statistical_values($min_max, $unit);
1354
    $out .= $min_max_markup . '</span>';
1355
  }
1356

    
1357
  if($sample_size_markup){
1358
    $out .= ' ' . $sample_size_markup;
1359

    
1360
  }
1361

    
1362
  // modifiers of the description element itself
1363
  $modifier_string = cdm_modifers_representations($element);
1364
  $out .= ($modifier_string ? ' ' . $modifier_string : '');
1365
  if (isset($element->modifyingText_L10n)) {
1366
    $out = $element->modifyingText_L10n . ' ' . $out;
1367
  }
1368
  return $out;
1369
}
1370

    
1371
function statistical_measure_term2min_max_key($term){
1372
  static $uuid2key = [
1373
    UUID_STATISTICALMEASURE_MIN => 'Min',
1374
    UUID_STATISTICALMEASURE_MAX => 'Max',
1375
    UUID_STATISTICALMEASURE_AVERAGE => 'Average',
1376
    UUID_STATISTICALMEASURE_SAMPLESIZE => 'SampleSize',
1377
    UUID_STATISTICALMEASURE_VARIANCE => 'Variance',
1378
    UUID_STATISTICALMEASURE_TYPICALLOWERBOUNDARY => 'TypicalLowerBoundary',
1379
    UUID_STATISTICALMEASURE_TYPICALUPPERBOUNDARY => 'TypicalUpperBoundary',
1380
    UUID_STATISTICALMEASURE_STANDARDDEVIATION => 'StandardDeviation',
1381
    UUID_STATISTICALMEASURE_EXACTVALUE => 'ExactValue',
1382
    UUID_STATISTICALMEASURE_STATISTICALMEASUREUNKNOWNDATA => 'StatisticalMeasureUnknownData'
1383
  ];
1384
  return $uuid2key[$term->uuid];
1385
}
1386

    
1387

    
1388
/**
1389
 * Wraps the render array for the given feature into an enclosing html tag.
1390
 *
1391
 * Optionally the elements can be sorted and glued together by a separator string.
1392
 *
1393
 * @param array $description_element_render_arrays
1394
 *   An list of render arrays. Which are usually are produced by the compose_description_element()
1395
 *   function. The render arrays should be of #type=html_tag, so that they can understand the #attribute property.
1396
 * @param  $feature :
1397
 *  The feature to which the elements given in $elements are belonging to.
1398
 * @param string $glue :
1399
 *  Defaults to empty string.
1400
 * @param bool $sort
1401
 *   Boolean Whether to sort the $elements alphabetically, default is FALSE
1402
 *
1403
 * @return array
1404
 *    A Drupal render array
1405
 *
1406
 * @ingroup compose
1407
 */
1408
  function compose_feature_block_wrap_elements(array $description_element_render_arrays, $feature, $glue = '', $sort = FALSE)
1409
  {
1410

    
1411
    $feature_block_settings = get_feature_block_settings($feature->uuid);
1412
    $enclosing_tag = $feature_block_settings['as_list'];
1413

    
1414
    if ($sort) { // TODO remove parameter and replace by $feature_block_settings['sort_elements']
1415
      usort($description_element_render_arrays, 'compare_description_element_render_arrays');
1416
    }
1417

    
1418
    $is_first = true;
1419
    foreach($description_element_render_arrays as &$element_render_array){
1420
      if(!is_array($element_render_array)){
1421
        $element_render_array = markup_to_render_array($element_render_array);
1422
      }
1423
      $element_render_array['#attributes']['class'][] = "feature-block-element";
1424

    
1425
      // add the glue!
1426
      if(!$is_first) {
1427
        if (isset($element_render_array['#value'])) {
1428
          $element_render_array['#value'] = $glue . $element_render_array['#value'];
1429
        } elseif (isset($element_render_array['#markup'])) {
1430
          $element_render_array['#markup'] = $glue . $element_render_array['#markup'];
1431
        }
1432
      }
1433
      $is_first = false;
1434
    }
1435

    
1436
    $render_array['elements']['children'] = $description_element_render_arrays;
1437

    
1438
    $render_array['elements']['#prefix'] = '<' . $enclosing_tag . ' class="feature-block-elements" id="' . $feature->representation_L10n . '">';
1439
    $render_array['elements']['#suffix'] = '</' . $enclosing_tag . '>';
1440

    
1441
    return $render_array;
1442
  }
1443

    
1444

    
1445
  /* compose nameInSource or originalNameString as markup
1446
   *
1447
   * @param $source
1448
   * @param $do_link_to_name_used_in_source
1449
   * @param $suppress_for_shown_taxon
1450
   *    the nameInSource will be suppressed when it has the same name as the accepted taxon
1451
   *    for which the taxon page is being created, Defaults to TRUE
1452
   *
1453
   * @return array
1454
   *    A Drupal render array with an additional element, the render array is empty
1455
   *    if the source had no name in source information
1456
   *    - #_plaintext: contains the plaintext version of the name (custom element)
1457
   *
1458
   * @ingroup compose
1459
   */
1460
  function compose_name_in_source($source, $do_link_to_name_used_in_source, $suppress_for_shown_taxon = TRUE) {
1461

    
1462
    $plaintext = NULL;
1463
    $markup = NULL;
1464
    $name_in_source_render_array = array();
1465

    
1466
    static $taxon_page_accepted_name = '';
1467
    $taxon_uuid = get_current_taxon_uuid();
1468
    if($suppress_for_shown_taxon && $taxon_uuid && empty($taxon_page_accepted_name)){
1469

    
1470
      $current_taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $taxon_uuid);
1471
      $taxon_page_accepted_name = $current_taxon->name->titleCache;
1472
    }
1473

    
1474
    if (isset($source->nameUsedInSource->uuid) && isset($source->nameUsedInSource->titleCache)) {
1475
      // it is a DescriptionElementSource !
1476
      $plaintext = $source->nameUsedInSource->titleCache;
1477
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1478
        return $name_in_source_render_array; // SKIP this name
1479
      }
1480
      $markup = render_taxon_or_name($source->nameUsedInSource);
1481
      if ($do_link_to_name_used_in_source) {
1482
        $markup = l(
1483
          $markup,
1484
          path_to_name($source->nameUsedInSource->uuid),
1485
          array(
1486
            'attributes' => array(),
1487
            'absolute' => TRUE,
1488
            'html' => TRUE,
1489
          ));
1490
      }
1491
    }
1492
    else if (isset($source->originalNameString) && !empty($source->originalNameString)) {
1493
      // the name used in source can not be expressed as valid taxon name,
1494
      // so the editor has chosen to put the freetext name into ReferencedEntityBase.originalNameString
1495
      // field
1496
      // using the originalNameString as key to avoid duplicate entries
1497
      $plaintext = $source->originalNameString;
1498
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1499
        return $name_in_source_render_array; // SKIP this name
1500
      }
1501
      $markup = $source->originalNameString;
1502
    }
1503

    
1504
    if ($plaintext) { // checks if we have any content
1505
      $name_in_source_render_array = markup_to_render_array($markup);
1506
      $name_in_source_render_array['#_plaintext'] = $plaintext;
1507
    }
1508

    
1509
    return $name_in_source_render_array;
1510
  }
1511

    
1512

    
1513

    
1514
  /**
1515
   * Return HTML for a list of description elements.
1516
   *
1517
   * Usually these are of a specific feature type.
1518
   *
1519
   * @param $description_elements
1520
   *   array of descriptionElements which belong to the same feature.
1521
   *   These descriptions elements of a Description must be ordered by the chosen feature tree by
1522
   *   calling the function _mergeFeatureTreeDescriptions().
1523
   *   @see _mergeFeatureTreeDescriptions()
1524
   *
1525
   * @param  $feature_uuid
1526
   *
1527
   * @return
1528
   *    A drupal render array for the $descriptionElements, may be an empty array if the textual content was empty.
1529
   *    Footnote key or anchors are not considered to be textual content.
1530
   *
1531
   * @ingroup compose
1532
   */
1533
  function compose_feature_block_items_generic($description_elements, $feature) {
1534

    
1535
    $elements_out_array = array();
1536
    $distribution_tree = null;
1537

    
1538
    /*
1539
     * $feature_block_has_content will be set true if at least one of the
1540
     * $descriptionElements contains some text which makes up some content
1541
     * for the feature block. Footnote keys are not considered
1542
     * to be content in this sense.
1543
     */
1544
    $feature_block_has_content = false;
1545

    
1546
    if (is_array($description_elements)) {
1547
      foreach ($description_elements as $description_element) {
1548
          /* decide based on the description element class
1549
           *
1550
           * Features handled here:
1551
           * all except DISTRIBUTION, COMMON_NAME, USES, IMAGES,
1552
           *
1553
           * TODO provide api_hook as extension point for this?
1554
           */
1555
        $feature_block_settings = get_feature_block_settings($description_element->feature->uuid);
1556
        switch ($description_element->class) {
1557
          case 'TextData':
1558
            $elements_out_array[] = compose_description_element_text_data($description_element, $description_element->feature->uuid, $feature_block_settings);
1559
            break;
1560
          case 'CategoricalData':
1561
            $elements_out_array[] = compose_description_element_categorical_data($description_element, $feature_block_settings);
1562
            break;
1563
          case 'QuantitativeData':
1564
            $elements_out_array[] = compose_description_element_quantitative_data($description_element, $feature_block_settings);
1565
            break;
1566
          case 'IndividualsAssociation':
1567
            $elements_out_array[] = compose_description_element_individuals_association($description_element, $feature_block_settings);
1568
            break;
1569
          case 'TaxonInteraction':
1570
            $elements_out_array[] = compose_description_element_taxon_interaction($description_element, $feature_block_settings);
1571
            break;
1572
          case 'CommonTaxonName':
1573
            $elements_out_array[] = compose_description_element_common_taxon_name($description_element, $feature_block_settings);
1574
            break;
1575
          case 'Uses':
1576
            /* IGNORE Uses classes, these are handled completely in compose_feature_block_items_use_records()  */
1577
            break;
1578
          default:
1579
            $feature_block_has_content = true;
1580
            $elements_out_array[] = markup_to_render_array('<li>No method for rendering unknown description class: ' . $description_element->class . '</li>');
1581
        }
1582
        $elements_out_array_last_item = $elements_out_array[count($elements_out_array) - 1];
1583
        // considering not empty as long as the last item added is a render array
1584
        $feature_block_has_content = $feature_block_has_content || !empty($elements_out_array_last_item['#type']);
1585
      }
1586

    
1587
      // If feature = CITATION sort the list of sources.
1588
      // This is ONLY for FLORA MALESIANA and FLORE d'AFRIQUE CENTRALE.
1589
      if (isset($description_element) && $description_element->feature->uuid == UUID_CITATION) {
1590
        sort($elements_out_array);
1591
      }
1592
    }
1593

    
1594
    // sanitize: remove empty and NULL items from the render array
1595
    $tmp_out_array = $elements_out_array;
1596
    $elements_out_array = array();
1597
    foreach($tmp_out_array as $item){
1598
      if(is_array($item) && count($item) > 0){
1599
        $elements_out_array[] = $item;
1600
      }
1601
    }
1602

    
1603
    return $elements_out_array;
1604
  }
1605

    
1606
/**
1607
 * Composes block of common names for the given DescriptionElements $elements which must be of the feature CommonName
1608
 *
1609
 * @parameter $elements
1610
 *  an array of CDM DescriptionElements either of type CommonName or TextData
1611
 * @parameter $feature
1612
 *  the common feature of all $elements, must be CommonName
1613
 *
1614
 * @return
1615
 *   A drupal render array
1616
 *
1617
 * @ingroup compose
1618
 */
1619
function compose_feature_block_items_feature_common_name($elements, $feature, $weight = FALSE) {
1620

    
1621
  $common_name_out = '';
1622
  $common_name_feature_elements = array();
1623
  $textData_commonNames = array();
1624

    
1625
  $footnote_key_suggestion = 'common-names-feature-block';
1626

    
1627
  $feature_block_settings = get_feature_block_settings(UUID_COMMON_NAME);
1628

    
1629
  if (is_array($elements)) {
1630
    foreach ($elements as $element) {
1631

    
1632
      if ($element->class == 'CommonTaxonName') {
1633

    
1634
        // common name without a language or area, should not happen but is possible
1635
        $language_area_key = '';
1636
        if (isset($element->language->representation_L10n)) {
1637
          $language_area_key .= $element->language->representation_L10n ;
1638
        }
1639
        if(isset($element->area->titleCache) && strlen($element->area->titleCache) > 0){
1640
          $language_area_key .= ($language_area_key ? ' '  : '') . '(' . $element->area->titleCache . ')';
1641
        }
1642
        if($language_area_key){
1643
          $language_area_key = '<span class="language-area-label">' . $language_area_key . '<span class="separator">: </span></span>';
1644
        }
1645

    
1646
        if(isset($common_names[$language_area_key][$element->name])) {
1647
          // same name already exists for language and area combination, se we merge the description elements
1648
          cdm_merge_description_elements($common_names[$language_area_key][$element->name], $element);
1649
        } else{
1650
          // otherwise add as new entry
1651
          $common_names[$language_area_key][$element->name] = $element;
1652
        }
1653

    
1654
      }
1655
      elseif ($element->class == 'TextData') {
1656
        $textData_commonNames[] = $element;
1657
      }
1658
    }
1659
  }
1660
  // Handling common names.
1661
  if (isset($common_names) && count($common_names) > 0) {
1662
    // Sorting the array based on the key (language, + area if set).
1663
    // Comment @WA there are common names without a language, so this sorting
1664
    // can give strange results.
1665
    ksort($common_names);
1666

    
1667
    // loop over set of elements per language area
1668
    foreach ($common_names as $language_area_key => $elements) {
1669
      ksort($elements); // sort names alphabetically
1670
      $per_language_area_out = array();
1671

    
1672
      foreach ($elements as $element) {
1673
        $common_name_render_array = compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion);
1674
        $common_name_markup = drupal_render($common_name_render_array);
1675
        // IMPORTANT!
1676
        // during the above drupal_render the theme_html_tag function is executed, which adds a "\n" character to the end of the markup
1677
        // this is an error and the trailing whitespace needs to be removed
1678
        if(str_endsWith($common_name_markup, "\n")){
1679
          $common_name_markup = substr($common_name_markup, 0, strlen($common_name_markup) - 1);
1680
        }
1681
        $per_language_area_out[] = $common_name_markup;
1682
      }
1683

    
1684
      $common_name_feature_elements[] = $language_area_key . join(', ', $per_language_area_out);
1685
    } // End of loop over set of elements per language area
1686

    
1687

    
1688
    $common_name_feature_elements_render_array = compose_feature_block_wrap_elements(
1689
      $common_name_feature_elements, $feature, '; ', FALSE
1690
    );
1691
    $common_name_out .= drupal_render($common_name_feature_elements_render_array); // FIXME should this be a render array instead?
1692

    
1693
  }
1694

    
1695
  // Handling commons names as text data.
1696
  $text_data_out = array();
1697

    
1698
  foreach ($textData_commonNames as $text_data_element) {
1699
    /* footnotes are not handled correctly in compose_description_element_text_data,
1700
       need to set 'common-names-feature-block' as $footnote_key_suggestion */
1701
    RenderHints::setFootnoteListKey($footnote_key_suggestion);
1702
    $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
1703
    $text_data_out[] = drupal_render($text_data_render_array);
1704
  }
1705

    
1706
  $common_name_out_text_data = compose_feature_block_wrap_elements(
1707
    $text_data_out, $feature
1708
  );
1709

    
1710
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . $footnote_key_suggestion));
1711
  $footnotes .= theme('cdm_footnotes', array('footnoteListKey' => $footnote_key_suggestion)); // FIXME is this needed at all?
1712
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnote_key_suggestion));
1713

    
1714
  return  markup_to_render_array(  // FIXME markup_to_render_array should no longer be needed
1715
    '<div class="common-taxon-name">' . $common_name_out . '</div>'
1716
    .'<div class="text-data">' . drupal_render($common_name_out_text_data) . '</div>'
1717
    .$footnotes,
1718
    $weight
1719
  );
1720
}
1721

    
1722
/**
1723
 * Renders a single instance of the type CommonTaxonName.
1724
 *
1725
 * @param $element
1726
 *   The CDM CommonTaxonName entity.
1727
 * @param $feature_block_settings
1728
 *
1729
 * @param $footnote_key_suggestion
1730
 *
1731
 * @param $element_tag_name
1732
 *
1733
 * @return array
1734
 *   Drupal render array
1735
 *
1736
 * @ingroup compose
1737
 */
1738
function compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion = NULL)
1739
{
1740

    
1741
  if(!$footnote_key_suggestion) {
1742
    $footnote_key_suggestion = $element->feature->uuid;
1743
  }
1744

    
1745
  $name = '';
1746
  if(isset($element->name)){
1747
    $name = $element->name;
1748
  }
1749

    
1750

    
1751
  return compose_description_element($element, $feature_block_settings, $name, $footnote_key_suggestion);
1752
}
1753

    
1754
/**
1755
 * Composes the render array for a CDM Distribution description element
1756
 *
1757
 * @param array $description_elements
1758
 *   Array of CDM Distribution instances
1759
 * @param $enclosingTag
1760
 *   The html tag to be use for the enclosing element
1761
 *
1762
 * @return array
1763
 *   A Drupal render array
1764
 *
1765
 * @ingroup compose
1766
 */
1767
function compose_description_elements_distribution($description_elements){
1768

    
1769
  $markup_array = array();
1770
  RenderHints::pushToRenderStack('descriptionElementDistribution');
1771
  RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1772

    
1773
  $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
1774
  $enclosingTag = cdm_feature_block_element_tag_name($feature_block_settings);
1775

    
1776
  foreach ($description_elements as $description_element) {
1777
    $annotations_and_sources = handle_annotations_and_sources(
1778
      $description_element,
1779
      handle_annotations_and_sources_config($feature_block_settings),
1780
      $description_element->area->representation_L10n,
1781
      UUID_DISTRIBUTION
1782
    );
1783

    
1784

    
1785
    list($status_label, $status_markup) = distribution_status_label_and_markup($description_element);
1786

    
1787
    $out = '';
1788
    $out .= '<' . $enclosingTag . ' class="descriptionElement descriptionElement-' . $description_element->uuid
1789
      . ' " title="' . $status_label. '">'
1790
      . $description_element->area->representation_L10n
1791
      . $status_markup;
1792
    if(!empty($annotations_and_sources['source_references'])){
1793
      $out .= ' ' . join(' ', $annotations_and_sources['source_references'] );
1794
    }
1795
    $out .= $annotations_and_sources['foot_note_keys']   . '</' . $enclosingTag . '>';
1796
    $markup_array[] = $out;
1797
  }
1798

    
1799
  RenderHints::popFromRenderStack();
1800
  return markup_to_render_array(join('<span class="separator">' . $feature_block_settings['glue'] . '</span>', $markup_array));
1801
}
1802

    
1803
  /**
1804
   * @param $descriptionElement
1805
   * @return array
1806
   */
1807
  function distribution_status_label_and_markup($descriptionElement, $status_glue = '&#8210; ') {
1808
    $status_markup = '';
1809
    $status_label = '';
1810

    
1811
    if (isset($descriptionElement->status)) {
1812
      $status_label = $descriptionElement->status->representation_L10n;
1813
      $status_markup =  '<span class="distributionStatus"> '
1814
        . $status_glue
1815
        . '<span class="distributionStatus-' . $descriptionElement->status->idInVocabulary . '">'
1816
        . $status_label
1817
        . '</span></span>';
1818

    
1819
    };
1820
    return array($status_label, $status_markup);
1821
  }
1822

    
1823

    
1824
  /**
1825
   * Provides the merged feature tree for a taxon profile page.
1826
   *
1827
   * The merging of the profile feature tree is actually done in
1828
   * _mergeFeatureTreeDescriptions(). See this method  for details
1829
   * on the structure of the merged tree.
1830
   *
1831
   * This method provides a hook which can be used to modify the
1832
   * merged feature tree after it has been created, see
1833
   * hook_merged_taxon_feature_tree_alter()
1834
   *
1835
   * @param $taxon
1836
   *    A CDM Taxon instance
1837
   *
1838
   * @return object
1839
   *    The merged feature tree
1840
   *
1841
   */
1842
  function merged_taxon_feature_tree($taxon) {
1843

    
1844
    // 1. fetch descriptions_by_featuretree but exclude the distribution feature
1845
    $merged_tree = cdm_ws_descriptions_by_featuretree(get_profile_feature_tree(), $taxon->uuid, array(UUID_DISTRIBUTION));
1846

    
1847

    
1848
    // 2. find the distribution feature node
1849
    $distribution_node =& cdm_feature_tree_find_node($merged_tree->root->childNodes, UUID_DISTRIBUTION);
1850

    
1851
    if ($distribution_node) {
1852
      // 3. get the distributionInfoDTO
1853
      $query_parameters = cdm_distribution_filter_query();
1854
      $query_parameters['part'] = array('mapUriParams');
1855
      if(variable_get(DISTRIBUTION_CONDENSED)){
1856
        $query_parameters['part'][] = 'condensedDistribution';
1857
      }
1858
      if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) == 'TREE') {
1859
        $query_parameters['part'][] = 'tree';
1860
      }
1861
      else {
1862
        $query_parameters['part'][] = 'elements';
1863
      }
1864
      $query_parameters['omitLevels'] = array();
1865
      foreach(variable_get(DISTRIBUTION_TREE_OMIT_LEVELS, array()) as $uuid){
1866
        if(is_uuid($uuid)){
1867
          $query_parameters['omitLevels'][] = $uuid;
1868
        }
1869
      }
1870
      $customStatusColorsJson = variable_get(DISTRIBUTION_STATUS_COLORS, NULL);
1871
      if ($customStatusColorsJson) {
1872
        $query_parameters['statusColors'] = $customStatusColorsJson;
1873
      }
1874

    
1875
      $distribution_info_dto = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION_DISTRIBUTION_INFO_FOR, $taxon->uuid, queryString($query_parameters));
1876
      // 4. get distribution TextData is there are any
1877
      $distribution_text_data = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON,
1878
        array(
1879
          'taxon' => $taxon->uuid,
1880
          'type' => 'TextData',
1881
          'features' => UUID_DISTRIBUTION
1882
        )
1883
      );
1884

    
1885
      // 5. put all distribution data into the distribution feature node
1886
      if ($distribution_text_data //if text data exists
1887
        || ($distribution_info_dto && isset($distribution_info_dto->tree) && $distribution_info_dto->tree->rootElement->numberOfChildren > 0) // OR if tree element has distribution elements
1888
        || ($distribution_info_dto && !empty($distribution_info_dto->elements))
1889
      ) { // OR if DTO has distribution elements
1890
        $distribution_node->descriptionElements = array('#type' => 'DTO');
1891
        if ($distribution_text_data) {
1892
          $distribution_node->descriptionElements['TextData'] = $distribution_text_data;
1893
        }
1894
        if ($distribution_info_dto) {
1895
          $distribution_node->descriptionElements['DistributionInfoDTO'] = $distribution_info_dto;
1896
        }
1897
      }
1898
    }
1899

    
1900
    // allows modifying the merged tree via a the hook_cdm_feature_node_block_content_alter
1901
    drupal_alter('merged_taxon_feature_tree', $taxon, $merged_tree);
1902

    
1903
    return $merged_tree;
1904
  }
1905

    
1906

    
1907
  function compose_distribution_hierarchy($distribution_tree, $feature_block_settings){
1908

    
1909
    static $hierarchy_style;
1910
    // TODO expose $hierarchy_style to administration or provide a hook
1911
    if( !isset($hierarchy_style)){
1912
      $hierarchy_style = get_array_variable_merged(DISTRIBUTION_HIERARCHY_STYLE, DISTRIBUTION_HIERARCHY_STYLE_DEFAULT);
1913
    }
1914

    
1915
    $render_array = array();
1916

    
1917
    RenderHints::pushToRenderStack('descriptionElementDistribution');
1918
    RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1919

    
1920
    // Returning NULL if there are no description elements.
1921
    if ($distribution_tree == null) {
1922
      return $render_array;
1923
    }
1924
    // for now we are not using a render array internally to avoid performance problems
1925
    $markup = '';
1926
    if (isset($distribution_tree->rootElement->children)) {
1927
      $tree_nodes = $distribution_tree->rootElement->children;
1928
      _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, $markup, $hierarchy_style);
1929
    }
1930

    
1931
    $render_array['distribution_hierarchy'] = markup_to_render_array(
1932
      $markup,
1933
      0,
1934
      '<div id="distribution_hierarchy" class="distribution_hierarchy">',
1935
      '</div>'
1936
    );
1937

    
1938
    RenderHints::popFromRenderStack();
1939

    
1940
    return $render_array;
1941
  }
1942

    
1943
  /**
1944
   * this function should produce markup as the compose_description_elements_distribution()
1945
   * function.
1946
   *
1947
   * @see compose_description_elements_distribution()
1948
   *
1949
   * @param $distribution_tree
1950
   * @param $feature_block_settings
1951
   * @param $tree_nodes
1952
   * @param $markup
1953
   * @param $hierarchy_style
1954
   */
1955
  function _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, &$markup, $hierarchy_style, $level_index = -1){
1956

    
1957
    $level_index++;
1958
    static $enclosingTag = "span";
1959

    
1960
    $level_style = array_shift($hierarchy_style);
1961
    if(count($hierarchy_style) == 0){
1962
      // lowest defined level style will be reused for all following levels
1963
      $hierarchy_style[] = $level_style;
1964
    }
1965

    
1966
    $node_index = -1;
1967
    $per_node_markup = array();
1968
    foreach ($tree_nodes as $node){
1969

    
1970
      $per_node_markup[++$node_index] = '';
1971

    
1972
      $label = $node->nodeId->representation_L10n;
1973

    
1974
      $distributions = $node->data;
1975
      $distribution_uuids = array();
1976
      $distribution_aggregate = NULL;
1977
        foreach($distributions as $distribution){
1978

    
1979
          $distribution_uuids[] = $distribution->uuid;
1980

    
1981
          // if there is more than one distribution we aggregate the sources and
1982
          // annotations into a synthetic distribution so that the footnote keys
1983
          // can be rendered consistently
1984
          if(!$distribution_aggregate) {
1985
            $distribution_aggregate = $distribution;
1986
            if(!isset($distribution_aggregate->sources[0])){
1987
              $distribution_aggregate->sources = array();
1988
            }
1989
            if(!isset($distribution_aggregate->annotations[0])){
1990
              $distribution_aggregate->annotations = array();
1991
            }
1992
          } else {
1993
            if(isset($distribution->sources[0])) {
1994
              $distribution_aggregate->sources = array_merge($distribution_aggregate->sources,
1995
                $distribution->sources);
1996
            }
1997
            if(isset($distribution->annotations[0])) {
1998
              $distribution_aggregate->annotations = array_merge($distribution_aggregate->annotations,
1999
                $distribution->annotations);
2000
            }
2001
          }
2002
        }
2003

    
2004
      $status_label= '';
2005
      $status_markup = '';
2006
      $annotations_and_sources =  null;
2007
      if($distribution_aggregate) {
2008
        $annotations_and_sources = handle_annotations_and_sources(
2009
          $distribution_aggregate,
2010
          handle_annotations_and_sources_config($feature_block_settings),
2011
          $label,
2012
          UUID_DISTRIBUTION
2013
        );
2014

    
2015
        list($status_label, $status_markup) = distribution_status_label_and_markup($distribution, $level_style['status_glue']);
2016
      }
2017

    
2018
      $per_node_markup[$node_index] .= '<' . $enclosingTag . ' class="descriptionElement'
2019
        . join(' descriptionElement-', $distribution_uuids)
2020
        . ' level_index_' . $level_index
2021
        . ' " title="' . $status_label . '">'
2022
        . '<span class="area_label">' . $label
2023
        . $level_style['label_suffix'] . '</span>'
2024
        . $status_markup
2025
      ;
2026

    
2027
      if(isset($annotations_and_sources)){
2028
        if(!empty($annotations_and_sources['source_references'])){
2029
          $per_node_markup[$node_index] .= ' ' . join(', ' , $annotations_and_sources['source_references']);
2030
        }
2031
        if($annotations_and_sources['foot_note_keys']) {
2032
          $per_node_markup[$node_index] .= $annotations_and_sources['foot_note_keys'];
2033
        }
2034
      }
2035

    
2036
      if(isset($node->children[0])){
2037
        _compose_distribution_hierarchy(
2038
          $node->children,
2039
          $feature_block_settings,
2040
          $per_node_markup[$node_index],
2041
          $hierarchy_style,
2042
          $level_index
2043
        );
2044
      }
2045

    
2046
      $per_node_markup[$node_index] .= '</' . $enclosingTag . '>';
2047
    }
2048
    $markup .= $level_style['item_group_prefix']  . join( $level_style['item_glue'], $per_node_markup) . $level_style['item_group_postfix'];
2049
  }
2050

    
2051

    
2052
/**
2053
 * Provides the content for a block of Uses Descriptions for a given taxon.
2054
 *
2055
 * Fetches the list of TaxonDescriptions tagged with the MARKERTYPE_USE
2056
 * and passes them to the theme function theme_cdm_UseDescription().
2057
 *
2058
 * @param string $taxon_uuid
2059
 *   The uuid of the Taxon
2060
 *
2061
 * @return array
2062
 *   A drupal render array
2063
 */
2064
function cdm_block_use_description_content($taxon_uuid, $feature) {
2065

    
2066
  $use_description_content = array();
2067

    
2068
  if (is_uuid($taxon_uuid )) {
2069
    $markerTypes = array();
2070
    $markerTypes['markerTypes'] = UUID_MARKERTYPE_USE;
2071
    $useDescriptions = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXON . '/' . $taxon_uuid . '/descriptions', $markerTypes);
2072
    if (!empty($useDescriptions)) {
2073
      $use_description_content = compose_feature_block_items_use_records($useDescriptions, $taxon_uuid, $feature);
2074
    }
2075
  }
2076

    
2077
  return $use_description_content;
2078
}
2079

    
2080
/**
2081
 * Creates a trunk of a feature object which can be used to build pseudo feature blocks like the Bibliography.
2082
 *
2083
 * @param $representation_L10n
2084
 * @param String $pseudo_feature_key
2085
 *    Will be set as uuid but should be one of 'BIBLIOGRAPHY', ... more to come. See also get_feature_block_settings()
2086
 *
2087
 * @return object
2088
 *  The feature object
2089
 */
2090
function make_pseudo_feature($representation_L10n, $pseudo_feature_key = null){
2091
  $feature = new stdClass;
2092
  $feature->representation_L10n = $representation_L10n;
2093
  $feature->uuid = NULL; // $pseudo_feature_key;
2094
  $feature->label = $pseudo_feature_key;
2095
  $feature->class = 'PseudoFeature';
2096

    
2097
  return $feature;
2098

    
2099
}
2100

    
2101
/**
2102
 * @param $root_nodes, for obtaining the  root nodes from a description you can
2103
 * use the function get_root_nodes_for_dataset($description);
2104
 *
2105
 * @return string
2106
 */
2107
function render_description_string($root_nodes, &$item_cnt = 0) {
2108

    
2109
  $out = '';
2110

    
2111
  $description_strings= [];
2112
  if (!empty($root_nodes)) {
2113
    foreach ($root_nodes as $root_node) {
2114
      if(isset($root_node->descriptionElements)) {
2115
        foreach ($root_node->descriptionElements as $element) {
2116
          $feature_label = $element->feature->representation_L10n;
2117
          if($item_cnt == 0){
2118
            $feature_label = ucfirst($feature_label);
2119
          }
2120
          switch ($element->class) {
2121
            case 'CategoricalData':
2122
              $state_data = render_state_data($element);
2123
              if (!empty($state_data)) {
2124
                if(is_suppress_state_present_display($element, $root_node)){
2125
                  $description_strings[] = '<span class="'. html_class_attribute_ref($element) .'"><span class="label">' . $feature_label . '</span>: '  . '</span>';
2126
                } else {
2127
                  $description_strings[] = '<span class="'. html_class_attribute_ref($element) .'"><span class="label">' . $feature_label . '</span>: ' . $state_data . '</span>;' ;
2128
                }
2129
              }
2130
              break;
2131
            case 'QuantitativeData':
2132
              $description_strings[] = '<span class="'. html_class_attribute_ref($element) .'"><span class="label">' . $feature_label . '</span>: ' . render_quantitative_statistics($element) . '</span>;';
2133
              break;
2134
          }
2135
        }
2136
        $item_cnt++;
2137
      }
2138

    
2139
      // recurse child nodes
2140
      $child_markup = render_description_string($root_node->childNodes, $item_cnt);
2141
      if($child_markup){
2142
        $description_strings[] = $child_markup;
2143
      }
2144
    }
2145
    if(count($description_strings) > 0){
2146
      // remove last semicolon
2147
      $description_strings[count($description_strings) - 1] = preg_replace('/;$/', '', $description_strings[count($description_strings) - 1]);
2148
    }
2149
    $out  = join($description_strings,  ' ');
2150
  }
2151
  return $out;
2152
}
2153

    
2154
/**
2155
 * Compose a description as a table of Feature<->State
2156
 *
2157
 * @param $description_uuid
2158
 *
2159
 * @return array
2160
 *    The drupal render array for the page
2161
 *
2162
 * @ingroup compose
2163
 */
2164
function  compose_description_table($description_uuid, $descriptive_dataset_uuid = NULL) {
2165

    
2166
  RenderHints::pushToRenderStack('description_table');
2167

    
2168
  $render_array = [];
2169

    
2170
  $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, [$description_uuid]);
2171
  $dataSet = NULL;
2172
  // dataset passed as parameter
2173
  if ($descriptive_dataset_uuid != NULL) {
2174
    foreach ($description->descriptiveDataSets as $set) {
2175
      if ($set->uuid == $descriptive_dataset_uuid) {
2176
        $dataSet = $set;
2177
        break;
2178
      }
2179
    }
2180
  }
2181

    
2182
  if(!empty($description->descriptiveDataSets)) {
2183
    // only one dataset present
2184
    if (!isset($dataSet) && sizeof($description->descriptiveDataSets) == 1) {
2185
      foreach ($description->descriptiveDataSets as $dataSet) {
2186
        break;
2187
      }
2188
    }
2189

    
2190
    // generate description title
2191
    RenderHints::pushToRenderStack('title');
2192
    if (isset($dataSet)) {
2193

    
2194
      $described_entity_title = NULL;
2195
      if(isset($description->describedSpecimenOrObservation)){
2196
        $described_entity_title = $description->describedSpecimenOrObservation->titleCache;
2197
      } else if($description->taxon) {
2198
          $described_entity_title = render_taxon_or_name($description->taxon);
2199
      }
2200
      $title = 'Descriptive Data ' . $dataSet->titleCache .
2201
        ($described_entity_title ? ' for ' . $described_entity_title : '');
2202
    }
2203
    $render_array['title'] = markup_to_render_array($title, null, '<h3 class="title">', '</h3>');
2204
    RenderHints::popFromRenderStack();
2205
    // END of --- generate description title
2206

    
2207
    if (isset($description->types)) {
2208
      foreach ($description->types as $type) {
2209
        if ($type == 'CLONE_FOR_SOURCE') {
2210
          $render_array['source'] = markup_to_render_array("Aggregation source from " . $description->created, null, '<div class="date-created">', '</div>');
2211
          break;
2212
        }
2213
      }
2214
    }
2215
  }
2216
  // multiple datasets present see #8714 "Show multiple datasets per description as list of links"
2217
  else {
2218
    $items = [];
2219
    foreach ($description->descriptiveDataSets as $dataSet) {
2220
      $path = path_to_description($description->uuid, $dataSet->uuid);
2221
      $attributes['class'][] = html_class_attribute_ref($description);
2222
      $items[] = [
2223
        'data' => $dataSet->titleCache . icon_link($path),
2224
      ];
2225
    }
2226
    $render_array['description_elements'] = [
2227
      '#title' => 'Available data sets for description',
2228
      '#theme' => 'item_list',
2229
      '#type' => 'ul',
2230
      '#items' => $items,
2231
    ];
2232
  }
2233

    
2234
  $described_entities = [];
2235
  if (isset($description->describedSpecimenOrObservation)) {
2236
    $decr_entitiy = '<span class="label">Specimen:</span> ' . render_cdm_specimen_link($description->describedSpecimenOrObservation);
2237
    $described_entities['specimen'] = markup_to_render_array($decr_entitiy, null, '<div>', '</div>');
2238
  }
2239
  if (isset($description->taxon)) {
2240
    $decr_entitiy = '<span class="label">Taxon:</span> ' . render_taxon_or_name($description->taxon, url(path_to_taxon($description->taxon->uuid)));
2241
    $described_entities['taxon'] = markup_to_render_array($decr_entitiy, null, '<div>', '</div>');
2242
  }
2243

    
2244
  if(count($described_entities)){
2245
    $render_array['described_entities'] = $described_entities;
2246
    $render_array['described_entities']['#prefix'] = '<div class="described-entities">';
2247
    $render_array['described_entities']['#suffix'] = '</div>';
2248
  }
2249

    
2250

    
2251
  $root_nodes = get_root_nodes_for_dataset($description);
2252

    
2253

    
2254
  $rows = [];
2255
  $rows = description_element_table_rows($root_nodes, $rows);
2256

    
2257
  // --- create headers
2258
  $header = [0 => [], 1 => []];
2259

    
2260
  foreach($rows as $row) {
2261
    if(array_search('Character', $row['class']) && array_search('Character', $header[0]) === false){
2262
      $header[0][] = 'Character';
2263
    } elseif (array_search('Feature', $row['class']) && array_search('Feature', $header[0]) === false){
2264
      $header[0][] = 'Feature';
2265
    }
2266
    if(array_search('has_state', $row['class']) && array_search('States', $header[1]) === false){
2267
      $header[1][] = 'States';
2268
    } elseif (array_search('has_values', $row['class']) && array_search('Values', $header[1]) === false){
2269
      $header[1][] = 'Values';
2270
    }
2271
  }
2272
  asort($header[0]);
2273
  asort($header[1]);
2274
  $header[0] = join('/', $header[0]);
2275
  $header[1] = join('/', $header[1]);
2276

    
2277
  // ---
2278

    
2279
  if (!empty($rows)) {
2280
    $render_array['table'] = markup_to_render_array(theme('table', [
2281
      'header' => $header,
2282
      'rows' => $rows,
2283
      'caption' => statistical_values_explanation(),
2284
      'title' => "Table"
2285
    ]));
2286
  }
2287

    
2288
  // --- sources
2289
  if (isset($description->sources) and !empty($description->sources)) {
2290
    $items = [];
2291
    foreach ($description->sources as $source) {
2292
      if ($source->type == 'Aggregation' and isset($source->cdmSource)){
2293
        $cdm_source_entity = $source->cdmSource;
2294
        switch($cdm_source_entity->class){
2295
          case 'Taxon':
2296
            $source_link_markup = render_taxon_or_name($cdm_source_entity) . icon_link(path_to_taxon($cdm_source_entity->uuid, false));
2297
            break;
2298
          case 'TaxonDescription':
2299
          case 'NameDescription':
2300
          case 'SpecimenDescription':
2301
            $source_link_markup = render_cdm_entity_link($cdm_source_entity);
2302
            break;
2303
          default:
2304
            $source_link_markup = '<span class="error">Unhandled CdmSource</span>';
2305
        }
2306
        $items[$cdm_source_entity->titleCache] = [
2307
          'data' => $source_link_markup
2308
        ];
2309
      }
2310
    }
2311
    ksort($items);
2312
    $render_array['sources'] = [
2313
      '#title' => 'Sources',
2314
      '#theme' => 'item_list',
2315
      '#type' => 'ul',
2316
      '#items' => $items,
2317
      '#attributes' => ['class' => 'sources']
2318
    ];
2319
    $render_array['#prefix'] = '<div class="description-table">';
2320
    $render_array['#suffix'] = '</div>';
2321
  }
2322

    
2323
  RenderHints::popFromRenderStack();
2324

    
2325
  return $render_array;
2326
}
2327

    
2328
/**
2329
 * For a given description returns the root nodes according to the
2330
 *corresponding term tree. The term tree is determined as follow:
2331
 * 1. If description is part of a descriptive data set the term tree of that
2332
 *    data set is used (FIXME handle multiple term trees)
2333
 * 2. Otherwise the portal taxon profile tree is used
2334
 * @param $description
2335
 *
2336
 * @return array
2337
 */
2338
function get_root_nodes_for_dataset($description) {
2339
  if (!empty($description->descriptiveDataSets)) {
2340
    foreach ($description->descriptiveDataSets as $dataSet) {
2341
      break;// FIXME handle multiple term trees
2342
    }
2343
    $tree = cdm_ws_get(CDM_WS_TERMTREE, $dataSet->descriptiveSystem->uuid);
2344
    $root_nodes = _mergeFeatureTreeDescriptions($tree->root->childNodes, $description->elements);
2345
  }
2346
  else {
2347
    $root_nodes = _mergeFeatureTreeDescriptions(get_profile_feature_tree()->root->childNodes, $description->elements);
2348
  }
2349
  return $root_nodes;
2350
}
2351

    
2352
/**
2353
 * Recursively creates an array of row items to be used in theme_table.
2354
 *
2355
 * The array items will have am element 'class' with information on the
2356
 * nature of the DescriptionElement ('has_values' | 'has_state') and on the
2357
 * type of the FeatureNode ('Feature' | 'Character')
2358
 *
2359
 * @param array $root_nodes
2360
 * @param array $row_items
2361
 * @param int $level
2362
 *     the depth in the hierarchy
2363
 *
2364
 * @return array
2365
 *  An array of row items to be used in theme_table
2366
 *
2367
 *
2368
 */
2369
function description_element_table_rows($root_nodes, $row_items, $level = 0) {
2370

    
2371
  $indent_string = '&nbsp;&nbsp;&nbsp;';
2372
  foreach ($root_nodes as $root_node) {
2373
    if(isset($root_node->descriptionElements)) {
2374
      foreach ($root_node->descriptionElements as $element) {
2375
        $level_indent = str_pad('', $level * strlen($indent_string), $indent_string);
2376
        switch ($element->class) {
2377
          case 'QuantitativeData':
2378
            $row_items[] = [
2379
              'data' => [
2380
                [
2381
                  'data' => markup_to_render_array($level_indent . $element->feature->representation_L10n),
2382
                  'class' => ['level_' . $level]
2383
                ],
2384
                render_quantitative_statistics($element)
2385
              ],
2386
              'class' => ['level_' . $level, 'has_values', $element->feature->class]
2387
            ];
2388
            break;
2389
          case 'CategoricalData':
2390
            default:
2391
            if (!empty($element->stateData)) {
2392
              $supress_state_display = is_suppress_state_present_display($element, $root_node);
2393
              if(!$supress_state_display){
2394
                $state_cell = render_state_data($element);
2395
              } else {
2396
                $state_cell = "<span> </span>";
2397
              }
2398
              $row_items[] = [
2399
                'data' => [
2400
                  [
2401
                    'data' => markup_to_render_array($level_indent . $element->feature->representation_L10n),
2402
                    'class' => ['level_' . $level]
2403
                  ],
2404
                  $state_cell,
2405
                ],
2406
                'class' => ['level_' . $level, 'has_state', $element->feature->class]
2407
              ];
2408
            }
2409
            break;
2410
        }
2411
      }
2412
    }
2413
    // recurse child nodes
2414
    $row_items = description_element_table_rows($root_node->childNodes, $row_items, $level + 1);
2415
  }
2416
  return $row_items;
2417
}
2418

    
2419
/**
2420
 * @param $element
2421
 * @param $root_node
2422
 *
2423
 * @return bool
2424
 */
2425
function is_suppress_state_present_display($element, $root_node) {
2426
  return count($element->stateData) == 1 & $element->stateData[0]->state->representation_L10n == 'present' && is_array($root_node->childNodes);
2427
}
2428

    
2429

    
2430

    
(3-3/13)