Project

General

Profile

Download (62.6 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)
29
{
30

    
31
    $specimen_details = compose_cdm_specimen_or_observation($specimen, true);
32
    //$detail_html .= drupal_render($specimen_details);
33

    
34
    $specimen->_fieldObjectMedia = cdm_ws_get(CDM_WS_DERIVEDUNIT_FACADE, array(
35
        $specimen->uuid,
36
        'fieldObjectMediaDTO',
37
    ));
38
    $specimen->_derivedUnitMedia = cdm_ws_get(CDM_WS_DERIVEDUNIT_FACADE, array(
39
        $specimen->uuid,
40
        'derivedUnitMedia',
41
    ));
42
    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
43
    $captionElements = array(
44
        'title',
45
        '#uri' => t('open media'),
46
    );
47
    if (isset($specimen->fieldObjectMediaDTO) ) {
48
        $gallery_html = compose_cdm_media_gallerie(array(
49
            'mediaList' => $specimen->fieldObjectMediaDTO,
50
            'galleryName' => $specimen->titleCache,
51
            'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
52
            'cols' => $gallery_settings['cdm_dataportal_media_cols'],
53
            'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
54
            'captionElements' => $captionElements,
55
            'mediaLinkType' => 'LIGHTBOX',
56
            'alternativeMediaUri' => NULL,
57
            'galleryLinkUri' => NULL,
58
        ));
59
        $specimen_details[] = markup_to_render_array($gallery_html);
60
    }
61

    
62
    if (isset($specimen->_derivedUnitMedia) ) {
63
        $gallery_html = compose_cdm_media_gallerie(array(
64
            'mediaList' => $specimen->_derivedUnitMedia,
65
            'galleryName' => $specimen->titleCache,
66
            'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
67
            'cols' => $gallery_settings['cdm_dataportal_media_cols'],
68
            'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
69
            'captionElements' => $captionElements,
70
            'mediaLinkType' => 'LIGHTBOX',
71
            'alternativeMediaUri' => NULL,
72
            'galleryLinkUri' => NULL,
73
        ));
74
        $specimen_details[] = markup_to_render_array($gallery_html);
75
    }
76

    
77
    $specimen_array = $specimen_details;
78
    $output = "";
79
    foreach($specimen_array as $value){
80
        $renderArray = array(
81
            '#theme' => 'item_list',
82
            '#items' => array($value),
83
            '#type' => 'ul');
84
        $output .= drupal_render($renderArray);
85
    }
86

    
87

    
88
    return $output;
89
}
90

    
91

    
92
/**
93
 * Provides the HTML markup for a specimen page
94
 *
95
 * @param $specimen
96
 *
97
 * @return string
98
 *  The markup for a specimen page
99
 */
100
function render_cdm_specimenDTO_page($specimen, $is_specimen_page = false)
101
{
102
    $detail_html = "";
103
    //link to specimen page
104
    $pathToSpecimen = path_to_specimen($specimen->uuid);
105
    if (!$is_specimen_page) {
106
        $specimenPageLink = l($specimen->accessionNumber, $pathToSpecimen, array('attributes' => array('target' => '_blank')));
107
        $detail_html .= "<strong>Specimen summary: $specimenPageLink</strong><br>";
108
    }
109

    
110

    
111
    if($is_specimen_page) {
112
        if($specimen->citation){
113
            $detail_html .= "<br>".$specimen->citation."<br>";
114

    
115
        }
116
    }
117
    if ($specimen->preferredStableUri) {
118
        $stableIdentifierLink = l($specimen->preferredStableUri, $specimen->preferredStableUri, array('attributes' => array('target' => '_blank')));
119
        $detail_html .= create_label("Preferred stable URI") . $stableIdentifierLink . "<br>";
120
    }
121
    if ($is_specimen_page) {
122
        // associated taxa
123
        if ($specimen->associatedTaxa) {
124
            $detail_html .= "<br>";
125
            $detail_html .= create_label("Associated with");
126
            if (sizeof($specimen->associatedTaxa) > 1) {
127
                $detail_html .= "<br>";
128
            }
129
            foreach ($specimen->associatedTaxa as $associatedTaxon) {
130
                $detail_html .= l($associatedTaxon->value, path_to_taxon($associatedTaxon->key, "specimens"));//$associatedTaxon->second."<br>";
131
            }
132
            $detail_html .= "<br>";
133
        }
134
    }
135
    // - type information
136
    $types = "";
137
    if (isset($specimen->types)) {
138
        //typed taxa
139
        foreach ($specimen->types as $typeStatus => $typedTaxa) {
140
            if($specimen->types){
141
                if(empty($typeStatus) || $typeStatus == "_empty_"|| $typeStatus == ""){
142
                    $detail_html .= "<i>Type for:</i> ";
143
                } else {
144
                    $detail_html .= "<i>".$typeStatus."</i> of ";
145
                }
146
                foreach($typedTaxa as $typedTaxon){
147
                    $detail_html .= $typedTaxon." ";
148

    
149
                }
150
            } else {
151
                $types .= $typeStatus . " ";
152

    
153
            }
154
        }
155
    }
156
    $derivateDataDTO = $specimen->derivateDataDTO;
157
    // - specimen scans
158
    $specimenScans = create_html_links($derivateDataDTO->specimenScans, true);
159
    // - molecular data
160
    $molecularData = "";
161
    if ($derivateDataDTO->molecularDataList) {
162
        foreach ($derivateDataDTO->molecularDataList as $molecular) {
163
            //provider link
164
            if (isset($molecular->providerLink)) {
165
                $molecularData .= create_html_link($molecular->providerLink, true);
166
            } else {
167
                $molecularData .= "[no provider]";
168
            }
169
            //contig link
170
            if (isset($molecular->contigFiles)) {
171
                $molecularData .= "[";
172
                if (sizeof($molecular->contigFiles) > 0) {
173
                    foreach ($molecular->contigFiles as $contigFile) {
174
                        if (isset($contigFile->contigLink)) {
175
                            if (isset($contigFile->contigLink->uri) and $contigFile->contigLink->uri != null) {
176
                                $molecularData .= create_html_link($contigFile->contigLink, true) . " ";
177
                            }
178
                        } else {
179
                            $molecularData .= "no contig ";
180
                        }
181
                        //primer links
182
                        if (isset($contigFile->primerLinks)) {
183
                            $molecularData .= create_html_links($contigFile->primerLinks, true);
184
                        }
185
                    }
186
                }
187
                $molecularData = rtrim($molecularData, " ");
188
                $molecularData .= "]";
189
            }
190
            //FIXME separate with comma (remove trailing comma)
191
        }
192
    }
193
    // - detail images
194
    $detailImages = create_html_links($derivateDataDTO->detailImages, true);
195

    
196
    if ($types) {
197
        $detail_html .= $is_specimen_page ? "<br>" : "";
198
        $detail_html .= create_label("Type(s)") . $types . "<br>";
199
    }
200
    if ($specimenScans and !$is_specimen_page) {
201
        $detail_html .= create_label("Specimen Scans") . $specimenScans . "<br>";
202
    }
203
    //specimen scan image gallery
204
    if ($is_specimen_page and isset($derivateDataDTO->specimenScanUuids) and !empty($derivateDataDTO->specimenScanUuids)) {
205
        $detail_html .= addImageGallery("Specimen scans", $derivateDataDTO->specimenScanUuids);
206
    }
207

    
208
    if ($molecularData) {
209
        $detail_html .= $is_specimen_page ? "<br>" : "";
210
        $detail_html .= create_label("Molecular Data") . $molecularData . "<br>";
211
    }
212

    
213
    if ($detailImages and !$is_specimen_page) {
214
        $detail_html .= create_label("Detail Images") . $detailImages . "<br>";
215
    }
216

    
217
    //detail image gallery
218
    if ($is_specimen_page and isset($derivateDataDTO->detailImageUuids) and !empty($derivateDataDTO->detailImageUuids)) {
219
        $detail_html .= addImageGallery("Detail Images", $derivateDataDTO->detailImageUuids);
220
    }
221

    
222
    //character data
223
    if ($specimen->characterData) {
224
        $detail_html .= $is_specimen_page ? "<br>" : "";
225
        $detail_html .= create_label("Character Data");
226
        if ($is_specimen_page) {
227
            $detail_html .= "<br>";
228
            foreach ($specimen->characterData as $characterStatePair) {
229
                $detail_html .= "<i>" . $characterStatePair->first . "</i>:" . $characterStatePair->second;
230
                $detail_html .= "<br>";
231
            }
232
        } else {
233
            $detail_html .= l("detail page", $pathToSpecimen, array('attributes' => array('target' => '_blank')));
234
            $detail_html .= "<br>";
235
        }
236
    }
237
    return $detail_html;
238
}
239

    
240
function addImageGallery($galleryName, $imageUuids)
241
{
242
    $images = array();
243
    foreach ($imageUuids as $uuid) {
244
        $images[] = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $uuid);
245
    }
246

    
247
    $gallery_html = '';
248
    if (count($imageUuids) > 0) {
249
        $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
250
        $captionElements = array(
251
            'title',
252
            'rights',
253
        );
254
        $alternativeMediaUris = array();
255
        foreach ($images as $image) {
256
            $mediaUri = getMediaUri($image);
257
            if ($mediaUri) {
258
                $alternativeMediaUris[] = $mediaUri;
259
            } else {
260
                $alternativeMediaUris[] = path_to_media($image->uuid);
261
            }
262
        }
263

    
264

    
265

    
266
        $gallery_html = compose_cdm_media_gallerie(array(
267
            'mediaList' => $images,
268
            'galleryName' => $galleryName,
269
            'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
270
            'cols' => $gallery_settings['cdm_dataportal_media_cols'],
271
            'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
272
            'captionElements' => $captionElements,
273
            'mediaLinkType' => 'LIGHTBOX',
274
          // 'alternativeMediaUri' => $alternativeMediaUris,
275
            'alternativeMediaUri' => NULL,
276
            'galleryLinkUri' => NULL,
277
        ));
278
    }
279
    return "<br>" . create_label($galleryName) . "<br>" . $gallery_html;
280
}
281

    
282
function getMediaUri($media)
283
{
284
    if (isset($media->representations) && sizeof($media->representations) == 1
285
        && isset($media->representations[0]->parts) &&
286
        sizeof($media->representations[0]->parts) == 1) {
287
        return $media->representations[0]->parts[0]->uri;
288
    }
289
    return null;
290
}
291

    
292

    
293
/**
294
 * Formats the given string to a label for displaying key-object pairs in HTML
295
 * @return string
296
 */
297
function create_label($label)
298
{
299
    return "<span class='specimen_table_label'>" . $label . ": </span>";
300
}
301

    
302
/**
303
 * Compose an render array from a CDM DerivedUnitFacade object.
304
 *
305
 * compose_hook() implementation
306
 *
307
 * @param object $specimenOrObservation
308
 *   the CDM instance of type SpecimenOrObservation to compose
309
 *   the render array for
310
 * @param array $derivatives
311
 *   the render array which contains the compositions of the derivatives
312
 *   of the supplied $specimenOrObservation
313
 *
314
 * @return array
315
 *   the supplied render array $derivatives to which the composition of the supplied
316
 *   $specimenOrObservation has been added to
317
 *
318
 * @ingroup compose
319
 */
320

    
321
function compose_cdm_specimen_or_observation($specimen_or_observation, $isSpecimen_page = false, &$derivatives = null)
322
{
323
    $exclude_occurrence_fields = &drupal_static(__FUNCTION__);
324
    if (!isset($exclude_occurrence_fields)) {
325
        $exclude_occurrence_fields = array(
326

    
327
            'titleCache',
328
            'protectedTitleCache',
329
            'derivedUnitMedia',
330
            'created',
331
            'publish',
332
            'updated',
333
            'class',
334
            'uuid',
335
            ''
336
        );
337
    }
338
    if (variable_get('cdm_dataportal_compressed_specimen_derivate_table') || !$isSpecimen_page){
339
        $exclude_occurrence_fields[] = 'derivationEvents';
340
    }
341

    
342

    
343
    // only show uuid it the user is logged in
344
 //   if(user_is_logged_in() && ($a_idx = array_search('uuid', $exclude_occurrence_fields)) !== FALSE ) {
345
  //      unset($exclude_occurrence_fields[$a_idx]);
346
  //  }
347

    
348
    if (!isset($derivatives)) {
349
        $derivatives = array();
350
    }
351

    
352
    $descriptions = null;
353
    $derivedFrom = null;
354

    
355

    
356

    
357
    if (is_object($specimen_or_observation)) {
358

    
359
        // request again for deeper initialization
360
        $specimen_or_observation = cdm_ws_get("portal/" . CDM_WS_OCCURRENCE, $specimen_or_observation->uuid);
361
        if ($specimen_or_observation->class == 'FieldUnit'){
362
            $specimen_or_observation->_derivedUnitMedia = cdm_ws_get(CDM_WS_DERIVEDUNIT_FACADE, array(
363
                $specimen_or_observation->uuid,
364
                'fieldObjectMediaDTO',
365
            ));
366
        }else{
367
            $specimen_or_observation->_derivedUnitMedia = cdm_ws_get(CDM_WS_DERIVEDUNIT_FACADE, array(
368
                $specimen_or_observation->uuid,
369
                'derivedUnitMedia',
370
            ));
371
        }
372

    
373
        $type_label = $specimen_or_observation->class;
374
        RenderHints::setFootnoteListKey($type_label . '-' . $specimen_or_observation->uuid);
375

    
376
        // collect typeStatus as label
377
        if (isset($specimen_or_observation->specimenTypeDesignations)) {
378
            $type_status = array();
379
            foreach ($specimen_or_observation->specimenTypeDesignations as $typeDesignation) {
380
                if (isset($typeDesignation->typeStatus->representation_L10n)) {
381
                    $type_status[] = $typeDesignation->typeStatus->representation_L10n;
382
                }
383
            }
384
            if (count($type_status) > 0) {
385
                $type_label = implode(', ', $type_status);
386
            }
387
        }
388

    
389
        $title = $type_label . ': ' . $specimen_or_observation->titleCache;
390

    
391
        $groups = array();
392

    
393
        // --- add initialized fields
394
        foreach (get_object_vars($specimen_or_observation) as $field => $value) {
395
            if (!in_array($field, $exclude_occurrence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
396
                switch ($field) {
397

    
398
                    /* ---- java.lang.Object --- */
399
                    case 'class':
400
                        if ($value != '' /* FieldUnit' */) {
401
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
402
                        }
403
                        break;
404

    
405
                    case 'markers':
406
                        $dd_elements = array();
407
                        foreach ($value as $marker) {
408
                            $dd_elements[] = compose_cdm_marker($marker);
409
                        }
410
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
411
                        break;
412

    
413

    
414
                    case 'annotations':
415
                        $dd_elements = array();
416
                        foreach ($value as $annotation) {
417
                            // TODO respect annotation type filter settings
418
                            $dd_elements[] = $annotation->text;
419
                        }
420
                        @_description_list_group_add($groups, t('Notes'), $dd_elements);
421
                        break;
422

    
423
                    /* ---- SpecimenOrObservationBase --- */
424
                    case 'sex':
425
                    case 'lifeStage':
426
                    case 'kindOfUnit':
427
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->representation_L10n);
428
                        break;
429

    
430
                    case 'definition':
431
                        // TODO
432
                        break;
433

    
434
                    case 'preferredStableUri':
435

    
436
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), array(array('#markup' => cdm_external_uri($value, false))));
437
                        break;
438

    
439
                    case 'specimenTypeDesignations':
440

    
441
                        //TODO: better display of details!!
442
                        @_description_list_group_add(
443
                            $groups,
444
                            cdm_occurrence_field_name_label($field),
445
                            array(
446
                                '#markup' => theme('cdm_typedesignations', array('typeDesignations' => $value)),
447
                            )
448
                        );
449
                        break;
450

    
451
                    case 'determinations':
452
                        $dd_elements = array();
453
                        $glue = ', ';
454

    
455
                        foreach ($value as $determinationEvent) {
456
                            $timeperiod_string = NULL;
457
                            if (isset($determinationEvent->timeperiod)) {
458
                                $timeperiod_string = timePeriodToString($determinationEvent->timeperiod);
459
                            }
460
                            $weight = isset($determinationEvent->preferred) && $determinationEvent->preferred == 1 ? '0' : ($timeperiod_string ? $timeperiod_string : '1');
461
                            // check key exists
462
                            while (array_key_exists($weight, $dd_elements)) {
463
                                $weight .= '0';
464
                            }
465

    
466
                            $taxon_name = '';
467
                            $name_link = '';
468
                            if ($determinationEvent->taxonName) {
469
                                $taxon_name = cdm_ws_get(CDM_WS_NAME, $determinationEvent->taxonName->uuid);
470
                                $taxon_name->taggedName = cdm_ws_get(CDM_WS_NAME, array($determinationEvent->taxonName->uuid, "taggedName"));
471
                                $name_link = path_to_name($determinationEvent->taxonName->uuid);
472
                            } else if ($determinationEvent->taxon) {
473
                                $taxon_name = cdm_ws_get(CDM_WS_TAXON . '/$0/name', $determinationEvent->taxon->uuid);
474
                                $name_link = path_to_taxon($determinationEvent->taxon->uuid);
475
                            }
476
                            if ($taxon_name) {
477
                                //$taxon_html = render_taxon_or_name($taxon_name, $name_link);
478

    
479
                                $taxon_html = l($taxon_name->titleCache, $name_link, array('attributes' => array('target' => '_blank')));
480
                                $dd_elements[$weight] = $taxon_html;
481
                            }
482
                            if (isset($determinationEvent->modifier)) {
483
                                $dd_elements[$weight] .= cdm_term_representation($determinationEvent->modifier);
484
                            }
485
                            if ($timeperiod_string) {
486
                                $dd_elements[$weight] .= $glue . $timeperiod_string;
487
                            }
488
                            if (isset($determinationEvent->actor->titleCache)) {
489
                                $dd_elements[$weight] .= $glue . $determinationEvent->actor->titleCache;
490
                            }
491
                            if (isset($determinationEvent->description)) {
492
                                $dd_elements[$weight] .= $glue . $determinationEvent->description;
493
                            }
494
                        }
495
                        ksort($dd_elements);
496
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('determinations'), $dd_elements);
497
                        break;
498

    
499
                    case 'descriptions':
500
                        $occurrence_featureTree = cdm_get_occurrence_featureTree();
501
                        $dd_elements = array();
502

    
503
                        foreach ($value as $description) {
504
                            $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, $description->uuid);
505

    
506
                            if ($description->imageGallery == TRUE) {
507
                                continue;
508
                            }
509
                            $elements_by_feature = _mergeFeatureTreeDescriptions($occurrence_featureTree->root->childNodes, $description->elements);
510
                            $description_render_elements = _block_get_renderable_array(make_feature_block_list($elements_by_feature, null));
511
                            $dd_elements[] = $description_render_elements;
512
                        }
513

    
514
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
515
                        break;
516
                    case '_derivedUnitMedia':
517
                        if ($isSpecimen_page) {
518
                            $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
519
                            $captionElements = array(
520
                                '#uri' => t('open media'),
521
                            );
522
                            $gallery_html = compose_cdm_media_gallerie(array(
523
                                'mediaList' => $value,
524
                                'galleryName' => $specimen_or_observation->titleCache,
525
                                'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
526
                                'cols' => $gallery_settings['cdm_dataportal_media_cols'],
527
                                'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
528
                                'captionElements' => $captionElements,
529
                                'mediaLinkType' => 'LIGHTBOX',
530
                                'alternativeMediaUri' => NULL,
531
                                'galleryLinkUri' => NULL,
532
                            ));
533
                            @_description_list_group_add($groups, "Detail Images:", markup_to_render_array($gallery_html), '', 20);
534
                        }
535

    
536
                        // $dd_elements[] = markup_to_render_array($gallery_html);
537

    
538
                        break;
539
                    case 'sources':
540
                        $annotations_and_sources = handle_annotations_and_sources(
541
                            $specimen_or_observation,
542
                            array(
543
                                'sources_as_content' => true,
544
                                'link_to_name_used_in_source' => 1,
545
                                'link_to_reference' => 0,
546
                                'add_footnote_keys' => 0
547
                            ),
548
                            NULL,
549
                            ''
550
                        );
551
                        if (!empty($annotations_and_sources['source_references'])) {
552
                            @_description_list_group_add($groups, t('Sources') . ':', join(', ', $annotations_and_sources['source_references']), '', 12);
553
                        }
554
                        break;
555

    
556

    
557
                    /* ---- DerivedUnitBase --- */
558
                    case 'derivedFrom':
559
                        $derivedFrom = $value;
560
                        if ($isSpecimen_page) {
561
                            foreach ($derivedFrom->originals as $original) {
562
                                $pathToSpecimen = path_to_specimen($original->uuid);
563
                                $description = "";
564
                                if (isset($derivedFrom->description) && $derivedFrom->description != '') {
565
                                    $description = $derivedFrom->description . ": ";
566
                                }
567

    
568
                                $originals[] = markup_to_render_array(l($description . $original->titleCache, $pathToSpecimen, array('attributes' => array('target' => '_blank'))));
569
                                if ($original->class == 'FieldUnit') {
570
                                    $label = t('Field data');
571
                                } else {
572
                                    $label = t('Derived from');
573
                                }
574
                                @_description_list_group_add($groups, $label . ':',
575
                                    $originals,
576
                                    '', 13);
577
                            }
578
                        }
579
                        break;
580
                    case 'derivationEvents':
581
                        $derivationEvents = $value;
582
                        $derived_units = array();
583
                        if ($isSpecimen_page) {
584
                            foreach ($derivationEvents as $derivationEvent) {
585
                                foreach ($derivationEvent->derivatives as $derived_unit) {
586
                                    $pathToSpecimen = path_to_specimen($derived_unit->uuid);
587
                                    $description = "";
588
                                    if (isset($derived_unit->description) && $derived_unit->description != '') {
589
                                        $description = $derived_unit->description . ": ";
590
                                    }
591

    
592
                                    $derived_units[] = markup_to_render_array(l($description . $derived_unit->titleCache, $pathToSpecimen, array('attributes' => array('target' => '_blank'))));
593
                                }
594
                            }
595
                            @_description_list_group_add($groups, t('Derivates') . ':',
596
                                $derived_units,
597
                                '', 100);
598

    
599
                        }
600
                        break;
601

    
602
                    case 'collection':
603
                        $sub_dl_groups = array();
604
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('code'), $value->code, NULL, 1);
605
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('codeStandard'), $value->codeStandard, NULL, 2);
606
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('institute'), $value->institute, NULL, 3);
607
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('townOrLocation'), $value->townOrLocation, NULL, 4);
608
                        // TODO "superCollection"
609
                        // TODO may have media
610

    
611
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
612
                            array(
613
                                array('#markup' => $value->titleCache),
614
                                array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
615
                            )
616
                        );
617
                        break;
618

    
619
                    case 'storedUnder':
620
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('storedUnder'), render_taxon_or_name($value));
621
                        break;
622
                    case 'dnaQuality':
623
                        $sub_dl_groups = array();
624

    
625
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('purificationMethod'), $value->purificationMethod, NULL, 1);
626
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('ratioOfAbsorbance260_230'), $value->ratioOfAbsorbance260_230, NULL, 2);
627
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('ratioOfAbsorbance260_280'), $value->ratioOfAbsorbance260_280, NULL, 3);
628
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('concentration'), $value->concentration, NULL, 4);
629
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('concentrationUnit'), $value->concentrationUnit, NULL, 4);
630
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('qualityTerm'), $value->qualityTerm, NULL, 4);
631
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('qualityCheckDate'), $value->qualityCheckDate, NULL, 4);
632

    
633
                        if (is_array($sub_dl_groups) && sizeof($sub_dl_groups)>0) {
634
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
635
                                array(
636
                                    array('#markup' => $value->titleCache),
637
                                    array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
638
                                )
639
                            );
640
                        }
641
                        break;
642

    
643
                    case 'preservation':
644
                        $sub_dl_groups = array();
645

    
646
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('medium'), $value->medium, NULL, 1);
647
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('temperature'), $value->temperature, NULL, 2);
648
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('definedMaterialOrMethod'), $value->definedMaterialOrMethod, NULL, 3);
649

    
650
                        if (is_array($sub_dl_groups) && sizeof($sub_dl_groups)>0) {
651
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
652
                                array(
653
                                    array('#markup' => $value->titleCache),
654
                                    array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
655
                                )
656
                            );
657
                        }
658
                        break;
659

    
660
                    /* ---- Specimen --- */
661
                    case 'sequences':
662
                        $dd_elements = array();
663
                        foreach ($value as $sequence) {
664
                            $dd_elements[] = compose_cdm_sequence($sequence);
665
                        }
666
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
667
                        break;
668

    
669
                    // TODO preservation
670
                    // TODO exsiccatum
671

    
672

    
673
                    /* ---- FieldObservation --- */
674
                    case 'gatheringEvent':
675
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('collector'), $value->actor->titleCache, '', 1);
676
                        @_description_list_group_add($groups, t('Gathering date'), timePeriodToString($value->timeperiod), '', 2);
677
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('description'), $value->description, '', 3);
678
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('locality'), $value->locality->text, '', 10);
679
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('country'), $value->country->representation_L10n, '', 4);
680
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingMethod'), $value->collectingMethod,'',5);
681
                        if (isset($value->absoluteElevation)) {
682
                            $min_max_markup = min_max_measure($value, 'absoluteElevation');
683
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('absoluteElevation'), $min_max_markup, '',6);
684
                        }
685
                        if (isset($value->distanceToGround)) {
686
                            $min_max_markup = min_max_measure($value, 'distanceToGround');
687
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToGround'), $min_max_markup,'',7);
688
                        }
689
                        if (isset($value->distanceToWaterSurface)) {
690
                            $min_max_markup = min_max_measure($value, 'distanceToWaterSurface');
691
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToWaterSurface'), $min_max_markup, '',8);
692
                        }
693
                        if (isset($value->collectingAreas) && count($value->collectingAreas) > 0) {
694
                            $area_representations = array();
695
                            foreach ($value->collectingAreas as $area) {
696
                                $area_representations[] = l($area->representation_L10n, path_to_named_area($area->uuid));
697
                            }
698
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingAreas'),
699
                                array(
700
                                    array('#markup' => implode(', ', $area_representations))
701
                                ),'',9
702
                            );
703
                        }
704
                        if (isset($value->exactLocation) && $value->exactLocation->sexagesimalString) {
705
                            $sub_dl_groups = array();
706
                            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('errorRadius'), $value->exactLocation->errorRadius, ' m', 1);
707
                            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('longitude'), round($value->exactLocation->longitude, 7), '°', 2);
708
                            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('latitude'), round($value->exactLocation->latitude, 7), '°', 3);
709
                            if (isset($value->exactLocation->referenceSystem)) {
710
                                @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('referenceSystem'), $value->exactLocation->referenceSystem->representation_L10n, '', 4);
711
                            }
712

    
713
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('exactLocation'),
714
                                array(
715
                                    array('#markup' => $value->exactLocation->sexagesimalString),
716
                                    array(
717
                                        '#theme' => 'description_list',
718
                                        '#groups' => $sub_dl_groups
719
                                    ),
720
                                ),'',11
721
                            );
722
                        }
723
                        break;
724

    
725
                    default:
726
                        if (is_object($value) || is_array($value)) {
727
                            drupal_set_message("Unhandled type in compose_cdm_specimen_or_observation() for field " . $field, "warning");
728
                        } else {
729
                            _description_list_group_add($groups, cdm_occurrence_field_name_label($field), cdm_occurrence_field_name_label($value));
730
                        }
731

    
732
                }
733

    
734
            }
735
        } // END of loop over $derivedUnitFacade fields
736

    
737
        // Extensions
738
        $extensions = cdm_ws_fetch_all(CDM_WS_PORTAL_OCCURRENCE . '/'  . $specimen_or_observation->uuid . '/extensions', array($specimen_or_observation->uuid));
739
        if ($extensions && count($extensions)) {
740

    
741
            $extensions_render_array = compose_extensions($extensions);
742
            @_description_list_group_add($groups, t('Extensions') . ':',
743
                $extensions_render_array,
744
                '', 100);
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

    
752
        $occurrence_elements = array(
753
           // '#title' => $title,
754
            '#theme' => 'description_list',
755
            '#groups' => $groups,
756
            '#attributes' => array('class' => html_class_attribute_ref($specimen_or_observation)),
757
        );
758

    
759
        $derivatives[] = drupal_render($occurrence_elements);
760
        // all footnotes which has been assembled so far (e.g. from typeDesignations) to here
761
//       $foonote_li_elements = theme('cdm_footnotes', array('footnoteListKey' => RenderHints::getFootnoteListKey(), 'enclosingTag' => 'span'));
762
//        if (!empty($foonote_li_elements)) {
763
//            $derivatives[] =  array(
764
//                '#markup' =>  '<div class="footnotes">' . $foonote_li_elements . '</div>',
765
//            );
766
//        }
767

    
768
        // --- recurse into originals
769
        if (!isset($derivedFrom)  && !$isSpecimen_page) {
770
            $derivedFrom = cdm_ws_get(
771
                CDM_WS_OCCURRENCE,
772
                array($specimen_or_observation->uuid, 'derivedFrom')
773
            );
774
        }
775
        if (isset($derivedFrom) && !$isSpecimen_page) {
776
            if (isset($derivedFrom->originals)) {
777
                $derived_from_label = t('derived');
778
                $preposition = t('from');
779
                if(isset($derivedFrom->type)){
780
                    $derived_from_label = $derivedFrom->type->representation_L10n;
781
                    if($derivedFrom->type->uuid == UUID_DERIVATIONEVENTTYPE_ACCESSIONING){
782
                        $preposition = t('of');
783
                    }
784
                }
785
                if (count($groups) > 0) {
786
                    // TODO  annotations
787

    
788
                    // only display the derived from information when the derivative has any element which will be diplayed
789
                    $derivatives[] = array(
790
                        '#markup' => '<div class="derived_from">' . $derived_from_label . ' ' . $preposition . ': </div>',
791
                    );
792
                }
793
                foreach ($derivedFrom->originals as $original) {
794
                    compose_cdm_specimen_or_observation($original, $isSpecimen_page, $derivatives);
795
                }
796
            }
797
        }
798

    
799

    
800

    
801

    
802
    } // END of $specimenOrObservation exists
803

    
804
    return $derivatives;
805
}
806

    
807

    
808
/**
809
 * Compose an render array from a CDM Sequence object.
810
 *
811
 * compose_hook() implementation
812
 *
813
 * @param object $sequence
814
 *   CDM instance of type Sequence
815
 * @return array
816
 *   A render array containing the fields of the supplied $sequence
817
 *
818
 * @ingroup compose
819
 */
820
function compose_cdm_sequence($sequence, $isSpecimenPage = false)
821
{
822

    
823
    $exclude_sequence_fields = &drupal_static(__FUNCTION__);
824
    if (!isset($exclude_sequence_fields)) {
825
        $exclude_sequence_fields = array(
826
            'titleCache',
827
            'protectedTitleCache',
828
            'microReference',
829
            'created',
830
            'updated',
831
            'class',
832
        );
833
    }
834

    
835
    $groups = array();
836

    
837
    // -- retrieve additional data if neesscary
838
    // TODO below call disabled since sequences are not yet supported,
839
    //      see  #3347 (services and REST service controller for molecular classes implemented)
840
    //
841
    // cdm_load_annotations($sequence);
842

    
843
    foreach (get_object_vars($sequence) as $field => $value) {
844

    
845

    
846
        if (!in_array($field, $exclude_sequence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
847
            switch ($field) {
848

    
849
                case 'geneticAccessionNumber';
850

    
851
                    @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 2);
852
                    break;
853

    
854

    
855
                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.
856
                    if (isset($value->name)) {
857
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->name, NULL, 3);
858
                    }
859
                    if (isset($value->description)) {
860
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field) . ' ' . t('description'), $value->description, NULL, 4);
861
                    }
862
                    @_description_list_group_add($groups, cdm_occurrence_field_name_label($field) , $value, NULL, 4);
863
                    break;
864

    
865
                case 'consensusSequence':
866
                    // format in genbank style, force linebreaks after each 70 nucleotites
867
                    // see also http://stackoverflow.com/questions/499137/css-how-can-i-force-a-long-string-without-any-blank-to-be-wrapped-in-xul-and
868
                    if ($value->length > 0) {
869
                        @_description_list_group_add(
870
                            $groups,
871
                            cdm_occurrence_field_name_label($field),
872
                            array(
873
                                array(
874
                                    '#markup' => '<div class="sequence-length">' . $value->length . ' ' . t('pb') . '</div><div>' . wordwrap($value->string, 70, '</br>', TRUE) . '</div>',
875
                                    '#wrapper_attributes' => array('class' => 'dna-sequence')
876
                                )
877
                            ),
878
                            5);
879
                    }
880
                    break;
881

    
882
                case 'dnaSample': // FIXME 3.3 implement
883
                    break;
884
                case 'singleReads': // FIXME 3.3 implement
885
                    break;
886
                case 'contigFile': // FIXME 3.3 implement - Media
887
                    break;
888
                case 'pherograms': // FIXME 3.3 implement - Media
889
                    break;
890
                case 'haplotype': // FIXME 3.3 implement
891
                    break;
892
                case 'dateSequenced': // FIXME 3.3 now in SingelRead
893
                    @_description_list_group_add($groups, t('Sequencing date'), timePeriodToString($value), NULL, 6);
894
                    break;
895

    
896
                case 'barcode': // boolean
897
                    @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value ? 'Yes' : 'No', NULL, 7);
898
                    break;
899
                case 'barcodeSequencePart': // FIXME 3.3 implement, compose sequence
900
                    break;
901

    
902
                case 'citation':
903
                    @_description_list_group_add($groups,
904
                        cdm_occurrence_field_name_label($field),
905
                        theme('cdm_reference', array('reference' => $value, 'microReference' => $sequence->microReference)),
906
                        NULL,
907
                        8
908
                    );
909
                    break;
910

    
911
                case 'publishedIn':
912
                    @_description_list_group_add($groups,
913
                        cdm_occurrence_field_name_label($field),
914
                        theme('cdm_reference', array('reference' => $value)),
915
                        NULL,
916
                        7
917
                    );
918
                    break;
919

    
920
                case 'rights':
921
                    array_merge($groups, cdm_rights_as_dl_groups($value));
922
                    break;
923

    
924
                case 'annotations':
925
                    $dd_elements = array();
926
                    foreach ($value as $annotation) {
927
                        // TODO respect annotation type filter settings
928
                        $dd_elements[] = $annotation->text;
929
                    }
930
                    @_description_list_group_add($groups, t('Notes'), $dd_elements, NULL, 9);
931
                    break;
932

    
933
                case 'markers':
934
                    $dd_elements = array();
935
                    foreach ($value as $marker) {
936
                        $dd_elements[] = compose_cdm_marker($marker);
937
                    }
938
                    @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements, NULL, 10);
939
                    break;
940

    
941
                case 'chromatograms':
942
                    @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
943
                        array(
944
                            '#markup' => compose_cdm_media_gallerie(array('medialist' => $value)),
945
                        ),
946
                        NULL,
947
                        11);
948
                    break;
949

    
950
                default:
951
                    if (is_object($value) || is_array($value)) {
952
                        drupal_set_message("Unhandled type in compose_cdm_sequence() for field " . $field, "warning");
953
                    } else {
954
                        if (!is_array($value) && strpos($value, 'http:') !== false ){
955
                            //make links for urls
956
                            $value = l($value, $value, array('attributes' => array('target' => '_blank')));
957
                            $value = markup_to_render_array($value);
958
                        }
959

    
960
                       _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 20);
961
                    }
962
            }
963
        }
964
    }
965

    
966
    // template_preprocess_description_list() is not worting by weight so we do it right here
967
    uasort($groups, 'element_sort');
968
    if ($isSpecimenPage) {
969
        $sequence_elements = array(
970
            '#title' => $sequence->dnaMarker,
971
            '#theme' => 'description_list',
972
            '#groups' => $groups
973
        );
974
    } else{
975
        $sequence_elements = array(
976
            '#title' => $sequence->dnaMarker -> titleCache,
977
            '#theme' => 'description_list',
978
            '#groups' => $groups
979
        );
980
    }
981

    
982
    return $sequence_elements;
983
}
984
/**
985
 * Creates an array from a list of FieldUnitDTOs.
986
 *
987
 *
988
 * @param object $specimenOrObservations
989
 *   list of FieldUnitDTOs
990
 * @return array
991
 *   An array containing the hierarchy of the field units corresponding to the taxon
992
 *
993
 * @ingroup compose
994
 */
995
function compose_specimen_array(array $specimenOrObservations){
996
    $items_specimen = array();
997
    $items = array();
998

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

    
1002
        $items['data'] = $specimenOrObservation->listLabel ;
1003
        $specimen = compose_cdm_specimen_or_observation_tree_entry($specimenOrObservation);
1004
        $children = array();
1005
        $child = array();
1006
        $child['data'] =$specimen;
1007
       // $children[] = create_specimen_array($specimenOrObservation->derivates);
1008
       if (isset($specimenOrObservation->derivates) && sizeof($specimenOrObservation->derivates) > 0){
1009
           $child['children']= compose_specimen_array($specimenOrObservation->derivates);
1010
       }
1011
       $children[]=$child;
1012
       $items['children'] = $children;
1013
       $items_specimen[] = $items;
1014
    }
1015
    return $items_specimen;
1016
}
1017

    
1018

    
1019
/**
1020
 * Compose an render array from a CDM DerivedUnitFacade object.
1021
 *
1022
 * compose_hook() implementation
1023
 *
1024
 * @param object $specimenOrObservation
1025
 *   the CDM instance of type SpecimenOrObservation to compose
1026
 *   the render array for
1027
 * @param array $derivatives
1028
 *   the render array which contains the compositions of the derivatives
1029
 *   of the supplied $specimenOrObservation
1030
 *
1031
 * @return array
1032
 *   the supplied render array $derivatives to which the composition of the supplied
1033
 *   $specimenOrObservation has been added to
1034
 *
1035
 * @ingroup compose
1036
 */
1037

    
1038
function compose_cdm_specimen_or_observation_tree_entry($specimen_or_observation)
1039
{
1040
    $exclude_occurrence_fields = &drupal_static(__FUNCTION__);
1041
    if (!isset($exclude_occurrence_fields)) {
1042
        $exclude_occurrence_fields = array(
1043
            'type',
1044
            'taxonRelatedDerivedUnits',
1045
            'label',
1046
            'titleCache',
1047
            'listLabel',
1048
            'protectedTitleCache',
1049
            'class',
1050
            'uuid',
1051
            'derivates',
1052
            'collection'
1053
        );
1054
    }
1055
    $items = array();
1056

    
1057
    // only show uuid it the user is logged in
1058
    if (user_is_logged_in() && ($a_idx = array_search('uuid', $exclude_occurrence_fields)) !== FALSE) {
1059
      //  unset($exclude_occurrence_fields[$a_idx]);
1060
    }
1061

    
1062

    
1063
    if (is_object($specimen_or_observation)) {
1064

    
1065
        $type_label = $specimen_or_observation->recordBase;
1066
        RenderHints::setFootnoteListKey($type_label . '-' . $specimen_or_observation->uuid);
1067

    
1068
        // collect typeStatus as label
1069
        if (isset($specimen_or_observation->specimenTypeDesignations)) {
1070
            $type_status = array();
1071
            foreach ($specimen_or_observation->specimenTypeDesignations as $typeDesignation) {
1072
                if (isset($typeDesignation->typeStatus->representation_L10n)) {
1073
                    $type_status[] = $typeDesignation->typeStatus->representation_L10n;
1074
                }
1075
            }
1076
            if (count($type_status) > 0) {
1077
                $type_label = implode(', ', $type_status);
1078
            }
1079
        }
1080

    
1081
        if (isset($typeDesignation)){
1082
            $title = $type_label . ' for: ' . $typeDesignation->typifiedNames;
1083
        }else{
1084
            $title = $type_label;
1085
        }
1086

    
1087
        $items['data'] = $title;
1088

    
1089
        $groups = array();
1090
        $items['children'] = $groups;
1091
        $children_items = array();
1092
        // --- add initialized fields
1093
        foreach (get_object_vars($specimen_or_observation) as $field => $value) {
1094
            $child_item = array();
1095

    
1096
            if (!in_array($field, $exclude_occurrence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
1097
                switch ($field) {
1098

    
1099
                    /* ---- SpecimenOrObservationBase --- */
1100
                    case 'derivationEvent':
1101
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 1);
1102
                        break;
1103
                    case 'kindOfUnit':
1104
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 1);
1105
                        break;
1106
                    case 'accessionNumber':
1107
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 0);
1108
                        break;
1109

    
1110
                    case 'preferredStableUri':
1111

    
1112
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), array(array('#markup' => cdm_external_uri($value, false))));
1113
                        break;
1114

    
1115
                    case 'specimenTypeDesignations':
1116
                        @_description_list_group_add(
1117
                            $groups,
1118
                            cdm_occurrence_field_name_label($field),
1119
                            array(
1120
                                '#markup' => theme('cdm_typedesignations', array('typeDesignations' => $value)),
1121
                            )
1122
                        );
1123
                        break;
1124

    
1125

    
1126

    
1127
                    case 'listOfMedia':
1128
                        $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
1129

    
1130
                        $captionElements = array(
1131
                            'title',
1132
                            '#uri' => t('open media'),
1133
                        );
1134
                        $gallery_html = compose_cdm_media_gallerie(array(
1135
                            'mediaList' => $value,
1136
                            'galleryName' => $specimen_or_observation->uuid,
1137
                            'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
1138
                            'cols' => $gallery_settings['cdm_dataportal_media_cols'],
1139
                            'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
1140
                            'captionElements' => $captionElements,
1141
                            'mediaLinkType' => 'LIGHTBOX',
1142
                            'alternativeMediaUri' => NULL,
1143
                            'galleryLinkUri' => NULL,
1144
                            'showCaption' => true
1145
                        ));
1146

    
1147
                         //@_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $gallery_html);
1148
                         break;
1149

    
1150

    
1151

    
1152
                    /* ---- DerivedUnitBase --- */
1153

    
1154

    
1155
                    case 'collectionDTO':
1156

    
1157
                        $sub_dl_groups = array();
1158
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('code'), $value->code, NULL, 1);
1159
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('codeStandard'), $value->codeStandard, NULL, 2);
1160
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('institute'), $value->institute, NULL, 3);
1161
                        @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('townOrLocation'), $value->townOrLocation, NULL, 4);
1162
                        // TODO "superCollection"
1163
                        // TODO may have media
1164

    
1165
                        @_description_list_group_add($groups, 'Collection',
1166
                            array(
1167
                                array('#markup' => $value->titleCache),
1168
                                array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
1169
                            )
1170
                        );
1171

    
1172
                        break;
1173

    
1174

    
1175

    
1176

    
1177
                    /* ---- Specimen --- */
1178
                    case 'sequences':
1179
                        $dd_elements = array();
1180
                        foreach ($value as $sequence) {
1181
                            $dd_elements[] = compose_cdm_sequence($sequence, true);
1182
                            $dd_elements[] = "";
1183
                        }
1184
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements,'', 100);
1185
                        break;
1186

    
1187
                    // TODO preservation
1188
                    // TODO exsiccatum
1189

    
1190

    
1191
                    /* ---- FieldObservation --- */
1192
                    case 'gatheringEvent':
1193

    
1194
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('collector'), $value->collector);
1195
                        @_description_list_group_add($groups, t('Gathering date'), timePeriodToString($value->timeperiod));
1196
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('description'), $value->description);
1197
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('locality'), $value->locality, '', 10);
1198
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('country'), $value->country);
1199
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingMethod'), $value->collectingMethod);
1200
                        if (isset($value->absoluteElevation)) {
1201
                            $min_max_markup = min_max_measure($value, 'absoluteElevation');
1202
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('absoluteElevation'), $min_max_markup);
1203
                        }
1204
                        if (isset($value->distanceToGround) && $value->distanceToGround >0) {
1205
                            $min_max_markup = min_max_measure($value, 'distanceToGround');
1206
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToGround'), $min_max_markup);
1207
                        }
1208
                        if (isset($value->distanceToWaterSurface) && $value->distanceToWaterSurface > 0) {
1209
                            $min_max_markup = min_max_measure($value, 'distanceToWaterSurface');
1210
                            @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToWaterSurface'), $min_max_markup);
1211
                        }
1212

    
1213
                        if (isset($value->collectingAreas)) {
1214
                            $area_representations = array();
1215
                            foreach ($value->collectingAreas as $area) {
1216
                               // $area_representations[] = l($area->representation_L10n, path_to_named_area($area->uuid));
1217
                                $area_representations[] = $area;
1218
                            }
1219
                            if (!empty($area_representations))
1220
                                @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingAreas'),
1221
                                    array(
1222
                                        array('#markup' => implode(', ', $area_representations))
1223
                                    )
1224
                                );
1225
                        }
1226
                        if (isset($value->exactLocation)  ) {
1227
                            $sub_dl_groups = array();
1228
                            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('errorRadius'), $value->exactLocation->errorRadius, ' m', 1);
1229
                            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('longitude'), round($value->exactLocation->longitude, 7), '°', 2);
1230
                            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('latitude'), round($value->exactLocation->latitude, 7), '°', 3);
1231
                            if (isset($value->exactLocation->referenceSystem)) {
1232
                                @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('referenceSystem'), $value->exactLocation->referenceSystem->representation_L10n, '', 4);
1233
                            }
1234
                            if ( isSet($value->exactLocation->sexagesimalString)){
1235
                                @_description_list_group_add($groups, cdm_occurrence_field_name_label('exactLocation'),
1236
                                    array(
1237
                                        array('#markup' => $value->exactLocation->sexagesimalString),
1238
                                        array(
1239
                                            '#theme' => 'description_list',
1240
                                            '#groups' => $sub_dl_groups
1241
                                        ),
1242
                                    ), '', 11
1243
                                );
1244
                            }
1245
                        }
1246

    
1247
                        break;
1248

    
1249
                    /* ---- DerivationEvent --- */
1250
                    case 'derivationEvents':
1251
                        //@_description_list_group_add($groups, t('Association type'), $value->description);
1252
                        break;
1253
                    case 'determinedNames':
1254
                        $dd_elements = array();
1255
                        foreach ($value as $name) {
1256
                            $taxon_name = cdm_ws_get(CDM_WS_NAME, $name->key);
1257
                            $taxon_name->taggedName = cdm_ws_get(CDM_WS_NAME, array($name->key, "taggedName"));
1258
                            $name_link = path_to_name($name->key);
1259

    
1260
                            //$taxon_html = render_taxon_or_name($taxon_name, $name_link);
1261
                            $taxon_html = l($taxon_name->titleCache, $name_link, array('attributes' => array('target' => '_blank')));
1262
                            $dd_elements[] = $taxon_html;
1263
                        }
1264
                        @_description_list_group_add($groups, cdm_occurrence_field_name_label("Identification(s)"), $dd_elements,'', 100);
1265
                        break;
1266

    
1267

    
1268
                    default:
1269
                        if (is_object($value) || is_array($value)) {
1270
                            drupal_set_message("Unhandled type in compose_cdm_specimenOrObservation() for field " . $field, "warning");
1271
                        } else {
1272
                            _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
1273
                        }
1274

    
1275
                }
1276

    
1277

    
1278
            }
1279
        } // END of loop over $derivedUnitFacade fields
1280

    
1281

    
1282

    
1283
        // template_preprocess_description_list() is not worting by weight so we do it right here
1284
        uasort($groups, 'element_sort');
1285

    
1286
        $occurrence_elements = array(
1287
           // '#title' => $title,
1288
            '#theme' => 'description_list',
1289
            '#groups' => $groups,
1290
            '#attributes' => array('class' => html_class_attribute_ref($specimen_or_observation)),
1291
        );
1292
        $output = drupal_render($occurrence_elements);
1293
        if (isset($gallery_html)){
1294
            $output .= $gallery_html;
1295
        }
1296
        $pathToSpecimen = path_to_specimen($specimen_or_observation->uuid);
1297
        $output .=  l("Detail page", $pathToSpecimen, array('attributes' => array('target' => '_blank')));
1298
    } // END of $specimenOrObservation exists
1299

    
1300
    return $output;
1301
}
1302

    
1303
function compose_table_of_blast_result(array $data){
1304
   // get icon images
1305
  $expand_icon = font_awesome_icon_markup(
1306
      'fa-plus-square-o',
1307
      array(
1308
          'alt' => 'Show details',
1309
          'class' => array('expand_icon')
1310
      )
1311
  );
1312
  $collapse_icon = font_awesome_icon_markup(
1313
      'fa-minus-square-o',
1314
      array(
1315
          'alt' => 'Show details',
1316
          'class' => array('collapse_icon')
1317
      )
1318
  );
1319
  //$detail_image_icon = '<img title="Detail Image" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/detail_image_derivate-16x16-32.png' . '"/>';
1320
    $rowcount = 0;
1321
    $rows = array();
1322

    
1323
    foreach ($data as $row_data){
1324
        $pathToSpecimen = path_to_specimen_by_accession_number($row_data['id']);
1325
        $specimenPageLink = l($row_data['id'], $pathToSpecimen, array('attributes' => array('target' => '_blank')));
1326
        $ncbiLink = l($row_data['id'], 'https://www.ncbi.nlm.nih.gov/nuccore/'.$row_data['id'].'?report=graph', array('attributes' => array('target' => '_blank')));
1327
        $rows[] =  array(
1328
            'data' => array(
1329
                array(
1330
                    'data' => $expand_icon . $collapse_icon,
1331
                    'class' => array('summary_row_cell', 'summary_row_icon', 'expand_column')
1332
                ),
1333
                array(
1334
                    'data' => $row_data['def'],
1335
                ),
1336
                array(
1337
                    'data' => $specimenPageLink,
1338
                ),
1339
                array(
1340
                    'data' => $row_data['hsp_align_length'],
1341
                ),
1342
                array(
1343
                   'data' => $ncbiLink,
1344
                ),
1345
                array(
1346
                    'data' => $row_data['hsp_identity']/$row_data['hsp_align_length']*100,
1347
                )
1348
            ),
1349
            'id' => 'blast_summary' . $rowcount, // summary row id
1350
            'class' => array('summary_row'),
1351
        );
1352
        $detail_html = "";
1353

    
1354
        $detail_html .= create_label("Details") . $row_data["hsp_align_length"] . "<br>";
1355

    
1356

    
1357
        $detail_html .= "<br>";
1358
        $detail_html .= $row_data["hsp_midline"];
1359

    
1360
        //$detail_html .= "<br>";
1361

    
1362
        $rows[] = array(
1363
            'data' => array(
1364
                array(
1365
                    'data' => "", //empty first column
1366
                    'class' => array('expand_column')
1367
                ),
1368
                array(
1369
                    'data' => $detail_html,
1370
                    'colspan' => 5,
1371
                ),
1372
            ),
1373
            'id' => 'blast_detail' . $rowcount,//details row ID
1374
            'class' => array('detail_row'),
1375
        );
1376
        $rowcount++;
1377
    }
1378

    
1379
    $tableId = "blast_result_table";
1380
    $blast_result_table = array(
1381
        "#theme" => "table",
1382
        "#weight" => 2,
1383
        "#header" => array(
1384
            array(
1385
                'data' => "",
1386
                'class' => array('expand_column')
1387
            ),
1388
                        "Name", "Accession Number", "Align Length", "NCBI", "% Identity"),
1389
        "#rows" => $rows,
1390
        "#attributes" => array(
1391
            "id" => $tableId,
1392
            "border" => 2
1393
        )
1394
    );
1395

    
1396
    drupal_add_js_rowToggle("#".$tableId);
1397

    
1398
    $render_array[$tableId] = $blast_result_table;
1399
    $out = drupal_render($render_array);
1400
   //$blast_result_page-> content = drupal_render($render_array);
1401
   return $out;
1402
}
1403

    
(6-6/10)