Project

General

Profile

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