Project

General

Profile

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