Project

General

Profile

Download (60.6 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

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

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

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

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

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

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

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

    
664
  return $render_array;
665
}
666

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

    
689
    if ($feature_block_settings['sources_as_content']) {
690
      foreach ($element->sources as $source) {
691

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

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

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

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

    
743
    } // END of references inline
744

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

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

    
767
  $common_name_out = '';
768
  $common_name_feature_elements = array();
769
  $textData_commonNames = array();
770

    
771
  $footnote_key_suggestion = 'common-names-feature-block';
772

    
773
  if (is_array($elements)) {
774
    foreach ($elements as $element) {
775

    
776
      if ($element->class == 'CommonTaxonName') {
777

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

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

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

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

    
822

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

    
832
  }
833

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

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

    
845
  $common_name_out_text_data = compose_cdm_feature_block_elements(
846
        $text_data_out,
847
        $feature
848
      );
849

    
850

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

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

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

    
883
  $render_array = array();
884
  $elements_out_array = array();
885
  $distribution_tree = null;
886

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

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

    
913
  RenderHints::pushToRenderStack('cdm_descriptionElements');
914

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

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

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

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

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

    
987
  RenderHints::popFromRenderStack();
988
  return $render_array;
989
}
990

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

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

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

    
1039
  RenderHints::popFromRenderStack();
1040
  return $out;
1041
}
1042

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

    
1058
  $out = '';
1059
  RenderHints::pushToRenderStack('descriptionElementDistribution');
1060
  RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1061

    
1062
  $feature_block_settings = get_feature_block_settings(UUID_DISTRIBUTION);
1063

    
1064
  foreach ($descriptionElements as $descriptionElement) {
1065
    $annotations_and_sources = handle_annotations_and_sources(
1066
      $descriptionElement,
1067
      $feature_block_settings,
1068
      $descriptionElement->area->representation_L10n,
1069
      UUID_DISTRIBUTION
1070
    );
1071

    
1072

    
1073
    $out .= '<' . $enclosingTag . ' class="descriptionElement descriptionElement-' . $descriptionElement->uuid . '">'
1074
        . $descriptionElement->area->representation_L10n . $annotations_and_sources['foot_note_keys']
1075
        . ' </' . $enclosingTag . '>';
1076
  }
1077

    
1078
  RenderHints::popFromRenderStack();
1079
  return markup_to_render_array($out);
1080
}
1081

    
1082

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

    
1108
/**
1109
 * @todo Please document this function.
1110
 * @see http://drupal.org/node/1354
1111
 */
1112
function theme_cdm_description_ordered_distributions($variables) {
1113

    
1114
  $distribution_tree = $variables['distribution_tree'];
1115

    
1116
  // Returning NULL if there are no description elements.
1117
  if ($distribution_tree == null) {
1118
    return NULL;
1119
  }
1120

    
1121
  // Initialization of some variables.
1122
  $out = '';
1123
  $separator = ',';
1124
  RenderHints::pushToRenderStack('descriptionElementDistribution');
1125
  RenderHints::setFootnoteListKey(UUID_DISTRIBUTION);
1126

    
1127
  $ordered_areas = $distribution_tree;
1128
  if (isset($ordered_areas->rootElement->children)) {
1129
    $ordered_areas = $ordered_areas->rootElement->children;
1130
  }
1131

    
1132
  // Printing the distributions.
1133
  if (is_array($ordered_areas) && count($ordered_areas) > 0) {
1134
    foreach ($ordered_areas as $element_level1) {
1135
      // Level1.
1136
      $out .= '<dt>' . $element_level1->nodeId->representation_L10n . ':</dt> ';
1137
      $out .= '<dd>';
1138

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

    
1222
          if (strlen($text_l4_aux) > 0) {
1223
            $out .= "$text_l3 $fnKeysLevel3Str ($text_l4_aux); ";
1224
          }
1225
          else {
1226
            $out .= "$text_l3 $fnKeysLevel3Str; ";
1227
          }
1228
        }
1229
      }// Level3.
1230
      $out = substr($out, 0, -2);
1231
      $out .= '.</dd>';
1232
    }// Level1.
1233
  }
1234
  RenderHints::popFromRenderStack();
1235
  return $out;
1236
}
1237

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

    
1265
    case "MediaKey":
1266
      $cdm_ws_pasepath = CDM_WS_MEDIAKEY;
1267
      break;
1268

    
1269
    case "MultiAccessKey":
1270
      $cdm_ws_pasepath = CDM_WS_MULTIACCESSKEY;
1271
      break;
1272

    
1273
  }
1274

    
1275
  if (!$cdm_ws_pasepath) {
1276
    drupal_set_message(t('Type parameter is not valid: ') . $type, 'error');
1277
  }
1278

    
1279
  $queryParameters = '';
1280
  if (is_numeric($pageSize)) {
1281
    $queryParameters = "pageSize=" . $pageSize;
1282
  }
1283
  else {
1284
    $queryParameters = "pageSize=0";
1285
  }
1286

    
1287
  if (is_numeric($pageNumber)) {
1288
    $queryParameters = "pageNumber=" . $pageNumber;
1289
  }
1290
  else {
1291
    $queryParameters = "pageNumber=0";
1292
  }
1293
  $queryParameters = NULL;
1294
  if ($taxonUuid) {
1295
    $queryParameters = "findByTaxonomicScope=$taxonUuid";
1296
  }
1297
  $pager = cdm_ws_get($cdm_ws_pasepath, NULL, $queryParameters);
1298

    
1299
  if (!$pager || $pager->count == 0) {
1300
    return array();
1301
  }
1302
  return $pager->records;
1303
}
1304

    
1305
/**
1306
 * @todo Please document this function.
1307
 * @see http://drupal.org/node/1354
1308
 */
1309
function theme_cdm_IdentificationKey($variables) {
1310
  $out = '';
1311
  $identificationKey = $variables['identificationKey'];
1312
  $doLinkToKeyPage = $variables['doLinkToKeyPage'];
1313
  $showIdentificationKeyTitle = $variables['showIdentificationKeyTitle'];
1314
  $parentRenderPath = RenderHints::getRenderPath();
1315
  RenderHints::pushToRenderStack("IdentificationKey");
1316

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

    
1338
/**
1339
 * @todo Please document this function.
1340
 * @see http://drupal.org/node/1354
1341
 */
1342
function theme_cdm_polytomousKey($variables) {
1343
  $polytomousKey = $variables['polytomousKey'];
1344

    
1345
  // TODO settings needed.
1346
  // @see http://en.wikipedia.org/wiki/Single_access_key#Presentation_styles
1347
  // @see http://dev.e-taxonomy.eu/trac/ticket/2152
1348
  $keyStyle = "linkedStyle";
1349

    
1350
  RenderHints::pushToRenderStack("polytomousKey");
1351
  // Key nodes in linked style.
1352
  $out = '<table class="polytomousKey polytomousKey_' . $keyStyle . '">';
1353
  $out .= theme('cdm_polytomousKey_' . $keyStyle . '_subgraph', array('polytomousKeyNode' => $polytomousKey->root));
1354
  $out .= '</table>';
1355
  RenderHints::popFromRenderStack();
1356
  return $out;
1357
}
1358

    
1359
/**
1360
 * @todo Please document this function.
1361
 * @see http://drupal.org/node/1354
1362
 */
1363
function theme_cdm_polytomousKey_linkedStyle_subgraph($variables) {
1364
  $polytomousKeyNode = $variables['polytomousKeyNode'];
1365
  static $statementCountCharacter = '\'';
1366
  $out = "";
1367

    
1368
  if (is_array($polytomousKeyNode->children)) {
1369
    $childIndex = 0;
1370

    
1371
    // Render edges of the current node.
1372
    foreach ($polytomousKeyNode->children as &$child) {
1373

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

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

    
1395
      // $indentEdge = $hasQuestion && $childIndex > 0;
1396
      // Question.
1397
      if ($hasQuestion && $childIndex == 0) {
1398
        // Place question, as extra table row.
1399
        $out .= '<tr class="question new_section">';
1400
        $out .= '<td class="nodeNumber">' . uuid_anchor($polytomousKeyNode->uuid, $polytomousKeyNode->nodeNumber) . "</td>";
1401
        $out .= '<td class="question">' . $polytomousKeyNode->question->label_l10n . '</td>';
1402
        $out .= '</tr>';
1403
      }
1404

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

    
1407
      if ($hasQuestion) {
1408
        $out .= '<td class="nodeNumber"></td>';
1409
      }
1410
      else {
1411
        $out .= '<td class="nodeNumber">' . uuid_anchor($polytomousKeyNode->uuid, $polytomousKeyNode->nodeNumber . str_pad("", $childIndex, $statementCountCharacter)) . "</td>";
1412
      }
1413

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

    
1416
      // Feature.
1417
      if ($hasFeature) {
1418
        $out .= $polytomousKeyNode->feature->representation_L10n . ": ";
1419
      }
1420

    
1421
      // Statement.
1422
      $out .= $child->statement->label_l10n;
1423

    
1424
      // --- Links to nodes taxa and subkeys.
1425
      $out .= '<div class="nodeLink">';
1426

    
1427
      // Link to a PolytomousKeyNode.
1428
      if ($islinkToNode) {
1429
        $out .= '<div class="nodeLinkToNode">';
1430
        if (isset($child->modifyingText)) {
1431
          $out .= theme('cdm_poytomousKeyNode_modifyingText', array('modifyingText' => $child->modifyingText));
1432
        }
1433
        $out .= l($child->nodeNumber, request_path(), array(
1434
          'attributes' => NULL,
1435
          'query' => NULL,
1436
          'fragment' => $child->uuid,
1437
        )) . '</div>';
1438
      }
1439

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

    
1453
      // Link to one or many taxa.
1454
      if ($islinkToTaxon || $islinkToManyTaxa) {
1455

    
1456
        if ($islinkToManyTaxa) {
1457
          $taxonChildren = $child->children;
1458
        }
1459
        else {
1460
          $taxonChildren = array(
1461
            $child,
1462
          );
1463
        }
1464

    
1465
        foreach ($taxonChildren as $taxonChild) {
1466
          // TODO many taxa $child->children->taxon.
1467
          $out .= '<div class="nodeLinkToTaxon">';
1468
          if (isset($taxonChild->modifyingText)) {
1469
            $out .= theme('cdm_poytomousKeyNode_modifyingText', array('modifyingText' => $taxonChild->modifyingText));
1470
          }
1471
          $out .= theme("cdm_taxonName", array('taxonName' => $taxonChild->taxon->name, 'nameLink' => url(path_to_taxon($taxonChild->taxon->uuid))));
1472
          $out .= '</div>';
1473
        }
1474

    
1475
        // Link to a subkey.
1476
        if ($islinkToSubKey) {
1477
          $out .= '<div class="nodeLinkToSubkey">' . theme('cdm_IdentificationKey', array('identificationKey' => $child->subkey)) . '</div>';
1478
        }
1479
      }
1480

    
1481
      $out .= '</div>'; // End node link.
1482
      $out .= '</td>'; // End edge.
1483
      $out .= '</tr>';
1484

    
1485
      $childIndex++;
1486
    }
1487

    
1488
    // Recurse into child nodes.
1489
    foreach ($polytomousKeyNode->children as &$child) {
1490
      $out .= theme('cdm_polytomousKey_linkedStyle_subgraph', array('polytomousKeyNode' => $child));
1491
    }
1492
  }
1493

    
1494
  return $out;
1495
}
1496

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

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

    
1536
  RenderHints::pushToRenderStack('list_IdentificationKeys');
1537
  $out = '<ul>';
1538
  foreach ($keyList as $key) {
1539
    $out .= '<li>';
1540
    $out .= theme('cdm_IdentificationKey', array('identificationKey' => $key));
1541
    $out .= '</li>';
1542
  }
1543
  $out .= '</ul>';
1544
  $out .= theme("cdm_annotation_footnotes", array('footnoteListKey' => RenderHints::getRenderPath()));
1545
  RenderHints::popFromRenderStack();
1546

    
1547
  return $out;
1548
}
1549

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

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

    
1595
  foreach ($descriptions as $description) {
1596
    $useSummary = '';
1597
    foreach ($description->elements as $element) {
1598

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

    
1624
      $is_about_current_taxon = $currentTaxon->name->uuid == $originalTaxonUsedInSource->uuid;
1625

    
1626
      if (!$is_about_current_taxon) {
1627
        $descriptionOut .= '<li class="descriptionText DescriptionElement">';
1628
        $name_used_in_source_link_to_show_use = l($source->originalNameString, path_to_name($originalTaxonUsedInSource->uuid), array(
1629
          'absolute' => TRUE,
1630
          'html' => TRUE,
1631
        ));
1632
        $descriptionOut .= $name_used_in_source_link_to_show_use . ': ';
1633
        $descriptionOut .= $useSummary;
1634
        foreach ($description->sources as $source) {
1635
          $descriptionOut .= " (" . theme('cdm_OriginalSource', array('source' => $source, 'doLink' => TRUE)) . ")";
1636
        }
1637
        $hasUseRecords = FALSE;
1638
        $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>';
1639
        foreach ($description->elements as $descriptionElement) {
1640
          if ($descriptionElement->feature->uuid == UUID_USE_RECORD) {
1641
            $hasUseRecords = TRUE;
1642
            // FIXME localization hardcoded to English
1643
            $useRecordTags = explode(';', $descriptionElement->modifyingText_l10n);
1644
            $descriptionUseRecordOut .= '<tr>';
1645
            $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>';
1646
            $descriptionUseRecordOut .= '</tr>';
1647
          }
1648
        }
1649
        $descriptionUseRecordOut .= '</table></div>';
1650
        if ($hasUseRecords) {
1651
          $descriptionOut .= $descriptionUseRecordOut . '</li>';
1652
        }
1653
      }
1654
      else {
1655
        // TODO +/- duplicate of above, unify this code
1656
        $synonymOut .= '<li class="descriptionText DescriptionElement">';
1657
        $name_used_in_source_link_to_show_use = l($source->originalNameString, path_to_name($originalTaxonUsedInSource->uuid), array(
1658
          'absolute' => TRUE,
1659
          'html' => TRUE,
1660
        ));
1661

    
1662
        $synonymOut .= $name_used_in_source_link_to_show_use . ': ';
1663
        $synonymOut .= $useSummary;
1664
        foreach ($description->sources as $source) {
1665
          $synonymOut .= " (" . theme('cdm_OriginalSource', array('source' => $source, 'doLink' => TRUE)) . ")";
1666
        }
1667

    
1668
        $hasUseRecords = FALSE;
1669
        $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>';
1670
        foreach ($description->elements as $descriptionElement) {
1671
          if ($descriptionElement->feature->uuid == UUID_USE_RECORD) {
1672
            $hasUseRecords = TRUE;
1673
            $useRecordTags = explode(';', $descriptionElement->modifyingText_l10n);
1674
            $useRecordTableOut .= '<tr>';
1675
            $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>';
1676
            $useRecordTableOut .= '</tr>';
1677
          }
1678
        }
1679
        $useRecordTableOut .= '</table></div>';
1680
        if ($hasUseRecords) {
1681
          $synonymOut .= $useRecordTableOut . '</li>';
1682
        }
1683
      }
1684

    
1685
      // }
1686
    }
1687
  }
1688
  $out .= $descriptionOut . $synonymOut;
1689
  $out .= "</ul></div>";
1690
  return $out;
1691
}
1692

    
1693

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

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

    
1717
    $out .= $formatUseDescriptions;
1718
    $out .= "</div>";
1719
  }
1720

    
1721
  return $out;
1722
}
(2-2/9)