Project

General

Profile

Download (26.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_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 = theme('cdm_reference', array(
147
          'reference' => $sensu_reference,
148
          'microReference' => NULL,
149
          'doLink' => false,
150
        ));
151
        $footnote_key = FootnoteManager::addNewFootnote($footnote_list_key, $sensu_reference_markup);
152
        $footnote_key = theme('cdm_footnote_key', array('footnoteKey' => $footnote_key));
153
        $joined_refs[$ref_key]['markup'] = '<span class="sensu">' . $sensu_strings['markup'] . $footnote_key . '</span>';
154
      }
155
    }
156
  }
157

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

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

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

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

    
216
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => $footnote_list_key, 'enclosingTag' => 'li'));
217
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnote_list_key, 'enclosingTag' => 'li'));
218

    
219
  $out .= '<ul class="footnotes">' . $footnotes . '</ul>';
220
  $out .= '</div>';
221

    
222
  RenderHints::popFromRenderStack();
223
  return $out;
224
}
225

    
226
/**
227
 * @param $taxon_relation
228
 * @param $misapplied
229
 * @param $joined_refs
230
 * @param $relationship_type_weights
231
 * @param $name_dedup_key
232
 */
233
function cdm_taxonRelationships_process_relationship_dto($taxon_relation, &$misapplied, &$joined_refs, $relationship_type_weights, $name_dedup_key) {
234

    
235
  $appended_phrase_text = join(' ', cdm_tagged_text_values($taxon_relation->taggedText, array('appendedPhrase')));
236
  // remove/extract appendedPhrase, secReference, relSecReference and add placeholders if needed
237
  tagged_text_extract($taxon_relation->taggedText, 'appendedPhrase', true);
238
  $sensu_tagged_text = tagged_text_extract_reference_and_detail($taxon_relation->taggedText, "secReference", true);
239
  $relsec_tagged_text = tagged_text_extract_reference_and_detail($taxon_relation->taggedText, "relSecReference", true);
240

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

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

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

    
289
  if (!isset($misapplied[$name_dedup_key]['out'])) {
290
    // 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)))));
291
    $misapplied[$name_dedup_key]['out'] = cdm_tagged_text_to_markup($taxon_relation->taggedText);
292
  } else {
293
    // We need to add the anchors for all of the other misapplied names not
294
    // being rendered explicitly.
295
    $misapplied[$name_dedup_key]['out'] = uuid_anchor($taxon_relation->taxonUuid, $misapplied[$name_dedup_key]['out']);
296
  }
297

    
298
}
299

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

    
334
  // 'taxonRelationships';
335
  $footnoteListKey = NULL;
336

    
337
  $skip_tags = array();
338

    
339
  $is_invalid = false;
340

    
341
  if (!$relsign) {
342

    
343
    switch ($reltype_uuid) {
344
      case UUID_HETEROTYPIC_SYNONYM_OF:
345
      case UUID_SYNONYM_OF:
346
        $relsign = $relsign_hetero;
347
        break;
348

    
349
      case UUID_HOMOTYPIC_SYNONYM_OF:
350
        $relsign = $relsign_homo;
351
        break;
352

    
353
      case UUID_MISAPPLIED_NAME_FOR:
354
      case UUID_INVALID_DESIGNATION_FOR:
355
        $skip_tags[] = 'authors';
356
        $is_invalid = true;
357
        $relsign = $relsign_invalid;
358

    
359
        break;
360

    
361
      default:
362
        $relsign = $relsign_invalid;
363
    }
364

    
365
  }
366

    
367
  if($doubtful) {
368
    $relsign = '?' . $relsign;
369
  }
370

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

    
385
  // Now rendering starts ..
386
  RenderHints::pushToRenderStack('related_taxon');
387

    
388
  if (isset($taxon->name->nomenclaturalReference)) {
389
    $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
390
  }
391
  $taxonUri = '';
392
  if ($doLinkTaxon) {
393
    $taxonUri = url(path_to_taxon($taxon->uuid, "synonymy"));
394
  }
395
  // Printing the taxonName and the handling the special case of annotations.
396
  if (!isset($referenceUri)) {
397
    $referenceUri = FALSE;
398
  }
399
  $out_taxon_part = render_taxon_or_name($taxon, $taxonUri, $referenceUri, TRUE, FALSE, $skip_tags, $is_invalid);
400
  $taxon_footnotes = theme('cdm_annotations_as_footnotekeys',
401
    array('cdmBase_list' => array(
402
      $taxon->name,
403
      $taxon,
404
    ),
405
      'footnote_list_key' => $footnoteListKey)
406
  );
407

    
408

    
409
  $name_relations = cdm_name_relationships_for_taxon($taxon);
410
  $name_relations_render_array = compose_name_relationships_inline($name_relations, $taxon->name->uuid, $taxon->uuid);
411

    
412
  $out = '<span class="relation_sign" title="' . $reltype_representation . '">' . $relsign . '</span>'
413
    . $out_taxon_part . $taxon_footnotes;
414
  if(isset($name_relations_render_array['list']['items'][0])){
415
    $out .= ' '  . drupal_render($name_relations_render_array);
416
  }
417

    
418
  $out = uuid_anchor($taxon->uuid, $out);
419

    
420
  RenderHints::popFromRenderStack();
421

    
422
  return $out;
423
}
424

    
425

    
426
/**
427
 * Creates markup for a taxon which is the accepted of another one
428
 *
429
 * @param $accepted_for_uuid
430
 *   The uuid of the accepted taxon
431
 */
432
function cdm_accepted_for($accepted_for_uuid) {
433

    
434
  if(!is_uuid($accepted_for_uuid)){
435
    return '';
436
  }
437

    
438
  RenderHints::pushToRenderStack('acceptedFor');
439
  $out = '';
440

    
441
  $synonym = cdm_ws_get(CDM_WS_PORTAL_TAXON, $accepted_for_uuid);
442
  if ($synonym) {
443
    $out .= '<span class="acceptedFor">';
444
    $out .= t('is accepted for ');
445
    $referenceUri = null;
446
    if (isset($synonym->name->nomenclaturalReference)) {
447
      $referenceUri = url(path_to_reference($synonym->name->nomenclaturalReference->uuid));
448
    }
449
    $out .= render_taxon_or_name($synonym->name, NULL, $referenceUri);
450
    $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $synonym));
451
    $out .= '</span>';
452
  }
453
  RenderHints::popFromRenderStack();
454
  return $out;
455
}
456

    
457
/**
458
 * Compose function for a list of taxa.
459
 *
460
 * This function is for used to:
461
 *
462
 * 1. Display search results
463
 * 2. List the taxa for a taxon name in the name page.
464
 *
465
 * @param $taxon_list array
466
 *   The list of CDM Taxon entities. e.g. The records array as contained in a pager object.
467
 * @param $freetext_search_results array
468
 * @param $show_classification boolean
469
 *
470
 * @ingroup compose
471
 *
472
 */
473
function compose_list_of_taxa($taxon_list, $freetext_search_results = array(), $show_classification = false) {
474

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

    
477
  RenderHints::pushToRenderStack('list_of_taxa');
478

    
479
  $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SEARCH_GALLERY_NAME);
480

    
481
  $showMedia_taxa = $gallery_settings['cdm_dataportal_show_taxon_thumbnails'];
482
  $showMedia_synonyms = $gallery_settings['cdm_dataportal_show_synonym_thumbnails'];
483
  $searched_in_classification = cdm_dataportal_searched_in_classification();
484
  $searched_in_classification_uuid = null;
485
  if(isset($searched_in_classification->uuid)){
486
    $searched_in_classification_uuid = $searched_in_classification->uuid;
487
  }
488

    
489
  // .. Well, for sure not as performant as before, but better than nothing.
490
  $synonym_uuids = array();
491
  $misappied_uuids = array();
492
  foreach ($taxon_list as $taxon) {
493
    if ($taxon->class == "Synonym") {
494
      if (!array_key_exists($taxon->uuid, $synonym_uuids)) {
495
        $synonym_uuids[$taxon->uuid] = $taxon->uuid;
496
      }
497
    }
498
    elseif (!_cdm_dataportal_acceptedByCurrentView($taxon)) {
499
      // Assuming that it is a misappied name, will be further examined below.
500
      $misappied_uuids[$taxon->uuid] = $taxon->uuid;
501
    }
502
  }
503

    
504
  // Batch service not jet implemented:
505
  // $table_of_accepted = cdm_ws_property(CDM_WS_PORTAL_TAXON_ACCEPTED,
506
  // join(',', $synonym_uuids));
507
  // thus ...
508
  $table_of_accepted = array();
509

    
510
  foreach ($synonym_uuids as $relatedUuid) {
511
    $classification_filter = '';
512
    if($searched_in_classification_uuid){
513
      $classification_filter = 'classificationFilter=' . $searched_in_classification_uuid;
514
    }
515
    $table_of_accepted[$relatedUuid] = cdm_ws_get(
516
      CDM_WS_PORTAL_TAXON_ACCEPTED,
517
      array($relatedUuid),
518
      $classification_filter
519
    );
520
  }
521

    
522
  foreach ($misappied_uuids as $relatedUuid) {
523
    $taxonRelations = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS, array(
524
      $relatedUuid,
525
    ));
526
    foreach ($taxonRelations as $relation) {
527
      if ($relation->type->uuid == UUID_MISAPPLIED_NAME_FOR && _cdm_dataportal_acceptedByCurrentView($relation->toTaxon)) {
528
        $table_of_accepted[$relatedUuid][] = $relation->toTaxon;
529
      }
530
    }
531
  }
532

    
533
  $out = '<div class="cdm-item-list" style="background-image: none;">';
534
  $itemCnt = -1;
535
  foreach ($taxon_list as $taxon) {
536
    $itemCnt++;
537
    if (isset($table_of_accepted[$taxon->uuid])) {
538
      // Its a synonym or misapplied name.
539
      $is_synonym = isset($synonym_uuids[$taxon->uuid]); //TODO better use the $taxon->class attribute?
540
      $taxon_type = $is_synonym ? TAXON_TYPE_SYNONYM :TAXON_TYPE_MISAPPLIEDNAME;
541

    
542
      $acceptedTaxa = $table_of_accepted[$taxon->uuid];
543

    
544
      if (!is_array($acceptedTaxa)) {
545
        $acceptedTaxa = array($acceptedTaxa);
546
      }
547

    
548
      foreach ($acceptedTaxa as $acceptedTaxon) {
549
        if (is_object($acceptedTaxon)) {
550

    
551
          $taxonUri = uri_to_synonym($taxon->uuid, $acceptedTaxon->uuid);
552
          $referenceUri = '';
553
          if (isset($acceptedTaxon->name->nomenclaturalReference)) {
554
            $referenceUri = url(path_to_reference($acceptedTaxon->name->nomenclaturalReference->uuid));
555
          }
556
          $taxon_or_name = $is_synonym ? $taxon->name : $taxon;
557
          // $taxon_or_name this is a trick to suppress the sec reference for synonyms
558
          // supplying the name will cause render_taxon_or_name() to not show the sec reference
559
          $out .= "<div class=\"item " . $taxon_type . "\">" . render_taxon_or_name($taxon_or_name, $taxonUri, $referenceUri);
560
          if($taxon_type == TAXON_TYPE_MISAPPLIEDNAME && is_object($taxon->sec)){
561
            if(isset($taxon->sec->authorship)){
562
              $authorship = $taxon->sec->authorship->titleCache;
563
            } else {
564
              $authorship = $taxon->sec->titleCache;
565
            }
566
            $out .=  ' sensu ' . $authorship;
567
          }
568
          if ($show_classification) {
569
            $classifications = get_classifications_for_taxon($taxon);
570
            $classification_titles = array();
571
            foreach ($classifications as $classification) {
572
              if (isset($classification->titleCache)) {
573
                $classification_titles[] = $classification->titleCache;
574
              }
575
            }
576
            if (count($classification_titles) == 0) {
577
              $classification_titles[] = $unclassified_snippet;
578
            }
579
            $out .= '<span class="classifications"><span class="separator"> : </span>' . implode(', ', $classification_titles) . '</span>';
580
          }
581
          $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
582
          if ($showMedia_synonyms) {
583
            $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $acceptedTaxon));
584
          }
585
        }
586
      }
587
    }
588
    else {
589
      // Its a Taxon.
590
      $taxonUri = url(path_to_taxon($taxon->uuid));
591
      $referenceUri = '';
592
      if (isset($taxon->name->nomenclaturalReference)) {
593
        $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
594
      }
595
      $out .= '<div class="item Taxon">' . render_taxon_or_name($taxon, $taxonUri, $referenceUri);
596
      if ($show_classification) {
597
        $classifications = get_classifications_for_taxon($taxon);
598
        $classification_titles = array();
599
        foreach ($classifications as $classification) {
600
          if (isset($classification->titleCache)) {
601
            $classification_titles[] = $classification->titleCache;
602
          }
603
        }
604
        if(count($classification_titles) == 0){
605
          $classification_titles[] = $unclassified_snippet;
606
        }
607
        $out .= '<span class="classifications"><span class="separator"> : </span>' . implode(', ', $classification_titles) . '</span>';
608
      }
609
      $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
610

    
611
      if ($showMedia_taxa) {
612
        $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $taxon));
613
      }
614
    }
615

    
616
    /*
617
     * the score field will be empty in case of MultiTermQueries like
618
     * WildcardQueries, since these are  constant score by default
619
     * since Lucene 2.9
620
     */
621
    if(isset($freetext_search_results[$itemCnt]) && $freetext_search_results[$itemCnt]->score && $freetext_search_results[$itemCnt]->maxScore){
622
      $percentage =  ( $freetext_search_results[$itemCnt]->score / $freetext_search_results[$itemCnt]->maxScore ) * 100;
623
      $out .= '<div class="score-bar"><div class="score-bar-indicator" style="width:' . $percentage .'% "></div></div>';
624
      $out .= '<div class="score-bar-value">' . number_format($percentage, 2) .'%</div>';
625
    }
626

    
627
    // Render highlighted fragments, these are made available by free text
628
    // searches.
629
    if (isset($freetext_search_results[$itemCnt]->fieldHighlightMap)) {
630
      $field_fragments = (array) $freetext_search_results[$itemCnt]->fieldHighlightMap;
631
      if (count($field_fragments) > 0) {
632
        $fragments_out = '';
633
        foreach ($field_fragments as $fieldName => $fragments) {
634
          $fragments_out .= '... <span class="' . $fieldName. '">' . filter_xss(join(" ... ", $fragments), array('b') ) . '</span>';
635
        }
636
        $out .= '<div class="fragment_highlight">' . $fragments_out . ' ...</div>';
637
      }
638
    }
639

    
640
    $out .= '</div>';
641
  }
642

    
643
  $out .= '</div>';
644
  RenderHints::popFromRenderStack();
645

    
646
  return markup_to_render_array($out); // TODO create render array of all list items in function
647
}
648

    
649

    
650
/**
651
 * Compose function for the taxonomic children
652
 *
653
 * @param $taxon_uuid
654
 *    The uuuid of the taxon to compose the list of taxonomic children for
655
 * @return
656
 *   A drupal render array.
657
 *
658
 * @ingroup compose
659
 */
660
function compose_taxonomic_children($taxon_uuid){
661

    
662
  $render_array = array();
663
  
664
  if($taxon_uuid) {
665
    $children = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY_CHILDNODES_OF_TAXON, array(
666
      get_current_classification_uuid(),
667
      $taxon_uuid
668
      ));
669
    if($children){
670
      $taxonomic_children = theme('cdm_taxontree', array('tree' => $children));
671
      $render_array = markup_to_render_array($taxonomic_children);
672
    }
673
  }
674
  return $render_array;
675
}
676

    
(10-10/11)