Project

General

Profile

Download (19 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Taxon Theming functions.
5
 *
6
 * @copyright
7
 *   (C) 2007-2012 EDIT
8
 *   European Distributed Institute of Taxonomy
9
 *   http://www.e-taxonomy.eu
10
 *
11
 *   The contents of this module are subject to the Mozilla
12
 *   Public License Version 1.1.
13
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
14
 */
15

    
16
/**
17
 * Returns HTML for misapplied names and invalid designations.
18
 *
19
 * Both relation types are currently treated the same!
20
 *
21
 * @param array $variables
22
 *   An associative array containing:
23
 *   - taxonRelationships
24
 *   - focusedTaxon
25
 *
26
 * @ingroup themeable
27
 *
28
 * @return string
29
 *    the rendered html
30
 */
31
function theme_cdm_taxonRelationships($variables) {
32
  $taxonRelationships = $variables['taxonRelationships'];
33
  $focusedTaxon = $variables['focusedTaxon'];
34
  if (!$taxonRelationships) {
35
    return null;
36
  }
37

    
38
  RenderHints::pushToRenderStack('taxonRelationships');
39
  $footnoteListKey = 'taxonRelationships';
40
  RenderHints::setFootnoteListKey($footnoteListKey);
41

    
42
  $misapplied = array();
43
  $joinedAuthorTeams = array();
44

    
45
  $taxon_relationship_types = variable_get(CDM_TAXON_RELATIONSHIP_TYPES, unserialize(CDM_TAXON_RELATIONSHIP_TYPES_DEFAULT));
46

    
47
  // Aggregate misapplied names having the same fullname:
48
  foreach ($taxonRelationships as $taxonRelation) {
49

    
50
    if (in_array($taxonRelation->type->uuid, $taxon_relationship_types)) {
51

    
52
      if ($taxonRelation->type->uuid == UUID_MISAPPLIED_NAME_FOR || $taxonRelation->type->uuid == UUID_INVALID_DESIGNATION_FOR) {
53

    
54
        $name = $taxonRelation->fromTaxon->name->titleCache;
55

    
56
        if(isset($taxonRelation->fromTaxon->sec)) {
57
          // taxa not always are have a sec reference (e.g. doubtful taxa)
58
          $authorteam = cdm_ws_get(CDM_WS_REFERENCE_AUTHORTEAM, $taxonRelation->fromTaxon->sec->uuid);
59
          $authorteam = $authorteam->titleCache;
60
        }
61

    
62
        if (!isset($misapplied[$name])) {
63
          // Render the first name found as representative for all others.
64
          $misapplied[$name]['out'] = cdm_related_taxon($taxonRelation->fromTaxon, UUID_MISAPPLIED_NAME_FOR);
65
        }
66
        else {
67
          // We need to add the anchors for all of the other mispplied names not
68
          // being rendered explicitly.
69
          $misapplied[$name]['out'] = uuid_anchor($taxonRelation->fromTaxon->uuid, $misapplied[$name]['out']);
70
        }
71

    
72
        // Collect all authors for this fullname.
73
        if (isset($authorteam)) {
74
          $misapplied[$name]['authorteam'][$authorteam] = '';
75
          $joinedAuthorTeams[$authorteam] = 'sensu ' . theme('cdm_reference', array('reference' => $taxonRelation->fromTaxon->sec));
76
        }
77
      }
78
      else {
79
        // All relationsship types but misapplied_name_for
80
        // invalid_designation_for.
81
        $taxon_relationships_lines[] = cdm_taxonRelationship($taxonRelation, TRUE, _is_invers_taxonRelationship($taxonRelation, $focusedTaxon));
82
      }
83
    }
84
  }
85

    
86
  // Sort the joinedAuthorTeams and create footnotes and footnotekeys.
87
  ksort($joinedAuthorTeams);
88
  foreach ($joinedAuthorTeams as $authorteam => $sensuCitation) {
89
    $footnoteKey = FootnoteManager::addNewFootnote($footnoteListKey, $sensuCitation);
90
    $joinedAuthorTeams[$authorteam] = '<span class="sensu">sensu '
91
      . $authorteam
92
      . theme('cdm_footnote_key', array('footnoteKey' => $footnoteKey))
93
      . '</span>';
94
  }
95

    
96
  // ---- Generate output ---- //
97

    
98
  $out = '<div class="taxon-relationships">';
99
  if (is_array($misapplied) && count($misapplied) > 0) {
100
    $out .= '<ul class="misapplied">';
101
    foreach ($misapplied as $misapplied_name) {
102

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

    
105
      if (isset($misapplied_name['authorteam'])) {
106
        // Fill authors with the renderedFootnoteKey and sorting 'em.
107
        foreach ($misapplied_name['authorteam'] as $authorteam => &$renderedFootnoteKey) {
108
          $renderedFootnoteKey = $joinedAuthorTeams[$authorteam];
109
        }
110
        ksort($misapplied_name['authorteam']);
111
        $out .= join('; ', $misapplied_name['authorteam']);
112
      }
113
      $out .= '</li>';
114
    }
115
    $out .= '</ul>';
116
  }
117

    
118
  if (isset($taxon_relationships_lines) && is_array($taxon_relationships_lines) && count($taxon_relationships_lines) > 0) {
119
    $out .= '<ul class="taxonRelationships">';
120
    foreach ($taxon_relationships_lines as $taxon_relationship_line) {
121
      $out .= '<li class="synonym">' . $taxon_relationship_line . '</li>';
122
    }
123
    $out .= '</ul>';
124
  }
125

    
126
  $footnotes = theme('cdm_footnotes', array('footnoteListKey' => $footnoteListKey, 'enclosingTag' => 'li'));
127
  $footnotes .= theme('cdm_annotation_footnotes', array('footnoteListKey' => $footnoteListKey, 'enclosingTag' => 'li'));
128

    
129
// AK: why splitting footnotes at the sennsu string ??? this is weired and hacky
130
//     TODO remove below dead code
131
//   $tr_footnotes_exploded = explode('sensu', $tr_footnotes);
132
//   $tr_footnotes_aux = '';
133
//   foreach ($tr_footnotes_exploded as $element) {
134
//     $tr_footnotes_aux .= $element;
135
//   }
136

    
137
  $out .= '<ul class="footnotes">' . $footnotes . '</ul>';
138

    
139
  $out .= '</div>';
140

    
141
  RenderHints::popFromRenderStack();
142
  return $out;
143
}
144

    
145
/**
146
 * @todo Please document this function.
147
 * @see http://drupal.org/node/1354
148
 */
149
function theme_cdm_acceptedFor($variables) {
150
  $accepted_for_uuid = $variables['acceptedFor'];
151

    
152
  if(!is_uuid($accepted_for_uuid)){
153
    return '';
154
  }
155

    
156
  RenderHints::pushToRenderStack('acceptedFor');
157
  $out = '';
158

    
159
  $synonym = cdm_ws_get(CDM_WS_PORTAL_TAXON, $accepted_for_uuid);
160
  if ($synonym) {
161
    $out .= '<span class="acceptedFor">';
162
    $out .= t('is accepted for ');
163
    if (isset($synonym->name->nomenclaturalReference)) {
164
      $referenceUri = url(path_to_reference($synonym->name->nomenclaturalReference->uuid));
165
    }
166
    $out .= render_taxon_or_name($synonym->name, NULL, $referenceUri);
167
    $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $synonym));
168
    $out .= '</span>';
169
  }
170
  RenderHints::popFromRenderStack();
171
  return $out;
172
}
173

    
174
/**
175
 * @todo document this function.
176
 */
177
function theme_cdm_list_of_taxa($variables) {
178

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

    
181
  $records = $variables['records'];
182
  $freetextSearchResults = $variables['freetextSearchResults'];
183
  $show_classification = $variables['show_classification'];
184

    
185
  RenderHints::pushToRenderStack('list_of_taxa');
186

    
187
  $form_name = 'search_gallery';
188
  // $default_values = unserialize(CDM_DATAPORTAL_GALLERY_SETTINGS);
189
  // $gallery_settings = variable_get($form_name, $default_values);
190
  $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SEARCH_GALLERY_NAME);
191

    
192
  $showMedia_taxa = $gallery_settings['cdm_dataportal_show_taxon_thumbnails'];
193
  $showMedia_synonyms = $gallery_settings['cdm_dataportal_show_synonym_thumbnails'];
194
  // $showMedia_taxa =
195
  // variable_get('cdm_dataportal_findtaxa_show_taxon_thumbnails', 1);
196
  // $showMedia_synonyms =
197
  // variable_get('cdm_dataportal_findtaxa_show_synonym_thumbnails', 0);
198
  $searched_in_classification = cdm_dataportal_searched_in_classification();
199
  $searched_in_classification_uuid = null;
200
  if(isset($searched_in_classification->uuid)){
201
    $searched_in_classification_uuid = $searched_in_classification->uuid;
202
  }
203

    
204
  // .. Well, for sure not as performant as before, but better than nothing.
205
  $synonym_uuids = array();
206
  $misappied_uuids = array();
207
  foreach ($records as $taxon) {
208
    if ($taxon->class == "Synonym") {
209
      if (!array_key_exists($taxon->uuid, $synonym_uuids)) {
210
        $synonym_uuids[$taxon->uuid] = $taxon->uuid;
211
      }
212
    }
213
    elseif (!_cdm_dataportal_acceptedByCurrentView($taxon)) {
214
      // Assuming that it is a misappied name, will be further examined below.
215
      $misappied_uuids[$taxon->uuid] = $taxon->uuid;
216
    }
217
  }
218

    
219
  // Batch service not jet implemented:
220
  // $table_of_accepted = cdm_ws_property(CDM_WS_PORTAL_TAXON_ACCEPTED,
221
  // join(',', $synonym_uuids));
222
  // thus ...
223
  $table_of_accepted = array();
224

    
225
  foreach ($synonym_uuids as $relatedUuid) {
226
    $table_of_accepted[$relatedUuid] = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, array(
227
      $relatedUuid,
228
      $searched_in_classification_uuid,
229
    ));
230
  }
231

    
232
  foreach ($misappied_uuids as $relatedUuid) {
233
    $taxonRelations = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS, array(
234
      $relatedUuid,
235
    ));
236
    foreach ($taxonRelations as $relation) {
237
      if ($relation->type->uuid == UUID_MISAPPLIED_NAME_FOR && _cdm_dataportal_acceptedByCurrentView($relation->toTaxon)) {
238
        $table_of_accepted[$relatedUuid][] = $relation->toTaxon;
239
      }
240
    }
241
  }
242

    
243
  $out = '<ul class="cdm_names" style="background-image: none;">';
244
  $itemCnt = -1;
245
  foreach ($records as $taxon) {
246
    $itemCnt++;
247
    if (isset($table_of_accepted[$taxon->uuid])) {
248
      // Its a synonym or misapplied name.
249
      $is_synonym = isset($synonym_uuids[$taxon->uuid]); //TODO better use the $taxon->class attribute?
250
      $taxon_type = $is_synonym ? "Synonym" : "misapplied-name";
251

    
252
      $acceptedTaxa = $table_of_accepted[$taxon->uuid];
253

    
254
      if (count($acceptedTaxa) == 1) {
255

    
256
        $acceptedTaxon = $acceptedTaxa[0];
257
        $taxonUri = uri_to_synonym($taxon->uuid, $acceptedTaxon->uuid, 'synonymy');
258
        $referenceUri = '';
259
        if (isset($acceptedTaxon->name->nomenclaturalReference)) {
260
          $referenceUri = url(path_to_reference($acceptedTaxon->name->nomenclaturalReference->uuid));
261
        }
262
        $taxon_or_name = $is_synonym ? $taxon->name : $taxon;
263
        // $taxon_or_name this is a trick to suppress the sec reference for sysnonyms
264
        // supplying the name will cause render_taxon_or_name() to not show the sec reference
265
        $out .= '<li class="' . $taxon_type . '">' . render_taxon_or_name($taxon_or_name, $taxonUri, $referenceUri);
266
        if ($show_classification) {
267
          $classifications = get_classifications_for_taxon($taxon);
268
          $classification_titles = array();
269
          foreach ($classifications as $classification) {
270
            if (isset($classification->titleCache)) {
271
              $classification_titles[] = $classification->titleCache;
272
            }
273
          }
274
          if(count($classification_titles) == 0){
275
            $classification_titles[] = $unclassified_snippet;
276
          }
277
          $out .= ' : <span class="classifications">' . implode(', ', $classification_titles) . '</span>';
278
        }
279
        $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
280
        if ($showMedia_synonyms) {
281
          $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $acceptedTaxon));
282
        }
283
      }
284
      else {
285

    
286
        // TODO avoid using Ajax in the cdm_dynabox .... why?
287
        // TODO add media.
288
        $out .= cdm_dynabox(
289
          $taxon->uuid,
290
          render_taxon_or_name($taxon->name, NULL, NULL, FALSE),
291
          cdm_compose_url(CDM_WS_PORTAL_TAXON_ACCEPTED,
292
            array(
293
              $taxon->uuid,
294
              $searched_in_classification_uuid
295
            )
296
          ),
297
          'cdm_list_of_taxa',
298
          'show accepted taxa of this ' . $taxon_type,
299
          array('li', 'ul'),
300
          array('class' => array($taxon_type))
301
         );
302
      }
303
    }
304
    else {
305
      // Its a Taxon.
306
      $taxonUri = url(path_to_taxon($taxon->uuid));
307
      $referenceUri = '';
308
      if (isset($taxon->name->nomenclaturalReference)) {
309
        $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
310
      }
311
      $out .= '<li class="Taxon">' . render_taxon_or_name($taxon, $taxonUri, $referenceUri);
312
      if ($show_classification) {
313
        $classifications = get_classifications_for_taxon($taxon);
314
        $classification_titles = array();
315
        foreach ($classifications as $classification) {
316
          if (isset($classification->titleCache)) {
317
            $classification_titles[] = $classification->titleCache;
318
          }
319
        }
320
        if(count($classification_titles) == 0){
321
          $classification_titles[] = $unclassified_snippet;
322
        }
323
        $out .= ' : <span class="classifications">' . implode(', ', $classification_titles) . '</span>';
324
      }
325
      $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
326

    
327
      if ($showMedia_taxa) {
328
        $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $taxon));
329
      }
330
    }
331

    
332
    /*
333
     * the score field will be empty in case of MultiTermQueries like
334
     * WildcardQueries, since these are  constant score by default
335
     * since Lucene 2.9
336
     */
337
    if(isset($freetextSearchResults[$itemCnt]) && $freetextSearchResults[$itemCnt]->score && $freetextSearchResults[$itemCnt]->maxScore){
338
      $percentage =  ( $freetextSearchResults[$itemCnt]->score / $freetextSearchResults[$itemCnt]->maxScore ) * 100;
339
      $out .= '<div class="score-bar"><div class="score-bar-indicator" style="width:' . $percentage .'% "></div></div>';
340
      $out .= '<div class="score-bar-value">' . number_format($percentage, 2) .'%</div>';
341
    }
342

    
343
    // Render highlighted fragments, these are made available by free text
344
    // searches.
345
    if (isset($freetextSearchResults[$itemCnt]->fieldHighlightMap)) {
346
      $field_fragments = (array) $freetextSearchResults[$itemCnt]->fieldHighlightMap;
347
      if (count($field_fragments) > 0) {
348
        $fragments_out = '';
349
        foreach ($field_fragments as $fieldName => $fragments) {
350
          $fragments_out .= '... <span class="' . $fieldName. '">' . filter_xss(join(" ... ", $fragments), array('b') ) . '</span>';
351
        }
352
        $out .= '<div class="fragment_highlight">' . $fragments_out . ' ...</div>';
353
      }
354
    }
355

    
356
    $out .= '</li>';
357
  }
358

    
359
  $out .= '</ul>';
360
  RenderHints::popFromRenderStack();
361

    
362
  return $out;
363
}
364

    
365
/**
366
 * Renders a representation of the given taxon relationship.
367
 *
368
 * According name relationships are also being rendered.
369
 *
370
 * @param unknown_type $taxonRelationship
371
 * @param boolean $doLinkTaxon
372
 *     whether to create a link to the related taxon
373
 * @param boolean $inverse
374
 *     whether the $taxonRelationship should be treaded as invers relation
375
 *
376
 * @return void|string
377
 */
378
function cdm_taxonRelationship($taxonRelationship, $doLinkTaxon = FALSE, $inverse = FALSE) {
379

    
380
  // Validate object.
381
  if (!(isset($taxonRelationship->toTaxon) && isset($taxonRelationship->fromTaxon) && isset($taxonRelationship->type))) {
382
    return null;
383
  }
384

    
385
  $taxonRelationType = $taxonRelationship->type;
386

    
387
  if ($inverse) {
388
    $toTaxon = $taxonRelationship->fromTaxon;
389
    $relsign = $taxonRelationType->inverseRepresentation_L10n_abbreviatedLabel;
390
    $reltype_representation = $taxonRelationType->inverseRepresentation_L10n;
391
  }
392
  else {
393
    $toTaxon = $taxonRelationship->toTaxon;
394
    $relsign = $taxonRelationType->representation_L10n_abbreviatedLabel;
395
    $reltype_representation = $taxonRelationType->representation_L10n;
396
  }
397

    
398
  return cdm_related_taxon($toTaxon, NULL, $relsign, $reltype_representation, $taxonRelationship->doubtful, $doLinkTaxon);
399
}
400

    
401
/**
402
 * Renders a representation of the given taxon relationship.
403
 *
404
 * According name relationships are also being rendered.
405
 *
406
 * @param $taxon
407
 *  The CDM TaxonBase entity
408
 * @param $reltype_uuid
409
 *  The UUID of the TaxonRelationshipType
410
 * @param $relsign
411
 *  Optional. Can be  used to override the internal decision strategy on finding a suitable icon for the relationship
412
 * @param $reltype_representation
413
 *   Optional: Defines the value for the title attribute of the html element enclosing the relsign
414
 * @param $doubtful
415
 *   TODO
416
 * @param $doLinkTaxon
417
 *   The taxon will be rendered as clickable link when true.
418
 *
419
 * @return string
420
 *   Markup for the taxon relationship.
421
 */
422
function cdm_related_taxon($taxon, $reltype_uuid = NULL, $relsign = NULL, $reltype_representation = NULL, $doubtful=false, $doLinkTaxon = FALSE) {
423
  static $relsign_homo = '≡';
424
  static $relsign_hetero = '=';
425
  static $relsign_invalid = '&ndash;';
426
  static $nom_status_invalid_type_uuids =  array(
427
      UUID_NOMENCLATURALSTATUS_TYPE_INVALID,
428
      UUID_NOMENCLATURALSTATUS_TYPE_NUDUM,
429
      UUID_NOMENCLATURALSTATUS_TYPE_COMBINATIONINVALID,
430
      UUID_NOMENCLATURALSTATUS_TYPE_PROVISIONAL
431
  );
432

    
433
  // 'taxonRelationships';
434
  $footnoteListKey = NULL;
435

    
436
  $skip_tags = array();
437

    
438
  $is_invalid = false;
439

    
440
  if (!$relsign) {
441

    
442
    switch ($reltype_uuid) {
443
      case UUID_HETEROTYPIC_SYNONYM_OF:
444
      case UUID_SYNONYM_OF:
445
        $relsign = $relsign_hetero;
446
        break;
447

    
448
      case UUID_HOMOTYPIC_SYNONYM_OF:
449
        $relsign = $relsign_homo;
450
        break;
451

    
452
      case UUID_MISAPPLIED_NAME_FOR:
453
      case UUID_INVALID_DESIGNATION_FOR:
454
        $skip_tags[] = 'authors';
455
        $is_invalid = true;
456
        $relsign = $relsign_invalid;
457

    
458
        break;
459

    
460
      default:
461
        $relsign = $relsign_invalid;
462
    }
463

    
464
  }
465

    
466
  if($doubtful) {
467
    $relsign = '?' . $relsign;
468
  }
469

    
470
  /*
471
  Names with status invalid or nudum are to be displayed with the
472
  $relsign_invalid, these names appear at the end of all names in their
473
  homotypic group (ordered correctly by the java cdm_lib).
474
  */
475
  if (isset($taxon->name->status) && is_array($taxon->name->status)) {
476
    foreach ($taxon->name->status as $status) {
477
      if (in_array($status->type->uuid , $nom_status_invalid_type_uuids)) {
478
        $relsign = $relsign_invalid;
479
        break;
480
      }
481
    }
482
  }
483

    
484
  // Now rendering starts ..
485
  RenderHints::pushToRenderStack('related_taxon');
486

    
487
  if (isset($taxon->name->nomenclaturalReference)) {
488
    $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
489
  }
490
  $taxonUri = '';
491
  if ($doLinkTaxon) {
492
    $taxonUri = url(path_to_taxon($taxon->uuid, "synonymy"));
493
  }
494
  // Printing the taxonName and the handling the special case of annotations.
495
  if (!isset($referenceUri)) {
496
    $referenceUri = FALSE;
497
  }
498
  $out_taxon_part = render_taxon_or_name($taxon, $taxonUri, $referenceUri, TRUE, FALSE, $skip_tags, $is_invalid);
499
  $taxon_footnotes = theme('cdm_annotations_as_footnotekeys',
500
       array('cdmBase_list' => array(
501
         $taxon->name,
502
         $taxon,
503
       ),
504
       'footnote_list_key' => $footnoteListKey)
505
  );
506

    
507
  $homonyms = cdm_name_relationships_of($taxon);
508

    
509
  $out = '<span class="relation_sign" title="' . $reltype_representation . '">' . $relsign . '</span>'
510
      . $out_taxon_part . $taxon_footnotes . ' '  . $homonyms;
511

    
512
  $out = uuid_anchor($taxon->uuid, $out);
513

    
514
  RenderHints::popFromRenderStack();
515

    
516
  return $out;
517
}
518

    
519

    
520
  /**
521
 * @todo document this function.
522
 */
523
function theme_cdm_taxon_list_thumbnails($variables) {
524

    
525
  $taxon = $variables['taxon'];
526
  $out = '';
527
  $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SEARCH_GALLERY_NAME);
528
  $showCaption = $gallery_settings['cdm_dataportal_show_thumbnail_captions'];
529
  if ($showCaption) {
530
    $captionElements = array(
531
      'title',
532
      'rights',
533
    );
534
  } else {
535
    $captionElements = array();
536
  }
537

    
538
  $gallery_name = $taxon->uuid;
539

    
540
  $mediaList = _load_media_for_taxon($taxon);
541

    
542
  $galleryLinkUri = path_to_taxon($taxon->uuid, 'images');
543

    
544
  $out .= theme('cdm_media_gallerie', array(
545
    'mediaList' => $mediaList,
546
    'galleryName' => $gallery_name,
547
    'maxExtend' => $gallery_settings['cdm_dataportal_media_maxextend'],
548
    'cols' => $gallery_settings['cdm_dataportal_media_cols'],
549
    'maxRows' => $gallery_settings['cdm_dataportal_media_maxRows'],
550
    'captionElements' => $captionElements,
551
    'mediaLinkType' => 'LIGHTBOX',
552
    'alternativeMediaUri' => NULL,
553
    'galleryLinkUri' => $galleryLinkUri,
554
     ));
555

    
556
  return $out;
557
}
(8-8/9)