Project

General

Profile

Download (31.4 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") . cdm_external_uri($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
            } else {
110
                $molecularData .= "[no provider]";
111
            }
112
            //contig link
113
            if (isset($molecular->contigFiles)) {
114
                $molecularData .= "[";
115
                if (sizeof($molecular->contigFiles) > 0) {
116
                    foreach ($molecular->contigFiles as $contigFile) {
117
                        if (isset($contigFile->contigLink)) {
118
                            if (isset($contigFile->contigLink->uri) and $contigFile->contigLink->uri != null) {
119
                                $molecularData .= create_html_link($contigFile->contigLink, true) . " ";
120
                            }
121
                        }
122
                        else {
123
                            $molecularData .= "no contig ";
124
                        }
125
                        //primer links
126
                        if(isset($contigFile->primerLinks)) {
127
                            $molecularData .= create_html_links($contigFile->primerLinks, true);
128
                        }
129
                    }
130
                }
131
                $molecularData = rtrim($molecularData, " ");
132
                $molecularData .= "]";
133
            }
134
            //FIXME separate with comma (remove trailing comma)
135
        }
136
    }
137
    // - detail images
138
    $detailImages = create_html_links($derivateDataDTO->detailImages, true);
139

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

    
152
    if ($molecularData) {
153
        $detail_html .= $isSpecimenPage?"<br>":"";
154
        $detail_html .= create_label("Molecular Data") . $molecularData . "<br>";
155
    }
156

    
157
    if ($detailImages and !$isSpecimenPage) {
158
        $detail_html .= create_label("Detail Images") . $detailImages . "<br>";
159
    }
160

    
161
    //detail image gallery
162
    if($isSpecimenPage and isset($derivateDataDTO->detailImageUuids) and !empty($derivateDataDTO->detailImageUuids)){
163
        $detail_html .= addImageGallery("Detail Images", $derivateDataDTO->detailImageUuids);
164
    }
165

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

    
185
function addImageGallery($galleryName, $imageUuids){
186
    $images = array();
187
    foreach ($imageUuids as $uuid) {
188
        $images[] = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $uuid);
189
    }
190

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

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

    
224
function getMediaUri($media){
225
  if(isset($media->representations) && sizeof($media->representations)==1
226
    && isset($media->representations[0]->parts) &&
227
    sizeof($media->representations[0]->parts)==1) {
228
    return $media->representations[0]->parts[0]->uri;
229
  }
230
  return null;
231
}
232

    
233

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

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

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

    
280

    
281
  // only show uuid it the user is logged in
282
  if(user_is_logged_in() && ($a_idx = array_search('uuid', $exclude_occurrence_fields)) !== FALSE ) {
283
    unset($exclude_occurrence_fields[$a_idx]);
284
  }
285

    
286
  if (!isset($derivatives)) {
287
    $derivatives = array();
288
  }
289

    
290
  $descriptions = null;
291
  $derivedFrom = null;
292

    
293
  if (is_object($specimenOrObservation)) {
294

    
295
    // request again for deeper initialization
296
    $specimenOrObservation = cdm_ws_get("portal/" . CDM_WS_OCCURRENCE, $specimenOrObservation->uuid);
297

    
298

    
299
    $type_label = $specimenOrObservation->class;
300
    RenderHints::setFootnoteListKey($type_label . '-' . $specimenOrObservation->uuid);
301

    
302
    // collect typeStatus as label
303
    if (isset($specimenOrObservation->specimenTypeDesignations)) {
304
      $type_status = array();
305
      foreach ($specimenOrObservation->specimenTypeDesignations as $typeDesignation) {
306
        if (isset($typeDesignation->typeStatus->representation_L10n)){
307
          $type_status[] = $typeDesignation->typeStatus->representation_L10n;
308
        }
309
      }
310
      if (count($type_status) > 0){
311
        $type_label = implode(', ', $type_status);
312
      }
313
    }
314

    
315
    $title = $type_label . ': '. $specimenOrObservation->titleCache;
316

    
317
    $groups = array();
318
    // --- add initialized fields
319
    foreach (get_object_vars($specimenOrObservation) as $field => $value) {
320
      if (!in_array($field, $exclude_occurrence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
321
        switch ($field) {
322

    
323
          /* ---- java.lang.Object --- */
324
          case 'class':
325
            if($value != '' /* FieldUnit' */){
326
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
327
            }
328
          break;
329

    
330
          case 'markers':
331
            $dd_elements = array();
332
            foreach ($value as $marker) {
333
              $dd_elements[] = compose_cdm_marker($marker);
334
            }
335
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
336
            break;
337

    
338

    
339
          case 'annotations':
340
            $dd_elements = array();
341
            foreach ($value as $annotation) {
342
              // TODO respect annotation type filter settings
343
              $dd_elements[] = $annotation->text;
344
            }
345
            @_description_list_group_add($groups, t('Notes'), $dd_elements);
346
            break;
347

    
348
          /* ---- SpecimenOrObservationBase --- */
349
          case 'sex':
350
          case 'lifeStage':
351
          case 'kindOfUnit':
352
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->representation_L10n);
353
            break;
354

    
355
          case 'definition':
356
            // TODO
357
            break;
358

    
359
          case 'preferredStableUri':
360

    
361
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), array(array('#markup' => cdm_external_uri($value, false))));
362
            break;
363

    
364
          case 'specimenTypeDesignations':
365
            @_description_list_group_add(
366
              $groups,
367
              cdm_occurrence_field_name_label($field),
368
              array(
369
                '#markup'=>theme('cdm_typedesignations', array('typeDesignations' => $value)),
370
              )
371
            );
372
            break;
373

    
374
          case 'determinations':
375
            $dd_elements = array();
376
            $glue = ', ';
377

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

    
415
          case 'descriptions':
416
            $descriptions = $value;
417
            $occurrence_featureTree = cdm_get_occurrence_featureTree();
418
            $dd_elements = array();
419

    
420
            foreach ($value as $description) {
421
              $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, $description->uuid);
422
//               if($description->imageGallery == TRUE) {
423
//                 continue;
424
//               }
425
              $elements_by_feature = _mergeFeatureTreeDescriptions($occurrence_featureTree->root->childNodes, $description->elements);
426
              $description_render_elements = compose_feature_blocks($elements_by_feature, null);
427
              $dd_elements[] = $description_render_elements;
428
            }
429

    
430
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
431
            break;
432

    
433
          case 'sources':
434
              $dd_elements = array();
435
              foreach ($value as $identifiable_source) {
436
                $dd_elements[] = theme('cdm_OriginalSource', array('source' => $identifiable_source));
437
              }
438
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
439
              break;
440

    
441

    
442
          /* ---- DerivedUnitBase --- */
443
          case 'derivedFrom':
444
            $derivedFrom = $value;
445
            break;
446

    
447
          case 'collection':
448
            $sub_dl_groups = array();
449
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('code'), $value->code, NULL, 1);
450
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('codeStandard'),  $value->codeStandard, NULL, 2);
451
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('institute'), $value->institute, NULL, 3);
452
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('townOrLocation'), $value->townOrLocation, NULL, 4);
453
            // TODO "superCollection"
454
            // TODO may have media
455

    
456
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
457
                array(
458
                    array('#markup' => $value->titleCache),
459
                    array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
460
                )
461
            );
462
            break;
463

    
464
            case 'storedUnder':
465
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('storedUnder'), render_taxon_or_name($value));
466
              break;
467

    
468

    
469
            /* ---- Specimen --- */
470
            case 'sequences':
471
              $dd_elements = array();
472
              foreach ($value as $sequence) {
473
                $dd_elements[] = compose_cdm_sequence($sequence);
474
              }
475
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
476
            break;
477

    
478
            // TODO preservation
479
            // TODO exsiccatum
480

    
481

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

    
548
            if (isset($value->collectingAreas)) {
549
              $area_representations = array();
550
              foreach($value->collectingAreas as $area) {
551
                $area_representations[] = $area->representation_L10n;
552
              }
553
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingAreas'), implode(', ', $area_representations));
554
            }
555
            if (isset($value->exactLocation) && $value->exactLocation->sexagesimalString) {
556
              $sub_dl_groups = array();
557
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('errorRadius'), $value->exactLocation->errorRadius, ' m', 1);
558
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('longitude'), round($value->exactLocation->longitude, 7), '°', 2);
559
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('latitude'), round($value->exactLocation->latitude, 7), '°', 3);
560
              if (isset($value->exactLocation->referenceSystem)) {
561
                @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('referenceSystem'), $value->exactLocation->referenceSystem->representation_L10n, '', 4);
562
              }
563

    
564
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('exactLocation'),
565
                  array(
566
                      array('#markup' => $value->exactLocation->sexagesimalString),
567
                      array(
568
                          '#theme' => 'description_list',
569
                          '#groups' => $sub_dl_groups
570
                      ),
571
                  )
572
              );
573
            }
574
            break;
575

    
576
          default:
577
            if(is_object($value) || is_array($value)){
578
              drupal_set_message("Unhandled type in compose_cdm_specimenOrObservation() for field " . $field, "warning");
579
            } else {
580
              _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
581
            }
582

    
583
        }
584

    
585
      }
586
    } // END of loop over $derivedUnitFacade fields
587

    
588

    
589
    // template_preprocess_description_list() is not worting by weight so we do it right here
590
    uasort($groups, 'element_sort');
591

    
592
    $occurrence_elements = array(
593
        '#title' => $title,
594
        '#theme' => 'description_list',
595
        '#groups' => $groups,
596
        '#attributes' => array('class' => html_class_attribute_ref($specimenOrObservation)),
597
    );
598
    $derivatives[] = $occurrence_elements;
599
    // all footnotes which has been assembled so far (e.g. from typeDesignations) to here
600
    $foonote_li_elements = theme('cdm_footnotes', array('footnoteListKey' => RenderHints::getFootnoteListKey(), 'enclosingTag' => 'span'));
601
    if (!empty($foonote_li_elements)) {
602
      $derivatives[] =  array(
603
          '#markup' =>  '<div class="footnotes">' . $foonote_li_elements . '</div>',
604
      );
605
    }
606

    
607
    // --- recurse into originals
608
    if (!isset($derivedFrom)) {
609
      $derivedFrom = cdm_ws_get(
610
          CDM_WS_OCCURRENCE,
611
          array($specimenOrObservation->uuid, 'derivedFrom')
612
        );
613
    }
614

    
615
    if (isset($derivedFrom)) {
616
      if (isset($derivedFrom->originals)) {
617
        $derived_from_label = t('derived');
618
        $preposition = t('from');
619
        if(isset($derivedFrom->type)){
620
          $derived_from_label = $derivedFrom->type->representation_L10n;
621
          if($derivedFrom->type->uuid == UUID_DERIVATIONEVENTTYPE_ACCESSIONING){
622
            $preposition = t('of');
623
          }
624
        }
625
        if (count($groups) > 0) {
626
          // TODO  annotations
627

    
628
          // only display the derived from information when the derivative has any element which will be diplayed
629
          $derivatives[] = array(
630
              '#markup' => '<div class="derived_from">' . $derived_from_label . ' ' . $preposition . ': </div>',
631
          );
632
        }
633
        foreach ($derivedFrom->originals as $original) {
634
          compose_cdm_specimenOrObservation($original, $derivatives);
635
        }
636
      }
637
    }
638

    
639
  } // END of $specimenOrObservation exists
640

    
641
  return $derivatives;
642
}
643

    
644
/**
645
 * Compose an render array from a CDM Sequence object.
646
 *
647
 * compose_hook() implementation
648
 *
649
 * @param object $sequence
650
 *   CDM instance of type Sequence
651
 * @return array
652
 *   A render array containing the fields of the supplied $sequence
653
 *
654
 * @ingroup compose
655
 */
656
function compose_cdm_sequence($sequence) {
657

    
658
  $exclude_sequence_fields = &drupal_static(__FUNCTION__);
659
  if (!isset($exclude_sequence_fields)) {
660
    $exclude_sequence_fields = array(
661
      'titleCache',
662
      'protectedTitleCache',
663
      'microReference',
664
      'created',
665
      'updated',
666
      'class',
667
    );
668
  }
669

    
670
  $groups = array();
671

    
672
  // -- retrieve additional data if neesscary
673
  // TODO below call disabled since sequences are not yet supported,
674
  //      see  #3347 (services and REST service controller for molecular classes implemented)
675
  //
676
  // cdm_load_annotations($sequence);
677

    
678
  foreach (get_object_vars($sequence) as $field => $value) {
679

    
680

    
681
    if (!in_array($field, $exclude_sequence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
682
      switch ($field) {
683

    
684
        case 'geneticAccessionNumber';
685
          $dd_elements = array();
686
          foreach ($value as $accession) {
687
            if (isset($accession->uri) ){
688
              $dd_elements[] = l($accession->accessionNumber, $accession->uri);
689
            } else {
690
              $dd_elements[] = $accession->accessionNumber;
691
            }
692
          }
693
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),  $dd_elements, NULL, 1);
694
          break;
695

    
696

    
697
        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.
698
          if (isset($value->name)) {
699
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),  $value->name, NULL, 3);
700
          }
701
          if (isset($value->description)) {
702
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field) . ' ' . t('description') , $value->description, NULL, 4);
703
          }
704
          break;
705

    
706
        case 'consensusSequence':
707
          // format in genbank style, force linebreaks after each 70 nucleotites
708
          // see also http://stackoverflow.com/questions/499137/css-how-can-i-force-a-long-string-without-any-blank-to-be-wrapped-in-xul-and
709
          @_description_list_group_add(
710
            $groups,
711
            cdm_occurrence_field_name_label($field),
712
            array(
713
              array(
714
                '#markup' => '<div class="sequence-length">' . $value->length . ' ' . t('pb'). '</div><div>' . wordwrap($value->string, 70, '</br>', TRUE) . '</div>',
715
                '#wrapper_attributes' => array('class'=>'dna-sequence')
716
                )
717
              ),
718
            5);
719
          break;
720

    
721
         case 'dnaSample': // FIXME 3.3 implement
722
            break;
723
        case 'singleReads': // FIXME 3.3 implement
724
          break;
725
        case 'contigFile': // FIXME 3.3 implement - Media
726
            break;
727
        case 'pherograms': // FIXME 3.3 implement - Media
728
          break;
729
        case 'haplotype': // FIXME 3.3 implement
730
            break;
731
        case 'dateSequenced': // FIXME 3.3 now in SingelRead
732
          @_description_list_group_add($groups, t('Sequencing date'),  timePeriodToString($value), NULL, 6);
733
          break;
734

    
735
        case 'barcode': // boolean
736
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value ? 'Yes': 'No', NULL, 7);
737
          break;
738
        case 'barcodeSequencePart': // FIXME 3.3 implement, compose sequence
739
            break;
740

    
741
        case 'citation':
742
          @_description_list_group_add($groups,
743
            cdm_occurrence_field_name_label($field),
744
            theme('cdm_reference', array('reference' =>$value, 'microReference' => $sequence->microReference)),
745
            NULL,
746
            8
747
          );
748
          break;
749

    
750
        case 'publishedIn':
751
          @_description_list_group_add($groups,
752
            cdm_occurrence_field_name_label($field),
753
            theme('cdm_reference', array('reference'=>$value)),
754
            NULL,
755
            7
756
          );
757

    
758
        case 'rights':
759
          array_merge($groups, cdm_rights_as_dl_groups($value));
760
        break;
761

    
762
        case 'annotations':
763
          $dd_elements = array();
764
          foreach ($value as $annotation) {
765
            // TODO respect annotation type filter settings
766
            $dd_elements[] = $annotation->text;
767
          }
768
          @_description_list_group_add($groups, t('Notes'), $dd_elements, NULL, 9);
769
          break;
770

    
771
        case 'markers':
772
          $dd_elements = array();
773
          foreach ($value as $marker) {
774
            $dd_elements[] = compose_cdm_marker($marker);
775
          }
776
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements, NULL, 10);
777
          break;
778

    
779
        case 'chromatograms':
780
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
781
              array(
782
                  '#markup'=>theme('cdm_media_gallerie', array('medialist'=>$value)),
783
              ),
784
              NULL,
785
              11);
786
          break;
787

    
788
        default:
789
          if(is_object($value) || is_array($value)){
790
            drupal_set_message("Unhandled type in compose_cdm_sequence() for field " . $field, "warning");
791
          } else {
792
            _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 20);
793
          }
794
      }
795
    }
796
  }
797

    
798
  // template_preprocess_description_list() is not worting by weight so we do it right here
799
  uasort($groups, 'element_sort');
800

    
801
  $sequence_elements = array(
802
      '#theme' => 'description_list',
803
      '#groups' => $groups
804
  );
805

    
806
  return $sequence_elements;
807
}
808

    
809

    
(6-6/10)