Project

General

Profile

Download (25.6 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 $sec
254
 *    the sec reference of a taxon having this name (optional)
255
 * @param $nameLink
256
 *    URI to the taxon, @see path_to_taxon(), must be processed by url() before passing to this method
257
 * @param $refenceLink
258
 *    URI to the reference, @see path_to_reference(), must be processed by url() before passing to this method
259
 * @param $show_annotations
260
 *    turns the display of annotations on
261
 * @param $is_type_designation
262
 *    To indicate that the supplied taxon name is a name type designation.
263
 * @param $skiptags
264
 *    an array of name elements tags like 'name', 'rank' to skip. The name part
265
 *          'authors' will not ber affected by this filter. This part is managed though the render template
266
 *          mechanism.
267
 * @param $is_invalid
268
 *   Indicates that this taxon is invalid. In this case the name part will be shown in double quotes.
269
 *   This is useful when rendering taxon relation ships.
270
 *
271
 * @return string
272
 *  The markup for a taxon name.
273
 *
274
 */
275
function render_taxon_or_name($taxon_name_or_taxon_base, $nameLink = NULL, $refenceLink = NULL,
276
  $show_annotations = true, $is_type_designation = false, $skiptags = array(), $is_invalid = false) {
277

    
278
  $is_doubtful = false;
279

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

    
291

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

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

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

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

    
310
  normalize_tagged_text($taggedTitle);
311

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

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

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

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

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

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

    
342

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

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

    
372

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

    
377
  // Fill name into $renderTemplate.
378
  array_setr('name', $name , $renderTemplate);
379

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

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

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

    
394
    // default separator
395
    $separator = '';
396

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

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

    
418

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

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

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

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

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

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

    
494
        }
495
      }
496
    }
497
    array_setr('description', $descriptionHtml, $renderTemplate);
498
  }
499

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

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

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

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

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

    
579
  static $inverse_name_rels_uuids = array(UUID_NAMERELATIONSHIPTYPE_BLOCKING_NAME_FOR);
580

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

    
585
  $selected_name_rel_uuids = variable_get(CDM_NAME_RELATIONSHIP_TYPES, unserialize(CDM_NAME_RELATIONSHIP_TYPES_DEFAULT));
586

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

    
593
  $homonyms_array = array();
594

    
595
  if ($name_relations) {
596

    
597
    foreach ($name_relations as $element) {
598

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

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

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

    
633
    }
634
  }
635

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

    
640

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

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

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

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

    
726
}
(5-5/10)