Project

General

Profile

Download (65.3 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
  // new default, TODO put into settings #5677
77
  $query['distributionOrder'] = 'AREA_ORDER';
78

    
79
  $query['recipe'] = variable_get(DISTRIBUTION_CONDENSED_RECIPE, DISTRIBUTION_CONDENSED_RECIPE_DEFAULT);
80

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

    
96
  return $query;
97
}
98

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

    
110
  foreach ($fields_to_merge as $field) {
111
    if (is_array($source->$field)) {
112
      if (!is_array($target->$field)) {
113
        $target->$field = $source->$field;
114
      }
115
      else {
116
        $target->$field = array_merge($target->$field, $source->$field);
117
      }
118
    }
119
  }
120
}
121

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

    
140
  if (!$fragment) {
141
    $fragment = $label;
142
  }
143
  $fragment = generalizeString($fragment);
144

    
145
  $class_attributes = 'feature-toc-item-' . $class_attribute_suffix;
146

    
147
  $new_item = toc_list_item(
148
    theme(
149
      'cdm_feature_name',
150
      array('feature_name' => $label)),
151
      array('class' => $class_attributes),
152
      $fragment
153
    );
154

    
155
  if ($as_first_element) {
156
    array_unshift($toc_list_items, $new_item);
157
  }
158
  else {
159
    $toc_list_items[] = $new_item;
160
  }
161

    
162
}
163

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

    
175
  return $toc_list_items;
176
}
177

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

    
205

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

    
233
    case "MediaKey":
234
      $cdm_ws_pasepath = CDM_WS_MEDIAKEY;
235
      break;
236

    
237
    case "MultiAccessKey":
238
      $cdm_ws_pasepath = CDM_WS_MULTIACCESSKEY;
239
      break;
240

    
241
  }
242

    
243
  if (!$cdm_ws_pasepath) {
244
    drupal_set_message(t('Type parameter is not valid: ') . $type, 'error');
245
  }
246

    
247
  $queryParameters = '';
248
  if (is_numeric($pageSize)) {
249
    $queryParameters = "pageSize=" . $pageSize;
250
  }
251
  else {
252
    $queryParameters = "pageSize=0";
253
  }
254

    
255
  if (is_numeric($pageNumber)) {
256
    $queryParameters = "pageNumber=" . $pageNumber;
257
  }
258
  else {
259
    $queryParameters = "pageNumber=0";
260
  }
261
  $queryParameters = NULL;
262
  if ($taxonUuid) {
263
    $queryParameters = "findByTaxonomicScope=$taxonUuid";
264
  }
265
  $pager = cdm_ws_get($cdm_ws_pasepath, NULL, $queryParameters);
266

    
267
  if (!$pager || $pager->count == 0) {
268
    return array();
269
  }
270
  return $pager->records;
271
}
272

    
273

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

    
286
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
287
  $http_request_params = drupal_static('http_request_params', drupal_get_query_parameters());
288

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

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

    
325

    
326
  // Annotations as footnotes.
327
  $footNoteKeys = cdm_annotations_as_footnotekeys($description_element, $footnote_list_key_suggestion);
328

    
329
  // Source references as footnotes.
330
  $bibliography_settings = get_bibliography_settings();
331
  $original_source_footnote_tag = $bibliography_settings['enabled'] == 1 ? 'div' : null; // null will cause original_source_footnote_list_key to use the default
332

    
333
  usort($description_element->sources, 'compare_original_sources');
334
  foreach ($description_element->sources as $source) {
335
    if (_is_original_source_type($source)) {
336
      $fn_key = FootnoteManager::addNewFootnote(
337
        original_source_footnote_list_key($footnote_list_key_suggestion),
338
        theme('cdm_OriginalSource', array(
339
          'source' => $source,
340
          'doLink' => $do_link_to_reference,
341
          'do_link_to_name_used_in_source' => $do_link_to_name_used_in_source
342

    
343
        )),
344
        $original_source_footnote_tag
345
      );
346
      // Ensure uniqueness of the footnote keys.
347
      cdm_add_footnote_to_array($footNoteKeys, $fn_key);
348
    }
349
  }
350
  // Sort and render footnote keys.
351
  $footnoteKeyListStr = '';
352
  asort($footNoteKeys);
353
  foreach ($footNoteKeys as $footNoteKey) {
354
    $footnoteKeyListStr .= theme('cdm_footnote_key',
355
      array(
356
        'footnoteKey' => $footNoteKey,
357
        'separator' => ($footnoteKeyListStr ? $separator : '')));
358
  }
359
  return $footnoteKeyListStr;
360
}
361

    
362

    
363
/**
364
 * Compare callback to be used in usort() to sort render arrays produced by compose_description_element().
365
 *
366
 * @param $a
367
 * @param $b
368
 */
369
function compare_description_element_render_arrays($a, $b){
370
  if ($a['#value'].$a['#value-suffix'] == $b['#value'].$b['#value-suffix']) {
371
    return 0;
372
  }
373
  return ($a['#value'].$a['#value-suffix'] < $b['#value'].$b['#value-suffix']) ? -1 : 1;
374

    
375
}
376

    
377

    
378
/**
379
 * @param $render_array
380
 * @param $element
381
 * @param $feature_block_settings
382
 * @param $element_markup
383
 * @param $footnote_list_key_suggestion
384
 */
385
function compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label = FALSE)
386
{
387

    
388
  $render_array = array(
389
    '#type' => 'html_tag',
390
    '#tag' => cdm_feature_block_element_tag_name($feature_block_settings),
391

    
392
    '#attributes' => array(
393
      'class' => array(
394
        'DescriptionElement',
395
        'DescriptionElement-' . $element->class,
396
        html_class_attribute_ref($element)
397
      )
398
    ),
399

    
400
    '#value' => '',
401
    '#value_suffix' => NULL
402

    
403
  );
404

    
405
  $annotations_and_sources = handle_annotations_and_sources($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion);
406

    
407
  // handle the special case were the TextData is used as container for original source with name
408
  // used in source information without any text stored in it.
409
  $names_used_in_source_markup = '';
410
  if (!empty($annotations_and_sources['names_used_in_source']) && empty($element_markup)) {
411
    $names_used_in_source_markup = join(', ', $annotations_and_sources['names_used_in_source']) . ': ';
412
    // remove all <span class="nameUsedInSource">...</span> from all source_references
413
    // these are expected to be at the end of the strings
414
    $pattern = '/ <span class="nameUsedInSource">.*$/';
415
    foreach( $annotations_and_sources['source_references'] as &$source_reference){
416
      $source_reference = preg_replace($pattern , '', $source_reference);
417
    }
418
  }
419

    
420

    
421
  $source_references_markup = '';
422
  if (!empty($annotations_and_sources['source_references'])) {
423
    $source_references_markup = '<span class="sources">' . join(' ', $annotations_and_sources['source_references']) . '<span>';
424
  }
425

    
426
  $feature_label = '';
427
  if ($prepend_feature_label) {
428
    $feature_label = '<span class="nested-feature-tree-feature-label">' . $element->feature->representation_L10n . ':</span> ';
429
  }
430
  $render_array['#value'] = $feature_label . $names_used_in_source_markup . $element_markup . $source_references_markup;
431
  $render_array['#value_suffix'] = $annotations_and_sources['foot_note_keys'];
432

    
433
  return $render_array;
434
}
435

    
436

    
437
  /**
438
   * @param $element
439
   * @param $feature_block_settings
440
   * @param $element_text
441
   *   used to decide if the source references should be enclosed in brackets or not
442
   * @param $footnote_list_key_suggestion
443
   * @return array
444
   *   an associative array with the following elements:
445
   *   - foot_note_keys: all footnote keys as markup
446
   *   - source_references: an array of the source references citations
447
   *   - names used in source: an associative array of the names in source,
448
   *        the name in source strings are de-duplicated
449
   *        !!!NOTE!!!!: this field will most probably be removed soon (TODO)
450
   *
451
   *
452
   */
453
  function handle_annotations_and_sources($element, $feature_block_settings, $element_text, $footnote_list_key_suggestion) {
454
    $annotations_and_sources = array(
455
      'foot_note_keys' => NULL,
456
      'source_references' => array(),
457
      'names_used_in_source' => array()
458
    );
459

    
460
    usort($element->sources, 'compare_original_sources');
461

    
462
    if ($feature_block_settings['sources_as_content'] == 1) {
463
      foreach ($element->sources as $source) {
464

    
465
        $referenceCitation = theme('cdm_OriginalSource',
466
          array(
467
            'source' => $source,
468
            'doLink' => $feature_block_settings['link_to_reference'] == 1,
469
            'do_link_to_name_used_in_source' => $feature_block_settings['link_to_name_used_in_source'] == 1,
470
          )
471
        );
472

    
473
        if ($referenceCitation) {
474
          if (empty($element_text)) {
475
            $annotations_and_sources['source_references'][] = $referenceCitation;
476
          }
477
          else {
478
            $annotations_and_sources['source_references'][] = ' (' . $referenceCitation . ')';
479
          }
480
        }
481

    
482
        $name_in_source_render_array = compose_name_in_source(
483
          $source,
484
          $feature_block_settings['link_to_name_used_in_source'] == 1
485
        );
486

    
487
        if(!empty($name_in_source_render_array)){
488
          $annotations_and_sources['names_used_in_source'][$name_in_source_render_array['#_plaintext']] = drupal_render($name_in_source_render_array);
489
        }
490
      } // END of loop over sources
491

    
492
      // annotations footnotes separate.
493
      $annotations_and_sources['foot_note_keys'] = theme('cdm_annotations_as_footnotekeys',
494
        array(
495
          'cdmBase_list' => $element,
496
          'footnote_list_key' => $footnote_list_key_suggestion,
497
        )
498
      );
499

    
500
    } // END of references inline
501

    
502
    // put sources into bibliography if requested ...
503
    if ($feature_block_settings['sources_as_content'] !== 1 || $feature_block_settings['sources_as_content_to_bibliography'] == 1) {
504
      $annotations_and_sources['foot_note_keys'] = cdm_create_description_element_footnotes(
505
        $element, ',',
506
        $footnote_list_key_suggestion,
507
        $feature_block_settings['link_to_reference'] == 1,
508
        $feature_block_settings['link_to_name_used_in_source'] == 1
509
      );
510
    }
511

    
512
    return $annotations_and_sources;
513
  }
514

    
515

    
516
  /**
517
   *
518
   *
519
   * @return string
520
   *  the footnote_list_key
521
   */
522
  function original_source_footnote_list_key($key_suggestion = null) {
523
    if(!$key_suggestion){
524
      $key_suggestion = RenderHints::getFootnoteListKey();
525
    }
526
    $bibliography_settings = get_bibliography_settings();
527
    $footnote_list_key = $bibliography_settings['enabled'] == 1 ? 'BIBLIOGRAPHY' : 'BIBLIOGRAPHY-' . $key_suggestion;
528
    return $footnote_list_key;
529
  }
530

    
531
  /**
532
   * Provides the according tag name for the description element markup which fits the  $feature_block_settings['as_list'] value
533
   *
534
   * @param $feature_block_settings
535
   *   A feature_block_settings array, for details, please see get_feature_block_settings($feature_uuid = 'DEFAULT')
536
   */
537
  function cdm_feature_block_element_tag_name($feature_block_settings){
538
    switch ($feature_block_settings['as_list']){
539
      case 'ul':
540
      case 'ol':
541
        return 'li';
542
      case 'div':
543
        if(isset($feature_block_settings['element_tag'])){
544
          return $feature_block_settings['element_tag'];
545
        }
546
        return 'span';
547
      case 'dl':
548
        return 'dd';
549
      default:
550
        return 'div'; // should never happen, throw error instead?
551
    }
552
  }
553

    
554

    
555
/* ==================== COMPOSE FUNCTIONS =============== */
556

    
557
  /**
558
   * Returns a set of feature blocks for a taxon profile from the $mergedFeatureNodes of a given $taxon.
559
   *
560
   * The taxon profile consists of drupal block elements, one for the description elements
561
   * of a specific feature. The structure is defined by specific FeatureTree.
562
   * The chosen FeatureTree is merged with the list of description elements prior to using this method.
563
   *
564
   * The merged nodes can be obtained by making use of the
565
   * function cdm_ws_descriptions_by_featuretree().
566
   *
567
   * @see cdm_ws_descriptions_by_featuretree()
568
   *
569
   * @param $mergedFeatureNodes
570
   *
571
   * @param $taxon
572
   *
573
   * @return array
574
   *  A Drupal render array containing feature blocks and the table of content
575
   *
576
   * @ingroup compose
577
   */
578
  function compose_feature_blocks($mergedFeatureNodes, $taxon) {
579

    
580
    $block_list = array();
581

    
582
    RenderHints::pushToRenderStack('feature_nodes');
583

    
584
    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME);
585

    
586
    // Create a drupal block for each feature
587
    foreach ($mergedFeatureNodes as $node) {
588

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

    
592
        $feature_name = cdm_term_representation($node->feature, 'Unnamed Feature');
593
        $feature_block_settings = get_feature_block_settings($node->feature->uuid);
594

    
595
        $block = feature_block($feature_name, $node->feature);
596
        $block->content = array();
597
        $block_content_is_empty = TRUE;
598

    
599
        /*
600
         * Content/DISTRIBUTION.
601
         */
602

    
603
        if ($node->feature->uuid == UUID_DISTRIBUTION) {
604
          $block = compose_feature_block_distribution($taxon, $node->descriptionElements, $node->feature);
605
          $block_content_is_empty = FALSE;
606
        }
607
        /*
608
         * Content/COMMON_NAME.
609
         */
610
        else if ($node->feature->uuid == UUID_COMMON_NAME) {
611
          $common_names_render_array = compose_feature_block_items_feature_common_name($node->descriptionElements, $node->feature);
612
          $block->content[] = $common_names_render_array;
613
          $block_content_is_empty = FALSE;
614
        }
615

    
616
        else if ($node->feature->uuid == UUID_USE_RECORD) {
617
          $block_uses_content_html = theme('cdm_block_Uses', array('taxonUuid' => $taxon->uuid));
618
          $block->content[] = markup_to_render_array($block_uses_content_html);
619
          $block_content_is_empty = FALSE;
620
        }
621

    
622
        /*
623
         * Content/ALL OTHER FEATURES.
624
         */
625
        else {
626

    
627
          $media_list = array();
628
          $elements_render_array = array();
629
          $child_elements_render_array = null;
630

    
631
          if (isset($node->descriptionElements[0])) {
632
            $elements_render_array = compose_feature_block_items_generic($node->descriptionElements, $node->feature);
633
          }
634

    
635
          // Content/ALL OTHER FEATURES/Subordinate Features
636
          // subordinate features are printed inline in one floating text,
637
          // it is expected hat subordinate features can "contain" TextData,
638
          // Qualitative- and Qualitative- DescriptionElements
639
          if (isset($node->childNodes[0])) {
640
            $child_elements_render_array = compose_feature_block_items_nested($node, $media_list, $feature_block_settings);
641
            $elements_render_array = array_merge($elements_render_array, $child_elements_render_array);
642
          }
643
          $block_content_is_empty = $block_content_is_empty && empty($media_list) && empty($elements_render_array);
644
          if(!$block_content_is_empty){
645
            $block->content[] = compose_feature_block_wrap_elements($elements_render_array, $node->feature);
646
            $block->content[] = compose_feature_media_gallery($node, $media_list, $gallery_settings);
647
            /*
648
             * Footnotes for the feature block
649
             */
650
            $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . $node->feature->uuid)));
651
            $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => $node->feature->uuid)));
652
            $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => $node->feature->uuid)));
653
          }
654
        } // END all other features
655

    
656
        // allows modifying the block contents via a the hook_cdm_feature_node_block_content_alter
657
        drupal_alter('cdm_feature_node_block_content', $block->content, $node->feature, $node->descriptionElements);
658

    
659
        if(!$block_content_is_empty){ // skip empty block content
660
          $block_list[] = $block;
661
          cdm_toc_list_add_item(cdm_term_representation($node->feature), $node->feature->uuid);
662
        } // END: skip empty block content
663
      } // END: skip empty or suppressed features
664
    } // END: creating a block per feature
665

    
666
    drupal_alter('cdm_feature_node_blocks', $block_list, $taxon);
667

    
668
    RenderHints::popFromRenderStack();
669

    
670
    return _block_get_renderable_array($block_list);
671
  }
672

    
673
/**
674
 * Creates a render array of description elements  held by child nodes of the given feature node.
675
 *
676
 * This function is called recursively!
677
 *
678
 * @param $node
679
 *   The feature node.
680
 * @param array $media_list
681
 *   List of CDM Media entities. All media of subordinate elements should be passed to the main feature.ä
682
 * @param $feature_block_settings
683
 *   The feature block settings.
684
 * @param $main_feature
685
 *  Only used internally in recursive calls.
686
 *
687
 * @return array
688
 *  A Drupal render array
689
 *
690
 * @ingroup compose
691
 */
692
function compose_feature_block_items_nested($node, &$media_list, $feature_block_settings, $main_feature = NULL)
693
{
694

    
695
  if(!$main_feature){
696
    $main_feature = $node->feature;
697
  }
698
  /*
699
   * TODO should be configurable, options; YES, NO, AUTOMATIC
700
   * (automatic will only place the label if the first word of the description element text is not the same)
701
   */
702
  $prepend_feature_label = false;
703

    
704
  $render_arrays = array();
705
  foreach ($node->childNodes as $child_node) {
706
    if (isset($child_node->descriptionElements[0])) {
707
      foreach ($child_node->descriptionElements as $element) {
708

    
709
        if (isset($element->media[0])) {
710
          // Append media of subordinate elements to list of main
711
          // feature.
712
          $media_list = array_merge($media_list, $element->media);
713
        }
714

    
715
        $child_node_element = null;
716
        switch ($element->class) {
717
          case 'TextData':
718
            $child_node_element = compose_description_element_text_data($element, $element->feature->uuid, $feature_block_settings, $prepend_feature_label);
719
            break;
720
          case 'CategoricalData':
721
            $child_node_element = compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label);
722
            break;
723
          case 'QuantitativeData':
724
            $child_node_element = compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label);
725

    
726
        }
727
        if (is_array($child_node_element)) {
728
          $render_arrays[] = $child_node_element;
729
        }
730
      }
731
    }
732

    
733
    if(isset($child_node->childNodes[0])){
734
      $render_arrays = array_merge($render_arrays, compose_feature_block_items_nested($child_node, $media_list, $feature_block_settings, $main_feature ));
735
    }
736
  }
737

    
738
  return $render_arrays;
739
}
740

    
741
  /**
742
   *
743
   * @param $node
744
   *  The merged feature three node which potentially contains media in its description elements.
745
   * @param $media_list
746
   *    Additional media to be merged witht the media contained in the nodes description elements
747
   * @param $gallery_settings
748
   * @return array
749
   *
750
   * @ingroup compose
751
   */
752
  function compose_feature_media_gallery($node, $media_list, $gallery_settings) {
753

    
754
    if (isset($node->descriptionElements)) {
755
      $media_list = array_merge($media_list, cdm_dataportal_media_from_descriptionElements($node->descriptionElements));
756
    }
757

    
758
    $captionElements = array('title', 'rights');
759

    
760
    if (isset($media_list[0]) && isset($gallery_settings['cdm_dataportal_media_maxextend']) && isset($gallery_settings['cdm_dataportal_media_cols'])) {
761
      $gallery = theme('cdm_media_gallerie', array(
762
        'mediaList' => $media_list,
763
        'galleryName' => CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME . '_' . $node->feature->uuid,
764
        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
765
        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
766
        'captionElements' => $captionElements,
767
      ));
768
      return markup_to_render_array($gallery);
769
    }
770

    
771
    return markup_to_render_array('');
772
  }
773

    
774
  /**
775
   * Composes the distribution feature block for a taxon
776
   *
777
   * @param $taxon
778
   * @param $descriptionElements
779
   *   an associative array with two elements:
780
   *   - '#type': must be 'DTO'
781
   *   - 'DistributionInfoDTO': a CDM DistributionInfoDTO object as returned by the DistributionInfo web service
782
   * @param $feature
783
   *
784
   * @return array
785
   *  A drupal render array
786
   *
787
   * @ingroup compose
788
   */
789
  function compose_feature_block_distribution($taxon, $descriptionElements, $feature) {
790
    $text_data_glue = '';
791
    $text_data_sortOutArray = FALSE;
792
    $text_data_enclosingTag = 'ul';
793
    $text_data_out_array = array();
794

    
795
    $distributionElements = NULL;
796
    $distribution_info_dto = NULL;
797
    $distribution_sortOutArray = FALSE;
798

    
799
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
800

    
801
    // TODO use feature_block_settings instead of DISTRIBUTION_ORDER_MODE
802
    if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) != 'TREE') {
803
      $distribution_glue = '';
804
      $distribution_enclosingTag = 'dl';
805
    } else {
806
      $distribution_glue = '';
807
      $distribution_enclosingTag = 'ul';
808
    }
809

    
810
    if (!isset($descriptionElements['#type']) || !$descriptionElements['#type'] == 'DTO') {
811
      // skip the DISTRIBUTION section if there is no DTO type element
812
      return array(); // FIXME is it ok to return an empty array?
813
    }
814

    
815
    $block = feature_block(
816
      cdm_term_representation($feature, 'Unnamed Feature'),
817
      $feature
818
    );
819

    
820
    // $$descriptionElements['TextData'] is added to to the feature node in merged_taxon_feature_tree()
821
    if (isset($descriptionElements['TextData'])) {
822
      // --- TextData
823
      foreach ($descriptionElements['TextData'] as $text_data_element) {
824
        $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
825
        $repr = drupal_render($text_data_render_array);
826

    
827
        if (!array_search($repr, $text_data_out_array)) { // de-duplication !!
828
          $text_data_out_array[] = $repr;
829
          // TODO HINT: sorting in compose_feature_block_wrap_elements will
830
          // not work since this array contains html attributes with uuids
831
          // and what is about cases like the bibliography where
832
          // any content can be prefixed with some foot-note anchors?
833
          $text_data_sortOutArray = TRUE;
834
          $text_data_glue = '<br/> ';
835
          $text_data_enclosingTag = 'p';
836
        }
837
      }
838
    }
839

    
840

    
841
    if ($text_data_out_array && variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
842
      $block->content[] = compose_feature_block_wrap_elements(
843
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
844
      );
845
    }
846

    
847
    // --- Distribution map
848
    $distribution_map_query_parameters = NULL;
849
    if (isset($descriptionElements['DistributionInfoDTO'])) {
850
      $distribution_map_query_parameters = $descriptionElements['DistributionInfoDTO']->mapUriParams;
851
    }
852
    $map_render_element = compose_distribution_map($taxon, $distribution_map_query_parameters);
853
    $block->content[] = $map_render_element;
854

    
855
    $dto_out_array = array();
856

    
857
    // --- Condensed Distribution
858
    if(variable_get(DISTRIBUTION_CONDENSED) && isset($descriptionElements['DistributionInfoDTO']->condensedDistribution)){
859
      $condensed_distribution_markup = '<p class="condensed_distribution">';
860

    
861
      $isFirst = true;
862
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous[0])){
863
        foreach($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous as $cdItem){
864
          if(!$isFirst){
865
            $condensed_distribution_markup .= ' ';
866
          }
867
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->idInVocabulary . '">'
868
          . $cdItem->areaStatusLabel . '</span>';
869
          $isFirst = false;
870
        }
871
      }
872

    
873
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign[0])) {
874
        if(!$isFirst){
875
          $condensed_distribution_markup .= ' ';
876
        }
877
        $isFirst = TRUE;
878
        $condensed_distribution_markup .= '[';
879
        foreach ($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign as $cdItem) {
880
          if (!$isFirst) {
881
            $condensed_distribution_markup .= ' ';
882
          }
883
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->titleCache . '">'
884
            . $cdItem->areaStatusLabel . '</span>';
885
          $isFirst = false;
886
        }
887
        $condensed_distribution_markup .= ']';
888
      }
889

    
890
      $condensed_distribution_markup .= '&nbsp;' . l(
891
          font_awesome_icon_markup(
892
            'fa-info-circle',
893
            array(
894
              'alt'=>'help',
895
              'class' => array('superscript')
896
            )
897
          ),
898
          variable_get(DISTRIBUTION_CONDENSED_INFO_PATH, DISTRIBUTION_CONDENSED_INFO_PATH_DEFAULT) ,
899
          array('html' => TRUE));
900
      $condensed_distribution_markup .= '</p>';
901
      $dto_out_array[] = $condensed_distribution_markup;
902
    }
903

    
904
    // --- tree or list
905
    if (isset($descriptionElements['DistributionInfoDTO'])) {
906
      $distribution_info_dto = $descriptionElements['DistributionInfoDTO'];
907

    
908
      // --- tree
909
      if (is_object($distribution_info_dto->tree)) {
910
        $distribution_tree_render_array = compose_distribution_hierarchy($distribution_info_dto->tree, $feature_block_settings);
911
        $dto_out_array[] = $distribution_tree_render_array;
912
      }
913

    
914
      // --- sorted element list
915
      if (is_array($distribution_info_dto->elements) && count($distribution_info_dto->elements) > 0) {
916
        foreach ($distribution_info_dto->elements as $descriptionElement) {
917
          if (is_object($descriptionElement->area)) {
918
            $sortKey = $descriptionElement->area->representation_L10n;
919
            $distributionElements[$sortKey] = $descriptionElement;
920
          }
921
        }
922
        ksort($distributionElements);
923
        $distribution_element_render_array = compose_description_elements_distribution($distributionElements);
924
        $dto_out_array[] = $distribution_element_render_array;
925

    
926
      }
927
      //
928
      $block->content[] = compose_feature_block_wrap_elements(
929
        $dto_out_array, $feature, $distribution_glue, $distribution_sortOutArray
930
      );
931
    }
932

    
933
    // --- TextData at the bottom
934
    if ($text_data_out_array && !variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
935
      $block->content[] = compose_feature_block_wrap_elements(
936
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
937
      );
938
    }
939

    
940
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . UUID_DISTRIBUTION)));
941
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
942
    $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
943

    
944
    return $block;
945
  }
946

    
947

    
948
  /**
949
   * Composes a drupal render array for single CDM TextData description element.
950
   *
951
   * @param $element
952
   *    The CDM TextData description element.
953
   *  @param $feature_uuid
954
   * @param bool $prepend_feature_label
955
   *   Used in nested feature trees to put the feature as label in front of the description element text representation.
956
   *
957
   * @return array
958
   *   A drupal render array with the following elements being used:
959
   *    - #tag: either 'div', 'li', ...
960
   *    ⁻ #attributes: class attributes
961
   *    - #value_prefix: (optionally) contains footnote anchors
962
   *    - #value: contains the textual content
963
   *    - #value_suffix: (optionally) contains footnote keys
964
   *
965
   * @ingroup compose
966
   */
967
  function compose_description_element_text_data($element, $feature_uuid, $feature_block_settings, $prepend_feature_label = FALSE) {
968

    
969
    $footnote_list_key_suggestion = $feature_uuid;
970

    
971
    $element_markup = '';
972
    if (isset($element->multilanguageText_L10n->text)) {
973
      // TODO replacement of \n by <br> should be configurable
974
      $element_markup = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
975
    }
976

    
977
    $render_array = compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label);
978

    
979
    return $render_array;
980
  }
981

    
982

    
983
/**
984
 * Theme function to render CDM DescriptionElements of the type TaxonInteraction.
985
 *
986
 * @param $element
987
 *  The CDM TaxonInteraction entity
988
 *
989
 * @return
990
 *  A drupal render array
991
 *
992
 * @ingroup compose
993
 */
994
function compose_description_element_taxon_interaction($element, $feature_block_settings) {
995

    
996
  $out = '';
997

    
998

    
999
  if (isset($element->description_L10n)) {
1000
    $out .=  ' ' . $element->description_L10n;
1001
  }
1002

    
1003
  if(isset($element->taxon2)){
1004
    $out = render_taxon_or_name($element->taxon2, url(path_to_taxon($element->taxon2->uuid)));
1005
  }
1006

    
1007
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1008

    
1009
  return $render_array;
1010
}
1011

    
1012

    
1013
/**
1014
 * Renders a single instance of the type IndividualsAssociations.
1015
 *
1016
 * @param $element
1017
 *   The CDM IndividualsAssociations entity.
1018
 * @param $feature_block_settings
1019
 *
1020
 * @return array
1021
 *   Drupal render array
1022
 *
1023
 * @ingroup compose
1024
 */
1025
function compose_description_element_individuals_association($element, $feature_block_settings) {
1026

    
1027
  $out = '';
1028

    
1029
  $render_array = compose_cdm_specimenOrObservation($element->associatedSpecimenOrObservation);
1030

    
1031
  if (isset($element->description_L10n)) {
1032
    $out .=  ' ' . $element->description_L10n;
1033
  }
1034

    
1035
  $out .= drupal_render($render_array);
1036

    
1037
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1038

    
1039
  return $render_array;
1040
}
1041

    
1042
/**
1043
 * Renders a single instance of the type CategoricalData.
1044
 *
1045
 * @param $element
1046
 *  The CDM CategoricalData entity
1047
 *
1048
 * @param $feature_block_settings
1049
 *
1050
 * @param bool $prepend_feature_label
1051
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1052
 *
1053
 * @return string
1054
 *   a html representation of the given CategoricalData element
1055
 *
1056
 * @ingroup compose
1057
 */
1058
function compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1059

    
1060
  $enclosing_tag = cdm_feature_block_element_tag_name($feature_block_settings);
1061

    
1062
  $state_data_strings = array();
1063
  if (isset($element->stateData)) {
1064
    foreach ($element->stateData as $state_data) {
1065

    
1066
      $state  = NULL;
1067

    
1068
      if (isset($state_data->state)) {
1069
        $state = cdm_term_representation($state_data->state);
1070
      }
1071

    
1072
      if (isset($state_data->modifyingText_L10n)) {
1073
        $state = ' ' . $state_data->modifyingText_L10n;
1074
      }
1075

    
1076
      $modifiers_strings = cdm_modifers_representations($state_data);
1077

    
1078
      $state_data_strings[] = $state . ($modifiers_strings ? ' ' . $modifiers_strings : '');
1079

    
1080
    }
1081
  }
1082

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

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

    
1087
  return $render_array;
1088
}
1089

    
1090

    
1091
/**
1092
 * Theme function to render CDM DescriptionElements of the type QuantitativeData.
1093
 *
1094
 * The function renders the statisticalValues contained in the QuantitativeData
1095
 * entity according to the following scheme:
1096
 *
1097
 * (ExtremeMin)-Min-Average-Max-(ExtremeMax)
1098
 *
1099
 * All modifiers of these values are appended.
1100
 *
1101
 * If the QuantitativeData is containing more statisticalValues with further
1102
 * statisticalValue types, these additional measures will be appended to the
1103
 * above string separated by whitespace.
1104
 *
1105
 * Special cases;
1106
 * 1. Min==Max: this will be interpreted as Average
1107
 *
1108
 * @param $element
1109
 *  The CDM QuantitativeData entity
1110
 *
1111
 * @param $feature_block_settings
1112
 *
1113
 * @param bool $prepend_feature_label
1114
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1115
 *
1116
 *
1117
 * @return string
1118
 *   a html representation of the given QuantitativeData element
1119
 *
1120
 * @ingroup themeable
1121
 */
1122
function compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1123
  /*
1124
   * - statisticalValues
1125
   *   - value
1126
   *   - modifiers
1127
   *   - type
1128
   * - unit->representation_L10n
1129
   * - modifyingText
1130
   * - modifiers
1131
   * - sources
1132
   */
1133

    
1134
  $out = '';
1135
  $type_representation = NULL;
1136
  $min_max = min_max_array();
1137

    
1138

    
1139
  $other_values = array();
1140

    
1141
  if (isset($element->statisticalValues)) {
1142
    $other_values_markup = array();
1143
    foreach ($element->statisticalValues as $statistical_val) {
1144

    
1145
      // compile the full value string which also may contain modifiers
1146
      if (isset($statistical_val->value)) {
1147
        $statistical_val->_value = $statistical_val->value;
1148
      }
1149
      $val_modifiers_strings = cdm_modifers_representations($statistical_val);
1150
      if ($val_modifiers_strings) {
1151
        $statistical_val->_value = ' ' . $val_modifiers_strings . ' ' . $statistical_val->_value;
1152
      }
1153

    
1154
      // either put into min max array or into $other_values
1155
      // for generic output to be appended to 'min-max' string
1156
      if (array_key_exists($statistical_val->type->titleCache, $min_max)) {
1157
        $min_max[$statistical_val->type->titleCache] = $statistical_val;
1158
      }
1159
      else {
1160
        $other_values[] = $statistical_val;
1161
      }
1162
    } // end of loop over statisticalValues
1163

    
1164
    // create markup
1165

    
1166
    $min_max_markup = min_max_markup($min_max);
1167

    
1168

    
1169
    foreach ($other_values as $statistical_val) {
1170
      $statistical_val_type_representation = NULL;
1171
      if (isset($statistical_val->type)) {
1172
        $statistical_val_type_representation = cdm_term_representation($statistical_val->type);
1173
        // $statistical_val->type->termType;
1174
        // $statistical_val->type->userFriendlyTypeName;
1175
      }
1176
      $value_markup = '<span class="' . html_class_attribute_ref($statistical_val) . ' ' . $statistical_val->type->termType . ' ">'
1177
        . $statistical_val->_value . '</span>';
1178
      $value_markup = $value_markup .
1179
        ($statistical_val_type_representation ? ' <span class="type">' . $statistical_val_type_representation . '</span>' : '');
1180
      $other_values_markup[] = $value_markup;
1181
    }
1182

    
1183

    
1184
    $out .= $min_max_markup . ' ' . implode($other_values_markup, ', ');
1185
  }
1186

    
1187
  if (isset($element->unit)) {
1188
    $out .= ' <span class="unit" title="'
1189
      . cdm_term_representation($element->unit) . '">'
1190
      . cdm_term_representation_abbreviated($element->unit)
1191
      . '</span>';
1192
  }
1193

    
1194
  // modifiers of the description element itself
1195
  $modifier_string = cdm_modifers_representations($element);
1196
  $out .= ($modifier_string ? ' ' . $modifier_string : '');
1197
  if (isset($element->modifyingText_L10n)) {
1198
    $out = $element->modifyingText_L10n . ' ' . $out;
1199
  }
1200

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

    
1203
  return $render_array;
1204
}
1205

    
1206

    
1207
/**
1208
 * Wraps the render array for the given feature into an enclosing html tag.
1209
 *
1210
 * Optionally the elements can be sorted and glued together by a separator string.
1211
 *
1212
 * @param array $description_element_render_arrays
1213
 *   An list of render arrays. Which are usually are produced by the compose_description_element()
1214
 *   function. The render arrays should be of #type=html_tag, so that they can understand the #attribute property.
1215
 * @param  $feature :
1216
 *  The feature to which the elements given in $elements are belonging to.
1217
 * @param string $glue :
1218
 *  Defaults to empty string.
1219
 * @param bool $sort
1220
 *   Boolean Whether to sort the $elements alphabetically, default is FALSE
1221
 *
1222
 * @return array
1223
 *    A Drupal render array
1224
 *
1225
 * @ingroup compose
1226
 */
1227
  function compose_feature_block_wrap_elements(array $description_element_render_arrays, $feature, $glue = '', $sort = FALSE)
1228
  {
1229

    
1230
    $feature_block_settings = get_feature_block_settings($feature->uuid);
1231
    $enclosing_tag = $feature_block_settings['as_list'];
1232

    
1233
    if ($sort) { // TODO remove parameter and replace by $feature_block_settings['sort_elements']
1234
      usort($description_element_render_arrays, 'compare_description_element_render_arrays');
1235
    }
1236

    
1237
    $is_first = true;
1238
    foreach($description_element_render_arrays as &$element_render_array){
1239
      if(!is_array($element_render_array)){
1240
        $element_render_array = markup_to_render_array($element_render_array);
1241
      }
1242
      $element_render_array['#attributes']['class'][] = "feature-block-element";
1243

    
1244
      // add the glue!
1245
      if(!$is_first) {
1246
        if (isset($element_render_array['#value'])) {
1247
          $element_render_array['#value'] = $glue . $element_render_array['#value'];
1248
        } elseif (isset($element_render_array['#markup'])) {
1249
          $element_render_array['#markup'] = $glue . $element_render_array['#markup'];
1250
        }
1251
      }
1252
      $is_first = false;
1253
    }
1254

    
1255
    $render_array['elements']['children'] = $description_element_render_arrays;
1256

    
1257
    $render_array['elements']['#prefix'] = '<' . $enclosing_tag . ' class="feature-block-elements" id="' . $feature->representation_L10n . '">';
1258
    $render_array['elements']['#suffix'] = '</' . $enclosing_tag . '>';
1259

    
1260
    return $render_array;
1261
  }
1262

    
1263

    
1264
  /* compose nameInSource or originalNameString as markup
1265
   *
1266
   * @param $source
1267
   * @param $do_link_to_name_used_in_source
1268
   * @param $suppress_for_shown_taxon
1269
   *    the nameInSource will be suppressed when it has the same name as the accepted taxon
1270
   *    for which the taxon page is being created, Defaults to TRUE
1271
   *
1272
   * @return array
1273
   *    A Drupal render array with an additional element, the render array is empty
1274
   *    if the source had no name in source information
1275
   *    - #_plaintext: contains the plaintext version of the name (custom element)
1276
   *
1277
   * @ingroup compose
1278
   */
1279
  function compose_name_in_source($source, $do_link_to_name_used_in_source, $suppress_for_shown_taxon = TRUE) {
1280

    
1281
    $plaintext = NULL;
1282
    $markup = NULL;
1283
    $name_in_source_render_array = array();
1284

    
1285
    static $taxon_page_accepted_name = '';
1286
    if($suppress_for_shown_taxon && arg(1) == 'taxon' && empty($taxon_page_accepted_name)){
1287

    
1288
      $current_taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, arg(2));
1289
      $taxon_page_accepted_name = $current_taxon->name->titleCache;
1290
    }
1291

    
1292
    if (isset($source->nameUsedInSource->uuid) && isset($source->nameUsedInSource->titleCache)) {
1293
      // it is a DescriptionElementSource !
1294
      $plaintext = $source->nameUsedInSource->titleCache;
1295
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1296
        return $name_in_source_render_array; // SKIP this name
1297
      }
1298
      $markup = render_taxon_or_name($source->nameUsedInSource);
1299
      if ($do_link_to_name_used_in_source) {
1300
        $markup = l(
1301
          $markup,
1302
          path_to_name($source->nameUsedInSource->uuid),
1303
          array(
1304
            'attributes' => array(),
1305
            'absolute' => TRUE,
1306
            'html' => TRUE,
1307
          ));
1308
      }
1309
    }
1310
    else if (isset($source->originalNameString) && !empty($source->originalNameString)) {
1311
      // the name used in source can not be expressed as valid taxon name,
1312
      // so the editor has chosen to put the freetext name into ReferencedEntityBase.originalNameString
1313
      // field
1314
      // using the originalNameString as key to avoid duplicate entries
1315
      $plaintext = $source->originalNameString;
1316
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1317
        return $name_in_source_render_array; // SKIP this name
1318
      }
1319
      $markup = $source->originalNameString;
1320
    }
1321

    
1322
    if ($plaintext) { // checks if we have any content
1323
      $name_in_source_render_array = markup_to_render_array($markup);
1324
      $name_in_source_render_array['#_plaintext'] = $plaintext;
1325
    }
1326

    
1327
    return $name_in_source_render_array;
1328
  }
1329

    
1330

    
1331

    
1332
  /**
1333
   * Return HTML for a list of description elements.
1334
   *
1335
   * Usually these are of a specific feature type.
1336
   *
1337
   * @param $description_elements
1338
   *   array of descriptionElements which belong to the same feature.
1339
   *   These descriptions elements of a Description must be ordered by the chosen feature tree by
1340
   *   calling the function _mergeFeatureTreeDescriptions().
1341
   *   @see _mergeFeatureTreeDescriptions()
1342
   *
1343
   * @param  $feature_uuid
1344
   *
1345
   * @return
1346
   *    A drupal render array for the $descriptionElements, may be an empty array if the textual content was empty.
1347
   *    Footnote key or anchors are not considered to be textual content.
1348
   *
1349
   * @ingroup compose
1350
   */
1351
  function compose_feature_block_items_generic($description_elements, $feature) {
1352

    
1353
    $elements_out_array = array();
1354
    $distribution_tree = null;
1355

    
1356
    /*
1357
     * $feature_block_has_content will be set true if at least one of the
1358
     * $descriptionElements contains some text which makes up some content
1359
     * for the feature block. Footnote keys are not considered
1360
     * to be content in this sense.
1361
     */
1362
    $feature_block_has_content = false;
1363

    
1364
    if (is_array($description_elements)) {
1365
      foreach ($description_elements as $description_element) {
1366
          /* decide based on the description element class
1367
           *
1368
           * Features handled here:
1369
           * all except DISTRIBUTION, COMMON_NAME, USES, IMAGES,
1370
           *
1371
           * TODO provide api_hook as extension point for this?
1372
           */
1373
        $feature_block_settings = get_feature_block_settings($description_element->feature->uuid);
1374
        switch ($description_element->class) {
1375
          case 'TextData':
1376
            $elements_out_array[] = compose_description_element_text_data($description_element, $description_element->feature->uuid, $feature_block_settings);
1377
            break;
1378
          case 'CategoricalData':
1379
            $elements_out_array[] = compose_description_element_categorical_data($description_element, $feature_block_settings);
1380
            break;
1381
          case 'QuantitativeData':
1382
            $elements_out_array[] = compose_description_element_quantitative_data($description_element, $feature_block_settings);
1383
            break;
1384
          case 'IndividualsAssociation':
1385
            $elements_out_array[] = compose_description_element_individuals_association($description_element, $feature_block_settings);
1386
            break;
1387
          case 'TaxonInteraction':
1388
            $elements_out_array[] = compose_description_element_taxon_interaction($description_element, $feature_block_settings);
1389
            break;
1390
          case 'CommonTaxonName':
1391
            $elements_out_array[] = compose_description_element_common_taxon_name($description_element, $feature_block_settings);
1392
            break;
1393
          case 'Uses':
1394
            /* IGNORE Uses classes, these are handled completely in theme_cdm_UseDescription */
1395
            break;
1396
          default:
1397
            $feature_block_has_content = true;
1398
            $elements_out_array[] = markup_to_render_array('<li>No method for rendering unknown description class: ' . $description_element->class . '</li>');
1399
        }
1400
        $elements_out_array_last_item = $elements_out_array[count($elements_out_array) - 1];
1401
        // considering not empty as long as the last item added is a render array
1402
        $feature_block_has_content = $feature_block_has_content || !empty($elements_out_array_last_item['#type']);
1403
      }
1404

    
1405
      // If feature = CITATION sort the list of sources.
1406
      // This is ONLY for FLORA MALESIANA and FLORE d'AFRIQUE CENTRALE.
1407
      if (isset($description_element) && $description_element->feature->uuid == UUID_CITATION) {
1408
        sort($elements_out_array);
1409
      }
1410
    }
1411

    
1412
    return $elements_out_array;
1413
  }
1414

    
1415
/**
1416
 * Composes block of common names for the given DescriptionElements $elements which must be of the feature CommonName
1417
 *
1418
 * @parameter $elements
1419
 *  an array of CDM DescriptionElements either of type CommonName or TextData
1420
 * @parameter $feature
1421
 *  the common feature of all $elements, must be CommonName
1422
 *
1423
 * @return
1424
 *   A drupal render array
1425
 *
1426
 * @ingroup compose
1427
 */
1428
function compose_feature_block_items_feature_common_name($elements, $feature, $weight = FALSE) {
1429

    
1430
  $common_name_out = '';
1431
  $common_name_feature_elements = array();
1432
  $textData_commonNames = array();
1433

    
1434
  $footnote_key_suggestion = 'common-names-feature-block';
1435

    
1436
  $feature_block_settings = get_feature_block_settings(UUID_COMMON_NAME);
1437

    
1438
  if (is_array($elements)) {
1439
    foreach ($elements as $element) {
1440

    
1441
      if ($element->class == 'CommonTaxonName') {
1442

    
1443
        // common name without a language or area, should not happen but is possible
1444
        $language_area_key = '';
1445
        if (isset($element->language->representation_L10n)) {
1446
          $language_area_key .= '<span class="language-label">' . $element->language->representation_L10n . '</span>';
1447
        }
1448
        if(isset($element->area->titleCache) && strlen($element->area->titleCache) > 0){
1449
          $language_area_key .= ($language_area_key ? ' '  : '') . '(' . $element->area->titleCache . ')';
1450
        }
1451

    
1452
        if(isset($common_names[$language_area_key][$element->name])) {
1453
          // same name already exists for language and area combination, se we merge the description elements
1454
          cdm_merge_description_elements($common_names[$language_area_key][$element->name], $element);
1455
        } else{
1456
          // otherwise add as new entry
1457
          $common_names[$language_area_key][$element->name] = $element;
1458
        }
1459

    
1460
      }
1461
      elseif ($element->class == 'TextData') {
1462
        $textData_commonNames[] = $element;
1463
      }
1464
    }
1465
  }
1466
  // Handling common names.
1467
  if (isset($common_names) && count($common_names) > 0) {
1468
    // Sorting the array based on the key (language, + area if set).
1469
    // Comment @WA there are common names without a language, so this sorting
1470
    // can give strange results.
1471
    ksort($common_names);
1472

    
1473
    // loop over set of elements per language area
1474
    foreach ($common_names as $language_area_key => $elements) {
1475
      ksort($elements); // sort names alphabetically
1476
      $per_language_area_out = array();
1477

    
1478
      foreach ($elements as $element) {
1479
        $common_name_render_array = compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion);
1480
        $common_name_markup = drupal_render($common_name_render_array);
1481
        // IMPORTANT!
1482
        // during the above drupal_render the theme_html_tag function is executed, which adds a "\n" character to the end of the markup
1483
        // this is an error and the trailing whitespace needs to be removed
1484
        if(str_endsWith($common_name_markup, "\n")){
1485
          $common_name_markup = substr($common_name_markup, 0, strlen($common_name_markup) - 1);
1486
        }
1487
        $per_language_area_out[] = $common_name_markup;
1488
      }
1489

    
1490
      $common_name_feature_elements[] = ($language_area_key ? $language_area_key . ': ' : '' ) . join(', ', $per_language_area_out);
1491
    } // End of loop over set of elements per language area
1492

    
1493

    
1494
    $common_name_feature_elements_render_array = compose_feature_block_wrap_elements(
1495
      $common_name_feature_elements, $feature, '; ', FALSE
1496
    );
1497
    $common_name_out .= drupal_render($common_name_feature_elements_render_array); // FIXME should this be a render array instead?
1498

    
1499
  }
1500

    
1501
  // Handling commons names as text data.
1502
  $text_data_out = array();
1503

    
1504
  foreach ($textData_commonNames as $text_data_element) {
1505
    /* footnotes are not handled correctly in compose_description_element_text_data,
1506
       need to set 'common-names-feature-block' as $footnote_key_suggestion */
1507
    RenderHints::setFootnoteListKey($footnote_key_suggestion);
1508
    $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
1509
    $text_data_out[] = drupal_render($text_data_render_array);
1510
  }
1511

    
1512
  $common_name_out_text_data = compose_feature_block_wrap_elements(
1513
    $text_data_out, $feature
1514
  );
1515

    
1516
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . $footnote_key_suggestion));
1517
  $footnotes .= theme('cdm_footnotes', array('footnoteListKey' => $footnote_key_suggestion)); // FIXME is this needed at all?
1518
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnote_key_suggestion));
1519

    
1520
  return  markup_to_render_array(  // FIXME markup_to_render_array should no longer be needed
1521
    '<div class="common-taxon-name">' . $common_name_out . '</div>'
1522
    .'<div class="text-data">' . drupal_render($common_name_out_text_data) . '</div>'
1523
    .$footnotes,
1524
    $weight
1525
  );
1526
}
1527

    
1528
/**
1529
 * Renders a single instance of the type CommonTaxonName.
1530
 *
1531
 * @param $element
1532
 *   The CDM CommonTaxonName entity.
1533
 * @param $feature_block_settings
1534
 *
1535
 * @param $footnote_key_suggestion
1536
 *
1537
 * @param $element_tag_name
1538
 *
1539
 * @return array
1540
 *   Drupal render array
1541
 *
1542
 * @ingroup compose
1543
 */
1544
function compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion = NULL)
1545
{
1546

    
1547
  if(!$footnote_key_suggestion) {
1548
    $footnote_key_suggestion = $element->feature->uuid;
1549
  }
1550

    
1551
  $name = '';
1552
  if(isset($element->name)){
1553
    $name = $element->name;
1554
  }
1555

    
1556

    
1557
  return compose_description_element($element, $feature_block_settings, $name, $footnote_key_suggestion);
1558
}
1559

    
1560
/**
1561
 * Composes the render array for a CDM Distribution description element
1562
 *
1563
 * @param array $description_elements
1564
 *   Array of CDM Distribution instances
1565
 * @param $enclosingTag
1566
 *   The html tag to be use for the enclosing element
1567
 *
1568
 * @return array
1569
 *   A Drupal render array
1570
 *
1571
 * @ingroup compose
1572
 */
1573
function compose_description_elements_distribution($description_elements){
1574

    
1575
  $out = '';
1576
  RenderHints::pushToRenderStack('descriptionElementDistribution');
1577
  RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1578

    
1579
  $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
1580
  $enclosingTag = cdm_feature_block_element_tag_name($feature_block_settings);
1581

    
1582
  foreach ($description_elements as $description_element) {
1583
    $annotations_and_sources = handle_annotations_and_sources(
1584
      $description_element,
1585
      $feature_block_settings,
1586
      $description_element->area->representation_L10n,
1587
      UUID_DISTRIBUTION
1588
    );
1589

    
1590

    
1591
    list($status_label, $status_markup) = distribution_status_label_and_markup($description_element);
1592

    
1593
    $out .= '<' . $enclosingTag . ' class="descriptionElement descriptionElement-' . $description_element->uuid
1594
      . ' " title="' . $status_label. '">'
1595
      . $description_element->area->representation_L10n
1596
      . $status_markup;
1597
    if(!empty($annotations_and_sources['source_references'])){
1598
      $out .= ' ' . join(' ', $annotations_and_sources['source_references'] );
1599
    }
1600
    $out .= $annotations_and_sources['foot_note_keys']   . ' </' . $enclosingTag . '>';
1601
  }
1602

    
1603
  RenderHints::popFromRenderStack();
1604
  return markup_to_render_array($out);
1605
}
1606

    
1607
  /**
1608
   * @param $descriptionElement
1609
   * @return array
1610
   */
1611
  function distribution_status_label_and_markup($descriptionElement) {
1612
    $status_markup = '';
1613
    $status_label = '';
1614

    
1615
    if (isset($descriptionElement->status)) {
1616
      $status_label = $descriptionElement->status->representation_L10n;
1617
      $status_markup = '<span class="distributionStatus distributionStatus-' . $descriptionElement->status->idInVocabulary . '"> '
1618
        . $status_label . ' </span>';
1619

    
1620
    };
1621
    return array($status_label, $status_markup);
1622
  }
1623

    
1624

    
1625
  /**
1626
   * Provides the merged feature tree for a taxon profile page.
1627
   *
1628
   * The merging of the profile feature tree is actully done in
1629
   * _mergeFeatureTreeDescriptions(). See this method  for details
1630
   * on the structure of the merged tree.
1631
   *
1632
   * This method provides t hook which can be used to modify the
1633
   * merged feature tree after it has been created, see
1634
   * hook_merged_taxon_feature_tree_alter()
1635
   *
1636
   * @param $taxon
1637
   *   A CDM Taxon instance
1638
   *
1639
   * @return object
1640
   *  The merged feature tree
1641
   *
1642
   */
1643
  function merged_taxon_feature_tree($taxon) {
1644

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

    
1648

    
1649
    // 2. find the distribution feature node
1650
    $distribution_node =& cdm_feature_tree_find_node($merged_tree->root->childNodes, UUID_DISTRIBUTION);
1651

    
1652
    if ($distribution_node) {
1653
      // 3. get the distributionInfoDTO
1654
      $query_parameters = cdm_distribution_filter_query();
1655
      $query_parameters['part'] = array('mapUriParams');
1656
      if(variable_get(DISTRIBUTION_CONDENSED)){
1657
        $query_parameters['part'][] = 'condensedDistribution';
1658
      }
1659
      if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) == 'TREE') {
1660
        $query_parameters['part'][] = 'tree';
1661
      }
1662
      else {
1663
        $query_parameters['part'][] = 'elements';
1664
      }
1665
      $query_parameters['omitLevels'] = array();
1666
      foreach(variable_get(DISTRIBUTION_TREE_OMIT_LEVELS, array()) as $uuid){
1667
        if(is_uuid($uuid)){
1668
          $query_parameters['omitLevels'][] = $uuid;
1669
        }
1670
      }
1671
      $customStatusColorsJson = variable_get(DISTRIBUTION_STATUS_COLORS, NULL);
1672
      if ($customStatusColorsJson) {
1673
        $query_parameters['statusColors'] = $customStatusColorsJson;
1674
      }
1675

    
1676
      $distribution_info_dto = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION_DISTRIBUTION_INFO_FOR, $taxon->uuid, queryString($query_parameters));
1677
      // 4. get distribution TextData is there are any
1678
      $distribution_text_data = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON,
1679
        array(
1680
          'taxon' => $taxon->uuid,
1681
          'type' => 'TextData',
1682
          'features' => UUID_DISTRIBUTION
1683
        )
1684
      );
1685

    
1686
      // 5. put all distribution data into the distribution feature node
1687
      if ($distribution_text_data //if text data exists
1688
        || ($distribution_info_dto && isset($distribution_info_dto->tree) && $distribution_info_dto->tree->rootElement->numberOfChildren > 0) // OR if tree element has distribution elements
1689
        || ($distribution_info_dto && !empty($distribution_info_dto->elements))
1690
      ) { // OR if DTO has distribution elements
1691
        $distribution_node->descriptionElements = array('#type' => 'DTO');
1692
        if ($distribution_text_data) {
1693
          $distribution_node->descriptionElements['TextData'] = $distribution_text_data;
1694
        }
1695
        if ($distribution_info_dto) {
1696
          $distribution_node->descriptionElements['DistributionInfoDTO'] = $distribution_info_dto;
1697
        }
1698
      }
1699
    }
1700

    
1701
    // allows modifying the merged tree via a the hook_cdm_feature_node_block_content_alter
1702
    drupal_alter('merged_taxon_feature_tree', $taxon, $merged_tree);
1703

    
1704
    return $merged_tree;
1705
  }
1706

    
1707

    
1708
  function compose_distribution_hierarchy($distribution_tree, $feature_block_settings){
1709

    
1710
    static $hierarchy_style;
1711
    // TODO expose $hierarchy_style to administration of provide a hook
1712
    if( !isset($hierarchy_style)){
1713
      $hierarchy_style = array(
1714
        // level 2
1715
        array(
1716
          'label_suffix' => '',
1717
          'element_glue' => ', ',
1718
          'element_set_pre' => '(',
1719
          'element_set_post' => ')'
1720
        ),
1721
        // level 1
1722
        array(
1723
          'label_suffix' => '',
1724
          'element_glue' => '; ',
1725
          'element_set_pre' => '',
1726
          'element_set_post' => ''
1727
        ),
1728
        // level 0
1729
        array(
1730
          'label_suffix' => ':',
1731
          'element_glue' => ' ',
1732
          'element_set_pre' => '',
1733
          'element_set_post' => ''
1734
        ),
1735
      );
1736
    }
1737

    
1738
    $render_array = array();
1739

    
1740
    RenderHints::pushToRenderStack('descriptionElementDistribution');
1741
    RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1742

    
1743
    // Returning NULL if there are no description elements.
1744
    if ($distribution_tree == null) {
1745
      return $render_array;
1746
    }
1747
    // for now we are not using a render array internally to avoid performance problems
1748
    $markup = '';
1749
    if (isset($distribution_tree->rootElement->children)) {
1750
      $tree_nodes = $distribution_tree->rootElement->children;
1751
      _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, $markup, $hierarchy_style);
1752
    }
1753

    
1754
    $render_array['distribution_hierarchy'] = markup_to_render_array(
1755
      $markup,
1756
      0,
1757
      '<div id="distribution_hierarchy" class="distribution_hierarchy">',
1758
      '</div>'
1759
    );
1760

    
1761
    return $render_array;
1762
  }
1763

    
1764
  /**
1765
   * this function should produce markup as the compose_description_elements_distribution()
1766
   * function.
1767
   *
1768
   * @see compose_description_elements_distribution()
1769
   *
1770
   * @param $distribution_tree
1771
   * @param $feature_block_settings
1772
   * @param $tree_nodes
1773
   * @param $markup
1774
   * @param $hierarchy_style
1775
   */
1776
  function _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, &$markup, $hierarchy_style, $level_index = -1){
1777

    
1778
    $level_index++;
1779
    static $enclosingTag = "span";
1780

    
1781
    $level_style = array_pop($hierarchy_style);
1782
    if(count($hierarchy_style) == 0){
1783
      // lowest defined level style will be reused for all following levels
1784
      $hierarchy_style[] = $level_style;
1785
    }
1786

    
1787
    $node_index = -1;
1788
    $per_node_markup = array();
1789
    foreach ($tree_nodes as $node){
1790

    
1791
      $per_node_markup[++$node_index] = '';
1792

    
1793
      $label = $node->nodeId->representation_L10n;
1794

    
1795
      $distributions = $node->data;
1796
      $distribution_uuids = array();
1797
      $distribution_aggregate = NULL;
1798
        foreach($distributions as $distribution){
1799

    
1800
          $distribution_uuids[] = $distribution->uuid;
1801

    
1802
          // if there is more than one distribution we aggregate the sources and
1803
          // annotations into a synthetic distribution so that the footnote keys
1804
          // can be rendered consistently
1805
          if(!$distribution_aggregate) {
1806
            $distribution_aggregate = $distribution;
1807
            if(!isset($distribution_aggregate->sources[0])){
1808
              $distribution_aggregate->sources = array();
1809
            }
1810
            if(!isset($distribution_aggregate->annotations[0])){
1811
              $distribution_aggregate->annotations = array();
1812
            }
1813
          } else {
1814
            if(isset($distribution->sources[0])) {
1815
              $distribution_aggregate->sources = array_merge($distribution_aggregate->sources,
1816
                $distribution->sources);
1817
            }
1818
            if(isset($distribution->annotations[0])) {
1819
              $distribution_aggregate->annotations = array_merge($distribution_aggregate->annotations,
1820
                $distribution->annotations);
1821
            }
1822
          }
1823
        }
1824

    
1825
      $status_label= '';
1826
      $status_markup = '';
1827
      $annotations_and_sources =  null;
1828
      if($distribution_aggregate) {
1829
        $annotations_and_sources = handle_annotations_and_sources(
1830
          $distribution_aggregate,
1831
          $feature_block_settings,
1832
          $label,
1833
          UUID_DISTRIBUTION
1834
        );
1835

    
1836
        list($status_label, $status_markup) = distribution_status_label_and_markup($distribution);
1837
      }
1838

    
1839
      $per_node_markup[$node_index] .= '<' . $enclosingTag . ' class="descriptionElement'
1840
        . join(' descriptionElement-', $distribution_uuids)
1841
        . ' level_index_' . $level_index
1842
        . ' " title="' . $status_label . '">'
1843
        . '<span class="area_label">' . $label
1844
        . $level_style['label_suffix'] . ' </span>'
1845
        .  $status_markup
1846
      ;
1847

    
1848
      if(isset($annotations_and_sources)){
1849
        if(!empty($annotations_and_sources['source_references'])){
1850
          $per_node_markup[$node_index] .= ' ' . join(', ' , $annotations_and_sources['source_references']);
1851
        }
1852
        if($annotations_and_sources['foot_note_keys']) {
1853
          $per_node_markup[$node_index] .= $annotations_and_sources['foot_note_keys'];
1854
        }
1855
      }
1856

    
1857
      if(isset($node->children[0])){
1858
        _compose_distribution_hierarchy(
1859
          $node->children,
1860
          $feature_block_settings,
1861
          $per_node_markup[$node_index],
1862
          $hierarchy_style,
1863
          $level_index
1864
        );
1865
      }
1866

    
1867
      $per_node_markup[$node_index] .= '</' . $enclosingTag . '>';
1868
    }
1869
    $markup .= $level_style['element_set_pre']  . join( $level_style['element_glue'], $per_node_markup) . $level_style['element_set_post'];
1870
  }
1871

    
1872

    
(2-2/8)