Project

General

Profile

Download (48.8 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
 * Composes 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
 * @ingroup Compose
33
 *
34
 */
35
function compose_cdm_taxon_page_specimens($taxon) {
36

    
37
    $render_array = array();
38
    RenderHints::pushToRenderStack('taxon_page_specimens');
39

    
40
    $fieldUnitDTOs = null;
41
    $specimensOrObservations = null;
42
    if (variable_get('cdm_dataportal_specimen_derivate_tree')){
43
      // get fieldUnitDTOs
44
      $fieldUnitDTOs = cdm_ws_get(CDM_WS_TAXON_FIELDUNIT_DTOS, array( $taxon->uuid));
45
      $fieldUnitDTOs = order_fieldUnitDtos_by_date_and_type($fieldUnitDTOs);
46
    } else {
47
      // $specimensOrObservations = cdm_ws_get(CDM_WS_TAXON, array( $taxon->uuid, 'specimensOrObservations'));
48
      // extend by associated taxas occurrences
49
      $relationship_filter_query_parameters = relationship_filter_query_parameters();
50

    
51
      if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
52
        $relationship_filter_query_parameters = array_merge($relationship_filter_query_parameters, $_REQUEST['pager']);
53
      }
54
      $by_associatedtaxon_query = http_build_query($relationship_filter_query_parameters);
55
      $pager = cdm_ws_get(CDM_WS_OCCURRENCE_BY_ASSOCIATEDTAXON,
56
        null,
57
        $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
58
      );
59
      $specimensOrObservations = array();
60
      if (isset($pager->records[0])) {
61
        $specimensOrObservations = $pager->records;
62
      }
63
      // cdm_ws_get(CDM_WS_OCCURRENCE_FIELDUNIT_DTO_BY_ASSOCIATEDTAXON,
64
      // null,
65
      // $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
66
      // );
67

    
68
      // $specimensOrObservations = array();
69
      // if(isset($pager->records[0])){
70
      //    $specimensOrObservations =  $pager->records;
71
      // }
72
      // Collect media (fieldObjectMedia, derivedUnitMedia) and add as a custom field
73
      foreach ($specimensOrObservations as &$occurrence) {
74
        $occurrence->_fieldObjectMedia = cdm_ws_get(CDM_WS_DERIVEDUNIT_FACADE, array(
75
          $occurrence->uuid,
76
          'fieldObjectMediaDTO',
77
        ));
78
        $occurrence->_derivedUnitMedia = cdm_ws_get(CDM_WS_DERIVEDUNIT_FACADE, array(
79
          $occurrence->uuid,
80
          'derivedUnitMedia',
81
        ));
82
      }
83
      $specimensOrObservations = order_specimens_or_observations_by_date_and_type($specimensOrObservations);
84
    }
85

    
86
    // --- get map service HTTP query parameters
87
    if (count($specimensOrObservations) > 0 || $fieldUnitDTOs > 0) {
88
        $render_array['map'] = occurrence_map_query_parameters($taxon);
89
    }
90

    
91
    // -------------------------------------------------------
92

    
93
    if(variable_get('cdm_dataportal_compressed_specimen_derivate_table')){
94

    
95
        //COMPRESSED SPECIMEN DERIVATE TABLE
96
        $associatedFieldUnitsQuery_parameters = array();
97
        if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
98
            $associatedFieldUnitsQuery_parameters = array_merge($associatedFieldUnitsQuery_parameters, $_REQUEST['pager']);
99
        }
100

    
101
        $by_associatedtaxon_query = http_build_query($associatedFieldUnitsQuery_parameters);
102
        $pager_field_units = cdm_ws_get(CDM_WS_PORTAL_TAXON,
103
            array($taxon->uuid, 'associatedFieldUnits'),
104
            $by_associatedtaxon_query . '&pageSize=' . variable_get('cdm_dataportal_compressed_specimen_derivate_table_page_size')
105
        );
106

    
107
        if (isset($pager_field_units->records[0])) {
108
          $field_unit_uuids = array();
109
          foreach ($pager_field_units->records as $field_unit) {
110
            $field_unit_uuids[] = $field_unit->uuid;
111
          }
112

    
113
          $render_array['derivate_hierarchy_table'] = compose_compressed_specimen_derivate_table($field_unit_uuids);
114
        }
115

    
116
        $render_array['pager'] = markup_to_render_array(
117
            theme('cdm_pager', array(
118
                'pager' => $pager_field_units,
119
                'path' => $_REQUEST['q'],
120
                'parameters' => $_REQUEST
121
            )),
122
            10 // weight
123
        );
124
    }
125
    else if(variable_get('cdm_dataportal_specimen_derivate_tree')){
126
      $render_array['specimen_list'] = compose_specimen_table_top_down($fieldUnitDTOs);
127
      $render_array['specimen_list']['#weight'] = 2;
128
    } else {
129
      $specimen_table = compose_specimens_table_bottom_up($specimensOrObservations);
130

    
131
      $render_array['specimen_list'] = $specimen_table;
132
      $render_array['pager'] = markup_to_render_array(
133
        theme('cdm_pager', array(
134
          'pager' => $pager,
135
          'path' => $_REQUEST['q'],
136
          'parameters' => $_REQUEST,
137
        )),
138
        10 // weight
139
      );
140
    }
141

    
142
    RenderHints::popFromRenderStack();
143
    return $render_array;
144
}
145

    
146
/**
147
 * @return array
148
 *
149
 * TODO move to cdm_dataportal.module or api.module?
150
 */
151
function relationship_filter_query_parameters()
152
{
153
  $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
154
  $relationship_choice['direct'] = get_selection($relationship_choice['direct']);
155
  $relationship_choice['invers'] = get_selection($relationship_choice['invers']);
156

    
157
  $by_associatedtaxon_query_parameters = array(
158
    'relationshipsInvers' => implode(',', $relationship_choice['invers']),
159
    'relationships' => implode(',', $relationship_choice['direct']),
160
  );
161
  return $by_associatedtaxon_query_parameters;
162
}
163

    
164

    
165
function create_html_link($link, $openInExternalWindow=false){
166
    $html = "";
167
    if($link->uri && $link->uri!=""){
168
        $html .= '<a  href="' . $link->uri . '"';
169
        if($openInExternalWindow){
170
            $html .= ' target="_blank"';
171
        }
172
        $html .= '>' . $link->linkText . '</a>';
173
    }
174
    else{
175
        $html .= $link->linkText;
176
    }
177
    return $html;
178
}
179

    
180
/**
181
 * Creates HTML links from the given link list concatenated by default by a comma.
182
 * @param $linkList the list with Link objects having "uri" and "linkText" as members
183
 * @return string the assembled HTML string containing the links
184
 */
185
function create_html_links($linkList, $openInExternalWindow=false, $separator=", ")
186
{
187
    $html = "";
188
    if ($linkList) {
189
        foreach ($linkList as $link) {
190
            $html .= create_html_link($link, $openInExternalWindow).$separator;
191
        }
192
        $html = rtrim($html, $separator);
193
    }
194
    return $html;
195
}
196

    
197
/**
198
 * Composes a taxon page which can consist of multiple parts like
199
 * 'General', 'Synonymy', 'Images', 'Keys'. These parts can be displayed
200
 * as tabs or as sections of a single page.
201
 *
202
 * It is headed by the name of the accepted taxon without author and reference.
203
 *
204
 * @param $taxon
205
 *   The CDM Taxon Instance to compose the page for.
206
 * @param $page_part
207
 *   Name of the part to display, valid values are:
208
 *    - 'description' -  for the general part
209
 *    - 'images'
210
 *    - 'synonymy'
211
 *    - 'keys'
212
 *    - 'all'
213
 *
214
 * @return array
215
 *   A drupal render array
216
 *
217
 * @ingroup compose
218
 */
219
function compose_cdm_taxon_page($taxon, $page_part = 'description') {
220

    
221
  // we better cache here since drupal_get_query_parameters has no internal static cache variable
222
  $http_request_params = drupal_get_query_parameters();
223

    
224
  // add all mandatory js sources
225
  _add_js_footnotes();
226

    
227

    
228
  $render_array = array();
229
  $weight = 0; // the weight for the render array elements
230

    
231
  $tabsToDisplay = variable_get('cdm_taxonpage_tabs_visibility', unserialize(TAXONPAGE_VISIBILITY_OPTIONS_DEFAULT));
232

    
233
  $page_part = variable_get('cdm_dataportal_taxonpage_tabs', 1) ? $page_part : 'all';
234

    
235
  $synonymy_as_tab = variable_get(CDM_SYNONYMY_AS_TAB, CDM_SYNONYMY_AS_TAB_DEFAULT) === 1;
236
  if(!$synonymy_as_tab){
237
    unset($tabsToDisplay["Synonymy"]);
238
    // the synonymy is located in the general part in this case
239
    if($page_part == 'synonymy'){
240
      $page_part = 'description';
241
    }
242
  }
243

    
244
  $media = _load_media_for_taxon($taxon);
245

    
246

    
247
  if (!isset($media[0]) || ($tabsToDisplay["Images"] == '0')) {
248
    taxon_page_tabs_hidden_add('images');
249
  }
250

    
251
  // --- GET specimensOrObservations --- //
252
  $specimensOrObservations = cdm_ws_get(CDM_WS_TAXON, array( $taxon->uuid, 'specimensOrObservationsCount'));
253

    
254
  $specimensOrObservationsCount = $specimensOrObservations != null ? $specimensOrObservations->result : 0;
255
  if ($specimensOrObservationsCount == 0 || ($tabsToDisplay["Specimens"] == '0')) {
256
    taxon_page_tabs_hidden_add('specimens');
257
  }
258

    
259
  // --- GET polytomousKeys --- //
260
  $polytomousKeysPager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, NULL, "findByTaxonomicScope=$taxon->uuid");
261
  $identificationKeyCount = 0;
262
  if ($polytomousKeysPager) {
263
    $identificationKeyCount += $polytomousKeysPager->count;
264
  }
265
  if ($identificationKeyCount == 0 || ($tabsToDisplay["Keys"] == '0')) {
266
    taxon_page_tabs_hidden_add('keys');
267
  }
268

    
269
  // --- GET TaxonNodeAgentRelations --- //
270
  $current_classification_uuid = get_current_classification_uuid();
271
  $taxon_node_agent_relations_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
272
      array(
273
          $taxon->uuid,
274
          $current_classification_uuid,
275
      ),
276
      "pageSize=1&pageIndex=0"// we are only interested into the count so we are fetching only one item, o is not possible!
277
  );
278
  if (!$taxon_node_agent_relations_pager || $taxon_node_agent_relations_pager->count == 0){
279
      taxon_page_tabs_hidden_add('experts');
280
  }
281

    
282
  if (!isset($tabsToDisplay["Synonymy"]) || $tabsToDisplay["Synonymy"] == '0') {
283
    taxon_page_tabs_hidden_add('synonymy');
284
  }
285

    
286
  // -------------------------------------------- //
287

    
288
  if (variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR) && isset($_REQUEST['acceptedFor'])) {
289
    $render_array['accepted_for'] = markup_to_render_array(cdm_accepted_for($_REQUEST['acceptedFor']), $weight++);
290
  }
291

    
292
  // --- PAGE PART: DESCRIPTION --- //
293
  if (!taxon_page_tabs_hidden_check('description') && ($page_part == 'description' || $page_part == 'all')) {
294

    
295
    $merged_tree = merged_taxon_feature_tree($taxon);
296

    
297

    
298
    $render_array['general'] = compose_cdm_taxon_page_profile($taxon, $merged_tree, $media, !$synonymy_as_tab);
299
    $render_array['general']['#weight'] = $weight++;
300
    $render_array['general']['#prefix'] = '<div id="general" class="page-part">';
301
    $render_array['general']['#suffix'] = '</div>';
302
  }
303

    
304
  // --- PAGE PART: IMAGES --- //
305
  if (!taxon_page_tabs_hidden_check('images') && ($page_part == 'images' || $page_part == 'all')) {
306
    $images_html = '<div id="images" class="page-part">';
307
    if ($page_part == 'all') {
308
      $images_html .= '<h2>' . t(cdm_taxonpage_tab_label('Images')) . '</h2>';
309
    }
310
    // Get the image gallery as configured by the admin.
311
    $default_image_gallery = 'default';
312
    $configured_image_gallery_viewer = variable_get('image_gallery_viewer', 'default');
313
    $taxon_image_gallery = '<div class="error">No image gallery viewer configured</div>';
314
    if($configured_image_gallery_viewer == $default_image_gallery || $configured_image_gallery_viewer == 'fsi'){
315
      // the fsi_gallery requires a flash plugin, in case the client browser is not supporting
316
      // flash we also need to provide an the default gallery as alternative
317
      $taxon_image_gallery = '<div id="default-gallery-viewer">' .
318
        call_user_func_array('taxon_image_gallery_' . $default_image_gallery, array(
319
          $taxon,
320
          $media,
321
        ))
322
        . '</div>';
323
    }
324
    if($configured_image_gallery_viewer == 'fsi'){
325
      $taxon_image_gallery .= '<div id="'. $configured_image_gallery_viewer .'-gallery-viewer">' .
326
        call_user_func_array('taxon_image_gallery_' . $configured_image_gallery_viewer, array(
327
          $taxon,
328
          $media,
329
        ))
330
       . '</div>';
331
    }
332
    $images_html .= $taxon_image_gallery;
333
    $images_html .= '</div>';
334
    $render_array['images'] = markup_to_render_array($images_html, $weight++);
335
  }
336

    
337
  // --- PAGE PART: SYNONYMY --- //
338
  if (!taxon_page_tabs_hidden_check('synonymy') && (($page_part == 'synonymy' || $page_part == 'all') && $synonymy_as_tab)) {
339
    $synonymy_html = '<div id="synonymy" class="page-part">';
340
    if ($page_part == 'all') {
341
      $synonymy_html .= '<h2>' . t(cdm_taxonpage_tab_label('Synonymy')) . '</h2>';
342
    }
343
    $addAcceptedTaxon = variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE);
344

    
345
    $synonym_a = compose_cdm_taxon_page_synonymy($taxon, $addAcceptedTaxon);
346
    $synonymy_html .= drupal_render($synonym_a);
347

    
348
    $synonymy_html .= '</div>';
349
    $render_array['synonymy'] = markup_to_render_array($synonymy_html, $weight++);
350

    
351
  }
352

    
353
  // --- PAGE PART: SPECIMENS --- //
354
  if (!taxon_page_tabs_hidden_check('specimens') && ($specimensOrObservationsCount > 0 && ($page_part == 'specimens' || $page_part == 'all'))) {
355
    $render_array['specimens'] = array(
356
        '#prefix' => '<div id="specimens" class="page-part">' . ($page_part == 'all' ? '<h2>' . t(cdm_taxonpage_tab_label('Specimens')) . '</h2>' : ''),
357
        'content' => compose_cdm_taxon_page_specimens($taxon), // returns render array
358
        '#suffix' => '</div>',
359
    );
360
  }
361

    
362
  // --- PAGE PART: KEYS --- //
363
  if(!taxon_page_tabs_hidden_check('keys')){
364
    if ($identificationKeyCount == 1 && $page_part == 'keys'){
365
      drupal_goto(path_to_key($polytomousKeysPager->records[0]->class, $polytomousKeysPager->records[0]->uuid));
366
    }
367
    else if ($identificationKeyCount > 0 && ($page_part == 'keys' || $page_part == 'all')) {
368
      $keys_html = '<div id="keys" class="page-part">';
369
      if ($page_part == 'all') {
370
        $keys_html .= '<h2>' . t(cdm_taxonpage_tab_label('Keys')) . '</h2>';
371
      }
372
      $keys_html .= theme('cdm_block_IdentificationKeys', array('taxonUuid' => $taxon->uuid));
373
      $keys_html .= '</div>';
374
      $render_array['keys'] = markup_to_render_array($keys_html, $weight++);
375
    }
376
  }
377

    
378
  // --- PAGE PART: EXPERTS --- //
379

    
380
  if (!taxon_page_tabs_hidden_check('experts') && ($page_part == 'experts' || $page_part == 'all')) {
381
    $render_array['experts'] = array(
382
        '#prefix' => '<div id="experts" class="page-part">' . ($page_part == 'all' ? '<h2>' . t(cdm_taxonpage_tab_label('Experts')) . '</h2>' : ''),
383
        'content' => compose_cdm_taxon_page_experts($taxon), // returns render array
384
        '#suffix' => '</div>',
385
    );
386
  }
387

    
388
  // ------------------ END OF PARTS -------------- //
389

    
390
  // adjust weights of page and toc elements according to the settings
391
  $taxontabs_weights = get_array_variable_merged(CDM_TAXONPAGE_TAB_WEIGHT, CDM_TAXONPAGE_TAB_WEIGHT_DEFAULT);
392
  foreach($taxontabs_weights as $tab_key => $weight){
393
    if(isset($render_array[$tab_key])){
394
      $render_array[$tab_key]['#weight'] = $weight;
395
    }
396
  }
397

    
398

    
399
  // set up the TOC for the pages which contain all pageparts
400
  if($page_part == 'all') {
401

    
402
    asort($taxontabs_weights);
403
    foreach(array_keys($taxontabs_weights) as $tab_key){
404
      if(isset($render_array[$tab_key])){
405
        if($tab_key != 'general'){
406
          // add entry for page part
407
          $toc_elements[] = array(
408
              'data' => l(t(cdm_taxonpage_tab_label(ucfirst($tab_key))), $_GET['q'], array('fragment' => $tab_key, 'query' => $http_request_params)),
409
              'class' => array('page-part-toc-item-' . $tab_key)
410
          );
411
        } else {
412
          // add content of profile part instead
413
          if(isset($render_array['general'])) {
414
            // in case all tabs are shown at once the feature tocs
415
            // should be integrated into the tabs toc as sub list
416
            // and the profile image should be on top of the page
417
            if(isset($render_array['general']['taxon_description_feature_toc'])){;
418
            foreach ($render_array['general']['taxon_description_feature_toc']['#items'] as $profile_toc_item){
419
              $toc_elements[] = $profile_toc_item;
420
            }
421
            unset($render_array['general']['taxon_description_feature_toc']);
422
            }
423
          }
424
        }
425
      }
426
    }
427

    
428
    // move profile image in page structure
429
    if(isset($render_array['general']['taxon_profile_image'])){
430
      $render_array['profile_image'] = $render_array['general']['taxon_profile_image'];
431
      $render_array['profile_image']['#weight'] = -100;
432
      unset($render_array['general']['taxon_profile_image']);
433
    }
434

    
435
    // finally add the table of contents to the render array
436
    $render_array['toc'] = array(
437
        '#theme' => 'item_list',
438
        '#items' => $toc_elements,
439
        '#title' => t('Content'),
440
        '#weight' => -101,
441
        '#suffix' => '</div>',
442
        '#prefix'=> '<div id="page-toc">'
443
    );
444
  }
445

    
446

    
447
  return $render_array;
448
}
449

    
450
/**
451
 * TODO should this function really be a compose function?
452
 *     For a compose function must there always be a theme function with the same name? (ak 8.8.2013)
453
 *
454
 * composes and returns an render array containing the components of the taxon profile tab:
455
 *  - 'taxon_profile_image'
456
 *  - 'taxon_description_feature_toc'
457
 *  - 'taxon_description_features'
458
 *
459
 *
460
 * @param object taxon
461
 * @param object $merged_tree
462
 * @param object media
463
 * @param bool $add_synonymy
464
 *
465
 * @return array
466
 *   A Drupal render array with the following elements:
467
 *     - 'taxon_profile_image'
468
 *     - 'taxon_description_feature_toc'
469
 *     - 'taxon_description_features'
470
 *
471
 * @throws Exception
472
 *
473
 * @ingroup compose
474
 */
475
function compose_cdm_taxon_page_profile($taxon, $merged_tree, $media, $add_synonymy) {
476

    
477
  $render_array = array();
478

    
479
  $taxon_profile_image_settings = variable_get(CDM_TAXON_PROFILE_IMAGE, unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT));
480

    
481
  $hide_taxon_profile_image = FALSE;
482
  if (variable_get('image_hide_rank', '0') != '0' && isset($taxon->name->rank->uuid)) {
483
    $rankCompare = rank_compare($taxon->name->rank->uuid, variable_get('image_hide_rank', '-99'));
484
    $hide_taxon_profile_image = ($rankCompare > -1);
485
  }
486

    
487
  if ($taxon_profile_image_settings['show'] && !$hide_taxon_profile_image) {
488

    
489
    $representationPart = new stdClass();
490
    $attributes = array();
491
    if (isset($media[0])) {
492
      // due to a bug the portal/taxon/{uuid}/media service only delivers a filtered media object
493
      // which only contains the thumbnail representation even if the height and width filters are not set.
494
      // -->
495
      $preferred_media = cdm_ws_get(CDM_WS_MEDIA, $media[0]->uuid);
496
      $preferred_representations = cdm_preferred_media_representations($preferred_media, array(
497
        'image/jpg',
498
        'image/jpeg',
499
        'image/png',
500
        'image/gif',
501
      ),
502
        $taxon_profile_image_settings['maxextend'],
503
        $taxon_profile_image_settings['maxextend']
504
      );
505
      if(count($preferred_representations) > 0){
506

    
507
        $representation = array_shift($preferred_representations);
508
        $representationPart = $representation->parts[0];
509
        $attributes['alt'] = $representationPart->uri;
510

    
511
        if (!empty($taxon_profile_image_settings['media_uri_query'])) {
512
          $representationPart->uri = $representationPart->uri
513
            . (strpos($representationPart->uri, '?') !== FALSE ? '&' : '?')
514
            . $taxon_profile_image_settings['media_uri_query'];
515
        }
516
      }
517
    }
518
    else {
519
      if ($taxon_profile_image_settings['custom_placeholder_enabled']) {
520
        // show placeholder image instead
521
        if (!empty($taxon_profile_image_settings['custom_placeholder_image_on']) && !empty($taxon_profile_image_settings['custom_placeholder_image_fid'])) {
522
          // use the user provided image
523
          $profile_image_file = file_load($taxon_profile_image_settings['custom_placeholder_image_fid']);
524
          $url = file_create_url($profile_image_file->uri);
525
          $image_info = image_get_info($profile_image_file->uri);
526
          $representationPart->width = $image_info['width'];
527
          $representationPart->height = $image_info['height'];
528
          $representationPart->uri = $url;
529
        }
530
        else {
531
          // use the hard coded default
532
          $representationPart->width = 184;
533
          $representationPart->height = 144;
534
          $representationPart->uri = base_path() . drupal_get_path('module',
535
              'cdm_dataportal') . '/images/no_picture.png';
536
        }
537
        $attributes['alt'] = "no image available";
538
      }
539
    }
540

    
541
    if (isset($representationPart->uri)) {
542
      $profile_image = cdm_media_gallerie_image($representationPart, $taxon_profile_image_settings['maxextend'], FALSE, $attributes);
543
      // NOTE: style="width:${maxextend}px' is needed for IE8 !!!
544
      $max_extend_with = $taxon_profile_image_settings['maxextend'] . 'px';
545
      $render_array['taxon_profile_image'] = markup_to_render_array('<div id="taxonProfileImage" style="width:' . $max_extend_with . '">' . $profile_image . '</div>',
546
        -101);
547
    }
548
  }
549

    
550
  if($add_synonymy){
551
    $synonymy_a = compose_cdm_taxon_page_synonymy($taxon, true);
552
    $synonymy_a['#weight'] = -102;
553
    $render_array['synonymy'] = $synonymy_a;
554
  }
555

    
556
  $pseudo_feature_blocks = array();
557
  $pseudo_feature_block_toc_items = array();
558

    
559

    
560
  // Render the sections for each real feature
561
  $feature_block_list = make_feature_block_list($merged_tree->root->childNodes, $taxon);
562

    
563
  // Bibliography
564
  $bibliography_settings = get_bibliography_settings();
565
  if($bibliography_settings['enabled'] == 1){
566
    $bibliography_markup = FootnoteManager::renderFootnoteList('BIBLIOGRAPHY', '');
567
    if($bibliography_markup) {
568
      $feature_bibliography = make_pseudo_feature('Bibliography', 'BIBLIOGRAPHY');
569
      $bibliography_item = markup_to_render_array($bibliography_markup);
570
      $bibliography_block = feature_block(t('Bibliography'), $feature_bibliography);
571
      $bibliography_block->content = array();
572
      $bibliography_block->content[] = compose_feature_block_wrap_elements(array($bibliography_item), $feature_bibliography);
573

    
574
      $pseudo_feature_block_toc_items['Bibliography']= 'bibliography';
575
      $pseudo_feature_blocks[] = $bibliography_block;
576
    }
577
  }
578

    
579
    $descriptionTypes = array();
580
    $descriptionTypes['descriptionTypes'] = ("AGGREGATED");
581
    $aggregatedDescriptions = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXON . '/' . $taxon->uuid . '/descriptions', $descriptionTypes);
582
    if (isset($aggregatedDescriptions) and !empty($aggregatedDescriptions)) {
583
        // descriptions pseudo feature
584
        $feature_description = make_pseudo_feature('Aggregation Descriptions', 'AGGREGATION_DESCRIPTIONS');
585
        $description_item = "";
586
        foreach ($aggregatedDescriptions as $description) {
587
            $description_item .= render_cdm_description_link($description);
588
        }
589
        $description_block = feature_block(t('Aggregation Descriptions'), $feature_description);
590
        $description_block->content = [];
591
        $description_block->content[] = compose_feature_block_wrap_elements([$description_item], $feature_description);
592

    
593
        $pseudo_feature_block_toc_items['Aggregation Descriptions'] = 'aggregation_descriptions';
594
        $pseudo_feature_blocks[] = $description_block;
595

    
596
    }
597
    $render_array['taxon_description_features'] = _block_get_renderable_array(
598
        array_merge($feature_block_list, $pseudo_feature_blocks)
599
    );
600

    
601
    if ($pseudo_feature_block_toc_items){
602
    foreach ($pseudo_feature_block_toc_items as $label=>$fragment){
603
      cdm_toc_list_add_item($label, $fragment);
604
    }
605
  }
606

    
607
  // create the table of content
608
  $toc = array(
609
      '#theme' => 'item_list',
610
    '#items' => cdm_toc_list(),
611
      '#title' => t('Content'),
612
    '#weight' => -100,                  // move to the top
613
      '#suffix' => '</div>',
614
      '#prefix'=> '<div id="page-toc">'
615
  );
616
  $render_array['taxon_description_feature_toc'] = $toc;
617

    
618
  return $render_array;
619
}
620

    
621
/**
622
 * Renders the link which will lead to the description detail page
623
 * @param $description the description which will be linked to
624
 *
625
 * @return string the HTML
626
 */
627
function render_cdm_description_link($description) {
628
  $pathToDescription = path_to_description($description->uuid);
629
  $attributes['class'][] = html_class_attribute_ref($description);
630
  $descriptionPageLink = l($description->titleCache, $pathToDescription, array('attributes' => $attributes));
631
  $detail_html = $descriptionPageLink."<br>";
632
  return $detail_html;
633
}
634

    
635
/**
636
 * Renders the link which will lead to the specimen detail page
637
 * @param $specimen the specimen which will be linked to
638
 *
639
 * @return string the HTML
640
 */
641
function render_cdm_specimen_link($specimen) {
642
  $path = path_to_specimen($specimen->uuid);
643
  $attributes['class'][] = html_class_attribute_ref($specimen);
644
  $link = l($specimen->titleCache, $path, array('attributes' => $attributes));
645
  return $link;
646
}
647

    
648
/**
649
 * Returns HTML containing the synonymy for the accepted taxon.
650
 *
651
 * Shows the whole synonymy for the accepted taxon.
652
 * The synonymy list is headed by the complete scientific name
653
 * of the accepted taxon with nomenclatural reference.
654
 *
655
 * @param object $taxon
656
 * @param boolean $add_accepted_taxon
657
 *
658
 * @return array
659
 *  Drupal render array for the synonymy
660
 *
661
 * @throws Exception
662
 *
663
 * @ingroup compose
664
 */
665
function compose_cdm_taxon_page_synonymy($taxon, $add_accepted_taxon) {
666

    
667
  RenderHints::pushToRenderStack('taxon_page_synonymy');
668

    
669
  // footnote key for the homotypic group and accepted taxon,
670
  // both should have the same footnote key
671
  RenderHints::setFootnoteListKey(RenderHints::getRenderPath());
672

    
673
  $synomymie = cdm_ws_get(CDM_WS_PORTAL_TAXON_SYNONYMY, array($taxon->uuid));
674

    
675
  $out = '';
676

    
677
  // Render accepted taxon.
678
  //
679
  // foonotes of the accepted taxon will be rendered in the homotypic group section
680
  // even if there are not synonyms in the homotypic group
681
  // homotypic group and accepted taxon should have the same footnote key
682
  $referenceUri = '';
683
  if ($add_accepted_taxon) {
684
    // remember the last part of the render path
685
    $synonymy_render_path = RenderHints::getRenderPath();
686
    // set new render path for the accepted taxon so
687
    // it can be styled differently via the name render part definitions
688
    RenderHints::pushToRenderStack('accepted_taxon');
689
    $accepted_name = '';
690
    if (variable_get(CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE, 0)) {
691
      $label = variable_get(CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE_LABEL, CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE_LABEL_DEFAULT);
692
      $accepted_name .= '<div class="secReference"><span class="label">' . t($label) . ':</span> ' . $taxon->sec->titleCache . '</div>';
693
    }
694
    if (isset($taxon->name->nomenclaturalReference)) {
695
      $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
696
    }
697

    
698
    $accepted_name .= '<div class="accepted-name">';
699
    $accepted_name .= render_taxon_or_name($taxon, NULL, $referenceUri);
700

    
701
    $name_relations = cdm_name_relationships_for_taxon($taxon);
702
    $name_relationships = compose_name_relationships_inline($name_relations, $taxon->name->uuid, $taxon->uuid);
703
    // Render relationships of accepted name.
704
    if(isset($name_relationships['list']['items'][0])){
705
      $accepted_name .= ' ' . drupal_render($name_relationships);
706
    }
707

    
708
    // handle annotations of the name and taxon
709
    $special_annotations_array = array();
710
    $special_annotations_array[] = $taxon->name;
711
    $special_annotations_array[] = $taxon;
712
    $accepted_name .= theme('cdm_annotations_as_footnotekeys', array(
713
        'cdmBase_list' => $special_annotations_array,
714
        'footnote_list_key' => $synonymy_render_path . '-annotations')
715
    );
716
    $accepted_name .= '</div>';
717
    RenderHints::popFromRenderStack();
718
  }
719

    
720
  // --- Render homotypic synonymy group
721
  if (!empty($accepted_name)) {
722
    $out .= $accepted_name;
723
  }
724

    
725
  // Render the homotypicSynonymyGroup including the type information.
726
  $out .= theme(
727
    'cdm_homotypicSynonymyGroup',
728
    array(
729
      'synonymList' => $synomymie->homotypicSynonymsByHomotypicGroup,
730
      'accepted_taxon_name_uuid' => $taxon->name->uuid
731
    )
732
  );
733

    
734

    
735
  // Render accepted taxon heterotypic synonymy groups.
736
  if ($synomymie->heterotypicSynonymyGroups) {
737
    foreach ($synomymie->heterotypicSynonymyGroups as $homotypicalGroup) {
738
      $out .= theme('cdm_heterotypicSynonymyGroup', array('homotypicalGroup' => $homotypicalGroup));
739
    }
740
  }
741
  // Render taxon relationships.
742
  if (variable_get(CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT)) {
743
    $taxonRelationshipsDTO = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS_DTO, $taxon->uuid);
744
    $out .= cdm_taxonRelationships($taxonRelationshipsDTO, $taxon);
745
  }
746

    
747
  RenderHints::popFromRenderStack();
748

    
749
  return markup_to_render_array($out);
750
}
751

    
752

    
753
/**
754
 * composes and returns an render array for the experts associated with the given taxon
755
 *
756
 * @param object taxon
757
 *
758
 * @return array
759
 *   A Drupal render array for a table with the experts
760
 *
761
 * @ingroup compose
762
 */
763
function compose_cdm_taxon_page_experts($taxon){
764

    
765
  $render_array = array();
766
  if(!isset($taxon->uuid)){
767
    return $render_array;
768
  }
769

    
770
  $current_classification_uuid = get_current_classification_uuid();
771
  // TODO use cdm_ws_fetchall below but this failes! needs fix!
772
  $taxon_node_agent_relations = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
773
    array(
774
      $taxon->uuid,
775
      $current_classification_uuid
776
    )
777
  );
778

    
779
  $header = array(
780
    array('data' => t('Expert')),
781
    array('data' => t('Role'))
782
  );
783
  $rows = array();
784

    
785

    
786
  foreach($taxon_node_agent_relations->records as $taxon_node_agent_relation){
787

    
788

    
789
    $expert_role_id = $taxon_node_agent_relation->agent->uuid . '-' . $taxon_node_agent_relation->type->uuid;
790
    $expert_details_container_id = 'expert_details_' . $expert_role_id;
791

    
792
    $agent_label_markup = cdm_dynabox(
793
      'expert_' . $expert_role_id,
794
      $taxon_node_agent_relation->agent->titleCache,
795
      // specifying both ends of the relationship will return only one record in the pager
796
      cdm_compose_url(CDM_WS_PORTAL_AGENT,
797
         array($taxon_node_agent_relation->agent->uuid, 'taxonNodeAgentRelations'),
798
        'taxon_uuid=' . $taxon->uuid . '&relType_uuid=' . $taxon_node_agent_relation->type->uuid),
799
      'cdm_taxon_expert',
800
      'Click for details',
801
      array('div', 'div'),
802
      array(), // attributes
803
      '#' . $expert_details_container_id // $content_element_selector
804
    );
805

    
806
    // Expert and Role
807
    $rows[] = array(
808
      'data' => array(
809
        array(
810
          'data' => $agent_label_markup,
811
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->agent))
812
        ),
813
        array(
814
          'data' => $taxon_node_agent_relation->type->representation_L10n,
815
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->type))
816
        )
817
      )
818
    );
819
    // Agent details
820
    $rows[] = array(
821
      'data' => array(
822
        array(
823
          'data' => '<!-- expert_details_container -->',
824
          'id' => $expert_details_container_id,
825
          'colspan' => 2
826
        )
827
      )
828
    );
829

    
830
  }
831

    
832

    
833
  $render_array['experts_table'] = array(
834
    '#theme' => 'table',
835
    '#header' => $header,
836
    '#rows' => $rows,
837
  );
838

    
839

    
840
  return $render_array;
841
}
842

    
843

    
844
/**
845
 * Manages the tabs to be hidden in the taxon page.
846
 *
847
 * The tabs are identified by their last menu link path element:
848
 *  - description
849
 *  - synonymy
850
 *  - images
851
 *  - specimens
852
 *  - key
853
 *
854
 * Internally the tabs are stored in a static variable which is
855
 * managed by drupal_static().
856
 *
857
 * @param string $add_tab
858
 *   Optional parameter. The given string will be added to the array of tabs
859
 *
860
 * @return
861
 *   The array of tabs
862
 */
863
function taxon_page_tabs_hidden_add($add_tab = NULL) {
864
  $tabs = &drupal_static('taxon_page_tabs_hidden');
865

    
866
  if(!isset($tabs)){
867
    $tabs = array();
868
  }
869

    
870
  if (isset($add_tab) && !array_key_exists($add_tab, $tabs)) {
871
    $tabs[] = $add_tab;
872
  }
873

    
874
  return $tabs;
875
}
876

    
877
/**
878
 * Manages the tabs to be hidden in the taxon page.
879
 *
880
 * The tabs names are identified by their last menu link path element:
881
 *  - description
882
 *  - synonymy
883
 *  - images
884
 *  - specimens
885
 *  - key
886
 *
887
 * Internally the tabs are stored in a static variable which is
888
 * managed by drupal_static().
889
 *
890
 * @param string $tabname
891
 *   The name of the tab to check
892
 *
893
 * @return boolean
894
 *   True if the tab or section is to be hidden
895
 */
896
function taxon_page_tabs_hidden_check($tabname) {
897

    
898
  $tabs = &drupal_static('taxon_page_tabs_hidden');
899

    
900
  if(!isset($tabs)){
901
    $tabs = array();
902
  }
903

    
904
  return array_search($tabname, $tabs) !== FALSE;
905
}
906

    
907
/**
908
 * Implements the hook_preprocess_HOOK() for theme_menu_local_tasks()
909
 *
910
 *  - Removes the tabs to be hidden, @see taxon_page_tabs_hidden_add()
911
 *  - Renames tabs according to the settings // TODO (this will replace the theme_cdm_taxonpage_tab() function !!!)
912
 *
913
 * @param array $variables
914
 *   The variables array
915
 */
916
function cdm_dataportal_preprocess_menu_local_tasks(&$variables) {
917

    
918
  $hidden_tabs = taxon_page_tabs_hidden_add();
919

    
920
  if (!variable_get(CDM_SEARCH_BLAST_ENABLED)) {
921
      if (is_array($variables['primary'])) {
922
          foreach ($variables['primary'] as $key => &$element) {
923
              if ($element['#link']['path'] == 'cdm_dataportal/search/blast') {
924
                  // remove the tab
925
                  unset($variables['primary'][$key]);
926
              }
927

    
928
          }
929
      }
930
  }
931
  if (is_array($variables['primary'])) {
932
    foreach ($variables['primary'] as $key => &$element) {
933

    
934
      // 1. Remove the tabs to be hidden
935
      foreach ($hidden_tabs as $tab) {
936
        if ($element['#link']['path'] == 'cdm_dataportal/taxon/%/' . $tab) {
937
          // remove the tab
938
          unset($variables['primary'][$key]);
939
        }
940
      }
941
    }
942
  }
943
}
944

    
945

    
946

    
947
/**
948
 * Implements the hook_preprocess_HOOK() for theme_menu_local_task()
949
 *
950
 *
951
 * @param array $variables
952
 *   An associative array containing:
953
 *     - element: A render element containing:
954
 *          #link: A menu link array with 'title', 'href', and 'localized_options' keys.
955
 *          #active: A boolean indicating whether the local task is active.
956
 *
957
 */
958
function cdm_dataportal_preprocess_menu_local_task(&$variables) {
959

    
960
  $link = $variables['element']['#link'];
961
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
962
    $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']) . '"/>';
963
    $link['localized_options']['html'] = TRUE;
964

    
965
    $variables['element']['#link'] = $link;
966
  }
967
}
968

    
969
/* =================== block composition ===================== */
970

    
971
/**
972
 * Composes and returns an render array for the classification breadcrumbs of the given taxon.
973
 *
974
 * @param taxon
975
 *
976
 * @return array
977
 *   A Drupal render array for a table with the experts
978
 *
979
 * @ingroup compose
980
 */
981
function compose_classification_breadcrumbs($taxon_uuid) {
982

    
983
  _add_js_taxonomic_children('#classification-breadcrumbs .taxonomic-children-button');
984

    
985
  $render_array = array();
986

    
987
  $render_array['#theme'] = 'item_list';
988
  $render_array['#type'] = 'ul';
989
  $render_array['#attributes'] = array(
990
    'id' => 'classification-breadcrumbs',
991
    'class' => 'breadcrumbs inline',
992
  );
993

    
994
  $items = array();
995

    
996
  $parent_taxon_nodes = null;
997
  if($taxon_uuid){
998
    $parent_taxon_nodes = cdm_ws_taxonomy_pathFromRoot($taxon_uuid);
999
  }
1000

    
1001
  $classifications = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXONOMY);
1002
  // find current classification in list
1003
  $classification = null;
1004
  $current_classification_uuid = get_current_classification_uuid();
1005
  foreach ($classifications as $classification){
1006
    if($classification->uuid == $current_classification_uuid){
1007
      break;
1008
    }
1009
  }
1010

    
1011
  $node_name = '';
1012
  if(count($classifications) > 1 ){
1013
    // need to add the current classification as first label
1014

    
1015
    $label = $classification->titleCache;
1016
    if(strlen($label) > 20){
1017
      $label = substr($label, 0, strpos($label, ' ', 15)) . '...';
1018
    }
1019
    $node_name = font_awesome_icon_markup('fa-th-list')  . ' ' . l($label, '#', array(
1020
      'attributes' => array(
1021
        'class' => 'taxonomic-children-button classification-chooser',
1022
        'data-destination-uri' => drupal_get_destination(),
1023
        'data-cdm-align-with' => array('prev')
1024
      ),
1025
      'html' => true
1026
    ));
1027
  }
1028

    
1029
  $rank_limit_uuid = variable_get(TAXONTREE_RANKLIMIT, TAXONTREE_RANKLIMIT_DEFAULT);
1030

    
1031
  $rank_separator = '<span> '
1032
    . font_awesome_icon_markup('fa-chevron-right')
1033
    . ' </span>';
1034
  $more_children_icon = font_awesome_icon_markup('fa-sitemap fa-rotate-270');
1035
  $more_children_label = '...';
1036

    
1037
  $items[] = $node_name;
1038

    
1039
  $more_children_for = null;
1040
  if($parent_taxon_nodes){
1041
    foreach ($parent_taxon_nodes as $node) {
1042

    
1043
      $is_first_item = count($items) == 0;
1044
      $is_last_item = count($items) == count($parent_taxon_nodes);
1045
      $node_name = cdm_dataportal_shortname_of($node);
1046
      $path = path_to_taxon($node->taxonUuid);
1047

    
1048
      if($node->taxonomicChildrenCount > 0) {
1049
        $more_children_for = $node->taxonUuid;
1050
      } else {
1051
        $more_children_for = null;
1052
      }
1053

    
1054
      // 'fa-sitemap'
1055

    
1056
      $items[] =
1057
        ($is_first_item ? '' : ' ')
1058
        . $rank_separator
1059
        . l(
1060
          '<span class="' . html_class_attribute_ref($node) . '">' . $node_name . '</span>',
1061
          $path,
1062
          array(
1063
            'attributes' => array(
1064
              'class' => array('taxonomic-children-button'),
1065
              'data-cdm-taxon-uuid' => array($node->taxonUuid),
1066
              'data-cdm-classification-mode' => array('siblings'),
1067
              'data-cdm-align-with' => array('prev')
1068
            ),
1069
            'html' => true
1070
          )
1071
        );
1072
      }
1073
    }
1074

    
1075
  // add more button to the end
1076
  if(!$parent_taxon_nodes) {
1077
    // not taxon focused yet, adding button to make  the root nodes available
1078
    $items[] = '<span>'
1079
      . $more_children_icon . '&nbsp;' .
1080
      '<span class="taxonomic-children-button" data-classification-uuid="' . $current_classification_uuid
1081
      . '" data-rank-limit-uuid="' . $rank_limit_uuid . '" data-cdm-align-with="prev"> ' . $more_children_label . '<span>'
1082
      . '</span>';
1083
  } else if($more_children_for){
1084
    // last parent item has child taxon nodes
1085
    $items[] = ' <span>'
1086
      . $more_children_icon . '&nbsp;' .
1087
      '<span class="taxonomic-children-button" data-cdm-taxon-uuid="' .$more_children_for
1088
      . '" data-cdm-classification-mode="children" data-cdm-align-with="prev"> ' . $more_children_label . '</span>'
1089
      . '</span>';
1090

    
1091
  }
1092

    
1093
  $render_array['#items'] = $items;
1094

    
1095
  return $render_array;
1096
}
1097

    
1098

    
1099
/**
1100
 * @param $specimen_uuid
1101
 * @return array
1102
 *    The drupal render array for the page
1103
 *
1104
 * @ingroup compose
1105
 */
1106
function compose_cdm_specimen_page($specimen_uuid)
1107
{
1108
  drupal_set_title("Specimen Details");
1109
  RenderHints::pushToRenderStack('specimen_page');
1110

    
1111
  $specimen_table = array(
1112
      '#theme' => 'table',
1113
      '#weight' => 2,
1114
      // prefix attributes and rows with '#' to let it pass toF the theme function,
1115
      // otherwise it is handled as child render array
1116

    
1117
    '#attributes' => array('class' => 'specimens'),
1118
      '#rows' => array(),
1119
      '#prefix' => '<div id="specimens">',
1120
      '#suffix' => '</div>',
1121

    
1122

    
1123
  );
1124
  $specimen = compose_cdm_specimen_or_observation($specimen_uuid, true);
1125
  $render_array = array(
1126
          '#theme' => 'item_list',
1127
          '#items' => array($specimen),
1128
          '#type' => 'ul');
1129
  $output = drupal_render($render_array);
1130

    
1131
  $specimen_table['#rows'][] = array(
1132
          // An array of table rows. Every row is an array of cells, or an associative array
1133
          'data' => $specimen,
1134
          'class' =>  array(
1135
              'descriptionElement',
1136
              'descriptionElement_IndividualsAssociation'
1137
          ),
1138
  );
1139

    
1140

    
1141
 // $detail_html = compose_cdm_specimen_or_observation($specimen_uuid, true);
1142
//    $render_array['markup'] = array(
1143
//        '#prefix' => '<div id="specimens" class="page">',
1144
//        'content' => $specimen_table,
1145
//        '#suffix' => '</div>',
1146
//    );
1147
    $render_array['specimen_table'] =  $specimen_table;
1148

    
1149
  RenderHints::popFromRenderStack();
1150
  return $specimen; // $render_array;
1151
}
1152

    
1153
/**
1154
 * Compose a description as a table of Feature<->State
1155
 *
1156
 * @param $description_uuid
1157
 *
1158
 * @return array
1159
 *    The drupal render array for the page
1160
 *
1161
 * @ingroup compose
1162
 */
1163
function compose_cdm_description($description_uuid) {
1164
  RenderHints::pushToRenderStack('description_page');
1165

    
1166
  $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, [$description_uuid]);
1167

    
1168
  $render_array = [];
1169

    
1170
  if (isset($description->describedSpecimenOrObservation)) {
1171
    $render_array[] = markup_to_render_array("<b>Specimen</b><br>");
1172
    $render_array[] = markup_to_render_array(render_cdm_specimen_link($description->describedSpecimenOrObservation));
1173
  }
1174

    
1175
  $header = ['Feature/Character', 'State'];
1176
  $rows = [];
1177
  foreach ($description->elements as $element) {
1178

    
1179
    switch ($element->class) {
1180
      case 'CategoricalData':
1181
        $rows[] = [
1182
          $element->feature->representation_L10n,
1183
          compose_state_data_strings($element),
1184
        ];
1185
        break;
1186
      case 'QuantitativeData':
1187
        $rows[] = [
1188
          $element->feature->representation_L10n,
1189
          compose_quant_stats($element),
1190
        ];
1191
        break;
1192
    }
1193
  }
1194
  $render_array[] = markup_to_render_array(theme('table', [
1195
    'header' => $header,
1196
    'rows' => $rows,
1197
  ]));
1198

    
1199
  if (isset($description->sources) and !empty($description->sources)) {
1200
    $items = [];
1201
    foreach ($description->sources as $source) {
1202
      if ($source->type == 'Aggregation' and $source->idNamespace == 'SpecimenDescription') {
1203
        $specimendescription = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, [$source->idInSource]);
1204
        $items[] = [
1205
          'data' => render_cdm_description_link($specimendescription),
1206
        ];
1207
      }
1208
    }
1209
    $render_array[] = [
1210
      '#title' => 'Sources',
1211
      '#theme' => 'item_list',
1212
      '#type' => 'ul',
1213
      '#items' => $items,
1214
    ];
1215
  }
1216

    
1217

    
1218
  RenderHints::popFromRenderStack();
1219
  return $render_array;
1220
}
1221

    
1222
/**
1223
 * @param $named_area_uuid
1224
 * @return array
1225
 *    The drupal render array for the page
1226
 *
1227
 * @ingroup compose
1228
 */
1229
function compose_cdm_named_area_page($named_area_uuid)
1230
{
1231

    
1232
  $named_area = cdm_ws_get(CDM_WS_PORTAL_TERM, array($named_area_uuid));
1233

    
1234
  $render_array = array();
1235
  RenderHints::pushToRenderStack('named_area_page');
1236

    
1237
  $groups = array();
1238
  @_description_list_group_add($groups, t('Name') . ':', $named_area->representation_L10n);
1239
  @_description_list_group_add($groups, t('IdInVocabulary') . ':', $named_area->idInVocabulary);
1240
  if(isset($named_area->level)) {
1241
    @_description_list_group_add($groups, t('Level') . ':', $named_area->level->representation_L10n);
1242
  }
1243

    
1244
  $name_area_details_elements = array(
1245
   // '#title' => $title,
1246
    '#theme' => 'description_list',
1247
    '#groups' => $groups,
1248
    '#attributes' => array('class' => html_class_attribute_ref($named_area)),
1249
  );
1250

    
1251
  $render_array[] = $name_area_details_elements;
1252

    
1253
  RenderHints::popFromRenderStack();
1254
  return $render_array;
1255
}
1256

    
1257

    
1258
/**
1259
 * Returns a drupal render array for a single reference page.
1260
 *
1261
 * Composes a page with all data on a single reference.
1262
 *
1263
 * @param string $uuid
1264
 *   An uuid for a cdm reference.
1265
 *
1266
 * @return array
1267
 *  A drupal render array
1268
 *
1269
 * @throws Exception
1270
 *
1271
 * @ingroup compose
1272
 */
1273
function compose_cdm_reference_page($uuid) {
1274

    
1275
  $pathelement = "reference_page";
1276
  RenderHints::pushToRenderStack($pathelement);
1277
  $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
1278
  if (isset($reference->titleCache)) {
1279
    drupal_set_title($reference->titleCache, PASS_THROUGH);
1280
  }
1281

    
1282
  $field_order = array(
1283
    "title",
1284
    "abbrevTitle",
1285
    // "titleCache" abbrevTitleCache
1286
    // "citation",
1287
    "authorship",
1288
    "editor",
1289
    "publisher",
1290
    "placePublished",
1291
    "datePublished",
1292
    "year",
1293
    "edition",// Class Book.
1294
    "volume",// Class Article.
1295
    "seriesPart",
1296
    "inReference",
1297
    "nomRefBase", // Class BookSection, Book, Article.
1298
    "pages",// Class Article.
1299
    "series",// Class Article, PrintSeries.
1300
    "school",// Class Thesis.
1301
    "institution",// Class Report.
1302
    "organization",// Class Proceedings.
1303
    "nextVersion",
1304
    "previousVersion",
1305
    "isbn",// Class Book.
1306
    "issn",// Class Journal.
1307
    "doi",
1308
    "uri"
1309
  );
1310

    
1311
  $table_rows = array();
1312

    
1313
  if (!isset($reference->authorship)) {
1314
    $authorship = cdm_ws_get(CDM_WS_REFERENCE_AUTHORTEAM, $reference->uuid);
1315
    $reference->authorship = isset($authorship->titleCache) ? $authorship->titleCache : '';
1316
  }
1317

    
1318
  if (!isset($reference->inReference)) {
1319
    $reference->inReference = cdm_ws_get(CDM_WS_REFERENCE, array(
1320
      $reference->uuid,
1321
      "inReference",
1322
    ));
1323
  }
1324

    
1325
  foreach ($field_order as $fieldname) {
1326

    
1327
    if (isset($reference->$fieldname)) {
1328

    
1329
      if ($fieldname == "datePublished") {
1330
        $period = $reference->$fieldname;
1331
        $datePublished = timePeriodToString($period);
1332
        if (isset($datePublished) && $datePublished != '') {
1333
          $table_rows[] = array(
1334
            t("Date published"),
1335
            $datePublished,
1336
          );
1337
        }
1338
      }
1339
      elseif ($fieldname == "doi" && is_object($reference->doi)) {
1340
        $table_rows[] = array(
1341
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1342
          cdm_doi($reference->doi, false)
1343
        );
1344
      }
1345
      elseif ($fieldname == "uri" && isset($reference->uri) && $reference->uri) {
1346
        $table_rows[] = array(
1347
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1348
          cdm_external_uri($reference->uri, false)
1349
        );
1350
      }
1351
      elseif (is_object($reference->$fieldname)) {
1352
        if ($fieldname == "authorship") {
1353
          $dump = $reference->$fieldname;
1354
          $teammembers = "teamMembers";
1355
          $team = $dump->$teammembers;
1356
          $nameArray = array();
1357

    
1358
          foreach ($team as $member) {
1359
            if (strlen($member->lastname) > 0) {
1360
              $nname = $member->lastname;
1361
              $name = $nname;
1362
              if (strlen($member->firstname) > 0) {
1363
                $vname = $member->firstname;
1364
                $name = $vname . " " . $nname;
1365
              }
1366
              $nameArray[] = $name;
1367
            }
1368
            else {
1369
              if (strlen($member->titleCache) > 0) {
1370
                $nameArray[] = $member->titleCache;
1371
              }
1372
            }
1373
          }
1374
          $value = join($nameArray, ", ");
1375
        }
1376
        elseif ($fieldname == "inReference") {
1377
          $type = $reference->$fieldname->type;
1378
          $value = l($reference->$fieldname->titleCache, path_to_reference($reference->$fieldname->uuid));
1379
          switch ($type) {
1380
            case "Book":
1381
              $fieldname = "in book";
1382
              break;
1383
            case "Journal":
1384
              $fieldname = "in journal";
1385
              break;
1386
            case "Proceedings":
1387
              $fieldname = "in proceedings";
1388
              break;
1389
          }
1390
        }
1391
        else {
1392
          $value = $reference->$fieldname->titleCache;
1393
        }
1394

    
1395

    
1396
        if (isset($value) && $value != '') {
1397
          $table_rows[] = array(
1398
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1399
            $value,
1400
          );
1401
        }
1402

    
1403
      }
1404
      else {
1405
        if (isset($reference->$fieldname) && $reference->$fieldname != '') {
1406
          $table_rows[] = array(
1407
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1408
            $reference->$fieldname,
1409
          );
1410
        }
1411
      }
1412
    }
1413
  }
1414

    
1415
  $out = theme_table(array(
1416
    'header' => array(),
1417
    'rows' => $table_rows,
1418
    'attributes' => array(
1419
      'class' => html_class_attribute_ref($reference)
1420
    ),
1421
    'caption' => NULL,
1422
    'colgroups' => NULL,
1423
    'sticky' => NULL,
1424
    'empty' => NULL,
1425
  ));
1426

    
1427
  if(isset($reference->referenceAbstract)){
1428
    $out .= '<h2 class="block-title">Abstract</h2><div class="abstract">' . $reference->referenceAbstract . '</div>';
1429
  }
1430

    
1431

    
1432
  // Annotations below the table
1433
  $annotations = cdm_fetch_visible_annotations($reference);
1434
  $out .= theme("cdm_annotations", array('annotations' => $annotations));
1435

    
1436
  $registration_working_set = cdm_ws_get("registrationWorkingSetDTO", array($uuid));
1437
  if($registration_working_set && count($registration_working_set->registrationDTOs) > 0){
1438
    $out .= "<h3>Nomenclatural acts:</h3><div class=\"cdm-item-list registration-item-list\">";
1439
    foreach($registration_working_set->registrationDTOs as $registration_dto){
1440
      if($registration_dto->status == "PUBLISHED"){
1441
        $registration_render_a = compose_registration_dto_compact($registration_dto, 'citation');
1442
        $registration_render_a["#prefix"] = "<div class=\"item item-registration\">";
1443
        $registration_render_a["#suffix"] = "</div>";
1444
        $out .= drupal_render($registration_render_a);
1445
      }
1446
    }
1447
    $out .= "</div>";
1448
  }
1449

    
1450
  RenderHints::popFromRenderStack();
1451

    
1452
  return markup_to_render_array($out);
1453
}
1454

    
1455
/**
1456
 * Provides the the label string for taxon page tabs.
1457
 *
1458
 * The $tabname as passed to the method will be returned if no override
1459
 * label is configured in the settings.
1460
 */
1461
function cdm_taxonpage_tab_label($tabname) {
1462
  static $taxon_tabs_labels = null;
1463
  if($taxon_tabs_labels == null){
1464
    $taxon_tabs_labels = get_array_variable_merged(CDM_TAXONPAGE_TAB_LABELS, CDM_TAXONPAGE_TAB_LABELS_DEFAULT);
1465
  }
1466
  $tabname_key = strtolower($tabname);
1467
  if(isset($taxon_tabs_labels[$tabname_key]) && $taxon_tabs_labels[$tabname_key]){
1468
    return $taxon_tabs_labels[$tabname_key];
1469
  }
1470
  return $tabname;
1471
}
(7-7/10)