Project

General

Profile

Download (64.6 KB) Statistics
| Branch: | Tag: | Revision:
1 aa7c7290 Andreas Kohlbecker
<?php
2
3 ce29c528 Andreas Kohlbecker
/**
4
 * Returns the localized representations of the modifiers hold by the
5
 * supplied cdm instance concatenated into one string.
6
 *
7
 * @param object $iModifieable
8
 *   cdm instance of an class implementing the interface IModifieable:
9
 *   DescriptionElementBase, StateDate, State
10
 *
11
 * @return String
12
 *   localized representations of the modifiers hold by the
13
 *   supplied cdm instance concatenated into one string
14
 */
15
function cdm_modifers_representations($iModifieable, $glue = ', ') {
16
  $modifiers_strings = array();
17
  if (isset($iModifieable->modifiers)) {
18
    foreach ($iModifieable->modifiers as $modifier) {
19
      $modifiers_strings[] = cdm_term_representation($modifier);
20 f19f47fa Andreas Kohlbecker
    }
21 aa7c7290 Andreas Kohlbecker
  }
22 ce29c528 Andreas Kohlbecker
  return implode(', ', $modifiers_strings);
23
}
24 bf97c2e8 Andreas Kohlbecker
25 ce29c528 Andreas Kohlbecker
/**
26
 * Filters the given set of description elements and prefers computed elements over others.
27
 *
28
 * Computed description elements
29
 * are identified by the MarkerType.COMPUTED()
30
 *
31
 * If the given set contains at least one computed element only
32
 * the computed elements are returned.
33
 *
34
 * @param array $description_elements
35
 *   An array of CDM DescriptionElementBase instances
36
 *
37
 * @return array
38
 *   only the computed description elements otherwise all others.
39
 *
40
 * @deprecated this is replaced by the cdmlib DistributionUtil class!!!
41
 */
42
function cdm_description_elements_prefer_computed($description_elements){
43 19e097a3 Andreas Kohlbecker
44 ce29c528 Andreas Kohlbecker
  $computed_elements = array();
45
  $other_elements = array();
46 19e097a3 Andreas Kohlbecker
47 ce29c528 Andreas Kohlbecker
  if (!empty($description_elements)) {
48
    foreach ($description_elements as $element) {
49
      if (cdm_entity_has_marker($element, UUID_MARKERTYPE_COMPUTED)) {
50
        $computed_elements[] = $element;
51
      }
52
      else {
53
        $other_elements[] = $element;
54 19e097a3 Andreas Kohlbecker
      }
55
    }
56 ce29c528 Andreas Kohlbecker
  }
57 19e097a3 Andreas Kohlbecker
58 ce29c528 Andreas Kohlbecker
  if (count($computed_elements) > 0) {
59
    return $computed_elements;
60
  }
61
  else {
62
    return $other_elements;
63 19e097a3 Andreas Kohlbecker
  }
64 ce29c528 Andreas Kohlbecker
}
65 ae0a5060 Andreas Kohlbecker
66 ce29c528 Andreas Kohlbecker
/**
67
 * Creates a query parameter array based on the setting stored in the drupal variable CDM_DISTRIBUTION_FILTER.
68
 *
69
 * @return array
70
 *   An array with distribution filter query parameters
71
 */
72
function cdm_distribution_filter_query() {
73
  $cdm_distribution_filter = get_array_variable_merged(CDM_DISTRIBUTION_FILTER, CDM_DISTRIBUTION_FILTER_DEFAULT);
74
  $query = array();
75 aa63dfb4 Andreas Kohlbecker
76 ce29c528 Andreas Kohlbecker
  if ($cdm_distribution_filter['filter_rules']['statusOrderPreference']) {
77
    $query['statusOrderPreference'] = 1;
78
  }
79
  if ($cdm_distribution_filter['filter_rules']['subAreaPreference']) {
80
    $query['subAreaPreference'] = 1;
81
  }
82 6fbf1bd3 Andreas Kohlbecker
  if (is_array($cdm_distribution_filter['hiddenAreaMarkerType']) && count($cdm_distribution_filter['hiddenAreaMarkerType']) > 0) {
83
    $query['hiddenAreaMarkerType'] = '';
84
    foreach ($cdm_distribution_filter['hiddenAreaMarkerType'] as $marker_type => $enabled) {
85 ce29c528 Andreas Kohlbecker
      if ($enabled) {
86 6fbf1bd3 Andreas Kohlbecker
        $query['hiddenAreaMarkerType'] .= ($query['hiddenAreaMarkerType'] ? ',' : '') . $marker_type;
87 ce29c528 Andreas Kohlbecker
      }
88 dd1c109a Andreas Kohlbecker
    }
89 aa63dfb4 Andreas Kohlbecker
  }
90
91 ce29c528 Andreas Kohlbecker
  return $query;
92
}
93 aa63dfb4 Andreas Kohlbecker
94 ce29c528 Andreas Kohlbecker
/**
95
 * Merge the fields 'annotations', 'markers', 'sources', 'media' from the source CDM DescriptionElement into  the target.
96
 *
97
 * @param object $target
98
 *   The source CDM DescriptionElement
99
 * @param object $source
100
 *   The target CDM DescriptionElement
101
 */
102
function cdm_merge_description_elements(&$target, &$source) {
103
  static $fields_to_merge = array('annotations', 'markers', 'sources', 'media');
104
105
  foreach ($fields_to_merge as $field) {
106
    if (is_array($source->$field)) {
107
      if (!is_array($target->$field)) {
108
        $target->$field = $source->$field;
109
      }
110
      else {
111
        $target->$field = array_merge($target->$field, $source->$field);
112 ae0a5060 Andreas Kohlbecker
      }
113
    }
114
  }
115 ce29c528 Andreas Kohlbecker
}
116 bf97c2e8 Andreas Kohlbecker
117 ce29c528 Andreas Kohlbecker
/**
118
 * Adds an entry to the end of the table of content items list
119
 *
120
 * The  table of content items are crated internally by calling
121
 * toc_list_item() the resulting item is added to the statically cached
122
 * list of toc elements
123
 *
124
 * @param string $label
125
 *   The label of toc entry
126
 * @param $class_attribute_suffix
127
 *   The suffix to be appended to the class attribute prefix: "feature-toc-item-"
128
 * @param string $fragment
129
 *   Optional parameter to define a url fragment different from the $label,
130
 *   if the $fragment is not defined the $label will be used
131
 */
132
function cdm_toc_list_add_item($label, $class_attribute_suffix, $fragment = NULL, $as_first_element = FALSE) {
133
  $toc_list_items = &cdm_toc_list();
134 dd1c109a Andreas Kohlbecker
135 ce29c528 Andreas Kohlbecker
  if (!$fragment) {
136
    $fragment = $label;
137
  }
138
  $fragment = generalizeString($fragment);
139 dd1c109a Andreas Kohlbecker
140 ce29c528 Andreas Kohlbecker
  $class_attributes = 'feature-toc-item-' . $class_attribute_suffix;
141 dd1c109a Andreas Kohlbecker
142 ce29c528 Andreas Kohlbecker
  $new_item = toc_list_item(
143
    theme(
144
      'cdm_feature_name',
145
      array('feature_name' => $label)),
146 dd1c109a Andreas Kohlbecker
      array('class' => $class_attributes),
147
      $fragment
148
    );
149
150 ce29c528 Andreas Kohlbecker
  if ($as_first_element) {
151
    array_unshift($toc_list_items, $new_item);
152
  }
153
  else {
154
    $toc_list_items[] = $new_item;
155 bf97c2e8 Andreas Kohlbecker
  }
156
157 ce29c528 Andreas Kohlbecker
}
158 bf97c2e8 Andreas Kohlbecker
159 ce29c528 Andreas Kohlbecker
/**
160
 * Returns the statically cached table of content items as render array.
161
 *
162
 * @see cdm_toc_list_add_item()
163
 *
164
 * @return array
165
 *   a render array of table of content items suitable for theme_item_list()
166
 */
167
function &cdm_toc_list(){
168
  $toc_list_items = &drupal_static('toc_list_items', array());
169
170
  return $toc_list_items;
171
}
172 1f11e206 Andreas Kohlbecker
173 f19f47fa Andreas Kohlbecker
/**
174
 * Prepares an empty Drupal block for displaying description elements of a specific CDM Feature.
175
 *
176
 * The block can also be used for pseudo Features like a bibliography. Pseudo features are
177
 * derived from other data on the fly and so not exist as such in the cdm data. In case
178
 * of pseudo features the $feature is left empty
179
 *
180
 * @param $feature_name
181 ce29c528 Andreas Kohlbecker
 *   A label describing the feature, usually the localized feature representation.
182
 * @param object $feature
183
 *   The CDM Feature for which the block is created. (optional)
184 f19f47fa Andreas Kohlbecker
 * @return object
185 ce29c528 Andreas Kohlbecker
 *   A Drupal block object
186 f19f47fa Andreas Kohlbecker
 */
187 ce29c528 Andreas Kohlbecker
function feature_block($feature_name, $feature = NULL) {
188 f19f47fa Andreas Kohlbecker
  $block = new stdclass(); // Empty object.
189
  $block->module = 'cdm_dataportal';
190
  $block->delta = generalizeString($feature_name);
191 ce29c528 Andreas Kohlbecker
  $block->region = NULL;
192 f19f47fa Andreas Kohlbecker
  $block->subject = '<a name="' . $block->delta . '"></a><span class="' . html_class_attribute_ref($feature) . '">'
193
    . theme('cdm_feature_name', array('feature_name' => $feature_name))
194
    . '</span>';
195
  $block->module = "cdm_dataportal-feature";
196
  $block->content = '';
197
  return $block;
198
}
199
200 dd1c109a Andreas Kohlbecker
201 ce29c528 Andreas Kohlbecker
/**
202
 * Returns a list of a specific type of IdentificationKeys.
203
 *
204
 * The list can be restricted by a taxon.
205
 *
206
 * @param string $type
207
 *   The simple name of the cdm class implementing the interface
208
 *   IdentificationKey, valid values are:
209
 *   PolytomousKey, MediaKey, MultiAccessKey.
210
 * @param string $taxonUuid
211
 *   If given this parameter restrict the listed keys to those which have
212
 *   the taxon identified be this uuid in scope.
213
 *
214
 * @return array
215
 *   List with identification keys.
216
 */
217
function _list_IdentificationKeys($type, $taxonUuid = NULL, $pageSize = NULL, $pageNumber = NULL) {
218
  if (!$type) {
219
    drupal_set_message(t('Type parameter is missing'), 'error');
220
    return;
221
  }
222
  $cdm_ws_pasepath = NULL;
223
  switch ($type) {
224
    case "PolytomousKey":
225
      $cdm_ws_pasepath = CDM_WS_POLYTOMOUSKEY;
226
      break;
227
228
    case "MediaKey":
229
      $cdm_ws_pasepath = CDM_WS_MEDIAKEY;
230
      break;
231 dd1c109a Andreas Kohlbecker
232 ce29c528 Andreas Kohlbecker
    case "MultiAccessKey":
233
      $cdm_ws_pasepath = CDM_WS_MULTIACCESSKEY;
234
      break;
235
236
  }
237
238
  if (!$cdm_ws_pasepath) {
239
    drupal_set_message(t('Type parameter is not valid: ') . $type, 'error');
240
  }
241
242
  $queryParameters = '';
243
  if (is_numeric($pageSize)) {
244
    $queryParameters = "pageSize=" . $pageSize;
245
  }
246
  else {
247
    $queryParameters = "pageSize=0";
248
  }
249
250
  if (is_numeric($pageNumber)) {
251
    $queryParameters = "pageNumber=" . $pageNumber;
252
  }
253
  else {
254
    $queryParameters = "pageNumber=0";
255
  }
256
  $queryParameters = NULL;
257
  if ($taxonUuid) {
258
    $queryParameters = "findByTaxonomicScope=$taxonUuid";
259
  }
260
  $pager = cdm_ws_get($cdm_ws_pasepath, NULL, $queryParameters);
261
262
  if (!$pager || $pager->count == 0) {
263
    return array();
264
  }
265
  return $pager->records;
266
}
267
268
269
/**
270
 * Creates a list item for a table of content, suitable as data element for a themed list
271
 *
272
 * @see theme_list()
273
 *
274
 * @param $label
275
 * @param $http_request_params
276
 * @param $attributes
277
 * @return array
278
 */
279
function toc_list_item($label, $attributes = array(), $fragment = null) {
280
281
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
282
  $http_request_params = drupal_static('http_request_params', drupal_get_query_parameters());
283
284
  $item =  array(
285
    'data' => l(
286
      $label,
287
      $_GET['q'],
288
      array(
289
        'attributes' => array('class' => array('toc')),
290
        'fragment' => generalizeString($label),
291
        'query' => $http_request_params,
292 f19f47fa Andreas Kohlbecker
      )
293 ce29c528 Andreas Kohlbecker
    ),
294
  );
295
  $item['attributes'] = $attributes;
296
  return $item;
297
}
298
299
/**
300
 * Creates the footnotes for the given CDM DescriptionElement instance.
301
 *
302
 * Footnotes are created for annotations and original sources,
303
 * optionally the sources are put into a separate bibliography.
304
 *
305
 * @param $descriptionElement
306 2d0d855a Andreas Kohlbecker
 *   A CDM DescriptionElement instance
307 ce29c528 Andreas Kohlbecker
 * @param $separator
308 2d0d855a Andreas Kohlbecker
 *   Optional parameter. The separator string to concatenate the footnote ids, default is ','
309 ce29c528 Andreas Kohlbecker
 * @param $footnote_list_key_suggestion
310 2d0d855a Andreas Kohlbecker
 *   will be overridden for original sources if the bibliography block is enabled
311 ce29c528 Andreas Kohlbecker
 *
312
 * @return String
313 2d0d855a Andreas Kohlbecker
 *   The foot note keys
314 ce29c528 Andreas Kohlbecker
 */
315
function cdm_create_description_element_footnotes($description_element, $separator = ',',
316
          $footnote_list_key_suggestion = null, $do_link_to_reference = FALSE,
317
          $do_link_to_name_used_in_source = FALSE
318
    ){
319
320
321
  // Annotations as footnotes.
322
  $footNoteKeys = cdm_annotations_as_footnotekeys($description_element, $footnote_list_key_suggestion);
323
324
  // Source references as footnotes.
325
  $bibliography_settings = get_bibliography_settings();
326
  $original_source_footnote_tag = $bibliography_settings['enabled'] == 1 ? 'div' : null; // null will cause original_source_footnote_list_key to use the default
327
328 9e2aa1ff Andreas Kohlbecker
  usort($description_element->sources, 'compare_original_sources');
329 ce29c528 Andreas Kohlbecker
  foreach ($description_element->sources as $source) {
330
    if (_is_original_source_type($source)) {
331
      $fn_key = FootnoteManager::addNewFootnote(
332
        original_source_footnote_list_key($footnote_list_key_suggestion),
333
        theme('cdm_OriginalSource', array(
334
          'source' => $source,
335
          'doLink' => $do_link_to_reference,
336
          'do_link_to_name_used_in_source' => $do_link_to_name_used_in_source
337
338
        )),
339
        $original_source_footnote_tag
340
      );
341
      // Ensure uniqueness of the footnote keys.
342
      cdm_add_footnote_to_array($footNoteKeys, $fn_key);
343
    }
344
  }
345
  // Sort and render footnote keys.
346
  $footnoteKeyListStr = '';
347
  asort($footNoteKeys);
348
  foreach ($footNoteKeys as $footNoteKey) {
349
    $footnoteKeyListStr .= theme('cdm_footnote_key',
350
      array(
351
        'footnoteKey' => $footNoteKey,
352
        'separator' => ($footnoteKeyListStr ? $separator : '')));
353 dd1c109a Andreas Kohlbecker
  }
354 ce29c528 Andreas Kohlbecker
  return $footnoteKeyListStr;
355
}
356
357 f19f47fa Andreas Kohlbecker
358 f23ae4e3 Andreas Kohlbecker
/**
359
 * Compare callback to be used in usort() to sort render arrays produced by compose_description_element().
360
 *
361
 * @param $a
362
 * @param $b
363
 */
364
function compare_description_element_render_arrays($a, $b){
365
  if ($a['#value'].$a['#value-suffix'] == $b['#value'].$b['#value-suffix']) {
366
    return 0;
367
  }
368
  return ($a['#value'].$a['#value-suffix'] < $b['#value'].$b['#value-suffix']) ? -1 : 1;
369
370
}
371
372 dfc49afb Andreas Kohlbecker
373
/**
374
 * @param $render_array
375
 * @param $element
376
 * @param $feature_block_settings
377
 * @param $element_markup
378
 * @param $footnote_list_key_suggestion
379
 */
380 c651d3b1 Andreas Kohlbecker
function compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label = FALSE)
381 dfc49afb Andreas Kohlbecker
{
382 0ef9a709 Andreas Kohlbecker
383
  $render_array = array(
384
    '#type' => 'html_tag',
385
    '#tag' => cdm_feature_block_element_tag_name($feature_block_settings),
386
387
    '#attributes' => array(
388 f23ae4e3 Andreas Kohlbecker
      'class' => array(
389
        'DescriptionElement',
390
        'DescriptionElement-' . $element->class,
391
        html_class_attribute_ref($element)
392
      )
393 0ef9a709 Andreas Kohlbecker
    ),
394
395
    '#value' => '',
396
    '#value_suffix' => NULL
397
398
  );
399
400 dfc49afb Andreas Kohlbecker
  $annotations_and_sources = handle_annotations_and_sources($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion);
401
402 0ef9a709 Andreas Kohlbecker
  // handle the special case were the TextData is used as container for original source with name
403
  // used in source information without any text stored in it.
404 dfc49afb Andreas Kohlbecker
  $names_used_in_source_markup = '';
405
  if (!empty($annotations_and_sources['names_used_in_source']) && empty($element_markup)) {
406
    $names_used_in_source_markup = join(', ', $annotations_and_sources['names_used_in_source']) . ': ';
407 72c12d45 Andreas Kohlbecker
    // remove all <span class="nameUsedInSource">...</span> from all source_references
408
    // these are expected to be at the end of the strings
409
    $pattern = '/ <span class="nameUsedInSource">.*$/';
410
    foreach( $annotations_and_sources['source_references'] as &$source_reference){
411
      $source_reference = preg_replace($pattern , '', $source_reference);
412
    }
413 dfc49afb Andreas Kohlbecker
  }
414
415 72c12d45 Andreas Kohlbecker
416 dfc49afb Andreas Kohlbecker
  $source_references_markup = '';
417
  if (!empty($annotations_and_sources['source_references'])) {
418
    $source_references_markup = '<span class="sources">' . join(' ', $annotations_and_sources['source_references']) . '<span>';
419
  }
420
421 c651d3b1 Andreas Kohlbecker
  $feature_label = '';
422
  if ($prepend_feature_label) {
423
    $feature_label = '<span class="nested-feature-tree-feature-label">' . $element->feature->representation_L10n . ':</span> ';
424
  }
425
  $render_array['#value'] = $feature_label . $names_used_in_source_markup . $element_markup . $source_references_markup;
426 dfc49afb Andreas Kohlbecker
  $render_array['#value_suffix'] = $annotations_and_sources['foot_note_keys'];
427 0ef9a709 Andreas Kohlbecker
428
  return $render_array;
429 dfc49afb Andreas Kohlbecker
}
430
431
432 dd1c109a Andreas Kohlbecker
  /**
433 ce29c528 Andreas Kohlbecker
   * @param $element
434
   * @param $feature_block_settings
435
   * @param $element_text
436 0686f307 Andreas Kohlbecker
   *   used to decide if the source references should be enclosed in brackets or not
437 dd1c109a Andreas Kohlbecker
   * @param $footnote_list_key_suggestion
438 ce29c528 Andreas Kohlbecker
   * @return array
439
   *   an associative array with the following elements:
440
   *   - foot_note_keys: all footnote keys as markup
441
   *   - source_references: an array of the source references citations
442
   *   - names used in source: an associative array of the names in source,
443
   *        the name in source strings are de-duplicated
444
   *        !!!NOTE!!!!: this field will most probably be removed soon (TODO)
445
   *
446 44d445c0 Andreas Kohlbecker
   *
447 dd1c109a Andreas Kohlbecker
   */
448 ce29c528 Andreas Kohlbecker
  function handle_annotations_and_sources($element, $feature_block_settings, $element_text, $footnote_list_key_suggestion) {
449
    $annotations_and_sources = array(
450
      'foot_note_keys' => NULL,
451
      'source_references' => array(),
452
      'names_used_in_source' => array()
453
    );
454 dd1c109a Andreas Kohlbecker
455 9e2aa1ff Andreas Kohlbecker
    usort($element->sources, 'compare_original_sources');
456
457 ce29c528 Andreas Kohlbecker
    if ($feature_block_settings['sources_as_content'] == 1) {
458
      foreach ($element->sources as $source) {
459 dd1c109a Andreas Kohlbecker
460 ce29c528 Andreas Kohlbecker
        $referenceCitation = theme('cdm_OriginalSource',
461
          array(
462 dd1c109a Andreas Kohlbecker
            'source' => $source,
463 ce29c528 Andreas Kohlbecker
            'doLink' => $feature_block_settings['link_to_reference'] == 1,
464
            'do_link_to_name_used_in_source' => $feature_block_settings['link_to_name_used_in_source'] == 1,
465
          )
466
        );
467
468
        if ($referenceCitation) {
469
          if (empty($element_text)) {
470
            $annotations_and_sources['source_references'][] = $referenceCitation;
471
          }
472
          else {
473
            $annotations_and_sources['source_references'][] = ' (' . $referenceCitation . ')';
474
          }
475
        }
476 bb3188cb Andreas Kohlbecker
477 ce29c528 Andreas Kohlbecker
        $name_in_source_render_array = compose_name_in_source(
478
          $source,
479
          $feature_block_settings['link_to_name_used_in_source'] == 1
480 dd1c109a Andreas Kohlbecker
        );
481 ce29c528 Andreas Kohlbecker
482
        if(!empty($name_in_source_render_array)){
483
          $annotations_and_sources['names_used_in_source'][$name_in_source_render_array['#_plaintext']] = drupal_render($name_in_source_render_array);
484
        }
485
      } // END of loop over sources
486
487
      // annotations footnotes separate.
488
      $annotations_and_sources['foot_note_keys'] = theme('cdm_annotations_as_footnotekeys',
489 dd1c109a Andreas Kohlbecker
        array(
490 ce29c528 Andreas Kohlbecker
          'cdmBase_list' => $element,
491
          'footnote_list_key' => $footnote_list_key_suggestion,
492
        )
493
      );
494
495
    } // END of references inline
496
497
    // put sources into bibliography if requested ...
498
    if ($feature_block_settings['sources_as_content'] !== 1 || $feature_block_settings['sources_as_content_to_bibliography'] == 1) {
499
      $annotations_and_sources['foot_note_keys'] = cdm_create_description_element_footnotes(
500
        $element, ',',
501
        $footnote_list_key_suggestion,
502
        $feature_block_settings['link_to_reference'] == 1,
503
        $feature_block_settings['link_to_name_used_in_source'] == 1
504
      );
505 dd1c109a Andreas Kohlbecker
    }
506 ce29c528 Andreas Kohlbecker
507
    return $annotations_and_sources;
508 1f11e206 Andreas Kohlbecker
  }
509 aa63dfb4 Andreas Kohlbecker
510 ce29c528 Andreas Kohlbecker
511 dd1c109a Andreas Kohlbecker
  /**
512
   *
513 57d513cb Andreas Kohlbecker
   *
514
   * @return string
515
   *  the footnote_list_key
516 dd1c109a Andreas Kohlbecker
   */
517
  function original_source_footnote_list_key($key_suggestion = null) {
518
    if(!$key_suggestion){
519
      $key_suggestion = RenderHints::getFootnoteListKey();
520
    }
521
    $bibliography_settings = get_bibliography_settings();
522 57d513cb Andreas Kohlbecker
    $footnote_list_key = $bibliography_settings['enabled'] == 1 ? 'BIBLIOGRAPHY' : 'BIBLIOGRAPHY-' . $key_suggestion;
523 dd1c109a Andreas Kohlbecker
    return $footnote_list_key;
524 f19f47fa Andreas Kohlbecker
  }
525
526 08a339ec Andreas Kohlbecker
  /**
527
   * Provides the according tag name for the description element markup which fits the  $feature_block_settings['as_list'] value
528
   *
529
   * @param $feature_block_settings
530
   *   A feature_block_settings array, for details, please see get_feature_block_settings($feature_uuid = 'DEFAULT')
531
   */
532
  function cdm_feature_block_element_tag_name($feature_block_settings){
533
    switch ($feature_block_settings['as_list']){
534
      case 'ul':
535
      case 'ol':
536
        return 'li';
537
      case 'div':
538 f2e44165 Andreas Kohlbecker
        if(isset($feature_block_settings['element_tag'])){
539
          return $feature_block_settings['element_tag'];
540
        }
541 08a339ec Andreas Kohlbecker
        return 'span';
542
      case 'dl':
543
        return 'dd';
544
      default:
545
        return 'div'; // should never happen, throw error instead?
546
    }
547
  }
548 ce29c528 Andreas Kohlbecker
549
550
/* ==================== COMPOSE FUNCTIONS =============== */
551
552
  /**
553
   * Returns a set of feature blocks for a taxon profile from the $mergedFeatureNodes of a given $taxon.
554
   *
555
   * The taxon profile consists of drupal block elements, one for the description elements
556
   * of a specific feature. The structure is defined by specific FeatureTree.
557
   * The chosen FeatureTree is merged with the list of description elements prior to using this method.
558
   *
559
   * The merged nodes can be obtained by making use of the
560
   * function cdm_ws_descriptions_by_featuretree().
561
   *
562
   * @see cdm_ws_descriptions_by_featuretree()
563
   *
564
   * @param $mergedFeatureNodes
565
   *
566
   * @param $taxon
567
   *
568
   * @return array
569
   *  A Drupal render array containing feature blocks and the table of content
570
   *
571
   * @ingroup compose
572
   */
573 aa26f9f0 Andreas Kohlbecker
  function compose_feature_blocks($mergedFeatureNodes, $taxon) {
574 ce29c528 Andreas Kohlbecker
575
    $block_list = array();
576
577
    RenderHints::pushToRenderStack('feature_nodes');
578
579
    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME);
580
581
    // Create a drupal block for each feature
582
    foreach ($mergedFeatureNodes as $node) {
583
584
      if ((isset($node->descriptionElements['#type']) ||
585 c651d3b1 Andreas Kohlbecker
          has_feature_node_description_elements($node)) && $node->feature->uuid != UUID_IMAGE) { // skip empty or suppressed features
586 ce29c528 Andreas Kohlbecker
587
        $feature_name = cdm_term_representation($node->feature, 'Unnamed Feature');
588 529c47ff Andreas Kohlbecker
        $feature_block_settings = get_feature_block_settings($node->feature->uuid);
589 ce29c528 Andreas Kohlbecker
590
        $block = feature_block($feature_name, $node->feature);
591
        $block->content = array();
592
        $block_content_is_empty = TRUE;
593
594
        /*
595
         * Content/DISTRIBUTION.
596
         */
597
598
        if ($node->feature->uuid == UUID_DISTRIBUTION) {
599
          $block = compose_feature_block_distribution($taxon, $node->descriptionElements, $node->feature);
600
          $block_content_is_empty = FALSE;
601
        }
602
        /*
603
         * Content/COMMON_NAME.
604
         */
605
        else if ($node->feature->uuid == UUID_COMMON_NAME) {
606 0ebb6efc Andreas Kohlbecker
          $common_names_render_array = compose_feature_block_items_feature_common_name($node->descriptionElements, $node->feature);
607 ce29c528 Andreas Kohlbecker
          $block->content[] = $common_names_render_array;
608
          $block_content_is_empty = FALSE;
609
        }
610
611
        else if ($node->feature->uuid == UUID_USE_RECORD) {
612
          $block_uses_content_html = theme('cdm_block_Uses', array('taxonUuid' => $taxon->uuid));
613
          $block->content[] = markup_to_render_array($block_uses_content_html);
614
          $block_content_is_empty = FALSE;
615
        }
616
617
        /*
618
         * Content/ALL OTHER FEATURES.
619
         */
620
        else {
621
622
          $media_list = array();
623 c651d3b1 Andreas Kohlbecker
          $elements_render_array = array();
624
          $child_elements_render_array = null;
625 ce29c528 Andreas Kohlbecker
626 c651d3b1 Andreas Kohlbecker
          if (isset($node->descriptionElements[0])) {
627 529c47ff Andreas Kohlbecker
            $elements_render_array = compose_feature_block_items_generic($node->descriptionElements, $node->feature);
628 ce29c528 Andreas Kohlbecker
          }
629
630
          // Content/ALL OTHER FEATURES/Subordinate Features
631
          // subordinate features are printed inline in one floating text,
632
          // it is expected hat subordinate features can "contain" TextData,
633 c651d3b1 Andreas Kohlbecker
          // Qualitative- and Qualitative- DescriptionElements
634 ce29c528 Andreas Kohlbecker
          if (isset($node->childNodes[0])) {
635 c651d3b1 Andreas Kohlbecker
            $child_elements_render_array = compose_feature_block_items_nested($node, $media_list, $feature_block_settings);
636
            $elements_render_array = array_merge($elements_render_array, $child_elements_render_array);
637
          }
638
          $block_content_is_empty = $block_content_is_empty && empty($media_list) && empty($elements_render_array);
639
          if(!$block_content_is_empty){
640
            $block->content[] = compose_feature_block_wrap_elements($elements_render_array, $node->feature);
641
            $block->content[] = compose_feature_media_gallery($node, $media_list, $gallery_settings);
642
            /*
643
             * Footnotes for the feature block
644
             */
645
            $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . $node->feature->uuid)));
646
            $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => $node->feature->uuid)));
647
            $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => $node->feature->uuid)));
648 ce29c528 Andreas Kohlbecker
          }
649
        } // END all other features
650
651 bdd382be Andreas Kohlbecker
        // allows modifying the block contents via a the hook_cdm_feature_node_block_content_alter
652
        drupal_alter('cdm_feature_node_block_content', $block->content, $node->feature, $node->descriptionElements);
653
654 ce29c528 Andreas Kohlbecker
        if(!$block_content_is_empty){ // skip empty block content
655
          $block_list[] = $block;
656
          cdm_toc_list_add_item(cdm_term_representation($node->feature), $node->feature->uuid);
657
        } // END: skip empty block content
658 c651d3b1 Andreas Kohlbecker
      } // END: skip empty or suppressed features
659 ce29c528 Andreas Kohlbecker
    } // END: creating a block per feature
660
661
    drupal_alter('cdm_feature_node_blocks', $block_list, $taxon);
662
663
    RenderHints::popFromRenderStack();
664
665
    return _block_get_renderable_array($block_list);
666
  }
667
668 c651d3b1 Andreas Kohlbecker
/**
669
 * Creates a render array of description elements  held by child nodes of the given feature node.
670
 *
671
 * This function is called recursively!
672
 *
673
 * @param $node
674
 *   The feature node.
675
 * @param array $media_list
676
 *   List of CDM Media entities. All media of subordinate elements should be passed to the main feature.ä
677
 * @param $feature_block_settings
678
 *   The feature block settings.
679
 * @param $main_feature
680
 *  Only used internally in recursive calls.
681
 *
682
 * @return array
683
 *  A Drupal render array
684
 *
685
 * @ingroup compose
686
 */
687
function compose_feature_block_items_nested($node, &$media_list, $feature_block_settings, $main_feature = NULL)
688
{
689
690
  if(!$main_feature){
691
    $main_feature = $node->feature;
692
  }
693
  /*
694
   * TODO should be configurable, options; YES, NO, AUTOMATIC
695
   * (automatic will only place the label if the first word of the description element text is not the same)
696
   */
697
  $prepend_feature_label = false;
698
699
  $render_arrays = array();
700
  foreach ($node->childNodes as $child_node) {
701
    if (isset($child_node->descriptionElements[0])) {
702
      foreach ($child_node->descriptionElements as $element) {
703
704
        if (isset($element->media[0])) {
705
          // Append media of subordinate elements to list of main
706
          // feature.
707
          $media_list = array_merge($media_list, $element->media);
708
        }
709
710
        $child_node_element = null;
711
        switch ($element->class) {
712
          case 'TextData':
713
            $child_node_element = compose_description_element_text_data($element, $element->feature->uuid, $feature_block_settings, $prepend_feature_label);
714
            break;
715
          case 'CategoricalData':
716
            $child_node_element = compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label);
717
            break;
718
          case 'QuantitativeData':
719
            $child_node_element = compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label);
720
721
        }
722
        if (is_array($child_node_element)) {
723
          $render_arrays[] = $child_node_element;
724
        }
725
      }
726
    }
727
728
    if(isset($child_node->childNodes[0])){
729
      $render_arrays = array_merge($render_arrays, compose_feature_block_items_nested($child_node, $media_list, $feature_block_settings, $main_feature ));
730
    }
731
  }
732
733
  return $render_arrays;
734
}
735
736 ce29c528 Andreas Kohlbecker
  /**
737 c651d3b1 Andreas Kohlbecker
   *
738 ce29c528 Andreas Kohlbecker
   * @param $node
739 c651d3b1 Andreas Kohlbecker
   *  The merged feature three node which potentially contains media in its description elements.
740 ce29c528 Andreas Kohlbecker
   * @param $media_list
741 c651d3b1 Andreas Kohlbecker
   *    Additional media to be merged witht the media contained in the nodes description elements
742 ce29c528 Andreas Kohlbecker
   * @param $gallery_settings
743
   * @return array
744 c651d3b1 Andreas Kohlbecker
   *
745
   * @ingroup compose
746 ce29c528 Andreas Kohlbecker
   */
747
  function compose_feature_media_gallery($node, $media_list, $gallery_settings) {
748
749
    if (isset($node->descriptionElements)) {
750
      $media_list = array_merge($media_list, cdm_dataportal_media_from_descriptionElements($node->descriptionElements));
751
    }
752
753
    $captionElements = array('title', 'rights');
754
755
    if (isset($media_list[0]) && isset($gallery_settings['cdm_dataportal_media_maxextend']) && isset($gallery_settings['cdm_dataportal_media_cols'])) {
756
      $gallery = theme('cdm_media_gallerie', array(
757
        'mediaList' => $media_list,
758
        'galleryName' => CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME . '_' . $node->feature->uuid,
759
        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
760
        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
761
        'captionElements' => $captionElements,
762
      ));
763
      return markup_to_render_array($gallery);
764
    }
765
766
    return markup_to_render_array('');
767
  }
768
769
  /**
770 4a236db6 Andreas Kohlbecker
   * Composes the distribution feature block for a taxon
771
   *
772 ce29c528 Andreas Kohlbecker
   * @param $taxon
773
   * @param $descriptionElements
774
   *   an associative array with two elements:
775
   *   - '#type': must be 'DTO'
776
   *   - 'DistributionInfoDTO': a CDM DistributionInfoDTO object as returned by the DistributionInfo web service
777
   * @param $feature
778
   *
779 18727898 Andreas Kohlbecker
   * @return array
780
   *  A drupal render array
781
   *
782 ce29c528 Andreas Kohlbecker
   * @ingroup compose
783
   */
784
  function compose_feature_block_distribution($taxon, $descriptionElements, $feature) {
785
    $text_data_glue = '';
786
    $text_data_sortOutArray = FALSE;
787
    $text_data_enclosingTag = 'ul';
788
    $text_data_out_array = array();
789
790
    $distributionElements = NULL;
791
    $distribution_info_dto = NULL;
792
    $distribution_sortOutArray = FALSE;
793
794
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
795
796 284fb36d Andreas Kohlbecker
    // TODO use feature_block_settings instead of DISTRIBUTION_ORDER_MODE
797 97ac3d38 Andreas Kohlbecker
    if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) != 'TREE') {
798 ce29c528 Andreas Kohlbecker
      $distribution_glue = '';
799
      $distribution_enclosingTag = 'dl';
800
    } else {
801
      $distribution_glue = '';
802
      $distribution_enclosingTag = 'ul';
803
    }
804
805
    if (!isset($descriptionElements['#type']) || !$descriptionElements['#type'] == 'DTO') {
806
      // skip the DISTRIBUTION section if there is no DTO type element
807
      return array(); // FIXME is it ok to return an empty array?
808
    }
809
810
    $block = feature_block(
811
      cdm_term_representation($feature, 'Unnamed Feature'),
812
      $feature
813
    );
814
815
    // $$descriptionElements['TextData'] is added to to the feature node in merged_taxon_feature_tree()
816
    if (isset($descriptionElements['TextData'])) {
817
      // --- TextData
818
      foreach ($descriptionElements['TextData'] as $text_data_element) {
819 0ef9a709 Andreas Kohlbecker
        $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
820 ce29c528 Andreas Kohlbecker
        $repr = drupal_render($text_data_render_array);
821
822
        if (!array_search($repr, $text_data_out_array)) { // de-duplication !!
823
          $text_data_out_array[] = $repr;
824 c651d3b1 Andreas Kohlbecker
          // TODO HINT: sorting in compose_feature_block_wrap_elements will
825 ce29c528 Andreas Kohlbecker
          // not work since this array contains html attributes with uuids
826
          // and what is about cases like the bibliography where
827
          // any content can be prefixed with some foot-note anchors?
828
          $text_data_sortOutArray = TRUE;
829
          $text_data_glue = '<br/> ';
830
          $text_data_enclosingTag = 'p';
831
        }
832
      }
833
    }
834
835
836
    if ($text_data_out_array && variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
837 c651d3b1 Andreas Kohlbecker
      $block->content[] = compose_feature_block_wrap_elements(
838 dfc49afb Andreas Kohlbecker
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
839 ce29c528 Andreas Kohlbecker
      );
840
    }
841
842
    // --- Distribution map
843
    $distribution_map_query_parameters = NULL;
844
    if (isset($descriptionElements['DistributionInfoDTO'])) {
845
      $distribution_map_query_parameters = $descriptionElements['DistributionInfoDTO']->mapUriParams;
846
    }
847
    $map_render_element = compose_distribution_map($taxon, $distribution_map_query_parameters);
848
    $block->content[] = $map_render_element;
849
850
    $dto_out_array = array();
851 bda17f32 Andreas Kohlbecker
852
    // --- Condensed Distribution
853
    if(variable_get(DISTRIBUTION_CONDENSED) && isset($descriptionElements['DistributionInfoDTO']->condensedDistribution)){
854
      $condensed_distribution_markup = '<p class="condensed_distribution">';
855
856
      $isFirst = true;
857
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous[0])){
858
        foreach($descriptionElements['DistributionInfoDTO']->condensedDistribution->indigenous as $cdItem){
859
          if(!$isFirst){
860
            $condensed_distribution_markup .= ' ';
861
          }
862
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->idInVocabulary . '">'
863
          . $cdItem->areaStatusLabel . '</span>';
864
          $isFirst = false;
865
        }
866
      }
867
868
      if(isset($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign[0])) {
869
        if(!$isFirst){
870
          $condensed_distribution_markup .= ' ';
871
        }
872
        $isFirst = TRUE;
873
        $condensed_distribution_markup .= '[';
874
        foreach ($descriptionElements['DistributionInfoDTO']->condensedDistribution->foreign as $cdItem) {
875
          if (!$isFirst) {
876
            $condensed_distribution_markup .= ' ';
877
          }
878
          $condensed_distribution_markup .= '<span class="status_' . $cdItem->status->titleCache . '">'
879
            . $cdItem->areaStatusLabel . '</span>';
880
          $isFirst = false;
881
        }
882
        $condensed_distribution_markup .= ']';
883
      }
884
885 423486b0 Andreas Kohlbecker
      $condensed_distribution_markup .= '&nbsp;' . l(
886
          font_awesome_icon_markup(
887
            'fa-info-circle',
888
            array(
889
              'alt'=>'help',
890
              'class' => array('superscript')
891
            )
892
          ),
893
          'cdm_dataportal/help/condensed_distribution',
894
          array('html' => TRUE));
895 bda17f32 Andreas Kohlbecker
      $condensed_distribution_markup .= '</p>';
896
      $dto_out_array[] = $condensed_distribution_markup;
897
    }
898
899
    // --- tree or list
900 ce29c528 Andreas Kohlbecker
    if (isset($descriptionElements['DistributionInfoDTO'])) {
901
      $distribution_info_dto = $descriptionElements['DistributionInfoDTO'];
902
903
      // --- tree
904
      if (is_object($distribution_info_dto->tree)) {
905 97ac3d38 Andreas Kohlbecker
        $distribution_tree_render_array = compose_distribution_hierarchy($distribution_info_dto->tree, $feature_block_settings);
906 f23ae4e3 Andreas Kohlbecker
        $dto_out_array[] = $distribution_tree_render_array;
907 ce29c528 Andreas Kohlbecker
      }
908
909
      // --- sorted element list
910
      if (is_array($distribution_info_dto->elements) && count($distribution_info_dto->elements) > 0) {
911
        foreach ($distribution_info_dto->elements as $descriptionElement) {
912
          if (is_object($descriptionElement->area)) {
913
            $sortKey = $descriptionElement->area->representation_L10n;
914
            $distributionElements[$sortKey] = $descriptionElement;
915
          }
916
        }
917
        ksort($distributionElements);
918 63740051 Andreas Kohlbecker
        $distribution_element_render_array = compose_description_elements_distribution($distributionElements);
919 f23ae4e3 Andreas Kohlbecker
        $dto_out_array[] = $distribution_element_render_array;
920 ce29c528 Andreas Kohlbecker
921
      }
922
      //
923 c651d3b1 Andreas Kohlbecker
      $block->content[] = compose_feature_block_wrap_elements(
924 dfc49afb Andreas Kohlbecker
        $dto_out_array, $feature, $distribution_glue, $distribution_sortOutArray
925 ce29c528 Andreas Kohlbecker
      );
926
    }
927
928
    // --- TextData at the bottom
929
    if ($text_data_out_array && !variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
930 c651d3b1 Andreas Kohlbecker
      $block->content[] = compose_feature_block_wrap_elements(
931 dfc49afb Andreas Kohlbecker
        $text_data_out_array, $feature, $text_data_glue, $text_data_sortOutArray
932 ce29c528 Andreas Kohlbecker
      );
933
    }
934
935
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . UUID_DISTRIBUTION)));
936
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
937
    $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
938
939
    return $block;
940
  }
941
942
943
  /**
944 0ebb6efc Andreas Kohlbecker
   * Composes a drupal render array for single CDM TextData description element.
945 ce29c528 Andreas Kohlbecker
   *
946
   * @param $element
947 0ef9a709 Andreas Kohlbecker
   *    The CDM TextData description element.
948 ce29c528 Andreas Kohlbecker
   *  @param $feature_uuid
949 c651d3b1 Andreas Kohlbecker
   * @param bool $prepend_feature_label
950
   *   Used in nested feature trees to put the feature as label in front of the description element text representation.
951 ce29c528 Andreas Kohlbecker
   *
952
   * @return array
953
   *   A drupal render array with the following elements being used:
954
   *    - #tag: either 'div', 'li', ...
955
   *    ⁻ #attributes: class attributes
956
   *    - #value_prefix: (optionally) contains footnote anchors
957
   *    - #value: contains the textual content
958
   *    - #value_suffix: (optionally) contains footnote keys
959
   *
960
   * @ingroup compose
961
   */
962 c651d3b1 Andreas Kohlbecker
  function compose_description_element_text_data($element, $feature_uuid, $feature_block_settings, $prepend_feature_label = FALSE) {
963 ce29c528 Andreas Kohlbecker
964
    $footnote_list_key_suggestion = $feature_uuid;
965
966 0ef9a709 Andreas Kohlbecker
    $element_markup = '';
967 ce29c528 Andreas Kohlbecker
    if (isset($element->multilanguageText_L10n->text)) {
968
      // TODO replacement of \n by <br> should be configurable
969 0ef9a709 Andreas Kohlbecker
      $element_markup = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
970 ce29c528 Andreas Kohlbecker
    }
971
972 c651d3b1 Andreas Kohlbecker
    $render_array = compose_description_element($element, $feature_block_settings, $element_markup, $footnote_list_key_suggestion, $prepend_feature_label);
973 ce29c528 Andreas Kohlbecker
974 dfc49afb Andreas Kohlbecker
    return $render_array;
975
  }
976 ce29c528 Andreas Kohlbecker
977
978 e413f218 Andreas Kohlbecker
/**
979
 * Theme function to render CDM DescriptionElements of the type TaxonInteraction.
980
 *
981
 * @param $element
982
 *  The CDM TaxonInteraction entity
983
 *
984
 * @return
985
 *  A drupal render array
986
 *
987
 * @ingroup compose
988
 */
989
function compose_description_element_taxon_interaction($element, $feature_block_settings) {
990
991
  $out = '';
992
  $enclosing_tag = cdm_feature_block_element_tag_name($feature_block_settings);
993
994
  if (isset($element->description_L10n)) {
995
    $out .=  ' ' . $element->description_L10n;
996
  }
997
998
  if(isset($element->taxon2)){
999
    $out = render_taxon_or_name($element->taxon2, url(path_to_taxon($element->taxon2->uuid)));
1000
  }
1001
1002 f23ae4e3 Andreas Kohlbecker
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1003 e413f218 Andreas Kohlbecker
1004 f23ae4e3 Andreas Kohlbecker
  return $render_array;
1005 e413f218 Andreas Kohlbecker
}
1006
1007
1008 dfc49afb Andreas Kohlbecker
/**
1009
 * Renders a single instance of the type IndividualsAssociations.
1010
 *
1011
 * @param $element
1012
 *   The CDM IndividualsAssociations entity.
1013
 * @param $feature_block_settings
1014
 *
1015
 * @return array
1016
 *   Drupal render array
1017
 *
1018
 * @ingroup compose
1019
 */
1020
function compose_description_element_individuals_association($element, $feature_block_settings) {
1021
1022
  $out = '';
1023
  $enclosing_tag = cdm_feature_block_element_tag_name($feature_block_settings);
1024 ce29c528 Andreas Kohlbecker
1025 dfc49afb Andreas Kohlbecker
  $render_array = compose_cdm_specimenOrObservation($element->associatedSpecimenOrObservation);
1026
1027
  if (isset($element->description_L10n)) {
1028
    $out .=  ' ' . $element->description_L10n;
1029 ce29c528 Andreas Kohlbecker
  }
1030
1031 dfc49afb Andreas Kohlbecker
  $out .= drupal_render($render_array);
1032 ce29c528 Andreas Kohlbecker
1033 f23ae4e3 Andreas Kohlbecker
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid);
1034 dfc49afb Andreas Kohlbecker
1035 f23ae4e3 Andreas Kohlbecker
  return $render_array;
1036 dfc49afb Andreas Kohlbecker
}
1037
1038 529c47ff Andreas Kohlbecker
/**
1039
 * Renders a single instance of the type CategoricalData.
1040
 *
1041
 * @param $element
1042 c651d3b1 Andreas Kohlbecker
 *  The CDM CategoricalData entity
1043
 *
1044 4f8e61ad Andreas Kohlbecker
 * @param $feature_block_settings
1045 529c47ff Andreas Kohlbecker
 *
1046 c651d3b1 Andreas Kohlbecker
 * @param bool $prepend_feature_label
1047
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1048
 *
1049 529c47ff Andreas Kohlbecker
 * @return string
1050
 *   a html representation of the given CategoricalData element
1051
 *
1052
 * @ingroup compose
1053
 */
1054 c651d3b1 Andreas Kohlbecker
function compose_description_element_categorical_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1055 529c47ff Andreas Kohlbecker
1056 4f8e61ad Andreas Kohlbecker
  $enclosing_tag = cdm_feature_block_element_tag_name($feature_block_settings);
1057
1058 529c47ff Andreas Kohlbecker
  $state_data_strings = array();
1059
  if (isset($element->stateData)) {
1060
    foreach ($element->stateData as $state_data) {
1061
1062
      $state  = NULL;
1063
1064
      if (isset($state_data->state)) {
1065
        $state = cdm_term_representation($state_data->state);
1066
      }
1067
1068
      if (isset($state_data->modifyingText_L10n)) {
1069
        $state = ' ' . $state_data->modifyingText_L10n;
1070
      }
1071
1072
      $modifiers_strings = cdm_modifers_representations($state_data);
1073
1074
      $state_data_strings[] = $state . ($modifiers_strings ? ' ' . $modifiers_strings : '');
1075
1076
    }
1077
  }
1078
1079
  $out = '<span class="' . html_class_attribute_ref($element) . '">' . implode(', ', $state_data_strings) . '</span>';
1080
1081 c651d3b1 Andreas Kohlbecker
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid, $prepend_feature_label);
1082 529c47ff Andreas Kohlbecker
1083 f23ae4e3 Andreas Kohlbecker
  return $render_array;
1084 4f8e61ad Andreas Kohlbecker
}
1085
1086
1087
/**
1088
 * Theme function to render CDM DescriptionElements of the type QuantitativeData.
1089
 *
1090
 * The function renders the statisticalValues contained in the QuantitativeData
1091
 * entity according to the following scheme:
1092
 *
1093
 * (ExtremeMin)-Min-Average-Max-(ExtremeMax)
1094
 *
1095
 * All modifiers of these values are appended.
1096
 *
1097
 * If the QuantitativeData is containing more statisticalValues with further
1098
 * statisticalValue types, these additional measures will be appended to the
1099
 * above string separated by whitespace.
1100
 *
1101
 * Special cases;
1102
 * 1. Min==Max: this will be interpreted as Average
1103
 *
1104 c651d3b1 Andreas Kohlbecker
 * @param $element
1105
 *  The CDM QuantitativeData entity
1106
 *
1107
 * @param $feature_block_settings
1108
 *
1109
 * @param bool $prepend_feature_label
1110
 *   Used in nested feature trees to put the feature as label in front of the description element text representation.
1111
 *
1112 4f8e61ad Andreas Kohlbecker
 *
1113
 * @return string
1114
 *   a html representation of the given QuantitativeData element
1115
 *
1116
 * @ingroup themeable
1117
 */
1118 c651d3b1 Andreas Kohlbecker
function compose_description_element_quantitative_data($element, $feature_block_settings, $prepend_feature_label = FALSE) {
1119 4f8e61ad Andreas Kohlbecker
  /*
1120
   * - statisticalValues
1121
   *   - value
1122
   *   - modifiers
1123
   *   - type
1124
   * - unit->representation_L10n
1125
   * - modifyingText
1126
   * - modifiers
1127
   * - sources
1128
   */
1129
1130
  $out = '';
1131
  $type_representation = NULL;
1132
  $min_max = min_max_array();
1133
1134
1135
  $other_values = array();
1136
1137
  if (isset($element->statisticalValues)) {
1138
    $other_values_markup = array();
1139
    foreach ($element->statisticalValues as $statistical_val) {
1140
1141
      // compile the full value string which also may contain modifiers
1142
      if (isset($statistical_val->value)) {
1143
        $statistical_val->_value = $statistical_val->value;
1144
      }
1145
      $val_modifiers_strings = cdm_modifers_representations($statistical_val);
1146
      if ($val_modifiers_strings) {
1147
        $statistical_val->_value = ' ' . $val_modifiers_strings . ' ' . $statistical_val->_value;
1148
      }
1149
1150
      // either put into min max array or into $other_values
1151
      // for generic output to be appended to 'min-max' string
1152
      if (array_key_exists($statistical_val->type->titleCache, $min_max)) {
1153
        $min_max[$statistical_val->type->titleCache] = $statistical_val;
1154
      }
1155
      else {
1156
        $other_values[] = $statistical_val;
1157
      }
1158
    } // end of loop over statisticalValues
1159
1160
    // create markup
1161
1162
    $min_max_markup = min_max_markup($min_max);
1163
1164
1165
    foreach ($other_values as $statistical_val) {
1166
      $statistical_val_type_representation = NULL;
1167
      if (isset($statistical_val->type)) {
1168
        $statistical_val_type_representation = cdm_term_representation($statistical_val->type);
1169
        // $statistical_val->type->termType;
1170
        // $statistical_val->type->userFriendlyTypeName;
1171
      }
1172
      $value_markup = '<span class="' . html_class_attribute_ref($statistical_val) . ' ' . $statistical_val->type->termType . ' ">'
1173
        . $statistical_val->_value . '</span>';
1174
      $value_markup = $value_markup .
1175
        ($statistical_val_type_representation ? ' <span class="type">' . $statistical_val_type_representation . '</span>' : '');
1176
      $other_values_markup[] = $value_markup;
1177
    }
1178
1179
1180
    $out .= $min_max_markup . ' ' . implode($other_values_markup, ', ');
1181
  }
1182
1183
  if (isset($element->unit)) {
1184
    $out .= ' <span class="unit" title="'
1185
      . cdm_term_representation($element->unit) . '">'
1186
      . cdm_term_representation_abbreviated($element->unit)
1187
      . '</span>';
1188
  }
1189
1190
  // modifiers of the description element itself
1191
  $modifier_string = cdm_modifers_representations($element);
1192
  $out .= ($modifier_string ? ' ' . $modifier_string : '');
1193
  if (isset($element->modifyingText_L10n)) {
1194
    $out = $element->modifyingText_L10n . ' ' . $out;
1195
  }
1196
1197 c651d3b1 Andreas Kohlbecker
  $render_array = compose_description_element($element, $feature_block_settings, $out, $element->feature->uuid, $prepend_feature_label);
1198 4f8e61ad Andreas Kohlbecker
1199 f23ae4e3 Andreas Kohlbecker
  return $render_array;
1200 529c47ff Andreas Kohlbecker
}
1201
1202 dfc49afb Andreas Kohlbecker
1203
/**
1204 c651d3b1 Andreas Kohlbecker
 * Wraps the render array for the given feature into an enclosing html tag.
1205 dfc49afb Andreas Kohlbecker
 *
1206 c651d3b1 Andreas Kohlbecker
 * Optionally the elements can be sorted and glued together by a separator string.
1207
 *
1208
 * @param array $description_element_render_arrays
1209 f23ae4e3 Andreas Kohlbecker
 *   An list of render arrays. Which are usually are produced by the compose_description_element()
1210
 *   function. The render arrays should be of #type=html_tag, so that they can understand the #attribute property.
1211 dfc49afb Andreas Kohlbecker
 * @param  $feature :
1212
 *  The feature to which the elements given in $elements are belonging to.
1213
 * @param string $glue :
1214
 *  Defaults to empty string.
1215
 * @param bool $sort
1216
 *   Boolean Whether to sort the $elements alphabetically, default is FALSE
1217
 *
1218 c651d3b1 Andreas Kohlbecker
 * @return array
1219
 *    A Drupal render array
1220 dfc49afb Andreas Kohlbecker
 *
1221
 * @ingroup compose
1222
 */
1223 c651d3b1 Andreas Kohlbecker
  function compose_feature_block_wrap_elements(array $description_element_render_arrays, $feature, $glue = '', $sort = FALSE)
1224 dfc49afb Andreas Kohlbecker
  {
1225 ce29c528 Andreas Kohlbecker
1226
    $feature_block_settings = get_feature_block_settings($feature->uuid);
1227 0ef9a709 Andreas Kohlbecker
    $enclosing_tag = $feature_block_settings['as_list'];
1228 ce29c528 Andreas Kohlbecker
1229 f23ae4e3 Andreas Kohlbecker
    if ($sort) { // TODO remove parameter and replace by $feature_block_settings['sort_elements']
1230 c651d3b1 Andreas Kohlbecker
      usort($description_element_render_arrays, 'compare_description_element_render_arrays');
1231 f23ae4e3 Andreas Kohlbecker
    }
1232 ce29c528 Andreas Kohlbecker
1233 f23ae4e3 Andreas Kohlbecker
    $is_first = true;
1234 c651d3b1 Andreas Kohlbecker
    foreach($description_element_render_arrays as &$element_render_array){
1235 f23ae4e3 Andreas Kohlbecker
      if(!is_array($element_render_array)){
1236
        $element_render_array = markup_to_render_array($element_render_array);
1237
      }
1238
      $element_render_array['#attributes']['class'][] = "feature-block-element";
1239
1240
      // add the glue!
1241
      if(!$is_first) {
1242
        if (isset($element_render_array['#value'])) {
1243
          $element_render_array['#value'] = $glue . $element_render_array['#value'];
1244
        } elseif (isset($element_render_array['#markup'])) {
1245
          $element_render_array['#markup'] = $glue . $element_render_array['#markup'];
1246
        }
1247
      }
1248
      $is_first = false;
1249 ce29c528 Andreas Kohlbecker
    }
1250
1251 c651d3b1 Andreas Kohlbecker
    $render_array['elements']['children'] = $description_element_render_arrays;
1252 ce29c528 Andreas Kohlbecker
1253 f23ae4e3 Andreas Kohlbecker
    $render_array['elements']['#prefix'] = '<' . $enclosing_tag . ' class="feature-block-elements" id="' . $feature->representation_L10n . '">';
1254
    $render_array['elements']['#suffix'] = '</' . $enclosing_tag . '>';
1255
1256
    return $render_array;
1257 ce29c528 Andreas Kohlbecker
  }
1258
1259
1260
  /* compose nameInSource or originalNameString as markup
1261
   *
1262
   * @param $source
1263
   * @param $do_link_to_name_used_in_source
1264
   * @param $suppress_for_shown_taxon
1265
   *    the nameInSource will be suppressed when it has the same name as the accepted taxon
1266
   *    for which the taxon page is being created, Defaults to TRUE
1267
   *
1268
   * @return array
1269
   *    A Drupal render array with an additional element, the render array is empty
1270
   *    if the source had no name in source information
1271
   *    - #_plaintext: contains the plaintext version of the name (custom element)
1272
   *
1273
   * @ingroup compose
1274
   */
1275
  function compose_name_in_source($source, $do_link_to_name_used_in_source, $suppress_for_shown_taxon = TRUE) {
1276
1277
    $plaintext = NULL;
1278
    $markup = NULL;
1279
    $name_in_source_render_array = array();
1280
1281
    static $taxon_page_accepted_name = '';
1282
    if($suppress_for_shown_taxon && arg(1) == 'taxon' && empty($taxon_page_accepted_name)){
1283
1284
      $current_taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, arg(2));
1285
      $taxon_page_accepted_name = $current_taxon->name->titleCache;
1286
    }
1287
1288
    if (isset($source->nameUsedInSource->uuid) && isset($source->nameUsedInSource->titleCache)) {
1289
      // it is a DescriptionElementSource !
1290
      $plaintext = $source->nameUsedInSource->titleCache;
1291
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1292
        return $name_in_source_render_array; // SKIP this name
1293
      }
1294 63c3ac73 Andreas Kohlbecker
      $markup = render_taxon_or_name($source->nameUsedInSource);
1295 ce29c528 Andreas Kohlbecker
      if ($do_link_to_name_used_in_source) {
1296
        $markup = l(
1297
          $markup,
1298
          path_to_name($source->nameUsedInSource->uuid),
1299
          array(
1300
            'attributes' => array(),
1301
            'absolute' => TRUE,
1302
            'html' => TRUE,
1303
          ));
1304
      }
1305
    }
1306
    else if (isset($source->originalNameString) && !empty($source->originalNameString)) {
1307
      // the name used in source can not be expressed as valid taxon name,
1308
      // so the editor has chosen to put the freetext name into ReferencedEntityBase.originalNameString
1309
      // field
1310
      // using the originalNameString as key to avoid duplicate entries
1311
      $plaintext = $source->originalNameString;
1312
      if($suppress_for_shown_taxon && $taxon_page_accepted_name == $plaintext){
1313
        return $name_in_source_render_array; // SKIP this name
1314
      }
1315
      $markup = $source->originalNameString;
1316
    }
1317
1318
    if ($plaintext) { // checks if we have any content
1319
      $name_in_source_render_array = markup_to_render_array($markup);
1320
      $name_in_source_render_array['#_plaintext'] = $plaintext;
1321
    }
1322
1323
    return $name_in_source_render_array;
1324
  }
1325
1326
1327
1328
  /**
1329
   * Return HTML for a list of description elements.
1330
   *
1331
   * Usually these are of a specific feature type.
1332
   *
1333 c651d3b1 Andreas Kohlbecker
   * @param $description_elements
1334 ce29c528 Andreas Kohlbecker
   *   array of descriptionElements which belong to the same feature.
1335
   *   These descriptions elements of a Description must be ordered by the chosen feature tree by
1336
   *   calling the function _mergeFeatureTreeDescriptions().
1337
   *   @see _mergeFeatureTreeDescriptions()
1338
   *
1339
   * @param  $feature_uuid
1340
   *
1341
   * @return
1342
   *    A drupal render array for the $descriptionElements, may be an empty array if the textual content was empty.
1343
   *    Footnote key or anchors are not considered to be textual content.
1344
   *
1345
   * @ingroup compose
1346
   */
1347 c651d3b1 Andreas Kohlbecker
  function compose_feature_block_items_generic($description_elements, $feature) {
1348 ce29c528 Andreas Kohlbecker
1349
    $elements_out_array = array();
1350
    $distribution_tree = null;
1351
1352
    /*
1353
     * $feature_block_has_content will be set true if at least one of the
1354
     * $descriptionElements contains some text which makes up some content
1355
     * for the feature block. Footnote keys are not considered
1356
     * to be content in this sense.
1357
     */
1358
    $feature_block_has_content = false;
1359
1360 c651d3b1 Andreas Kohlbecker
    if (is_array($description_elements)) {
1361
      foreach ($description_elements as $description_element) {
1362 ce29c528 Andreas Kohlbecker
          /* decide based on the description element class
1363
           *
1364
           * Features handled here:
1365
           * all except DISTRIBUTION, COMMON_NAME, USES, IMAGES,
1366
           *
1367
           * TODO provide api_hook as extension point for this?
1368
           */
1369 c651d3b1 Andreas Kohlbecker
        $feature_block_settings = get_feature_block_settings($description_element->feature->uuid);
1370
        switch ($description_element->class) {
1371 f23ae4e3 Andreas Kohlbecker
          case 'TextData':
1372 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_text_data($description_element, $description_element->feature->uuid, $feature_block_settings);
1373 f23ae4e3 Andreas Kohlbecker
            break;
1374
          case 'CategoricalData':
1375 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_categorical_data($description_element, $feature_block_settings);
1376 f23ae4e3 Andreas Kohlbecker
            break;
1377
          case 'QuantitativeData':
1378 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_quantitative_data($description_element, $feature_block_settings);
1379 f23ae4e3 Andreas Kohlbecker
            break;
1380
          case 'IndividualsAssociation':
1381 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_individuals_association($description_element, $feature_block_settings);
1382 f23ae4e3 Andreas Kohlbecker
            break;
1383
          case 'TaxonInteraction':
1384 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = compose_description_element_taxon_interaction($description_element, $feature_block_settings);
1385 f23ae4e3 Andreas Kohlbecker
            break;
1386
          case 'Uses':
1387
            /* IGNORE Uses classes, these are handled completely in theme_cdm_UseDescription */
1388
            break;
1389
          default:
1390
            $feature_block_has_content = true;
1391 c651d3b1 Andreas Kohlbecker
            $elements_out_array[] = markup_to_render_array('<li>No method for rendering unknown description class: ' . $description_element->class . '</li>');
1392 f23ae4e3 Andreas Kohlbecker
        }
1393
        $elements_out_array_last_item = $elements_out_array[count($elements_out_array) - 1];
1394
        // considering not empty as long as the last item added is a render array
1395
        $feature_block_has_content = $feature_block_has_content || !empty($elements_out_array_last_item['#type']);
1396
      }
1397 ce29c528 Andreas Kohlbecker
1398
      // If feature = CITATION sort the list of sources.
1399
      // This is ONLY for FLORA MALESIANA and FLORE d'AFRIQUE CENTRALE.
1400 c651d3b1 Andreas Kohlbecker
      if (isset($description_element) && $description_element->feature->uuid == UUID_CITATION) {
1401 ce29c528 Andreas Kohlbecker
        sort($elements_out_array);
1402
      }
1403 aa26f9f0 Andreas Kohlbecker
    }
1404 ce29c528 Andreas Kohlbecker
1405 c651d3b1 Andreas Kohlbecker
    return $elements_out_array;
1406 ce29c528 Andreas Kohlbecker
  }
1407
1408 4a236db6 Andreas Kohlbecker
/**
1409
 * Composes block of common names for the given DescriptionElements $elements which must be of the feature CommonName
1410
 *
1411
 * @parameter $elements
1412
 *  an array of CDM DescriptionElements either of type CommonName or TextData
1413
 * @parameter $feature
1414
 *  the common feature of all $elements, must be CommonName
1415
 *
1416
 * @return
1417
 *   A drupal render array
1418
 *
1419
 * @ingroup compose
1420
 */
1421 0ebb6efc Andreas Kohlbecker
function compose_feature_block_items_feature_common_name($elements, $feature, $weight = FALSE) {
1422 4a236db6 Andreas Kohlbecker
1423
  $common_name_out = '';
1424
  $common_name_feature_elements = array();
1425
  $textData_commonNames = array();
1426
1427
  $footnote_key_suggestion = 'common-names-feature-block';
1428
1429
  $feature_block_settings = get_feature_block_settings(UUID_COMMON_NAME);
1430
1431
  $element_tag_name = cdm_feature_block_element_tag_name($feature_block_settings);
1432
1433
  if (is_array($elements)) {
1434
    foreach ($elements as $element) {
1435
1436
      if ($element->class == 'CommonTaxonName') {
1437
1438
        // common name without a language or area, should not happen but is possible
1439
        $language_area_key = '';
1440
        if (isset($element->language->representation_L10n)) {
1441
          $language_area_key .= '<b>' . $element->language->representation_L10n . '</b>';
1442
        }
1443
        if(isset($element->area->titleCache) && strlen($element->area->titleCache) > 0){
1444
          $language_area_key .= ($language_area_key ? ' '  : '') . '(' . $element->area->titleCache . ')';
1445
        }
1446
1447
        if(isset($common_names[$language_area_key][$element->name])) {
1448 f23ae4e3 Andreas Kohlbecker
          // same name already exists for language and area combination, se we merge the description elements
1449 4a236db6 Andreas Kohlbecker
          cdm_merge_description_elements($common_names[$language_area_key][$element->name], $element);
1450
        } else{
1451
          // otherwise add as new entry
1452
          $common_names[$language_area_key][$element->name] = $element;
1453
        }
1454
1455
      }
1456
      elseif ($element->class == 'TextData') {
1457
        $textData_commonNames[] = $element;
1458
      }
1459
    }
1460
  }
1461
  // Handling common names.
1462
  if (isset($common_names) && count($common_names) > 0) {
1463
    // Sorting the array based on the key (language, + area if set).
1464
    // Comment @WA there are common names without a language, so this sorting
1465
    // can give strange results.
1466
    ksort($common_names);
1467
1468
    // loop over set of elements per language area
1469
    foreach ($common_names as $language_area_key => $elements) {
1470
      ksort($elements); // sort names alphabetically
1471
      $per_language_area_out = array();
1472
      // loop over set of individual elements
1473
      foreach ($elements as $element) {
1474
        if ($element->name) {
1475
          $annotations_and_sources = handle_annotations_and_sources($element, $feature_block_settings, $element->name, $footnote_key_suggestion);
1476
          $source_references_markup = '';
1477
          if(!empty($annotations_and_sources['source_references'])){
1478
            $source_references_markup = ' ' . join(', ', $annotations_and_sources['source_references']);
1479
          }
1480
          $per_language_area_out[] = '<' . $element_tag_name. ' class="' . html_class_attribute_ref($element) . '">'
1481
            . $element->name . $source_references_markup . $annotations_and_sources['foot_note_keys'] . '</' . $element_tag_name. '>';
1482
        }
1483
      } // End of loop over set of individual elements
1484
      $common_name_feature_elements[] = ($language_area_key ? $language_area_key . ': ' : '' ) . join(', ', $per_language_area_out);
1485
    } // End of loop over set of elements per language area
1486
1487
1488 c651d3b1 Andreas Kohlbecker
    $common_name_feature_elements_render_array = compose_feature_block_wrap_elements(
1489 dfc49afb Andreas Kohlbecker
      $common_name_feature_elements, $feature, '; ', FALSE
1490 4a236db6 Andreas Kohlbecker
    );
1491 f23ae4e3 Andreas Kohlbecker
    $common_name_out .= drupal_render($common_name_feature_elements_render_array); // FIXME should this be a render array instead?
1492 4a236db6 Andreas Kohlbecker
1493
  }
1494
1495
  // Handling commons names as text data.
1496
  $text_data_out = array();
1497
1498
  foreach ($textData_commonNames as $text_data_element) {
1499 0ef9a709 Andreas Kohlbecker
    /* footnotes are not handled correctly in compose_description_element_text_data,
1500 4a236db6 Andreas Kohlbecker
       need to set 'common-names-feature-block' as $footnote_key_suggestion */
1501
    RenderHints::setFootnoteListKey($footnote_key_suggestion);
1502 0ef9a709 Andreas Kohlbecker
    $text_data_render_array = compose_description_element_text_data($text_data_element, $text_data_element->feature->uuid, $feature_block_settings);
1503 4a236db6 Andreas Kohlbecker
    $text_data_out[] = drupal_render($text_data_render_array);
1504
  }
1505
1506 c651d3b1 Andreas Kohlbecker
  $common_name_out_text_data = compose_feature_block_wrap_elements(
1507 dfc49afb Andreas Kohlbecker
    $text_data_out, $feature
1508 4a236db6 Andreas Kohlbecker
  );
1509
1510
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => 'BIBLIOGRAPHY-' . $footnote_key_suggestion));
1511
  $footnotes .= theme('cdm_footnotes', array('footnoteListKey' => $footnote_key_suggestion)); // FIXME is this needed at all?
1512
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnote_key_suggestion));
1513
1514 f23ae4e3 Andreas Kohlbecker
  return  markup_to_render_array(  // FIXME markup_to_render_array should no longer be needed
1515 4a236db6 Andreas Kohlbecker
    '<div class="common_names_as_common_names">' . $common_name_out . '</div>'
1516 f23ae4e3 Andreas Kohlbecker
    .'<div class="common_names_as_text_data">' . drupal_render($common_name_out_text_data) . '</div>'
1517 4a236db6 Andreas Kohlbecker
    .$footnotes,
1518
    $weight
1519
  );
1520
}
1521 ce29c528 Andreas Kohlbecker
1522
  /**
1523 0ebb6efc Andreas Kohlbecker
   * Composes the render array for a CDM Distribution description element
1524 ce29c528 Andreas Kohlbecker
   *
1525 dfc49afb Andreas Kohlbecker
   * @param array $description_elements
1526 0ebb6efc Andreas Kohlbecker
   *   Array of CDM Distribution instances
1527 ce29c528 Andreas Kohlbecker
   * @param $enclosingTag
1528
   *   The html tag to be use for the enclosing element
1529
   *
1530
   * @return array
1531
   *   A Drupal render array
1532
   *
1533
   * @ingroup compose
1534
   */
1535 dfc49afb Andreas Kohlbecker
  function compose_description_elements_distribution($description_elements){
1536 ce29c528 Andreas Kohlbecker
1537
    $out = '';
1538
    RenderHints::pushToRenderStack('descriptionElementDistribution');
1539
    RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1540
1541
    $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
1542 63740051 Andreas Kohlbecker
    $enclosingTag = cdm_feature_block_element_tag_name($feature_block_settings);
1543 ce29c528 Andreas Kohlbecker
1544 dfc49afb Andreas Kohlbecker
    foreach ($description_elements as $description_element) {
1545 ce29c528 Andreas Kohlbecker
      $annotations_and_sources = handle_annotations_and_sources(
1546 dfc49afb Andreas Kohlbecker
        $description_element,
1547 ce29c528 Andreas Kohlbecker
        $feature_block_settings,
1548 dfc49afb Andreas Kohlbecker
        $description_element->area->representation_L10n,
1549 ce29c528 Andreas Kohlbecker
        UUID_DISTRIBUTION
1550
      );
1551
1552 18727898 Andreas Kohlbecker
1553 dfc49afb Andreas Kohlbecker
      list($status_label, $status_markup) = distribution_status_label_and_markup($description_element);
1554 18727898 Andreas Kohlbecker
1555 dfc49afb Andreas Kohlbecker
      $out .= '<' . $enclosingTag . ' class="descriptionElement descriptionElement-' . $description_element->uuid
1556 18727898 Andreas Kohlbecker
        . ' " title="' . $status_label. '">'
1557 dfc49afb Andreas Kohlbecker
        . $description_element->area->representation_L10n
1558 18727898 Andreas Kohlbecker
        . $status_markup;
1559 ce29c528 Andreas Kohlbecker
      if(!empty($annotations_and_sources['source_references'])){
1560 09ded97c Andreas Kohlbecker
        $out .= ' ' . join(' ', $annotations_and_sources['source_references'] );
1561 ce29c528 Andreas Kohlbecker
      }
1562
      $out .= $annotations_and_sources['foot_note_keys']   . ' </' . $enclosingTag . '>';
1563
    }
1564
1565
    RenderHints::popFromRenderStack();
1566
    return markup_to_render_array($out);
1567
  }
1568
1569 65345976 Andreas Kohlbecker
  /**
1570
   * @param $descriptionElement
1571
   * @return array
1572
   */
1573
  function distribution_status_label_and_markup($descriptionElement) {
1574
    $status_markup = '';
1575
    $status_label = '';
1576
1577
    if (isset($descriptionElement->status)) {
1578
      $status_label = $descriptionElement->status->representation_L10n;
1579
      $status_markup = '<span class="distributionStatus distributionStatus-' . $descriptionElement->status->idInVocabulary . '"> '
1580
        . $status_label . ' </span>';
1581
1582
    };
1583
    return array($status_label, $status_markup);
1584
  }
1585
1586 ce29c528 Andreas Kohlbecker
1587 7508605c Andreas Kohlbecker
  /**
1588
   * Provides the merged feature tree for a taxon profile page.
1589
   *
1590
   * The merging of the profile feature tree is actully done in
1591
   * _mergeFeatureTreeDescriptions(). See this method  for details
1592
   * on the structure of the merged tree.
1593
   *
1594
   * This method provides t hook which can be used to modify the
1595
   * merged feature tree after it has been created, see
1596
   * hook_merged_taxon_feature_tree_alter()
1597
   *
1598
   * @param $taxon
1599
   *   A CDM Taxon instance
1600
   *
1601
   * @return object
1602
   *  The merged feature tree
1603
   *
1604
   */
1605
  function merged_taxon_feature_tree($taxon) {
1606
1607
    // 1. fetch descriptions_by_featuretree but exclude the distribution feature
1608
    $merged_tree = cdm_ws_descriptions_by_featuretree(get_profile_feature_tree(), $taxon->uuid, array(UUID_DISTRIBUTION));
1609
1610
1611
    // 2. find the distribution feature node
1612
    $distribution_node =& cdm_feature_tree_find_node($merged_tree->root->childNodes, UUID_DISTRIBUTION);
1613
1614
    if ($distribution_node) {
1615
      // 3. get the distributionInfoDTO
1616
      $query_parameters = cdm_distribution_filter_query();
1617
      $query_parameters['part'] = array('mapUriParams');
1618
      if(variable_get(DISTRIBUTION_CONDENSED)){
1619
        $query_parameters['part'][] = 'condensedDistribution';
1620
      }
1621 97ac3d38 Andreas Kohlbecker
      if (variable_get(DISTRIBUTION_ORDER_MODE, DISTRIBUTION_ORDER_MODE_DEFAULT) == 'TREE') {
1622 7508605c Andreas Kohlbecker
        $query_parameters['part'][] = 'tree';
1623
      }
1624
      else {
1625
        $query_parameters['part'][] = 'elements';
1626
      }
1627 284fb36d Andreas Kohlbecker
      $query_parameters['omitLevels'] = array();
1628
      foreach(variable_get(DISTRIBUTION_TREE_OMIT_LEVELS, array()) as $uuid){
1629
        if(is_uuid($uuid)){
1630
          $query_parameters['omitLevels'][] = $uuid;
1631
        }
1632
      }
1633 7508605c Andreas Kohlbecker
      $customStatusColorsJson = variable_get(DISTRIBUTION_STATUS_COLORS, NULL);
1634
      if ($customStatusColorsJson) {
1635
        $query_parameters['statusColors'] = $customStatusColorsJson;
1636
      }
1637
1638
      $distribution_info_dto = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION_DISTRIBUTION_INFO_FOR, $taxon->uuid, queryString($query_parameters));
1639
      // 4. get distribution TextData is there are any
1640
      $distribution_text_data = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON,
1641
        array(
1642
          'taxon' => $taxon->uuid,
1643
          'type' => 'TextData',
1644
          'features' => UUID_DISTRIBUTION
1645
        )
1646
      );
1647
1648
      // 5. put all distribution data into the distribution feature node
1649
      if ($distribution_text_data //if text data exists
1650
        || ($distribution_info_dto && isset($distribution_info_dto->tree) && $distribution_info_dto->tree->rootElement->numberOfChildren > 0) // OR if tree element has distribution elements
1651
        || ($distribution_info_dto && !empty($distribution_info_dto->elements))
1652
      ) { // OR if DTO has distribution elements
1653
        $distribution_node->descriptionElements = array('#type' => 'DTO');
1654
        if ($distribution_text_data) {
1655
          $distribution_node->descriptionElements['TextData'] = $distribution_text_data;
1656
        }
1657
        if ($distribution_info_dto) {
1658
          $distribution_node->descriptionElements['DistributionInfoDTO'] = $distribution_info_dto;
1659
        }
1660
      }
1661
    }
1662
1663
    // allows modifying the merged tree via a the hook_cdm_feature_node_block_content_alter
1664
    drupal_alter('merged_taxon_feature_tree', $taxon, $merged_tree);
1665
1666
    return $merged_tree;
1667
  }
1668
1669
1670 65345976 Andreas Kohlbecker
  function compose_distribution_hierarchy($distribution_tree, $feature_block_settings){
1671
1672
    static $hierarchy_style;
1673
    // TODO expose $hierarchy_style to administration of provide a hook
1674
    if( !isset($hierarchy_style)){
1675
      $hierarchy_style = array(
1676
        // level 2
1677
        array(
1678
          'label_suffix' => '',
1679
          'element_glue' => ', ',
1680
          'element_set_pre' => '(',
1681
          'element_set_post' => ')'
1682
        ),
1683
        // level 1
1684
        array(
1685
          'label_suffix' => '',
1686
          'element_glue' => '; ',
1687
          'element_set_pre' => '',
1688
          'element_set_post' => ''
1689
        ),
1690
        // level 0
1691
        array(
1692
          'label_suffix' => ':',
1693 330d08aa Andreas Kohlbecker
          'element_glue' => ' ',
1694 65345976 Andreas Kohlbecker
          'element_set_pre' => '',
1695
          'element_set_post' => ''
1696
        ),
1697
      );
1698
    }
1699
1700
    $render_array = array();
1701
1702
    RenderHints::pushToRenderStack('descriptionElementDistribution');
1703
    RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1704
1705
    // Returning NULL if there are no description elements.
1706
    if ($distribution_tree == null) {
1707
      return $render_array;
1708
    }
1709
    // for now we are not using a render array internally to avoid performance problems
1710
    $markup = '';
1711
    if (isset($distribution_tree->rootElement->children)) {
1712
      $tree_nodes = $distribution_tree->rootElement->children;
1713
      _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, $markup, $hierarchy_style);
1714
    }
1715
1716
    $render_array['distribution_hierarchy'] = markup_to_render_array(
1717
      $markup,
1718
      0,
1719
      '<div id="distribution_hierarchy" class="distribution_hierarchy">',
1720
      '</div>'
1721
    );
1722
1723
    return $render_array;
1724
  }
1725
1726
  /**
1727 63740051 Andreas Kohlbecker
   * this function should produce markup as the compose_description_elements_distribution()
1728 65345976 Andreas Kohlbecker
   * function.
1729
   *
1730 63740051 Andreas Kohlbecker
   * @see compose_description_elements_distribution()
1731 65345976 Andreas Kohlbecker
   *
1732
   * @param $distribution_tree
1733
   * @param $feature_block_settings
1734
   * @param $tree_nodes
1735
   * @param $markup
1736
   * @param $hierarchy_style
1737
   */
1738
  function _compose_distribution_hierarchy($tree_nodes, $feature_block_settings, &$markup, $hierarchy_style, $level_index = -1){
1739
1740
    $level_index++;
1741
    static $enclosingTag = "span";
1742
1743
    $level_style = array_pop($hierarchy_style);
1744 330d08aa Andreas Kohlbecker
    if(count($hierarchy_style) == 0){
1745
      // lowest defined level style will be reused for all following levels
1746
      $hierarchy_style[] = $level_style;
1747
    }
1748 65345976 Andreas Kohlbecker
1749
    $node_index = -1;
1750
    $per_node_markup = array();
1751
    foreach ($tree_nodes as $node){
1752
1753
      $per_node_markup[++$node_index] = '';
1754
1755
      $label = $node->nodeId->representation_L10n;
1756
1757
      $distributions = $node->data;
1758
      $distribution_uuids = array();
1759
      $distribution_aggregate = NULL;
1760
        foreach($distributions as $distribution){
1761
1762
          $distribution_uuids[] = $distribution->uuid;
1763
1764
          // if there is more than one distribution we aggregate the sources and
1765
          // annotations into a synthetic distribution so that the footnote keys
1766
          // can be rendered consistently
1767
          if(!$distribution_aggregate) {
1768
            $distribution_aggregate = $distribution;
1769
            if(!isset($distribution_aggregate->sources[0])){
1770
              $distribution_aggregate->sources = array();
1771
            }
1772
            if(!isset($distribution_aggregate->annotations[0])){
1773
              $distribution_aggregate->annotations = array();
1774
            }
1775
          } else {
1776
            if(isset($distribution->sources[0])) {
1777
              $distribution_aggregate->sources = array_merge($distribution_aggregate->sources,
1778
                $distribution->sources);
1779
            }
1780
            if(isset($distribution->annotations[0])) {
1781
              $distribution_aggregate->annotations = array_merge($distribution_aggregate->annotations,
1782
                $distribution->annotations);
1783
            }
1784
          }
1785
        }
1786
1787
      $status_label= '';
1788
      $status_markup = '';
1789 9e2aa1ff Andreas Kohlbecker
      $annotations_and_sources =  null;
1790 65345976 Andreas Kohlbecker
      if($distribution_aggregate) {
1791
        $annotations_and_sources = handle_annotations_and_sources(
1792
          $distribution_aggregate,
1793
          $feature_block_settings,
1794
          $label,
1795
          UUID_DISTRIBUTION
1796
        );
1797
1798
        list($status_label, $status_markup) = distribution_status_label_and_markup($distribution);
1799
      }
1800
1801
      $per_node_markup[$node_index] .= '<' . $enclosingTag . ' class="descriptionElement'
1802
        . join(' descriptionElement-', $distribution_uuids)
1803
        . ' level_index_' . $level_index
1804
        . ' " title="' . $status_label . '">'
1805
        . '<span class="area_label">' . $label
1806
        . $level_style['label_suffix'] . ' </span>'
1807
        .  $status_markup
1808
      ;
1809
1810 97ac3d38 Andreas Kohlbecker
      if(isset($annotations_and_sources)){
1811 65345976 Andreas Kohlbecker
        if(!empty($annotations_and_sources['source_references'])){
1812 97ac3d38 Andreas Kohlbecker
          $per_node_markup[$node_index] .= ' ' . join(', ' , $annotations_and_sources['source_references']);
1813 65345976 Andreas Kohlbecker
        }
1814
        if($annotations_and_sources['foot_note_keys']) {
1815
          $per_node_markup[$node_index] .= $annotations_and_sources['foot_note_keys'];
1816
        }
1817
      }
1818
1819
      if(isset($node->children[0])){
1820
        _compose_distribution_hierarchy(
1821
          $node->children,
1822
          $feature_block_settings,
1823
          $per_node_markup[$node_index],
1824
          $hierarchy_style,
1825
          $level_index
1826
        );
1827
      }
1828
1829
      $per_node_markup[$node_index] .= '</' . $enclosingTag . '>';
1830
    }
1831
    $markup .= $level_style['element_set_pre']  . join( $level_style['element_glue'], $per_node_markup) . $level_style['element_set_post'];
1832
  }
1833