Project

General

Profile

Download (26.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Functions for dealing with CDM entities from the package model.common
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
 * @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
 * Composes an HTML element class attribute value composed of
36
 * the short-name of the cdm class and the uuid of the entity.
37
 * This class attribute should be used wherever an cdm-entity is rendered.
38
 *
39
 * In case of Taxon entities or TaxonNodeDTOs the secReference is also added
40
 * to the class attributes as 'sec_uuid:<uuid>'. In case of TaxonNodeDTOs the
41
 * Taxon uuid is added also as taxon_uuid:<uuid>
42
 *
43
 * These according class selectors in css must be escaped, eg:
44
 *    .cdm\:TextData
45
 *
46
 * @param $cdm_entity
47
 *    A CDM entity, TaxonNodeDTO or TypedEntityReference
48
 */
49
function html_class_attribute_ref($cdm_entity) {
50

    
51
  $attributes = '';
52
  if (is_cdm_entity($cdm_entity)) {
53
    $attributes =  "cdm:" . $cdm_entity->class . " uuid:" . $cdm_entity->uuid;
54
  } elseif (isset($cdm_entity->class)) {
55
    switch ($cdm_entity->class){
56
      case 'TypedEntityReference':
57
        $attributes =  "cdm:" . $cdm_entity->type . " uuid:" . $cdm_entity->uuid;
58
      break;
59
      case 'TaxonNodeDto':
60
        $attributes .= " taxon_uuid:"  . $cdm_entity->taxonUuid . " sec_uuid:"  . $cdm_entity->secUuid;;
61
      break;
62
      case 'Taxon':
63
        if(isset($cdm_entity->sec->uuid)){
64
        $attributes .= " sec_uuid:"  . $cdm_entity->sec->uuid;
65
      }
66
      break;
67
    }
68
  }
69

    
70
  return $attributes;
71
}
72

    
73

    
74
/**
75
 * Compose an render array from a CDM Marker object.
76
 *
77
 * compose_hook() implementation
78
 *
79
 * @param object $marker
80
 *   CDM instance of type Marker
81
 * @return array
82
 *   A drupal render array
83
 *
84
 * @ingroup compose
85
 */
86
function compose_cdm_marker($marker) {
87

    
88
  $render_array = array(
89
      // ---- generic
90
      //  these entries should be common to all cdm enitiy render arrays
91
      '#theme' => 'cdm_marker', // TODO   add alternative theme funcitons: 'cdm_marker_' . marker.type.label
92
      '#attributes' => array('class' => html_class_attribute_ref($marker)),
93

    
94
      // ---- individual
95
      '#label' => $marker->markerType->representation_L10n . ': ' . (($marker->flag !== TRUE ? t('yes') : t('no'))),
96
  );
97

    
98
  return $render_array;
99
}
100

    
101
/**
102
 * Checks if the given $cdm_entitiy has a marker the type references by the
103
 * $marker_type_uuid and returns TRUE if a matching marker has been found.
104
 *
105
 * @param object $cdm_entitiy A CDM Entity
106
 * @param string $marker_type_uuid
107
 */
108
function cdm_entity_has_marker($cdm_entitiy, $marker_type_uuid) {
109
  if(isset($cdm_entitiy->markers[0]) && !is_uuid($marker_type_uuid)){
110
    foreach ($cdm_entitiy->markers as $marker) {
111
      if(isset($marker->markerType) && $marker->markerType->uuid == $marker_type_uuid){
112
        return TRUE;
113
      }
114
    }
115
  }
116
  return FALSE;
117
}
118

    
119
/**
120
 * Sorts an array of CDM IdentifiableSource instances by 1. by the
121
 * author teams family names and 2. by the publication date.
122
 *
123
 * @param array $sources
124
 *    The array of CDM IdentifiableSource instances
125
 * @return array
126
 *  An array of drupal render arrays
127
 */
128
function oder_and_render_original_sources($sources){
129
    $sort_array = array();
130
    foreach ($sources as $source) {
131

    
132
      $order_key = '';
133

    
134
      // find the familynames
135
      if(isset($source->citation->uuid) && !isset($source->citation->authorship)){
136
        $authorteam = cdm_ws_get(CDM_WS_REFERENCE_AUTHORTEAM, $source->citation->uuid);
137

    
138
        $persons = array();
139
        if($authorteam->class == 'Team'){
140
          if(isset($authorteam->teamMembers)){
141
            $persons = $authorteam->teamMembers;
142
          }
143
        } else {
144
          $persons[] = $authorteam;
145
        }
146

    
147
        foreach($persons as $person){
148
          if(!empty($person->lastname)){
149
            $order_key .= $person->lastname;
150
          } else {
151
            $order_key .= $person->titleCache;
152
          }
153
        }
154
        if(empty($order_key)){
155
          $order_key = $authorteam->titleCache;
156
        }
157

    
158
      }
159
      $order_key = str_pad($order_key, 50);
160

    
161
      // add publication date to the key
162
      if(isset($source->citation->datePublished)){
163
        $order_key .= '_' . timePeriodAsOrderKey($source->citation->datePublished);
164
      } else {
165
        $order_key .= '_' . "0000";
166
      }
167

    
168
      // padd key until unique
169
      while(array_key_exists($order_key, $sort_array)){
170
        $order_key .= "_";
171
      }
172

    
173
      $sort_array[$order_key] = render_original_source($source);
174
    }
175
    ksort($sort_array);
176
    return array_values ($sort_array);
177
}
178

    
179
/**
180
 * Compare callback to be used in usort to sort image sources of CDM OriginalSource instances.
181
 *
182
 * TODO the compare strategy implemented in oder_and_render_original_sources() is probably better but is not taking the
183
 * originalName into account.
184
 *
185
 * @param $a
186
 * @param $b
187
 */
188
function compare_original_sources($a, $b){
189

    
190
  $a_string = '';
191
  if(isset($a->citation->titleCache)) {
192
    $a_string = $a->citation->titleCache;
193
  }
194
  if((isset($a->nameUsedInSource))){
195
    $a_string .= $a->nameUsedInSource->titleCache;
196
  } elseif (isset($a->originalNameString)){
197
    $a_string .= $a->originalNameString;
198
  }
199

    
200
  $b_string = '';
201
  if(isset($b->citation->titleCache)) {
202
    $b_string = $b->citation->titleCache;
203
  };
204
  if((isset($b->nameUsedInSource))){
205
    $b_string .= $b->nameUsedInSource->titleCache;
206
  } elseif (isset($b->originalNameString)){
207
    $b_string .= $b->originalNameString;
208
  }
209

    
210
  if ($a_string == $b_string) {
211
    return 0;
212
  }
213
  return ($a_string < $b_string) ? -1 : 1;
214
}
215

    
216
/**
217
 * Compare callback to be used in usort to sort image sources of CDM Media instances.
218
 *
219
 * @param $a
220
 * @param $b
221
 */
222
function compare_text_data($a, $b) {
223

    
224
  if ($a->multilanguageText_L10n->text == $b->multilanguageText_L10n->text) {
225
    return 0;
226
  }
227
  return ($a->multilanguageText_L10n->text < $b->multilanguageText_L10n->text) ? -1 : 1;
228
}
229

    
230
  /**
231
   * Compare two different footnotes objects.
232
   *
233
   * The comparison is based on the footnote key. The one which is
234
   * displayed as footnote number.
235
   *
236
   * @param mixed $a
237
   *   Footnote object $a.
238
   * @param mixed $b
239
   *   Footnote object $b.
240
   */
241
  function footnotes_key_compare($a, $b) {
242
    $res = 0;
243
    if (empty($a) || empty($b)) {
244
      return $res;
245
    }
246
    if ($a->keyStr < $b->keyStr) {
247
      $res = -1;
248
    }
249
    elseif ($a->keyStr > $b->keyStr) {
250
      $res = 1;
251
    }
252
    return $res;
253
  }
254

    
255
/**
256
 * Provides an explanatory text on the statistical values representation as generated by statistical_values()
257
 *
258
 * @return string
259
 *     the text
260
 */
261
  function statistical_values_explanation(){
262
    return t("A single or the first number in square brackets denotes sample size");
263
  }
264

    
265
/**
266
 * Creates an array suitable to be used in statistical_values()
267
 * The min max structure is suitable for being used in the context
268
 * of GatheringEvents and StatisticalMeasures (see render_quantitative_statistics()).
269
 *
270
 * The order of the items is important for the display and must not be changed.
271
 *
272
 * @return array
273
 */
274
function statistical_values_array() {
275

    
276
  $min_max = [
277
    'Min' => NULL,
278
    'TypicalLowerBoundary' => NULL,
279
    'TypicalUpperBoundary' => NULL,
280
    'Max' => NULL,
281
    'SampleSize' => NULL,
282
    'Average' => NULL,
283
    'Variance' => NULL,
284
    'StandardDeviation' => NULL,
285
    'ExactValue' => NULL
286
  ];
287
  return $min_max;
288
}
289

    
290
/**
291
 * Creates markup from a min max array.
292
 *
293
 * NOTE: use  statistical_values_array() to create an appropriate array
294
 *
295
 * Internally Min will be translated to TypicalLowerBoundary if no such value is present.
296
 * The same also accounts for Max and TypicalUpperBoundary.
297
 *
298
 * For further details see #3742, #8766
299
 *
300
 * @param $stat_vals_arr
301
 *  the statistical values array as produced by statistical_values_array()
302
 * @param $unit
303
 *  Defaults to no unit
304
 * @return string
305
 */
306
function statistical_values($stat_vals_arr, $unit = '') {
307

    
308
  static $xbar_equals = 'x&#772;='; // x&#772; is x-bar (http://www.personal.psu.edu/ejp10/blogs/gotunicode/2010/03/dealing-with-x-bar-x-and-p-hat.html)
309

    
310
  $min_max_markup = '';
311
  $other_vals_array = [];
312

    
313
  if(statistical_values_is_numeric($stat_vals_arr['ExactValue'])){
314
    // ExactValue has just been introduces as new statistical measure
315
    //  --> translating ExactValue to TypicalLowerBoundary
316
    // TODO this is only a quick temporary solution which should be sofisticated
317
    $stat_vals_arr['TypicalLowerBoundary'] = $stat_vals_arr['ExactValue'];
318
    unset($stat_vals_arr['ExactValue']);
319
  }
320

    
321
  // --- sanitize values
322
  if(statistical_values_num_equals($stat_vals_arr, 'Min', 'TypicalLowerBoundary')){
323
    $stat_vals_arr['Min'] = NULL;
324
  }
325

    
326
  if(statistical_values_num_equals($stat_vals_arr, 'Max', 'TypicalUpperBoundary')){
327
    $stat_vals_arr['Max'] = NULL;
328
  }
329

    
330
  if($stat_vals_arr['TypicalLowerBoundary'] === null && $stat_vals_arr['Min'] !== null){
331
    $stat_vals_arr['TypicalLowerBoundary'] = $stat_vals_arr['Min'];
332
    $stat_vals_arr['Min'] = NULL;
333
  }
334

    
335
  if($stat_vals_arr['TypicalUpperBoundary'] === null && $stat_vals_arr['Max']  !== null){
336
    $stat_vals_arr['TypicalUpperBoundary'] = $stat_vals_arr['Max'];
337
    $stat_vals_arr['Max'] = NULL;
338
  }
339

    
340
  if (statistical_values_num_equals($stat_vals_arr, 'TypicalUpperBoundary', 'TypicalLowerBoundary')) {
341
    $stat_vals_arr['Average'] = $stat_vals_arr['TypicalUpperBoundary'];
342
    $stat_vals_arr['TypicalLowerBoundary'] = NULL;
343
    $stat_vals_arr['TypicalUpperBoundary'] = NULL;
344
  }
345

    
346
  // --- check for inconsistent cases, eg. only Max and average given
347
  if ($stat_vals_arr['TypicalLowerBoundary'] === NULL && $stat_vals_arr['TypicalUpperBoundary']  !== null) {
348
    // min missing
349
    $stat_vals_arr['TypicalLowerBoundary'] = '?';
350
  }
351
  if ($stat_vals_arr['TypicalLowerBoundary'] !== null && $stat_vals_arr['TypicalUpperBoundary'] === NULL) {
352
    // max missing
353
    $stat_vals_arr['TypicalUpperBoundary'] = '?';
354
  }
355

    
356
  if(statistical_values_is_numeric($stat_vals_arr['Average']) && statistical_values_is_numeric($stat_vals_arr['TypicalUpperBoundary']) && statistical_values_is_numeric($stat_vals_arr['TypicalLowerBoundary'])) {
357
    statistical_values_adjust_significant_figures($stat_vals_arr['Average'], $stat_vals_arr['TypicalLowerBoundary'], $stat_vals_arr['TypicalUpperBoundary']);
358
  }
359

    
360
  foreach ($stat_vals_arr as $key => $statistical_val) {
361

    
362
    if ($statistical_val !== NULL) {
363
      if ($statistical_val == '?') {
364
        $val_markup = $statistical_val;
365
      }
366
      else {
367
        if (is_numeric($statistical_val->_value)) {
368
          $val_markup = '<span class="'
369
            . html_class_attribute_ref($statistical_val) . ' '
370
            . (isset($statistical_val->type) ? $statistical_val->type->termType : '') . ' ' . $key . '" title="' . $key . '">'
371
            . $statistical_val->_value . '</span>';
372

    
373
        }
374
        else {
375
          $val_markup = NULL;
376
        }
377
      }
378

    
379
      if ($val_markup) {
380
        switch ($key) {
381
          // ---- min_max_element
382
          case 'Min':
383
            $min_max_markup .= "($val_markup&ndash;)";
384
            break;
385
          case 'Max':
386
            $min_max_markup .= "(&ndash;$val_markup)";
387
            break;
388
          case 'TypicalLowerBoundary':
389
            $min_max_markup .= "$val_markup";
390
            break;
391
          case 'TypicalUpperBoundary':
392
            $min_max_markup .= "&ndash;$val_markup";
393
            break;
394
          // ---- other values
395
          case 'SampleSize':
396
            $other_vals_array[$key] = $val_markup;
397
            break;
398
          case 'Average':
399
            $other_vals_array[$key] = $xbar_equals . $val_markup;
400
            break;
401
          case 'Variance':
402
            $other_vals_array[$key] = 'σ²=' . $val_markup;
403
            break;
404
          case 'StandardDeviation':
405
            $other_vals_array[$key] = 'σ=' . $val_markup;
406
            break;
407
        }
408
      }
409
    }
410
  }
411

    
412
  if(!$min_max_markup && !empty($other_vals_array['Average'])){
413
    // this could be the case in which we only have one value for Average
414
    // this trivial case needs to be displayed a simpler way
415
    $min_max_markup = str_replace($xbar_equals, '' , $other_vals_array['Average']);
416
    unset($other_vals_array['Average']);
417
  }
418
  if($unit){
419
    $min_max_markup .= ' ' . $unit;
420
  }
421
  if(count($other_vals_array)){
422
    $min_max_markup .= ' [' . join(';', $other_vals_array) . ']';
423
  }
424

    
425
  return $min_max_markup;
426
}
427

    
428
/**
429
 * Calculates the required precision for the target value to be significantly different from min and may and rounds it.
430
 *
431
 * @param $target
432
 *    The statistical value to be rounded to the least significant precision
433
 * @param $min
434
 *    The lower bound to calculate the least significant precision
435
 * @param $max
436
 *    The upper bound to calculate the least significant precision
437
 * @param int $threshold
438
 *    Per default 1, but can be set to any other value > 0 and < 1 to define
439
 *    another threshold for the transition to the next precision level.
440
 *    E.g. A value of 0.5 will cause values > 50 and <= 500 to be shown with
441
 *    a precision of 2, whereas with a threshold of 1 the values > 10 and <= 100
442
 *    will be shown with a precision of 2 figures
443
 */
444
function statistical_values_adjust_significant_figures(&$target, $min,  $max, $threshold = 1){
445

    
446
  $precision = 1;
447
  if($min->_value !== $max->_value){
448
    $precision = floor(log10(abs($max->_value - $min->_value) * (1 / $threshold)));
449
    // increase precision by one
450
    $precision += $precision < 0 ? - 1 : 1;
451
  }
452

    
453
  $target->_value = sigFig($target->_value, $precision);
454
}
455

    
456
/**
457
 * based on an idea taken from https://stackoverflow.com/questions/37618679/format-number-to-n-significant-digits-in-php#answer-48283297
458
 *
459
 * @param $value
460
 * @param $digits
461
 *
462
 * @return float|string
463
 */
464
function sigFig($value, $digits, $round_only = true)
465
{
466
  if ($value == 0) {
467
    $decimalPlaces = $digits - 1;
468
  } elseif ($value < 0) {
469
    $decimalPlaces = $digits - floor(log10($value * -1)) - 1;
470
  } else {
471
    $decimalPlaces = $digits - floor(log10($value)) - 1;
472
  }
473

    
474
  $answer = ($decimalPlaces > 0) && !$round_only ?
475
    number_format($value, $decimalPlaces) : round($value, abs($decimalPlaces));
476
  return $answer;
477
}
478

    
479

    
480

    
481
/**
482
 * Used internally in statistical_values() do determine numerically equality of stat_vals_arr values
483
 *
484
 * @param $stat_vals_arr
485
 * @param $key1
486
 * @param $key2
487
 *
488
 * @return bool
489
 */
490
function statistical_values_num_equals($stat_vals_arr, $key1, $key2){
491

    
492
  return $stat_vals_arr[$key1] !== NULL && $stat_vals_arr[$key2] !== NULL && $stat_vals_arr[$key1]->_value == $stat_vals_arr[$key2]->_value && is_numeric($stat_vals_arr[$key1]->_value);
493
}
494

    
495
function statistical_values_is_numeric($stat_val){
496
  return $stat_val !== null && is_numeric($stat_val->_value);
497
}
498

    
499
/**
500
 * Creates min max markup to represent a min-average-max measure optionally with an error value.
501
 *
502
 * The fields that are taken into account are:
503
 * - field_base_name = min
504
 * - field_base_nameMax = max
505
 * - field_base_nameText = free text
506
 * - field_base_nameError = error value
507
 *
508
 * @param $object
509
 *    The object having min max measurement fields e.g.: GatheringEvent
510
 * @param string $field_base_name
511
 *    The base name for all measurement fields. This name is at the same time the full name of the
512
 *    min value.
513
 * @return string
514
 *   The markup for the min max
515
 *
516
 * @see statistical_values()
517
 */
518
function statistical_values_from_gathering_event($object, $field_base_name)
519
{
520
  static $default_unit = 'm';
521

    
522
  $field_name = $field_base_name . 'Text';
523
  if (@is_string($object->$field_name)) {
524
    // Freetext overrides all other data
525
    $min_max_markup = ' ' . $object->$field_name;
526
  } else {
527
    // create markup for the atomized min max data
528
    $min_max_array = statistical_values_array();
529
    if (@is_numeric($object->$field_base_name)) {
530
      $min_max_array['Min'] = new stdClass();
531
      $min_max_array['Min']->_value = $object->$field_base_name;
532
    }
533
    $field_name = $field_base_name . 'Max';
534
    if (@is_numeric($object->$field_name)) {
535
      $min_max_array['Max'] = new stdClass();
536
      $min_max_array['Max']->_value = $object->$field_name;
537
    }
538
    $min_max_markup = statistical_values($min_max_array, $default_unit);
539
  }
540

    
541
  return $min_max_markup;
542
}
543

    
544
// TODO  move below code into new file: agent.inc
545

    
546
/*
547
 * Compose an render array from a CDM TaxonNodeAgentRelation object as Taxon Expert.
548
 *
549
 * compose_hook() implementation
550
 *
551
 * @param object $taxon_node_agent_relation
552
 *   CDM instance of type TaxonNodeAgentRelation
553
 * @return array
554
 *   A drupal render array
555
 *
556
 * @ingroup compose
557
 */
558
function compose_cdm_taxon_expert($taxon_node_agent_relation) {
559

    
560
  $agent_details = null;
561

    
562
  $label_suffix = ':';
563

    
564
  if($taxon_node_agent_relation->class == 'DefaultPagerImpl'){
565
    // oops this is a pager
566
    // this situation will occur when this compose is executed
567
    // through the proxy_content() method
568
    $taxon_node_agent_relation = $taxon_node_agent_relation->records[0];
569

    
570
  }
571

    
572
  if(is_object($taxon_node_agent_relation->agent)) {
573
    $agent_details = compose_cdm_team_or_person_base($taxon_node_agent_relation->agent);
574
    // all data will be added to the groups of the agent_details render array
575
    $groups = &$agent_details[0]['#groups'];
576

    
577
    @_description_list_group_add($groups, t('Role'). $label_suffix, $taxon_node_agent_relation->type->representation_L10n);
578

    
579
    $family_tnars = cdm_ws_fetch_all(CDM_WS_PORTAL_AGENT . '/' . $taxon_node_agent_relation->agent->uuid . '/taxonNodeAgentRelations', array("rank"=>"Familia"));
580

    
581
    $taxa_markup = array(
582
      '#theme_wrappers' => array('container'),
583
      '#attributes' => array('class' => array('managed_taxa')),
584
      '#wrapper_attributes' => array('class' => 'sublist-container')
585
      );
586
    foreach($family_tnars as $tnar){
587
      if(is_object($tnar->taxonNode->taxon)){
588
        $taxa_markup[$tnar->taxonNode->taxon->titleCache] = markup_to_render_array(render_taxon_or_name($tnar->taxonNode->taxon, url(path_to_taxon($tnar->taxonNode->taxon->uuid))));
589
      }
590
    }
591
    ksort($taxa_markup);
592

    
593
    @_description_list_group_add($groups, t('Families'). $label_suffix, array($taxa_markup));
594

    
595
  }
596

    
597
  return $agent_details;
598
}
599

    
600

    
601
/*
602
 * Compose an render array from a CDM TeamOrPersonBase object.
603
 *
604
 * compose_hook() implementation
605
 *
606
 * TODO: currently mainly implemented for Agent, add Team details
607
 *
608
 * @param object $team_or_person
609
 *   CDM instance of type TeamOrPersonBase
610
 * @return array
611
 *   A drupal render array
612
 *
613
 * @ingroup compose
614
 */
615
function compose_cdm_team_or_person_base($team_or_person, $data = array()) {
616

    
617
  $groups = array();
618

    
619
  $label_suffix = ':';
620

    
621
  // $weight = 0;
622
  if($team_or_person){
623

    
624
    if(is_object($team_or_person->lifespan)){
625
      // ToDo render as date* - date† ?
626
      @_description_list_group_add($groups, t('Lifespan'). $label_suffix, timePeriodToString($team_or_person->lifespan) /*, '' , $weight++ */);
627
    }
628

    
629
    // nomenclaturalTitle
630
    @_description_list_group_add($groups, "Nomenclatural Title". $label_suffix, $team_or_person->nomenclaturalTitle);
631
    // collectorTitle
632
    @_description_list_group_add($groups, "Collector Title". $label_suffix, $team_or_person->collectorTitle);
633

    
634
    // institutionalMemberships
635
    if(is_array($team_or_person->institutionalMemberships)){
636

    
637
      $institutes_ra =  array();
638
      foreach($team_or_person->institutionalMemberships as $membership) {
639
        $membership_groups = array();
640
        @_description_list_group_add($membership_groups, t('Department'). $label_suffix, $membership->department);
641
        @_description_list_group_add($membership_groups, t('Role'). $label_suffix, $membership->role);
642
        if(is_object($membership->period)){
643
          @_description_list_group_add($membership_groups, t('Period'). $label_suffix, timePeriodToString($membership->period));
644
        }
645
        if(is_object($membership->institute->contact)){
646
          $institute_contact_details = compose_cdm_contact($membership->institute->contact, $membership->institute->titleCache);
647
          if(is_array($institute_contact_details[0]['#groups'])){
648
            $membership_groups = array_merge($membership_groups, $institute_contact_details[0]['#groups']);
649
          }
650
        }
651
        if(count($membership_groups) > 0){
652
          $institutes_ra[]  = array(
653
            '#title' => $membership->institute->titleCache,
654
            '#theme' => 'description_list',
655
            '#groups' => $membership_groups,
656
            '#attributes' => array('class' => html_class_attribute_ref($membership)),
657
            '#wrapper_attributes' => array('class' => 'sublist-container')
658
          );
659
        } else {
660
          // no further details for the membership, display the title
661
          $institutes_ra[] = markup_to_render_array('<h3>' . $membership->institute->titleCache . '</h3>');
662
        }
663

    
664
      }
665

    
666
      $label = count($institutes_ra) > 1 ? t('Institutes'):  t('Institute');
667
      @_description_list_group_add($groups, $label. $label_suffix, $institutes_ra /*, '' , $weight++ */);
668
    }
669

    
670

    
671
    // Contact
672
    $agent_contact_details = compose_cdm_contact($team_or_person->contact, $team_or_person->titleCache);
673
    if(is_array($agent_contact_details[0]['#groups'])){
674
      $groups = array_merge($groups, $agent_contact_details[0]['#groups']);
675
    }
676

    
677
    // additional data
678
    foreach($data as $key=>$value){
679
      @_description_list_group_add($sub_dl_groups, t('@key', array('@key' => $key)), $value /*, '' , $weight++ */);
680
    }
681

    
682
  }
683

    
684
  $team_or_person_details = array(
685
    '#title' => $team_or_person->titleCache,
686
    '#theme' => 'description_list',
687
    '#groups' => $groups,
688
    '#attributes' => array('class' => html_class_attribute_ref($team_or_person)),
689
  );
690
  return array($team_or_person_details);
691
}
692

    
693

    
694
/*
695
 * Compose an render array from a CDM Contact object.
696
 *
697
 * compose_hook() implementation
698
 *
699
 * TODO: currently mainly implemented for Agent, add Team details
700
 *
701
 * @param object $contact
702
 *   CDM instance of type Contact
703
 * @param $title
704
 *   The title for the description list header
705
 * @param $weight
706
 *   Optional weight for the description list entries
707
 * @return array
708
 *   A drupal render array
709
 *
710
 * @ingroup compose
711
 */
712
function compose_cdm_contact($contact, $title, $weight = 0)
713
{
714

    
715
  $groups = array();
716

    
717
  $contact_details = null;
718

    
719
  $label_suffix = ':';
720

    
721
  $contact_field_names_map = array(
722
    'emailAddresses' => t('Email'),
723
    'urls' => t('Urls'),
724
    'phoneNumbers' => t('Phone'),
725
    'faxNumbers' => t('Fax'),
726
  );
727

    
728
  // Contact
729
  if(is_object($contact)){
730
    if(isset($contact->addresses)){
731
      // TODO ....
732
      // $sub_groups = array();
733
      // foreach($contact->addresses as $address){
734
      //   @_description_list_group_add($sub_groups, $label, $contact->$fieldName, '', $weight++);
735
      // }
736
    }
737
    foreach($contact_field_names_map as $fieldName => $label){
738
      if(is_array($contact->$fieldName)){
739
        @_description_list_group_add($groups, $label . $label_suffix, $contact->$fieldName, '', $weight++);
740
      }
741
    }
742
    $contact_details = array(
743
      '#title' => $title,
744
      '#theme' => 'description_list',
745
      '#groups' => $groups
746
    );
747

    
748

    
749
  } else if(is_string($title)) {
750
    // if the contact entity is empty but the title is given anyway
751
    // we are only adding the title, using the description_list
752
    // structure is not possible since it would be empty due to
753
    // missing group data
754
    $contact_details = array('#markup' => '<h3>' . $title . '</h3>');
755
  }
756

    
757
  return array($contact_details);
758

    
759
}
760

    
761
/**
762
 * Compose an render array from a CDM Extension objects.
763
 *
764
 * @param $extensions
765
 *    An array of CDM Extension objects
766
 * @return array
767
 *   A render array containing the fields of the supplied $sequence
768
 *
769
 * @ingroup compose
770
 */
771
function compose_extensions($extensions)
772
{
773
  $extensions_render_array= null;
774
  $extensions_by_type = array();
775
  foreach ($extensions as $extension) {
776
    if (@is_string($extension->value)) {
777
      if (!isset($extensions_by_type[$extension->type->representation_L10n])) {
778
        $extensions_by_type[$extension->type->representation_L10n] = array();
779
      }
780
      $extensions_by_type[$extension->type->representation_L10n][] = markup_to_render_array($extension->value);
781
    }
782
  }
783

    
784
  if (count($extensions_by_type)) {
785
    $sub_dl_groups = array();
786
    foreach ($extensions_by_type as $type_label => $text_list) {
787
      @_description_list_group_add($sub_dl_groups, $type_label . ':', $text_list);
788
    }
789
    $extensions_render_array = array(
790
      array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
791
    );
792
    return $extensions_render_array;
793
  }
794
  return $extensions_render_array;
795
}
796

    
797
function formatParams($params) {
798
    $paramString = null;
799
    if (is_array($params)){
800
        $keys =array_keys($params);
801
        $paramString = '';
802
        foreach ($keys as $k ){
803
            if ($k != 'pageNumber' && $k != 'pageSize'){
804
                $paramString .= ' -'.$k.'='.urlencode($params[$k]);
805
            }
806
        }
807
    }
808
    return $paramString;
809
}
810

    
811
function formatWSParams($params) {
812
  $paramString = null;
813
    if (is_array($params)){
814
        $keys =array_keys($params);
815
        $paramString = '';
816
        foreach ($keys as $k ){
817
            if ($k != 'pageNumber' && $k != 'pageSize'){
818
                $paramString .= '&'.$k.'='.urlencode($params[$k]);
819
            }
820
        }
821
    }
822
    return $paramString;
823
}
824

    
825
/**
826
 *
827
 * @param $cdm_entity
828
 *
829
 * @return string the markup
830
 */
831
function render_cdm_entity_link($cdm_entity) {
832

    
833
  switch ($cdm_entity->class) {
834
    case 'TaxonDescription':
835
    case 'NameDescription':
836
    case 'SpecimenDescription':
837
      $link =  '<span class="' . html_class_attribute_ref($cdm_entity) . '">' . $cdm_entity->titleCache . '</span> ' . icon_link(path_to_description($cdm_entity->uuid));
838
      break;
839
    default:
840
      $link = '<span class="error">UNSUPPORTED CDM ENTITY TYPE</span>';
841
  }
842
  return $link;
843
}
844

    
845
/**
846
 * Creates an icon which links to the given path
847
 * @param $path
848
 *
849
 * @return string
850
 */
851
function icon_link($path, $fragment = '') {
852
  $iconlink = l(custom_icon_font_markup('icon-interal-link-alt-solid', ['class' => ['superscript']]), $path, ['html' => TRUE, 'fragment' => $fragment] );
853
  return $iconlink;
854
}
(1-1/11)