Project

General

Profile

Download (48.5 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 array
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
  $root_unit_dtos = null;
41
  $specimensOrObservations = array();
42
  $current_view_mode = variable_get(CDM_SPECIMEN_LIST_VIEW_MODE, CDM_SPECIMEN_LIST_VIEW_MODE_DEFAULT);
43
  if ($current_view_mode == CDM_SPECIMEN_LIST_VIEW_MODE_OPTION_DERIVATE_TABLE || $current_view_mode == CDM_SPECIMEN_LIST_VIEW_MODE_OPTION_DERIVATE_TREE){
44
      // get fieldUnitDTOs
45
      $root_unit_dtos = cdm_ws_get(CDM_WS_PORTAL_TAXON_ROOTUNIT_DTOS, array( $taxon->uuid));
46
      $root_unit_dtos = order_fieldUnitDtos_by_date_and_type($root_unit_dtos);
47
    } else {
48
      // $specimensOrObservations = cdm_ws_get(CDM_WS_TAXON, array( $taxon->uuid, 'specimensOrObservations'));
49
      // extend by associated taxas occurrences
50
      $relationship_filter_query_parameters = relationship_filter_query_parameters();
51

    
52
      if (isset($_REQUEST['pager']) && is_array($_REQUEST['pager'])) {
53
        $relationship_filter_query_parameters = array_merge($relationship_filter_query_parameters, $_REQUEST['pager']);
54
      }
55
      $by_associatedtaxon_query = http_build_query($relationship_filter_query_parameters);
56
      $pager = cdm_ws_get(CDM_WS_OCCURRENCE_BY_ASSOCIATEDTAXON,
57
        null,
58
        $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
59
      );
60
      if (isset($pager->records[0])) {
61
        $specimensOrObservations = $pager->records;
62
      }
63
      // cdm_ws_get(CDM_WS_OCCURRENCE_ROOTUNIT_DTO_BY_ASSOCIATEDTAXON // was 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 || $root_unit_dtos > 0) {
88
       $render_array['map'] = occurrence_map_query_parameters($taxon);
89
    }
90

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

    
93
    if(($current_view_mode == CDM_SPECIMEN_LIST_VIEW_MODE_OPTION_DERIVATE_TABLE)){
94

    
95
        //COMPRESSED SPECIMEN DERIVATE TABLE
96
        $pager_root_units = cdm_ws_page(
97
          cdm_compose_url(CDM_WS_PORTAL_TAXON_ASSOCIATED_ROOTUNITS, [$taxon->uuid]),
98
          variable_get(CDM_SEARCH_RESULT_PAGE_SIZE, CDM_SEARCH_RESULT_PAGE_SIZE_DEFAULT) * 2,
99
          isset($_REQUEST['pager']['pageIndex']) ? isset($_REQUEST['pager']['pageIndex']) : 0
100
        );
101

    
102
        if (isset($pager_root_units->records[0])) {
103
          $root_unit_uuids = array();
104
          foreach ($pager_root_units->records as $root_unit) {
105
            $root_unit_uuids[] = $root_unit->uuid;
106
          }
107

    
108
          $render_array['derivate_hierarchy_table'] = compose_compressed_specimen_derivate_table($root_unit_uuids);
109
        }
110

    
111
        $render_array['pager'] = markup_to_render_array(
112
            theme('cdm_pager', array(
113
                'pager' => $pager_root_units,
114
                'path' => $_REQUEST['q'],
115
                'parameters' => $_REQUEST
116
            )),
117
            10 // weight
118
        );
119
    }
120
    else if(($current_view_mode == CDM_SPECIMEN_LIST_VIEW_MODE_OPTION_DERIVATE_TREE)){ // FIXME this seems to be wrong
121
      $derivationTreeComposer = new DerivationTreeComposer($root_unit_dtos);
122
      $derivationTreeComposer->setWithDetails(true);
123
      $derivationTreeComposer->setCollapsible(true);
124
      $render_array['specimen_list'] = $derivationTreeComposer->compose();
125
      $render_array['specimen_list']['#weight'] = 2;
126
    } else {
127
      $specimen_table = compose_specimens_table_bottom_up($specimensOrObservations);
128

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

    
140
    RenderHints::popFromRenderStack();
141
    return $render_array;
142
}
143

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

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

    
162

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

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

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

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

    
222
  // add all mandatory js sources
223
  _add_js_footnotes();
224

    
225

    
226
  $render_array = array();
227
  $weight = 0; // the weight for the render array elements
228

    
229
  $tabsToDisplay = variable_get('cdm_taxonpage_tabs_visibility', unserialize(TAXONPAGE_VISIBILITY_OPTIONS_DEFAULT));
230

    
231
  $page_part = variable_get('cdm_dataportal_taxonpage_tabs', 1) ? $page_part : 'all';
232

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

    
242
  $media = _load_media_for_taxon($taxon);
243

    
244

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

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

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

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

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

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

    
284
  // -------------------------------------------- //
285

    
286
  if(variable_get(CDM_TAXONPAGE_TAXON_NODE_SHOW_STATES, 1) && ( $page_part == 'description' || $page_part == 'synonymy' || $page_part == 'all')){
287
    $taxon_nodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, array($taxon->uuid));
288
    if(is_array($taxon_nodes)){
289
      $render_array['taxon_node_status'] = compose_taxon_node_status($taxon_nodes);
290
      $render_array['taxon_node_status']['#weight'] = -100;
291
    }
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
          if($image_info){
529
            $representationPart->width = $image_info['width'];
530
            $representationPart->height = $image_info['height'];
531
            $representationPart->uri = $url;
532
          }
533
        }
534
        if(!isset($representationPart->width)){
535
          // either no custom placeholder defined or $image_info could not be loaded
536
          // use the hard coded default
537
          $representationPart->width = 184;
538
          $representationPart->height = 144;
539
          $representationPart->uri = base_path() . drupal_get_path('module',
540
              'cdm_dataportal') . '/images/no_picture.png';
541
        }
542
        $attributes['alt'] = "no image available";
543
      }
544
    }
545

    
546
    if (isset($representationPart->uri)) {
547
      $profile_image = cdm_media_gallery_entry_image(
548
        ImageMediaItem::fromMediaRepresentationPart($representationPart),
549
        $taxon_profile_image_settings['maxextend'],
550
        FALSE,
551
        $attributes
552
      );
553
      // NOTE: style="width:${maxextend}px' is needed for IE8 !!!
554
      $max_extend_with = $taxon_profile_image_settings['maxextend'] . 'px';
555
      $render_array['taxon_profile_image'] = markup_to_render_array('<div id="taxonProfileImage" style="width:' . $max_extend_with . '">' . $profile_image . '</div>',
556
        -101);
557
    }
558
  }
559

    
560
  if($add_synonymy){
561
    $synonymy_a = compose_cdm_taxon_page_synonymy($taxon, true);
562
    $synonymy_a['#weight'] = -102;
563
    $synonymy_a['#prefix'] = '<div id="synonymy">';
564
    $synonymy_a['#suffix'] = '</div>';
565
    $render_array['synonymy'] = $synonymy_a;
566
  }
567

    
568
  //  $pseudo_feature_block_toc_items = array();
569

    
570
  // Render the sections for each real feature
571
  $feature_block_list = make_feature_block_list($merged_tree->root->childNodes, $taxon);
572

    
573
  // >>>>>>>>>>>>>>>>>>> PSEUDO FEATURES >>>>>>>>>>>>>>>>>>>
574
  $pseudo_feature_weights = get_array_variable_merged(CDM_PSEUDO_FEATURE_BLOCK_WEIGHTS, CDM_PSEUDO_FEATURE_BLOCK_WEIGHTS_DEFAULT);
575

    
576
  // Bibliography
577
  $bibliography_block = make_bibliography_feature_block();
578
  if($bibliography_block){
579
    $feature_block_list[$pseudo_feature_weights[PSEUDO_FEATURE_BIBLIOGRAPHY]] = $bibliography_block;
580
    cdm_toc_list_add_item('Bibliography', 'bibliography', null, false, $pseudo_feature_weights[PSEUDO_FEATURE_BIBLIOGRAPHY]);
581
  }
582

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

    
602
      $feature_block_list[$pseudo_feature_weights[PSEUDO_FEATURE_AGGREGATION_DESCRIPTIONS]] = $description_block;
603
      cdm_toc_list_add_item('Descriptions (aggregated)', 'aggregation_descriptions', null, false, $pseudo_feature_weights[PSEUDO_FEATURE_AGGREGATION_DESCRIPTIONS]);
604

    
605

    
606
  }
607

    
608
  // sort by weight
609
  ksort($feature_block_list);
610
  $render_array['taxon_description_features'] = _block_get_renderable_array($feature_block_list);
611

    
612
/*  // update TOC
613
  if ($pseudo_feature_block_toc_items){
614
    foreach ($pseudo_feature_block_toc_items as $label=>$fragment){
615
      cdm_toc_list_add_item($label, $fragment);
616
    }
617
  }
618
*/
619
  // <<<<<<<<<<<<<<<<<<< PSEUDO FEATURES <<<<<<<<<<<<<<<<<<<
620

    
621
  // create the table of content
622
  $toc = array(
623
      '#theme' => 'item_list',
624
    '#items' => cdm_toc_list(),
625
      '#title' => t('Content'),
626
    '#weight' => -100,                  // move to the top
627
      '#suffix' => '</div>',
628
      '#prefix'=> '<div id="page-toc">'
629
  );
630
  $render_array['taxon_description_feature_toc'] = $toc;
631

    
632
  return $render_array;
633
}
634

    
635
/**
636
 * @return object|\stdclass
637
 */
638
function make_bibliography_feature_block() {
639
  $bibliography_block = null;
640
  $bibliography_settings = get_bibliography_settings();
641
  if ($bibliography_settings['enabled'] == 1) {
642
    $bibliography_markup = FootnoteManager::renderFootnoteList(PSEUDO_FEATURE_BIBLIOGRAPHY, '');
643
    if ($bibliography_markup) {
644
      $feature_bibliography = make_pseudo_feature('Bibliography', PSEUDO_FEATURE_BIBLIOGRAPHY);
645
      $bibliography_item = markup_to_render_array($bibliography_markup);
646
      $bibliography_block = feature_block(t('Bibliography'), $feature_bibliography);
647
      $bibliography_block->content = [];
648
      $bibliography_block->content[] = compose_feature_block_wrap_elements([$bibliography_item], $feature_bibliography);
649
    }
650
  }
651
  return $bibliography_block;
652
}
653

    
654

    
655
/**
656
 * Renders the link which will lead to the specimen detail page
657
 * @param object $specimen
658
 *    the cdm specimen entity which will be linked to
659
 *
660
 * @return string
661
 *     the markup for the link
662
 */
663
function render_cdm_specimen_link($specimen) {
664
  $path = path_to_specimen($specimen->uuid);
665
  $attributes['class'][] = html_class_attribute_ref($specimen);
666
  return $specimen->titleCache.icon_link($path);
667
}
668

    
669
/**
670
 * Returns HTML containing the synonymy for the accepted taxon.
671
 *
672
 * Shows the whole synonymy for the accepted taxon.
673
 * The synonymy list is headed by the complete scientific name
674
 * of the accepted taxon with nomenclatural reference.
675
 *
676
 * @param object $taxon
677
 * @param boolean $add_accepted_taxon
678
 *
679
 * @return array
680
 *  Drupal render array for the synonymy
681
 *
682
 * @throws Exception
683
 *
684
 * @ingroup compose
685
 */
686
function compose_cdm_taxon_page_synonymy($taxon, $add_accepted_taxon) {
687

    
688
  RenderHints::pushToRenderStack('taxon_page_synonymy');
689

    
690
  // footnote key for the homotypic group and accepted taxon,
691
  // both should have the same footnote key
692
  RenderHints::setFootnoteListKey(RenderHints::getRenderPath());
693

    
694
  $synomymie = cdm_ws_get(CDM_WS_PORTAL_TAXON_SYNONYMY, array($taxon->uuid));
695

    
696
  $out = '';
697

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

    
717
    $accepted_name .= '<div class="accepted-name">';
718
    $accepted_name .= render_taxon_or_name($taxon, NULL, $referenceUri);
719

    
720
    $name_relations = cdm_name_relationships_for_taxon($taxon);
721
    $name_relationships = compose_name_relationships_inline($name_relations, $taxon->name->uuid, $taxon->uuid);
722
    // Render relationships of accepted name.
723
    if(isset($name_relationships['list']['items'][0])){
724
      $accepted_name .= ' ' . drupal_render($name_relationships);
725
    }
726
    $accepted_name .= '</div>';
727
    RenderHints::popFromRenderStack();
728
  }
729

    
730
  // --- Render homotypic synonymy group
731
  if (!empty($accepted_name)) {
732
    $out .= $accepted_name;
733
  }
734

    
735
  // Render the homotypicSynonymyGroup including the type information.
736
  $out .= theme(
737
    'cdm_homotypicSynonymyGroup',
738
    array(
739
      'synonymList' => $synomymie->homotypicSynonymsByHomotypicGroup,
740
      'accepted_taxon_name_uuid' => $taxon->name->uuid
741
    )
742
  );
743

    
744

    
745
  // Render accepted taxon heterotypic synonymy groups.
746
  if ($synomymie->heterotypicSynonymyGroups) {
747
    foreach ($synomymie->heterotypicSynonymyGroups as $homotypicalGroup) {
748
      $out .= theme('cdm_heterotypicSynonymyGroup', array('homotypicalGroup' => $homotypicalGroup));
749
    }
750
  }
751
  // Render taxon relationships.
752
  if (variable_get(CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS, CDM_DATAPORTAL_DISPLAY_TAXON_RELATIONSHIPS_DEFAULT)) {
753
    $taxonRelationshipsDTO = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS_DTO, $taxon->uuid);
754
    $out .= cdm_taxonRelationships($taxonRelationshipsDTO, $taxon);
755
  }
756

    
757
  RenderHints::popFromRenderStack();
758

    
759
  return markup_to_render_array($out);
760
}
761

    
762

    
763
/**
764
 * composes and returns an render array for the experts associated with the given taxon
765
 *
766
 * @param object taxon
767
 *
768
 * @return array
769
 *   A Drupal render array for a table with the experts
770
 *
771
 * @ingroup compose
772
 */
773
function compose_cdm_taxon_page_experts($taxon){
774

    
775
  $render_array = array();
776
  if(!isset($taxon->uuid)){
777
    return $render_array;
778
  }
779

    
780
  $current_classification_uuid = get_current_classification_uuid();
781
  // TODO use cdm_ws_fetchall below but this failes! needs fix!
782
  $taxon_node_agent_relations = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
783
    array(
784
      $taxon->uuid,
785
      $current_classification_uuid
786
    )
787
  );
788

    
789
  $header = array(
790
    array('data' => t('Expert')),
791
    array('data' => t('Role'))
792
  );
793
  $rows = array();
794

    
795

    
796
  foreach($taxon_node_agent_relations->records as $taxon_node_agent_relation){
797

    
798

    
799
    $expert_role_id = $taxon_node_agent_relation->agent->uuid . '-' . $taxon_node_agent_relation->type->uuid;
800
    $expert_details_container_id = 'expert_details_' . $expert_role_id;
801

    
802
    $agent_label_markup = cdm_dynabox(
803
      'expert_' . $expert_role_id,
804
      $taxon_node_agent_relation->agent->titleCache,
805
      // specifying both ends of the relationship will return only one record in the pager
806
      cdm_compose_ws_url(CDM_WS_PORTAL_AGENT,
807
         array($taxon_node_agent_relation->agent->uuid, 'taxonNodeAgentRelations'),
808
        'taxon_uuid=' . $taxon->uuid . '&relType_uuid=' . $taxon_node_agent_relation->type->uuid),
809
      'cdm_taxon_expert',
810
      'Click for details',
811
      array('div', 'div'),
812
      array(), // attributes
813
      '#' . $expert_details_container_id // $content_element_selector
814
    );
815

    
816
    // Expert and Role
817
    $rows[] = array(
818
      'data' => array(
819
        array(
820
          'data' => $agent_label_markup,
821
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->agent))
822
        ),
823
        array(
824
          'data' => $taxon_node_agent_relation->type->representation_L10n,
825
          'class' => array(html_class_attribute_ref($taxon_node_agent_relation->type))
826
        )
827
      )
828
    );
829
    // Agent details
830
    $rows[] = array(
831
      'data' => array(
832
        array(
833
          'data' => '<!-- expert_details_container -->',
834
          'id' => $expert_details_container_id,
835
          'colspan' => 2
836
        )
837
      )
838
    );
839

    
840
  }
841

    
842

    
843
  $render_array['experts_table'] = array(
844
    '#theme' => 'table',
845
    '#header' => $header,
846
    '#rows' => $rows,
847
  );
848

    
849

    
850
  return $render_array;
851
}
852

    
853

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

    
876
  if(!isset($tabs)){
877
    $tabs = array();
878
  }
879

    
880
  if (isset($add_tab) && !array_key_exists($add_tab, $tabs)) {
881
    $tabs[] = $add_tab;
882
  }
883

    
884
  return $tabs;
885
}
886

    
887
/**
888
 * Manages the tabs to be hidden in the taxon page.
889
 *
890
 * The tabs names are identified by their last menu link path element:
891
 *  - description
892
 *  - synonymy
893
 *  - images
894
 *  - specimens
895
 *  - key
896
 *
897
 * Internally the tabs are stored in a static variable which is
898
 * managed by drupal_static().
899
 *
900
 * @param string $tabname
901
 *   The name of the tab to check
902
 *
903
 * @return boolean
904
 *   True if the tab or section is to be hidden
905
 */
906
function taxon_page_tabs_hidden_check($tabname) {
907

    
908
  $tabs = &drupal_static('taxon_page_tabs_hidden');
909

    
910
  if(!isset($tabs)){
911
    $tabs = array();
912
  }
913

    
914
  return array_search($tabname, $tabs) !== FALSE;
915
}
916

    
917
/**
918
 * Implements the hook_preprocess_HOOK() for theme_menu_local_tasks()
919
 *
920
 *  - Removes the tabs to be hidden, @see taxon_page_tabs_hidden_add()
921
 *  - Renames tabs according to the settings // TODO (this will replace the theme_cdm_taxonpage_tab() function !!!)
922
 *
923
 * @param array $variables
924
 *   The variables array
925
 */
926
function cdm_dataportal_preprocess_menu_local_tasks(&$variables) {
927

    
928
  $hidden_tabs = taxon_page_tabs_hidden_add();
929

    
930
  if (!variable_get(CDM_SEARCH_BLAST_ENABLED)) {
931
      if (is_array($variables['primary'])) {
932
          foreach ($variables['primary'] as $key => &$element) {
933
              if ($element['#link']['path'] == 'cdm_dataportal/search/blast') {
934
                  // remove the tab
935
                  unset($variables['primary'][$key]);
936
              }
937

    
938
          }
939
      }
940
  }
941
  if (is_array($variables['primary'])) {
942
    foreach ($variables['primary'] as $key => &$element) {
943

    
944
      // 1. Remove the tabs to be hidden
945
      foreach ($hidden_tabs as $tab) {
946
        if ($element['#link']['path'] == 'cdm_dataportal/taxon/%/' . $tab) {
947
          // remove the tab
948
          unset($variables['primary'][$key]);
949
        }
950
      }
951
    }
952
  }
953
}
954

    
955

    
956

    
957
/**
958
 * Implements the hook_preprocess_HOOK() for theme_menu_local_task()
959
 *
960
 *
961
 * @param array $variables
962
 *   An associative array containing:
963
 *     - element: A render element containing:
964
 *          #link: A menu link array with 'title', 'href', and 'localized_options' keys.
965
 *          #active: A boolean indicating whether the local task is active.
966
 *
967
 */
968
function cdm_dataportal_preprocess_menu_local_task(&$variables) {
969

    
970
  $link = $variables['element']['#link'];
971
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
972
    $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']) . '"/>';
973
    $link['localized_options']['html'] = TRUE;
974

    
975
    $variables['element']['#link'] = $link;
976
  }
977
}
978

    
979
/* =================== block composition ===================== */
980

    
981
/**
982
 * Composes and returns an render array for the classification breadcrumbs of the given taxon.
983
 *
984
 * @param $taxon_uuid
985
 *
986
 * @return array
987
 *   A Drupal render array for a table with the experts
988
 *
989
 * @ingroup compose
990
 */
991
function compose_classification_breadcrumbs($taxon_uuid) {
992

    
993
  _add_js_taxonomic_children('#classification-breadcrumbs .taxonomic-children-button');
994

    
995
  $render_array = array();
996

    
997
  $render_array['#theme'] = 'item_list';
998
  $render_array['#type'] = 'ul';
999
  $render_array['#attributes'] = array(
1000
    'id' => 'classification-breadcrumbs',
1001
    'class' => 'breadcrumbs inline',
1002
  );
1003

    
1004
  $items = array();
1005

    
1006
  $parent_taxon_nodes = null;
1007
  if($taxon_uuid){
1008
    $parent_taxon_nodes = cdm_ws_taxonomy_pathFromRoot($taxon_uuid);
1009
  }
1010

    
1011
  $classifications = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXONOMY);
1012
  // find current classification in list
1013
  $classification = null;
1014
  $current_classification_uuid = get_current_classification_uuid();
1015
  foreach ($classifications as $classification){
1016
    if($classification->uuid == $current_classification_uuid){
1017
      break;
1018
    }
1019
  }
1020

    
1021
  $node_name = '';
1022
  if(count($classifications) > 1 ){
1023
    // need to add the current classification as first label
1024

    
1025
    $label = $classification->titleCache;
1026
    if(strlen($label) > 20){
1027
      $label = substr($label, 0, strpos($label, ' ', 15)) . '...';
1028
    }
1029
    $node_name = font_awesome_icon_markup('fa-th-list')  . ' ' . l($label, '#', array(
1030
      'attributes' => array(
1031
        'class' => 'taxonomic-children-button classification-chooser',
1032
        'data-destination-uri' => drupal_get_destination(),
1033
        'data-cdm-align-with' => array('prev')
1034
      ),
1035
      'html' => true
1036
    ));
1037
  }
1038

    
1039
  $rank_limit_uuid = variable_get(TAXONTREE_RANKLIMIT, TAXONTREE_RANKLIMIT_DEFAULT);
1040

    
1041
  $rank_separator = '<span> '
1042
    . font_awesome_icon_markup('fa-' . SYMBOL_COLLAPSIBLE_CLOSED)
1043
    . ' </span>';
1044
  $more_children_icon = font_awesome_icon_markup('fa-sitemap fa-rotate-270');
1045
  $more_children_label = '...';
1046

    
1047
  $items[] = $node_name;
1048

    
1049
  $more_children_for = null;
1050
  if($parent_taxon_nodes){
1051
    foreach ($parent_taxon_nodes as $node) {
1052

    
1053
      $is_first_item = count($items) == 0;
1054
      $is_last_item = count($items) == count($parent_taxon_nodes);
1055
      $node_name = cdm_dataportal_shortname_of($node);
1056
      $path = path_to_taxon($node->taxonUuid);
1057

    
1058
      if($node->taxonomicChildrenCount > 0) {
1059
        $more_children_for = $node->taxonUuid;
1060
      } else {
1061
        $more_children_for = null;
1062
      }
1063

    
1064
      // 'fa-sitemap'
1065

    
1066
      $items[] =
1067
        ($is_first_item ? '' : ' ')
1068
        . $rank_separator
1069
        . l(
1070
          '<span class="' . html_class_attribute_ref($node) . '">' . $node_name . '</span>',
1071
          $path,
1072
          array(
1073
            'attributes' => array(
1074
              'class' => array('taxonomic-children-button'),
1075
              'data-cdm-taxon-uuid' => array($node->taxonUuid),
1076
              'data-cdm-classification-mode' => array('siblings'),
1077
              'data-cdm-align-with' => array('prev')
1078
            ),
1079
            'html' => true
1080
          )
1081
        );
1082
      }
1083
    }
1084

    
1085
  // add more button to the end
1086
  if(!$parent_taxon_nodes) {
1087
    // not taxon focused yet, adding button to make  the root nodes available
1088
    $items[] = '<span>'
1089
      . $more_children_icon . '&nbsp;' .
1090
      '<span class="taxonomic-children-button" data-classification-uuid="' . $current_classification_uuid
1091
      . '" data-rank-limit-uuid="' . $rank_limit_uuid . '" data-cdm-align-with="prev"> ' . $more_children_label . '<span>'
1092
      . '</span>';
1093
  } else if($more_children_for){
1094
    // last parent item has child taxon nodes
1095
    $items[] = ' <span>'
1096
      . $more_children_icon . '&nbsp;' .
1097
      '<span class="taxonomic-children-button" data-cdm-taxon-uuid="' .$more_children_for
1098
      . '" data-cdm-classification-mode="children" data-cdm-align-with="prev"> ' . $more_children_label . '</span>'
1099
      . '</span>';
1100

    
1101
  }
1102

    
1103
  $render_array['#items'] = $items;
1104

    
1105
  return $render_array;
1106
}
1107

    
1108

    
1109
/**
1110
 *
1111
 * @param $sob_dto
1112
 *   A SpecimenOrObservationDTO
1113
 *
1114
 * @return array
1115
 *    The drupal render array for the page
1116
 *
1117
 * @ingroup compose
1118
 */
1119
function compose_cdm_specimen_page($sob_dto)
1120
{
1121
  drupal_set_title($sob_dto->label);
1122
  RenderHints::pushToRenderStack('specimen_page');
1123
  $render_array = compose_cdm_specimen_or_observation_new($sob_dto);
1124
  RenderHints::popFromRenderStack();
1125
  return $render_array;
1126
}
1127

    
1128
/**
1129
 * @param $named_area_uuid
1130
 * @return array
1131
 *    The drupal render array for the page
1132
 *
1133
 * @ingroup compose
1134
 */
1135
function compose_cdm_named_area_page($named_area_uuid)
1136
{
1137

    
1138
  $named_area = cdm_ws_get(CDM_WS_PORTAL_TERM, array($named_area_uuid));
1139

    
1140
  $render_array = array();
1141
  RenderHints::pushToRenderStack('named_area_page');
1142

    
1143
  $groups = array();
1144
  @_description_list_group_add($groups, t('Name') . ':', $named_area->representation_L10n);
1145
  @_description_list_group_add($groups, t('IdInVocabulary') . ':', $named_area->idInVocabulary);
1146
  if(isset($named_area->level)) {
1147
    @_description_list_group_add($groups, t('Level') . ':', $named_area->level->representation_L10n);
1148
  }
1149

    
1150
  $name_area_details_elements = array(
1151
   // '#title' => $title,
1152
    '#theme' => 'description_list',
1153
    '#groups' => $groups,
1154
    '#attributes' => array('class' => html_class_attribute_ref($named_area)),
1155
  );
1156

    
1157
  $render_array[] = $name_area_details_elements;
1158

    
1159
  RenderHints::popFromRenderStack();
1160
  return $render_array;
1161
}
1162

    
1163

    
1164
/**
1165
 * Returns a drupal render array for a single reference page.
1166
 *
1167
 * Composes a page with all data on a single reference.
1168
 *
1169
 * @param string $uuid
1170
 *   An uuid for a cdm reference.
1171
 *
1172
 * @return array
1173
 *  A drupal render array
1174
 *
1175
 * @throws Exception
1176
 *
1177
 * @ingroup compose
1178
 */
1179
function compose_cdm_reference_page($uuid) {
1180

    
1181
  $pathelement = "reference_page";
1182
  RenderHints::pushToRenderStack($pathelement);
1183
  $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
1184
  if (!$reference) {
1185
    drupal_set_title(t('Reference does not exist'), PASS_THROUGH);
1186
    return "";
1187
  }
1188
  if (isset($reference->titleCache)) {
1189
    drupal_set_title($reference->titleCache, PASS_THROUGH);
1190
  }
1191

    
1192
  $field_order = array(
1193
    "title",
1194
    "abbrevTitle",
1195
    // "titleCache" abbrevTitleCache
1196
    // "citation",
1197
    "authorship",
1198
    "editor",
1199
    "publisher",
1200
    "placePublished",
1201
    "datePublished",
1202
    "year",
1203
    "edition",// Class Book.
1204
    "volume",// Class Article.
1205
    "seriesPart",
1206
    "inReference",
1207
    "nomRefBase", // Class BookSection, Book, Article.
1208
    "pages",// Class Article.
1209
    "series",// Class Article, PrintSeries.
1210
    "school",// Class Thesis.
1211
    "institution",// Class Report.
1212
    "organization",// Class Proceedings.
1213
    "nextVersion",
1214
    "previousVersion",
1215
    "isbn",// Class Book.
1216
    "issn",// Class Journal.
1217
    "doi",
1218
    "uri"
1219
  );
1220

    
1221
  $table_rows = array();
1222

    
1223
  if (!isset($reference->authorship)) {
1224
    $authorship = cdm_ws_get(CDM_WS_REFERENCE_AUTHORTEAM, $reference->uuid);
1225
    $reference->authorship = isset($authorship->titleCache) ? $authorship->titleCache : '';
1226
  }
1227

    
1228
  if (!isset($reference->inReference)) {
1229
    $reference->inReference = cdm_ws_get(CDM_WS_REFERENCE, array(
1230
      $reference->uuid,
1231
      "inReference",
1232
    ));
1233
  }
1234

    
1235
  foreach ($field_order as $fieldname) {
1236

    
1237
    if (isset($reference->$fieldname)) {
1238

    
1239
      if ($fieldname == "datePublished") {
1240
        $period = $reference->$fieldname;
1241
        $datePublished = timePeriodToString($period);
1242
        if (isset($datePublished) && $datePublished != '') {
1243
          $table_rows[] = array(
1244
            t("Date published"),
1245
            $datePublished,
1246
          );
1247
        }
1248
      }
1249
      elseif ($fieldname == "doi" && is_object($reference->doi)) {
1250
        $table_rows[] = array(
1251
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1252
          cdm_doi($reference->doi, false)
1253
        );
1254
      }
1255
      elseif ($fieldname == "uri" && isset($reference->uri) && $reference->uri) {
1256
        $table_rows[] = array(
1257
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1258
          cdm_external_uri($reference->uri, false)
1259
        );
1260
      }
1261
      elseif (is_object($reference->$fieldname)) {
1262
        if ($fieldname == "authorship") {
1263
          $dump = $reference->$fieldname;
1264
          $teammembers = "teamMembers";
1265
          $team = $dump->$teammembers;
1266
          $nameArray = array();
1267

    
1268
          foreach ($team as $member) {
1269
            if (strlen($member->lastname) > 0) {
1270
              $nname = $member->lastname;
1271
              $name = $nname;
1272
              if (strlen($member->firstname) > 0) {
1273
                $vname = $member->firstname;
1274
                $name = $vname . " " . $nname;
1275
              }
1276
              $nameArray[] = $name;
1277
            }
1278
            else {
1279
              if (strlen($member->titleCache) > 0) {
1280
                $nameArray[] = $member->titleCache;
1281
              }
1282
            }
1283
          }
1284
          $value = join($nameArray, ", ");
1285
        }
1286
        elseif ($fieldname == "inReference") {
1287
          $type = $reference->$fieldname->type;
1288
          $value = l($reference->$fieldname->titleCache, path_to_reference($reference->$fieldname->uuid));
1289
          switch ($type) {
1290
            case "Book":
1291
              $fieldname = "in book";
1292
              break;
1293
            case "Journal":
1294
              $fieldname = "in journal";
1295
              break;
1296
            case "Proceedings":
1297
              $fieldname = "in proceedings";
1298
              break;
1299
          }
1300
        }
1301
        else {
1302
          $value = $reference->$fieldname->titleCache;
1303
        }
1304

    
1305

    
1306
        if (isset($value) && $value != '') {
1307
          $table_rows[] = array(
1308
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1309
            $value,
1310
          );
1311
        }
1312

    
1313
      }
1314
      else {
1315
        if (isset($reference->$fieldname) && $reference->$fieldname != '') {
1316
          $table_rows[] = array(
1317
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1318
            $reference->$fieldname,
1319
          );
1320
        }
1321
      }
1322
    }
1323
  }
1324

    
1325
  $out = theme_table(array(
1326
    'header' => array(),
1327
    'rows' => $table_rows,
1328
    'attributes' => array(
1329
      'class' => html_class_attribute_ref($reference)
1330
    ),
1331
    'caption' => NULL,
1332
    'colgroups' => NULL,
1333
    'sticky' => NULL,
1334
    'empty' => NULL,
1335
  ));
1336

    
1337
  if(isset($reference->referenceAbstract)){
1338
    $out .= '<h2 class="block-title">Abstract</h2><div class="abstract">' . $reference->referenceAbstract . '</div>';
1339
  }
1340

    
1341

    
1342
  // Annotations below the table
1343
  $annotations = cdm_fetch_visible_annotations($reference);
1344
  $out .= cdm_annotations($annotations);
1345

    
1346
  $registration_working_set = cdm_ws_get("registrationWorkingSetDTO", array($uuid));
1347
  if($registration_working_set && count($registration_working_set->registrationDTOs) > 0){
1348
    $out .= "<h3>Nomenclatural acts:</h3><div class=\"cdm-item-list registration-item-list\">";
1349
    foreach($registration_working_set->registrationDTOs as $registration_dto){
1350
      if($registration_dto->status == "PUBLISHED"){
1351
        $registration_render_a = compose_registration_dto_compact($registration_dto, 'citation');
1352
        $registration_render_a["#prefix"] = "<div class=\"item item-registration\">";
1353
        $registration_render_a["#suffix"] = "</div>";
1354
        $out .= drupal_render($registration_render_a);
1355
      }
1356
    }
1357
    $out .= "</div>";
1358
  }
1359

    
1360
  RenderHints::popFromRenderStack();
1361

    
1362
  return markup_to_render_array($out);
1363
}
1364

    
1365
/**
1366
 * Provides the the label string for taxon page tabs.
1367
 *
1368
 * The $tabname as passed to the method will be returned if no override
1369
 * label is configured in the settings.
1370
 *
1371
 * @param $tabname
1372
 *
1373
 * @return mixed
1374
 */
1375
function cdm_taxonpage_tab_label($tabname) {
1376
  static $taxon_tabs_labels = null;
1377
  if($taxon_tabs_labels == null){
1378
    $taxon_tabs_labels = get_array_variable_merged(CDM_TAXONPAGE_TAB_LABELS, CDM_TAXONPAGE_TAB_LABELS_DEFAULT);
1379
  }
1380
  $tabname_key = strtolower($tabname);
1381
  if(isset($taxon_tabs_labels[$tabname_key]) && $taxon_tabs_labels[$tabname_key]){
1382
    return $taxon_tabs_labels[$tabname_key];
1383
  }
1384
  return $tabname;
1385
}
1386

    
1387
/**
1388
 *  Returns HTML for the default title of a specimen page.
1389
 *  The returned title is a the identifier of the specimen.
1390
 *
1391
 * @param object $sob_dto
1392
 *  A SpecimenOrObservationDTO to create a tile string for.
1393
 *
1394
 * @return string
1395
 *  Markup for the title of a specimen page
1396
 */
1397
function render_specimen_page_title($sob_dto)
1398
{
1399
  RenderHints::pushToRenderStack('render_specimen_page_title');
1400
  $out = '';
1401
  if ($sob_dto->type == 'FieldUnit'){
1402
    $out .= '<span class="type-label">Field unit: </span>';
1403
  }else{
1404
    $out .= '<span class="type-label">Specimen:  </span>';
1405
    $out .= $sob_dto->specimenShortTitle;
1406
  }
1407
  RenderHints::popFromRenderStack();
1408

    
1409
  return '<span class="' . html_class_attribute_ref($sob_dto) . '">' . $out . '</span>';
1410
}
1411

    
1412
/**
1413
 * Returns HTML for the default title of a specimen page.
1414
 * The returned title is a the identifier of the specimen.
1415
 *
1416
 * @param object $occurrence_dto
1417
 *   An dto for a cdm SpecimenOrObservation entity
1418
 *
1419
 * @return string
1420
 *  Markup for the title of a specimen page
1421
 *
1422
 */
1423
function render_cdm_specimen_dto_page_title($occurrence_dto)
1424
{
1425
  RenderHints::pushToRenderStack('render_specimen_page_title');
1426
  $out = '';
1427
  list($specimenID, $collection) = specimen_id_and_collection_for($occurrence_dto);
1428
  if ($occurrence_dto->class == 'FieldUnit'){
1429
    $out .= "FieldUnit ";
1430
  }else{
1431
    $out .= "Specimen ";
1432
  }
1433
  if($collection){
1434
    $out .= $collection." ";
1435
  }
1436
  $out .= $specimenID;
1437

    
1438
  RenderHints::popFromRenderStack();
1439
  return '<span class="' . $occurrence_dto->class . '">' . $out . '</span>';
1440
}
1441

    
(10-10/16)