Project

General

Profile

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

    
19

    
20
/**
21
 * Composes a render array representing the ocurrences associetad with the $taxon.
22
 *
23
 * The resulting render array contains two elements:
24
 *  - 'map': A map showing all point locations of the occurences is availabale
25
 *  - 'specimen_list': the list of occurences prepated as table for theme_table()
26
 *
27
 * @param object $taxon
28
 *   A cdm Taxon object
29
 * @return
30
 *   A render array suitable for drupal_render()
31
 *
32
 * @ingroup Compose
33
 *
34
 */
35
function compose_cdm_taxon_page_specimens($taxon) {
36

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

    
40
  $fieldUnitDTOs = null;
41
  $specimensOrObservations = 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
      $fieldUnitDTOs = cdm_ws_get(CDM_WS_TAXON_FIELDUNIT_DTOS, array( $taxon->uuid));
46
      $fieldUnitDTOs = order_fieldUnitDtos_by_date_and_type($fieldUnitDTOs);
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_FIELDUNIT_DTO_BY_ASSOCIATEDTAXON,
64
      // null,
65
      // $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
66
      // );
67

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

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

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

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

    
95
        //COMPRESSED SPECIMEN DERIVATE TABLE
96
        $pager_field_units = cdm_ws_page(
97
          cdm_compose_url(CDM_WS_PORTAL_TAXON, [
98
            $taxon->uuid,
99
            'associatedFieldUnits'
100
          ]),
101
          variable_get(CDM_SEARCH_RESULT_PAGE_SIZE, CDM_SEARCH_RESULT_PAGE_SIZE_DEFAULT) * 2,
102
          isset($_REQUEST['pager']['pageNumber']) ? isset($_REQUEST['pager']['pageNumber']) : 0
103
        );
104

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

    
111
          $render_array['derivate_hierarchy_table'] = compose_compressed_specimen_derivate_table($field_unit_uuids);
112
        }
113

    
114
        $render_array['pager'] = markup_to_render_array(
115
            theme('cdm_pager', array(
116
                'pager' => $pager_field_units,
117
                'path' => $_REQUEST['q'],
118
                'parameters' => $_REQUEST
119
            )),
120
            10 // weight
121
        );
122
    }
123
    else if(($current_view_mode == CDM_SPECIMEN_LIST_VIEW_MODE_OPTION_DERIVATE_TREE)){ // FIXME this seems to be wrong
124
      $render_array['specimen_list'] = compose_specimen_table_top_down($fieldUnitDTOs);
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 $linkList the 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
    $render_array['taxon_node_status'] = compose_taxon_node_status($taxon_nodes);
289
    $render_array['taxon_node_status']['#weight'] = -100;
290
  }
291

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

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

    
299
    $merged_tree = merged_taxon_feature_tree($taxon);
300

    
301

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

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

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

    
336
    $synonym_a = compose_cdm_taxon_page_synonymy($taxon, $addAcceptedTaxon);
337
    $synonymy_html .= drupal_render($synonym_a);
338

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

    
342
  }
343

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

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

    
369
  // --- PAGE PART: EXPERTS --- //
370

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

    
379
  // ------------------ END OF PARTS -------------- //
380

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

    
389

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

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

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

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

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

    
444
  }
445

    
446

    
447
  return $render_array;
448
}
449

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

    
477
  $render_array = array();
478

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

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

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

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

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

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

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

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

    
558
  //  $pseudo_feature_block_toc_items = array();
559

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

    
563
  // >>>>>>>>>>>>>>>>>>> PSEUDO FEATURES >>>>>>>>>>>>>>>>>>>
564
  $pseudo_feature_weights = get_array_variable_merged(CDM_PSEUDO_FEATURE_BLOCK_WEIGHTS, CDM_PSEUDO_FEATURE_BLOCK_WEIGHTS_DEFAULT);
565

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

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

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

    
595

    
596
  }
597

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

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

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

    
622
  return $render_array;
623
}
624

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

    
644

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

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

    
678
  RenderHints::pushToRenderStack('taxon_page_synonymy');
679

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

    
684
  $synomymie = cdm_ws_get(CDM_WS_PORTAL_TAXON_SYNONYMY, array($taxon->uuid));
685

    
686
  $out = '';
687

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

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

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

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

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

    
734

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

    
747
  RenderHints::popFromRenderStack();
748

    
749
  return markup_to_render_array($out);
750
}
751

    
752

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

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

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

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

    
785

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

    
788

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

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

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

    
830
  }
831

    
832

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

    
839

    
840
  return $render_array;
841
}
842

    
843

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

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

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

    
874
  return $tabs;
875
}
876

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

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

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

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

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

    
918
  $hidden_tabs = taxon_page_tabs_hidden_add();
919

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

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

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

    
945

    
946

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

    
960
  $link = $variables['element']['#link'];
961
  if (preg_match('/cdm_dataportal\/.*\/refresh$/', $link['href'])) {
962
    $link['title'] = '<img class="refresh" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/view-refresh.png' . '" alt="' . check_plain($link['title']) . '" title="' . check_plain($link['title']) . '"/>';
963
    $link['localized_options']['html'] = TRUE;
964

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

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

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

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

    
985
  $render_array = array();
986

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

    
994
  $items = array();
995

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

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

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

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

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

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

    
1037
  $items[] = $node_name;
1038

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

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

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

    
1054
      // 'fa-sitemap'
1055

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

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

    
1091
  }
1092

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

    
1095
  return $render_array;
1096
}
1097

    
1098

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

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

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

    
1122

    
1123
  );
1124
  $specimen = compose_cdm_specimen_or_observation($specimen_uuid, true);
1125
  /*
1126
  $render_array = array(
1127
          '#theme' => 'item_list',
1128
          '#items' => array($specimen),
1129
          '#type' => 'ul');
1130
  $output = drupal_render($render_array);
1131
 */
1132
  $specimen_table['#rows'][] = array(
1133
          // An array of table rows. Every row is an array of cells, or an associative array
1134
          'data' => $specimen,
1135
          'class' =>  array(
1136
              'descriptionElement',
1137
              'descriptionElement_IndividualsAssociation'
1138
          ),
1139
  );
1140

    
1141

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

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

    
1154
/**
1155
 * @param $named_area_uuid
1156
 * @return array
1157
 *    The drupal render array for the page
1158
 *
1159
 * @ingroup compose
1160
 */
1161
function compose_cdm_named_area_page($named_area_uuid)
1162
{
1163

    
1164
  $named_area = cdm_ws_get(CDM_WS_PORTAL_TERM, array($named_area_uuid));
1165

    
1166
  $render_array = array();
1167
  RenderHints::pushToRenderStack('named_area_page');
1168

    
1169
  $groups = array();
1170
  @_description_list_group_add($groups, t('Name') . ':', $named_area->representation_L10n);
1171
  @_description_list_group_add($groups, t('IdInVocabulary') . ':', $named_area->idInVocabulary);
1172
  if(isset($named_area->level)) {
1173
    @_description_list_group_add($groups, t('Level') . ':', $named_area->level->representation_L10n);
1174
  }
1175

    
1176
  $name_area_details_elements = array(
1177
   // '#title' => $title,
1178
    '#theme' => 'description_list',
1179
    '#groups' => $groups,
1180
    '#attributes' => array('class' => html_class_attribute_ref($named_area)),
1181
  );
1182

    
1183
  $render_array[] = $name_area_details_elements;
1184

    
1185
  RenderHints::popFromRenderStack();
1186
  return $render_array;
1187
}
1188

    
1189

    
1190
/**
1191
 * Returns a drupal render array for a single reference page.
1192
 *
1193
 * Composes a page with all data on a single reference.
1194
 *
1195
 * @param string $uuid
1196
 *   An uuid for a cdm reference.
1197
 *
1198
 * @return array
1199
 *  A drupal render array
1200
 *
1201
 * @throws Exception
1202
 *
1203
 * @ingroup compose
1204
 */
1205
function compose_cdm_reference_page($uuid) {
1206

    
1207
  $pathelement = "reference_page";
1208
  RenderHints::pushToRenderStack($pathelement);
1209
  $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
1210
  if (!$reference) {
1211
    drupal_set_title(t('Reference does not exist'), PASS_THROUGH);
1212
    return "";
1213
  }
1214
  if (isset($reference->titleCache)) {
1215
    drupal_set_title($reference->titleCache, PASS_THROUGH);
1216
  }
1217

    
1218
  $field_order = array(
1219
    "title",
1220
    "abbrevTitle",
1221
    // "titleCache" abbrevTitleCache
1222
    // "citation",
1223
    "authorship",
1224
    "editor",
1225
    "publisher",
1226
    "placePublished",
1227
    "datePublished",
1228
    "year",
1229
    "edition",// Class Book.
1230
    "volume",// Class Article.
1231
    "seriesPart",
1232
    "inReference",
1233
    "nomRefBase", // Class BookSection, Book, Article.
1234
    "pages",// Class Article.
1235
    "series",// Class Article, PrintSeries.
1236
    "school",// Class Thesis.
1237
    "institution",// Class Report.
1238
    "organization",// Class Proceedings.
1239
    "nextVersion",
1240
    "previousVersion",
1241
    "isbn",// Class Book.
1242
    "issn",// Class Journal.
1243
    "doi",
1244
    "uri"
1245
  );
1246

    
1247
  $table_rows = array();
1248

    
1249
  if (!isset($reference->authorship)) {
1250
    $authorship = cdm_ws_get(CDM_WS_REFERENCE_AUTHORTEAM, $reference->uuid);
1251
    $reference->authorship = isset($authorship->titleCache) ? $authorship->titleCache : '';
1252
  }
1253

    
1254
  if (!isset($reference->inReference)) {
1255
    $reference->inReference = cdm_ws_get(CDM_WS_REFERENCE, array(
1256
      $reference->uuid,
1257
      "inReference",
1258
    ));
1259
  }
1260

    
1261
  foreach ($field_order as $fieldname) {
1262

    
1263
    if (isset($reference->$fieldname)) {
1264

    
1265
      if ($fieldname == "datePublished") {
1266
        $period = $reference->$fieldname;
1267
        $datePublished = timePeriodToString($period);
1268
        if (isset($datePublished) && $datePublished != '') {
1269
          $table_rows[] = array(
1270
            t("Date published"),
1271
            $datePublished,
1272
          );
1273
        }
1274
      }
1275
      elseif ($fieldname == "doi" && is_object($reference->doi)) {
1276
        $table_rows[] = array(
1277
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1278
          cdm_doi($reference->doi, false)
1279
        );
1280
      }
1281
      elseif ($fieldname == "uri" && isset($reference->uri) && $reference->uri) {
1282
        $table_rows[] = array(
1283
          t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1284
          cdm_external_uri($reference->uri, false)
1285
        );
1286
      }
1287
      elseif (is_object($reference->$fieldname)) {
1288
        if ($fieldname == "authorship") {
1289
          $dump = $reference->$fieldname;
1290
          $teammembers = "teamMembers";
1291
          $team = $dump->$teammembers;
1292
          $nameArray = array();
1293

    
1294
          foreach ($team as $member) {
1295
            if (strlen($member->lastname) > 0) {
1296
              $nname = $member->lastname;
1297
              $name = $nname;
1298
              if (strlen($member->firstname) > 0) {
1299
                $vname = $member->firstname;
1300
                $name = $vname . " " . $nname;
1301
              }
1302
              $nameArray[] = $name;
1303
            }
1304
            else {
1305
              if (strlen($member->titleCache) > 0) {
1306
                $nameArray[] = $member->titleCache;
1307
              }
1308
            }
1309
          }
1310
          $value = join($nameArray, ", ");
1311
        }
1312
        elseif ($fieldname == "inReference") {
1313
          $type = $reference->$fieldname->type;
1314
          $value = l($reference->$fieldname->titleCache, path_to_reference($reference->$fieldname->uuid));
1315
          switch ($type) {
1316
            case "Book":
1317
              $fieldname = "in book";
1318
              break;
1319
            case "Journal":
1320
              $fieldname = "in journal";
1321
              break;
1322
            case "Proceedings":
1323
              $fieldname = "in proceedings";
1324
              break;
1325
          }
1326
        }
1327
        else {
1328
          $value = $reference->$fieldname->titleCache;
1329
        }
1330

    
1331

    
1332
        if (isset($value) && $value != '') {
1333
          $table_rows[] = array(
1334
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1335
            $value,
1336
          );
1337
        }
1338

    
1339
      }
1340
      else {
1341
        if (isset($reference->$fieldname) && $reference->$fieldname != '') {
1342
          $table_rows[] = array(
1343
            t('@fieldname', array('@fieldname' => ucfirst(strtolower($fieldname)))),
1344
            $reference->$fieldname,
1345
          );
1346
        }
1347
      }
1348
    }
1349
  }
1350

    
1351
  $out = theme_table(array(
1352
    'header' => array(),
1353
    'rows' => $table_rows,
1354
    'attributes' => array(
1355
      'class' => html_class_attribute_ref($reference)
1356
    ),
1357
    'caption' => NULL,
1358
    'colgroups' => NULL,
1359
    'sticky' => NULL,
1360
    'empty' => NULL,
1361
  ));
1362

    
1363
  if(isset($reference->referenceAbstract)){
1364
    $out .= '<h2 class="block-title">Abstract</h2><div class="abstract">' . $reference->referenceAbstract . '</div>';
1365
  }
1366

    
1367

    
1368
  // Annotations below the table
1369
  $annotations = cdm_fetch_visible_annotations($reference);
1370
  $out .= cdm_annotations($annotations);
1371

    
1372
  $registration_working_set = cdm_ws_get("registrationWorkingSetDTO", array($uuid));
1373
  if($registration_working_set && count($registration_working_set->registrationDTOs) > 0){
1374
    $out .= "<h3>Nomenclatural acts:</h3><div class=\"cdm-item-list registration-item-list\">";
1375
    foreach($registration_working_set->registrationDTOs as $registration_dto){
1376
      if($registration_dto->status == "PUBLISHED"){
1377
        $registration_render_a = compose_registration_dto_compact($registration_dto, 'citation');
1378
        $registration_render_a["#prefix"] = "<div class=\"item item-registration\">";
1379
        $registration_render_a["#suffix"] = "</div>";
1380
        $out .= drupal_render($registration_render_a);
1381
      }
1382
    }
1383
    $out .= "</div>";
1384
  }
1385

    
1386
  RenderHints::popFromRenderStack();
1387

    
1388
  return markup_to_render_array($out);
1389
}
1390

    
1391
/**
1392
 * Provides the the label string for taxon page tabs.
1393
 *
1394
 * The $tabname as passed to the method will be returned if no override
1395
 * label is configured in the settings.
1396
 */
1397
function cdm_taxonpage_tab_label($tabname) {
1398
  static $taxon_tabs_labels = null;
1399
  if($taxon_tabs_labels == null){
1400
    $taxon_tabs_labels = get_array_variable_merged(CDM_TAXONPAGE_TAB_LABELS, CDM_TAXONPAGE_TAB_LABELS_DEFAULT);
1401
  }
1402
  $tabname_key = strtolower($tabname);
1403
  if(isset($taxon_tabs_labels[$tabname_key]) && $taxon_tabs_labels[$tabname_key]){
1404
    return $taxon_tabs_labels[$tabname_key];
1405
  }
1406
  return $tabname;
1407
}
1408

    
1409
/**
1410
 *  Returns HTML for the default title of a specimen page.
1411
 *  The returned title is a the identifier of the specimen.
1412
 *
1413
 * @param object $occurrence
1414
 *  The CDM SpecimenOrObservation instance
1415
 *     being formatted for the title.
1416
 *
1417
 * @return string
1418
 *  Markup for the title of a specimen page
1419
 */
1420
function render_specimen_page_title($occurrence)
1421
{
1422
  RenderHints::pushToRenderStack('render_specimen_page_title');
1423
  $out = '';
1424
  list($specimenID, $collection) = specimen_id_and_collection_for($occurrence);
1425
  if ($occurrence->class == 'FieldUnit'){
1426
    $out .= '<span class="type-label">Field unit: </span>';
1427
  }else{
1428
    $out .= '<span class="type-label">Specimen:  </span>';
1429
  }
1430

    
1431
  if($collection){
1432
    $out .= $collection." ";
1433
  }
1434
  $out .= $specimenID;
1435

    
1436
  RenderHints::popFromRenderStack();
1437

    
1438
  return '<span class="' . $occurrence->class . '">' . $out . '</span>';
1439
}
1440

    
1441
/**
1442
 * Returns HTML for the default title of a specimen page.
1443
 * The returned title is a the identifier of the specimen.
1444
 *
1445
 * @param object $occurrence_dto
1446
 *   An dto for a cdm SpecimenOrObservation entity
1447
 *
1448
 * @return string
1449
 *  Markup for the title of a specimen page
1450
 *
1451
 */
1452
function render_cdm_specimen_dto_page_title($occurrence_dto)
1453
{
1454
  RenderHints::pushToRenderStack('render_specimen_page_title');
1455
  $out = '';
1456
  list($specimenID, $collection) = specimen_id_and_collection_for($occurrence_dto);
1457
  if ($occurrence_dto->class == 'FieldUnit'){
1458
    $out .= "FieldUnit ";
1459
  }else{
1460
    $out .= "Specimen ";
1461
  }
1462
  if($collection){
1463
    $out .= $collection." ";
1464
  }
1465
  $out .= $specimenID;
1466

    
1467
  RenderHints::popFromRenderStack();
1468
  return '<span class="' . $occurrence_dto->class . '">' . $out . '</span>';
1469
}
1470

    
1471
/**
1472
 * @param $specimen
1473
 *
1474
 * @return array
1475
 */
1476
function specimen_id_and_collection_for($specimen) {
1477
  $specimenID = '';
1478
  $collection = NULL;
1479
  if (!($specimen->class == 'FieldUnit')) {
1480
    if ($specimen->collection) {
1481
      if ($specimen->collection->code) {
1482
        $collection = $specimen->collection->code;
1483
      }
1484
      elseif ($specimen->collection->name) {
1485
        $collection = $specimen->collection->name;
1486
      }
1487
    }
1488
    if ($specimen->accessionNumber) {
1489
      $specimenID = $specimen->accessionNumber;
1490
    }
1491
    elseif ($specimen->barcode) {
1492
      $specimenID = $specimen->barcode;
1493
    }
1494
    elseif ($specimen->catalogNumber) {
1495
      $specimenID = $specimen->catalogNumber;
1496
    }
1497
    elseif ($specimen->titleCache) {
1498
      $specimenID = $specimen->titleCache;
1499
    }
1500
    if (!isset($specimenID) and !isset($collection)) {
1501
      $specimenID = $specimen->uuid;
1502
    }
1503
  }
1504
  else {
1505
    if ($specimen->titleCache) {
1506
      $specimenID = $specimen->titleCache;
1507
    }
1508
    if (!isset($specimenID) and !isset($collection)) {
1509
      $specimenID = $specimen->uuid;
1510
    }
1511
  }
1512
  return [$specimenID, $collection];
1513
}
1514

    
1515
/**
1516
 * Renders a cdm collection entity as html markup.
1517
 *
1518
 * institute and subcollection
1519
 *
1520
 * @param $collection
1521
 *
1522
 * @return string
1523
 *  The markup for the collection,
1524
 */
1525
function render_collection($collection, $do_link = true){
1526
   if(!is_object($collection)){
1527
     return '';
1528
   }
1529
   $collection_str_list = [];
1530
   $super_collection = $collection;
1531
   while($super_collection){
1532
     $collection_str = $super_collection->titleCache;
1533
     if(is_object($super_collection->institute) && $super_collection->institute->titleCache){
1534
       $collection_str .= ' at ' . $super_collection->institute->titleCache;
1535
     }
1536
     $collection_str_list[] = $collection_str;
1537
     if(isset($super_collection->superCollection)){
1538
      $super_collection = $super_collection->superCollection;
1539
     } else {
1540
       $super_collection = null;
1541
     }
1542
   }
1543
   $markup = join(' in ', $collection_str_list);
1544
   if($markup){
1545
     $markup = '<span class="' . html_class_attribute_ref($collection) . '">' . $markup . '</span>';
1546
   }
1547
   return $markup;
1548
}
1549

    
(9-9/14)