Project

General

Profile

Download (30.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
    if(variable_get('cdm_dataportal_compressed_specimen_derivate_table')){
103

    
104
        //COMPRESSED SPECIMEN DERIVATE TABLE
105
        $fieldUnits = cdm_ws_get(CDM_WS_PORTAL_TAXON, array($taxon->uuid, 'associatedFieldUnits'));
106
        $detail_image_icon = '<img title="Detail Image" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/detail_image_derivate-16x16-32.png' . '"/>';
107
        $checked_box_icon = '<img src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/step_done.gif' . '"/>';
108
        $sequence_icon = '<img title="Molecular Data" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/sequence_derivate-16x16-32.png' . '"/>';
109
        if ($fieldUnits) {
110
            $rowcount = 0;
111
            foreach ($fieldUnits as $fieldUnit) {
112
                //get derivate hierarchy for the FieldUnit
113
                $derivateHierarchy = cdm_ws_get(CDM_WS_PORTAL_OCCURRENCE, array($fieldUnit->uuid, 'derivateHierarchy'), 'taxonUuid=' . $taxon->uuid);
114
                if($derivateHierarchy){
115
                    //summary row
116
                    $rows[] = array(
117
                        'data' => array(
118
                            $derivateHierarchy->country,
119
                            $derivateHierarchy->date,
120
                            $derivateHierarchy->collection,
121
                            $derivateHierarchy->herbarium,
122
                            array(
123
                                'data' => $derivateHierarchy->hasType? $checked_box_icon:"",
124
                                'class' => array('checkBoxCell')
125
                            ),
126
                            array(
127
                                'data' => $derivateHierarchy->hasSpecimenScan? $checked_box_icon:"",
128
                                'class' => array('checkBoxCell')
129
                            ),
130
                            ($derivateHierarchy->hasDna? $sequence_icon :"") . ($derivateHierarchy->hasDetailImage?$detail_image_icon:"")
131
                        ),
132
                        'id' => ('derivate_summary' . $rowcount), // summary row id
133
                        'class' => array('summary_row'),
134
                    );
135

    
136
                    //assemble detail html
137
                    $taxonName = $derivateHierarchy->taxonName;
138
                    $protologue = $derivateHierarchy->protologue;
139
                    $citation = $derivateHierarchy->citation;
140
                    $types = "";
141
                    foreach($derivateHierarchy->types as $typeStatus => $accessionList){
142
                        $types .= $typeStatus;
143
                        foreach($accessionList as $accessionNumber){
144
                            $types .= " (" . $accessionNumber . ") ";
145
                        }
146
                    }
147
                    $specimenScans = createLinks($derivateHierarchy->specimenScans);
148
                    $molecularData = createLinks($derivateHierarchy->molecularData);
149
                    $detailImages = createLinks($derivateHierarchy->detailImages);
150

    
151
                    $detail_html = "";
152
                    if($taxonName){
153
                        $detail_html .= "<b>Name:</b> " . $taxonName . "<br>";
154
                    }
155
                    if($protologue){
156
                        //$detail_html .= "<b>Protologue:</b> " . $protologue . "<br>"
157
                    }
158
                    if($citation){
159
                        $detail_html .= "<b>Citation:</b> " . $citation . "<br>";
160
                    }
161
                    if($types){
162
                        $detail_html .= "<b>Types:</b> " . $types . "<br>";
163
                    }
164
                    if($specimenScans){
165
                        $detail_html .= "<b>Specimen Scans:</b> " . $specimenScans . "<br>";
166
                    }
167
                    if($molecularData){
168
                        $detail_html .= "<b>Molecular Data:</b> " . $molecularData . "<br>";
169
                    }
170
                    if($detailImages){
171
                        $detail_html .=  "<b>Detail Images:</b> " . $detailImages . "<br>";;
172
                    }
173

    
174
                    //detail row resp. one BIG detail cell
175
                    $rows[] = array(
176
                        'data' => array(
177
                            array(
178
                                'data' => $detail_html,
179
                                'colspan' => 8,
180
                            ),
181
                        ),
182
                        'id' => ('derivate_details' . $rowcount),//details row ID
183
                        'class' => array('detail_row'),
184
                    );
185
                    $rowcount++;
186
                }
187
            }
188
        }
189

    
190
        $tableId = "derivate_hierarchy_table";
191
        $derivateHierarchyTable = array(
192
            "#theme" => "table",
193
            "#weight" => 2,
194
            "#header" => array("Country", "Date", "Collection", "Herbaria", "Type", "Image", "Derivates"),
195
            "#rows" => $rows,
196
            "#attributes" => array(
197
                "id" => $tableId,
198
                "border" => 2
199
            )
200
        );
201

    
202
        //add toggle functionality to derivate hierarchy table
203
        drupal_add_js_rowToggle("#".$tableId);
204

    
205
        $render_array['derivate_hierarchy_table'] = $derivateHierarchyTable;
206
    }
207
    else{
208
        //BOTTOM-UP-SPECIMEN-TABLE
209
        // --- generate the specimen list as table
210
        $specimen_table = array(
211
            '#theme' => 'table',
212
            '#weight' => 2,
213
            // prefix attributes and rows with '#' to let it pass to the theme function,
214
            // otherwise it is handled as child render array
215
            '#attributes' => array('class' => 'specimens'),
216
            '#rows' => array(),
217
        );
218

    
219
        if ($specimensOrObservations) {
220

    
221
            foreach ($specimensOrObservations as $specimenOrObservation) {
222

    
223
                $mediaList = array();
224
                if (is_array($specimenOrObservation->_fieldObjectMedia)) {
225
                    $mediaList = array_merge($mediaList, $specimenOrObservation->_fieldObjectMedia);
226
                }
227
                if (is_array($specimenOrObservation->_derivedUnitMedia)) {
228
                    $mediaList = array_merge($mediaList, $specimenOrObservation->_derivedUnitMedia);
229
                }
230

    
231
                // typelabel will contain the typeStatus
232
                $type_label = '';
233
                $typeDesignationPager = cdm_ws_get(CDM_WS_OCCURRENCE . '/$0/specimenTypeDesignations', $specimenOrObservation->uuid);
234
                if (isset($typeDesignationPager)) {
235
                    $type_status = array();
236
                    foreach ($typeDesignationPager->records as $typeDesignation) {
237
                        if (isset($typeDesignation->typeStatus->representation_L10n)){
238
                            $type_status[] = $typeDesignation->typeStatus->representation_L10n;
239
                        }
240
                    }
241
                    $type_label = implode(', ', $type_status);
242
                    if($type_label){
243
                        $type_label .= ': ' ;
244
                    }
245
                }
246

    
247
                // --- Specimen entry as dynamic label:
248
                //     -> Dynabox for the specimenOrObservation
249
                $gallery_name = $specimenOrObservation->uuid;
250

    
251
                $derived_unit_ws_request = cdm_compose_url(CDM_WS_OCCURRENCE, array( $specimenOrObservation->uuid));
252
                $label_html = cdm_dynabox(
253
                    $specimenOrObservation->uuid,
254
                    $type_label . $specimenOrObservation->titleCache,
255
                    $derived_unit_ws_request,
256
                    'cdm_specimenOrObservation', // the theme or compose function to use
257
                    'Click for details',
258
                    array('div','div'),
259
                    array(),
260
                    'function(){ jQuery(\'#media_gallery_' . $gallery_name . '\').hide(); }', // open_callback
261
                    'function(){ jQuery(\'#media_gallery_' . $gallery_name . '\').show(); }' // close_callback
262
                );
263

    
264
                // --- Render associated media.
265
                $gallery_html = '';
266
                if (count($mediaList) > 0) {
267
                    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
268
                    $captionElements = array(
269
                        '#uri' => t('open media'),
270
                    );
271

    
272
                    $gallery_html = theme('cdm_media_gallerie', array(
273
                        'mediaList' => $mediaList,
274
                        'galleryName' => $gallery_name,
275
                        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
276
                        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
277
                        'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
278
                        'captionElements' => $captionElements,
279
                        'mediaLinkType' => 'LIGHTBOX',
280
                        'alternativeMediaUri' => NULL,
281
                        'galleryLinkUri' => NULL,
282
                    ));
283
                }
284

    
285
                $specimen_table['#rows'][] = array(
286
                    // An array of table rows. Every row is an array of cells, or an associative array
287
                    'data' => array(
288
                        // Each cell can be either a string or an associative array
289
                        $label_html . $gallery_html
290
                    ),
291
                    'class' =>  array(
292
                        'descriptionElement',
293
                        'descriptionElement_IndividualsAssociation'
294
                    ),
295
                );
296
            }
297
        }
298

    
299
        $render_array['specimen_list'] = $specimen_table;
300
        $render_array['pager'] = markup_to_render_array(
301
            theme('cdm_pager', array(
302
                'pager' => $pager,
303
                'path' => $_REQUEST['q'],
304
                'parameters' => $_REQUEST,
305
            )),
306
            10 // weight
307
        );
308
    }
309
    RenderHints::popFromRenderStack();
310
    return $render_array;
311
}
312

    
313
/**
314
 * Creates HTML links from the given pair list concatenated by a white space.
315
 * @param $linkTextPairList the list with pairs which hold the uri as "first" and uriText as "second"
316
 * @return string the assembled HTML string containing the links
317
 */
318
function createLinks($linkTextPairList)
319
{
320
    $html = "";
321
    if ($linkTextPairList) {
322
        foreach ($linkTextPairList as $linkTextPair) {
323
            $html .= '<a href="' . $linkTextPair->first . '">' . $linkTextPair->second . "</a>";
324
        }
325
    }
326
    return $html;
327
}
328
/**
329
 * Composes a taxon page which can consist of multiple parts like
330
 * 'General', 'Synonymy', 'Images', 'Keys'. These parts can be displayed
331
 * as tabs or as sections of a single page.
332
 *
333
 * It is headed by the name of the accepted taxon without author and reference.
334
 *
335
 * @param $taxon
336
 *   The CDM Taxon Instance to compose the page for.
337
 * @param $page_part
338
 *   Name of the part to display, valid values are:
339
 *    - 'description' -  for the general part
340
 *    - 'images'
341
 *    - 'synonymy'
342
 *    - 'keys'
343
 *    - 'all'
344
 *
345
 * @return array
346
 *   A drupal render array
347
 *
348
 * @ingroup compose
349
 */
350
function compose_cdm_taxon_page($taxon, $page_part = 'description') {
351

    
352
  global $theme;
353

    
354
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
355
  $http_request_params = drupal_get_query_parameters();
356

    
357
  // add all mandatory js sources
358
  _add_js_footnotes();
359

    
360

    
361
  $render_array = array();
362
  $weight = 0; // the weight for the render array elements
363

    
364
  $tabsToDisplay = variable_get('cdm_taxonpage_tabs_visibility', unserialize(TAXONPAGE_VISIBILITY_OPTIONS_DEFAULT));
365

    
366
  $page_part = variable_get('cdm_dataportal_taxonpage_tabs', 1) ? $page_part : 'all';
367

    
368
  $media = _load_media_for_taxon($taxon);
369

    
370

    
371
  if (!isset($media[0]) || ($tabsToDisplay["Images"] == '0')) {
372
    taxon_page_tabs_hidden('images');
373
  }
374

    
375
  // --- GET specimensOrObservations --- //
376
  $specimensOrObservations = cdm_ws_get(CDM_WS_TAXON, array( $taxon->uuid, 'specimensOrObservations'));
377

    
378
  $specimensOrObservationsCount = is_array($specimensOrObservations) ? count($specimensOrObservations) : 0;
379
  if ($specimensOrObservationsCount == 0 || ($tabsToDisplay["Specimens"] == '0')) {
380
    taxon_page_tabs_hidden('specimens');
381
  }
382

    
383
  // --- GET polytomousKeys --- //
384
  $polytomousKeysPager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, NULL, "findByTaxonomicScope=$taxon->uuid");
385
  $identificationKeyCount = 0;
386
  if ($polytomousKeysPager) {
387
    $identificationKeyCount += $polytomousKeysPager->count;
388
  }
389
  if ($identificationKeyCount == 0 || ($tabsToDisplay["Keys"] == '0')) {
390
    taxon_page_tabs_hidden('keys');
391
  }
392

    
393
  if ($tabsToDisplay["Synonymy"] == '0') {
394
    taxon_page_tabs_hidden('synonymy');
395
  }
396

    
397
  // -------------------------------------------- //
398

    
399
  $render_array['back_to_search'] = markup_to_render_array(theme('cdm_back_to_search_result_button'), -103);
400

    
401
  if (variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR) && isset($_REQUEST['acceptedFor'])) {
402
    $render_array['accepted_for'] = markup_to_render_array(theme('cdm_acceptedFor', array('acceptedFor' => $_REQUEST['acceptedFor'])), $weight++);
403
  }
404

    
405
  // --- PAGE PART: DESCRIPTION --- //
406
  if ($page_part == 'description' || $page_part == 'all') {
407

    
408
    $merged_tree = merged_taxon_feature_tree($taxon);
409

    
410

    
411
    $render_array['general'] = compose_cdm_taxon_page_profile($taxon, $merged_tree, $media);
412
    $render_array['general']['#weight'] = $weight++;
413
    $render_array['general']['#prefix'] = '<div id="general" class="page-part">';
414
    $render_array['general']['#suffix'] = '</div>';
415
  }
416

    
417
  // --- PAGE PART: IMAGES --- //
418
  if (array_search('images', taxon_page_tabs_hidden()) === FALSE && ($page_part == 'images' || $page_part == 'all')) {
419
    $images_html = '<div id="images" class="page-part">';
420
    if ($page_part == 'all') {
421
      $images_html .= '<h2>' . t('Images') . '</h2>';
422
    }
423
    // Get the image gallery as configured by the admin.
424
    $taxon_image_gallery = call_user_func_array('taxon_image_gallery_' . variable_get('image_gallery_viewer', 'default'), array(
425
        $taxon,
426
        $media,
427
    ));
428
    $images_html .= $taxon_image_gallery;
429
    $images_html .= '</div>';
430
    $render_array['images'] = markup_to_render_array($images_html, $weight++);
431
  }
432

    
433
  // --- PAGE PART: SYNONYMY --- //
434
  if (($page_part == 'synonymy' || $page_part == 'all')) {
435
    $synonymy_html = '<div id="synonymy" class="page-part">';
436
    if ($page_part == 'all') {
437
      $synonymy_html .= '<h2>' . t('Synonymy') . '</h2>';
438
    }
439
    $addAcceptedTaxon = variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE);
440
    $synonymy_html .= theme('cdm_taxon_page_synonymy', array('taxon' => $taxon, 'addAcceptedTaxon' => $addAcceptedTaxon));
441

    
442
    $synonymy_html .= '</div>';
443
    $render_array['synonymy'] = markup_to_render_array($synonymy_html, $weight++);
444

    
445
  }
446

    
447
  // --- PAGE PART: SPECIMENS --- //
448
  if ($specimensOrObservationsCount > 0 && ($page_part == 'specimens' || $page_part == 'all')) {
449
    $render_array['specimens'] = array(
450
        '#prefix' => '<div id="specimens" class="page-part">' . ($page_part == 'all' ? '<h2>' . t('Specimens') . '</h2>' : ''),
451
        'content' => cdm_dataportal_taxon_page_specimens($taxon), // returns render array
452
        '#suffix' => '</div>',
453
    );
454
  }
455

    
456
  // --- PAGE PART: KEYS --- //
457
  if ($identificationKeyCount == 1 && $page_part == 'keys'){
458
    drupal_goto(path_to_key($polytomousKeysPager->records[0]->class, $polytomousKeysPager->records[0]->uuid));
459
  }
460
  else if ($identificationKeyCount > 0 && ($page_part == 'keys' || $page_part == 'all')) {
461
    $keys_html = '<div id="keys" class="page-part">';
462
    if ($page_part == 'all') {
463
      $keys_html .= '<h2>' . t('Keys') . '</h2>';
464
    }
465
    $keys_html .= theme('cdm_block_IdentificationKeys', array('taxonUuid' => $taxon->uuid));
466
    $keys_html .= '</div>';
467
    $render_array['keys'] = markup_to_render_array($keys_html, $weight++);
468
  }
469

    
470

    
471
  // adjust weights of page and toc elements according to the settings
472
  $taxontabs_weights = get_array_variable_merged('cdm_taxonpage_tabs_weight', CDM_TAXONPAGE_TAB_WEIGHT_DEFAULT);
473
  foreach($taxontabs_weights as $tab_key => $weight){
474
    if(isset($render_array[$tab_key])){
475
      $render_array[$tab_key]['#weight'] = $weight;
476
    }
477
  }
478

    
479

    
480
  // set up the TOC for the pages which contain all pageparts
481
  if($page_part == 'all') {
482

    
483
    asort($taxontabs_weights);
484
    foreach(array_keys($taxontabs_weights) as $tab_key){
485
      if(isset($render_array[$tab_key])){
486
        if($tab_key != 'general'){
487
          // add entry for page part
488
          $toc_elements[] = array(
489
              'data' => l(t(ucfirst($tab_key)), $_GET['q'], array('fragment' => $tab_key, 'query' => $http_request_params)),
490
              'class' => array('page-part-toc-item-' . $tab_key)
491
          );
492
        } else {
493
          // add content of profile part instead
494
          if(isset($render_array['general'])) {
495
            // in case all tabs are shown at once the feature tocs
496
            // should be integrated into the tabs toc as sub list
497
            // and the profile image should be on top of the page
498
            if(isset($render_array['general']['taxon_description_feature_toc'])){;
499
            foreach ($render_array['general']['taxon_description_feature_toc']['#items'] as $profile_toc_item){
500
              $toc_elements[] = $profile_toc_item;
501
            }
502
            unset($render_array['general']['taxon_description_feature_toc']);
503
            }
504
          }
505
        }
506
      }
507
    }
508

    
509
    // move profile image in page structure
510
    if(isset($render_array['general']['taxon_profile_image'])){
511
      $render_array['profile_image'] = $render_array['general']['taxon_profile_image'];
512
      $render_array['profile_image']['#weight'] = -100;
513
      unset($render_array['general']['taxon_profile_image']);
514
    }
515

    
516
    // finally add the table of contents to the render array
517
    $render_array['toc'] = array(
518
        '#theme' => 'item_list',
519
        '#items' => $toc_elements,
520
        '#title' => t('Content'),
521
        '#weight' => -101,
522
        '#suffix' => '</div>',
523
        '#prefix'=> '<div id="page-toc">'
524
    );
525
  }
526

    
527

    
528
  return $render_array;
529
}
530

    
531
  /**
532
   * TODO move into description.inc or in cdm_api.module?
533
   *
534
   * @param $taxon
535
   * @param $distribution_node
536
   * @return array
537
   */
538
  function merged_taxon_feature_tree($taxon) {
539

    
540
    // 1. fetch descriptions_by_featuretree but exclude the distribution feature
541
    $merged_tree = cdm_ws_descriptions_by_featuretree(get_profile_featureTree(), $taxon->uuid, array(UUID_DISTRIBUTION));
542

    
543

    
544
    // 2. find the distribution feature node
545
    $distribution_node =& cdm_feature_tree_find_node($merged_tree->root->childNodes, UUID_DISTRIBUTION);
546

    
547
    if ($distribution_node) {
548
      // 3. get the distributionInfoDTO
549
      $query_parameters = cdm_distribution_filter_query();
550
      $query_parameters['part'] = array('mapUriParams');
551
      if (variable_get('distribution_sort', 'NO_SORT') != 'NO_SORT') {
552
        $query_parameters['part'][] = 'tree';
553
      }
554
      else {
555
        $query_parameters['part'][] = 'elements';
556
      }
557
      $query_parameters['omitLevels'] = array(UUID_NAMEDAREALEVEL_TDWGLEVEL_2);
558
      $customStatusColorsJson = variable_get(DISTRIBUTION_STATUS_COLORS, NULL);
559
      if ($customStatusColorsJson) {
560
        $query_parameters['statusColors'] = $customStatusColorsJson;
561
      }
562

    
563
      $distribution_info_dto = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION_DISTRIBUTION_INFO_FOR, $taxon->uuid, queryString($query_parameters));
564
      // 4. get distribution TextData is there are any
565
      $distribution_text_data = cdm_ws_fetch_all(CDM_WS_DESCRIPTIONELEMENT_BY_TAXON,
566
        array(
567
          'taxon' => $taxon->uuid,
568
          'type' => 'TextData',
569
          'features' => UUID_DISTRIBUTION
570
        )
571
      );
572

    
573
      // 5. put all distribution data into the distribution feature node
574
      if ($distribution_text_data //if text data exists
575
        || ($distribution_info_dto && isset($distribution_info_dto->tree) && $distribution_info_dto->tree->rootElement->numberOfChildren > 0) // OR if tree element has distribution elements
576
        || ($distribution_info_dto && !empty($distribution_info_dto->elements))
577
      ) { // OR if DTO has distribution elements
578
        $distribution_node->descriptionElements = array('#type' => 'DTO');
579
        if ($distribution_text_data) {
580
          $distribution_node->descriptionElements['TextData'] = $distribution_text_data;
581
        }
582
        if ($distribution_info_dto) {
583
          $distribution_node->descriptionElements['DistributionInfoDTO'] = $distribution_info_dto;
584
        }
585
      }
586
    }
587
    return $merged_tree;
588
  }
589

    
590

    
591
/**
592
 * TODO should this function really be a compose function?
593
 *     For a compose function must there always be a theme function with the same name? (ak 8.8.2013)
594
 *
595
 * composes and returns an render array containing the components of the taxon profile tab:
596
 *  - 'taxon_profile_image'
597
 *  - 'taxon_description_feature_toc'
598
 *  - 'taxon_description_features'
599
 *
600
 *
601
 * @param array $variables
602
 *   An associative array containing:
603
 *   - taxon
604
 *   - mergedTrees
605
 *   - media
606
 *
607
 * @return array
608
 *   A Drupal reder array with the following elements:
609
 *     - 'taxon_profile_image'
610
 *     - 'taxon_description_feature_toc'
611
 *     - 'taxon_description_features'
612
 *
613
 * @ingroup compose
614
 */
615
function compose_cdm_taxon_page_profile($taxon, $merged_tree, $media) {
616

    
617
  $render_array = array();
618

    
619
  $taxon_profile_image_settings = variable_get(CDM_TAXON_PROFILE_IMAGE, unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT));
620

    
621
  $hide_taxon_profile_image = FALSE;
622
  if (variable_get('image_hide_rank', '0') != '0' && isset($taxon->name->rank->uuid)) {
623
    $rankCompare = rank_compare($taxon->name->rank->uuid, variable_get('image_hide_rank', '-99'));
624
    $hide_taxon_profile_image = ($rankCompare > -1);
625
  }
626

    
627
  if ($taxon_profile_image_settings['show'] && !$hide_taxon_profile_image) {
628

    
629
    $representationPart = new stdClass();
630
    $attributes = array();
631
    if (isset($media[0]->representations[0]->parts[0])) {
632
      $representationPart = $media[0]->representations[0]->parts[0];
633
      $attributes['alt'] = $representationPart->uri;
634

    
635
      if(!empty($taxon_profile_image_settings['media_uri_query'])){
636
        $representationPart->uri = $representationPart->uri
637
        . (strpos($representationPart->uri, '?') !== FALSE ? '&' : '?')
638
        . $taxon_profile_image_settings['media_uri_query'];
639
      }
640
    }
641
    else {
642
      // show placeholder image instead
643
      if(!empty($taxon_profile_image_settings['custom_placeholder_image_on']) && !empty($taxon_profile_image_settings['custom_placeholder_image_fid'])){
644
        // use the user provided image
645
        $profile_image_file = file_load($taxon_profile_image_settings['custom_placeholder_image_fid']);
646
        $url = file_create_url($profile_image_file->uri);
647
        $image_info = image_get_info($profile_image_file->uri);
648
        $representationPart->width = $image_info['width'];
649
        $representationPart->height = $image_info['height'];
650
        $representationPart->uri = $url;
651
      } else {
652
        // use the hard coded default
653
        $representationPart->width = 184;
654
        $representationPart->height = 144;
655
        $representationPart->uri = base_path() .  drupal_get_path('module', 'cdm_dataportal') . '/images/no_picture.png';
656
      }
657
      $attributes['alt'] = "no image available";
658
    }
659

    
660
    $profile_image = theme('cdm_media_gallerie_image', array(
661
        'mediaRepresentationPart' => $representationPart,
662
        'maxExtend' => $taxon_profile_image_settings['maxextend'],
663
        'addPassePartout' => FALSE,
664
        'attributes' => $attributes,
665
    ));
666
    // NOTE: style="width:${maxextend}px' is needed for IE8 !!!
667
    $render_array['taxon_profile_image'] = markup_to_render_array('<div id="taxonProfileImage" style="width:' . $taxon_profile_image_settings['maxextend'] . 'px">' . $profile_image. '</div>', -101);
668
  }
669

    
670

    
671
  // Render the sections for each feature
672
  $render_array['taxon_description_features'] = compose_cdm_feature_nodes($merged_tree->root->childNodes, $taxon);
673

    
674
  $bibliography_settings = get_bibliography_settings();
675
  if($bibliography_settings['enabled'] == 1){
676
    $bibliography_block = feature_block(t('Bibliography'));
677
    $bibliography_block->content = FootnoteManager::renderFootnoteList('BIBLIOGRAPHY', '');
678

    
679
    if(!empty($bibliography_block->content)){
680
      $render_array['taxon_description_bibliography'] =  markup_to_render_array(
681
        theme('block',
682
          array(
683
            'elements' => array(
684
              '#block' => $bibliography_block,
685
              '#children' => $bibliography_block->content,
686
            )
687
          )
688
        ),
689
        100 // weight
690
      );
691
      cdm_toc_list_add_item('Bibliography', 'bibliography');
692
    }
693

    
694
  }
695

    
696
  // create the table of content
697
  $toc = array(
698
      '#theme' => 'item_list',
699
    '#items' => cdm_toc_list(),
700
      '#title' => t('Content'),
701
    '#weight' => -100,                  // move to the top
702
      '#suffix' => '</div>',
703
      '#prefix'=> '<div id="page-toc">'
704
  );
705
  $render_array['taxon_description_feature_toc'] = $toc;
706

    
707
  return $render_array;
708
}
709

    
710

    
711
/**
712
 * Manages the tab to be hidden in the taxon page.
713
 *
714
 * The tabs are identified by their last menu link path element:
715
 *  - description
716
 *  - synonymy
717
 *  - images
718
 *  - specimens
719
 *  - key
720
 *
721
 * Internally the tabs are stored in a static variable which is
722
 * managed by drupal_static().
723
 *
724
 * @param string $add_tab
725
 *   Optinal parameter. The given string will be added to the array of tabs
726
 *
727
 * @return
728
 *   The array of tabs
729
 */
730
function taxon_page_tabs_hidden($add_tab = NULL) {
731
  $tabs = &drupal_static(__FUNCTION__);
732

    
733
  if(!isset($tabs)){
734
    $tabs = array();
735
  }
736

    
737
  if (isset($add_tab) && !array_key_exists($add_tab, $tabs)) {
738
    $tabs[] = $add_tab;
739
  }
740

    
741
  return $tabs;
742
}
743

    
744
/**
745
 * Implements the hook_preprocess_HOOK() for theme_menu_local_tasks()
746
 *
747
 *  - Removes the tabs to be hidden, @see taxon_page_tabs_hidden()
748
 *  - Renames tabs according to the settings // TODO (this will replace the theme_cdm_taxonpage_tab() function !!!)
749
 *
750
 * @param array $variables
751
 *   The variables array
752
 */
753
function cdm_dataportal_preprocess_menu_local_tasks(&$variables) {
754

    
755
  $hidden_tabs = taxon_page_tabs_hidden();
756

    
757
  if (is_array($variables['primary'])) {
758
    foreach ($variables['primary'] as $key => &$element) {
759

    
760
      // 1. Remove the tabs to be hidden
761
      foreach ($hidden_tabs as $tab) {
762
        if ($element['#link']['path'] == 'cdm_dataportal/taxon/%/' . $tab) {
763
          // remove the tab
764
          unset($variables['primary'][$key]);
765
        }
766
      }
767
    }
768
  }
769
}
770

    
771

    
772

    
773
/**
774
 * Implements the hook_preprocess_HOOK() for theme_menu_local_task()
775
 *
776
 *
777
 * @param array $variables
778
 *   An associative array containing:
779
 *     - element: A render element containing:
780
 *          #link: A menu link array with 'title', 'href', and 'localized_options' keys.
781
 *          #active: A boolean indicating whether the local task is active.
782
 *
783
 */
784
function cdm_dataportal_preprocess_menu_local_task(&$variables) {
785

    
786
  $link = $variables['element']['#link'];
787
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
788
    $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']) . '"/>';
789
    $link['localized_options']['html'] = TRUE;
790

    
791
    $variables['element']['#link'] = $link;
792
  }
793
}
(6-6/7)