Project

General

Profile

Download (20.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Functions for dealing with CDM entities of type SpeciemenOrOccurrences
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
 * Compose an render array from a CDM DerivedUnitFacade object.
21
 *
22
 * compose_hook() implementation
23
 *
24
 * @param object $specimenOrObservation
25
 *   the CDM instance of type SpecimenOrObservation to compose
26
 *   the render array for
27
 * @param array $derivatives
28
 *   the render array which contains the compositions of the derivatives
29
 *   of the supplied $specimenOrObservation
30
 *
31
 * @return array
32
 *   the supplied render array $derivatives to which the composition of the supplied
33
 *   $specimenOrObservation has been added to
34
 *
35
 * @ingroup compose
36
 */
37
function compose_cdm_specimenOrObservation($specimenOrObservation, &$derivatives = null) {
38

    
39
  $exclude_occurrence_fields = &drupal_static(__FUNCTION__);
40
  if (!isset($exclude_occurrence_fields)) {
41
     $exclude_occurrence_fields = array(
42
        'derivationEvents',
43
        'extensions', // TODO ignored for now, how to handle extensions?
44
        'titleCache',
45
        'protectedTitleCache',
46
        'derivedUnitMedia',
47
        'created',
48
        'updated',
49
        'class',
50
        'uuid',
51
       ''
52
    );
53
  }
54

    
55

    
56
  // only show uuid it the user is logged in
57
  if(user_is_logged_in() && ($a_idx = array_search('uuid', $exclude_occurrence_fields)) !== FALSE ) {
58
    unset($exclude_occurrence_fields[$a_idx]);
59
  }
60

    
61
  if (!isset($derivatives)) {
62
    $derivatives = array();
63
  }
64

    
65
  $descriptions = null;
66
  $derivedFrom = null;
67

    
68
  if (is_object($specimenOrObservation)) {
69

    
70
    // request again for deeper initialization
71
    $specimenOrObservation = cdm_ws_get("portal/" . CDM_WS_OCCURRENCE, $specimenOrObservation->uuid);
72

    
73

    
74
    $type_label = $specimenOrObservation->class;
75
    RenderHints::setFootnoteListKey($type_label . '-' . $specimenOrObservation->uuid);
76

    
77
    // collect typeStatus as label
78
    if (isset($specimenOrObservation->specimenTypeDesignations)) {
79
      $type_status = array();
80
      foreach ($specimenOrObservation->specimenTypeDesignations as $typeDesignation) {
81
        if (isset($typeDesignation->typeStatus->representation_L10n)){
82
          $type_status[] = $typeDesignation->typeStatus->representation_L10n;
83
        }
84
      }
85
      if (count($type_status) > 0){
86
        $type_label = implode(', ', $type_status);
87
      }
88
    }
89

    
90
    $title = $type_label . ': '. $specimenOrObservation->titleCache;
91

    
92
    $groups = array();
93
    // --- add initialized fields
94
    foreach (get_object_vars($specimenOrObservation) as $field => $value) {
95
      if (!in_array($field, $exclude_occurrence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
96
        switch ($field) {
97

    
98
          /* ---- java.lang.Object --- */
99
          case 'class':
100
            if($value != '' /* FieldObservation' */){
101
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
102
            }
103
          break;
104

    
105
          case 'markers':
106
            $dd_elements = array();
107
            foreach ($value as $marker) {
108
              $dd_elements[] = compose_cdm_marker($marker);
109
            }
110
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
111
            break;
112

    
113

    
114
          case 'annotations':
115
            $dd_elements = array();
116
            foreach ($value as $annotation) {
117
              // TODO respect annotation type filter settings
118
              $dd_elements[] = $annotation->text;
119
            }
120
            @_description_list_group_add($groups, t('Notes'), $dd_elements);
121
            break;
122

    
123
          /* ---- SpecimenOrObservationBase --- */
124
          case 'sex':
125
          case 'lifeStage':
126
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->representation_L10n);
127
            break;
128

    
129
          case 'definition':
130
            // TODO
131
            break;
132

    
133
          case 'specimenTypeDesignations':
134
            @_description_list_group_add(
135
              $groups,
136
              cdm_occurrence_field_name_label($field),
137
              array(
138
                '#markup'=>theme('cdm_typedesignations', array('typeDesignations' => $value)),
139
              )
140
            );
141
            break;
142

    
143
          case 'determinations':
144
            $dd_elements = array();
145
            $glue = ', ';
146

    
147
            foreach  ($value as $determinationEvent){
148
              $timeperiod_string = NULL;
149
              if (isset($determinationEvent->timeperiod)) {
150
                $timeperiod_string = timePeriodToString($determinationEvent->timeperiod);
151
              }
152
              $weight = isset($determinationEvent->preferred) && $determinationEvent->preferred == 1 ? '0' : ($timeperiod_string ? $timeperiod_string : '1');
153
              // check key exists
154
              while (array_key_exists($weight, $dd_elements)) {
155
                $weight .= '0';
156
              }
157
              $taxon_name = cdm_ws_get(CDM_WS_TAXON . '/$0/name', $determinationEvent->taxon->uuid);
158
              $taxon_html = theme('cdm_taxonName',
159
                  array(
160
                      'taxonName' => $taxon_name,
161
                      'nameLink' => path_to_taxon($determinationEvent->taxon->uuid),
162
                  )
163
              );
164
              $dd_elements[$weight] = $taxon_html;
165
              if (isset($determinationEvent->modifier)) {
166
                $dd_elements[$weight] .= cdm_term_representation($determinationEvent->modifier);
167
              }
168
              if ($timeperiod_string) {
169
                $dd_elements[$weight] .= $glue . $timeperiod_string;
170
              }
171
              if (isset($determinationEvent->actor->titleCache)) {
172
                $dd_elements[$weight] .= $glue . $determinationEvent->actor->titleCache;
173
              }
174
              if (isset($determinationEvent->description)) {
175
                $dd_elements[$weight] .= $glue . $determinationEvent->description;
176
              }
177
            }
178
            ksort($dd_elements);
179
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('determinations'), $dd_elements);
180
            break;
181

    
182
          case 'descriptions':
183
            $descriptions = $value;
184
            $occurrence_featureTree = cdm_get_occurrence_featureTree();
185
            $dd_elements = array();
186

    
187
            foreach ($value as $description) {
188
              $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, $description->uuid);
189
//               if($description->imageGallery == TRUE) {
190
//                 continue;
191
//               }
192
              $elements_by_feature = _mergeFeatureTreeDescriptions($occurrence_featureTree->root->childNodes, $description->elements);
193
              $rendered_description = theme(
194
                 'cdm_feature_nodes',
195
                 array('mergedFeatureNodes' => $elements_by_feature)
196
              );
197
              $description_render_elements = array();
198
//               $description_render_elements[] = array('#markup' => '<h4>'. $description->titleCache . '</h4>');
199
              $description_render_elements[] = array('#markup' => $rendered_description);
200
              $dd_elements[] = $description_render_elements;
201
            }
202

    
203
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
204
            break;
205

    
206
          case 'sources':
207
              $dd_elements = array();
208
              foreach ($value as $identifiable_source) {
209
                $dd_elements[] = theme('cdm_OriginalSource', array('source' => $identifiable_source));
210
              }
211
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
212
              break;
213

    
214

    
215
          /* ---- DerivedUnitBase --- */
216
          case 'derivedFrom':
217
            $derivedFrom = $value;
218
            break;
219

    
220
          case 'collection':
221
            $sub_dl_groups = array();
222
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('code'), $value->code, NULL, 1);
223
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('codeStandard'),  $value->codeStandard, NULL, 2);
224
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('institute'), $value->institute, NULL, 3);
225
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('townOrLocation'), $value->townOrLocation, NULL, 4);
226
            // TODO "superCollection"
227
            // TODO may have media
228

    
229
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
230
                array(
231
                    array('#markup' => $value->titleCache),
232
                    array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
233
                )
234
            );
235
            break;
236

    
237
            case 'storedUnder':
238
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('storedUnder'), theme('cdm_taxonName', $value));
239
              break;
240

    
241

    
242
            /* ---- Specimen --- */
243
            case 'sequences':
244
              $dd_elements = array();
245
              foreach ($value as $sequence) {
246
                $dd_elements[] = compose_cdm_sequence($sequence);
247
              }
248
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
249
            break;
250

    
251
            // TODO preservation
252
            // TODO exsiccatum
253

    
254

    
255
          /* ---- FieldObservation --- */
256
          case 'gatheringEvent':
257
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('collector'), $value->actor->titleCache);
258
            @_description_list_group_add($groups, t('Gathering time'), timePeriodToString($value->timeperiod));
259
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('description'), $value->description);
260
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('locality'), $value->locality->text);
261
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('country'), $value->country->representation_L10n);
262
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingMethod'), $value->collectingMethod);
263
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('absoluteElevation'), $value->absoluteElevation, ' m');
264
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('absoluteElevationError'), $value->absoluteElevationError, ' m');
265
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToGround'), $value->distanceToGround, ' m');
266
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('distanceToWaterSurface'), $value->distanceToWaterSurface, ' m');
267
            if (isset($value->collectingAreas)) {
268
              $area_representations = array();
269
              foreach($value->collectingAreas as $area) {
270
                $area_representations[] = $area->representation_L10n;
271
              }
272
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('collectingAreas'), implode(', ', $area_representations));
273
            }
274
            if (isset($value->exactLocation)) {
275
              $sub_dl_groups = array();
276
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('errorRadius'), $value->exactLocation->errorRadius, ' m', 1);
277
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('longitude'), round($value->exactLocation->longitude, 7), '°', 2);
278
              @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('latitude'), round($value->exactLocation->latitude, 7), '°', 3);
279
              if (isset($value->exactLocation->referenceSystem)) {
280
                @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('referenceSystem'), $value->exactLocation->referenceSystem->representation_L10n, '', 4);
281
              }
282

    
283
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('exactLocation'),
284
                  array(
285
                      array('#markup' => $value->exactLocation->sexagesimalString),
286
                      array(
287
                          '#theme' => 'description_list',
288
                          '#groups' => $sub_dl_groups
289
                      ),
290
                  )
291
              );
292
            }
293
            break;
294

    
295
          default:
296
            if(is_object($value) || is_array($value)){
297
              drupal_set_message("Unhandled type in compose_cdm_specimenOrObservation() for field " . $field, "warning");
298
            } else {
299
              _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
300
            }
301

    
302
        }
303

    
304
      }
305
    } // END of loop over $derivedUnitFacade fields
306

    
307

    
308
    // template_preprocess_description_list() is not worting by weight so we do it right here
309
    uasort($groups, 'element_sort');
310

    
311
    $occurrence_elements = array(
312
        '#title' => $title,
313
        '#theme' => 'description_list',
314
        '#groups' => $groups,
315
        '#attributes' => array('class' => html_class_atttibute_ref($specimenOrObservation)),
316
    );
317
    $derivatives[] = $occurrence_elements;
318
    // all footnotes which has been assembled so far (e.g. from typeDesignations) to here
319
    $foonote_li_elements = theme('cdm_footnotes', array('footnoteListKey' => RenderHints::getFootnoteListKey(), 'enclosingTag' => 'span'));
320
    if (!empty($foonote_li_elements)) {
321
      $derivatives[] =  array(
322
          '#markup' =>  '<div class="footnotes">' . $foonote_li_elements . '</div>',
323
      );
324
    }
325

    
326
    // --- recurse into originals
327
    if (!isset($derivedFrom)) {
328
      $derivedFrom = cdm_ws_get(
329
          CDM_WS_OCCURRENCE,
330
          array($specimenOrObservation->uuid, 'derivedFrom')
331
        );
332
    }
333

    
334
    if (isset($derivedFrom)) {
335
      if (isset($derivedFrom->originals)) {
336
        $derived_from_label = t('derived');
337
        $preposition = t('from');
338
        if(isset($derivedFrom->type)){
339
          $derived_from_label = $derivedFrom->type->representation_L10n;
340
          if($derivedFrom->type->uuid == UUID_DERIVATIONEVENTTYPE_ACCESSIONING){
341
            $preposition = t('of');
342
          }
343
        }
344
        if (count($groups) > 0) {
345
          // TODO  annotations
346

    
347
          // only display the derived from information when the derivative has any element which will be diplayed
348
          $derivatives[] = array(
349
              '#markup' => '<div class="derived_from">' . $derived_from_label . ' ' . $preposition . ': </div>',
350
          );
351
        }
352
        foreach ($derivedFrom->originals as $original) {
353
          compose_cdm_specimenOrObservation($original, $derivatives);
354
        }
355
      }
356
    }
357

    
358
  } // END of $specimenOrObservation exists
359

    
360
  return $derivatives;
361
}
362

    
363
/**
364
 * Compose an render array from a CDM Sequence object.
365
 *
366
 * compose_hook() implementation
367
 *
368
 * @param object $sequence
369
 *   CDM instance of type Sequence
370
 * @return array
371
 *   A render array containing the fields of the supplied $sequence
372
 *
373
 * @ingroup compose
374
 */
375
function compose_cdm_sequence($sequence) {
376

    
377
  $exclude_sequence_fields = &drupal_static(__FUNCTION__);
378
  if (!isset($exclude_sequence_fields)) {
379
    $exclude_sequence_fields = array(
380
      'titleCache',
381
      'protectedTitleCache',
382
      'microReference',
383
      'created',
384
      'updated',
385
      'class',
386
    );
387
  }
388

    
389
  $groups = array();
390

    
391
  // -- retrieve additional data if neesscary
392
  // TODO below call disabled since sequences are not yet supported,
393
  //      see  #3347 (services and REST service controller for molecular classes implemented)
394
  //
395
  // cdm_load_annotations($sequence);
396

    
397
  foreach (get_object_vars($sequence) as $field => $value) {
398

    
399

    
400
    if (!in_array($field, $exclude_sequence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
401
      switch ($field) {
402

    
403
        case 'geneticAccessionNumber';
404
          $dd_elements = array();
405
          foreach ($value as $accession) {
406
            if (isset($accession->uri) ){
407
              $dd_elements[] = l($accession->accessionNumber, $accession->uri);
408
            } else {
409
              $dd_elements[] = $accession->accessionNumber;
410
            }
411
          }
412
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),  $dd_elements, NULL, 1);
413
          break;
414

    
415

    
416
        case 'locus': // FIXME 3.3 now dnaMarker (DefinedTerm)  if multiple amplifications where used to build this consensus sequence it may be the super set of the markers used in amplification.
417
          if (isset($value->name)) {
418
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),  $value->name, NULL, 3);
419
          }
420
          if (isset($value->description)) {
421
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field) . ' ' . t('description') , $value->description, NULL, 4);
422
          }
423
          break;
424

    
425
        case 'consensusSequence':
426
          // format in genbank style, force linebreaks after each 70 nucleotites
427
          // see also http://stackoverflow.com/questions/499137/css-how-can-i-force-a-long-string-without-any-blank-to-be-wrapped-in-xul-and
428
          @_description_list_group_add(
429
            $groups,
430
            cdm_occurrence_field_name_label($field),
431
            array(
432
              array(
433
                '#markup' => '<div class="sequence-length">' . $value->length . ' ' . t('pb'). '</div><div>' . wordwrap($value->string, 70, '</br>', TRUE) . '</div>',
434
                '#wrapper_attributes' => array('class'=>'dna-sequence')
435
                )
436
              ),
437
            5);
438
          break;
439

    
440
         case 'dnaSample': // FIXME 3.3 implement
441
            break;
442
        case 'singleReads': // FIXME 3.3 implement
443
          break;
444
        case 'contigFile': // FIXME 3.3 implement - Media
445
            break;
446
        case 'pherograms': // FIXME 3.3 implement - Media
447
          break;
448
        case 'haplotype': // FIXME 3.3 implement
449
            break;
450
        case 'dateSequenced': // FIXME 3.3 now in SingelRead
451
          @_description_list_group_add($groups, t('Sequencing date'),  timePeriodToString($value), NULL, 6);
452
          break;
453

    
454
        case 'barcode': // boolean
455
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value ? 'Yes': 'No', NULL, 7);
456
          break;
457
        case 'barcodeSequencePart': // FIXME 3.3 implement, compose sequence
458
            break;
459

    
460
        case 'citation':
461
          @_description_list_group_add($groups,
462
            cdm_occurrence_field_name_label($field),
463
            theme('cdm_reference', array('reference' =>$value, 'microReference' => $sequence->microReference)),
464
            NULL,
465
            8
466
          );
467
          break;
468

    
469
        case 'publishedIn':
470
          @_description_list_group_add($groups,
471
            cdm_occurrence_field_name_label($field),
472
            theme('cdm_reference', array('reference'=>$value)),
473
            NULL,
474
            7
475
          );
476

    
477
        case 'rights':
478
          array_merge($groups, cdm_rights_as_dl_groups($value));
479
        break;
480

    
481
        case 'annotations':
482
          $dd_elements = array();
483
          foreach ($value as $annotation) {
484
            // TODO respect annotation type filter settings
485
            $dd_elements[] = $annotation->text;
486
          }
487
          @_description_list_group_add($groups, t('Notes'), $dd_elements, NULL, 9);
488
          break;
489

    
490
        case 'markers':
491
          $dd_elements = array();
492
          foreach ($value as $marker) {
493
            $dd_elements[] = compose_cdm_marker($marker);
494
          }
495
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements, NULL, 10);
496
          break;
497

    
498
        case 'chromatograms':
499
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
500
              array(
501
                  '#markup'=>theme('cdm_media_gallerie', array('medialist'=>$value)),
502
              ),
503
              NULL,
504
              11);
505
          break;
506

    
507
        default:
508
          if(is_object($value) || is_array($value)){
509
            drupal_set_message("Unhandled type in compose_cdm_sequence() for field " . $field, "warning");
510
          } else {
511
            _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value, NULL, 20);
512
          }
513
      }
514
    }
515
  }
516

    
517
  // template_preprocess_description_list() is not worting by weight so we do it right here
518
  uasort($groups, 'element_sort');
519

    
520
  $sequence_elements = array(
521
      '#theme' => 'description_list',
522
      '#groups' => $groups
523
  );
524

    
525
  return $sequence_elements;
526
}
527

    
528

    
(5-5/7)