Project

General

Profile

Download (24.1 KB) Statistics
| Branch: | Tag: | Revision:
1 b7a20282 Andreas Kohlbecker
<?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 eebf415b Andreas Kohlbecker
define('TAXON_TYPE_SYNONYM', 'Synonym');
35
define('TAXON_TYPE_MISAPPLIEDNAME', 'misapplied-name');
36 b7a20282 Andreas Kohlbecker
37
/**
38
 * Returns HTML for misapplied names and invalid designations.
39
 *
40
 * Both relation types are currently treated the same!
41
 *
42 116fb348 Andreas Kohlbecker
 * @param object taxonRelationships
43
 *   A TaxonRelationshipsDTO, see taxon/{uuid}/taxonRelationshipsDTO
44
 *
45
 * @param object focusedTaxon
46 f659b3dd Andreas Kohlbecker
 *  The taxon being in the focus of the application
47 b7a20282 Andreas Kohlbecker
 *
48
 * @return string
49
 *    the rendered html
50
 */
51 116fb348 Andreas Kohlbecker
function cdm_taxonRelationships($taxonRelationshipsDTO, $focusedTaxon){
52 b7a20282 Andreas Kohlbecker
53 116fb348 Andreas Kohlbecker
  if (!$taxonRelationshipsDTO || $taxonRelationshipsDTO->size < 1) {
54 b7a20282 Andreas Kohlbecker
    return null;
55
  }
56
57 df6226aa Andreas Kohlbecker
  RenderHints::pushToRenderStack('taxon_relationships');
58 83dc60b9 Andreas Kohlbecker
  $footnote_list_key = 'taxon_relationships';
59
  RenderHints::setFootnoteListKey($footnote_list_key);
60 b7a20282 Andreas Kohlbecker
61
  $misapplied = array();
62 67c8cca9 Andreas Kohlbecker
  $joined_refs = array();
63 b7a20282 Andreas Kohlbecker
64 f659b3dd Andreas Kohlbecker
  $taxon_relationship_types = variable_get(CDM_TAXON_RELATIONSHIP_TYPES, unserialize(CDM_TAXON_RELATIONSHIP_TYPES_DEFAULT));
65 b7a20282 Andreas Kohlbecker
66
  // Aggregate misapplied names having the same fullname:
67 763b610c Andreas Kohlbecker
  //  - deduplicate misapplied names, so that the same name it not shown multiple times in case it
68 a9dc4b35 Andreas Kohlbecker
  //    the duplicates only have different sensu references with detail and appended phrase (see #5647)
69 83dc60b9 Andreas Kohlbecker
  //  - show only the author team as short citation for the sec reference
70
  //  - show the according reference as footnote to this short citation
71 763b610c Andreas Kohlbecker
  //
72
  // Example:
73
  // "Xenoxylum foobar" sensu Grumbach¹; sensu Lem²
74
  //    1. Novel marsian species, Grumbach, 2022
75
  //    2. Flora solaris, Lem, 2019
76 f659b3dd Andreas Kohlbecker
  if (isset($taxonRelationshipsDTO->relations[0])) {
77
    foreach($taxonRelationshipsDTO->relations as $taxon_relation){
78 b7a20282 Andreas Kohlbecker
79 f659b3dd Andreas Kohlbecker
      if (in_array($taxon_relation->type->uuid, $taxon_relationship_types)) {
80 b7a20282 Andreas Kohlbecker
81 f659b3dd Andreas Kohlbecker
        if ($taxon_relation->type->uuid == UUID_MISAPPLIED_NAME_FOR || $taxon_relation->type->uuid == UUID_INVALID_DESIGNATION_FOR) {
82 b7a20282 Andreas Kohlbecker
83 116fb348 Andreas Kohlbecker
          RenderHints::pushToRenderStack('misapplied_name_for'); // TODO the render path string should in future come from $taxonRelation->type->...
84 df6226aa Andreas Kohlbecker
85 a9dc4b35 Andreas Kohlbecker
          // full name with relation symbol, rel sec as de-duplication key
86 439f531a Andreas Kohlbecker
          // the sensu part will be removed from the key below in case it is present
87
          $name_text = join(' ', cdm_tagged_text_values($taxon_relation->taggedText, array('name')));
88 23ebb91f Andreas Kohlbecker
          $full_name_key = cdm_tagged_text_to_string($taxon_relation->taggedText, array('symbol', 'appendedPhrase'));
89 a9dc4b35 Andreas Kohlbecker
          $symbol_text = join(' ', cdm_tagged_text_values($taxon_relation->taggedText, array('symbol')));
90 23ebb91f Andreas Kohlbecker
          $full_name_key = $name_text . ' ' . str_replace($name_text, '', $full_name_key) . ' ' . $symbol_text;
91
92 a9dc4b35 Andreas Kohlbecker
          cdm_taxonRelationships_process_relationship_dto($taxon_relation, $misapplied, $joined_refs, $full_name_key);
93 116fb348 Andreas Kohlbecker
94 a9dc4b35 Andreas Kohlbecker
          RenderHints::popFromRenderStack();
95 23ebb91f Andreas Kohlbecker
        } else {
96
          RenderHints::pushToRenderStack('other_taxon_relationship');
97
          // All relationship types except misapplied_name_for and invalid_designation_for.
98 d961cf30 Andreas Kohlbecker
99 18c71cd1 Andreas Kohlbecker
          $taxon_relationships_lines[] = cdm_tagged_text_to_markup($taxon_relation->taggedText);
100 a9dc4b35 Andreas Kohlbecker
101
          RenderHints::popFromRenderStack();
102 b7a20282 Andreas Kohlbecker
        }
103
      }
104 83dc60b9 Andreas Kohlbecker
    } // END loop over $taxonRelationshipsDTO->relations
105 a9dc4b35 Andreas Kohlbecker
106 b7a20282 Andreas Kohlbecker
  }
107
108 67c8cca9 Andreas Kohlbecker
  // Sort the $joined_refs and create footnotes and footnote keys.
109
  uasort($joined_refs, function($a, $b) {
110
    if ($a['order_by_key'] == $b['order_by_key']) {
111 83dc60b9 Andreas Kohlbecker
      return 0;
112 763b610c Andreas Kohlbecker
    }
113 67c8cca9 Andreas Kohlbecker
    return ($a['order_by_key'] < $b['order_by_key']) ? -1 : 1;
114 83dc60b9 Andreas Kohlbecker
  });
115
116
117 23ebb91f Andreas Kohlbecker
  foreach ($joined_refs as $ref_key => $sensu_strings) {
118 83dc60b9 Andreas Kohlbecker
    if(!empty($sensu_strings)) {
119 23ebb91f Andreas Kohlbecker
      $ref_key_tokens = explode('#', $ref_key);
120
      $sensu_uuid = $ref_key_tokens[0];
121 83dc60b9 Andreas Kohlbecker
      $sensu_reference = cdm_ws_get(CDM_WS_REFERENCE, $sensu_uuid);
122
      if(!$sensu_reference){
123
        drupal_set_message("Problem fetching sensu reference with uuid " . $sensu_uuid, 'error');
124
      }
125 67c8cca9 Andreas Kohlbecker
      if($sensu_strings['order_by_key'] != $sensu_reference->titleCache){
126
        $footnote_key = FootnoteManager::addNewFootnote($footnote_list_key, $sensu_reference->titleCache);
127
        $footnote_key = theme('cdm_footnote_key', array('footnoteKey' => $footnote_key));
128 23ebb91f Andreas Kohlbecker
        $joined_refs[$ref_key]['markup'] = '<span class="sensu">' . $sensu_strings['markup'] . $footnote_key . '</span>';
129 67c8cca9 Andreas Kohlbecker
      }
130 763b610c Andreas Kohlbecker
    }
131 b7a20282 Andreas Kohlbecker
  }
132
133
  // ---- Generate output ---- //
134
  $out = '<div class="taxon-relationships">';
135 23ebb91f Andreas Kohlbecker
  if (count($misapplied) > 0) {
136 83dc60b9 Andreas Kohlbecker
    ksort($misapplied); // order the misapplied by scientific name
137 b7a20282 Andreas Kohlbecker
    $out .= '<ul class="misapplied">';
138 83dc60b9 Andreas Kohlbecker
139 23ebb91f Andreas Kohlbecker
    // create the list entries per misapplied name
140 b7a20282 Andreas Kohlbecker
    foreach ($misapplied as $misapplied_name) {
141 83dc60b9 Andreas Kohlbecker
      $misapplied_name_markup = $misapplied_name['out'];
142 23ebb91f Andreas Kohlbecker
      // all sensu and auct. for this MAN
143 83dc60b9 Andreas Kohlbecker
      if (isset($misapplied_name['sensu_uuids'])) {
144
        $sensu_refs_with_fkey = array();
145 23ebb91f Andreas Kohlbecker
        foreach ($misapplied_name['sensu_uuids'] as $sensu_data) {
146
          if($sensu_data['uuid']){
147
            $joined_ref_key = $sensu_data['uuid'] . ($sensu_data['citation_detail'] ? '#' .  $sensu_data['citation_detail'] : '');
148
            $sensu_refs_with_fkey[$sensu_data['prefix'] . $joined_refs[$joined_ref_key]['order_by_key']] = $sensu_data['prefix'] . $joined_refs[$joined_ref_key]['markup'];
149
          } else {
150
            $sensu_refs_with_fkey[$sensu_data['prefix']] = $sensu_data['prefix'];
151
          }
152 83dc60b9 Andreas Kohlbecker
        }
153
        ksort($sensu_refs_with_fkey);
154
        $sensu_refs_with_fkey_markup = join('; ', $sensu_refs_with_fkey);
155 23ebb91f Andreas Kohlbecker
        $misapplied_name_markup = str_replace('{PLACEHOLDER_secReference}', $sensu_refs_with_fkey_markup, $misapplied_name_markup);
156 83dc60b9 Andreas Kohlbecker
      }
157 b7a20282 Andreas Kohlbecker
158 23ebb91f Andreas Kohlbecker
      // append the err. sec. if there is any for this MAN
159 83dc60b9 Andreas Kohlbecker
      if (isset($misapplied_name['relsec_uuids'])) {
160
        $relsec_refs_with_fkey = array();
161 a9dc4b35 Andreas Kohlbecker
        foreach ($misapplied_name['relsec_uuids'] as $relsec_data) {
162
          if($relsec_data['uuid']) {
163
            $relsec_refs_with_fkey[$relsec_data['prefix'] . $joined_refs[$relsec_data['uuid']]['order_by_key']] = $relsec_data['prefix'] . $joined_refs[$relsec_data['uuid']]['markup'];
164
          }
165 b7a20282 Andreas Kohlbecker
        }
166 83dc60b9 Andreas Kohlbecker
        ksort($relsec_refs_with_fkey);
167
        $relsec_refs_with_fkey_markup = join('; ', $relsec_refs_with_fkey);
168 d961cf30 Andreas Kohlbecker
        $misapplied_name_markup = str_replace('{PLACEHOLDER_relSecReference}', $relsec_refs_with_fkey_markup, $misapplied_name_markup);
169 b7a20282 Andreas Kohlbecker
      }
170 23ebb91f Andreas Kohlbecker
      // final line
171
      $out .= '<li class="synonym"><span class="misapplied">' . $misapplied_name_markup . ' </span></li>';
172 b7a20282 Andreas Kohlbecker
    }
173
    $out .= '</ul>';
174
  }
175
176
  if (isset($taxon_relationships_lines) && is_array($taxon_relationships_lines) && count($taxon_relationships_lines) > 0) {
177
    $out .= '<ul class="taxonRelationships">';
178
    foreach ($taxon_relationships_lines as $taxon_relationship_line) {
179
      $out .= '<li class="synonym">' . $taxon_relationship_line . '</li>';
180
    }
181
    $out .= '</ul>';
182
  }
183
184 83dc60b9 Andreas Kohlbecker
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => $footnote_list_key, 'enclosingTag' => 'li'));
185
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnote_list_key, 'enclosingTag' => 'li'));
186 b7a20282 Andreas Kohlbecker
187
  $out .= '<ul class="footnotes">' . $footnotes . '</ul>';
188
  $out .= '</div>';
189
190
  RenderHints::popFromRenderStack();
191
  return $out;
192
}
193
194 a9dc4b35 Andreas Kohlbecker
/**
195
 * @param $taxon_relation
196
 * @param $name_dedup_key
197
 * @param $misapplied
198
 * @param $joined_refs
199
200
 */
201
function cdm_taxonRelationships_process_relationship_dto($taxon_relation, &$misapplied, &$joined_refs, $name_dedup_key = null) {
202
  $appended_phrase_text = join(' ', cdm_tagged_text_values($taxon_relation->taggedText, array('appendedPhrase')));
203
  // remove/extract appendedPhrase, secReference, relSecReference and add placeholders if needed
204
  tagged_text_extract($taxon_relation->taggedText, 'appendedPhrase');
205
  $sensu_tagged_text = tagged_text_extract_secref($taxon_relation->taggedText, "secReference", true);
206
  $relsec_tagged_text = tagged_text_extract_secref($taxon_relation->taggedText, "relSecReference", true);
207
208
  if (isset($sensu_tagged_text[1])) {
209
    // for de-duplication everything else needs to be equal except for appendedPhrase + MAN.sec + MAN.secDetail. see #7658#note-21
210
    $name_dedup_key = str_replace(cdm_tagged_text_to_string($sensu_tagged_text), ' ', $name_dedup_key);
211
    $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
212
    $sensu_citation_detail = trim(join(' ', cdm_tagged_text_values($sensu_tagged_text, array('secMicroReference'))));
213
    $sensu_citation_short_markup = cdm_tagged_text_to_markup($sensu_tagged_text);
214
    $sensu_citation_short = cdm_tagged_text_to_string($sensu_tagged_text);
215
    $sensu_uuid = $sensu_tagged_text[0]->entityReference->uuid;
216
    $name_dedup_key = preg_replace('/\s+/', ' ', $name_dedup_key); // sanitize multiple whitespace characters
217
    $misapplied[$name_dedup_key]['sensu_uuids'][] = array('uuid' => $sensu_uuid, 'prefix' => $appended_phrase_text, 'citation_detail' => $sensu_citation_detail);
218
    $ref_key = $sensu_uuid . ($sensu_citation_detail ? '#' . $sensu_citation_detail : '');
219
    if (!isset($joined_refs[$ref_key])) {
220
      $joined_refs[$ref_key] = array(
221
        'order_by_key' => $sensu_citation_short,
222
        'markup' => $sensu_citation_short_markup // the footnote key will be appended later
223
      );
224
    }
225
  } else if ($appended_phrase_text) {
226
    // appended phrase without reference
227
    $name_dedup_key = preg_replace('/\s+/', ' ', $name_dedup_key); // sanitize multiple whitespace characters
228
    $misapplied[$name_dedup_key]['sensu_uuids'][] = array('uuid' => null, 'prefix' => $appended_phrase_text);
229
  }
230
231
  if (isset($relsec_tagged_text[1])) {
232
    $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
233
    $relsec_citation_detail = trim(join(' ', cdm_tagged_text_values($relsec_tagged_text, array('secMicroReference'))));
234
    $relsec_citation_short_markup = cdm_tagged_text_to_markup($relsec_tagged_text);
235
    $relsec_citation_short = cdm_tagged_text_to_string($relsec_tagged_text);
236
    $relsec_uuid = $relsec_tagged_text[0]->entityReference->uuid;
237
    $misapplied[$name_dedup_key]['relsec_uuids'][] = array('uuid' => $relsec_uuid, 'prefix' => $appended_phrase_text, 'citation_detail' => $relsec_citation_detail);;
238
    $ref_key = $relsec_uuid . ($relsec_citation_detail ? '#' . $relsec_citation_detail : '');
239
    if (!isset($joined_refs[$ref_key])) {
240
      $joined_refs[$ref_key] = array(
241
        'order_by_key' => $relsec_citation_short,
242
        'markup' => $relsec_citation_short_markup // the footnote key will be appended later
243
      );
244
    }
245
  }
246
247
  if (!isset($misapplied[$name_dedup_key]['out'])) {
248
    $misapplied[$name_dedup_key]['out'] = cdm_tagged_text_to_markup($taxon_relation->taggedText);
249
  } else {
250
    // We need to add the anchors for all of the other misapplied names not
251
    // being rendered explicitly.
252
    $misapplied[$name_dedup_key]['out'] = uuid_anchor($taxon_relation->taxonUuid, $misapplied[$name_dedup_key]['out']);
253
  }
254
}
255
256 b7a20282 Andreas Kohlbecker
/**
257
 * Renders a representation of the given taxon relationship.
258
 *
259
 * According name relationships are also being rendered.
260
 *
261
 * @param $taxon
262
 *  The CDM TaxonBase entity
263
 * @param $reltype_uuid
264
 *  The UUID of the TaxonRelationshipType
265
 * @param $relsign
266
 *  Optional. Can be  used to override the internal decision strategy on finding a suitable icon for the relationship
267
 * @param $reltype_representation
268
 *   Optional: Defines the value for the title attribute of the html element enclosing the relsign
269
 * @param $doubtful
270
 *   TODO
271
 * @param $doLinkTaxon
272
 *   The taxon will be rendered as clickable link when true.
273
 *
274
 * @return string
275
 *   Markup for the taxon relationship.
276 f659b3dd Andreas Kohlbecker
 *
277
 * @throws Exception
278 b7a20282 Andreas Kohlbecker
 */
279
function cdm_related_taxon($taxon, $reltype_uuid = NULL, $relsign = NULL, $reltype_representation = NULL, $doubtful=false, $doLinkTaxon = FALSE) {
280
  static $relsign_homo = '≡';
281
  static $relsign_hetero = '=';
282
  static $relsign_invalid = '&ndash;';
283
  static $nom_status_invalid_type_uuids =  array(
284
    UUID_NOMENCLATURALSTATUS_TYPE_INVALID,
285
    UUID_NOMENCLATURALSTATUS_TYPE_NUDUM,
286
    UUID_NOMENCLATURALSTATUS_TYPE_COMBINATIONINVALID,
287
    UUID_NOMENCLATURALSTATUS_TYPE_PROVISIONAL
288
  );
289
290
  // 'taxonRelationships';
291
  $footnoteListKey = NULL;
292
293
  $skip_tags = array();
294
295
  $is_invalid = false;
296
297
  if (!$relsign) {
298
299
    switch ($reltype_uuid) {
300
      case UUID_HETEROTYPIC_SYNONYM_OF:
301
      case UUID_SYNONYM_OF:
302
        $relsign = $relsign_hetero;
303
        break;
304
305
      case UUID_HOMOTYPIC_SYNONYM_OF:
306
        $relsign = $relsign_homo;
307
        break;
308
309
      case UUID_MISAPPLIED_NAME_FOR:
310
      case UUID_INVALID_DESIGNATION_FOR:
311
        $skip_tags[] = 'authors';
312
        $is_invalid = true;
313
        $relsign = $relsign_invalid;
314
315
        break;
316
317
      default:
318
        $relsign = $relsign_invalid;
319
    }
320
321
  }
322
323
  if($doubtful) {
324
    $relsign = '?' . $relsign;
325
  }
326
327
  /*
328
  Names with status invalid or nudum are to be displayed with the
329
  $relsign_invalid, these names appear at the end of all names in their
330
  homotypic group (ordered correctly by the java cdm_lib).
331
  */
332
  if (isset($taxon->name->status) && is_array($taxon->name->status)) {
333
    foreach ($taxon->name->status as $status) {
334
      if (in_array($status->type->uuid , $nom_status_invalid_type_uuids)) {
335
        $relsign = $relsign_invalid;
336
        break;
337
      }
338
    }
339
  }
340
341
  // Now rendering starts ..
342
  RenderHints::pushToRenderStack('related_taxon');
343
344
  if (isset($taxon->name->nomenclaturalReference)) {
345
    $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
346
  }
347
  $taxonUri = '';
348
  if ($doLinkTaxon) {
349
    $taxonUri = url(path_to_taxon($taxon->uuid, "synonymy"));
350
  }
351
  // Printing the taxonName and the handling the special case of annotations.
352
  if (!isset($referenceUri)) {
353
    $referenceUri = FALSE;
354
  }
355
  $out_taxon_part = render_taxon_or_name($taxon, $taxonUri, $referenceUri, TRUE, FALSE, $skip_tags, $is_invalid);
356
  $taxon_footnotes = theme('cdm_annotations_as_footnotekeys',
357
    array('cdmBase_list' => array(
358
      $taxon->name,
359
      $taxon,
360
    ),
361
      'footnote_list_key' => $footnoteListKey)
362
  );
363
364
  $homonyms = cdm_name_relationships_of($taxon);
365
366
  $out = '<span class="relation_sign" title="' . $reltype_representation . '">' . $relsign . '</span>'
367
    . $out_taxon_part . $taxon_footnotes . ' '  . $homonyms;
368
369
  $out = uuid_anchor($taxon->uuid, $out);
370
371
  RenderHints::popFromRenderStack();
372
373
  return $out;
374
}
375
376 87b304a7 Andreas Kohlbecker
377
/**
378
 * Creates markup for a taxon which is the accepted of another one
379
 *
380
 * @param $accepted_for_uuid
381
 *   The uuid of the accepted taxon
382
 */
383
function cdm_accepted_for($accepted_for_uuid) {
384
385
  if(!is_uuid($accepted_for_uuid)){
386
    return '';
387
  }
388
389
  RenderHints::pushToRenderStack('acceptedFor');
390
  $out = '';
391
392
  $synonym = cdm_ws_get(CDM_WS_PORTAL_TAXON, $accepted_for_uuid);
393
  if ($synonym) {
394
    $out .= '<span class="acceptedFor">';
395
    $out .= t('is accepted for ');
396
    if (isset($synonym->name->nomenclaturalReference)) {
397
      $referenceUri = url(path_to_reference($synonym->name->nomenclaturalReference->uuid));
398
    }
399
    $out .= render_taxon_or_name($synonym->name, NULL, $referenceUri);
400
    $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $synonym));
401
    $out .= '</span>';
402
  }
403
  RenderHints::popFromRenderStack();
404
  return $out;
405
}
406
407
/**
408
 * Compose function for a list of taxa.
409
 *
410 6789eff8 Andreas Kohlbecker
 * This function is for used to:
411
 *
412
 * 1. Display search results
413
 * 2. List the taxa for a taxon name in the name page.
414 87b304a7 Andreas Kohlbecker
 *
415
 * @param $taxon_list array
416
 *   The list of CDM Taxon entities. e.g. The records array as contained in a pager object.
417
 * @param $freetext_search_results array
418
 * @param $show_classification boolean
419
 *
420
 * @ingroup compose
421
 *
422
 */
423
function compose_list_of_taxa($taxon_list, $freetext_search_results = array(), $show_classification = false) {
424
425
  $unclassified_snippet = '<span class="unclassified">' . t('unclassified') . '</span>';
426
427
  RenderHints::pushToRenderStack('list_of_taxa');
428
429
  $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SEARCH_GALLERY_NAME);
430
431
  $showMedia_taxa = $gallery_settings['cdm_dataportal_show_taxon_thumbnails'];
432
  $showMedia_synonyms = $gallery_settings['cdm_dataportal_show_synonym_thumbnails'];
433
  $searched_in_classification = cdm_dataportal_searched_in_classification();
434
  $searched_in_classification_uuid = null;
435
  if(isset($searched_in_classification->uuid)){
436
    $searched_in_classification_uuid = $searched_in_classification->uuid;
437
  }
438
439
  // .. Well, for sure not as performant as before, but better than nothing.
440
  $synonym_uuids = array();
441
  $misappied_uuids = array();
442
  foreach ($taxon_list as $taxon) {
443
    if ($taxon->class == "Synonym") {
444
      if (!array_key_exists($taxon->uuid, $synonym_uuids)) {
445
        $synonym_uuids[$taxon->uuid] = $taxon->uuid;
446
      }
447
    }
448
    elseif (!_cdm_dataportal_acceptedByCurrentView($taxon)) {
449
      // Assuming that it is a misappied name, will be further examined below.
450
      $misappied_uuids[$taxon->uuid] = $taxon->uuid;
451
    }
452
  }
453
454
  // Batch service not jet implemented:
455
  // $table_of_accepted = cdm_ws_property(CDM_WS_PORTAL_TAXON_ACCEPTED,
456
  // join(',', $synonym_uuids));
457
  // thus ...
458
  $table_of_accepted = array();
459
460
  foreach ($synonym_uuids as $relatedUuid) {
461 8bb9f541 Andreas Kohlbecker
    $classification_filter = '';
462
    if($searched_in_classification_uuid){
463
      $classification_filter = 'classificationFilter=' . $searched_in_classification_uuid;
464
    }
465
    $table_of_accepted[$relatedUuid] = cdm_ws_get(
466
      CDM_WS_PORTAL_TAXON_ACCEPTED,
467 9128dcb4 Andreas Kohlbecker
      array($relatedUuid),
468 8bb9f541 Andreas Kohlbecker
      $classification_filter
469 9128dcb4 Andreas Kohlbecker
    );
470 87b304a7 Andreas Kohlbecker
  }
471
472
  foreach ($misappied_uuids as $relatedUuid) {
473
    $taxonRelations = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS, array(
474
      $relatedUuid,
475
    ));
476
    foreach ($taxonRelations as $relation) {
477
      if ($relation->type->uuid == UUID_MISAPPLIED_NAME_FOR && _cdm_dataportal_acceptedByCurrentView($relation->toTaxon)) {
478
        $table_of_accepted[$relatedUuid][] = $relation->toTaxon;
479
      }
480
    }
481
  }
482
483 6789eff8 Andreas Kohlbecker
  $out = '<div class="cdm-item-list" style="background-image: none;">';
484 87b304a7 Andreas Kohlbecker
  $itemCnt = -1;
485
  foreach ($taxon_list as $taxon) {
486
    $itemCnt++;
487
    if (isset($table_of_accepted[$taxon->uuid])) {
488
      // Its a synonym or misapplied name.
489
      $is_synonym = isset($synonym_uuids[$taxon->uuid]); //TODO better use the $taxon->class attribute?
490 eebf415b Andreas Kohlbecker
      $taxon_type = $is_synonym ? TAXON_TYPE_SYNONYM :TAXON_TYPE_MISAPPLIEDNAME;
491 87b304a7 Andreas Kohlbecker
492 eebf415b Andreas Kohlbecker
      $acceptedTaxa = $table_of_accepted[$taxon->uuid];
493 87b304a7 Andreas Kohlbecker
494 eebf415b Andreas Kohlbecker
      if (!is_array($acceptedTaxa)) {
495
        $acceptedTaxa = array($acceptedTaxa);
496
      }
497 87b304a7 Andreas Kohlbecker
498 eebf415b Andreas Kohlbecker
      foreach ($acceptedTaxa as $acceptedTaxon) {
499
        if (is_object($acceptedTaxon)) {
500
501
          $taxonUri = uri_to_synonym($taxon->uuid, $acceptedTaxon->uuid);
502
          $referenceUri = '';
503
          if (isset($acceptedTaxon->name->nomenclaturalReference)) {
504
            $referenceUri = url(path_to_reference($acceptedTaxon->name->nomenclaturalReference->uuid));
505
          }
506
          $taxon_or_name = $is_synonym ? $taxon->name : $taxon;
507
          // $taxon_or_name this is a trick to suppress the sec reference for synonyms
508
          // supplying the name will cause render_taxon_or_name() to not show the sec reference
509 6789eff8 Andreas Kohlbecker
          $out .= "<div class=\"item " . $taxon_type . "\">" . render_taxon_or_name($taxon_or_name, $taxonUri, $referenceUri);
510 018cd7f8 Andreas Kohlbecker
          if($taxon_type == TAXON_TYPE_MISAPPLIEDNAME && is_object($taxon->sec)){
511 eebf415b Andreas Kohlbecker
            if(isset($taxon->sec->authorship)){
512
              $authorship = $taxon->sec->authorship->titleCache;
513
            } else {
514
              $authorship = $taxon->sec->titleCache;
515 87b304a7 Andreas Kohlbecker
            }
516 eebf415b Andreas Kohlbecker
            $out .=  ' sensu ' . $authorship;
517 87b304a7 Andreas Kohlbecker
          }
518 eebf415b Andreas Kohlbecker
          if ($show_classification) {
519
            $classifications = get_classifications_for_taxon($taxon);
520
            $classification_titles = array();
521
            foreach ($classifications as $classification) {
522
              if (isset($classification->titleCache)) {
523
                $classification_titles[] = $classification->titleCache;
524
              }
525
            }
526
            if (count($classification_titles) == 0) {
527
              $classification_titles[] = $unclassified_snippet;
528
            }
529
            $out .= '<span class="classifications"><span class="separator"> : </span>' . implode(', ', $classification_titles) . '</span>';
530
          }
531
          $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
532
          if ($showMedia_synonyms) {
533
            $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $acceptedTaxon));
534 87b304a7 Andreas Kohlbecker
          }
535
        }
536
      }
537
    }
538
    else {
539
      // Its a Taxon.
540
      $taxonUri = url(path_to_taxon($taxon->uuid));
541
      $referenceUri = '';
542
      if (isset($taxon->name->nomenclaturalReference)) {
543
        $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
544
      }
545 6789eff8 Andreas Kohlbecker
      $out .= '<div class="item Taxon">' . render_taxon_or_name($taxon, $taxonUri, $referenceUri);
546 87b304a7 Andreas Kohlbecker
      if ($show_classification) {
547
        $classifications = get_classifications_for_taxon($taxon);
548
        $classification_titles = array();
549
        foreach ($classifications as $classification) {
550
          if (isset($classification->titleCache)) {
551
            $classification_titles[] = $classification->titleCache;
552
          }
553
        }
554
        if(count($classification_titles) == 0){
555
          $classification_titles[] = $unclassified_snippet;
556
        }
557 2813096f Andreas Kohlbecker
        $out .= '<span class="classifications"><span class="separator"> : </span>' . implode(', ', $classification_titles) . '</span>';
558 87b304a7 Andreas Kohlbecker
      }
559
      $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
560
561
      if ($showMedia_taxa) {
562
        $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $taxon));
563
      }
564
    }
565
566
    /*
567
     * the score field will be empty in case of MultiTermQueries like
568
     * WildcardQueries, since these are  constant score by default
569
     * since Lucene 2.9
570
     */
571
    if(isset($freetext_search_results[$itemCnt]) && $freetext_search_results[$itemCnt]->score && $freetext_search_results[$itemCnt]->maxScore){
572
      $percentage =  ( $freetext_search_results[$itemCnt]->score / $freetext_search_results[$itemCnt]->maxScore ) * 100;
573
      $out .= '<div class="score-bar"><div class="score-bar-indicator" style="width:' . $percentage .'% "></div></div>';
574
      $out .= '<div class="score-bar-value">' . number_format($percentage, 2) .'%</div>';
575
    }
576
577
    // Render highlighted fragments, these are made available by free text
578
    // searches.
579
    if (isset($freetext_search_results[$itemCnt]->fieldHighlightMap)) {
580
      $field_fragments = (array) $freetext_search_results[$itemCnt]->fieldHighlightMap;
581
      if (count($field_fragments) > 0) {
582
        $fragments_out = '';
583
        foreach ($field_fragments as $fieldName => $fragments) {
584
          $fragments_out .= '... <span class="' . $fieldName. '">' . filter_xss(join(" ... ", $fragments), array('b') ) . '</span>';
585
        }
586
        $out .= '<div class="fragment_highlight">' . $fragments_out . ' ...</div>';
587
      }
588
    }
589
590 6789eff8 Andreas Kohlbecker
    $out .= '</div>';
591 87b304a7 Andreas Kohlbecker
  }
592
593 6789eff8 Andreas Kohlbecker
  $out .= '</div>';
594 87b304a7 Andreas Kohlbecker
  RenderHints::popFromRenderStack();
595
596
  return markup_to_render_array($out); // TODO create render array of all list items in function
597
}
598
599 bc400a17 Andreas Kohlbecker
600 8938d9a0 Andreas Kohlbecker
/**
601
 * Compose function for the taxonomic children
602
 *
603
 * @param $taxon_uuid
604
 *    The uuuid of the taxon to compose the list of taxonomic children for
605
 * @return
606
 *   A drupal render array.
607
 *
608
 * @ingroup compose
609
 */
610 bc400a17 Andreas Kohlbecker
function compose_taxonomic_children($taxon_uuid){
611
612
  $render_array = array();
613 f58a08fa Andreas Kohlbecker
  
614
  if($taxon_uuid) {
615
    $children = cdm_ws_get(CDM_WS_PORTAL_TAXONOMY_CHILDNODES_OF_TAXON, array(
616
      get_current_classification_uuid(),
617
      $taxon_uuid
618
      ));
619
    if($children){
620
      $taxonomic_children = theme('cdm_taxontree', array('tree' => $children));
621
      $render_array = markup_to_render_array($taxonomic_children);
622
    }
623 bc400a17 Andreas Kohlbecker
  }
624
  return $render_array;
625
}