Project

General

Profile

Download (52.7 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
    $ws_string = 'specimensOrObservation';
54
    if (variable_get('cdm_dataportal_specimen_derivate_tree')){
55
        $ws_string .= "DTOs";
56
    }else{
57
        $ws_string .= "s";
58
    }
59

    
60
    $specimensOrObservations = cdm_ws_get(CDM_WS_TAXON, array( $taxon->uuid, $ws_string));
61
    if(!variable_get('cdm_dataportal_specimen_derivate_tree')) {
62
        $pager = cdm_ws_get(CDM_WS_OCCURRENCE_BY_ASSOCIATEDTAXON,
63
            null,
64
            $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
65
        );
66
        $specimensOrObservations = array();
67
        if (isset($pager->records[0])) {
68
            $specimensOrObservations = $pager->records;
69
        }
70
    }
71
     // cdm_ws_get(CDM_WS_OCCURRENCE_FIELDUNIT_DTO_BY_ASSOCIATEDTAXON,
72
     // null,
73
     // $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
74
    // );
75

    
76
   // $specimensOrObservations = array();
77
   // if(isset($pager->records[0])){
78
    //    $specimensOrObservations =  $pager->records;
79
   // }
80

    
81
    // order occurrences by date but types should be on top of the list
82
    $type_specimens = array();
83
    $other_occurrences = array();
84
    foreach ($specimensOrObservations as $occurrence) {
85
       $typeDesignationsPager = cdm_ws_get(CDM_WS_OCCURRENCE . '/$0/specimenTypeDesignations', $occurrence->uuid);
86
        if (isset($typeDesignationsPager->count) && $typeDesignationsPager->count > 0) {
87
            $type_specimens[] = $occurrence;
88
       } else {
89
           $other_occurrences[] = $occurrence;
90
        }
91
    }
92
    $specimensOrObservations = array_merge($type_specimens, $other_occurrences);
93

    
94
    // Collect media (fieldObjectMedia, derivedUnitMedia) and add as a custom field
95
    if (!variable_get('cdm_dataportal_specimen_derivate_tree')) {
96
        foreach ($specimensOrObservations as &$occurrence) {
97
            $occurrence->_fieldObjectMedia = cdm_ws_get(CDM_WS_DERIVEDUNIT_FACADE, array(
98
                $occurrence->uuid,
99
                'fieldObjectMediaDTO',
100
            ));
101
            $occurrence->_derivedUnitMedia = cdm_ws_get(CDM_WS_DERIVEDUNIT_FACADE, array(
102
                $occurrence->uuid,
103
                'derivedUnitMedia',
104
            ));
105
        }
106
    }
107

    
108
    // --- get map service HTTP query parameters
109

    
110
    if (count($specimensOrObservations) > 0) {
111

    
112
        $occurrence_queryDto = cdm_ws_get(CDM_WS_GEOSERVICE_OCCURRENCEMAP, $taxon->uuid, $by_associatedtaxon_query);
113

    
114
        $map_visibility = variable_get(SPECIMEN_MAP_VISIBILITY, SPECIMEN_MAP_VISIBILITY_DEFAULT);
115
        if ($map_visibility == 'always' ||
116
            variable_get(SPECIMEN_MAP_VISIBILITY, SPECIMEN_MAP_VISIBILITY_DEFAULT) == 'automatic' &&
117
            (isset($occurrence_queryDto->fieldUnitPoints[0]) || isset($occurrence_queryDto->derivedUnitPoints[0]))) {
118
            $occurrence_query = $occurrence_queryDto->occurrenceQuery;
119
            $legend_format_query = null;
120
            $distribution_query = NULL;
121
            $render_array['map'] = compose_map('specimens', $occurrence_query, $distribution_query, $legend_format_query, array());
122
        }
123
    }
124

    
125
    // -------------------------------------------------------
126

    
127

    
128

    
129
    if(variable_get('cdm_dataportal_compressed_specimen_derivate_table')){
130

    
131
        //COMPRESSED SPECIMEN DERIVATE TABLE
132
        $associatedFieldUnitsQuery_parameters = array();
133
        if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
134
            $associatedFieldUnitsQuery_parameters = array_merge($associatedFieldUnitsQuery_parameters, $_REQUEST['pager']);
135
        }
136

    
137
        $by_associatedtaxon_query = http_build_query($associatedFieldUnitsQuery_parameters);
138
        $pager_field_units = cdm_ws_get(CDM_WS_PORTAL_TAXON,
139
            array($taxon->uuid, 'associatedFieldUnits'),
140
            $by_associatedtaxon_query . '&pageSize=' . variable_get('cdm_dataportal_compressed_specimen_derivate_table_page_size')
141
        );
142

    
143
        if (isset($pager_field_units->records[0])) {
144
          $field_unit_uuids = array();
145
          foreach ($pager_field_units->records as $field_unit) {
146
            $field_unit_uuids[] = $field_unit->uuid;
147
          }
148

    
149
          $render_array['derivate_hierarchy_table'] = compose_specimen_table($field_unit_uuids);
150
        }
151

    
152
        $render_array['pager'] = markup_to_render_array(
153
            theme('cdm_pager', array(
154
                'pager' => $pager_field_units,
155
                'path' => $_REQUEST['q'],
156
                'parameters' => $_REQUEST
157
            )),
158
            10 // weight
159
        );
160
    }
161
    else if(variable_get('cdm_dataportal_specimen_derivate_tree')){
162

    
163
        $expand_icon = font_awesome_icon_markup(
164
            'fa-plus-square-o',
165
            array(
166
                'alt' => 'Show details',
167
                'class' => array('expand_icon')
168
            )
169
        );
170
        $collapse_icon = font_awesome_icon_markup(
171
            'fa-minus-square-o',
172
            array(
173
                'alt' => 'Show details',
174
                'class' => array('collapse_icon')
175
            )
176
        );
177
        $tableId = "specimen_tree_table";
178
        $specimen_table = array(
179
            '#theme' => 'table',
180
            '#weight' => 2,
181
            // prefix attributes and rows with '#' to let it pass toF the theme function,
182
            // otherwise it is handled as child render array
183
            '#attributes' => array('class' => 'specimens',
184
                                    'id' => $tableId),
185
            '#rows' => array(),
186
        );
187
        $specimen_array = compose_specimen_array($specimensOrObservations);
188
        $rowcount = 0;
189
        foreach($specimen_array as $value){
190
            $renderArray = array(
191
                '#theme' => 'item_list',
192
                '#items' => $value['children'],
193
                '#type' => 'ul');
194
            $output = drupal_render($renderArray);
195

    
196
            $specimen_table['#rows'][] = array(
197
                'data' =>array(
198
                    array(
199
                        'data' => $expand_icon . $collapse_icon,
200
                        'class' => array('summary_row_cell', 'summary_row_icon', 'expand_column')
201
                    ),
202
                    $value['data']),
203

    
204
                'id' => 'label' . $rowcount, // summary row id
205
                'class' => array('summary_row'),
206
            );
207
            $specimen_table['#rows'][] = array(
208
                'data' =>  array(
209
                        array(
210
                            'data' => "", //empty first column
211
                            'class' => array('expand_column')
212
                        ),
213
                        array(
214
                        'data' => $output
215

    
216
                        )
217
                    ),
218
                    'class' =>  array('detail_row')
219
//                'class' =>  array(
220
//                    'descriptionElement',
221
//                    'descriptionElement_IndividualsAssociation'
222
 //               ),
223
            );
224
            $rowcount++;
225

    
226
        }
227
        drupal_add_js_rowToggle("#".$tableId);
228

    
229
        $render_array['specimen_list'] = $specimen_table;
230

    
231

    
232
    }
233
    else{
234
      $specimen_table = compose_specimens_table_bottom_up($specimensOrObservations);
235

    
236
      $render_array['specimen_list'] = $specimen_table;
237
      $render_array['pager'] = markup_to_render_array(
238
        theme('cdm_pager', array(
239
          'pager' => $pager,
240
          'path' => $_REQUEST['q'],
241
          'parameters' => $_REQUEST,
242
        )),
243
        10 // weight
244
      );
245
    }
246

    
247
    RenderHints::popFromRenderStack();
248
    return $render_array;
249
}
250

    
251

    
252
/**
253
 * Creates the drupal render array for the table showing all derivated which are
254
 * derive from a common gathering event.
255
 *
256
 * @param $field_unit_uuids array
257
 *  An array of uuids for cdm FieldUnit entities.
258
 *
259
 * @return array
260
 *  A drupal render array for a table
261
 *
262
 * @ingroup compose
263
 */
264
function compose_specimen_table($field_unit_uuids)
265
{
266
// get icon images
267
  $expand_icon = font_awesome_icon_markup(
268
    'fa-plus-square-o',
269
    array(
270
      'alt' => 'Show details',
271
      'class' => array('expand_icon')
272
    )
273
  );
274
  $collapse_icon = font_awesome_icon_markup(
275
    'fa-minus-square-o',
276
    array(
277
      'alt' => 'Show details',
278
      'class' => array('collapse_icon')
279
    )
280
  );
281
  $detail_image_icon = '<img title="Detail Image" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/detail_image_derivate-16x16-32.png' . '"/>';
282
  $checked_box_icon = '<img src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/step_done.gif' . '"/>';
283
  $sequence_icon = '<img title="Molecular Data" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/sequence_derivate-16x16-32.png' . '"/>';
284
  $character_data_icon = '<img title="Character Data" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/character_data_derivate-16x16-32.png' . '"/>';
285

    
286
  $rowcount = 0;
287
  $rows = array();
288

    
289
  foreach ($field_unit_uuids as $field_unit_uuid) {
290

    
291
    //get derivate hierarchy for the FieldUnit
292
    $derivateHierarchy = cdm_ws_get(CDM_WS_PORTAL_OCCURRENCE, array($field_unit_uuid, 'derivateHierarchy'));
293
    if ($derivateHierarchy) {
294
      //summary row
295
      $rows[] = array(
296
        'data' => array(
297
          array(
298
            'data' => $expand_icon . $collapse_icon,
299
            'class' => array('summary_row_cell', 'summary_row_icon', 'expand_column')
300
          ),
301
          array(
302
            'data' => $derivateHierarchy->country,
303
            'class' => array('summary_row_cell')
304
          ),
305
          array(
306
            'data' => $derivateHierarchy->date,
307
            'class' => array('summary_row_cell')
308
          ),
309
          array(
310
            'data' => $derivateHierarchy->collectingString,
311
            'class' => array('summary_row_cell')
312
          ),
313
          @array(
314
            'data' => $derivateHierarchy->collection,
315
            'class' => array('summary_row_cell')
316
          ),
317
          array(
318
            'data' => $derivateHierarchy->hasType ? $checked_box_icon : "",
319
            'class' => array('summary_row_cell', 'summary_row_icon')
320
          ),
321
          array(
322
            'data' => $derivateHierarchy->hasSpecimenScan ? $checked_box_icon : "",
323
            'class' => array('summary_row_cell', 'summary_row_icon')
324
          ),
325
          array(
326
            'data' => ($derivateHierarchy->hasDna ? $sequence_icon : "") . " "
327
              . ($derivateHierarchy->hasDetailImage ? $detail_image_icon : "") . " "
328
              . ($derivateHierarchy->hasCharacterData ? $character_data_icon : ""),
329
            'class' => array('summary_row_cell', 'summary_row_icon')
330
          )
331
        ),
332
        'id' => 'derivate_summary' . $rowcount, // summary row id
333
        'class' => array('summary_row'),
334
      );
335

    
336
      //assemble field unit details
337
      $detail_html = "";
338
      // - citation
339
      if ($derivateHierarchy->citation) {
340
        $detail_html .= create_label("Citation") . $derivateHierarchy->citation . "<br>";
341
      }
342
      //assemble specimen details
343
      if ($derivateHierarchy->derivates) {
344
        foreach ($derivateHierarchy->derivates as $preservedSpecimenDTO) {
345
          $detail_html .= "<br>";
346
          $detail_html .= render_cdm_specimenDTO_page($preservedSpecimenDTO);
347
      }
348
      }
349
      $detail_html .= "<br>";
350
      //detail row resp. one BIG detail cell
351
      $rows[] = array(
352
        'data' => array(
353
          array(
354
            'data' => "", //empty first column
355
            'class' => array('expand_column')
356
          ),
357
          array(
358
            'data' => $detail_html,
359
            'colspan' => 7,
360
          ),
361
        ),
362
        'id' => 'derivate_details' . $rowcount,//details row ID
363
        'class' => array('detail_row'),
364
      );
365
      $rowcount++;
366
    }
367
  }
368

    
369
  $tableId = "derivate_hierarchy_table";
370
  $derivateHierarchyTable = array(
371
    "#theme" => "table",
372
    "#weight" => 2,
373
    "#header" => array(
374
      array(
375
        'data' => "",
376
        'class' => array('expand_column')
377
      ),
378
      "Country", "Date", "Collector + collecting number", "Herbaria", "Type", "Scan", "Derivatives"),
379
    "#rows" => $rows,
380
    "#attributes" => array(
381
      "id" => $tableId,
382
      "border" => 2
383
    )
384
  );
385

    
386
  //add toggle functionality to derivate hierarchy table
387
  drupal_add_js_rowToggle("#" . $tableId);
388

    
389
  return $derivateHierarchyTable;
390
}
391

    
392
function create_html_link($link, $openInExternalWindow=false){
393
    $html = "";
394
    if($link->uri && $link->uri!=""){
395
        $html .= '<a  href="' . $link->uri . '"';
396
        if($openInExternalWindow){
397
            $html .= ' target="_blank"';
398
        }
399
        $html .= '>' . $link->linkText . '</a>';
400
    }
401
    else{
402
        $html .= $link->linkText;
403
    }
404
    return $html;
405
}
406

    
407
/**
408
 * Creates HTML links from the given link list concatenated by default by a comma.
409
 * @param $linkList the list with Link objects having "uri" and "linkText" as members
410
 * @return string the assembled HTML string containing the links
411
 */
412
function create_html_links($linkList, $openInExternalWindow=false, $separator=", ")
413
{
414
    $html = "";
415
    if ($linkList) {
416
        foreach ($linkList as $link) {
417
            $html .= create_html_link($link, $openInExternalWindow).$separator;
418
        }
419
        $html = rtrim($html, $separator);
420
    }
421
    return $html;
422
}
423

    
424
/**
425
 * Composes a taxon page which can consist of multiple parts like
426
 * 'General', 'Synonymy', 'Images', 'Keys'. These parts can be displayed
427
 * as tabs or as sections of a single page.
428
 *
429
 * It is headed by the name of the accepted taxon without author and reference.
430
 *
431
 * @param $taxon
432
 *   The CDM Taxon Instance to compose the page for.
433
 * @param $page_part
434
 *   Name of the part to display, valid values are:
435
 *    - 'description' -  for the general part
436
 *    - 'images'
437
 *    - 'synonymy'
438
 *    - 'keys'
439
 *    - 'all'
440
 *
441
 * @return array
442
 *   A drupal render array
443
 *
444
 * @ingroup compose
445
 */
446
function compose_cdm_taxon_page($taxon, $page_part = 'description') {
447

    
448
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
449
  $http_request_params = drupal_get_query_parameters();
450

    
451
  // add all mandatory js sources
452
  _add_js_footnotes();
453

    
454

    
455
  $render_array = array();
456
  $weight = 0; // the weight for the render array elements
457

    
458
  $tabsToDisplay = variable_get('cdm_taxonpage_tabs_visibility', unserialize(TAXONPAGE_VISIBILITY_OPTIONS_DEFAULT));
459

    
460
  $page_part = variable_get('cdm_dataportal_taxonpage_tabs', 1) ? $page_part : 'all';
461

    
462
  $synonymy_as_tab = variable_get(CDM_SYNONYMY_AS_TAB, CDM_SYNONYMY_AS_TAB_DEFAULT) === 1;
463
  if(!$synonymy_as_tab){
464
    unset($tabsToDisplay["Synonymy"]);
465
    // the synonymy is located in the general part in this case
466
    if($page_part == 'synonymy'){
467
      $page_part = 'description';
468
    }
469
  }
470

    
471
  $media = _load_media_for_taxon($taxon);
472

    
473

    
474
  if (!isset($media[0]) || ($tabsToDisplay["Images"] == '0')) {
475
    taxon_page_tabs_hidden_add('images');
476
  }
477

    
478
  // --- GET specimensOrObservations --- //
479
  $specimensOrObservations = cdm_ws_get(CDM_WS_TAXON, array( $taxon->uuid, 'specimensOrObservationsCount'));
480

    
481
  $specimensOrObservationsCount = $specimensOrObservations != null ? $specimensOrObservations->result : 0;
482
  if ($specimensOrObservationsCount == 0 || ($tabsToDisplay["Specimens"] == '0')) {
483
    taxon_page_tabs_hidden_add('specimens');
484
  }
485

    
486
  // --- GET polytomousKeys --- //
487
  $polytomousKeysPager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, NULL, "findByTaxonomicScope=$taxon->uuid");
488
  $identificationKeyCount = 0;
489
  if ($polytomousKeysPager) {
490
    $identificationKeyCount += $polytomousKeysPager->count;
491
  }
492
  if ($identificationKeyCount == 0 || ($tabsToDisplay["Keys"] == '0')) {
493
    taxon_page_tabs_hidden_add('keys');
494
  }
495

    
496
  // --- GET TaxonNodeAgentRelations --- //
497
  $current_classification_uuid = get_current_classification_uuid();
498
  $taxon_node_agent_relations_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
499
      array(
500
          $taxon->uuid,
501
          $current_classification_uuid,
502
      ),
503
      "pageSize=1&pageIndex=0"// we are only interested into the count so we are fetching only one item, o is not possible!
504
  );
505
  if (!$taxon_node_agent_relations_pager || $taxon_node_agent_relations_pager->count == 0){
506
      taxon_page_tabs_hidden_add('experts');
507
  }
508

    
509
  if (!isset($tabsToDisplay["Synonymy"]) || $tabsToDisplay["Synonymy"] == '0') {
510
    taxon_page_tabs_hidden_add('synonymy');
511
  }
512

    
513
  // -------------------------------------------- //
514

    
515
  if (variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR) && isset($_REQUEST['acceptedFor'])) {
516
    $render_array['accepted_for'] = markup_to_render_array(cdm_accepted_for($_REQUEST['acceptedFor']), $weight++);
517
  }
518

    
519
  // --- PAGE PART: DESCRIPTION --- //
520
  if (!taxon_page_tabs_hidden_check('description') && ($page_part == 'description' || $page_part == 'all')) {
521

    
522
    $merged_tree = merged_taxon_feature_tree($taxon);
523

    
524

    
525
    $render_array['general'] = compose_cdm_taxon_page_profile($taxon, $merged_tree, $media, !$synonymy_as_tab);
526
    $render_array['general']['#weight'] = $weight++;
527
    $render_array['general']['#prefix'] = '<div id="general" class="page-part">';
528
    $render_array['general']['#suffix'] = '</div>';
529
  }
530

    
531
  // --- PAGE PART: IMAGES --- //
532
  if (!taxon_page_tabs_hidden_check('images') && ($page_part == 'images' || $page_part == 'all')) {
533
    $images_html = '<div id="images" class="page-part">';
534
    if ($page_part == 'all') {
535
      $images_html .= '<h2>' . t(cdm_taxonpage_tab_label('Images')) . '</h2>';
536
    }
537
    // Get the image gallery as configured by the admin.
538
    $default_image_gallery = 'default';
539
    $configured_image_gallery_viewer = variable_get('image_gallery_viewer', 'default');
540
    $taxon_image_gallery = '<div class="error">No image gallery viewer configured</div>';
541
    if($configured_image_gallery_viewer == $default_image_gallery || $configured_image_gallery_viewer == 'fsi'){
542
      // the fsi_gallery requires a flash plugin, in case the client browser is not supporting
543
      // flash we also need to provide an the default gallery as alternative
544
      $taxon_image_gallery = '<div id="default-gallery-viewer">' .
545
        call_user_func_array('taxon_image_gallery_' . $default_image_gallery, array(
546
          $taxon,
547
          $media,
548
        ))
549
        . '</div>';
550
    }
551
    if($configured_image_gallery_viewer == 'fsi'){
552
      $taxon_image_gallery .= '<div id="'. $configured_image_gallery_viewer .'-gallery-viewer">' .
553
        call_user_func_array('taxon_image_gallery_' . $configured_image_gallery_viewer, array(
554
          $taxon,
555
          $media,
556
        ))
557
       . '</div>';
558
    }
559
    $images_html .= $taxon_image_gallery;
560
    $images_html .= '</div>';
561
    $render_array['images'] = markup_to_render_array($images_html, $weight++);
562
  }
563

    
564
  // --- PAGE PART: SYNONYMY --- //
565
  if (!taxon_page_tabs_hidden_check('synonymy') && (($page_part == 'synonymy' || $page_part == 'all') && $synonymy_as_tab)) {
566
    $synonymy_html = '<div id="synonymy" class="page-part">';
567
    if ($page_part == 'all') {
568
      $synonymy_html .= '<h2>' . t(cdm_taxonpage_tab_label('Synonymy')) . '</h2>';
569
    }
570
    $addAcceptedTaxon = variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE);
571

    
572
    $synonym_a = compose_cdm_taxon_page_synonymy($taxon, $addAcceptedTaxon);
573
    $synonymy_html .= drupal_render($synonym_a);
574

    
575
    $synonymy_html .= '</div>';
576
    $render_array['synonymy'] = markup_to_render_array($synonymy_html, $weight++);
577

    
578
  }
579

    
580
  // --- PAGE PART: SPECIMENS --- //
581
  if (!taxon_page_tabs_hidden_check('specimens') && ($specimensOrObservationsCount > 0 && ($page_part == 'specimens' || $page_part == 'all'))) {
582
    $render_array['specimens'] = array(
583
        '#prefix' => '<div id="specimens" class="page-part">' . ($page_part == 'all' ? '<h2>' . t(cdm_taxonpage_tab_label('Specimens')) . '</h2>' : ''),
584
        'content' => cdm_dataportal_taxon_page_specimens($taxon), // returns render array
585
        '#suffix' => '</div>',
586
    );
587
  }
588

    
589
  // --- PAGE PART: KEYS --- //
590
  if(!taxon_page_tabs_hidden_check('keys')){
591
    if ($identificationKeyCount == 1 && $page_part == 'keys'){
592
      drupal_goto(path_to_key($polytomousKeysPager->records[0]->class, $polytomousKeysPager->records[0]->uuid));
593
    }
594
    else if ($identificationKeyCount > 0 && ($page_part == 'keys' || $page_part == 'all')) {
595
      $keys_html = '<div id="keys" class="page-part">';
596
      if ($page_part == 'all') {
597
        $keys_html .= '<h2>' . t(cdm_taxonpage_tab_label('Keys')) . '</h2>';
598
      }
599
      $keys_html .= theme('cdm_block_IdentificationKeys', array('taxonUuid' => $taxon->uuid));
600
      $keys_html .= '</div>';
601
      $render_array['keys'] = markup_to_render_array($keys_html, $weight++);
602
    }
603
  }
604

    
605
  // --- PAGE PART: EXPERTS --- //
606

    
607
  if (!taxon_page_tabs_hidden_check('experts') && ($page_part == 'experts' || $page_part == 'all')) {
608
    $render_array['experts'] = array(
609
        '#prefix' => '<div id="experts" class="page-part">' . ($page_part == 'all' ? '<h2>' . t(cdm_taxonpage_tab_label('Experts')) . '</h2>' : ''),
610
        'content' => compose_cdm_taxon_page_experts($taxon), // returns render array
611
        '#suffix' => '</div>',
612
    );
613
  }
614

    
615
  // ------------------ END OF PARTS -------------- //
616

    
617
  // adjust weights of page and toc elements according to the settings
618
  $taxontabs_weights = get_array_variable_merged(CDM_TAXONPAGE_TAB_WEIGHT, CDM_TAXONPAGE_TAB_WEIGHT_DEFAULT);
619
  foreach($taxontabs_weights as $tab_key => $weight){
620
    if(isset($render_array[$tab_key])){
621
      $render_array[$tab_key]['#weight'] = $weight;
622
    }
623
  }
624

    
625

    
626
  // set up the TOC for the pages which contain all pageparts
627
  if($page_part == 'all') {
628

    
629
    asort($taxontabs_weights);
630
    foreach(array_keys($taxontabs_weights) as $tab_key){
631
      if(isset($render_array[$tab_key])){
632
        if($tab_key != 'general'){
633
          // add entry for page part
634
          $toc_elements[] = array(
635
              'data' => l(t(cdm_taxonpage_tab_label(ucfirst($tab_key))), $_GET['q'], array('fragment' => $tab_key, 'query' => $http_request_params)),
636
              'class' => array('page-part-toc-item-' . $tab_key)
637
          );
638
        } else {
639
          // add content of profile part instead
640
          if(isset($render_array['general'])) {
641
            // in case all tabs are shown at once the feature tocs
642
            // should be integrated into the tabs toc as sub list
643
            // and the profile image should be on top of the page
644
            if(isset($render_array['general']['taxon_description_feature_toc'])){;
645
            foreach ($render_array['general']['taxon_description_feature_toc']['#items'] as $profile_toc_item){
646
              $toc_elements[] = $profile_toc_item;
647
            }
648
            unset($render_array['general']['taxon_description_feature_toc']);
649
            }
650
          }
651
        }
652
      }
653
    }
654

    
655
    // move profile image in page structure
656
    if(isset($render_array['general']['taxon_profile_image'])){
657
      $render_array['profile_image'] = $render_array['general']['taxon_profile_image'];
658
      $render_array['profile_image']['#weight'] = -100;
659
      unset($render_array['general']['taxon_profile_image']);
660
    }
661

    
662
    // finally add the table of contents to the render array
663
    $render_array['toc'] = array(
664
        '#theme' => 'item_list',
665
        '#items' => $toc_elements,
666
        '#title' => t('Content'),
667
        '#weight' => -101,
668
        '#suffix' => '</div>',
669
        '#prefix'=> '<div id="page-toc">'
670
    );
671
  }
672

    
673

    
674
  return $render_array;
675
}
676

    
677
/**
678
 * TODO should this function really be a compose function?
679
 *     For a compose function must there always be a theme function with the same name? (ak 8.8.2013)
680
 *
681
 * composes and returns an render array containing the components of the taxon profile tab:
682
 *  - 'taxon_profile_image'
683
 *  - 'taxon_description_feature_toc'
684
 *  - 'taxon_description_features'
685
 *
686
 *
687
 * @param object taxon
688
 * @param object $merged_tree
689
 * @param object media
690
 * @param bool $add_synonymy
691
 *
692
 * @return array
693
 *   A Drupal render array with the following elements:
694
 *     - 'taxon_profile_image'
695
 *     - 'taxon_description_feature_toc'
696
 *     - 'taxon_description_features'
697
 *
698
 * @throws Exception
699
 *
700
 * @ingroup compose
701
 */
702
function compose_cdm_taxon_page_profile($taxon, $merged_tree, $media, $add_synonymy) {
703

    
704
  $render_array = array();
705

    
706
  $taxon_profile_image_settings = variable_get(CDM_TAXON_PROFILE_IMAGE, unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT));
707

    
708
  $hide_taxon_profile_image = FALSE;
709
  if (variable_get('image_hide_rank', '0') != '0' && isset($taxon->name->rank->uuid)) {
710
    $rankCompare = rank_compare($taxon->name->rank->uuid, variable_get('image_hide_rank', '-99'));
711
    $hide_taxon_profile_image = ($rankCompare > -1);
712
  }
713

    
714
  if ($taxon_profile_image_settings['show'] && !$hide_taxon_profile_image) {
715

    
716
    $representationPart = new stdClass();
717
    $attributes = array();
718
    if (isset($media[0])) {
719
      // due to a bug the portal/taxon/{uuid}/media service only delivers a filtered media object
720
      // which only contains the thumbnail representation even if the height and width filters are not set.
721
      // -->
722
      $preferred_media = cdm_ws_get(CDM_WS_MEDIA, $media[0]->uuid);
723
      $preferred_representations = cdm_preferred_media_representations($preferred_media, array(
724
        'image/jpg',
725
        'image/jpeg',
726
        'image/png',
727
        'image/gif',
728
      ),
729
        $taxon_profile_image_settings['maxextend'],
730
        $taxon_profile_image_settings['maxextend']
731
      );
732
      if(count($preferred_representations) > 0){
733

    
734
        $representation = array_shift($preferred_representations);
735
        $representationPart = $representation->parts[0];
736
        $attributes['alt'] = $representationPart->uri;
737

    
738
        if (!empty($taxon_profile_image_settings['media_uri_query'])) {
739
          $representationPart->uri = $representationPart->uri
740
            . (strpos($representationPart->uri, '?') !== FALSE ? '&' : '?')
741
            . $taxon_profile_image_settings['media_uri_query'];
742
        }
743
      }
744
    }
745
    else {
746
      if ($taxon_profile_image_settings['custom_placeholder_enabled']) {
747
        // show placeholder image instead
748
        if (!empty($taxon_profile_image_settings['custom_placeholder_image_on']) && !empty($taxon_profile_image_settings['custom_placeholder_image_fid'])) {
749
          // use the user provided image
750
          $profile_image_file = file_load($taxon_profile_image_settings['custom_placeholder_image_fid']);
751
          $url = file_create_url($profile_image_file->uri);
752
          $image_info = image_get_info($profile_image_file->uri);
753
          $representationPart->width = $image_info['width'];
754
          $representationPart->height = $image_info['height'];
755
          $representationPart->uri = $url;
756
        }
757
        else {
758
          // use the hard coded default
759
          $representationPart->width = 184;
760
          $representationPart->height = 144;
761
          $representationPart->uri = base_path() . drupal_get_path('module',
762
              'cdm_dataportal') . '/images/no_picture.png';
763
        }
764
        $attributes['alt'] = "no image available";
765
      }
766
    }
767

    
768
    if (isset($representationPart->uri)) {
769
      $profile_image = cdm_media_gallerie_image($representationPart, $taxon_profile_image_settings['maxextend'], FALSE, $attributes);
770
      // NOTE: style="width:${maxextend}px' is needed for IE8 !!!
771
      $max_extend_with = $taxon_profile_image_settings['maxextend'] . 'px';
772
      $render_array['taxon_profile_image'] = markup_to_render_array('<div id="taxonProfileImage" style="width:' . $max_extend_with . '>' . $profile_image . '</div>',
773
        -101);
774
    }
775
  }
776

    
777
  if($add_synonymy){
778
    $synonymy_a = compose_cdm_taxon_page_synonymy($taxon, true);
779
    $synonymy_a['#weight'] = -102;
780
    $render_array['synonymy'] = $synonymy_a;
781
  }
782

    
783
  $pseudo_feature_blocks = array();
784
  $pseudo_feature_block_toc_items = array();
785

    
786

    
787
  // Render the sections for each real feature
788
  $feature_block_list = make_feature_block_list($merged_tree->root->childNodes, $taxon);
789

    
790
  // Bibliography
791
  $bibliography_settings = get_bibliography_settings();
792
  if($bibliography_settings['enabled'] == 1){
793
    $bibliography_markup = FootnoteManager::renderFootnoteList('BIBLIOGRAPHY', '');
794
    if($bibliography_markup) {
795
      $feature_bibliography = make_pseudo_feature('Bibliography', 'BIBLIOGRAPHY');
796
      $bibliography_block = feature_block(t('Bibliography'), $feature_bibliography);
797
      $bibliography_item = markup_to_render_array($bibliography_markup);
798
      $bibliography_block->content[] = compose_feature_block_wrap_elements(array($bibliography_item), $feature_bibliography);
799

    
800
      $pseudo_feature_block_toc_items['Bibliography']= 'bibliography';
801
      $pseudo_feature_blocks[] = $bibliography_block;
802
    }
803
  }
804

    
805
  $render_array['taxon_description_features'] = _block_get_renderable_array(
806
    array_merge($feature_block_list, $pseudo_feature_blocks)
807
  );
808

    
809
  if($pseudo_feature_block_toc_items){
810
    foreach ($pseudo_feature_block_toc_items as $label=>$fragment){
811
      cdm_toc_list_add_item($label, $fragment);
812
    }
813
  }
814

    
815
  // create the table of content
816
  $toc = array(
817
      '#theme' => 'item_list',
818
    '#items' => cdm_toc_list(),
819
      '#title' => t('Content'),
820
    '#weight' => -100,                  // move to the top
821
      '#suffix' => '</div>',
822
      '#prefix'=> '<div id="page-toc">'
823
  );
824
  $render_array['taxon_description_feature_toc'] = $toc;
825

    
826
  return $render_array;
827
}
828

    
829

    
830
/**
831
 * Returns HTML containing the synonymy for the accepted taxon.
832
 *
833
 * Shows the whole synonymy for the accepted taxon.
834
 * The synonymy list is headed by the complete scientific name
835
 * of the accepted taxon with nomenclatural reference.
836
 *
837
 * @param object $taxon
838
 * @param boolean $add_accepted_taxon
839
 *
840
 * @return array
841
 *  Drupal render array for the synonymy
842
 *
843
 * @throws Exception
844
 *
845
 * @ingroup compose
846
 */
847
function compose_cdm_taxon_page_synonymy($taxon, $add_accepted_taxon) {
848

    
849
  RenderHints::pushToRenderStack('taxon_page_synonymy');
850

    
851
  // footnote key for the homotypic group and accepted taxon,
852
  // both should have the same footnote key
853
  RenderHints::setFootnoteListKey(RenderHints::getRenderPath());
854

    
855
  $synomymie = cdm_ws_get(CDM_WS_PORTAL_TAXON_SYNONYMY, array($taxon->uuid));
856

    
857
  $out = '';
858

    
859
  // Render accepted taxon.
860
  //
861
  // foonotes of the accepted taxon will be rendered in the homotypic group section
862
  // even if there are not synonyms in the homotypic group
863
  // homotypic group and accepted taxon should have the same footnote key
864
  $referenceUri = '';
865
  if ($add_accepted_taxon) {
866
    // remember the last part of the render path
867
    $synonymy_render_path = RenderHints::getRenderPath();
868
    // set new render path for the accepted taxon so
869
    // it can be styled differently via the name render part definitions
870
    RenderHints::pushToRenderStack('accepted_taxon');
871
    $accepted_name = '';
872
    if (variable_get(CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE, 0)) {
873
      $label = variable_get(CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE_LABEL, CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE_LABEL_DEFAULT);
874
      $accepted_name .= '<div class="secReference"><span class="label">' . t($label) . ':</span> ' . $taxon->sec->titleCache . '</div>';
875
    }
876
    if (isset($taxon->name->nomenclaturalReference)) {
877
      $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
878
    }
879

    
880
    $accepted_name .= '<div class="accepted-name">';
881
    $accepted_name .= render_taxon_or_name($taxon, NULL, $referenceUri);
882

    
883
    $name_relations = cdm_name_relationships_for_taxon($taxon);
884
    $name_relationships = compose_name_relationships_inline($name_relations, $taxon->name->uuid, $taxon->uuid);
885
    // Render relationships of accepted name.
886
    if(isset($name_relationships['list']['items'][0])){
887
      $accepted_name .= ' ' . drupal_render($name_relationships);
888
    }
889

    
890
    // handle annotations of the name and taxon
891
    $special_annotations_array = array();
892
    $special_annotations_array[] = $taxon->name;
893
    $special_annotations_array[] = $taxon;
894
    $accepted_name .= theme('cdm_annotations_as_footnotekeys', array(
895
        'cdmBase_list' => $special_annotations_array,
896
        'footnote_list_key' => $synonymy_render_path . '-annotations')
897
    );
898
    $accepted_name .= '</div>';
899
    RenderHints::popFromRenderStack();
900
  }
901

    
902
  // --- Render homotypic synonymy group
903
  if (!empty($accepted_name)) {
904
    $out .= $accepted_name;
905
  }
906

    
907
  // Render the homotypicSynonymyGroup including the type information.
908
  $out .= theme(
909
    'cdm_homotypicSynonymyGroup',
910
    array(
911
      'synonymList' => $synomymie->homotypicSynonymsByHomotypicGroup,
912
      'accepted_taxon_name_uuid' => $taxon->name->uuid
913
    )
914
  );
915

    
916

    
917
  // Render accepted taxon heterotypic synonymy groups.
918
  if ($synomymie->heterotypicSynonymyGroups) {
919
    foreach ($synomymie->heterotypicSynonymyGroups as $homotypicalGroup) {
920
      $out .= theme('cdm_heterotypicSynonymyGroup', array('homotypicalGroup' => $homotypicalGroup));
921
    }
922
  }
923
  // Render taxon relationships.
924
  if (variable_get(CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT)) {
925
    $taxonRelationshipsDTO = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS_DTO, $taxon->uuid);
926
    $out .= cdm_taxonRelationships($taxonRelationshipsDTO, $taxon);
927
  }
928

    
929
  RenderHints::popFromRenderStack();
930

    
931
  return markup_to_render_array($out);
932
}
933

    
934

    
935
/**
936
 * composes and returns an render array for the experts associated with the given taxon
937
 *
938
 * @param object taxon
939
 *
940
 * @return array
941
 *   A Drupal render array for a table with the experts
942
 *
943
 * @ingroup compose
944
 */
945
function compose_cdm_taxon_page_experts($taxon){
946

    
947
  $render_array = array();
948
  if(!isset($taxon->uuid)){
949
    return $render_array;
950
  }
951

    
952
  $current_classification_uuid = get_current_classification_uuid();
953
  // TODO use cdm_ws_fetchall below but this failes! needs fix!
954
  $taxon_node_agent_relations = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
955
    array(
956
      $taxon->uuid,
957
      $current_classification_uuid
958
    )
959
  );
960

    
961
  $header = array(
962
    array('data' => t('Expert')),
963
    array('data' => t('Role'))
964
  );
965
  $rows = array();
966

    
967

    
968
  foreach($taxon_node_agent_relations->records as $taxon_node_agent_relation){
969

    
970

    
971
    $expert_role_id = $taxon_node_agent_relation->agent->uuid . '-' . $taxon_node_agent_relation->type->uuid;
972
    $expert_details_container_id = 'expert_details_' . $expert_role_id;
973

    
974
    $agent_label_markup = cdm_dynabox(
975
      'expert_' . $expert_role_id,
976
      $taxon_node_agent_relation->agent->titleCache,
977
      // specifying both ends of the relationship will return only one record in the pager
978
      cdm_compose_url(CDM_WS_PORTAL_AGENT,
979
         array($taxon_node_agent_relation->agent->uuid, 'taxonNodeAgentRelations'),
980
        'taxon_uuid=' . $taxon->uuid . '&relType_uuid=' . $taxon_node_agent_relation->type->uuid),
981
      'cdm_taxon_expert',
982
      'Click for details',
983
      array('div', 'div'),
984
      array(), // attributes
985
      '#' . $expert_details_container_id // $content_element_selector
986
    );
987

    
988
    // Expert and Role
989
    $rows[] = array(
990
      'data' => array(
991
        array(
992
          'data' => $agent_label_markup,
993
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->agent))
994
        ),
995
        array(
996
          'data' => $taxon_node_agent_relation->type->representation_L10n,
997
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->type))
998
        )
999
      )
1000
    );
1001
    // Agent details
1002
    $rows[] = array(
1003
      'data' => array(
1004
        array(
1005
          'data' => '<!-- expert_details_container -->',
1006
          'id' => $expert_details_container_id,
1007
          'colspan' => 2
1008
        )
1009
      )
1010
    );
1011

    
1012
  }
1013

    
1014

    
1015
  $render_array['experts_table'] = array(
1016
    '#theme' => 'table',
1017
    '#header' => $header,
1018
    '#rows' => $rows,
1019
  );
1020

    
1021

    
1022
  return $render_array;
1023
}
1024

    
1025

    
1026
/**
1027
 * Manages the tabs to be hidden in the taxon page.
1028
 *
1029
 * The tabs are identified by their last menu link path element:
1030
 *  - description
1031
 *  - synonymy
1032
 *  - images
1033
 *  - specimens
1034
 *  - key
1035
 *
1036
 * Internally the tabs are stored in a static variable which is
1037
 * managed by drupal_static().
1038
 *
1039
 * @param string $add_tab
1040
 *   Optional parameter. The given string will be added to the array of tabs
1041
 *
1042
 * @return
1043
 *   The array of tabs
1044
 */
1045
function taxon_page_tabs_hidden_add($add_tab = NULL) {
1046
  $tabs = &drupal_static('taxon_page_tabs_hidden');
1047

    
1048
  if(!isset($tabs)){
1049
    $tabs = array();
1050
  }
1051

    
1052
  if (isset($add_tab) && !array_key_exists($add_tab, $tabs)) {
1053
    $tabs[] = $add_tab;
1054
  }
1055

    
1056
  return $tabs;
1057
}
1058

    
1059
/**
1060
 * Manages the tabs to be hidden in the taxon page.
1061
 *
1062
 * The tabs names are identified by their last menu link path element:
1063
 *  - description
1064
 *  - synonymy
1065
 *  - images
1066
 *  - specimens
1067
 *  - key
1068
 *
1069
 * Internally the tabs are stored in a static variable which is
1070
 * managed by drupal_static().
1071
 *
1072
 * @param string $tabname
1073
 *   The name of the tab to check
1074
 *
1075
 * @return boolean
1076
 *   True if the tab or section is to be hidden
1077
 */
1078
function taxon_page_tabs_hidden_check($tabname) {
1079

    
1080
  $tabs = &drupal_static('taxon_page_tabs_hidden');
1081

    
1082
  if(!isset($tabs)){
1083
    $tabs = array();
1084
  }
1085

    
1086
  return array_search($tabname, $tabs) !== FALSE;
1087
}
1088

    
1089
/**
1090
 * Implements the hook_preprocess_HOOK() for theme_menu_local_tasks()
1091
 *
1092
 *  - Removes the tabs to be hidden, @see taxon_page_tabs_hidden_add()
1093
 *  - Renames tabs according to the settings // TODO (this will replace the theme_cdm_taxonpage_tab() function !!!)
1094
 *
1095
 * @param array $variables
1096
 *   The variables array
1097
 */
1098
function cdm_dataportal_preprocess_menu_local_tasks(&$variables) {
1099

    
1100
  $hidden_tabs = taxon_page_tabs_hidden_add();
1101

    
1102
  if (!variable_get(CDM_SEARCH_BLAST_ENABLED)) {
1103
      if (is_array($variables['primary'])) {
1104
          foreach ($variables['primary'] as $key => &$element) {
1105
              if ($element['#link']['path'] == 'cdm_dataportal/search/blast') {
1106
                  // remove the tab
1107
                  unset($variables['primary'][$key]);
1108
              }
1109

    
1110
          }
1111
      }
1112
  }
1113
  if (is_array($variables['primary'])) {
1114
    foreach ($variables['primary'] as $key => &$element) {
1115

    
1116
      // 1. Remove the tabs to be hidden
1117
      foreach ($hidden_tabs as $tab) {
1118
        if ($element['#link']['path'] == 'cdm_dataportal/taxon/%/' . $tab) {
1119
          // remove the tab
1120
          unset($variables['primary'][$key]);
1121
        }
1122
      }
1123
    }
1124
  }
1125
}
1126

    
1127

    
1128

    
1129
/**
1130
 * Implements the hook_preprocess_HOOK() for theme_menu_local_task()
1131
 *
1132
 *
1133
 * @param array $variables
1134
 *   An associative array containing:
1135
 *     - element: A render element containing:
1136
 *          #link: A menu link array with 'title', 'href', and 'localized_options' keys.
1137
 *          #active: A boolean indicating whether the local task is active.
1138
 *
1139
 */
1140
function cdm_dataportal_preprocess_menu_local_task(&$variables) {
1141

    
1142
  $link = $variables['element']['#link'];
1143
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
1144
    $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']) . '"/>';
1145
    $link['localized_options']['html'] = TRUE;
1146

    
1147
    $variables['element']['#link'] = $link;
1148
  }
1149
}
1150

    
1151
/* =================== block composition ===================== */
1152

    
1153
/**
1154
 * Composes and returns an render array for the classification breadcrumbs of the given taxon.
1155
 *
1156
 * @param taxon
1157
 *
1158
 * @return array
1159
 *   A Drupal render array for a table with the experts
1160
 *
1161
 * @ingroup compose
1162
 */
1163
function compose_classification_breadcrumbs($taxon_uuid) {
1164

    
1165
  _add_js_taxonomic_children('#classification-breadcrumbs .taxonomic-children-button');
1166

    
1167
  $render_array = array();
1168

    
1169
  $render_array['#theme'] = 'item_list';
1170
  $render_array['#type'] = 'ul';
1171
  $render_array['#attributes'] = array(
1172
    'id' => 'classification-breadcrumbs',
1173
    'class' => 'breadcrumbs inline',
1174
  );
1175

    
1176
  $items = array();
1177

    
1178
  $parent_taxon_nodes = null;
1179
  if($taxon_uuid){
1180
    $parent_taxon_nodes = cdm_ws_taxonomy_pathFromRoot($taxon_uuid);
1181
  }
1182

    
1183
  $classifications = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXONOMY);
1184
  // find current classification in list
1185
  $classification = null;
1186
  $current_classification_uuid = get_current_classification_uuid();
1187
  foreach ($classifications as $classification){
1188
    if($classification->uuid == $current_classification_uuid){
1189
      break;
1190
    }
1191
  }
1192

    
1193
  $node_name = '';
1194
  if(count($classifications) > 1 ){
1195
    // need to add the current classification as first label
1196

    
1197
    $label = $classification->titleCache;
1198
    if(strlen($label) > 20){
1199
      $label = substr($label, 0, strpos($label, ' ', 15)) . '...';
1200
    }
1201
    $node_name = font_awesome_icon_markup('fa-th-list')  . ' ' . l($label, '#', array(
1202
      'attributes' => array(
1203
        'class' => 'taxonomic-children-button classification-chooser',
1204
        'data-destination-uri' => drupal_get_destination(),
1205
        'data-cdm-align-with' => array('prev')
1206
      ),
1207
      'html' => true
1208
    ));
1209
  }
1210

    
1211
  $rank_limit_uuid = variable_get(TAXONTREE_RANKLIMIT, TAXONTREE_RANKLIMIT_DEFAULT);
1212

    
1213
  $rank_separator = '<span> '
1214
    . font_awesome_icon_markup('fa-chevron-right')
1215
    . ' </span>';
1216
  $more_children_icon = font_awesome_icon_markup('fa-sitemap fa-rotate-270');
1217
  $more_children_label = '...';
1218

    
1219
  $items[] = $node_name;
1220

    
1221
  $more_children_for = null;
1222
  if($parent_taxon_nodes){
1223
    foreach ($parent_taxon_nodes as $node) {
1224

    
1225
      $is_first_item = count($items) == 0;
1226
      $is_last_item = count($items) == count($parent_taxon_nodes);
1227
      $node_name = cdm_dataportal_shortname_of($node);
1228
      $path = path_to_taxon($node->taxonUuid);
1229

    
1230
      if($node->taxonomicChildrenCount > 0) {
1231
        $more_children_for = $node->taxonUuid;
1232
      } else {
1233
        $more_children_for = null;
1234
      }
1235

    
1236
      // 'fa-sitemap'
1237

    
1238
      $items[] =
1239
        ($is_first_item ? '' : ' ')
1240
        . $rank_separator
1241
        . l(
1242
          '<span class="' . html_class_attribute_ref($node) . '">' . $node_name . '</span>',
1243
          $path,
1244
          array(
1245
            'attributes' => array(
1246
              'class' => array('taxonomic-children-button'),
1247
              'data-cdm-taxon-uuid' => array($node->taxonUuid),
1248
              'data-cdm-classification-mode' => array('siblings'),
1249
              'data-cdm-align-with' => array('prev')
1250
            ),
1251
            'html' => true
1252
          )
1253
        );
1254
      }
1255
    }
1256

    
1257
  // add more button to the end
1258
  if(!$parent_taxon_nodes) {
1259
    // not taxon focused yet, adding button to make  the root nodes available
1260
    $items[] = '<span>'
1261
      . $more_children_icon . '&nbsp;' .
1262
      '<span class="taxonomic-children-button" data-classification-uuid="' . $current_classification_uuid
1263
      . '" data-rank-limit-uuid="' . $rank_limit_uuid . '" data-cdm-align-with="prev"> ' . $more_children_label . '<span>'
1264
      . '</span>';
1265
  } else if($more_children_for){
1266
    // last parent item has child taxon nodes
1267
    $items[] = ' <span>'
1268
      . $more_children_icon . '&nbsp;' .
1269
      '<span class="taxonomic-children-button" data-cdm-taxon-uuid="' .$more_children_for
1270
      . '" data-cdm-classification-mode="children" data-cdm-align-with="prev"> ' . $more_children_label . '</span>'
1271
      . '</span>';
1272

    
1273
  }
1274

    
1275
  $render_array['#items'] = $items;
1276

    
1277
  return $render_array;
1278
}
1279

    
1280

    
1281
/**
1282
 * @param $specimen_uuid
1283
 * @return array
1284
 *    The drupal render array for the page
1285
 *
1286
 * @ingroup compose
1287
 */
1288
function compose_cdm_specimen_page($specimen_uuid)
1289
{
1290
  drupal_set_title("Specimen Details");
1291
  $render_array = array();
1292
  RenderHints::pushToRenderStack('specimen_page');
1293

    
1294
  $specimen_table = array(
1295
      '#theme' => 'table',
1296
      '#weight' => 2,
1297
      // prefix attributes and rows with '#' to let it pass toF the theme function,
1298
      // otherwise it is handled as child render array
1299

    
1300
    '#attributes' => array('class' => 'specimens'),
1301
      '#rows' => array(),
1302
      '#prefix' => '<div id="specimens">',
1303
      '#suffix' => '</div>',
1304

    
1305

    
1306
  );
1307
  $specimen = compose_cdm_specimen_or_observation($specimen_uuid, true);
1308
  $renderArray = array(
1309
          '#theme' => 'item_list',
1310
          '#items' => array($specimen),
1311
          '#type' => 'ul');
1312
  $output = drupal_render($renderArray);
1313

    
1314
  $specimen_table['#rows'][] = array(
1315
          // An array of table rows. Every row is an array of cells, or an associative array
1316
          'data' => array($output),
1317
          'class' =>  array(
1318
              'descriptionElement',
1319
              'descriptionElement_IndividualsAssociation'
1320
          ),
1321

    
1322
  );
1323

    
1324

    
1325
 // $detail_html = compose_cdm_specimen_or_observation($specimen_uuid, true);
1326
//    $render_array['markup'] = array(
1327
//        '#prefix' => '<div id="specimens" class="page">',
1328
//        'content' => $specimen_table,
1329
//        '#suffix' => '</div>',
1330
//    );
1331
    $render_array['specimen_table'] =  $specimen_table;
1332

    
1333
  RenderHints::popFromRenderStack();
1334
  return $render_array;
1335
}
1336

    
1337
/**
1338
 * @param $named_area_uuid
1339
 * @return array
1340
 *    The drupal render array for the page
1341
 *
1342
 * @ingroup compose
1343
 */
1344
function compose_cdm_named_area_page($named_area_uuid)
1345
{
1346

    
1347
  $named_area = cdm_ws_get(CDM_WS_PORTAL_TERM, array($named_area_uuid));
1348

    
1349
  $render_array = array();
1350
  RenderHints::pushToRenderStack('named_area_page');
1351

    
1352
  $groups = array();
1353
  @_description_list_group_add($groups, t('Name') . ':', $named_area->representation_L10n);
1354
  @_description_list_group_add($groups, t('IdInVocabulary') . ':', $named_area->idInVocabulary);
1355
  if(isset($named_area->level)) {
1356
    @_description_list_group_add($groups, t('Level') . ':', $named_area->level->representation_L10n);
1357
  }
1358

    
1359
  $name_area_details_elements = array(
1360
   // '#title' => $title,
1361
    '#theme' => 'description_list',
1362
    '#groups' => $groups,
1363
    '#attributes' => array('class' => html_class_attribute_ref($named_area)),
1364
  );
1365

    
1366
  $render_array[] = $name_area_details_elements;
1367

    
1368
  RenderHints::popFromRenderStack();
1369
  return $render_array;
1370
}
1371

    
1372

    
1373
/**
1374
 * Returns a drupal render array for a single reference page.
1375
 *
1376
 * Composes a page with all data on a single reference.
1377
 *
1378
 * @param string $uuid
1379
 *   An uuid for a cdm reference.
1380
 *
1381
 * @return array
1382
 *  A drupal render array
1383
 *
1384
 * @throws Exception
1385
 *
1386
 * @ingroup compose
1387
 */
1388
function compose_cdm_reference_page($uuid) {
1389

    
1390
  $pathelement = "reference_page";
1391
  RenderHints::pushToRenderStack($pathelement);
1392
  $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
1393
  if (isset($reference->titleCache)) {
1394
    drupal_set_title($reference->titleCache, PASS_THROUGH);
1395
  }
1396

    
1397
  $field_order = array(
1398
    "title",
1399
    "abbrevTitle",
1400
    // "titleCache" abbrevTitleCache
1401
    // "citation",
1402
    "authorship",
1403
    "editor",
1404
    "publisher",
1405
    "placePublished",
1406
    "datePublished",
1407
    "year",
1408
    "edition",// Class Book.
1409
    "volume",// Class Article.
1410
    "seriesPart",
1411
    "inReference",
1412
    "nomRefBase", // Class BookSection, Book, Article.
1413
    "pages",// Class Article.
1414
    "series",// Class Article, PrintSeries.
1415
    "school",// Class Thesis.
1416
    "institution",// Class Report.
1417
    "organization",// Class Proceedings.
1418
    "nextVersion",
1419
    "previousVersion",
1420
    "isbn",// Class Book.
1421
    "issn",// Class Journal.
1422
    "doi",
1423
    "uri"
1424
  );
1425

    
1426
  $table_rows = array();
1427

    
1428
  if (!isset($reference->authorship)) {
1429
    $authorship = cdm_ws_get(CDM_WS_REFERENCE_AUTHORTEAM, $reference->uuid);
1430
    $reference->authorship = isset($authorship->titleCache) ? $authorship->titleCache : '';
1431
  }
1432

    
1433
  if (!isset($reference->inReference)) {
1434
    $reference->inReference = cdm_ws_get(CDM_WS_REFERENCE, array(
1435
      $reference->uuid,
1436
      "inReference",
1437
    ));
1438
  }
1439

    
1440
  foreach ($field_order as $fieldname) {
1441

    
1442
    if (isset($reference->$fieldname)) {
1443

    
1444
      if ($fieldname == "datePublished") {
1445
        $period = $reference->$fieldname;
1446
        $datePublished = timePeriodToString($period);
1447
        if (isset($datePublished) && $datePublished != '') {
1448
          $table_rows[] = array(
1449
            t("Date published"),
1450
            $datePublished,
1451
          );
1452
        }
1453
      }
1454
      elseif ($fieldname == "doi" && is_object($reference->doi)) {
1455
        $table_rows[] = array(
1456
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1457
          cdm_doi($reference->doi, false)
1458
        );
1459
      }
1460
      elseif ($fieldname == "uri" && isset($reference->uri) && $reference->uri) {
1461
        $table_rows[] = array(
1462
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1463
          cdm_external_uri($reference->uri, false)
1464
        );
1465
      }
1466
      elseif (is_object($reference->$fieldname)) {
1467
        if ($fieldname == "authorship") {
1468
          $dump = $reference->$fieldname;
1469
          $teammembers = "teamMembers";
1470
          $team = $dump->$teammembers;
1471
          $nameArray = array();
1472

    
1473
          foreach ($team as $member) {
1474
            if (strlen($member->lastname) > 0) {
1475
              $nname = $member->lastname;
1476
              $name = $nname;
1477
              if (strlen($member->firstname) > 0) {
1478
                $vname = $member->firstname;
1479
                $name = $vname . " " . $nname;
1480
              }
1481
              $nameArray[] = $name;
1482
            }
1483
            else {
1484
              if (strlen($member->titleCache) > 0) {
1485
                $nameArray[] = $member->titleCache;
1486
              }
1487
            }
1488
          }
1489
          $value = join($nameArray, ", ");
1490
        }
1491
        elseif ($fieldname == "inReference") {
1492
          $type = $reference->$fieldname->type;
1493
          $value = l($reference->$fieldname->titleCache, path_to_reference($reference->$fieldname->uuid));
1494
          switch ($type) {
1495
            case "Book":
1496
              $fieldname = "in book";
1497
              break;
1498
            case "Journal":
1499
              $fieldname = "in journal";
1500
              break;
1501
            case "Proceedings":
1502
              $fieldname = "in proceedings";
1503
              break;
1504
          }
1505
        }
1506
        else {
1507
          $value = $reference->$fieldname->titleCache;
1508
        }
1509

    
1510

    
1511
        if (isset($value) && $value != '') {
1512
          $table_rows[] = array(
1513
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1514
            $value,
1515
          );
1516
        }
1517

    
1518
      }
1519
      else {
1520
        if (isset($reference->$fieldname) && $reference->$fieldname != '') {
1521
          $table_rows[] = array(
1522
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1523
            $reference->$fieldname,
1524
          );
1525
        }
1526
      }
1527
    }
1528
  }
1529

    
1530
  $out = theme_table(array(
1531
    'header' => array(),
1532
    'rows' => $table_rows,
1533
    'attributes' => array(
1534
      'class' => html_class_attribute_ref($reference)
1535
    ),
1536
    'caption' => NULL,
1537
    'colgroups' => NULL,
1538
    'sticky' => NULL,
1539
    'empty' => NULL,
1540
  ));
1541

    
1542
  if(isset($reference->referenceAbstract)){
1543
    $out .= '<h2 class="block-title">Abstract</h2><div class="abstract">' . $reference->referenceAbstract . '</div>';
1544
  }
1545

    
1546

    
1547
  // Annotations below the table
1548
  $annotations = cdm_fetch_visible_annotations($reference);
1549
  $out .= theme("cdm_annotations", array('annotations' => $annotations));
1550

    
1551
  $registration_working_set = cdm_ws_get("registrationWorkingSetDTO", array($uuid));
1552
  if($registration_working_set && count($registration_working_set->registrationDTOs) > 0){
1553
    $out .= "<h3>Nomenclatural acts:</h3><div class=\"cdm-item-list registration-item-list\">";
1554
    foreach($registration_working_set->registrationDTOs as $registration_dto){
1555
      if($registration_dto->status == "PUBLISHED"){
1556
        $registration_render_a = compose_registration_dto_compact($registration_dto, 'citation');
1557
        $registration_render_a["#prefix"] = "<div class=\"item item-registration\">";
1558
        $registration_render_a["#suffix"] = "</div>";
1559
        $out .= drupal_render($registration_render_a);
1560
      }
1561
    }
1562
    $out .= "</div>";
1563
  }
1564

    
1565
  RenderHints::popFromRenderStack();
1566

    
1567
  return markup_to_render_array($out);
1568
}
1569

    
1570
/**
1571
 * Provides the the label string for taxon page tabs.
1572
 *
1573
 * The $tabname as passed to the method will be returned if no override
1574
 * label is configured in the settings.
1575
 */
1576
function cdm_taxonpage_tab_label($tabname) {
1577
  static $taxon_tabs_labels = null;
1578
  if($taxon_tabs_labels == null){
1579
    $taxon_tabs_labels = get_array_variable_merged(CDM_TAXONPAGE_TAB_LABELS, CDM_TAXONPAGE_TAB_LABELS_DEFAULT);
1580
  }
1581
  $tabname_key = strtolower($tabname);
1582
  if(isset($taxon_tabs_labels[$tabname_key]) && $taxon_tabs_labels[$tabname_key]){
1583
    return $taxon_tabs_labels[$tabname_key];
1584
  }
1585
  return $tabname;
1586
}
(7-7/10)