Project

General

Profile

Download (14 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
 * @todo Please document this function.
25
 * @see http://drupal.org/node/1354
26
 */
27
function compose_cdm_derivedUnitFacade($derivedUnitFacade) {
28

    
29
  static $excludeFields = array(
30
      'class',
31
      'titleCache',
32
      'derivedUnitMedia',
33
  );
34

    
35
/*
36
  $groups = array();
37
  if (is_object($derivedUnitFacade)) {
38
    foreach (get_object_vars($derivedUnitFacade) as $field => $value) {
39
      if (!in_array($field, $excludeFields) && ($value && (!is_object($value) || isset($value->class)))) {
40
        switch ($field) {
41

    
42
          case 'locality':
43
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->text);
44
            break;
45

    
46
          case 'country':
47
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->representation_L10n);
48
            break;
49

    
50
          case 'collector':
51
            $sub_dl_groups = array();
52
            @_description_list_group_add($sub_dl_groups, 'code', $value->code, 1);
53
            @_description_list_group_add($sub_dl_groups, 'codeStandard',  $value->codeStandard, 2);
54
            @_description_list_group_add($sub_dl_groups, 'institute', $value->institute, 3);
55
            @_description_list_group_add($sub_dl_groups, 'townOrLocation', $value->townOrLocation, 4);
56

    
57
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
58
                array(
59
                    array('#markup' => $value->titleCache),
60
                    array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
61
                )
62
            );
63
            break;
64

    
65
          case 'collection':
66
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->titleCache);
67
            break;
68

    
69
          case 'exactLocation':
70
            $sub_dl_groups = array();
71
            @_description_list_group_add($sub_dl_groups, 'errorRadius', $value->errorRadius, 1);
72
//             @_description_list_group_add($sub_dl_groups, 'longitude', $value->longitude, 2);
73
//             @_description_list_group_add($sub_dl_groups, 'latitude', $value->latitude, 3);
74

    
75
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
76
                array(
77
                    array('#markup' => $value->sexagesimalString),
78
                    array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
79
                )
80
            );
81
            break;
82

    
83
          case 'getGatheringPeriod':
84
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), timePeriodToString($value));
85
            break;
86

    
87
          default:
88
            _description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
89
        }
90

    
91
      }
92
    } // END of loop over $derivedUnitFacade fields
93

    
94
    $render_array = array(
95
        '#theme' => 'description_list',
96
        '#groups' => $groups,
97
    );
98
  }
99
*/
100
  return $render_array;
101
}
102

    
103
/**
104
 * Compose an render array from a CDM DerivedUnitFacade object.
105
 *
106
 *
107
 *
108
 * compose_hook() implementation
109
 *
110
 * @todo Please document this function.
111
 * @see http://drupal.org/node/1354
112
 */
113
function compose_cdm_specimenOrObservation($specimenOrObservation, &$derivatives = null) {
114

    
115
  static $excludeFields = array(
116
      'derivationEvents',
117
      'titleCache',
118
      'derivedUnitMedia',
119
      'created',
120
      'updated',
121
      'class'
122
//       'uuid'
123
  );
124

    
125
  if (!isset($derivatives)) {
126
    $derivatives = array();
127
  }
128

    
129
  $descriptions = null;
130
  $derivedFrom = null;
131

    
132
  if (is_object($specimenOrObservation)) {
133

    
134
    // request again for deeper initialization
135
    $specimenOrObservation = cdm_ws_get("portal/" . CDM_WS_OCCURRENCE, $specimenOrObservation->uuid);
136

    
137
    $title = $specimenOrObservation->class . ': ' . $specimenOrObservation->titleCache;
138

    
139
    $groups = array();
140
    // --- add initialized fields
141
    foreach (get_object_vars($specimenOrObservation) as $field => $value) {
142
      if (!in_array($field, $excludeFields) && ($value && (!is_object($value) || isset($value->class)))) {
143
        switch ($field) {
144

    
145
          /* ---- java.lang.Object --- */
146
          case 'class':
147
            if($value != '' /* FieldObservation' */){
148
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value);
149
            }
150
          break;
151

    
152
          /* ---- SpecimenOrObservationBase --- */
153
          case 'sex':
154
          case 'lifeStage':
155
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $value->representation_L10n);
156
            break;
157

    
158
          case 'definition':
159
            // TODO
160
            break;
161

    
162
          case 'determinations':
163
            $dd_array = array();
164
            $glue = ', ';
165
            foreach  ($value as $determinationEvent){
166
              $timeperiod_string = NULL;
167
              if (isset($determinationEvent->timeperiod)) {
168
                $timeperiod_string = timePeriodToString($determinationEvent->timeperiod);
169
              }
170
              $weight = isset($determinationEvent->preferred) && $determinationEvent->preferred == 1 ? '0' : ($timeperiod_string ? $timeperiod_string : '1');
171
              // check key exists
172
              while (array_key_exists($weight, $dd_array)) {
173
                $weight .= '0';
174
              }
175
              $taxon_name = cdm_ws_get(CDM_WS_TAXON . '/$0/name', $determinationEvent->taxon->uuid);
176
              $taxon_html = theme('cdm_taxonName',
177
                  array(
178
                      'taxonName' => $taxon_name,
179
                      'nameLink' => path_to_taxon($determinationEvent->taxon->uuid),
180
                  )
181
              );
182
              $dd_array[$weight] = $taxon_html;
183
              if ($timeperiod_string) {
184
                $dd_array[$weight] .= $glue . $timeperiod_string;
185
              }
186
              if (isset($determinationEvent->actor->titleCache)) {
187
                $dd_array[$weight] .= $glue . $determinationEvent->actor->titleCache;
188
              }
189
              if (isset($determinationEvent->description)) {
190
                $dd_array[$weight] .= $glue . $determinationEvent->description;
191
              }
192
            }
193
            ksort($dd_array);
194
            @_description_list_group_add($groups, cdm_occurrence_field_name_label('determinations'), $dd_array);
195
            break;
196

    
197
          case 'descriptions':
198
            // TODO compose descriptions
199
            // store in variable so we don't need to ask the cdmserver for them again
200
            $descriptions = $value;
201
            $occurrence_featureTree = cdm_get_occurrence_featureTree();
202
            $dd_elements = array();
203

    
204
            foreach ($value as $description) {
205
              $dd_elements[] = array('#markup' => $description->titleCache . ' ' . $description->uuid);
206
              $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, $description->uuid);
207
              $elements_by_feature = _mergeFeatureTreeDescriptions($occurrence_featureTree->root->children, $description->elements);
208
              $rendered_description = theme(
209
                 'cdm_feature_nodes',
210
                 array('mergedFeatureNodes' => $elements_by_feature)
211
              );
212
              $dd_elements[] = array('#markup' => $rendered_description);
213
            }
214

    
215
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_elements);
216
            break;
217

    
218
          case 'sources':
219
              $dd_array = array();
220
              foreach ($value as $identifiable_source) {
221
                $dd_array[] = theme('cdm_OriginalSource', array('source' => $identifiable_source));
222
              }
223
              @_description_list_group_add($groups, cdm_occurrence_field_name_label($field), $dd_array);
224
              break;
225

    
226

    
227
          /* ---- DerivedUnitBase --- */
228
          case 'derivedFrom':
229
            $derivedFrom = $value;
230
            break;
231

    
232
          case 'collection':
233
            $sub_dl_groups = array();
234
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('code'), $value->code, 1);
235
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('codeStandard'),  $value->codeStandard, NULL, 2);
236
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('institute'), $value->institute, NULL, 3);
237
            @_description_list_group_add($sub_dl_groups, cdm_occurrence_field_name_label('townOrLocation'), $value->townOrLocation, NULL, 4);
238
            // TODO "superCollection"
239
            // TODO may have media
240

    
241
            @_description_list_group_add($groups, cdm_occurrence_field_name_label($field),
242
                array(
243
                    array('#markup' => $value->titleCache),
244
                    array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
245
                )
246
            );
247
            break;
248

    
249
            case 'storedUnder':
250
              @_description_list_group_add($groups, cdm_occurrence_field_name_label('storedUnder'), theme('cdm_taxonName', $value));
251
              break;
252

    
253

    
254
          /* ---- Specimen --- */
255
          // TODO preservation
256
          // TODO exsiccatum
257

    
258

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

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

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

    
306
        }
307

    
308
      }
309
    } // END of loop over $derivedUnitFacade fields
310

    
311

    
312
    $occurrence_elements = array(
313
        '#title' => $title,
314
        '#theme' => 'description_list',
315
        '#groups' => $groups,
316
        '#attributes' => array('class' => html_class_atttibute_ref($specimenOrObservation)),
317
    );
318

    
319
    $derivatives[] = $occurrence_elements;
320

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

    
329
    if (isset($derivedFrom)) {
330
      if (isset($derivedFrom->originals)) {
331
        $derived_from_label = t('derived');
332
        if(isset($derivedFrom->type)){
333
          $derived_from_label = $derivedFrom->representation_L10n;
334
        }
335
        $derivatives[] = array(
336
            '#markup' => '<div>' . $derived_from_label . ' ' . t('from') . ': </div>',
337
        );
338
        foreach ($derivedFrom->originals as $original) {
339
          compose_cdm_specimenOrObservation($original, $derivatives);
340
        }
341
      }
342
    }
343

    
344
  } // END of $specimenOrObservation exists
345

    
346
  return $derivatives;
347
}
(1-1/3)