Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / modules / cdm_dataportal / includes / occurrences.inc @ 52f57468

History | View | Annotate | Download (51.4 KB)

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) && count($value->collectingAreas) > 0) {
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, $isSpecimenPage = false)
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
            'dnaMarker'
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

    
640
                    @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 2);
641
                    break;
642

    
643

    
644
                /*case 'dnaMarker': // 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.
645
                    /*if (isset($value->name)) {
646
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->name, NULL, 3);
647
                    }
648
                    if (isset($value->description)) {
649
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field) . ' ' . t('description'), $value->description, NULL, 4);
650
                    }
651
                    @_description_list_group_add($groups, cdm_occurrence_field_name_label($field) , $value, NULL, 4);
652
                    break;
653
                */
654
                case 'consensusSequence':
655
                    // format in genbank style, force linebreaks after each 70 nucleotites
656
                    // see also http://stackoverflow.com/questions/499137/css-how-can-i-force-a-long-string-without-any-blank-to-be-wrapped-in-xul-and
657
                    if ($value->length > 0) {
658
                        @_description_list_group_add(
659
                            $groups,
660
                            cdm_occurrence_field_name_label($field),
661
                            array(
662
                                array(
663
                                    '#markup' => '<div class="sequence-length">' . $value->length . ' ' . t('pb') . '</div><div>' . wordwrap($value->string, 70, '</br>', TRUE) . '</div>',
664
                                    '#wrapper_attributes' => array('class' => 'dna-sequence')
665
                                )
666
                            ),
667
                            5);
668
                    }
669
                    break;
670

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

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

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

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

    
709
                case 'rights':
710
                    array_merge($groups, cdm_rights_as_dl_groups($value));
711
                    break;
712

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

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

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

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

    
749
    // template_preprocess_description_list() is not worting by weight so we do it right here
750
    uasort($groups, 'element_sort');
751
    if ($isSpecimenPage) {
752
        $sequence_elements = array(
753
            '#title' => $sequence->dnaMarker,
754
            '#theme' => 'description_list',
755
            '#groups' => $groups
756
        );
757
    } else{
758
        $sequence_elements = array(
759
            '#title' => $sequence->dnaMarker -> titleCache,
760
            '#theme' => 'description_list',
761
            '#groups' => $groups
762
        );
763
    }
764

    
765
    return $sequence_elements;
766
}
767
/**
768
 * Creates an array from a list of FieldUnitDTOs.
769
 *
770
 *
771
 * @param object $specimenOrObservations
772
 *   list of FieldUnitDTOs
773
 * @return array
774
 *   An array containing the hierarchy of the field units corresponding to the taxon
775
 *
776
 * @ingroup compose
777
 */
778
function create_specimen_array(array $specimenOrObservations){
779
    $items_specimen = array();
780
    $items = array();
781

    
782
    //we need one more item to contain the items of one level (fieldunit, derivate data etc.)
783
    foreach ($specimenOrObservations as &$specimenOrObservation) {
784

    
785
        $items['data'] = $specimenOrObservation->listLabel ;
786
        $specimen = create_cdm_specimen_or_observation($specimenOrObservation);
787
        $children = array();
788
        $child = array();
789
        $child['data'] =$specimen;
790
       // $children[] = create_specimen_array($specimenOrObservation->derivates);
791
       if (isset($specimenOrObservation->derivates) && sizeof($specimenOrObservation->derivates) > 0){
792
           $child['children']= create_specimen_array($specimenOrObservation->derivates);
793
       }
794
       $children[]=$child;
795
       $items['children'] = $children;
796
       $items_specimen[] = $items;
797
    }
798
    return $items_specimen;
799
}
800

    
801

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

    
821
function create_cdm_specimen_or_observation($specimenOrObservation)
822
{
823
    $exclude_occurrence_fields = &drupal_static(__FUNCTION__);
824
    if (!isset($exclude_occurrence_fields)) {
825
        $exclude_occurrence_fields = array(
826
            'type',
827
            'derivationEvent',
828
            'taxonRelatedDerivedUnits',
829
            'label',
830
            'titleCache',
831
            'listLabel',
832
            'protectedTitleCache',
833
            'class',
834
            'uuid',
835
            'collectionDTO',
836
            'derivates'
837
        );
838
    }
839
    $items = array();
840

    
841
    // only show uuid it the user is logged in
842
    if (user_is_logged_in() && ($a_idx = array_search('uuid', $exclude_occurrence_fields)) !== FALSE) {
843
      //  unset($exclude_occurrence_fields[$a_idx]);
844
    }
845

    
846

    
847
    if (is_object($specimenOrObservation)) {
848

    
849
        $type_label = $specimenOrObservation->recordBase;
850
        RenderHints::setFootnoteListKey($type_label . '-' . $specimenOrObservation->uuid);
851

    
852
        // collect typeStatus as label
853
        if (isset($specimenOrObservation->specimenTypeDesignations)) {
854
            $type_status = array();
855
            foreach ($specimenOrObservation->specimenTypeDesignations as $typeDesignation) {
856
                if (isset($typeDesignation->typeStatus->representation_L10n)) {
857
                    $type_status[] = $typeDesignation->typeStatus->representation_L10n;
858
                }
859
            }
860
            if (count($type_status) > 0) {
861
                $type_label = implode(', ', $type_status);
862
            }
863
        }
864

    
865
        $title = $type_label . ': ' . $specimenOrObservation->titleCache;
866
        $items['data'] = $title;
867

    
868
        $groups = array();
869
        $items['children'] = $groups;
870
        $children_items = array();
871
        // --- add initialized fields
872
        foreach (get_object_vars($specimenOrObservation) as $field => $value) {
873
            $child_item = array();
874

    
875
            if (!in_array($field, $exclude_occurrence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
876
                switch ($field) {
877
//            case 'recordBasis':
878
//                if ($value != '' /* FieldUnit' */) {
879
//                    @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
880
//                }
881
//                break;
882
                    /* ---- java.lang.Object --- */
883

    
884
                    case 'markers':
885
                       /* $dd_elements = array();
886
                        foreach ($value as $marker) {
887
                            $dd_elements[] = compose_cdm_marker($marker);
888
                        }
889
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
890
                       */
891
                       break;
892

    
893

    
894
                    case 'annotations':
895
                        /*$dd_elements = array();
896
                        foreach ($value as $annotation) {
897
                            // TODO respect annotation type filter settings
898
                            $dd_elements[] = $annotation->text;
899
                        }
900
                        @_description_list_group_add($groups, t('Notes'), $dd_elements);
901
                        */
902
                        break;
903

    
904
                    /* ---- SpecimenOrObservationBase --- */
905
                    case 'sex':
906
                    case 'lifeStage':
907
                    case 'kindOfUnit':
908
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 1);
909
                        break;
910

    
911
                    case 'definition':
912
                        // TODO
913
                        break;
914

    
915
                    case 'preferredStableUri':
916

    
917
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), array(array('#markup' => cdm_external_uri($value, false))));
918
                        break;
919

    
920
                    case 'specimenTypeDesignations':
921
                      /*     $groups,
922
                            cdm_occurrence_field_name_label($field),
923
                            array(
924
                                '#markup' => theme('cdm_typedesignations', array('typeDesignations' => $value)),
925
                            )
926
                        );
927
                      */
928
                        break;
929

    
930
                    case 'determinations':
931
                        /*$dd_elements = array();
932
                        $glue = ', ';
933

    
934
                        foreach ($value as $determinationEvent) {
935
                            $timeperiod_string = NULL;
936
                            if (isset($determinationEvent->timeperiod)) {
937
                                $timeperiod_string = timePeriodToString($determinationEvent->timeperiod);
938
                            }
939
                            $weight = isset($determinationEvent->preferred) && $determinationEvent->preferred == 1 ? '0' : ($timeperiod_string ? $timeperiod_string : '1');
940
                            // check key exists
941
                            while (array_key_exists($weight, $dd_elements)) {
942
                                $weight .= '0';
943
                            }
944

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

    
974
                    case 'listOfMedia':
975
                        $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
976
                        $captionElements = array(
977
                            '#uri' => t('open media'),
978
                        );
979
                        $gallery_html = compose_cdm_media_gallerie(array(
980
                            'mediaList' => $value,
981
                            'galleryName' => $specimenOrObservation->label,
982
                            'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
983
                            'cols' => $gallery_settings['cdm_dataportal_media_cols'],
984
                            'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
985
                            'captionElements' => $captionElements,
986
                            'mediaLinkType' => 'LIGHTBOX',
987
                            'alternativeMediaUri' => NULL,
988
                            'galleryLinkUri' => NULL,
989
                        ));
990

    
991
                         //@_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $gallery_html);
992
                         break;
993

    
994
                    case 'sources':
995
                        /*
996
                        $dd_elements = array();
997
                        foreach ($value as $identifiable_source) {
998
                            $dd_elements[] = theme('cdm_OriginalSource', array('source' => $identifiable_source));
999
                        }
1000
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements, NULL, 10);
1001
                        */
1002
                        break;
1003

    
1004

    
1005
                    /* ---- DerivedUnitBase --- */
1006

    
1007

    
1008
                    case 'collection':
1009

    
1010
                        $sub_dl_groups = array();
1011
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('code'), $value->code, NULL, 1);
1012
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('codeStandard'), $value->codeStandard, NULL, 2);
1013
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('institute'), $value->institute, NULL, 3);
1014
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('townOrLocation'), $value->townOrLocation, NULL, 4);
1015
                        // TODO "superCollection"
1016
                        // TODO may have media
1017

    
1018
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
1019
                            array(
1020
                                array('#markup' => $value->titleCache),
1021
                                array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
1022
                            )
1023
                        );
1024

    
1025
                        break;
1026

    
1027
                    case 'storedUnder':
1028

    
1029
                        //@_description_list_group_add($groups, cdm_occurrence_field_name_label('storedUnder'), render_taxon_or_name($value));
1030
                        break;
1031

    
1032

    
1033
                    /* ---- Specimen --- */
1034
                    case 'sequences':
1035
                        $dd_elements = array();
1036
                        foreach ($value as $sequence) {
1037
                            $dd_elements[] = compose_cdm_sequence($sequence, true);
1038
                            $dd_elements[] = "";
1039
                        }
1040
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
1041
                        break;
1042

    
1043
                    // TODO preservation
1044
                    // TODO exsiccatum
1045

    
1046

    
1047
                    /* ---- FieldObservation --- */
1048
                    case 'gatheringEvent':
1049

    
1050
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('collector'), $value->collector);
1051
                        @_description_list_group_add($groups, t('Gathering time'), timePeriodToString($value->timeperiod));
1052
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('description'), $value->description);
1053
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('locality'), $value->locality);
1054
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('country'), $value->country);
1055
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingMethod'), $value->collectingMethod);
1056
                        if (isset($value->absoluteElevation)) {
1057
                            $min_max_markup = min_max_measure($value, 'absoluteElevation');
1058
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('absoluteElevation'), $min_max_markup);
1059
                        }
1060
                        if (isset($value->distanceToGround) && $value->distanceToGround >0) {
1061
                            $min_max_markup = min_max_measure($value, 'distanceToGround');
1062
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToGround'), $min_max_markup);
1063
                        }
1064
                        if (isset($value->distanceToWaterSurface) && $value->distanceToWaterSurface > 0) {
1065
                            $min_max_markup = min_max_measure($value, 'distanceToWaterSurface');
1066
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToWaterSurface'), $min_max_markup);
1067
                        }
1068

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

    
1091
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('exactLocation'),
1092
                                array(
1093
                                    array('#markup' => $value->exactLocation->sexagesimalString),
1094
                                    array(
1095
                                        '#theme' => 'description_list',
1096
                                        '#groups' => $sub_dl_groups
1097
                                    ),
1098
                                )
1099
                            );
1100
                        }
1101

    
1102
                        break;
1103

    
1104
                    /* ---- DerivationEvent --- */
1105
                    case 'derivationEvents':
1106
                        //@_description_list_group_add($groups, t('Association type'), $value->description);
1107
                        break;
1108

    
1109

    
1110
                    default:
1111
                        if (is_object($value) || is_array($value)) {
1112
                            drupal_set_message("Unhandled type in compose_cdm_specimenOrObservation() for field " . $field, "warning");
1113
                        } else {
1114
                            _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
1115
                        }
1116

    
1117
                }
1118

    
1119

    
1120
            }
1121
        } // END of loop over $derivedUnitFacade fields
1122

    
1123
        // Extensions
1124
       /* $extensions = cdm_ws_fetch_all(CDM_WS_PORTAL_OCCURRENCE . '/' . $specimenOrObservation->uuid . '/extensions', array($specimenOrObservation->uuid));
1125
        if ($extensions && count($extensions)) {
1126

    
1127
            $extensions_render_array = compose_extensions($extensions);
1128
            @_description_list_group_add($groups, t('Extensions') . ':',
1129
                $extensions_render_array,
1130
                '', 100);
1131
        }
1132
*/
1133

    
1134
        // template_preprocess_description_list() is not worting by weight so we do it right here
1135
        uasort($groups, 'element_sort');
1136

    
1137
        $occurrence_elements = array(
1138
           // '#title' => $title,
1139
            '#theme' => 'description_list',
1140
            '#groups' => $groups,
1141
            '#attributes' => array('class' => html_class_attribute_ref($specimenOrObservation)),
1142
        );
1143
        $output = drupal_render($occurrence_elements);
1144
        $output .= $gallery_html;
1145
        $pathToSpecimen = path_to_specimen($specimenOrObservation->uuid);
1146
        $output .=  l("detail page", $pathToSpecimen, array('attributes' => array('target' => '_blank')));
1147

    
1148

    
1149

    
1150

    
1151

    
1152

    
1153

    
1154

    
1155

    
1156
    } // END of $specimenOrObservation exists
1157

    
1158
    return $output;
1159
}
1160

    
Add picture from clipboard (Maximum size: 40 MB)