Project

General

Profile

Download (13.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Page functions.
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
 * Creates a render array representing the ocurrences associetad with the $taxon.
22
 *
23
 * The resulting render array contains two elements:
24
 *  - 'map': A map showing all point locations of the occurences is availabale
25
 *  - 'specimen_list': the list of occurences prepated as table for theme_table()
26
 *
27
 * @param object $taxon
28
 *   A cdm Taxon object
29
 * @return
30
 *   A render array suitable for drupal_render()
31
 *
32
 */
33
function cdm_dataportal_taxon_page_specimens($taxon) {
34

    
35
  $render_array = array();
36
  RenderHints::pushToRenderStack('taxon_page_specimens');
37

    
38
  $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
39
  $relationship_choice['direct'] = get_selection($relationship_choice['direct']);
40
  $relationship_choice['invers'] = get_selection($relationship_choice['invers']);
41

    
42
  $by_associatedtaxon_query_parameters = array(
43
      'relationshipsInvers' => implode(',', $relationship_choice['invers']),
44
      'relationships' => implode(',', $relationship_choice['direct']),
45
  );
46

    
47
  if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
48
    $by_associatedtaxon_query_parameters = array_merge($by_associatedtaxon_query_parameters, $_REQUEST['pager']);
49
  }
50

    
51
  $by_associatedtaxon_query = http_build_query($by_associatedtaxon_query_parameters);
52

    
53
  $pager = cdm_ws_get(CDM_WS_OCCURRENCE_BY_ASSOCIATEDTAXON,
54
      null,
55
      $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
56
  );
57

    
58
  $specimensOrObservations = array();
59
  if(isset($pager->records[0])){
60
    $specimensOrObservations =  $pager->records;
61
  }
62

    
63
  // order occurrences by date but types should be on top of the list
64
  $type_specimens = array();
65
  $other_occurrences = array();
66
  foreach ($specimensOrObservations as &$occurrence) {
67
    $typeDesignationsPager = cdm_ws_get(CDM_WS_OCCURRENCE . '/$0/specimenTypeDesignations', $occurrence->uuid);
68
    if (isset($typeDesignationsPager->count) && $typeDesignationsPager->count > 0) {
69
      $type_specimens[] = $occurrence;
70
    } else {
71
      $other_occurrences[] = $occurrence;
72
    }
73
  }
74
  $specimensOrObservations = array_merge($type_specimens, $other_occurrences);
75

    
76
  // Collect media (fieldObjectMedia, derivedUnitMedia) and add as a custom field
77
  // $occurrence->_fieldObjectMedia
78
  foreach ($specimensOrObservations as &$occurrence) {
79
    $occurrence->_fieldObjectMedia = cdm_ws_get(CDM_WS_DERIVEDUNIT_FACADE, array(
80
        $occurrence->uuid,
81
        'fieldObjectMedia',
82
    ));
83
    $occurrence->_derivedUnitMedia = cdm_ws_get(CDM_WS_DERIVEDUNIT_FACADE, array(
84
        $occurrence->uuid,
85
        'derivedUnitMedia',
86
    ));
87
  }
88

    
89
  // --- get map service HTTP query paramaters
90
  if (count($specimensOrObservations) > 0) {
91
    $occurrence_query = cdm_ws_get(CDM_WS_GEOSERVICE_OCCURRENCEMAP, $taxon->uuid,  $by_associatedtaxon_query);
92

    
93
    if( isset($occurrence_query->String) ) {
94
      $occurrence_query = $occurrence_query->String;
95
      $legend_format_query = null;
96
      $distribution_query = NULL;
97

    
98
      $render_array['map'] = compose_map($occurrence_query, $distribution_query, $legend_format_query);
99
    }
100
  }
101

    
102
  // --- generate the specimen list as table
103
  $specimen_table = array(
104
      '#theme' => 'table',
105
      '#weight' => 2,
106
      // prefix attributes and rows with '#' to let it pass to the theme function,
107
      // otherwise it is handled as child render array
108
      '#attributes' => array('class' => 'specimens'),
109
      '#rows' => array(),
110
  );
111

    
112
  if ($specimensOrObservations) {
113

    
114
    foreach ($specimensOrObservations as $specimenOrObservation) {
115

    
116
      $mediaList = array();
117
      if (is_array($specimenOrObservation->_fieldObjectMedia)) {
118
        $mediaList = array_merge($mediaList, $specimenOrObservation->_fieldObjectMedia);
119
      }
120
      if (is_array($specimenOrObservation->_derivedUnitMedia)) {
121
        $mediaList = array_merge($mediaList, $specimenOrObservation->_derivedUnitMedia);
122
      }
123

    
124
      // typelabel will contain the typeStatus
125
      $type_label = '';
126
      $typeDesignationPager = cdm_ws_get(CDM_WS_OCCURRENCE . '/$0/specimenTypeDesignations', $specimenOrObservation->uuid);
127
      if (isset($typeDesignationPager)) {
128
        $type_status = array();
129
        foreach ($typeDesignationPager->records as $typeDesignation) {
130
          if (isset($typeDesignation->typeStatus->representation_L10n)){
131
            $type_status[] = $typeDesignation->typeStatus->representation_L10n;
132
          }
133
        }
134
        $type_label = implode(', ', $type_status);
135
        if($type_label){
136
          $type_label .= ': ' ;
137
        }
138
      }
139

    
140
      // --- Specimen entry as dynamic label:
141
      //     -> Dynabox for the specimenOrObservation
142
      $gallery_name = $specimenOrObservation->uuid;
143

    
144
      $derived_unit_ws_request = cdm_compose_url(CDM_WS_OCCURRENCE, array( $specimenOrObservation->uuid));
145
      $label_html = cdm_dynabox(
146
          $specimenOrObservation->uuid,
147
          $type_label . $specimenOrObservation->titleCache,
148
          $derived_unit_ws_request,
149
          'cdm_specimenOrObservation', // the theme or compose function to use
150
          'Click for details',
151
          array('div','div'),
152
          'function(){ jQuery(\'#media_gallery_' . $gallery_name . '\').hide(); }', // open_callback
153
          'function(){ jQuery(\'#media_gallery_' . $gallery_name . '\').show(); }' // close_callback
154
      );
155

    
156
      // --- Render associated media.
157
      $gallery_html = '';
158
      if (count($mediaList) > 0) {
159
        $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
160
        $captionElements = array(
161
            '#uri' => t('open media'),
162
        );
163

    
164
        $gallery_html = theme('cdm_media_gallerie', array(
165
            'mediaList' => $mediaList,
166
            'galleryName' => $gallery_name,
167
            'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
168
            'cols' => $gallery_settings['cdm_dataportal_media_cols'],
169
            'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
170
            'captionElements' => $captionElements,
171
            'mediaLinkType' => 'LIGHTBOX',
172
            'alternativeMediaUri' => NULL,
173
            'galleryLinkUri' => NULL,
174
        ));
175
      }
176

    
177
      $specimen_table['#rows'][] = array(
178
          // An array of table rows. Every row is an array of cells, or an associative array
179
          'data' => array(
180
              // Each cell can be either a string or an associative array
181
              $label_html . $gallery_html
182
          ),
183
          'class' =>  array(
184
              'descriptionElement',
185
              'descriptionElement_IndividualsAssociation'
186
          ),
187
      );
188
    }
189
  }
190

    
191
  $render_array['specimen_list'] = $specimen_table;
192
  $render_array['pager'] = markup_to_render_array(
193
      theme('cdm_pager', array(
194
          'pager' => $pager,
195
          'path' => $_REQUEST['q'],
196
          'parameters' => $_REQUEST,
197
      )),
198
      10 // weight
199
  );
200

    
201
  RenderHints::popFromRenderStack();
202
  return $render_array;
203
}
204

    
205

    
206
/**
207
 * TODO should this function really be a compose funtion?
208
 *     For a compose function must there alway be a theme function with the same name? (ak 8.8.2013)
209
 *
210
 * composes and returns an render array containing the components of the taxon profile tab:
211
 *  - 'taxon_profile_image'
212
 *  - 'taxon_description_feature_toc'
213
 *  - 'taxon_description_features'
214
 *
215
 *
216
 * @param array $variables
217
 *   An associative array containing:
218
 *   - taxon
219
 *   - mergedTrees
220
 *   - media
221
 *
222
 * @return array
223
 *   A Drupal reder array with the following elements:
224
 *     - 'taxon_profile_image'
225
 *     - 'taxon_description_feature_toc'
226
 *     - 'taxon_description_features'
227
 *
228
 * @ingroup compose
229
 */
230
function compose_cdm_taxon_page_profile($taxon, $merged_tree, $media) {
231

    
232
  $render_array = array();
233

    
234
  $taxon_profile_image_settings = variable_get(CDM_TAXON_PROFILE_IMAGE, unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT));
235

    
236
  $hide_taxon_profile_image = FALSE;
237
  if (variable_get('image_hide_rank', '0') != '0' && isset($taxon->name->rank->uuid)) {
238
    $rankCompare = rank_compare($taxon->name->rank->uuid, variable_get('image_hide_rank', '-99'));
239
    $hide_taxon_profile_image = ($rankCompare > -1);
240
  }
241

    
242
  if ($taxon_profile_image_settings['show'] && !$hide_taxon_profile_image) {
243

    
244
    $representationPart = new stdClass();
245
    $attributes = array();
246
    if (isset($media[0]->representations[0]->parts[0])) {
247
      $representationPart = $media[0]->representations[0]->parts[0];
248
      $attributes['alt'] = $representationPart->uri;
249

    
250
      if(!empty($taxon_profile_image_settings['media_uri_query'])){
251
        $representationPart->uri = $representationPart->uri
252
        . (strpos($representationPart->uri, '?') !== FALSE ? '&' : '?')
253
        . $taxon_profile_image_settings['media_uri_query'];
254
      }
255
    }
256
    else {
257
      // show placeholder image instead
258
      if(!empty($taxon_profile_image_settings['custom_placeholder_image_on']) && !empty($taxon_profile_image_settings['custom_placeholder_image_fid'])){
259
        // use the user provided image
260
        $profile_image_file = file_load($taxon_profile_image_settings['custom_placeholder_image_fid']);
261
        $url = file_create_url($profile_image_file->uri);
262
        $image_info = image_get_info($profile_image_file->uri);
263
        $representationPart->width = $image_info['width'];
264
        $representationPart->height = $image_info['height'];
265
        $representationPart->uri = $url;
266
      } else {
267
        // use the hard coded default
268
        $representationPart->width = 184;
269
        $representationPart->height = 144;
270
        $representationPart->uri = base_path() .  drupal_get_path('module', 'cdm_dataportal') . '/images/no_picture.png';
271
      }
272
      $attributes['alt'] = "no image available";
273
    }
274

    
275
    $profile_image = theme('cdm_media_gallerie_image', array(
276
        'mediaRepresentationPart' => $representationPart,
277
        'maxExtend' => $taxon_profile_image_settings['maxextend'],
278
        'addPassePartout' => FALSE,
279
        'attributes' => $attributes,
280
    ));
281
    // NOTE: style="width:${maxextend}px' is needed for IE8 !!!
282
    $render_array['taxon_profile_image'] = markup_to_render_array('<div id="taxonProfileImage" style="width:' . $taxon_profile_image_settings['maxextend'] . 'px">' . $profile_image. '</div>', -101);
283
  }
284

    
285
  $toc = array(
286
      '#theme' => 'item_list',
287
      '#items' => cdm_feature_node_toc_items($merged_tree->root->children),
288
      '#title' => t('Content'),
289
      '#weight' => -100,
290
      '#suffix' => '</div>',
291
      '#prefix'=> '<div id="featureTOC">' // reusing id featureTOC even if this is semantically not correct
292
  );
293
  $render_array['taxon_description_feature_toc'] = $toc; //markup_to_render_array($toc_html);
294

    
295
  // Description.
296
  $render_array['taxon_description_features'] = markup_to_render_array(
297
      theme(
298
          'cdm_feature_nodes',
299
          array('mergedFeatureNodes' => $merged_tree->root->children, 'taxon' => $taxon)
300
      )
301
  );
302

    
303
  return $render_array;
304
}
305

    
306

    
307
/**
308
 * Manages the tab to be hidden in the taxon page.
309
 *
310
 * The tabs are identified by their last menu link path element:
311
 *  - description
312
 *  - synonymy
313
 *  - images
314
 *  - specimens
315
 *  - key
316
 *
317
 * Internally the tabs are stored in a static variable which is
318
 * managed by drupal_static().
319
 *
320
 * @param string $add_tab
321
 *   Optinal parameter. The given string will be added to the array of tabs
322
 *
323
 * @return
324
 *   The array of tabs
325
 */
326
function taxon_page_tabs_hidden($add_tab = NULL) {
327
  $tabs = &drupal_static(__FUNCTION__);
328

    
329
  if(!isset($tabs)){
330
    $tabs = array();
331
  }
332

    
333
  if (isset($add_tab) && !array_key_exists($add_tab, $tabs)) {
334
    $tabs[] = $add_tab;
335
  }
336

    
337
  return $tabs;
338
}
339

    
340
/**
341
 * Implements the hook_preprocess_HOOK() for theme_menu_local_tasks()
342
 *
343
 *  - Removes the tabs to be hidden, @see taxon_page_tabs_hidden()
344
 *  - Renames tabs according to the settings // TODO (this will replace the theme_cdm_taxonpage_tab() function !!!)
345
 *
346
 * @param array $variables
347
 *   The variables array
348
 */
349
function cdm_dataportal_preprocess_menu_local_tasks(&$variables) {
350

    
351
  $hidden_tabs = taxon_page_tabs_hidden();
352

    
353
  if (is_array($variables['primary'])) {
354
    foreach ($variables['primary'] as $key => &$element) {
355

    
356
      // 1. Remove the tabs to be hidden
357
      foreach ($hidden_tabs as $tab) {
358
        if ($element['#link']['path'] == 'cdm_dataportal/taxon/%/' . $tab) {
359
          // remove the tab
360
          unset($variables['primary'][$key]);
361
        }
362
      }
363
    }
364
  }
365
}
366

    
367

    
368

    
369
/**
370
 * Implements the hook_preprocess_HOOK() for theme_menu_local_task()
371
 *
372
 *
373
 * @param array $variables
374
 *   An associative array containing:
375
 *     - element: A render element containing:
376
 *          #link: A menu link array with 'title', 'href', and 'localized_options' keys.
377
 *          #active: A boolean indicating whether the local task is active.
378
 *
379
 */
380
function cdm_dataportal_preprocess_menu_local_task(&$variables) {
381

    
382
  $link = $variables['element']['#link'];
383
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
384
    $link['title'] = '<img class="refresh" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/view-refresh.png' . '" alt="' . check_plain($link['title']) . '" title="' . check_plain($link['title']) . '"/>';
385
    $link['localized_options']['html'] = TRUE;
386

    
387
    $variables['element']['#link'] = $link;
388
  }
389
}
(6-6/7)