Project

General

Profile

Download (78.8 KB) Statistics
| Branch: | Tag: | Revision:
1 aa7c7290 Andreas Kohlbecker
<?php
2 c3a41ddb Andreas Kohlbecker
3
const BIBLIOGRAPHY_FOOTNOTE_KEY = PSEUDO_FEATURE_BIBLIOGRAPHY;
4
5 aa7c7290 Andreas Kohlbecker
6 ce29c528 Andreas Kohlbecker
/**
7
 * Returns the localized representations of the modifiers hold by the
8
 * supplied cdm instance concatenated into one string.
9
 *
10 a6d7907d Andreas Kohlbecker
 * @param object $is_modifieable
11 ce29c528 Andreas Kohlbecker
 *   cdm instance of an class implementing the interface IModifieable:
12
 *   DescriptionElementBase, StateDate, State
13
 *
14
 * @return String
15
 *   localized representations of the modifiers hold by the
16
 *   supplied cdm instance concatenated into one string
17
 */
18 a6d7907d Andreas Kohlbecker
function cdm_modifiers_representations($is_modifieable, $glue = ', ') {
19 ce29c528 Andreas Kohlbecker
  $modifiers_strings = array();
20 a6d7907d Andreas Kohlbecker
  if (isset($is_modifieable->modifiers)) {
21
    foreach ($is_modifieable->modifiers as $modifier) {
22 ce29c528 Andreas Kohlbecker
      $modifiers_strings[] = cdm_term_representation($modifier);
23 f19f47fa Andreas Kohlbecker
    }
24 aa7c7290 Andreas Kohlbecker
  }
25 ce29c528 Andreas Kohlbecker
  return implode(', ', $modifiers_strings);
26
}
27 bf97c2e8 Andreas Kohlbecker
28 ce29c528 Andreas Kohlbecker
/**
29
 * Filters the given set of description elements and prefers computed elements over others.
30
 *
31
 * Computed description elements
32
 * are identified by the MarkerType.COMPUTED()
33
 *
34
 * If the given set contains at least one computed element only
35
 * the computed elements are returned.
36
 *
37
 * @param array $description_elements
38
 *   An array of CDM DescriptionElementBase instances
39
 *
40
 * @return array
41
 *   only the computed description elements otherwise all others.
42
 *
43
 * @deprecated this is replaced by the cdmlib DistributionUtil class!!!
44
 */
45
function cdm_description_elements_prefer_computed($description_elements){
46 19e097a3 Andreas Kohlbecker
47 ce29c528 Andreas Kohlbecker
  $computed_elements = array();
48
  $other_elements = array();
49 19e097a3 Andreas Kohlbecker
50 ce29c528 Andreas Kohlbecker
  if (!empty($description_elements)) {
51
    foreach ($description_elements as $element) {
52
      if (cdm_entity_has_marker($element, UUID_MARKERTYPE_COMPUTED)) {
53
        $computed_elements[] = $element;
54
      }
55
      else {
56
        $other_elements[] = $element;
57 19e097a3 Andreas Kohlbecker
      }
58
    }
59 ce29c528 Andreas Kohlbecker
  }
60 19e097a3 Andreas Kohlbecker
61 ce29c528 Andreas Kohlbecker
  if (count($computed_elements) > 0) {
62
    return $computed_elements;
63
  }
64
  else {
65
    return $other_elements;
66 19e097a3 Andreas Kohlbecker
  }
67 ce29c528 Andreas Kohlbecker
}
68 ae0a5060 Andreas Kohlbecker
69 ce29c528 Andreas Kohlbecker
/**
70
 * Creates a query parameter array based on the setting stored in the drupal variable CDM_DISTRIBUTION_FILTER.
71
 *
72
 * @return array
73
 *   An array with distribution filter query parameters
74
 */
75
function cdm_distribution_filter_query() {
76
  $cdm_distribution_filter = get_array_variable_merged(CDM_DISTRIBUTION_FILTER, CDM_DISTRIBUTION_FILTER_DEFAULT);
77
  $query = array();
78 aa63dfb4 Andreas Kohlbecker
79 3eccfdb9 Andreas Kohlbecker
  $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
80
  if($feature_block_settings['sort_elements'] == SORT_HIERARCHICAL){
81
    $query['distributionOrder'] = 'AREA_ORDER';
82
  }
83 ec2cb73b Andreas Kohlbecker
84 846c0606 Andreas Kohlbecker
  $query['recipe'] = variable_get(DISTRIBUTION_CONDENSED_RECIPE, DISTRIBUTION_CONDENSED_RECIPE_DEFAULT);
85
86 3eccfdb9 Andreas Kohlbecker
87
88 ce29c528 Andreas Kohlbecker
  if ($cdm_distribution_filter['filter_rules']['statusOrderPreference']) {
89
    $query['statusOrderPreference'] = 1;
90
  }
91
  if ($cdm_distribution_filter['filter_rules']['subAreaPreference']) {
92
    $query['subAreaPreference'] = 1;
93
  }
94 6fbf1bd3 Andreas Kohlbecker
  if (is_array($cdm_distribution_filter['hiddenAreaMarkerType']) && count($cdm_distribution_filter['hiddenAreaMarkerType']) > 0) {
95
    $query['hiddenAreaMarkerType'] = '';
96
    foreach ($cdm_distribution_filter['hiddenAreaMarkerType'] as $marker_type => $enabled) {
97 ce29c528 Andreas Kohlbecker
      if ($enabled) {
98 6fbf1bd3 Andreas Kohlbecker
        $query['hiddenAreaMarkerType'] .= ($query['hiddenAreaMarkerType'] ? ',' : '') . $marker_type;
99 ce29c528 Andreas Kohlbecker
      }
100 dd1c109a Andreas Kohlbecker
    }
101 aa63dfb4 Andreas Kohlbecker
  }
102
103 ce29c528 Andreas Kohlbecker
  return $query;
104
}
105 aa63dfb4 Andreas Kohlbecker
106 ce29c528 Andreas Kohlbecker
/**
107
 * Merge the fields 'annotations', 'markers', 'sources', 'media' from the source CDM DescriptionElement into  the target.
108
 *
109
 * @param object $target
110
 *   The source CDM DescriptionElement
111
 * @param object $source
112
 *   The target CDM DescriptionElement
113
 */
114 a6d7907d Andreas Kohlbecker
function cdm_merge_description_elements($target, $source) {
115 ce29c528 Andreas Kohlbecker
  static $fields_to_merge = array('annotations', 'markers', 'sources', 'media');
116
117
  foreach ($fields_to_merge as $field) {
118
    if (is_array($source->$field)) {
119
      if (!is_array($target->$field)) {
120
        $target->$field = $source->$field;
121
      }
122
      else {
123
        $target->$field = array_merge($target->$field, $source->$field);
124 ae0a5060 Andreas Kohlbecker
      }
125
    }
126
  }
127 ce29c528 Andreas Kohlbecker
}
128 bf97c2e8 Andreas Kohlbecker
129 ce29c528 Andreas Kohlbecker
/**
130
 * Adds an entry to the end of the table of content items list
131
 *
132
 * The  table of content items are crated internally by calling
133
 * toc_list_item() the resulting item is added to the statically cached
134
 * list of toc elements
135
 *
136
 * @param string $label
137
 *   The label of toc entry
138
 * @param $class_attribute_suffix
139 a6d7907d Andreas Kohlbecker
 *   The suffix to be appended to the class attribute prefix:
140
 *   "feature-toc-item-"
141 ce29c528 Andreas Kohlbecker
 * @param string $fragment
142
 *   Optional parameter to define a url fragment different from the $label,
143
 *   if the $fragment is not defined the $label will be used
144 c3a41ddb Andreas Kohlbecker
 * @param boolean $as_first_element
145 a6d7907d Andreas Kohlbecker
 *   Place the new item as fist one. This parameter is ignored when $weight is
146
 *   being set
147 c3a41ddb Andreas Kohlbecker
 * @param integer $weight
148
 *   Defines the weight for the ordering the item in the toc list.
149
 *   The item will be placed at the end if weigth is NULL.
150 a6d7907d Andreas Kohlbecker
 *
151
 * @throws \Exception re-throws exception from theme()
152 ce29c528 Andreas Kohlbecker
 */
153 c3a41ddb Andreas Kohlbecker
function cdm_toc_list_add_item($label, $class_attribute_suffix, $fragment = NULL, $as_first_element = FALSE, $weight = null) {
154
155 ce29c528 Andreas Kohlbecker
  $toc_list_items = &cdm_toc_list();
156 dd1c109a Andreas Kohlbecker
157 ce29c528 Andreas Kohlbecker
  if (!$fragment) {
158
    $fragment = $label;
159
  }
160
  $fragment = generalizeString($fragment);
161 dd1c109a Andreas Kohlbecker
162 ce29c528 Andreas Kohlbecker
  $class_attributes = 'feature-toc-item-' . $class_attribute_suffix;
163 dd1c109a Andreas Kohlbecker
164 ce29c528 Andreas Kohlbecker
  $new_item = toc_list_item(
165
    theme(
166
      'cdm_feature_name',
167
      array('feature_name' => $label)),
168 dd1c109a Andreas Kohlbecker
      array('class' => $class_attributes),
169
      $fragment
170
    );
171
172 c3a41ddb Andreas Kohlbecker
  if ($weight === null && $as_first_element) {
173
    reset($toc_list_items);
174
    $weight = key($toc_list_items); // returns null for empty arrays
175
    $weight = $weight !== null ? $weight - FEATURE_BLOCK_WEIGHT_INCREMENT : 0;
176 ce29c528 Andreas Kohlbecker
  }
177 c3a41ddb Andreas Kohlbecker
  else if($weight === null) {
178
      end($toc_list_items);
179
      $weight = key($toc_list_items); // returns null for empty arrays
180
      $weight = $weight !== null ? $weight + FEATURE_BLOCK_WEIGHT_INCREMENT : 0;
181 bf97c2e8 Andreas Kohlbecker
  }
182 c3a41ddb Andreas Kohlbecker
  $toc_list_items[$weight] = $new_item;
183
  ksort($toc_list_items); // sort so that the last element has always the highest weight
184 bf97c2e8 Andreas Kohlbecker
185 ce29c528 Andreas Kohlbecker
}
186 bf97c2e8 Andreas Kohlbecker
187 ce29c528 Andreas Kohlbecker
/**
188
 * Returns the statically cached table of content items as render array.
189
 *
190
 * @see cdm_toc_list_add_item()
191
 *
192
 * @return array
193
 *   a render array of table of content items suitable for theme_item_list()
194
 */
195
function &cdm_toc_list(){
196 a6d7907d Andreas Kohlbecker
197 ce29c528 Andreas Kohlbecker
  $toc_list_items = &drupal_static('toc_list_items', array());
198
199
  return $toc_list_items;
200
}
201 1f11e206 Andreas Kohlbecker
202 f19f47fa Andreas Kohlbecker
/**
203 a6d7907d Andreas Kohlbecker
 * Prepares an empty Drupal block for displaying description elements of a
204
 * specific CDM Feature.
205 f19f47fa Andreas Kohlbecker
 *
206 a6d7907d Andreas Kohlbecker
 * The block can also be used for pseudo Features like a bibliography. Pseudo
207
 * features are derived from other data on the fly and so not exist as such in
208
 * the cdm data. For pseudo features the $feature can be created using
209
 * make_pseudo_feature().
210 f19f47fa Andreas Kohlbecker
 *
211
 * @param $feature_name
212 a6d7907d Andreas Kohlbecker
 *   A label describing the feature, usually the localized feature
213
 *   representation.
214 ce29c528 Andreas Kohlbecker
 * @param object $feature
215 fec3fd41 Andreas Kohlbecker
 *   The CDM Feature for which the block is created.
216 a6d7907d Andreas Kohlbecker
 *
217 f19f47fa Andreas Kohlbecker
 * @return object
218 ce29c528 Andreas Kohlbecker
 *   A Drupal block object
219 a6d7907d Andreas Kohlbecker
 *
220
 * @throws \Exception re-throws exception from theme()
221 f19f47fa Andreas Kohlbecker
 */
222 fec3fd41 Andreas Kohlbecker
function feature_block($feature_name, $feature) {
223
  $block = new stdclass();
224 f19f47fa Andreas Kohlbecker
  $block->module = 'cdm_dataportal';
225 ce29c528 Andreas Kohlbecker
  $block->region = NULL;
226 fec3fd41 Andreas Kohlbecker
  $class_attribute = html_class_attribute_ref($feature);
227 d43a3486 Andreas Kohlbecker
  $block_delta = $feature->class !== 'PseudoFeature' ? $feature_name : $feature->label;
228 fec3fd41 Andreas Kohlbecker
  $block->delta = generalizeString($block_delta);
229 28ec5a3e Andreas Kohlbecker
  $block->subject = '<a name="' . $block->delta . '"></a><span class="' . $class_attribute . '">'
230 f19f47fa Andreas Kohlbecker
    . theme('cdm_feature_name', array('feature_name' => $feature_name))
231
    . '</span>';
232
  $block->module = "cdm_dataportal-feature";
233
  $block->content = '';
234
  return $block;
235
}
236
237 dd1c109a Andreas Kohlbecker
238 ce29c528 Andreas Kohlbecker
/**
239
 * Returns a list of a specific type of IdentificationKeys.
240
 *
241
 * The list can be restricted by a taxon.
242
 *
243
 * @param string $type
244
 *   The simple name of the cdm class implementing the interface
245
 *   IdentificationKey, valid values are:
246
 *   PolytomousKey, MediaKey, MultiAccessKey.
247
 * @param string $taxonUuid
248
 *   If given this parameter restrict the listed keys to those which have
249
 *   the taxon identified be this uuid in scope.
250
 *
251
 * @return array
252
 *   List with identification keys.
253
 */
254
function _list_IdentificationKeys($type, $taxonUuid = NULL, $pageSize = NULL, $pageNumber = NULL) {
255
  if (!$type) {
256
    drupal_set_message(t('Type parameter is missing'), 'error');
257
    return;
258
  }
259 a6d7907d Andreas Kohlbecker
  $cdm_ws_base_path = NULL;
260 ce29c528 Andreas Kohlbecker
  switch ($type) {
261
    case "PolytomousKey":
262 a6d7907d Andreas Kohlbecker
      $cdm_ws_base_path = CDM_WS_POLYTOMOUSKEY;
263 ce29c528 Andreas Kohlbecker
      break;
264
265
    case "MediaKey":
266 a6d7907d Andreas Kohlbecker
      $cdm_ws_base_path = CDM_WS_MEDIAKEY;
267 ce29c528 Andreas Kohlbecker
      break;
268 dd1c109a Andreas Kohlbecker
269 ce29c528 Andreas Kohlbecker
    case "MultiAccessKey":
270 a6d7907d Andreas Kohlbecker
      $cdm_ws_base_path = CDM_WS_MULTIACCESSKEY;
271 ce29c528 Andreas Kohlbecker
      break;
272
273
  }
274
275 a6d7907d Andreas Kohlbecker
  if (!$cdm_ws_base_path) {
276 ce29c528 Andreas Kohlbecker
    drupal_set_message(t('Type parameter is not valid: ') . $type, 'error');
277
  }
278
279
  $queryParameters = '';
280
  if (is_numeric($pageSize)) {
281
    $queryParameters = "pageSize=" . $pageSize;
282
  }
283
  else {
284
    $queryParameters = "pageSize=0";
285
  }
286
287
  if (is_numeric($pageNumber)) {
288
    $queryParameters = "pageNumber=" . $pageNumber;
289
  }
290
  else {
291
    $queryParameters = "pageNumber=0";
292
  }
293
  $queryParameters = NULL;
294
  if ($taxonUuid) {
295
    $queryParameters = "findByTaxonomicScope=$taxonUuid";
296
  }
297 a6d7907d Andreas Kohlbecker
  $pager = cdm_ws_get($cdm_ws_base_path, NULL, $queryParameters);
298 ce29c528 Andreas Kohlbecker
299
  if (!$pager || $pager->count == 0) {
300
    return array();
301
  }
302
  return $pager->records;
303
}
304
305
306
/**
307
 * Creates a list item for a table of content, suitable as data element for a themed list
308
 *
309
 * @see theme_list()
310
 *
311
 * @param $label
312
 * @param $http_request_params
313
 * @param $attributes
314
 * @return array
315
 */
316
function toc_list_item($label, $attributes = array(), $fragment = null) {
317
318
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
319
  $http_request_params = drupal_static('http_request_params', drupal_get_query_parameters());
320
321
  $item =  array(
322
    'data' => l(
323
      $label,
324
      $_GET['q'],
325
      array(
326
        'attributes' => array('class' => array('toc')),
327
        'fragment' => generalizeString($label),
328
        'query' => $http_request_params,
329 f19f47fa Andreas Kohlbecker
      )
330 ce29c528 Andreas Kohlbecker
    ),
331
  );
332
  $item['attributes'] = $attributes;
333
  return $item;
334
}
335
336 f23ae4e3 Andreas Kohlbecker
/**
337 a6d7907d Andreas Kohlbecker
 * Compare callback to be used in usort() to sort render arrays produced by
338
 * compose_description_element().
339 f23ae4e3 Andreas Kohlbecker
 *
340
 * @param $a
341
 * @param $b
342 a6d7907d Andreas Kohlbecker
 *
343
 * @return int Returns < 0 if $a['#value'].$a['#value-suffix'] from is less than
344
 * $b['#value'].$b['#value-suffix'], > 0 if it is greater than $b['#value'].$b['#value-suffix'],
345
 * and 0 if they are equal.
346 f23ae4e3 Andreas Kohlbecker
 */
347
function compare_description_element_render_arrays($a, $b){
348
  if ($a['#value'].$a['#value-suffix'] == $b['#value'].$b['#value-suffix']) {
349
    return 0;
350
  }
351
  return ($a['#value'].$a['#value-suffix'] < $b['#value'].$b['#value-suffix']) ? -1 : 1;
352
353
}
354
355 dfc49afb Andreas Kohlbecker
356
/**
357
 * @param $element
358
 * @param $feature_block_settings
359
 * @param $element_markup
360
 * @param $footnote_list_key_suggestion
361 a6d7907d Andreas Kohlbecker
 * @param bool $prepend_feature_label
362
 *
363
 * @return array|null
364 dfc49afb Andreas Kohlbecker
 */
365 c651d3b1 Andreas Kohlbecker
function compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label = FALSE)
366 dfc49afb Andreas Kohlbecker
{
367 0ef9a709 Andreas Kohlbecker
368
  $render_array = array(
369
    '#type' => 'html_tag',
370
    '#tag' => cdm_feature_block_element_tag_name($feature_block_settings),
371
372
    '#attributes' => array(
373 f23ae4e3 Andreas Kohlbecker
      'class' => array(
374
        'DescriptionElement',
375
        'DescriptionElement-' . $element->class,
376
        html_class_attribute_ref($element)
377
      )
378 0ef9a709 Andreas Kohlbecker
    ),
379
380
    '#value' => '',
381
    '#value_suffix' => NULL
382
383
  );
384
385 0c2b9b9d Andreas Kohlbecker
  $annotations_and_sources = handle_annotations_and_sources(
386
    $element,
387
    handle_annotations_and_sources_config($feature_block_settings),
388
    $element_markup,
389
    $footnote_list_key_suggestion
390
  );
391 dfc49afb Andreas Kohlbecker
392 90425089 Andreas Kohlbecker
  $timescope_markup = '';
393
  if(isset($element->timeperiod)){
394 ef6a4ce9 Andreas Kohlbecker
    $timescope_markup = ' ' . timePeriodToString($element->timeperiod, true);
395 90425089 Andreas Kohlbecker
  }
396
397 0ef9a709 Andreas Kohlbecker
  // handle the special case were the TextData is used as container for original source with name
398
  // used in source information without any text stored in it.
399 dfc49afb Andreas Kohlbecker
  $names_used_in_source_markup = '';
400
  if (!empty($annotations_and_sources['names_used_in_source']) && empty($element_markup)) {
401
    $names_used_in_source_markup = join(', ', $annotations_and_sources['names_used_in_source']) . ': ';
402 72c12d45 Andreas Kohlbecker
    // remove all <span class="nameUsedInSource">...</span> from all source_references
403
    // these are expected to be at the end of the strings
404
    $pattern = '/ <span class="nameUsedInSource">.*$/';
405
    foreach( $annotations_and_sources['source_references'] as &$source_reference){
406
      $source_reference = preg_replace($pattern , '', $source_reference);
407
    }
408 dfc49afb Andreas Kohlbecker
  }
409
410
  $source_references_markup = '';
411
  if (!empty($annotations_and_sources['source_references'])) {
412
    $source_references_markup = '<span class="sources">' . join(' ', $annotations_and_sources['source_references']) . '<span>';
413
  }
414
415 c651d3b1 Andreas Kohlbecker
  $feature_label = '';
416
  if ($prepend_feature_label) {
417
    $feature_label = '<span class="nested-feature-tree-feature-label">' . $element->feature->representation_L10n . ':</span> ';
418
  }
419 90425089 Andreas Kohlbecker
  $content_markup = $names_used_in_source_markup . $element_markup . $timescope_markup . $source_references_markup;
420 0ef9a709 Andreas Kohlbecker
421 caab0bd2 Andreas Kohlbecker
  if(!$content_markup && $feature_block_settings['sources_as_content'] !== 1){
422
    // no textual content? So skip this element completely, even if there could be an footnote key
423 7a751db4 Andreas Kohlbecker
    // see #4379
424
    return null;
425
  }
426
427
    $render_array['#value'] = $feature_label . $content_markup;
428
    $render_array['#value_suffix'] = $annotations_and_sources['foot_note_keys'];
429 0ef9a709 Andreas Kohlbecker
  return $render_array;
430 dfc49afb Andreas Kohlbecker
}
431
432 0c2b9b9d Andreas Kohlbecker
/**
433
 * Creates a handle_annotations_and_sources configuration array from feature_block_settings.
434
 *
435
 * The handle_annotations_and_sources configuration array is meant to be used for the
436
 * method handle_annotations_and_sources().
437
 *
438
 * @param $feature_block_settings array
439
 *
440
 * @return array
441
 *   The configuration array for handle_annotations_and_sources()
442
 */
443
function handle_annotations_and_sources_config($feature_block_settings){
444
445
  $config = $feature_block_settings;
446
  unset($config['sources_as_content_to_bibliography']);
447 f79d32d6 Andreas Kohlbecker
  $config['add_footnote_keys'] = 0;
448 0c2b9b9d Andreas Kohlbecker
  if($feature_block_settings['sources_as_content'] !== 1 || $feature_block_settings['sources_as_content_to_bibliography'] == 1) {
449
    $config['add_footnote_keys'] = 1;
450
  }
451
  $config['bibliography_aware'] = 1;
452
453
  return $config;
454
}
455 dfc49afb Andreas Kohlbecker
456 a6d7907d Andreas Kohlbecker
/**
457
 * Provides the according tag name for the description element markup which
458
 * fits the  $feature_block_settings['as_list'] value
459
 *
460
 * @param $feature_block_settings
461
 *   A feature_block_settings array, for details, please see
462
 *   get_feature_block_settings($feature_uuid = 'DEFAULT')
463
 *
464
 * @return mixed|string
465
 */
466 08a339ec Andreas Kohlbecker
  function cdm_feature_block_element_tag_name($feature_block_settings){
467
    switch ($feature_block_settings['as_list']){
468
      case 'ul':
469
      case 'ol':
470
        return 'li';
471
      case 'div':
472 f2e44165 Andreas Kohlbecker
        if(isset($feature_block_settings['element_tag'])){
473
          return $feature_block_settings['element_tag'];
474
        }
475 08a339ec Andreas Kohlbecker
        return 'span';
476
      case 'dl':
477
        return 'dd';
478
      default:
479
        return 'div'; // should never happen, throw error instead?
480
    }
481
  }
482 ce29c528 Andreas Kohlbecker
483
484
/* ==================== COMPOSE FUNCTIONS =============== */
485
486
  /**
487
   * Returns a set of feature blocks for a taxon profile from the $mergedFeatureNodes of a given $taxon.
488
   *
489
   * The taxon profile consists of drupal block elements, one for the description elements
490
   * of a specific feature. The structure is defined by specific FeatureTree.
491
   * The chosen FeatureTree is merged with the list of description elements prior to using this method.
492
   *
493
   * The merged nodes can be obtained by making use of the
494
   * function cdm_ws_descriptions_by_featuretree().
495
   *
496
   * @see cdm_ws_descriptions_by_featuretree()
497
   *
498
   * @param $mergedFeatureNodes
499
   *
500
   * @param $taxon
501
   *
502
   * @return array
503
   *  A Drupal render array containing feature blocks and the table of content
504
   *
505
   * @ingroup compose
506
   */
507 1b756c5f Andreas Kohlbecker
  function make_feature_block_list($mergedFeatureNodes, $taxon) {
508 ce29c528 Andreas Kohlbecker
509
    $block_list = array();
510
511
    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME);
512
513 4feafea8 Andreas Kohlbecker
    $use_description_features = array(UUID_USE);
514
515 ddc8c754 Andreas Kohlbecker
516
    RenderHints::pushToRenderStack('feature_block');
517 ce29c528 Andreas Kohlbecker
    // Create a drupal block for each feature
518 c3a41ddb Andreas Kohlbecker
    $block_weight = - FEATURE_BLOCK_WEIGHT_INCREMENT;
519 b011743c Andreas Kohlbecker
    foreach ($mergedFeatureNodes as $feature_node) {
520 ce29c528 Andreas Kohlbecker
521 c3a41ddb Andreas Kohlbecker
      $block_weight = $block_weight + FEATURE_BLOCK_WEIGHT_INCREMENT;
522
523 b011743c Andreas Kohlbecker
      if ((isset($feature_node->descriptionElements['#type']) ||
524
          has_feature_node_description_elements($feature_node)) && $feature_node->term->uuid != UUID_IMAGE) { // skip empty or suppressed features
525 ce29c528 Andreas Kohlbecker
526 b011743c Andreas Kohlbecker
        RenderHints::pushToRenderStack($feature_node->term->uuid);
527 ddc8c754 Andreas Kohlbecker
          
528 b011743c Andreas Kohlbecker
        $feature_name = cdm_term_representation($feature_node->term, 'Unnamed Feature');
529
        $feature_block_settings = get_feature_block_settings($feature_node->term->uuid);
530 ddc8c754 Andreas Kohlbecker
        
531 ce29c528 Andreas Kohlbecker
532 b011743c Andreas Kohlbecker
        $block = feature_block($feature_name, $feature_node->term);
533 ce29c528 Andreas Kohlbecker
        $block->content = array();
534
        $block_content_is_empty = TRUE;
535
536 b011743c Andreas Kohlbecker
        if(array_search($feature_node->term->uuid, $use_description_features) !== false) {
537 4feafea8 Andreas Kohlbecker
          // do not show features which belong to the UseDescriptions, these are
538 1b756c5f Andreas Kohlbecker
          // handled by compose_feature_block_items_use_records() where the according descriptions are
539 4feafea8 Andreas Kohlbecker
          // fetched again separately.
540 1b756c5f Andreas Kohlbecker
          // UseDescriptions are a special feature introduced for palmweb
541 4feafea8 Andreas Kohlbecker
          continue;
542
        }
543
544 ce29c528 Andreas Kohlbecker
        /*
545
         * Content/DISTRIBUTION.
546
         */
547 b011743c Andreas Kohlbecker
        if ($feature_node->term->uuid == UUID_DISTRIBUTION) {
548
          $block = compose_feature_block_distribution($taxon, $feature_node->descriptionElements, $feature_node->term);
549 ce29c528 Andreas Kohlbecker
          $block_content_is_empty = FALSE;
550
        }
551 4feafea8 Andreas Kohlbecker
552 ce29c528 Andreas Kohlbecker
        /*
553
         * Content/COMMON_NAME.
554
         */
555 b011743c Andreas Kohlbecker
        else if ($feature_node->term->uuid == UUID_COMMON_NAME) {
556
          $common_names_render_array = compose_feature_block_items_feature_common_name($feature_node->descriptionElements, $feature_node->term);
557 ce29c528 Andreas Kohlbecker
          $block->content[] = $common_names_render_array;
558
          $block_content_is_empty = FALSE;
559
        }
560
561 4feafea8 Andreas Kohlbecker
        /*
562 1b756c5f Andreas Kohlbecker
         * Content/Use Description (Use + UseRecord)
563 4feafea8 Andreas Kohlbecker
         */
564 b011743c Andreas Kohlbecker
        else if ($feature_node->term->uuid == UUID_USE_RECORD) {
565
          $block->content[] = cdm_block_use_description_content($taxon->uuid, $feature_node->term);
566 ce29c528 Andreas Kohlbecker
          $block_content_is_empty = FALSE;
567
        }
568
569
        /*
570
         * Content/ALL OTHER FEATURES.
571
         */
572
        else {
573
574
          $media_list = array();
575 c651d3b1 Andreas Kohlbecker
          $elements_render_array = array();
576
          $child_elements_render_array = null;
577 ce29c528 Andreas Kohlbecker
578 b011743c Andreas Kohlbecker
          if (isset($feature_node->descriptionElements[0])) {
579
            $elements_render_array = compose_feature_block_items_generic($feature_node->descriptionElements, $feature_node->term);
580 ce29c528 Andreas Kohlbecker
          }
581
582
          // Content/ALL OTHER FEATURES/Subordinate Features
583
          // subordinate features are printed inline in one floating text,
584
          // it is expected hat subordinate features can "contain" TextData,
585 c651d3b1 Andreas Kohlbecker
          // Qualitative- and Qualitative- DescriptionElements
586 b011743c Andreas Kohlbecker
          if (isset($feature_node->childNodes[0])) {
587
            $child_elements_render_array = compose_feature_block_items_nested($feature_node, $media_list, $feature_block_settings);
588 c651d3b1 Andreas Kohlbecker
            $elements_render_array = array_merge($elements_render_array, $child_elements_render_array);
589
          }
590
          $block_content_is_empty = $block_content_is_empty && empty($media_list) && empty($elements_render_array);
591
          if(!$block_content_is_empty){
592 b011743c Andreas Kohlbecker
            $block->content[] = compose_feature_block_wrap_elements($elements_render_array, $feature_node->term, $feature_block_settings['glue']);
593
            $block->content[] = compose_feature_media_gallery($feature_node, $media_list, $gallery_settings);
594 c651d3b1 Andreas Kohlbecker
            /*
595
             * Footnotes for the feature block
596
             */
597 d8069342 Andreas Kohlbecker
            $block->content[] = markup_to_render_array(render_footnotes(PSEUDO_FEATURE_BIBLIOGRAPHY . '-' . $feature_node->term->uuid));
598
            $block->content[] = markup_to_render_array(render_footnotes($feature_node->term->uuid));
599 ce29c528 Andreas Kohlbecker
          }
600
        } // END all other features
601
602 bdd382be Andreas Kohlbecker
        // allows modifying the block contents via a the hook_cdm_feature_node_block_content_alter
603 b011743c Andreas Kohlbecker
        drupal_alter('cdm_feature_node_block_content', $block->content, $feature_node->term, $feature_node->descriptionElements);
604 bdd382be Andreas Kohlbecker
605 ce29c528 Andreas Kohlbecker
        if(!$block_content_is_empty){ // skip empty block content
606 c3a41ddb Andreas Kohlbecker
          $block_list[$block_weight] = $block;
607
          cdm_toc_list_add_item(cdm_term_representation($feature_node->term), $feature_node->term->uuid, null, FALSE, $block_weight);
608 ce29c528 Andreas Kohlbecker
        } // END: skip empty block content
609 c651d3b1 Andreas Kohlbecker
      } // END: skip empty or suppressed features
610 ddc8c754 Andreas Kohlbecker
      RenderHints::popFromRenderStack();
611 ce29c528 Andreas Kohlbecker
    } // END: creating a block per feature
612
613 ddc8c754 Andreas Kohlbecker
    RenderHints::popFromRenderStack();
614
615 ce29c528 Andreas Kohlbecker
    drupal_alter('cdm_feature_node_blocks', $block_list, $taxon);
616 6421984d Andreas Kohlbecker
617 c3a41ddb Andreas Kohlbecker
    ksort($block_list);
618
619 1b756c5f Andreas Kohlbecker
    return  $block_list;
620 ce29c528 Andreas Kohlbecker
  }
621
622 c651d3b1 Andreas Kohlbecker
/**
623
 * Creates a render array of description elements  held by child nodes of the given feature node.
624
 *
625
 * This function is called recursively!
626
 *
627 b011743c Andreas Kohlbecker
 * @param $feature_node
628 c651d3b1 Andreas Kohlbecker
 *   The feature node.
629
 * @param array $media_list
630
 *   List of CDM Media entities. All media of subordinate elements should be passed to the main feature.ä
631
 * @param $feature_block_settings
632
 *   The feature block settings.
633
 * @param $main_feature
634
 *  Only used internally in recursive calls.
635
 *
636
 * @return array
637
 *  A Drupal render array
638
 *
639
 * @ingroup compose
640
 */
641 b011743c Andreas Kohlbecker
function compose_feature_block_items_nested($feature_node, &$media_list, $feature_block_settings, $main_feature = NULL)
642 c651d3b1 Andreas Kohlbecker
{
643
644
  if(!$main_feature){
645 b011743c Andreas Kohlbecker
    $main_feature = $feature_node->term;
646 c651d3b1 Andreas Kohlbecker
  }
647
  /*
648
   * TODO should be configurable, options; YES, NO, AUTOMATIC
649
   * (automatic will only place the label if the first word of the description element text is not the same)
650
   */
651
  $prepend_feature_label = false;
652
653
  $render_arrays = array();
654 b011743c Andreas Kohlbecker
  foreach ($feature_node->childNodes as $child_node) {
655 c651d3b1 Andreas Kohlbecker
    if (isset($child_node->descriptionElements[0])) {
656
      foreach ($child_node->descriptionElements as $element) {
657
658
        if (isset($element->media[0])) {
659
          // Append media of subordinate elements to list of main
660
          // feature.
661
          $media_list = array_merge($media_list, $element->media);
662
        }
663
664
        $child_node_element = null;
665
        switch ($element->class) {
666
          case 'TextData':
667
            $child_node_element = compose_description_element_text_data($element, $element->feature->uuid, $feature_block_settings, $prepend_feature_label);
668
            break;
669
          case 'CategoricalData':
670
            $child_node_element = compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label);
671
            break;
672
          case 'QuantitativeData':
673
            $child_node_element = compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label);
674
675
        }
676
        if (is_array($child_node_element)) {
677
          $render_arrays[] = $child_node_element;
678
        }
679
      }
680
    }
681
682
    if(isset($child_node->childNodes[0])){
683
      $render_arrays = array_merge($render_arrays, compose_feature_block_items_nested($child_node, $media_list, $feature_block_settings, $main_feature ));
684
    }
685
  }
686
687
  return $render_arrays;
688
}
689
690 ce29c528 Andreas Kohlbecker
  /**
691 c651d3b1 Andreas Kohlbecker
   *
692 b011743c Andreas Kohlbecker
   * @param $feature_node
693 c651d3b1 Andreas Kohlbecker
   *  The merged feature three node which potentially contains media in its description elements.
694 ce29c528 Andreas Kohlbecker
   * @param $media_list
695 c651d3b1 Andreas Kohlbecker
   *    Additional media to be merged witht the media contained in the nodes description elements
696 ce29c528 Andreas Kohlbecker
   * @param $gallery_settings
697
   * @return array
698 c651d3b1 Andreas Kohlbecker
   *
699
   * @ingroup compose
700 ce29c528 Andreas Kohlbecker
   */
701 b011743c Andreas Kohlbecker
  function compose_feature_media_gallery($feature_node, $media_list, $gallery_settings) {
702 ce29c528 Andreas Kohlbecker
703 b011743c Andreas Kohlbecker
    if (isset($feature_node->descriptionElements)) {
704
      $media_list = array_merge($media_list, cdm_dataportal_media_from_descriptionElements($feature_node->descriptionElements));
705 ce29c528 Andreas Kohlbecker
    }
706
707
    $captionElements = array('title', 'rights');
708
709
    if (isset($media_list[0]) && isset($gallery_settings['cdm_dataportal_media_maxextend']) && isset($gallery_settings['cdm_dataportal_media_cols'])) {
710 a9815578 Andreas Kohlbecker
      $gallery = compose_cdm_media_gallerie(array(
711 ce29c528 Andreas Kohlbecker
        'mediaList' => $media_list,
712 b011743c Andreas Kohlbecker
        'galleryName' => CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME . '_' . $feature_node->term->uuid,
713 ce29c528 Andreas Kohlbecker
        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
714
        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
715
        'captionElements' => $captionElements,
716
      ));
717
      return markup_to_render_array($gallery);
718
    }
719
720
    return markup_to_render_array('');
721
  }
722
723
  /**
724 4a236db6 Andreas Kohlbecker
   * Composes the distribution feature block for a taxon
725
   *
726 ce29c528 Andreas Kohlbecker
   * @param $taxon
727
   * @param $descriptionElements
728
   *   an associative array with two elements:
729
   *   - '#type': must be 'DTO'
730
   *   - 'DistributionInfoDTO': a CDM DistributionInfoDTO object as returned by the DistributionInfo web service
731
   * @param $feature
732
   *
733 18727898 Andreas Kohlbecker
   * @return array
734
   *  A drupal render array
735
   *
736 ce29c528 Andreas Kohlbecker
   * @ingroup compose
737
   */
738
  function compose_feature_block_distribution($taxon, $descriptionElements, $feature) {
739
    $text_data_glue = '';
740
    $text_data_sortOutArray = FALSE;
741
    $text_data_enclosingTag = 'ul';
742
    $text_data_out_array = array();
743
744
    $distributionElements = NULL;
745
    $distribution_info_dto = NULL;
746
    $distribution_sortOutArray = FALSE;
747
748
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
749
750 284fb36d Andreas Kohlbecker
    // TODO use feature_block_settings instead of DISTRIBUTION_ORDER_MODE
751 97ac3d38 Andreas Kohlbecker
    if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) != 'TREE') {
752 ce29c528 Andreas Kohlbecker
      $distribution_glue = '';
753
      $distribution_enclosingTag = 'dl';
754
    } else {
755
      $distribution_glue = '';
756
      $distribution_enclosingTag = 'ul';
757
    }
758
759
    if (!isset($descriptionElements['#type']) || !$descriptionElements['#type'] == 'DTO') {
760
      // skip the DISTRIBUTION section if there is no DTO type element
761
      return array(); // FIXME is it ok to return an empty array?
762
    }
763
764
    $block = feature_block(
765
      cdm_term_representation($feature, 'Unnamed Feature'),
766
      $feature
767
    );
768 f3baa35e Andreas Kohlbecker
    $block->content = array();
769 ce29c528 Andreas Kohlbecker
770
    // $$descriptionElements['TextData'] is added to to the feature node in merged_taxon_feature_tree()
771
    if (isset($descriptionElements['TextData'])) {
772
      // --- TextData
773
      foreach ($descriptionElements['TextData'] as $text_data_element) {
774 0ef9a709 Andreas Kohlbecker
        $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
775 ce29c528 Andreas Kohlbecker
        $repr = drupal_render($text_data_render_array);
776
777
        if (!array_search($repr, $text_data_out_array)) { // de-duplication !!
778
          $text_data_out_array[] = $repr;
779 c651d3b1 Andreas Kohlbecker
          // TODO HINT: sorting in compose_feature_block_wrap_elements will
780 ce29c528 Andreas Kohlbecker
          // not work since this array contains html attributes with uuids
781
          // and what is about cases like the bibliography where
782
          // any content can be prefixed with some foot-note anchors?
783
          $text_data_sortOutArray = TRUE;
784
          $text_data_glue = '<br/> ';
785
          $text_data_enclosingTag = 'p';
786
        }
787
      }
788
    }
789
790
791
    if ($text_data_out_array && variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
792 c651d3b1 Andreas Kohlbecker
      $block->content[] = compose_feature_block_wrap_elements(
793 dfc49afb Andreas Kohlbecker
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
794 ce29c528 Andreas Kohlbecker
      );
795
    }
796
797
    // --- Distribution map
798
    $distribution_map_query_parameters = NULL;
799 378bc1ce Andreas Kohlbecker
800 e0613197 Andreas Kohlbecker
    $map_visibility = variable_get(DISTRIBUTION_MAP_VISIBILITY, DISTRIBUTION_MAP_VISIBILITY_DEFAULT);
801
    if(variable_get(DISTRIBUTION_MAP_VISIBILITY, DISTRIBUTION_MAP_VISIBILITY_DEFAULT) == 'always' ||
802
        $map_visibility == 'automatic' && isset($descriptionElements['DistributionInfoDTO']->mapUriParams)
803 378bc1ce Andreas Kohlbecker
      )
804
    {
805 ce29c528 Andreas Kohlbecker
      $distribution_map_query_parameters = $descriptionElements['DistributionInfoDTO']->mapUriParams;
806
    }
807 653e9c6b Andreas Kohlbecker
    $map_render_element = compose_distribution_map($distribution_map_query_parameters);
808 ce29c528 Andreas Kohlbecker
    $block->content[] = $map_render_element;
809
810
    $dto_out_array = array();
811 bda17f32 Andreas Kohlbecker
812
    // --- Condensed Distribution
813
    if(variable_get(DISTRIBUTION_CONDENSED) && isset($descriptionElements['DistributionInfoDTO']->condensedDistribution)){
814
      $condensed_distribution_markup = '<p class="condensed_distribution">';
815
816
      $isFirst = true;
817
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous[0])){
818
        foreach($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous as $cdItem){
819
          if(!$isFirst){
820
            $condensed_distribution_markup .= ' ';
821
          }
822
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->idInVocabulary . '">'
823
          . $cdItem->areaStatusLabel . '</span>';
824
          $isFirst = false;
825
        }
826
      }
827
828
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign[0])) {
829
        if(!$isFirst){
830
          $condensed_distribution_markup .= ' ';
831
        }
832
        $isFirst = TRUE;
833
        $condensed_distribution_markup .= '[';
834
        foreach ($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign as $cdItem) {
835
          if (!$isFirst) {
836
            $condensed_distribution_markup .= ' ';
837
          }
838
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->titleCache . '">'
839
            . $cdItem->areaStatusLabel . '</span>';
840
          $isFirst = false;
841
        }
842
        $condensed_distribution_markup .= ']';
843
      }
844
845 423486b0 Andreas Kohlbecker
      $condensed_distribution_markup .= '&nbsp;' . l(
846
          font_awesome_icon_markup(
847
            'fa-info-circle',
848
            array(
849
              'alt'=>'help',
850
              'class' => array('superscript')
851
            )
852
          ),
853 6858b474 Andreas Kohlbecker
          variable_get(DISTRIBUTION_CONDENSED_INFO_PATH, DISTRIBUTION_CONDENSED_INFO_PATH_DEFAULT) ,
854 423486b0 Andreas Kohlbecker
          array('html' => TRUE));
855 bda17f32 Andreas Kohlbecker
      $condensed_distribution_markup .= '</p>';
856
      $dto_out_array[] = $condensed_distribution_markup;
857
    }
858
859
    // --- tree or list
860 ce29c528 Andreas Kohlbecker
    if (isset($descriptionElements['DistributionInfoDTO'])) {
861
      $distribution_info_dto = $descriptionElements['DistributionInfoDTO'];
862
863
      // --- tree
864
      if (is_object($distribution_info_dto->tree)) {
865 97ac3d38 Andreas Kohlbecker
        $distribution_tree_render_array = compose_distribution_hierarchy($distribution_info_dto->tree, $feature_block_settings);
866 f23ae4e3 Andreas Kohlbecker
        $dto_out_array[] = $distribution_tree_render_array;
867 ce29c528 Andreas Kohlbecker
      }
868
869
      // --- sorted element list
870
      if (is_array($distribution_info_dto->elements) && count($distribution_info_dto->elements) > 0) {
871
        foreach ($distribution_info_dto->elements as $descriptionElement) {
872
          if (is_object($descriptionElement->area)) {
873
            $sortKey = $descriptionElement->area->representation_L10n;
874
            $distributionElements[$sortKey] = $descriptionElement;
875
          }
876
        }
877
        ksort($distributionElements);
878 63740051 Andreas Kohlbecker
        $distribution_element_render_array = compose_description_elements_distribution($distributionElements);
879 f23ae4e3 Andreas Kohlbecker
        $dto_out_array[] = $distribution_element_render_array;
880 ce29c528 Andreas Kohlbecker
881
      }
882
      //
883 c651d3b1 Andreas Kohlbecker
      $block->content[] = compose_feature_block_wrap_elements(
884 dfc49afb Andreas Kohlbecker
        $dto_out_array, $feature, $distribution_glue, $distribution_sortOutArray
885 ce29c528 Andreas Kohlbecker
      );
886
    }
887
888
    // --- TextData at the bottom
889
    if ($text_data_out_array && !variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
890 c651d3b1 Andreas Kohlbecker
      $block->content[] = compose_feature_block_wrap_elements(
891 dfc49afb Andreas Kohlbecker
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
892 ce29c528 Andreas Kohlbecker
      );
893
    }
894
895 d8069342 Andreas Kohlbecker
    $block->content[] = markup_to_render_array(render_footnotes('BIBLIOGRAPHY-' . UUID_DISTRIBUTION));
896
    $block->content[] = markup_to_render_array(render_footnotes(UUID_DISTRIBUTION));
897 ce29c528 Andreas Kohlbecker
898
    return $block;
899
  }
900
901
902
  /**
903 0ebb6efc Andreas Kohlbecker
   * Composes a drupal render array for single CDM TextData description element.
904 ce29c528 Andreas Kohlbecker
   *
905
   * @param $element
906 0ef9a709 Andreas Kohlbecker
   *    The CDM TextData description element.
907 ce29c528 Andreas Kohlbecker
   *  @param $feature_uuid
908 c651d3b1 Andreas Kohlbecker
   * @param bool $prepend_feature_label
909
   *   Used in nested feature trees to put the feature as label in front of the description element text representation.
910 ce29c528 Andreas Kohlbecker
   *
911
   * @return array
912
   *   A drupal render array with the following elements being used:
913
   *    - #tag: either 'div', 'li', ...
914
   *    ⁻ #attributes: class attributes
915
   *    - #value_prefix: (optionally) contains footnote anchors
916
   *    - #value: contains the textual content
917
   *    - #value_suffix: (optionally) contains footnote keys
918
   *
919
   * @ingroup compose
920
   */
921 c651d3b1 Andreas Kohlbecker
  function compose_description_element_text_data($element, $feature_uuid, $feature_block_settings, $prepend_feature_label = FALSE) {
922 ce29c528 Andreas Kohlbecker
923
    $footnote_list_key_suggestion = $feature_uuid;
924
925 0ef9a709 Andreas Kohlbecker
    $element_markup = '';
926 ce29c528 Andreas Kohlbecker
    if (isset($element->multilanguageText_L10n->text)) {
927
      // TODO replacement of \n by <br> should be configurable
928 0ef9a709 Andreas Kohlbecker
      $element_markup = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
929 ce29c528 Andreas Kohlbecker
    }
930
931 c651d3b1 Andreas Kohlbecker
    $render_array = compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label);
932 ce29c528 Andreas Kohlbecker
933 dfc49afb Andreas Kohlbecker
    return $render_array;
934
  }
935 ce29c528 Andreas Kohlbecker
936
937 e413f218 Andreas Kohlbecker
/**
938
 * Theme function to render CDM DescriptionElements of the type TaxonInteraction.
939
 *
940
 * @param $element
941
 *  The CDM TaxonInteraction entity
942
 *
943
 * @return
944
 *  A drupal render array
945
 *
946
 * @ingroup compose
947
 */
948
function compose_description_element_taxon_interaction($element, $feature_block_settings) {
949
950
  $out = '';
951 57991ddf Andreas Kohlbecker
952 e413f218 Andreas Kohlbecker
953
  if (isset($element->description_L10n)) {
954
    $out .=  ' ' . $element->description_L10n;
955
  }
956
957
  if(isset($element->taxon2)){
958
    $out = render_taxon_or_name($element->taxon2, url(path_to_taxon($element->taxon2->uuid)));
959
  }
960
961 f23ae4e3 Andreas Kohlbecker
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
962 e413f218 Andreas Kohlbecker
963 f23ae4e3 Andreas Kohlbecker
  return $render_array;
964 e413f218 Andreas Kohlbecker
}
965
966
967 dfc49afb Andreas Kohlbecker
/**
968
 * Renders a single instance of the type IndividualsAssociations.
969
 *
970
 * @param $element
971
 *   The CDM IndividualsAssociations entity.
972
 * @param $feature_block_settings
973
 *
974
 * @return array
975
 *   Drupal render array
976
 *
977
 * @ingroup compose
978
 */
979
function compose_description_element_individuals_association($element, $feature_block_settings) {
980
981
  $out = '';
982 ce29c528 Andreas Kohlbecker
983 bd814ea2 Andreas Kohlbecker
  $render_array = compose_cdm_specimen_or_observation($element->associatedSpecimenOrObservation);
984 dfc49afb Andreas Kohlbecker
985
  if (isset($element->description_L10n)) {
986
    $out .=  ' ' . $element->description_L10n;
987 ce29c528 Andreas Kohlbecker
  }
988
989 dfc49afb Andreas Kohlbecker
  $out .= drupal_render($render_array);
990 ce29c528 Andreas Kohlbecker
991 f23ae4e3 Andreas Kohlbecker
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
992 dfc49afb Andreas Kohlbecker
993 f23ae4e3 Andreas Kohlbecker
  return $render_array;
994 dfc49afb Andreas Kohlbecker
}
995
996 529c47ff Andreas Kohlbecker
/**
997
 * Renders a single instance of the type CategoricalData.
998
 *
999
 * @param $element
1000 c651d3b1 Andreas Kohlbecker
 *  The CDM CategoricalData entity
1001
 *
1002 4f8e61ad Andreas Kohlbecker
 * @param $feature_block_settings
1003 529c47ff Andreas Kohlbecker
 *
1004 c651d3b1 Andreas Kohlbecker
 * @param bool $prepend_feature_label
1005
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1006
 *
1007 55236f7f Andreas Kohlbecker
 * @return array
1008
 *   a drupal render array for given CategoricalData element
1009 529c47ff Andreas Kohlbecker
 *
1010
 * @ingroup compose
1011
 */
1012 c651d3b1 Andreas Kohlbecker
function compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1013 529c47ff Andreas Kohlbecker
1014 f63479ae Andreas Kohlbecker
  $state_data_markup = render_state_data($element);
1015 e170a53e Patrick Plitzner
1016 f63479ae Andreas Kohlbecker
  $render_array = compose_description_element($element, $feature_block_settings, $state_data_markup, $element->feature->uuid, $prepend_feature_label);
1017 e170a53e Patrick Plitzner
1018
  return $render_array;
1019
}
1020
1021
/**
1022
 * @param $element
1023
 *
1024
 * @return string
1025 f63479ae Andreas Kohlbecker
 * the markup
1026 e170a53e Patrick Plitzner
 */
1027 f63479ae Andreas Kohlbecker
function render_state_data($element) {
1028 882e2c02 Patrick Plitzner
1029 f63479ae Andreas Kohlbecker
  $state_data_items = [];
1030 882e2c02 Patrick Plitzner
1031 f146528a Andreas Kohlbecker
  $out = '';
1032
1033 529c47ff Andreas Kohlbecker
  if (isset($element->stateData)) {
1034
    foreach ($element->stateData as $state_data) {
1035
1036 e170a53e Patrick Plitzner
      $state = NULL;
1037 529c47ff Andreas Kohlbecker
1038
      if (isset($state_data->state)) {
1039
        $state = cdm_term_representation($state_data->state);
1040
1041 fec3fd41 Andreas Kohlbecker
          $sample_count = 0;
1042 4b428921 Patrick Plitzner
          if (isset($state_data->count)) {
1043 fec3fd41 Andreas Kohlbecker
            $sample_count = $state_data->count;
1044 f63479ae Andreas Kohlbecker
            $state .= ' [' . $state_data->count . ']';
1045 4b428921 Patrick Plitzner
          }
1046
    
1047
          if (isset($state_data->modifyingText_L10n)) {
1048
            $state .= ' ' . $state_data->modifyingText_L10n;
1049
          }
1050
    
1051 a6d7907d Andreas Kohlbecker
          $modifiers_strings = cdm_modifiers_representations($state_data);
1052 4c9d7e32 Andreas Kohlbecker
          $state_data_markup = $state . ($modifiers_strings ? ' ' . $modifiers_strings : '');
1053
          // we could use strip_tags() to reduce the markup to text for the key but this is expensive
1054 fec3fd41 Andreas Kohlbecker
          $sort_key = str_pad($sample_count, 6, '0', STR_PAD_LEFT) . '_' . $state_data_markup;
1055 f63479ae Andreas Kohlbecker
          $state_data_items[$sort_key] = $state_data_markup;
1056 529c47ff Andreas Kohlbecker
      }
1057
1058
    }
1059 f63479ae Andreas Kohlbecker
    krsort($state_data_items);
1060
    $out = '<span class="' . html_class_attribute_ref($element) . '">' . join(', ', $state_data_items) .  '</span>';
1061 529c47ff Andreas Kohlbecker
  }
1062 f63479ae Andreas Kohlbecker
  return $out;
1063 4f8e61ad Andreas Kohlbecker
}
1064
1065
1066
/**
1067
 * Theme function to render CDM DescriptionElements of the type QuantitativeData.
1068
 *
1069
 * The function renders the statisticalValues contained in the QuantitativeData
1070
 * entity according to the following scheme:
1071
 *
1072
 * (ExtremeMin)-Min-Average-Max-(ExtremeMax)
1073
 *
1074
 * All modifiers of these values are appended.
1075
 *
1076
 * If the QuantitativeData is containing more statisticalValues with further
1077
 * statisticalValue types, these additional measures will be appended to the
1078
 * above string separated by whitespace.
1079
 *
1080
 * Special cases;
1081
 * 1. Min==Max: this will be interpreted as Average
1082
 *
1083 c651d3b1 Andreas Kohlbecker
 * @param $element
1084
 *  The CDM QuantitativeData entity
1085
 *
1086
 * @param $feature_block_settings
1087
 *
1088
 * @param bool $prepend_feature_label
1089
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1090
 *
1091 4f8e61ad Andreas Kohlbecker
 *
1092 55236f7f Andreas Kohlbecker
 * @return array
1093
 *   drupal render array for the given QuantitativeData element
1094 4f8e61ad Andreas Kohlbecker
 *
1095 55236f7f Andreas Kohlbecker
 * @ingroup compose
1096 4f8e61ad Andreas Kohlbecker
 */
1097 c651d3b1 Andreas Kohlbecker
function compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1098 4f8e61ad Andreas Kohlbecker
  /*
1099
   * - statisticalValues
1100
   *   - value
1101
   *   - modifiers
1102
   *   - type
1103
   * - unit->representation_L10n
1104
   * - modifyingText
1105
   * - modifiers
1106
   * - sources
1107
   */
1108
1109 fec3fd41 Andreas Kohlbecker
  $out = render_quantitative_statistics($element);
1110 e170a53e Patrick Plitzner
1111
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid, $prepend_feature_label);
1112
1113
  return $render_array;
1114
}
1115
1116
/**
1117
 * Composes the HTML for quantitative statistics
1118
 * @param $element
1119
 *
1120
 * @return string
1121
 */
1122 fec3fd41 Andreas Kohlbecker
function render_quantitative_statistics($element) {
1123 ad7fa57c Andreas Kohlbecker
1124 4f8e61ad Andreas Kohlbecker
  $out = '';
1125
  $type_representation = NULL;
1126 2f4646e0 Andreas Kohlbecker
  $min_max = statistical_values_array();
1127 ad7fa57c Andreas Kohlbecker
  $sample_size_markup = null;
1128 4f8e61ad Andreas Kohlbecker
1129
  if (isset($element->statisticalValues)) {
1130 fec3fd41 Andreas Kohlbecker
    $out = '<span class=\"' . html_class_attribute_ref($element) . '\">';
1131 e170a53e Patrick Plitzner
    $other_values_markup = [];
1132 4f8e61ad Andreas Kohlbecker
    foreach ($element->statisticalValues as $statistical_val) {
1133
1134
      // compile the full value string which also may contain modifiers
1135
      if (isset($statistical_val->value)) {
1136
        $statistical_val->_value = $statistical_val->value;
1137
      }
1138 a6d7907d Andreas Kohlbecker
      $val_modifiers_strings = cdm_modifiers_representations($statistical_val);
1139 4f8e61ad Andreas Kohlbecker
      if ($val_modifiers_strings) {
1140
        $statistical_val->_value = ' ' . $val_modifiers_strings . ' ' . $statistical_val->_value;
1141
      }
1142
1143
      // either put into min max array or into $other_values
1144
      // for generic output to be appended to 'min-max' string
1145 f63479ae Andreas Kohlbecker
      if (array_key_exists(statistical_measure_term2min_max_key($statistical_val->type), $min_max)) {
1146
        $min_max[statistical_measure_term2min_max_key($statistical_val->type)] = $statistical_val;
1147 4f8e61ad Andreas Kohlbecker
      }
1148
      else {
1149 7f36e33a Andreas Kohlbecker
        drupal_set_message("Unsupported statistical value type: " . $statistical_val->type->uuid, "error");
1150 4f8e61ad Andreas Kohlbecker
      }
1151
    } // end of loop over statisticalValues
1152
1153
    // create markup
1154 7f36e33a Andreas Kohlbecker
    $unit = null;
1155
    if (isset($element->unit)) {
1156
      $unit = ' <span class="unit" title="'
1157
        . cdm_term_representation($element->unit) . '">'
1158
        . cdm_term_representation_abbreviated($element->unit)
1159
        . '</span>';
1160 4f8e61ad Andreas Kohlbecker
    }
1161 7f36e33a Andreas Kohlbecker
    $min_max_markup = statistical_values($min_max, $unit);
1162
    $out .= $min_max_markup . '</span>';
1163 4f8e61ad Andreas Kohlbecker
  }
1164
1165 ad7fa57c Andreas Kohlbecker
  if($sample_size_markup){
1166
    $out .= ' ' . $sample_size_markup;
1167
1168
  }
1169
1170 4f8e61ad Andreas Kohlbecker
  // modifiers of the description element itself
1171 a6d7907d Andreas Kohlbecker
  $modifier_string = cdm_modifiers_representations($element);
1172 4f8e61ad Andreas Kohlbecker
  $out .= ($modifier_string ? ' ' . $modifier_string : '');
1173
  if (isset($element->modifyingText_L10n)) {
1174
    $out = $element->modifyingText_L10n . ' ' . $out;
1175
  }
1176 e170a53e Patrick Plitzner
  return $out;
1177 529c47ff Andreas Kohlbecker
}
1178
1179 f63479ae Andreas Kohlbecker
function statistical_measure_term2min_max_key($term){
1180
  static $uuid2key = [
1181
    UUID_STATISTICALMEASURE_MIN => 'Min',
1182
    UUID_STATISTICALMEASURE_MAX => 'Max',
1183
    UUID_STATISTICALMEASURE_AVERAGE => 'Average',
1184
    UUID_STATISTICALMEASURE_SAMPLESIZE => 'SampleSize',
1185
    UUID_STATISTICALMEASURE_VARIANCE => 'Variance',
1186
    UUID_STATISTICALMEASURE_TYPICALLOWERBOUNDARY => 'TypicalLowerBoundary',
1187
    UUID_STATISTICALMEASURE_TYPICALUPPERBOUNDARY => 'TypicalUpperBoundary',
1188
    UUID_STATISTICALMEASURE_STANDARDDEVIATION => 'StandardDeviation',
1189
    UUID_STATISTICALMEASURE_EXACTVALUE => 'ExactValue',
1190
    UUID_STATISTICALMEASURE_STATISTICALMEASUREUNKNOWNDATA => 'StatisticalMeasureUnknownData'
1191
  ];
1192
  return $uuid2key[$term->uuid];
1193
}
1194
1195 dfc49afb Andreas Kohlbecker
1196
/**
1197 c651d3b1 Andreas Kohlbecker
 * Wraps the render array for the given feature into an enclosing html tag.
1198 dfc49afb Andreas Kohlbecker
 *
1199 c651d3b1 Andreas Kohlbecker
 * Optionally the elements can be sorted and glued together by a separator string.
1200
 *
1201
 * @param array $description_element_render_arrays
1202 f23ae4e3 Andreas Kohlbecker
 *   An list of render arrays. Which are usually are produced by the compose_description_element()
1203
 *   function. The render arrays should be of #type=html_tag, so that they can understand the #attribute property.
1204 dfc49afb Andreas Kohlbecker
 * @param  $feature :
1205
 *  The feature to which the elements given in $elements are belonging to.
1206
 * @param string $glue :
1207
 *  Defaults to empty string.
1208
 * @param bool $sort
1209
 *   Boolean Whether to sort the $elements alphabetically, default is FALSE
1210
 *
1211 c651d3b1 Andreas Kohlbecker
 * @return array
1212
 *    A Drupal render array
1213 dfc49afb Andreas Kohlbecker
 *
1214
 * @ingroup compose
1215
 */
1216 c651d3b1 Andreas Kohlbecker
  function compose_feature_block_wrap_elements(array $description_element_render_arrays, $feature, $glue = '', $sort = FALSE)
1217 dfc49afb Andreas Kohlbecker
  {
1218 ce29c528 Andreas Kohlbecker
1219
    $feature_block_settings = get_feature_block_settings($feature->uuid);
1220 0ef9a709 Andreas Kohlbecker
    $enclosing_tag = $feature_block_settings['as_list'];
1221 ce29c528 Andreas Kohlbecker
1222 f23ae4e3 Andreas Kohlbecker
    if ($sort) { // TODO remove parameter and replace by $feature_block_settings['sort_elements']
1223 c651d3b1 Andreas Kohlbecker
      usort($description_element_render_arrays, 'compare_description_element_render_arrays');
1224 f23ae4e3 Andreas Kohlbecker
    }
1225 ce29c528 Andreas Kohlbecker
1226 f23ae4e3 Andreas Kohlbecker
    $is_first = true;
1227 c651d3b1 Andreas Kohlbecker
    foreach($description_element_render_arrays as &$element_render_array){
1228 f23ae4e3 Andreas Kohlbecker
      if(!is_array($element_render_array)){
1229
        $element_render_array = markup_to_render_array($element_render_array);
1230
      }
1231
      $element_render_array['#attributes']['class'][] = "feature-block-element";
1232
1233
      // add the glue!
1234
      if(!$is_first) {
1235
        if (isset($element_render_array['#value'])) {
1236
          $element_render_array['#value'] = $glue . $element_render_array['#value'];
1237
        } elseif (isset($element_render_array['#markup'])) {
1238
          $element_render_array['#markup'] = $glue . $element_render_array['#markup'];
1239
        }
1240
      }
1241
      $is_first = false;
1242 ce29c528 Andreas Kohlbecker
    }
1243
1244 c651d3b1 Andreas Kohlbecker
    $render_array['elements']['children'] = $description_element_render_arrays;
1245 ce29c528 Andreas Kohlbecker
1246 f23ae4e3 Andreas Kohlbecker
    $render_array['elements']['#prefix'] = '<' . $enclosing_tag . ' class="feature-block-elements" id="' . $feature->representation_L10n . '">';
1247
    $render_array['elements']['#suffix'] = '</' . $enclosing_tag . '>';
1248
1249
    return $render_array;
1250 ce29c528 Andreas Kohlbecker
  }
1251
1252
1253
  /* compose nameInSource or originalNameString as markup
1254
   *
1255
   * @param $source
1256
   * @param $do_link_to_name_used_in_source
1257
   * @param $suppress_for_shown_taxon
1258
   *    the nameInSource will be suppressed when it has the same name as the accepted taxon
1259
   *    for which the taxon page is being created, Defaults to TRUE
1260
   *
1261
   * @return array
1262
   *    A Drupal render array with an additional element, the render array is empty
1263
   *    if the source had no name in source information
1264
   *    - #_plaintext: contains the plaintext version of the name (custom element)
1265
   *
1266
   * @ingroup compose
1267
   */
1268
  function compose_name_in_source($source, $do_link_to_name_used_in_source, $suppress_for_shown_taxon = TRUE) {
1269
1270
    $plaintext = NULL;
1271
    $markup = NULL;
1272
    $name_in_source_render_array = array();
1273
1274
    static $taxon_page_accepted_name = '';
1275 0af3ce28 Andreas Kohlbecker
    $taxon_uuid = get_current_taxon_uuid();
1276
    if($suppress_for_shown_taxon && $taxon_uuid && empty($taxon_page_accepted_name)){
1277 ce29c528 Andreas Kohlbecker
1278 0af3ce28 Andreas Kohlbecker
      $current_taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $taxon_uuid);
1279 ce29c528 Andreas Kohlbecker
      $taxon_page_accepted_name = $current_taxon->name->titleCache;
1280
    }
1281
1282
    if (isset($source->nameUsedInSource->uuid) && isset($source->nameUsedInSource->titleCache)) {
1283
      // it is a DescriptionElementSource !
1284
      $plaintext = $source->nameUsedInSource->titleCache;
1285
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1286
        return $name_in_source_render_array; // SKIP this name
1287
      }
1288 63c3ac73 Andreas Kohlbecker
      $markup = render_taxon_or_name($source->nameUsedInSource);
1289 ce29c528 Andreas Kohlbecker
      if ($do_link_to_name_used_in_source) {
1290
        $markup = l(
1291
          $markup,
1292
          path_to_name($source->nameUsedInSource->uuid),
1293
          array(
1294
            'attributes' => array(),
1295
            'absolute' => TRUE,
1296
            'html' => TRUE,
1297
          ));
1298
      }
1299
    }
1300
    else if (isset($source->originalNameString) && !empty($source->originalNameString)) {
1301
      // the name used in source can not be expressed as valid taxon name,
1302
      // so the editor has chosen to put the freetext name into ReferencedEntityBase.originalNameString
1303
      // field
1304
      // using the originalNameString as key to avoid duplicate entries
1305
      $plaintext = $source->originalNameString;
1306
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1307
        return $name_in_source_render_array; // SKIP this name
1308
      }
1309
      $markup = $source->originalNameString;
1310
    }
1311
1312
    if ($plaintext) { // checks if we have any content
1313
      $name_in_source_render_array = markup_to_render_array($markup);
1314
      $name_in_source_render_array['#_plaintext'] = $plaintext;
1315
    }
1316
1317
    return $name_in_source_render_array;
1318
  }
1319
1320
1321
1322
  /**
1323
   * Return HTML for a list of description elements.
1324
   *
1325
   * Usually these are of a specific feature type.
1326
   *
1327 c651d3b1 Andreas Kohlbecker
   * @param $description_elements
1328 ce29c528 Andreas Kohlbecker
   *   array of descriptionElements which belong to the same feature.
1329
   *   These descriptions elements of a Description must be ordered by the chosen feature tree by
1330
   *   calling the function _mergeFeatureTreeDescriptions().
1331
   *   @see _mergeFeatureTreeDescriptions()
1332
   *
1333
   * @param  $feature_uuid
1334
   *
1335
   * @return
1336
   *    A drupal render array for the $descriptionElements, may be an empty array if the textual content was empty.
1337
   *    Footnote key or anchors are not considered to be textual content.
1338
   *
1339
   * @ingroup compose
1340
   */
1341 c651d3b1 Andreas Kohlbecker
  function compose_feature_block_items_generic($description_elements, $feature) {
1342 ce29c528 Andreas Kohlbecker
1343
    $elements_out_array = array();
1344
    $distribution_tree = null;
1345
1346
    /*
1347
     * $feature_block_has_content will be set true if at least one of the
1348
     * $descriptionElements contains some text which makes up some content
1349
     * for the feature block. Footnote keys are not considered
1350
     * to be content in this sense.
1351
     */
1352
    $feature_block_has_content = false;
1353
1354 c651d3b1 Andreas Kohlbecker
    if (is_array($description_elements)) {
1355
      foreach ($description_elements as $description_element) {
1356 ce29c528 Andreas Kohlbecker
          /* decide based on the description element class
1357
           *
1358
           * Features handled here:
1359
           * all except DISTRIBUTION, COMMON_NAME, USES, IMAGES,
1360
           *
1361
           * TODO provide api_hook as extension point for this?
1362
           */
1363 c651d3b1 Andreas Kohlbecker
        $feature_block_settings = get_feature_block_settings($description_element->feature->uuid);
1364
        switch ($description_element->class) {
1365 f23ae4e3 Andreas Kohlbecker
          case 'TextData':
1366 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_text_data($description_element, $description_element->feature->uuid, $feature_block_settings);
1367 f23ae4e3 Andreas Kohlbecker
            break;
1368
          case 'CategoricalData':
1369 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_categorical_data($description_element, $feature_block_settings);
1370 f23ae4e3 Andreas Kohlbecker
            break;
1371
          case 'QuantitativeData':
1372 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_quantitative_data($description_element, $feature_block_settings);
1373 f23ae4e3 Andreas Kohlbecker
            break;
1374
          case 'IndividualsAssociation':
1375 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_individuals_association($description_element, $feature_block_settings);
1376 f23ae4e3 Andreas Kohlbecker
            break;
1377
          case 'TaxonInteraction':
1378 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_taxon_interaction($description_element, $feature_block_settings);
1379 f23ae4e3 Andreas Kohlbecker
            break;
1380 57991ddf Andreas Kohlbecker
          case 'CommonTaxonName':
1381
            $elements_out_array[] = compose_description_element_common_taxon_name($description_element, $feature_block_settings);
1382
            break;
1383 f23ae4e3 Andreas Kohlbecker
          case 'Uses':
1384 1b756c5f Andreas Kohlbecker
            /* IGNORE Uses classes, these are handled completely in compose_feature_block_items_use_records()  */
1385 f23ae4e3 Andreas Kohlbecker
            break;
1386
          default:
1387
            $feature_block_has_content = true;
1388 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = markup_to_render_array('<li>No method for rendering unknown description class: ' . $description_element->class . '</li>');
1389 f23ae4e3 Andreas Kohlbecker
        }
1390
        $elements_out_array_last_item = $elements_out_array[count($elements_out_array) - 1];
1391
        // considering not empty as long as the last item added is a render array
1392
        $feature_block_has_content = $feature_block_has_content || !empty($elements_out_array_last_item['#type']);
1393
      }
1394 ce29c528 Andreas Kohlbecker
1395
      // If feature = CITATION sort the list of sources.
1396
      // This is ONLY for FLORA MALESIANA and FLORE d'AFRIQUE CENTRALE.
1397 c651d3b1 Andreas Kohlbecker
      if (isset($description_element) && $description_element->feature->uuid == UUID_CITATION) {
1398 ce29c528 Andreas Kohlbecker
        sort($elements_out_array);
1399
      }
1400 aa26f9f0 Andreas Kohlbecker
    }
1401 ce29c528 Andreas Kohlbecker
1402 7a751db4 Andreas Kohlbecker
    // sanitize: remove empty and NULL items from the render array
1403
    $tmp_out_array = $elements_out_array;
1404
    $elements_out_array = array();
1405
    foreach($tmp_out_array as $item){
1406 6a0ca989 Andreas Kohlbecker
      if(is_array($item) && count($item) > 0){
1407 7a751db4 Andreas Kohlbecker
        $elements_out_array[] = $item;
1408
      }
1409
    }
1410
1411 c651d3b1 Andreas Kohlbecker
    return $elements_out_array;
1412 ce29c528 Andreas Kohlbecker
  }
1413
1414 4a236db6 Andreas Kohlbecker
/**
1415
 * Composes block of common names for the given DescriptionElements $elements which must be of the feature CommonName
1416
 *
1417
 * @parameter $elements
1418
 *  an array of CDM DescriptionElements either of type CommonName or TextData
1419
 * @parameter $feature
1420
 *  the common feature of all $elements, must be CommonName
1421
 *
1422
 * @return
1423
 *   A drupal render array
1424
 *
1425
 * @ingroup compose
1426
 */
1427 0ebb6efc Andreas Kohlbecker
function compose_feature_block_items_feature_common_name($elements, $feature, $weight = FALSE) {
1428 4a236db6 Andreas Kohlbecker
1429
  $common_name_out = '';
1430
  $common_name_feature_elements = array();
1431
  $textData_commonNames = array();
1432
1433
  $footnote_key_suggestion = 'common-names-feature-block';
1434
1435
  $feature_block_settings = get_feature_block_settings(UUID_COMMON_NAME);
1436
1437
  if (is_array($elements)) {
1438
    foreach ($elements as $element) {
1439
1440
      if ($element->class == 'CommonTaxonName') {
1441
1442
        // common name without a language or area, should not happen but is possible
1443
        $language_area_key = '';
1444
        if (isset($element->language->representation_L10n)) {
1445 a20ddbc4 Andreas Kohlbecker
          $language_area_key .= $element->language->representation_L10n ;
1446 4a236db6 Andreas Kohlbecker
        }
1447
        if(isset($element->area->titleCache) && strlen($element->area->titleCache) > 0){
1448
          $language_area_key .= ($language_area_key ? ' '  : '') . '(' . $element->area->titleCache . ')';
1449
        }
1450 a20ddbc4 Andreas Kohlbecker
        if($language_area_key){
1451
          $language_area_key = '<span class="language-area-label">' . $language_area_key . '<span class="separator">: </span></span>';
1452
        }
1453 4a236db6 Andreas Kohlbecker
1454
        if(isset($common_names[$language_area_key][$element->name])) {
1455 f23ae4e3 Andreas Kohlbecker
          // same name already exists for language and area combination, se we merge the description elements
1456 4a236db6 Andreas Kohlbecker
          cdm_merge_description_elements($common_names[$language_area_key][$element->name], $element);
1457
        } else{
1458
          // otherwise add as new entry
1459
          $common_names[$language_area_key][$element->name] = $element;
1460
        }
1461
1462
      }
1463
      elseif ($element->class == 'TextData') {
1464
        $textData_commonNames[] = $element;
1465
      }
1466
    }
1467
  }
1468
  // Handling common names.
1469
  if (isset($common_names) && count($common_names) > 0) {
1470
    // Sorting the array based on the key (language, + area if set).
1471
    // Comment @WA there are common names without a language, so this sorting
1472
    // can give strange results.
1473
    ksort($common_names);
1474
1475
    // loop over set of elements per language area
1476
    foreach ($common_names as $language_area_key => $elements) {
1477
      ksort($elements); // sort names alphabetically
1478
      $per_language_area_out = array();
1479 57991ddf Andreas Kohlbecker
1480 4a236db6 Andreas Kohlbecker
      foreach ($elements as $element) {
1481 57991ddf Andreas Kohlbecker
        $common_name_render_array = compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key_suggestion);
1482
        $common_name_markup = drupal_render($common_name_render_array);
1483
        // IMPORTANT!
1484
        // during the above drupal_render the theme_html_tag function is executed, which adds a "\n" character to the end of the markup
1485
        // this is an error and the trailing whitespace needs to be removed
1486
        if(str_endsWith($common_name_markup, "\n")){
1487
          $common_name_markup = substr($common_name_markup, 0, strlen($common_name_markup) - 1);
1488 4a236db6 Andreas Kohlbecker
        }
1489 57991ddf Andreas Kohlbecker
        $per_language_area_out[] = $common_name_markup;
1490
      }
1491
1492 a20ddbc4 Andreas Kohlbecker
      $common_name_feature_elements[] = $language_area_key . join(', ', $per_language_area_out);
1493 4a236db6 Andreas Kohlbecker
    } // End of loop over set of elements per language area
1494
1495
1496 c651d3b1 Andreas Kohlbecker
    $common_name_feature_elements_render_array = compose_feature_block_wrap_elements(
1497 dfc49afb Andreas Kohlbecker
      $common_name_feature_elements, $feature, '; ', FALSE
1498 4a236db6 Andreas Kohlbecker
    );
1499 f23ae4e3 Andreas Kohlbecker
    $common_name_out .= drupal_render($common_name_feature_elements_render_array); // FIXME should this be a render array instead?
1500 4a236db6 Andreas Kohlbecker
1501
  }
1502
1503
  // Handling commons names as text data.
1504
  $text_data_out = array();
1505
1506
  foreach ($textData_commonNames as $text_data_element) {
1507 0ef9a709 Andreas Kohlbecker
    /* footnotes are not handled correctly in compose_description_element_text_data,
1508 4a236db6 Andreas Kohlbecker
       need to set 'common-names-feature-block' as $footnote_key_suggestion */
1509
    RenderHints::setFootnoteListKey($footnote_key_suggestion);
1510 0ef9a709 Andreas Kohlbecker
    $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
1511 4a236db6 Andreas Kohlbecker
    $text_data_out[] = drupal_render($text_data_render_array);
1512
  }
1513
1514 c651d3b1 Andreas Kohlbecker
  $common_name_out_text_data = compose_feature_block_wrap_elements(
1515 dfc49afb Andreas Kohlbecker
    $text_data_out, $feature
1516 4a236db6 Andreas Kohlbecker
  );
1517
1518 d8069342 Andreas Kohlbecker
  $footnotes = render_footnotes('BIBLIOGRAPHY-' . $footnote_key_suggestion);
1519 d0d0a092 Andreas Kohlbecker
  $footnotes .= render_footnotes($footnote_key_suggestion);
1520 4a236db6 Andreas Kohlbecker
1521 f23ae4e3 Andreas Kohlbecker
  return  markup_to_render_array(  // FIXME markup_to_render_array should no longer be needed
1522 57991ddf Andreas Kohlbecker
    '<div class="common-taxon-name">' . $common_name_out . '</div>'
1523
    .'<div class="text-data">' . drupal_render($common_name_out_text_data) . '</div>'
1524 4a236db6 Andreas Kohlbecker
    .$footnotes,
1525
    $weight
1526
  );
1527
}
1528 ce29c528 Andreas Kohlbecker
1529 57991ddf Andreas Kohlbecker
/**
1530
 * Renders a single instance of the type CommonTaxonName.
1531
 *
1532
 * @param $element
1533
 *   The CDM CommonTaxonName entity.
1534
 * @param $feature_block_settings
1535
 *
1536 9f9fe101 Andreas Kohlbecker
 * @param $footnote_key
1537 57991ddf Andreas Kohlbecker
 *
1538
 * @return array
1539
 *   Drupal render array
1540
 *
1541
 * @ingroup compose
1542
 */
1543 9f9fe101 Andreas Kohlbecker
function compose_description_element_common_taxon_name($element, $feature_block_settings, $footnote_key = NULL)
1544 57991ddf Andreas Kohlbecker
{
1545 ce29c528 Andreas Kohlbecker
1546 9f9fe101 Andreas Kohlbecker
  if(!$footnote_key) {
1547
    $footnote_key = $element->feature->uuid;
1548 57991ddf Andreas Kohlbecker
  }
1549 ce29c528 Andreas Kohlbecker
1550 57991ddf Andreas Kohlbecker
  $name = '';
1551
  if(isset($element->name)){
1552
    $name = $element->name;
1553
  }
1554 ce29c528 Andreas Kohlbecker
1555 18727898 Andreas Kohlbecker
1556 9f9fe101 Andreas Kohlbecker
  return compose_description_element($element, $feature_block_settings, $name, $footnote_key);
1557 57991ddf Andreas Kohlbecker
}
1558 18727898 Andreas Kohlbecker
1559 57991ddf Andreas Kohlbecker
/**
1560
 * Composes the render array for a CDM Distribution description element
1561
 *
1562
 * @param array $description_elements
1563
 *   Array of CDM Distribution instances
1564
 * @param $enclosingTag
1565
 *   The html tag to be use for the enclosing element
1566
 *
1567
 * @return array
1568
 *   A Drupal render array
1569
 *
1570
 * @ingroup compose
1571
 */
1572
function compose_description_elements_distribution($description_elements){
1573 ce29c528 Andreas Kohlbecker
1574 bf4b3dfa Andreas Kohlbecker
  $markup_array = array();
1575 57991ddf Andreas Kohlbecker
  RenderHints::pushToRenderStack('descriptionElementDistribution');
1576
  RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1577
1578
  $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
1579
  $enclosingTag = cdm_feature_block_element_tag_name($feature_block_settings);
1580
1581
  foreach ($description_elements as $description_element) {
1582
    $annotations_and_sources = handle_annotations_and_sources(
1583
      $description_element,
1584 0c2b9b9d Andreas Kohlbecker
      handle_annotations_and_sources_config($feature_block_settings),
1585 57991ddf Andreas Kohlbecker
      $description_element->area->representation_L10n,
1586
      UUID_DISTRIBUTION
1587
    );
1588
1589
1590 29857cb0 Andreas Kohlbecker
    $status = distribution_status_label_and_markup([$description_element]);
1591 57991ddf Andreas Kohlbecker
1592 bf4b3dfa Andreas Kohlbecker
    $out = '';
1593 57991ddf Andreas Kohlbecker
    $out .= '<' . $enclosingTag . ' class="descriptionElement descriptionElement-' . $description_element->uuid
1594 c797d7db Andreas Kohlbecker
      . ' " title="' . $status['label']. '">'
1595 57991ddf Andreas Kohlbecker
      . $description_element->area->representation_L10n
1596 c797d7db Andreas Kohlbecker
      . $status['markup'];
1597 57991ddf Andreas Kohlbecker
    if(!empty($annotations_and_sources['source_references'])){
1598
      $out .= ' ' . join(' ', $annotations_and_sources['source_references'] );
1599
    }
1600 bf4b3dfa Andreas Kohlbecker
    $out .= $annotations_and_sources['foot_note_keys']   . '</' . $enclosingTag . '>';
1601
    $markup_array[] = $out;
1602 ce29c528 Andreas Kohlbecker
  }
1603
1604 57991ddf Andreas Kohlbecker
  RenderHints::popFromRenderStack();
1605 bf4b3dfa Andreas Kohlbecker
  return markup_to_render_array(join('<span class="separator">' . $feature_block_settings['glue'] . '</span>', $markup_array));
1606 57991ddf Andreas Kohlbecker
}
1607
1608 65345976 Andreas Kohlbecker
  /**
1609 29857cb0 Andreas Kohlbecker
   * @param array $distribution_status
1610 c797d7db Andreas Kohlbecker
   * @return array an array with following keys
1611
   *   - 'label': the plain text status label
1612
   *   - 'markup': markup for the status
1613 65345976 Andreas Kohlbecker
   */
1614 29857cb0 Andreas Kohlbecker
  function distribution_status_label_and_markup(array $distribution_status, $status_glue = '&#8210; ') {
1615
1616 65345976 Andreas Kohlbecker
    $status_markup = '';
1617
    $status_label = '';
1618
1619 29857cb0 Andreas Kohlbecker
    foreach($distribution_status as $status) {
1620 654bed00 Andreas Kohlbecker
      $status_label .= (isset($status->representation_L10n) ? $status_glue : '') . $status->representation_L10n;
1621 29857cb0 Andreas Kohlbecker
      $status_markup .=  '<span class="distributionStatus"> '
1622
        . ($status_markup ? $status_glue : '')
1623
        . '<span class="distributionStatus-' . $status->idInVocabulary . '">'
1624
        .  $status->representation_L10n
1625 3de12c0d Andreas Kohlbecker
        . '</span></span>';
1626 65345976 Andreas Kohlbecker
1627
    };
1628 c797d7db Andreas Kohlbecker
    return ['label' => $status_label, 'markup' => $status_markup];
1629 65345976 Andreas Kohlbecker
  }
1630
1631 ce29c528 Andreas Kohlbecker
1632 7508605c Andreas Kohlbecker
  /**
1633
   * Provides the merged feature tree for a taxon profile page.
1634
   *
1635 4feafea8 Andreas Kohlbecker
   * The merging of the profile feature tree is actually done in
1636 7508605c Andreas Kohlbecker
   * _mergeFeatureTreeDescriptions(). See this method  for details
1637
   * on the structure of the merged tree.
1638
   *
1639 4feafea8 Andreas Kohlbecker
   * This method provides a hook which can be used to modify the
1640 7508605c Andreas Kohlbecker
   * merged feature tree after it has been created, see
1641
   * hook_merged_taxon_feature_tree_alter()
1642
   *
1643
   * @param $taxon
1644 6527c2f5 Andreas Kohlbecker
   *    A CDM Taxon instance
1645 7508605c Andreas Kohlbecker
   *
1646
   * @return object
1647 6527c2f5 Andreas Kohlbecker
   *    The merged feature tree
1648 7508605c Andreas Kohlbecker
   *
1649
   */
1650
  function merged_taxon_feature_tree($taxon) {
1651
1652
    // 1. fetch descriptions_by_featuretree but exclude the distribution feature
1653
    $merged_tree = cdm_ws_descriptions_by_featuretree(get_profile_feature_tree(), $taxon->uuid, array(UUID_DISTRIBUTION));
1654
1655
1656
    // 2. find the distribution feature node
1657
    $distribution_node =& cdm_feature_tree_find_node($merged_tree->root->childNodes, UUID_DISTRIBUTION);
1658
1659
    if ($distribution_node) {
1660
      // 3. get the distributionInfoDTO
1661
      $query_parameters = cdm_distribution_filter_query();
1662
      $query_parameters['part'] = array('mapUriParams');
1663
      if(variable_get(DISTRIBUTION_CONDENSED)){
1664
        $query_parameters['part'][] = 'condensedDistribution';
1665
      }
1666 97ac3d38 Andreas Kohlbecker
      if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) == 'TREE') {
1667 7508605c Andreas Kohlbecker
        $query_parameters['part'][] = 'tree';
1668
      }
1669
      else {
1670
        $query_parameters['part'][] = 'elements';
1671
      }
1672 284fb36d Andreas Kohlbecker
      $query_parameters['omitLevels'] = array();
1673
      foreach(variable_get(DISTRIBUTION_TREE_OMIT_LEVELS, array()) as $uuid){
1674
        if(is_uuid($uuid)){
1675
          $query_parameters['omitLevels'][] = $uuid;
1676
        }
1677
      }
1678 7508605c Andreas Kohlbecker
      $customStatusColorsJson = variable_get(DISTRIBUTION_STATUS_COLORS, NULL);
1679
      if ($customStatusColorsJson) {
1680
        $query_parameters['statusColors'] = $customStatusColorsJson;
1681
      }
1682
1683
      $distribution_info_dto = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION_DISTRIBUTION_INFO_FOR, $taxon->uuid, queryString($query_parameters));
1684
      // 4. get distribution TextData is there are any
1685
      $distribution_text_data = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON,
1686
        array(
1687
          'taxon' => $taxon->uuid,
1688
          'type' => 'TextData',
1689
          'features' => UUID_DISTRIBUTION
1690
        )
1691
      );
1692
1693
      // 5. put all distribution data into the distribution feature node
1694
      if ($distribution_text_data //if text data exists
1695
        || ($distribution_info_dto && isset($distribution_info_dto->tree) && $distribution_info_dto->tree->rootElement->numberOfChildren > 0) // OR if tree element has distribution elements
1696
        || ($distribution_info_dto && !empty($distribution_info_dto->elements))
1697
      ) { // OR if DTO has distribution elements
1698
        $distribution_node->descriptionElements = array('#type' => 'DTO');
1699
        if ($distribution_text_data) {
1700
          $distribution_node->descriptionElements['TextData'] = $distribution_text_data;
1701
        }
1702
        if ($distribution_info_dto) {
1703
          $distribution_node->descriptionElements['DistributionInfoDTO'] = $distribution_info_dto;
1704
        }
1705
      }
1706
    }
1707
1708
    // allows modifying the merged tree via a the hook_cdm_feature_node_block_content_alter
1709
    drupal_alter('merged_taxon_feature_tree', $taxon, $merged_tree);
1710
1711
    return $merged_tree;
1712
  }
1713
1714 29857cb0 Andreas Kohlbecker
  /**
1715
   * @param $distribution_tree
1716
   *  A tree cdm TreeNode items. A TreeNode item has a NamedArea as nodeId
1717
   *  and Distribution items as data array. Per data array some Distributions may
1718
   *  be with status information, others only with sources, others with both.
1719
   *  Each node may also have subordinate node items in the children field.
1720
   *  TreeNode:
1721
   *   - array data
1722
   *   - array children
1723
   *   - int numberOfChildren
1724
   *   - stdClass nodeId
1725
   *
1726
   * @param $feature_block_settings
1727
   *
1728
   * @return array
1729
   * @throws \Exception
1730
   */
1731 65345976 Andreas Kohlbecker
  function compose_distribution_hierarchy($distribution_tree, $feature_block_settings){
1732
1733
    static $hierarchy_style;
1734 38dd933d Andreas Kohlbecker
    // TODO expose $hierarchy_style to administration or provide a hook
1735 65345976 Andreas Kohlbecker
    if( !isset($hierarchy_style)){
1736 38dd933d Andreas Kohlbecker
      $hierarchy_style = get_array_variable_merged(DISTRIBUTION_HIERARCHY_STYLE, DISTRIBUTION_HIERARCHY_STYLE_DEFAULT);
1737 65345976 Andreas Kohlbecker
    }
1738
1739
    $render_array = array();
1740
1741
    RenderHints::pushToRenderStack('descriptionElementDistribution');
1742
    RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1743
1744
    // Returning NULL if there are no description elements.
1745
    if ($distribution_tree == null) {
1746
      return $render_array;
1747
    }
1748
    // for now we are not using a render array internally to avoid performance problems
1749
    $markup = '';
1750
    if (isset($distribution_tree->rootElement->children)) {
1751
      $tree_nodes = $distribution_tree->rootElement->children;
1752
      _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, $markup, $hierarchy_style);
1753
    }
1754
1755
    $render_array['distribution_hierarchy'] = markup_to_render_array(
1756
      $markup,
1757
      0,
1758
      '<div id="distribution_hierarchy" class="distribution_hierarchy">',
1759
      '</div>'
1760
    );
1761
1762 71acbd65 Andreas Kohlbecker
    RenderHints::popFromRenderStack();
1763
1764 65345976 Andreas Kohlbecker
    return $render_array;
1765
  }
1766
1767 29857cb0 Andreas Kohlbecker
/**
1768
 * this function should produce markup as the
1769
 * compose_description_elements_distribution() function.
1770
 *
1771
 * @param array $tree_nodes
1772
 *  An array of cdm TreeNode items. A TreeNode item has a NamedArea as nodeId
1773
 *  and Distribution items as data array. Per data array some Distributions may
1774
 *  be with status information, others only with sources, others with both.
1775
 *  TreeNode:
1776
 *   - array data
1777
 *   - array children
1778
 *   - int numberOfChildren
1779
 *   - stdClass nodeId
1780
 * @param array $feature_block_settings
1781
 * @param $markup
1782
 * @param $hierarchy_style
1783
 * @param int $level_index
1784
 *
1785
 * @throws \Exception
1786
 *
1787
 * @see compose_description_elements_distribution()
1788
 * @see compose_distribution_hierarchy()
1789
 *
1790
 */
1791
  function _compose_distribution_hierarchy(array $tree_nodes, array $feature_block_settings, &$markup, $hierarchy_style, $level_index = -1){
1792 65345976 Andreas Kohlbecker
1793
    $level_index++;
1794
    static $enclosingTag = "span";
1795
1796 38dd933d Andreas Kohlbecker
    $level_style = array_shift($hierarchy_style);
1797 330d08aa Andreas Kohlbecker
    if(count($hierarchy_style) == 0){
1798
      // lowest defined level style will be reused for all following levels
1799
      $hierarchy_style[] = $level_style;
1800
    }
1801 65345976 Andreas Kohlbecker
1802
    $node_index = -1;
1803
    $per_node_markup = array();
1804 29857cb0 Andreas Kohlbecker
1805 65345976 Andreas Kohlbecker
    foreach ($tree_nodes as $node){
1806
1807
      $per_node_markup[++$node_index] = '';
1808
      $label = $node->nodeId->representation_L10n;
1809
1810
      $distributions = $node->data;
1811
      $distribution_uuids = array();
1812
      $distribution_aggregate = NULL;
1813 29857cb0 Andreas Kohlbecker
      $status = ['label' => '', 'markup' => ''];
1814
1815
      foreach($distributions as $distribution){
1816
        $distribution_uuids[] = $distribution->uuid;
1817
        // if there is more than one distribution we aggregate the sources and
1818
        // annotations into a synthetic distribution so that the footnote keys
1819
        // can be rendered consistently
1820
        if(!$distribution_aggregate) {
1821
          $distribution_aggregate = $distribution;
1822
          if(isset($distribution->status)){
1823
            $distribution_aggregate->status = [$distribution->status];
1824 65345976 Andreas Kohlbecker
          } else {
1825 29857cb0 Andreas Kohlbecker
            $distribution_aggregate->status = [];
1826
          }
1827
          if(!isset($distribution_aggregate->sources[0])){
1828
            $distribution_aggregate->sources = array();
1829
          }
1830
          if(!isset($distribution_aggregate->annotations[0])){
1831
            $distribution_aggregate->annotations = array();
1832
          }
1833
        } else {
1834
          if(isset($distribution->status)){
1835
            $distribution_aggregate->status[] = $distribution->status;
1836
          }
1837
          if(isset($distribution->sources[0])) {
1838
            $distribution_aggregate->sources = array_merge($distribution_aggregate->sources,
1839
              $distribution->sources);
1840
          }
1841
          if(isset($distribution->annotations[0])) {
1842
            $distribution_aggregate->annotations = array_merge($distribution_aggregate->annotations,
1843
              $distribution->annotations);
1844 65345976 Andreas Kohlbecker
          }
1845
        }
1846 29857cb0 Andreas Kohlbecker
      }
1847 65345976 Andreas Kohlbecker
1848 9e2aa1ff Andreas Kohlbecker
      $annotations_and_sources =  null;
1849 65345976 Andreas Kohlbecker
      if($distribution_aggregate) {
1850
        $annotations_and_sources = handle_annotations_and_sources(
1851
          $distribution_aggregate,
1852 0c2b9b9d Andreas Kohlbecker
          handle_annotations_and_sources_config($feature_block_settings),
1853 65345976 Andreas Kohlbecker
          $label,
1854
          UUID_DISTRIBUTION
1855
        );
1856
1857 29857cb0 Andreas Kohlbecker
        $status = distribution_status_label_and_markup($distribution_aggregate->status, $level_style['status_glue']);
1858
      }
1859 65345976 Andreas Kohlbecker
1860
      $per_node_markup[$node_index] .= '<' . $enclosingTag . ' class="descriptionElement'
1861
        . join(' descriptionElement-', $distribution_uuids)
1862
        . ' level_index_' . $level_index
1863 c797d7db Andreas Kohlbecker
        . ' " title="' . $status['label'] . '">'
1864 65345976 Andreas Kohlbecker
        . '<span class="area_label">' . $label
1865 38dd933d Andreas Kohlbecker
        . $level_style['label_suffix'] . '</span>'
1866 c797d7db Andreas Kohlbecker
        . $status['markup']
1867 65345976 Andreas Kohlbecker
      ;
1868
1869 97ac3d38 Andreas Kohlbecker
      if(isset($annotations_and_sources)){
1870 65345976 Andreas Kohlbecker
        if(!empty($annotations_and_sources['source_references'])){
1871 97ac3d38 Andreas Kohlbecker
          $per_node_markup[$node_index] .= ' ' . join(', ' , $annotations_and_sources['source_references']);
1872 65345976 Andreas Kohlbecker
        }
1873
        if($annotations_and_sources['foot_note_keys']) {
1874
          $per_node_markup[$node_index] .= $annotations_and_sources['foot_note_keys'];
1875
        }
1876
      }
1877
1878
      if(isset($node->children[0])){
1879
        _compose_distribution_hierarchy(
1880
          $node->children,
1881
          $feature_block_settings,
1882
          $per_node_markup[$node_index],
1883
          $hierarchy_style,
1884
          $level_index
1885
        );
1886
      }
1887
1888
      $per_node_markup[$node_index] .= '</' . $enclosingTag . '>';
1889
    }
1890 38dd933d Andreas Kohlbecker
    $markup .= $level_style['item_group_prefix']  . join( $level_style['item_glue'], $per_node_markup) . $level_style['item_group_postfix'];
1891 65345976 Andreas Kohlbecker
  }
1892
1893
1894 4295d33d Andreas Kohlbecker
/**
1895
 * Provides the content for a block of Uses Descriptions for a given taxon.
1896
 *
1897
 * Fetches the list of TaxonDescriptions tagged with the MARKERTYPE_USE
1898
 * and passes them to the theme function theme_cdm_UseDescription().
1899
 *
1900
 * @param string $taxon_uuid
1901
 *   The uuid of the Taxon
1902
 *
1903
 * @return array
1904
 *   A drupal render array
1905
 */
1906 1b756c5f Andreas Kohlbecker
function cdm_block_use_description_content($taxon_uuid, $feature) {
1907 4295d33d Andreas Kohlbecker
1908 1b756c5f Andreas Kohlbecker
  $use_description_content = array();
1909 4295d33d Andreas Kohlbecker
1910
  if (is_uuid($taxon_uuid )) {
1911
    $markerTypes = array();
1912
    $markerTypes['markerTypes'] = UUID_MARKERTYPE_USE;
1913
    $useDescriptions = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXON . '/' . $taxon_uuid . '/descriptions', $markerTypes);
1914
    if (!empty($useDescriptions)) {
1915 1b756c5f Andreas Kohlbecker
      $use_description_content = compose_feature_block_items_use_records($useDescriptions, $taxon_uuid, $feature);
1916 4295d33d Andreas Kohlbecker
    }
1917
  }
1918
1919 1b756c5f Andreas Kohlbecker
  return $use_description_content;
1920
}
1921
1922
/**
1923
 * Creates a trunk of a feature object which can be used to build pseudo feature blocks like the Bibliography.
1924
 *
1925
 * @param $representation_L10n
1926
 * @param String $pseudo_feature_key
1927
 *    Will be set as uuid but should be one of 'BIBLIOGRAPHY', ... more to come. See also get_feature_block_settings()
1928
 *
1929
 * @return object
1930
 *  The feature object
1931
 */
1932
function make_pseudo_feature($representation_L10n, $pseudo_feature_key = null){
1933
  $feature = new stdClass;
1934
  $feature->representation_L10n = $representation_L10n;
1935 d43a3486 Andreas Kohlbecker
  $feature->uuid = NULL; // $pseudo_feature_key;
1936
  $feature->label = $pseudo_feature_key;
1937 9e7ae433 Andreas Kohlbecker
  $feature->class = 'PseudoFeature';
1938 1b756c5f Andreas Kohlbecker
1939
  return $feature;
1940
1941 4295d33d Andreas Kohlbecker
}
1942
1943 9b434f6a Andreas Kohlbecker
/**
1944 2527a83c Andreas Kohlbecker
 * @param $root_nodes, for obtaining the  root nodes from a description you can
1945
 * use the function get_root_nodes_for_dataset($description);
1946 9b434f6a Andreas Kohlbecker
 *
1947
 * @return string
1948
 */
1949 2527a83c Andreas Kohlbecker
function render_description_string($root_nodes, &$item_cnt = 0) {
1950 caf1ed73 Andreas Kohlbecker
1951
  $out = '';
1952
1953
  $description_strings= [];
1954 9b434f6a Andreas Kohlbecker
  if (!empty($root_nodes)) {
1955
    foreach ($root_nodes as $root_node) {
1956 06d7ad52 Patrick Plitzner
      if(isset($root_node->descriptionElements)) {
1957
        foreach ($root_node->descriptionElements as $element) {
1958 2bc2b13e Andreas Kohlbecker
          $feature_label = $element->feature->representation_L10n;
1959
          if($item_cnt == 0){
1960
            $feature_label = ucfirst($feature_label);
1961
          }
1962 06d7ad52 Patrick Plitzner
          switch ($element->class) {
1963
            case 'CategoricalData':
1964 f63479ae Andreas Kohlbecker
              $state_data = render_state_data($element);
1965 06d7ad52 Patrick Plitzner
              if (!empty($state_data)) {
1966 834f548a Andreas Kohlbecker
                if(is_suppress_state_present_display($element, $root_node)){
1967
                  $description_strings[] = '<span class="'. html_class_attribute_ref($element) .'"><span class="label">' . $feature_label . '</span>: '  . '</span>';
1968
                } else {
1969
                  $description_strings[] = '<span class="'. html_class_attribute_ref($element) .'"><span class="label">' . $feature_label . '</span>: ' . $state_data . '</span>;' ;
1970
                }
1971 06d7ad52 Patrick Plitzner
              }
1972
              break;
1973
            case 'QuantitativeData':
1974 834f548a Andreas Kohlbecker
              $description_strings[] = '<span class="'. html_class_attribute_ref($element) .'"><span class="label">' . $feature_label . '</span>: ' . render_quantitative_statistics($element) . '</span>;';
1975 06d7ad52 Patrick Plitzner
              break;
1976
          }
1977 9b434f6a Andreas Kohlbecker
        }
1978 2bc2b13e Andreas Kohlbecker
        $item_cnt++;
1979 9b434f6a Andreas Kohlbecker
      }
1980 caf1ed73 Andreas Kohlbecker
1981 9b434f6a Andreas Kohlbecker
      // recurse child nodes
1982 2527a83c Andreas Kohlbecker
      $child_markup = render_description_string($root_node->childNodes, $item_cnt);
1983 caf1ed73 Andreas Kohlbecker
      if($child_markup){
1984 16ab19f1 Andreas Kohlbecker
        $description_strings[] = $child_markup;
1985 caf1ed73 Andreas Kohlbecker
      }
1986 9b434f6a Andreas Kohlbecker
    }
1987 834f548a Andreas Kohlbecker
    if(count($description_strings) > 0){
1988
      // remove last semicolon
1989
      $description_strings[count($description_strings) - 1] = preg_replace('/;$/', '', $description_strings[count($description_strings) - 1]);
1990
    }
1991
    $out  = join($description_strings,  ' ');
1992 9b434f6a Andreas Kohlbecker
  }
1993 caf1ed73 Andreas Kohlbecker
  return $out;
1994 9b434f6a Andreas Kohlbecker
}
1995
1996 c7d8a579 Andreas Kohlbecker
/**
1997
 * Compose a description as a table of Feature<->State
1998
 *
1999
 * @param $description_uuid
2000
 *
2001
 * @return array
2002
 *    The drupal render array for the page
2003
 *
2004
 * @ingroup compose
2005
 */
2006 20bfe15b Andreas Kohlbecker
function  compose_description_table($description_uuid, $descriptive_dataset_uuid = NULL) {
2007
2008 c7d8a579 Andreas Kohlbecker
  RenderHints::pushToRenderStack('description_table');
2009
2010
  $render_array = [];
2011
2012
  $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, [$description_uuid]);
2013
  $dataSet = NULL;
2014
  // dataset passed as parameter
2015
  if ($descriptive_dataset_uuid != NULL) {
2016
    foreach ($description->descriptiveDataSets as $set) {
2017
      if ($set->uuid == $descriptive_dataset_uuid) {
2018
        $dataSet = $set;
2019
        break;
2020
      }
2021
    }
2022
  }
2023 20bfe15b Andreas Kohlbecker
2024 c7d8a579 Andreas Kohlbecker
  if(!empty($description->descriptiveDataSets)) {
2025
    // only one dataset present
2026
    if (!isset($dataSet) && sizeof($description->descriptiveDataSets) == 1) {
2027
      foreach ($description->descriptiveDataSets as $dataSet) {
2028
        break;
2029
      }
2030
    }
2031 20bfe15b Andreas Kohlbecker
2032
    // generate description title
2033
    RenderHints::pushToRenderStack('title');
2034 c7d8a579 Andreas Kohlbecker
    if (isset($dataSet)) {
2035 20bfe15b Andreas Kohlbecker
2036
      $described_entity_title = NULL;
2037
      if(isset($description->describedSpecimenOrObservation)){
2038
        $described_entity_title = $description->describedSpecimenOrObservation->titleCache;
2039
      } else if($description->taxon) {
2040
          $described_entity_title = render_taxon_or_name($description->taxon);
2041
      }
2042
      $title = 'Descriptive Data ' . $dataSet->titleCache .
2043
        ($described_entity_title ? ' for ' . $described_entity_title : '');
2044 753a8083 Andreas Kohlbecker
    }
2045
    $render_array['title'] = markup_to_render_array($title, null, '<h3 class="title">', '</h3>');
2046 20bfe15b Andreas Kohlbecker
    RenderHints::popFromRenderStack();
2047 753a8083 Andreas Kohlbecker
    // END of --- generate description title
2048 20bfe15b Andreas Kohlbecker
2049 753a8083 Andreas Kohlbecker
    if (isset($description->types)) {
2050
      foreach ($description->types as $type) {
2051
        if ($type == 'CLONE_FOR_SOURCE') {
2052
          $render_array['source'] = markup_to_render_array("Aggregation source from " . $description->created, null, '<div class="date-created">', '</div>');
2053
          break;
2054 c7d8a579 Andreas Kohlbecker
        }
2055
      }
2056
    }
2057 753a8083 Andreas Kohlbecker
  }
2058
  // multiple datasets present see #8714 "Show multiple datasets per description as list of links"
2059
  else {
2060
    $items = [];
2061
    foreach ($description->descriptiveDataSets as $dataSet) {
2062
      $path = path_to_description($description->uuid, $dataSet->uuid);
2063
      $attributes['class'][] = html_class_attribute_ref($description);
2064
      $items[] = [
2065
        'data' => $dataSet->titleCache . icon_link($path),
2066 c7d8a579 Andreas Kohlbecker
      ];
2067
    }
2068 753a8083 Andreas Kohlbecker
    $render_array['description_elements'] = [
2069
      '#title' => 'Available data sets for description',
2070
      '#theme' => 'item_list',
2071
      '#type' => 'ul',
2072
      '#items' => $items,
2073
    ];
2074
  }
2075 c7d8a579 Andreas Kohlbecker
2076 3db61e83 Andreas Kohlbecker
  $described_entities = [];
2077 c7d8a579 Andreas Kohlbecker
  if (isset($description->describedSpecimenOrObservation)) {
2078 3db61e83 Andreas Kohlbecker
    $decr_entitiy = '<span class="label">Specimen:</span> ' . render_cdm_specimen_link($description->describedSpecimenOrObservation);
2079
    $described_entities['specimen'] = markup_to_render_array($decr_entitiy, null, '<div>', '</div>');
2080 c7d8a579 Andreas Kohlbecker
  }
2081
  if (isset($description->taxon)) {
2082 3db61e83 Andreas Kohlbecker
    $decr_entitiy = '<span class="label">Taxon:</span> ' . render_taxon_or_name($description->taxon, url(path_to_taxon($description->taxon->uuid)));
2083
    $described_entities['taxon'] = markup_to_render_array($decr_entitiy, null, '<div>', '</div>');
2084
  }
2085
2086
  if(count($described_entities)){
2087
    $render_array['described_entities'] = $described_entities;
2088
    $render_array['described_entities']['#prefix'] = '<div class="described-entities">';
2089
    $render_array['described_entities']['#suffix'] = '</div>';
2090 c7d8a579 Andreas Kohlbecker
  }
2091
2092 3db61e83 Andreas Kohlbecker
2093 c7d8a579 Andreas Kohlbecker
  $root_nodes = get_root_nodes_for_dataset($description);
2094
2095
2096
  $rows = [];
2097 209feb13 Andreas Kohlbecker
  $rows = description_element_table_rows($root_nodes, $rows);
2098 c7d8a579 Andreas Kohlbecker
2099 0e414cc1 Andreas Kohlbecker
  // --- create headers
2100 dfd71699 Andreas Kohlbecker
  $header = [0 => [], 1 => []];
2101 0e414cc1 Andreas Kohlbecker
2102
  foreach($rows as $row) {
2103 dfd71699 Andreas Kohlbecker
    if(array_search('Character', $row['class']) && array_search('Character', $header[0]) === false){
2104
      $header[0][] = 'Character';
2105
    } elseif (array_search('Feature', $row['class']) && array_search('Feature', $header[0]) === false){
2106
      $header[0][] = 'Feature';
2107 0e414cc1 Andreas Kohlbecker
    }
2108 dfd71699 Andreas Kohlbecker
    if(array_search('has_state', $row['class']) && array_search('States', $header[1]) === false){
2109
      $header[1][] = 'States';
2110
    } elseif (array_search('has_values', $row['class']) && array_search('Values', $header[1]) === false){
2111
      $header[1][] = 'Values';
2112 0e414cc1 Andreas Kohlbecker
    }
2113
  }
2114 dfd71699 Andreas Kohlbecker
  asort($header[0]);
2115 0e414cc1 Andreas Kohlbecker
  asort($header[1]);
2116 dfd71699 Andreas Kohlbecker
  $header[0] = join('/', $header[0]);
2117 0e414cc1 Andreas Kohlbecker
  $header[1] = join('/', $header[1]);
2118
2119
  // ---
2120
2121 c7d8a579 Andreas Kohlbecker
  if (!empty($rows)) {
2122 3db61e83 Andreas Kohlbecker
    $render_array['table'] = markup_to_render_array(theme('table', [
2123 c7d8a579 Andreas Kohlbecker
      'header' => $header,
2124
      'rows' => $rows,
2125 cd3ea2e8 Andreas Kohlbecker
      'caption' => statistical_values_explanation(),
2126 6722d9a5 Andreas Kohlbecker
      'title' => "Table"
2127 c7d8a579 Andreas Kohlbecker
    ]));
2128
  }
2129
2130 753a8083 Andreas Kohlbecker
  // --- sources
2131 c7d8a579 Andreas Kohlbecker
  if (isset($description->sources) and !empty($description->sources)) {
2132
    $items = [];
2133
    foreach ($description->sources as $source) {
2134
      if ($source->type == 'Aggregation' and isset($source->cdmSource)){
2135 fec3fd41 Andreas Kohlbecker
        $cdm_source_entity = $source->cdmSource;
2136
        switch($cdm_source_entity->class){
2137
          case 'Taxon':
2138 cbe9b085 Andreas Kohlbecker
            $source_link_markup = render_taxon_or_name($cdm_source_entity) . icon_link(path_to_taxon($cdm_source_entity->uuid, false));
2139 fec3fd41 Andreas Kohlbecker
            break;
2140
          case 'TaxonDescription':
2141
          case 'NameDescription':
2142 3db61e83 Andreas Kohlbecker
          case 'SpecimenDescription':
2143 92e522db Andreas Kohlbecker
            $source_link_markup = render_cdm_entity_link($cdm_source_entity);
2144 fec3fd41 Andreas Kohlbecker
            break;
2145
          default:
2146 2d78d276 Andreas Kohlbecker
            $source_link_markup = '<span class="error">Unhandled CdmSource</span>';
2147 fec3fd41 Andreas Kohlbecker
        }
2148 92e522db Andreas Kohlbecker
        $items[$cdm_source_entity->titleCache] = [
2149 fec3fd41 Andreas Kohlbecker
          'data' => $source_link_markup
2150 c7d8a579 Andreas Kohlbecker
        ];
2151
      }
2152
    }
2153 92e522db Andreas Kohlbecker
    ksort($items);
2154 3db61e83 Andreas Kohlbecker
    $render_array['sources'] = [
2155 c7d8a579 Andreas Kohlbecker
      '#title' => 'Sources',
2156
      '#theme' => 'item_list',
2157
      '#type' => 'ul',
2158
      '#items' => $items,
2159 3db61e83 Andreas Kohlbecker
      '#attributes' => ['class' => 'sources']
2160 c7d8a579 Andreas Kohlbecker
    ];
2161 3db61e83 Andreas Kohlbecker
    $render_array['#prefix'] = '<div class="description-table">';
2162
    $render_array['#suffix'] = '</div>';
2163 c7d8a579 Andreas Kohlbecker
  }
2164
2165
  RenderHints::popFromRenderStack();
2166 20bfe15b Andreas Kohlbecker
2167 c7d8a579 Andreas Kohlbecker
  return $render_array;
2168
}
2169
2170
/**
2171
 * For a given description returns the root nodes according to the
2172
 *corresponding term tree. The term tree is determined as follow:
2173
 * 1. If description is part of a descriptive data set the term tree of that
2174
 *    data set is used (FIXME handle multiple term trees)
2175
 * 2. Otherwise the portal taxon profile tree is used
2176
 * @param $description
2177
 *
2178
 * @return array
2179
 */
2180
function get_root_nodes_for_dataset($description) {
2181
  if (!empty($description->descriptiveDataSets)) {
2182
    foreach ($description->descriptiveDataSets as $dataSet) {
2183
      break;// FIXME handle multiple term trees
2184
    }
2185
    $tree = cdm_ws_get(CDM_WS_TERMTREE, $dataSet->descriptiveSystem->uuid);
2186
    $root_nodes = _mergeFeatureTreeDescriptions($tree->root->childNodes, $description->elements);
2187
  }
2188
  else {
2189
    $root_nodes = _mergeFeatureTreeDescriptions(get_profile_feature_tree()->root->childNodes, $description->elements);
2190
  }
2191
  return $root_nodes;
2192
}
2193
2194
/**
2195 0e414cc1 Andreas Kohlbecker
 * Recursively creates an array of row items to be used in theme_table.
2196
 *
2197
 * The array items will have am element 'class' with information on the
2198
 * nature of the DescriptionElement ('has_values' | 'has_state') and on the
2199
 * type of the FeatureNode ('Feature' | 'Character')
2200 209feb13 Andreas Kohlbecker
 *
2201
 * @param array $root_nodes
2202
 * @param array $row_items
2203
 * @param int $level
2204
 *     the depth in the hierarchy
2205 c7d8a579 Andreas Kohlbecker
 *
2206
 * @return array
2207 209feb13 Andreas Kohlbecker
 *  An array of row items to be used in theme_table
2208
 *
2209
 *
2210 c7d8a579 Andreas Kohlbecker
 */
2211 209feb13 Andreas Kohlbecker
function description_element_table_rows($root_nodes, $row_items, $level = 0) {
2212 0e414cc1 Andreas Kohlbecker
2213 209feb13 Andreas Kohlbecker
  $indent_string = '&nbsp;&nbsp;&nbsp;';
2214 c7d8a579 Andreas Kohlbecker
  foreach ($root_nodes as $root_node) {
2215
    if(isset($root_node->descriptionElements)) {
2216
      foreach ($root_node->descriptionElements as $element) {
2217 209feb13 Andreas Kohlbecker
        $level_indent = str_pad('', $level * strlen($indent_string), $indent_string);
2218 c7d8a579 Andreas Kohlbecker
        switch ($element->class) {
2219 0e414cc1 Andreas Kohlbecker
          case 'QuantitativeData':
2220
            $row_items[] = [
2221
              'data' => [
2222
                [
2223
                  'data' => markup_to_render_array($level_indent . $element->feature->representation_L10n),
2224
                  'class' => ['level_' . $level]
2225
                ],
2226
                render_quantitative_statistics($element)
2227
              ],
2228
              'class' => ['level_' . $level, 'has_values', $element->feature->class]
2229
            ];
2230
            break;
2231 c7d8a579 Andreas Kohlbecker
          case 'CategoricalData':
2232 0e414cc1 Andreas Kohlbecker
            default:
2233 c7d8a579 Andreas Kohlbecker
            if (!empty($element->stateData)) {
2234 834f548a Andreas Kohlbecker
              $supress_state_display = is_suppress_state_present_display($element, $root_node);
2235 358e0b7f Andreas Kohlbecker
              if(!$supress_state_display){
2236
                $state_cell = render_state_data($element);
2237
              } else {
2238
                $state_cell = "<span> </span>";
2239
              }
2240 209feb13 Andreas Kohlbecker
              $row_items[] = [
2241
                'data' => [
2242
                  [
2243
                    'data' => markup_to_render_array($level_indent . $element->feature->representation_L10n),
2244 88e622d2 Andreas Kohlbecker
                    'class' => ['level_' . $level]
2245 209feb13 Andreas Kohlbecker
                  ],
2246 358e0b7f Andreas Kohlbecker
                  $state_cell,
2247 209feb13 Andreas Kohlbecker
                ],
2248 0e414cc1 Andreas Kohlbecker
                'class' => ['level_' . $level, 'has_state', $element->feature->class]
2249 c7d8a579 Andreas Kohlbecker
              ];
2250
            }
2251
            break;
2252
        }
2253
      }
2254
    }
2255
    // recurse child nodes
2256 209feb13 Andreas Kohlbecker
    $row_items = description_element_table_rows($root_node->childNodes, $row_items, $level + 1);
2257 c7d8a579 Andreas Kohlbecker
  }
2258 209feb13 Andreas Kohlbecker
  return $row_items;
2259 c7d8a579 Andreas Kohlbecker
}
2260
2261 834f548a Andreas Kohlbecker
/**
2262
 * @param $element
2263
 * @param $root_node
2264
 *
2265
 * @return bool
2266
 */
2267
function is_suppress_state_present_display($element, $root_node) {
2268
  return count($element->stateData) == 1 & $element->stateData[0]->state->representation_L10n == 'present' && is_array($root_node->childNodes);
2269
}
2270
2271 c7d8a579 Andreas Kohlbecker