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

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

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

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

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

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

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

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

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

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

    
216

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

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

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

    
262

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

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

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

    
275
  if (is_object($specimenOrObservation)) {
276

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

    
280

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

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

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

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

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

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

    
320

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

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

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

    
341
          case 'preferredStableUri':
342

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

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

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

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

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

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

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

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

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

    
425

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

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

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

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

    
452

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

    
462
          // TODO preservation
463
          // TODO exsiccatum
464

    
465

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

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

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

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

    
526
        }
527

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

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

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

    
541

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

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

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

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

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

    
592
  } // END of $specimenOrObservation exists
593

    
594
  return $derivatives;
595
}
596

    
597

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

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

    
624
  $groups = array();
625

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

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

    
634

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

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

    
650

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

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

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

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

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

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

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

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

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

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

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

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

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

    
761
  return $sequence_elements;
762
}
763

    
764

    
(6-6/10)