Project

General

Profile

Download (63.5 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
  if ($cdm_distribution_filter['filter_rules']['statusOrderPreference']) {
77
    $query['statusOrderPreference'] = 1;
78
  }
79
  if ($cdm_distribution_filter['filter_rules']['subAreaPreference']) {
80
    $query['subAreaPreference'] = 1;
81
  }
82
  if (is_array($cdm_distribution_filter['hiddenAreaMarkerType']) && count($cdm_distribution_filter['hiddenAreaMarkerType']) > 0) {
83
    $query['hiddenAreaMarkerType'] = '';
84
    foreach ($cdm_distribution_filter['hiddenAreaMarkerType'] as $marker_type => $enabled) {
85
      if ($enabled) {
86
        $query['hiddenAreaMarkerType'] .= ($query['hiddenAreaMarkerType'] ? ',' : '') . $marker_type;
87
      }
88
    }
89
  }
90

    
91
  return $query;
92
}
93

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

    
105
  foreach ($fields_to_merge as $field) {
106
    if (is_array($source->$field)) {
107
      if (!is_array($target->$field)) {
108
        $target->$field = $source->$field;
109
      }
110
      else {
111
        $target->$field = array_merge($target->$field, $source->$field);
112
      }
113
    }
114
  }
115
}
116

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

    
135
  if (!$fragment) {
136
    $fragment = $label;
137
  }
138
  $fragment = generalizeString($fragment);
139

    
140
  $class_attributes = 'feature-toc-item-' . $class_attribute_suffix;
141

    
142
  $new_item = toc_list_item(
143
    theme(
144
      'cdm_feature_name',
145
      array('feature_name' => $label)),
146
      array('class' => $class_attributes),
147
      $fragment
148
    );
149

    
150
  if ($as_first_element) {
151
    array_unshift($toc_list_items, $new_item);
152
  }
153
  else {
154
    $toc_list_items[] = $new_item;
155
  }
156

    
157
}
158

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

    
170
  return $toc_list_items;
171
}
172

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

    
200

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

    
228
    case "MediaKey":
229
      $cdm_ws_pasepath = CDM_WS_MEDIAKEY;
230
      break;
231

    
232
    case "MultiAccessKey":
233
      $cdm_ws_pasepath = CDM_WS_MULTIACCESSKEY;
234
      break;
235

    
236
  }
237

    
238
  if (!$cdm_ws_pasepath) {
239
    drupal_set_message(t('Type parameter is not valid: ') . $type, 'error');
240
  }
241

    
242
  $queryParameters = '';
243
  if (is_numeric($pageSize)) {
244
    $queryParameters = "pageSize=" . $pageSize;
245
  }
246
  else {
247
    $queryParameters = "pageSize=0";
248
  }
249

    
250
  if (is_numeric($pageNumber)) {
251
    $queryParameters = "pageNumber=" . $pageNumber;
252
  }
253
  else {
254
    $queryParameters = "pageNumber=0";
255
  }
256
  $queryParameters = NULL;
257
  if ($taxonUuid) {
258
    $queryParameters = "findByTaxonomicScope=$taxonUuid";
259
  }
260
  $pager = cdm_ws_get($cdm_ws_pasepath, NULL, $queryParameters);
261

    
262
  if (!$pager || $pager->count == 0) {
263
    return array();
264
  }
265
  return $pager->records;
266
}
267

    
268

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

    
281
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
282
  $http_request_params = drupal_static('http_request_params', drupal_get_query_parameters());
283

    
284
  $item =  array(
285
    'data' => l(
286
      $label,
287
      $_GET['q'],
288
      array(
289
        'attributes' => array('class' => array('toc')),
290
        'fragment' => generalizeString($label),
291
        'query' => $http_request_params,
292
      )
293
    ),
294
  );
295
  $item['attributes'] = $attributes;
296
  return $item;
297
}
298

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

    
320

    
321
  // Annotations as footnotes.
322
  $footNoteKeys = cdm_annotations_as_footnotekeys($description_element, $footnote_list_key_suggestion);
323

    
324
  // Source references as footnotes.
325
  $bibliography_settings = get_bibliography_settings();
326
  $original_source_footnote_tag = $bibliography_settings['enabled'] == 1 ? 'div' : null; // null will cause original_source_footnote_list_key to use the default
327

    
328
  usort($description_element->sources, 'compare_original_sources');
329
  foreach ($description_element->sources as $source) {
330
    if (_is_original_source_type($source)) {
331
      $fn_key = FootnoteManager::addNewFootnote(
332
        original_source_footnote_list_key($footnote_list_key_suggestion),
333
        theme('cdm_OriginalSource', array(
334
          'source' => $source,
335
          'doLink' => $do_link_to_reference,
336
          'do_link_to_name_used_in_source' => $do_link_to_name_used_in_source
337

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

    
357

    
358

    
359
/**
360
 * @param $render_array
361
 * @param $element
362
 * @param $feature_block_settings
363
 * @param $element_markup
364
 * @param $footnote_list_key_suggestion
365
 */
366
function compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion)
367
{
368

    
369
  $render_array = array(
370
    '#type' => 'html_tag',
371
    '#tag' => cdm_feature_block_element_tag_name($feature_block_settings),
372

    
373
    '#attributes' => array(
374
      'class' => 'DescriptionElement DescriptionElement-' . $element->class . '
375
      ' .  html_class_attribute_ref($element)
376
    ),
377

    
378
    '#value' => '',
379
    '#value_suffix' => NULL
380

    
381
  );
382

    
383
  $annotations_and_sources = handle_annotations_and_sources($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion);
384

    
385
  // handle the special case were the TextData is used as container for original source with name
386
  // used in source information without any text stored in it.
387
  $names_used_in_source_markup = '';
388
  if (!empty($annotations_and_sources['names_used_in_source']) && empty($element_markup)) {
389
    // $element_text ==  NULL  usually occurs only in the case of CITATIONS!!!
390
    $names_used_in_source_markup = join(', ', $annotations_and_sources['names_used_in_source']) . ': ';
391
  }
392

    
393
  $source_references_markup = '';
394
  if (!empty($annotations_and_sources['source_references'])) {
395
    $source_references_markup = '<span class="sources">' . join(' ', $annotations_and_sources['source_references']) . '<span>';
396
  }
397

    
398
  $render_array['#value'] = $names_used_in_source_markup . $element_markup . $source_references_markup;
399
  $render_array['#value_suffix'] = $annotations_and_sources['foot_note_keys'];
400

    
401
  return $render_array;
402
}
403

    
404

    
405
  /**
406
   * @param $element
407
   * @param $feature_block_settings
408
   * @param $element_text
409
   *   used to decide if the source references should be enclosed in brackets or not
410
   * @param $footnote_list_key_suggestion
411
   * @return array
412
   *   an associative array with the following elements:
413
   *   - foot_note_keys: all footnote keys as markup
414
   *   - source_references: an array of the source references citations
415
   *   - names used in source: an associative array of the names in source,
416
   *        the name in source strings are de-duplicated
417
   *        !!!NOTE!!!!: this field will most probably be removed soon (TODO)
418
   *
419
   *
420
   */
421
  function handle_annotations_and_sources($element, $feature_block_settings, $element_text, $footnote_list_key_suggestion) {
422
    $annotations_and_sources = array(
423
      'foot_note_keys' => NULL,
424
      'source_references' => array(),
425
      'names_used_in_source' => array()
426
    );
427

    
428
    usort($element->sources, 'compare_original_sources');
429

    
430
    if ($feature_block_settings['sources_as_content'] == 1) {
431
      foreach ($element->sources as $source) {
432

    
433
        $referenceCitation = theme('cdm_OriginalSource',
434
          array(
435
            'source' => $source,
436
            'doLink' => $feature_block_settings['link_to_reference'] == 1,
437
            'do_link_to_name_used_in_source' => $feature_block_settings['link_to_name_used_in_source'] == 1,
438
          )
439
        );
440

    
441
        if ($referenceCitation) {
442
          if (empty($element_text)) {
443
            $annotations_and_sources['source_references'][] = $referenceCitation;
444
          }
445
          else {
446
            $annotations_and_sources['source_references'][] = ' (' . $referenceCitation . ')';
447
          }
448
        }
449

    
450
        $name_in_source_render_array = compose_name_in_source(
451
          $source,
452
          $feature_block_settings['link_to_name_used_in_source'] == 1
453
        );
454

    
455
        if(!empty($name_in_source_render_array)){
456
          $annotations_and_sources['names_used_in_source'][$name_in_source_render_array['#_plaintext']] = drupal_render($name_in_source_render_array);
457
        }
458
      } // END of loop over sources
459

    
460
      // annotations footnotes separate.
461
      $annotations_and_sources['foot_note_keys'] = theme('cdm_annotations_as_footnotekeys',
462
        array(
463
          'cdmBase_list' => $element,
464
          'footnote_list_key' => $footnote_list_key_suggestion,
465
        )
466
      );
467

    
468
    } // END of references inline
469

    
470
    // put sources into bibliography if requested ...
471
    if ($feature_block_settings['sources_as_content'] !== 1 || $feature_block_settings['sources_as_content_to_bibliography'] == 1) {
472
      $annotations_and_sources['foot_note_keys'] = cdm_create_description_element_footnotes(
473
        $element, ',',
474
        $footnote_list_key_suggestion,
475
        $feature_block_settings['link_to_reference'] == 1,
476
        $feature_block_settings['link_to_name_used_in_source'] == 1
477
      );
478
    }
479

    
480
    return $annotations_and_sources;
481
  }
482

    
483

    
484
  /**
485
   *
486
   *
487
   * @return string
488
   *  the footnote_list_key
489
   */
490
  function original_source_footnote_list_key($key_suggestion = null) {
491
    if(!$key_suggestion){
492
      $key_suggestion = RenderHints::getFootnoteListKey();
493
    }
494
    $bibliography_settings = get_bibliography_settings();
495
    $footnote_list_key = $bibliography_settings['enabled'] == 1 ? 'BIBLIOGRAPHY' : 'BIBLIOGRAPHY-' . $key_suggestion;
496
    return $footnote_list_key;
497
  }
498

    
499
  /**
500
   * Provides the according tag name for the description element markup which fits the  $feature_block_settings['as_list'] value
501
   *
502
   * @param $feature_block_settings
503
   *   A feature_block_settings array, for details, please see get_feature_block_settings($feature_uuid = 'DEFAULT')
504
   */
505
  function cdm_feature_block_element_tag_name($feature_block_settings){
506
    switch ($feature_block_settings['as_list']){
507
      case 'ul':
508
      case 'ol':
509
        return 'li';
510
      case 'div':
511
        if(isset($feature_block_settings['element_tag'])){
512
          return $feature_block_settings['element_tag'];
513
        }
514
        return 'span';
515
      case 'dl':
516
        return 'dd';
517
      default:
518
        return 'div'; // should never happen, throw error instead?
519
    }
520
  }
521

    
522

    
523
/* ==================== COMPOSE FUNCTIONS =============== */
524

    
525
  /**
526
   * Returns a set of feature blocks for a taxon profile from the $mergedFeatureNodes of a given $taxon.
527
   *
528
   * The taxon profile consists of drupal block elements, one for the description elements
529
   * of a specific feature. The structure is defined by specific FeatureTree.
530
   * The chosen FeatureTree is merged with the list of description elements prior to using this method.
531
   *
532
   * The merged nodes can be obtained by making use of the
533
   * function cdm_ws_descriptions_by_featuretree().
534
   *
535
   * @see cdm_ws_descriptions_by_featuretree()
536
   *
537
   * @param $mergedFeatureNodes
538
   *
539
   * @param $taxon
540
   *
541
   * @return array
542
   *  A Drupal render array containing feature blocks and the table of content
543
   *
544
   * @ingroup compose
545
   */
546
  function compose_feature_blocks($mergedFeatureNodes, $taxon) {
547

    
548
    $block_list = array();
549

    
550
    RenderHints::pushToRenderStack('feature_nodes');
551

    
552
    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME);
553

    
554
    // Create a drupal block for each feature
555
    foreach ($mergedFeatureNodes as $node) {
556

    
557
      if ((isset($node->descriptionElements['#type']) ||
558
          has_feature_node_description_elements($node)) && $node->feature->uuid != UUID_IMAGE) { // skip empty or supressed features
559

    
560
        $feature_name = cdm_term_representation($node->feature, 'Unnamed Feature');
561
        $feature_block_settings = get_feature_block_settings($node->feature->uuid);
562

    
563
        $block = feature_block($feature_name, $node->feature);
564
        $block->content = array();
565
        $block_content_is_empty = TRUE;
566
        $block_content_weight = 0;
567

    
568
        /*
569
         * Content/DISTRIBUTION.
570
         */
571

    
572
        if ($node->feature->uuid == UUID_DISTRIBUTION) {
573
          $block = compose_feature_block_distribution($taxon, $node->descriptionElements, $node->feature);
574
          $block_content_is_empty = FALSE;
575
        }
576
        /*
577
         * Content/COMMON_NAME.
578
         */
579
        else if ($node->feature->uuid == UUID_COMMON_NAME) {
580
          $common_names_render_array = compose_feature_block_items_feature_common_name($node->descriptionElements, $node->feature);
581
          $block->content[] = $common_names_render_array;
582
          $block_content_is_empty = FALSE;
583
        }
584

    
585
        else if ($node->feature->uuid == UUID_USE_RECORD) {
586
          $block_uses_content_html = theme('cdm_block_Uses', array('taxonUuid' => $taxon->uuid));
587
          $block->content[] = markup_to_render_array($block_uses_content_html);
588
          $block_content_is_empty = FALSE;
589
        }
590

    
591
        /*
592
         * Content/ALL OTHER FEATURES.
593
         */
594
        else {
595

    
596
          $media_list = array();
597
          $out_child_elements = '';
598

    
599
          if (isset($node->descriptionElements)) {
600
            $elements_render_array = compose_feature_block_items_generic($node->descriptionElements, $node->feature);
601
            $block_content_is_empty = empty($elements_render_array);
602
            $block->content[] = $elements_render_array;
603
          }
604

    
605
          // Content/ALL OTHER FEATURES/Subordinate Features
606
          // subordinate features are printed inline in one floating text,
607
          // it is expected hat subordinate features can "contain" TextData,
608
          // Qualitative- and Qualitative- DescriptioneElements
609
          if (isset($node->childNodes[0])) {
610

    
611
            // TODO support more than one level of children.
612
            // can this be solved by recursively calling this function?
613
            // @see http://dev.e-taxonomy.eu/trac/ticket/2393
614
            $text = '';
615
            foreach ($node->childNodes as $child) {
616

    
617
              if (isset($child->descriptionElements) && is_array($child->descriptionElements)) {
618
                foreach ($child->descriptionElements as $element) {
619

    
620
                  //TODO it would be better if we could use compose_feature_block_items_generic() directly instead of the below stuff
621

    
622
                  if (is_array($element->media)) {
623
                    // Append media of subordinate elements to list of main
624
                    // feature.
625
                    $media_list = array_merge($media_list, $element->media);
626
                  }
627

    
628
                  switch ($element->class) {
629
                    case 'TextData':
630
                      // FIXME use compose_description_element_text_data()
631
                      if(isset($element->multilanguageText_L10n->text)){
632
                        $out_child_elements = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
633
                      }
634
                      $out_child_elements = str_replace($child->feature->titleCache, '<em>' . $child->feature->representation_L10n . '</em>', $out_child_elements);
635
                      break;
636
                    case 'CategoricalData':
637
                      $out_child_elements  = '<em>' . $child->feature->representation_L10n . '</em> '
638
                        . compose_description_element_categorical_data($element, $feature_block_settings);
639
                      break;
640
                    case 'QuantitativeData':
641
                      $out_child_elements = '<em>' . $child->feature->representation_L10n . '</em> '
642
                        . compose_description_element_quantitative_data($element, $feature_block_settings);
643

    
644
                  }
645

    
646
                }
647
                $text .= " " . $out_child_elements;
648
                $out_child_elements = '';
649
              }
650
            }
651
            $block_content_is_empty = $block_content_is_empty && empty($text);
652
            $block->content[] = markup_to_render_array($text);
653
            $block_content_is_empty = $block_content_is_empty && empty($media_list);
654
          }
655

    
656
          $block->content[] = compose_feature_media_gallery($node, $media_list, $gallery_settings);
657
          /*
658
           * Footnotes for the feature block
659
           */
660
          $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . $node->feature->uuid)));
661
          $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => $node->feature->uuid)));
662
          $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => $node->feature->uuid)));
663
        } // END all other features
664

    
665
        // allows modifying the block contents via a the hook_cdm_feature_node_block_content_alter
666
        drupal_alter('cdm_feature_node_block_content', $block->content, $node->feature, $node->descriptionElements);
667

    
668
        if(!$block_content_is_empty){ // skip empty block content
669
          $block_list[] = $block;
670
          cdm_toc_list_add_item(cdm_term_representation($node->feature), $node->feature->uuid);
671
        } // END: skip empty block content
672
      } // END: skip empty or supressed features
673
    } // END: creating a block per feature
674

    
675
    drupal_alter('cdm_feature_node_blocks', $block_list, $taxon);
676

    
677
    RenderHints::popFromRenderStack();
678

    
679
    return _block_get_renderable_array($block_list);
680
  }
681

    
682
  /**
683
   * @param $node
684
   * @param $media_list
685
   * @param $gallery_settings
686
   * @return array
687
   */
688
  function compose_feature_media_gallery($node, $media_list, $gallery_settings) {
689

    
690
    if (isset($node->descriptionElements)) {
691
      $media_list = array_merge($media_list, cdm_dataportal_media_from_descriptionElements($node->descriptionElements));
692
    }
693

    
694
    $captionElements = array('title', 'rights');
695

    
696
    if (isset($media_list[0]) && isset($gallery_settings['cdm_dataportal_media_maxextend']) && isset($gallery_settings['cdm_dataportal_media_cols'])) {
697
      $gallery = theme('cdm_media_gallerie', array(
698
        'mediaList' => $media_list,
699
        'galleryName' => CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME . '_' . $node->feature->uuid,
700
        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
701
        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
702
        'captionElements' => $captionElements,
703
      ));
704
      return markup_to_render_array($gallery);
705
    }
706

    
707
    return markup_to_render_array('');
708
  }
709

    
710
  /**
711
   * Composes the distribution feature block for a taxon
712
   *
713
   * @param $taxon
714
   * @param $descriptionElements
715
   *   an associative array with two elements:
716
   *   - '#type': must be 'DTO'
717
   *   - 'DistributionInfoDTO': a CDM DistributionInfoDTO object as returned by the DistributionInfo web service
718
   * @param $feature
719
   *
720
   * @return array
721
   *  A drupal render array
722
   *
723
   * @ingroup compose
724
   */
725
  function compose_feature_block_distribution($taxon, $descriptionElements, $feature) {
726
    $text_data_glue = '';
727
    $text_data_sortOutArray = FALSE;
728
    $text_data_enclosingTag = 'ul';
729
    $text_data_out_array = array();
730

    
731
    $distributionElements = NULL;
732
    $distribution_info_dto = NULL;
733
    $distribution_sortOutArray = FALSE;
734

    
735
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
736

    
737
    // TODO use feature_block_settings instead of DISTRIBUTION_ORDER_MODE
738
    if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) != 'TREE') {
739
      $distribution_glue = '';
740
      $distribution_enclosingTag = 'dl';
741
    } else {
742
      $distribution_glue = '';
743
      $distribution_enclosingTag = 'ul';
744
    }
745

    
746
    if (!isset($descriptionElements['#type']) || !$descriptionElements['#type'] == 'DTO') {
747
      // skip the DISTRIBUTION section if there is no DTO type element
748
      return array(); // FIXME is it ok to return an empty array?
749
    }
750

    
751
    $block = feature_block(
752
      cdm_term_representation($feature, 'Unnamed Feature'),
753
      $feature
754
    );
755

    
756
    // $$descriptionElements['TextData'] is added to to the feature node in merged_taxon_feature_tree()
757
    if (isset($descriptionElements['TextData'])) {
758
      // --- TextData
759
      foreach ($descriptionElements['TextData'] as $text_data_element) {
760
        $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
761
        $repr = drupal_render($text_data_render_array);
762

    
763
        if (!array_search($repr, $text_data_out_array)) { // de-duplication !!
764
          $text_data_out_array[] = $repr;
765
          // TODO HINT: sorting in compose_feature_block_elements will
766
          // not work since this array contains html attributes with uuids
767
          // and what is about cases like the bibliography where
768
          // any content can be prefixed with some foot-note anchors?
769
          $text_data_sortOutArray = TRUE;
770
          $text_data_glue = '<br/> ';
771
          $text_data_enclosingTag = 'p';
772
        }
773
      }
774
    }
775

    
776

    
777
    if ($text_data_out_array && variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
778
      $block->content[] = compose_feature_block_elements(
779
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
780
      );
781
    }
782

    
783
    // --- Distribution map
784
    $distribution_map_query_parameters = NULL;
785
    if (isset($descriptionElements['DistributionInfoDTO'])) {
786
      $distribution_map_query_parameters = $descriptionElements['DistributionInfoDTO']->mapUriParams;
787
    }
788
    $map_render_element = compose_distribution_map($taxon, $distribution_map_query_parameters);
789
    $block->content[] = $map_render_element;
790

    
791
    $dto_out_array = array();
792

    
793
    // --- Condensed Distribution
794
    if(variable_get(DISTRIBUTION_CONDENSED) && isset($descriptionElements['DistributionInfoDTO']->condensedDistribution)){
795
      $condensed_distribution_markup = '<p class="condensed_distribution">';
796

    
797
      $isFirst = true;
798
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous[0])){
799
        foreach($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous as $cdItem){
800
          if(!$isFirst){
801
            $condensed_distribution_markup .= ' ';
802
          }
803
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->idInVocabulary . '">'
804
          . $cdItem->areaStatusLabel . '</span>';
805
          $isFirst = false;
806
        }
807
      }
808

    
809
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign[0])) {
810
        if(!$isFirst){
811
          $condensed_distribution_markup .= ' ';
812
        }
813
        $isFirst = TRUE;
814
        $condensed_distribution_markup .= '[';
815
        foreach ($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign as $cdItem) {
816
          if (!$isFirst) {
817
            $condensed_distribution_markup .= ' ';
818
          }
819
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->titleCache . '">'
820
            . $cdItem->areaStatusLabel . '</span>';
821
          $isFirst = false;
822
        }
823
        $condensed_distribution_markup .= ']';
824
      }
825

    
826
      $condensed_distribution_markup .= '&nbsp;' . l(
827
          font_awesome_icon_markup(
828
            'fa-info-circle',
829
            array(
830
              'alt'=>'help',
831
              'class' => array('superscript')
832
            )
833
          ),
834
          'cdm_dataportal/help/condensed_distribution',
835
          array('html' => TRUE));
836
      $condensed_distribution_markup .= '</p>';
837
      $dto_out_array[] = $condensed_distribution_markup;
838
    }
839

    
840
    // --- tree or list
841
    if (isset($descriptionElements['DistributionInfoDTO'])) {
842
      $distribution_info_dto = $descriptionElements['DistributionInfoDTO'];
843

    
844
      // --- tree
845
      if (is_object($distribution_info_dto->tree)) {
846
        $distribution_tree_render_array = compose_distribution_hierarchy($distribution_info_dto->tree, $feature_block_settings);
847
        $dto_out_array[] = drupal_render($distribution_tree_render_array);
848
      }
849

    
850
      // --- sorted element list
851
      if (is_array($distribution_info_dto->elements) && count($distribution_info_dto->elements) > 0) {
852
        foreach ($distribution_info_dto->elements as $descriptionElement) {
853
          if (is_object($descriptionElement->area)) {
854
            $sortKey = $descriptionElement->area->representation_L10n;
855
            $distributionElements[$sortKey] = $descriptionElement;
856
          }
857
        }
858
        ksort($distributionElements);
859
        $distribution_element_render_array = compose_description_elements_distribution($distributionElements);
860
        $dto_out_array[] = drupal_render($distribution_element_render_array);
861

    
862
      }
863
      //
864
      $block->content[] = compose_feature_block_elements(
865
        $dto_out_array, $feature, $distribution_glue, $distribution_sortOutArray
866
      );
867
    }
868

    
869
    // --- TextData at the bottom
870
    if ($text_data_out_array && !variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
871
      $block->content[] = compose_feature_block_elements(
872
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
873
      );
874
    }
875

    
876
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . UUID_DISTRIBUTION)));
877
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
878
    $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
879

    
880
    return $block;
881
  }
882

    
883

    
884
  /**
885
   * Composes a drupal render array for single CDM TextData description element.
886
   *
887
   * @param $element
888
   *    The CDM TextData description element.
889
   *  @param $feature_uuid
890
   *
891
   * @return array
892
   *   A drupal render array with the following elements being used:
893
   *    - #tag: either 'div', 'li', ...
894
   *    ⁻ #attributes: class attributes
895
   *    - #value_prefix: (optionally) contains footnote anchors
896
   *    - #value: contains the textual content
897
   *    - #value_suffix: (optionally) contains footnote keys
898
   *
899
   * @ingroup compose
900
   */
901
  function compose_description_element_text_data($element, $feature_uuid, $feature_block_settings) {
902

    
903
    $footnote_list_key_suggestion = $feature_uuid;
904

    
905
    /*
906
    // FIXME remove this hack ---------------------------
907
    $default_theme = variable_get('theme_default', NULL);
908
    if($default_theme == 'garland_cichorieae' || $default_theme == 'cyprus'
909
      || $default_theme == 'flore_afrique_centrale' || $default_theme == 'flora_malesiana' || $default_theme == 'flore_gabon' ){
910
      $asListElement = $feature_block_settings['as_list'] == 'ul';
911
    }
912
    // --------------------------------------------------
913
    */
914

    
915
    $element_markup = '';
916
    if (isset($element->multilanguageText_L10n->text)) {
917
      // TODO replacement of \n by <br> should be configurable
918
      $element_markup = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
919
    }
920

    
921
    $render_array = compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion);
922

    
923
    return $render_array;
924
  }
925

    
926

    
927
/**
928
 * Theme function to render CDM DescriptionElements of the type TaxonInteraction.
929
 *
930
 * @param $element
931
 *  The CDM TaxonInteraction entity
932
 *
933
 * @return
934
 *  A drupal render array
935
 *
936
 * @ingroup compose
937
 */
938
function compose_description_element_taxon_interaction($element, $feature_block_settings) {
939

    
940
  $out = '';
941
  $enclosing_tag = cdm_feature_block_element_tag_name($feature_block_settings);
942

    
943
  if (isset($element->description_L10n)) {
944
    $out .=  ' ' . $element->description_L10n;
945
  }
946

    
947
  if(isset($element->taxon2)){
948
    $out = render_taxon_or_name($element->taxon2, url(path_to_taxon($element->taxon2->uuid)));
949
  }
950

    
951
  $annotations_and_sources = handle_annotations_and_sources(
952
    $element,
953
    $feature_block_settings,
954
    $out, // the description element text
955
    $element->feature->uuid
956
  );
957

    
958
  if(!empty($annotations_and_sources['source_references'])){
959
    $out .= ' ' . join(' ', $annotations_and_sources['source_references'] );
960
  }
961
  return '<' . $enclosing_tag . '>' . $out . $annotations_and_sources['foot_note_keys'] . '</' . $enclosing_tag . '>';
962

    
963
}
964

    
965

    
966
/**
967
 * Renders a single instance of the type IndividualsAssociations.
968
 *
969
 * @param $element
970
 *   The CDM IndividualsAssociations entity.
971
 * @param $feature_block_settings
972
 *
973
 * @return array
974
 *   Drupal render array
975
 *
976
 * @ingroup compose
977
 */
978
function compose_description_element_individuals_association($element, $feature_block_settings) {
979

    
980
  $out = '';
981
  $enclosing_tag = cdm_feature_block_element_tag_name($feature_block_settings);
982

    
983
  $render_array = compose_cdm_specimenOrObservation($element->associatedSpecimenOrObservation);
984

    
985
  if (isset($element->description_L10n)) {
986
    $out .=  ' ' . $element->description_L10n;
987
  }
988

    
989
  $out .= drupal_render($render_array);
990

    
991
  $annotations_and_sources = handle_annotations_and_sources(
992
    $element,
993
    $feature_block_settings,
994
    $out, // the description element text
995
    $element->feature->uuid
996
  );
997

    
998
  if(!empty($annotations_and_sources['source_references'])){
999
    $out .= ' ' . join(' ', $annotations_and_sources['source_references'] );
1000
  }
1001
  return '<' . $enclosing_tag . '>' . $out . $annotations_and_sources['foot_note_keys'] . '</' . $enclosing_tag . '>';
1002

    
1003

    
1004
}
1005

    
1006
/**
1007
 * Renders a single instance of the type CategoricalData.
1008
 *
1009
 * @param $element
1010
 *  The CDM CategoricalData entity#
1011
 * @param $feature_block_settings
1012
 *
1013
 * @return string
1014
 *   a html representation of the given CategoricalData element
1015
 *
1016
 * @ingroup compose
1017
 */
1018
function compose_description_element_categorical_data($element, $feature_block_settings) {
1019

    
1020
  $enclosing_tag = cdm_feature_block_element_tag_name($feature_block_settings);
1021

    
1022
  $state_data_strings = array();
1023
  if (isset($element->stateData)) {
1024
    foreach ($element->stateData as $state_data) {
1025

    
1026
      $state  = NULL;
1027

    
1028
      if (isset($state_data->state)) {
1029
        $state = cdm_term_representation($state_data->state);
1030
      }
1031

    
1032
      if (isset($state_data->modifyingText_L10n)) {
1033
        $state = ' ' . $state_data->modifyingText_L10n;
1034
      }
1035

    
1036
      $modifiers_strings = cdm_modifers_representations($state_data);
1037

    
1038
      $state_data_strings[] = $state . ($modifiers_strings ? ' ' . $modifiers_strings : '');
1039

    
1040
    }
1041
  }
1042

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

    
1045
  $feature_block_settings = get_feature_block_settings($element->feature->uuid);
1046
  $annotations_and_sources = handle_annotations_and_sources(
1047
    $element,
1048
    $feature_block_settings,
1049
    $out, // The description element text.
1050
    $element->feature->uuid
1051
  );
1052

    
1053
  if (!empty($annotations_and_sources['source_references'])) {
1054
    $out .= ' ' . implode(' ', $annotations_and_sources['source_references']);
1055
  }
1056
  return '<' . $enclosing_tag . '>' . $out . $annotations_and_sources['foot_note_keys'] . '</' . $enclosing_tag . '>';
1057
}
1058

    
1059

    
1060
/**
1061
 * Theme function to render CDM DescriptionElements of the type QuantitativeData.
1062
 *
1063
 * The function renders the statisticalValues contained in the QuantitativeData
1064
 * entity according to the following scheme:
1065
 *
1066
 * (ExtremeMin)-Min-Average-Max-(ExtremeMax)
1067
 *
1068
 * All modifiers of these values are appended.
1069
 *
1070
 * If the QuantitativeData is containing more statisticalValues with further
1071
 * statisticalValue types, these additional measures will be appended to the
1072
 * above string separated by whitespace.
1073
 *
1074
 * Special cases;
1075
 * 1. Min==Max: this will be interpreted as Average
1076
 *
1077
 * @param array $variables
1078
 *   An associative array containing:
1079
 *    - element: the QuantitativeData element
1080
 *
1081
 * @return string
1082
 *   a html representation of the given QuantitativeData element
1083
 *
1084
 * @ingroup themeable
1085
 */
1086
function compose_description_element_quantitative_data($element, $feature_block_settings) {
1087
  /*
1088
   * - statisticalValues
1089
   *   - value
1090
   *   - modifiers
1091
   *   - type
1092
   * - unit->representation_L10n
1093
   * - modifyingText
1094
   * - modifiers
1095
   * - sources
1096
   */
1097

    
1098
  $out = '';
1099
  $type_representation = NULL;
1100
  $min_max = min_max_array();
1101

    
1102

    
1103
  $other_values = array();
1104

    
1105
  if (isset($element->statisticalValues)) {
1106
    $other_values_markup = array();
1107
    foreach ($element->statisticalValues as $statistical_val) {
1108

    
1109
      // compile the full value string which also may contain modifiers
1110
      if (isset($statistical_val->value)) {
1111
        $statistical_val->_value = $statistical_val->value;
1112
      }
1113
      $val_modifiers_strings = cdm_modifers_representations($statistical_val);
1114
      if ($val_modifiers_strings) {
1115
        $statistical_val->_value = ' ' . $val_modifiers_strings . ' ' . $statistical_val->_value;
1116
      }
1117

    
1118
      // either put into min max array or into $other_values
1119
      // for generic output to be appended to 'min-max' string
1120
      if (array_key_exists($statistical_val->type->titleCache, $min_max)) {
1121
        $min_max[$statistical_val->type->titleCache] = $statistical_val;
1122
      }
1123
      else {
1124
        $other_values[] = $statistical_val;
1125
      }
1126
    } // end of loop over statisticalValues
1127

    
1128
    // create markup
1129

    
1130
    $min_max_markup = min_max_markup($min_max);
1131

    
1132

    
1133
    foreach ($other_values as $statistical_val) {
1134
      $statistical_val_type_representation = NULL;
1135
      if (isset($statistical_val->type)) {
1136
        $statistical_val_type_representation = cdm_term_representation($statistical_val->type);
1137
        // $statistical_val->type->termType;
1138
        // $statistical_val->type->userFriendlyTypeName;
1139
      }
1140
      $value_markup = '<span class="' . html_class_attribute_ref($statistical_val) . ' ' . $statistical_val->type->termType . ' ">'
1141
        . $statistical_val->_value . '</span>';
1142
      $value_markup = $value_markup .
1143
        ($statistical_val_type_representation ? ' <span class="type">' . $statistical_val_type_representation . '</span>' : '');
1144
      $other_values_markup[] = $value_markup;
1145
    }
1146

    
1147

    
1148
    $out .= $min_max_markup . ' ' . implode($other_values_markup, ', ');
1149
  }
1150

    
1151
  if (isset($element->unit)) {
1152
    $out .= ' <span class="unit" title="'
1153
      . cdm_term_representation($element->unit) . '">'
1154
      . cdm_term_representation_abbreviated($element->unit)
1155
      . '</span>';
1156
  }
1157

    
1158
  // modifiers of the description element itself
1159
  $modifier_string = cdm_modifers_representations($element);
1160
  $out .= ($modifier_string ? ' ' . $modifier_string : '');
1161
  if (isset($element->modifyingText_L10n)) {
1162
    $out = $element->modifyingText_L10n . ' ' . $out;
1163
  }
1164

    
1165
  $feature_block_settings = get_feature_block_settings($element->feature->uuid);
1166
  $annotations_and_sources = handle_annotations_and_sources(
1167
    $element,
1168
    $feature_block_settings,
1169
    $out, // the description element text
1170
    $element->feature->uuid
1171
  );
1172

    
1173
  if (!empty($annotations_and_sources['source_references'])) {
1174
    $out .= ' ' . implode(' ', $annotations_and_sources['source_references']);
1175
  }
1176
  return $out . $annotations_and_sources['foot_note_keys'];
1177

    
1178
}
1179

    
1180

    
1181
/**
1182
 * Creates a render array for string representations of description elements for the given feature.
1183
 *
1184
 * @param array $elements
1185
 *   An array of strings. Can be plaintext or markup.
1186
 * @param  $feature :
1187
 *  The feature to which the elements given in $elements are belonging to.
1188
 * @param string $glue :
1189
 *  Defaults to empty string.
1190
 * @param bool $sort
1191
 *   Boolean Whether to sort the $elements alphabetically, default is FALSE
1192
 *
1193
 * @return array|void
1194
 *
1195
 * @ingroup compose
1196
 */
1197
  function compose_feature_block_elements(array $elements, $feature, $glue = '', $sort = FALSE)
1198
  {
1199

    
1200
    $feature_block_settings = get_feature_block_settings($feature->uuid);
1201
    $enclosing_tag = $feature_block_settings['as_list'];
1202

    
1203
    $out = '<' . $enclosing_tag . ' class="feature-block-elements" id="' . $feature->representation_L10n . '">';
1204

    
1205
    if ($sort) {
1206
      sort($elements);
1207
    }
1208

    
1209
    //TODO it would be nice if any element would get "feature-block-element" as additional class attribute
1210
    //     this will become possible once all $elements are real Drupal render arrays
1211
    $out .= join($glue, $elements) ;
1212

    
1213
    $out .= '</' . $enclosing_tag . '>';
1214
    return markup_to_render_array($out);
1215
  }
1216

    
1217

    
1218
  /* compose nameInSource or originalNameString as markup
1219
   *
1220
   * @param $source
1221
   * @param $do_link_to_name_used_in_source
1222
   * @param $suppress_for_shown_taxon
1223
   *    the nameInSource will be suppressed when it has the same name as the accepted taxon
1224
   *    for which the taxon page is being created, Defaults to TRUE
1225
   *
1226
   * @return array
1227
   *    A Drupal render array with an additional element, the render array is empty
1228
   *    if the source had no name in source information
1229
   *    - #_plaintext: contains the plaintext version of the name (custom element)
1230
   *
1231
   * @ingroup compose
1232
   */
1233
  function compose_name_in_source($source, $do_link_to_name_used_in_source, $suppress_for_shown_taxon = TRUE) {
1234

    
1235
    $plaintext = NULL;
1236
    $markup = NULL;
1237
    $name_in_source_render_array = array();
1238

    
1239
    static $taxon_page_accepted_name = '';
1240
    if($suppress_for_shown_taxon && arg(1) == 'taxon' && empty($taxon_page_accepted_name)){
1241

    
1242
      $current_taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, arg(2));
1243
      $taxon_page_accepted_name = $current_taxon->name->titleCache;
1244
    }
1245

    
1246
    if (isset($source->nameUsedInSource->uuid) && isset($source->nameUsedInSource->titleCache)) {
1247
      // it is a DescriptionElementSource !
1248
      $plaintext = $source->nameUsedInSource->titleCache;
1249
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1250
        return $name_in_source_render_array; // SKIP this name
1251
      }
1252
      $markup = render_taxon_or_name($source->nameUsedInSource);
1253
      if ($do_link_to_name_used_in_source) {
1254
        $markup = l(
1255
          $markup,
1256
          path_to_name($source->nameUsedInSource->uuid),
1257
          array(
1258
            'attributes' => array(),
1259
            'absolute' => TRUE,
1260
            'html' => TRUE,
1261
          ));
1262
      }
1263
    }
1264
    else if (isset($source->originalNameString) && !empty($source->originalNameString)) {
1265
      // the name used in source can not be expressed as valid taxon name,
1266
      // so the editor has chosen to put the freetext name into ReferencedEntityBase.originalNameString
1267
      // field
1268
      // using the originalNameString as key to avoid duplicate entries
1269
      $plaintext = $source->originalNameString;
1270
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1271
        return $name_in_source_render_array; // SKIP this name
1272
      }
1273
      $markup = $source->originalNameString;
1274
    }
1275

    
1276
    if ($plaintext) { // checks if we have any content
1277
      $name_in_source_render_array = markup_to_render_array($markup);
1278
      $name_in_source_render_array['#_plaintext'] = $plaintext;
1279
    }
1280

    
1281
    return $name_in_source_render_array;
1282
  }
1283

    
1284

    
1285

    
1286
  /**
1287
   * Return HTML for a list of description elements.
1288
   *
1289
   * Usually these are of a specific feature type.
1290
   *
1291
   * @param $descriptionElements
1292
   *   array of descriptionElements which belong to the same feature.
1293
   *   These descriptions elements of a Description must be ordered by the chosen feature tree by
1294
   *   calling the function _mergeFeatureTreeDescriptions().
1295
   *   @see _mergeFeatureTreeDescriptions()
1296
   *
1297
   * @param  $feature_uuid
1298
   *
1299
   * @return
1300
   *    A drupal render array for the $descriptionElements, may be an empty array if the textual content was empty.
1301
   *    Footnote key or anchors are not considered to be textual content.
1302
   *
1303
   * @ingroup compose
1304
   */
1305
  function compose_feature_block_items_generic($descriptionElements, $feature) {
1306

    
1307
    $render_array = array();
1308
    $elements_out_array = array();
1309
    $distribution_tree = null;
1310

    
1311
    /*
1312
     * $feature_block_has_content will be set true if at least one of the
1313
     * $descriptionElements contains some text which makes up some content
1314
     * for the feature block. Footnote keys are not considered
1315
     * to be content in this sense.
1316
     */
1317
    $feature_block_has_content = false;
1318

    
1319
    RenderHints::pushToRenderStack($feature->uuid);
1320

    
1321
    if (is_array($descriptionElements)) {
1322
      foreach ($descriptionElements as $descriptionElement) {
1323
          /* decide based on the description element class
1324
           *
1325
           * Features handled here:
1326
           * all except DISTRIBUTION, COMMON_NAME, USES, IMAGES,
1327
           *
1328
           *
1329
           * TODO provide api_hook as extension point for this?
1330
           */
1331
          $feature_block_settings = get_feature_block_settings($descriptionElement->feature->uuid);
1332
        switch ($descriptionElement->class) {
1333
            case 'TextData':
1334
              $text_data_render_array = compose_description_element_text_data($descriptionElement, $descriptionElement->feature->uuid, $feature_block_settings);
1335
              $feature_block_has_content = $feature_block_has_content || !empty($text_data_render_array['#value']);
1336
              $elements_out_array[] = drupal_render($text_data_render_array);
1337
              break;
1338
            case 'CategoricalData':
1339
              $feature_block_has_content = true;
1340
              $elements_out_array[] = compose_description_element_categorical_data($descriptionElement, $feature_block_settings);
1341
              break;
1342
            case 'QuantitativeData':
1343
              $feature_block_has_content = true;
1344
              $elements_out_array[] = compose_description_element_quantitative_data($descriptionElement, $feature_block_settings);
1345
              break;
1346
            case 'IndividualsAssociation':
1347
              $feature_block_has_content = true;
1348
              $elements_out_array[] = compose_description_element_individuals_association($descriptionElement, $feature_block_settings);
1349
              break;
1350
            case 'TaxonInteraction':
1351
              $feature_block_has_content = true;
1352
              $elements_out_array[] = compose_description_element_taxon_interaction($descriptionElement, $feature_block_settings);
1353
              break;
1354
            case 'Uses':
1355
              /* IGNORE Uses classes, these are handled completely in theme_cdm_UseDescription */
1356
              break;
1357
            default:
1358
              $feature_block_has_content = true;
1359
              $elements_out_array[] = '<li>No method for rendering unknown description class: ' . $descriptionElement->class . '</li>';
1360
          }
1361
      } // --- END loop over normal description element arrays
1362

    
1363
      // If feature = CITATION sort the list of sources.
1364
      // This is ONLY for FLORA MALESIANA and FLORE d'AFRIQUE CENTRALE.
1365
      if (isset($descriptionElement) && $descriptionElement->feature->uuid == UUID_CITATION) {
1366
        sort($elements_out_array);
1367
      }
1368
    }
1369

    
1370
    if($feature_block_has_content){
1371
      $render_array = compose_feature_block_elements(
1372
        $elements_out_array, $feature
1373
      );
1374
    }
1375

    
1376
    RenderHints::popFromRenderStack();
1377
    return $render_array;
1378
  }
1379

    
1380
/**
1381
 * Composes block of common names for the given DescriptionElements $elements which must be of the feature CommonName
1382
 *
1383
 * @parameter $elements
1384
 *  an array of CDM DescriptionElements either of type CommonName or TextData
1385
 * @parameter $feature
1386
 *  the common feature of all $elements, must be CommonName
1387
 *
1388
 * @return
1389
 *   A drupal render array
1390
 *
1391
 * @ingroup compose
1392
 */
1393
function compose_feature_block_items_feature_common_name($elements, $feature, $weight = FALSE) {
1394

    
1395
  $common_name_out = '';
1396
  $common_name_feature_elements = array();
1397
  $textData_commonNames = array();
1398

    
1399
  $footnote_key_suggestion = 'common-names-feature-block';
1400

    
1401
  $feature_block_settings = get_feature_block_settings(UUID_COMMON_NAME);
1402

    
1403
  $element_tag_name = cdm_feature_block_element_tag_name($feature_block_settings);
1404

    
1405
  if (is_array($elements)) {
1406
    foreach ($elements as $element) {
1407

    
1408
      if ($element->class == 'CommonTaxonName') {
1409

    
1410
        // common name without a language or area, should not happen but is possible
1411
        $language_area_key = '';
1412
        if (isset($element->language->representation_L10n)) {
1413
          $language_area_key .= '<b>' . $element->language->representation_L10n . '</b>';
1414
        }
1415
        if(isset($element->area->titleCache) && strlen($element->area->titleCache) > 0){
1416
          $language_area_key .= ($language_area_key ? ' '  : '') . '(' . $element->area->titleCache . ')';
1417
        }
1418

    
1419
        if(isset($common_names[$language_area_key][$element->name])) {
1420
          // same name already exists for language and areae combination, se we merge the description elements
1421
          cdm_merge_description_elements($common_names[$language_area_key][$element->name], $element);
1422
        } else{
1423
          // otherwise add as new entry
1424
          $common_names[$language_area_key][$element->name] = $element;
1425
        }
1426

    
1427
      }
1428
      elseif ($element->class == 'TextData') {
1429
        $textData_commonNames[] = $element;
1430
      }
1431
    }
1432
  }
1433
  // Handling common names.
1434
  if (isset($common_names) && count($common_names) > 0) {
1435
    // Sorting the array based on the key (language, + area if set).
1436
    // Comment @WA there are common names without a language, so this sorting
1437
    // can give strange results.
1438
    ksort($common_names);
1439

    
1440
    // loop over set of elements per language area
1441
    foreach ($common_names as $language_area_key => $elements) {
1442
      ksort($elements); // sort names alphabetically
1443
      $per_language_area_out = array();
1444
      // loop over set of individual elements
1445
      foreach ($elements as $element) {
1446
        if ($element->name) {
1447
          $annotations_and_sources = handle_annotations_and_sources($element, $feature_block_settings, $element->name, $footnote_key_suggestion);
1448
          $source_references_markup = '';
1449
          if(!empty($annotations_and_sources['source_references'])){
1450
            $source_references_markup = ' ' . join(', ', $annotations_and_sources['source_references']);
1451
          }
1452
          $per_language_area_out[] = '<' . $element_tag_name. ' class="' . html_class_attribute_ref($element) . '">'
1453
            . $element->name . $source_references_markup . $annotations_and_sources['foot_note_keys'] . '</' . $element_tag_name. '>';
1454
        }
1455
      } // End of loop over set of individual elements
1456
      $common_name_feature_elements[] = ($language_area_key ? $language_area_key . ': ' : '' ) . join(', ', $per_language_area_out);
1457
    } // End of loop over set of elements per language area
1458

    
1459

    
1460
    $common_name_feature_elements_render_array = compose_feature_block_elements(
1461
      $common_name_feature_elements, $feature, '; ', FALSE
1462
    );
1463
    $common_name_out .= $common_name_feature_elements_render_array['#markup'];
1464

    
1465
  }
1466

    
1467
  // Handling commons names as text data.
1468
  $text_data_out = array();
1469

    
1470
  foreach ($textData_commonNames as $text_data_element) {
1471
    /* footnotes are not handled correctly in compose_description_element_text_data,
1472
       need to set 'common-names-feature-block' as $footnote_key_suggestion */
1473
    RenderHints::setFootnoteListKey($footnote_key_suggestion);
1474
    $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
1475
    $text_data_out[] = drupal_render($text_data_render_array);
1476
  }
1477

    
1478
  $common_name_out_text_data = compose_feature_block_elements(
1479
    $text_data_out, $feature
1480
  );
1481

    
1482
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . $footnote_key_suggestion));
1483
  $footnotes .= theme('cdm_footnotes', array('footnoteListKey' => $footnote_key_suggestion)); // FIXME is this needed at all?
1484
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnote_key_suggestion));
1485

    
1486
  return  markup_to_render_array(
1487
    '<div class="common_names_as_common_names">' . $common_name_out . '</div>'
1488
    .'<div class="common_names_as_text_data">' . $common_name_out_text_data['#markup'] . '</div>'
1489
    .$footnotes,
1490
    $weight
1491
  );
1492
}
1493

    
1494
  /**
1495
   * Composes the render array for a CDM Distribution description element
1496
   *
1497
   * @param array $description_elements
1498
   *   Array of CDM Distribution instances
1499
   * @param $enclosingTag
1500
   *   The html tag to be use for the enclosing element
1501
   *
1502
   * @return array
1503
   *   A Drupal render array
1504
   *
1505
   * @ingroup compose
1506
   */
1507
  function compose_description_elements_distribution($description_elements){
1508

    
1509
    $out = '';
1510
    RenderHints::pushToRenderStack('descriptionElementDistribution');
1511
    RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1512

    
1513
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
1514
    $enclosingTag = cdm_feature_block_element_tag_name($feature_block_settings);
1515

    
1516
    foreach ($description_elements as $description_element) {
1517
      $annotations_and_sources = handle_annotations_and_sources(
1518
        $description_element,
1519
        $feature_block_settings,
1520
        $description_element->area->representation_L10n,
1521
        UUID_DISTRIBUTION
1522
      );
1523

    
1524

    
1525
      list($status_label, $status_markup) = distribution_status_label_and_markup($description_element);
1526

    
1527
      $out .= '<' . $enclosingTag . ' class="descriptionElement descriptionElement-' . $description_element->uuid
1528
        . ' " title="' . $status_label. '">'
1529
        . $description_element->area->representation_L10n
1530
        . $status_markup;
1531
      if(!empty($annotations_and_sources['source_references'])){
1532
        $out .= ' ' . join(' ', $annotations_and_sources['source_references'] );
1533
      }
1534
      $out .= $annotations_and_sources['foot_note_keys']   . ' </' . $enclosingTag . '>';
1535
    }
1536

    
1537
    RenderHints::popFromRenderStack();
1538
    return markup_to_render_array($out);
1539
  }
1540

    
1541
  /**
1542
   * @param $descriptionElement
1543
   * @return array
1544
   */
1545
  function distribution_status_label_and_markup($descriptionElement) {
1546
    $status_markup = '';
1547
    $status_label = '';
1548

    
1549
    if (isset($descriptionElement->status)) {
1550
      $status_label = $descriptionElement->status->representation_L10n;
1551
      $status_markup = '<span class="distributionStatus distributionStatus-' . $descriptionElement->status->idInVocabulary . '"> '
1552
        . $status_label . ' </span>';
1553

    
1554
    };
1555
    return array($status_label, $status_markup);
1556
  }
1557

    
1558

    
1559
  /**
1560
   * Provides the merged feature tree for a taxon profile page.
1561
   *
1562
   * The merging of the profile feature tree is actully done in
1563
   * _mergeFeatureTreeDescriptions(). See this method  for details
1564
   * on the structure of the merged tree.
1565
   *
1566
   * This method provides t hook which can be used to modify the
1567
   * merged feature tree after it has been created, see
1568
   * hook_merged_taxon_feature_tree_alter()
1569
   *
1570
   * @param $taxon
1571
   *   A CDM Taxon instance
1572
   *
1573
   * @return object
1574
   *  The merged feature tree
1575
   *
1576
   */
1577
  function merged_taxon_feature_tree($taxon) {
1578

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

    
1582

    
1583
    // 2. find the distribution feature node
1584
    $distribution_node =& cdm_feature_tree_find_node($merged_tree->root->childNodes, UUID_DISTRIBUTION);
1585

    
1586
    if ($distribution_node) {
1587
      // 3. get the distributionInfoDTO
1588
      $query_parameters = cdm_distribution_filter_query();
1589
      $query_parameters['part'] = array('mapUriParams');
1590
      if(variable_get(DISTRIBUTION_CONDENSED)){
1591
        $query_parameters['part'][] = 'condensedDistribution';
1592
      }
1593
      if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) == 'TREE') {
1594
        $query_parameters['part'][] = 'tree';
1595
      }
1596
      else {
1597
        $query_parameters['part'][] = 'elements';
1598
      }
1599
      $query_parameters['omitLevels'] = array();
1600
      foreach(variable_get(DISTRIBUTION_TREE_OMIT_LEVELS, array()) as $uuid){
1601
        if(is_uuid($uuid)){
1602
          $query_parameters['omitLevels'][] = $uuid;
1603
        }
1604
      }
1605
      $customStatusColorsJson = variable_get(DISTRIBUTION_STATUS_COLORS, NULL);
1606
      if ($customStatusColorsJson) {
1607
        $query_parameters['statusColors'] = $customStatusColorsJson;
1608
      }
1609

    
1610
      $distribution_info_dto = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION_DISTRIBUTION_INFO_FOR, $taxon->uuid, queryString($query_parameters));
1611
      // 4. get distribution TextData is there are any
1612
      $distribution_text_data = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON,
1613
        array(
1614
          'taxon' => $taxon->uuid,
1615
          'type' => 'TextData',
1616
          'features' => UUID_DISTRIBUTION
1617
        )
1618
      );
1619

    
1620
      // 5. put all distribution data into the distribution feature node
1621
      if ($distribution_text_data //if text data exists
1622
        || ($distribution_info_dto && isset($distribution_info_dto->tree) && $distribution_info_dto->tree->rootElement->numberOfChildren > 0) // OR if tree element has distribution elements
1623
        || ($distribution_info_dto && !empty($distribution_info_dto->elements))
1624
      ) { // OR if DTO has distribution elements
1625
        $distribution_node->descriptionElements = array('#type' => 'DTO');
1626
        if ($distribution_text_data) {
1627
          $distribution_node->descriptionElements['TextData'] = $distribution_text_data;
1628
        }
1629
        if ($distribution_info_dto) {
1630
          $distribution_node->descriptionElements['DistributionInfoDTO'] = $distribution_info_dto;
1631
        }
1632
      }
1633
    }
1634

    
1635
    // allows modifying the merged tree via a the hook_cdm_feature_node_block_content_alter
1636
    drupal_alter('merged_taxon_feature_tree', $taxon, $merged_tree);
1637

    
1638
    return $merged_tree;
1639
  }
1640

    
1641

    
1642
  function compose_distribution_hierarchy($distribution_tree, $feature_block_settings){
1643

    
1644
    static $hierarchy_style;
1645
    // TODO expose $hierarchy_style to administration of provide a hook
1646
    if( !isset($hierarchy_style)){
1647
      $hierarchy_style = array(
1648
        // level 2
1649
        array(
1650
          'label_suffix' => '',
1651
          'element_glue' => ', ',
1652
          'element_set_pre' => '(',
1653
          'element_set_post' => ')'
1654
        ),
1655
        // level 1
1656
        array(
1657
          'label_suffix' => '',
1658
          'element_glue' => '; ',
1659
          'element_set_pre' => '',
1660
          'element_set_post' => ''
1661
        ),
1662
        // level 0
1663
        array(
1664
          'label_suffix' => ':',
1665
          'element_glue' => ' ',
1666
          'element_set_pre' => '',
1667
          'element_set_post' => ''
1668
        ),
1669
      );
1670
    }
1671

    
1672
    $render_array = array();
1673

    
1674
    RenderHints::pushToRenderStack('descriptionElementDistribution');
1675
    RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1676

    
1677
    // Returning NULL if there are no description elements.
1678
    if ($distribution_tree == null) {
1679
      return $render_array;
1680
    }
1681
    // for now we are not using a render array internally to avoid performance problems
1682
    $markup = '';
1683
    if (isset($distribution_tree->rootElement->children)) {
1684
      $tree_nodes = $distribution_tree->rootElement->children;
1685
      _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, $markup, $hierarchy_style);
1686
    }
1687

    
1688
    $render_array['distribution_hierarchy'] = markup_to_render_array(
1689
      $markup,
1690
      0,
1691
      '<div id="distribution_hierarchy" class="distribution_hierarchy">',
1692
      '</div>'
1693
    );
1694

    
1695
    return $render_array;
1696
  }
1697

    
1698
  /**
1699
   * this function should produce markup as the compose_description_elements_distribution()
1700
   * function.
1701
   *
1702
   * @see compose_description_elements_distribution()
1703
   *
1704
   * @param $distribution_tree
1705
   * @param $feature_block_settings
1706
   * @param $tree_nodes
1707
   * @param $markup
1708
   * @param $hierarchy_style
1709
   */
1710
  function _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, &$markup, $hierarchy_style, $level_index = -1){
1711

    
1712
    $level_index++;
1713
    static $enclosingTag = "span";
1714

    
1715
    $level_style = array_pop($hierarchy_style);
1716
    if(count($hierarchy_style) == 0){
1717
      // lowest defined level style will be reused for all following levels
1718
      $hierarchy_style[] = $level_style;
1719
    }
1720

    
1721
    $node_index = -1;
1722
    $per_node_markup = array();
1723
    foreach ($tree_nodes as $node){
1724

    
1725
      $per_node_markup[++$node_index] = '';
1726

    
1727
      $label = $node->nodeId->representation_L10n;
1728

    
1729
      $distributions = $node->data;
1730
      $distribution_uuids = array();
1731
      $distribution_aggregate = NULL;
1732
        foreach($distributions as $distribution){
1733

    
1734
          $distribution_uuids[] = $distribution->uuid;
1735

    
1736
          // if there is more than one distribution we aggregate the sources and
1737
          // annotations into a synthetic distribution so that the footnote keys
1738
          // can be rendered consistently
1739
          if(!$distribution_aggregate) {
1740
            $distribution_aggregate = $distribution;
1741
            if(!isset($distribution_aggregate->sources[0])){
1742
              $distribution_aggregate->sources = array();
1743
            }
1744
            if(!isset($distribution_aggregate->annotations[0])){
1745
              $distribution_aggregate->annotations = array();
1746
            }
1747
          } else {
1748
            if(isset($distribution->sources[0])) {
1749
              $distribution_aggregate->sources = array_merge($distribution_aggregate->sources,
1750
                $distribution->sources);
1751
            }
1752
            if(isset($distribution->annotations[0])) {
1753
              $distribution_aggregate->annotations = array_merge($distribution_aggregate->annotations,
1754
                $distribution->annotations);
1755
            }
1756
          }
1757
        }
1758

    
1759
      $status_label= '';
1760
      $status_markup = '';
1761
      $annotations_and_sources =  null;
1762
      if($distribution_aggregate) {
1763
        $annotations_and_sources = handle_annotations_and_sources(
1764
          $distribution_aggregate,
1765
          $feature_block_settings,
1766
          $label,
1767
          UUID_DISTRIBUTION
1768
        );
1769

    
1770
        list($status_label, $status_markup) = distribution_status_label_and_markup($distribution);
1771
      }
1772

    
1773
      $per_node_markup[$node_index] .= '<' . $enclosingTag . ' class="descriptionElement'
1774
        . join(' descriptionElement-', $distribution_uuids)
1775
        . ' level_index_' . $level_index
1776
        . ' " title="' . $status_label . '">'
1777
        . '<span class="area_label">' . $label
1778
        . $level_style['label_suffix'] . ' </span>'
1779
        .  $status_markup
1780
      ;
1781

    
1782
      if(isset($annotations_and_sources)){
1783
        if(!empty($annotations_and_sources['source_references'])){
1784
          $per_node_markup[$node_index] .= ' ' . join(', ' , $annotations_and_sources['source_references']);
1785
        }
1786
        if($annotations_and_sources['foot_note_keys']) {
1787
          $per_node_markup[$node_index] .= $annotations_and_sources['foot_note_keys'];
1788
        }
1789
      }
1790

    
1791
      if(isset($node->children[0])){
1792
        _compose_distribution_hierarchy(
1793
          $node->children,
1794
          $feature_block_settings,
1795
          $per_node_markup[$node_index],
1796
          $hierarchy_style,
1797
          $level_index
1798
        );
1799
      }
1800

    
1801
      $per_node_markup[$node_index] .= '</' . $enclosingTag . '>';
1802
    }
1803
    $markup .= $level_style['element_set_pre']  . join( $level_style['element_glue'], $per_node_markup) . $level_style['element_set_post'];
1804
  }
1805

    
1806

    
(2-2/8)