Project

General

Profile

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

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

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

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

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

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

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

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

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

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

    
215

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

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

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

    
261

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

    
267
  if (!isset($derivatives)) {
268
    $derivatives = array();
269
  }
270

    
271
  $descriptions = null;
272
  $derivedFrom = null;
273

    
274
  if (is_object($specimenOrObservation)) {
275

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

    
279

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

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

    
296
    $title = $type_label . ': ' . $specimenOrObservation->titleCache;
297

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

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

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

    
319

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

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

    
336
          case 'definition':
337
            // TODO
338
            break;
339

    
340
          case 'preferredStableUri':
341

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

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

    
355
          case 'determinations':
356
            $dd_elements = array();
357
            $glue = ', ';
358

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

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

    
399
          case 'descriptions':
400
            $occurrence_featureTree = cdm_get_occurrence_featureTree();
401
            $dd_elements = array();
402

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

    
413
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
414
            break;
415

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

    
424

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

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

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

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

    
451

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

    
461
          // TODO preservation
462
          // TODO exsiccatum
463

    
464

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

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

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

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

    
525
        }
526

    
527
      }
528
    } // END of loop over $derivedUnitFacade fields
529

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

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

    
540

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

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

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

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

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

    
591
  } // END of $specimenOrObservation exists
592

    
593
  return $derivatives;
594
}
595

    
596

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

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

    
623
  $groups = array();
624

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

    
631
  foreach (get_object_vars($sequence) as $field => $value) {
632

    
633

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

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

    
649

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

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

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

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

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

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

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

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

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

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

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

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

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

    
760
  return $sequence_elements;
761
}
762

    
763

    
(6-6/10)