Project

General

Profile

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

    
3
/**
4
 * Returns the localized representations of the modifiers hold by the
5
 * supplied cdm instance concatenated into one string.
6
 *
7
 * @param object $iModifieable
8
 *   cdm instance of an class implementing the interface IModifieable:
9
 *   DescriptionElementBase, StateDate, State
10
 *
11
 * @return String
12
 *   localized representations of the modifiers hold by the
13
 *   supplied cdm instance concatenated into one string
14
 */
15
function cdm_modifers_representations($iModifieable, $glue = ', ') {
16
  $modifiers_strings = array();
17
  if (isset($iModifieable->modifiers)) {
18
    foreach ($iModifieable->modifiers as $modifier) {
19
      $modifiers_strings[] = cdm_term_representation($modifier);
20
    }
21
  }
22
  return implode(', ', $modifiers_strings);
23
}
24

    
25
/**
26
 * Filters the given set of description elements and prefers computed elements over others.
27
 *
28
 * Computed description elements
29
 * are identified by the MarkerType.COMPUTED()
30
 *
31
 * If the given set contains at least one computed element only
32
 * the computed elements are returned.
33
 *
34
 * @param array $description_elements
35
 *   An array of CDM DescriptionElementBase instances
36
 *
37
 * @return array
38
 *   only the computed description elements otherwise all others.
39
 *
40
 * @deprecated this is replaced by the cdmlib DistributionUtil class!!!
41
 */
42
function cdm_description_elements_prefer_computed($description_elements){
43

    
44
  $computed_elements = array();
45
  $other_elements = array();
46

    
47
  if (!empty($description_elements)) {
48
    foreach ($description_elements as $element) {
49
      if (cdm_entity_has_marker($element, UUID_MARKERTYPE_COMPUTED)) {
50
        $computed_elements[] = $element;
51
      }
52
      else {
53
        $other_elements[] = $element;
54
      }
55
    }
56
  }
57

    
58
  if (count($computed_elements) > 0) {
59
    return $computed_elements;
60
  }
61
  else {
62
    return $other_elements;
63
  }
64
}
65

    
66
/**
67
 * Creates a query parameter array based on the setting stored in the drupal variable CDM_DISTRIBUTION_FILTER.
68
 *
69
 * @return array
70
 *   An array with distribution filter query parameters
71
 */
72
function cdm_distribution_filter_query() {
73
  $cdm_distribution_filter = get_array_variable_merged(CDM_DISTRIBUTION_FILTER, CDM_DISTRIBUTION_FILTER_DEFAULT);
74
  $query = array();
75

    
76
  $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
77
  if($feature_block_settings['sort_elements'] == SORT_HIERARCHICAL){
78
    $query['distributionOrder'] = 'AREA_ORDER';
79
  }
80

    
81
  $query['recipe'] = variable_get(DISTRIBUTION_CONDENSED_RECIPE, DISTRIBUTION_CONDENSED_RECIPE_DEFAULT);
82

    
83

    
84

    
85
  if ($cdm_distribution_filter['filter_rules']['statusOrderPreference']) {
86
    $query['statusOrderPreference'] = 1;
87
  }
88
  if ($cdm_distribution_filter['filter_rules']['subAreaPreference']) {
89
    $query['subAreaPreference'] = 1;
90
  }
91
  if (is_array($cdm_distribution_filter['hiddenAreaMarkerType']) && count($cdm_distribution_filter['hiddenAreaMarkerType']) > 0) {
92
    $query['hiddenAreaMarkerType'] = '';
93
    foreach ($cdm_distribution_filter['hiddenAreaMarkerType'] as $marker_type => $enabled) {
94
      if ($enabled) {
95
        $query['hiddenAreaMarkerType'] .= ($query['hiddenAreaMarkerType'] ? ',' : '') . $marker_type;
96
      }
97
    }
98
  }
99

    
100
  return $query;
101
}
102

    
103
/**
104
 * Merge the fields 'annotations', 'markers', 'sources', 'media' from the source CDM DescriptionElement into  the target.
105
 *
106
 * @param object $target
107
 *   The source CDM DescriptionElement
108
 * @param object $source
109
 *   The target CDM DescriptionElement
110
 */
111
function cdm_merge_description_elements(&$target, &$source) {
112
  static $fields_to_merge = array('annotations', 'markers', 'sources', 'media');
113

    
114
  foreach ($fields_to_merge as $field) {
115
    if (is_array($source->$field)) {
116
      if (!is_array($target->$field)) {
117
        $target->$field = $source->$field;
118
      }
119
      else {
120
        $target->$field = array_merge($target->$field, $source->$field);
121
      }
122
    }
123
  }
124
}
125

    
126
/**
127
 * Adds an entry to the end of the table of content items list
128
 *
129
 * The  table of content items are crated internally by calling
130
 * toc_list_item() the resulting item is added to the statically cached
131
 * list of toc elements
132
 *
133
 * @param string $label
134
 *   The label of toc entry
135
 * @param $class_attribute_suffix
136
 *   The suffix to be appended to the class attribute prefix: "feature-toc-item-"
137
 * @param string $fragment
138
 *   Optional parameter to define a url fragment different from the $label,
139
 *   if the $fragment is not defined the $label will be used
140
 */
141
function cdm_toc_list_add_item($label, $class_attribute_suffix, $fragment = NULL, $as_first_element = FALSE) {
142
  $toc_list_items = &cdm_toc_list();
143

    
144
  if (!$fragment) {
145
    $fragment = $label;
146
  }
147
  $fragment = generalizeString($fragment);
148

    
149
  $class_attributes = 'feature-toc-item-' . $class_attribute_suffix;
150

    
151
  $new_item = toc_list_item(
152
    theme(
153
      'cdm_feature_name',
154
      array('feature_name' => $label)),
155
      array('class' => $class_attributes),
156
      $fragment
157
    );
158

    
159
  if ($as_first_element) {
160
    array_unshift($toc_list_items, $new_item);
161
  }
162
  else {
163
    $toc_list_items[] = $new_item;
164
  }
165

    
166
}
167

    
168
/**
169
 * Returns the statically cached table of content items as render array.
170
 *
171
 * @see cdm_toc_list_add_item()
172
 *
173
 * @return array
174
 *   a render array of table of content items suitable for theme_item_list()
175
 */
176
function &cdm_toc_list(){
177
  $toc_list_items = &drupal_static('toc_list_items', array());
178

    
179
  return $toc_list_items;
180
}
181

    
182
/**
183
 * Prepares an empty Drupal block for displaying description elements of a specific CDM Feature.
184
 *
185
 * The block can also be used for pseudo Features like a bibliography. Pseudo features are
186
 * derived from other data on the fly and so not exist as such in the cdm data. In case
187
 * of pseudo features the $feature is left empty
188
 *
189
 * @param $feature_name
190
 *   A label describing the feature, usually the localized feature representation.
191
 * @param object $feature
192
 *   The CDM Feature for which the block is created. (optional)
193
 * @return object
194
 *   A Drupal block object
195
 */
196
function feature_block($feature_name, $feature = NULL) {
197
  $block = new stdclass(); // Empty object.
198
  $block->module = 'cdm_dataportal';
199
  $block->delta = generalizeString($feature_name);
200
  $block->region = NULL;
201
  $block->subject = '<a name="' . $block->delta . '"></a><span class="' . html_class_attribute_ref($feature) . '">'
202
    . theme('cdm_feature_name', array('feature_name' => $feature_name))
203
    . '</span>';
204
  $block->module = "cdm_dataportal-feature";
205
  $block->content = '';
206
  return $block;
207
}
208

    
209

    
210
/**
211
 * Returns a list of a specific type of IdentificationKeys.
212
 *
213
 * The list can be restricted by a taxon.
214
 *
215
 * @param string $type
216
 *   The simple name of the cdm class implementing the interface
217
 *   IdentificationKey, valid values are:
218
 *   PolytomousKey, MediaKey, MultiAccessKey.
219
 * @param string $taxonUuid
220
 *   If given this parameter restrict the listed keys to those which have
221
 *   the taxon identified be this uuid in scope.
222
 *
223
 * @return array
224
 *   List with identification keys.
225
 */
226
function _list_IdentificationKeys($type, $taxonUuid = NULL, $pageSize = NULL, $pageNumber = NULL) {
227
  if (!$type) {
228
    drupal_set_message(t('Type parameter is missing'), 'error');
229
    return;
230
  }
231
  $cdm_ws_pasepath = NULL;
232
  switch ($type) {
233
    case "PolytomousKey":
234
      $cdm_ws_pasepath = CDM_WS_POLYTOMOUSKEY;
235
      break;
236

    
237
    case "MediaKey":
238
      $cdm_ws_pasepath = CDM_WS_MEDIAKEY;
239
      break;
240

    
241
    case "MultiAccessKey":
242
      $cdm_ws_pasepath = CDM_WS_MULTIACCESSKEY;
243
      break;
244

    
245
  }
246

    
247
  if (!$cdm_ws_pasepath) {
248
    drupal_set_message(t('Type parameter is not valid: ') . $type, 'error');
249
  }
250

    
251
  $queryParameters = '';
252
  if (is_numeric($pageSize)) {
253
    $queryParameters = "pageSize=" . $pageSize;
254
  }
255
  else {
256
    $queryParameters = "pageSize=0";
257
  }
258

    
259
  if (is_numeric($pageNumber)) {
260
    $queryParameters = "pageNumber=" . $pageNumber;
261
  }
262
  else {
263
    $queryParameters = "pageNumber=0";
264
  }
265
  $queryParameters = NULL;
266
  if ($taxonUuid) {
267
    $queryParameters = "findByTaxonomicScope=$taxonUuid";
268
  }
269
  $pager = cdm_ws_get($cdm_ws_pasepath, NULL, $queryParameters);
270

    
271
  if (!$pager || $pager->count == 0) {
272
    return array();
273
  }
274
  return $pager->records;
275
}
276

    
277

    
278
/**
279
 * Creates a list item for a table of content, suitable as data element for a themed list
280
 *
281
 * @see theme_list()
282
 *
283
 * @param $label
284
 * @param $http_request_params
285
 * @param $attributes
286
 * @return array
287
 */
288
function toc_list_item($label, $attributes = array(), $fragment = null) {
289

    
290
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
291
  $http_request_params = drupal_static('http_request_params', drupal_get_query_parameters());
292

    
293
  $item =  array(
294
    'data' => l(
295
      $label,
296
      $_GET['q'],
297
      array(
298
        'attributes' => array('class' => array('toc')),
299
        'fragment' => generalizeString($label),
300
        'query' => $http_request_params,
301
      )
302
    ),
303
  );
304
  $item['attributes'] = $attributes;
305
  return $item;
306
}
307

    
308
/**
309
 * Creates the footnotes for the given CDM DescriptionElement instance.
310
 *
311
 * Footnotes are created for annotations and original sources,
312
 * optionally the sources are put into a separate bibliography.
313
 *
314
 * @param $descriptionElement
315
 *   A CDM DescriptionElement instance
316
 * @param $separator
317
 *   Optional parameter. The separator string to concatenate the footnote ids, default is ','
318
 * @param $footnote_list_key_suggestion
319
 *   will be overridden for original sources if the bibliography block is enabled
320
 *
321
 * @return String
322
 *   The foot note keys
323
 */
324
function cdm_create_description_element_footnotes(
325
          $description_element,
326
          $separator = ',',
327
          $footnote_list_key_suggestion = null,
328
          $do_link_to_reference = FALSE,
329
          $do_link_to_name_used_in_source = FALSE
330
    )
331
{
332

    
333
  $bibliography_settings = get_bibliography_settings();
334

    
335
  return cdm_create_footnotes($description_element,
336
    $separator,
337
    original_source_footnote_list_key($footnote_list_key_suggestion),
338
    $do_link_to_reference,
339
    $do_link_to_name_used_in_source,
340
    $bibliography_settings['enabled'] == 1 ? 'div' : null // null will cause original_source_footnote_list_key to use the default
341

    
342
  );
343
}
344

    
345
/**
346
 * Creates the footnotes for the given CDM instance.
347
 *
348
 * Footnotes are created for annotations and original sources.
349
 *
350
 * @param $descriptionElement
351
 *   A CDM DescriptionElement instance
352
 * @param $separator
353
 *   Optional parameter. The separator string to concatenate the footnote ids, default is ','
354
 * @param $footnote_list_key_suggestion
355
 *   will be overridden for original sources if the bibliography block is enabled
356
 * @$original_source_footnote_tag
357
 *    null will cause original_source_footnote_list_key to use the default
358
 *
359
 * @return String
360
 *   The foot note keys
361
 */
362
function cdm_create_footnotes(
363
    $description_element,
364
    $separator = ',',
365
    $footnote_list_key = null,
366
    $do_link_to_reference = FALSE,
367
    $do_link_to_name_used_in_source = FALSE,
368
    $original_source_footnote_tag = NULL
369
  ){
370

    
371
  // Annotations as footnotes.
372
  $footNoteKeys = cdm_annotations_as_footnotekeys($description_element, $footnote_list_key);
373

    
374
  // Source references as footnotes.
375
  usort($description_element->sources, 'compare_original_sources');
376
  foreach ($description_element->sources as $source) {
377
    if (_is_original_source_type($source)) {
378
      $fn_key = FootnoteManager::addNewFootnote(
379
        $footnote_list_key,
380
        theme('cdm_OriginalSource', array(
381
          'source' => $source,
382
          'doLink' => $do_link_to_reference,
383
          'do_link_to_name_used_in_source' => $do_link_to_name_used_in_source
384

    
385
        )),
386
        $original_source_footnote_tag
387
      );
388
      // Ensure uniqueness of the footnote keys.
389
      cdm_add_footnote_to_array($footNoteKeys, $fn_key);
390
    }
391
  }
392
  // Sort and render footnote keys.
393
  $footnoteKeyListStr = '';
394
  asort($footNoteKeys);
395
  foreach ($footNoteKeys as $footNoteKey) {
396
    $footnoteKeyListStr .= theme('cdm_footnote_key',
397
      array(
398
        'footnoteKey' => $footNoteKey,
399
        'separator' => ($footnoteKeyListStr ? $separator : '')));
400
  }
401
  return $footnoteKeyListStr;
402
}
403

    
404

    
405
/**
406
 * Compare callback to be used in usort() to sort render arrays produced by compose_description_element().
407
 *
408
 * @param $a
409
 * @param $b
410
 */
411
function compare_description_element_render_arrays($a, $b){
412
  if ($a['#value'].$a['#value-suffix'] == $b['#value'].$b['#value-suffix']) {
413
    return 0;
414
  }
415
  return ($a['#value'].$a['#value-suffix'] < $b['#value'].$b['#value-suffix']) ? -1 : 1;
416

    
417
}
418

    
419

    
420
/**
421
 * @param $render_array
422
 * @param $element
423
 * @param $feature_block_settings
424
 * @param $element_markup
425
 * @param $footnote_list_key_suggestion
426
 */
427
function compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label = FALSE)
428
{
429

    
430
  $render_array = array(
431
    '#type' => 'html_tag',
432
    '#tag' => cdm_feature_block_element_tag_name($feature_block_settings),
433

    
434
    '#attributes' => array(
435
      'class' => array(
436
        'DescriptionElement',
437
        'DescriptionElement-' . $element->class,
438
        html_class_attribute_ref($element)
439
      )
440
    ),
441

    
442
    '#value' => '',
443
    '#value_suffix' => NULL
444

    
445
  );
446

    
447
  $annotations_and_sources = handle_annotations_and_sources(
448
    $element,
449
    handle_annotations_and_sources_config($feature_block_settings),
450
    $element_markup,
451
    $footnote_list_key_suggestion
452
  );
453

    
454
  // handle the special case were the TextData is used as container for original source with name
455
  // used in source information without any text stored in it.
456
  $names_used_in_source_markup = '';
457
  if (!empty($annotations_and_sources['names_used_in_source']) && empty($element_markup)) {
458
    $names_used_in_source_markup = join(', ', $annotations_and_sources['names_used_in_source']) . ': ';
459
    // remove all <span class="nameUsedInSource">...</span> from all source_references
460
    // these are expected to be at the end of the strings
461
    $pattern = '/ <span class="nameUsedInSource">.*$/';
462
    foreach( $annotations_and_sources['source_references'] as &$source_reference){
463
      $source_reference = preg_replace($pattern , '', $source_reference);
464
    }
465
  }
466

    
467

    
468
  $source_references_markup = '';
469
  if (!empty($annotations_and_sources['source_references'])) {
470
    $source_references_markup = '<span class="sources">' . join(' ', $annotations_and_sources['source_references']) . '<span>';
471
  }
472

    
473
  $feature_label = '';
474
  if ($prepend_feature_label) {
475
    $feature_label = '<span class="nested-feature-tree-feature-label">' . $element->feature->representation_L10n . ':</span> ';
476
  }
477
  $content_markup = $names_used_in_source_markup . $element_markup . $source_references_markup;
478

    
479
  if(!$content_markup && $feature_block_settings['sources_as_content'] !== 1 || $feature_block_settings['sources_as_content_to_bibliography'] == 1){
480
    // no textual content, so skip this element completely, even if there could be an footnote key
481
    // see #4379
482
    return null;
483
  }
484

    
485
    $render_array['#value'] = $feature_label . $content_markup;
486
    $render_array['#value_suffix'] = $annotations_and_sources['foot_note_keys'];
487
  return $render_array;
488
}
489

    
490
/**
491
 * Creates a handle_annotations_and_sources configuration array from feature_block_settings.
492
 *
493
 * The handle_annotations_and_sources configuration array is meant to be used for the
494
 * method handle_annotations_and_sources().
495
 *
496
 * @param $feature_block_settings array
497
 *
498
 * @return array
499
 *   The configuration array for handle_annotations_and_sources()
500
 */
501
function handle_annotations_and_sources_config($feature_block_settings){
502

    
503
  $config = $feature_block_settings;
504
  unset($config['sources_as_content_to_bibliography']);
505
  $config['add_footnote_keys'] = 0;
506
  if($feature_block_settings['sources_as_content'] !== 1 || $feature_block_settings['sources_as_content_to_bibliography'] == 1) {
507
    $config['add_footnote_keys'] = 1;
508
  }
509
  $config['bibliography_aware'] = 1;
510

    
511
  return $config;
512
}
513

    
514
  /**
515
   * @param $entity
516
   * @param $config array
517
   *   An associative array to configure the display of the annotations and sources.
518
   *   The array has the following keys
519
   *   - sources_as_content
520
   *   - link_to_name_used_in_source
521
   *   - link_to_reference
522
   *   - add_footnote_keys
523
   *   - bibliography_aware
524
   *   Valid values are 1 or 0.
525
   * @param $inline_text_prefix
526
   *   Only used to decide if the source references should be enclosed in brackets or not when displayed inline.
527
   *   This text will not be included into the response.
528
   * @param $footnote_list_key_suggestion
529
   *
530
   * @return array
531
   *   an associative array with the following elements:
532
   *   - foot_note_keys: all footnote keys as markup
533
   *   - source_references: an array of the source references citations
534
   *   - names used in source: an associative array of the names in source,
535
   *        the name in source strings are de-duplicated
536
   *        !!!NOTE!!!!: this field will most probably be removed soon (TODO)
537
   */
538
  function handle_annotations_and_sources($entity, $config, $inline_text_prefix, $footnote_list_key_suggestion) {
539

    
540
    $annotations_and_sources = array(
541
      'foot_note_keys' => NULL,
542
      'source_references' => array(),
543
      'names_used_in_source' => array()
544
    );
545

    
546
    usort($entity->sources, 'compare_original_sources');
547

    
548
    if ($config['sources_as_content'] == 1) {
549
      foreach ($entity->sources as $source) {
550

    
551
        $reference_citation = theme('cdm_OriginalSource',
552
          array(
553
            'source' => $source,
554
            'doLink' => $config['link_to_reference'] == 1,
555
            'do_link_to_name_used_in_source' => $config['link_to_name_used_in_source'] == 1,
556
          )
557
        );
558

    
559
        if ($reference_citation) {
560
          if (empty($inline_text_prefix)) {
561
            $annotations_and_sources['source_references'][] = $reference_citation;
562
          }
563
          else {
564
            $annotations_and_sources['source_references'][] = ' (' . $reference_citation . ')';
565
          }
566
        }
567

    
568
        $name_in_source_render_array = compose_name_in_source(
569
          $source,
570
          $config['link_to_name_used_in_source'] == 1
571
        );
572

    
573
        if(!empty($name_in_source_render_array)){
574
          $annotations_and_sources['names_used_in_source'][$name_in_source_render_array['#_plaintext']] = drupal_render($name_in_source_render_array);
575
        }
576
      } // END of loop over sources
577

    
578
      // annotations footnotes separate.
579
      $annotations_and_sources['foot_note_keys'] = theme('cdm_annotations_as_footnotekeys',
580
        array(
581
          'cdmBase_list' => $entity,
582
          'footnote_list_key' => $footnote_list_key_suggestion,
583
        )
584
      );
585

    
586
    } // END of references inline
587

    
588
    // sources as footnotes or put into into bibliography if requested ...
589
    if ($config['add_footnote_keys'] == 1) {
590
      if(empty($config['bibliography_aware'])) {
591
        $annotations_and_sources['foot_note_keys'] = cdm_create_footnotes(
592
          $entity, ',',
593
          $footnote_list_key_suggestion,
594
          $config['link_to_reference'] == 1,
595
          $config['link_to_name_used_in_source'] == 1
596
        );
597
      } else {
598
        $annotations_and_sources['foot_note_keys'] = cdm_create_description_element_footnotes(
599
          $entity, ',',
600
          $footnote_list_key_suggestion,
601
          $config['link_to_reference'] == 1,
602
          $config['link_to_name_used_in_source'] == 1
603
        );
604
      }
605
    }
606

    
607
    return $annotations_and_sources;
608
  }
609

    
610

    
611
  /**
612
   *
613
   *
614
   * @return string
615
   *  the footnote_list_key
616
   */
617
  function original_source_footnote_list_key($key_suggestion = null) {
618
    if(!$key_suggestion){
619
      $key_suggestion = RenderHints::getFootnoteListKey();
620
    }
621
    $bibliography_settings = get_bibliography_settings();
622
    $footnote_list_key = $bibliography_settings['enabled'] == 1 ? 'BIBLIOGRAPHY' : 'BIBLIOGRAPHY-' . $key_suggestion;
623
    return $footnote_list_key;
624
  }
625

    
626
  /**
627
   * Provides the according tag name for the description element markup which fits the  $feature_block_settings['as_list'] value
628
   *
629
   * @param $feature_block_settings
630
   *   A feature_block_settings array, for details, please see get_feature_block_settings($feature_uuid = 'DEFAULT')
631
   */
632
  function cdm_feature_block_element_tag_name($feature_block_settings){
633
    switch ($feature_block_settings['as_list']){
634
      case 'ul':
635
      case 'ol':
636
        return 'li';
637
      case 'div':
638
        if(isset($feature_block_settings['element_tag'])){
639
          return $feature_block_settings['element_tag'];
640
        }
641
        return 'span';
642
      case 'dl':
643
        return 'dd';
644
      default:
645
        return 'div'; // should never happen, throw error instead?
646
    }
647
  }
648

    
649

    
650
/* ==================== COMPOSE FUNCTIONS =============== */
651

    
652
  /**
653
   * Returns a set of feature blocks for a taxon profile from the $mergedFeatureNodes of a given $taxon.
654
   *
655
   * The taxon profile consists of drupal block elements, one for the description elements
656
   * of a specific feature. The structure is defined by specific FeatureTree.
657
   * The chosen FeatureTree is merged with the list of description elements prior to using this method.
658
   *
659
   * The merged nodes can be obtained by making use of the
660
   * function cdm_ws_descriptions_by_featuretree().
661
   *
662
   * @see cdm_ws_descriptions_by_featuretree()
663
   *
664
   * @param $mergedFeatureNodes
665
   *
666
   * @param $taxon
667
   *
668
   * @return array
669
   *  A Drupal render array containing feature blocks and the table of content
670
   *
671
   * @ingroup compose
672
   */
673
  function compose_feature_blocks($mergedFeatureNodes, $taxon) {
674

    
675
    $block_list = array();
676

    
677
    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME);
678

    
679

    
680
    RenderHints::pushToRenderStack('feature_block');
681
    // Create a drupal block for each feature
682
    foreach ($mergedFeatureNodes as $node) {
683

    
684
      if ((isset($node->descriptionElements['#type']) ||
685
          has_feature_node_description_elements($node)) && $node->feature->uuid != UUID_IMAGE) { // skip empty or suppressed features
686

    
687
        RenderHints::pushToRenderStack($node->feature->uuid);
688
          
689
        $feature_name = cdm_term_representation($node->feature, 'Unnamed Feature');
690
        $feature_block_settings = get_feature_block_settings($node->feature->uuid);
691
        
692

    
693
        $block = feature_block($feature_name, $node->feature);
694
        $block->content = array();
695
        $block_content_is_empty = TRUE;
696

    
697
        /*
698
         * Content/DISTRIBUTION.
699
         */
700
        if ($node->feature->uuid == UUID_DISTRIBUTION) {
701
          $block = compose_feature_block_distribution($taxon, $node->descriptionElements, $node->feature);
702
          $block_content_is_empty = FALSE;
703
        }
704
        /*
705
         * Content/COMMON_NAME.
706
         */
707
        else if ($node->feature->uuid == UUID_COMMON_NAME) {
708
          $common_names_render_array = compose_feature_block_items_feature_common_name($node->descriptionElements, $node->feature);
709
          $block->content[] = $common_names_render_array;
710
          $block_content_is_empty = FALSE;
711
        }
712

    
713
        else if ($node->feature->uuid == UUID_USE_RECORD) {
714
          $block_uses_content_html = theme('cdm_block_Uses', array('taxonUuid' => $taxon->uuid));
715
          $block->content[] = markup_to_render_array($block_uses_content_html);
716
          $block_content_is_empty = FALSE;
717
        }
718

    
719
        /*
720
         * Content/ALL OTHER FEATURES.
721
         */
722
        else {
723

    
724
          $media_list = array();
725
          $elements_render_array = array();
726
          $child_elements_render_array = null;
727

    
728
          if (isset($node->descriptionElements[0])) {
729
            $elements_render_array = compose_feature_block_items_generic($node->descriptionElements, $node->feature);
730
          }
731

    
732
          // Content/ALL OTHER FEATURES/Subordinate Features
733
          // subordinate features are printed inline in one floating text,
734
          // it is expected hat subordinate features can "contain" TextData,
735
          // Qualitative- and Qualitative- DescriptionElements
736
          if (isset($node->childNodes[0])) {
737
            $child_elements_render_array = compose_feature_block_items_nested($node, $media_list, $feature_block_settings);
738
            $elements_render_array = array_merge($elements_render_array, $child_elements_render_array);
739
          }
740
          $block_content_is_empty = $block_content_is_empty && empty($media_list) && empty($elements_render_array);
741
          if(!$block_content_is_empty){
742
            $block->content[] = compose_feature_block_wrap_elements($elements_render_array, $node->feature);
743
            $block->content[] = compose_feature_media_gallery($node, $media_list, $gallery_settings);
744
            /*
745
             * Footnotes for the feature block
746
             */
747
            $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . $node->feature->uuid)));
748
            $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => $node->feature->uuid)));
749
            $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => $node->feature->uuid)));
750
          }
751
        } // END all other features
752

    
753
        // allows modifying the block contents via a the hook_cdm_feature_node_block_content_alter
754
        drupal_alter('cdm_feature_node_block_content', $block->content, $node->feature, $node->descriptionElements);
755

    
756
        if(!$block_content_is_empty){ // skip empty block content
757
          $block_list[] = $block;
758
          cdm_toc_list_add_item(cdm_term_representation($node->feature), $node->feature->uuid);
759
        } // END: skip empty block content
760
      } // END: skip empty or suppressed features
761
      RenderHints::popFromRenderStack();
762
    } // END: creating a block per feature
763

    
764
    RenderHints::popFromRenderStack();
765

    
766
    drupal_alter('cdm_feature_node_blocks', $block_list, $taxon);
767

    
768
    return _block_get_renderable_array($block_list);
769
  }
770

    
771
/**
772
 * Creates a render array of description elements  held by child nodes of the given feature node.
773
 *
774
 * This function is called recursively!
775
 *
776
 * @param $node
777
 *   The feature node.
778
 * @param array $media_list
779
 *   List of CDM Media entities. All media of subordinate elements should be passed to the main feature.ä
780
 * @param $feature_block_settings
781
 *   The feature block settings.
782
 * @param $main_feature
783
 *  Only used internally in recursive calls.
784
 *
785
 * @return array
786
 *  A Drupal render array
787
 *
788
 * @ingroup compose
789
 */
790
function compose_feature_block_items_nested($node, &$media_list, $feature_block_settings, $main_feature = NULL)
791
{
792

    
793
  if(!$main_feature){
794
    $main_feature = $node->feature;
795
  }
796
  /*
797
   * TODO should be configurable, options; YES, NO, AUTOMATIC
798
   * (automatic will only place the label if the first word of the description element text is not the same)
799
   */
800
  $prepend_feature_label = false;
801

    
802
  $render_arrays = array();
803
  foreach ($node->childNodes as $child_node) {
804
    if (isset($child_node->descriptionElements[0])) {
805
      foreach ($child_node->descriptionElements as $element) {
806

    
807
        if (isset($element->media[0])) {
808
          // Append media of subordinate elements to list of main
809
          // feature.
810
          $media_list = array_merge($media_list, $element->media);
811
        }
812

    
813
        $child_node_element = null;
814
        switch ($element->class) {
815
          case 'TextData':
816
            $child_node_element = compose_description_element_text_data($element, $element->feature->uuid, $feature_block_settings, $prepend_feature_label);
817
            break;
818
          case 'CategoricalData':
819
            $child_node_element = compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label);
820
            break;
821
          case 'QuantitativeData':
822
            $child_node_element = compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label);
823

    
824
        }
825
        if (is_array($child_node_element)) {
826
          $render_arrays[] = $child_node_element;
827
        }
828
      }
829
    }
830

    
831
    if(isset($child_node->childNodes[0])){
832
      $render_arrays = array_merge($render_arrays, compose_feature_block_items_nested($child_node, $media_list, $feature_block_settings, $main_feature ));
833
    }
834
  }
835

    
836
  return $render_arrays;
837
}
838

    
839
  /**
840
   *
841
   * @param $node
842
   *  The merged feature three node which potentially contains media in its description elements.
843
   * @param $media_list
844
   *    Additional media to be merged witht the media contained in the nodes description elements
845
   * @param $gallery_settings
846
   * @return array
847
   *
848
   * @ingroup compose
849
   */
850
  function compose_feature_media_gallery($node, $media_list, $gallery_settings) {
851

    
852
    if (isset($node->descriptionElements)) {
853
      $media_list = array_merge($media_list, cdm_dataportal_media_from_descriptionElements($node->descriptionElements));
854
    }
855

    
856
    $captionElements = array('title', 'rights');
857

    
858
    if (isset($media_list[0]) && isset($gallery_settings['cdm_dataportal_media_maxextend']) && isset($gallery_settings['cdm_dataportal_media_cols'])) {
859
      $gallery = compose_cdm_media_gallerie(array(
860
        'mediaList' => $media_list,
861
        'galleryName' => CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME . '_' . $node->feature->uuid,
862
        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
863
        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
864
        'captionElements' => $captionElements,
865
      ));
866
      return markup_to_render_array($gallery);
867
    }
868

    
869
    return markup_to_render_array('');
870
  }
871

    
872
  /**
873
   * Composes the distribution feature block for a taxon
874
   *
875
   * @param $taxon
876
   * @param $descriptionElements
877
   *   an associative array with two elements:
878
   *   - '#type': must be 'DTO'
879
   *   - 'DistributionInfoDTO': a CDM DistributionInfoDTO object as returned by the DistributionInfo web service
880
   * @param $feature
881
   *
882
   * @return array
883
   *  A drupal render array
884
   *
885
   * @ingroup compose
886
   */
887
  function compose_feature_block_distribution($taxon, $descriptionElements, $feature) {
888
    $text_data_glue = '';
889
    $text_data_sortOutArray = FALSE;
890
    $text_data_enclosingTag = 'ul';
891
    $text_data_out_array = array();
892

    
893
    $distributionElements = NULL;
894
    $distribution_info_dto = NULL;
895
    $distribution_sortOutArray = FALSE;
896

    
897
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
898

    
899
    // TODO use feature_block_settings instead of DISTRIBUTION_ORDER_MODE
900
    if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) != 'TREE') {
901
      $distribution_glue = '';
902
      $distribution_enclosingTag = 'dl';
903
    } else {
904
      $distribution_glue = '';
905
      $distribution_enclosingTag = 'ul';
906
    }
907

    
908
    if (!isset($descriptionElements['#type']) || !$descriptionElements['#type'] == 'DTO') {
909
      // skip the DISTRIBUTION section if there is no DTO type element
910
      return array(); // FIXME is it ok to return an empty array?
911
    }
912

    
913
    $block = feature_block(
914
      cdm_term_representation($feature, 'Unnamed Feature'),
915
      $feature
916
    );
917

    
918
    // $$descriptionElements['TextData'] is added to to the feature node in merged_taxon_feature_tree()
919
    if (isset($descriptionElements['TextData'])) {
920
      // --- TextData
921
      foreach ($descriptionElements['TextData'] as $text_data_element) {
922
        $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
923
        $repr = drupal_render($text_data_render_array);
924

    
925
        if (!array_search($repr, $text_data_out_array)) { // de-duplication !!
926
          $text_data_out_array[] = $repr;
927
          // TODO HINT: sorting in compose_feature_block_wrap_elements will
928
          // not work since this array contains html attributes with uuids
929
          // and what is about cases like the bibliography where
930
          // any content can be prefixed with some foot-note anchors?
931
          $text_data_sortOutArray = TRUE;
932
          $text_data_glue = '<br/> ';
933
          $text_data_enclosingTag = 'p';
934
        }
935
      }
936
    }
937

    
938

    
939
    if ($text_data_out_array && variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
940
      $block->content[] = compose_feature_block_wrap_elements(
941
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
942
      );
943
    }
944

    
945
    // --- Distribution map
946
    $distribution_map_query_parameters = NULL;
947

    
948
    $map_visibility = variable_get(DISTRIBUTION_MAP_VISIBILITY, DISTRIBUTION_MAP_VISIBILITY_DEFAULT);
949
    if(variable_get(DISTRIBUTION_MAP_VISIBILITY, DISTRIBUTION_MAP_VISIBILITY_DEFAULT) == 'always' ||
950
        $map_visibility == 'automatic' && isset($descriptionElements['DistributionInfoDTO']->mapUriParams)
951
      )
952
    {
953
      $distribution_map_query_parameters = $descriptionElements['DistributionInfoDTO']->mapUriParams;
954
    }
955
    $map_render_element = compose_distribution_map($distribution_map_query_parameters);
956
    $block->content[] = $map_render_element;
957

    
958
    $dto_out_array = array();
959

    
960
    // --- Condensed Distribution
961
    if(variable_get(DISTRIBUTION_CONDENSED) && isset($descriptionElements['DistributionInfoDTO']->condensedDistribution)){
962
      $condensed_distribution_markup = '<p class="condensed_distribution">';
963

    
964
      $isFirst = true;
965
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous[0])){
966
        foreach($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous as $cdItem){
967
          if(!$isFirst){
968
            $condensed_distribution_markup .= ' ';
969
          }
970
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->idInVocabulary . '">'
971
          . $cdItem->areaStatusLabel . '</span>';
972
          $isFirst = false;
973
        }
974
      }
975

    
976
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign[0])) {
977
        if(!$isFirst){
978
          $condensed_distribution_markup .= ' ';
979
        }
980
        $isFirst = TRUE;
981
        $condensed_distribution_markup .= '[';
982
        foreach ($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign as $cdItem) {
983
          if (!$isFirst) {
984
            $condensed_distribution_markup .= ' ';
985
          }
986
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->titleCache . '">'
987
            . $cdItem->areaStatusLabel . '</span>';
988
          $isFirst = false;
989
        }
990
        $condensed_distribution_markup .= ']';
991
      }
992

    
993
      $condensed_distribution_markup .= '&nbsp;' . l(
994
          font_awesome_icon_markup(
995
            'fa-info-circle',
996
            array(
997
              'alt'=>'help',
998
              'class' => array('superscript')
999
            )
1000
          ),
1001
          variable_get(DISTRIBUTION_CONDENSED_INFO_PATH, DISTRIBUTION_CONDENSED_INFO_PATH_DEFAULT) ,
1002
          array('html' => TRUE));
1003
      $condensed_distribution_markup .= '</p>';
1004
      $dto_out_array[] = $condensed_distribution_markup;
1005
    }
1006

    
1007
    // --- tree or list
1008
    if (isset($descriptionElements['DistributionInfoDTO'])) {
1009
      $distribution_info_dto = $descriptionElements['DistributionInfoDTO'];
1010

    
1011
      // --- tree
1012
      if (is_object($distribution_info_dto->tree)) {
1013
        $distribution_tree_render_array = compose_distribution_hierarchy($distribution_info_dto->tree, $feature_block_settings);
1014
        $dto_out_array[] = $distribution_tree_render_array;
1015
      }
1016

    
1017
      // --- sorted element list
1018
      if (is_array($distribution_info_dto->elements) && count($distribution_info_dto->elements) > 0) {
1019
        foreach ($distribution_info_dto->elements as $descriptionElement) {
1020
          if (is_object($descriptionElement->area)) {
1021
            $sortKey = $descriptionElement->area->representation_L10n;
1022
            $distributionElements[$sortKey] = $descriptionElement;
1023
          }
1024
        }
1025
        ksort($distributionElements);
1026
        $distribution_element_render_array = compose_description_elements_distribution($distributionElements);
1027
        $dto_out_array[] = $distribution_element_render_array;
1028

    
1029
      }
1030
      //
1031
      $block->content[] = compose_feature_block_wrap_elements(
1032
        $dto_out_array, $feature, $distribution_glue, $distribution_sortOutArray
1033
      );
1034
    }
1035

    
1036
    // --- TextData at the bottom
1037
    if ($text_data_out_array && !variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
1038
      $block->content[] = compose_feature_block_wrap_elements(
1039
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
1040
      );
1041
    }
1042

    
1043
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . UUID_DISTRIBUTION)));
1044
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
1045
    $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
1046

    
1047
    return $block;
1048
  }
1049

    
1050

    
1051
  /**
1052
   * Composes a drupal render array for single CDM TextData description element.
1053
   *
1054
   * @param $element
1055
   *    The CDM TextData description element.
1056
   *  @param $feature_uuid
1057
   * @param bool $prepend_feature_label
1058
   *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1059
   *
1060
   * @return array
1061
   *   A drupal render array with the following elements being used:
1062
   *    - #tag: either 'div', 'li', ...
1063
   *    ⁻ #attributes: class attributes
1064
   *    - #value_prefix: (optionally) contains footnote anchors
1065
   *    - #value: contains the textual content
1066
   *    - #value_suffix: (optionally) contains footnote keys
1067
   *
1068
   * @ingroup compose
1069
   */
1070
  function compose_description_element_text_data($element, $feature_uuid, $feature_block_settings, $prepend_feature_label = FALSE) {
1071

    
1072
    $footnote_list_key_suggestion = $feature_uuid;
1073

    
1074
    $element_markup = '';
1075
    if (isset($element->multilanguageText_L10n->text)) {
1076
      // TODO replacement of \n by <br> should be configurable
1077
      $element_markup = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
1078
    }
1079

    
1080
    $render_array = compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label);
1081

    
1082
    return $render_array;
1083
  }
1084

    
1085

    
1086
/**
1087
 * Theme function to render CDM DescriptionElements of the type TaxonInteraction.
1088
 *
1089
 * @param $element
1090
 *  The CDM TaxonInteraction entity
1091
 *
1092
 * @return
1093
 *  A drupal render array
1094
 *
1095
 * @ingroup compose
1096
 */
1097
function compose_description_element_taxon_interaction($element, $feature_block_settings) {
1098

    
1099
  $out = '';
1100

    
1101

    
1102
  if (isset($element->description_L10n)) {
1103
    $out .=  ' ' . $element->description_L10n;
1104
  }
1105

    
1106
  if(isset($element->taxon2)){
1107
    $out = render_taxon_or_name($element->taxon2, url(path_to_taxon($element->taxon2->uuid)));
1108
  }
1109

    
1110
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1111

    
1112
  return $render_array;
1113
}
1114

    
1115

    
1116
/**
1117
 * Renders a single instance of the type IndividualsAssociations.
1118
 *
1119
 * @param $element
1120
 *   The CDM IndividualsAssociations entity.
1121
 * @param $feature_block_settings
1122
 *
1123
 * @return array
1124
 *   Drupal render array
1125
 *
1126
 * @ingroup compose
1127
 */
1128
function compose_description_element_individuals_association($element, $feature_block_settings) {
1129

    
1130
  $out = '';
1131

    
1132
  $render_array = compose_cdm_specimenOrObservation($element->associatedSpecimenOrObservation);
1133

    
1134
  if (isset($element->description_L10n)) {
1135
    $out .=  ' ' . $element->description_L10n;
1136
  }
1137

    
1138
  $out .= drupal_render($render_array);
1139

    
1140
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1141

    
1142
  return $render_array;
1143
}
1144

    
1145
/**
1146
 * Renders a single instance of the type CategoricalData.
1147
 *
1148
 * @param $element
1149
 *  The CDM CategoricalData entity
1150
 *
1151
 * @param $feature_block_settings
1152
 *
1153
 * @param bool $prepend_feature_label
1154
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1155
 *
1156
 * @return string
1157
 *   a html representation of the given CategoricalData element
1158
 *
1159
 * @ingroup compose
1160
 */
1161
function compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1162

    
1163
  $enclosing_tag = cdm_feature_block_element_tag_name($feature_block_settings);
1164

    
1165
  $state_data_strings = array();
1166
  if (isset($element->stateData)) {
1167
    foreach ($element->stateData as $state_data) {
1168

    
1169
      $state  = NULL;
1170

    
1171
      if (isset($state_data->state)) {
1172
        $state = cdm_term_representation($state_data->state);
1173
      }
1174

    
1175
      if (isset($state_data->modifyingText_L10n)) {
1176
        $state = ' ' . $state_data->modifyingText_L10n;
1177
      }
1178

    
1179
      $modifiers_strings = cdm_modifers_representations($state_data);
1180

    
1181
      $state_data_strings[] = $state . ($modifiers_strings ? ' ' . $modifiers_strings : '');
1182

    
1183
    }
1184
  }
1185

    
1186
  $out = '<span class="' . html_class_attribute_ref($element) . '">' . implode(', ', $state_data_strings) . '</span>';
1187

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

    
1190
  return $render_array;
1191
}
1192

    
1193

    
1194
/**
1195
 * Theme function to render CDM DescriptionElements of the type QuantitativeData.
1196
 *
1197
 * The function renders the statisticalValues contained in the QuantitativeData
1198
 * entity according to the following scheme:
1199
 *
1200
 * (ExtremeMin)-Min-Average-Max-(ExtremeMax)
1201
 *
1202
 * All modifiers of these values are appended.
1203
 *
1204
 * If the QuantitativeData is containing more statisticalValues with further
1205
 * statisticalValue types, these additional measures will be appended to the
1206
 * above string separated by whitespace.
1207
 *
1208
 * Special cases;
1209
 * 1. Min==Max: this will be interpreted as Average
1210
 *
1211
 * @param $element
1212
 *  The CDM QuantitativeData entity
1213
 *
1214
 * @param $feature_block_settings
1215
 *
1216
 * @param bool $prepend_feature_label
1217
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1218
 *
1219
 *
1220
 * @return string
1221
 *   a html representation of the given QuantitativeData element
1222
 *
1223
 * @ingroup themeable
1224
 */
1225
function compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1226
  /*
1227
   * - statisticalValues
1228
   *   - value
1229
   *   - modifiers
1230
   *   - type
1231
   * - unit->representation_L10n
1232
   * - modifyingText
1233
   * - modifiers
1234
   * - sources
1235
   */
1236

    
1237
  $out = '';
1238
  $type_representation = NULL;
1239
  $min_max = min_max_array();
1240

    
1241
  $other_values = array();
1242

    
1243
  if (isset($element->statisticalValues)) {
1244
    $other_values_markup = array();
1245
    foreach ($element->statisticalValues as $statistical_val) {
1246

    
1247
      // compile the full value string which also may contain modifiers
1248
      if (isset($statistical_val->value)) {
1249
        $statistical_val->_value = $statistical_val->value;
1250
      }
1251
      $val_modifiers_strings = cdm_modifers_representations($statistical_val);
1252
      if ($val_modifiers_strings) {
1253
        $statistical_val->_value = ' ' . $val_modifiers_strings . ' ' . $statistical_val->_value;
1254
      }
1255

    
1256
      // either put into min max array or into $other_values
1257
      // for generic output to be appended to 'min-max' string
1258
      if (array_key_exists($statistical_val->type->titleCache, $min_max)) {
1259
        $min_max[$statistical_val->type->titleCache] = $statistical_val;
1260
      }
1261
      else {
1262
        $other_values[] = $statistical_val;
1263
      }
1264
    } // end of loop over statisticalValues
1265

    
1266
    // create markup
1267

    
1268
    $min_max_markup = min_max_markup($min_max);
1269

    
1270

    
1271
    foreach ($other_values as $statistical_val) {
1272
      $statistical_val_type_representation = NULL;
1273
      if (isset($statistical_val->type)) {
1274
        $statistical_val_type_representation = cdm_term_representation($statistical_val->type);
1275
        // $statistical_val->type->termType;
1276
        // $statistical_val->type->userFriendlyTypeName;
1277
      }
1278
      $value_markup = '<span class="' . html_class_attribute_ref($statistical_val) . ' ' . $statistical_val->type->termType . ' ">'
1279
        . $statistical_val->_value . '</span>';
1280
      $value_markup = $value_markup .
1281
        ($statistical_val_type_representation ? ' <span class="type">' . $statistical_val_type_representation . '</span>' : '');
1282
      $other_values_markup[] = $value_markup;
1283
    }
1284

    
1285

    
1286
    $out .= $min_max_markup . ' ' . implode($other_values_markup, ', ');
1287
  }
1288

    
1289
  if (isset($element->unit)) {
1290
    $out .= ' <span class="unit" title="'
1291
      . cdm_term_representation($element->unit) . '">'
1292
      . cdm_term_representation_abbreviated($element->unit)
1293
      . '</span>';
1294
  }
1295

    
1296
  // modifiers of the description element itself
1297
  $modifier_string = cdm_modifers_representations($element);
1298
  $out .= ($modifier_string ? ' ' . $modifier_string : '');
1299
  if (isset($element->modifyingText_L10n)) {
1300
    $out = $element->modifyingText_L10n . ' ' . $out;
1301
  }
1302

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

    
1305
  return $render_array;
1306
}
1307

    
1308

    
1309
/**
1310
 * Wraps the render array for the given feature into an enclosing html tag.
1311
 *
1312
 * Optionally the elements can be sorted and glued together by a separator string.
1313
 *
1314
 * @param array $description_element_render_arrays
1315
 *   An list of render arrays. Which are usually are produced by the compose_description_element()
1316
 *   function. The render arrays should be of #type=html_tag, so that they can understand the #attribute property.
1317
 * @param  $feature :
1318
 *  The feature to which the elements given in $elements are belonging to.
1319
 * @param string $glue :
1320
 *  Defaults to empty string.
1321
 * @param bool $sort
1322
 *   Boolean Whether to sort the $elements alphabetically, default is FALSE
1323
 *
1324
 * @return array
1325
 *    A Drupal render array
1326
 *
1327
 * @ingroup compose
1328
 */
1329
  function compose_feature_block_wrap_elements(array $description_element_render_arrays, $feature, $glue = '', $sort = FALSE)
1330
  {
1331

    
1332
    $feature_block_settings = get_feature_block_settings($feature->uuid);
1333
    $enclosing_tag = $feature_block_settings['as_list'];
1334

    
1335
    if ($sort) { // TODO remove parameter and replace by $feature_block_settings['sort_elements']
1336
      usort($description_element_render_arrays, 'compare_description_element_render_arrays');
1337
    }
1338

    
1339
    $is_first = true;
1340
    foreach($description_element_render_arrays as &$element_render_array){
1341
      if(!is_array($element_render_array)){
1342
        $element_render_array = markup_to_render_array($element_render_array);
1343
      }
1344
      $element_render_array['#attributes']['class'][] = "feature-block-element";
1345

    
1346
      // add the glue!
1347
      if(!$is_first) {
1348
        if (isset($element_render_array['#value'])) {
1349
          $element_render_array['#value'] = $glue . $element_render_array['#value'];
1350
        } elseif (isset($element_render_array['#markup'])) {
1351
          $element_render_array['#markup'] = $glue . $element_render_array['#markup'];
1352
        }
1353
      }
1354
      $is_first = false;
1355
    }
1356

    
1357
    $render_array['elements']['children'] = $description_element_render_arrays;
1358

    
1359
    $render_array['elements']['#prefix'] = '<' . $enclosing_tag . ' class="feature-block-elements" id="' . $feature->representation_L10n . '">';
1360
    $render_array['elements']['#suffix'] = '</' . $enclosing_tag . '>';
1361

    
1362
    return $render_array;
1363
  }
1364

    
1365

    
1366
  /* compose nameInSource or originalNameString as markup
1367
   *
1368
   * @param $source
1369
   * @param $do_link_to_name_used_in_source
1370
   * @param $suppress_for_shown_taxon
1371
   *    the nameInSource will be suppressed when it has the same name as the accepted taxon
1372
   *    for which the taxon page is being created, Defaults to TRUE
1373
   *
1374
   * @return array
1375
   *    A Drupal render array with an additional element, the render array is empty
1376
   *    if the source had no name in source information
1377
   *    - #_plaintext: contains the plaintext version of the name (custom element)
1378
   *
1379
   * @ingroup compose
1380
   */
1381
  function compose_name_in_source($source, $do_link_to_name_used_in_source, $suppress_for_shown_taxon = TRUE) {
1382

    
1383
    $plaintext = NULL;
1384
    $markup = NULL;
1385
    $name_in_source_render_array = array();
1386

    
1387
    static $taxon_page_accepted_name = '';
1388
    $taxon_uuid = get_current_taxon_uuid();
1389
    if($suppress_for_shown_taxon && $taxon_uuid && empty($taxon_page_accepted_name)){
1390

    
1391
      $current_taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $taxon_uuid);
1392
      $taxon_page_accepted_name = $current_taxon->name->titleCache;
1393
    }
1394

    
1395
    if (isset($source->nameUsedInSource->uuid) && isset($source->nameUsedInSource->titleCache)) {
1396
      // it is a DescriptionElementSource !
1397
      $plaintext = $source->nameUsedInSource->titleCache;
1398
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1399
        return $name_in_source_render_array; // SKIP this name
1400
      }
1401
      $markup = render_taxon_or_name($source->nameUsedInSource);
1402
      if ($do_link_to_name_used_in_source) {
1403
        $markup = l(
1404
          $markup,
1405
          path_to_name($source->nameUsedInSource->uuid),
1406
          array(
1407
            'attributes' => array(),
1408
            'absolute' => TRUE,
1409
            'html' => TRUE,
1410
          ));
1411
      }
1412
    }
1413
    else if (isset($source->originalNameString) && !empty($source->originalNameString)) {
1414
      // the name used in source can not be expressed as valid taxon name,
1415
      // so the editor has chosen to put the freetext name into ReferencedEntityBase.originalNameString
1416
      // field
1417
      // using the originalNameString as key to avoid duplicate entries
1418
      $plaintext = $source->originalNameString;
1419
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1420
        return $name_in_source_render_array; // SKIP this name
1421
      }
1422
      $markup = $source->originalNameString;
1423
    }
1424

    
1425
    if ($plaintext) { // checks if we have any content
1426
      $name_in_source_render_array = markup_to_render_array($markup);
1427
      $name_in_source_render_array['#_plaintext'] = $plaintext;
1428
    }
1429

    
1430
    return $name_in_source_render_array;
1431
  }
1432

    
1433

    
1434

    
1435
  /**
1436
   * Return HTML for a list of description elements.
1437
   *
1438
   * Usually these are of a specific feature type.
1439
   *
1440
   * @param $description_elements
1441
   *   array of descriptionElements which belong to the same feature.
1442
   *   These descriptions elements of a Description must be ordered by the chosen feature tree by
1443
   *   calling the function _mergeFeatureTreeDescriptions().
1444
   *   @see _mergeFeatureTreeDescriptions()
1445
   *
1446
   * @param  $feature_uuid
1447
   *
1448
   * @return
1449
   *    A drupal render array for the $descriptionElements, may be an empty array if the textual content was empty.
1450
   *    Footnote key or anchors are not considered to be textual content.
1451
   *
1452
   * @ingroup compose
1453
   */
1454
  function compose_feature_block_items_generic($description_elements, $feature) {
1455

    
1456
    $elements_out_array = array();
1457
    $distribution_tree = null;
1458

    
1459
    /*
1460
     * $feature_block_has_content will be set true if at least one of the
1461
     * $descriptionElements contains some text which makes up some content
1462
     * for the feature block. Footnote keys are not considered
1463
     * to be content in this sense.
1464
     */
1465
    $feature_block_has_content = false;
1466

    
1467
    if (is_array($description_elements)) {
1468
      foreach ($description_elements as $description_element) {
1469
          /* decide based on the description element class
1470
           *
1471
           * Features handled here:
1472
           * all except DISTRIBUTION, COMMON_NAME, USES, IMAGES,
1473
           *
1474
           * TODO provide api_hook as extension point for this?
1475
           */
1476
        $feature_block_settings = get_feature_block_settings($description_element->feature->uuid);
1477
        switch ($description_element->class) {
1478
          case 'TextData':
1479
            $elements_out_array[] = compose_description_element_text_data($description_element, $description_element->feature->uuid, $feature_block_settings);
1480
            break;
1481
          case 'CategoricalData':
1482
            $elements_out_array[] = compose_description_element_categorical_data($description_element, $feature_block_settings);
1483
            break;
1484
          case 'QuantitativeData':
1485
            $elements_out_array[] = compose_description_element_quantitative_data($description_element, $feature_block_settings);
1486
            break;
1487
          case 'IndividualsAssociation':
1488
            $elements_out_array[] = compose_description_element_individuals_association($description_element, $feature_block_settings);
1489
            break;
1490
          case 'TaxonInteraction':
1491
            $elements_out_array[] = compose_description_element_taxon_interaction($description_element, $feature_block_settings);
1492
            break;
1493
          case 'CommonTaxonName':
1494
            $elements_out_array[] = compose_description_element_common_taxon_name($description_element, $feature_block_settings);
1495
            break;
1496
          case 'Uses':
1497
            /* IGNORE Uses classes, these are handled completely in theme_cdm_UseDescription */
1498
            break;
1499
          default:
1500
            $feature_block_has_content = true;
1501
            $elements_out_array[] = markup_to_render_array('<li>No method for rendering unknown description class: ' . $description_element->class . '</li>');
1502
        }
1503
        $elements_out_array_last_item = $elements_out_array[count($elements_out_array) - 1];
1504
        // considering not empty as long as the last item added is a render array
1505
        $feature_block_has_content = $feature_block_has_content || !empty($elements_out_array_last_item['#type']);
1506
      }
1507

    
1508
      // If feature = CITATION sort the list of sources.
1509
      // This is ONLY for FLORA MALESIANA and FLORE d'AFRIQUE CENTRALE.
1510
      if (isset($description_element) && $description_element->feature->uuid == UUID_CITATION) {
1511
        sort($elements_out_array);
1512
      }
1513
    }
1514

    
1515
    // sanitize: remove empty and NULL items from the render array
1516
    $tmp_out_array = $elements_out_array;
1517
    $elements_out_array = array();
1518
    foreach($tmp_out_array as $item){
1519
      if(is_array($item) && count($item) > 0){
1520
        $elements_out_array[] = $item;
1521
      }
1522
    }
1523

    
1524
    return $elements_out_array;
1525
  }
1526

    
1527
/**
1528
 * Composes block of common names for the given DescriptionElements $elements which must be of the feature CommonName
1529
 *
1530
 * @parameter $elements
1531
 *  an array of CDM DescriptionElements either of type CommonName or TextData
1532
 * @parameter $feature
1533
 *  the common feature of all $elements, must be CommonName
1534
 *
1535
 * @return
1536
 *   A drupal render array
1537
 *
1538
 * @ingroup compose
1539
 */
1540
function compose_feature_block_items_feature_common_name($elements, $feature, $weight = FALSE) {
1541

    
1542
  $common_name_out = '';
1543
  $common_name_feature_elements = array();
1544
  $textData_commonNames = array();
1545

    
1546
  $footnote_key_suggestion = 'common-names-feature-block';
1547

    
1548
  $feature_block_settings = get_feature_block_settings(UUID_COMMON_NAME);
1549

    
1550
  if (is_array($elements)) {
1551
    foreach ($elements as $element) {
1552

    
1553
      if ($element->class == 'CommonTaxonName') {
1554

    
1555
        // common name without a language or area, should not happen but is possible
1556
        $language_area_key = '';
1557
        if (isset($element->language->representation_L10n)) {
1558
          $language_area_key .= $element->language->representation_L10n ;
1559
        }
1560
        if(isset($element->area->titleCache) && strlen($element->area->titleCache) > 0){
1561
          $language_area_key .= ($language_area_key ? ' '  : '') . '(' . $element->area->titleCache . ')';
1562
        }
1563
        if($language_area_key){
1564
          $language_area_key = '<span class="language-area-label">' . $language_area_key . '<span class="separator">: </span></span>';
1565
        }
1566

    
1567
        if(isset($common_names[$language_area_key][$element->name])) {
1568
          // same name already exists for language and area combination, se we merge the description elements
1569
          cdm_merge_description_elements($common_names[$language_area_key][$element->name], $element);
1570
        } else{
1571
          // otherwise add as new entry
1572
          $common_names[$language_area_key][$element->name] = $element;
1573
        }
1574

    
1575
      }
1576
      elseif ($element->class == 'TextData') {
1577
        $textData_commonNames[] = $element;
1578
      }
1579
    }
1580
  }
1581
  // Handling common names.
1582
  if (isset($common_names) && count($common_names) > 0) {
1583
    // Sorting the array based on the key (language, + area if set).
1584
    // Comment @WA there are common names without a language, so this sorting
1585
    // can give strange results.
1586
    ksort($common_names);
1587

    
1588
    // loop over set of elements per language area
1589
    foreach ($common_names as $language_area_key => $elements) {
1590
      ksort($elements); // sort names alphabetically
1591
      $per_language_area_out = array();
1592

    
1593
      foreach ($elements as $element) {
1594
        $common_name_render_array = compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion);
1595
        $common_name_markup = drupal_render($common_name_render_array);
1596
        // IMPORTANT!
1597
        // during the above drupal_render the theme_html_tag function is executed, which adds a "\n" character to the end of the markup
1598
        // this is an error and the trailing whitespace needs to be removed
1599
        if(str_endsWith($common_name_markup, "\n")){
1600
          $common_name_markup = substr($common_name_markup, 0, strlen($common_name_markup) - 1);
1601
        }
1602
        $per_language_area_out[] = $common_name_markup;
1603
      }
1604

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

    
1608

    
1609
    $common_name_feature_elements_render_array = compose_feature_block_wrap_elements(
1610
      $common_name_feature_elements, $feature, '; ', FALSE
1611
    );
1612
    $common_name_out .= drupal_render($common_name_feature_elements_render_array); // FIXME should this be a render array instead?
1613

    
1614
  }
1615

    
1616
  // Handling commons names as text data.
1617
  $text_data_out = array();
1618

    
1619
  foreach ($textData_commonNames as $text_data_element) {
1620
    /* footnotes are not handled correctly in compose_description_element_text_data,
1621
       need to set 'common-names-feature-block' as $footnote_key_suggestion */
1622
    RenderHints::setFootnoteListKey($footnote_key_suggestion);
1623
    $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
1624
    $text_data_out[] = drupal_render($text_data_render_array);
1625
  }
1626

    
1627
  $common_name_out_text_data = compose_feature_block_wrap_elements(
1628
    $text_data_out, $feature
1629
  );
1630

    
1631
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . $footnote_key_suggestion));
1632
  $footnotes .= theme('cdm_footnotes', array('footnoteListKey' => $footnote_key_suggestion)); // FIXME is this needed at all?
1633
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnote_key_suggestion));
1634

    
1635
  return  markup_to_render_array(  // FIXME markup_to_render_array should no longer be needed
1636
    '<div class="common-taxon-name">' . $common_name_out . '</div>'
1637
    .'<div class="text-data">' . drupal_render($common_name_out_text_data) . '</div>'
1638
    .$footnotes,
1639
    $weight
1640
  );
1641
}
1642

    
1643
/**
1644
 * Renders a single instance of the type CommonTaxonName.
1645
 *
1646
 * @param $element
1647
 *   The CDM CommonTaxonName entity.
1648
 * @param $feature_block_settings
1649
 *
1650
 * @param $footnote_key_suggestion
1651
 *
1652
 * @param $element_tag_name
1653
 *
1654
 * @return array
1655
 *   Drupal render array
1656
 *
1657
 * @ingroup compose
1658
 */
1659
function compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion = NULL)
1660
{
1661

    
1662
  if(!$footnote_key_suggestion) {
1663
    $footnote_key_suggestion = $element->feature->uuid;
1664
  }
1665

    
1666
  $name = '';
1667
  if(isset($element->name)){
1668
    $name = $element->name;
1669
  }
1670

    
1671

    
1672
  return compose_description_element($element, $feature_block_settings, $name, $footnote_key_suggestion);
1673
}
1674

    
1675
/**
1676
 * Composes the render array for a CDM Distribution description element
1677
 *
1678
 * @param array $description_elements
1679
 *   Array of CDM Distribution instances
1680
 * @param $enclosingTag
1681
 *   The html tag to be use for the enclosing element
1682
 *
1683
 * @return array
1684
 *   A Drupal render array
1685
 *
1686
 * @ingroup compose
1687
 */
1688
function compose_description_elements_distribution($description_elements){
1689

    
1690
  $out = '';
1691
  RenderHints::pushToRenderStack('descriptionElementDistribution');
1692
  RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1693

    
1694
  $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
1695
  $enclosingTag = cdm_feature_block_element_tag_name($feature_block_settings);
1696

    
1697
  foreach ($description_elements as $description_element) {
1698
    $annotations_and_sources = handle_annotations_and_sources(
1699
      $description_element,
1700
      handle_annotations_and_sources_config($feature_block_settings),
1701
      $description_element->area->representation_L10n,
1702
      UUID_DISTRIBUTION
1703
    );
1704

    
1705

    
1706
    list($status_label, $status_markup) = distribution_status_label_and_markup($description_element);
1707

    
1708
    $out .= '<' . $enclosingTag . ' class="descriptionElement descriptionElement-' . $description_element->uuid
1709
      . ' " title="' . $status_label. '">'
1710
      . $description_element->area->representation_L10n
1711
      . $status_markup;
1712
    if(!empty($annotations_and_sources['source_references'])){
1713
      $out .= ' ' . join(' ', $annotations_and_sources['source_references'] );
1714
    }
1715
    $out .= $annotations_and_sources['foot_note_keys']   . ' </' . $enclosingTag . '>';
1716
  }
1717

    
1718
  RenderHints::popFromRenderStack();
1719
  return markup_to_render_array($out);
1720
}
1721

    
1722
  /**
1723
   * @param $descriptionElement
1724
   * @return array
1725
   */
1726
  function distribution_status_label_and_markup($descriptionElement, $status_glue = '&#8210; ') {
1727
    $status_markup = '';
1728
    $status_label = '';
1729

    
1730
    if (isset($descriptionElement->status)) {
1731
      $status_label = $descriptionElement->status->representation_L10n;
1732
      $status_markup =  '<span class="distributionStatus"> '
1733
        . $status_glue
1734
        . '<span class="distributionStatus-' . $descriptionElement->status->idInVocabulary . '">'
1735
        . $status_label
1736
        . '</span></span>';
1737

    
1738
    };
1739
    return array($status_label, $status_markup);
1740
  }
1741

    
1742

    
1743
  /**
1744
   * Provides the merged feature tree for a taxon profile page.
1745
   *
1746
   * The merging of the profile feature tree is actully done in
1747
   * _mergeFeatureTreeDescriptions(). See this method  for details
1748
   * on the structure of the merged tree.
1749
   *
1750
   * This method provides t hook which can be used to modify the
1751
   * merged feature tree after it has been created, see
1752
   * hook_merged_taxon_feature_tree_alter()
1753
   *
1754
   * @param $taxon
1755
   *   A CDM Taxon instance
1756
   *
1757
   * @return object
1758
   *  The merged feature tree
1759
   *
1760
   */
1761
  function merged_taxon_feature_tree($taxon) {
1762

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

    
1766

    
1767
    // 2. find the distribution feature node
1768
    $distribution_node =& cdm_feature_tree_find_node($merged_tree->root->childNodes, UUID_DISTRIBUTION);
1769

    
1770
    if ($distribution_node) {
1771
      // 3. get the distributionInfoDTO
1772
      $query_parameters = cdm_distribution_filter_query();
1773
      $query_parameters['part'] = array('mapUriParams');
1774
      if(variable_get(DISTRIBUTION_CONDENSED)){
1775
        $query_parameters['part'][] = 'condensedDistribution';
1776
      }
1777
      if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) == 'TREE') {
1778
        $query_parameters['part'][] = 'tree';
1779
      }
1780
      else {
1781
        $query_parameters['part'][] = 'elements';
1782
      }
1783
      $query_parameters['omitLevels'] = array();
1784
      foreach(variable_get(DISTRIBUTION_TREE_OMIT_LEVELS, array()) as $uuid){
1785
        if(is_uuid($uuid)){
1786
          $query_parameters['omitLevels'][] = $uuid;
1787
        }
1788
      }
1789
      $customStatusColorsJson = variable_get(DISTRIBUTION_STATUS_COLORS, NULL);
1790
      if ($customStatusColorsJson) {
1791
        $query_parameters['statusColors'] = $customStatusColorsJson;
1792
      }
1793

    
1794
      $distribution_info_dto = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION_DISTRIBUTION_INFO_FOR, $taxon->uuid, queryString($query_parameters));
1795
      // 4. get distribution TextData is there are any
1796
      $distribution_text_data = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON,
1797
        array(
1798
          'taxon' => $taxon->uuid,
1799
          'type' => 'TextData',
1800
          'features' => UUID_DISTRIBUTION
1801
        )
1802
      );
1803

    
1804
      // 5. put all distribution data into the distribution feature node
1805
      if ($distribution_text_data //if text data exists
1806
        || ($distribution_info_dto && isset($distribution_info_dto->tree) && $distribution_info_dto->tree->rootElement->numberOfChildren > 0) // OR if tree element has distribution elements
1807
        || ($distribution_info_dto && !empty($distribution_info_dto->elements))
1808
      ) { // OR if DTO has distribution elements
1809
        $distribution_node->descriptionElements = array('#type' => 'DTO');
1810
        if ($distribution_text_data) {
1811
          $distribution_node->descriptionElements['TextData'] = $distribution_text_data;
1812
        }
1813
        if ($distribution_info_dto) {
1814
          $distribution_node->descriptionElements['DistributionInfoDTO'] = $distribution_info_dto;
1815
        }
1816
      }
1817
    }
1818

    
1819
    // allows modifying the merged tree via a the hook_cdm_feature_node_block_content_alter
1820
    drupal_alter('merged_taxon_feature_tree', $taxon, $merged_tree);
1821

    
1822
    return $merged_tree;
1823
  }
1824

    
1825

    
1826
  function compose_distribution_hierarchy($distribution_tree, $feature_block_settings){
1827

    
1828
    static $hierarchy_style;
1829
    // TODO expose $hierarchy_style to administration or provide a hook
1830
    if( !isset($hierarchy_style)){
1831
      $hierarchy_style = get_array_variable_merged(DISTRIBUTION_HIERARCHY_STYLE, DISTRIBUTION_HIERARCHY_STYLE_DEFAULT);
1832
    }
1833

    
1834
    $render_array = array();
1835

    
1836
    RenderHints::pushToRenderStack('descriptionElementDistribution');
1837
    RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1838

    
1839
    // Returning NULL if there are no description elements.
1840
    if ($distribution_tree == null) {
1841
      return $render_array;
1842
    }
1843
    // for now we are not using a render array internally to avoid performance problems
1844
    $markup = '';
1845
    if (isset($distribution_tree->rootElement->children)) {
1846
      $tree_nodes = $distribution_tree->rootElement->children;
1847
      _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, $markup, $hierarchy_style);
1848
    }
1849

    
1850
    $render_array['distribution_hierarchy'] = markup_to_render_array(
1851
      $markup,
1852
      0,
1853
      '<div id="distribution_hierarchy" class="distribution_hierarchy">',
1854
      '</div>'
1855
    );
1856

    
1857
    RenderHints::popFromRenderStack();
1858

    
1859
    return $render_array;
1860
  }
1861

    
1862
  /**
1863
   * this function should produce markup as the compose_description_elements_distribution()
1864
   * function.
1865
   *
1866
   * @see compose_description_elements_distribution()
1867
   *
1868
   * @param $distribution_tree
1869
   * @param $feature_block_settings
1870
   * @param $tree_nodes
1871
   * @param $markup
1872
   * @param $hierarchy_style
1873
   */
1874
  function _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, &$markup, $hierarchy_style, $level_index = -1){
1875

    
1876
    $level_index++;
1877
    static $enclosingTag = "span";
1878

    
1879
    $level_style = array_shift($hierarchy_style);
1880
    if(count($hierarchy_style) == 0){
1881
      // lowest defined level style will be reused for all following levels
1882
      $hierarchy_style[] = $level_style;
1883
    }
1884

    
1885
    $node_index = -1;
1886
    $per_node_markup = array();
1887
    foreach ($tree_nodes as $node){
1888

    
1889
      $per_node_markup[++$node_index] = '';
1890

    
1891
      $label = $node->nodeId->representation_L10n;
1892

    
1893
      $distributions = $node->data;
1894
      $distribution_uuids = array();
1895
      $distribution_aggregate = NULL;
1896
        foreach($distributions as $distribution){
1897

    
1898
          $distribution_uuids[] = $distribution->uuid;
1899

    
1900
          // if there is more than one distribution we aggregate the sources and
1901
          // annotations into a synthetic distribution so that the footnote keys
1902
          // can be rendered consistently
1903
          if(!$distribution_aggregate) {
1904
            $distribution_aggregate = $distribution;
1905
            if(!isset($distribution_aggregate->sources[0])){
1906
              $distribution_aggregate->sources = array();
1907
            }
1908
            if(!isset($distribution_aggregate->annotations[0])){
1909
              $distribution_aggregate->annotations = array();
1910
            }
1911
          } else {
1912
            if(isset($distribution->sources[0])) {
1913
              $distribution_aggregate->sources = array_merge($distribution_aggregate->sources,
1914
                $distribution->sources);
1915
            }
1916
            if(isset($distribution->annotations[0])) {
1917
              $distribution_aggregate->annotations = array_merge($distribution_aggregate->annotations,
1918
                $distribution->annotations);
1919
            }
1920
          }
1921
        }
1922

    
1923
      $status_label= '';
1924
      $status_markup = '';
1925
      $annotations_and_sources =  null;
1926
      if($distribution_aggregate) {
1927
        $annotations_and_sources = handle_annotations_and_sources(
1928
          $distribution_aggregate,
1929
          handle_annotations_and_sources_config($feature_block_settings),
1930
          $label,
1931
          UUID_DISTRIBUTION
1932
        );
1933

    
1934
        list($status_label, $status_markup) = distribution_status_label_and_markup($distribution, $level_style['status_glue']);
1935
      }
1936

    
1937
      $per_node_markup[$node_index] .= '<' . $enclosingTag . ' class="descriptionElement'
1938
        . join(' descriptionElement-', $distribution_uuids)
1939
        . ' level_index_' . $level_index
1940
        . ' " title="' . $status_label . '">'
1941
        . '<span class="area_label">' . $label
1942
        . $level_style['label_suffix'] . '</span>'
1943
        . $status_markup
1944
      ;
1945

    
1946
      if(isset($annotations_and_sources)){
1947
        if(!empty($annotations_and_sources['source_references'])){
1948
          $per_node_markup[$node_index] .= ' ' . join(', ' , $annotations_and_sources['source_references']);
1949
        }
1950
        if($annotations_and_sources['foot_note_keys']) {
1951
          $per_node_markup[$node_index] .= $annotations_and_sources['foot_note_keys'];
1952
        }
1953
      }
1954

    
1955
      if(isset($node->children[0])){
1956
        _compose_distribution_hierarchy(
1957
          $node->children,
1958
          $feature_block_settings,
1959
          $per_node_markup[$node_index],
1960
          $hierarchy_style,
1961
          $level_index
1962
        );
1963
      }
1964

    
1965
      $per_node_markup[$node_index] .= '</' . $enclosingTag . '>';
1966
    }
1967
    $markup .= $level_style['item_group_prefix']  . join( $level_style['item_glue'], $per_node_markup) . $level_style['item_group_postfix'];
1968
  }
1969

    
1970

    
(2-2/10)