Project

General

Profile

Download (50.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 .= 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
        foreach ($specimen->types as $typeStatus => $typedTaxa) {
66
            if ($is_specimen_page) {
67
                if ($specimen->types) {
68
                    $detail_html .= "<i>" . $typeStatus . "</i>: ";
69
                    foreach ($typedTaxa as $typedTaxon) {
70
                        $detail_html .= $typedTaxon . " ";
71
                    }
72
                    $detail_html .= "<br>";
73
                }
74
            } else {
75
                $types .= $typeStatus . " ";
76

    
77
            }
78
        }
79
    }
80
    $derivateDataDTO = $specimen->derivateDataDTO;
81
    // - specimen scans
82
    $specimenScans = create_html_links($derivateDataDTO->specimenScans, true);
83
    // - molecular data
84
    $molecularData = "";
85
    if ($derivateDataDTO->molecularDataList) {
86
        foreach ($derivateDataDTO->molecularDataList as $molecular) {
87
            //provider link
88
            if (isset($molecular->providerLink)) {
89
                $molecularData .= create_html_link($molecular->providerLink, true);
90
            } else {
91
                $molecularData .= "[no provider]";
92
            }
93
            //contig link
94
            if (isset($molecular->contigFiles)) {
95
                $molecularData .= "[";
96
                if (sizeof($molecular->contigFiles) > 0) {
97
                    foreach ($molecular->contigFiles as $contigFile) {
98
                        if (isset($contigFile->contigLink)) {
99
                            if (isset($contigFile->contigLink->uri) and $contigFile->contigLink->uri != null) {
100
                                $molecularData .= create_html_link($contigFile->contigLink, true) . " ";
101
                            }
102
                        } else {
103
                            $molecularData .= "no contig ";
104
                        }
105
                        //primer links
106
                        if (isset($contigFile->primerLinks)) {
107
                            $molecularData .= create_html_links($contigFile->primerLinks, true);
108
                        }
109
                    }
110
                }
111
                $molecularData = rtrim($molecularData, " ");
112
                $molecularData .= "]";
113
            }
114
            //FIXME separate with comma (remove trailing comma)
115
        }
116
    }
117
    // - detail images
118
    $detailImages = create_html_links($derivateDataDTO->detailImages, true);
119

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

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

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

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

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

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

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

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

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

    
213

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

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

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

    
260

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

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

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

    
273
    if (is_object($specimenOrObservation)) {
274

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

    
278

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

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

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

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

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

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

    
318

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

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

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

    
339
                    case 'preferredStableUri':
340

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

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

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

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

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

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

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

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

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

    
423

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

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

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

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

    
450

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

    
460
                    // TODO preservation
461
                    // TODO exsiccatum
462

    
463

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

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

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

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

    
524
                }
525

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

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

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

    
539

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

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

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

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

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

    
590
    } // END of $specimenOrObservation exists
591

    
592
    return $derivatives;
593
}
594

    
595

    
596
/**
597
 * Compose an render array from a CDM Sequence object.
598
 *
599
 * compose_hook() implementation
600
 *
601
 * @param object $sequence
602
 *   CDM instance of type Sequence
603
 * @return array
604
 *   A render array containing the fields of the supplied $sequence
605
 *
606
 * @ingroup compose
607
 */
608
function compose_cdm_sequence($sequence)
609
{
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, 2);
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
 * Creates an array from a list of FieldUnitDTOs.
764
 *
765
 *
766
 * @param object $specimenOrObservations
767
 *   list of FieldUnitDTOs
768
 * @return array
769
 *   An array containing the hierarchy of the field units corresponding to the taxon
770
 *
771
 * @ingroup compose
772
 */
773
function create_specimen_array(array $specimenOrObservations){
774
    $items_specimen = array();
775
    $items = array();
776

    
777
    //we need one more item to contain the items of one level (fieldunit, derivate data etc.)
778
    foreach ($specimenOrObservations as &$specimenOrObservation) {
779
        $items['data'] = $specimenOrObservation->listLabel;
780
        $specimen = create_cdm_specimen_or_observation($specimenOrObservation);
781
        $children = array();
782
        $child = array();
783
        $child['data'] =$specimen;
784
       // $children[] = create_specimen_array($specimenOrObservation->derivates);
785
       if (isset($specimenOrObservation->derivates) && sizeof($specimenOrObservation->derivates) > 0){
786
           $child['children']= create_specimen_array($specimenOrObservation->derivates);
787
       }
788
       $children[]=$child;
789
       $items['children'] = $children;
790
       $items_specimen[] = $items;
791
    }
792
    return $items_specimen;
793
}
794

    
795

    
796
/**
797
 * Compose an render array from a CDM DerivedUnitFacade object.
798
 *
799
 * compose_hook() implementation
800
 *
801
 * @param object $specimenOrObservation
802
 *   the CDM instance of type SpecimenOrObservation to compose
803
 *   the render array for
804
 * @param array $derivatives
805
 *   the render array which contains the compositions of the derivatives
806
 *   of the supplied $specimenOrObservation
807
 *
808
 * @return array
809
 *   the supplied render array $derivatives to which the composition of the supplied
810
 *   $specimenOrObservation has been added to
811
 *
812
 * @ingroup compose
813
 */
814

    
815
function create_cdm_specimen_or_observation($specimenOrObservation)
816
{
817
    $exclude_occurrence_fields = &drupal_static(__FUNCTION__);
818
    if (!isset($exclude_occurrence_fields)) {
819
        $exclude_occurrence_fields = array(
820
            'type',
821
            'derivationEvent',
822
            'taxonRelatedDerivedUnits',
823
            'label',
824
            'titleCache',
825
            'listLabel',
826
            'protectedTitleCache',
827
            'class',
828
            'uuid',
829
            'collectionDTO'
830
        );
831
    }
832
    $items = array();
833

    
834
    // only show uuid it the user is logged in
835
    if (user_is_logged_in() && ($a_idx = array_search('uuid', $exclude_occurrence_fields)) !== FALSE) {
836
      //  unset($exclude_occurrence_fields[$a_idx]);
837
    }
838

    
839

    
840
    if (is_object($specimenOrObservation)) {
841

    
842
        $type_label = $specimenOrObservation->recordBase;
843
        RenderHints::setFootnoteListKey($type_label . '-' . $specimenOrObservation->uuid);
844

    
845
        // collect typeStatus as label
846
        if (isset($specimenOrObservation->specimenTypeDesignations)) {
847
            $type_status = array();
848
            foreach ($specimenOrObservation->specimenTypeDesignations as $typeDesignation) {
849
                if (isset($typeDesignation->typeStatus->representation_L10n)) {
850
                    $type_status[] = $typeDesignation->typeStatus->representation_L10n;
851
                }
852
            }
853
            if (count($type_status) > 0) {
854
                $type_label = implode(', ', $type_status);
855
            }
856
        }
857

    
858
        $title = $type_label . ': ' . $specimenOrObservation->titleCache;
859
        $items['data'] = $title;
860

    
861
        $groups = array();
862
        $items['children'] = $groups;
863
        $children_items = array();
864
        // --- add initialized fields
865
        foreach (get_object_vars($specimenOrObservation) as $field => $value) {
866
            $child_item = array();
867

    
868
            if (!in_array($field, $exclude_occurrence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
869
                switch ($field) {
870
//            case 'recordBasis':
871
//                if ($value != '' /* FieldUnit' */) {
872
//                    @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
873
//                }
874
//                break;
875
                    /* ---- java.lang.Object --- */
876

    
877
                    case 'markers':
878
                       /* $dd_elements = array();
879
                        foreach ($value as $marker) {
880
                            $dd_elements[] = compose_cdm_marker($marker);
881
                        }
882
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
883
                       */
884
                       break;
885

    
886

    
887
                    case 'annotations':
888
                        /*$dd_elements = array();
889
                        foreach ($value as $annotation) {
890
                            // TODO respect annotation type filter settings
891
                            $dd_elements[] = $annotation->text;
892
                        }
893
                        @_description_list_group_add($groups, t('Notes'), $dd_elements);
894
                        */
895
                        break;
896

    
897
                    /* ---- SpecimenOrObservationBase --- */
898
                    case 'sex':
899
                    case 'lifeStage':
900
                    case 'kindOfUnit':
901
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->representation_L10n, NULL, 1);
902
                        break;
903

    
904
                    case 'definition':
905
                        // TODO
906
                        break;
907

    
908
                    case 'preferredStableUri':
909

    
910
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), array(array('#markup' => cdm_external_uri($value, false))));
911
                        break;
912

    
913
                    case 'specimenTypeDesignations':
914
                      /*     $groups,
915
                            cdm_occurrence_field_name_label($field),
916
                            array(
917
                                '#markup' => theme('cdm_typedesignations', array('typeDesignations' => $value)),
918
                            )
919
                        );
920
                      */
921
                        break;
922

    
923
                    case 'determinations':
924
                        /*$dd_elements = array();
925
                        $glue = ', ';
926

    
927
                        foreach ($value as $determinationEvent) {
928
                            $timeperiod_string = NULL;
929
                            if (isset($determinationEvent->timeperiod)) {
930
                                $timeperiod_string = timePeriodToString($determinationEvent->timeperiod);
931
                            }
932
                            $weight = isset($determinationEvent->preferred) && $determinationEvent->preferred == 1 ? '0' : ($timeperiod_string ? $timeperiod_string : '1');
933
                            // check key exists
934
                            while (array_key_exists($weight, $dd_elements)) {
935
                                $weight .= '0';
936
                            }
937

    
938
                            $taxon_name = '';
939
                            $name_link = '';
940
                            if ($determinationEvent->taxonName) {
941
                                $taxon_name = $determinationEvent->taxonName;
942
                            } else if ($determinationEvent->taxon) {
943
                                $taxon_name = cdm_ws_get(CDM_WS_TAXON . '/$0/name', $determinationEvent->taxon->uuid);
944
                                $name_link = path_to_taxon($determinationEvent->taxon->uuid);
945
                            }
946
                            if ($taxon_name) {
947
                                $taxon_html = render_taxon_or_name($taxon_name, $name_link);
948
                                $dd_elements[$weight] = $taxon_html;
949
                            }
950
                            if (isset($determinationEvent->modifier)) {
951
                            }
952
                            if ($timeperiod_string) {
953
                                $dd_elements[$weight] .= $glue . $timeperiod_string;
954
                            }
955
                            if (isset($determinationEvent->actor->titleCache)) {
956
                                $dd_elements[$weight] .= $glue . $determinationEvent->actor->titleCache;
957
                            }
958
                            if (isset($determinationEvent->description)) {
959
                                $dd_elements[$weight] .= $glue . $determinationEvent->description;
960
                            }
961
                        }
962
                        ksort($dd_elements);
963
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('determinations'), $dd_elements);
964
                        */
965
                        break;
966

    
967
                    case 'listOfMedia':
968
                        $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
969
                        $captionElements = array(
970
                            '#uri' => t('open media'),
971
                        );
972
                        $gallery_html = compose_cdm_media_gallerie(array(
973
                            'mediaList' => $value,
974
                            'galleryName' => $specimenOrObservation->label,
975
                            'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
976
                            'cols' => $gallery_settings['cdm_dataportal_media_cols'],
977
                            'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
978
                            'captionElements' => $captionElements,
979
                            'mediaLinkType' => 'LIGHTBOX',
980
                            'alternativeMediaUri' => NULL,
981
                            'galleryLinkUri' => NULL,
982
                        ));
983

    
984
                         //@_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $gallery_html);
985
                         break;
986

    
987
                    case 'sources':
988
                        /*
989
                        $dd_elements = array();
990
                        foreach ($value as $identifiable_source) {
991
                            $dd_elements[] = theme('cdm_OriginalSource', array('source' => $identifiable_source));
992
                        }
993
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements, NULL, 10);
994
                        */
995
                        break;
996

    
997

    
998
                    /* ---- DerivedUnitBase --- */
999

    
1000

    
1001
                    case 'collection':
1002

    
1003
                        $sub_dl_groups = array();
1004
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('code'), $value->code, NULL, 1);
1005
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('codeStandard'), $value->codeStandard, NULL, 2);
1006
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('institute'), $value->institute, NULL, 3);
1007
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('townOrLocation'), $value->townOrLocation, NULL, 4);
1008
                        // TODO "superCollection"
1009
                        // TODO may have media
1010

    
1011
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
1012
                            array(
1013
                                array('#markup' => $value->titleCache),
1014
                                array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
1015
                            )
1016
                        );
1017

    
1018
                        break;
1019

    
1020
                    case 'storedUnder':
1021

    
1022
                        //@_description_list_group_add($groups, cdm_occurrence_field_name_label('storedUnder'), render_taxon_or_name($value));
1023
                        break;
1024

    
1025

    
1026
                    /* ---- Specimen --- */
1027
                    case 'sequences':
1028
                        /*
1029
                        $dd_elements = array();
1030
                        foreach ($value as $sequence) {
1031
                            $dd_elements[] = compose_cdm_sequence($sequence);
1032
                        }
1033
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
1034
                        */
1035
                        break;
1036

    
1037
                    // TODO preservation
1038
                    // TODO exsiccatum
1039

    
1040

    
1041
                    /* ---- FieldObservation --- */
1042
                    case 'gatheringEvent':
1043

    
1044
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('collector'), $value->actor->titleCache);
1045
                        @_description_list_group_add($groups, t('Gathering time'), timePeriodToString($value->timeperiod));
1046
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('description'), $value->description);
1047
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('locality'), $value->locality->text);
1048
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('country'), $value->country->representation_L10n);
1049
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingMethod'), $value->collectingMethod);
1050
                        if (isset($value->absoluteElevation)) {
1051
                            $min_max_markup = min_max_measure($value, 'absoluteElevation');
1052
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('absoluteElevation'), $min_max_markup);
1053
                        }
1054
                        if (isset($value->distanceToGround) && $value->distanceToGround >0) {
1055
                            $min_max_markup = min_max_measure($value, 'distanceToGround');
1056
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToGround'), $min_max_markup);
1057
                        }
1058
                        if (isset($value->distanceToWaterSurface) && $value->distanceToWaterSurface > 0) {
1059
                            $min_max_markup = min_max_measure($value, 'distanceToWaterSurface');
1060
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToWaterSurface'), $min_max_markup);
1061
                        }
1062

    
1063
                        if (isset($value->collectingAreas)) {
1064
                            $area_representations = array();
1065
                            foreach ($value->collectingAreas as $area) {
1066
                                $area_representations[] = l($area->representation_L10n, path_to_named_area($area->uuid));
1067
                            }
1068
                            if (!empty($area_representations))
1069
                                @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingAreas'),
1070
                                    array(
1071
                                        array('#markup' => implode(', ', $area_representations))
1072
                                    )
1073
                                );
1074
                        }
1075
                        if (isset($value->exactLocation)  && $value->exactLocation->sexagesimalString) {
1076
                            $sub_dl_groups = array();
1077
                            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('errorRadius'), $value->exactLocation->errorRadius, ' m', 1);
1078
                            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('longitude'), round($value->exactLocation->longitude, 7), '°', 2);
1079
                            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('latitude'), round($value->exactLocation->latitude, 7), '°', 3);
1080
                            if (isset($value->exactLocation->referenceSystem)) {
1081
                                @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('referenceSystem'), $value->exactLocation->referenceSystem->representation_L10n, '', 4);
1082
                            }
1083

    
1084
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('exactLocation'),
1085
                                array(
1086
                                    array('#markup' => $value->exactLocation->sexagesimalString),
1087
                                    array(
1088
                                        '#theme' => 'description_list',
1089
                                        '#groups' => $sub_dl_groups
1090
                                    ),
1091
                                )
1092
                            );
1093
                        }
1094

    
1095
                        break;
1096

    
1097
                    /* ---- DerivationEvent --- */
1098
                    case 'derivationEvents':
1099
                        //@_description_list_group_add($groups, t('Association type'), $value->description);
1100
                        break;
1101

    
1102

    
1103
                    default:
1104
                        if (is_object($value) || is_array($value)) {
1105
                           // drupal_set_message("Unhandled type in compose_cdm_specimenOrObservation() for field " . $field, "warning");
1106
                        } else {
1107
                            _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
1108
                        }
1109

    
1110
                }
1111

    
1112

    
1113
            }
1114
        } // END of loop over $derivedUnitFacade fields
1115

    
1116
        // Extensions
1117
       /* $extensions = cdm_ws_fetch_all(CDM_WS_PORTAL_OCCURRENCE . '/' . $specimenOrObservation->uuid . '/extensions', array($specimenOrObservation->uuid));
1118
        if ($extensions && count($extensions)) {
1119

    
1120
            $extensions_render_array = compose_extensions($extensions);
1121
            @_description_list_group_add($groups, t('Extensions') . ':',
1122
                $extensions_render_array,
1123
                '', 100);
1124
        }
1125
*/
1126

    
1127
        // template_preprocess_description_list() is not worting by weight so we do it right here
1128
        uasort($groups, 'element_sort');
1129

    
1130
        $occurrence_elements = array(
1131
           // '#title' => $title,
1132
            '#theme' => 'description_list',
1133
            '#groups' => $groups,
1134
            '#attributes' => array('class' => html_class_attribute_ref($specimenOrObservation)),
1135
        );
1136
        $output = drupal_render($occurrence_elements);
1137
        $output .= $gallery_html;
1138

    
1139

    
1140

    
1141

    
1142

    
1143

    
1144

    
1145

    
1146
    } // END of $specimenOrObservation exists
1147

    
1148
    return $output;
1149
}
1150

    
(6-6/10)