Project

General

Profile

Download (38.4 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 = font_awesome_icon_markup(
112
            'fa-plus-square-o',
113
            array(
114
                'alt'=>'Show details',
115
                'class' => array('expand_icon')
116
            )
117
        );
118
        $collapse_icon = font_awesome_icon_markup(
119
            'fa-minus-square-o',
120
            array(
121
                'alt'=>'Show details',
122
                'class' => array('collapse_icon')
123
            )
124
        );
125
        $detail_image_icon = '<img title="Detail Image" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/detail_image_derivate-16x16-32.png' . '"/>';
126
        $checked_box_icon = '<img src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/step_done.gif' . '"/>';
127
        $sequence_icon = '<img title="Molecular Data" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/sequence_derivate-16x16-32.png' . '"/>';
128
        $character_data_icon = '<img title="Character Data" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/character_data_derivate-16x16-32.png' . '"/>';
129
        if (isset($pagerFieldUnits->records[0])) {
130
            $rowcount = 0;
131
            foreach ($pagerFieldUnits->records as $fieldUnit) {
132
                //get derivate hierarchy for the FieldUnit
133
                $derivateHierarchy = cdm_ws_get(CDM_WS_PORTAL_OCCURRENCE, array($fieldUnit->uuid, 'derivateHierarchy'), 'taxonUuid=' . $taxon->uuid);
134
                if($derivateHierarchy){
135
                    //summary row
136
                    $rows[] = array(
137
                        'data' => array(
138
                            array(
139
                                'data' => $expand_icon . $collapse_icon,
140
                                'class' => array('summary_row_cell', 'summary_row_icon', 'expand_column')
141
                            ),
142
                            array(
143
                                'data' => $derivateHierarchy->country,
144
                                'class' => array('summary_row_cell')
145
                            ),
146
                            array(
147
                                'data' => $derivateHierarchy->date,
148
                                'class' => array('summary_row_cell')
149
                            ),
150
                            array(
151
                                'data' => $derivateHierarchy->collection,
152
                                'class' => array('summary_row_cell')
153
                            ),
154
                            array(
155
                                'data' => $derivateHierarchy->herbarium,
156
                                'class' => array('summary_row_cell')
157
                            ),
158
                            array(
159
                                'data' => $derivateHierarchy->hasType? $checked_box_icon:"",
160
                                'class' => array('summary_row_cell', 'summary_row_icon')
161
                            ),
162
                            array(
163
                                'data' => $derivateHierarchy->hasSpecimenScan? $checked_box_icon:"",
164
                                'class' => array('summary_row_cell', 'summary_row_icon')
165
                            ),
166
                            array(
167
                                'data' => ($derivateHierarchy->hasDna? $sequence_icon :"")." "
168
                                    .($derivateHierarchy->hasDetailImage?$detail_image_icon:"")." "
169
                                    .($derivateHierarchy->hasCharacterData?$character_data_icon:""),
170
                                'class' => array('summary_row_cell', 'summary_row_icon')
171
                            )
172
                        ),
173
                        'id' => ('derivate_summary' . $rowcount), // summary row id
174
                        'class' => array('summary_row'),
175
                    );
176

    
177
                    //assemble field unit details
178
                    $detail_html = "";
179
                    // - taxon name
180
                    if (variable_get('cdm_dataportal_compressed_specimen_derivate_table_show_determined_as') && $derivateHierarchy->taxonName) {
181
                        $detail_html .= create_label("Associated with") . $derivateHierarchy->taxonName . "<br>";
182
                    }
183
                    // - prtologue
184
                    if ($derivateHierarchy->protologue) {
185
                        //$detail_html .= "<strong>Protologue:</strong> " . $derivateHierarchy->protologue . "<br>"
186
                    }
187
                    // - citation
188
                    if ($derivateHierarchy->citation) {
189
                        $detail_html .= create_label("Citation") . $derivateHierarchy->citation . "<br>";
190
                    }
191
                    //assemble specimen details
192
                    if($derivateHierarchy->preservedSpecimenDTOs){
193
                        foreach($derivateHierarchy->preservedSpecimenDTOs as $preservedSpecimenDTO) {
194
                            $detail_html .= "<br>";
195
                            $detail_html .= compose_cdm_specimen_page_html($preservedSpecimenDTO);
196
                        }
197
                    }
198
                    $detail_html .= "<br>";
199
                    //detail row resp. one BIG detail cell
200
                    $rows[] = array(
201
                        'data' => array(
202
                            array(
203
                                'data' => "", //empty first column
204
                                'class' => array('expand_column')
205
                            ),
206
                            array(
207
                                'data' => $detail_html,
208
                                'colspan' => 7,
209
                            ),
210
                        ),
211
                        'id' => ('derivate_details' . $rowcount),//details row ID
212
                        'class' => array('detail_row'),
213
                    );
214
                    $rowcount++;
215
                }
216
            }
217
        }
218

    
219
        $tableId = "derivate_hierarchy_table";
220
        $derivateHierarchyTable = array(
221
            "#theme" => "table",
222
            "#weight" => 2,
223
            "#header" => array(
224
                array(
225
                    'data' => "",
226
                    'class' => array('expand_column')
227
                ),
228
                "Country", "Date", "Collector + collecting number", "Herbaria", "Type", "Scan", "Derivatives"),
229
            "#rows" => $rows,
230
            "#attributes" => array(
231
                "id" => $tableId,
232
                "border" => 2
233
            )
234
        );
235

    
236
        //add toggle functionality to derivate hierarchy table
237
        drupal_add_js_rowToggle("#".$tableId);
238

    
239
        $render_array['derivate_hierarchy_table'] = $derivateHierarchyTable;
240
        $render_array['pager'] = markup_to_render_array(
241
            theme('cdm_pager', array(
242
                'pager' => $pagerFieldUnits,
243
                'path' => $_REQUEST['q'],
244
                'parameters' => $_REQUEST
245
            )),
246
            10 // weight
247
        );
248
    }
249
    else{
250
        //BOTTOM-UP-SPECIMEN-TABLE
251
        // --- generate the specimen list as table
252
        $specimen_table = array(
253
            '#theme' => 'table',
254
            '#weight' => 2,
255
            // prefix attributes and rows with '#' to let it pass to the theme function,
256
            // otherwise it is handled as child render array
257
            '#attributes' => array('class' => 'specimens'),
258
            '#rows' => array(),
259
        );
260

    
261
        if ($specimensOrObservations) {
262

    
263
            foreach ($specimensOrObservations as $specimenOrObservation) {
264

    
265
                $mediaList = array();
266
                if (is_array($specimenOrObservation->_fieldObjectMedia)) {
267
                    $mediaList = array_merge($mediaList, $specimenOrObservation->_fieldObjectMedia);
268
                }
269
                if (is_array($specimenOrObservation->_derivedUnitMedia)) {
270
                    $mediaList = array_merge($mediaList, $specimenOrObservation->_derivedUnitMedia);
271
                }
272

    
273
                // typelabel will contain the typeStatus
274
                $type_label = '';
275
                $typeDesignationPager = cdm_ws_get(CDM_WS_OCCURRENCE . '/$0/specimenTypeDesignations', $specimenOrObservation->uuid);
276
                if (isset($typeDesignationPager) and isset($typeDesignationPager->records)) {
277
                    $type_status = array();
278
                    foreach ($typeDesignationPager->records as $typeDesignation) {
279
                        if (isset($typeDesignation->typeStatus->representation_L10n)){
280
                            $type_status[] = $typeDesignation->typeStatus->representation_L10n;
281
                        }
282
                    }
283
                    $type_label = implode(', ', $type_status);
284
                    if($type_label){
285
                        $type_label .= ': ' ;
286
                    }
287
                }
288

    
289
                // --- Specimen entry as dynamic label:
290
                //     -> Dynabox for the specimenOrObservation
291
                $gallery_name = $specimenOrObservation->uuid;
292

    
293
                $derived_unit_ws_request = cdm_compose_url(CDM_WS_OCCURRENCE, array( $specimenOrObservation->uuid));
294
                $label_html = cdm_dynabox(
295
                  $specimenOrObservation->uuid,
296
                  $type_label . $specimenOrObservation->titleCache,
297
                  $derived_unit_ws_request,
298
                  'cdm_specimenOrObservation',
299
                  'Click for details',
300
                  array('div', 'div'),
301
                  array(),
302
                  null, // $content_element_selector
303
                  'function(){ jQuery(\'#media_gallery_' . $gallery_name . '\').hide(); }', // open_callback
304
                  'function(){ jQuery(\'#media_gallery_' . $gallery_name . '\').show(); }' // close_callback
305
                );
306

    
307
                // --- Render associated media.
308
                $gallery_html = '';
309
                if (count($mediaList) > 0) {
310
                    $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SPECIMEN_GALLERY_NAME);
311
                    $captionElements = array(
312
                        '#uri' => t('open media'),
313
                    );
314

    
315
                    $gallery_html = theme('cdm_media_gallerie', array(
316
                        'mediaList' => $mediaList,
317
                        'galleryName' => $gallery_name,
318
                        'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
319
                        'cols' => $gallery_settings['cdm_dataportal_media_cols'],
320
                        'maxRows' => isset($gallery_settings['cdm_dataportal_media_maxRows']) ? isset($gallery_settings['cdm_dataportal_media_maxRows']) : null,
321
                        'captionElements' => $captionElements,
322
                        'mediaLinkType' => 'LIGHTBOX',
323
                        'alternativeMediaUri' => NULL,
324
                        'galleryLinkUri' => NULL,
325
                    ));
326
                }
327

    
328
                $specimen_table['#rows'][] = array(
329
                    // An array of table rows. Every row is an array of cells, or an associative array
330
                    'data' => array(
331
                        // Each cell can be either a string or an associative array
332
                        $label_html . $gallery_html
333
                    ),
334
                    'class' =>  array(
335
                        'descriptionElement',
336
                        'descriptionElement_IndividualsAssociation'
337
                    ),
338
                );
339
            }
340
        }
341

    
342
        $render_array['specimen_list'] = $specimen_table;
343
        $render_array['pager'] = markup_to_render_array(
344
            theme('cdm_pager', array(
345
                'pager' => $pager,
346
                'path' => $_REQUEST['q'],
347
                'parameters' => $_REQUEST,
348
            )),
349
            10 // weight
350
        );
351
    }
352
    RenderHints::popFromRenderStack();
353
    return $render_array;
354
}
355
 
356
function create_html_link($link){
357
    $html = "";
358
    if($link->uri!=""){
359
        $html .= '<a href="' . $link->uri . '">' . $link->linkText . "</a>";
360
    }
361
    else{
362
        $html .= $link->linkText;
363
    }
364
    return $html;
365
}
366

    
367
/**
368
 * Creates HTML links from the given link list concatenated by a white space.
369
 * @param $linkList the list with Link objects having "uri" and "linkText" as members
370
 * @return string the assembled HTML string containing the links
371
 */
372
function create_html_links($linkList)
373
{
374
    $html = "";
375
    if ($linkList) {
376
        foreach ($linkList as $link) {
377
            $html .= create_html_link($link)." ";
378
        }
379
    }
380
    return $html;
381
}
382

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

    
428
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
429
  $http_request_params = drupal_get_query_parameters();
430

    
431
  // add all mandatory js sources
432
  _add_js_footnotes();
433

    
434

    
435
  $render_array = array();
436
  $weight = 0; // the weight for the render array elements
437

    
438
  $tabsToDisplay = variable_get('cdm_taxonpage_tabs_visibility', unserialize(TAXONPAGE_VISIBILITY_OPTIONS_DEFAULT));
439

    
440
  $page_part = variable_get('cdm_dataportal_taxonpage_tabs', 1) ? $page_part : 'all';
441

    
442
  $media = _load_media_for_taxon($taxon);
443

    
444

    
445
  if (!isset($media[0]) || ($tabsToDisplay["Images"] == '0')) {
446
    taxon_page_tabs_hidden('images');
447
  }
448

    
449
  // --- GET specimensOrObservations --- //
450
  $specimensOrObservations = cdm_ws_get(CDM_WS_TAXON, array( $taxon->uuid, 'specimensOrObservations'));
451

    
452
  $specimensOrObservationsCount = is_array($specimensOrObservations) ? count($specimensOrObservations) : 0;
453
  if ($specimensOrObservationsCount == 0 || ($tabsToDisplay["Specimens"] == '0')) {
454
    taxon_page_tabs_hidden('specimens');
455
  }
456

    
457
  // --- GET polytomousKeys --- //
458
  $polytomousKeysPager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, NULL, "findByTaxonomicScope=$taxon->uuid");
459
  $identificationKeyCount = 0;
460
  if ($polytomousKeysPager) {
461
    $identificationKeyCount += $polytomousKeysPager->count;
462
  }
463
  if ($identificationKeyCount == 0 || ($tabsToDisplay["Keys"] == '0')) {
464
    taxon_page_tabs_hidden('keys');
465
  }
466

    
467
    // --- GET TaxonNodeAgentRelations --- //
468
    $current_classification_uuid = get_current_classification_uuid();
469
    $taxon_node_agent_relations_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
470
        array(
471
            $taxon->uuid,
472
            $current_classification_uuid,
473
        ),
474
        "pageSize=1&pageIndex=0"// we are only interested into the count so we are fetching only one item, o is not possible!
475
    );
476
    if (!$taxon_node_agent_relations_pager || $taxon_node_agent_relations_pager->count == 0){
477
        taxon_page_tabs_hidden('experts');
478
    }
479

    
480
    if ($tabsToDisplay["Synonymy"] == '0') {
481
    taxon_page_tabs_hidden('synonymy');
482
  }
483

    
484
  // -------------------------------------------- //
485

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

    
488
  if (variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR) && isset($_REQUEST['acceptedFor'])) {
489
    $render_array['accepted_for'] = markup_to_render_array(cdm_accepted_for($_REQUEST['acceptedFor']), $weight++);
490
  }
491

    
492
  // --- PAGE PART: DESCRIPTION --- //
493
  if ($page_part == 'description' || $page_part == 'all') {
494

    
495
    $merged_tree = merged_taxon_feature_tree($taxon);
496

    
497

    
498
    $render_array['general'] = compose_cdm_taxon_page_profile($taxon, $merged_tree, $media);
499
    $render_array['general']['#weight'] = $weight++;
500
    $render_array['general']['#prefix'] = '<div id="general" class="page-part">';
501
    $render_array['general']['#suffix'] = '</div>';
502
  }
503

    
504
  // --- PAGE PART: IMAGES --- //
505
  if (array_search('images', taxon_page_tabs_hidden()) === FALSE && ($page_part == 'images' || $page_part == 'all')) {
506
    $images_html = '<div id="images" class="page-part">';
507
    if ($page_part == 'all') {
508
      $images_html .= '<h2>' . t('Images') . '</h2>';
509
    }
510
    // Get the image gallery as configured by the admin.
511
    $default_image_gallery = 'default';
512
    $configured_image_gallery_viewer = variable_get('image_gallery_viewer', 'default');
513
    if($configured_image_gallery_viewer == $default_image_gallery || $configured_image_gallery_viewer == 'fsi'){
514
      // the fsi_gallery requires a flash plugin, in case the client browser is not supporting
515
      // flash we also need to provide an the default gallery as alternative
516
      $taxon_image_gallery = '<div id="default-gallery-viewer">' .
517
        call_user_func_array('taxon_image_gallery_' . $default_image_gallery, array(
518
          $taxon,
519
          $media,
520
        ))
521
        . '</div>';
522
    }
523
    if($configured_image_gallery_viewer == 'fsi'){
524
      $taxon_image_gallery .= '<div id="'. $configured_image_gallery_viewer .'-gallery-viewer">' .
525
        call_user_func_array('taxon_image_gallery_' . $configured_image_gallery_viewer, array(
526
          $taxon,
527
          $media,
528
        ))
529
       . '</div>';
530
    }
531
    $images_html .= $taxon_image_gallery;
532
    $images_html .= '</div>';
533
    $render_array['images'] = markup_to_render_array($images_html, $weight++);
534
  }
535

    
536
  // --- PAGE PART: SYNONYMY --- //
537
  if (($page_part == 'synonymy' || $page_part == 'all')) {
538
    $synonymy_html = '<div id="synonymy" class="page-part">';
539
    if ($page_part == 'all') {
540
      $synonymy_html .= '<h2>' . t('Synonymy') . '</h2>';
541
    }
542
    $addAcceptedTaxon = variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE);
543

    
544
    $synonymy_html .= theme('cdm_taxon_page_synonymy', array('taxon' => $taxon, 'addAcceptedTaxon' => $addAcceptedTaxon));
545

    
546
    $synonymy_html .= '</div>';
547
    $render_array['synonymy'] = markup_to_render_array($synonymy_html, $weight++);
548

    
549
  }
550

    
551
  // --- PAGE PART: SPECIMENS --- //
552
  if ($specimensOrObservationsCount > 0 && ($page_part == 'specimens' || $page_part == 'all')) {
553
    $render_array['specimens'] = array(
554
        '#prefix' => '<div id="specimens" class="page-part">' . ($page_part == 'all' ? '<h2>' . t('Specimens') . '</h2>' : ''),
555
        'content' => cdm_dataportal_taxon_page_specimens($taxon), // returns render array
556
        '#suffix' => '</div>',
557
    );
558
  }
559

    
560
  // --- PAGE PART: KEYS --- //
561
  if ($identificationKeyCount == 1 && $page_part == 'keys'){
562
    drupal_goto(path_to_key($polytomousKeysPager->records[0]->class, $polytomousKeysPager->records[0]->uuid));
563
  }
564
  else if ($identificationKeyCount > 0 && ($page_part == 'keys' || $page_part == 'all')) {
565
    $keys_html = '<div id="keys" class="page-part">';
566
    if ($page_part == 'all') {
567
      $keys_html .= '<h2>' . t('Keys') . '</h2>';
568
    }
569
    $keys_html .= theme('cdm_block_IdentificationKeys', array('taxonUuid' => $taxon->uuid));
570
    $keys_html .= '</div>';
571
    $render_array['keys'] = markup_to_render_array($keys_html, $weight++);
572
  }
573

    
574
  // --- PAGE PART: EXPERTS --- //
575

    
576
  if (array_search('experts', taxon_page_tabs_hidden()) === FALSE && ($page_part == 'experts' || $page_part == 'all')) {
577
    $render_array['experts'] = array(
578
        '#prefix' => '<div id="experts" class="page-part">' . ($page_part == 'all' ? '<h2>' . t('Experts') . '</h2>' : ''),
579
        'content' => compose_cdm_taxon_page_experts($taxon), // returns render array
580
        '#suffix' => '</div>',
581
    );
582
  }
583

    
584
  // ------------------ END OF PARTS -------------- //
585

    
586
  // adjust weights of page and toc elements according to the settings
587
  $taxontabs_weights = get_array_variable_merged('cdm_taxonpage_tabs_weight', CDM_TAXONPAGE_TAB_WEIGHT_DEFAULT);
588
  foreach($taxontabs_weights as $tab_key => $weight){
589
    if(isset($render_array[$tab_key])){
590
      $render_array[$tab_key]['#weight'] = $weight;
591
    }
592
  }
593

    
594

    
595
  // set up the TOC for the pages which contain all pageparts
596
  if($page_part == 'all') {
597

    
598
    asort($taxontabs_weights);
599
    foreach(array_keys($taxontabs_weights) as $tab_key){
600
      if(isset($render_array[$tab_key])){
601
        if($tab_key != 'general'){
602
          // add entry for page part
603
          $toc_elements[] = array(
604
              'data' => l(t(ucfirst($tab_key)), $_GET['q'], array('fragment' => $tab_key, 'query' => $http_request_params)),
605
              'class' => array('page-part-toc-item-' . $tab_key)
606
          );
607
        } else {
608
          // add content of profile part instead
609
          if(isset($render_array['general'])) {
610
            // in case all tabs are shown at once the feature tocs
611
            // should be integrated into the tabs toc as sub list
612
            // and the profile image should be on top of the page
613
            if(isset($render_array['general']['taxon_description_feature_toc'])){;
614
            foreach ($render_array['general']['taxon_description_feature_toc']['#items'] as $profile_toc_item){
615
              $toc_elements[] = $profile_toc_item;
616
            }
617
            unset($render_array['general']['taxon_description_feature_toc']);
618
            }
619
          }
620
        }
621
      }
622
    }
623

    
624
    // move profile image in page structure
625
    if(isset($render_array['general']['taxon_profile_image'])){
626
      $render_array['profile_image'] = $render_array['general']['taxon_profile_image'];
627
      $render_array['profile_image']['#weight'] = -100;
628
      unset($render_array['general']['taxon_profile_image']);
629
    }
630

    
631
    // finally add the table of contents to the render array
632
    $render_array['toc'] = array(
633
        '#theme' => 'item_list',
634
        '#items' => $toc_elements,
635
        '#title' => t('Content'),
636
        '#weight' => -101,
637
        '#suffix' => '</div>',
638
        '#prefix'=> '<div id="page-toc">'
639
    );
640
  }
641

    
642

    
643
  return $render_array;
644
}
645

    
646
/**
647
 * TODO should this function really be a compose function?
648
 *     For a compose function must there always be a theme function with the same name? (ak 8.8.2013)
649
 *
650
 * composes and returns an render array containing the components of the taxon profile tab:
651
 *  - 'taxon_profile_image'
652
 *  - 'taxon_description_feature_toc'
653
 *  - 'taxon_description_features'
654
 *
655
 *
656
 * @param taxon
657
 * @param mergedTrees
658
 * @param media
659
 *
660
 * @return array
661
 *   A Drupal render array with the following elements:
662
 *     - 'taxon_profile_image'
663
 *     - 'taxon_description_feature_toc'
664
 *     - 'taxon_description_features'
665
 *
666
 * @ingroup compose
667
 */
668
function compose_cdm_taxon_page_profile($taxon, $merged_tree, $media) {
669

    
670
  $render_array = array();
671

    
672
  $taxon_profile_image_settings = variable_get(CDM_TAXON_PROFILE_IMAGE, unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT));
673

    
674
  $hide_taxon_profile_image = FALSE;
675
  if (variable_get('image_hide_rank', '0') != '0' && isset($taxon->name->rank->uuid)) {
676
    $rankCompare = rank_compare($taxon->name->rank->uuid, variable_get('image_hide_rank', '-99'));
677
    $hide_taxon_profile_image = ($rankCompare > -1);
678
  }
679

    
680
  if ($taxon_profile_image_settings['show'] && !$hide_taxon_profile_image) {
681

    
682
    $representationPart = new stdClass();
683
    $attributes = array();
684
    if (isset($media[0]->representations[0]->parts[0])) {
685
      $representationPart = $media[0]->representations[0]->parts[0];
686
      $attributes['alt'] = $representationPart->uri;
687

    
688
      if (!empty($taxon_profile_image_settings['media_uri_query'])) {
689
        $representationPart->uri = $representationPart->uri
690
          . (strpos($representationPart->uri, '?') !== FALSE ? '&' : '?')
691
          . $taxon_profile_image_settings['media_uri_query'];
692
      }
693
    }
694
    else {
695
      if ($taxon_profile_image_settings['custom_placeholder_enabled']) {
696
        // show placeholder image instead
697
        if (!empty($taxon_profile_image_settings['custom_placeholder_image_on']) && !empty($taxon_profile_image_settings['custom_placeholder_image_fid'])) {
698
          // use the user provided image
699
          $profile_image_file = file_load($taxon_profile_image_settings['custom_placeholder_image_fid']);
700
          $url = file_create_url($profile_image_file->uri);
701
          $image_info = image_get_info($profile_image_file->uri);
702
          $representationPart->width = $image_info['width'];
703
          $representationPart->height = $image_info['height'];
704
          $representationPart->uri = $url;
705
        }
706
        else {
707
          // use the hard coded default
708
          $representationPart->width = 184;
709
          $representationPart->height = 144;
710
          $representationPart->uri = base_path() . drupal_get_path('module',
711
              'cdm_dataportal') . '/images/no_picture.png';
712
        }
713
        $attributes['alt'] = "no image available";
714
      }
715
    }
716

    
717
    if (isset($representationPart->uri)) {
718
      $profile_image = theme('cdm_media_gallerie_image', array(
719
        'mediaRepresentationPart' => $representationPart,
720
        'maxExtend' => $taxon_profile_image_settings['maxextend'],
721
        'addPassePartout' => FALSE,
722
        'attributes' => $attributes,
723
      ));
724
      // NOTE: style="width:${maxextend}px' is needed for IE8 !!!
725
      $render_array['taxon_profile_image'] = markup_to_render_array('<div id="taxonProfileImage" style="width:' . $taxon_profile_image_settings['maxextend'] . 'px">' . $profile_image . '</div>',
726
        -101);
727
    }
728
  }
729

    
730

    
731
  // Render the sections for each feature
732
  $render_array['taxon_description_features'] = compose_feature_blocks($merged_tree->root->childNodes, $taxon);
733

    
734
  $bibliography_settings = get_bibliography_settings();
735
  if($bibliography_settings['enabled'] == 1){
736
    $bibliography_block = feature_block(t('Bibliography'));
737
    $bibliography_block->content = FootnoteManager::renderFootnoteList('BIBLIOGRAPHY', '');
738

    
739
    if(!empty($bibliography_block->content)){
740
      $render_array['taxon_description_bibliography'] =  markup_to_render_array(
741
        theme('block',
742
          array(
743
            'elements' => array(
744
              '#block' => $bibliography_block,
745
              '#children' => $bibliography_block->content,
746
            )
747
          )
748
        ),
749
        100 // weight
750
      );
751
      cdm_toc_list_add_item('Bibliography', 'bibliography');
752
    }
753

    
754
  }
755

    
756
  // create the table of content
757
  $toc = array(
758
      '#theme' => 'item_list',
759
    '#items' => cdm_toc_list(),
760
      '#title' => t('Content'),
761
    '#weight' => -100,                  // move to the top
762
      '#suffix' => '</div>',
763
      '#prefix'=> '<div id="page-toc">'
764
  );
765
  $render_array['taxon_description_feature_toc'] = $toc;
766

    
767
  return $render_array;
768
}
769

    
770
/**
771
 * composes and returns an render array for the experts associated with the given taxon
772
 *
773
 * @param taxon
774
 *
775
 * @return array
776
 *   A Drupal render array for a table with the experts
777
 *
778
 * @ingroup compose
779
 */
780
function compose_cdm_taxon_page_experts($taxon){
781

    
782
  $render_array = array();
783
  if(!isset($taxon->uuid)){
784
    return $render_array;
785
  }
786

    
787
  $current_classification_uuid = get_current_classification_uuid();
788
  // TODO use cdm_ws_fetchall below but this failes! needs fix!
789
  $taxon_node_agent_relations = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
790
    array(
791
      $taxon->uuid,
792
      $current_classification_uuid
793
    )
794
  );
795

    
796
  $header = array(
797
    array('data' => t('Expert')),
798
    array('data' => t('Role'))
799
  );
800
  $rows = array();
801

    
802

    
803
  foreach($taxon_node_agent_relations->records as $taxon_node_agent_relation){
804

    
805

    
806
    $expert_role_id = $taxon_node_agent_relation->agent->uuid . '-' . $taxon_node_agent_relation->type->uuid;
807
    $expert_details_container_id = 'expert_details_' . $expert_role_id;
808

    
809
    $agent_label_markup = cdm_dynabox(
810
      'expert_' . $expert_role_id,
811
      $taxon_node_agent_relation->agent->titleCache,
812
      // specifying both ends of the relationship will return only one record in the pager
813
      cdm_compose_url(CDM_WS_PORTAL_AGENT,
814
         array($taxon_node_agent_relation->agent->uuid, 'taxonNodeAgentRelations'),
815
        'taxon_uuid=' . $taxon->uuid . '&relType_uuid=' . $taxon_node_agent_relation->type->uuid),
816
      'cdm_taxon_expert',
817
      'Click for details',
818
      array('div', 'div'),
819
      array(), // attributes
820
      '#' . $expert_details_container_id // $content_element_selector
821
    );
822

    
823
    // Expert and Role
824
    $rows[] = array(
825
      'data' => array(
826
        array(
827
          'data' => $agent_label_markup,
828
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->agent))
829
        ),
830
        array(
831
          'data' => $taxon_node_agent_relation->type->representation_L10n,
832
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->type))
833
        )
834
      )
835
    );
836
    // Agent details
837
    $rows[] = array(
838
      'data' => array(
839
        array(
840
          'data' => '<!-- expert_details_container -->',
841
          'id' => $expert_details_container_id,
842
          'colspan' => 2
843
        )
844
      )
845
    );
846

    
847
  }
848

    
849

    
850
  $render_array['experts_table'] = array(
851
    '#theme' => 'table',
852
    '#header' => $header,
853
    '#rows' => $rows,
854
  );
855

    
856

    
857
  return $render_array;
858
}
859

    
860

    
861
/**
862
 * Manages the tabs to be hidden in the taxon page.
863
 *
864
 * The tabs are identified by their last menu link path element:
865
 *  - description
866
 *  - synonymy
867
 *  - images
868
 *  - specimens
869
 *  - key
870
 *
871
 * Internally the tabs are stored in a static variable which is
872
 * managed by drupal_static().
873
 *
874
 * @param string $add_tab
875
 *   Optional parameter. The given string will be added to the array of tabs
876
 *
877
 * @return
878
 *   The array of tabs
879
 */
880
function taxon_page_tabs_hidden($add_tab = NULL) {
881
  $tabs = &drupal_static(__FUNCTION__);
882

    
883
  if(!isset($tabs)){
884
    $tabs = array();
885
  }
886

    
887
  if (isset($add_tab) && !array_key_exists($add_tab, $tabs)) {
888
    $tabs[] = $add_tab;
889
  }
890

    
891
  return $tabs;
892
}
893

    
894
/**
895
 * Implements the hook_preprocess_HOOK() for theme_menu_local_tasks()
896
 *
897
 *  - Removes the tabs to be hidden, @see taxon_page_tabs_hidden()
898
 *  - Renames tabs according to the settings // TODO (this will replace the theme_cdm_taxonpage_tab() function !!!)
899
 *
900
 * @param array $variables
901
 *   The variables array
902
 */
903
function cdm_dataportal_preprocess_menu_local_tasks(&$variables) {
904

    
905
  $hidden_tabs = taxon_page_tabs_hidden();
906

    
907
  if (is_array($variables['primary'])) {
908
    foreach ($variables['primary'] as $key => &$element) {
909

    
910
      // 1. Remove the tabs to be hidden
911
      foreach ($hidden_tabs as $tab) {
912
        if ($element['#link']['path'] == 'cdm_dataportal/taxon/%/' . $tab) {
913
          // remove the tab
914
          unset($variables['primary'][$key]);
915
        }
916
      }
917
    }
918
  }
919
}
920

    
921

    
922

    
923
/**
924
 * Implements the hook_preprocess_HOOK() for theme_menu_local_task()
925
 *
926
 *
927
 * @param array $variables
928
 *   An associative array containing:
929
 *     - element: A render element containing:
930
 *          #link: A menu link array with 'title', 'href', and 'localized_options' keys.
931
 *          #active: A boolean indicating whether the local task is active.
932
 *
933
 */
934
function cdm_dataportal_preprocess_menu_local_task(&$variables) {
935

    
936
  $link = $variables['element']['#link'];
937
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
938
    $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']) . '"/>';
939
    $link['localized_options']['html'] = TRUE;
940

    
941
    $variables['element']['#link'] = $link;
942
  }
943
}
944

    
945
/* =================== block composition ===================== */
946

    
947
/**
948
 * Composes and returns an render array for the classification breadcrumbs of the given taxon.
949
 *
950
 * @param taxon
951
 *
952
 * @return array
953
 *   A Drupal render array for a table with the experts
954
 *
955
 * @ingroup compose
956
 */
957
function compose_classification_breadcrumbs($taxon_uuid) {
958

    
959
  _add_js_taxonomic_children('#classification-breadcrumbs .taxonomic-children-button');
960

    
961
  $render_array = array();
962

    
963
  $render_array['#theme'] = 'item_list';
964
  $render_array['#type'] = 'ul';
965
  $render_array['#attributes'] = array(
966
    'id' => 'classification-breadcrumbs',
967
    'class' => 'breadcrumbs inline',
968
  );
969

    
970
  $items = array();
971

    
972
  $parent_taxon_nodes = null;
973
  if($taxon_uuid){
974
    $parent_taxon_nodes = cdm_ws_taxonomy_pathFromRoot($taxon_uuid);
975
  }
976

    
977
  $classifications = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXONOMY);
978
  // find current classification in list
979
  $classification = null;
980
  $current_classification_uuid = get_current_classification_uuid();
981
  foreach ($classifications as $classification){
982
    if($classification->uuid == $current_classification_uuid){
983
      break;
984
    }
985
  }
986

    
987
  $node_name = '';
988
  if(count($classifications) > 1 ){
989
    // need to add the current classification as first label
990
    $node_name = '<span '. html_class_attribute_ref($classification) .'>'
991
      . $classification->titleCache . '</span>';
992
  }
993

    
994
  $rank_limit_uuid = variable_get(TAXONTREE_RANKLIMIT, TAXONTREE_RANKLIMIT_DEFAULT);
995

    
996
  /*
997
  if($parent_taxon_nodes) {
998
    // the path to root never contains the classification->root, by asking for
999
    // the parent node we either get this or the predecessor of the virtual root node.
1000
    $root_parent_taxon_node_dto = cdm_ws_get(CDM_WS_TAXONNODE_PARENT, array($parent_taxon_nodes[0]->uuid));
1001
  } else {
1002

    
1003

    
1004
  }*/
1005

    
1006
  $children_indicator =  '<span class="taxonomic-children-button" data-classification-uuid="'. $current_classification_uuid
1007
    . '" data-rank-limit-uuid="'. $rank_limit_uuid . '" > '
1008
    . font_awesome_icon_markup('fa-chevron-right')
1009
    . ' </span>';
1010

    
1011
  $items[] = $node_name . $children_indicator;
1012

    
1013
  if($parent_taxon_nodes){
1014
    foreach ($parent_taxon_nodes as $node){
1015

    
1016
      $is_first_item = count($items) == 0;
1017
      $node_name = cdm_dataportal_shortname_of($node);
1018
      $path = path_to_taxon($node->taxonUuid);
1019

    
1020
      $children_indicator = '';
1021
      if($node->taxonomicChildrenCount){
1022
        $children_indicator =  '<span class="taxonomic-children-button" data-cdm-taxon-uuid="'. $node->taxonUuid. '" > '
1023
          . font_awesome_icon_markup('fa-chevron-right')
1024
          . ' </span>';
1025
      }
1026

    
1027
      $items[] =
1028
        ($is_first_item ? '':  ' ')
1029
        . l($node_name, $path)
1030
        . $children_indicator;
1031
    }
1032
  }
1033

    
1034
  $render_array['#items'] = $items;
1035

    
1036
  return $render_array;
1037

    
1038
}
(7-7/10)