Project

General

Profile

Download (25.3 KB) Statistics
| Branch: | Tag: | Revision:
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_MISAPPLIED_NAME_FOR  => 1,
59
    //
60
    UUID_PROPARTE_SYNONYM_FOR  => 2,
61
    UUID_PARTIAL_SYNONYM_FOR  => 2
62
  ];
63

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

    
68
  static $dedup_rel_type_uuids = array(
69
    UUID_MISAPPLIED_NAME_FOR,
70
    UUID_PARTIAL_MISAPPLIEDNAME_FOR,
71
    UUID_PROPARTE_MISAPPLIEDNAME_FOR
72
  );
73

    
74
  RenderHints::pushToRenderStack('taxon_relationships');
75
  $footnote_list_key = 'taxon_relationships';
76
  RenderHints::setFootnoteListKey($footnote_list_key);
77

    
78
  $misapplied = array();
79
  $joined_refs = array();
80

    
81
  $taxon_relationship_types = variable_get(CDM_TAXON_RELATIONSHIP_TYPES, unserialize(CDM_TAXON_RELATIONSHIP_TYPES_DEFAULT));
82

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

    
96
      if (in_array($taxon_relation->type->uuid, $taxon_relationship_types)) {
97

    
98
        if (in_array($taxon_relation->type->uuid, $dedup_rel_type_uuids)) {
99

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

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

    
109
          cdm_taxonRelationships_process_relationship_dto($taxon_relation, $misapplied, $joined_refs, $relationship_type_order_weights , $full_name_key);
110

    
111
          RenderHints::popFromRenderStack();
112
        } else {
113
          RenderHints::pushToRenderStack('other_taxon_relationship');
114
          // All relationship types except misapplied_name_for and invalid_designation_for.
115

    
116
          $taxon_relationships_lines[] = cdm_tagged_text_to_markup($taxon_relation->taggedText);
117

    
118
          RenderHints::popFromRenderStack();
119
        }
120
      }
121
    } // END loop over $taxonRelationshipsDTO->relations
122

    
123
  }
124

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

    
133

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

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

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

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

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

    
209
  $footnotes = render_footnotes($footnote_list_key, 'li');
210

    
211
  $out .= '<ul class="footnotes">' . $footnotes . '</ul>';
212
  $out .= '</div>';
213

    
214
  RenderHints::popFromRenderStack();
215
  return $out;
216
}
217

    
218
/**
219
 * @param $taxon_relation
220
 * @param $misapplied
221
 * @param $joined_refs
222
 * @param $relationship_type_weights
223
 * @param $name_dedup_key
224
 */
225
function cdm_taxonRelationships_process_relationship_dto($taxon_relation, &$misapplied, &$joined_refs, $relationship_type_weights, $name_dedup_key) {
226

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

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

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

    
265
  if (isset($relsec_tagged_text[1])) {
266
    $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
267
    $relsec_citation_detail = trim(join(' ', cdm_tagged_text_values($relsec_tagged_text, array('secMicroReference'))));
268
    $relsec_citation_short_markup = cdm_tagged_text_to_markup($relsec_tagged_text);
269
    $relsec_citation_short = cdm_tagged_text_to_string($relsec_tagged_text);
270
    $relsec_uuid = $relsec_tagged_text[0]->entityReference->uuid;
271
    $misapplied[$name_dedup_key]['relsec_uuids'][] = array('uuid' => $relsec_uuid, 'prefix' => $appended_phrase_text, 'citation_detail' => $relsec_citation_detail);;
272
    $ref_key = $relsec_uuid . ($relsec_citation_detail ? '#' . $relsec_citation_detail : '');
273
    if (!isset($joined_refs[$ref_key])) {
274
      $joined_refs[$ref_key] = array(
275
        'order_by_key' => $relsec_citation_short,
276
        'markup' => $relsec_citation_short_markup // the footnote key will be appended later
277
      );
278
    }
279
  }
280

    
281
  if (!isset($misapplied[$name_dedup_key]['out'])) {
282
    // 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)))));
283
    $misapplied[$name_dedup_key]['out'] = cdm_tagged_text_to_markup($taxon_relation->taggedText);
284
  } else {
285
    // We need to add the anchors for all of the other misapplied names not
286
    // being rendered explicitly.
287
    $misapplied[$name_dedup_key]['out'] = uuid_anchor($taxon_relation->taxonUuid, $misapplied[$name_dedup_key]['out']);
288
  }
289

    
290
}
291

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

    
326
  // 'taxonRelationships';
327
  $footnoteListKey = NULL;
328

    
329
  $skip_render_template_keys = array();
330

    
331
  $is_invalid = false;
332

    
333

    
334
  switch ($reltype_uuid) {
335
    case UUID_HETEROTYPIC_SYNONYM_OF:
336
    case UUID_SYNONYM_OF:
337
      $relsign = $relsign_hetero;
338
      break;
339

    
340
    case UUID_HOMOTYPIC_SYNONYM_OF:
341
      $relsign = $relsign_homo;
342
      break;
343

    
344
    case UUID_MISAPPLIED_NAME_FOR:
345
      $skip_render_template_keys[] = 'nameAuthorPart';
346
      $is_invalid = true;
347
      $relsign = $relsign_invalid;
348

    
349
      break;
350

    
351
    default:
352
      $relsign = $relsign_invalid;
353
  }
354

    
355

    
356
  /*
357
  Names with status invalid or nudum are to be displayed with the
358
  $relsign_invalid, these names appear at the end of all names in their
359
  homotypic group (ordered correctly by the java cdm_lib).
360
  */
361
  if (isset($taxon->name->status) && is_array($taxon->name->status)) {
362
    foreach ($taxon->name->status as $status) {
363
      if (in_array($status->type->uuid , $nom_status_invalid_type_uuids)) {
364
        $relsign = $relsign_invalid;
365
        break;
366
      }
367
    }
368
  }
369

    
370
  // Now rendering starts ..
371
  RenderHints::pushToRenderStack('related_taxon');
372

    
373
  if (isset($taxon->name->nomenclaturalSource->citation)) {
374
    $referenceUri = url(path_to_reference($taxon->name->nomenclaturalSource->citation->uuid));
375
  }
376
  $taxonUri = '';
377

    
378
  // Printing the taxonName and the handling the special case of annotations.
379
  if (!isset($referenceUri)) {
380
    $referenceUri = FALSE;
381
  }
382
  $out_taxon_part = render_taxon_or_name($taxon, $taxonUri, $referenceUri, TRUE, FALSE, $skip_render_template_keys, $is_invalid);
383
  $name_relations = cdm_name_relationships_for_taxon($taxon);
384
  $name_relations_render_array = compose_name_relationships_inline($name_relations, $taxon->name->uuid, $taxon->uuid);
385

    
386
  $out = '<span class="relation_sign">' . $relsign . '</span>'
387
    . $out_taxon_part;
388
  if(isset($name_relations_render_array['list']['items'][0])){
389
    $out .= ' '  . drupal_render($name_relations_render_array);
390
  }
391

    
392
  $out = uuid_anchor($taxon->uuid, $out);
393

    
394
  RenderHints::popFromRenderStack();
395

    
396
  return $out;
397
}
398

    
399
/**
400
 * Creates markup for a taxon which is the accepted of another one
401
 *
402
 * @param $accepted_for_uuid
403
 *   The uuid of the accepted taxon
404
 */
405
function cdm_accepted_for($accepted_for_uuid) {
406

    
407
  if(!is_uuid($accepted_for_uuid)){
408
    return '';
409
  }
410

    
411
  RenderHints::pushToRenderStack('acceptedFor');
412
  $out = '';
413

    
414
  $synonym = cdm_ws_get(CDM_WS_PORTAL_TAXON, $accepted_for_uuid);
415
  if ($synonym) {
416
    $out .= '<span class="acceptedFor">';
417
    $out .= t('is accepted for ');
418
    $referenceUri = null;
419
    if (isset($synonym->name->nomenclaturalSource->citation)) {
420
      $referenceUri = url(path_to_reference($synonym->name->nomenclaturalSource->citation->uuid));
421
    }
422
    $out .= render_taxon_or_name($synonym->name, NULL, $referenceUri);
423
    RenderHints::setAnnotationsAndSourceConfig(synonymy_annotations_and_source_config());
424
    $annotations_and_sources = handle_annotations_and_sources($synonym);
425
    $out .= $annotations_and_sources->footNoteKeysMarkup();
426
    $out .= '</span>';
427
  }
428
  RenderHints::popFromRenderStack();
429
  return $out;
430
}
431

    
432
/**
433
 * Compose function for a list of taxa.
434
 *
435
 * This function is for used to:
436
 *
437
 * 1. Display search results
438
 * 2. List the taxa for a taxon name in the name page.
439
 *
440
 * @param $taxon_list array
441
 *   The list of CDM Taxon entities. e.g. The records array as contained in a pager object.
442
 * @param $freetext_search_results array
443
 * @param $show_classification boolean
444
 *
445
 * @ingroup compose
446
 */
447
function compose_list_of_taxa($taxon_list, $freetext_search_results = array(), $show_classification = false) {
448

    
449
  RenderHints::pushToRenderStack('list_of_taxa');
450

    
451
  $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SEARCH_GALLERY_NAME);
452

    
453
  $showMedia_taxa = $gallery_settings['cdm_dataportal_show_taxon_thumbnails'];
454
  $showMedia_synonyms = $gallery_settings['cdm_dataportal_show_synonym_thumbnails'];
455
  $searched_in_classification = cdm_dataportal_searched_in_classification();
456
  $searched_in_classification_uuid = null;
457
  if(isset($searched_in_classification->uuid)){
458
    $searched_in_classification_uuid = $searched_in_classification->uuid;
459
  }
460

    
461
  // .. Well, for sure not as performant as before, but better than nothing.
462
  $synonym_uuids = array();
463
  $misapplied_uuids = array();
464
  foreach ($taxon_list as $taxon) {
465
    if ($taxon->class == "Synonym") {
466
      if (!array_key_exists($taxon->uuid, $synonym_uuids)) {
467
        $synonym_uuids[$taxon->uuid] = $taxon->uuid;
468
      }
469
    }
470
    elseif (!_cdm_dataportal_acceptedByCurrentView($taxon)) {
471
      // Assuming that it is a misapplied name, will be further examined below.
472
      $misapplied_uuids[$taxon->uuid] = $taxon->uuid;
473
    }
474
  }
475

    
476
  // Batch service not jet implemented:
477
  // $table_of_accepted = cdm_ws_property(CDM_WS_PORTAL_TAXON_ACCEPTED,
478
  // join(',', $synonym_uuids));
479
  // thus ...
480
  $table_of_accepted = array();
481

    
482
  foreach ($synonym_uuids as $misapplication_uuid) {
483
    $classification_filter = '';
484
    if($searched_in_classification_uuid){
485
      $classification_filter = 'classificationFilter=' . $searched_in_classification_uuid;
486
    }
487
    $table_of_accepted[$misapplication_uuid] = cdm_ws_get(
488
      CDM_WS_PORTAL_TAXON_ACCEPTED,
489
      array($misapplication_uuid),
490
      $classification_filter
491
    );
492
  }
493

    
494
  foreach ($misapplied_uuids as $mispplication_uuid) {
495
    $taxonRelationsDTO = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS_DTO, array(
496
      $mispplication_uuid,
497
    ));
498
    if(isset($taxonRelationsDTO->relations)){
499
      foreach ($taxonRelationsDTO->relations as $relation) {
500
        if ($relation->type->uuid == UUID_MISAPPLIED_NAME_FOR && _cdm_dataportal_matches_current_view($relation->classificationsUUIDs)) {
501
          $table_of_accepted[$mispplication_uuid][] = cdm_ws_get(CDM_WS_TAXON, $relation->taxonUuid);
502
        }
503
      }
504
    }
505
  }
506

    
507
  $out = '<div class="cdm-item-list" style="background-image: none;">';
508
  $itemCnt = -1;
509
  foreach ($taxon_list as $taxon) {
510
    $itemCnt++;
511

    
512
    if (isset($table_of_accepted[$taxon->uuid])) {
513
      // Its a synonym or misapplied name.
514
      $is_synonym = isset($synonym_uuids[$taxon->uuid]); //TODO better use the $taxon->class attribute?
515
      $taxon_type = $is_synonym ? TAXON_TYPE_SYNONYM :TAXON_TYPE_MISAPPLIEDNAME;
516

    
517
      $acceptedTaxa = $table_of_accepted[$taxon->uuid];
518

    
519
      if (!is_array($acceptedTaxa)) {
520
        $acceptedTaxa = array($acceptedTaxa);
521
      }
522

    
523
      foreach ($acceptedTaxa as $acceptedTaxon) {
524
        if (is_object($acceptedTaxon)) {
525

    
526
          $taxonUri = uri_to_synonym($taxon->uuid, $acceptedTaxon->uuid);
527
          $referenceUri = '';
528
          if (isset($acceptedTaxon->name->nomenclaturalSource->citation)) {
529
            $referenceUri = url(path_to_reference($acceptedTaxon->name->nomenclaturalSource->citation->uuid));
530
          }
531
          $skip_render_template_parts = $is_synonym ? ['secReferencePart'] : [];
532
          // $taxon_or_name this is a trick to suppress the sec reference for synonyms
533
          // supplying the name will cause render_taxon_or_name() to not show the sec reference
534
          $out .= "<div class=\"item " . $taxon_type . "\">" . render_taxon_or_name($taxon, $taxonUri, $referenceUri, true, false, $skip_render_template_parts);
535
          if($taxon_type == TAXON_TYPE_MISAPPLIEDNAME && isset_not_empty($taxon->secSource) && isset_not_empty($taxon->secSource->citation)) {
536
            if (isset($taxon->secSource->citation->authorship)) {
537
              $authorship = $taxon->secSource->citation->authorship->titleCache;
538
            }
539
            else {
540
              $authorship = $taxon->secSource->citation->titleCache;
541
            }
542
            $out .= ' sensu ' . $authorship;
543
          }
544
          if ($show_classification) {
545
            $out .= render_classifications_for_taxon($taxon);
546
          }
547
          if ($showMedia_synonyms) {
548
            $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $acceptedTaxon));
549
          }
550
        }
551
      }
552
    }
553
    else {
554
      // Its a Taxon.
555
      $taxonUri = url(path_to_taxon($taxon->uuid));
556
      $referenceUri = '';
557
      if (isset($taxon->name->nomenclaturalSource->citation)) {
558
        $referenceUri = url(path_to_reference($taxon->name->nomenclaturalSource->citation->uuid));
559
      }
560
      $out .= '<div class="item Taxon">' . render_taxon_or_name($taxon, $taxonUri, $referenceUri);
561
      if ($show_classification) {
562
        $out .= render_classifications_for_taxon($taxon);
563
      }
564
      if ($showMedia_taxa) {
565
        $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $taxon));
566
      }
567
    }
568

    
569
    /*
570
     * the score field will be empty in case of MultiTermQueries like
571
     * WildcardQueries, since these are  constant score by default
572
     * since Lucene 2.9
573
     */
574
    if(isset($freetext_search_results[$itemCnt]) && $freetext_search_results[$itemCnt]->score && $freetext_search_results[$itemCnt]->maxScore){
575
      $percentage =  ( $freetext_search_results[$itemCnt]->score / $freetext_search_results[$itemCnt]->maxScore ) * 100;
576
      $out .= '<div class="score-bar"><div class="score-bar-indicator" style="width:' . $percentage .'% "></div></div>';
577
      $out .= '<div class="score-bar-value">' . number_format($percentage, 2) .'%</div>';
578
    }
579

    
580
    // Render highlighted fragments, these are made available by free text
581
    // searches.
582
    if (isset($freetext_search_results[$itemCnt]->fieldHighlightMap)) {
583
      $field_fragments = (array) $freetext_search_results[$itemCnt]->fieldHighlightMap;
584
      if (count($field_fragments) > 0) {
585
        $fragments_out = '';
586
        foreach ($field_fragments as $fieldName => $fragments) {
587
          $fragments_out .= '... <span class="' . $fieldName. '">' . filter_xss(join(" ... ", $fragments), array('b') ) . '</span>';
588
        }
589
        $out .= '<div class="fragment_highlight">' . $fragments_out . ' ...</div>';
590
      }
591
    }
592

    
593
    $out .= '</div>';
594
  }
595

    
596
  $out .= '</div>';
597
  RenderHints::popFromRenderStack();
598

    
599
  return markup_to_render_array($out); // TODO create render array of all list items in function
600
}
601

    
602

    
603
function render_classifications_for_taxon($taxon) {
604
  $unclassified_snippet = '<span class="unclassified">' . t('unclassified') . '</span>';
605
  $classifications = get_classifications_for_taxon($taxon);
606
  $classification_titles = array();
607
  foreach ($classifications as $classification) {
608
    if (isset($classification->titleCache)) {
609
      $classification_titles[] = $classification->titleCache;
610
    }
611
  }
612
  if (count($classification_titles) == 0) {
613
    $classification_titles[] = $unclassified_snippet;
614
  }
615
  return '<span class="classifications"><span class="separator"> : </span>' . implode(', ', $classification_titles) . '</span>';
616
}
617

    
618
/**
619
 * Compose function for the taxonomic children
620
 *
621
 * @param $taxon_uuid
622
 *    The uuuid of the taxon to compose the list of taxonomic children for
623
 * @return
624
 *   A drupal render array.
625
 *
626
 * @ingroup compose
627
 */
628
function compose_taxonomic_children($taxon_uuid){
629

    
630
  $render_array = array();
631
  
632
  if($taxon_uuid) {
633
    $children = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY_CHILDNODES_OF_TAXON, array(
634
      get_current_classification_uuid(),
635
      $taxon_uuid
636
      ));
637
    if($children){
638
      $taxonomic_children = theme('cdm_taxontree', array('tree' => $children));
639
      $render_array = markup_to_render_array($taxonomic_children);
640
    }
641
  }
642
  return $render_array;
643
}
644

    
(15-15/16)