Project

General

Profile

Download (31 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Functions for dealing with CDM entities of type SpecimenOrOccurrences
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
 * @author
16
 *   - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
17
 */
18

    
19

    
20
/**
21
 * @param $preservedSpecimenDTO
22
 * @param $detail_html
23
 * @return string
24
 */
25
function compose_cdm_specimen_page($specimenUuid)
26
{
27
    drupal_set_title("Specimen Details");
28
    $preservedSpecimenDTO = cdm_ws_get(CDM_WS_PORTAL_OCCURRENCE, array($specimenUuid, 'specimenDerivates'));
29

    
30
    $render_array = array();
31
    RenderHints::pushToRenderStack('specimen_page');
32

    
33
    $detail_html = compose_cdm_specimen_page_html($preservedSpecimenDTO, true);
34
    $render_array['specimen_html'] = array(
35
        '#markup' => $detail_html
36
    );
37

    
38
    RenderHints::popFromRenderStack();
39
    return $render_array;
40
}
41

    
42
/**
43
 * @param $preservedSpecimenDTO
44
 * @return string
45
 */
46
function compose_cdm_specimen_page_html($preservedSpecimenDTO, $isSpecimenPage = false)
47
{
48
    $detail_html = "";
49
    //link to specimen page
50
    $pathToSpecimen = path_to_specimen($preservedSpecimenDTO->uuid);
51
    if (!$isSpecimenPage) {
52
        $specimenPageLink = l($preservedSpecimenDTO->accessionNumber, $pathToSpecimen, array('attributes' => array('target' => '_blank')));
53
        $detail_html .= "<strong>$specimenPageLink</strong><br>";
54
    }
55

    
56
    if($isSpecimenPage) {
57
        if($preservedSpecimenDTO->citation){
58
            $detail_html .= "<br>".create_label("Citation") . $preservedSpecimenDTO->citation . "<br>";
59
        }
60
    }
61
    if($preservedSpecimenDTO->preferredStableUri){
62
        $detail_html .= "<br>".create_label("Preferred stable URI") . l($preservedSpecimenDTO->preferredStableUri, $preservedSpecimenDTO->preferredStableUri) . "<br>";
63
    }
64
    if($isSpecimenPage){
65
        // associated taxa
66
        if($preservedSpecimenDTO->associatedTaxa){
67
            $detail_html .= "<br>";
68
            $detail_html .= create_label("Associated with");
69
                if(sizeof($preservedSpecimenDTO->associatedTaxa)>1){
70
                    $detail_html .= "<br>";
71
                }
72
            foreach($preservedSpecimenDTO->associatedTaxa as $associatedTaxon){
73
                $detail_html .= l($associatedTaxon->second, path_to_taxon($associatedTaxon->first, "specimens"));//$associatedTaxon->second."<br>";
74
            }
75
        }
76
    }
77
    // - type information
78
    $types = "";
79
    if (isset($preservedSpecimenDTO->types)) {
80
        //typed taxa
81
        if(sizeof($preservedSpecimenDTO->types)>1){
82
            $detail_html .= "<br>";
83
        }
84
        foreach ($preservedSpecimenDTO->types as $typeStatus => $typedTaxa) {
85
            if($isSpecimenPage){
86
                if($preservedSpecimenDTO->types){
87
                    $detail_html .= "<i>".$typeStatus."</i>: ";
88
                    foreach($typedTaxa as $typedTaxon){
89
                        $detail_html .= $typedTaxon." ";
90
                    }
91
                    $detail_html .= "<br>";
92
                }
93
            }
94
            else{
95
                $types .= $typeStatus." ";
96
            }
97
        }
98
    }
99
    $derivateDataDTO = $preservedSpecimenDTO->derivateDataDTO;
100
    // - specimen scans
101
    $specimenScans = create_html_links($derivateDataDTO->specimenScans, true);
102
    // - molecular data
103
    $molecularData = "";
104
    if ($derivateDataDTO->molecularDataList) {
105
        foreach ($derivateDataDTO->molecularDataList as $molecular) {
106
            //provider link
107
            if (isset($molecular->providerLink)) {
108
                $molecularData .= create_html_link($molecular->providerLink, true);
109
            }
110
            //contig link
111
            if (isset($molecular->contigFiles[0])) {//FIXME check if empty
112
                $molecularData .= "[";
113
                if (isset($molecular->contigFiles)) {
114
                    foreach ($molecular->contigFiles as $contigFile) {
115
                        if (isset($contigFile->contigLink)){
116
                            if(isset($contigFile->contigLink->uri) and $contigFile->contigLink->uri!=null) {
117
                               $molecularData .= create_html_link($contigFile->contigLink, true)." ";
118
                            }
119
                            //primer links
120
                            $molecularData .= create_html_links($contigFile->primerLinks, true);
121
                        }
122
                    }
123
                } else {
124
                    $molecularData .= "no contig";
125
                }
126
                $molecularData = rtrim($molecularData, " ");
127
                $molecularData .= "]";
128
            }
129
            //FIXME separate with comma (remove trailing comma)
130
        }
131
    }
132
    // - detail images
133
    $detailImages = create_html_links($derivateDataDTO->detailImages, true);
134

    
135
    if ($types) {
136
        $detail_html .= $isSpecimenPage?"<br>":"";
137
        $detail_html .= create_label("Type(s)") . $types . "<br>";
138
    }
139
    if ($specimenScans and !$isSpecimenPage) {
140
        $detail_html .= create_label("Specimen Scans") . $specimenScans . "<br>";
141
    }
142
    //specimen scan image gallery
143
    if($isSpecimenPage and isset($derivateDataDTO->specimenScanUuids) and !empty($derivateDataDTO->specimenScanUuids)) {
144
        $detail_html .= addImageGallery("Specimen scans", $derivateDataDTO->specimenScanUuids);
145
    }
146

    
147
    if ($molecularData) {
148
        $detail_html .= $isSpecimenPage?"<br>":"";
149
        $detail_html .= create_label("Molecular Data") . $molecularData . "<br>";
150
    }
151

    
152
    if ($detailImages and !$isSpecimenPage) {
153
        $detail_html .= create_label("Detail Images") . $detailImages . "<br>";
154
    }
155

    
156
    //detail image gallery
157
    if($isSpecimenPage and isset($derivateDataDTO->detailImageUuids) and !empty($derivateDataDTO->detailImageUuids)){
158
        $detail_html .= addImageGallery("Detail Images", $derivateDataDTO->detailImageUuids);
159
    }
160

    
161
    //character data
162
    if ($preservedSpecimenDTO->characterData) {
163
        $detail_html .= $isSpecimenPage?"<br>":"";
164
        $detail_html .= create_label("Character Data");
165
        if($isSpecimenPage) {
166
            $detail_html .= "<br>";
167
            foreach ($preservedSpecimenDTO->characterData as $characterStatePair) {
168
                $detail_html .= "<i>" . $characterStatePair->first . "</i>:" . $characterStatePair->second;
169
                $detail_html .= "<br>";
170
            }
171
        }
172
        else{
173
            $detail_html .= l("detail page", $pathToSpecimen,array('attributes' => array('target'=>'_blank')));
174
            $detail_html .= "<br>";
175
        }
176
    }
177
    return $detail_html;
178
}
179

    
180
function addImageGallery($galleryName, $imageUuids){
181
    $images = array();
182
    foreach ($imageUuids as $uuid) {
183
        $images[] = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $uuid);
184
    }
185

    
186
    $gallery_html = '';
187
    if (count($imageUuids) > 0) {
188
        $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
189
        $captionElements = array(
190
            'title',
191
            'rights',
192
        );
193
        $alternativeMediaUris = array();
194
        foreach($images as $image){
195
          $mediaUri = getMediaUri($image);
196
          if($mediaUri){
197
            $alternativeMediaUris[] = $mediaUri;
198
          }
199
          else{
200
            $alternativeMediaUris[] = path_to_media($image->uuid);
201
          }
202
        }
203

    
204
        $gallery_html = theme('cdm_media_gallerie', array(
205
            'mediaList' => $images,
206
            'galleryName' => $galleryName,
207
            'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
208
            'cols' => $gallery_settings['cdm_dataportal_media_cols'],
209
            'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
210
            'captionElements' => $captionElements,
211
            'mediaLinkType' => 'LIGHTBOX',
212
            'alternativeMediaUri' => $alternativeMediaUris,
213
            'galleryLinkUri' => NULL,
214
        ));
215
    }
216
    return "<br>".create_label($galleryName)."<br>".$gallery_html;
217
}
218

    
219
function getMediaUri($media){
220
  if(isset($media->representations) && sizeof($media->representations)==1
221
    && isset($media->representations[0]->parts) &&
222
    sizeof($media->representations[0]->parts)==1) {
223
    return $media->representations[0]->parts[0]->uri;
224
  }
225
  return null;
226
}
227

    
228

    
229
/**
230
 * Formats the given string to a label for displaying key-value pairs in HTML
231
 * @return string
232
 */
233
function create_label($label)
234
{
235
    return "<span class='specimen_table_label'>".$label.": </span>";
236
}
237

    
238
/**
239
 * Compose an render array from a CDM DerivedUnitFacade object.
240
 *
241
 * compose_hook() implementation
242
 *
243
 * @param object $specimenOrObservation
244
 *   the CDM instance of type SpecimenOrObservation to compose
245
 *   the render array for
246
 * @param array $derivatives
247
 *   the render array which contains the compositions of the derivatives
248
 *   of the supplied $specimenOrObservation
249
 *
250
 * @return array
251
 *   the supplied render array $derivatives to which the composition of the supplied
252
 *   $specimenOrObservation has been added to
253
 *
254
 * @ingroup compose
255
 */
256
function compose_cdm_specimenOrObservation($specimenOrObservation, &$derivatives = null) {
257

    
258
  $exclude_occurrence_fields = &drupal_static(__FUNCTION__);
259
  if (!isset($exclude_occurrence_fields)) {
260
     $exclude_occurrence_fields = array(
261
        'derivationEvents',
262
        'extensions', // TODO ignored for now, how to handle extensions?
263
        'titleCache',
264
        'protectedTitleCache',
265
        'derivedUnitMedia',
266
        'created',
267
        'publish',
268
        'updated',
269
        'class',
270
        'uuid',
271
       ''
272
    );
273
  }
274

    
275

    
276
  // only show uuid it the user is logged in
277
  if(user_is_logged_in() && ($a_idx = array_search('uuid', $exclude_occurrence_fields)) !== FALSE ) {
278
    unset($exclude_occurrence_fields[$a_idx]);
279
  }
280

    
281
  if (!isset($derivatives)) {
282
    $derivatives = array();
283
  }
284

    
285
  $descriptions = null;
286
  $derivedFrom = null;
287

    
288
  if (is_object($specimenOrObservation)) {
289

    
290
    // request again for deeper initialization
291
    $specimenOrObservation = cdm_ws_get("portal/" . CDM_WS_OCCURRENCE, $specimenOrObservation->uuid);
292

    
293

    
294
    $type_label = $specimenOrObservation->class;
295
    RenderHints::setFootnoteListKey($type_label . '-' . $specimenOrObservation->uuid);
296

    
297
    // collect typeStatus as label
298
    if (isset($specimenOrObservation->specimenTypeDesignations)) {
299
      $type_status = array();
300
      foreach ($specimenOrObservation->specimenTypeDesignations as $typeDesignation) {
301
        if (isset($typeDesignation->typeStatus->representation_L10n)){
302
          $type_status[] = $typeDesignation->typeStatus->representation_L10n;
303
        }
304
      }
305
      if (count($type_status) > 0){
306
        $type_label = implode(', ', $type_status);
307
      }
308
    }
309

    
310
    $title = $type_label . ': '. $specimenOrObservation->titleCache;
311

    
312
    $groups = array();
313
    // --- add initialized fields
314
    foreach (get_object_vars($specimenOrObservation) as $field => $value) {
315
      if (!in_array($field, $exclude_occurrence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
316
        switch ($field) {
317

    
318
          /* ---- java.lang.Object --- */
319
          case 'class':
320
            if($value != '' /* FieldUnit' */){
321
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
322
            }
323
          break;
324

    
325
          case 'markers':
326
            $dd_elements = array();
327
            foreach ($value as $marker) {
328
              $dd_elements[] = compose_cdm_marker($marker);
329
            }
330
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
331
            break;
332

    
333

    
334
          case 'annotations':
335
            $dd_elements = array();
336
            foreach ($value as $annotation) {
337
              // TODO respect annotation type filter settings
338
              $dd_elements[] = $annotation->text;
339
            }
340
            @_description_list_group_add($groups, t('Notes'), $dd_elements);
341
            break;
342

    
343
          /* ---- SpecimenOrObservationBase --- */
344
          case 'sex':
345
          case 'lifeStage':
346
          case 'kindOfUnit':
347
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->representation_L10n);
348
            break;
349

    
350
          case 'definition':
351
            // TODO
352
            break;
353

    
354
          case 'specimenTypeDesignations':
355
            @_description_list_group_add(
356
              $groups,
357
              cdm_occurrence_field_name_label($field),
358
              array(
359
                '#markup'=>theme('cdm_typedesignations', array('typeDesignations' => $value)),
360
              )
361
            );
362
            break;
363

    
364
          case 'determinations':
365
            $dd_elements = array();
366
            $glue = ', ';
367

    
368
            foreach  ($value as $determinationEvent){
369
                $timeperiod_string = NULL;
370
                if (isset($determinationEvent->timeperiod)) {
371
                    $timeperiod_string = timePeriodToString($determinationEvent->timeperiod);
372
                }
373
                $weight = isset($determinationEvent->preferred) && $determinationEvent->preferred == 1 ? '0' : ($timeperiod_string ? $timeperiod_string : '1');
374
                // check key exists
375
                while (array_key_exists($weight, $dd_elements)) {
376
                    $weight .= '0';
377
                }
378
                if($determinationEvent->taxonName){
379
                    $taxon_name = $determinationEvent->taxonName;
380
                    $name_link = "";
381
                }
382
                else if($determinationEvent->taxon){
383
                    $taxon_name = cdm_ws_get(CDM_WS_TAXON . '/$0/name', $determinationEvent->taxon->uuid);
384
                    $name_link = path_to_taxon($determinationEvent->taxon->uuid);
385
                }
386
                $taxon_html = render_taxon_or_name($taxon_name, $name_link);
387
                $dd_elements[$weight] = $taxon_html;
388
                if (isset($determinationEvent->modifier)) {
389
                    $dd_elements[$weight] .= cdm_term_representation($determinationEvent->modifier);
390
                }
391
                if ($timeperiod_string) {
392
                    $dd_elements[$weight] .= $glue . $timeperiod_string;
393
                }
394
                if (isset($determinationEvent->actor->titleCache)) {
395
                    $dd_elements[$weight] .= $glue . $determinationEvent->actor->titleCache;
396
                }
397
                if (isset($determinationEvent->description)) {
398
                    $dd_elements[$weight] .= $glue . $determinationEvent->description;
399
                }
400
            }
401
            ksort($dd_elements);
402
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('determinations'), $dd_elements);
403
            break;
404

    
405
          case 'descriptions':
406
            $descriptions = $value;
407
            $occurrence_featureTree = cdm_get_occurrence_featureTree();
408
            $dd_elements = array();
409

    
410
            foreach ($value as $description) {
411
              $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, $description->uuid);
412
//               if($description->imageGallery == TRUE) {
413
//                 continue;
414
//               }
415
              $elements_by_feature = _mergeFeatureTreeDescriptions($occurrence_featureTree->root->childNodes, $description->elements);
416
              $description_render_elements = compose_feature_blocks($elements_by_feature, null);
417
              $dd_elements[] = $description_render_elements;
418
            }
419

    
420
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
421
            break;
422

    
423
          case 'sources':
424
              $dd_elements = array();
425
              foreach ($value as $identifiable_source) {
426
                $dd_elements[] = theme('cdm_OriginalSource', array('source' => $identifiable_source));
427
              }
428
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
429
              break;
430

    
431

    
432
          /* ---- DerivedUnitBase --- */
433
          case 'derivedFrom':
434
            $derivedFrom = $value;
435
            break;
436

    
437
          case 'collection':
438
            $sub_dl_groups = array();
439
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('code'), $value->code, NULL, 1);
440
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('codeStandard'),  $value->codeStandard, NULL, 2);
441
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('institute'), $value->institute, NULL, 3);
442
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('townOrLocation'), $value->townOrLocation, NULL, 4);
443
            // TODO "superCollection"
444
            // TODO may have media
445

    
446
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
447
                array(
448
                    array('#markup' => $value->titleCache),
449
                    array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
450
                )
451
            );
452
            break;
453

    
454
            case 'storedUnder':
455
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('storedUnder'), render_taxon_or_name($value));
456
              break;
457

    
458

    
459
            /* ---- Specimen --- */
460
            case 'sequences':
461
              $dd_elements = array();
462
              foreach ($value as $sequence) {
463
                $dd_elements[] = compose_cdm_sequence($sequence);
464
              }
465
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
466
            break;
467

    
468
            // TODO preservation
469
            // TODO exsiccatum
470

    
471

    
472
          /* ---- FieldObservation --- */
473
          case 'gatheringEvent':
474
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('collector'), $value->actor->titleCache);
475
            @_description_list_group_add($groups, t('Gathering time'), timePeriodToString($value->timeperiod));
476
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('description'), $value->description);
477
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('locality'), $value->locality->text);
478
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('country'), $value->country->representation_L10n);
479
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingMethod'), $value->collectingMethod);
480
            if (isset($value->absoluteElevation)) {
481
                $min_max_array = min_max_array();
482
                if(is_numeric($value->absoluteElevation)){
483
                    $min_max_array['Min'] = new stdClass();
484
                    $min_max_array['Min']->_value = $value->absoluteElevation . ' m';
485
                }
486
                if(is_numeric($value->absoluteElevation)){
487
                    $min_max_array['Max'] = new stdClass();
488
                    $min_max_array['Max']->_value = $value->absoluteElevationMax . ' m';
489
                }
490
                $min_max_markup = min_max_markup($min_max_array);
491
                if(isset($value->absoluteElevationError)){
492
                    $min_max_markup .= ' +/-' .  $value->absoluteElevationError . ' m';
493
                }
494
                if(is_string($value->absoluteElevationText)){
495
                    $min_max_markup .= ' ' . $value->absoluteElevationText;
496
                }
497
                @_description_list_group_add($groups, cdm_occurrence_field_name_label('absoluteElevation'), $min_max_markup);
498
            }
499
            if (isset($value->distanceToGround)) {
500
              $min_max_array = min_max_array();
501
              if(is_numeric($value->distanceToGround)){
502
                  $min_max_array['Min'] = new stdClass();
503
                  $min_max_array['Min']->_value = $value->distanceToGround . ' m';
504
              }
505
              if(is_numeric($value->distanceToGround)){
506
                  $min_max_array['Max'] = new stdClass();
507
                  $min_max_array['Max']->_value = $value->distanceToGroundMax . ' m';
508
              }
509
              $min_max_markup = min_max_markup($min_max_array);
510
              if(isset($value->distanceToGroundError)){
511
                  $min_max_markup .= ' +/-' .  $value->distanceToGroundError . ' m';
512
              }
513
              if(is_string($value->distanceToGroundText)){
514
                  $min_max_markup .= ' ' . $value->distanceToGroundText;
515
              }
516
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToGround'), $min_max_markup);
517
            }
518
            if (isset($value->distanceToWaterSurface)) {
519
              $min_max_array = min_max_array();
520
              if(is_numeric($value->distanceToWaterSurface)){
521
                  $min_max_array['Min'] = new stdClass();
522
                  $min_max_array['Min']->_value = $value->distanceToWaterSurface . ' m';
523
              }
524
              if(is_numeric($value->distanceToWaterSurface)){
525
                  $min_max_array['Max'] = new stdClass();
526
                  $min_max_array['Max']->_value = $value->distanceToWaterSurfaceMax . ' m';
527
              }
528
              $min_max_markup = min_max_markup($min_max_array);
529
              if(isset($value->distanceToWaterSurfaceError)){
530
                  $min_max_markup .= ' +/-' .  $value->distanceToWaterSurfaceError . ' m';
531
              }
532
              if(is_string($value->distanceToWaterSurfaceText)){
533
                  $min_max_markup .= ' ' . $value->distanceToWaterSurfaceText;
534
              }
535
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToWaterSurface'), $min_max_markup);
536
            }
537

    
538
            if (isset($value->collectingAreas)) {
539
              $area_representations = array();
540
              foreach($value->collectingAreas as $area) {
541
                $area_representations[] = $area->representation_L10n;
542
              }
543
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingAreas'), implode(', ', $area_representations));
544
            }
545
            if (isset($value->exactLocation) && $value->exactLocation->sexagesimalString) {
546
              $sub_dl_groups = array();
547
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('errorRadius'), $value->exactLocation->errorRadius, ' m', 1);
548
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('longitude'), round($value->exactLocation->longitude, 7), '°', 2);
549
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('latitude'), round($value->exactLocation->latitude, 7), '°', 3);
550
              if (isset($value->exactLocation->referenceSystem)) {
551
                @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('referenceSystem'), $value->exactLocation->referenceSystem->representation_L10n, '', 4);
552
              }
553

    
554
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('exactLocation'),
555
                  array(
556
                      array('#markup' => $value->exactLocation->sexagesimalString),
557
                      array(
558
                          '#theme' => 'description_list',
559
                          '#groups' => $sub_dl_groups
560
                      ),
561
                  )
562
              );
563
            }
564
            break;
565

    
566
          default:
567
            if(is_object($value) || is_array($value)){
568
              drupal_set_message("Unhandled type in compose_cdm_specimenOrObservation() for field " . $field, "warning");
569
            } else {
570
              _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
571
            }
572

    
573
        }
574

    
575
      }
576
    } // END of loop over $derivedUnitFacade fields
577

    
578

    
579
    // template_preprocess_description_list() is not worting by weight so we do it right here
580
    uasort($groups, 'element_sort');
581

    
582
    $occurrence_elements = array(
583
        '#title' => $title,
584
        '#theme' => 'description_list',
585
        '#groups' => $groups,
586
        '#attributes' => array('class' => html_class_attribute_ref($specimenOrObservation)),
587
    );
588
    $derivatives[] = $occurrence_elements;
589
    // all footnotes which has been assembled so far (e.g. from typeDesignations) to here
590
    $foonote_li_elements = theme('cdm_footnotes', array('footnoteListKey' => RenderHints::getFootnoteListKey(), 'enclosingTag' => 'span'));
591
    if (!empty($foonote_li_elements)) {
592
      $derivatives[] =  array(
593
          '#markup' =>  '<div class="footnotes">' . $foonote_li_elements . '</div>',
594
      );
595
    }
596

    
597
    // --- recurse into originals
598
    if (!isset($derivedFrom)) {
599
      $derivedFrom = cdm_ws_get(
600
          CDM_WS_OCCURRENCE,
601
          array($specimenOrObservation->uuid, 'derivedFrom')
602
        );
603
    }
604

    
605
    if (isset($derivedFrom)) {
606
      if (isset($derivedFrom->originals)) {
607
        $derived_from_label = t('derived');
608
        $preposition = t('from');
609
        if(isset($derivedFrom->type)){
610
          $derived_from_label = $derivedFrom->type->representation_L10n;
611
          if($derivedFrom->type->uuid == UUID_DERIVATIONEVENTTYPE_ACCESSIONING){
612
            $preposition = t('of');
613
          }
614
        }
615
        if (count($groups) > 0) {
616
          // TODO  annotations
617

    
618
          // only display the derived from information when the derivative has any element which will be diplayed
619
          $derivatives[] = array(
620
              '#markup' => '<div class="derived_from">' . $derived_from_label . ' ' . $preposition . ': </div>',
621
          );
622
        }
623
        foreach ($derivedFrom->originals as $original) {
624
          compose_cdm_specimenOrObservation($original, $derivatives);
625
        }
626
      }
627
    }
628

    
629
  } // END of $specimenOrObservation exists
630

    
631
  return $derivatives;
632
}
633

    
634
/**
635
 * Compose an render array from a CDM Sequence object.
636
 *
637
 * compose_hook() implementation
638
 *
639
 * @param object $sequence
640
 *   CDM instance of type Sequence
641
 * @return array
642
 *   A render array containing the fields of the supplied $sequence
643
 *
644
 * @ingroup compose
645
 */
646
function compose_cdm_sequence($sequence) {
647

    
648
  $exclude_sequence_fields = &drupal_static(__FUNCTION__);
649
  if (!isset($exclude_sequence_fields)) {
650
    $exclude_sequence_fields = array(
651
      'titleCache',
652
      'protectedTitleCache',
653
      'microReference',
654
      'created',
655
      'updated',
656
      'class',
657
    );
658
  }
659

    
660
  $groups = array();
661

    
662
  // -- retrieve additional data if neesscary
663
  // TODO below call disabled since sequences are not yet supported,
664
  //      see  #3347 (services and REST service controller for molecular classes implemented)
665
  //
666
  // cdm_load_annotations($sequence);
667

    
668
  foreach (get_object_vars($sequence) as $field => $value) {
669

    
670

    
671
    if (!in_array($field, $exclude_sequence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
672
      switch ($field) {
673

    
674
        case 'geneticAccessionNumber';
675
          $dd_elements = array();
676
          foreach ($value as $accession) {
677
            if (isset($accession->uri) ){
678
              $dd_elements[] = l($accession->accessionNumber, $accession->uri);
679
            } else {
680
              $dd_elements[] = $accession->accessionNumber;
681
            }
682
          }
683
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),  $dd_elements, NULL, 1);
684
          break;
685

    
686

    
687
        case 'locus': // FIXME 3.3 now dnaMarker (DefinedTerm)  if multiple amplifications where used to build this consensus sequence it may be the super set of the markers used in amplification.
688
          if (isset($value->name)) {
689
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),  $value->name, NULL, 3);
690
          }
691
          if (isset($value->description)) {
692
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field) . ' ' . t('description') , $value->description, NULL, 4);
693
          }
694
          break;
695

    
696
        case 'consensusSequence':
697
          // format in genbank style, force linebreaks after each 70 nucleotites
698
          // see also http://stackoverflow.com/questions/499137/css-how-can-i-force-a-long-string-without-any-blank-to-be-wrapped-in-xul-and
699
          @_description_list_group_add(
700
            $groups,
701
            cdm_occurrence_field_name_label($field),
702
            array(
703
              array(
704
                '#markup' => '<div class="sequence-length">' . $value->length . ' ' . t('pb'). '</div><div>' . wordwrap($value->string, 70, '</br>', TRUE) . '</div>',
705
                '#wrapper_attributes' => array('class'=>'dna-sequence')
706
                )
707
              ),
708
            5);
709
          break;
710

    
711
         case 'dnaSample': // FIXME 3.3 implement
712
            break;
713
        case 'singleReads': // FIXME 3.3 implement
714
          break;
715
        case 'contigFile': // FIXME 3.3 implement - Media
716
            break;
717
        case 'pherograms': // FIXME 3.3 implement - Media
718
          break;
719
        case 'haplotype': // FIXME 3.3 implement
720
            break;
721
        case 'dateSequenced': // FIXME 3.3 now in SingelRead
722
          @_description_list_group_add($groups, t('Sequencing date'),  timePeriodToString($value), NULL, 6);
723
          break;
724

    
725
        case 'barcode': // boolean
726
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value ? 'Yes': 'No', NULL, 7);
727
          break;
728
        case 'barcodeSequencePart': // FIXME 3.3 implement, compose sequence
729
            break;
730

    
731
        case 'citation':
732
          @_description_list_group_add($groups,
733
            cdm_occurrence_field_name_label($field),
734
            theme('cdm_reference', array('reference' =>$value, 'microReference' => $sequence->microReference)),
735
            NULL,
736
            8
737
          );
738
          break;
739

    
740
        case 'publishedIn':
741
          @_description_list_group_add($groups,
742
            cdm_occurrence_field_name_label($field),
743
            theme('cdm_reference', array('reference'=>$value)),
744
            NULL,
745
            7
746
          );
747

    
748
        case 'rights':
749
          array_merge($groups, cdm_rights_as_dl_groups($value));
750
        break;
751

    
752
        case 'annotations':
753
          $dd_elements = array();
754
          foreach ($value as $annotation) {
755
            // TODO respect annotation type filter settings
756
            $dd_elements[] = $annotation->text;
757
          }
758
          @_description_list_group_add($groups, t('Notes'), $dd_elements, NULL, 9);
759
          break;
760

    
761
        case 'markers':
762
          $dd_elements = array();
763
          foreach ($value as $marker) {
764
            $dd_elements[] = compose_cdm_marker($marker);
765
          }
766
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements, NULL, 10);
767
          break;
768

    
769
        case 'chromatograms':
770
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
771
              array(
772
                  '#markup'=>theme('cdm_media_gallerie', array('medialist'=>$value)),
773
              ),
774
              NULL,
775
              11);
776
          break;
777

    
778
        default:
779
          if(is_object($value) || is_array($value)){
780
            drupal_set_message("Unhandled type in compose_cdm_sequence() for field " . $field, "warning");
781
          } else {
782
            _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 20);
783
          }
784
      }
785
    }
786
  }
787

    
788
  // template_preprocess_description_list() is not worting by weight so we do it right here
789
  uasort($groups, 'element_sort');
790

    
791
  $sequence_elements = array(
792
      '#theme' => 'description_list',
793
      '#groups' => $groups
794
  );
795

    
796
  return $sequence_elements;
797
}
798

    
799

    
(6-6/10)