Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / modules / cdm_dataportal / includes / common.inc @ 8867b63b

History | View | Annotate | Download (25.8 KB)

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
  ];
286
  return $min_max;
287
}
288

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

    
307
  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)
308

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

    
312
  // --- sanitize values
313
  if(statistical_values_num_equals($stat_vals_arr, 'Min', 'TypicalLowerBoundary')){
314
    $stat_vals_arr['Min'] = NULL;
315
  }
316

    
317
  if(statistical_values_num_equals($stat_vals_arr, 'Max', 'TypicalUpperBoundary')){
318
    $stat_vals_arr['Max'] = NULL;
319
  }
320

    
321
  if($stat_vals_arr['TypicalLowerBoundary'] === null && $stat_vals_arr['Min'] !== null){
322
    $stat_vals_arr['TypicalLowerBoundary'] = $stat_vals_arr['Min'];
323
    $stat_vals_arr['Min'] = NULL;
324
  }
325

    
326
  if($stat_vals_arr['TypicalUpperBoundary'] === null && $stat_vals_arr['Max']  !== null){
327
    $stat_vals_arr['TypicalUpperBoundary'] = $stat_vals_arr['Max'];
328
    $stat_vals_arr['Max'] = NULL;
329
  }
330

    
331
  if (statistical_values_num_equals($stat_vals_arr, 'TypicalUpperBoundary', 'TypicalLowerBoundary')) {
332
    $stat_vals_arr['Average'] = $stat_vals_arr['TypicalUpperBoundary'];
333
    $stat_vals_arr['TypicalLowerBoundary'] = NULL;
334
    $stat_vals_arr['TypicalUpperBoundary'] = NULL;
335
  }
336

    
337
  // --- check for inconsistent cases, eg. only Max and average given
338
  if ($stat_vals_arr['TypicalLowerBoundary'] === NULL && $stat_vals_arr['TypicalUpperBoundary']  !== null) {
339
    // min missing
340
    $stat_vals_arr['TypicalLowerBoundary'] = '?';
341
  }
342
  if ($stat_vals_arr['TypicalLowerBoundary'] !== null && $stat_vals_arr['TypicalUpperBoundary'] === NULL) {
343
    // max missing
344
    $stat_vals_arr['TypicalUpperBoundary'] = '?';
345
  }
346

    
347
  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'])) {
348
    statistical_values_adjust_significant_figures($stat_vals_arr['Average'], $stat_vals_arr['TypicalLowerBoundary'], $stat_vals_arr['TypicalUpperBoundary']);
349
  }
350

    
351
  foreach ($stat_vals_arr as $key => $statistical_val) {
352

    
353
    if ($statistical_val !== NULL) {
354
      if ($statistical_val == '?') {
355
        $val_markup = $statistical_val;
356
      }
357
      else {
358
        if (is_numeric($statistical_val->_value)) {
359
          $val_markup = '<span class="'
360
            . html_class_attribute_ref($statistical_val) . ' '
361
            . (isset($statistical_val->type) ? $statistical_val->type->termType : '') . ' ' . $key . '" title="' . $key . '">'
362
            . $statistical_val->_value . '</span>';
363

    
364
        }
365
        else {
366
          $val_markup = NULL;
367
        }
368
      }
369

    
370
      if ($val_markup) {
371
        switch ($key) {
372
          // ---- min_max_element
373
          case 'Min':
374
            $min_max_markup .= "($val_markup&ndash;)";
375
            break;
376
          case 'Max':
377
            $min_max_markup .= "(&ndash;$val_markup)";
378
            break;
379
          case 'TypicalLowerBoundary':
380
            $min_max_markup .= "$val_markup";
381
            break;
382
          case 'TypicalUpperBoundary':
383
            $min_max_markup .= "&ndash;$val_markup";
384
            break;
385
          // ---- other values
386
          case 'SampleSize':
387
            $other_vals_array[$key] = $val_markup;
388
            break;
389
          case 'Average':
390
            $other_vals_array[$key] = $xbar_equals . $val_markup;
391
            break;
392
          case 'Variance':
393
            $other_vals_array[$key] = 'σ²=' . $val_markup;
394
            break;
395
          case 'StandardDeviation':
396
            $other_vals_array[$key] = 'σ=' . $val_markup;
397
            break;
398
        }
399
      }
400
    }
401
  }
402

    
403
  if(!$min_max_markup && !empty($other_vals_array['Average'])){
404
    // this could be the case in which we only have one value for Average
405
    // this trivial case needs to be displayed a simpler way
406
    $min_max_markup = str_replace($xbar_equals, '' , $other_vals_array['Average']);
407
    unset($other_vals_array['Average']);
408
    $min_max_markup .= '[' . join(';', $other_vals_array) . ']';
409
  } else {
410
    if(count($other_vals_array)){
411
      $min_max_markup .= '[' . join(';', $other_vals_array) . ']';
412
    }
413
  }
414

    
415
  return $min_max_markup . ($unit ? ' ' . $unit : '');
416
}
417

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

    
436
  $precision = 1;
437
  if($min->_value !== $max->_value){
438
    $precision = floor(log10(abs($max->_value - $min->_value) * (1 / $threshold)));
439
    // increase precision by one
440
    $precision += $precision < 0 ? - 1 : 1;
441
  }
442

    
443
  $target->_value = sigFig($target->_value, $precision);
444
}
445

    
446
/**
447
 * based on an idea taken from https://stackoverflow.com/questions/37618679/format-number-to-n-significant-digits-in-php#answer-48283297
448
 *
449
 * @param $value
450
 * @param $digits
451
 *
452
 * @return float|string
453
 */
454
function sigFig($value, $digits, $round_only = true)
455
{
456
  if ($value == 0) {
457
    $decimalPlaces = $digits - 1;
458
  } elseif ($value < 0) {
459
    $decimalPlaces = $digits - floor(log10($value * -1)) - 1;
460
  } else {
461
    $decimalPlaces = $digits - floor(log10($value)) - 1;
462
  }
463

    
464
  $answer = ($decimalPlaces > 0) && !$round_only ?
465
    number_format($value, $decimalPlaces) : round($value, abs($decimalPlaces));
466
  return $answer;
467
}
468

    
469

    
470

    
471
/**
472
 * Used internally in statistical_values() do determine numerically equality of stat_vals_arr values
473
 *
474
 * @param $stat_vals_arr
475
 * @param $key1
476
 * @param $key2
477
 *
478
 * @return bool
479
 */
480
function statistical_values_num_equals($stat_vals_arr, $key1, $key2){
481

    
482
  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);
483
}
484

    
485
function statistical_values_is_numeric($stat_val){
486
  return $stat_val !== null && is_numeric($stat_val->_value);
487
}
488

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

    
512
  $field_name = $field_base_name . 'Text';
513
  if (@is_string($object->$field_name)) {
514
    // Freetext overrides all other data
515
    $min_max_markup = ' ' . $object->$field_name;
516
  } else {
517
    // create markup for the atomized min max data
518
    $min_max_array = statistical_values_array();
519
    if (@is_numeric($object->$field_base_name)) {
520
      $min_max_array['Min'] = new stdClass();
521
      $min_max_array['Min']->_value = $object->$field_base_name;
522
    }
523
    $field_name = $field_base_name . 'Max';
524
    if (@is_numeric($object->$field_name)) {
525
      $min_max_array['Max'] = new stdClass();
526
      $min_max_array['Max']->_value = $object->$field_name;
527
    }
528
    $min_max_markup = statistical_values($min_max_array, $default_unit);
529
  }
530

    
531
  return $min_max_markup;
532
}
533

    
534
// TODO  move below code into new file: agent.inc
535

    
536
/*
537
 * Compose an render array from a CDM TaxonNodeAgentRelation object as Taxon Expert.
538
 *
539
 * compose_hook() implementation
540
 *
541
 * @param object $taxon_node_agent_relation
542
 *   CDM instance of type TaxonNodeAgentRelation
543
 * @return array
544
 *   A drupal render array
545
 *
546
 * @ingroup compose
547
 */
548
function compose_cdm_taxon_expert($taxon_node_agent_relation) {
549

    
550
  $agent_details = null;
551

    
552
  $label_suffix = ':';
553

    
554
  if($taxon_node_agent_relation->class == 'DefaultPagerImpl'){
555
    // oops this is a pager
556
    // this situation will occur when this compose is executed
557
    // through the proxy_content() method
558
    $taxon_node_agent_relation = $taxon_node_agent_relation->records[0];
559

    
560
  }
561

    
562
  if(is_object($taxon_node_agent_relation->agent)) {
563
    $agent_details = compose_cdm_team_or_person_base($taxon_node_agent_relation->agent);
564
    // all data will be added to the groups of the agent_details render array
565
    $groups = &$agent_details[0]['#groups'];
566

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

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

    
571
    $taxa_markup = array(
572
      '#theme_wrappers' => array('container'),
573
      '#attributes' => array('class' => array('managed_taxa')),
574
      '#wrapper_attributes' => array('class' => 'sublist-container')
575
      );
576
    foreach($family_tnars as $tnar){
577
      if(is_object($tnar->taxonNode->taxon)){
578
        $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))));
579
      }
580
    }
581
    ksort($taxa_markup);
582

    
583
    @_description_list_group_add($groups, t('Families'). $label_suffix, array($taxa_markup));
584

    
585
  }
586

    
587
  return $agent_details;
588
}
589

    
590

    
591
/*
592
 * Compose an render array from a CDM TeamOrPersonBase object.
593
 *
594
 * compose_hook() implementation
595
 *
596
 * TODO: currently mainly implemented for Agent, add Team details
597
 *
598
 * @param object $team_or_person
599
 *   CDM instance of type TeamOrPersonBase
600
 * @return array
601
 *   A drupal render array
602
 *
603
 * @ingroup compose
604
 */
605
function compose_cdm_team_or_person_base($team_or_person, $data = array()) {
606

    
607
  $groups = array();
608

    
609
  $label_suffix = ':';
610

    
611
  // $weight = 0;
612
  if($team_or_person){
613

    
614
    if(is_object($team_or_person->lifespan)){
615
      // ToDo render as date* - date† ?
616
      @_description_list_group_add($groups, t('Lifespan'). $label_suffix, timePeriodToString($team_or_person->lifespan) /*, '' , $weight++ */);
617
    }
618

    
619
    // nomenclaturalTitle
620
    @_description_list_group_add($groups, "Nomenclatural Title". $label_suffix, $team_or_person->nomenclaturalTitle);
621
    // collectorTitle
622
    @_description_list_group_add($groups, "Collector Title". $label_suffix, $team_or_person->collectorTitle);
623

    
624
    // institutionalMemberships
625
    if(is_array($team_or_person->institutionalMemberships)){
626

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

    
654
      }
655

    
656
      $label = count($institutes_ra) > 1 ? t('Institutes'):  t('Institute');
657
      @_description_list_group_add($groups, $label. $label_suffix, $institutes_ra /*, '' , $weight++ */);
658
    }
659

    
660

    
661
    // Contact
662
    $agent_contact_details = compose_cdm_contact($team_or_person->contact, $team_or_person->titleCache);
663
    if(is_array($agent_contact_details[0]['#groups'])){
664
      $groups = array_merge($groups, $agent_contact_details[0]['#groups']);
665
    }
666

    
667
    // additional data
668
    foreach($data as $key=>$value){
669
      @_description_list_group_add($sub_dl_groups, t('@key', array('@key' => $key)), $value /*, '' , $weight++ */);
670
    }
671

    
672
  }
673

    
674
  $team_or_person_details = array(
675
    '#title' => $team_or_person->titleCache,
676
    '#theme' => 'description_list',
677
    '#groups' => $groups,
678
    '#attributes' => array('class' => html_class_attribute_ref($team_or_person)),
679
  );
680
  return array($team_or_person_details);
681
}
682

    
683

    
684
/*
685
 * Compose an render array from a CDM Contact object.
686
 *
687
 * compose_hook() implementation
688
 *
689
 * TODO: currently mainly implemented for Agent, add Team details
690
 *
691
 * @param object $contact
692
 *   CDM instance of type Contact
693
 * @param $title
694
 *   The title for the description list header
695
 * @param $weight
696
 *   Optional weight for the description list entries
697
 * @return array
698
 *   A drupal render array
699
 *
700
 * @ingroup compose
701
 */
702
function compose_cdm_contact($contact, $title, $weight = 0)
703
{
704

    
705
  $groups = array();
706

    
707
  $contact_details = null;
708

    
709
  $label_suffix = ':';
710

    
711
  $contact_field_names_map = array(
712
    'emailAddresses' => t('Email'),
713
    'urls' => t('Urls'),
714
    'phoneNumbers' => t('Phone'),
715
    'faxNumbers' => t('Fax'),
716
  );
717

    
718
  // Contact
719
  if(is_object($contact)){
720
    if(isset($contact->addresses)){
721
      // TODO ....
722
      // $sub_groups = array();
723
      // foreach($contact->addresses as $address){
724
      //   @_description_list_group_add($sub_groups, $label, $contact->$fieldName, '', $weight++);
725
      // }
726
    }
727
    foreach($contact_field_names_map as $fieldName => $label){
728
      if(is_array($contact->$fieldName)){
729
        @_description_list_group_add($groups, $label . $label_suffix, $contact->$fieldName, '', $weight++);
730
      }
731
    }
732
    $contact_details = array(
733
      '#title' => $title,
734
      '#theme' => 'description_list',
735
      '#groups' => $groups
736
    );
737

    
738

    
739
  } else if(is_string($title)) {
740
    // if the contact entity is empty but the title is given anyway
741
    // we are only adding the title, using the description_list
742
    // structure is not possible since it would be empty due to
743
    // missing group data
744
    $contact_details = array('#markup' => '<h3>' . $title . '</h3>');
745
  }
746

    
747
  return array($contact_details);
748

    
749
}
750

    
751
/**
752
 * Compose an render array from a CDM Extension objects.
753
 *
754
 * @param $extensions
755
 *    An array of CDM Extension objects
756
 * @return array
757
 *   A render array containing the fields of the supplied $sequence
758
 *
759
 * @ingroup compose
760
 */
761
function compose_extensions($extensions)
762
{
763
  $extensions_render_array= null;
764
  $extensions_by_type = array();
765
  foreach ($extensions as $extension) {
766
    if (@is_string($extension->value)) {
767
      if (!isset($extensions_by_type[$extension->type->representation_L10n])) {
768
        $extensions_by_type[$extension->type->representation_L10n] = array();
769
      }
770
      $extensions_by_type[$extension->type->representation_L10n][] = markup_to_render_array($extension->value);
771
    }
772
  }
773

    
774
  if (count($extensions_by_type)) {
775
    $sub_dl_groups = array();
776
    foreach ($extensions_by_type as $type_label => $text_list) {
777
      @_description_list_group_add($sub_dl_groups, $type_label . ':', $text_list);
778
    }
779
    $extensions_render_array = array(
780
      array('#theme' => 'description_list', '#groups' => $sub_dl_groups)
781
    );
782
    return $extensions_render_array;
783
  }
784
  return $extensions_render_array;
785
}
786

    
787
function formatParams($params) {
788
    $paramString = null;
789
    if (is_array($params)){
790
        $keys =array_keys($params);
791
        $paramString = '';
792
        foreach ($keys as $k ){
793
            if ($k != 'pageNumber' && $k != 'pageSize'){
794
                $paramString .= ' -'.$k.'='.urlencode($params[$k]);
795
            }
796
        }
797
    }
798
    return $paramString;
799
}
800

    
801
function formatWSParams($params) {
802
  $paramString = null;
803
    if (is_array($params)){
804
        $keys =array_keys($params);
805
        $paramString = '';
806
        foreach ($keys as $k ){
807
            if ($k != 'pageNumber' && $k != 'pageSize'){
808
                $paramString .= '&'.$k.'='.urlencode($params[$k]);
809
            }
810
        }
811
    }
812
    return $paramString;
813
}
814

    
815
/**
816
 *
817
 * @param $cdm_entity
818
 *
819
 * @return string the markup
820
 */
821
function render_cdm_entity_link($cdm_entity) {
822

    
823
  switch ($cdm_entity->class) {
824
    case 'TaxonDescription':
825
    case 'NameDescription':
826
    case 'SpecimenDescription':
827
      $link =  '<span class="' . html_class_attribute_ref($cdm_entity) . '">' . $cdm_entity->titleCache . '</span> ' . icon_link(path_to_description($cdm_entity->uuid));
828
      break;
829
    default:
830
      $link = '<span class="error">UNSUPPORTED CDM ENTITY TYPE</span>';
831
  }
832
  return $link;
833
}
834

    
835
/**
836
 * Creates an icon which links to the given path
837
 * @param $path
838
 *
839
 * @return string
840
 */
841
function icon_link($path, $fragment = '') {
842
  $iconlink = l(custom_icon_font_markup('icon-interal-link-alt-solid', ['class' => ['superscript']]), $path, ['html' => TRUE, 'fragment' => $fragment] );
843
  return $iconlink;
844
}
Add picture from clipboard (Maximum size: 40 MB)