Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / modules / cdm_dataportal / includes / taxon.inc @ 6679207a

History | View | Annotate | Download (27.1 KB)

1
<?php
2

    
3
/**
4
 * @file
5
 * Functions for dealing with CDM entities from the package model.taxon
6
 *
7
 * @copyright
8
 *   (C) 2007-2016 EDIT
9
 *   European Distributed Institute of Taxonomy
10
 *   http://www.e-taxonomy.eu
11
 *
12
 *   The contents of this module are subject to the Mozilla
13
 *   Public License Version 1.1.
14
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
15
 *
16
 * @author
17
 *   - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
18
 */
19

    
20
/**
21
 * @defgroup compose Compose functions
22
 * @{
23
 * Functions which are composing Drupal render arrays
24
 *
25
 * The cdm_dataportal module needs to compose rather complex render arrays from
26
 * the data returned by the CDM REST service. The compose functions are
27
 * responsible for creating the render arrays.
28
 *
29
 * All these functions are also implementations of the compose_hook()
30
 * which is used in the proxy_content() function.
31
 * @}
32
 */
33

    
34
define('TAXON_TYPE_SYNONYM', 'Synonym');
35
define('TAXON_TYPE_MISAPPLIEDNAME', 'misapplied-name');
36

    
37
/**
38
 * Returns HTML for misapplied names and invalid designations.
39
 *
40
 * Both relation types are currently treated the same!
41
 *
42
 * @param object taxonRelationships
43
 *   A TaxonRelationshipsDTO, see taxon/{uuid}/taxonRelationshipsDTO
44
 *
45
 * @param object focusedTaxon
46
 *  The taxon being in the focus of the application
47
 *
48
 * @return string
49
 *    the rendered html
50
 */
51
function cdm_taxonRelationships($taxonRelationshipsDTO, $focusedTaxon){
52

    
53
  static $relationship_type_order_weights = [
54
    //
55
    UUID_PROPARTE_MISAPPLIEDNAME_FOR => 0,
56
    UUID_PARTIAL_MISAPPLIEDNAME_FOR => 0,
57
    //
58
    UUID_INVALID_DESIGNATION_FOR  => 1,
59
    //
60
    UUID_MISAPPLIED_NAME_FOR  => 2,
61
    //
62
    UUID_PROPARTE_SYNONYM_FOR  => 3,
63
    UUID_PARTIAL_SYNONYM_FOR  => 3
64
  ];
65

    
66
  if (!$taxonRelationshipsDTO || $taxonRelationshipsDTO->size < 1) {
67
    return null;
68
  }
69

    
70
  static $dedup_rel_type_uuids = array(
71
    UUID_MISAPPLIED_NAME_FOR,
72
    UUID_INVALID_DESIGNATION_FOR,
73
    UUID_PARTIAL_MISAPPLIEDNAME_FOR,
74
    UUID_PROPARTE_MISAPPLIEDNAME_FOR
75
  );
76

    
77
  RenderHints::pushToRenderStack('taxon_relationships');
78
  $footnote_list_key = 'taxon_relationships';
79
  RenderHints::setFootnoteListKey($footnote_list_key);
80

    
81
  $misapplied = array();
82
  $joined_refs = array();
83

    
84
  $taxon_relationship_types = variable_get(CDM_TAXON_RELATIONSHIP_TYPES, unserialize(CDM_TAXON_RELATIONSHIP_TYPES_DEFAULT));
85

    
86
  // Aggregate misapplied names having the same fullname:
87
  //  - deduplicate misapplied names, so that the same name it not shown multiple times in case it
88
  //    the duplicates only have different sensu references with detail and appended phrase (see #5647)
89
  //  - show only the author team as short citation for the sec reference
90
  //  - show the according reference as footnote to this short citation
91
  //
92
  // Example:
93
  // "Xenoxylum foobar" sensu Grumbach¹; sensu Lem²
94
  //    1. Novel marsian species, Grumbach, 2022
95
  //    2. Flora solaris, Lem, 2019
96
  if (isset($taxonRelationshipsDTO->relations[0])) {
97
    foreach($taxonRelationshipsDTO->relations as $taxon_relation){
98

    
99
      if (in_array($taxon_relation->type->uuid, $taxon_relationship_types)) {
100

    
101
        if (in_array($taxon_relation->type->uuid, $dedup_rel_type_uuids)) {
102

    
103
          RenderHints::pushToRenderStack('misapplied_name_for'); // TODO the render path string should in future come from $taxonRelation->type->...
104

    
105
          // full name with relation symbol, rel sec as de-duplication key
106
          // the sensu part will be removed from the key below in case it is present
107
          $name_text = join(' ', cdm_tagged_text_values($taxon_relation->taggedText, array('name')));
108
          $full_name_key = cdm_tagged_text_to_string($taxon_relation->taggedText, array('symbol', 'appendedPhrase'));
109
          $symbol_text = join(' ', cdm_tagged_text_values($taxon_relation->taggedText, array('symbol')));
110
          $full_name_key = $name_text . ' ' . str_replace($name_text, '', $full_name_key) . ' ' . $symbol_text;
111

    
112
          cdm_taxonRelationships_process_relationship_dto($taxon_relation, $misapplied, $joined_refs, $relationship_type_order_weights , $full_name_key);
113

    
114
          RenderHints::popFromRenderStack();
115
        } else {
116
          RenderHints::pushToRenderStack('other_taxon_relationship');
117
          // All relationship types except misapplied_name_for and invalid_designation_for.
118

    
119
          $taxon_relationships_lines[] = cdm_tagged_text_to_markup($taxon_relation->taggedText);
120

    
121
          RenderHints::popFromRenderStack();
122
        }
123
      }
124
    } // END loop over $taxonRelationshipsDTO->relations
125

    
126
  }
127

    
128
  // Sort the $joined_refs and create footnotes and footnote keys.
129
  uasort($joined_refs, function($a, $b) {
130
    if ($a['order_by_key'] == $b['order_by_key']) {
131
      return 0;
132
    }
133
    return ($a['order_by_key'] < $b['order_by_key']) ? -1 : 1;
134
  });
135

    
136

    
137
  foreach ($joined_refs as $ref_key => $sensu_strings) {
138
    if(!empty($sensu_strings)) {
139
      $ref_key_tokens = explode('#', $ref_key);
140
      $sensu_uuid = $ref_key_tokens[0];
141
      $sensu_reference = cdm_ws_get(CDM_WS_REFERENCE, $sensu_uuid);
142
      if(!$sensu_reference){
143
        drupal_set_message("Problem fetching sensu reference with uuid " . $sensu_uuid, 'error');
144
      }
145
      if($sensu_strings['order_by_key'] != $sensu_reference->titleCache){
146
        $sensu_reference_markup = cdm_reference_markup($sensu_reference);
147
        $footnote_key = FootnoteManager::addNewFootnote($footnote_list_key, $sensu_reference_markup);
148
        $footnote_key = render_footnote_key($footnote_key);
149
        $joined_refs[$ref_key]['markup'] = '<span class="sensu">' . $sensu_strings['markup'] . $footnote_key . '</span>';
150
      }
151
    }
152
  }
153

    
154
  // ---- Generate output ---- //
155
  $out = '<div class="taxon-relationships">';
156
  if (count($misapplied) > 0) {
157
    ksort($misapplied); // order the misapplied by scientific name
158
    $out .= '<ul class="misapplied">';
159

    
160
    // create the list entries per misapplied name
161
    foreach ($misapplied as $misapplied_name) {
162
      $misapplied_name_markup = $misapplied_name['out'];
163
      // all sensu and auct. for this MAN
164
      if (isset($misapplied_name['sensu_uuids'])) {
165
        $sensu_refs_with_fkey = array();
166
        foreach ($misapplied_name['sensu_uuids'] as $sensu_data) {
167
          if($sensu_data['uuid']){
168
            $joined_ref_key = $sensu_data['uuid'] . ($sensu_data['citation_detail'] ? '#' .  $sensu_data['citation_detail'] : '');
169
            $sensu_refs_with_fkey[$sensu_data['prefix'] . $joined_refs[$joined_ref_key]['order_by_key']] = $sensu_data['prefix'] . $joined_refs[$joined_ref_key]['markup'];
170
          } else {
171
            $sensu_refs_with_fkey[$sensu_data['prefix']] = $sensu_data['prefix'];
172
          }
173
        }
174
        ksort($sensu_refs_with_fkey);
175
        $sensu_refs_with_fkey_markup = join('; ', $sensu_refs_with_fkey);
176
        if(strpos($misapplied_name_markup, '{PLACEHOLDER_secReference}') !== false){
177
          $misapplied_name_markup = str_replace('{PLACEHOLDER_secReference}', $sensu_refs_with_fkey_markup, $misapplied_name_markup);
178
          // just remove the appendedPhrase placeholder as was included in the sensu_refs_with_fkey_markup
179
          $misapplied_name_markup = str_replace('{PLACEHOLDER_appendedPhrase}', '', $misapplied_name_markup);
180
        } else {
181
          // no sec ref so there could only be the appended phrase
182
          $misapplied_name_markup = str_replace('{PLACEHOLDER_appendedPhrase}', $sensu_refs_with_fkey_markup, $misapplied_name_markup);
183
        }
184
      }
185

    
186
      // append the err. sec. if there is any for this MAN
187
      if (isset($misapplied_name['relsec_uuids'])) {
188
        $relsec_refs_with_fkey = array();
189
        foreach ($misapplied_name['relsec_uuids'] as $relsec_data) {
190
          if($relsec_data['uuid']) {
191
            $relsec_refs_with_fkey[$relsec_data['prefix'] . $joined_refs[$relsec_data['uuid']]['order_by_key']] = $relsec_data['prefix'] . $joined_refs[$relsec_data['uuid']]['markup'];
192
          }
193
        }
194
        ksort($relsec_refs_with_fkey);
195
        $relsec_refs_with_fkey_markup = join('; ', $relsec_refs_with_fkey);
196
        $misapplied_name_markup = str_replace('{PLACEHOLDER_relSecReference}', $relsec_refs_with_fkey_markup, $misapplied_name_markup);
197
      }
198
      // final line
199
      $out .= '<li class="synonym"><span class="misapplied">' . $misapplied_name_markup . ' </span></li>';
200
    }
201
    $out .= '</ul>';
202
  }
203

    
204
  if (isset($taxon_relationships_lines) && is_array($taxon_relationships_lines) && count($taxon_relationships_lines) > 0) {
205
    $out .= '<ul class="taxonRelationships">';
206
    foreach ($taxon_relationships_lines as $taxon_relationship_line) {
207
      $out .= '<li class="synonym">' . $taxon_relationship_line . '</li>';
208
    }
209
    $out .= '</ul>';
210
  }
211

    
212
  $footnotes = render_footnotes($footnote_list_key, 'li');
213

    
214
  $out .= '<ul class="footnotes">' . $footnotes . '</ul>';
215
  $out .= '</div>';
216

    
217
  RenderHints::popFromRenderStack();
218
  return $out;
219
}
220

    
221
/**
222
 * @param $taxon_relation
223
 * @param $misapplied
224
 * @param $joined_refs
225
 * @param $relationship_type_weights
226
 * @param $name_dedup_key
227
 */
228
function cdm_taxonRelationships_process_relationship_dto($taxon_relation, &$misapplied, &$joined_refs, $relationship_type_weights, $name_dedup_key) {
229

    
230
  $appended_phrase_text = join(' ', cdm_tagged_text_values($taxon_relation->taggedText, array('appendedPhrase')));
231
  // remove/extract appendedPhrase, secReference, relSecReference and add placeholders if needed
232
  tagged_text_extract($taxon_relation->taggedText, 'appendedPhrase', true);
233
  $sensu_tagged_text = tagged_text_extract_reference_and_detail($taxon_relation->taggedText, "secReference", true);
234
  $relsec_tagged_text = tagged_text_extract_reference_and_detail($taxon_relation->taggedText, "relSecReference", true);
235

    
236
  // prepend the weight value for the relationship type to the $name_dedup_key so that the key can be used for ordering the
237
  // entries in the $misapplied array
238
  if(isset($relationship_type_weights[$taxon_relation->type->uuid])){
239
    $reltype_weight = $relationship_type_weights[$taxon_relation->type->uuid];
240
  } else {
241
    $reltype_weight = 99;
242
  }
243
  $name_dedup_key = str_pad( $reltype_weight, 2, '0', STR_PAD_LEFT) . '-' . $name_dedup_key;
244

    
245
  if (isset($sensu_tagged_text[1])) {
246
    // for de-duplication everything else needs to be equal except for appendedPhrase + MAN.sec + MAN.secDetail. see #7658#note-21
247
    $name_dedup_key = str_replace(cdm_tagged_text_to_string($sensu_tagged_text), ' ', $name_dedup_key);
248
    $appended_phrase_text = $appended_phrase_text . array_shift($sensu_tagged_text)->text; // remove first element which contains the "sensu", this will be added later in this code
249
    $sensu_citation_detail = trim(join(' ', cdm_tagged_text_values($sensu_tagged_text, array('secMicroReference'))));
250
    $sensu_citation_short_markup = cdm_tagged_text_to_markup($sensu_tagged_text);
251
    $sensu_citation_short = cdm_tagged_text_to_string($sensu_tagged_text);
252
    $sensu_uuid = $sensu_tagged_text[0]->entityReference->uuid;
253
    $name_dedup_key = preg_replace('/\s+/', ' ', $name_dedup_key); // sanitize multiple whitespace characters
254
    $misapplied[$name_dedup_key]['sensu_uuids'][] = array('uuid' => $sensu_uuid, 'prefix' => $appended_phrase_text, 'citation_detail' => $sensu_citation_detail);
255
    $ref_key = $sensu_uuid . ($sensu_citation_detail ? '#' . $sensu_citation_detail : '');
256
    if (!isset($joined_refs[$ref_key])) {
257
      $joined_refs[$ref_key] = array(
258
        'order_by_key' => $sensu_citation_short,
259
        'markup' => $sensu_citation_short_markup // the footnote key will be appended later
260
      );
261
    }
262
  } else if ($appended_phrase_text) {
263
    // appended phrase without reference
264
    $name_dedup_key = preg_replace('/\s+/', ' ', $name_dedup_key); // sanitize multiple whitespace characters
265
    $misapplied[$name_dedup_key]['sensu_uuids'][] = array('uuid' => null, 'prefix' => $appended_phrase_text);
266
  }
267

    
268
  if (isset($relsec_tagged_text[1])) {
269
    $appended_phrase_text = array_shift($relsec_tagged_text)->text; // remove first element which contains the "err. sec", this will be added later in this code
270
    $relsec_citation_detail = trim(join(' ', cdm_tagged_text_values($relsec_tagged_text, array('secMicroReference'))));
271
    $relsec_citation_short_markup = cdm_tagged_text_to_markup($relsec_tagged_text);
272
    $relsec_citation_short = cdm_tagged_text_to_string($relsec_tagged_text);
273
    $relsec_uuid = $relsec_tagged_text[0]->entityReference->uuid;
274
    $misapplied[$name_dedup_key]['relsec_uuids'][] = array('uuid' => $relsec_uuid, 'prefix' => $appended_phrase_text, 'citation_detail' => $relsec_citation_detail);;
275
    $ref_key = $relsec_uuid . ($relsec_citation_detail ? '#' . $relsec_citation_detail : '');
276
    if (!isset($joined_refs[$ref_key])) {
277
      $joined_refs[$ref_key] = array(
278
        'order_by_key' => $relsec_citation_short,
279
        'markup' => $relsec_citation_short_markup // the footnote key will be appended later
280
      );
281
    }
282
  }
283

    
284
  if (!isset($misapplied[$name_dedup_key]['out'])) {
285
    // helpful for debugging: // cdm_tagged_text_add_options($taxon_relation->taggedText, array(array('filter-type' => 'symbol', 'attributes' => array('title' =>  array($taxon_relation->type->representations[0]->label)))));
286
    $misapplied[$name_dedup_key]['out'] = cdm_tagged_text_to_markup($taxon_relation->taggedText);
287
  } else {
288
    // We need to add the anchors for all of the other misapplied names not
289
    // being rendered explicitly.
290
    $misapplied[$name_dedup_key]['out'] = uuid_anchor($taxon_relation->taxonUuid, $misapplied[$name_dedup_key]['out']);
291
  }
292

    
293
}
294

    
295
/**
296
 * Renders a representation of the given taxon relationship.
297
 *
298
 * According name relationships are also being rendered.
299
 *
300
 * @param $taxon
301
 *  The CDM TaxonBase entity
302
 * @param $reltype_uuid
303
 *  The UUID of the TaxonRelationshipType
304
 * @param $relsign
305
 *  Optional. Can be  used to override the internal decision strategy on finding a suitable icon for the relationship
306
 * @param $reltype_representation
307
 *   Optional: Defines the value for the title attribute of the html element enclosing the relsign
308
 * @param $doubtful
309
 *   TODO
310
 * @param $doLinkTaxon
311
 *   The taxon will be rendered as clickable link when true.
312
 *
313
 * @return string
314
 *   Markup for the taxon relationship.
315
 *
316
 * @throws Exception
317
 */
318
function cdm_related_taxon($taxon, $reltype_uuid = NULL, $relsign = NULL, $reltype_representation = NULL, $doubtful=false, $doLinkTaxon = FALSE) {
319
  static $relsign_homo = '≡';
320
  static $relsign_hetero = '=';
321
  static $relsign_invalid = '&ndash;';
322
  static $nom_status_invalid_type_uuids =  array(
323
    UUID_NOMENCLATURALSTATUS_TYPE_INVALID,
324
    UUID_NOMENCLATURALSTATUS_TYPE_NUDUM,
325
    UUID_NOMENCLATURALSTATUS_TYPE_COMBINATIONINVALID,
326
    UUID_NOMENCLATURALSTATUS_TYPE_PROVISIONAL
327
  );
328

    
329
  // 'taxonRelationships';
330
  $footnoteListKey = NULL;
331

    
332
  $skip_tags = array();
333

    
334
  $is_invalid = false;
335

    
336
  if (!$relsign) {
337

    
338
    switch ($reltype_uuid) {
339
      case UUID_HETEROTYPIC_SYNONYM_OF:
340
      case UUID_SYNONYM_OF:
341
        $relsign = $relsign_hetero;
342
        break;
343

    
344
      case UUID_HOMOTYPIC_SYNONYM_OF:
345
        $relsign = $relsign_homo;
346
        break;
347

    
348
      case UUID_MISAPPLIED_NAME_FOR:
349
      case UUID_INVALID_DESIGNATION_FOR:
350
        $skip_tags[] = 'authors';
351
        $is_invalid = true;
352
        $relsign = $relsign_invalid;
353

    
354
        break;
355

    
356
      default:
357
        $relsign = $relsign_invalid;
358
    }
359

    
360
  }
361

    
362
  if($doubtful) {
363
    $relsign = '?' . $relsign;
364
  }
365

    
366
  /*
367
  Names with status invalid or nudum are to be displayed with the
368
  $relsign_invalid, these names appear at the end of all names in their
369
  homotypic group (ordered correctly by the java cdm_lib).
370
  */
371
  if (isset($taxon->name->status) && is_array($taxon->name->status)) {
372
    foreach ($taxon->name->status as $status) {
373
      if (in_array($status->type->uuid , $nom_status_invalid_type_uuids)) {
374
        $relsign = $relsign_invalid;
375
        break;
376
      }
377
    }
378
  }
379

    
380
  // Now rendering starts ..
381
  RenderHints::pushToRenderStack('related_taxon');
382

    
383
  if (isset($taxon->name->nomenclaturalSource->citation)) {
384
    $referenceUri = url(path_to_reference($taxon->name->nomenclaturalSource->citation->uuid));
385
  }
386
  $taxonUri = '';
387
  if ($doLinkTaxon) {
388
    $taxonUri = url(path_to_taxon($taxon->uuid, "synonymy"));
389
  }
390
  // Printing the taxonName and the handling the special case of annotations.
391
  if (!isset($referenceUri)) {
392
    $referenceUri = FALSE;
393
  }
394
  $out_taxon_part = render_taxon_or_name($taxon, $taxonUri, $referenceUri, TRUE, FALSE, $skip_tags, $is_invalid);
395

    
396
  // must come before the rendering the relationships so that the footnote key
397
  // numbers are in correct order
398
  $taxon_and_name_footnotes = render_taxon_and_name_footnotes($taxon);
399

    
400
  $name_relations = cdm_name_relationships_for_taxon($taxon);
401
  $name_relations_render_array = compose_name_relationships_inline($name_relations, $taxon->name->uuid, $taxon->uuid);
402

    
403
  $out = '<span class="relation_sign" title="' . $reltype_representation . '">' . $relsign . '</span>'
404
    . $out_taxon_part . $taxon_and_name_footnotes;
405
  if(isset($name_relations_render_array['list']['items'][0])){
406
    $out .= ' '  . drupal_render($name_relations_render_array);
407
  }
408

    
409
  $out = uuid_anchor($taxon->uuid, $out);
410

    
411
  RenderHints::popFromRenderStack();
412

    
413
  return $out;
414
}
415

    
416
/**
417
 * Convenience function to create footnotes of annotations and sources for a
418
 * taxon and name at the same time. Calls handle_annotations_and_sources()
419
 * internally twice and combines the resulting footnote markup
420
 *
421
 * @param $taxon
422
 *
423
 * @return string
424
 *
425
 * @see synonymy_annotations_and_source_config()
426
 */
427
function render_taxon_and_name_footnotes($taxon) {
428
  $annotations_and_sources_taxon = handle_annotations_and_sources(
429
    $taxon,
430
    synonymy_annotations_and_source_config(),
431
    '',
432
    RenderHints::getFootnoteListKey()
433
  );
434
  $annotations_and_sources_name = handle_annotations_and_sources(
435
    $taxon->name,
436
    synonymy_annotations_and_source_config(),
437
    '',
438
    RenderHints::getFootnoteListKey()
439
  );
440
  $footnotes = $annotations_and_sources_taxon['foot_note_keys'] .
441
    ($annotations_and_sources_taxon['foot_note_keys'] && $annotations_and_sources_name['foot_note_keys'] ? ',' : '') .
442
    $annotations_and_sources_name['foot_note_keys'];
443
  return $footnotes;
444
}
445

    
446

    
447
/**
448
 * Creates markup for a taxon which is the accepted of another one
449
 *
450
 * @param $accepted_for_uuid
451
 *   The uuid of the accepted taxon
452
 */
453
function cdm_accepted_for($accepted_for_uuid) {
454

    
455
  if(!is_uuid($accepted_for_uuid)){
456
    return '';
457
  }
458

    
459
  RenderHints::pushToRenderStack('acceptedFor');
460
  $out = '';
461

    
462
  $synonym = cdm_ws_get(CDM_WS_PORTAL_TAXON, $accepted_for_uuid);
463
  if ($synonym) {
464
    $out .= '<span class="acceptedFor">';
465
    $out .= t('is accepted for ');
466
    $referenceUri = null;
467
    if (isset($synonym->name->nomenclaturalSource->citation)) {
468
      $referenceUri = url(path_to_reference($synonym->name->nomenclaturalSource->citation->uuid));
469
    }
470
    $out .= render_taxon_or_name($synonym->name, NULL, $referenceUri);
471
    $annotations_and_sources = handle_annotations_and_sources(
472
      $synonym,
473
      annotations_and_source_config(),
474
      '',
475
      RenderHints::getFootnoteListKey());
476
    $out .= $annotations_and_sources['foot_note_keys'];
477
    $out .= '</span>';
478
  }
479
  RenderHints::popFromRenderStack();
480
  return $out;
481
}
482

    
483
/**
484
 * Compose function for a list of taxa.
485
 *
486
 * This function is for used to:
487
 *
488
 * 1. Display search results
489
 * 2. List the taxa for a taxon name in the name page.
490
 *
491
 * @param $taxon_list array
492
 *   The list of CDM Taxon entities. e.g. The records array as contained in a pager object.
493
 * @param $freetext_search_results array
494
 * @param $show_classification boolean
495
 *
496
 * @ingroup compose
497
 *
498
 */
499
function compose_list_of_taxa($taxon_list, $freetext_search_results = array(), $show_classification = false) {
500

    
501
  $unclassified_snippet = '<span class="unclassified">' . t('unclassified') . '</span>';
502

    
503
  RenderHints::pushToRenderStack('list_of_taxa');
504

    
505
  $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SEARCH_GALLERY_NAME);
506

    
507
  $showMedia_taxa = $gallery_settings['cdm_dataportal_show_taxon_thumbnails'];
508
  $showMedia_synonyms = $gallery_settings['cdm_dataportal_show_synonym_thumbnails'];
509
  $searched_in_classification = cdm_dataportal_searched_in_classification();
510
  $searched_in_classification_uuid = null;
511
  if(isset($searched_in_classification->uuid)){
512
    $searched_in_classification_uuid = $searched_in_classification->uuid;
513
  }
514

    
515
  // .. Well, for sure not as performant as before, but better than nothing.
516
  $synonym_uuids = array();
517
  $misapplied_uuids = array();
518
  foreach ($taxon_list as $taxon) {
519
    if ($taxon->class == "Synonym") {
520
      if (!array_key_exists($taxon->uuid, $synonym_uuids)) {
521
        $synonym_uuids[$taxon->uuid] = $taxon->uuid;
522
      }
523
    }
524
    elseif (!_cdm_dataportal_acceptedByCurrentView($taxon)) {
525
      // Assuming that it is a misapplied name, will be further examined below.
526
      $misapplied_uuids[$taxon->uuid] = $taxon->uuid;
527
    }
528
  }
529

    
530
  // Batch service not jet implemented:
531
  // $table_of_accepted = cdm_ws_property(CDM_WS_PORTAL_TAXON_ACCEPTED,
532
  // join(',', $synonym_uuids));
533
  // thus ...
534
  $table_of_accepted = array();
535

    
536
  foreach ($synonym_uuids as $relatedUuid) {
537
    $classification_filter = '';
538
    if($searched_in_classification_uuid){
539
      $classification_filter = 'classificationFilter=' . $searched_in_classification_uuid;
540
    }
541
    $table_of_accepted[$relatedUuid] = cdm_ws_get(
542
      CDM_WS_PORTAL_TAXON_ACCEPTED,
543
      array($relatedUuid),
544
      $classification_filter
545
    );
546
  }
547

    
548
  foreach ($misapplied_uuids as $relatedUuid) {
549
    $taxonRelations = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS, array(
550
      $relatedUuid,
551
    ));
552
    foreach ($taxonRelations as $relation) {
553
      if ($relation->type->uuid == UUID_MISAPPLIED_NAME_FOR && _cdm_dataportal_acceptedByCurrentView($relation->toTaxon)) {
554
        $table_of_accepted[$relatedUuid][] = $relation->toTaxon;
555
      }
556
    }
557
  }
558

    
559
  $out = '<div class="cdm-item-list" style="background-image: none;">';
560
  $itemCnt = -1;
561
  foreach ($taxon_list as $taxon) {
562
    $itemCnt++;
563
    if (isset($table_of_accepted[$taxon->uuid])) {
564
      // Its a synonym or misapplied name.
565
      $is_synonym = isset($synonym_uuids[$taxon->uuid]); //TODO better use the $taxon->class attribute?
566
      $taxon_type = $is_synonym ? TAXON_TYPE_SYNONYM :TAXON_TYPE_MISAPPLIEDNAME;
567

    
568
      $acceptedTaxa = $table_of_accepted[$taxon->uuid];
569

    
570
      if (!is_array($acceptedTaxa)) {
571
        $acceptedTaxa = array($acceptedTaxa);
572
      }
573

    
574
      foreach ($acceptedTaxa as $acceptedTaxon) {
575
        if (is_object($acceptedTaxon)) {
576

    
577
          $taxonUri = uri_to_synonym($taxon->uuid, $acceptedTaxon->uuid);
578
          $referenceUri = '';
579
          if (isset($acceptedTaxon->name->nomenclaturalSource->citation)) {
580
            $referenceUri = url(path_to_reference($acceptedTaxon->name->nomenclaturalSource->citation->uuid));
581
          }
582
          $taxon_or_name = $is_synonym ? $taxon->name : $taxon;
583
          // $taxon_or_name this is a trick to suppress the sec reference for synonyms
584
          // supplying the name will cause render_taxon_or_name() to not show the sec reference
585
          $out .= "<div class=\"item " . $taxon_type . "\">" . render_taxon_or_name($taxon_or_name, $taxonUri, $referenceUri);
586
          if($taxon_type == TAXON_TYPE_MISAPPLIEDNAME && is_object($taxon->sec)){
587
            if(isset($taxon->sec->authorship)){
588
              $authorship = $taxon->sec->authorship->titleCache;
589
            } else {
590
              $authorship = $taxon->sec->titleCache;
591
            }
592
            $out .=  ' sensu ' . $authorship;
593
          }
594
          if ($show_classification) {
595
            $classifications = get_classifications_for_taxon($taxon);
596
            $classification_titles = array();
597
            foreach ($classifications as $classification) {
598
              if (isset($classification->titleCache)) {
599
                $classification_titles[] = $classification->titleCache;
600
              }
601
            }
602
            if (count($classification_titles) == 0) {
603
              $classification_titles[] = $unclassified_snippet;
604
            }
605
            $out .= '<span class="classifications"><span class="separator"> : </span>' . implode(', ', $classification_titles) . '</span>';
606
          }
607
          $out .= render_taxon_and_name_footnotes($taxon);
608
          if ($showMedia_synonyms) {
609
            $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $acceptedTaxon));
610
          }
611
        }
612
      }
613
    }
614
    else {
615
      // Its a Taxon.
616
      $taxonUri = url(path_to_taxon($taxon->uuid));
617
      $referenceUri = '';
618
      if (isset($taxon->name->nomenclaturalSource->citation)) {
619
        $referenceUri = url(path_to_reference($taxon->name->nomenclaturalSource->citation->uuid));
620
      }
621
      $out .= '<div class="item Taxon">' . render_taxon_or_name($taxon, $taxonUri, $referenceUri);
622
      if ($show_classification) {
623
        $classifications = get_classifications_for_taxon($taxon);
624
        $classification_titles = array();
625
        foreach ($classifications as $classification) {
626
          if (isset($classification->titleCache)) {
627
            $classification_titles[] = $classification->titleCache;
628
          }
629
        }
630
        if(count($classification_titles) == 0){
631
          $classification_titles[] = $unclassified_snippet;
632
        }
633
        $out .= '<span class="classifications"><span class="separator"> : </span>' . implode(', ', $classification_titles) . '</span>';
634
      }
635
      $out .= render_taxon_and_name_footnotes($taxon);
636

    
637
      if ($showMedia_taxa) {
638
        $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $taxon));
639
      }
640
    }
641

    
642
    /*
643
     * the score field will be empty in case of MultiTermQueries like
644
     * WildcardQueries, since these are  constant score by default
645
     * since Lucene 2.9
646
     */
647
    if(isset($freetext_search_results[$itemCnt]) && $freetext_search_results[$itemCnt]->score && $freetext_search_results[$itemCnt]->maxScore){
648
      $percentage =  ( $freetext_search_results[$itemCnt]->score / $freetext_search_results[$itemCnt]->maxScore ) * 100;
649
      $out .= '<div class="score-bar"><div class="score-bar-indicator" style="width:' . $percentage .'% "></div></div>';
650
      $out .= '<div class="score-bar-value">' . number_format($percentage, 2) .'%</div>';
651
    }
652

    
653
    // Render highlighted fragments, these are made available by free text
654
    // searches.
655
    if (isset($freetext_search_results[$itemCnt]->fieldHighlightMap)) {
656
      $field_fragments = (array) $freetext_search_results[$itemCnt]->fieldHighlightMap;
657
      if (count($field_fragments) > 0) {
658
        $fragments_out = '';
659
        foreach ($field_fragments as $fieldName => $fragments) {
660
          $fragments_out .= '... <span class="' . $fieldName. '">' . filter_xss(join(" ... ", $fragments), array('b') ) . '</span>';
661
        }
662
        $out .= '<div class="fragment_highlight">' . $fragments_out . ' ...</div>';
663
      }
664
    }
665

    
666
    $out .= '</div>';
667
  }
668

    
669
  $out .= '</div>';
670
  RenderHints::popFromRenderStack();
671

    
672
  return markup_to_render_array($out); // TODO create render array of all list items in function
673
}
674

    
675

    
676
/**
677
 * Compose function for the taxonomic children
678
 *
679
 * @param $taxon_uuid
680
 *    The uuuid of the taxon to compose the list of taxonomic children for
681
 * @return
682
 *   A drupal render array.
683
 *
684
 * @ingroup compose
685
 */
686
function compose_taxonomic_children($taxon_uuid){
687

    
688
  $render_array = array();
689
  
690
  if($taxon_uuid) {
691
    $children = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY_CHILDNODES_OF_TAXON, array(
692
      get_current_classification_uuid(),
693
      $taxon_uuid
694
      ));
695
    if($children){
696
      $taxonomic_children = theme('cdm_taxontree', array('tree' => $children));
697
      $render_array = markup_to_render_array($taxonomic_children);
698
    }
699
  }
700
  return $render_array;
701
}
702

    
Add picture from clipboard (Maximum size: 40 MB)