Project

General

Profile

Download (18.9 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

    
35
/**
36
 * Returns HTML for misapplied names and invalid designations.
37
 *
38
 * Both relation types are currently treated the same!
39
 *
40
 * @param taxonRelationships
41
 * @param focusedTaxon
42
 *
43
 * @return string
44
 *    the rendered html
45
 */
46
function cdm_taxonRelationships($taxonRelationships, $focusedTaxon){
47

    
48
  static $NULL_AUTHORTEAM = 'NULL_AUTHORTEAM';
49

    
50
  if (!$taxonRelationships) {
51
    return null;
52
  }
53

    
54
  RenderHints::pushToRenderStack('taxon_relationships');
55
  $footnoteListKey = 'taxon_relationships';
56
  RenderHints::setFootnoteListKey($footnoteListKey);
57

    
58
  $misapplied = array();
59
  $joinedAuthorTeams = array();
60

    
61
  $taxon_relationship_types = variable_get(CDM_TAXON_RELATIONSHIP_TYPES, unserialize(CDM_TAXON_RELATIONSHIP_TYPES_DEFAULT));
62

    
63
  // Aggregate misapplied names having the same fullname:
64
  foreach ($taxonRelationships as $taxonRelation) {
65

    
66
    if (in_array($taxonRelation->type->uuid, $taxon_relationship_types)) {
67

    
68

    
69
      if ($taxonRelation->type->uuid == UUID_MISAPPLIED_NAME_FOR || $taxonRelation->type->uuid == UUID_INVALID_DESIGNATION_FOR) {
70

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

    
73
        $name = $taxonRelation->fromTaxon->name->titleCache;
74

    
75
        if(isset($taxonRelation->fromTaxon->sec)) {
76
          // taxa not always are have a sec reference (e.g. doubtful taxa)
77
          $authorteam = cdm_ws_get(CDM_WS_REFERENCE_AUTHORTEAM, $taxonRelation->fromTaxon->sec->uuid);
78
          $authorteam = $authorteam->titleCache;
79
          if(!$authorteam){
80
            $authorteam = $NULL_AUTHORTEAM;
81
          }
82
        }
83

    
84
        if (!isset($misapplied[$name])) {
85
          // Render the first name found as representative for all others.
86
          $misapplied[$name]['out'] = cdm_related_taxon($taxonRelation->fromTaxon, UUID_MISAPPLIED_NAME_FOR);
87
        }
88
        else {
89
          // We need to add the anchors for all of the other misapplied names not
90
          // being rendered explicitly.
91
          $misapplied[$name]['out'] = uuid_anchor($taxonRelation->fromTaxon->uuid, $misapplied[$name]['out']);
92
        }
93

    
94
        // Collect all authors for this fullname.
95
        if (isset($authorteam)) {
96
          $misapplied[$name]['authorteam'][$authorteam] = '';
97
          $joinedAuthorTeams[$authorteam] = 'sensu ' . theme('cdm_reference', array('reference' => $taxonRelation->fromTaxon->sec));
98
        }
99

    
100
      }
101
      else {
102
        RenderHints::pushToRenderStack('other_taxon_relationship');
103
        // All relationsship types but misapplied_name_for
104
        // invalid_designation_for.
105
        $taxon_relationships_lines[] = cdm_taxonRelationship($taxonRelation, TRUE, _is_invers_taxonRelationship($taxonRelation, $focusedTaxon));
106
      }
107

    
108
      RenderHints::popFromRenderStack();
109
    }
110
  }
111

    
112
  // Sort the joinedAuthorTeams and create footnotes and footnotekeys.
113
  ksort($joinedAuthorTeams);
114
  $author_team_cnt = 0;
115
  foreach ($joinedAuthorTeams as $authorteam => $sensuCitation) {
116
    $footnoteKey = FootnoteManager::addNewFootnote($footnoteListKey, $sensuCitation);
117

    
118
    $sensu = ++$author_team_cnt == 0 ? '' : 'sensu ';
119

    
120
    $joinedAuthorTeams[$authorteam] =  sprintf(
121
        '<span class="sensu">%s%s%s</span>',
122
        $sensu,
123
        ($authorteam != $NULL_AUTHORTEAM ? $authorteam : ''),
124
        theme('cdm_footnote_key', array('footnoteKey' => $footnoteKey))
125
      );
126
  }
127

    
128
  // ---- Generate output ---- //
129

    
130
  $out = '<div class="taxon-relationships">';
131
  if (is_array($misapplied) && count($misapplied) > 0) {
132
    $out .= '<ul class="misapplied">';
133
    foreach ($misapplied as $misapplied_name) {
134

    
135
      $out .= '<li class="synonym"><span class="misapplied">' . $misapplied_name['out'] . ' </span>';
136

    
137
      if (isset($misapplied_name['authorteam'])) {
138
        // Fill authors with the renderedFootnoteKey and sorting 'em.
139
        foreach ($misapplied_name['authorteam'] as $authorteam => &$renderedFootnoteKey) {
140
          $renderedFootnoteKey = $joinedAuthorTeams[$authorteam];
141
        }
142
        ksort($misapplied_name['authorteam']);
143
        $out .= join('; ', $misapplied_name['authorteam']);
144
      }
145
      $out .= '</li>';
146
    }
147
    $out .= '</ul>';
148
  }
149

    
150
  if (isset($taxon_relationships_lines) && is_array($taxon_relationships_lines) && count($taxon_relationships_lines) > 0) {
151
    $out .= '<ul class="taxonRelationships">';
152
    foreach ($taxon_relationships_lines as $taxon_relationship_line) {
153
      $out .= '<li class="synonym">' . $taxon_relationship_line . '</li>';
154
    }
155
    $out .= '</ul>';
156
  }
157

    
158
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => $footnoteListKey, 'enclosingTag' => 'li'));
159
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnoteListKey, 'enclosingTag' => 'li'));
160

    
161
// AK: why splitting footnotes at the sensu string ??? this is weired and hacky
162
//     TODO remove below dead code
163
//   $tr_footnotes_exploded = explode('sensu', $tr_footnotes);
164
//   $tr_footnotes_aux = '';
165
//   foreach ($tr_footnotes_exploded as $element) {
166
//     $tr_footnotes_aux .= $element;
167
//   }
168

    
169
  $out .= '<ul class="footnotes">' . $footnotes . '</ul>';
170

    
171
  $out .= '</div>';
172

    
173
  RenderHints::popFromRenderStack();
174
  return $out;
175
}
176

    
177

    
178
/**
179
 * Renders a representation of the given taxon relationship.
180
 *
181
 * According name relationships are also being rendered.
182
 *
183
 * @param unknown_type $taxonRelationship
184
 * @param boolean $doLinkTaxon
185
 *     whether to create a link to the related taxon
186
 * @param boolean $inverse
187
 *     whether the $taxonRelationship should be treaded as invers relation
188
 *
189
 * @return void|string
190
 */
191
function cdm_taxonRelationship($taxonRelationship, $doLinkTaxon = FALSE, $inverse = FALSE) {
192

    
193
  // Validate object.
194
  if (!(isset($taxonRelationship->toTaxon) && isset($taxonRelationship->fromTaxon) && isset($taxonRelationship->type))) {
195
    return null;
196
  }
197

    
198
  $taxonRelationType = $taxonRelationship->type;
199

    
200
  if ($inverse) {
201
    $toTaxon = $taxonRelationship->fromTaxon;
202
    $relsign = $taxonRelationType->inverseRepresentation_L10n_abbreviatedLabel;
203
    $reltype_representation = $taxonRelationType->inverseRepresentation_L10n;
204
  }
205
  else {
206
    $toTaxon = $taxonRelationship->toTaxon;
207
    $relsign = $taxonRelationType->representation_L10n_abbreviatedLabel;
208
    $reltype_representation = $taxonRelationType->representation_L10n;
209
  }
210

    
211
  return cdm_related_taxon($toTaxon, NULL, $relsign, $reltype_representation, $taxonRelationship->doubtful, $doLinkTaxon);
212
}
213

    
214
/**
215
 * Renders a representation of the given taxon relationship.
216
 *
217
 * According name relationships are also being rendered.
218
 *
219
 * @param $taxon
220
 *  The CDM TaxonBase entity
221
 * @param $reltype_uuid
222
 *  The UUID of the TaxonRelationshipType
223
 * @param $relsign
224
 *  Optional. Can be  used to override the internal decision strategy on finding a suitable icon for the relationship
225
 * @param $reltype_representation
226
 *   Optional: Defines the value for the title attribute of the html element enclosing the relsign
227
 * @param $doubtful
228
 *   TODO
229
 * @param $doLinkTaxon
230
 *   The taxon will be rendered as clickable link when true.
231
 *
232
 * @return string
233
 *   Markup for the taxon relationship.
234
 */
235
function cdm_related_taxon($taxon, $reltype_uuid = NULL, $relsign = NULL, $reltype_representation = NULL, $doubtful=false, $doLinkTaxon = FALSE) {
236
  static $relsign_homo = '≡';
237
  static $relsign_hetero = '=';
238
  static $relsign_invalid = '&ndash;';
239
  static $nom_status_invalid_type_uuids =  array(
240
    UUID_NOMENCLATURALSTATUS_TYPE_INVALID,
241
    UUID_NOMENCLATURALSTATUS_TYPE_NUDUM,
242
    UUID_NOMENCLATURALSTATUS_TYPE_COMBINATIONINVALID,
243
    UUID_NOMENCLATURALSTATUS_TYPE_PROVISIONAL
244
  );
245

    
246
  // 'taxonRelationships';
247
  $footnoteListKey = NULL;
248

    
249
  $skip_tags = array();
250

    
251
  $is_invalid = false;
252

    
253
  if (!$relsign) {
254

    
255
    switch ($reltype_uuid) {
256
      case UUID_HETEROTYPIC_SYNONYM_OF:
257
      case UUID_SYNONYM_OF:
258
        $relsign = $relsign_hetero;
259
        break;
260

    
261
      case UUID_HOMOTYPIC_SYNONYM_OF:
262
        $relsign = $relsign_homo;
263
        break;
264

    
265
      case UUID_MISAPPLIED_NAME_FOR:
266
      case UUID_INVALID_DESIGNATION_FOR:
267
        $skip_tags[] = 'authors';
268
        $is_invalid = true;
269
        $relsign = $relsign_invalid;
270

    
271
        break;
272

    
273
      default:
274
        $relsign = $relsign_invalid;
275
    }
276

    
277
  }
278

    
279
  if($doubtful) {
280
    $relsign = '?' . $relsign;
281
  }
282

    
283
  /*
284
  Names with status invalid or nudum are to be displayed with the
285
  $relsign_invalid, these names appear at the end of all names in their
286
  homotypic group (ordered correctly by the java cdm_lib).
287
  */
288
  if (isset($taxon->name->status) && is_array($taxon->name->status)) {
289
    foreach ($taxon->name->status as $status) {
290
      if (in_array($status->type->uuid , $nom_status_invalid_type_uuids)) {
291
        $relsign = $relsign_invalid;
292
        break;
293
      }
294
    }
295
  }
296

    
297
  // Now rendering starts ..
298
  RenderHints::pushToRenderStack('related_taxon');
299

    
300
  if (isset($taxon->name->nomenclaturalReference)) {
301
    $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
302
  }
303
  $taxonUri = '';
304
  if ($doLinkTaxon) {
305
    $taxonUri = url(path_to_taxon($taxon->uuid, "synonymy"));
306
  }
307
  // Printing the taxonName and the handling the special case of annotations.
308
  if (!isset($referenceUri)) {
309
    $referenceUri = FALSE;
310
  }
311
  $out_taxon_part = render_taxon_or_name($taxon, $taxonUri, $referenceUri, TRUE, FALSE, $skip_tags, $is_invalid);
312
  $taxon_footnotes = theme('cdm_annotations_as_footnotekeys',
313
    array('cdmBase_list' => array(
314
      $taxon->name,
315
      $taxon,
316
    ),
317
      'footnote_list_key' => $footnoteListKey)
318
  );
319

    
320
  $homonyms = cdm_name_relationships_of($taxon);
321

    
322
  $out = '<span class="relation_sign" title="' . $reltype_representation . '">' . $relsign . '</span>'
323
    . $out_taxon_part . $taxon_footnotes . ' '  . $homonyms;
324

    
325
  $out = uuid_anchor($taxon->uuid, $out);
326

    
327
  RenderHints::popFromRenderStack();
328

    
329
  return $out;
330
}
331

    
332

    
333
/**
334
 * Creates markup for a taxon which is the accepted of another one
335
 *
336
 * @param $accepted_for_uuid
337
 *   The uuid of the accepted taxon
338
 */
339
function cdm_accepted_for($accepted_for_uuid) {
340

    
341
  if(!is_uuid($accepted_for_uuid)){
342
    return '';
343
  }
344

    
345
  RenderHints::pushToRenderStack('acceptedFor');
346
  $out = '';
347

    
348
  $synonym = cdm_ws_get(CDM_WS_PORTAL_TAXON, $accepted_for_uuid);
349
  if ($synonym) {
350
    $out .= '<span class="acceptedFor">';
351
    $out .= t('is accepted for ');
352
    if (isset($synonym->name->nomenclaturalReference)) {
353
      $referenceUri = url(path_to_reference($synonym->name->nomenclaturalReference->uuid));
354
    }
355
    $out .= render_taxon_or_name($synonym->name, NULL, $referenceUri);
356
    $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $synonym));
357
    $out .= '</span>';
358
  }
359
  RenderHints::popFromRenderStack();
360
  return $out;
361
}
362

    
363
/**
364
 * Compose function for a list of taxa.
365
 *
366
 * This function is for example used toi display search results or the taxa for a taxon name in the name page.
367
 *
368
 * @param $taxon_list array
369
 *   The list of CDM Taxon entities. e.g. The records array as contained in a pager object.
370
 * @param $freetext_search_results array
371
 * @param $show_classification boolean
372
 *
373
 * @ingroup compose
374
 *
375
 */
376
function compose_list_of_taxa($taxon_list, $freetext_search_results = array(), $show_classification = false) {
377

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

    
380
  RenderHints::pushToRenderStack('list_of_taxa');
381

    
382
  $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SEARCH_GALLERY_NAME);
383

    
384
  $showMedia_taxa = $gallery_settings['cdm_dataportal_show_taxon_thumbnails'];
385
  $showMedia_synonyms = $gallery_settings['cdm_dataportal_show_synonym_thumbnails'];
386
  $searched_in_classification = cdm_dataportal_searched_in_classification();
387
  $searched_in_classification_uuid = null;
388
  if(isset($searched_in_classification->uuid)){
389
    $searched_in_classification_uuid = $searched_in_classification->uuid;
390
  }
391

    
392
  // .. Well, for sure not as performant as before, but better than nothing.
393
  $synonym_uuids = array();
394
  $misappied_uuids = array();
395
  foreach ($taxon_list as $taxon) {
396
    if ($taxon->class == "Synonym") {
397
      if (!array_key_exists($taxon->uuid, $synonym_uuids)) {
398
        $synonym_uuids[$taxon->uuid] = $taxon->uuid;
399
      }
400
    }
401
    elseif (!_cdm_dataportal_acceptedByCurrentView($taxon)) {
402
      // Assuming that it is a misappied name, will be further examined below.
403
      $misappied_uuids[$taxon->uuid] = $taxon->uuid;
404
    }
405
  }
406

    
407
  // Batch service not jet implemented:
408
  // $table_of_accepted = cdm_ws_property(CDM_WS_PORTAL_TAXON_ACCEPTED,
409
  // join(',', $synonym_uuids));
410
  // thus ...
411
  $table_of_accepted = array();
412

    
413
  foreach ($synonym_uuids as $relatedUuid) {
414
    $table_of_accepted[$relatedUuid] = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, array(
415
      $relatedUuid,
416
      $searched_in_classification_uuid,
417
    ));
418
  }
419

    
420
  foreach ($misappied_uuids as $relatedUuid) {
421
    $taxonRelations = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS, array(
422
      $relatedUuid,
423
    ));
424
    foreach ($taxonRelations as $relation) {
425
      if ($relation->type->uuid == UUID_MISAPPLIED_NAME_FOR && _cdm_dataportal_acceptedByCurrentView($relation->toTaxon)) {
426
        $table_of_accepted[$relatedUuid][] = $relation->toTaxon;
427
      }
428
    }
429
  }
430

    
431
  $out = '<ul class="cdm_names" style="background-image: none;">';
432
  $itemCnt = -1;
433
  foreach ($taxon_list as $taxon) {
434
    $itemCnt++;
435
    if (isset($table_of_accepted[$taxon->uuid])) {
436
      // Its a synonym or misapplied name.
437
      $is_synonym = isset($synonym_uuids[$taxon->uuid]); //TODO better use the $taxon->class attribute?
438
      $taxon_type = $is_synonym ? "Synonym" : "misapplied-name";
439

    
440
      $acceptedTaxa = $table_of_accepted[$taxon->uuid];
441

    
442
      if (count($acceptedTaxa) == 1) {
443

    
444
        $acceptedTaxon = $acceptedTaxa[0];
445
        $taxonUri = uri_to_synonym($taxon->uuid, $acceptedTaxon->uuid, 'synonymy');
446
        $referenceUri = '';
447
        if (isset($acceptedTaxon->name->nomenclaturalReference)) {
448
          $referenceUri = url(path_to_reference($acceptedTaxon->name->nomenclaturalReference->uuid));
449
        }
450
        $taxon_or_name = $is_synonym ? $taxon->name : $taxon;
451
        // $taxon_or_name this is a trick to suppress the sec reference for sysnonyms
452
        // supplying the name will cause render_taxon_or_name() to not show the sec reference
453
        $out .= '<li class="' . $taxon_type . '">' . render_taxon_or_name($taxon_or_name, $taxonUri, $referenceUri);
454
        if ($show_classification) {
455
          $classifications = get_classifications_for_taxon($taxon);
456
          $classification_titles = array();
457
          foreach ($classifications as $classification) {
458
            if (isset($classification->titleCache)) {
459
              $classification_titles[] = $classification->titleCache;
460
            }
461
          }
462
          if(count($classification_titles) == 0){
463
            $classification_titles[] = $unclassified_snippet;
464
          }
465
          $out .= ' : <span class="classifications">' . implode(', ', $classification_titles) . '</span>';
466
        }
467
        $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
468
        if ($showMedia_synonyms) {
469
          $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $acceptedTaxon));
470
        }
471
      }
472
      else {
473

    
474
        // TODO avoid using Ajax in the cdm_dynabox .... why?
475
        // TODO add media.
476
        $out .= cdm_dynabox(
477
          $taxon->uuid,
478
          render_taxon_or_name($taxon->name, NULL, NULL, FALSE),
479
          cdm_compose_url(CDM_WS_PORTAL_TAXON_ACCEPTED,
480
            array(
481
              $taxon->uuid,
482
              $searched_in_classification_uuid
483
            )
484
          ),
485
          'cdm_list_of_taxa',
486
          'show accepted taxa of this ' . $taxon_type,
487
          array('li', 'ul'),
488
          array('class' => array($taxon_type))
489
        );
490
      }
491
    }
492
    else {
493
      // Its a Taxon.
494
      $taxonUri = url(path_to_taxon($taxon->uuid));
495
      $referenceUri = '';
496
      if (isset($taxon->name->nomenclaturalReference)) {
497
        $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
498
      }
499
      $out .= '<li class="Taxon">' . render_taxon_or_name($taxon, $taxonUri, $referenceUri);
500
      if ($show_classification) {
501
        $classifications = get_classifications_for_taxon($taxon);
502
        $classification_titles = array();
503
        foreach ($classifications as $classification) {
504
          if (isset($classification->titleCache)) {
505
            $classification_titles[] = $classification->titleCache;
506
          }
507
        }
508
        if(count($classification_titles) == 0){
509
          $classification_titles[] = $unclassified_snippet;
510
        }
511
        $out .= ' : <span class="classifications">' . implode(', ', $classification_titles) . '</span>';
512
      }
513
      $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
514

    
515
      if ($showMedia_taxa) {
516
        $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $taxon));
517
      }
518
    }
519

    
520
    /*
521
     * the score field will be empty in case of MultiTermQueries like
522
     * WildcardQueries, since these are  constant score by default
523
     * since Lucene 2.9
524
     */
525
    if(isset($freetext_search_results[$itemCnt]) && $freetext_search_results[$itemCnt]->score && $freetext_search_results[$itemCnt]->maxScore){
526
      $percentage =  ( $freetext_search_results[$itemCnt]->score / $freetext_search_results[$itemCnt]->maxScore ) * 100;
527
      $out .= '<div class="score-bar"><div class="score-bar-indicator" style="width:' . $percentage .'% "></div></div>';
528
      $out .= '<div class="score-bar-value">' . number_format($percentage, 2) .'%</div>';
529
    }
530

    
531
    // Render highlighted fragments, these are made available by free text
532
    // searches.
533
    if (isset($freetext_search_results[$itemCnt]->fieldHighlightMap)) {
534
      $field_fragments = (array) $freetext_search_results[$itemCnt]->fieldHighlightMap;
535
      if (count($field_fragments) > 0) {
536
        $fragments_out = '';
537
        foreach ($field_fragments as $fieldName => $fragments) {
538
          $fragments_out .= '... <span class="' . $fieldName. '">' . filter_xss(join(" ... ", $fragments), array('b') ) . '</span>';
539
        }
540
        $out .= '<div class="fragment_highlight">' . $fragments_out . ' ...</div>';
541
      }
542
    }
543

    
544
    $out .= '</li>';
545
  }
546

    
547
  $out .= '</ul>';
548
  RenderHints::popFromRenderStack();
549

    
550
  return markup_to_render_array($out); // TODO create render array of all list items in function
551
}
552

    
(10-10/10)