Project

General

Profile

Download (85.9 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
    usort($entity->sources, 'compare_original_sources');
562

    
563
    if ($config['sources_as_content'] == 1) {
564
      foreach ($entity->sources as $source) {
565
        if (_is_original_source_type($source)) {
566
          $reference_citation = render_original_source(
567
            $source,
568
            $config['link_to_reference'] == 1,
569
            $config['link_to_name_used_in_source'] == 1
570
          );
571

    
572
          if ($reference_citation) {
573
            if (empty($inline_text_prefix)) {
574
              $annotations_and_sources['source_references'][] = $reference_citation;
575
            } else {
576
              $annotations_and_sources['source_references'][] = ' (' . $reference_citation . ')';
577
            }
578
          }
579

    
580
          // also put the name in source into the array, these are already included in the $reference_citation but are
581
          // still required to be available separately in some contexts.
582
          $name_in_source_render_array = compose_name_in_source(
583
            $source,
584
            $config['link_to_name_used_in_source'] == 1
585
          );
586

    
587
          if (!empty($name_in_source_render_array)) {
588
            $annotations_and_sources['names_used_in_source'][$name_in_source_render_array['#_plaintext']] = drupal_render($name_in_source_render_array);
589
          }
590
        }
591
      } // END of loop over sources
592

    
593
    // annotations footnotes separate.
594
    $annotations_and_sources['foot_note_keys'] = theme('cdm_annotations_as_footnotekeys',
595
      array(
596
        'cdmBase_list' => $entity,
597
        'footnote_list_key' => $footnote_list_key_suggestion,
598
      )
599
    );
600

    
601
    } // END of references inline
602

    
603
    // footnotes for sources and annotations or put into into bibliography if requested ...
604
    if ($config['add_footnote_keys'] == 1) {
605
        $annotations_and_sources['foot_note_keys'] = cdm_create_footnotes(
606
          $entity, ',',
607
          $footnote_list_key_suggestion,
608
          $config['link_to_reference'] == 1,
609
          $config['link_to_name_used_in_source'] == 1,
610
          !empty($config['bibliography_aware'])
611
        );
612
    }
613

    
614
    return $annotations_and_sources;
615
  }
616

    
617

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

    
631
   *
632
   * @return string
633
   *  the footnote_list_key
634
   */
635
  function original_source_footnote_list_key($key_suggestion = null) {
636
    if(!$key_suggestion){
637
      $key_suggestion = RenderHints::getFootnoteListKey();
638
    }
639
    $bibliography_settings = get_bibliography_settings();
640
    $footnote_list_key = $bibliography_settings['enabled'] == 1 ? BIBLIOGRAPHY_FOOTNOTE_KEY : BIBLIOGRAPHY_FOOTNOTE_KEY . '-' . $key_suggestion;
641
    return $footnote_list_key;
642
  }
643

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

    
667

    
668
/* ==================== COMPOSE FUNCTIONS =============== */
669

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

    
693
    $block_list = array();
694

    
695
    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME);
696

    
697
    $use_description_features = array(UUID_USE);
698

    
699

    
700
    RenderHints::pushToRenderStack('feature_block');
701
    // Create a drupal block for each feature
702
    $block_weight = - FEATURE_BLOCK_WEIGHT_INCREMENT;
703
    foreach ($mergedFeatureNodes as $feature_node) {
704

    
705
      $block_weight = $block_weight + FEATURE_BLOCK_WEIGHT_INCREMENT;
706

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

    
710
        RenderHints::pushToRenderStack($feature_node->term->uuid);
711
          
712
        $feature_name = cdm_term_representation($feature_node->term, 'Unnamed Feature');
713
        $feature_block_settings = get_feature_block_settings($feature_node->term->uuid);
714
        
715

    
716
        $block = feature_block($feature_name, $feature_node->term);
717
        $block->content = array();
718
        $block_content_is_empty = TRUE;
719

    
720
        if(array_search($feature_node->term->uuid, $use_description_features) !== false) {
721
          // do not show features which belong to the UseDescriptions, these are
722
          // handled by compose_feature_block_items_use_records() where the according descriptions are
723
          // fetched again separately.
724
          // UseDescriptions are a special feature introduced for palmweb
725
          continue;
726
        }
727

    
728
        /*
729
         * Content/DISTRIBUTION.
730
         */
731
        if ($feature_node->term->uuid == UUID_DISTRIBUTION) {
732
          $block = compose_feature_block_distribution($taxon, $feature_node->descriptionElements, $feature_node->term);
733
          $block_content_is_empty = FALSE;
734
        }
735

    
736
        /*
737
         * Content/COMMON_NAME.
738
         */
739
        else if ($feature_node->term->uuid == UUID_COMMON_NAME) {
740
          $common_names_render_array = compose_feature_block_items_feature_common_name($feature_node->descriptionElements, $feature_node->term);
741
          $block->content[] = $common_names_render_array;
742
          $block_content_is_empty = FALSE;
743
        }
744

    
745
        /*
746
         * Content/Use Description (Use + UseRecord)
747
         */
748
        else if ($feature_node->term->uuid == UUID_USE_RECORD) {
749
          $block->content[] = cdm_block_use_description_content($taxon->uuid, $feature_node->term);
750
          $block_content_is_empty = FALSE;
751
        }
752

    
753
        /*
754
         * Content/ALL OTHER FEATURES.
755
         */
756
        else {
757

    
758
          $media_list = array();
759
          $elements_render_array = array();
760
          $child_elements_render_array = null;
761

    
762
          if (isset($feature_node->descriptionElements[0])) {
763
            $elements_render_array = compose_feature_block_items_generic($feature_node->descriptionElements, $feature_node->term);
764
          }
765

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

    
787
        // allows modifying the block contents via a the hook_cdm_feature_node_block_content_alter
788
        drupal_alter('cdm_feature_node_block_content', $block->content, $feature_node->term, $feature_node->descriptionElements);
789

    
790
        if(!$block_content_is_empty){ // skip empty block content
791
          $block_list[$block_weight] = $block;
792
          cdm_toc_list_add_item(cdm_term_representation($feature_node->term), $feature_node->term->uuid, null, FALSE, $block_weight);
793
        } // END: skip empty block content
794
      } // END: skip empty or suppressed features
795
      RenderHints::popFromRenderStack();
796
    } // END: creating a block per feature
797

    
798
    RenderHints::popFromRenderStack();
799

    
800
    drupal_alter('cdm_feature_node_blocks', $block_list, $taxon);
801

    
802
    ksort($block_list);
803

    
804
    return  $block_list;
805
  }
806

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

    
829
  if(!$main_feature){
830
    $main_feature = $feature_node->term;
831
  }
832
  /*
833
   * TODO should be configurable, options; YES, NO, AUTOMATIC
834
   * (automatic will only place the label if the first word of the description element text is not the same)
835
   */
836
  $prepend_feature_label = false;
837

    
838
  $render_arrays = array();
839
  foreach ($feature_node->childNodes as $child_node) {
840
    if (isset($child_node->descriptionElements[0])) {
841
      foreach ($child_node->descriptionElements as $element) {
842

    
843
        if (isset($element->media[0])) {
844
          // Append media of subordinate elements to list of main
845
          // feature.
846
          $media_list = array_merge($media_list, $element->media);
847
        }
848

    
849
        $child_node_element = null;
850
        switch ($element->class) {
851
          case 'TextData':
852
            $child_node_element = compose_description_element_text_data($element, $element->feature->uuid, $feature_block_settings, $prepend_feature_label);
853
            break;
854
          case 'CategoricalData':
855
            $child_node_element = compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label);
856
            break;
857
          case 'QuantitativeData':
858
            $child_node_element = compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label);
859

    
860
        }
861
        if (is_array($child_node_element)) {
862
          $render_arrays[] = $child_node_element;
863
        }
864
      }
865
    }
866

    
867
    if(isset($child_node->childNodes[0])){
868
      $render_arrays = array_merge($render_arrays, compose_feature_block_items_nested($child_node, $media_list, $feature_block_settings, $main_feature ));
869
    }
870
  }
871

    
872
  return $render_arrays;
873
}
874

    
875
  /**
876
   *
877
   * @param $feature_node
878
   *  The merged feature three node which potentially contains media in its description elements.
879
   * @param $media_list
880
   *    Additional media to be merged witht the media contained in the nodes description elements
881
   * @param $gallery_settings
882
   * @return array
883
   *
884
   * @ingroup compose
885
   */
886
  function compose_feature_media_gallery($feature_node, $media_list, $gallery_settings) {
887

    
888
    if (isset($feature_node->descriptionElements)) {
889
      $media_list = array_merge($media_list, cdm_dataportal_media_from_descriptionElements($feature_node->descriptionElements));
890
    }
891

    
892
    $captionElements = array('title', 'rights');
893

    
894
    if (isset($media_list[0]) && isset($gallery_settings['cdm_dataportal_media_maxextend']) && isset($gallery_settings['cdm_dataportal_media_cols'])) {
895
      $gallery = compose_cdm_media_gallerie(array(
896
        'mediaList' => $media_list,
897
        'galleryName' => CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME . '_' . $feature_node->term->uuid,
898
        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
899
        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
900
        'captionElements' => $captionElements,
901
      ));
902
      return markup_to_render_array($gallery);
903
    }
904

    
905
    return markup_to_render_array('');
906
  }
907

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

    
929
    $distributionElements = NULL;
930
    $distribution_info_dto = NULL;
931
    $distribution_sortOutArray = FALSE;
932

    
933
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
934

    
935
    // TODO use feature_block_settings instead of DISTRIBUTION_ORDER_MODE
936
    if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) != 'TREE') {
937
      $distribution_glue = '';
938
      $distribution_enclosingTag = 'dl';
939
    } else {
940
      $distribution_glue = '';
941
      $distribution_enclosingTag = 'ul';
942
    }
943

    
944
    if (!isset($descriptionElements['#type']) || !$descriptionElements['#type'] == 'DTO') {
945
      // skip the DISTRIBUTION section if there is no DTO type element
946
      return array(); // FIXME is it ok to return an empty array?
947
    }
948

    
949
    $block = feature_block(
950
      cdm_term_representation($feature, 'Unnamed Feature'),
951
      $feature
952
    );
953
    $block->content = array();
954

    
955
    // $$descriptionElements['TextData'] is added to to the feature node in merged_taxon_feature_tree()
956
    if (isset($descriptionElements['TextData'])) {
957
      // --- TextData
958
      foreach ($descriptionElements['TextData'] as $text_data_element) {
959
        $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
960
        $repr = drupal_render($text_data_render_array);
961

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

    
975

    
976
    if ($text_data_out_array && variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
977
      $block->content[] = compose_feature_block_wrap_elements(
978
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
979
      );
980
    }
981

    
982
    // --- Distribution map
983
    $distribution_map_query_parameters = NULL;
984

    
985
    $map_visibility = variable_get(DISTRIBUTION_MAP_VISIBILITY, DISTRIBUTION_MAP_VISIBILITY_DEFAULT);
986
    if(variable_get(DISTRIBUTION_MAP_VISIBILITY, DISTRIBUTION_MAP_VISIBILITY_DEFAULT) == 'always' ||
987
        $map_visibility == 'automatic' && isset($descriptionElements['DistributionInfoDTO']->mapUriParams)
988
      )
989
    {
990
      $distribution_map_query_parameters = $descriptionElements['DistributionInfoDTO']->mapUriParams;
991
    }
992
    $map_render_element = compose_distribution_map($distribution_map_query_parameters);
993
    $block->content[] = $map_render_element;
994

    
995
    $dto_out_array = array();
996

    
997
    // --- Condensed Distribution
998
    if(variable_get(DISTRIBUTION_CONDENSED) && isset($descriptionElements['DistributionInfoDTO']->condensedDistribution)){
999
      $condensed_distribution_markup = '<p class="condensed_distribution">';
1000

    
1001
      $isFirst = true;
1002
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous[0])){
1003
        foreach($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous as $cdItem){
1004
          if(!$isFirst){
1005
            $condensed_distribution_markup .= ' ';
1006
          }
1007
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->idInVocabulary . '">'
1008
          . $cdItem->areaStatusLabel . '</span>';
1009
          $isFirst = false;
1010
        }
1011
      }
1012

    
1013
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign[0])) {
1014
        if(!$isFirst){
1015
          $condensed_distribution_markup .= ' ';
1016
        }
1017
        $isFirst = TRUE;
1018
        $condensed_distribution_markup .= '[';
1019
        foreach ($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign as $cdItem) {
1020
          if (!$isFirst) {
1021
            $condensed_distribution_markup .= ' ';
1022
          }
1023
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->titleCache . '">'
1024
            . $cdItem->areaStatusLabel . '</span>';
1025
          $isFirst = false;
1026
        }
1027
        $condensed_distribution_markup .= ']';
1028
      }
1029

    
1030
      $condensed_distribution_markup .= '&nbsp;' . l(
1031
          font_awesome_icon_markup(
1032
            'fa-info-circle',
1033
            array(
1034
              'alt'=>'help',
1035
              'class' => array('superscript')
1036
            )
1037
          ),
1038
          variable_get(DISTRIBUTION_CONDENSED_INFO_PATH, DISTRIBUTION_CONDENSED_INFO_PATH_DEFAULT) ,
1039
          array('html' => TRUE));
1040
      $condensed_distribution_markup .= '</p>';
1041
      $dto_out_array[] = $condensed_distribution_markup;
1042
    }
1043

    
1044
    // --- tree or list
1045
    if (isset($descriptionElements['DistributionInfoDTO'])) {
1046
      $distribution_info_dto = $descriptionElements['DistributionInfoDTO'];
1047

    
1048
      // --- tree
1049
      if (is_object($distribution_info_dto->tree)) {
1050
        $distribution_tree_render_array = compose_distribution_hierarchy($distribution_info_dto->tree, $feature_block_settings);
1051
        $dto_out_array[] = $distribution_tree_render_array;
1052
      }
1053

    
1054
      // --- sorted element list
1055
      if (is_array($distribution_info_dto->elements) && count($distribution_info_dto->elements) > 0) {
1056
        foreach ($distribution_info_dto->elements as $descriptionElement) {
1057
          if (is_object($descriptionElement->area)) {
1058
            $sortKey = $descriptionElement->area->representation_L10n;
1059
            $distributionElements[$sortKey] = $descriptionElement;
1060
          }
1061
        }
1062
        ksort($distributionElements);
1063
        $distribution_element_render_array = compose_description_elements_distribution($distributionElements);
1064
        $dto_out_array[] = $distribution_element_render_array;
1065

    
1066
      }
1067
      //
1068
      $block->content[] = compose_feature_block_wrap_elements(
1069
        $dto_out_array, $feature, $distribution_glue, $distribution_sortOutArray
1070
      );
1071
    }
1072

    
1073
    // --- TextData at the bottom
1074
    if ($text_data_out_array && !variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
1075
      $block->content[] = compose_feature_block_wrap_elements(
1076
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
1077
      );
1078
    }
1079

    
1080
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . UUID_DISTRIBUTION)));
1081
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
1082
    $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
1083

    
1084
    return $block;
1085
  }
1086

    
1087

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

    
1109
    $footnote_list_key_suggestion = $feature_uuid;
1110

    
1111
    $element_markup = '';
1112
    if (isset($element->multilanguageText_L10n->text)) {
1113
      // TODO replacement of \n by <br> should be configurable
1114
      $element_markup = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
1115
    }
1116

    
1117
    $render_array = compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label);
1118

    
1119
    return $render_array;
1120
  }
1121

    
1122

    
1123
/**
1124
 * Theme function to render CDM DescriptionElements of the type TaxonInteraction.
1125
 *
1126
 * @param $element
1127
 *  The CDM TaxonInteraction entity
1128
 *
1129
 * @return
1130
 *  A drupal render array
1131
 *
1132
 * @ingroup compose
1133
 */
1134
function compose_description_element_taxon_interaction($element, $feature_block_settings) {
1135

    
1136
  $out = '';
1137

    
1138

    
1139
  if (isset($element->description_L10n)) {
1140
    $out .=  ' ' . $element->description_L10n;
1141
  }
1142

    
1143
  if(isset($element->taxon2)){
1144
    $out = render_taxon_or_name($element->taxon2, url(path_to_taxon($element->taxon2->uuid)));
1145
  }
1146

    
1147
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1148

    
1149
  return $render_array;
1150
}
1151

    
1152

    
1153
/**
1154
 * Renders a single instance of the type IndividualsAssociations.
1155
 *
1156
 * @param $element
1157
 *   The CDM IndividualsAssociations entity.
1158
 * @param $feature_block_settings
1159
 *
1160
 * @return array
1161
 *   Drupal render array
1162
 *
1163
 * @ingroup compose
1164
 */
1165
function compose_description_element_individuals_association($element, $feature_block_settings) {
1166

    
1167
  $out = '';
1168

    
1169
  $render_array = compose_cdm_specimen_or_observation($element->associatedSpecimenOrObservation);
1170

    
1171
  if (isset($element->description_L10n)) {
1172
    $out .=  ' ' . $element->description_L10n;
1173
  }
1174

    
1175
  $out .= drupal_render($render_array);
1176

    
1177
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1178

    
1179
  return $render_array;
1180
}
1181

    
1182
/**
1183
 * Renders a single instance of the type CategoricalData.
1184
 *
1185
 * @param $element
1186
 *  The CDM CategoricalData entity
1187
 *
1188
 * @param $feature_block_settings
1189
 *
1190
 * @param bool $prepend_feature_label
1191
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1192
 *
1193
 * @return array
1194
 *   a drupal render array for given CategoricalData element
1195
 *
1196
 * @ingroup compose
1197
 */
1198
function compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1199

    
1200
  $state_data_markup = render_state_data($element);
1201

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

    
1204
  return $render_array;
1205
}
1206

    
1207
/**
1208
 * @param $element
1209
 *
1210
 * @return string
1211
 * the markup
1212
 */
1213
function render_state_data($element) {
1214

    
1215
  $state_data_items = [];
1216

    
1217
  if (isset($element->stateData)) {
1218
    foreach ($element->stateData as $state_data) {
1219

    
1220
      $state = NULL;
1221

    
1222
      if (isset($state_data->state)) {
1223
        $state = cdm_term_representation($state_data->state);
1224

    
1225
          $sample_count = 0;
1226
          if (isset($state_data->count)) {
1227
            $sample_count = $state_data->count;
1228
            $state .= ' [' . $state_data->count . ']';
1229
          }
1230
    
1231
          if (isset($state_data->modifyingText_L10n)) {
1232
            $state .= ' ' . $state_data->modifyingText_L10n;
1233
          }
1234
    
1235
          $modifiers_strings = cdm_modifers_representations($state_data);
1236
          $state_data_markup = $state . ($modifiers_strings ? ' ' . $modifiers_strings : '');
1237
          // we could use strip_tags() to reduce the markup to text for the key but this is expensive
1238
          $sort_key = str_pad($sample_count, 6, '0', STR_PAD_LEFT) . '_' . $state_data_markup;
1239
          $state_data_items[$sort_key] = $state_data_markup;
1240
      }
1241

    
1242
    }
1243
    krsort($state_data_items);
1244
    $out = '<span class="' . html_class_attribute_ref($element) . '">' . join(', ', $state_data_items) .  '</span>';
1245
  }
1246
  return $out;
1247
}
1248

    
1249

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

    
1293
  $out = render_quantitative_statistics($element);
1294

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

    
1297
  return $render_array;
1298
}
1299

    
1300
/**
1301
 * Composes the HTML for quantitative statistics
1302
 * @param $element
1303
 *
1304
 * @return string
1305
 */
1306
function render_quantitative_statistics($element) {
1307

    
1308
  $out = '';
1309
  $type_representation = NULL;
1310
  $min_max = statistical_values_array();
1311
  $sample_size_markup = null;
1312

    
1313
  if (isset($element->statisticalValues)) {
1314
    $out = '<span class=\"' . html_class_attribute_ref($element) . '\">';
1315
    $other_values_markup = [];
1316
    foreach ($element->statisticalValues as $statistical_val) {
1317

    
1318
      // compile the full value string which also may contain modifiers
1319
      if (isset($statistical_val->value)) {
1320
        $statistical_val->_value = $statistical_val->value;
1321
      }
1322
      $val_modifiers_strings = cdm_modifers_representations($statistical_val);
1323
      if ($val_modifiers_strings) {
1324
        $statistical_val->_value = ' ' . $val_modifiers_strings . ' ' . $statistical_val->_value;
1325
      }
1326

    
1327
      // either put into min max array or into $other_values
1328
      // for generic output to be appended to 'min-max' string
1329
      if (array_key_exists(statistical_measure_term2min_max_key($statistical_val->type), $min_max)) {
1330
        $min_max[statistical_measure_term2min_max_key($statistical_val->type)] = $statistical_val;
1331
      }
1332
      else {
1333
        drupal_set_message("Unsupported statistical value type: " . $statistical_val->type->uuid, "error");
1334
      }
1335
    } // end of loop over statisticalValues
1336

    
1337
    // create markup
1338
    $unit = null;
1339
    if (isset($element->unit)) {
1340
      $unit = ' <span class="unit" title="'
1341
        . cdm_term_representation($element->unit) . '">'
1342
        . cdm_term_representation_abbreviated($element->unit)
1343
        . '</span>';
1344
    }
1345
    $min_max_markup = statistical_values($min_max, $unit);
1346
    $out .= $min_max_markup . '</span>';
1347
  }
1348

    
1349
  if($sample_size_markup){
1350
    $out .= ' ' . $sample_size_markup;
1351

    
1352
  }
1353

    
1354
  // modifiers of the description element itself
1355
  $modifier_string = cdm_modifers_representations($element);
1356
  $out .= ($modifier_string ? ' ' . $modifier_string : '');
1357
  if (isset($element->modifyingText_L10n)) {
1358
    $out = $element->modifyingText_L10n . ' ' . $out;
1359
  }
1360
  return $out;
1361
}
1362

    
1363
function statistical_measure_term2min_max_key($term){
1364
  static $uuid2key = [
1365
    UUID_STATISTICALMEASURE_MIN => 'Min',
1366
    UUID_STATISTICALMEASURE_MAX => 'Max',
1367
    UUID_STATISTICALMEASURE_AVERAGE => 'Average',
1368
    UUID_STATISTICALMEASURE_SAMPLESIZE => 'SampleSize',
1369
    UUID_STATISTICALMEASURE_VARIANCE => 'Variance',
1370
    UUID_STATISTICALMEASURE_TYPICALLOWERBOUNDARY => 'TypicalLowerBoundary',
1371
    UUID_STATISTICALMEASURE_TYPICALUPPERBOUNDARY => 'TypicalUpperBoundary',
1372
    UUID_STATISTICALMEASURE_STANDARDDEVIATION => 'StandardDeviation',
1373
    UUID_STATISTICALMEASURE_EXACTVALUE => 'ExactValue',
1374
    UUID_STATISTICALMEASURE_STATISTICALMEASUREUNKNOWNDATA => 'StatisticalMeasureUnknownData'
1375
  ];
1376
  return $uuid2key[$term->uuid];
1377
}
1378

    
1379

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

    
1403
    $feature_block_settings = get_feature_block_settings($feature->uuid);
1404
    $enclosing_tag = $feature_block_settings['as_list'];
1405

    
1406
    if ($sort) { // TODO remove parameter and replace by $feature_block_settings['sort_elements']
1407
      usort($description_element_render_arrays, 'compare_description_element_render_arrays');
1408
    }
1409

    
1410
    $is_first = true;
1411
    foreach($description_element_render_arrays as &$element_render_array){
1412
      if(!is_array($element_render_array)){
1413
        $element_render_array = markup_to_render_array($element_render_array);
1414
      }
1415
      $element_render_array['#attributes']['class'][] = "feature-block-element";
1416

    
1417
      // add the glue!
1418
      if(!$is_first) {
1419
        if (isset($element_render_array['#value'])) {
1420
          $element_render_array['#value'] = $glue . $element_render_array['#value'];
1421
        } elseif (isset($element_render_array['#markup'])) {
1422
          $element_render_array['#markup'] = $glue . $element_render_array['#markup'];
1423
        }
1424
      }
1425
      $is_first = false;
1426
    }
1427

    
1428
    $render_array['elements']['children'] = $description_element_render_arrays;
1429

    
1430
    $render_array['elements']['#prefix'] = '<' . $enclosing_tag . ' class="feature-block-elements" id="' . $feature->representation_L10n . '">';
1431
    $render_array['elements']['#suffix'] = '</' . $enclosing_tag . '>';
1432

    
1433
    return $render_array;
1434
  }
1435

    
1436

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

    
1454
    $plaintext = NULL;
1455
    $markup = NULL;
1456
    $name_in_source_render_array = array();
1457

    
1458
    static $taxon_page_accepted_name = '';
1459
    $taxon_uuid = get_current_taxon_uuid();
1460
    if($suppress_for_shown_taxon && $taxon_uuid && empty($taxon_page_accepted_name)){
1461

    
1462
      $current_taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $taxon_uuid);
1463
      $taxon_page_accepted_name = $current_taxon->name->titleCache;
1464
    }
1465

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

    
1496
    if ($plaintext) { // checks if we have any content
1497
      $name_in_source_render_array = markup_to_render_array($markup);
1498
      $name_in_source_render_array['#_plaintext'] = $plaintext;
1499
    }
1500

    
1501
    return $name_in_source_render_array;
1502
  }
1503

    
1504

    
1505

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

    
1527
    $elements_out_array = array();
1528
    $distribution_tree = null;
1529

    
1530
    /*
1531
     * $feature_block_has_content will be set true if at least one of the
1532
     * $descriptionElements contains some text which makes up some content
1533
     * for the feature block. Footnote keys are not considered
1534
     * to be content in this sense.
1535
     */
1536
    $feature_block_has_content = false;
1537

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

    
1579
      // If feature = CITATION sort the list of sources.
1580
      // This is ONLY for FLORA MALESIANA and FLORE d'AFRIQUE CENTRALE.
1581
      if (isset($description_element) && $description_element->feature->uuid == UUID_CITATION) {
1582
        sort($elements_out_array);
1583
      }
1584
    }
1585

    
1586
    // sanitize: remove empty and NULL items from the render array
1587
    $tmp_out_array = $elements_out_array;
1588
    $elements_out_array = array();
1589
    foreach($tmp_out_array as $item){
1590
      if(is_array($item) && count($item) > 0){
1591
        $elements_out_array[] = $item;
1592
      }
1593
    }
1594

    
1595
    return $elements_out_array;
1596
  }
1597

    
1598
/**
1599
 * Composes block of common names for the given DescriptionElements $elements which must be of the feature CommonName
1600
 *
1601
 * @parameter $elements
1602
 *  an array of CDM DescriptionElements either of type CommonName or TextData
1603
 * @parameter $feature
1604
 *  the common feature of all $elements, must be CommonName
1605
 *
1606
 * @return
1607
 *   A drupal render array
1608
 *
1609
 * @ingroup compose
1610
 */
1611
function compose_feature_block_items_feature_common_name($elements, $feature, $weight = FALSE) {
1612

    
1613
  $common_name_out = '';
1614
  $common_name_feature_elements = array();
1615
  $textData_commonNames = array();
1616

    
1617
  $footnote_key_suggestion = 'common-names-feature-block';
1618

    
1619
  $feature_block_settings = get_feature_block_settings(UUID_COMMON_NAME);
1620

    
1621
  if (is_array($elements)) {
1622
    foreach ($elements as $element) {
1623

    
1624
      if ($element->class == 'CommonTaxonName') {
1625

    
1626
        // common name without a language or area, should not happen but is possible
1627
        $language_area_key = '';
1628
        if (isset($element->language->representation_L10n)) {
1629
          $language_area_key .= $element->language->representation_L10n ;
1630
        }
1631
        if(isset($element->area->titleCache) && strlen($element->area->titleCache) > 0){
1632
          $language_area_key .= ($language_area_key ? ' '  : '') . '(' . $element->area->titleCache . ')';
1633
        }
1634
        if($language_area_key){
1635
          $language_area_key = '<span class="language-area-label">' . $language_area_key . '<span class="separator">: </span></span>';
1636
        }
1637

    
1638
        if(isset($common_names[$language_area_key][$element->name])) {
1639
          // same name already exists for language and area combination, se we merge the description elements
1640
          cdm_merge_description_elements($common_names[$language_area_key][$element->name], $element);
1641
        } else{
1642
          // otherwise add as new entry
1643
          $common_names[$language_area_key][$element->name] = $element;
1644
        }
1645

    
1646
      }
1647
      elseif ($element->class == 'TextData') {
1648
        $textData_commonNames[] = $element;
1649
      }
1650
    }
1651
  }
1652
  // Handling common names.
1653
  if (isset($common_names) && count($common_names) > 0) {
1654
    // Sorting the array based on the key (language, + area if set).
1655
    // Comment @WA there are common names without a language, so this sorting
1656
    // can give strange results.
1657
    ksort($common_names);
1658

    
1659
    // loop over set of elements per language area
1660
    foreach ($common_names as $language_area_key => $elements) {
1661
      ksort($elements); // sort names alphabetically
1662
      $per_language_area_out = array();
1663

    
1664
      foreach ($elements as $element) {
1665
        $common_name_render_array = compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion);
1666
        $common_name_markup = drupal_render($common_name_render_array);
1667
        // IMPORTANT!
1668
        // during the above drupal_render the theme_html_tag function is executed, which adds a "\n" character to the end of the markup
1669
        // this is an error and the trailing whitespace needs to be removed
1670
        if(str_endsWith($common_name_markup, "\n")){
1671
          $common_name_markup = substr($common_name_markup, 0, strlen($common_name_markup) - 1);
1672
        }
1673
        $per_language_area_out[] = $common_name_markup;
1674
      }
1675

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

    
1679

    
1680
    $common_name_feature_elements_render_array = compose_feature_block_wrap_elements(
1681
      $common_name_feature_elements, $feature, '; ', FALSE
1682
    );
1683
    $common_name_out .= drupal_render($common_name_feature_elements_render_array); // FIXME should this be a render array instead?
1684

    
1685
  }
1686

    
1687
  // Handling commons names as text data.
1688
  $text_data_out = array();
1689

    
1690
  foreach ($textData_commonNames as $text_data_element) {
1691
    /* footnotes are not handled correctly in compose_description_element_text_data,
1692
       need to set 'common-names-feature-block' as $footnote_key_suggestion */
1693
    RenderHints::setFootnoteListKey($footnote_key_suggestion);
1694
    $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
1695
    $text_data_out[] = drupal_render($text_data_render_array);
1696
  }
1697

    
1698
  $common_name_out_text_data = compose_feature_block_wrap_elements(
1699
    $text_data_out, $feature
1700
  );
1701

    
1702
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . $footnote_key_suggestion));
1703
  $footnotes .= theme('cdm_footnotes', array('footnoteListKey' => $footnote_key_suggestion)); // FIXME is this needed at all?
1704
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnote_key_suggestion));
1705

    
1706
  return  markup_to_render_array(  // FIXME markup_to_render_array should no longer be needed
1707
    '<div class="common-taxon-name">' . $common_name_out . '</div>'
1708
    .'<div class="text-data">' . drupal_render($common_name_out_text_data) . '</div>'
1709
    .$footnotes,
1710
    $weight
1711
  );
1712
}
1713

    
1714
/**
1715
 * Renders a single instance of the type CommonTaxonName.
1716
 *
1717
 * @param $element
1718
 *   The CDM CommonTaxonName entity.
1719
 * @param $feature_block_settings
1720
 *
1721
 * @param $footnote_key_suggestion
1722
 *
1723
 * @param $element_tag_name
1724
 *
1725
 * @return array
1726
 *   Drupal render array
1727
 *
1728
 * @ingroup compose
1729
 */
1730
function compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion = NULL)
1731
{
1732

    
1733
  if(!$footnote_key_suggestion) {
1734
    $footnote_key_suggestion = $element->feature->uuid;
1735
  }
1736

    
1737
  $name = '';
1738
  if(isset($element->name)){
1739
    $name = $element->name;
1740
  }
1741

    
1742

    
1743
  return compose_description_element($element, $feature_block_settings, $name, $footnote_key_suggestion);
1744
}
1745

    
1746
/**
1747
 * Composes the render array for a CDM Distribution description element
1748
 *
1749
 * @param array $description_elements
1750
 *   Array of CDM Distribution instances
1751
 * @param $enclosingTag
1752
 *   The html tag to be use for the enclosing element
1753
 *
1754
 * @return array
1755
 *   A Drupal render array
1756
 *
1757
 * @ingroup compose
1758
 */
1759
function compose_description_elements_distribution($description_elements){
1760

    
1761
  $markup_array = array();
1762
  RenderHints::pushToRenderStack('descriptionElementDistribution');
1763
  RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1764

    
1765
  $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
1766
  $enclosingTag = cdm_feature_block_element_tag_name($feature_block_settings);
1767

    
1768
  foreach ($description_elements as $description_element) {
1769
    $annotations_and_sources = handle_annotations_and_sources(
1770
      $description_element,
1771
      handle_annotations_and_sources_config($feature_block_settings),
1772
      $description_element->area->representation_L10n,
1773
      UUID_DISTRIBUTION
1774
    );
1775

    
1776

    
1777
    list($status_label, $status_markup) = distribution_status_label_and_markup($description_element);
1778

    
1779
    $out = '';
1780
    $out .= '<' . $enclosingTag . ' class="descriptionElement descriptionElement-' . $description_element->uuid
1781
      . ' " title="' . $status_label. '">'
1782
      . $description_element->area->representation_L10n
1783
      . $status_markup;
1784
    if(!empty($annotations_and_sources['source_references'])){
1785
      $out .= ' ' . join(' ', $annotations_and_sources['source_references'] );
1786
    }
1787
    $out .= $annotations_and_sources['foot_note_keys']   . '</' . $enclosingTag . '>';
1788
    $markup_array[] = $out;
1789
  }
1790

    
1791
  RenderHints::popFromRenderStack();
1792
  return markup_to_render_array(join('<span class="separator">' . $feature_block_settings['glue'] . '</span>', $markup_array));
1793
}
1794

    
1795
  /**
1796
   * @param $descriptionElement
1797
   * @return array
1798
   */
1799
  function distribution_status_label_and_markup($descriptionElement, $status_glue = '&#8210; ') {
1800
    $status_markup = '';
1801
    $status_label = '';
1802

    
1803
    if (isset($descriptionElement->status)) {
1804
      $status_label = $descriptionElement->status->representation_L10n;
1805
      $status_markup =  '<span class="distributionStatus"> '
1806
        . $status_glue
1807
        . '<span class="distributionStatus-' . $descriptionElement->status->idInVocabulary . '">'
1808
        . $status_label
1809
        . '</span></span>';
1810

    
1811
    };
1812
    return array($status_label, $status_markup);
1813
  }
1814

    
1815

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

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

    
1839

    
1840
    // 2. find the distribution feature node
1841
    $distribution_node =& cdm_feature_tree_find_node($merged_tree->root->childNodes, UUID_DISTRIBUTION);
1842

    
1843
    if ($distribution_node) {
1844
      // 3. get the distributionInfoDTO
1845
      $query_parameters = cdm_distribution_filter_query();
1846
      $query_parameters['part'] = array('mapUriParams');
1847
      if(variable_get(DISTRIBUTION_CONDENSED)){
1848
        $query_parameters['part'][] = 'condensedDistribution';
1849
      }
1850
      if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) == 'TREE') {
1851
        $query_parameters['part'][] = 'tree';
1852
      }
1853
      else {
1854
        $query_parameters['part'][] = 'elements';
1855
      }
1856
      $query_parameters['omitLevels'] = array();
1857
      foreach(variable_get(DISTRIBUTION_TREE_OMIT_LEVELS, array()) as $uuid){
1858
        if(is_uuid($uuid)){
1859
          $query_parameters['omitLevels'][] = $uuid;
1860
        }
1861
      }
1862
      $customStatusColorsJson = variable_get(DISTRIBUTION_STATUS_COLORS, NULL);
1863
      if ($customStatusColorsJson) {
1864
        $query_parameters['statusColors'] = $customStatusColorsJson;
1865
      }
1866

    
1867
      $distribution_info_dto = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION_DISTRIBUTION_INFO_FOR, $taxon->uuid, queryString($query_parameters));
1868
      // 4. get distribution TextData is there are any
1869
      $distribution_text_data = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON,
1870
        array(
1871
          'taxon' => $taxon->uuid,
1872
          'type' => 'TextData',
1873
          'features' => UUID_DISTRIBUTION
1874
        )
1875
      );
1876

    
1877
      // 5. put all distribution data into the distribution feature node
1878
      if ($distribution_text_data //if text data exists
1879
        || ($distribution_info_dto && isset($distribution_info_dto->tree) && $distribution_info_dto->tree->rootElement->numberOfChildren > 0) // OR if tree element has distribution elements
1880
        || ($distribution_info_dto && !empty($distribution_info_dto->elements))
1881
      ) { // OR if DTO has distribution elements
1882
        $distribution_node->descriptionElements = array('#type' => 'DTO');
1883
        if ($distribution_text_data) {
1884
          $distribution_node->descriptionElements['TextData'] = $distribution_text_data;
1885
        }
1886
        if ($distribution_info_dto) {
1887
          $distribution_node->descriptionElements['DistributionInfoDTO'] = $distribution_info_dto;
1888
        }
1889
      }
1890
    }
1891

    
1892
    // allows modifying the merged tree via a the hook_cdm_feature_node_block_content_alter
1893
    drupal_alter('merged_taxon_feature_tree', $taxon, $merged_tree);
1894

    
1895
    return $merged_tree;
1896
  }
1897

    
1898

    
1899
  function compose_distribution_hierarchy($distribution_tree, $feature_block_settings){
1900

    
1901
    static $hierarchy_style;
1902
    // TODO expose $hierarchy_style to administration or provide a hook
1903
    if( !isset($hierarchy_style)){
1904
      $hierarchy_style = get_array_variable_merged(DISTRIBUTION_HIERARCHY_STYLE, DISTRIBUTION_HIERARCHY_STYLE_DEFAULT);
1905
    }
1906

    
1907
    $render_array = array();
1908

    
1909
    RenderHints::pushToRenderStack('descriptionElementDistribution');
1910
    RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1911

    
1912
    // Returning NULL if there are no description elements.
1913
    if ($distribution_tree == null) {
1914
      return $render_array;
1915
    }
1916
    // for now we are not using a render array internally to avoid performance problems
1917
    $markup = '';
1918
    if (isset($distribution_tree->rootElement->children)) {
1919
      $tree_nodes = $distribution_tree->rootElement->children;
1920
      _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, $markup, $hierarchy_style);
1921
    }
1922

    
1923
    $render_array['distribution_hierarchy'] = markup_to_render_array(
1924
      $markup,
1925
      0,
1926
      '<div id="distribution_hierarchy" class="distribution_hierarchy">',
1927
      '</div>'
1928
    );
1929

    
1930
    RenderHints::popFromRenderStack();
1931

    
1932
    return $render_array;
1933
  }
1934

    
1935
  /**
1936
   * this function should produce markup as the compose_description_elements_distribution()
1937
   * function.
1938
   *
1939
   * @see compose_description_elements_distribution()
1940
   *
1941
   * @param $distribution_tree
1942
   * @param $feature_block_settings
1943
   * @param $tree_nodes
1944
   * @param $markup
1945
   * @param $hierarchy_style
1946
   */
1947
  function _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, &$markup, $hierarchy_style, $level_index = -1){
1948

    
1949
    $level_index++;
1950
    static $enclosingTag = "span";
1951

    
1952
    $level_style = array_shift($hierarchy_style);
1953
    if(count($hierarchy_style) == 0){
1954
      // lowest defined level style will be reused for all following levels
1955
      $hierarchy_style[] = $level_style;
1956
    }
1957

    
1958
    $node_index = -1;
1959
    $per_node_markup = array();
1960
    foreach ($tree_nodes as $node){
1961

    
1962
      $per_node_markup[++$node_index] = '';
1963

    
1964
      $label = $node->nodeId->representation_L10n;
1965

    
1966
      $distributions = $node->data;
1967
      $distribution_uuids = array();
1968
      $distribution_aggregate = NULL;
1969
        foreach($distributions as $distribution){
1970

    
1971
          $distribution_uuids[] = $distribution->uuid;
1972

    
1973
          // if there is more than one distribution we aggregate the sources and
1974
          // annotations into a synthetic distribution so that the footnote keys
1975
          // can be rendered consistently
1976
          if(!$distribution_aggregate) {
1977
            $distribution_aggregate = $distribution;
1978
            if(!isset($distribution_aggregate->sources[0])){
1979
              $distribution_aggregate->sources = array();
1980
            }
1981
            if(!isset($distribution_aggregate->annotations[0])){
1982
              $distribution_aggregate->annotations = array();
1983
            }
1984
          } else {
1985
            if(isset($distribution->sources[0])) {
1986
              $distribution_aggregate->sources = array_merge($distribution_aggregate->sources,
1987
                $distribution->sources);
1988
            }
1989
            if(isset($distribution->annotations[0])) {
1990
              $distribution_aggregate->annotations = array_merge($distribution_aggregate->annotations,
1991
                $distribution->annotations);
1992
            }
1993
          }
1994
        }
1995

    
1996
      $status_label= '';
1997
      $status_markup = '';
1998
      $annotations_and_sources =  null;
1999
      if($distribution_aggregate) {
2000
        $annotations_and_sources = handle_annotations_and_sources(
2001
          $distribution_aggregate,
2002
          handle_annotations_and_sources_config($feature_block_settings),
2003
          $label,
2004
          UUID_DISTRIBUTION
2005
        );
2006

    
2007
        list($status_label, $status_markup) = distribution_status_label_and_markup($distribution, $level_style['status_glue']);
2008
      }
2009

    
2010
      $per_node_markup[$node_index] .= '<' . $enclosingTag . ' class="descriptionElement'
2011
        . join(' descriptionElement-', $distribution_uuids)
2012
        . ' level_index_' . $level_index
2013
        . ' " title="' . $status_label . '">'
2014
        . '<span class="area_label">' . $label
2015
        . $level_style['label_suffix'] . '</span>'
2016
        . $status_markup
2017
      ;
2018

    
2019
      if(isset($annotations_and_sources)){
2020
        if(!empty($annotations_and_sources['source_references'])){
2021
          $per_node_markup[$node_index] .= ' ' . join(', ' , $annotations_and_sources['source_references']);
2022
        }
2023
        if($annotations_and_sources['foot_note_keys']) {
2024
          $per_node_markup[$node_index] .= $annotations_and_sources['foot_note_keys'];
2025
        }
2026
      }
2027

    
2028
      if(isset($node->children[0])){
2029
        _compose_distribution_hierarchy(
2030
          $node->children,
2031
          $feature_block_settings,
2032
          $per_node_markup[$node_index],
2033
          $hierarchy_style,
2034
          $level_index
2035
        );
2036
      }
2037

    
2038
      $per_node_markup[$node_index] .= '</' . $enclosingTag . '>';
2039
    }
2040
    $markup .= $level_style['item_group_prefix']  . join( $level_style['item_glue'], $per_node_markup) . $level_style['item_group_postfix'];
2041
  }
2042

    
2043

    
2044
/**
2045
 * Provides the content for a block of Uses Descriptions for a given taxon.
2046
 *
2047
 * Fetches the list of TaxonDescriptions tagged with the MARKERTYPE_USE
2048
 * and passes them to the theme function theme_cdm_UseDescription().
2049
 *
2050
 * @param string $taxon_uuid
2051
 *   The uuid of the Taxon
2052
 *
2053
 * @return array
2054
 *   A drupal render array
2055
 */
2056
function cdm_block_use_description_content($taxon_uuid, $feature) {
2057

    
2058
  $use_description_content = array();
2059

    
2060
  if (is_uuid($taxon_uuid )) {
2061
    $markerTypes = array();
2062
    $markerTypes['markerTypes'] = UUID_MARKERTYPE_USE;
2063
    $useDescriptions = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXON . '/' . $taxon_uuid . '/descriptions', $markerTypes);
2064
    if (!empty($useDescriptions)) {
2065
      $use_description_content = compose_feature_block_items_use_records($useDescriptions, $taxon_uuid, $feature);
2066
    }
2067
  }
2068

    
2069
  return $use_description_content;
2070
}
2071

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

    
2089
  return $feature;
2090

    
2091
}
2092

    
2093
/**
2094
 * @param $root_nodes, for obtaining the  root nodes from a description you can
2095
 * use the function get_root_nodes_for_dataset($description);
2096
 *
2097
 * @return string
2098
 */
2099
function render_description_string($root_nodes, &$item_cnt = 0) {
2100

    
2101
  $out = '';
2102

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

    
2131
      // recurse child nodes
2132
      $child_markup = render_description_string($root_node->childNodes, $item_cnt);
2133
      if($child_markup){
2134
        $description_strings[] = $child_markup;
2135
      }
2136
    }
2137
    if(count($description_strings) > 0){
2138
      // remove last semicolon
2139
      $description_strings[count($description_strings) - 1] = preg_replace('/;$/', '', $description_strings[count($description_strings) - 1]);
2140
    }
2141
    $out  = join($description_strings,  ' ');
2142
  }
2143
  return $out;
2144
}
2145

    
2146
/**
2147
 * Compose a description as a table of Feature<->State
2148
 *
2149
 * @param $description_uuid
2150
 *
2151
 * @return array
2152
 *    The drupal render array for the page
2153
 *
2154
 * @ingroup compose
2155
 */
2156
function  compose_description_table($description_uuid, $descriptive_dataset_uuid = NULL) {
2157

    
2158
  RenderHints::pushToRenderStack('description_table');
2159

    
2160
  $render_array = [];
2161

    
2162
  $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, [$description_uuid]);
2163
  $dataSet = NULL;
2164
  // dataset passed as parameter
2165
  if ($descriptive_dataset_uuid != NULL) {
2166
    foreach ($description->descriptiveDataSets as $set) {
2167
      if ($set->uuid == $descriptive_dataset_uuid) {
2168
        $dataSet = $set;
2169
        break;
2170
      }
2171
    }
2172
  }
2173

    
2174
  if(!empty($description->descriptiveDataSets)) {
2175
    // only one dataset present
2176
    if (!isset($dataSet) && sizeof($description->descriptiveDataSets) == 1) {
2177
      foreach ($description->descriptiveDataSets as $dataSet) {
2178
        break;
2179
      }
2180
    }
2181

    
2182
    // generate description title
2183
    RenderHints::pushToRenderStack('title');
2184
    if (isset($dataSet)) {
2185

    
2186
      $described_entity_title = NULL;
2187
      if(isset($description->describedSpecimenOrObservation)){
2188
        $described_entity_title = $description->describedSpecimenOrObservation->titleCache;
2189
      } else if($description->taxon) {
2190
          $described_entity_title = render_taxon_or_name($description->taxon);
2191
      }
2192
      $title = 'Descriptive Data ' . $dataSet->titleCache .
2193
        ($described_entity_title ? ' for ' . $described_entity_title : '');
2194
    }
2195
    $render_array['title'] = markup_to_render_array($title, null, '<h3 class="title">', '</h3>');
2196
    RenderHints::popFromRenderStack();
2197
    // END of --- generate description title
2198

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

    
2226
  $described_entities = [];
2227
  if (isset($description->describedSpecimenOrObservation)) {
2228
    $decr_entitiy = '<span class="label">Specimen:</span> ' . render_cdm_specimen_link($description->describedSpecimenOrObservation);
2229
    $described_entities['specimen'] = markup_to_render_array($decr_entitiy, null, '<div>', '</div>');
2230
  }
2231
  if (isset($description->taxon)) {
2232
    $decr_entitiy = '<span class="label">Taxon:</span> ' . render_taxon_or_name($description->taxon, url(path_to_taxon($description->taxon->uuid)));
2233
    $described_entities['taxon'] = markup_to_render_array($decr_entitiy, null, '<div>', '</div>');
2234
  }
2235

    
2236
  if(count($described_entities)){
2237
    $render_array['described_entities'] = $described_entities;
2238
    $render_array['described_entities']['#prefix'] = '<div class="described-entities">';
2239
    $render_array['described_entities']['#suffix'] = '</div>';
2240
  }
2241

    
2242

    
2243
  $root_nodes = get_root_nodes_for_dataset($description);
2244

    
2245

    
2246
  $rows = [];
2247
  $rows = description_element_table_rows($root_nodes, $rows);
2248

    
2249
  // --- create headers
2250
  $header = [0 => [], 1 => []];
2251

    
2252
  foreach($rows as $row) {
2253
    if(array_search('Character', $row['class']) && array_search('Character', $header[0]) === false){
2254
      $header[0][] = 'Character';
2255
    } elseif (array_search('Feature', $row['class']) && array_search('Feature', $header[0]) === false){
2256
      $header[0][] = 'Feature';
2257
    }
2258
    if(array_search('has_state', $row['class']) && array_search('States', $header[1]) === false){
2259
      $header[1][] = 'States';
2260
    } elseif (array_search('has_values', $row['class']) && array_search('Values', $header[1]) === false){
2261
      $header[1][] = 'Values';
2262
    }
2263
  }
2264
  asort($header[0]);
2265
  asort($header[1]);
2266
  $header[0] = join('/', $header[0]);
2267
  $header[1] = join('/', $header[1]);
2268

    
2269
  // ---
2270

    
2271
  if (!empty($rows)) {
2272
    $render_array['table'] = markup_to_render_array(theme('table', [
2273
      'header' => $header,
2274
      'rows' => $rows,
2275
      'caption' => statistical_values_explanation(),
2276
      'title' => "Table"
2277
    ]));
2278
  }
2279

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

    
2315
  RenderHints::popFromRenderStack();
2316

    
2317
  return $render_array;
2318
}
2319

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

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

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

    
2411
/**
2412
 * @param $element
2413
 * @param $root_node
2414
 *
2415
 * @return bool
2416
 */
2417
function is_suppress_state_present_display($element, $root_node) {
2418
  return count($element->stateData) == 1 & $element->stateData[0]->state->representation_L10n == 'present' && is_array($root_node->childNodes);
2419
}
2420

    
2421

    
2422

    
(2-2/11)