Project

General

Profile

« Previous | Next » 

Revision 87b304a7

Added by Andreas Kohlbecker about 8 years ago

refactoring: refactored functions moved into includes

View differences:

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