Project

General

Profile

Revision 87b304a7

ID87b304a76f5eb08bb24b2907fc4cffaa387a84ef
Parent 7272d19c
Child 7ca5ef1e

Added by Andreas Kohlbecker about 3 years ago

refactoring: refactored functions moved into includes

View differences:

modules/cdm_dataportal/includes/references.inc
29 29
 * All these functions are also implementations of the compose_hook()
30 30
 * which is used in the proxy_content() function.
31 31
 * @}
32
 */
32
 */
33

  
34
/**
35
 * Cleanup of the textual short form of an author team.
36
 *
37
 * The cdm fails to create correct representations of author teams.
38
 * The '&' gluing the authors together need to be cleaned up.
39
 *
40
 * @param $author_team
41
 *  the authorTeam string
42
 *
43
 * @return string
44

  
45
 */
46
function _short_form_of_author_team($author_team) {
47
  $number_of_authors = substr_count($author_team, ' & ') + 1;
48
  // var_dump($author_team);
49
  // var_dump($number_of_authors);
50
  switch ($number_of_authors) {
51
    case 1:
52
      $result = $author_team;
53
      break;
54

  
55
    case 2:
56
      $result = str_replace(' & ', ' and ', $author_team);
57
      break;
58

  
59
    default:
60
      $result_parts = explode(' & ', $author_team);
61
      $result = $result_parts[0] . ' et al.';
62
  }
63
  return $result;
64
}
modules/cdm_dataportal/includes/taxon.inc
311 311
  return $out;
312 312
}
313 313

  
314

  
315
/**
316
 * Creates markup for a taxon which is the accepted of another one
317
 *
318
 * @param $accepted_for_uuid
319
 *   The uuid of the accepted taxon
320
 */
321
function cdm_accepted_for($accepted_for_uuid) {
322

  
323
  if(!is_uuid($accepted_for_uuid)){
324
    return '';
325
  }
326

  
327
  RenderHints::pushToRenderStack('acceptedFor');
328
  $out = '';
329

  
330
  $synonym = cdm_ws_get(CDM_WS_PORTAL_TAXON, $accepted_for_uuid);
331
  if ($synonym) {
332
    $out .= '<span class="acceptedFor">';
333
    $out .= t('is accepted for ');
334
    if (isset($synonym->name->nomenclaturalReference)) {
335
      $referenceUri = url(path_to_reference($synonym->name->nomenclaturalReference->uuid));
336
    }
337
    $out .= render_taxon_or_name($synonym->name, NULL, $referenceUri);
338
    $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $synonym));
339
    $out .= '</span>';
340
  }
341
  RenderHints::popFromRenderStack();
342
  return $out;
343
}
344

  
345
/**
346
 * Compose function for a list of taxa.
347
 *
348
 * This function is for example used toi display search results or the taxa for a taxon name in the name page.
349
 *
350
 * @param $taxon_list array
351
 *   The list of CDM Taxon entities. e.g. The records array as contained in a pager object.
352
 * @param $freetext_search_results array
353
 * @param $show_classification boolean
354
 *
355
 * @ingroup compose
356
 *
357
 */
358
function compose_list_of_taxa($taxon_list, $freetext_search_results = array(), $show_classification = false) {
359

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

  
362
  RenderHints::pushToRenderStack('list_of_taxa');
363

  
364
  $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SEARCH_GALLERY_NAME);
365

  
366
  $showMedia_taxa = $gallery_settings['cdm_dataportal_show_taxon_thumbnails'];
367
  $showMedia_synonyms = $gallery_settings['cdm_dataportal_show_synonym_thumbnails'];
368
  $searched_in_classification = cdm_dataportal_searched_in_classification();
369
  $searched_in_classification_uuid = null;
370
  if(isset($searched_in_classification->uuid)){
371
    $searched_in_classification_uuid = $searched_in_classification->uuid;
372
  }
373

  
374
  // .. Well, for sure not as performant as before, but better than nothing.
375
  $synonym_uuids = array();
376
  $misappied_uuids = array();
377
  foreach ($taxon_list as $taxon) {
378
    if ($taxon->class == "Synonym") {
379
      if (!array_key_exists($taxon->uuid, $synonym_uuids)) {
380
        $synonym_uuids[$taxon->uuid] = $taxon->uuid;
381
      }
382
    }
383
    elseif (!_cdm_dataportal_acceptedByCurrentView($taxon)) {
384
      // Assuming that it is a misappied name, will be further examined below.
385
      $misappied_uuids[$taxon->uuid] = $taxon->uuid;
386
    }
387
  }
388

  
389
  // Batch service not jet implemented:
390
  // $table_of_accepted = cdm_ws_property(CDM_WS_PORTAL_TAXON_ACCEPTED,
391
  // join(',', $synonym_uuids));
392
  // thus ...
393
  $table_of_accepted = array();
394

  
395
  foreach ($synonym_uuids as $relatedUuid) {
396
    $table_of_accepted[$relatedUuid] = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, array(
397
      $relatedUuid,
398
      $searched_in_classification_uuid,
399
    ));
400
  }
401

  
402
  foreach ($misappied_uuids as $relatedUuid) {
403
    $taxonRelations = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS, array(
404
      $relatedUuid,
405
    ));
406
    foreach ($taxonRelations as $relation) {
407
      if ($relation->type->uuid == UUID_MISAPPLIED_NAME_FOR && _cdm_dataportal_acceptedByCurrentView($relation->toTaxon)) {
408
        $table_of_accepted[$relatedUuid][] = $relation->toTaxon;
409
      }
410
    }
411
  }
412

  
413
  $out = '<ul class="cdm_names" style="background-image: none;">';
414
  $itemCnt = -1;
415
  foreach ($taxon_list as $taxon) {
416
    $itemCnt++;
417
    if (isset($table_of_accepted[$taxon->uuid])) {
418
      // Its a synonym or misapplied name.
419
      $is_synonym = isset($synonym_uuids[$taxon->uuid]); //TODO better use the $taxon->class attribute?
420
      $taxon_type = $is_synonym ? "Synonym" : "misapplied-name";
421

  
422
      $acceptedTaxa = $table_of_accepted[$taxon->uuid];
423

  
424
      if (count($acceptedTaxa) == 1) {
425

  
426
        $acceptedTaxon = $acceptedTaxa[0];
427
        $taxonUri = uri_to_synonym($taxon->uuid, $acceptedTaxon->uuid, 'synonymy');
428
        $referenceUri = '';
429
        if (isset($acceptedTaxon->name->nomenclaturalReference)) {
430
          $referenceUri = url(path_to_reference($acceptedTaxon->name->nomenclaturalReference->uuid));
431
        }
432
        $taxon_or_name = $is_synonym ? $taxon->name : $taxon;
433
        // $taxon_or_name this is a trick to suppress the sec reference for sysnonyms
434
        // supplying the name will cause render_taxon_or_name() to not show the sec reference
435
        $out .= '<li class="' . $taxon_type . '">' . render_taxon_or_name($taxon_or_name, $taxonUri, $referenceUri);
436
        if ($show_classification) {
437
          $classifications = get_classifications_for_taxon($taxon);
438
          $classification_titles = array();
439
          foreach ($classifications as $classification) {
440
            if (isset($classification->titleCache)) {
441
              $classification_titles[] = $classification->titleCache;
442
            }
443
          }
444
          if(count($classification_titles) == 0){
445
            $classification_titles[] = $unclassified_snippet;
446
          }
447
          $out .= ' : <span class="classifications">' . implode(', ', $classification_titles) . '</span>';
448
        }
449
        $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
450
        if ($showMedia_synonyms) {
451
          $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $acceptedTaxon));
452
        }
453
      }
454
      else {
455

  
456
        // TODO avoid using Ajax in the cdm_dynabox .... why?
457
        // TODO add media.
458
        $out .= cdm_dynabox(
459
          $taxon->uuid,
460
          render_taxon_or_name($taxon->name, NULL, NULL, FALSE),
461
          cdm_compose_url(CDM_WS_PORTAL_TAXON_ACCEPTED,
462
            array(
463
              $taxon->uuid,
464
              $searched_in_classification_uuid
465
            )
466
          ),
467
          'cdm_list_of_taxa',
468
          'show accepted taxa of this ' . $taxon_type,
469
          array('li', 'ul'),
470
          array('class' => array($taxon_type))
471
        );
472
      }
473
    }
474
    else {
475
      // Its a Taxon.
476
      $taxonUri = url(path_to_taxon($taxon->uuid));
477
      $referenceUri = '';
478
      if (isset($taxon->name->nomenclaturalReference)) {
479
        $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
480
      }
481
      $out .= '<li class="Taxon">' . render_taxon_or_name($taxon, $taxonUri, $referenceUri);
482
      if ($show_classification) {
483
        $classifications = get_classifications_for_taxon($taxon);
484
        $classification_titles = array();
485
        foreach ($classifications as $classification) {
486
          if (isset($classification->titleCache)) {
487
            $classification_titles[] = $classification->titleCache;
488
          }
489
        }
490
        if(count($classification_titles) == 0){
491
          $classification_titles[] = $unclassified_snippet;
492
        }
493
        $out .= ' : <span class="classifications">' . implode(', ', $classification_titles) . '</span>';
494
      }
495
      $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
496

  
497
      if ($showMedia_taxa) {
498
        $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $taxon));
499
      }
500
    }
501

  
502
    /*
503
     * the score field will be empty in case of MultiTermQueries like
504
     * WildcardQueries, since these are  constant score by default
505
     * since Lucene 2.9
506
     */
507
    if(isset($freetext_search_results[$itemCnt]) && $freetext_search_results[$itemCnt]->score && $freetext_search_results[$itemCnt]->maxScore){
508
      $percentage =  ( $freetext_search_results[$itemCnt]->score / $freetext_search_results[$itemCnt]->maxScore ) * 100;
509
      $out .= '<div class="score-bar"><div class="score-bar-indicator" style="width:' . $percentage .'% "></div></div>';
510
      $out .= '<div class="score-bar-value">' . number_format($percentage, 2) .'%</div>';
511
    }
512

  
513
    // Render highlighted fragments, these are made available by free text
514
    // searches.
515
    if (isset($freetext_search_results[$itemCnt]->fieldHighlightMap)) {
516
      $field_fragments = (array) $freetext_search_results[$itemCnt]->fieldHighlightMap;
517
      if (count($field_fragments) > 0) {
518
        $fragments_out = '';
519
        foreach ($field_fragments as $fieldName => $fragments) {
520
          $fragments_out .= '... <span class="' . $fieldName. '">' . filter_xss(join(" ... ", $fragments), array('b') ) . '</span>';
521
        }
522
        $out .= '<div class="fragment_highlight">' . $fragments_out . ' ...</div>';
523
      }
524
    }
525

  
526
    $out .= '</li>';
527
  }
528

  
529
  $out .= '</ul>';
530
  RenderHints::popFromRenderStack();
531

  
532
  return markup_to_render_array($out); // TODO create render array of all list items in function
533
}
534

  
modules/cdm_dataportal/theme/cdm_dataportal.references.theme
130 130
}
131 131

  
132 132
/**
133
 * Cleanup of the textual short form of an author team.
134
 *
135
 * The cdm fails to create correct representations of author teams.
136
 * The '&' gluing the authors together need to be cleaned up.
137
 *
138
 * @param $author_team
139
 *  the authorTeam string
140
 *
141
 * @return string
142

  
143
 */
144
function _short_form_of_author_team($author_team) {
145
  $number_of_authors = substr_count($author_team, ' & ') + 1;
146
  // var_dump($author_team);
147
  // var_dump($number_of_authors);
148
  switch ($number_of_authors) {
149
    case 1:
150
      $result = $author_team;
151
      break;
152

  
153
    case 2:
154
      $result = str_replace(' & ', ' and ', $author_team);
155
      break;
156

  
157
    default:
158
      $result_parts = explode(' & ', $author_team);
159
      $result = $result_parts[0] . ' et al.';
160
  }
161
  return $result;
162
}
163

  
164
/**
165 133
 * @todo Please document this function.
166 134
 * @see http://drupal.org/node/1354
167 135
 */
modules/cdm_dataportal/theme/cdm_dataportal.taxon.theme
13 13
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
14 14
 */
15 15

  
16
/**
17
 * Creates markup for a taxon which is the accepted of another one
18
 *
19
 * @param $accepted_for_uuid
20
 *   The uuid of the accepted taxon
21
 */
22
function cdm_accepted_for($accepted_for_uuid) {
23

  
24
  if(!is_uuid($accepted_for_uuid)){
25
    return '';
26
  }
27

  
28
  RenderHints::pushToRenderStack('acceptedFor');
29
  $out = '';
30

  
31
  $synonym = cdm_ws_get(CDM_WS_PORTAL_TAXON, $accepted_for_uuid);
32
  if ($synonym) {
33
    $out .= '<span class="acceptedFor">';
34
    $out .= t('is accepted for ');
35
    if (isset($synonym->name->nomenclaturalReference)) {
36
      $referenceUri = url(path_to_reference($synonym->name->nomenclaturalReference->uuid));
37
    }
38
    $out .= render_taxon_or_name($synonym->name, NULL, $referenceUri);
39
    $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $synonym));
40
    $out .= '</span>';
41
  }
42
  RenderHints::popFromRenderStack();
43
  return $out;
44
}
45

  
46
/**
47
 * Compose function for a list of taxa.
48
 *
49
 * This function is for example used toi display search results or the taxa for a taxon name in the name page.
50
 *
51
 * @param $taxon_list array
52
 *   The list of CDM Taxon entities. e.g. The records array as contained in a pager object.
53
 * @param $freetext_search_results array
54
 * @param $show_classification boolean
55
 *
56
 * @ingroup compose
57
 *
58
 */
59
function compose_list_of_taxa($taxon_list, $freetext_search_results = array(), $show_classification = false) {
60

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

  
63
  RenderHints::pushToRenderStack('list_of_taxa');
64

  
65
  $gallery_settings = getGallerySettings(CDM_DATAPORTAL_SEARCH_GALLERY_NAME);
66

  
67
  $showMedia_taxa = $gallery_settings['cdm_dataportal_show_taxon_thumbnails'];
68
  $showMedia_synonyms = $gallery_settings['cdm_dataportal_show_synonym_thumbnails'];
69
  $searched_in_classification = cdm_dataportal_searched_in_classification();
70
  $searched_in_classification_uuid = null;
71
  if(isset($searched_in_classification->uuid)){
72
    $searched_in_classification_uuid = $searched_in_classification->uuid;
73
  }
74

  
75
  // .. Well, for sure not as performant as before, but better than nothing.
76
  $synonym_uuids = array();
77
  $misappied_uuids = array();
78
  foreach ($taxon_list as $taxon) {
79
    if ($taxon->class == "Synonym") {
80
      if (!array_key_exists($taxon->uuid, $synonym_uuids)) {
81
        $synonym_uuids[$taxon->uuid] = $taxon->uuid;
82
      }
83
    }
84
    elseif (!_cdm_dataportal_acceptedByCurrentView($taxon)) {
85
      // Assuming that it is a misappied name, will be further examined below.
86
      $misappied_uuids[$taxon->uuid] = $taxon->uuid;
87
    }
88
  }
89

  
90
  // Batch service not jet implemented:
91
  // $table_of_accepted = cdm_ws_property(CDM_WS_PORTAL_TAXON_ACCEPTED,
92
  // join(',', $synonym_uuids));
93
  // thus ...
94
  $table_of_accepted = array();
95

  
96
  foreach ($synonym_uuids as $relatedUuid) {
97
    $table_of_accepted[$relatedUuid] = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, array(
98
      $relatedUuid,
99
      $searched_in_classification_uuid,
100
    ));
101
  }
102

  
103
  foreach ($misappied_uuids as $relatedUuid) {
104
    $taxonRelations = cdm_ws_get(CDM_WS_PORTAL_TAXON_RELATIONS, array(
105
      $relatedUuid,
106
    ));
107
    foreach ($taxonRelations as $relation) {
108
      if ($relation->type->uuid == UUID_MISAPPLIED_NAME_FOR && _cdm_dataportal_acceptedByCurrentView($relation->toTaxon)) {
109
        $table_of_accepted[$relatedUuid][] = $relation->toTaxon;
110
      }
111
    }
112
  }
113

  
114
  $out = '<ul class="cdm_names" style="background-image: none;">';
115
  $itemCnt = -1;
116
  foreach ($taxon_list as $taxon) {
117
    $itemCnt++;
118
    if (isset($table_of_accepted[$taxon->uuid])) {
119
      // Its a synonym or misapplied name.
120
      $is_synonym = isset($synonym_uuids[$taxon->uuid]); //TODO better use the $taxon->class attribute?
121
      $taxon_type = $is_synonym ? "Synonym" : "misapplied-name";
122

  
123
      $acceptedTaxa = $table_of_accepted[$taxon->uuid];
124

  
125
      if (count($acceptedTaxa) == 1) {
126

  
127
        $acceptedTaxon = $acceptedTaxa[0];
128
        $taxonUri = uri_to_synonym($taxon->uuid, $acceptedTaxon->uuid, 'synonymy');
129
        $referenceUri = '';
130
        if (isset($acceptedTaxon->name->nomenclaturalReference)) {
131
          $referenceUri = url(path_to_reference($acceptedTaxon->name->nomenclaturalReference->uuid));
132
        }
133
        $taxon_or_name = $is_synonym ? $taxon->name : $taxon;
134
        // $taxon_or_name this is a trick to suppress the sec reference for sysnonyms
135
        // supplying the name will cause render_taxon_or_name() to not show the sec reference
136
        $out .= '<li class="' . $taxon_type . '">' . render_taxon_or_name($taxon_or_name, $taxonUri, $referenceUri);
137
        if ($show_classification) {
138
          $classifications = get_classifications_for_taxon($taxon);
139
          $classification_titles = array();
140
          foreach ($classifications as $classification) {
141
            if (isset($classification->titleCache)) {
142
              $classification_titles[] = $classification->titleCache;
143
            }
144
          }
145
          if(count($classification_titles) == 0){
146
            $classification_titles[] = $unclassified_snippet;
147
          }
148
          $out .= ' : <span class="classifications">' . implode(', ', $classification_titles) . '</span>';
149
        }
150
        $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
151
        if ($showMedia_synonyms) {
152
          $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $acceptedTaxon));
153
        }
154
      }
155
      else {
156

  
157
        // TODO avoid using Ajax in the cdm_dynabox .... why?
158
        // TODO add media.
159
        $out .= cdm_dynabox(
160
          $taxon->uuid,
161
          render_taxon_or_name($taxon->name, NULL, NULL, FALSE),
162
          cdm_compose_url(CDM_WS_PORTAL_TAXON_ACCEPTED,
163
            array(
164
              $taxon->uuid,
165
              $searched_in_classification_uuid
166
            )
167
          ),
168
          'cdm_list_of_taxa',
169
          'show accepted taxa of this ' . $taxon_type,
170
          array('li', 'ul'),
171
          array('class' => array($taxon_type))
172
         );
173
      }
174
    }
175
    else {
176
      // Its a Taxon.
177
      $taxonUri = url(path_to_taxon($taxon->uuid));
178
      $referenceUri = '';
179
      if (isset($taxon->name->nomenclaturalReference)) {
180
        $referenceUri = url(path_to_reference($taxon->name->nomenclaturalReference->uuid));
181
      }
182
      $out .= '<li class="Taxon">' . render_taxon_or_name($taxon, $taxonUri, $referenceUri);
183
      if ($show_classification) {
184
        $classifications = get_classifications_for_taxon($taxon);
185
        $classification_titles = array();
186
        foreach ($classifications as $classification) {
187
          if (isset($classification->titleCache)) {
188
            $classification_titles[] = $classification->titleCache;
189
          }
190
        }
191
        if(count($classification_titles) == 0){
192
          $classification_titles[] = $unclassified_snippet;
193
        }
194
        $out .= ' : <span class="classifications">' . implode(', ', $classification_titles) . '</span>';
195
      }
196
      $out .= theme('cdm_annotations_as_footnotekeys', array('cdmBase_list' => $taxon));
197

  
198
      if ($showMedia_taxa) {
199
        $out .= theme('cdm_taxon_list_thumbnails', array('taxon' => $taxon));
200
      }
201
    }
202

  
203
    /*
204
     * the score field will be empty in case of MultiTermQueries like
205
     * WildcardQueries, since these are  constant score by default
206
     * since Lucene 2.9
207
     */
208
    if(isset($freetext_search_results[$itemCnt]) && $freetext_search_results[$itemCnt]->score && $freetext_search_results[$itemCnt]->maxScore){
209
      $percentage =  ( $freetext_search_results[$itemCnt]->score / $freetext_search_results[$itemCnt]->maxScore ) * 100;
210
      $out .= '<div class="score-bar"><div class="score-bar-indicator" style="width:' . $percentage .'% "></div></div>';
211
      $out .= '<div class="score-bar-value">' . number_format($percentage, 2) .'%</div>';
212
    }
213

  
214
    // Render highlighted fragments, these are made available by free text
215
    // searches.
216
    if (isset($freetext_search_results[$itemCnt]->fieldHighlightMap)) {
217
      $field_fragments = (array) $freetext_search_results[$itemCnt]->fieldHighlightMap;
218
      if (count($field_fragments) > 0) {
219
        $fragments_out = '';
220
        foreach ($field_fragments as $fieldName => $fragments) {
221
          $fragments_out .= '... <span class="' . $fieldName. '">' . filter_xss(join(" ... ", $fragments), array('b') ) . '</span>';
222
        }
223
        $out .= '<div class="fragment_highlight">' . $fragments_out . ' ...</div>';
224
      }
225
    }
226

  
227
    $out .= '</li>';
228
  }
229

  
230
  $out .= '</ul>';
231
  RenderHints::popFromRenderStack();
232

  
233
  return markup_to_render_array($out); // TODO create render array of all list items in function
234
}
235 16

  
236 17
  /**
237 18
 * @todo document this function.

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)