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
        'publish',
49
        'updated',
50
        'class',
51
        'uuid',
52
       ''
53
    );
54
  }
55

    
56

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

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

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

    
69
  if (is_object($specimenOrObservation)) {
70

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

    
74

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

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

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

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

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

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

    
114

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

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

    
131
          case 'definition':
132
            // TODO
133
            break;
134

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

    
145
          case 'determinations':
146
            $dd_elements = array();
147
            $glue = ', ';
148

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

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

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

    
205
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
206
            break;
207

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

    
216

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

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

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

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

    
243

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

    
253
            // TODO preservation
254
            // TODO exsiccatum
255

    
256

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

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

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

    
304
        }
305

    
306
      }
307
    } // END of loop over $derivedUnitFacade fields
308

    
309

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

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

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

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

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

    
360
  } // END of $specimenOrObservation exists
361

    
362
  return $derivatives;
363
}
364

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

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

    
391
  $groups = array();
392

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

    
399
  foreach (get_object_vars($sequence) as $field => $value) {
400

    
401

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

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

    
417

    
418
        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.
419
          if (isset($value->name)) {
420
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),  $value->name, NULL, 3);
421
          }
422
          if (isset($value->description)) {
423
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field) . ' ' . t('description') , $value->description, NULL, 4);
424
          }
425
          break;
426

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

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

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

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

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

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

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

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

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

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

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

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

    
527
  return $sequence_elements;
528
}
529

    
530

    
(5-5/7)