Project

General

Profile

Download (48.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Page functions.
5
 *
6
 * @copyright
7
 *   (C) 2007-2012 EDIT
8
 *   European Distributed Institute of Taxonomy
9
 *   http://www.e-taxonomy.eu
10
 *
11
 *   The contents of this module are subject to the Mozilla
12
 *   Public License Version 1.1.
13
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
14
 *
15
 * @author
16
 *   - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
17
 */
18

    
19

    
20
/**
21
 * 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_TAXONPAGE_TAXON_NODE_SHOW_STATES, 1) && ( $page_part == 'description' || $page_part == 'synonymy' || $page_part == 'all')){
289
    $taxon_nodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, array($taxon->uuid));
290
    $render_array['taxon_node_status'] = compose_taxon_node_status($taxon_nodes);
291
    $render_array['taxon_node_status']['#weight'] = -100;
292
  }
293

    
294
  if (variable_get('cdm_dataportal_display_is_accepted_for', CDM_DATAPORTAL_DISPLAY_IS_ACCEPTED_FOR) && isset($_REQUEST['acceptedFor'])) {
295
    $render_array['accepted_for'] = markup_to_render_array(cdm_accepted_for($_REQUEST['acceptedFor']), $weight++);
296
  }
297

    
298
  // --- PAGE PART: DESCRIPTION --- //
299
  if (!taxon_page_tabs_hidden_check('description') && ($page_part == 'description' || $page_part == 'all')) {
300

    
301
    $merged_tree = merged_taxon_feature_tree($taxon);
302

    
303

    
304
    $render_array['general'] = compose_cdm_taxon_page_profile($taxon, $merged_tree, $media, !$synonymy_as_tab);
305
    $render_array['general']['#weight'] = $weight++;
306
    $render_array['general']['#prefix'] = '<div id="general" class="page-part">';
307
    $render_array['general']['#suffix'] = '</div>';
308
  }
309

    
310
  // --- PAGE PART: IMAGES --- //
311
  if (!taxon_page_tabs_hidden_check('images') && ($page_part == 'images' || $page_part == 'all')) {
312
    $images_html = '<div id="images" class="page-part">';
313
    if ($page_part == 'all') {
314
      $images_html .= '<h2>' . t(cdm_taxonpage_tab_label('Images')) . '</h2>';
315
    }
316
    // Get the image gallery as configured by the admin.
317
    $configured_image_gallery_viewer = variable_get(CDM_MEDIA_GALLERY_VIEWER, CDM_MEDIA_GALLERY_VIEWER_DEFAULT);
318
    if($configured_image_gallery_viewer != 'fsi'){
319
      $images_html .= render_taxon_media_gallery($taxon, $configured_image_gallery_viewer, $media);
320
    } else {
321
      // the fsi_gallery requires a flash plugin, in case the client browser is not supporting
322
      // flash we also need to provide an the default gallery as alternative
323
      $images_html .= render_taxon_media_gallery($taxon, CDM_MEDIA_GALLERY_VIEWER_DEFAULT, $media);
324
      $images_html .= render_taxon_media_gallery($taxon, $configured_image_gallery_viewer, $media);
325
    }
326
    $images_html .= '</div>'; // END of <div id="images">
327
    $render_array['images'] = markup_to_render_array($images_html, $weight++);
328
  }
329

    
330
  // --- PAGE PART: SYNONYMY --- //
331
  if (!taxon_page_tabs_hidden_check('synonymy') && (($page_part == 'synonymy' || $page_part == 'all') && $synonymy_as_tab)) {
332
    $synonymy_html = '<div id="synonymy" class="page-part">';
333
    if ($page_part == 'all') {
334
      $synonymy_html .= '<h2>' . t(cdm_taxonpage_tab_label('Synonymy')) . '</h2>';
335
    }
336
    $addAcceptedTaxon = variable_get(CDM_DATAPORTAL_NOMREF_IN_TITLE, CDM_DATAPORTAL_NOMREF_IN_TITLE_DEFAULT);
337

    
338
    $synonym_a = compose_cdm_taxon_page_synonymy($taxon, $addAcceptedTaxon);
339
    $synonymy_html .= drupal_render($synonym_a);
340

    
341
    $synonymy_html .= '</div>';
342
    $render_array['synonymy'] = markup_to_render_array($synonymy_html, $weight++);
343

    
344
  }
345

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

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

    
371
  // --- PAGE PART: EXPERTS --- //
372

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

    
381
  // ------------------ END OF PARTS -------------- //
382

    
383
  // adjust weights of page and toc elements according to the settings
384
  $taxontabs_weights = get_array_variable_merged(CDM_TAXONPAGE_TAB_WEIGHT, CDM_TAXONPAGE_TAB_WEIGHT_DEFAULT);
385
  foreach($taxontabs_weights as $tab_key => $weight){
386
    if(isset($render_array[$tab_key])){
387
      $render_array[$tab_key]['#weight'] = $weight;
388
    }
389
  }
390

    
391

    
392
  // set up the TOC and pseudo feature blocks for the pages which contain all page parts
393
  if($page_part == 'all' && (!isset( $render_array['general']) || count($render_array['general']) == 0 )) {
394

    
395
    $bibliography_block = make_bibliography_feature_block();
396
    if($bibliography_block){
397
      $render_array['bibliography_block'] = _block_get_renderable_array([$bibliography_block]);
398
      $render_array['bibliography_block']['#weight'] = 100;
399
      cdm_toc_list_add_item('Bibliography', 'bibliography', null, false, $render_array['bibliography_block']['#weight']);
400
    }
401

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

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

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

    
446
  }
447

    
448

    
449
  return $render_array;
450
}
451

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

    
479
  $render_array = array();
480

    
481
  $taxon_profile_image_settings = variable_get(CDM_TAXON_PROFILE_IMAGE, unserialize(CDM_TAXON_PROFILE_IMAGE_DEFAULT));
482

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

    
489
  if ($taxon_profile_image_settings['show'] && !$hide_taxon_profile_image) {
490

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

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

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

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

    
552
  if($add_synonymy){
553
    $synonymy_a = compose_cdm_taxon_page_synonymy($taxon, true);
554
    $synonymy_a['#weight'] = -102;
555
    $synonymy_a['#prefix'] = '<div id="synonymy">';
556
    $synonymy_a['#suffix'] = '</div>';
557
    $render_array['synonymy'] = $synonymy_a;
558
  }
559

    
560
  //  $pseudo_feature_block_toc_items = array();
561

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

    
565
  // >>>>>>>>>>>>>>>>>>> PSEUDO FEATURES >>>>>>>>>>>>>>>>>>>
566
  $pseudo_feature_weights = get_array_variable_merged(CDM_PSEUDO_FEATURE_BLOCK_WEIGHTS, CDM_PSEUDO_FEATURE_BLOCK_WEIGHTS_DEFAULT);
567

    
568
  // Bibliography
569
  $bibliography_block = make_bibliography_feature_block();
570
  if($bibliography_block){
571
    $feature_block_list[$pseudo_feature_weights[PSEUDO_FEATURE_BIBLIOGRAPHY]] = $bibliography_block;
572
    cdm_toc_list_add_item('Bibliography', 'bibliography', null, false, $pseudo_feature_weights[PSEUDO_FEATURE_BIBLIOGRAPHY]);
573
  }
574

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

    
594
      $feature_block_list[$pseudo_feature_weights[PSEUDO_FEATURE_AGGREGATION_DESCRIPTIONS]] = $description_block;
595
      cdm_toc_list_add_item('Descriptions (aggregated)', 'aggregation_descriptions', null, false, $pseudo_feature_weights[PSEUDO_FEATURE_AGGREGATION_DESCRIPTIONS]);
596

    
597

    
598
  }
599

    
600
  // sort by weight
601
  ksort($feature_block_list);
602
  $render_array['taxon_description_features'] = _block_get_renderable_array($feature_block_list);
603

    
604
/*  // update TOC
605
  if ($pseudo_feature_block_toc_items){
606
    foreach ($pseudo_feature_block_toc_items as $label=>$fragment){
607
      cdm_toc_list_add_item($label, $fragment);
608
    }
609
  }
610
*/
611
  // <<<<<<<<<<<<<<<<<<< PSEUDO FEATURES <<<<<<<<<<<<<<<<<<<
612

    
613
  // create the table of content
614
  $toc = array(
615
      '#theme' => 'item_list',
616
    '#items' => cdm_toc_list(),
617
      '#title' => t('Content'),
618
    '#weight' => -100,                  // move to the top
619
      '#suffix' => '</div>',
620
      '#prefix'=> '<div id="page-toc">'
621
  );
622
  $render_array['taxon_description_feature_toc'] = $toc;
623

    
624
  return $render_array;
625
}
626

    
627
/**
628
 * @return object|\stdclass
629
 */
630
function make_bibliography_feature_block() {
631
  $bibliography_block = null;
632
  $bibliography_settings = get_bibliography_settings();
633
  if ($bibliography_settings['enabled'] == 1) {
634
    $bibliography_markup = FootnoteManager::renderFootnoteList(PSEUDO_FEATURE_BIBLIOGRAPHY, '');
635
    if ($bibliography_markup) {
636
      $feature_bibliography = make_pseudo_feature('Bibliography', PSEUDO_FEATURE_BIBLIOGRAPHY);
637
      $bibliography_item = markup_to_render_array($bibliography_markup);
638
      $bibliography_block = feature_block(t('Bibliography'), $feature_bibliography);
639
      $bibliography_block->content = [];
640
      $bibliography_block->content[] = compose_feature_block_wrap_elements([$bibliography_item], $feature_bibliography);
641
    }
642
  }
643
  return $bibliography_block;
644
}
645

    
646

    
647
/**
648
 * Renders the link which will lead to the specimen detail page
649
 * @param object $specimen
650
 *    the cdm specimen entity which will be linked to
651
 *
652
 * @return string
653
 *     the markup for the link
654
 */
655
function render_cdm_specimen_link($specimen) {
656
  $path = path_to_specimen($specimen->uuid);
657
  $attributes['class'][] = html_class_attribute_ref($specimen);
658
  return $specimen->titleCache.icon_link($path);
659
}
660

    
661
/**
662
 * Returns HTML containing the synonymy for the accepted taxon.
663
 *
664
 * Shows the whole synonymy for the accepted taxon.
665
 * The synonymy list is headed by the complete scientific name
666
 * of the accepted taxon with nomenclatural reference.
667
 *
668
 * @param object $taxon
669
 * @param boolean $add_accepted_taxon
670
 *
671
 * @return array
672
 *  Drupal render array for the synonymy
673
 *
674
 * @throws Exception
675
 *
676
 * @ingroup compose
677
 */
678
function compose_cdm_taxon_page_synonymy($taxon, $add_accepted_taxon) {
679

    
680
  RenderHints::pushToRenderStack('taxon_page_synonymy');
681

    
682
  // footnote key for the homotypic group and accepted taxon,
683
  // both should have the same footnote key
684
  RenderHints::setFootnoteListKey(RenderHints::getRenderPath());
685

    
686
  $synomymie = cdm_ws_get(CDM_WS_PORTAL_TAXON_SYNONYMY, array($taxon->uuid));
687

    
688
  $out = '';
689

    
690
  // Render accepted taxon.
691
  //
692
  // foonotes of the accepted taxon will be rendered in the homotypic group section
693
  // even if there are not synonyms in the homotypic group
694
  // homotypic group and accepted taxon should have the same footnote key
695
  $referenceUri = '';
696
  if ($add_accepted_taxon) {
697
    // set new render path for the accepted taxon so
698
    // it can be styled differently via the name render part definitions
699
    RenderHints::pushToRenderStack('accepted_taxon');
700
    $accepted_name = '';
701
    if (variable_get(CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE, 0)) {
702
      $label = variable_get(CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE_LABEL, CDM_SYNONYMY_ACCEPTED_TAXON_SEC_SEPARATE_LABEL_DEFAULT);
703
      $accepted_name .= '<div class="secReference"><span class="label">' . t($label) . ':</span> ' . $taxon->sec->titleCache . '</div>';
704
    }
705
    if (isset($taxon->name->nomenclaturalReference)) {
706
      $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
707
    }
708

    
709
    $accepted_name .= '<div class="accepted-name">';
710
    $accepted_name .= render_taxon_or_name($taxon, NULL, $referenceUri);
711

    
712
    $name_relations = cdm_name_relationships_for_taxon($taxon);
713
    $name_relationships = compose_name_relationships_inline($name_relations, $taxon->name->uuid, $taxon->uuid);
714
    // Render relationships of accepted name.
715
    if(isset($name_relationships['list']['items'][0])){
716
      $accepted_name .= ' ' . drupal_render($name_relationships);
717
    }
718
    $accepted_name .= render_taxon_and_name_footnotes($taxon);
719
    $accepted_name .= '</div>';
720
    RenderHints::popFromRenderStack();
721
  }
722

    
723
  // --- Render homotypic synonymy group
724
  if (!empty($accepted_name)) {
725
    $out .= $accepted_name;
726
  }
727

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

    
737

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

    
750
  RenderHints::popFromRenderStack();
751

    
752
  return markup_to_render_array($out);
753
}
754

    
755
/**
756
 * Provides the default configuration for handle_annotations_and_sources() in the
757
 * synonymy.
758
 *
759
 * @return bool[]
760
 */
761
function synonymy_annotations_and_source_config() {
762
  static $annotations_and_sources_config = null;
763
  if(!$annotations_and_sources_config){
764
    $bibliography_settings = get_bibliography_settings();
765
    $annotations_and_sources_config = [
766
      'sources_as_content' => FALSE,
767
      'link_to_name_used_in_source' => TRUE,
768
      'link_to_reference' => TRUE,
769
      'add_footnote_keys' => TRUE,
770
      'bibliography_aware' => $bibliography_settings['enabled'] == 1
771
    ];
772
  }
773
  return $annotations_and_sources_config;
774
}
775

    
776

    
777
/**
778
 * composes and returns an render array for the experts associated with the given taxon
779
 *
780
 * @param object taxon
781
 *
782
 * @return array
783
 *   A Drupal render array for a table with the experts
784
 *
785
 * @ingroup compose
786
 */
787
function compose_cdm_taxon_page_experts($taxon){
788

    
789
  $render_array = array();
790
  if(!isset($taxon->uuid)){
791
    return $render_array;
792
  }
793

    
794
  $current_classification_uuid = get_current_classification_uuid();
795
  // TODO use cdm_ws_fetchall below but this failes! needs fix!
796
  $taxon_node_agent_relations = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
797
    array(
798
      $taxon->uuid,
799
      $current_classification_uuid
800
    )
801
  );
802

    
803
  $header = array(
804
    array('data' => t('Expert')),
805
    array('data' => t('Role'))
806
  );
807
  $rows = array();
808

    
809

    
810
  foreach($taxon_node_agent_relations->records as $taxon_node_agent_relation){
811

    
812

    
813
    $expert_role_id = $taxon_node_agent_relation->agent->uuid . '-' . $taxon_node_agent_relation->type->uuid;
814
    $expert_details_container_id = 'expert_details_' . $expert_role_id;
815

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

    
830
    // Expert and Role
831
    $rows[] = array(
832
      'data' => array(
833
        array(
834
          'data' => $agent_label_markup,
835
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->agent))
836
        ),
837
        array(
838
          'data' => $taxon_node_agent_relation->type->representation_L10n,
839
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->type))
840
        )
841
      )
842
    );
843
    // Agent details
844
    $rows[] = array(
845
      'data' => array(
846
        array(
847
          'data' => '<!-- expert_details_container -->',
848
          'id' => $expert_details_container_id,
849
          'colspan' => 2
850
        )
851
      )
852
    );
853

    
854
  }
855

    
856

    
857
  $render_array['experts_table'] = array(
858
    '#theme' => 'table',
859
    '#header' => $header,
860
    '#rows' => $rows,
861
  );
862

    
863

    
864
  return $render_array;
865
}
866

    
867

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

    
890
  if(!isset($tabs)){
891
    $tabs = array();
892
  }
893

    
894
  if (isset($add_tab) && !array_key_exists($add_tab, $tabs)) {
895
    $tabs[] = $add_tab;
896
  }
897

    
898
  return $tabs;
899
}
900

    
901
/**
902
 * Manages the tabs to be hidden in the taxon page.
903
 *
904
 * The tabs names are identified by their last menu link path element:
905
 *  - description
906
 *  - synonymy
907
 *  - images
908
 *  - specimens
909
 *  - key
910
 *
911
 * Internally the tabs are stored in a static variable which is
912
 * managed by drupal_static().
913
 *
914
 * @param string $tabname
915
 *   The name of the tab to check
916
 *
917
 * @return boolean
918
 *   True if the tab or section is to be hidden
919
 */
920
function taxon_page_tabs_hidden_check($tabname) {
921

    
922
  $tabs = &drupal_static('taxon_page_tabs_hidden');
923

    
924
  if(!isset($tabs)){
925
    $tabs = array();
926
  }
927

    
928
  return array_search($tabname, $tabs) !== FALSE;
929
}
930

    
931
/**
932
 * Implements the hook_preprocess_HOOK() for theme_menu_local_tasks()
933
 *
934
 *  - Removes the tabs to be hidden, @see taxon_page_tabs_hidden_add()
935
 *  - Renames tabs according to the settings // TODO (this will replace the theme_cdm_taxonpage_tab() function !!!)
936
 *
937
 * @param array $variables
938
 *   The variables array
939
 */
940
function cdm_dataportal_preprocess_menu_local_tasks(&$variables) {
941

    
942
  $hidden_tabs = taxon_page_tabs_hidden_add();
943

    
944
  if (!variable_get(CDM_SEARCH_BLAST_ENABLED)) {
945
      if (is_array($variables['primary'])) {
946
          foreach ($variables['primary'] as $key => &$element) {
947
              if ($element['#link']['path'] == 'cdm_dataportal/search/blast') {
948
                  // remove the tab
949
                  unset($variables['primary'][$key]);
950
              }
951

    
952
          }
953
      }
954
  }
955
  if (is_array($variables['primary'])) {
956
    foreach ($variables['primary'] as $key => &$element) {
957

    
958
      // 1. Remove the tabs to be hidden
959
      foreach ($hidden_tabs as $tab) {
960
        if ($element['#link']['path'] == 'cdm_dataportal/taxon/%/' . $tab) {
961
          // remove the tab
962
          unset($variables['primary'][$key]);
963
        }
964
      }
965
    }
966
  }
967
}
968

    
969

    
970

    
971
/**
972
 * Implements the hook_preprocess_HOOK() for theme_menu_local_task()
973
 *
974
 *
975
 * @param array $variables
976
 *   An associative array containing:
977
 *     - element: A render element containing:
978
 *          #link: A menu link array with 'title', 'href', and 'localized_options' keys.
979
 *          #active: A boolean indicating whether the local task is active.
980
 *
981
 */
982
function cdm_dataportal_preprocess_menu_local_task(&$variables) {
983

    
984
  $link = $variables['element']['#link'];
985
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
986
    $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']) . '"/>';
987
    $link['localized_options']['html'] = TRUE;
988

    
989
    $variables['element']['#link'] = $link;
990
  }
991
}
992

    
993
/* =================== block composition ===================== */
994

    
995
/**
996
 * Composes and returns an render array for the classification breadcrumbs of the given taxon.
997
 *
998
 * @param taxon
999
 *
1000
 * @return array
1001
 *   A Drupal render array for a table with the experts
1002
 *
1003
 * @ingroup compose
1004
 */
1005
function compose_classification_breadcrumbs($taxon_uuid) {
1006

    
1007
  _add_js_taxonomic_children('#classification-breadcrumbs .taxonomic-children-button');
1008

    
1009
  $render_array = array();
1010

    
1011
  $render_array['#theme'] = 'item_list';
1012
  $render_array['#type'] = 'ul';
1013
  $render_array['#attributes'] = array(
1014
    'id' => 'classification-breadcrumbs',
1015
    'class' => 'breadcrumbs inline',
1016
  );
1017

    
1018
  $items = array();
1019

    
1020
  $parent_taxon_nodes = null;
1021
  if($taxon_uuid){
1022
    $parent_taxon_nodes = cdm_ws_taxonomy_pathFromRoot($taxon_uuid);
1023
  }
1024

    
1025
  $classifications = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXONOMY);
1026
  // find current classification in list
1027
  $classification = null;
1028
  $current_classification_uuid = get_current_classification_uuid();
1029
  foreach ($classifications as $classification){
1030
    if($classification->uuid == $current_classification_uuid){
1031
      break;
1032
    }
1033
  }
1034

    
1035
  $node_name = '';
1036
  if(count($classifications) > 1 ){
1037
    // need to add the current classification as first label
1038

    
1039
    $label = $classification->titleCache;
1040
    if(strlen($label) > 20){
1041
      $label = substr($label, 0, strpos($label, ' ', 15)) . '...';
1042
    }
1043
    $node_name = font_awesome_icon_markup('fa-th-list')  . ' ' . l($label, '#', array(
1044
      'attributes' => array(
1045
        'class' => 'taxonomic-children-button classification-chooser',
1046
        'data-destination-uri' => drupal_get_destination(),
1047
        'data-cdm-align-with' => array('prev')
1048
      ),
1049
      'html' => true
1050
    ));
1051
  }
1052

    
1053
  $rank_limit_uuid = variable_get(TAXONTREE_RANKLIMIT, TAXONTREE_RANKLIMIT_DEFAULT);
1054

    
1055
  $rank_separator = '<span> '
1056
    . font_awesome_icon_markup('fa-chevron-right')
1057
    . ' </span>';
1058
  $more_children_icon = font_awesome_icon_markup('fa-sitemap fa-rotate-270');
1059
  $more_children_label = '...';
1060

    
1061
  $items[] = $node_name;
1062

    
1063
  $more_children_for = null;
1064
  if($parent_taxon_nodes){
1065
    foreach ($parent_taxon_nodes as $node) {
1066

    
1067
      $is_first_item = count($items) == 0;
1068
      $is_last_item = count($items) == count($parent_taxon_nodes);
1069
      $node_name = cdm_dataportal_shortname_of($node);
1070
      $path = path_to_taxon($node->taxonUuid);
1071

    
1072
      if($node->taxonomicChildrenCount > 0) {
1073
        $more_children_for = $node->taxonUuid;
1074
      } else {
1075
        $more_children_for = null;
1076
      }
1077

    
1078
      // 'fa-sitemap'
1079

    
1080
      $items[] =
1081
        ($is_first_item ? '' : ' ')
1082
        . $rank_separator
1083
        . l(
1084
          '<span class="' . html_class_attribute_ref($node) . '">' . $node_name . '</span>',
1085
          $path,
1086
          array(
1087
            'attributes' => array(
1088
              'class' => array('taxonomic-children-button'),
1089
              'data-cdm-taxon-uuid' => array($node->taxonUuid),
1090
              'data-cdm-classification-mode' => array('siblings'),
1091
              'data-cdm-align-with' => array('prev')
1092
            ),
1093
            'html' => true
1094
          )
1095
        );
1096
      }
1097
    }
1098

    
1099
  // add more button to the end
1100
  if(!$parent_taxon_nodes) {
1101
    // not taxon focused yet, adding button to make  the root nodes available
1102
    $items[] = '<span>'
1103
      . $more_children_icon . '&nbsp;' .
1104
      '<span class="taxonomic-children-button" data-classification-uuid="' . $current_classification_uuid
1105
      . '" data-rank-limit-uuid="' . $rank_limit_uuid . '" data-cdm-align-with="prev"> ' . $more_children_label . '<span>'
1106
      . '</span>';
1107
  } else if($more_children_for){
1108
    // last parent item has child taxon nodes
1109
    $items[] = ' <span>'
1110
      . $more_children_icon . '&nbsp;' .
1111
      '<span class="taxonomic-children-button" data-cdm-taxon-uuid="' .$more_children_for
1112
      . '" data-cdm-classification-mode="children" data-cdm-align-with="prev"> ' . $more_children_label . '</span>'
1113
      . '</span>';
1114

    
1115
  }
1116

    
1117
  $render_array['#items'] = $items;
1118

    
1119
  return $render_array;
1120
}
1121

    
1122

    
1123
/**
1124
 * @param $specimen_uuid
1125
 * @return array
1126
 *    The drupal render array for the page
1127
 *
1128
 * @ingroup compose
1129
 */
1130
function compose_cdm_specimen_page($specimen_uuid)
1131
{
1132
  drupal_set_title("Specimen Details");
1133
  RenderHints::pushToRenderStack('specimen_page');
1134

    
1135
  $specimen_table = array(
1136
      '#theme' => 'table',
1137
      '#weight' => 2,
1138
      // prefix attributes and rows with '#' to let it pass to the theme function,
1139
      // otherwise it is handled as child render array
1140

    
1141
    '#attributes' => array('class' => 'specimens'),
1142
      '#rows' => array(),
1143
      '#prefix' => '<div id="specimens">',
1144
      '#suffix' => '</div>',
1145

    
1146

    
1147
  );
1148
  $specimen = compose_cdm_specimen_or_observation($specimen_uuid, true);
1149
  /*
1150
  $render_array = array(
1151
          '#theme' => 'item_list',
1152
          '#items' => array($specimen),
1153
          '#type' => 'ul');
1154
  $output = drupal_render($render_array);
1155
 */
1156
  $specimen_table['#rows'][] = array(
1157
          // An array of table rows. Every row is an array of cells, or an associative array
1158
          'data' => $specimen,
1159
          'class' =>  array(
1160
              'descriptionElement',
1161
              'descriptionElement_IndividualsAssociation'
1162
          ),
1163
  );
1164

    
1165

    
1166
 // $detail_html = compose_cdm_specimen_or_observation($specimen_uuid, true);
1167
//    $render_array['markup'] = array(
1168
//        '#prefix' => '<div id="specimens" class="page">',
1169
//        'content' => $specimen_table,
1170
//        '#suffix' => '</div>',
1171
//    );
1172
  $render_array['specimen_table'] =  $specimen_table;
1173

    
1174
  RenderHints::popFromRenderStack();
1175
  return $specimen; // $render_array;
1176
}
1177

    
1178
/**
1179
 * @param $named_area_uuid
1180
 * @return array
1181
 *    The drupal render array for the page
1182
 *
1183
 * @ingroup compose
1184
 */
1185
function compose_cdm_named_area_page($named_area_uuid)
1186
{
1187

    
1188
  $named_area = cdm_ws_get(CDM_WS_PORTAL_TERM, array($named_area_uuid));
1189

    
1190
  $render_array = array();
1191
  RenderHints::pushToRenderStack('named_area_page');
1192

    
1193
  $groups = array();
1194
  @_description_list_group_add($groups, t('Name') . ':', $named_area->representation_L10n);
1195
  @_description_list_group_add($groups, t('IdInVocabulary') . ':', $named_area->idInVocabulary);
1196
  if(isset($named_area->level)) {
1197
    @_description_list_group_add($groups, t('Level') . ':', $named_area->level->representation_L10n);
1198
  }
1199

    
1200
  $name_area_details_elements = array(
1201
   // '#title' => $title,
1202
    '#theme' => 'description_list',
1203
    '#groups' => $groups,
1204
    '#attributes' => array('class' => html_class_attribute_ref($named_area)),
1205
  );
1206

    
1207
  $render_array[] = $name_area_details_elements;
1208

    
1209
  RenderHints::popFromRenderStack();
1210
  return $render_array;
1211
}
1212

    
1213

    
1214
/**
1215
 * Returns a drupal render array for a single reference page.
1216
 *
1217
 * Composes a page with all data on a single reference.
1218
 *
1219
 * @param string $uuid
1220
 *   An uuid for a cdm reference.
1221
 *
1222
 * @return array
1223
 *  A drupal render array
1224
 *
1225
 * @throws Exception
1226
 *
1227
 * @ingroup compose
1228
 */
1229
function compose_cdm_reference_page($uuid) {
1230

    
1231
  $pathelement = "reference_page";
1232
  RenderHints::pushToRenderStack($pathelement);
1233
  $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
1234
  if (!$reference) {
1235
    drupal_set_title(t('Reference does not exist'), PASS_THROUGH);
1236
    return "";
1237
  }
1238
  if (isset($reference->titleCache)) {
1239
    drupal_set_title($reference->titleCache, PASS_THROUGH);
1240
  }
1241

    
1242
  $field_order = array(
1243
    "title",
1244
    "abbrevTitle",
1245
    // "titleCache" abbrevTitleCache
1246
    // "citation",
1247
    "authorship",
1248
    "editor",
1249
    "publisher",
1250
    "placePublished",
1251
    "datePublished",
1252
    "year",
1253
    "edition",// Class Book.
1254
    "volume",// Class Article.
1255
    "seriesPart",
1256
    "inReference",
1257
    "nomRefBase", // Class BookSection, Book, Article.
1258
    "pages",// Class Article.
1259
    "series",// Class Article, PrintSeries.
1260
    "school",// Class Thesis.
1261
    "institution",// Class Report.
1262
    "organization",// Class Proceedings.
1263
    "nextVersion",
1264
    "previousVersion",
1265
    "isbn",// Class Book.
1266
    "issn",// Class Journal.
1267
    "doi",
1268
    "uri"
1269
  );
1270

    
1271
  $table_rows = array();
1272

    
1273
  if (!isset($reference->authorship)) {
1274
    $authorship = cdm_ws_get(CDM_WS_REFERENCE_AUTHORTEAM, $reference->uuid);
1275
    $reference->authorship = isset($authorship->titleCache) ? $authorship->titleCache : '';
1276
  }
1277

    
1278
  if (!isset($reference->inReference)) {
1279
    $reference->inReference = cdm_ws_get(CDM_WS_REFERENCE, array(
1280
      $reference->uuid,
1281
      "inReference",
1282
    ));
1283
  }
1284

    
1285
  foreach ($field_order as $fieldname) {
1286

    
1287
    if (isset($reference->$fieldname)) {
1288

    
1289
      if ($fieldname == "datePublished") {
1290
        $period = $reference->$fieldname;
1291
        $datePublished = timePeriodToString($period);
1292
        if (isset($datePublished) && $datePublished != '') {
1293
          $table_rows[] = array(
1294
            t("Date published"),
1295
            $datePublished,
1296
          );
1297
        }
1298
      }
1299
      elseif ($fieldname == "doi" && is_object($reference->doi)) {
1300
        $table_rows[] = array(
1301
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1302
          cdm_doi($reference->doi, false)
1303
        );
1304
      }
1305
      elseif ($fieldname == "uri" && isset($reference->uri) && $reference->uri) {
1306
        $table_rows[] = array(
1307
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1308
          cdm_external_uri($reference->uri, false)
1309
        );
1310
      }
1311
      elseif (is_object($reference->$fieldname)) {
1312
        if ($fieldname == "authorship") {
1313
          $dump = $reference->$fieldname;
1314
          $teammembers = "teamMembers";
1315
          $team = $dump->$teammembers;
1316
          $nameArray = array();
1317

    
1318
          foreach ($team as $member) {
1319
            if (strlen($member->lastname) > 0) {
1320
              $nname = $member->lastname;
1321
              $name = $nname;
1322
              if (strlen($member->firstname) > 0) {
1323
                $vname = $member->firstname;
1324
                $name = $vname . " " . $nname;
1325
              }
1326
              $nameArray[] = $name;
1327
            }
1328
            else {
1329
              if (strlen($member->titleCache) > 0) {
1330
                $nameArray[] = $member->titleCache;
1331
              }
1332
            }
1333
          }
1334
          $value = join($nameArray, ", ");
1335
        }
1336
        elseif ($fieldname == "inReference") {
1337
          $type = $reference->$fieldname->type;
1338
          $value = l($reference->$fieldname->titleCache, path_to_reference($reference->$fieldname->uuid));
1339
          switch ($type) {
1340
            case "Book":
1341
              $fieldname = "in book";
1342
              break;
1343
            case "Journal":
1344
              $fieldname = "in journal";
1345
              break;
1346
            case "Proceedings":
1347
              $fieldname = "in proceedings";
1348
              break;
1349
          }
1350
        }
1351
        else {
1352
          $value = $reference->$fieldname->titleCache;
1353
        }
1354

    
1355

    
1356
        if (isset($value) && $value != '') {
1357
          $table_rows[] = array(
1358
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1359
            $value,
1360
          );
1361
        }
1362

    
1363
      }
1364
      else {
1365
        if (isset($reference->$fieldname) && $reference->$fieldname != '') {
1366
          $table_rows[] = array(
1367
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1368
            $reference->$fieldname,
1369
          );
1370
        }
1371
      }
1372
    }
1373
  }
1374

    
1375
  $out = theme_table(array(
1376
    'header' => array(),
1377
    'rows' => $table_rows,
1378
    'attributes' => array(
1379
      'class' => html_class_attribute_ref($reference)
1380
    ),
1381
    'caption' => NULL,
1382
    'colgroups' => NULL,
1383
    'sticky' => NULL,
1384
    'empty' => NULL,
1385
  ));
1386

    
1387
  if(isset($reference->referenceAbstract)){
1388
    $out .= '<h2 class="block-title">Abstract</h2><div class="abstract">' . $reference->referenceAbstract . '</div>';
1389
  }
1390

    
1391

    
1392
  // Annotations below the table
1393
  $annotations = cdm_fetch_visible_annotations($reference);
1394
  $out .= cdm_annotations($annotations);
1395

    
1396
  $registration_working_set = cdm_ws_get("registrationWorkingSetDTO", array($uuid));
1397
  if($registration_working_set && count($registration_working_set->registrationDTOs) > 0){
1398
    $out .= "<h3>Nomenclatural acts:</h3><div class=\"cdm-item-list registration-item-list\">";
1399
    foreach($registration_working_set->registrationDTOs as $registration_dto){
1400
      if($registration_dto->status == "PUBLISHED"){
1401
        $registration_render_a = compose_registration_dto_compact($registration_dto, 'citation');
1402
        $registration_render_a["#prefix"] = "<div class=\"item item-registration\">";
1403
        $registration_render_a["#suffix"] = "</div>";
1404
        $out .= drupal_render($registration_render_a);
1405
      }
1406
    }
1407
    $out .= "</div>";
1408
  }
1409

    
1410
  RenderHints::popFromRenderStack();
1411

    
1412
  return markup_to_render_array($out);
1413
}
1414

    
1415
/**
1416
 * Provides the the label string for taxon page tabs.
1417
 *
1418
 * The $tabname as passed to the method will be returned if no override
1419
 * label is configured in the settings.
1420
 */
1421
function cdm_taxonpage_tab_label($tabname) {
1422
  static $taxon_tabs_labels = null;
1423
  if($taxon_tabs_labels == null){
1424
    $taxon_tabs_labels = get_array_variable_merged(CDM_TAXONPAGE_TAB_LABELS, CDM_TAXONPAGE_TAB_LABELS_DEFAULT);
1425
  }
1426
  $tabname_key = strtolower($tabname);
1427
  if(isset($taxon_tabs_labels[$tabname_key]) && $taxon_tabs_labels[$tabname_key]){
1428
    return $taxon_tabs_labels[$tabname_key];
1429
  }
1430
  return $tabname;
1431
}
(9-9/14)