Project

General

Profile

Download (47 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
    $configured_image_gallery_viewer = variable_get(CDM_MEDIA_GALLERY_VIEWER, CDM_MEDIA_GALLERY_VIEWER_DEFAULT);
312
    if($configured_image_gallery_viewer != 'fsi'){
313
      $images_html .= render_taxon_media_gallery($taxon, $configured_image_gallery_viewer, $media);
314
    } else {
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
      $images_html .= render_taxon_media_gallery($taxon, CDM_MEDIA_GALLERY_VIEWER_DEFAULT, $media);
318
      $images_html .= render_taxon_media_gallery($taxon, $configured_image_gallery_viewer, $media);
319
    }
320
    $images_html .= '</div>'; // END of <div id="images">
321
    $render_array['images'] = markup_to_render_array($images_html, $weight++);
322
  }
323

    
324
  // --- PAGE PART: SYNONYMY --- //
325
  if (!taxon_page_tabs_hidden_check('synonymy') && (($page_part == 'synonymy' || $page_part == 'all') && $synonymy_as_tab)) {
326
    $synonymy_html = '<div id="synonymy" class="page-part">';
327
    if ($page_part == 'all') {
328
      $synonymy_html .= '<h2>' . t(cdm_taxonpage_tab_label('Synonymy')) . '</h2>';
329
    }
330
    $addAcceptedTaxon = variable_get('cdm_dataportal_nomref_in_title', CDM_DATAPORTAL_NOMREF_IN_TITLE);
331

    
332
    $synonym_a = compose_cdm_taxon_page_synonymy($taxon, $addAcceptedTaxon);
333
    $synonymy_html .= drupal_render($synonym_a);
334

    
335
    $synonymy_html .= '</div>';
336
    $render_array['synonymy'] = markup_to_render_array($synonymy_html, $weight++);
337

    
338
  }
339

    
340
  // --- PAGE PART: SPECIMENS --- //
341
  if (!taxon_page_tabs_hidden_check('specimens') && ($specimensOrObservationsCount > 0 && ($page_part == 'specimens' || $page_part == 'all'))) {
342
    $render_array['specimens'] = array(
343
        '#prefix' => '<div id="specimens" class="page-part">' . ($page_part == 'all' ? '<h2>' . t(cdm_taxonpage_tab_label('Specimens')) . '</h2>' : ''),
344
        'content' => compose_cdm_taxon_page_specimens($taxon), // returns render array
345
        '#suffix' => '</div>',
346
    );
347
  }
348

    
349
  // --- PAGE PART: KEYS --- //
350
  if(!taxon_page_tabs_hidden_check('keys')){
351
    if ($identificationKeyCount == 1 && $page_part == 'keys'){
352
      drupal_goto(path_to_key($polytomousKeysPager->records[0]->class, $polytomousKeysPager->records[0]->uuid));
353
    }
354
    else if ($identificationKeyCount > 0 && ($page_part == 'keys' || $page_part == 'all')) {
355
      $keys_html = '<div id="keys" class="page-part">';
356
      if ($page_part == 'all') {
357
        $keys_html .= '<h2>' . t(cdm_taxonpage_tab_label('Keys')) . '</h2>';
358
      }
359
      $keys_html .= theme('cdm_block_IdentificationKeys', array('taxonUuid' => $taxon->uuid));
360
      $keys_html .= '</div>';
361
      $render_array['keys'] = markup_to_render_array($keys_html, $weight++);
362
    }
363
  }
364

    
365
  // --- PAGE PART: EXPERTS --- //
366

    
367
  if (!taxon_page_tabs_hidden_check('experts') && ($page_part == 'experts' || $page_part == 'all')) {
368
    $render_array['experts'] = array(
369
        '#prefix' => '<div id="experts" class="page-part">' . ($page_part == 'all' ? '<h2>' . t(cdm_taxonpage_tab_label('Experts')) . '</h2>' : ''),
370
        'content' => compose_cdm_taxon_page_experts($taxon), // returns render array
371
        '#suffix' => '</div>',
372
    );
373
  }
374

    
375
  // ------------------ END OF PARTS -------------- //
376

    
377
  // adjust weights of page and toc elements according to the settings
378
  $taxontabs_weights = get_array_variable_merged(CDM_TAXONPAGE_TAB_WEIGHT, CDM_TAXONPAGE_TAB_WEIGHT_DEFAULT);
379
  foreach($taxontabs_weights as $tab_key => $weight){
380
    if(isset($render_array[$tab_key])){
381
      $render_array[$tab_key]['#weight'] = $weight;
382
    }
383
  }
384

    
385

    
386
  // set up the TOC for the pages which contain all pageparts
387
  if($page_part == 'all') {
388

    
389
    asort($taxontabs_weights);
390
    foreach(array_keys($taxontabs_weights) as $tab_key){
391
      if(isset($render_array[$tab_key])){
392
        if($tab_key != 'general'){
393
          // add entry for page part
394
          $toc_elements[] = array(
395
              'data' => l(t(cdm_taxonpage_tab_label(ucfirst($tab_key))), $_GET['q'], array('fragment' => $tab_key, 'query' => $http_request_params)),
396
              'class' => array('page-part-toc-item-' . $tab_key)
397
          );
398
        } else {
399
          // add content of profile part instead
400
          if(isset($render_array['general'])) {
401
            // in case all tabs are shown at once the feature tocs
402
            // should be integrated into the tabs toc as sub list
403
            // and the profile image should be on top of the page
404
            if(isset($render_array['general']['taxon_description_feature_toc'])){;
405
            foreach ($render_array['general']['taxon_description_feature_toc']['#items'] as $profile_toc_item){
406
              $toc_elements[] = $profile_toc_item;
407
            }
408
            unset($render_array['general']['taxon_description_feature_toc']);
409
            }
410
          }
411
        }
412
      }
413
    }
414

    
415
    // move profile image in page structure
416
    if(isset($render_array['general']['taxon_profile_image'])){
417
      $render_array['profile_image'] = $render_array['general']['taxon_profile_image'];
418
      $render_array['profile_image']['#weight'] = -100;
419
      unset($render_array['general']['taxon_profile_image']);
420
    }
421

    
422
    // finally add the table of contents to the render array
423
    $render_array['toc'] = array(
424
        '#theme' => 'item_list',
425
        '#items' => $toc_elements,
426
        '#title' => t('Content'),
427
        '#weight' => -101,
428
        '#suffix' => '</div>',
429
        '#prefix'=> '<div id="page-toc">'
430
    );
431
  }
432

    
433

    
434
  return $render_array;
435
}
436

    
437
/**
438
 * TODO should this function really be a compose function?
439
 *     For a compose function must there always be a theme function with the same name? (ak 8.8.2013)
440
 *
441
 * composes and returns an render array containing the components of the taxon profile tab:
442
 *  - 'taxon_profile_image'
443
 *  - 'taxon_description_feature_toc'
444
 *  - 'taxon_description_features'
445
 *
446
 *
447
 * @param object taxon
448
 * @param object $merged_tree
449
 * @param object media
450
 * @param bool $add_synonymy
451
 *
452
 * @return array
453
 *   A Drupal render array with the following elements:
454
 *     - 'taxon_profile_image'
455
 *     - 'taxon_description_feature_toc'
456
 *     - 'taxon_description_features'
457
 *
458
 * @throws Exception
459
 *
460
 * @ingroup compose
461
 */
462
function compose_cdm_taxon_page_profile($taxon, $merged_tree, $media, $add_synonymy) {
463

    
464
  $render_array = array();
465

    
466
  $taxon_profile_image_settings = variable_get(CDM_TAXON_PROFILE_IMAGE, unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT));
467

    
468
  $hide_taxon_profile_image = FALSE;
469
  if (variable_get('image_hide_rank', '0') != '0' && isset($taxon->name->rank->uuid)) {
470
    $rankCompare = rank_compare($taxon->name->rank->uuid, variable_get('image_hide_rank', '-99'));
471
    $hide_taxon_profile_image = ($rankCompare > -1);
472
  }
473

    
474
  if ($taxon_profile_image_settings['show'] && !$hide_taxon_profile_image) {
475

    
476
    $representationPart = new stdClass();
477
    $attributes = array();
478
    if (isset($media[0])) {
479
      // due to a bug the portal/taxon/{uuid}/media service only delivers a filtered media object
480
      // which only contains the thumbnail representation even if the height and width filters are not set.
481
      // -->
482
      $preferred_media = cdm_ws_get(CDM_WS_MEDIA, $media[0]->uuid);
483
      $preferred_representations = cdm_preferred_media_representations($preferred_media, array(
484
        'image/jpg',
485
        'image/jpeg',
486
        'image/png',
487
        'image/gif',
488
      ),
489
        $taxon_profile_image_settings['maxextend'],
490
        $taxon_profile_image_settings['maxextend']
491
      );
492
      if(count($preferred_representations) > 0){
493

    
494
        $representation = array_shift($preferred_representations);
495
        $representationPart = $representation->parts[0];
496
        $attributes['alt'] = $representationPart->uri;
497

    
498
        if (!empty($taxon_profile_image_settings['media_uri_query'])) {
499
          $representationPart->uri = $representationPart->uri
500
            . (strpos($representationPart->uri, '?') !== FALSE ? '&' : '?')
501
            . $taxon_profile_image_settings['media_uri_query'];
502
        }
503
      }
504
    }
505
    else {
506
      if ($taxon_profile_image_settings['custom_placeholder_enabled']) {
507
        // show placeholder image instead
508
        if (!empty($taxon_profile_image_settings['custom_placeholder_image_on']) && !empty($taxon_profile_image_settings['custom_placeholder_image_fid'])) {
509
          // use the user provided image
510
          $profile_image_file = file_load($taxon_profile_image_settings['custom_placeholder_image_fid']);
511
          $url = file_create_url($profile_image_file->uri);
512
          $image_info = image_get_info($profile_image_file->uri);
513
          $representationPart->width = $image_info['width'];
514
          $representationPart->height = $image_info['height'];
515
          $representationPart->uri = $url;
516
        }
517
        else {
518
          // use the hard coded default
519
          $representationPart->width = 184;
520
          $representationPart->height = 144;
521
          $representationPart->uri = base_path() . drupal_get_path('module',
522
              'cdm_dataportal') . '/images/no_picture.png';
523
        }
524
        $attributes['alt'] = "no image available";
525
      }
526
    }
527

    
528
    if (isset($representationPart->uri)) {
529
      $profile_image = cdm_media_gallerie_image($representationPart, $taxon_profile_image_settings['maxextend'], FALSE, $attributes);
530
      // NOTE: style="width:${maxextend}px' is needed for IE8 !!!
531
      $max_extend_with = $taxon_profile_image_settings['maxextend'] . 'px';
532
      $render_array['taxon_profile_image'] = markup_to_render_array('<div id="taxonProfileImage" style="width:' . $max_extend_with . '">' . $profile_image . '</div>',
533
        -101);
534
    }
535
  }
536

    
537
  if($add_synonymy){
538
    $synonymy_a = compose_cdm_taxon_page_synonymy($taxon, true);
539
    $synonymy_a['#weight'] = -102;
540
    $render_array['synonymy'] = $synonymy_a;
541
  }
542

    
543
//  $pseudo_feature_block_toc_items = array();
544

    
545

    
546
  // Render the sections for each real feature
547
  $feature_block_list = make_feature_block_list($merged_tree->root->childNodes, $taxon);
548

    
549
  // >>>>>>>>>>>>>>>>>>> PSEUDO FEATURES >>>>>>>>>>>>>>>>>>>
550
  $pseudo_feature_weights = get_array_variable_merged(CDM_PSEUDO_FEATURE_BLOCK_WEIGHTS, CDM_PSEUDO_FEATURE_BLOCK_WEIGHTS_DEFAULT);
551
  // Bibliography
552
  $bibliography_settings = get_bibliography_settings();
553
  if($bibliography_settings['enabled'] == 1){
554
    $bibliography_markup = FootnoteManager::renderFootnoteList(PSEUDO_FEATURE_BIBLIOGRAPHY, '');
555
    if($bibliography_markup) {
556
      $feature_bibliography = make_pseudo_feature('Bibliography', PSEUDO_FEATURE_BIBLIOGRAPHY);
557
      $bibliography_item = markup_to_render_array($bibliography_markup);
558
      $bibliography_block = feature_block(t('Bibliography'), $feature_bibliography);
559
      $bibliography_block->content = array();
560
      $bibliography_block->content[] = compose_feature_block_wrap_elements(array($bibliography_item), $feature_bibliography);
561

    
562
      $feature_block_list[$pseudo_feature_weights[PSEUDO_FEATURE_BIBLIOGRAPHY]] = $bibliography_block;
563
      cdm_toc_list_add_item('Bibliography', 'bibliography', null, false, $pseudo_feature_weights[PSEUDO_FEATURE_BIBLIOGRAPHY]);
564
    }
565
  }
566

    
567
  // Descriptions (aggregated)
568
  $descriptionTypes = array();
569
  $descriptionTypes['descriptionTypes'] = ("AGGREGATED_STRUC_DESC");
570
  $aggregatedDescriptions = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXON . '/' . $taxon->uuid . '/descriptions', $descriptionTypes);
571
  if (isset($aggregatedDescriptions) and !empty($aggregatedDescriptions)) {
572
      // if($feature_block_list) ....TODO
573
      $feature_description = make_pseudo_feature('Descriptions (aggregated)', PSEUDO_FEATURE_AGGREGATION_DESCRIPTIONS);
574
      $description_item = '';
575
      foreach ($aggregatedDescriptions as $description) {
576
        $description_item = '<div class="' . html_class_attribute_ref($description) . '">';
577
        $description_item .= render_description_string(get_root_nodes_for_dataset($description));
578
        $description_item .= ' ' . icon_link(path_to_description($description->uuid));
579
        $description_item .= '<div class="content-caption">'. statistical_values_explanation() . '</div>';
580
        $description_item .= '</div>';
581
      }
582
      $description_block = feature_block(t('Descriptions (aggregated)'), $feature_description);
583
      $description_block->content = [];
584
      $description_block->content[] = compose_feature_block_wrap_elements([$description_item], $feature_description);
585

    
586
      $feature_block_list[$pseudo_feature_weights[PSEUDO_FEATURE_AGGREGATION_DESCRIPTIONS]] = $description_block;
587
      cdm_toc_list_add_item('Descriptions (aggregated)', 'aggregation_descriptions', null, false, $pseudo_feature_weights[PSEUDO_FEATURE_AGGREGATION_DESCRIPTIONS]);
588

    
589

    
590
  }
591

    
592
  // sort by weight
593
  ksort($feature_block_list);
594
  $render_array['taxon_description_features'] = _block_get_renderable_array($feature_block_list);
595

    
596
/*  // update TOC
597
  if ($pseudo_feature_block_toc_items){
598
    foreach ($pseudo_feature_block_toc_items as $label=>$fragment){
599
      cdm_toc_list_add_item($label, $fragment);
600
    }
601
  }
602
*/
603
  // <<<<<<<<<<<<<<<<<<< PSEUDO FEATURES <<<<<<<<<<<<<<<<<<<
604

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

    
616
  return $render_array;
617
}
618

    
619

    
620
/**
621
 * Renders the link which will lead to the specimen detail page
622
 * @param object $specimen
623
 *    the cdm specimen entity which will be linked to
624
 *
625
 * @return string
626
 *     the markup for the link
627
 */
628
function render_cdm_specimen_link($specimen) {
629
  $path = path_to_specimen($specimen->uuid);
630
  $attributes['class'][] = html_class_attribute_ref($specimen);
631
  return $specimen->titleCache.icon_link($path);
632
}
633

    
634
/**
635
 * Returns HTML containing the synonymy for the accepted taxon.
636
 *
637
 * Shows the whole synonymy for the accepted taxon.
638
 * The synonymy list is headed by the complete scientific name
639
 * of the accepted taxon with nomenclatural reference.
640
 *
641
 * @param object $taxon
642
 * @param boolean $add_accepted_taxon
643
 *
644
 * @return array
645
 *  Drupal render array for the synonymy
646
 *
647
 * @throws Exception
648
 *
649
 * @ingroup compose
650
 */
651
function compose_cdm_taxon_page_synonymy($taxon, $add_accepted_taxon) {
652

    
653
  RenderHints::pushToRenderStack('taxon_page_synonymy');
654

    
655
  // footnote key for the homotypic group and accepted taxon,
656
  // both should have the same footnote key
657
  RenderHints::setFootnoteListKey(RenderHints::getRenderPath());
658

    
659
  $synomymie = cdm_ws_get(CDM_WS_PORTAL_TAXON_SYNONYMY, array($taxon->uuid));
660

    
661
  $out = '';
662

    
663
  // Render accepted taxon.
664
  //
665
  // foonotes of the accepted taxon will be rendered in the homotypic group section
666
  // even if there are not synonyms in the homotypic group
667
  // homotypic group and accepted taxon should have the same footnote key
668
  $referenceUri = '';
669
  if ($add_accepted_taxon) {
670
    // remember the last part of the render path
671
    $synonymy_render_path = RenderHints::getRenderPath();
672
    // set new render path for the accepted taxon so
673
    // it can be styled differently via the name render part definitions
674
    RenderHints::pushToRenderStack('accepted_taxon');
675
    $accepted_name = '';
676
    if (variable_get(CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE, 0)) {
677
      $label = variable_get(CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE_LABEL, CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE_LABEL_DEFAULT);
678
      $accepted_name .= '<div class="secReference"><span class="label">' . t($label) . ':</span> ' . $taxon->sec->titleCache . '</div>';
679
    }
680
    if (isset($taxon->name->nomenclaturalReference)) {
681
      $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
682
    }
683

    
684
    $accepted_name .= '<div class="accepted-name">';
685
    $accepted_name .= render_taxon_or_name($taxon, NULL, $referenceUri);
686

    
687
    $name_relations = cdm_name_relationships_for_taxon($taxon);
688
    $name_relationships = compose_name_relationships_inline($name_relations, $taxon->name->uuid, $taxon->uuid);
689
    // Render relationships of accepted name.
690
    if(isset($name_relationships['list']['items'][0])){
691
      $accepted_name .= ' ' . drupal_render($name_relationships);
692
    }
693

    
694
    // handle annotations of the name and taxon
695
    $special_annotations_array = array();
696
    $special_annotations_array[] = $taxon->name;
697
    $special_annotations_array[] = $taxon;
698
    $accepted_name .= theme('cdm_annotations_as_footnotekeys', array(
699
        'cdmBase_list' => $special_annotations_array,
700
        'footnote_list_key' => $synonymy_render_path . '-annotations')
701
    );
702
    $accepted_name .= '</div>';
703
    RenderHints::popFromRenderStack();
704
  }
705

    
706
  // --- Render homotypic synonymy group
707
  if (!empty($accepted_name)) {
708
    $out .= $accepted_name;
709
  }
710

    
711
  // Render the homotypicSynonymyGroup including the type information.
712
  $out .= theme(
713
    'cdm_homotypicSynonymyGroup',
714
    array(
715
      'synonymList' => $synomymie->homotypicSynonymsByHomotypicGroup,
716
      'accepted_taxon_name_uuid' => $taxon->name->uuid
717
    )
718
  );
719

    
720

    
721
  // Render accepted taxon heterotypic synonymy groups.
722
  if ($synomymie->heterotypicSynonymyGroups) {
723
    foreach ($synomymie->heterotypicSynonymyGroups as $homotypicalGroup) {
724
      $out .= theme('cdm_heterotypicSynonymyGroup', array('homotypicalGroup' => $homotypicalGroup));
725
    }
726
  }
727
  // Render taxon relationships.
728
  if (variable_get(CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT)) {
729
    $taxonRelationshipsDTO = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS_DTO, $taxon->uuid);
730
    $out .= cdm_taxonRelationships($taxonRelationshipsDTO, $taxon);
731
  }
732

    
733
  RenderHints::popFromRenderStack();
734

    
735
  return markup_to_render_array($out);
736
}
737

    
738

    
739
/**
740
 * composes and returns an render array for the experts associated with the given taxon
741
 *
742
 * @param object taxon
743
 *
744
 * @return array
745
 *   A Drupal render array for a table with the experts
746
 *
747
 * @ingroup compose
748
 */
749
function compose_cdm_taxon_page_experts($taxon){
750

    
751
  $render_array = array();
752
  if(!isset($taxon->uuid)){
753
    return $render_array;
754
  }
755

    
756
  $current_classification_uuid = get_current_classification_uuid();
757
  // TODO use cdm_ws_fetchall below but this failes! needs fix!
758
  $taxon_node_agent_relations = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
759
    array(
760
      $taxon->uuid,
761
      $current_classification_uuid
762
    )
763
  );
764

    
765
  $header = array(
766
    array('data' => t('Expert')),
767
    array('data' => t('Role'))
768
  );
769
  $rows = array();
770

    
771

    
772
  foreach($taxon_node_agent_relations->records as $taxon_node_agent_relation){
773

    
774

    
775
    $expert_role_id = $taxon_node_agent_relation->agent->uuid . '-' . $taxon_node_agent_relation->type->uuid;
776
    $expert_details_container_id = 'expert_details_' . $expert_role_id;
777

    
778
    $agent_label_markup = cdm_dynabox(
779
      'expert_' . $expert_role_id,
780
      $taxon_node_agent_relation->agent->titleCache,
781
      // specifying both ends of the relationship will return only one record in the pager
782
      cdm_compose_url(CDM_WS_PORTAL_AGENT,
783
         array($taxon_node_agent_relation->agent->uuid, 'taxonNodeAgentRelations'),
784
        'taxon_uuid=' . $taxon->uuid . '&relType_uuid=' . $taxon_node_agent_relation->type->uuid),
785
      'cdm_taxon_expert',
786
      'Click for details',
787
      array('div', 'div'),
788
      array(), // attributes
789
      '#' . $expert_details_container_id // $content_element_selector
790
    );
791

    
792
    // Expert and Role
793
    $rows[] = array(
794
      'data' => array(
795
        array(
796
          'data' => $agent_label_markup,
797
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->agent))
798
        ),
799
        array(
800
          'data' => $taxon_node_agent_relation->type->representation_L10n,
801
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->type))
802
        )
803
      )
804
    );
805
    // Agent details
806
    $rows[] = array(
807
      'data' => array(
808
        array(
809
          'data' => '<!-- expert_details_container -->',
810
          'id' => $expert_details_container_id,
811
          'colspan' => 2
812
        )
813
      )
814
    );
815

    
816
  }
817

    
818

    
819
  $render_array['experts_table'] = array(
820
    '#theme' => 'table',
821
    '#header' => $header,
822
    '#rows' => $rows,
823
  );
824

    
825

    
826
  return $render_array;
827
}
828

    
829

    
830
/**
831
 * Manages the tabs to be hidden in the taxon page.
832
 *
833
 * The tabs are identified by their last menu link path element:
834
 *  - description
835
 *  - synonymy
836
 *  - images
837
 *  - specimens
838
 *  - key
839
 *
840
 * Internally the tabs are stored in a static variable which is
841
 * managed by drupal_static().
842
 *
843
 * @param string $add_tab
844
 *   Optional parameter. The given string will be added to the array of tabs
845
 *
846
 * @return
847
 *   The array of tabs
848
 */
849
function taxon_page_tabs_hidden_add($add_tab = NULL) {
850
  $tabs = &drupal_static('taxon_page_tabs_hidden');
851

    
852
  if(!isset($tabs)){
853
    $tabs = array();
854
  }
855

    
856
  if (isset($add_tab) && !array_key_exists($add_tab, $tabs)) {
857
    $tabs[] = $add_tab;
858
  }
859

    
860
  return $tabs;
861
}
862

    
863
/**
864
 * Manages the tabs to be hidden in the taxon page.
865
 *
866
 * The tabs names are identified by their last menu link path element:
867
 *  - description
868
 *  - synonymy
869
 *  - images
870
 *  - specimens
871
 *  - key
872
 *
873
 * Internally the tabs are stored in a static variable which is
874
 * managed by drupal_static().
875
 *
876
 * @param string $tabname
877
 *   The name of the tab to check
878
 *
879
 * @return boolean
880
 *   True if the tab or section is to be hidden
881
 */
882
function taxon_page_tabs_hidden_check($tabname) {
883

    
884
  $tabs = &drupal_static('taxon_page_tabs_hidden');
885

    
886
  if(!isset($tabs)){
887
    $tabs = array();
888
  }
889

    
890
  return array_search($tabname, $tabs) !== FALSE;
891
}
892

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

    
904
  $hidden_tabs = taxon_page_tabs_hidden_add();
905

    
906
  if (!variable_get(CDM_SEARCH_BLAST_ENABLED)) {
907
      if (is_array($variables['primary'])) {
908
          foreach ($variables['primary'] as $key => &$element) {
909
              if ($element['#link']['path'] == 'cdm_dataportal/search/blast') {
910
                  // remove the tab
911
                  unset($variables['primary'][$key]);
912
              }
913

    
914
          }
915
      }
916
  }
917
  if (is_array($variables['primary'])) {
918
    foreach ($variables['primary'] as $key => &$element) {
919

    
920
      // 1. Remove the tabs to be hidden
921
      foreach ($hidden_tabs as $tab) {
922
        if ($element['#link']['path'] == 'cdm_dataportal/taxon/%/' . $tab) {
923
          // remove the tab
924
          unset($variables['primary'][$key]);
925
        }
926
      }
927
    }
928
  }
929
}
930

    
931

    
932

    
933
/**
934
 * Implements the hook_preprocess_HOOK() for theme_menu_local_task()
935
 *
936
 *
937
 * @param array $variables
938
 *   An associative array containing:
939
 *     - element: A render element containing:
940
 *          #link: A menu link array with 'title', 'href', and 'localized_options' keys.
941
 *          #active: A boolean indicating whether the local task is active.
942
 *
943
 */
944
function cdm_dataportal_preprocess_menu_local_task(&$variables) {
945

    
946
  $link = $variables['element']['#link'];
947
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
948
    $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']) . '"/>';
949
    $link['localized_options']['html'] = TRUE;
950

    
951
    $variables['element']['#link'] = $link;
952
  }
953
}
954

    
955
/* =================== block composition ===================== */
956

    
957
/**
958
 * Composes and returns an render array for the classification breadcrumbs of the given taxon.
959
 *
960
 * @param taxon
961
 *
962
 * @return array
963
 *   A Drupal render array for a table with the experts
964
 *
965
 * @ingroup compose
966
 */
967
function compose_classification_breadcrumbs($taxon_uuid) {
968

    
969
  _add_js_taxonomic_children('#classification-breadcrumbs .taxonomic-children-button');
970

    
971
  $render_array = array();
972

    
973
  $render_array['#theme'] = 'item_list';
974
  $render_array['#type'] = 'ul';
975
  $render_array['#attributes'] = array(
976
    'id' => 'classification-breadcrumbs',
977
    'class' => 'breadcrumbs inline',
978
  );
979

    
980
  $items = array();
981

    
982
  $parent_taxon_nodes = null;
983
  if($taxon_uuid){
984
    $parent_taxon_nodes = cdm_ws_taxonomy_pathFromRoot($taxon_uuid);
985
  }
986

    
987
  $classifications = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXONOMY);
988
  // find current classification in list
989
  $classification = null;
990
  $current_classification_uuid = get_current_classification_uuid();
991
  foreach ($classifications as $classification){
992
    if($classification->uuid == $current_classification_uuid){
993
      break;
994
    }
995
  }
996

    
997
  $node_name = '';
998
  if(count($classifications) > 1 ){
999
    // need to add the current classification as first label
1000

    
1001
    $label = $classification->titleCache;
1002
    if(strlen($label) > 20){
1003
      $label = substr($label, 0, strpos($label, ' ', 15)) . '...';
1004
    }
1005
    $node_name = font_awesome_icon_markup('fa-th-list')  . ' ' . l($label, '#', array(
1006
      'attributes' => array(
1007
        'class' => 'taxonomic-children-button classification-chooser',
1008
        'data-destination-uri' => drupal_get_destination(),
1009
        'data-cdm-align-with' => array('prev')
1010
      ),
1011
      'html' => true
1012
    ));
1013
  }
1014

    
1015
  $rank_limit_uuid = variable_get(TAXONTREE_RANKLIMIT, TAXONTREE_RANKLIMIT_DEFAULT);
1016

    
1017
  $rank_separator = '<span> '
1018
    . font_awesome_icon_markup('fa-chevron-right')
1019
    . ' </span>';
1020
  $more_children_icon = font_awesome_icon_markup('fa-sitemap fa-rotate-270');
1021
  $more_children_label = '...';
1022

    
1023
  $items[] = $node_name;
1024

    
1025
  $more_children_for = null;
1026
  if($parent_taxon_nodes){
1027
    foreach ($parent_taxon_nodes as $node) {
1028

    
1029
      $is_first_item = count($items) == 0;
1030
      $is_last_item = count($items) == count($parent_taxon_nodes);
1031
      $node_name = cdm_dataportal_shortname_of($node);
1032
      $path = path_to_taxon($node->taxonUuid);
1033

    
1034
      if($node->taxonomicChildrenCount > 0) {
1035
        $more_children_for = $node->taxonUuid;
1036
      } else {
1037
        $more_children_for = null;
1038
      }
1039

    
1040
      // 'fa-sitemap'
1041

    
1042
      $items[] =
1043
        ($is_first_item ? '' : ' ')
1044
        . $rank_separator
1045
        . l(
1046
          '<span class="' . html_class_attribute_ref($node) . '">' . $node_name . '</span>',
1047
          $path,
1048
          array(
1049
            'attributes' => array(
1050
              'class' => array('taxonomic-children-button'),
1051
              'data-cdm-taxon-uuid' => array($node->taxonUuid),
1052
              'data-cdm-classification-mode' => array('siblings'),
1053
              'data-cdm-align-with' => array('prev')
1054
            ),
1055
            'html' => true
1056
          )
1057
        );
1058
      }
1059
    }
1060

    
1061
  // add more button to the end
1062
  if(!$parent_taxon_nodes) {
1063
    // not taxon focused yet, adding button to make  the root nodes available
1064
    $items[] = '<span>'
1065
      . $more_children_icon . '&nbsp;' .
1066
      '<span class="taxonomic-children-button" data-classification-uuid="' . $current_classification_uuid
1067
      . '" data-rank-limit-uuid="' . $rank_limit_uuid . '" data-cdm-align-with="prev"> ' . $more_children_label . '<span>'
1068
      . '</span>';
1069
  } else if($more_children_for){
1070
    // last parent item has child taxon nodes
1071
    $items[] = ' <span>'
1072
      . $more_children_icon . '&nbsp;' .
1073
      '<span class="taxonomic-children-button" data-cdm-taxon-uuid="' .$more_children_for
1074
      . '" data-cdm-classification-mode="children" data-cdm-align-with="prev"> ' . $more_children_label . '</span>'
1075
      . '</span>';
1076

    
1077
  }
1078

    
1079
  $render_array['#items'] = $items;
1080

    
1081
  return $render_array;
1082
}
1083

    
1084

    
1085
/**
1086
 * @param $specimen_uuid
1087
 * @return array
1088
 *    The drupal render array for the page
1089
 *
1090
 * @ingroup compose
1091
 */
1092
function compose_cdm_specimen_page($specimen_uuid)
1093
{
1094
  drupal_set_title("Specimen Details");
1095
  RenderHints::pushToRenderStack('specimen_page');
1096

    
1097
  $specimen_table = array(
1098
      '#theme' => 'table',
1099
      '#weight' => 2,
1100
      // prefix attributes and rows with '#' to let it pass to the theme function,
1101
      // otherwise it is handled as child render array
1102

    
1103
    '#attributes' => array('class' => 'specimens'),
1104
      '#rows' => array(),
1105
      '#prefix' => '<div id="specimens">',
1106
      '#suffix' => '</div>',
1107

    
1108

    
1109
  );
1110
  $specimen = compose_cdm_specimen_or_observation($specimen_uuid, true);
1111
  $render_array = array(
1112
          '#theme' => 'item_list',
1113
          '#items' => array($specimen),
1114
          '#type' => 'ul');
1115
  $output = drupal_render($render_array);
1116

    
1117
  $specimen_table['#rows'][] = array(
1118
          // An array of table rows. Every row is an array of cells, or an associative array
1119
          'data' => $specimen,
1120
          'class' =>  array(
1121
              'descriptionElement',
1122
              'descriptionElement_IndividualsAssociation'
1123
          ),
1124
  );
1125

    
1126

    
1127
 // $detail_html = compose_cdm_specimen_or_observation($specimen_uuid, true);
1128
//    $render_array['markup'] = array(
1129
//        '#prefix' => '<div id="specimens" class="page">',
1130
//        'content' => $specimen_table,
1131
//        '#suffix' => '</div>',
1132
//    );
1133
  $render_array['specimen_table'] =  $specimen_table;
1134

    
1135
  RenderHints::popFromRenderStack();
1136
  return $specimen; // $render_array;
1137
}
1138

    
1139
/**
1140
 * @param $named_area_uuid
1141
 * @return array
1142
 *    The drupal render array for the page
1143
 *
1144
 * @ingroup compose
1145
 */
1146
function compose_cdm_named_area_page($named_area_uuid)
1147
{
1148

    
1149
  $named_area = cdm_ws_get(CDM_WS_PORTAL_TERM, array($named_area_uuid));
1150

    
1151
  $render_array = array();
1152
  RenderHints::pushToRenderStack('named_area_page');
1153

    
1154
  $groups = array();
1155
  @_description_list_group_add($groups, t('Name') . ':', $named_area->representation_L10n);
1156
  @_description_list_group_add($groups, t('IdInVocabulary') . ':', $named_area->idInVocabulary);
1157
  if(isset($named_area->level)) {
1158
    @_description_list_group_add($groups, t('Level') . ':', $named_area->level->representation_L10n);
1159
  }
1160

    
1161
  $name_area_details_elements = array(
1162
   // '#title' => $title,
1163
    '#theme' => 'description_list',
1164
    '#groups' => $groups,
1165
    '#attributes' => array('class' => html_class_attribute_ref($named_area)),
1166
  );
1167

    
1168
  $render_array[] = $name_area_details_elements;
1169

    
1170
  RenderHints::popFromRenderStack();
1171
  return $render_array;
1172
}
1173

    
1174

    
1175
/**
1176
 * Returns a drupal render array for a single reference page.
1177
 *
1178
 * Composes a page with all data on a single reference.
1179
 *
1180
 * @param string $uuid
1181
 *   An uuid for a cdm reference.
1182
 *
1183
 * @return array
1184
 *  A drupal render array
1185
 *
1186
 * @throws Exception
1187
 *
1188
 * @ingroup compose
1189
 */
1190
function compose_cdm_reference_page($uuid) {
1191

    
1192
  $pathelement = "reference_page";
1193
  RenderHints::pushToRenderStack($pathelement);
1194
  $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
1195
  if (!$reference) {
1196
    drupal_set_title(t('Reference does not exist'), PASS_THROUGH);
1197
    return "";
1198
  }
1199
  if (isset($reference->titleCache)) {
1200
    drupal_set_title($reference->titleCache, PASS_THROUGH);
1201
  }
1202

    
1203
  $field_order = array(
1204
    "title",
1205
    "abbrevTitle",
1206
    // "titleCache" abbrevTitleCache
1207
    // "citation",
1208
    "authorship",
1209
    "editor",
1210
    "publisher",
1211
    "placePublished",
1212
    "datePublished",
1213
    "year",
1214
    "edition",// Class Book.
1215
    "volume",// Class Article.
1216
    "seriesPart",
1217
    "inReference",
1218
    "nomRefBase", // Class BookSection, Book, Article.
1219
    "pages",// Class Article.
1220
    "series",// Class Article, PrintSeries.
1221
    "school",// Class Thesis.
1222
    "institution",// Class Report.
1223
    "organization",// Class Proceedings.
1224
    "nextVersion",
1225
    "previousVersion",
1226
    "isbn",// Class Book.
1227
    "issn",// Class Journal.
1228
    "doi",
1229
    "uri"
1230
  );
1231

    
1232
  $table_rows = array();
1233

    
1234
  if (!isset($reference->authorship)) {
1235
    $authorship = cdm_ws_get(CDM_WS_REFERENCE_AUTHORTEAM, $reference->uuid);
1236
    $reference->authorship = isset($authorship->titleCache) ? $authorship->titleCache : '';
1237
  }
1238

    
1239
  if (!isset($reference->inReference)) {
1240
    $reference->inReference = cdm_ws_get(CDM_WS_REFERENCE, array(
1241
      $reference->uuid,
1242
      "inReference",
1243
    ));
1244
  }
1245

    
1246
  foreach ($field_order as $fieldname) {
1247

    
1248
    if (isset($reference->$fieldname)) {
1249

    
1250
      if ($fieldname == "datePublished") {
1251
        $period = $reference->$fieldname;
1252
        $datePublished = timePeriodToString($period);
1253
        if (isset($datePublished) && $datePublished != '') {
1254
          $table_rows[] = array(
1255
            t("Date published"),
1256
            $datePublished,
1257
          );
1258
        }
1259
      }
1260
      elseif ($fieldname == "doi" && is_object($reference->doi)) {
1261
        $table_rows[] = array(
1262
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1263
          cdm_doi($reference->doi, false)
1264
        );
1265
      }
1266
      elseif ($fieldname == "uri" && isset($reference->uri) && $reference->uri) {
1267
        $table_rows[] = array(
1268
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1269
          cdm_external_uri($reference->uri, false)
1270
        );
1271
      }
1272
      elseif (is_object($reference->$fieldname)) {
1273
        if ($fieldname == "authorship") {
1274
          $dump = $reference->$fieldname;
1275
          $teammembers = "teamMembers";
1276
          $team = $dump->$teammembers;
1277
          $nameArray = array();
1278

    
1279
          foreach ($team as $member) {
1280
            if (strlen($member->lastname) > 0) {
1281
              $nname = $member->lastname;
1282
              $name = $nname;
1283
              if (strlen($member->firstname) > 0) {
1284
                $vname = $member->firstname;
1285
                $name = $vname . " " . $nname;
1286
              }
1287
              $nameArray[] = $name;
1288
            }
1289
            else {
1290
              if (strlen($member->titleCache) > 0) {
1291
                $nameArray[] = $member->titleCache;
1292
              }
1293
            }
1294
          }
1295
          $value = join($nameArray, ", ");
1296
        }
1297
        elseif ($fieldname == "inReference") {
1298
          $type = $reference->$fieldname->type;
1299
          $value = l($reference->$fieldname->titleCache, path_to_reference($reference->$fieldname->uuid));
1300
          switch ($type) {
1301
            case "Book":
1302
              $fieldname = "in book";
1303
              break;
1304
            case "Journal":
1305
              $fieldname = "in journal";
1306
              break;
1307
            case "Proceedings":
1308
              $fieldname = "in proceedings";
1309
              break;
1310
          }
1311
        }
1312
        else {
1313
          $value = $reference->$fieldname->titleCache;
1314
        }
1315

    
1316

    
1317
        if (isset($value) && $value != '') {
1318
          $table_rows[] = array(
1319
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1320
            $value,
1321
          );
1322
        }
1323

    
1324
      }
1325
      else {
1326
        if (isset($reference->$fieldname) && $reference->$fieldname != '') {
1327
          $table_rows[] = array(
1328
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1329
            $reference->$fieldname,
1330
          );
1331
        }
1332
      }
1333
    }
1334
  }
1335

    
1336
  $out = theme_table(array(
1337
    'header' => array(),
1338
    'rows' => $table_rows,
1339
    'attributes' => array(
1340
      'class' => html_class_attribute_ref($reference)
1341
    ),
1342
    'caption' => NULL,
1343
    'colgroups' => NULL,
1344
    'sticky' => NULL,
1345
    'empty' => NULL,
1346
  ));
1347

    
1348
  if(isset($reference->referenceAbstract)){
1349
    $out .= '<h2 class="block-title">Abstract</h2><div class="abstract">' . $reference->referenceAbstract . '</div>';
1350
  }
1351

    
1352

    
1353
  // Annotations below the table
1354
  $annotations = cdm_fetch_visible_annotations($reference);
1355
  $out .= theme("cdm_annotations", array('annotations' => $annotations));
1356

    
1357
  $registration_working_set = cdm_ws_get("registrationWorkingSetDTO", array($uuid));
1358
  if($registration_working_set && count($registration_working_set->registrationDTOs) > 0){
1359
    $out .= "<h3>Nomenclatural acts:</h3><div class=\"cdm-item-list registration-item-list\">";
1360
    foreach($registration_working_set->registrationDTOs as $registration_dto){
1361
      if($registration_dto->status == "PUBLISHED"){
1362
        $registration_render_a = compose_registration_dto_compact($registration_dto, 'citation');
1363
        $registration_render_a["#prefix"] = "<div class=\"item item-registration\">";
1364
        $registration_render_a["#suffix"] = "</div>";
1365
        $out .= drupal_render($registration_render_a);
1366
      }
1367
    }
1368
    $out .= "</div>";
1369
  }
1370

    
1371
  RenderHints::popFromRenderStack();
1372

    
1373
  return markup_to_render_array($out);
1374
}
1375

    
1376
/**
1377
 * Provides the the label string for taxon page tabs.
1378
 *
1379
 * The $tabname as passed to the method will be returned if no override
1380
 * label is configured in the settings.
1381
 */
1382
function cdm_taxonpage_tab_label($tabname) {
1383
  static $taxon_tabs_labels = null;
1384
  if($taxon_tabs_labels == null){
1385
    $taxon_tabs_labels = get_array_variable_merged(CDM_TAXONPAGE_TAB_LABELS, CDM_TAXONPAGE_TAB_LABELS_DEFAULT);
1386
  }
1387
  $tabname_key = strtolower($tabname);
1388
  if(isset($taxon_tabs_labels[$tabname_key]) && $taxon_tabs_labels[$tabname_key]){
1389
    return $taxon_tabs_labels[$tabname_key];
1390
  }
1391
  return $tabname;
1392
}
(7-7/11)