Project

General

Profile

Download (68.6 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
gca       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
  if($feature_block_settings['sources_as_content'] !== 1 || $feature_block_settings['sources_as_content_to_bibliography'] == 1) {
506
    $config['add_footnote_keys'] = 1;
507
  }
508
  $config['bibliography_aware'] = 1;
509

    
510
  return $config;
511
}
512

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

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

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

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

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

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

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

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

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

    
585
    } // END of references inline
586

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

    
606
    return $annotations_and_sources;
607
  }
608

    
609

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

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

    
648

    
649
/* ==================== COMPOSE FUNCTIONS =============== */
650

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

    
674
    $block_list = array();
675

    
676
    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME);
677

    
678

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

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

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

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

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

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

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

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

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

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

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

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

    
763
    RenderHints::popFromRenderStack();
764

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

    
767
    return _block_get_renderable_array($block_list);
768
  }
769

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

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

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

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

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

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

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

    
835
  return $render_arrays;
836
}
837

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

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

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

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

    
868
    return markup_to_render_array('');
869
  }
870

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

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

    
896
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
897

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

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

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

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

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

    
937

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

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

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

    
957
    $dto_out_array = array();
958

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

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

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

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

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

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

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

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

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

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

    
1046
    return $block;
1047
  }
1048

    
1049

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

    
1071
    $footnote_list_key_suggestion = $feature_uuid;
1072

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

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

    
1081
    return $render_array;
1082
  }
1083

    
1084

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

    
1098
  $out = '';
1099

    
1100

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

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

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

    
1111
  return $render_array;
1112
}
1113

    
1114

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

    
1129
  $out = '';
1130

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

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

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

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

    
1141
  return $render_array;
1142
}
1143

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

    
1162
  $enclosing_tag = cdm_feature_block_element_tag_name($feature_block_settings);
1163

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

    
1168
      $state  = NULL;
1169

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

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

    
1178
      $modifiers_strings = cdm_modifers_representations($state_data);
1179

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

    
1182
    }
1183
  }
1184

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

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

    
1189
  return $render_array;
1190
}
1191

    
1192

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

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

    
1240
  $other_values = array();
1241

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

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

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

    
1265
    // create markup
1266

    
1267
    $min_max_markup = min_max_markup($min_max);
1268

    
1269

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

    
1284

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

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

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

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

    
1304
  return $render_array;
1305
}
1306

    
1307

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

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

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

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

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

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

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

    
1361
    return $render_array;
1362
  }
1363

    
1364

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

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

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

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

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

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

    
1429
    return $name_in_source_render_array;
1430
  }
1431

    
1432

    
1433

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

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

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

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

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

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

    
1523
    return $elements_out_array;
1524
  }
1525

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

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

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

    
1547
  $feature_block_settings = get_feature_block_settings(UUID_COMMON_NAME);
1548

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

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

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

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

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

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

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

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

    
1607

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

    
1613
  }
1614

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

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

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

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

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

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

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

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

    
1670

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

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

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

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

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

    
1704

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

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

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

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

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

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

    
1741

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

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

    
1765

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

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

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

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

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

    
1821
    return $merged_tree;
1822
  }
1823

    
1824

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

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

    
1833
    $render_array = array();
1834

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

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

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

    
1856
    RenderHints::popFromRenderStack();
1857

    
1858
    return $render_array;
1859
  }
1860

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1969

    
(2-2/10)