Project

General

Profile

Download (20.2 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
              $description_render_elements = compose_cdm_feature_nodes($elements_by_feature, null);
196
              $dd_elements[] = $description_render_elements;
197
            }
198

    
199
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
200
            break;
201

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

    
210

    
211
          /* ---- DerivedUnitBase --- */
212
          case 'derivedFrom':
213
            $derivedFrom = $value;
214
            break;
215

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

    
225
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
226
                array(
227
                    array('#markup' => $value->titleCache),
228
                    array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
229
                )
230
            );
231
            break;
232

    
233
            case 'storedUnder':
234
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('storedUnder'), theme('cdm_taxonName', $value));
235
              break;
236

    
237

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

    
247
            // TODO preservation
248
            // TODO exsiccatum
249

    
250

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

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

    
291
          default:
292
            if(is_object($value) || is_array($value)){
293
              drupal_set_message("Unhandled type in compose_cdm_specimenOrObservation() for field " . $field, "warning");
294
            } else {
295
              _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
296
            }
297

    
298
        }
299

    
300
      }
301
    } // END of loop over $derivedUnitFacade fields
302

    
303

    
304
    // template_preprocess_description_list() is not worting by weight so we do it right here
305
    uasort($groups, 'element_sort');
306

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

    
322
    // --- recurse into originals
323
    if (!isset($derivedFrom)) {
324
      $derivedFrom = cdm_ws_get(
325
          CDM_WS_OCCURRENCE,
326
          array($specimenOrObservation->uuid, 'derivedFrom')
327
        );
328
    }
329

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

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

    
354
  } // END of $specimenOrObservation exists
355

    
356
  return $derivatives;
357
}
358

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

    
373
  $exclude_sequence_fields = &drupal_static(__FUNCTION__);
374
  if (!isset($exclude_sequence_fields)) {
375
    $exclude_sequence_fields = array(
376
      'titleCache',
377
      'protectedTitleCache',
378
      'microReference',
379
      'created',
380
      'updated',
381
      'class',
382
    );
383
  }
384

    
385
  $groups = array();
386

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

    
393
  foreach (get_object_vars($sequence) as $field => $value) {
394

    
395

    
396
    if (!in_array($field, $exclude_sequence_fields) && ($value && (!is_object($value) || isset($value->class)))) {
397
      switch ($field) {
398

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

    
411

    
412
        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.
413
          if (isset($value->name)) {
414
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),  $value->name, NULL, 3);
415
          }
416
          if (isset($value->description)) {
417
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field) . ' ' . t('description') , $value->description, NULL, 4);
418
          }
419
          break;
420

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

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

    
450
        case 'barcode': // boolean
451
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value ? 'Yes': 'No', NULL, 7);
452
          break;
453
        case 'barcodeSequencePart': // FIXME 3.3 implement, compose sequence
454
            break;
455

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

    
465
        case 'publishedIn':
466
          @_description_list_group_add($groups,
467
            cdm_occurrence_field_name_label($field),
468
            theme('cdm_reference', array('reference'=>$value)),
469
            NULL,
470
            7
471
          );
472

    
473
        case 'rights':
474
          array_merge($groups, cdm_rights_as_dl_groups($value));
475
        break;
476

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

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

    
494
        case 'chromatograms':
495
          @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
496
              array(
497
                  '#markup'=>theme('cdm_media_gallerie', array('medialist'=>$value)),
498
              ),
499
              NULL,
500
              11);
501
          break;
502

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

    
513
  // template_preprocess_description_list() is not worting by weight so we do it right here
514
  uasort($groups, 'element_sort');
515

    
516
  $sequence_elements = array(
517
      '#theme' => 'description_list',
518
      '#groups' => $groups
519
  );
520

    
521
  return $sequence_elements;
522
}
523

    
524

    
(5-5/7)