Project

General

Profile

Download (28.9 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
 * Provides the HTML markup for a specimen page
22
 *
23
 * @param $specimen
24
 *
25
 * @return string
26
 *  The markup for a specimen page
27
 */
28
function render_cdm_specimen_page($specimen, $is_specimen_page = false)
29
{
30
    $detail_html = "";
31
    //link to specimen page
32
    $pathToSpecimen = path_to_specimen($specimen->uuid);
33
    if (!$is_specimen_page) {
34
        $specimenPageLink = l($specimen->accessionNumber, $pathToSpecimen, array('attributes' => array('target' => '_blank')));
35
        $detail_html .= "<strong>$specimenPageLink</strong><br>";
36
    }
37

    
38
    if($is_specimen_page) {
39
        if($specimen->citation){
40
            $detail_html .= "<br>".create_label("Citation") . $specimen->citation . "<br>";
41
        }
42
    }
43
    if($specimen->preferredStableUri){
44
        $stableIdentifierLink  = l($specimen->preferredStableUri, $specimen->preferredStableUri, array('attributes' => array('target' => '_blank')));
45
        $detail_html .= "<br>".create_label("Preferred stable URI") . $stableIdentifierLink . "<br>";
46
    }
47
    if($is_specimen_page){
48
        // associated taxa
49
        if($specimen->associatedTaxa){
50
            $detail_html .= "<br>";
51
          $detail_html .= create_label("Associated with");
52
          if(sizeof($specimen->associatedTaxa)>1){
53
            $detail_html .= "<br>";
54
          }
55
          foreach($specimen->associatedTaxa as $associatedTaxon){
56
            $detail_html .= l($associatedTaxon->second, path_to_taxon($associatedTaxon->first, "specimens"));//$associatedTaxon->second."<br>";
57
          }
58
          $detail_html .= "<br>";
59
        }
60
    }
61
  // - type information
62
    $types = "";
63
    if (isset($specimen->types)) {
64
        //typed taxa
65
        if(sizeof($specimen->types)>1){
66
            $detail_html .= "<br>";
67
        }
68
        foreach ($specimen->types as $typeStatus => $typedTaxa) {
69
            if($is_specimen_page){
70
                if($specimen->types){
71
                    $detail_html .= "<i>".$typeStatus."</i>: ";
72
                    foreach($typedTaxa as $typedTaxon){
73
                        $detail_html .= $typedTaxon." ";
74
                    }
75
                    $detail_html .= "<br>";
76
                }
77
            }
78
            else{
79
                $types .= $typeStatus." ";
80
            }
81
        }
82
    }
83
    $derivateDataDTO = $specimen->derivateDataDTO;
84
    // - specimen scans
85
    $specimenScans = create_html_links($derivateDataDTO->specimenScans, true);
86
    // - molecular data
87
    $molecularData = "";
88
    if ($derivateDataDTO->molecularDataList) {
89
        foreach ($derivateDataDTO->molecularDataList as $molecular) {
90
            //provider link
91
            if (isset($molecular->providerLink)) {
92
                $molecularData .= create_html_link($molecular->providerLink, true);
93
            } else {
94
                $molecularData .= "[no provider]";
95
            }
96
            //contig link
97
            if (isset($molecular->contigFiles)) {
98
                $molecularData .= "[";
99
                if (sizeof($molecular->contigFiles) > 0) {
100
                    foreach ($molecular->contigFiles as $contigFile) {
101
                        if (isset($contigFile->contigLink)) {
102
                            if (isset($contigFile->contigLink->uri) and $contigFile->contigLink->uri != null) {
103
                                $molecularData .= create_html_link($contigFile->contigLink, true) . " ";
104
                            }
105
                        }
106
                        else {
107
                            $molecularData .= "no contig ";
108
                        }
109
                        //primer links
110
                        if(isset($contigFile->primerLinks)) {
111
                            $molecularData .= create_html_links($contigFile->primerLinks, true);
112
                        }
113
                    }
114
                }
115
                $molecularData = rtrim($molecularData, " ");
116
                $molecularData .= "]";
117
            }
118
            //FIXME separate with comma (remove trailing comma)
119
        }
120
    }
121
    // - detail images
122
    $detailImages = create_html_links($derivateDataDTO->detailImages, true);
123

    
124
    if ($types) {
125
        $detail_html .= $is_specimen_page?"<br>":"";
126
        $detail_html .= create_label("Type(s)") . $types . "<br>";
127
    }
128
    if ($specimenScans and !$is_specimen_page) {
129
        $detail_html .= create_label("Specimen Scans") . $specimenScans . "<br>";
130
    }
131
    //specimen scan image gallery
132
    if($is_specimen_page and isset($derivateDataDTO->specimenScanUuids) and !empty($derivateDataDTO->specimenScanUuids)) {
133
        $detail_html .= addImageGallery("Specimen scans", $derivateDataDTO->specimenScanUuids);
134
    }
135

    
136
    if ($molecularData) {
137
        $detail_html .= $is_specimen_page?"<br>":"";
138
        $detail_html .= create_label("Molecular Data") . $molecularData . "<br>";
139
    }
140

    
141
    if ($detailImages and !$is_specimen_page) {
142
        $detail_html .= create_label("Detail Images") . $detailImages . "<br>";
143
    }
144

    
145
    //detail image gallery
146
    if($is_specimen_page and isset($derivateDataDTO->detailImageUuids) and !empty($derivateDataDTO->detailImageUuids)){
147
        $detail_html .= addImageGallery("Detail Images", $derivateDataDTO->detailImageUuids);
148
    }
149

    
150
    //character data
151
    if ($specimen->characterData) {
152
        $detail_html .= $is_specimen_page?"<br>":"";
153
        $detail_html .= create_label("Character Data");
154
        if($is_specimen_page) {
155
            $detail_html .= "<br>";
156
            foreach ($specimen->characterData as $characterStatePair) {
157
                $detail_html .= "<i>" . $characterStatePair->first . "</i>:" . $characterStatePair->second;
158
                $detail_html .= "<br>";
159
            }
160
        }
161
        else{
162
            $detail_html .= l("detail page", $pathToSpecimen,array('attributes' => array('target'=>'_blank')));
163
            $detail_html .= "<br>";
164
        }
165
    }
166
    return $detail_html;
167
}
168

    
169
function addImageGallery($galleryName, $imageUuids){
170
    $images = array();
171
    foreach ($imageUuids as $uuid) {
172
        $images[] = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $uuid);
173
    }
174

    
175
    $gallery_html = '';
176
    if (count($imageUuids) > 0) {
177
        $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
178
        $captionElements = array(
179
            'title',
180
            'rights',
181
        );
182
        $alternativeMediaUris = array();
183
        foreach($images as $image){
184
          $mediaUri = getMediaUri($image);
185
          if($mediaUri){
186
            $alternativeMediaUris[] = $mediaUri;
187
          }
188
          else{
189
            $alternativeMediaUris[] = path_to_media($image->uuid);
190
          }
191
        }
192

    
193
        $gallery_html = compose_cdm_media_gallerie(array(
194
            'mediaList' => $images,
195
            'galleryName' => $galleryName,
196
            'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
197
            'cols' => $gallery_settings['cdm_dataportal_media_cols'],
198
            'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
199
            'captionElements' => $captionElements,
200
            'mediaLinkType' => 'LIGHTBOX',
201
            'alternativeMediaUri' => $alternativeMediaUris,
202
            'galleryLinkUri' => NULL,
203
        ));
204
    }
205
    return "<br>".create_label($galleryName)."<br>".$gallery_html;
206
}
207

    
208
function getMediaUri($media){
209
  if(isset($media->representations) && sizeof($media->representations)==1
210
    && isset($media->representations[0]->parts) &&
211
    sizeof($media->representations[0]->parts)==1) {
212
    return $media->representations[0]->parts[0]->uri;
213
  }
214
  return null;
215
}
216

    
217

    
218
/**
219
 * Formats the given string to a label for displaying key-object pairs in HTML
220
 * @return string
221
 */
222
function create_label($label)
223
{
224
    return "<span class='specimen_table_label'>".$label.": </span>";
225
}
226

    
227
/**
228
 * Compose an render array from a CDM DerivedUnitFacade object.
229
 *
230
 * compose_hook() implementation
231
 *
232
 * @param object $specimenOrObservation
233
 *   the CDM instance of type SpecimenOrObservation to compose
234
 *   the render array for
235
 * @param array $derivatives
236
 *   the render array which contains the compositions of the derivatives
237
 *   of the supplied $specimenOrObservation
238
 *
239
 * @return array
240
 *   the supplied render array $derivatives to which the composition of the supplied
241
 *   $specimenOrObservation has been added to
242
 *
243
 * @ingroup compose
244
 */
245
function compose_cdm_specimenOrObservation($specimenOrObservation, &$derivatives = null) {
246

    
247
  $exclude_occurrence_fields = &drupal_static(__FUNCTION__);
248
  if (!isset($exclude_occurrence_fields)) {
249
     $exclude_occurrence_fields = array(
250
        'derivationEvents',
251
        'titleCache',
252
        'protectedTitleCache',
253
        'derivedUnitMedia',
254
        'created',
255
        'publish',
256
        'updated',
257
        'class',
258
        'uuid',
259
       ''
260
    );
261
  }
262

    
263

    
264
  // only show uuid it the user is logged in
265
  if(user_is_logged_in() && ($a_idx = array_search('uuid', $exclude_occurrence_fields)) !== FALSE ) {
266
    unset($exclude_occurrence_fields[$a_idx]);
267
  }
268

    
269
  if (!isset($derivatives)) {
270
    $derivatives = array();
271
  }
272

    
273
  $descriptions = null;
274
  $derivedFrom = null;
275

    
276
  if (is_object($specimenOrObservation)) {
277

    
278
    // request again for deeper initialization
279
    $specimenOrObservation = cdm_ws_get("portal/" . CDM_WS_OCCURRENCE, $specimenOrObservation->uuid);
280

    
281

    
282
    $type_label = $specimenOrObservation->class;
283
    RenderHints::setFootnoteListKey($type_label . '-' . $specimenOrObservation->uuid);
284

    
285
    // collect typeStatus as label
286
    if (isset($specimenOrObservation->specimenTypeDesignations)) {
287
      $type_status = array();
288
      foreach ($specimenOrObservation->specimenTypeDesignations as $typeDesignation) {
289
        if (isset($typeDesignation->typeStatus->representation_L10n)) {
290
          $type_status[] = $typeDesignation->typeStatus->representation_L10n;
291
        }
292
      }
293
      if (count($type_status) > 0) {
294
        $type_label = implode(', ', $type_status);
295
      }
296
    }
297

    
298
    $title = $type_label . ': ' . $specimenOrObservation->titleCache;
299

    
300
    $groups = array();
301
    // --- add initialized fields
302
    foreach (get_object_vars($specimenOrObservation) as $field => $value) {
303
      if (!in_array($field, $exclude_occurrence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
304
        switch ($field) {
305

    
306
          /* ---- java.lang.Object --- */
307
          case 'class':
308
            if ($value != '' /* FieldUnit' */) {
309
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
310
            }
311
            break;
312

    
313
          case 'markers':
314
            $dd_elements = array();
315
            foreach ($value as $marker) {
316
              $dd_elements[] = compose_cdm_marker($marker);
317
            }
318
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
319
            break;
320

    
321

    
322
          case 'annotations':
323
            $dd_elements = array();
324
            foreach ($value as $annotation) {
325
              // TODO respect annotation type filter settings
326
              $dd_elements[] = $annotation->text;
327
            }
328
            @_description_list_group_add($groups, t('Notes'), $dd_elements);
329
            break;
330

    
331
          /* ---- SpecimenOrObservationBase --- */
332
          case 'sex':
333
          case 'lifeStage':
334
          case 'kindOfUnit':
335
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->representation_L10n);
336
            break;
337

    
338
          case 'definition':
339
            // TODO
340
            break;
341

    
342
          case 'preferredStableUri':
343

    
344
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), array(array('#markup' => cdm_external_uri($value, false))));
345
            break;
346

    
347
          case 'specimenTypeDesignations':
348
            @_description_list_group_add(
349
              $groups,
350
              cdm_occurrence_field_name_label($field),
351
              array(
352
                '#markup' => theme('cdm_typedesignations', array('typeDesignations' => $value)),
353
              )
354
            );
355
            break;
356

    
357
          case 'determinations':
358
            $dd_elements = array();
359
            $glue = ', ';
360

    
361
            foreach ($value as $determinationEvent) {
362
              $timeperiod_string = NULL;
363
              if (isset($determinationEvent->timeperiod)) {
364
                $timeperiod_string = timePeriodToString($determinationEvent->timeperiod);
365
              }
366
              $weight = isset($determinationEvent->preferred) && $determinationEvent->preferred == 1 ? '0' : ($timeperiod_string ? $timeperiod_string : '1');
367
              // check key exists
368
              while (array_key_exists($weight, $dd_elements)) {
369
                $weight .= '0';
370
              }
371

    
372
              $taxon_name = '';
373
              $name_link = '';
374
              if ($determinationEvent->taxonName) {
375
                $taxon_name = $determinationEvent->taxonName;
376
              } else if ($determinationEvent->taxon) {
377
                $taxon_name = cdm_ws_get(CDM_WS_TAXON . '/$0/name', $determinationEvent->taxon->uuid);
378
                $name_link = path_to_taxon($determinationEvent->taxon->uuid);
379
              }
380
              if ($taxon_name) {
381
                $taxon_html = render_taxon_or_name($taxon_name, $name_link);
382
                $dd_elements[$weight] = $taxon_html;
383
              }
384
              if (isset($determinationEvent->modifier)) {
385
                $dd_elements[$weight] .= cdm_term_representation($determinationEvent->modifier);
386
              }
387
              if ($timeperiod_string) {
388
                $dd_elements[$weight] .= $glue . $timeperiod_string;
389
              }
390
              if (isset($determinationEvent->actor->titleCache)) {
391
                $dd_elements[$weight] .= $glue . $determinationEvent->actor->titleCache;
392
              }
393
              if (isset($determinationEvent->description)) {
394
                $dd_elements[$weight] .= $glue . $determinationEvent->description;
395
              }
396
            }
397
            ksort($dd_elements);
398
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('determinations'), $dd_elements);
399
            break;
400

    
401
          case 'descriptions':
402
            $occurrence_featureTree = cdm_get_occurrence_featureTree();
403
            $dd_elements = array();
404

    
405
            foreach ($value as $description) {
406
              $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, $description->uuid);
407
//               if($description->imageGallery == TRUE) {
408
//                 continue;
409
//               }
410
              $elements_by_feature = _mergeFeatureTreeDescriptions($occurrence_featureTree->root->childNodes, $description->elements);
411
              $description_render_elements = _block_get_renderable_array(make_feature_block_list($elements_by_feature, null));
412
              $dd_elements[] = $description_render_elements;
413
            }
414

    
415
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
416
            break;
417

    
418
          case 'sources':
419
            $dd_elements = array();
420
            foreach ($value as $identifiable_source) {
421
              $dd_elements[] = theme('cdm_OriginalSource', array('source' => $identifiable_source));
422
            }
423
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
424
            break;
425

    
426

    
427
          /* ---- DerivedUnitBase --- */
428
          case 'derivedFrom':
429
            $derivedFrom = $value;
430
            break;
431

    
432
          case 'collection':
433
            $sub_dl_groups = array();
434
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('code'), $value->code, NULL, 1);
435
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('codeStandard'), $value->codeStandard, NULL, 2);
436
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('institute'), $value->institute, NULL, 3);
437
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('townOrLocation'), $value->townOrLocation, NULL, 4);
438
            // TODO "superCollection"
439
            // TODO may have media
440

    
441
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
442
              array(
443
                array('#markup' => $value->titleCache),
444
                array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
445
              )
446
            );
447
            break;
448

    
449
          case 'storedUnder':
450
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('storedUnder'), render_taxon_or_name($value));
451
            break;
452

    
453

    
454
          /* ---- Specimen --- */
455
          case 'sequences':
456
            $dd_elements = array();
457
            foreach ($value as $sequence) {
458
              $dd_elements[] = compose_cdm_sequence($sequence);
459
            }
460
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
461
            break;
462

    
463
          // TODO preservation
464
          // TODO exsiccatum
465

    
466

    
467
          /* ---- FieldObservation --- */
468
          case 'gatheringEvent':
469
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('collector'), $value->actor->titleCache);
470
            @_description_list_group_add($groups, t('Gathering time'), timePeriodToString($value->timeperiod));
471
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('description'), $value->description);
472
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('locality'), $value->locality->text);
473
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('country'), $value->country->representation_L10n);
474
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingMethod'), $value->collectingMethod);
475
            if (isset($value->absoluteElevation)) {
476
              $min_max_markup = min_max_measure($value, 'absoluteElevation');
477
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('absoluteElevation'), $min_max_markup);
478
            }
479
            if (isset($value->distanceToGround)) {
480
              $min_max_markup = min_max_measure($value, 'distanceToGround');
481
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToGround'), $min_max_markup);
482
            }
483
            if (isset($value->distanceToWaterSurface)) {
484
              $min_max_markup = min_max_measure($value, 'distanceToWaterSurface');
485
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToWaterSurface'), $min_max_markup);
486
            }
487

    
488
            if (isset($value->collectingAreas)) {
489
              $area_representations = array();
490
              foreach ($value->collectingAreas as $area) {
491
                $area_representations[] = l($area->representation_L10n, path_to_named_area($area->uuid));
492
              }
493
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingAreas'),
494
                array(
495
                  array('#markup' => implode(', ', $area_representations))
496
                )
497
              );
498
            }
499
            if (isset($value->exactLocation) && $value->exactLocation->sexagesimalString) {
500
              $sub_dl_groups = array();
501
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('errorRadius'), $value->exactLocation->errorRadius, ' m', 1);
502
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('longitude'), round($value->exactLocation->longitude, 7), '°', 2);
503
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('latitude'), round($value->exactLocation->latitude, 7), '°', 3);
504
              if (isset($value->exactLocation->referenceSystem)) {
505
                @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('referenceSystem'), $value->exactLocation->referenceSystem->representation_L10n, '', 4);
506
              }
507

    
508
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('exactLocation'),
509
                array(
510
                  array('#markup' => $value->exactLocation->sexagesimalString),
511
                  array(
512
                    '#theme' => 'description_list',
513
                    '#groups' => $sub_dl_groups
514
                  ),
515
                )
516
              );
517
            }
518
            break;
519

    
520
          default:
521
            if (is_object($value) || is_array($value)) {
522
              drupal_set_message("Unhandled type in compose_cdm_specimenOrObservation() for field " . $field, "warning");
523
            } else {
524
              _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
525
            }
526

    
527
        }
528

    
529
      }
530
    } // END of loop over $derivedUnitFacade fields
531

    
532
    // Extensions
533
    $extensions = cdm_ws_fetch_all(CDM_WS_PORTAL_OCCURRENCE . '/'  . $specimenOrObservation->uuid . '/extensions', array($specimenOrObservation->uuid));
534
    if ($extensions && count($extensions)) {
535

    
536
      $extensions_render_array = compose_extensions($extensions);
537
      @_description_list_group_add($groups, t('Extensions') . ':',
538
        $extensions_render_array,
539
        '', 100);
540
    }
541

    
542

    
543
    // template_preprocess_description_list() is not worting by weight so we do it right here
544
    uasort($groups, 'element_sort');
545

    
546
    $occurrence_elements = array(
547
        '#title' => $title,
548
        '#theme' => 'description_list',
549
        '#groups' => $groups,
550
        '#attributes' => array('class' => html_class_attribute_ref($specimenOrObservation)),
551
    );
552
    $derivatives[] = $occurrence_elements;
553
    // all footnotes which has been assembled so far (e.g. from typeDesignations) to here
554
    $foonote_li_elements = theme('cdm_footnotes', array('footnoteListKey' => RenderHints::getFootnoteListKey(), 'enclosingTag' => 'span'));
555
    if (!empty($foonote_li_elements)) {
556
      $derivatives[] =  array(
557
          '#markup' =>  '<div class="footnotes">' . $foonote_li_elements . '</div>',
558
      );
559
    }
560

    
561
    // --- recurse into originals
562
    if (!isset($derivedFrom)) {
563
      $derivedFrom = cdm_ws_get(
564
          CDM_WS_OCCURRENCE,
565
          array($specimenOrObservation->uuid, 'derivedFrom')
566
        );
567
    }
568

    
569
    if (isset($derivedFrom)) {
570
      if (isset($derivedFrom->originals)) {
571
        $derived_from_label = t('derived');
572
        $preposition = t('from');
573
        if(isset($derivedFrom->type)){
574
          $derived_from_label = $derivedFrom->type->representation_L10n;
575
          if($derivedFrom->type->uuid == UUID_DERIVATIONEVENTTYPE_ACCESSIONING){
576
            $preposition = t('of');
577
          }
578
        }
579
        if (count($groups) > 0) {
580
          // TODO  annotations
581

    
582
          // only display the derived from information when the derivative has any element which will be diplayed
583
          $derivatives[] = array(
584
              '#markup' => '<div class="derived_from">' . $derived_from_label . ' ' . $preposition . ': </div>',
585
          );
586
        }
587
        foreach ($derivedFrom->originals as $original) {
588
          compose_cdm_specimenOrObservation($original, $derivatives);
589
        }
590
      }
591
    }
592

    
593
  } // END of $specimenOrObservation exists
594

    
595
  return $derivatives;
596
}
597

    
598

    
599
/**
600
 * Compose an render array from a CDM Sequence object.
601
 *
602
 * compose_hook() implementation
603
 *
604
 * @param object $sequence
605
 *   CDM instance of type Sequence
606
 * @return array
607
 *   A render array containing the fields of the supplied $sequence
608
 *
609
 * @ingroup compose
610
 */
611
function compose_cdm_sequence($sequence) {
612

    
613
  $exclude_sequence_fields = &drupal_static(__FUNCTION__);
614
  if (!isset($exclude_sequence_fields)) {
615
    $exclude_sequence_fields = array(
616
      'titleCache',
617
      'protectedTitleCache',
618
      'microReference',
619
      'created',
620
      'updated',
621
      'class',
622
    );
623
  }
624

    
625
  $groups = array();
626

    
627
  // -- retrieve additional data if neesscary
628
  // TODO below call disabled since sequences are not yet supported,
629
  //      see  #3347 (services and REST service controller for molecular classes implemented)
630
  //
631
  // cdm_load_annotations($sequence);
632

    
633
  foreach (get_object_vars($sequence) as $field => $value) {
634

    
635

    
636
    if (!in_array($field, $exclude_sequence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
637
      switch ($field) {
638

    
639
        case 'geneticAccessionNumber';
640
          $dd_elements = array();
641
          foreach ($value as $accession) {
642
            if (isset($accession->uri) ){
643
              $dd_elements[] = l($accession->accessionNumber, $accession->uri);
644
            } else {
645
              $dd_elements[] = $accession->accessionNumber;
646
            }
647
          }
648
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),  $dd_elements, NULL, 1);
649
          break;
650

    
651

    
652
        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.
653
          if (isset($value->name)) {
654
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),  $value->name, NULL, 3);
655
          }
656
          if (isset($value->description)) {
657
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field) . ' ' . t('description') , $value->description, NULL, 4);
658
          }
659
          break;
660

    
661
        case 'consensusSequence':
662
          // format in genbank style, force linebreaks after each 70 nucleotites
663
          // see also http://stackoverflow.com/questions/499137/css-how-can-i-force-a-long-string-without-any-blank-to-be-wrapped-in-xul-and
664
          @_description_list_group_add(
665
            $groups,
666
            cdm_occurrence_field_name_label($field),
667
            array(
668
              array(
669
                '#markup' => '<div class="sequence-length">' . $value->length . ' ' . t('pb'). '</div><div>' . wordwrap($value->string, 70, '</br>', TRUE) . '</div>',
670
                '#wrapper_attributes' => array('class'=>'dna-sequence')
671
                )
672
              ),
673
            5);
674
          break;
675

    
676
         case 'dnaSample': // FIXME 3.3 implement
677
            break;
678
        case 'singleReads': // FIXME 3.3 implement
679
          break;
680
        case 'contigFile': // FIXME 3.3 implement - Media
681
            break;
682
        case 'pherograms': // FIXME 3.3 implement - Media
683
          break;
684
        case 'haplotype': // FIXME 3.3 implement
685
            break;
686
        case 'dateSequenced': // FIXME 3.3 now in SingelRead
687
          @_description_list_group_add($groups, t('Sequencing date'),  timePeriodToString($value), NULL, 6);
688
          break;
689

    
690
        case 'barcode': // boolean
691
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value ? 'Yes': 'No', NULL, 7);
692
          break;
693
        case 'barcodeSequencePart': // FIXME 3.3 implement, compose sequence
694
            break;
695

    
696
        case 'citation':
697
          @_description_list_group_add($groups,
698
            cdm_occurrence_field_name_label($field),
699
            theme('cdm_reference', array('reference' =>$value, 'microReference' => $sequence->microReference)),
700
            NULL,
701
            8
702
          );
703
          break;
704

    
705
        case 'publishedIn':
706
          @_description_list_group_add($groups,
707
            cdm_occurrence_field_name_label($field),
708
            theme('cdm_reference', array('reference'=>$value)),
709
            NULL,
710
            7
711
          );
712
          break;
713

    
714
        case 'rights':
715
          array_merge($groups, cdm_rights_as_dl_groups($value));
716
          break;
717

    
718
        case 'annotations':
719
          $dd_elements = array();
720
          foreach ($value as $annotation) {
721
            // TODO respect annotation type filter settings
722
            $dd_elements[] = $annotation->text;
723
          }
724
          @_description_list_group_add($groups, t('Notes'), $dd_elements, NULL, 9);
725
          break;
726

    
727
        case 'markers':
728
          $dd_elements = array();
729
          foreach ($value as $marker) {
730
            $dd_elements[] = compose_cdm_marker($marker);
731
          }
732
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements, NULL, 10);
733
          break;
734

    
735
        case 'chromatograms':
736
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
737
              array(
738
                  '#markup'=>compose_cdm_media_gallerie(array('medialist'=>$value)),
739
              ),
740
              NULL,
741
              11);
742
          break;
743

    
744
        default:
745
          if(is_object($value) || is_array($value)){
746
            drupal_set_message("Unhandled type in compose_cdm_sequence() for field " . $field, "warning");
747
          } else {
748
            _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 20);
749
          }
750
      }
751
    }
752
  }
753

    
754
  // template_preprocess_description_list() is not worting by weight so we do it right here
755
  uasort($groups, 'element_sort');
756

    
757
  $sequence_elements = array(
758
      '#theme' => 'description_list',
759
      '#groups' => $groups
760
  );
761

    
762
  return $sequence_elements;
763
}
764

    
765

    
(6-6/10)