Project

General

Profile

Download (34.3 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 parameters
90
    if (count($specimensOrObservations) > 0) {
91
        $occurrence_queryDto = cdm_ws_get(CDM_WS_GEOSERVICE_OCCURRENCEMAP, $taxon->uuid,  $by_associatedtaxon_query);
92

    
93
        if( isset($occurrence_queryDto->occurrenceQuery) ) {
94
            $occurrence_query = $occurrence_queryDto->occurrenceQuery;
95
            $legend_format_query = null;
96
            $distribution_query = NULL;
97
            $render_array['map'] = compose_map($occurrence_query, $distribution_query, $legend_format_query, array());
98
        }
99
    }
100
    if(variable_get('cdm_dataportal_compressed_specimen_derivate_table')){
101

    
102
        //COMPRESSED SPECIMEN DERIVATE TABLE
103
        $associatedFieldUnitsQuery_parameters = array();
104
        if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
105
            $associatedFieldUnitsQuery_parameters = array_merge($associatedFieldUnitsQuery_parameters, $_REQUEST['pager']);
106
        }
107

    
108
        $by_associatedtaxon_query = http_build_query($associatedFieldUnitsQuery_parameters);
109
        $pagerFieldUnits = cdm_ws_get(CDM_WS_PORTAL_TAXON, array($taxon->uuid, 'associatedFieldUnits'), $by_associatedtaxon_query);
110
        // get icon images
111
        $expand_icon = '<img class="expand_icon" title="Show details" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/btn_expand.gif' . '"/>';
112
        $collapse_icon = '<img class="collapse_icon" title="Hide details" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/btn_collapse.gif' . '"/>';
113
        $detail_image_icon = '<img title="Detail Image" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/detail_image_derivate-16x16-32.png' . '"/>';
114
        $checked_box_icon = '<img src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/step_done.gif' . '"/>';
115
        $sequence_icon = '<img title="Molecular Data" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/sequence_derivate-16x16-32.png' . '"/>';
116
        $character_data_icon = '<img title="Character Data" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/character_data_derivate-16x16-32.png' . '"/>';
117
        if (isset($pagerFieldUnits->records[0])) {
118
            $rowcount = 0;
119
            foreach ($pagerFieldUnits->records as $fieldUnit) {
120
                //get derivate hierarchy for the FieldUnit
121
                $derivateHierarchy = cdm_ws_get(CDM_WS_PORTAL_OCCURRENCE, array($fieldUnit->uuid, 'derivateHierarchy'), 'taxonUuid=' . $taxon->uuid);
122
                if($derivateHierarchy){
123
                    //summary row
124
                    $rows[] = array(
125
                        'data' => array(
126
                            array(
127
                                'data' => $expand_icon . $collapse_icon,
128
                                'class' => array('summary_row_cell', 'summary_row_icon', 'expand_column')
129
                            ),
130
                            array(
131
                                'data' => $derivateHierarchy->country,
132
                                'class' => array('summary_row_cell')
133
                            ),
134
                            array(
135
                                'data' => $derivateHierarchy->date,
136
                                'class' => array('summary_row_cell')
137
                            ),
138
                            array(
139
                                'data' => $derivateHierarchy->collection,
140
                                'class' => array('summary_row_cell')
141
                            ),
142
                            array(
143
                                'data' => $derivateHierarchy->herbarium,
144
                                'class' => array('summary_row_cell')
145
                            ),
146
                            array(
147
                                'data' => $derivateHierarchy->hasType? $checked_box_icon:"",
148
                                'class' => array('summary_row_cell', 'summary_row_icon')
149
                            ),
150
                            array(
151
                                'data' => $derivateHierarchy->hasSpecimenScan? $checked_box_icon:"",
152
                                'class' => array('summary_row_cell', 'summary_row_icon')
153
                            ),
154
                            array(
155
                                'data' => ($derivateHierarchy->hasDna? $sequence_icon :"")." "
156
                                    .($derivateHierarchy->hasDetailImage?$detail_image_icon:"")." "
157
                                    .($derivateHierarchy->hasCharacterData?$character_data_icon:""),
158
                                'class' => array('summary_row_cell', 'summary_row_icon')
159
                            )
160
                        ),
161
                        'id' => ('derivate_summary' . $rowcount), // summary row id
162
                        'class' => array('summary_row'),
163
                    );
164

    
165
                    //assemble field unit details
166
                    $detail_html = "";
167
                    // - taxon name
168
                    if (variable_get('cdm_dataportal_compressed_specimen_derivate_table_show_determined_as') && $derivateHierarchy->taxonName) {
169
                        $detail_html .= create_label("Determined as") . $derivateHierarchy->taxonName . "<br>";
170
                    }
171
                    // - prtologue
172
                    if ($derivateHierarchy->protologue) {
173
                        //$detail_html .= "<strong>Protologue:</strong> " . $derivateHierarchy->protologue . "<br>"
174
                    }
175
                    // - citation
176
                    if ($derivateHierarchy->citation) {
177
                        $detail_html .= create_label("Citation") . $derivateHierarchy->citation . "<br>";
178
                    }
179
                    //assemble specimen details
180
                    if($derivateHierarchy->preservedSpecimenDTOs){
181
                        foreach($derivateHierarchy->preservedSpecimenDTOs as $preservedSpecimenDTO) {
182
                            $detail_html .= "<br>";
183
                            $detail_html .= compose_cdm_specimen_page_html($preservedSpecimenDTO);
184
                        }
185
                    }
186
                    $detail_html .= "<br>";
187
                    //detail row resp. one BIG detail cell
188
                    $rows[] = array(
189
                        'data' => array(
190
                            array(
191
                                'data' => "", //empty first column
192
                                'class' => array('expand_column')
193
                            ),
194
                            array(
195
                                'data' => $detail_html,
196
                                'colspan' => 7,
197
                            ),
198
                        ),
199
                        'id' => ('derivate_details' . $rowcount),//details row ID
200
                        'class' => array('detail_row'),
201
                    );
202
                    $rowcount++;
203
                }
204
            }
205
        }
206

    
207
        $tableId = "derivate_hierarchy_table";
208
        $derivateHierarchyTable = array(
209
            "#theme" => "table",
210
            "#weight" => 2,
211
            "#header" => array(
212
                array(
213
                    'data' => "",
214
                    'class' => array('expand_column')
215
                ),
216
                "Country", "Date", "Collector + collecting number", "Herbaria", "Type", "Scan", "Derivatives"),
217
            "#rows" => $rows,
218
            "#attributes" => array(
219
                "id" => $tableId,
220
                "border" => 2
221
            )
222
        );
223

    
224
        //add toggle functionality to derivate hierarchy table
225
        drupal_add_js_rowToggle("#".$tableId);
226

    
227
        $render_array['derivate_hierarchy_table'] = $derivateHierarchyTable;
228
        $render_array['pager'] = markup_to_render_array(
229
            theme('cdm_pager', array(
230
                'pager' => $pagerFieldUnits,
231
                'path' => $_REQUEST['q'],
232
                'parameters' => $_REQUEST
233
            )),
234
            10 // weight
235
        );
236
    }
237
    else{
238
        //BOTTOM-UP-SPECIMEN-TABLE
239
        // --- generate the specimen list as table
240
        $specimen_table = array(
241
            '#theme' => 'table',
242
            '#weight' => 2,
243
            // prefix attributes and rows with '#' to let it pass to the theme function,
244
            // otherwise it is handled as child render array
245
            '#attributes' => array('class' => 'specimens'),
246
            '#rows' => array(),
247
        );
248

    
249
        if ($specimensOrObservations) {
250

    
251
            foreach ($specimensOrObservations as $specimenOrObservation) {
252

    
253
                $mediaList = array();
254
                if (is_array($specimenOrObservation->_fieldObjectMedia)) {
255
                    $mediaList = array_merge($mediaList, $specimenOrObservation->_fieldObjectMedia);
256
                }
257
                if (is_array($specimenOrObservation->_derivedUnitMedia)) {
258
                    $mediaList = array_merge($mediaList, $specimenOrObservation->_derivedUnitMedia);
259
                }
260

    
261
                // typelabel will contain the typeStatus
262
                $type_label = '';
263
                $typeDesignationPager = cdm_ws_get(CDM_WS_OCCURRENCE . '/$0/specimenTypeDesignations', $specimenOrObservation->uuid);
264
                if (isset($typeDesignationPager) and isset($typeDesignationPager->records)) {
265
                    $type_status = array();
266
                    foreach ($typeDesignationPager->records as $typeDesignation) {
267
                        if (isset($typeDesignation->typeStatus->representation_L10n)){
268
                            $type_status[] = $typeDesignation->typeStatus->representation_L10n;
269
                        }
270
                    }
271
                    $type_label = implode(', ', $type_status);
272
                    if($type_label){
273
                        $type_label .= ': ' ;
274
                    }
275
                }
276

    
277
                // --- Specimen entry as dynamic label:
278
                //     -> Dynabox for the specimenOrObservation
279
                $gallery_name = $specimenOrObservation->uuid;
280

    
281
                $derived_unit_ws_request = cdm_compose_url(CDM_WS_OCCURRENCE, array( $specimenOrObservation->uuid));
282
                $label_html = cdm_dynabox(
283
                    $specimenOrObservation->uuid,
284
                    $type_label . $specimenOrObservation->titleCache,
285
                    $derived_unit_ws_request,
286
                    'cdm_specimenOrObservation', // the theme or compose function to use
287
                    'Click for details',
288
                    array('div','div'),
289
                    array(),
290
                    'function(){ jQuery(\'#media_gallery_' . $gallery_name . '\').hide(); }', // open_callback
291
                    'function(){ jQuery(\'#media_gallery_' . $gallery_name . '\').show(); }' // close_callback
292
                );
293

    
294
                // --- Render associated media.
295
                $gallery_html = '';
296
                if (count($mediaList) > 0) {
297
                    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
298
                    $captionElements = array(
299
                        '#uri' => t('open media'),
300
                    );
301

    
302
                    $gallery_html = theme('cdm_media_gallerie', array(
303
                        'mediaList' => $mediaList,
304
                        'galleryName' => $gallery_name,
305
                        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
306
                        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
307
                        'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
308
                        'captionElements' => $captionElements,
309
                        'mediaLinkType' => 'LIGHTBOX',
310
                        'alternativeMediaUri' => NULL,
311
                        'galleryLinkUri' => NULL,
312
                    ));
313
                }
314

    
315
                $specimen_table['#rows'][] = array(
316
                    // An array of table rows. Every row is an array of cells, or an associative array
317
                    'data' => array(
318
                        // Each cell can be either a string or an associative array
319
                        $label_html . $gallery_html
320
                    ),
321
                    'class' =>  array(
322
                        'descriptionElement',
323
                        'descriptionElement_IndividualsAssociation'
324
                    ),
325
                );
326
            }
327
        }
328

    
329
        $render_array['specimen_list'] = $specimen_table;
330
        $render_array['pager'] = markup_to_render_array(
331
            theme('cdm_pager', array(
332
                'pager' => $pager,
333
                'path' => $_REQUEST['q'],
334
                'parameters' => $_REQUEST,
335
            )),
336
            10 // weight
337
        );
338
    }
339
    RenderHints::popFromRenderStack();
340
    return $render_array;
341
}
342
 
343
function create_html_link($link){
344
    $html = "";
345
    if($link->uri!=""){
346
        $html .= '<a href="' . $link->uri . '">' . $link->linkText . "</a>";
347
    }
348
    else{
349
        $html .= $link->linkText;
350
    }
351
    return $html;
352
}
353

    
354
/**
355
 * Creates HTML links from the given link list concatenated by a white space.
356
 * @param $linkList the list with Link objects having "uri" and "linkText" as members
357
 * @return string the assembled HTML string containing the links
358
 */
359
function create_html_links($linkList)
360
{
361
    $html = "";
362
    if ($linkList) {
363
        foreach ($linkList as $link) {
364
            $html .= create_html_link($link)." ";
365
        }
366
    }
367
    return $html;
368
}
369

    
370
/**
371
 * Creates HTML links from the given pair list concatenated by a white space.
372
 * @param $linkTextPairList the list with pairs which hold the uri as "first" and uriText as "second"
373
 * @return string the assembled HTML string containing the links
374
 */
375
function create_links($linkTextPairList)
376
{
377
    $html = "";
378
    if ($linkTextPairList) {
379
        foreach ($linkTextPairList as $linkTextPair) {
380
            if($linkTextPair->first!=""){
381
            $html .= '<a href="' . $linkTextPair->first . '">' . $linkTextPair->second . ", </a>";
382
        	}
383
            else{
384
                $html .= $linkTextPair->second.", ";
385
            }
386
        }
387
    }
388
    $html = rtrim($html, ", ");
389
    return $html;
390
}
391
/**
392
 * Composes a taxon page which can consist of multiple parts like
393
 * 'General', 'Synonymy', 'Images', 'Keys'. These parts can be displayed
394
 * as tabs or as sections of a single page.
395
 *
396
 * It is headed by the name of the accepted taxon without author and reference.
397
 *
398
 * @param $taxon
399
 *   The CDM Taxon Instance to compose the page for.
400
 * @param $page_part
401
 *   Name of the part to display, valid values are:
402
 *    - 'description' -  for the general part
403
 *    - 'images'
404
 *    - 'synonymy'
405
 *    - 'keys'
406
 *    - 'all'
407
 *
408
 * @return array
409
 *   A drupal render array
410
 *
411
 * @ingroup compose
412
 */
413
function compose_cdm_taxon_page($taxon, $page_part = 'description') {
414

    
415
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
416
  $http_request_params = drupal_get_query_parameters();
417

    
418
  // add all mandatory js sources
419
  _add_js_footnotes();
420

    
421

    
422
  $render_array = array();
423
  $weight = 0; // the weight for the render array elements
424

    
425
  $tabsToDisplay = variable_get('cdm_taxonpage_tabs_visibility', unserialize(TAXONPAGE_VISIBILITY_OPTIONS_DEFAULT));
426

    
427
  $page_part = variable_get('cdm_dataportal_taxonpage_tabs', 1) ? $page_part : 'all';
428

    
429
  $media = _load_media_for_taxon($taxon);
430

    
431

    
432
  if (!isset($media[0]) || ($tabsToDisplay["Images"] == '0')) {
433
    taxon_page_tabs_hidden('images');
434
  }
435

    
436
  // --- GET specimensOrObservations --- //
437
  $specimensOrObservations = cdm_ws_get(CDM_WS_TAXON, array( $taxon->uuid, 'specimensOrObservations'));
438

    
439
  $specimensOrObservationsCount = is_array($specimensOrObservations) ? count($specimensOrObservations) : 0;
440
  if ($specimensOrObservationsCount == 0 || ($tabsToDisplay["Specimens"] == '0')) {
441
    taxon_page_tabs_hidden('specimens');
442
  }
443

    
444
  // --- GET polytomousKeys --- //
445
  $polytomousKeysPager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, NULL, "findByTaxonomicScope=$taxon->uuid");
446
  $identificationKeyCount = 0;
447
  if ($polytomousKeysPager) {
448
    $identificationKeyCount += $polytomousKeysPager->count;
449
  }
450
  if ($identificationKeyCount == 0 || ($tabsToDisplay["Keys"] == '0')) {
451
    taxon_page_tabs_hidden('keys');
452
  }
453

    
454
    // --- GET TaxonNodeAgentRelations --- //
455
    $current_classification_uuid = get_current_classification_uuid();
456
    $taxon_node_agent_relations_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
457
        array(
458
            $taxon->uuid,
459
            $current_classification_uuid,
460
        ),
461
        "pageSize=1&pageIndex=0"// we are only interested into the count so we are fetching only one item, o is not possible!
462
    );
463
    if (!$taxon_node_agent_relations_pager || $taxon_node_agent_relations_pager->count == 0){
464
        taxon_page_tabs_hidden('experts');
465
    }
466

    
467
    if ($tabsToDisplay["Synonymy"] == '0') {
468
    taxon_page_tabs_hidden('synonymy');
469
  }
470

    
471
  // -------------------------------------------- //
472

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

    
475
  if (variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR) && isset($_REQUEST['acceptedFor'])) {
476
    $render_array['accepted_for'] = markup_to_render_array(theme('cdm_acceptedFor', array('acceptedFor' => $_REQUEST['acceptedFor'])), $weight++);
477
  }
478

    
479
  // --- PAGE PART: DESCRIPTION --- //
480
  if ($page_part == 'description' || $page_part == 'all') {
481

    
482
    $merged_tree = merged_taxon_feature_tree($taxon);
483

    
484

    
485
    $render_array['general'] = compose_cdm_taxon_page_profile($taxon, $merged_tree, $media);
486
    $render_array['general']['#weight'] = $weight++;
487
    $render_array['general']['#prefix'] = '<div id="general" class="page-part">';
488
    $render_array['general']['#suffix'] = '</div>';
489
  }
490

    
491
  // --- PAGE PART: IMAGES --- //
492
  if (array_search('images', taxon_page_tabs_hidden()) === FALSE && ($page_part == 'images' || $page_part == 'all')) {
493
    $images_html = '<div id="images" class="page-part">';
494
    if ($page_part == 'all') {
495
      $images_html .= '<h2>' . t('Images') . '</h2>';
496
    }
497
    // Get the image gallery as configured by the admin.
498
    $default_image_gallery = 'default';
499
    $configured_image_gallery_viewer = variable_get('image_gallery_viewer', 'default');
500
    if($configured_image_gallery_viewer == $default_image_gallery || $configured_image_gallery_viewer == 'fsi'){
501
      // the fsi_gallery requires a flash plugin, in case the client browser is not supporting
502
      // flash we also need to provide an the default gallery as alternative
503
      $taxon_image_gallery = '<div id="default-gallery-viewer">' .
504
        call_user_func_array('taxon_image_gallery_' . $default_image_gallery, array(
505
          $taxon,
506
          $media,
507
        ))
508
        . '</div>';
509
    }
510
    if($configured_image_gallery_viewer == 'fsi'){
511
      $taxon_image_gallery .= '<div id="'. $configured_image_gallery_viewer .'-gallery-viewer">' .
512
        call_user_func_array('taxon_image_gallery_' . $configured_image_gallery_viewer, array(
513
          $taxon,
514
          $media,
515
        ))
516
       . '</div>';
517
    }
518
    $images_html .= $taxon_image_gallery;
519
    $images_html .= '</div>';
520
    $render_array['images'] = markup_to_render_array($images_html, $weight++);
521
  }
522

    
523
  // --- PAGE PART: SYNONYMY --- //
524
  if (($page_part == 'synonymy' || $page_part == 'all')) {
525
    $synonymy_html = '<div id="synonymy" class="page-part">';
526
    if ($page_part == 'all') {
527
      $synonymy_html .= '<h2>' . t('Synonymy') . '</h2>';
528
    }
529
    $addAcceptedTaxon = variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE);
530

    
531
    $synonymy_html .= theme('cdm_taxon_page_synonymy', array('taxon' => $taxon, 'addAcceptedTaxon' => $addAcceptedTaxon));
532

    
533
    $synonymy_html .= '</div>';
534
    $render_array['synonymy'] = markup_to_render_array($synonymy_html, $weight++);
535

    
536
  }
537

    
538
  // --- PAGE PART: SPECIMENS --- //
539
  if ($specimensOrObservationsCount > 0 && ($page_part == 'specimens' || $page_part == 'all')) {
540
    $render_array['specimens'] = array(
541
        '#prefix' => '<div id="specimens" class="page-part">' . ($page_part == 'all' ? '<h2>' . t('Specimens') . '</h2>' : ''),
542
        'content' => cdm_dataportal_taxon_page_specimens($taxon), // returns render array
543
        '#suffix' => '</div>',
544
    );
545
  }
546

    
547
  // --- PAGE PART: KEYS --- //
548
  if ($identificationKeyCount == 1 && $page_part == 'keys'){
549
    drupal_goto(path_to_key($polytomousKeysPager->records[0]->class, $polytomousKeysPager->records[0]->uuid));
550
  }
551
  else if ($identificationKeyCount > 0 && ($page_part == 'keys' || $page_part == 'all')) {
552
    $keys_html = '<div id="keys" class="page-part">';
553
    if ($page_part == 'all') {
554
      $keys_html .= '<h2>' . t('Keys') . '</h2>';
555
    }
556
    $keys_html .= theme('cdm_block_IdentificationKeys', array('taxonUuid' => $taxon->uuid));
557
    $keys_html .= '</div>';
558
    $render_array['keys'] = markup_to_render_array($keys_html, $weight++);
559
  }
560

    
561
  // --- PAGE PART: EXPERTS --- //
562

    
563
  if (array_search('experts', taxon_page_tabs_hidden()) === FALSE && ($page_part == 'experts' || $page_part == 'all')) {
564
    $render_array['experts'] = array(
565
        '#prefix' => '<div id="experts" class="page-part">' . ($page_part == 'all' ? '<h2>' . t('Experts') . '</h2>' : ''),
566
        'content' => compose_cdm_taxon_page_experts($taxon), // returns render array
567
        '#suffix' => '</div>',
568
    );
569
  }
570

    
571
  // ------------------ END OF PARTS -------------- //
572

    
573
  // adjust weights of page and toc elements according to the settings
574
  $taxontabs_weights = get_array_variable_merged('cdm_taxonpage_tabs_weight', CDM_TAXONPAGE_TAB_WEIGHT_DEFAULT);
575
  foreach($taxontabs_weights as $tab_key => $weight){
576
    if(isset($render_array[$tab_key])){
577
      $render_array[$tab_key]['#weight'] = $weight;
578
    }
579
  }
580

    
581

    
582
  // set up the TOC for the pages which contain all pageparts
583
  if($page_part == 'all') {
584

    
585
    asort($taxontabs_weights);
586
    foreach(array_keys($taxontabs_weights) as $tab_key){
587
      if(isset($render_array[$tab_key])){
588
        if($tab_key != 'general'){
589
          // add entry for page part
590
          $toc_elements[] = array(
591
              'data' => l(t(ucfirst($tab_key)), $_GET['q'], array('fragment' => $tab_key, 'query' => $http_request_params)),
592
              'class' => array('page-part-toc-item-' . $tab_key)
593
          );
594
        } else {
595
          // add content of profile part instead
596
          if(isset($render_array['general'])) {
597
            // in case all tabs are shown at once the feature tocs
598
            // should be integrated into the tabs toc as sub list
599
            // and the profile image should be on top of the page
600
            if(isset($render_array['general']['taxon_description_feature_toc'])){;
601
            foreach ($render_array['general']['taxon_description_feature_toc']['#items'] as $profile_toc_item){
602
              $toc_elements[] = $profile_toc_item;
603
            }
604
            unset($render_array['general']['taxon_description_feature_toc']);
605
            }
606
          }
607
        }
608
      }
609
    }
610

    
611
    // move profile image in page structure
612
    if(isset($render_array['general']['taxon_profile_image'])){
613
      $render_array['profile_image'] = $render_array['general']['taxon_profile_image'];
614
      $render_array['profile_image']['#weight'] = -100;
615
      unset($render_array['general']['taxon_profile_image']);
616
    }
617

    
618
    // finally add the table of contents to the render array
619
    $render_array['toc'] = array(
620
        '#theme' => 'item_list',
621
        '#items' => $toc_elements,
622
        '#title' => t('Content'),
623
        '#weight' => -101,
624
        '#suffix' => '</div>',
625
        '#prefix'=> '<div id="page-toc">'
626
    );
627
  }
628

    
629

    
630
  return $render_array;
631
}
632

    
633
/**
634
 * TODO should this function really be a compose function?
635
 *     For a compose function must there always be a theme function with the same name? (ak 8.8.2013)
636
 *
637
 * composes and returns an render array containing the components of the taxon profile tab:
638
 *  - 'taxon_profile_image'
639
 *  - 'taxon_description_feature_toc'
640
 *  - 'taxon_description_features'
641
 *
642
 *
643
 * @param taxon
644
 * @param mergedTrees
645
 * @param media
646
 *
647
 * @return array
648
 *   A Drupal render array with the following elements:
649
 *     - 'taxon_profile_image'
650
 *     - 'taxon_description_feature_toc'
651
 *     - 'taxon_description_features'
652
 *
653
 * @ingroup compose
654
 */
655
function compose_cdm_taxon_page_profile($taxon, $merged_tree, $media) {
656

    
657
  $render_array = array();
658

    
659
  $taxon_profile_image_settings = variable_get(CDM_TAXON_PROFILE_IMAGE, unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT));
660

    
661
  $hide_taxon_profile_image = FALSE;
662
  if (variable_get('image_hide_rank', '0') != '0' && isset($taxon->name->rank->uuid)) {
663
    $rankCompare = rank_compare($taxon->name->rank->uuid, variable_get('image_hide_rank', '-99'));
664
    $hide_taxon_profile_image = ($rankCompare > -1);
665
  }
666

    
667
  if ($taxon_profile_image_settings['show'] && !$hide_taxon_profile_image) {
668

    
669
    $representationPart = new stdClass();
670
    $attributes = array();
671
    if (isset($media[0]->representations[0]->parts[0])) {
672
      $representationPart = $media[0]->representations[0]->parts[0];
673
      $attributes['alt'] = $representationPart->uri;
674

    
675
      if (!empty($taxon_profile_image_settings['media_uri_query'])) {
676
        $representationPart->uri = $representationPart->uri
677
          . (strpos($representationPart->uri, '?') !== FALSE ? '&' : '?')
678
          . $taxon_profile_image_settings['media_uri_query'];
679
      }
680
    }
681
    else {
682
      if ($taxon_profile_image_settings['custom_placeholder_enabled']) {
683
        // show placeholder image instead
684
        if (!empty($taxon_profile_image_settings['custom_placeholder_image_on']) && !empty($taxon_profile_image_settings['custom_placeholder_image_fid'])) {
685
          // use the user provided image
686
          $profile_image_file = file_load($taxon_profile_image_settings['custom_placeholder_image_fid']);
687
          $url = file_create_url($profile_image_file->uri);
688
          $image_info = image_get_info($profile_image_file->uri);
689
          $representationPart->width = $image_info['width'];
690
          $representationPart->height = $image_info['height'];
691
          $representationPart->uri = $url;
692
        }
693
        else {
694
          // use the hard coded default
695
          $representationPart->width = 184;
696
          $representationPart->height = 144;
697
          $representationPart->uri = base_path() . drupal_get_path('module',
698
              'cdm_dataportal') . '/images/no_picture.png';
699
        }
700
        $attributes['alt'] = "no image available";
701
      }
702
    }
703

    
704
    if (isset($representationPart->uri)) {
705
      $profile_image = theme('cdm_media_gallerie_image', array(
706
        'mediaRepresentationPart' => $representationPart,
707
        'maxExtend' => $taxon_profile_image_settings['maxextend'],
708
        'addPassePartout' => FALSE,
709
        'attributes' => $attributes,
710
      ));
711
      // NOTE: style="width:${maxextend}px' is needed for IE8 !!!
712
      $render_array['taxon_profile_image'] = markup_to_render_array('<div id="taxonProfileImage" style="width:' . $taxon_profile_image_settings['maxextend'] . 'px">' . $profile_image . '</div>',
713
        -101);
714
    }
715
  }
716

    
717

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

    
721
  $bibliography_settings = get_bibliography_settings();
722
  if($bibliography_settings['enabled'] == 1){
723
    $bibliography_block = feature_block(t('Bibliography'));
724
    $bibliography_block->content = FootnoteManager::renderFootnoteList('BIBLIOGRAPHY', '');
725

    
726
    if(!empty($bibliography_block->content)){
727
      $render_array['taxon_description_bibliography'] =  markup_to_render_array(
728
        theme('block',
729
          array(
730
            'elements' => array(
731
              '#block' => $bibliography_block,
732
              '#children' => $bibliography_block->content,
733
            )
734
          )
735
        ),
736
        100 // weight
737
      );
738
      cdm_toc_list_add_item('Bibliography', 'bibliography');
739
    }
740

    
741
  }
742

    
743
  // create the table of content
744
  $toc = array(
745
      '#theme' => 'item_list',
746
    '#items' => cdm_toc_list(),
747
      '#title' => t('Content'),
748
    '#weight' => -100,                  // move to the top
749
      '#suffix' => '</div>',
750
      '#prefix'=> '<div id="page-toc">'
751
  );
752
  $render_array['taxon_description_feature_toc'] = $toc;
753

    
754
  return $render_array;
755
}
756

    
757
function compose_cdm_taxon_page_experts($taxon){
758

    
759
  $render_array = array();
760
  if(!isset($taxon->uuid)){
761
    return $render_array;
762
  }
763

    
764
  $current_classification_uuid = get_current_classification_uuid();
765
  // TODO use cdm_ws_fetchall below but this failes! needs fix!
766
  $taxon_node_agent_relations = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
767
    array(
768
      $taxon->uuid,
769
      $current_classification_uuid
770
    )
771
  );
772

    
773

    
774
  $header = array(
775
    array('data' => t('Expert')),
776
    array('data' => t('Role'))
777
  );
778
  $rows = array();
779

    
780

    
781
  foreach($taxon_node_agent_relations->records as $taxon_node_agent_relation){
782
    $rows[] = array(
783

    
784
        'data' => array(
785
          $taxon_node_agent_relation->agent->titleCache,
786
          $taxon_node_agent_relation->type->representation_L10n
787
        ),
788
        //'class' => array(html_class_attribute_ref($taxon_node_agent_relation->agent)),
789
      
790
    );
791
  }
792

    
793

    
794
  $render_array['experts_table'] = array(
795
    '#theme' => 'table',
796
    '#header' => $header,
797
    '#rows' => $rows,
798
  );
799

    
800

    
801
  return $render_array;
802
}
803

    
804

    
805
/**
806
 * Manages the tabs to be hidden in the taxon page.
807
 *
808
 * The tabs are identified by their last menu link path element:
809
 *  - description
810
 *  - synonymy
811
 *  - images
812
 *  - specimens
813
 *  - key
814
 *
815
 * Internally the tabs are stored in a static variable which is
816
 * managed by drupal_static().
817
 *
818
 * @param string $add_tab
819
 *   Optional parameter. The given string will be added to the array of tabs
820
 *
821
 * @return
822
 *   The array of tabs
823
 */
824
function taxon_page_tabs_hidden($add_tab = NULL) {
825
  $tabs = &drupal_static(__FUNCTION__);
826

    
827
  if(!isset($tabs)){
828
    $tabs = array();
829
  }
830

    
831
  if (isset($add_tab) && !array_key_exists($add_tab, $tabs)) {
832
    $tabs[] = $add_tab;
833
  }
834

    
835
  return $tabs;
836
}
837

    
838
/**
839
 * Implements the hook_preprocess_HOOK() for theme_menu_local_tasks()
840
 *
841
 *  - Removes the tabs to be hidden, @see taxon_page_tabs_hidden()
842
 *  - Renames tabs according to the settings // TODO (this will replace the theme_cdm_taxonpage_tab() function !!!)
843
 *
844
 * @param array $variables
845
 *   The variables array
846
 */
847
function cdm_dataportal_preprocess_menu_local_tasks(&$variables) {
848

    
849
  $hidden_tabs = taxon_page_tabs_hidden();
850

    
851
  if (is_array($variables['primary'])) {
852
    foreach ($variables['primary'] as $key => &$element) {
853

    
854
      // 1. Remove the tabs to be hidden
855
      foreach ($hidden_tabs as $tab) {
856
        if ($element['#link']['path'] == 'cdm_dataportal/taxon/%/' . $tab) {
857
          // remove the tab
858
          unset($variables['primary'][$key]);
859
        }
860
      }
861
    }
862
  }
863
}
864

    
865

    
866

    
867
/**
868
 * Implements the hook_preprocess_HOOK() for theme_menu_local_task()
869
 *
870
 *
871
 * @param array $variables
872
 *   An associative array containing:
873
 *     - element: A render element containing:
874
 *          #link: A menu link array with 'title', 'href', and 'localized_options' keys.
875
 *          #active: A boolean indicating whether the local task is active.
876
 *
877
 */
878
function cdm_dataportal_preprocess_menu_local_task(&$variables) {
879

    
880
  $link = $variables['element']['#link'];
881
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
882
    $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']) . '"/>';
883
    $link['localized_options']['html'] = TRUE;
884

    
885
    $variables['element']['#link'] = $link;
886
  }
887
}
(7-7/8)