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_gallerie_image($representationPart, $taxon_profile_image_settings['maxextend'], FALSE, $attributes);
548
      // NOTE: style="width:${maxextend}px' is needed for IE8 !!!
549
      $max_extend_with = $taxon_profile_image_settings['maxextend'] . 'px';
550
      $render_array['taxon_profile_image'] = markup_to_render_array('<div id="taxonProfileImage" style="width:' . $max_extend_with . '">' . $profile_image . '</div>',
551
        -101);
552
    }
553
  }
554

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

    
563
  //  $pseudo_feature_block_toc_items = array();
564

    
565
  // Render the sections for each real feature
566
  $feature_block_list = make_feature_block_list($merged_tree->root->childNodes, $taxon);
567

    
568
  // >>>>>>>>>>>>>>>>>>> PSEUDO FEATURES >>>>>>>>>>>>>>>>>>>
569
  $pseudo_feature_weights = get_array_variable_merged(CDM_PSEUDO_FEATURE_BLOCK_WEIGHTS, CDM_PSEUDO_FEATURE_BLOCK_WEIGHTS_DEFAULT);
570

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

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

    
597
      $feature_block_list[$pseudo_feature_weights[PSEUDO_FEATURE_AGGREGATION_DESCRIPTIONS]] = $description_block;
598
      cdm_toc_list_add_item('Descriptions (aggregated)', 'aggregation_descriptions', null, false, $pseudo_feature_weights[PSEUDO_FEATURE_AGGREGATION_DESCRIPTIONS]);
599

    
600

    
601
  }
602

    
603
  // sort by weight
604
  ksort($feature_block_list);
605
  $render_array['taxon_description_features'] = _block_get_renderable_array($feature_block_list);
606

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

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

    
627
  return $render_array;
628
}
629

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

    
649

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

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

    
683
  RenderHints::pushToRenderStack('taxon_page_synonymy');
684

    
685
  // footnote key for the homotypic group and accepted taxon,
686
  // both should have the same footnote key
687
  RenderHints::setFootnoteListKey(RenderHints::getRenderPath());
688

    
689
  $synomymie = cdm_ws_get(CDM_WS_PORTAL_TAXON_SYNONYMY, array($taxon->uuid));
690

    
691
  $out = '';
692

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

    
712
    $accepted_name .= '<div class="accepted-name">';
713
    $accepted_name .= render_taxon_or_name($taxon, NULL, $referenceUri);
714

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

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

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

    
739

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

    
752
  RenderHints::popFromRenderStack();
753

    
754
  return markup_to_render_array($out);
755
}
756

    
757

    
758
/**
759
 * composes and returns an render array for the experts associated with the given taxon
760
 *
761
 * @param object taxon
762
 *
763
 * @return array
764
 *   A Drupal render array for a table with the experts
765
 *
766
 * @ingroup compose
767
 */
768
function compose_cdm_taxon_page_experts($taxon){
769

    
770
  $render_array = array();
771
  if(!isset($taxon->uuid)){
772
    return $render_array;
773
  }
774

    
775
  $current_classification_uuid = get_current_classification_uuid();
776
  // TODO use cdm_ws_fetchall below but this failes! needs fix!
777
  $taxon_node_agent_relations = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODEAGENTRELATIONS,
778
    array(
779
      $taxon->uuid,
780
      $current_classification_uuid
781
    )
782
  );
783

    
784
  $header = array(
785
    array('data' => t('Expert')),
786
    array('data' => t('Role'))
787
  );
788
  $rows = array();
789

    
790

    
791
  foreach($taxon_node_agent_relations->records as $taxon_node_agent_relation){
792

    
793

    
794
    $expert_role_id = $taxon_node_agent_relation->agent->uuid . '-' . $taxon_node_agent_relation->type->uuid;
795
    $expert_details_container_id = 'expert_details_' . $expert_role_id;
796

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

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

    
835
  }
836

    
837

    
838
  $render_array['experts_table'] = array(
839
    '#theme' => 'table',
840
    '#header' => $header,
841
    '#rows' => $rows,
842
  );
843

    
844

    
845
  return $render_array;
846
}
847

    
848

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

    
871
  if(!isset($tabs)){
872
    $tabs = array();
873
  }
874

    
875
  if (isset($add_tab) && !array_key_exists($add_tab, $tabs)) {
876
    $tabs[] = $add_tab;
877
  }
878

    
879
  return $tabs;
880
}
881

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

    
903
  $tabs = &drupal_static('taxon_page_tabs_hidden');
904

    
905
  if(!isset($tabs)){
906
    $tabs = array();
907
  }
908

    
909
  return array_search($tabname, $tabs) !== FALSE;
910
}
911

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

    
923
  $hidden_tabs = taxon_page_tabs_hidden_add();
924

    
925
  if (!variable_get(CDM_SEARCH_BLAST_ENABLED)) {
926
      if (is_array($variables['primary'])) {
927
          foreach ($variables['primary'] as $key => &$element) {
928
              if ($element['#link']['path'] == 'cdm_dataportal/search/blast') {
929
                  // remove the tab
930
                  unset($variables['primary'][$key]);
931
              }
932

    
933
          }
934
      }
935
  }
936
  if (is_array($variables['primary'])) {
937
    foreach ($variables['primary'] as $key => &$element) {
938

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

    
950

    
951

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

    
965
  $link = $variables['element']['#link'];
966
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
967
    $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']) . '"/>';
968
    $link['localized_options']['html'] = TRUE;
969

    
970
    $variables['element']['#link'] = $link;
971
  }
972
}
973

    
974
/* =================== block composition ===================== */
975

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

    
988
  _add_js_taxonomic_children('#classification-breadcrumbs .taxonomic-children-button');
989

    
990
  $render_array = array();
991

    
992
  $render_array['#theme'] = 'item_list';
993
  $render_array['#type'] = 'ul';
994
  $render_array['#attributes'] = array(
995
    'id' => 'classification-breadcrumbs',
996
    'class' => 'breadcrumbs inline',
997
  );
998

    
999
  $items = array();
1000

    
1001
  $parent_taxon_nodes = null;
1002
  if($taxon_uuid){
1003
    $parent_taxon_nodes = cdm_ws_taxonomy_pathFromRoot($taxon_uuid);
1004
  }
1005

    
1006
  $classifications = cdm_ws_fetch_all(CDM_WS_PORTAL_TAXONOMY);
1007
  // find current classification in list
1008
  $classification = null;
1009
  $current_classification_uuid = get_current_classification_uuid();
1010
  foreach ($classifications as $classification){
1011
    if($classification->uuid == $current_classification_uuid){
1012
      break;
1013
    }
1014
  }
1015

    
1016
  $node_name = '';
1017
  if(count($classifications) > 1 ){
1018
    // need to add the current classification as first label
1019

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

    
1034
  $rank_limit_uuid = variable_get(TAXONTREE_RANKLIMIT, TAXONTREE_RANKLIMIT_DEFAULT);
1035

    
1036
  $rank_separator = '<span> '
1037
    . font_awesome_icon_markup('fa-' . SYMBOL_COLLAPSIBLE_CLOSED)
1038
    . ' </span>';
1039
  $more_children_icon = font_awesome_icon_markup('fa-sitemap fa-rotate-270');
1040
  $more_children_label = '...';
1041

    
1042
  $items[] = $node_name;
1043

    
1044
  $more_children_for = null;
1045
  if($parent_taxon_nodes){
1046
    foreach ($parent_taxon_nodes as $node) {
1047

    
1048
      $is_first_item = count($items) == 0;
1049
      $is_last_item = count($items) == count($parent_taxon_nodes);
1050
      $node_name = cdm_dataportal_shortname_of($node);
1051
      $path = path_to_taxon($node->taxonUuid);
1052

    
1053
      if($node->taxonomicChildrenCount > 0) {
1054
        $more_children_for = $node->taxonUuid;
1055
      } else {
1056
        $more_children_for = null;
1057
      }
1058

    
1059
      // 'fa-sitemap'
1060

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

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

    
1096
  }
1097

    
1098
  $render_array['#items'] = $items;
1099

    
1100
  return $render_array;
1101
}
1102

    
1103

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

    
1123
/**
1124
 * @param $named_area_uuid
1125
 * @return array
1126
 *    The drupal render array for the page
1127
 *
1128
 * @ingroup compose
1129
 */
1130
function compose_cdm_named_area_page($named_area_uuid)
1131
{
1132

    
1133
  $named_area = cdm_ws_get(CDM_WS_PORTAL_TERM, array($named_area_uuid));
1134

    
1135
  $render_array = array();
1136
  RenderHints::pushToRenderStack('named_area_page');
1137

    
1138
  $groups = array();
1139
  @_description_list_group_add($groups, t('Name') . ':', $named_area->representation_L10n);
1140
  @_description_list_group_add($groups, t('IdInVocabulary') . ':', $named_area->idInVocabulary);
1141
  if(isset($named_area->level)) {
1142
    @_description_list_group_add($groups, t('Level') . ':', $named_area->level->representation_L10n);
1143
  }
1144

    
1145
  $name_area_details_elements = array(
1146
   // '#title' => $title,
1147
    '#theme' => 'description_list',
1148
    '#groups' => $groups,
1149
    '#attributes' => array('class' => html_class_attribute_ref($named_area)),
1150
  );
1151

    
1152
  $render_array[] = $name_area_details_elements;
1153

    
1154
  RenderHints::popFromRenderStack();
1155
  return $render_array;
1156
}
1157

    
1158

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

    
1176
  $pathelement = "reference_page";
1177
  RenderHints::pushToRenderStack($pathelement);
1178
  $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
1179
  if (!$reference) {
1180
    drupal_set_title(t('Reference does not exist'), PASS_THROUGH);
1181
    return "";
1182
  }
1183
  if (isset($reference->titleCache)) {
1184
    drupal_set_title($reference->titleCache, PASS_THROUGH);
1185
  }
1186

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

    
1216
  $table_rows = array();
1217

    
1218
  if (!isset($reference->authorship)) {
1219
    $authorship = cdm_ws_get(CDM_WS_REFERENCE_AUTHORTEAM, $reference->uuid);
1220
    $reference->authorship = isset($authorship->titleCache) ? $authorship->titleCache : '';
1221
  }
1222

    
1223
  if (!isset($reference->inReference)) {
1224
    $reference->inReference = cdm_ws_get(CDM_WS_REFERENCE, array(
1225
      $reference->uuid,
1226
      "inReference",
1227
    ));
1228
  }
1229

    
1230
  foreach ($field_order as $fieldname) {
1231

    
1232
    if (isset($reference->$fieldname)) {
1233

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

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

    
1300

    
1301
        if (isset($value) && $value != '') {
1302
          $table_rows[] = array(
1303
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1304
            $value,
1305
          );
1306
        }
1307

    
1308
      }
1309
      else {
1310
        if (isset($reference->$fieldname) && $reference->$fieldname != '') {
1311
          $table_rows[] = array(
1312
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1313
            $reference->$fieldname,
1314
          );
1315
        }
1316
      }
1317
    }
1318
  }
1319

    
1320
  $out = theme_table(array(
1321
    'header' => array(),
1322
    'rows' => $table_rows,
1323
    'attributes' => array(
1324
      'class' => html_class_attribute_ref($reference)
1325
    ),
1326
    'caption' => NULL,
1327
    'colgroups' => NULL,
1328
    'sticky' => NULL,
1329
    'empty' => NULL,
1330
  ));
1331

    
1332
  if(isset($reference->referenceAbstract)){
1333
    $out .= '<h2 class="block-title">Abstract</h2><div class="abstract">' . $reference->referenceAbstract . '</div>';
1334
  }
1335

    
1336

    
1337
  // Annotations below the table
1338
  $annotations = cdm_fetch_visible_annotations($reference);
1339
  $out .= cdm_annotations($annotations);
1340

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

    
1355
  RenderHints::popFromRenderStack();
1356

    
1357
  return markup_to_render_array($out);
1358
}
1359

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

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

    
1404
  return '<span class="' . html_class_attribute_ref($sob_dto) . '">' . $out . '</span>';
1405
}
1406

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

    
1433
  RenderHints::popFromRenderStack();
1434
  return '<span class="' . $occurrence_dto->class . '">' . $out . '</span>';
1435
}
1436

    
(10-10/16)