Project

General

Profile

Download (60.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Description theming functions.
5
 *
6
 * @copyright
7
 *   (C) 2007-2012 EDIT
8
 *   European Distributed Institute of Taxonomy
9
 *   http://www.e-taxonomy.eu
10
 *
11
 *   The contents of this module are subject to the Mozilla
12
 *   Public License Version 1.1.
13
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
14
 */
15

    
16

    
17
/**
18
 * Theme function to alter the feature representation.
19
 *
20
 * It is highly qeutionalbe if this function should be completely removed.
21
 * If a feature needs a different representation this should be edited directly
22
 * in the cdm data but it shoud not be tweeked like this in the portal.
23
 *
24
 * Used in:
25
 *  - theme_cdm_feature_nodesTOC()
26
 *  - theme_cdm_feature_nodes()
27
 *  - theme_cdm_media_mime_application()
28
 *  - theme_cdm_media_mime_text()
29
 *
30
 * TODO delete this function? (a.kohlbecker feb 2013)
31
 *
32
 */
33
function theme_cdm_feature_name($variables) {
34
  $feature_name = $variables['feature_name'];
35
  return t($feature_name);
36
}
37

    
38
/**
39
 * Returns a set of feature blocks for a taxon profile from the $mergedFeatureNodes of a given $taxon.
40
 *
41
 * The taxon profile consists of drupal block elements, one for the description elements
42
 * of a specific feature. The structure is defined by specific FeatureTree.
43
 * The chosen FeatureTree is merged with the list of description elements prior to using this method.
44
 *
45
 * The merged nodes can be obtained by making use of the
46
 * function cdm_ws_descriptions_by_featuretree().
47
 *
48
 * @see cdm_ws_descriptions_by_featuretree()
49
 *
50
 * @param $mergedFeatureNodes
51
 *
52
 * @param $taxon
53
 *
54
 * @return array
55
 *  A Drupal render array containing feature blocks and the table of content
56
 *
57
 * @ingroup compose
58
 */
59
function compose_cdm_feature_nodes($mergedFeatureNodes, $taxon) {
60

    
61
  $block_list = array();
62

    
63
  RenderHints::pushToRenderStack('feature_nodes');
64

    
65
  $gallery_settings = getGallerySettings(CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME);
66

    
67
  // Create a drupal block for each feature
68
  foreach ($mergedFeatureNodes as $node) {
69

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

    
73
      $feature_name = cdm_term_representation($node->feature, 'Unnamed Feature');
74

    
75
      $block = feature_block($feature_name, $node->feature);
76
      $block->content = array();
77
      $block_content_is_empty = TRUE;
78
      $block_content_weight = 0;
79

    
80
      /*
81
       * Content/DISTRIBUTION.
82
       */
83

    
84
      if ($node->feature->uuid == UUID_DISTRIBUTION) {
85
        $block = compose_feature_block_distribution($taxon, $node->descriptionElements, $node->feature);
86
        $block_content_is_empty = FALSE;
87
      }
88
      /*
89
       * Content/COMMON_NAME.
90
       */
91
      else if ($node->feature->uuid == UUID_COMMON_NAME) {
92
        $common_names_render_array = compose_cdm_common_names($node->descriptionElements, $node->feature);
93
        $block->content[] = $common_names_render_array;
94
        $block_content_is_empty = FALSE;
95
      }
96

    
97
      else if ($node->feature->uuid == UUID_USE_RECORD) {
98
        $block_uses_content_html = theme('cdm_block_Uses', array('taxonUuid' => $taxon->uuid));
99
        $block->content[] = markup_to_render_array($block_uses_content_html);
100
        $block_content_is_empty = FALSE;
101
      }
102

    
103
      /*
104
       * Content/ALL OTHER FEATURES.
105
       */
106
      else {
107

    
108
        $media_list = array();
109
        $out_child_elements = '';
110

    
111
        if (isset($node->descriptionElements)) {
112
          $taxon_uuid = NULL;
113
          if(isset($taxon) ) {
114
            $taxon_uuid = $taxon->uuid;
115
          }
116
          $elements_render_array = compose_cdm_descriptionElements($node->descriptionElements, $node->feature, $taxon_uuid);
117
          $block_content_is_empty = empty($elements_render_array);
118
          $block->content[] = $elements_render_array;
119
        }
120

    
121
          // Content/ALL OTHER FEATURES/Subordinate Features
122
          // subordinate features are printed inline in one floating text,
123
          // it is expected hat subordinate features can "contain" TextData,
124
          // Qualitative- and Qualitative- DescriptioneElements
125
          if (isset($node->childNodes[0])) {
126

    
127
          // TODO support more than one level of children.
128
          // can this be solved by recursively calling this function?
129
          // @see http://dev.e-taxonomy.eu/trac/ticket/2393
130
          $text = '';
131
          foreach ($node->childNodes as $child) {
132

    
133
            if (isset($child->descriptionElements) && is_array($child->descriptionElements)) {
134
              foreach ($child->descriptionElements as $element) {
135

    
136
                //TODO it woud be better if we could use compose_cdm_descriptionElements() directly instead of the below stuff
137

    
138
                if (is_array($element->media)) {
139
                  // Append media of supordinate elements to list of main
140
                  // feature.
141
                  $media_list = array_merge($media_list, $element->media);
142
                }
143

    
144
                switch ($element->class) {
145
                  case 'TextData':
146
                    // FIXME use theme_cdm_descriptionElementTextData()
147
                    if(isset($element->multilanguageText_L10n->text)){
148
                      $out_child_elements = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
149
                    }
150
                    $out_child_elements = str_replace($child->feature->titleCache, '<em>' . $child->feature->representation_L10n . '</em>', $out_child_elements);
151
                    break;
152
                  case 'CategoricalData':
153
                    $out_child_elements  = '<em>' . $child->feature->representation_L10n . '</em> '
154
                      . theme('cdm_descriptionElement_CategoricalData', array('element' => $element));
155
                    break;
156
                  case 'QuantitativeData':
157
                    $out_child_elements = '<em>' . $child->feature->representation_L10n . '</em> '
158
                      . theme('cdm_descriptionElement_QuantitativeData', array('element' => $element));
159

    
160
                }
161

    
162
              }
163
              $text .= " " . $out_child_elements;
164
              $out_child_elements = '';
165
            }
166
          }
167
          $block_content_is_empty = $block_content_is_empty && empty($text);
168
          $block->content[] = markup_to_render_array($text);
169
          $block_content_is_empty = $block_content_is_empty && empty($media_list);
170
          $block->content[] = compose_feature_media_gallery($node, $media_list, $gallery_settings);
171

    
172

    
173
          }
174
        /*
175
         * Footnotes for the feature block
176
         */
177
        $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => $node->feature->uuid)));
178
        $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => $node->feature->uuid)));
179
      } // END all other features
180

    
181
      if(!$block_content_is_empty){ // skip empty block content
182
        $block_list[] = $block;
183
        cdm_toc_list_add_item(cdm_term_representation($node->feature), $node->feature->uuid);
184
      } // END: skip empty block content
185
    } // END: skip empty or supressed features
186
  } // END: creating a block per feature
187

    
188
  drupal_alter('cdm_feature_node_blocks', $block_list, $taxon);
189

    
190
  RenderHints::popFromRenderStack();
191

    
192
  return _block_get_renderable_array($block_list);
193
}
194

    
195
  /**
196
   * @param $node
197
   * @param $media_list
198
   * @param $gallery_settings
199
   * @return array
200
   */
201
  function compose_feature_media_gallery($node, $media_list, $gallery_settings) {
202
    if (isset($node->descriptionElements)) {
203
      $media_list = array_merge($media_list, cdm_dataportal_media_from_descriptionElements($node->descriptionElements));
204
    }
205
    $captionElements = array('title', 'rights');
206
    $gallery = '';
207
    if (isset($gallery_settings['cdm_dataportal_media_maxextend']) && isset($gallery_settings['cdm_dataportal_media_cols'])) {
208
      $gallery = theme('cdm_media_gallerie', array(
209
        'mediaList' => $media_list,
210
        'galleryName' => CDM_DATAPORTAL_DESCRIPTION_GALLERY_NAME . '_' . $node->feature->uuid,
211
        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
212
        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
213
        'captionElements' => $captionElements,
214
      ));
215
      return markup_to_render_array($gallery);
216
    }
217
    return markup_to_render_array($gallery);
218
  }
219

    
220
  /**
221
   * @param $taxon
222
   * @param $descriptionElements
223
   *   an associative array with two elements:
224
   *   - '#type': must be 'DTO'
225
   *   - 'DistributionInfoDTO': a CDM DistributionInfoDTO object as returned by the DistributionInfo web service
226
   * @param $feature
227
   *
228
   * @ingroup compose
229
   */
230
  function compose_feature_block_distribution($taxon, $descriptionElements, $feature) {
231
    $text_data_glue = '';
232
    $text_data_sortOutArray = FALSE;
233
    $text_data_enclosingTag = 'ul';
234
    $text_data_out_array = array();
235

    
236
    $distributionElements = NULL;
237
    $distribution_info_dto = NULL;
238
    $distribution_sortOutArray = FALSE;
239

    
240
    if (variable_get('distribution_sort', 'NO_SORT') != 'NO_SORT') {
241
      $distribution_glue = '';
242
      $distribution_enclosingTag = 'dl';
243
    } else {
244
      $distribution_glue = '';
245
      $distribution_enclosingTag = 'ul';
246
    }
247

    
248
    if (!isset($descriptionElements['#type']) || !$descriptionElements['#type'] == 'DTO') {
249
      // skip the DISTRIBUTION section if there is no DTO type element
250
      return array(); // FIXME is it ok to return an empty array?
251
    }
252

    
253
    $block = feature_block(
254
      cdm_term_representation($feature, 'Unnamed Feature'),
255
      $feature
256
    );
257

    
258
    // $$descriptionElements['TextData'] is added to to the feature node in merged_taxon_feature_tree()
259
    if (isset($descriptionElements['TextData'])) {
260
      // --- TextData
261
      foreach ($descriptionElements['TextData'] as $text_data_element) {
262
        $asListElement = FALSE;
263
        $text_data_render_array = compose_cdm_descriptionElementTextData($text_data_element, $asListElement, $text_data_element->feature->uuid);
264
        $repr = drupal_render($text_data_render_array);
265

    
266
        if (!array_search($repr, $text_data_out_array)) { // de-duplication !!
267
          $text_data_out_array[] = $repr;
268
          // TODO HINT: sorting in compose_cdm_feature_block_elements will
269
          // not work since this array contains html attributes with uuids
270
          // and what is about cases like the bibliography where
271
          // any content can be prefixed with some foot-note anchors?
272
          $text_data_sortOutArray = TRUE;
273
          $text_data_glue = '<br/> ';
274
          $text_data_enclosingTag = 'p';
275
        }
276
      }
277
    }
278

    
279

    
280
    if ($text_data_out_array && variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
281
      $block->content[] = compose_cdm_feature_block_elements(
282
        $text_data_out_array,
283
        $feature,
284
        $text_data_glue,
285
        $text_data_sortOutArray,
286
        $text_data_enclosingTag
287
      );
288
    }
289

    
290
    // --- Distribution map
291
    $distribution_map_query_parameters = NULL;
292
    if (isset($descriptionElements['DistributionInfoDTO'])) {
293
      $distribution_map_query_parameters = $descriptionElements['DistributionInfoDTO']->mapUriParams;
294
    }
295
    $map_render_element = compose_distribution_map($taxon, $distribution_map_query_parameters);
296
    $block->content[] = $map_render_element;
297

    
298
    // --- tree or list
299
    $dto_out_array = array();
300
    if (isset($descriptionElements['DistributionInfoDTO'])) {
301
      $distribution_info_dto = $descriptionElements['DistributionInfoDTO'];
302

    
303
      // --- tree
304
      if (is_object($distribution_info_dto->tree)) {
305
        $dto_out_array[] = theme('cdm_description_ordered_distributions', array('distribution_tree' => $distribution_info_dto->tree));
306
      }
307

    
308
      // --- sorted element list
309
      if (is_array($distribution_info_dto->elements) && count($distribution_info_dto->elements) > 0) {
310
        foreach ($distribution_info_dto->elements as $descriptionElement) {
311
          if (is_object($descriptionElement->area)) {
312
            $sortKey = $descriptionElement->area->representation_L10n;
313
            $distributionElements[$sortKey] = $descriptionElement;
314
          }
315
        }
316
        ksort($distributionElements);
317
        $distribution_element_render_array = compose_cdm_descriptionElement_Distribution($distributionElements);
318
        $dto_out_array[] = drupal_render($distribution_element_render_array);
319

    
320
      }
321
      //
322
      $block->content[] = compose_cdm_feature_block_elements(
323
        $dto_out_array,
324
        $feature,
325
        $distribution_glue,
326
        $distribution_sortOutArray,
327
        $distribution_enclosingTag
328
      );
329
    }
330

    
331
    // --- TextData at the bottom
332
    if ($text_data_out_array && !variable_get(DISTRIBUTION_TEXTDATA_DISPLAY_ON_TOP, 0)) {
333
      $block->content[] = compose_cdm_feature_block_elements(
334
        $text_data_out_array,
335
        $feature,
336
        $text_data_glue,
337
        $text_data_sortOutArray,
338
        $text_data_enclosingTag
339
      );
340
    }
341

    
342
    $block->content[] = markup_to_render_array(theme('cdm_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
343
    $block->content[] = markup_to_render_array(theme('cdm_annotation_footnotes', array('footnoteListKey' => UUID_DISTRIBUTION)));
344

    
345
    return $block;
346
  }
347

    
348

    
349
  /**
350
 * @todo Please document this function.
351
 * @see http://drupal.org/node/1354
352
 */
353
function theme_FeatureTree_hierarchy($variables) {
354

    
355
  $FeatureTreeUuid = $variables['FeatureTreeUuid'];
356
  if (!is_uuid($FeatureTreeUuid)) {
357
    return;
358
  }
359

    
360
  $out = '';
361
  $featureTree = cdm_ws_get(CDM_WS_FEATURETREE, array(
362
    $FeatureTreeUuid,
363
  ));
364

    
365
  if (isset($featureTree) && isset($featureTree->root)) {
366
    $out = '<ul class="' . $featureTree->class . '">';
367
    $out .= theme('FeatureTree_hierarchy_children', array('node' => $featureTree->root));
368
    $out .= '</ul>';
369
  }
370
  return $out;
371
}
372

    
373
/**
374
 * @todo Please document this function.
375
 * @see http://drupal.org/node/1354
376
 */
377
function theme_FeatureTree_hierarchy_children($variables) {
378

    
379
  $node = $variables['node'];
380
  $out = '';
381
  if (isset($node->childNodes)) {
382

    
383
    foreach ($node->childNodes as $childNode) {
384
      $out .= '<li>' . check_plain($childNode->feature->representation_L10n);
385
      if (isset($childNode->childNodes) && count($childNode->childNodes) > 0) {
386
        $out .= '<ul>' . theme('FeatureTree_hierarchy_children', array('node' => $childNode)) . '</ul>';
387
      }
388
      $out .= '</li>';
389
    }
390
  }
391
  return $out;
392
}
393

    
394
/**
395
 * Returns HTML for the texts in a description $elements.
396
 *
397
 * Joins the texts in $elements and encloses with a HTML tag.
398
 *
399
 * @param  $elements
400
 * @param  feature:
401
 *  The feature to which the elements given in $elements are belonging to.
402
 * @param  glue:
403
 *  Defaults to empty string.
404
 * @param $sort
405
 *   Boolean Whether to sort the $elements alphabetically, default is FALSE
406
 * @param  $enclosing_tag
407
 *
408
 * @ingroup compose
409
 */
410
function compose_cdm_feature_block_elements($elements, $feature, $glue = '', $sort = FALSE, $enclosing_tag = 'ul') {
411

    
412
  $feature_block_settings = get_feature_block_settings($feature->uuid);
413
  if($feature_block_settings['as_list']){
414
    $enclosing_tag = $feature_block_settings['as_list'];
415
  }
416

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

    
419
  if ($sort) {
420
    sort($elements);
421
  }
422

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

    
427
  $out .= '</' . $enclosing_tag . '>';
428
  return markup_to_render_array($out);
429
}
430

    
431

    
432
/**
433
 * Theme function to render CDM DescriptionElements of the type CategoricalData.
434
 *
435
 * @param array $variables
436
 *   An associative array containing:
437
 *  - element: the CategoricalData element
438
 * @return a html representation of the given CategoricalData element
439
 *
440
 * @ingroup themeable
441
 */
442
function theme_cdm_descriptionElement_CategoricalData($variables) {
443
  $element = $variables['element'];
444

    
445
  $state_data_strings = array();
446
  if (isset($element->stateData)) {
447
    foreach ($element->stateData as $stateData) {
448

    
449
      $state  = NULL;
450

    
451
      if(isset($stateData->state)){
452
        $state = cdm_term_representation($stateData->state);
453
      }
454

    
455
      if (isset($stateData->modifyingText_L10n)) {
456
        $state = ' ' . $stateData->modifyingText_L10n;
457
      }
458

    
459
      $modifiers_strings = cdm_modifers_representations($stateData);
460

    
461
      $state_data_strings[] = $state . ($modifiers_strings ? ' ' . $modifiers_strings : '');
462

    
463
    }
464
  }
465

    
466
  $footnote_key_list_str = cdm_create_description_element_footnotes($element);
467

    
468
  $out = '<span class="' . html_class_attribute_ref($element) . '">' . implode(', ', $state_data_strings) . '</span>';
469
  return $out . $footnote_key_list_str;
470
}
471

    
472
/**
473
 * Theme function to render CDM DescriptionElements of the type QuantitativeData.
474
 *
475
 * @param array $variables
476
 *   An associative array containing:
477
 *  - element: the QuantitativeData element
478
 * @return a html representation of the given QuantitativeData element
479
 *
480
 * @ingroup themeable
481
 */
482
function theme_cdm_descriptionElement_QuantitativeData($variables) {
483
  /*
484
   * - statisticalValues
485
   *   - value
486
   *   - modifiers
487
   *   - type
488
   * - unit->representation_L10n
489
   * - modifyingText
490
   * - modifiers
491
   * - sources
492
   */
493
  $element = $variables['element'];
494

    
495
  $out = '';
496

    
497
  $type_representation = NULL;
498
  $modifiers_strings = array();
499

    
500

    
501
  if (isset($element->statisticalValues)) {
502
    $value_array = array();
503
    foreach ($element->statisticalValues as $val) {
504
      if (isset($val->value)) {
505
        $value_array[] = $val->value;
506
      }
507
    }
508

    
509
    $out .= implode($value_array, ', ');
510
  }
511

    
512
  if (isset($element->unit)) {
513
    $out .= ' '. cdm_term_representation($element->unit);
514
  }
515

    
516
  if (isset($element->statisticalValues->modifyingText_L10n)) {
517
    $out .=  ' ' . $element->statisticalValues->modifyingText_L10n;
518
  }
519
  $modifiers_strings = cdm_modifers_representations($element->statisticalValues);
520
  $out .= ($modifiers_strings ? ' ' . cdm_modifers_representations($element->statisticalValues) : '');
521

    
522
  $footnote_key_list_str = cdm_create_description_element_footnotes($element);
523

    
524
  return $out . $footnote_key_list_str;
525

    
526
}
527

    
528
/**
529
 * Theme function to render CDM DescriptionElements of the type IndividualsAssociations.
530
 *
531
 * @param array $variables
532
 *   An associative array containing:
533
 *  - element: the IndividualsAssociations element
534
 * @return html representation of the given IndividualsAssociations element
535
 *
536
 * @ingroup themeable
537
 */
538
function theme_cdm_descriptionElement_IndividualsAssociation($variables) {
539

    
540
  $element = $variables['element'];
541

    
542
  $out = '';
543

    
544
  $render_array = compose_cdm_specimenOrObservation($element->associatedSpecimenOrObservation);
545

    
546
  if (isset($element->description_L10n)) {
547
    $out .=  ' ' . $element->description_L10n;
548
  }
549

    
550
  $out .= drupal_render($render_array);
551

    
552
  $footnote_key_list_str = cdm_create_description_element_footnotes($element);
553

    
554
  return $out . $footnote_key_list_str;
555

    
556

    
557
}
558

    
559
/**
560
 * Theme function to render CDM DescriptionElements of the type TaxonInteraction.
561
 *
562
 * @param array $variables
563
 *   An associative array containing:
564
 *  - element: the TaxonInteraction element
565
 * @return html representation of the given TaxonInteraction element
566
 *
567
 * @ingroup themeable
568
 */
569
function theme_cdm_descriptionElement_TaxonInteraction($variables) {
570

    
571
  $element = $variables['element'];
572

    
573
  $out = '';
574

    
575
  if (isset($element->description_L10n)) {
576
    $out .=  ' ' . $element->description_L10n;
577
  }
578

    
579
  if(isset($element->taxon2)){
580
    $out = theme('cdm_taxonName',
581
        array(
582
            'taxonName' => $element->taxon2->name,
583
            'nameLink' => url(path_to_taxon($element->taxon2->uuid))
584
        )
585
      );
586
  }
587

    
588
  $footnote_key_list_str = cdm_create_description_element_footnotes($element);
589

    
590
  return $out . $footnote_key_list_str;
591

    
592

    
593
}
594

    
595
/**
596
 * Composes a drupal render array for CDM TextData description elements.
597
 *
598
 * @param $element
599
 *    The CDM TextData description element which contains the textual  information.
600
 * @param $asListElement
601
 *    A boolean which determines whether the citations should
602
 *     be rendered as a list or not.
603
 *    TODO remove and replace $asListElement by $feature_block_settings['as_list']
604
 *  @param $feature_uuid
605
 *
606
 * @return array
607
 *   A drupal render array with the following elements being used:
608
 *    - #tag: either 'div', 'li', ...
609
 *    ⁻ #attributes: class attributes
610
 *    - #value_prefix: (optionally) contains footnote anchors
611
 *    - #value: contains the textual content
612
 *    - #value_suffix: (optionally) contains footnote keys
613
 *
614
 * @ingroup compose
615
 */
616
function compose_cdm_descriptionElementTextData($element, $asListElement, $feature_uuid) {
617

    
618
  $footnote_list_key_suggestion = $feature_uuid;
619

    
620
  // FIXME $feature_block_settings should be passed as parameter!!!!!
621
  $feature_block_settings = get_feature_block_settings($feature_uuid);
622
  $do_references_inline = $feature_block_settings['sources_as_content'];
623

    
624
  // FIXME remove this hack ---------------------------
625
  $default_theme = variable_get('theme_default', NULL);
626
  if($default_theme == 'garland_cichorieae' || $default_theme == 'cyprus'
627
    || $default_theme == 'flore_afrique_centrale' || $default_theme == 'flora_malesiana' || $default_theme == 'flore_gabon' ){
628
    $asListElement = $feature_block_settings['as_list'] == 'ul';
629
  }
630
  // --------------------------------------------------
631

    
632
  $render_array = array(
633
    '#type' => 'html_tag',
634
    '#tag' => $asListElement ? 'li' : 'span',
635
    '#attributes' => array(
636
      'class' => 'DescriptionElement DescriptionElement-' . $element->class . '
637
      ' .  html_class_attribute_ref($element)
638
    ),
639
    '#value' => '',
640
    '#value_suffix' => NULL
641
  );
642

    
643
  $element_text = '';
644
  if (isset($element->multilanguageText_L10n->text)) {
645
    // TODO replacement of \n by <br> should be configurable
646
    $element_text = str_replace("\n", "<br/>", $element->multilanguageText_L10n->text);
647
  }
648

    
649
  $annotations_and_sources = handle_annotations_and_sources($element, $do_references_inline, $feature_block_settings, $element_text, $footnote_list_key_suggestion);
650

    
651
  $names_used_in_source_markup = '';
652
  if (!empty($annotations_and_sources['names_used_in_source']) && empty($element_text)) {
653
    // $element_text ==  NULL  usually occurs only in the case of CITATIONS!!!
654
    $names_used_in_source_markup = join(', ', $annotations_and_sources['names_used_in_source']) . ': ';
655
  }
656

    
657
  $source_references_markup = '';
658
  if(!empty($annotations_and_sources['source_references'])){
659
    $source_references_markup = '<span class="sources">'. join(' ', $annotations_and_sources['source_references']) . '<span>';
660
  }
661

    
662
  $render_array['#value'] = $names_used_in_source_markup . $element_text . $source_references_markup;
663
  $render_array['#value-suffix'] = $annotations_and_sources['foot_note_keys'];
664

    
665
  return $render_array;
666
}
667

    
668
  /**
669
   * @param $element
670
   * @param $do_references_inline
671
   * @param $feature_block_settings
672
   * @param $element_text
673
   * @param $footnote_list_key_suggestion
674
   * @return array
675
   *   an associative array with the following elements:
676
   *   - foot_note_keys: all footnote keys as markup
677
   *   - source_references: an array of the source references citations
678
   *   - names used in source: an associative array of the names in source,
679
   *        the name in source string are deduplicated
680
   *        !!!NOTE!!!!: this field will most probably be removed soon (TODO)
681
   *
682
   *
683
   */
684
  function handle_annotations_and_sources($element, $do_references_inline, $feature_block_settings, $element_text, $footnote_list_key_suggestion) {
685
    $annotations_and_sources = array(
686
      'foot_note_keys' => NULL,
687
      'source_references' => array(),
688
      'names_used_in_source' => array()
689
    );
690

    
691
    if ($do_references_inline) {
692
      foreach ($element->sources as $source) {
693

    
694
        $referenceCitation = theme('cdm_OriginalSource',
695
          array(
696
            'source' => $source,
697
            'doLink' => $feature_block_settings['link_to_reference']
698
          )
699
        );
700

    
701
        if ($referenceCitation) {
702
          if (empty($element_text)) {
703
            $annotations_and_sources['source_references'][] = $referenceCitation;
704
          }
705
          else {
706
            $annotations_and_sources['source_references'][] = ' (' . $referenceCitation . ')';
707
          }
708
        }
709

    
710
        if (isset($source->nameUsedInSource->uuid) && isset($source->nameUsedInSource->titleCache)) {
711
          // it is a DescriptionElementSource
712
          if ($feature_block_settings['link_to_name_used_in_source']) {
713
            // using the titleCache as key to avoid duplicate entries
714
            $annotations_and_sources['names_used_in_source'][$source->nameUsedInSource->titleCache] = l(
715
              $source->nameUsedInSource->titleCache,
716
              path_to_name($source->nameUsedInSource->uuid),
717
              array(
718
                'attributes' => array(),
719
                'absolute' => TRUE,
720
                'html' => TRUE,
721
              ));
722
          }
723
          else {
724
            // using the titleCache as key to avoid duplicate entries
725
            $annotations_and_sources['names_used_in_source'][$source->nameUsedInSource->titleCache] = $source->nameUsedInSource->titleCache;
726
          }
727
        }
728
        else if (isset($source->originalNameString) && !empty($source->originalNameString)) {
729
          // the name used in source can not be expressed as valid taxon name,
730
          // so the editor has chosen to put the freetext name into ReferencedEntityBase.originalNameString
731
          // field
732
          // using the originalNameString as key to avoid duplicate entries
733
          $annotations_and_sources['names_used_in_source'][$source->originalNameString] = $source->originalNameString;
734
        }
735
      } // END of loop over sources
736

    
737
      // annotations footnotes separate.
738
      $annotations_and_sources['foot_note_keys'] = theme('cdm_annotations_as_footnotekeys',
739
        array(
740
          'cdmBase_list' => $element,
741
          'footnote_list_key' => $footnote_list_key_suggestion,
742
        )
743
      );
744

    
745
    } // END of references inline
746

    
747
    if (!$do_references_inline || $feature_block_settings['sources_as_content_to_bibliography']) {
748
      $annotations_and_sources['foot_note_keys'] = cdm_create_description_element_footnotes($element, ',', $footnote_list_key_suggestion);
749
      return $annotations_and_sources;
750
    }
751
    return $annotations_and_sources;
752
  }
753

    
754
  /**
755
 * Composes block of common names for the given DescriptionElements $elements which must be of the feature CommonName
756
 *
757
 * @parameter $elements
758
 *  an array of CDM DescriptionElements either of type CommonName or TextData
759
 * @parameter $feature
760
 *  the common feature of all $elements, must be CommonName
761
 *
762
 * @return
763
 *   A drupal render array
764
 *
765
 * @ingroup compose
766
 */
767
function compose_cdm_common_names($elements, $feature, $weight = FALSE) {
768

    
769
  $common_name_out = '';
770
  $common_name_feature_elements = array();
771
  $textData_commonNames = array();
772

    
773
  $footnote_key_suggestion = 'common-names-feature-block';
774

    
775
  if (is_array($elements)) {
776
    foreach ($elements as $element) {
777

    
778
      if ($element->class == 'CommonTaxonName') {
779

    
780
        // common name without a language or area, should not happen but is possible
781
        $language_area_key = '';
782
        if (isset($element->language->representation_L10n)) {
783
          $language_area_key .= '<b>' . $element->language->representation_L10n . '</b>';
784
        }
785
        if(isset($element->area->titleCache) && strlen($element->area->titleCache) > 0){
786
          $language_area_key .= ($language_area_key ? ' '  : '') . '(' . $element->area->titleCache . ')';
787
        }
788

    
789
        if(isset($common_names[$language_area_key][$element->name])) {
790
          // same name already exists for language and areae combination, se we merge the description elements
791
          cdm_merge_description_elements($common_names[$language_area_key][$element->name], $element);
792
        } else{
793
          // otherwise add as new entry
794
          $common_names[$language_area_key][$element->name] = $element;
795
        }
796

    
797
      }
798
      elseif ($element->class == 'TextData') {
799
        $textData_commonNames[] = $element;
800
      }
801
    }
802
  }
803
  // Handling common names.
804
  if (isset($common_names) && count($common_names) > 0) {
805
    // Sorting the array based on the key (language, + area if set).
806
    // Comment @WA there are common names without a language, so this sorting
807
    // can give strange results.
808
    ksort($common_names);
809

    
810
    // loop over set of elements per language area
811
    foreach ($common_names as $language_area_key => $elements) {
812
      ksort($elements); // sort names alphabetically
813
      $per_language_area_out = array();
814
      // loop over set of individual elements
815
      foreach ($elements as $element) {
816
        if ($element->name) {
817
          $per_language_area_out[] = '<span class="' . html_class_attribute_ref($element) . '">'
818
          . $element->name . cdm_create_description_element_footnotes($element, ',', $footnote_key_suggestion) . '</span>';
819
        }
820
      } // End of loop over set of individual elements
821
      $common_name_feature_elements[] = ($language_area_key ? $language_area_key . ': ' : '' ) . join(', ', $per_language_area_out);
822
    } // End of loop over set of elements per language area
823

    
824

    
825
    $common_name_feature_elements_render_array = compose_cdm_feature_block_elements(
826
      $common_name_feature_elements,
827
      $feature,
828
      '; ',
829
      FALSE,
830
      'div'
831
      );
832
    $common_name_out .= $common_name_feature_elements_render_array['#markup'];
833

    
834
  }
835

    
836
  // Handling commons names as text data.
837
  $text_data_out = array();
838

    
839
  foreach ($textData_commonNames as $text_data_element) {
840
    /* footnotes are not handled correctly in theme_cdm_descriptionElementTextData,
841
       need to set 'common-names-feature-block' as $footnote_key_suggestion */
842
    RenderHints::setFootnoteListKey($footnote_key_suggestion);
843
    $text_data_render_array = compose_cdm_descriptionElementTextData($text_data_element, TRUE, $text_data_element->feature->uuid);
844
    $text_data_out[] = drupal_render($text_data_render_array);
845
  }
846

    
847
  $common_name_out_text_data = compose_cdm_feature_block_elements(
848
        $text_data_out,
849
        $feature
850
      );
851

    
852

    
853
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => $footnote_key_suggestion)); // FIXME is this needed at all?
854
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnote_key_suggestion));
855

    
856
  return  markup_to_render_array(
857
    '<div class="common_names_as_common_names">' . $common_name_out . '</div>'
858
    .'<div class="common_names_as_text_data">' . $common_name_out_text_data['#markup'] . '</div>'
859
    .$footnotes,
860
    $weight
861
  );
862
}
863

    
864
/**
865
 * Return HTML for a list of description elements.
866
 *
867
 * Usually these are of a specific feature type.
868
 *
869
 * @param $descriptionElements
870
 *   array of descriptionElements which belong to the same feature.
871
 *   These descriptions elements of a Description must be ordered by the chosen feature tree by
872
 *   calling the function _mergeFeatureTreeDescriptions().
873
 *   @see _mergeFeatureTreeDescriptions()
874
 *
875
 * @param  $feature_uuid
876
 *
877
 * @return
878
 *    A drupal render array for the $descriptionElements, may be an empty array if the textual content was empty.
879
 *    Footnote key or anchors are not considered to be textual content.
880
 *
881
 * @ingroup compose
882
 */
883
function compose_cdm_descriptionElements($descriptionElements, $feature) {
884

    
885
  $render_array = array();
886
  $elements_out_array = array();
887
  $distribution_tree = null;
888

    
889
  /*
890
   * $feature_block_has_content will be set true if at least one of the
891
   * $descriptionElements contains some text which makes up some content
892
   * for the feature block. Footnote keys are not considered
893
   * to be content in this sense.
894
   */
895
  $feature_block_has_content = false;
896

    
897
  /*
898
  $userDefined = mixed_variable_get(LAYOUT_SETTING_PREFIX . $feature_uuid, FEATURE_TREE_LAYOUT_DEFAULTS);
899
  if(variable_get('distribution_sort',
900
      'NO_SORT') != 'NO_SORT'){
901
      $glue = '';
902
      $enclosingTag = 'dl';
903
      $entryEnclosingTag = NULL;
904
  } else if($userDefined &&
905
      $userDefined['enabled']){
906
    $glue = $userDefined['glue'];
907
    $enclosingTag =  $userDefined['enclosingTag'];
908
    $entryEnclosingTag = $userDefined['entryEnclosingTag'];
909
  } else { // TODO remove once  LAYOUT_SETTING_PREFIX-{uuid} setting are configured to work for all portals(selenium test still missing!!!)
910
    $glue = ''; $enclosingTag = 'ul';
911
    $entryEnclosingTag = NULL ;
912
  }
913
  */
914

    
915
  RenderHints::pushToRenderStack('cdm_descriptionElements');
916

    
917
  if (is_array($descriptionElements)) {
918
    // --- normal description element arrays
919
    foreach ($descriptionElements as $descriptionElement) {
920

    
921
      // --- IMAGE_SOURCES --- //
922
      if ($descriptionElement->feature->uuid == UUID_IMAGE_SOURCES) {
923
        $image_sources[] = $descriptionElement;
924
      }
925
      // --- USE TEXTDATA --- //
926
      elseif ($descriptionElement->feature->uuid == UUID_USE) {
927
        // Do nothing to avoid rendering.
928
      } else {
929
        /* decide based on the description element class
930
         *
931
         * Features handled here:
932
         * all except DISTRIBUTION, COMMON_NAME, USES, IMAGES,
933
         *
934
         *
935
         * TODO provide api_hook as extension point for this?
936
         */
937
        $feature_block_settings = get_feature_block_settings($descriptionElement->feature->uuid);
938
        $asListElement  = $feature_block_settings['as_list'] == 'ul';
939
        switch ($descriptionElement->class) {
940
          case 'TextData':
941
              $text_data_render_array = compose_cdm_descriptionElementTextData($descriptionElement, $asListElement, $descriptionElement->feature->uuid);
942
              $feature_block_has_content = $feature_block_has_content || !empty($text_data_render_array['#value']);
943
              $elements_out_array[] = drupal_render($text_data_render_array);
944
            break;
945
          case 'CategoricalData':
946
            $feature_block_has_content = true;
947
            $elements_out_array[] = theme('cdm_descriptionElement_CategoricalData', array('element' => $descriptionElement));
948
            break;
949
          case 'QuantitativeData':
950
            $feature_block_has_content = true;
951
            $elements_out_array[] = theme('cdm_descriptionElement_QuantitativeData', array('element' => $descriptionElement));
952
            break;
953
          case 'IndividualsAssociation':
954
            $feature_block_has_content = true;
955
            $elements_out_array[] = theme('cdm_descriptionElement_IndividualsAssociation', array('element' => $descriptionElement));
956
            break;
957
          case 'TaxonInteraction':
958
            $feature_block_has_content = true;
959
            $elements_out_array[] = theme('cdm_descriptionElement_TaxonInteraction', array('element' => $descriptionElement));
960
            break;
961
          case 'Uses':
962
          /* IGNORE Uses classes, these are handled completely in theme_cdm_UseDescription */
963
          break;
964
        default:
965
          $feature_block_has_content = true;
966
          $elements_out_array[] = '<li>No method for rendering unknown description class: ' . $descriptionElement->class . '</li>';
967
      }
968
    }
969
    } // --- END loop over normal description element arrays
970

    
971
    // If feature = CITATION sort the list of sources.
972
    // This is ONLY for FLORA MALESIANA and FLORE d'AFRIQUE CENTRALE.
973
    if ($descriptionElement->feature->uuid == UUID_CITATION) {
974
      sort($elements_out_array);
975
    }
976

    
977
    if (isset($image_sources)) {
978
      $elements_out_array[] = theme('cdm_description_element_image_source', array('image_sources' => $image_sources, 'asListElement' => TRUE));
979
    }
980
  } // END normal description element arrays
981

    
982
  if($feature_block_has_content){
983
    $render_array = compose_cdm_feature_block_elements(
984
      $elements_out_array,
985
      $feature
986
    );
987
  }
988

    
989
  RenderHints::popFromRenderStack();
990
  return $render_array;
991
}
992

    
993
/**
994
 * @todo Please document this function.
995
 * @see http://drupal.org/node/1354
996
 */
997
function compare_image_sources($a, $b) {
998
  if ($a->multilanguageText_L10n->text == $b->multilanguageText_L10n->text) {
999
    return 0;
1000
  }
1001
  return ($a->multilanguageText_L10n->text < $b->multilanguageText_L10n->text) ? -1 : 1;
1002
}
1003

    
1004
/**
1005
 * @todo Please document this function.
1006
 * @see http://drupal.org/node/1354
1007
 */
1008
function theme_cdm_description_element_image_source($variables) {
1009
  $image_sources = $variables['image_sources'];
1010
  $asListElement = $variables['asListElement'];
1011
  $out = '';
1012
  $separator = ',';
1013
  RenderHints::pushToRenderStack('descriptionElementImageSource');
1014
  RenderHints::setFootnoteListKey(UUID_IMAGE_SOURCES);
1015

    
1016
  // Sorting the image sources.
1017
  usort($image_sources, "compare_image_sources");
1018
  // Generate the footnotes.
1019
  foreach ($image_sources as $image_source) {
1020
    $footNoteKeys = cdm_annotations_as_footnotekeys($image_source);
1021
    foreach ($image_source->sources as $source) {
1022
      if (_is_original_source_type($source)) {
1023
        $fn_key = FootnoteManager::addNewFootnote(original_source_footnote_list_key(), theme('cdm_OriginalSource', array(
1024
          'source' => $source,
1025
          'doLink' => FALSE,
1026
        )));
1027
        // Ensure uniqueness of the footnote keys.
1028
        cdm_add_footnote_to_array($footNoteKeys, $fn_key);
1029
      }
1030
    }
1031
    // Sort and render footnote keys.
1032
    $footnote_key_list_str = '';
1033
    asort($footNoteKeys);
1034
    foreach ($footNoteKeys as $footNoteKey) {
1035
      $footnote_key_list_str .= theme('cdm_footnote_key', array('footnoteKey' => $footNoteKey, 'separator' => ($footnote_key_list_str ? $separator : '')));
1036
    }
1037
    // Return value!
1038
    $out .= '<span class="descriptionElement descriptionElement-' . $image_source->uuid . '">' . $image_source->multilanguageText_L10n->text . $footnote_key_list_str . '; </span>';
1039
  }
1040

    
1041
  RenderHints::popFromRenderStack();
1042
  return $out;
1043
}
1044

    
1045
/**
1046
 * Composes the render array for a CDM Distribution element
1047
 *
1048
 * @param $descriptionElements
1049
 *   The CDM Distribution element
1050
 * @param $enclosingTag
1051
 *   The html tag to be use for the enclosing element
1052
 *
1053
 * @return array
1054
 *   A Drupal render array
1055
 *
1056
 * @ingroup compose
1057
 */
1058
function compose_cdm_descriptionElement_Distribution($descriptionElements, $enclosingTag = "span"){
1059

    
1060
  $out = '';
1061
  RenderHints::pushToRenderStack('descriptionElementDistribution');
1062
  RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1063

    
1064
  foreach ($descriptionElements as $descriptionElement) {
1065
    $footnote_key_list_str = cdm_create_description_element_footnotes($descriptionElement);
1066

    
1067
    $out .= '<' . $enclosingTag . ' class="descriptionElement descriptionElement-' . $descriptionElement->uuid . '">'
1068
        . $descriptionElement->area->representation_L10n . $footnote_key_list_str
1069
        . ' </' . $enclosingTag . '>';
1070
  }
1071

    
1072
  RenderHints::popFromRenderStack();
1073
  return markup_to_render_array($out);
1074
}
1075

    
1076

    
1077
/**
1078
 * Compare two different footnotes objects.
1079
 *
1080
 * The comparison is based on the footnote key. The one which is
1081
 * displayed as footnote number.
1082
 *
1083
 * @param mixed $a
1084
 *   Footnote object $a.
1085
 * @param mixed $b
1086
 *   Footnote object $b.
1087
 */
1088
function footnotes_key_compare($a, $b) {
1089
  $res = 0;
1090
  if (empty($a) || empty($b)) {
1091
    return $res;
1092
  }
1093
  if ($a->keyStr < $b->keyStr) {
1094
    $res = -1;
1095
  }
1096
  elseif ($a->keyStr > $b->keyStr) {
1097
    $res = 1;
1098
  }
1099
  return $res;
1100
}
1101

    
1102
/**
1103
 * @todo Please document this function.
1104
 * @see http://drupal.org/node/1354
1105
 */
1106
function theme_cdm_description_ordered_distributions($variables) {
1107

    
1108
  $distribution_tree = $variables['distribution_tree'];
1109

    
1110
  // Returning NULL if there are no description elements.
1111
  if ($distribution_tree == null) {
1112
    return NULL;
1113
  }
1114

    
1115
  // Initialization of some variables.
1116
  $out = '';
1117
  $separator = ',';
1118
  RenderHints::pushToRenderStack('descriptionElementDistribution');
1119
  RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1120

    
1121
  $ordered_areas = $distribution_tree;
1122
  if (isset($ordered_areas->rootElement->children)) {
1123
    $ordered_areas = $ordered_areas->rootElement->children;
1124
  }
1125

    
1126
  // Printing the distributions.
1127
  if (is_array($ordered_areas) && count($ordered_areas) > 0) {
1128
    foreach ($ordered_areas as $element_level1) {
1129
      // Level1.
1130
      $out .= '<dt>' . $element_level1->nodeId->representation_L10n . ':</dt> ';
1131
      $out .= '<dd>';
1132

    
1133
      // Level3.
1134
      foreach ($element_level1->children as $element_level3) {
1135
        $text_l3 = $element_level3->nodeId->representation_L10n;
1136
        $fnKeysLevel3Str = '';
1137
        $fnKeysLevel3 = cdm_annotations_as_footnotekeys($element_level3->data);
1138
        if (isset($element_level3->data[0])) {
1139
          foreach ($element_level3->data as $description_level3){
1140
            foreach ($description_level3->sources as $source) {
1141
              if (_is_original_source_type($source)) {
1142
                $fn_key3 = FootnoteManager::addNewFootnote(
1143
                  original_source_footnote_list_key(),
1144
                  theme('cdm_OriginalSource', array('source' => $source, 'doLink' => FALSE)
1145
                ));
1146
                cdm_add_footnote_to_array($fnKeysLevel3, $fn_key3);
1147
              }
1148
            }
1149
          }
1150
        }
1151
        // Level4.
1152
        $l4_regions = array();
1153
        foreach ($element_level3->children as $element_level4) {
1154
          if (isset($element_level4->data[0])) {
1155
            $text_l4 = $element_level4->nodeId->representation_L10n;
1156
            $l4_regions[$element_level3->nodeId->representation_L10n] = array();
1157
            $fnKeysLevel4Str = '';
1158
            foreach($element_level4->data as $description_level4) {
1159
              $fnKeysLevel4 = cdm_annotations_as_footnotekeys($description_level4);
1160
              foreach ($description_level4->sources as $source) {
1161
                if (_is_original_source_type($source)) {
1162
                  $fn_key4 = FootnoteManager::addNewFootnote(
1163
                      original_source_footnote_list_key(),
1164
                      theme('cdm_OriginalSource', array('source' => $source, 'doLink' => FALSE)));
1165
                  cdm_add_footnote_to_array($fnKeysLevel4, $fn_key4);
1166
                }
1167
              }
1168
              usort($fnKeysLevel4, "footnotes_key_compare");
1169
              if(!isset( $l4_regions[$text_l4])){
1170
                $l4_regions[$text_l4] = $fnKeysLevel4;
1171
              } else {
1172
                $l4_regions[$text_l4] = array_merge($l4_regions[$text_l4], $fnKeysLevel4);
1173
              }
1174
            }
1175
          }
1176
        }// Level4.
1177
        // Managing level3 and level4 for generating the right output.
1178
        usort($fnKeysLevel3, "footnotes_key_compare");
1179
        foreach ($fnKeysLevel3 as $key3) {
1180
          foreach ($l4_regions as $key4 => $value4) {
1181
            cdm_add_footnote_to_array($l4_regions[$key4], $key3);
1182
          }
1183
        }
1184
        if ($element_level3->numberOfChildren == 1 && $text_l3 == $element_level3->children[0]->nodeId->representation_L10n) {
1185
          // var_dump($element_level3->children[0]->data->area->representation_L10n);
1186
          $fnStr = '';
1187
          $region = array_pop($l4_regions);
1188
          foreach ($region as $key) {
1189
            $fnStr .= theme('cdm_footnote_key', array('footnoteKey' => $key, 'separator' => ($fnStr ? $separator : '')));
1190
          }
1191
          $out .= "$text_l3 $fnStr; ";
1192
          // Removing whitespaces when &fnStr is empty.
1193
          if (substr($out, -3) == ' ; ') {
1194
            $out = substr($out, 0, -3) . '; ';
1195
          }
1196
        } else {
1197
          $fnKeysLevel3Str = '';
1198
          foreach ($fnKeysLevel3 as $key) {
1199
            $fnKeysLevel3Str .= theme('cdm_footnote_key', array('footnoteKey' => $key, 'separator' => ($fnKeysLevel3Str ? $separator : '')));
1200
          }
1201
          $text_l4_aux = '';
1202
          foreach ($l4_regions as $key => $value) {
1203
            $fnKeysLevel4Str = '';
1204
            if (is_array($l4_regions[$key])) {
1205
              foreach ($l4_regions[$key] as $fnkey) {
1206
                $fnKeysLevel4Str .= theme('cdm_footnote_key', array('footnoteKey' => $fnkey, 'separator' => ($fnKeysLevel4Str ? $separator : '')));
1207
              }
1208
            }
1209
            // if ($key != $text_l3 || sizeof($l4_regions > 1)){
1210
            if ($key != $text_l3) {
1211
              $text_l4_aux .= "$key $fnKeysLevel4Str, ";
1212
            }
1213
          }
1214
          $text_l4_aux = substr($text_l4_aux, 0, -2);
1215

    
1216
          if (strlen($text_l4_aux) > 0) {
1217
            $out .= "$text_l3 $fnKeysLevel3Str ($text_l4_aux); ";
1218
          }
1219
          else {
1220
            $out .= "$text_l3 $fnKeysLevel3Str; ";
1221
          }
1222
        }
1223
      }// Level3.
1224
      $out = substr($out, 0, -2);
1225
      $out .= '.</dd>';
1226
    }// Level1.
1227
  }
1228
  RenderHints::popFromRenderStack();
1229
  return $out;
1230
}
1231

    
1232
/**
1233
 * Returns a list of a specific type of IdentificationKeys.
1234
 *
1235
 * The list can be restricteded by a taxon.
1236
 *
1237
 * @param string $type
1238
 *   The simple name of the cdm class implementing the interface
1239
 *   IdentificationKey, valid values are:
1240
 *   PolytomousKey, MediaKey, MultiAccessKey.
1241
 * @param string $taxonUuid
1242
 *   If given this parameter restrict the listed keys to those which have
1243
 *   the taxon identified be this uuid in scope.
1244
 *
1245
 * @return array
1246
 *   List with identification keys.
1247
 */
1248
function _list_IdentificationKeys($type, $taxonUuid = NULL, $pageSize = NULL, $pageNumber = NULL) {
1249
  if (!$type) {
1250
    drupal_set_message(t('Type parameter is missing'), 'error');
1251
    return;
1252
  }
1253
  $cdm_ws_pasepath = NULL;
1254
  switch ($type) {
1255
    case "PolytomousKey":
1256
      $cdm_ws_pasepath = CDM_WS_POLYTOMOUSKEY;
1257
      break;
1258

    
1259
    case "MediaKey":
1260
      $cdm_ws_pasepath = CDM_WS_MEDIAKEY;
1261
      break;
1262

    
1263
    case "MultiAccessKey":
1264
      $cdm_ws_pasepath = CDM_WS_MULTIACCESSKEY;
1265
      break;
1266

    
1267
  }
1268

    
1269
  if (!$cdm_ws_pasepath) {
1270
    drupal_set_message(t('Type parameter is not valid: ') . $type, 'error');
1271
  }
1272

    
1273
  $queryParameters = '';
1274
  if (is_numeric($pageSize)) {
1275
    $queryParameters = "pageSize=" . $pageSize;
1276
  }
1277
  else {
1278
    $queryParameters = "pageSize=0";
1279
  }
1280

    
1281
  if (is_numeric($pageNumber)) {
1282
    $queryParameters = "pageNumber=" . $pageNumber;
1283
  }
1284
  else {
1285
    $queryParameters = "pageNumber=0";
1286
  }
1287
  $queryParameters = NULL;
1288
  if ($taxonUuid) {
1289
    $queryParameters = "findByTaxonomicScope=$taxonUuid";
1290
  }
1291
  $pager = cdm_ws_get($cdm_ws_pasepath, NULL, $queryParameters);
1292

    
1293
  if (!$pager || $pager->count == 0) {
1294
    return array();
1295
  }
1296
  return $pager->records;
1297
}
1298

    
1299
/**
1300
 * @todo Please document this function.
1301
 * @see http://drupal.org/node/1354
1302
 */
1303
function theme_cdm_IdentificationKey($variables) {
1304
  $out = '';
1305
  $identificationKey = $variables['identificationKey'];
1306
  $doLinkToKeyPage = $variables['doLinkToKeyPage'];
1307
  $showIdentificationKeyTitle = $variables['showIdentificationKeyTitle'];
1308
  $parentRenderPath = RenderHints::getRenderPath();
1309
  RenderHints::pushToRenderStack("IdentificationKey");
1310

    
1311
  if ($showIdentificationKeyTitle) {
1312
    if ($doLinkToKeyPage) {
1313
      $out = l($identificationKey->titleCache, path_to_key($identificationKey->class, $identificationKey->uuid));
1314
    }
1315
    else {
1316
      $out = $identificationKey->titleCache;
1317
    }
1318
  }
1319
  if (isset($identificationKey->sources) && is_array($identificationKey->sources)) {
1320
    // order and display sources.
1321
    $sources = oder_sources($identificationKey->sources, TRUE);
1322
    $out .= '<div class="sources">';
1323
    $out .=  implode('', $sources);
1324
    $out .= '</div>';
1325
  }
1326
  // Display annotations.
1327
  $out .= theme('cdm_annotations', array('annotations' => cdm_ws_getAnnotationsFor($identificationKey), 'enclosingTag' => 'div'));
1328
  RenderHints::popFromRenderStack();
1329
  return $out;
1330
}
1331

    
1332
/**
1333
 * @todo Please document this function.
1334
 * @see http://drupal.org/node/1354
1335
 */
1336
function theme_cdm_polytomousKey($variables) {
1337
  $polytomousKey = $variables['polytomousKey'];
1338

    
1339
  // TODO settings needed.
1340
  // @see http://en.wikipedia.org/wiki/Single_access_key#Presentation_styles
1341
  // @see http://dev.e-taxonomy.eu/trac/ticket/2152
1342
  $keyStyle = "linkedStyle";
1343

    
1344
  RenderHints::pushToRenderStack("polytomousKey");
1345
  // Key nodes in linked style.
1346
  $out = '<table class="polytomousKey polytomousKey_' . $keyStyle . '">';
1347
  $out .= theme('cdm_polytomousKey_' . $keyStyle . '_subgraph', array('polytomousKeyNode' => $polytomousKey->root));
1348
  $out .= '</table>';
1349
  RenderHints::popFromRenderStack();
1350
  return $out;
1351
}
1352

    
1353
/**
1354
 * @todo Please document this function.
1355
 * @see http://drupal.org/node/1354
1356
 */
1357
function theme_cdm_polytomousKey_linkedStyle_subgraph($variables) {
1358
  $polytomousKeyNode = $variables['polytomousKeyNode'];
1359
  static $statementCountCharacter = '\'';
1360
  $out = "";
1361

    
1362
  if (is_array($polytomousKeyNode->children)) {
1363
    $childIndex = 0;
1364

    
1365
    // Render edges of the current node.
1366
    foreach ($polytomousKeyNode->children as &$child) {
1367

    
1368
      if (!isset($child->statement) && isset($child->taxon->uuid)) {
1369
        // Skip node with empty statements (see below for explanation: "Special
1370
        // case").
1371
        // this skipping here happens always in the next deeper level of iteration
1372
        // the check below is node on the level above
1373
        continue;
1374
      }
1375

    
1376
      /*
1377
       * Special case: Child nodes with empty statements but taxa as leaf are to
1378
       * treated as if all those taxa where direct children of the source node.
1379
       */
1380
      $islinkToManyTaxa = !isset($child->children[0]->statement) && isset($child->children[0]->taxon->uuid);
1381
      $islinkToTaxon = isset($child->taxon->uuid);
1382
      $islinkToSubKey = isset($child->subkey->uuid);
1383
      $islinkToOtherNode = isset($child->otherNode);
1384
      // Either NULL or 0.
1385
      $islinkToNode = $child->nodeNumber && !$islinkToManyTaxa && !$islinkToOtherNode;
1386
      $hasQuestion = !empty($polytomousKeyNode->question->label_l10n);
1387
      $hasFeature = isset($polytomousKeyNode->feature);
1388

    
1389
      // $indentEdge = $hasQuestion && $childIndex > 0;
1390
      // Question.
1391
      if ($hasQuestion && $childIndex == 0) {
1392
        // Place question, as extra table row.
1393
        $out .= '<tr class="question new_section">';
1394
        $out .= '<td class="nodeNumber">' . uuid_anchor($polytomousKeyNode->uuid, $polytomousKeyNode->nodeNumber) . "</td>";
1395
        $out .= '<td class="question">' . $polytomousKeyNode->question->label_l10n . '</td>';
1396
        $out .= '</tr>';
1397
      }
1398

    
1399
      $out .= '<tr class="childCount_' . $childIndex . (!$hasQuestion && $childIndex == 0 ? ' new_section' : '') . '">';
1400

    
1401
      if ($hasQuestion) {
1402
        $out .= '<td class="nodeNumber"></td>';
1403
      }
1404
      else {
1405
        $out .= '<td class="nodeNumber">' . uuid_anchor($polytomousKeyNode->uuid, $polytomousKeyNode->nodeNumber . str_pad("", $childIndex, $statementCountCharacter)) . "</td>";
1406
      }
1407

    
1408
      $out .= '<td ' . RenderHints::getHtmlElementID($child) . '  class="edge' . ($hasQuestion ? ' edge-indent' : '') . '">';
1409

    
1410
      // Feature.
1411
      if ($hasFeature) {
1412
        $out .= $polytomousKeyNode->feature->representation_L10n . ": ";
1413
      }
1414

    
1415
      // Statement.
1416
      $out .= $child->statement->label_l10n;
1417

    
1418
      // --- Links to nodes taxa and subkeys.
1419
      $out .= '<div class="nodeLink">';
1420

    
1421
      // Link to a PolytomousKeyNode.
1422
      if ($islinkToNode) {
1423
        $out .= '<div class="nodeLinkToNode">';
1424
        if (isset($child->modifyingText)) {
1425
          $out .= theme('cdm_poytomousKeyNode_modifyingText', array('modifyingText' => $child->modifyingText));
1426
        }
1427
        $out .= l($child->nodeNumber, request_path(), array(
1428
          'attributes' => NULL,
1429
          'query' => NULL,
1430
          'fragment' => $child->uuid,
1431
        )) . '</div>';
1432
      }
1433

    
1434
      // Link to a PolytomousKeyNode.
1435
      if ($islinkToOtherNode) {
1436
        $out .= '<div class="nodeLinkToOtherNode">';
1437
        if (isset($child->modifyingText)) {
1438
          $out .= theme('cdm_poytomousKeyNode_modifyingText', array('modifyingText' => $child->modifyingText));
1439
        }
1440
        $out .= l($child->otherNode->nodeNumber, $_REQUEST["q"], array(
1441
          'attributes' => NULL,
1442
          'query' => NULL,
1443
          'fragment' => $child->otherNode->uuid,
1444
        )) . '</div>';
1445
      }
1446

    
1447
      // Link to one or many taxa.
1448
      if ($islinkToTaxon || $islinkToManyTaxa) {
1449

    
1450
        if ($islinkToManyTaxa) {
1451
          $taxonChildren = $child->children;
1452
        }
1453
        else {
1454
          $taxonChildren = array(
1455
            $child,
1456
          );
1457
        }
1458

    
1459
        foreach ($taxonChildren as $taxonChild) {
1460
          // TODO many taxa $child->children->taxon.
1461
          $out .= '<div class="nodeLinkToTaxon">';
1462
          if (isset($taxonChild->modifyingText)) {
1463
            $out .= theme('cdm_poytomousKeyNode_modifyingText', array('modifyingText' => $taxonChild->modifyingText));
1464
          }
1465
          $out .= theme("cdm_taxonName", array('taxonName' => $taxonChild->taxon->name, 'nameLink' => url(path_to_taxon($taxonChild->taxon->uuid))));
1466
          $out .= '</div>';
1467
        }
1468

    
1469
        // Link to a subkey.
1470
        if ($islinkToSubKey) {
1471
          $out .= '<div class="nodeLinkToSubkey">' . theme('cdm_IdentificationKey', array('identificationKey' => $child->subkey)) . '</div>';
1472
        }
1473
      }
1474

    
1475
      $out .= '</div>'; // End node link.
1476
      $out .= '</td>'; // End edge.
1477
      $out .= '</tr>';
1478

    
1479
      $childIndex++;
1480
    }
1481

    
1482
    // Recurse into child nodes.
1483
    foreach ($polytomousKeyNode->children as &$child) {
1484
      $out .= theme('cdm_polytomousKey_linkedStyle_subgraph', array('polytomousKeyNode' => $child));
1485
    }
1486
  }
1487

    
1488
  return $out;
1489
}
1490

    
1491
/**
1492
 * @todo Please document this function.
1493
 * @see http://drupal.org/node/1354
1494
 */
1495
function theme_cdm_poytomousKeyNode_modifyingText($variables) {
1496
  $out = '';
1497
  $modifyingText = $variables['modifyingText'];
1498
  if (is_object($modifyingText)) {
1499
    $i = 0;
1500
    foreach (get_object_vars($modifyingText) as $lang => $languageString) {
1501
      $out .= ($i++ > 0 ? ', ' : '') . '<span class="modifyingText">' . $languageString->text . '</span> ';
1502
    }
1503
  }
1504
  return $out;
1505
}
1506

    
1507
/**
1508
 * Returns HTML for a list of a specific type of IdentificationKeys.
1509
 *
1510
 * The list can be restricteded by a taxon.
1511
 *
1512
 * @param array $variables
1513
 *   An associative array containing:
1514
 *   - type: The simple name of the cdm class implementing the interface
1515
 *     IdentificationKey, valid values are:
1516
 *     PolytomousKey, MediaKey, MultiAccessKey
1517
 *   - taxonUuid: If given, this parameter restrict the listed keys to those
1518
 *     which have the taxon identified be this uuid in scope.
1519
 *
1520
 * @ingroup themeable
1521
 */
1522
function theme_cdm_list_IdentificationKeys($variables) {
1523
  $type = $variables['type'];
1524
  $taxonUuid = $variables['taxonUuid'];
1525
  $keyList = _list_IdentificationKeys($type, $taxonUuid);
1526
  if (!$keyList || count($keyList) == 0) {
1527
    return;
1528
  }
1529

    
1530
  RenderHints::pushToRenderStack('list_IdentificationKeys');
1531
  $out = '<ul>';
1532
  foreach ($keyList as $key) {
1533
    $out .= '<li>';
1534
    $out .= theme('cdm_IdentificationKey', array('identificationKey' => $key));
1535
    $out .= '</li>';
1536
  }
1537
  $out .= '</ul>';
1538
  $out .= theme("cdm_annotation_footnotes", array('footnoteListKey' => RenderHints::getRenderPath()));
1539
  RenderHints::popFromRenderStack();
1540

    
1541
  return $out;
1542
}
1543

    
1544
/**
1545
 * @todo Please document this function.
1546
 * @see http://drupal.org/node/1354
1547
 */
1548
function theme_cdm_block_IdentificationKeys($variables) {
1549
  $taxonUuid = $variables['taxonUuid'];
1550
  static $types = array(
1551
    "PolytomousKey" => "Polytomous",
1552
    "MediaKey" => "Media",
1553
    "MultiAccessKey" => "Multiaccess",
1554
  );
1555
  RenderHints::pushToRenderStack('block_IdentificationKeys');
1556
  $out = '';
1557
  foreach ($types as $type => $label) {
1558
    $keylist = theme('cdm_list_IdentificationKeys', array('type' => $type, 'taxonUuid' => $taxonUuid));
1559
    if (!$keylist) {
1560
      continue;
1561
    }
1562
    $out .= '<div class="' . $type . '">';
1563
    $out .= '<h3>' . t($label) . "</h3>";
1564
    $out .= $keylist;
1565
    $out .= '</div>';
1566
  }
1567
  RenderHints::popFromRenderStack();
1568
  return $out;
1569
}
1570

    
1571
/**
1572
 * This theming function formats the use description and use record list for
1573
 * these descriptions.
1574
 *
1575
 * @see http://drupal.org/node/1354
1576
 */
1577
function theme_cdm_UseDescription($variables) {
1578
  $descriptions = $variables['description'];
1579
  $taxonUuid = $variables['taxonUuid'];
1580
  $out = '<div id="content"><ul id="Description" class ="description">';
1581
  if ($descriptions == NULL) {
1582
    return;
1583
  }
1584
  $descriptionSynonyms = '';
1585
  $descriptionOut = '';
1586
  $synonymOut = '';
1587
  $currentTaxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $taxonUuid);
1588

    
1589
  foreach ($descriptions as $description) {
1590
    $useSummary = '';
1591
    foreach ($description->elements as $element) {
1592

    
1593
      if ($element->feature->uuid == UUID_USE && !(strlen($useSummary) > 0) && isset($element->multilanguageText_L10n)) {
1594
        $useSummary = $element->multilanguageText_L10n->text;
1595
      }
1596
    }
1597
    // uses will be ordered by source
1598
    foreach ($description->sources as $source) {
1599
      $is_about_current_taxon = FALSE;
1600
      $originalTaxonUsedInSource = NULL;
1601
      $originalTaxonPager = NULL;
1602
      if ($source->originalNameString) {
1603
        $request_params = array();
1604
        $request_params['query'] = $source->originalNameString;
1605
        $request_params['matchMode'] = "EXACT";
1606
        $originalTaxonPager = cdm_ws_get(CDM_WS_PORTAL_NAME_FINDBYNAME, NULL, queryString($request_params));
1607
        if ($originalTaxonPager->count > 0) {
1608
          $originalTaxonUsedInSource = $originalTaxonPager->records[0];
1609
        }
1610
        else {
1611
          $originalTaxonUsedInSource = $currentTaxon->name;
1612
        }
1613
      }
1614
      else {
1615
        $originalTaxonUsedInSource = $currentTaxon->name;
1616
      }
1617

    
1618
      $is_about_current_taxon = $currentTaxon->name->uuid == $originalTaxonUsedInSource->uuid;
1619

    
1620
      if (!$is_about_current_taxon) {
1621
        $descriptionOut .= '<li class="descriptionText DescriptionElement">';
1622
        $name_used_in_source_link_to_show_use = l($source->originalNameString, path_to_name($originalTaxonUsedInSource->uuid), array(
1623
          'absolute' => TRUE,
1624
          'html' => TRUE,
1625
        ));
1626
        $descriptionOut .= $name_used_in_source_link_to_show_use . ': ';
1627
        $descriptionOut .= $useSummary;
1628
        foreach ($description->sources as $source) {
1629
          $descriptionOut .= " (" . theme('cdm_OriginalSource', array('source' => $source, 'doLink' => TRUE)) . ")";
1630
        }
1631
        $hasUseRecords = FALSE;
1632
        $descriptionUseRecordOut = '<div id=useRecords><table><th>Use Category</th><th>Use Sub Category</th><th>Plant Part</th><th>Human Group</th><th>Ethnic Group</th><th>Country</th>';
1633
        foreach ($description->elements as $descriptionElement) {
1634
          if ($descriptionElement->feature->uuid == UUID_USE_RECORD) {
1635
            $hasUseRecords = TRUE;
1636
            // FIXME localization hardcoded to English
1637
            $useRecordTags = explode(';', $descriptionElement->modifyingText_l10n);
1638
            $descriptionUseRecordOut .= '<tr>';
1639
            $descriptionUseRecordOut .= '<td>' . $useRecordTags[0] . '</td>' . '<td>' . $useRecordTags[1] . '</td>' . '<td>' . $useRecordTags[3] . '</td>' . '<td>' . $useRecordTags[4] . '</td>' . '<td>' . $useRecordTags[5] . '</td>' . '<td>' . $useRecordTags[2] . '</td>';
1640
            $descriptionUseRecordOut .= '</tr>';
1641
          }
1642
        }
1643
        $descriptionUseRecordOut .= '</table></div>';
1644
        if ($hasUseRecords) {
1645
          $descriptionOut .= $descriptionUseRecordOut . '</li>';
1646
        }
1647
      }
1648
      else {
1649
        // TODO +/- duplicate of above, unify this code
1650
        $synonymOut .= '<li class="descriptionText DescriptionElement">';
1651
        $name_used_in_source_link_to_show_use = l($source->originalNameString, path_to_name($originalTaxonUsedInSource->uuid), array(
1652
          'absolute' => TRUE,
1653
          'html' => TRUE,
1654
        ));
1655

    
1656
        $synonymOut .= $name_used_in_source_link_to_show_use . ': ';
1657
        $synonymOut .= $useSummary;
1658
        foreach ($description->sources as $source) {
1659
          $synonymOut .= " (" . theme('cdm_OriginalSource', array('source' => $source, 'doLink' => TRUE)) . ")";
1660
        }
1661

    
1662
        $hasUseRecords = FALSE;
1663
        $useRecordTableOut = '<div id=useRecords><table><th>Use Category</th><th>Use Sub Category</th><th>Plant Part</th><th>Human Group</th><th>Ethnic Group</th><th>Country</th>';
1664
        foreach ($description->elements as $descriptionElement) {
1665
          if ($descriptionElement->feature->uuid == UUID_USE_RECORD) {
1666
            $hasUseRecords = TRUE;
1667
            $useRecordTags = explode(';', $descriptionElement->modifyingText_l10n);
1668
            $useRecordTableOut .= '<tr>';
1669
            $useRecordTableOut .= '<td>' . $useRecordTags[0] . '</td>' . '<td>' . $useRecordTags[1] . '</td>' . '<td>' . $useRecordTags[3] . '</td>' . '<td>' . $useRecordTags[4] . '</td>' . '<td>' . $useRecordTags[5] . '</td>' . '<td>' . $useRecordTags[2] . '</td>';
1670
            $useRecordTableOut .= '</tr>';
1671
          }
1672
        }
1673
        $useRecordTableOut .= '</table></div>';
1674
        if ($hasUseRecords) {
1675
          $synonymOut .= $useRecordTableOut . '</li>';
1676
        }
1677
      }
1678

    
1679
      // }
1680
    }
1681
  }
1682
  $out .= $descriptionOut . $synonymOut;
1683
  $out .= "</ul></div>";
1684
  return $out;
1685
}
1686

    
1687

    
1688
/**
1689
 * The theming function for a block of Uses Descriptions for a given taxon.
1690
 *
1691
 * The Uses block has been removed from the code but the according theme function
1692
 * is kept for compatibility reasons with existing code regarding palmweb.
1693
 *
1694
 */
1695
function theme_cdm_block_Uses($variables) {
1696
  $taxonUuid = $variables['taxonUuid'];
1697
  RenderHints::pushToRenderStack('block_Uses');
1698

    
1699
  if ($taxonUuid == NULL) {
1700
    return;
1701
  }
1702
  $out = '';
1703
  $markerTypes = array();
1704
  $markerTypes['markerTypes'] = UUID_MARKERTYPE_USE;
1705
  $useDescriptions = cdm_ws_get(CDM_WS_PORTAL_TAXON_DESCRIPTIONS, $taxonUuid, queryString($markerTypes));
1706
  if (!empty($useDescriptions)) {
1707
    // FIXME use theme_block instaed of hardcoding the block html here !!!!
1708
    $out .= '<div id="block-cdm_dataportal-feature-description" class="clear-block block block-cdm_dataportal-feature"><H2><a name="userecords"> </a> Uses </H2>';
1709
    $formatUseDescriptions = theme('cdm_UseDescription', array('description' => $useDescriptions, 'taxonUuid' => $taxonUuid));
1710

    
1711
    $out .= $formatUseDescriptions;
1712
    $out .= "</div>";
1713
  }
1714

    
1715
  return $out;
1716
}
(3-3/10)