Project

General

Profile

Download (25.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Functions for dealing with CDM entities from the package model.name
5
 *
6
 * @copyright
7
 *   (C) 2007-2015 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
 * @author
16
 *   - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
17
 */
18

    
19
/**
20
 * @defgroup compose Compose functions
21
 * @{
22
 * Functions which are composing Drupal render arays
23
 *
24
 * The cdm_dataportal module needs to compose rather complex render arrays from
25
 * the data returned by the CDM REST service. The compose functions are
26
 * responsible for creating the render arrays.
27
 *
28
 * All these functions are also implementations of the compose_hook()
29
 * which is used in the proxy_content() function.
30
 * @}
31
 */
32

    
33

    
34
/**
35
 * Provides the name render template to be used within the page elements identified the the $renderPath.
36
 *
37
 * The render templates arrays contains one or more name render templates to be used within the page elements identified the the
38
 * renderPath. The renderPath is the key of the subelements whereas the value is the name render template.
39
 *
40
 * The render paths used for a cdm_dataportal page can be visualized by supplying the HTTP query parameter RENDER_PATH=1.
41
 *
42
 * It will be tried to find  the best matching default RenderTemplate by stripping the dot separated render path
43
 * element by element. If no matching template is found the DEFAULT will be used:
44
 *
45
 * - related_taxon.heterotypicSynonymyGroup.taxon_page_synonymy
46
 * - related_taxon.heterotypicSynonymyGroup.taxon_page_synonymy
47
 * - related_taxon.heterotypicSynonymyGroup.taxon_page_synonymy
48
 *
49
 * A single render template can be used for multiple render paths. In this case the according key of the render templates
50
 * array element should be the list of these render paths concatenated by ONLY a comma character without any whitespace.
51
 *
52
 * A render template is an associative array. The keys of this array are referring to the keys as defined in the part
53
 * definitions array.
54
 * @see get_partDefinition($taxonNameType) for more information
55
 *
56
 * The value of the render template element must be set to TRUE in order to let this part being rendered.
57
 * The namePart, nameAuthorPart and referencePart can also hold an associative array with a single
58
 * element: array('#uri' => TRUE). The value of the #uri element will be replaced by the according
59
 * links if the parameters $nameLink or $refenceLink are set.
60
 *
61
 * @param string $render_path
62
 *   The render path can consist of multiple dot separated elements
63
 *   @see RenderHints::getRenderPath()
64
 * @param string $nameLink
65
 *   The link path ot URL to be used for name parts if a link is forseen in the template
66
 *   matching the given $renderPath.
67
 * @param string $referenceLink
68
 *   The link path ot URL to be used for nomenclatural reference parts if a link is forseen
69
 *   in the template matching the given $renderPath.
70
 * @return array
71
 *   An associative array, the render template
72
 */
73
function get_nameRenderTemplate($render_path, $nameLink = NULL, $referenceLink = NULL) {
74

    
75
  static $default_render_templates = NULL;
76
  static $split_render_templates = NULL;
77

    
78

    
79
  if (!isset($default_render_templates)) {
80
    $default_render_templates = unserialize(CDM_NAME_RENDER_TEMPLATES_DEFAULT);
81
  }
82
  if($split_render_templates == NULL) {
83
    $render_templates = variable_get(CDM_NAME_RENDER_TEMPLATES, $default_render_templates);
84
    // needs to be converted to an array
85
    $render_templates = (convert_to_array($render_templates));
86

    
87
    // separate render templates which are combined with a comma
88
    $split_render_templates = array();
89
    foreach($render_templates as $key => $template){
90
      if(strpos($key, ',')){
91
        foreach(explode(',', $key) as $path){
92
          $split_render_templates[$path] = $template;
93
        }
94
      } else {
95
        $split_render_templates[$key] = $template;
96
      }
97
    }
98
  }
99

    
100
  // get the base element of the renderPath
101
  if (($separatorPos = strpos($render_path, '.')) > 0) {
102
    $renderPath_base = substr($render_path, 0, $separatorPos);
103
  } else {
104
    $renderPath_base = $render_path;
105
  }
106

    
107
  $template = NULL;
108
  // 1. try to find a template using the render path base element
109
  if(array_key_exists($renderPath_base, $split_render_templates)){
110
    $template = (array)$split_render_templates[$renderPath_base];
111
  }
112

    
113
  // 2. Find best matching default RenderTemplate
114
  // by stripping the dot separated render path element by element
115
  // if no matching template is found the DEFAULT will be used.
116
  while (!is_array($template) && strlen($render_path) > 0) {
117
    foreach ($split_render_templates as $path => $t) {
118
      if ($path == $render_path) {
119
        $template = $t;
120
        break;
121
      }
122
    }
123
    // shorten by one element
124
    $render_path = substr($render_path, strrpos($render_path, '.') + 1, strlen($render_path));
125
  }
126

    
127

    
128
  // 3. Otherwise get default RenderTemplate from theme.
129
  if (!is_array($template)) {
130
    $template = $split_render_templates['#DEFAULT'];
131
  }
132

    
133
  // --- set the link uris to the according template fields if they exist
134
  if(isset($template['nameAuthorPart']) && isset($template['nameAuthorPart']['#uri'])) {
135
    if ($nameLink) {
136
      $template['nameAuthorPart']['#uri'] = $nameLink;
137
    }
138
    else {
139
      unset($template['nameAuthorPart']['#uri']);
140
    }
141
  }
142

    
143
  if ($nameLink && isset($template['namePart']['#uri'])) {
144
    $template['namePart']['#uri'] = $nameLink;
145
  }
146
  else {
147
    unset($template['namePart']['#uri']);
148
  }
149

    
150
  if ($referenceLink && isset($template['referencePart']['#uri'])) {
151
    $template['referencePart']['#uri'] = $referenceLink;
152
  }
153
  else {
154
    unset($template['referencePart']['#uri']);
155
  }
156

    
157
  return $template;
158
}
159

    
160
/**
161
 * The part definitions define the specific parts of which a rendered taxon name plus additional information will consist.
162
 *
163
 * A full taxon name plus additional information can consist of the following elements:
164
 *
165
 *   - name: the taxon name inclugin rank nbut without author
166
 *   - authorTeam:  The authors of a reference, also used in taxon names
167
 *   - authors:  The authors of a reference, also used in taxon names
168
 *   - reference: the nomenclatural reference,
169
 *   - microreference:  Volume, page number etc.
170
 *   - status:  The nomenclatural status of a name
171
 *   - description: name descriptions like protologues etc ...
172
 *
173
 * These elements are combined in the part definitions array to from the specific parts to be rendered.
174
 * Usually the following parts are formed:
175
 *
176
 * The name "Lapsana communis L., Sp. Pl.: 811. 1753" shall be an example here:
177
 *  - namePart: the name and rank (in example: "Lapsana communis")
178
 *  - authorshipPart: the author (in example: "L.")
179
 *  - nameAuthorPart: the combination of name and author part (in example: "Lapsana communis L.").
180
 *     This is useful for zoological names where the authorshipPart belongs to the name and both should
181
 *     be combined when a link to the taxon is rendered.
182
 *  - referencePart: the nomencaltural reference (in example: "Sp. Pl. 1753")
183
 *  - microreferencePart: usually the page number (in example ": 811.")
184
 *  - statusPart: the nomenclatorical status
185
 *  - descriptionPart:
186
 *
187
 * Each set of parts is dedicated to render a specific TaxonName type, the type names are used as keys for the
188
 * specific parts part definitions:
189
 *
190
 *  - BotanicalName
191
 *  - ZoologicalName
192
 *  - #DEFAULT:  covers ViralNames and general NonViralNames
193
 *
194
 * An example:
195
 * @code
196
 * array(
197
 *    'ZoologicalName' => array(
198
 *        'namePart' => array('name' => TRUE),
199
 *        'referencePart' => array('authorTeam' => TRUE),
200
 *        'microreferencePart' => array('microreference' => TRUE),
201
 *        'statusPart' => array('status' => TRUE),
202
 *        'descriptionPart' => array('description' => TRUE),
203
 *    ),
204
 *    'BotanicalName' => array(
205
 *        'namePart' => array(
206
 *            'name' => TRUE,
207
 *            'authors' => TRUE,
208
 *        ),
209
 *        'referencePart' => array(
210
 *            'reference' => TRUE,
211
 *            'microreference' => TRUE,
212
 *        ),
213
 *        'statusPart' => array('status' => TRUE),
214
 *        'descriptionPart' => array('description' => TRUE),
215
 *    ),
216
 *  );
217
 * @endcode
218
 *
219
 */
220
function get_partDefinition($taxonNameType) {
221

    
222
  static $default_part_definitions = null;
223
  if (!isset($default_part_definitions)) {
224
    $default_part_definitions= unserialize(CDM_PART_DEFINITIONS_DEFAULT);
225
  }
226

    
227
  static $part_definitions = null;
228
  if (!isset($part_definitions)) {
229
    $part_definitions = convert_to_array(variable_get(CDM_PART_DEFINITIONS, $default_part_definitions));
230
  }
231

    
232
  $dtype = nameTypeToDTYPE($taxonNameType);
233
  if (array_key_exists($taxonNameType, $part_definitions)) {
234
    return $part_definitions[$taxonNameType];
235
  } else if (array_key_exists($dtype, $part_definitions)) {
236
    return $part_definitions[$dtype];
237
  } else {
238
    return $part_definitions['#DEFAULT']; // covers ViralNames and general NonViralNames
239
  }
240

    
241
}
242

    
243

    
244
/**
245
 * Renders the markup for a CDM TaxonName instance.
246
 *
247
 * The layout of the name representation is configured by the
248
 * part_definitions and render_templates (see get_partDefinition() and
249
 * get_nameRenderTemplate())
250
 *
251
 * @param $taxonName
252
 *    cdm TaxonName instance
253
 * @param $nameLink
254
 *    URI to the taxon, @see path_to_taxon(), must be processed by url() before passing to this method
255
 * @param $refenceLink
256
 *    URI to the reference, @see path_to_reference(), must be processed by url() before passing to this method
257
 * @param $show_annotations
258
 *    turns the display of annotations on
259
 * @param $is_type_designation
260
 *    To indicate that the supplied taxon name is a name type designation.
261
 * @param $skiptags
262
 *    an array of name elements tags like 'name', 'rank' to skip. The name part
263
 *          'authors' will not ber affected by this filter. This part is managed though the render template
264
 *          mechanism.
265
 * @param $is_invalid
266
 *   Indicates that this taxon is invalid. In this case the name part will be shown in double quotes.
267
 *   This is useful when rendering taxon relation ships.
268
 *
269
 * @return string
270
 *  The markup for a taxon name.
271
 *
272
 */
273
function render_taxon_or_name($taxon_name_or_taxon_base, $nameLink = NULL, $refenceLink = NULL,
274
  $show_annotations = true, $is_type_designation = false, $skiptags = array(), $is_invalid = false) {
275

    
276
  $is_doubtful = false;
277

    
278
  if($taxon_name_or_taxon_base->class == 'Taxon' || $taxon_name_or_taxon_base->class == 'Synonym'){
279
    $taxonName = $taxon_name_or_taxon_base->name;
280
    $is_doubtful = $taxon_name_or_taxon_base->doubtful;
281
    // use the TaxonBase.taggedTitle so we have the secRef
282
    $taggedTitle = $taxon_name_or_taxon_base->taggedTitle;
283
  } else {
284
    // assuming this is a TaxonName
285
    $taxonName = $taxon_name_or_taxon_base;
286
    $taggedTitle = $taxon_name_or_taxon_base->taggedName;
287
  }
288

    
289

    
290
  $renderTemplate = get_nameRenderTemplate(RenderHints::getRenderPath(), $nameLink, $refenceLink);
291
  $partDefinition = get_partDefinition($taxonName->nameType);
292

    
293
  // Apply definitions to template.
294
  foreach ($renderTemplate as $part => $uri) {
295

    
296
    if (isset($partDefinition[$part])) {
297
      $renderTemplate[$part] = $partDefinition[$part];
298
    }
299
    if (is_array($uri) && isset($uri['#uri'])) {
300
      $renderTemplate[$part]['#uri'] = $uri['#uri'];
301
    }
302
  }
303

    
304
  $secref_tagged_text = split_secref_from_tagged_text($taggedTitle);
305
  $nom_status_tagged_text = split_nomstatus_from_tagged_text($taggedTitle);
306
  $appended_phrase_tagged_text = array(); // this is filled later
307

    
308
  normalize_tagged_text($taggedTitle);
309

    
310
  $firstEntryIsValidNamePart =
311
    isset($taggedTitle)
312
    && is_array($taggedTitle)
313
    && isset($taggedTitle[0]->text)
314
    && is_string($taggedTitle[0]->text)
315
    && $taggedTitle[0]->text != ''
316
    && isset($taggedTitle[0]->type)
317
    && $taggedTitle[0]->type == 'name';
318
  $lastAuthorElementString = FALSE;
319

    
320
  $name_encasement = $is_invalid ? '"' : '';
321
  $doubtful_marker = $is_doubtful ? '?&#8239;' : ''; // 	&#8239; =  NARROW NO-BREAK SPACE
322
  $doubtful_marker_markup = '';
323

    
324
  if($doubtful_marker){
325
    $doubtful_marker_markup = '<span class="doubtful">' . $doubtful_marker . '</span>';
326
  }
327

    
328
  // split off all appendedPhrase item  from the end of the array (usually there only should  be one)
329
  while($taggedTitle[count($taggedTitle)-1]->type == "appendedPhrase"){
330
    $appended_phrase_tagged_text[] = array_pop($taggedTitle);
331
  }
332

    
333
  // Got to use second entry as first one, see ToDo comment below ...
334
  if ($firstEntryIsValidNamePart) {
335

    
336
    $taggedName = $taggedTitle;
337
    $hasNamePart_with_Authors = isset($renderTemplate['namePart']) && isset($renderTemplate['namePart']['authors']);
338
    $hasNameAuthorPart_with_Authors = isset($renderTemplate['nameAuthorPart']) && isset($renderTemplate['nameAuthorPart']['authors']);
339

    
340

    
341
    if (!(($hasNamePart_with_Authors) || ($hasNameAuthorPart_with_Authors))) {
342
      // Find author and split off from name.
343
      // TODO expecting to find the author as the last element.
344
      /*
345
      if($taggedName[count($taggedName)- 1]->type == 'authors'){
346
        $authorTeam = $taggedName[count($taggedName)- 1]->text;
347
        unset($taggedName[count($taggedName)- 1]);
348
      }
349
      */
350

    
351
      // Remove all authors.
352
      $taggedNameNew = array();
353
      foreach ($taggedName as $element) {
354
        if ($element->type != 'authors') {
355
          $taggedNameNew[] = $element;
356
        }
357
        else {
358
          $lastAuthorElementString = $element->text;
359
        }
360
      }
361
      $taggedName = $taggedNameNew;
362
      unset($taggedNameNew);
363
    }
364
    $name = '<span class="' . $taxonName->class . '">' . $doubtful_marker_markup . $name_encasement . cdm_tagged_text_to_markup($taggedName, 'span', ' ', $skiptags) . $name_encasement . '</span>';
365
  }
366
  else {
367
    $name = '<span class="' . $taxonName->class . '_titleCache">' . $doubtful_marker_markup . $name_encasement . $taxonName->titleCache . $name_encasement . '</span>';
368
  }
369

    
370

    
371
  if(isset($appended_phrase_tagged_text[0])){
372
    $name .= ' <span class="appended-phrase">'. cdm_tagged_text_to_markup($appended_phrase_tagged_text) . '</span>';
373
  }
374

    
375
  // Fill name into $renderTemplate.
376
  array_setr('name', $name , $renderTemplate);
377

    
378
  // Fill with authorTeam.
379
  /*
380
  if($authorTeam){
381
    $authorTeamHtml = ' <span class="authorTeam">'.$authorTeam.'</span>';
382
    array_setr('authorTeam', $authorTeamHtml, $renderTemplate);
383
  }
384
  */
385

    
386
  // Fill with reference.
387
  if (isset($renderTemplate['referencePart']) && !$is_type_designation) {
388

    
389
    $registrations = cdm_ws_get(CDM_WS_NAME, array($taxonName->uuid, "registrations"));
390
    $registration_markup = render_registrations($registrations);
391

    
392
    // default separator
393
    $separator = '';
394

    
395
    // [Eckhard]:"Komma nach dem Taxonnamen ist grunsätzlich falsch,
396
    // Komma nach dem Autornamen ist überall dort falsch, wo ein "in" folgt."
397
    if (isset($renderTemplate['referencePart']['reference']) && isset($taxonName->nomenclaturalReference)) {
398
      $microreference = NULL;
399
      if (isset($renderTemplate['referencePart']['microreference'])&& isset($taxonName->nomenclaturalMicroReference)) {
400
        $microreference = $taxonName->nomenclaturalMicroReference;
401
      }
402
      $citation = cdm_ws_getNomenclaturalReference($taxonName->nomenclaturalReference->uuid, $microreference);
403

    
404
      // Find preceding element of the reference.
405
      $precedingKey = get_preceding_contentElementKey('reference', $renderTemplate);
406
      if (str_beginsWith($citation, ", in")) {
407
        $citation = substr($citation, 2);
408
        $separator = ' ';
409
      }
410
      elseif (!str_beginsWith($citation, "in") && $precedingKey == 'authors') {
411
        $separator = ', ';
412
      } else {
413
        $separator = ' ';
414
      }
415

    
416

    
417
      $referenceArray['#separator'] = $separator;
418
      $referenceArray['#html'] = '<span class="reference">' . $citation . '</span>' . $registration_markup;
419
      array_setr('reference', $referenceArray, $renderTemplate);
420
    }
421

    
422
    // If authors have been removed from the name part the last named authorteam
423
    // should be added to the reference citation, otherwise, keep the separator
424
    // out of the reference.
425
    if (isset($renderTemplate['referencePart']['authors']) && $lastAuthorElementString) {
426
      // If the nomenclaturalReference citation is not included in the
427
      // reference part but display of the microreference
428
      // is wanted, append the microreference to the authorTeam.
429
      $citation = '';
430
      if (!isset($renderTemplate['referencePart']['reference']) && isset($renderTemplate['referencePart']['microreference'])) {
431
        $separator = ": ";
432
        $citation = $taxonName->nomenclaturalMicroReference;
433
      }
434
      $referenceArray['#html'] = ' <span class="reference">' . $lastAuthorElementString . $separator . $citation . '</span>';
435
      array_setr('authors', $referenceArray, $renderTemplate);
436
    }
437
  }
438

    
439
  $is_reference_year = false;
440
  if (isset($renderTemplate['referenceYearPart']['reference.year'])) {
441
    if(isset($taxonName->nomenclaturalReference->datePublished)){
442
      $referenceArray['#html'] = ' <span class="reference">' . timePeriodToString($taxonName->nomenclaturalReference->datePublished) . '</span>';
443
      array_setr('reference.year', $referenceArray, $renderTemplate);
444
      $is_reference_year = true;
445
    }
446
  }
447

    
448
  // Fill with status.
449
  if(isset($renderTemplate['statusPart']['status'])){
450
    if (isset($nom_status_tagged_text[0])) {
451
        array_setr('status', '<span class="nomenclatural_status">' . cdm_tagged_text_to_markup($nom_status_tagged_text, 'span', '', array('postSeparator')) . '</span>', $renderTemplate);
452
    }
453
  }
454

    
455
  if (isset($renderTemplate['secReferencePart'])){
456
    if(isset($secref_tagged_text[1])){
457
      $post_separator_markup = $is_reference_year ? '.': '';
458
      if(isset($nom_status_tagged_text[count($nom_status_tagged_text) - 1]) && ($nom_status_tagged_text[count($nom_status_tagged_text) - 1]->type ==  'postSeparator')){
459
        $post_separator_markup = cdm_tagged_text_to_markup(array($nom_status_tagged_text[count($nom_status_tagged_text) - 1 ]));
460
      };
461
      array_setr('secReference',
462
        $post_separator_markup
463
          . ' <span class="sec_reference">'
464
          . join('', cdm_tagged_text_values($secref_tagged_text))
465
          . '</span>', $renderTemplate);
466
    }
467
  }
468

    
469
  // Fill with protologues etc...
470
  $descriptionHtml = '';
471
  if (array_setr('description', TRUE, $renderTemplate)) {
472
    $descriptions = cdm_ws_get(CDM_WS_PORTAL_NAME_DESCRIPTIONS, $taxonName->uuid);
473
    foreach ($descriptions as $description) {
474
      if (!empty($description)) {
475
        foreach ($description->elements as $description_element) {
476
          $second_citation = '';
477
          if (isset($description_element->multilanguageText_L10n) && $description_element->multilanguageText_L10n->text) {
478
            $second_citation = '[& ' . $description_element->multilanguageText_L10n->text . '].';
479
          }
480
          $descriptionHtml .= $second_citation;
481
          $descriptionHtml .= cdm_description_element_media(
482
              $description_element,
483
              array(
484
                'application/pdf',
485
                'image/png',
486
                'image/jpeg',
487
                'image/gif',
488
                'text/html',
489
              )
490
          );
491

    
492
        }
493
      }
494
    }
495
    array_setr('description', $descriptionHtml, $renderTemplate);
496
  }
497

    
498
  // Render.
499
  $out = '';
500
  if(isset($_REQUEST['RENDER_PATH'])){
501
    // developer option to show the render path with each taxon name
502
    $out .= '<span class="render-path">' . RenderHints::getRenderPath() . '</span>';
503
  }
504
  $out .= '<span class="' . html_class_attribute_ref($taxon_name_or_taxon_base)
505
    . '" data-cdm-ref="/name/' . $taxonName->uuid . '" data-cdm-render-path="' . RenderHints::getRenderPath() .'">';
506

    
507
  foreach ($renderTemplate as $partName => $part) {
508
    $separator = '';
509
    $partHtml = '';
510
    $uri = FALSE;
511
    if (!is_array($part)) {
512
      continue;
513
    }
514
    if (isset($part['#uri']) && is_string($part['#uri'])) {
515
      $uri = $part['#uri'];
516
      unset($part['#uri']);
517
    }
518
    foreach ($part as $key => $content) {
519
      $html = '';
520
      if (is_array($content)) {
521
        $html = $content['#html'];
522
        if(isset($content['#separator'])) {
523
          $separator = $content['#separator'];
524
        }
525
      }
526
      elseif (is_string($content)) {
527
        $html = $content;
528
      }
529
      $partHtml .= '<span class="' . $key . '">' . $html . '</span>';
530
    }
531
    if ($uri) {
532
      // cannot use l() here since the #uri aleady should have been processed through uri() at this point
533
      $out .= $separator . '<a href="' . $uri . '" class="' . $partName . '">' . $partHtml . '</a>';
534

    
535
    }
536
    else {
537
      $out .= $separator . $partHtml;
538
    }
539
  }
540
  $out .= '</span>';
541
  if ($show_annotations) {
542
    // $out .= theme('cdm_annotations_as_footnotekeys', $taxonName);
543
  }
544
  return $out;
545
}
546

    
547
/**
548
 * @param $registrations
549
 * @return string
550
 */
551
function render_registrations($registrations)
552
{
553
  $registration_markup = '';
554
  $registration_markup_array = array();
555
  if ($registrations) {
556
    foreach ($registrations as $reg) {
557
      $registration_markup_array[] = render_registration($reg);
558
    }
559
    $registration_markup = " Registration" . (count($registration_markup_array) > 1 ? 's: ' : ': ')
560
      . join(', ', $registration_markup_array);
561
  }
562
  return $registration_markup;
563
}
564

    
565
/**
566
 * Renders the string of Homonyms for a given taxon.
567
 *
568
 * @param $taxon
569
 *    A CDM Taxon instance
570
 * @return String
571
 *    The string of homomyns
572
 *
573
 * @throws \Exception
574
 */
575
function cdm_name_relationships_of($taxon) {
576

    
577
  static $inverse_name_rels_uuids = array(UUID_NAMERELATIONSHIPTYPE_BLOCKING_NAME_FOR);
578

    
579
  // =========== START OF HOMONYMS ========== //
580
  RenderHints::pushToRenderStack('homonym');
581
  // the render stack element homonyms is being used in the default render templates !!!, see CDM_NAME_RENDER_TEMPLATES_DEFAULT
582

    
583
  $selected_name_rel_uuids = variable_get(CDM_NAME_RELATIONSHIP_TYPES, unserialize(CDM_NAME_RELATIONSHIP_TYPES_DEFAULT));
584

    
585
  $from_name_relations = cdm_ws_get(CDM_WS_PORTAL_TAXON_FROM_NAMERELATIONS,
586
    $taxon->uuid);
587
  $to_name_relations = cdm_ws_get(CDM_WS_PORTAL_TAXON_TO_NAMERELATIONS,
588
    $taxon->uuid);
589
  $name_relations = array_merge($from_name_relations, $to_name_relations);
590

    
591
  $homonyms_array = array();
592

    
593
  if ($name_relations) {
594

    
595
    foreach ($name_relations as $element) {
596

    
597
      if(!(isset($selected_name_rel_uuids[$element->type->uuid]) && $selected_name_rel_uuids[$element->type->uuid])){
598
        // skip if not selected in the settings
599
        continue;
600
      }
601
      $taxon_html = null;
602
      if(array_search($element->type->uuid, $inverse_name_rels_uuids) !== false) {
603
        // case of UUID_NAMERELATIONSHIPTYPE_BLOCKING_NAME_FOR
604
        //to relation ships -> only shown at toName-synonym
605
        $elementTaxonBasesUuid = isset ($element->fromName->taxonBases [0]->uuid) ? $element->fromName->taxonBases [0]->uuid : '';
606
        if ($taxon->name->uuid == $element->toName->uuid) {
607
          $taxon_html = render_taxon_or_name($element->fromName,
608
            url(path_to_name($element->fromName->uuid, $taxon->uuid, $elementTaxonBasesUuid))
609
          );
610
        }
611

    
612
      } else {
613
        $elementTaxonBasesUuid = isset ($element->toName->taxonBases [0]->uuid) ? $element->toName->taxonBases [0]->uuid : '';
614
        //from relation ships -> only shown at fromName-synonym
615
        if ($taxon->name->uuid == $element->fromName->uuid) {
616
          $taxon_html = render_taxon_or_name($element->toName,
617
            url(path_to_name($element->toName->uuid, $taxon->uuid, $elementTaxonBasesUuid))
618
          );
619
        }
620
      }
621

    
622
      if($taxon_html){
623
        if (count($homonyms_array)) {
624
          // lat: "non nec" == german: "weder noch"
625
          $homonyms_array [] = 'nec ' . $taxon_html;
626
        } else {
627
          $homonyms_array [] = 'non ' . $taxon_html;
628
        }
629
      }
630

    
631
    }
632
  }
633

    
634
  RenderHints::popFromRenderStack();
635
  return (count($homonyms_array) ?'[' . trim(join(" ", $homonyms_array)) . ']' : '');
636
}
637

    
638

    
639
  /**
640
 * Recursively searches the array for the $key and sets the given value.
641
 *
642
 * @param mixed $key
643
 *   Key to search for.
644
 * @param mixed $value
645
 *   Value to set.'
646
 * @param array $array
647
 *   Array to search in.
648
 *
649
 * @return bool
650
 *   True if the key has been found.
651
 */
652
function &array_setr($key, $value, array &$array) {
653
  $res = NULL;
654
  foreach ($array as $k => &$v) {
655
    if ($key == $k) {
656
      $v = $value;
657
      return $array;
658
    }
659
    elseif (is_array($v)) {
660
      $innerArray = array_setr($key, $value, $v);
661
      if ($innerArray) {
662
        return $array;
663
      }
664
    }
665
  }
666
  return $res;
667
}
668

    
669
/**
670
 * @todo Please document this function.
671
 * @see http://drupal.org/node/1354
672
 */
673
function &get_preceding_contentElement($contentElementKey, array &$renderTemplate) {
674
  $res = NULL;
675
  $precedingElement = NULL;
676
  foreach ($renderTemplate as &$part) {
677
    foreach ($part as $key => &$element) {
678
      if ($key == $contentElementKey) {
679
        return $precedingElement;
680
      }
681
      $precedingElement = $element;
682
    }
683
  }
684
  return $res;
685
}
686

    
687
/**
688
 * @todo Please document this function.
689
 * @see http://drupal.org/node/1354
690
 */
691
function &get_preceding_contentElementKey($contentElementKey, array &$renderTemplate) {
692
  $res = NULL;
693
  $precedingKey = NULL;
694
  foreach ($renderTemplate as &$part) {
695
    if (is_array($part)) {
696
      foreach ($part as $key => &$element) {
697
        if ($key == $contentElementKey) {
698
          return $precedingKey;
699
        }
700
        if (!str_beginsWith($key, '#')) {
701
          $precedingKey = $key;
702
        }
703
      }
704
    }
705
  }
706
  return $res;
707
}
708

    
709
function nameTypeToDTYPE($dtype){
710
  static $nameTypeLabelMap = array(
711
    "ICNB" => "BacterialName",
712
    "ICNAFP" => "BotanicalName",
713
    "ICNCP" => "CultivarPlantName",
714
    "ICZN" => "ZoologicalName",
715
    "ICVCN" => "ViralName",
716
    "Any taxon name" => "TaxonName",
717
    "NonViral" => "TaxonName",
718
    "Fungus" => "BotanicalName",
719
    "Plant" => "BotanicalName",
720
    "Algae" => "BotanicalName",
721
  );
722
  return $nameTypeLabelMap[$dtype];
723

    
724
}
(5-5/10)