Project

General

Profile

Download (75.1 KB) Statistics
| Branch: | Tag: | Revision:
1 6657531f Andreas Kohlbecker
<?php
2
/**
3
 * @file
4
 * Module to provide a CDM Dataportal.
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
 *   - Wouter Addink <w.addink@eti.uva.nl> (migration from Drupal 5 to Drupal7)
18
 */
19
20 38b27a36 Andreas Kohlbecker
  module_load_include('php', 'cdm_dataportal', 'node_types');
21
  module_load_include('php', 'cdm_dataportal', 'settings');
22
  module_load_include('php', 'cdm_dataportal', 'help');
23
  module_load_include('php', 'cdm_dataportal', 'cdm_dataportal.search');
24
25
  module_load_include('inc', 'cdm_dataportal', 'includes/common');
26 2e31ea9e Andreas Kohlbecker
  module_load_include('inc', 'cdm_dataportal', 'includes/name');
27 38b27a36 Andreas Kohlbecker
  module_load_include('inc', 'cdm_dataportal', 'includes/pages');
28
  module_load_include('inc', 'cdm_dataportal', 'includes/media');
29
  module_load_include('inc', 'cdm_dataportal', 'includes/maps');
30
  module_load_include('inc', 'cdm_dataportal', 'includes/occurrences');
31
  module_load_include('inc', 'cdm_dataportal', 'includes/descriptions');
32
  module_load_include('inc', 'cdm_dataportal', 'includes/pre-drupal8');
33
34
  module_load_include('inc', 'cdm_dataportal', 'theme/theme_registry');
35
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.common');
36
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.descriptions');
37
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.media');
38
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.occurrence');
39
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.page');
40
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.taxon');
41
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.name');
42
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.references');
43
44
  module_load_include('php', 'cdm_dataportal', 'classes/footnotemanager');
45
  module_load_include('php', 'cdm_dataportal', 'classes/footnote');
46
  module_load_include('php', 'cdm_dataportal', 'classes/footnotekey');
47
  module_load_include('php', 'cdm_dataportal', 'classes/renderhints');
48
49
50
  /* ============================ java script functions ============================= */
51
52
53
  /**
54
  * loads external java script files asynchronously.
55
  *
56
  * @param unknown_type $script_url
57
  */
58
  function drupal_add_js_async($script_url, $callback){
59
60
    drupal_add_js("
61
          jQuery(document).ready(function() {
62
            jQuery.ajax({
63
              url: '" . $script_url . "',
64
              dataType: 'script',
65
              cache: true, // otherwise will get fresh copy every page load
66
              success: function() {
67
                    " . $callback . "
68
              }
69
            });
70
          });"
71
    , 'inline');
72 6657531f Andreas Kohlbecker
  }
73
74 38b27a36 Andreas Kohlbecker
  /**
75
   */
76
  function drupal_add_js_rowToggle($tableId){
77 6657531f Andreas Kohlbecker
78 38b27a36 Andreas Kohlbecker
      drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/table_modification.js');
79
      drupal_add_js('jQuery(document).ready(function(){
80
          addRowToggle("' . $tableId . '");
81 6657531f Andreas Kohlbecker
      });
82 38b27a36 Andreas Kohlbecker
      ', array('type' => 'inline'));
83
  }
84 d2fd2a4c Andreas Kohlbecker
85 38b27a36 Andreas Kohlbecker
  /**
86
   * @param unknown_type $link_element_selector
87
   * @param unknown_type $progress_element_selector
88
   */
89
  function _add_js_cdm_ws_progressbar($link_element_selector, $progress_element_selector){
90
91
    $callback = "jQuery('" . $link_element_selector . "').cdm_ws_progress('" . $progress_element_selector . "');";
92
93
    drupal_add_js_async(variable_get('cdm_webservice_url', '').'js/cdm_ws_progress.js', $callback);
94
95
    //   drupal_add_js("
96
    //   	  if (Drupal.jsEnabled) {
97
    //         $(document).ready(function() {
98
    //       		$('" . $link_element_selector . "').cdm_ws_progress('" . $progress_element_selector . "');
99
    //         });
100
    //       }", 'inline');
101
    }
102
103
  /**
104
   * @todo Please document this function.
105
   * @see http://drupal.org/node/1354
106
   */
107
  function _add_js_treeselector() {
108
    // drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/treeselector.js');
109
    drupal_add_js("
110
        jQuery(document).ready(function() {
111
           jQuery('#cdm-taxonomictree-selector-form #edit-val').change(function () {
112
                jQuery('#cdm-taxonomictree-selector-form').submit();
113
            });
114
115
        });
116
      ",
117
      array(
118
        'type' => 'inline',
119
        'scope' => 'footer'
120
      )
121
    );
122 5ea8b301 Andreas Kohlbecker
  }
123
124 38b27a36 Andreas Kohlbecker
  function _add_js_openlayers() {
125
126
    $openlayers = '/js/map/OpenLayers-2.13.1/OpenLayers.js';
127
    $proj4js = '/js/map/proj4js-1.1.0/proj4js-compressed.js';
128
129
    if(variable_get('cdm_js_devel_mode', FALSE)){
130
      // develooper mode libs
131
  //     $openlayers = '/js/map/OpenLayers-2.13.1/lib/OpenLayers.js';
132
      $openlayers = '/js/map/OpenLayers-2.13.1/OpenLayers.debug.js';
133
      $proj4js = '/js/map/proj4js-1.1.0/proj4js-combined.js';
134
    }
135
136
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . $openlayers,
137
      array(
138
        'type' => 'file',
139
        'weight' => JS_LIBRARY,
140
        'cache' => TRUE,
141
        'preprocess' => FALSE
142
      )
143
    );
144
145
    // see https://github.com/proj4js/proj4js
146
    // http://openlayers.org/dev/examples/using-proj4js.html
147
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . $proj4js,
148
      array(
149
        'type' => 'file',
150
        'weight' => JS_LIBRARY,
151
        'cache' => TRUE,
152
      )
153
    );
154
155
    // configure the theme
156
    $openlayers_theme_path = drupal_get_path('module', 'cdm_dataportal') . '/js/map/OpenLayers-2.13.1/theme/default/';
157
    $openlayers_imp_path = drupal_get_path('module', 'cdm_dataportal') . '/js/map/img/dark/';
158
    drupal_add_js('OpenLayers.ImgPath="' . base_path() . $openlayers_imp_path . '";', array(
159
        'type' => 'inline',
160
        'weight' => JS_LIBRARY,
161
        'cache' => TRUE,
162
        'preprocess' => FALSE
163
      ));
164
165
    drupal_add_css($openlayers_theme_path . 'style.tidy.css',
166
      array(
167
        'type' => 'file',
168
        'cache' => TRUE,
169
        'preprocess' => FALSE
170
      )
171
    );
172 e3026d72 Andreas Kohlbecker
173 38b27a36 Andreas Kohlbecker
  }
174 e3026d72 Andreas Kohlbecker
175 38b27a36 Andreas Kohlbecker
  /**
176
   * @todo Please document this function.
177
   * @see http://drupal.org/node/1354
178
   */
179
  function _add_js_thickbox() {
180
    // ---- jQuery thickbox:
181
    /*
182
    * bug: compat-1.0.js && thickbox.js line 237 .trigger("unload") -> event is
183
    * not triggered because of problems with compat-1.0.js' see INSTALL.txt
184
    */
185
    // drupal_add_js(drupal_get_path('module',
186
    // 'cdm_dataportal').'/js/jquery.imagetool.min.js');
187
    //
188
    // Add a setting for the path to cdm_dataportal module, used to find the path
189
    // for the loading animation image in thickbox.
190
    drupal_add_js(array(
191
    'cdm_dataportal' => array(
192
    'cdm_dataportal_path' => base_path() . drupal_get_path('module', 'cdm_dataportal'),
193 10f6aa4b Andreas Kohlbecker
    )
194 38b27a36 Andreas Kohlbecker
    ),
195
    'setting'
196
        );
197
        drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/thickbox/thickbox.js');
198
        drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/js/thickbox/cdm_thickbox.css');
199
  }
200 94550ff9 Andreas Kohlbecker
201 38b27a36 Andreas Kohlbecker
  /**
202
   * @todo Please document this function.
203
   * @see http://drupal.org/node/1354
204
   */
205
  function _add_js_lightbox($galleryID) {
206
    /*
207
     * Important Notice: The jquery.lightbox-0.5.js has been modified in order to
208
    * allow using the "alt" attribute for captions instead of the "title"
209
    * attribute
210
    */
211
    $lightbox_base_path =  drupal_get_path('module', 'cdm_dataportal') . '/js/jquery-lightbox-0.5';
212
    $lightbox_image_path = base_path() . $lightbox_base_path . '/images/';
213
    drupal_add_js($lightbox_base_path . '/js/jquery.lightbox-0.5.js');
214
    drupal_add_css($lightbox_base_path . '/css/jquery.lightbox-0.5.css');
215
    drupal_add_js('jQuery(document).ready(function() {
216
        jQuery(\'#' . $galleryID . ' a.lightbox\').lightBox({
217
          fixedNavigation:  true,
218
          imageLoading:     \'' . $lightbox_image_path . 'lightbox-ico-loading.gif\',
219
          imageBtnPrev:     \'' . $lightbox_image_path . 'lightbox-btn-prev.gif\',
220
          imageBtnNext:     \'' . $lightbox_image_path . 'lightbox-btn-next.gif\',
221
          imageBtnClose:    \'' . $lightbox_image_path . 'lightbox-btn-close.gif\',
222
          imageBlank:       \'' . $lightbox_image_path . 'lightbox-blank.gif\',
223
          adjustToWindow: true
224
        });
225 94550ff9 Andreas Kohlbecker
      });
226 38b27a36 Andreas Kohlbecker
      ', array('type' => 'inline'));
227
  }
228 94550ff9 Andreas Kohlbecker
229 38b27a36 Andreas Kohlbecker
  /**
230
   * @todo Please document this function.
231
   * @see http://drupal.org/node/1354
232
   */
233
  function _add_js_footnotes() {
234
    _add_js_domEvent();
235
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/footnotes.js');
236
  }
237 94550ff9 Andreas Kohlbecker
238 38b27a36 Andreas Kohlbecker
  /**
239
   * @todo Please document this function.
240
   * @see http://drupal.org/node/1354
241
   */
242
  function _add_js_cluetip() {
243
244
    // TODO replace by
245
    // @see http://www.socialembedded.com/labs/jQuery-Tooltip-Plugin/jQuery-Tooltip-Plugin.html
246
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.cluetip.min.js');
247
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/jquery.dimensions.js');
248
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.hoverIntent.js');
249
    if(variable_get('cdm_js_devel_mode', FALSE)){
250
      drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.cluetip.js');
251
    }
252
    drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.cluetip.css');
253
    drupal_add_js("jQuery(document).ready(function(){
254
        jQuery('.cluetip').css({color: '#0062C2'}).cluetip({
255
          splitTitle: '|',
256
          showTitle: true,
257
          activation: 'hover',
258
          sicky: true,
259
          arrows: true,
260
          dropShadow: false,
261
          cluetipClass: 'rounded'
262
        });
263
      });", array('type' => 'inline'));
264 5ea8b301 Andreas Kohlbecker
  }
265 94550ff9 Andreas Kohlbecker
266 38b27a36 Andreas Kohlbecker
  /**
267
   * @todo Please document this function.
268
   * @see http://drupal.org/node/1354
269
   */
270
  function _add_js_ahah() {
271 b386ae48 Andreas Kohlbecker
272 38b27a36 Andreas Kohlbecker
    _add_js_domEvent(); // requires domEvent.js
273
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/ahah-content.js');
274
  }
275 94550ff9 Andreas Kohlbecker
276 38b27a36 Andreas Kohlbecker
  /**
277
   * Adds the external javascript file for domEvent.js.
278
   *
279
   * @see drupal_add_js()
280
   */
281
  function _add_js_domEvent() {
282
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/domEvent.js');
283
  }
284 b386ae48 Andreas Kohlbecker
285 087bb473 Andreas Kohlbecker
  function _add_jquery_ui()
286
  {
287
    drupal_add_css(drupal_get_path('module',
288
        'cdm_dataportal') . '/js/jquery-ui-1.8.24/themes/base/jquery.ui.all.css');
289
    drupal_add_js(drupal_get_path('module',
290
        'cdm_dataportal') . '/js/jquery-ui-1.8.24/ui/jquery-ui.js',
291
      array(
292
        'type' => 'file',
293
        'weight' => JS_LIBRARY,
294
        'cache' => TRUE,
295
        'preprocess' => FALSE
296
      )
297
    );
298
  }
299
300 38b27a36 Andreas Kohlbecker
  /**
301
   * Provides the markup for an font awesome icon.
302
   *
303
   * The icons is created in default size without any extra features.
304
   *
305
   * The available icons are listed here http://fontawesome.io/cheatsheet/
306 ef12d0c6 Andreas Kohlbecker
   * fontawesome icons have much more features than implemented here in this function,
307
   * for spinning icons, fixed width icons, rotation, etc please checkout the
308 38b27a36 Andreas Kohlbecker
   * examples at http://fontawesome.io/examples/
309
   *
310
   * @parameter $icon_name
311
   *  The name of the icon which starts with 'fa-'
312
   *
313
   * @return String
314
   *    the markup for the icon in an <i> tag
315
   *
316
   */
317 b0aa9a8f Andreas Kohlbecker
  function font_awesome_icon_markup($icon_name = NULL, $attributes = array()){
318 38b27a36 Andreas Kohlbecker
319
320
    //<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
321
322 b0aa9a8f Andreas Kohlbecker
    $font_awesome_css_uri=drupal_get_path('module', 'cdm_dataportal').'/font-awesome/4.5.0/css/font-awesome.min.css';
323
    $font_awesome_css_uri="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css";
324 38b27a36 Andreas Kohlbecker
325
    drupal_add_html_head_link(
326
      array(
327
        'href' => $font_awesome_css_uri,
328
        'rel' => 'stylesheet'
329
      )
330
    );
331
332 b0aa9a8f Andreas Kohlbecker
    if($icon_name){
333
      if(!isset($attributes['class'])){
334
        $attributes['class'] = array();
335
      }
336
      $attributes['class'][] = 'fa';
337
      $attributes['class'][] = $icon_name;
338
339
      return '<i ' . drupal_attributes($attributes) . '></i>';
340 38b27a36 Andreas Kohlbecker
    }
341
342 b0aa9a8f Andreas Kohlbecker
    return '';
343 38b27a36 Andreas Kohlbecker
  }
344 94550ff9 Andreas Kohlbecker
345 38b27a36 Andreas Kohlbecker
346
  /* ====================== hook implementations ====================== */
347
  /**
348
   * Implements hook_permission().
349
   *
350
   * Valid permissions for this module.
351
   *
352
   * @return array
353
   *   An array of valid permissions for the portfolio module.
354
   */
355
  function cdm_dataportal_permission() {
356
    return array(
357
      'administer cdm_dataportal' => array(
358
        'title' => t('administer cdm_dataportal'),
359
        'description' => t("TODO Add a description for 'administer cdm_dataportal'"),
360
      ),
361
      'cdm_dataportal view notes' => array(
362
        'title' => t('cdm_dataportal view notes'),
363
        'description' => t("TODO Add a description for 'cdm_dataportal view notes'"),
364
      ),
365
      // TODO Which other permissions are required?
366
      // -> check the WP6 requirements document.
367
    );
368
  }
369 6657531f Andreas Kohlbecker
370
/**
371 f19f47fa Andreas Kohlbecker
 * Implements hook_menu().
372
 */
373 6657531f Andreas Kohlbecker
function cdm_dataportal_menu() {
374
  $items = array();
375
376
  // @see settings.php.
377
  cdm_dataportal_menu_admin($items);
378
  cdm_dataportal_menu_help($items);
379
380
  $items['cdm_dataportal/names'] = array(
381
    'page callback' => 'cdm_dataportal_view_names',
382
    'access arguments' => array('access content'),
383
    'type' => MENU_CALLBACK,
384
  );
385
386
  // Optional callback arguments: page.
387
  $items['cdm_dataportal/taxon'] = array(
388
    'page callback' => 'cdm_dataportal_taxon_page_view',
389
    'access arguments' => array('access content'),
390
    'type' => MENU_CALLBACK,
391
    // Expected callback arguments: uuid.
392
  );
393
394 9d37c9d9 Patric Plitzner
   // Optional callback arguments: page.
395 d6d4cc03 Patric Plitzner
    //FIXME point to view/page method in this module
396 9d37c9d9 Patric Plitzner
    $items['cdm_dataportal/specimen'] = array(
397 b810158e Patric Plitzner
        'page callback' => 'cdm_dataportal_specimen_page_view',
398 9d37c9d9 Patric Plitzner
        'access arguments' => array('access content'),
399
        'type' => MENU_CALLBACK,
400
        // Expected callback arguments: uuid.
401
    );
402
403 6657531f Andreas Kohlbecker
  $items['cdm_dataportal/name'] = array(
404
    'page callback' => 'cdm_dataportal_name_page_view',
405
      /*
406
    'page arguments' => array(
407
       'taxon_name_uuid',
408
       'taxon_to_hide_uuid',
409
       'synonym_uuid' => NULL
410
      ),
411
      */
412
    'access arguments' => array('access content'),
413
    'type' => MENU_CALLBACK,
414
    // Expected callback arguments: uuid.
415
  );
416
417
  $items['cdm_dataportal/reference'] = array(
418
    'page callback' => 'cdm_dataportal_view_reference',
419
    'access arguments' => array('access content'),
420
    'type' => MENU_CALLBACK,
421
    // Expected callback arguments: uuid.
422
  );
423
424
  $items['cdm_dataportal/reference/list'] = array(
425
    'page callback' => 'cdm_dataportal_view_reference_list',
426
    'access arguments' => array('access content'),
427
    'type' => MENU_CALLBACK,
428
    // Expected callback arguments: uuid.
429
  );
430
431
  $items['cdm_dataportal/media'] = array(
432
    'page callback' => 'cdm_dataportal_view_media',
433
    'access arguments' => array('access content'),
434
    'type' => MENU_CALLBACK,
435
    // Expected callback arguments:
436
    // uuid, mediarepresentation_uuid, part_uuid or part#.
437
  );
438
439
  $items['cdm_dataportal/polytomousKey'] = array(
440
    'page callback' => 'cdm_dataportal_view_polytomousKey',
441
    'access arguments' => array('access content'),
442
    'type' => MENU_CALLBACK,
443
    // Expected callback arguments: polytomousKey->uuid.
444
  );
445
446
  $items['cdm_dataportal/search'] = array(
447
    'page callback' => 'cdm_dataportal_view_search_advanced',
448
    'access arguments' => array('access content'),
449
    'type' => MENU_CALLBACK,
450
  );
451
452
  $items['cdm_dataportal/search/advanced'] = array(
453 90a76081 Andreas Kohlbecker
    'title' => t('Advanced'),
454 6657531f Andreas Kohlbecker
    'page callback' => 'cdm_dataportal_view_search_advanced',
455
    'access arguments' => array('access content'),
456
    'type' => MENU_DEFAULT_LOCAL_TASK,
457
  );
458
459
  $items['cdm_dataportal/search/taxon_by_description'] = array(
460 90a76081 Andreas Kohlbecker
    'title' => t('By factual data'),
461 6657531f Andreas Kohlbecker
    'page callback' => 'cdm_dataportal_view_search_taxon_by_description',
462
    'access arguments' => array('access content'),
463
    'type' => MENU_LOCAL_TASK,
464
  );
465
466
  $items['cdm_dataportal/search/results/taxon'] = array(
467
    'page callback' => 'cdm_dataportal_view_search_results_taxon',
468
    'access arguments' => array('access content'),
469
    'type' => MENU_CALLBACK,
470
  );
471
  /*
472
   $items['cdm/xml2json'] = array(
473
   'page callback' => 'cdm_view_xml2json',
474
   'access arguments' => array('access content'),
475
   'type' => MENU_CALLBACK,
476
   );
477
   */
478
479
  // if (arg(0)=='user' && ($uid=arg(1)) && is_numeric($uid)) {
480
  // User configuration of cdm_dataportal.
481
  $items['user/%/cdm_dataportal'] = array(
482
    'title' => 'cdm_dataportal',
483
    'access arguments' => array('access content'),
484
    'page callback' => 'drupal_get_form',
485
    'page arguments' => array('cdm_dataportal_user_form'),
486
    'type' => MENU_LOCAL_TASK,
487
    'weight' => 10,
488
  );
489
  // }
490
491
  // 'May not cache' in D5.
492
  $items['cdm_dataportal/name/%'] = array(
493
    // 'page callback' => 'cdm_dataportal_view_name',
494
    'page callback' => 'cdm_dataportal_name_page_view',
495
    'page arguments' => array(2, 3, 4),
496
    'access arguments' => array('access content'),
497
    'type' => MENU_CALLBACK,
498
  );
499
500 6f1a217c Andreas Kohlbecker
  // --- Local tasks for Taxon.
501
  // --- tabbed taxon page
502 6657531f Andreas Kohlbecker
  if (variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
503
    $items['cdm_dataportal/taxon/%'] = array(
504
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'General')),
505
      'page callback' => 'cdm_dataportal_taxon_page_view',
506
      'access arguments' => array('access content'),
507
      'type' => MENU_CALLBACK,
508
      'weight' => 1,
509
      'page arguments' => array(2, "description")
510 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
511 6657531f Andreas Kohlbecker
    );
512
513
    $items['cdm_dataportal/taxon/%/all'] = array(
514
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'General')),
515
      'page callback' => 'cdm_dataportal_taxon_page_view',
516
      'access arguments' => array('access content'),
517
      'type' => MENU_CALLBACK,
518
      'weight' => 2,
519
      'page arguments' => array(2, "all")
520 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
521 6657531f Andreas Kohlbecker
    );
522
523
    $items['cdm_dataportal/taxon/%/description'] = array(
524
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'General')),
525
      'page callback' => 'cdm_dataportal_taxon_page_view',
526
      'access arguments' => array('access content'),
527
      'type' => MENU_DEFAULT_LOCAL_TASK,
528
      'weight' => 2,
529
      'page arguments' => array(2, "description")
530 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
531 6657531f Andreas Kohlbecker
    );
532
533
    $items['cdm_dataportal/taxon/%/synonymy'] = array(
534
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Synonymy')),
535
      'page callback' => 'cdm_dataportal_taxon_page_view',
536
      'access arguments' => array('access content'),
537
      'type' => MENU_LOCAL_TASK,
538
      'weight' => 4,
539
      'page arguments' => array(2, "synonymy", 4)
540 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid and ...
541 6657531f Andreas Kohlbecker
    );
542
    $items['cdm_dataportal/taxon/%/images'] = array(
543
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Images')),
544
      'page callback' => 'cdm_dataportal_taxon_page_view',
545
      'access arguments' => array('access content'),
546
      'type' => MENU_LOCAL_TASK,
547
      'weight' => 5,
548
      'page arguments' => array(2, "images")
549 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
550 6657531f Andreas Kohlbecker
    );
551
552
    $items['cdm_dataportal/taxon/%/specimens'] = array(
553
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Specimens')),
554
      'page callback' => 'cdm_dataportal_taxon_page_view',
555
      'access arguments' => array('access content'),
556
      'type' => MENU_LOCAL_TASK,
557
      'weight' => 6,
558
      'page arguments' => array(2, "specimens")
559 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
560 6657531f Andreas Kohlbecker
    );
561
562
    $items['cdm_dataportal/taxon/%/keys'] = array(
563
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Keys')),
564
      'page callback' => 'cdm_dataportal_taxon_page_view',
565
      'access arguments' => array('access content'),
566
      'type' => MENU_LOCAL_TASK,
567
      'weight' => 6,
568
      'page arguments' => array(2, "keys")
569 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
570
    );
571
572
    $items['cdm_dataportal/taxon/%/experts'] = array(
573
        'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Experts')),
574
        'page callback' => 'cdm_dataportal_taxon_page_view',
575
        'access arguments' => array('access content'),
576
        'type' => MENU_LOCAL_TASK,
577
        'weight' => 6,
578
        'page arguments' => array(2, "experts")
579
    , // Expected callback arguments: taxon_uuid.
580 6657531f Andreas Kohlbecker
    );
581
  }
582
583 6f1a217c Andreas Kohlbecker
  // --- refresh link for all cdmnode types
584
  foreach (cdm_get_nodetypes() as $type=>$name) {
585
    $items['cdm_dataportal/' . $name . '/%/refresh'] = array(
586
        'title' => t('Refresh'),
587
        'page callback' => 'cdm_dataportal_refresh_node',
588 3113db26 Andreas Kohlbecker
        'access arguments' => array('administer cdm_dataportal'),
589 6f1a217c Andreas Kohlbecker
        'type' => MENU_LOCAL_TASK,
590
        'weight' => 100,
591
        'page arguments' => array($name, 2)
592
    );
593
  }
594
595 6657531f Andreas Kohlbecker
  return $items;
596
}
597
598
/**
599 b85ab055 Andreas Kohlbecker
 * Implements hook_init().
600
 *
601 6657531f Andreas Kohlbecker
 */
602
function cdm_dataportal_init() {
603 5ea8b301 Andreas Kohlbecker
  //FIXME To add CSS or JS that should be present on all pages, modules
604
  //      should not implement this hook, but declare these files in their .info file.
605 6657531f Andreas Kohlbecker
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal.css');
606
  // drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
607
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal_screen.css', array('type' => 'screen'));
608 2dd59bb5 Andreas Kohlbecker
609
  if(variable_get('cdm_debug_mode', FALSE)){
610
    $file = 'temporary://drupal_debug.txt';
611
    file_put_contents($file, 'CDM DEBUG LOG for ' . $_GET['q']. "\n"); // will overwrite the file
612
  }
613 5611d467 Andreas Kohlbecker
614
  $bibliography_settings = get_bibliography_settings();
615 57d513cb Andreas Kohlbecker
  $enclosing_tag = $bibliography_settings['enabled'] == 1 ? 'div' : 'span';
616
  FootnoteManager::registerFootnoteSet('BIBLIOGRAPHY', $enclosing_tag, $bibliography_settings['key_format']);
617 6657531f Andreas Kohlbecker
}
618
619 6f1a217c Andreas Kohlbecker
function cdm_dataportal_refresh_node($cdm_node_name, $uuid, $parameters = array()){
620
621
  $base_path = 'cdm_dataportal/' . $cdm_node_name . '/' . $uuid;
622
623
  if($cdm_node_name == 'taxon' && variable_get('cdm_dataportal_taxonpage_tabs', 1)){
624
    // force reloading of all and notify user about this special loading
625
    drupal_set_message(t('The level 2 cache has been cleared for all tabs of this taxon page at once, please click here to return to the tabbed page: ')
626
        . l('Back to tabbed taxon page', $base_path));
627
    $base_path .= '/all';
628
  } else {
629
    drupal_set_message(t('The level 2 cache has been cleared for this page'));
630
  }
631
632
  $parameters['cacheL2_refresh'] ='1';
633
634
635
  drupal_goto($base_path, array('query' => $parameters));
636
}
637
638 6657531f Andreas Kohlbecker
/**
639
 * The function generate form for own user cdm dataportal configurations.
640
 */
641
function cdm_dataportal_user_form($form, &$form_state) {
642
  global $user;
643
  $checkbox_value = 'cdm_dataportal_' . $user->uid . '_default_tab_active';
644
645 8e1dc7ec Andreas Kohlbecker
  $form['taxon_page_tabs'] = array(
646
      '#type' => 'fieldset',
647
      '#title' => t('Taxon page tabs'),
648
  );
649
  $form['taxon_page_tabs']['check'] = array(
650 6657531f Andreas Kohlbecker
    '#type' => 'checkbox',
651
    '#title' => t('Activate user default configuration'),
652
    '#default_value' => variable_get($checkbox_value, 0),
653
    '#description' => t('Check this if you want configure your own default tab from the below menu.'),
654
  );
655
656 8e1dc7ec Andreas Kohlbecker
  $form['taxon_page_tabs']['cdm_dataportal_user_form'] = array(
657 6657531f Andreas Kohlbecker
    '#type' => 'select',
658
    '#title' => t('Default tab to display'),
659
    '#default_value' => get_default_taxon_tab(TRUE),
660
    '#options' => unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB),
661
    '#description' => t('<p>Select the default tab to display when visiting a taxon page. Only available if Tabbed Taxon Page is enable.</p>
662
              <strong>Note:</strong> After performing a search and clicking in any synonym, the taxon tab
663
              to be renderized will be the synonymy of the accepted taxon and not the above selected tab.'),
664
  );
665
666
  $form['submit'] = array(
667
    '#type' => 'submit',
668
    '#value' => t('Submit'),
669
  );
670
671
  return $form;
672
}
673
674
/**
675
 * Form submission handler for user_form().
676
 *
677
 * Submits the user cdm dataportal configurations.
678
 */
679
function cdm_dataportal_user_form_submit($form, &$form_state) {
680
  global $user;
681
  $msg_type = 'status';
682
  $username = $user->name;
683
  $variable_to_use = 'cdm_dataportal_' . $user->uid . '_default_tab';
684
685
  // If is the right user the variables are setted.
686
  if (arg(0) == 'user' && ($uid = arg(1)) && is_numeric($uid) && $user->uid == $uid) {
687
    $variable = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
688
    variable_set($variable_to_use . '_active', $form_state['values']['check']);
689
    variable_set($variable_to_use, $form_state['values']['cdm_dataportal_user_form']);
690
    if ($form_state['values']['check']) {
691
      drupal_set_message(check_plain(t('The user default tab will be used for the next taxon site visit.')));
692
      drupal_set_message(check_plain(t('The user default tab has been changed to: !tab for the user !user', array(
693
        '!tab' => $variable[variable_get($variable_to_use, 0)],
694
        '!user' => $username,
695
      ))), $msg_type);
696
    }
697
    else {
698
      drupal_set_message(check_plain(t('The user default tab wont be used for
699
        the next taxon site, check the box if you want to use the user default configuration.')));
700
    }
701
    // Problem with the user id => variables wont be saved.
702
  }
703
  else {
704
    $msg_type = 'warning';
705
    drupal_set_message(check_plain(t('Default tab has not been saved due to user id problems')), $msg_type);
706
  }
707
}
708
709
/**
710
 * Implements hook_block_info().
711
 */
712
function cdm_dataportal_block_info() {
713
714
    // $block[0]["info"] = t("CDM DataPortal DevLinks");
715
    // $block[1]["info"] = t("CDM DataPortal Credits");
716 fc13aa30 Andreas Kohlbecker
    $block["2"] = array(
717
        "info" => t("CDM Search Taxa"),
718
        "cache" => DRUPAL_NO_CACHE
719
      );
720 6657531f Andreas Kohlbecker
    // $block[3]["info"] = t("CDM Filters");
721
    $block["4"]["info"] = t("CDM Dataportal Print");
722
    $block["keys"]["info"] = t("CDM identification keys");
723
    $block["fundedByEDIT"]["info"] = t('Funded by EDIT');
724 f9a3d0f6 Andreas Kohlbecker
    $block["classification_breadcrumbs"] =  array(
725
        'info' => t('Classification breadcrumbs'),
726
        'cache' => DRUPAL_CACHE_PER_PAGE
727
      );
728 6657531f Andreas Kohlbecker
729
    return $block;
730
}
731
732
/**
733
 * Implements hook_block_view().
734
 */
735
function cdm_dataportal_block_view($delta) {
736
  // TODO Rename block deltas (e.g. '2') to readable strings.
737
  switch ($delta) {
738
    // case 'delta-1':
739
    // $block['subject'] = t('Credits');
740
    // $block['content'] = theme('cdm_credits');
741
    // return $block;
742
    case '2':
743
      $block['subject'] = t('Search taxa');
744 191f88fd Andreas Kohlbecker
      $form = drupal_get_form('cdm_dataportal_search_taxon_form');
745
      $block['content'] = drupal_render($form);
746 6657531f Andreas Kohlbecker
747
      if (variable_get('cdm_dataportal_show_advanced_search', 1)) {
748
        $block['content'] .= '<div>' . l(t('Advanced Search'), 'cdm_dataportal/search') . '</div>';
749
      }
750
      return $block;
751
    case '4':
752 c8234bdf Andreas Kohlbecker
      $block['subject'] = '';
753 6657531f Andreas Kohlbecker
      $block['content'] = theme('cdm_print_button');
754
      return $block;
755
    case "keys":
756
      $block['subject'] = t('Identification Keys');
757
      $block['content'] = theme('cdm_block_IdentificationKeys', array('taxonUuid' => NULL));
758
      return $block;
759
    case "fundedByEDIT":
760
      // t('Funded by EDIT');
761
      $text = '<none>';
762
      $block['subject'] = $text;
763 889c74b7 Andreas Kohlbecker
      $img_tag = '<img src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/powered_by_edit.png' . '" alt="' . $text . '"/>';
764
      $block['content'] = l($img_tag, "http://cybertaxonomy.org/", array(
765 6657531f Andreas Kohlbecker
        'attributes' => array("target" => "EDIT"),
766
        'absolute' => TRUE,
767
        'html' => TRUE,
768
      ));
769
      return $block;
770 f9a3d0f6 Andreas Kohlbecker
    case 'classification_breadcrumbs':
771
      if (arg(1) == 'taxon' && is_uuid(arg(2))) {
772
        $block['subject'] = '<none>';
773
        $block['content'] = compose_classification_breadcrumbs(arg(2));
774
        return $block;
775
      }
776
      break;
777 6657531f Andreas Kohlbecker
  }
778
}
779
780
/*
781
 function cdm_dataportal_session_clear($cdm_ws_uri_update = FALSE){
782
 $_SESSION['cdm'] = NULL;
783
 if(is_string($cdm_ws_uri_update)){
784
 $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
785
 }
786
 }
787
788
 function cdm_dataportal_session_validate(){
789
 if(!isset($_SESSION['cdm']['ws_uri'])){
790
 $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', FALSE));
791
 } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', FALSE)){
792
 cdm_dataportal_session_clear(variable_get('cdm_webservice_url', FALSE));
793
 }
794
 }
795
 */
796
797
/**
798 d7588d36 Andreas Kohlbecker
 * creates a  selector form for taxonomic trees.
799
 *
800
 * @return a drupal form array
801 6657531f Andreas Kohlbecker
 */
802
function cdm_taxonomictree_selector() {
803
  _add_js_treeselector();
804
805 a8a8f23c Andreas Kohlbecker
  $form = drupal_get_form('cdm_taxonomictree_selector_form');
806 d7588d36 Andreas Kohlbecker
  return $form;
807 6657531f Andreas Kohlbecker
}
808
809
/**
810
 * @todo Please document this function.
811
 * @see http://drupal.org/node/1354
812
 */
813
function cdm_taxonomictree_selector_form($form, &$form_state) {
814
815
  $url = url('cdm_api/setvalue/session', array('query' => NULL));
816
  $form['#action'] = $url;
817
818
  $form['var'] = array(
819
    '#weight' => -3,
820
    '#type' => 'hidden',
821
    '#value' => '[cdm][taxonomictree_uuid]',
822
  );
823
824
  $destination_array = drupal_get_destination();
825
  $destination = $destination_array['destination'];
826
827
  $form['destination'] = array(
828
    '#weight' => -3,
829
    '#type' => 'hidden',
830
    '#value' =>  $destination,
831
  );
832
833 26fb3778 Andreas Kohlbecker
  $options = cdm_get_taxontrees_as_options();
834
  $taxontree_includes = variable_get(CDM_TAXONTREE_INCLUDES, null);
835
  if($taxontree_includes){
836
    $filtered_options = array();
837
    foreach($options as $uuid=>$label){
838
      if(!empty($taxontree_includes[$uuid])){
839
        $filtered_options[$uuid] = $label;
840
      }
841
    }
842
    $options = $filtered_options;
843
  }
844
845 6657531f Andreas Kohlbecker
  $form['val'] = array(
846
    '#type' => 'select',
847
    '#title' => t('Available classifications'),
848 7663cd0b Andreas Kohlbecker
    '#default_value' => get_current_classification_uuid(),
849 26fb3778 Andreas Kohlbecker
    '#options' => $options,
850 088a7028 Andreas Kohlbecker
    '#attributes' => array('class' => array('highlite-first-child'))
851 6657531f Andreas Kohlbecker
  );
852
853
  return $form;
854
855
}
856
857
/* UNREACHABLE since action of form directly links to view.
858
 function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
859
860
 $_SESSION['cdm']['search'] = $form_values;
861
 //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
862
 return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
863
 //$paramstr = compose_url_prameterstr($form_values);
864
 //return url('/cdm_dataportal/search/taxon/', array('query' => $paramstr));
865
 }
866
 */
867
/* ====================== menu callback functions ====================== */
868
/**
869
 * @todo Please document this function.
870
 * @see http://drupal.org/node/1354
871
 */
872
/*
873
function cdm_dataportal_form_alter(&$form, &$form_state, $form_id) {
874
  static $comment_node_disabled =  0;
875
  static $comment_node_read_only =  1;
876
  static $comment_node_read_write =  2;
877
878
  if ($form_id == 'node_type_form'
879
   && isset($form['identity']['type'])
880
   && array_key_exists($form['#node_type']->type, cdm_get_nodetypes())
881
  ) {
882
    $form['workflow']['comment'] = array(
883
      '#type' => 'radios',
884
      '#title' => t('Default comment setting'),
885
      '#default_value' => variable_get('comment__' . $node->type . $form['#node_type']->type, $comment_node_disabled),
886
      '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
887
      '#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
888
    );
889
  }
890
}
891
*/
892
893
/**
894
 * Displays a list of the known taxonomic names.
895
 *
896
 * When the list of taxonomic names is displayed, long lists are split up into
897
 * multiple pages.
898
 *
899
 * TODO: Parameters are still preliminary.
900
 *
901
 * @param string $beginsWith
902
 * @param string $page
903
 *   Page number to diplay defaults to page 1.
904
 * @param bool $onlyAccepted
905
 */
906
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = FALSE) {
907
908
  $out = t('<h3>Sorry, the name list feature is not yet available in this version of the DataPortal software<h3>');
909
910
  /*
911
  // FIXME the filter for accepted names will be a form element, thus this
912
  // widget should be generated via form api preferably as block.
913
  $out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
914
  $out .= theme('cdm_dataportal_widget_names_list', $names, $page);
915
  $out .= theme('cdm_listof_taxa', $taxonPager);
916
  return $out;
917
  */
918
}
919
920
/**
921
 * @todo Please document this function.
922
 * @see http://drupal.org/node/1354
923
 */
924
function cdm_dataportal_view_reference($uuid, $arg2 = NULL) {
925 f26245c8 Andreas Kohlbecker
926
  cdm_check_valid_portal_page();
927
928 6657531f Andreas Kohlbecker
  $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
929
  return theme('cdm_reference_page', array('reference' => $reference));
930
}
931
932
/**
933 b5dca1e2 Andreas Kohlbecker
 * Created a view on a all references contained in the portal.
934
 *
935
 * This function is used at the path cdm_dataportal/reference/list
936 6657531f Andreas Kohlbecker
 */
937
function cdm_dataportal_view_reference_list($pageNumber) {
938
  $referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE), $pageNumber);
939
  return theme('cdm_reference_pager', array(
940
    'referencePager' => $referencePager,
941
    'path' => 'cdm_dataportal/reference/list/',
942
    ));
943
}
944
945
/**
946
 * @todo Please document this function.
947
 * @see http://drupal.org/node/1354
948
 */
949
function cdm_dataportal_view_media($mediaUuid, $mediarepresentation_uuid = FALSE, $part = 0) {
950 f26245c8 Andreas Kohlbecker
951
  cdm_check_valid_portal_page();
952
953 6657531f Andreas Kohlbecker
  $media = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $mediaUuid);
954
  return theme('cdm_media_page', array(
955
    'media' => $media,
956
    'mediarepresentation_uuid' => $mediarepresentation_uuid,
957
    'partId' => $part,
958
    ));
959
}
960
961
/**
962
 * @todo Please document this function.
963
 * @see http://drupal.org/node/1354
964
 */
965
function _load_taxonBase(&$taxonBase) {
966
  if (isset($taxonBase->uuid)) {
967
    $taxonBase->name = cdm_ws_get(CDM_WS_TAXON, array($taxonBase->uuid, "name"));
968
    $taxonBase->name->taggedName = cdm_ws_get(CDM_WS_NAME, array($taxonBase->name->uuid, "taggedName"));
969
    $taxonBase->name->nomenclaturalReference = cdm_ws_get(CDM_WS_NAME, array($taxonBase->name->uuid, "nomenclaturalReference"));
970
  }
971
}
972
973 9438ad3a Andreas Kohlbecker
/**
974
 * Loads the media associated to the given taxon from the cdm server.
975
 * The aggregation settings regarding taxon relathionships and
976
 * taxonnomic childen are taken into account.
977
 *
978
 * The media lists are cached in a static variable.
979
 *
980
 * @param Taxon $taxon
981
 *   A CDM Taxon entitiy
982
 *
983
 * @return array
984
 *   An array of CDM Media entities
985
 *
986
 */
987
function _load_media_for_taxon($taxon) {
988
989
  static $media = NULL;
990
991
  if(!isset($media)) {
992
    $media = array();
993
  }
994
  if (!isset($media[$taxon->uuid])) {
995
996 f19f47fa Andreas Kohlbecker
    // --- GET Images --- //
997
    $mediaQueryParameters = array(
998
        "type" => "ImageFile",
999
    );
1000
1001
    $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
1002
    $mediaQueryParameters['relationships'] = implode(',', get_selection($relationship_choice['direct']));
1003 a950f2f9 Andreas Kohlbecker
    $mediaQueryParameters['relationshipsInvers'] = implode(',', get_selection($relationship_choice['invers']));
1004
1005
    $taxon_media_filter_choice = variable_get(CDM_TAXON_MEDIA_FILTER, unserialize(CDM_TAXON_MEDIA_FILTER_DEFAULT));
1006
    $mediaQueryParameters['includeTaxonDescriptions'] = (boolean) $taxon_media_filter_choice['includeTaxonDescriptions'] != 0;
1007
    $mediaQueryParameters['includeOccurrences'] = (boolean) $taxon_media_filter_choice['includeOccurrences'] != 0;
1008
    $mediaQueryParameters['includeTaxonNameDescriptions'] = (boolean) $taxon_media_filter_choice['includeTaxonNameDescriptions'] != 0;
1009 9438ad3a Andreas Kohlbecker
1010 f19f47fa Andreas Kohlbecker
    $ws_endpoint = NULL;
1011 9438ad3a Andreas Kohlbecker
    if ( variable_get('cdm_images_include_children', 0) == 0) {
1012
      $ws_endpoint = CDM_WS_PORTAL_TAXON_MEDIA;
1013
    } else {
1014
      $ws_endpoint = CDM_WS_PORTAL_TAXON_SUBTREE_MEDIA;
1015
    }
1016 f19f47fa Andreas Kohlbecker
1017
    $media[$taxon->uuid] = cdm_ws_get($ws_endpoint,
1018
        array(
1019
            $taxon->uuid,
1020
        ),
1021 9438ad3a Andreas Kohlbecker
        queryString($mediaQueryParameters)
1022
       );
1023
  }
1024
1025
  return $media[$taxon->uuid];
1026
}
1027
1028
/**
1029
 *
1030
 * @param Taxon $taxon
1031
 *   A CDM Taxon entitiy
1032
 *
1033
 * @return array
1034
 *   An array of CDM SpecimenOrObservation entities
1035
 *
1036
function _load_occurences_for_taxon($taxon){
1037
1038
  static $occurences = NULL;
1039 f19f47fa Andreas Kohlbecker
1040
  if(!isset($occurences)) {
1041
    $occurences = array();
1042
  }
1043 9438ad3a Andreas Kohlbecker
1044
  if (!isset($occurences[$taxon->uuid])){
1045
1046
    $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
1047
    $relationship_choice['direct'] = get_selection($relationship_choice['direct']);
1048 f19f47fa Andreas Kohlbecker
    $relationship_choice['invers'] = get_selection($relationship_choice['invers']);
1049
1050
    $by_associatedtaxon_query = http_build_query(array(
1051
        'relationshipsInvers' => implode(',', $relationship_choice['invers']),
1052
        'relationships' => implode(',', $relationship_choice['direct']),
1053
        'pageSize' => null // all hits in one page
1054
    )
1055
    );
1056
1057
    $pager = cdm_ws_get(CDM_WS_OCCURRENCE_BY_ASSOCIATEDTAXON,
1058
        null,
1059
        $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
1060
    );
1061
1062
1063
    if(isset($pager->records[0])){
1064
      $occurences[$taxon->uuid] =  $pager->records;
1065
    }
1066
  }
1067 9438ad3a Andreas Kohlbecker
  return $occurences[$taxon->uuid];
1068
}
1069
 */
1070
1071 6657531f Andreas Kohlbecker
/**
1072
 * Gets a Drupal variable, string or array and returns it.
1073
 *
1074
 * Similar to the variable_get() function of Drupal, except that this function
1075
 * is able to handle arrays correctly. This function is especially useful
1076 8c962983 Andreas Kohlbecker
 * when dealing with collections of settings form elements (#tree = TRUE).
1077 6657531f Andreas Kohlbecker
 *
1078
 * @param string $variableKey
1079
 *   The Unique key of the Drupal variable in the Drupal variables table.
1080
 * @param string $defaultValueString
1081
 *   A string as for example derived from a CONSTANT.
1082
 *
1083
 * @return mixed
1084 8c962983 Andreas Kohlbecker
 *   usually an array, depending on the nature of the variable.
1085
 *
1086
 * TODO compare with get_array_variable_merged() duplicate functions?
1087 0f7c9d15 Andreas Kohlbecker
 * @deprecated rather use get_array_variable_merged() since this function
1088
 * used an array as second parameter
1089 6657531f Andreas Kohlbecker
 */
1090
function mixed_variable_get($variableKey, $defaultValueString) {
1091
  $systemDefaults = unserialize($defaultValueString);
1092
  $storedSettings = variable_get($variableKey, array());
1093
  if (is_array($storedSettings)) {
1094 8c962983 Andreas Kohlbecker
    // TODO better use drupal_array_merge_deep() ?
1095 6657531f Andreas Kohlbecker
    $settings = array_merge($systemDefaults, $storedSettings);
1096
  }
1097
  else {
1098
    $settings = $systemDefaults;
1099
  }
1100
  return $settings;
1101
}
1102
1103 b5519d3a Andreas Kohlbecker
/**
1104 0f7c9d15 Andreas Kohlbecker
 * Recursive function to convert an object into an array.
1105 b5519d3a Andreas Kohlbecker
 * also subordinate objects will be converted.
1106
 *
1107
 * @param object $object
1108
 * @return the array
1109
 */
1110
function convert_to_array($object) {
1111
  if(is_object($object) || is_array($object)) {
1112
    $array = (array)$object;
1113
    foreach ($array as $key=>$value){
1114
      $array[$key] = convert_to_array($value);
1115
    }
1116
    return $array;
1117
  } else {
1118
    return $object;
1119
  }
1120
}
1121
1122 2fd6da0b Andreas Kohlbecker
/**
1123
 * Searches the $collection for the cdm entitiy given as $element.
1124
 *
1125
 * The elements are compared by their UUID.
1126
 *
1127
 * @param $element
1128
 *  the CDM entitiy to search for
1129
 * @param $collection
1130
 *  the list of CDM entities to search in
1131
 *
1132
 * @return boolean TRUE if the $collection contains the $element, otheriwse FALSE
1133
 *
1134
 */
1135
function contains_cdm_entitiy($element, $collection) {
1136
  $result = FALSE;
1137
  foreach ($collection as $a) {
1138
    if ($a->uuid == $element->uuid) {
1139
      $result = TRUE;
1140
    }
1141
  }
1142
  return $result;
1143
}
1144
1145
/**
1146
 * Fiters the array $entity_list of CDM entities by the list
1147
 * of $excludes. Any element contained in the $excludes will be removed
1148
 * from included int the retuned list.
1149
 *
1150
 * If the $entity_list is not an array the $excludes will be returned.
1151
 */
1152
function filter_cdm_entity_list($entity_list, $excludes) {
1153
  if (is_array($entity_list)) {
1154
    $result = $entity_list;
1155
    if ($excludes) {
1156
      foreach ($excludes as $exclude) {
1157
        if (!contains_cdm_entitiy($exclude, $entity_list)) {
1158
          $result[] = $exclude;
1159
        }
1160
      }
1161
    }
1162
  }
1163
  else {
1164
    $result = $excludes;
1165
  }
1166
  return $result;
1167
}
1168
1169 0a1151a4 Andreas Kohlbecker
/**
1170 6f5c7f2a Andreas Kohlbecker
 * Wraps the given $html string into a render array suitable for drupal_render()
1171 0a1151a4 Andreas Kohlbecker
 *
1172 65345976 Andreas Kohlbecker
 * @param $html
1173 0a1151a4 Andreas Kohlbecker
 *   the html string, see
1174
 *   http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#markup
1175 65345976 Andreas Kohlbecker
 * @param $weight
1176 0a1151a4 Andreas Kohlbecker
 *   A positive or negative number (integer or decimal).
1177
 *   see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#weightval
1178 65345976 Andreas Kohlbecker
 * @param $prefix
1179
 *   Optional markup for the '#prefix' element of the render array
1180
 * @param $suffix
1181
 *   Optional markup for the '#suffix' element of the render array
1182 0a1151a4 Andreas Kohlbecker
 *
1183 65345976 Andreas Kohlbecker
 * @return array
1184 0a1151a4 Andreas Kohlbecker
 *   A render array
1185
 *
1186
 */
1187 65345976 Andreas Kohlbecker
function markup_to_render_array($html, $weight = FALSE, $prefix = NULL, $suffix = NULL) {
1188 6f5c7f2a Andreas Kohlbecker
  $render_array = array(
1189
    '#markup' => $html
1190 1ce9afb7 Patric Plitzner
      );
1191 6f5c7f2a Andreas Kohlbecker
  if (is_numeric($weight)) {
1192
    $render_array['#weight'] = $weight;
1193 65345976 Andreas Kohlbecker
  }
1194
  if($prefix){
1195
    $render_array['#prefix'] = $prefix;
1196
  }
1197
  if($suffix) {
1198
    $render_array['#suffix'] = $suffix;
1199
  }
1200 6f5c7f2a Andreas Kohlbecker
  return $render_array;
1201 0a1151a4 Andreas Kohlbecker
}
1202
1203 6657531f Andreas Kohlbecker
/**
1204
 * Loads the subgraph of a given PolytomousKeyNode.
1205
 *
1206
 * Loads the subgraph of the given PolytomousKeyNode recursively from
1207
 * the CDM REST service.
1208
 *
1209
 * @param mixed $polytomousKeyNode
1210
 *   PolytomousKeyNode passed by reference.
1211
 *
1212
 * @return void
1213
 */
1214
function _load_polytomousKeySubGraph(&$polytomousKeyNode) {
1215
1216
  if (!$polytomousKeyNode) {
1217
    return;
1218
  }
1219
  if ($polytomousKeyNode->class != "PolytomousKeyNode") {
1220 f19f47fa Andreas Kohlbecker
    drupal_set_message('_load_polytomousKeySubGraph(): ' . t('invalid type given.'), 'error');
1221 6657531f Andreas Kohlbecker
    return;
1222
  }
1223
  if (!is_uuid($polytomousKeyNode->uuid)) {
1224 f19f47fa Andreas Kohlbecker
    drupal_set_message('_load_polytomousKeySubGraph(): ' . t('invalid type given.'), 'error');
1225 6657531f Andreas Kohlbecker
    return;
1226
  }
1227
1228
  $polytomousKeyNode = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, $polytomousKeyNode->uuid);
1229
1230
  if (!$polytomousKeyNode) {
1231
    // drupal_set_message("_load_polytomousKeyChildNodes() : could not load polytomousKeyNode", "error");
1232
    return;
1233
  }
1234
1235
  // Load children.
1236
  foreach ($polytomousKeyNode->children as &$childNode) {
1237
    _load_polytomousKeySubGraph($childNode);
1238
  }
1239
1240
  // Load subkey.
1241
  $polytomousKeyNode->subkey = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, array($polytomousKeyNode->uuid, "subkey"));
1242
1243
  // Load taxon.
1244
  $polytomousKeyNode->taxon = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, array($polytomousKeyNode->uuid, "taxon"));
1245
  _load_taxonBase($polytomousKeyNode->taxon);
1246
  return;
1247
}
1248
1249
/**
1250
 * @todo Please document this function.
1251
 * @see http://drupal.org/node/1354
1252
 */
1253
function cdm_dataportal_view_polytomousKey($polytomousKeyUuid) {
1254 f26245c8 Andreas Kohlbecker
1255
  cdm_check_valid_portal_page();
1256
1257 6657531f Andreas Kohlbecker
  $polytomousKey = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, $polytomousKeyUuid);
1258
1259
  $sourcePager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'sources'));
1260
  if (is_array($sourcePager->records)) {
1261
    $polytomousKey->sources = $sourcePager->records;
1262
    // $polytomousKey->sources->citation = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'sources'));
1263
  }
1264
1265
  $annotationPager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'annotations'));
1266
  if (is_array($annotationPager->records)) {
1267
    $polytomousKey->annotations = $annotationPager->records;
1268
  }
1269
1270
  _load_polytomousKeySubGraph($polytomousKey->root);
1271
  return theme('cdm_polytomousKey_page', array('polytomousKey' => $polytomousKey));
1272
}
1273
1274
/**
1275
 * Creates a taxon page view or a chapter of it.
1276
 *
1277
 * The taxon page gives detailed information on a taxon, it shows:
1278
 *  - Taxon name.
1279
 *  - Full list of synonyms homotypic synonyms on top, followed by the
1280
 *    heterotypic and finally followed by misapplied names.
1281
 *    The list is ordered historically.
1282
 *  - All description associated with the taxon.
1283
 *
1284
 * @param string $uuid
1285
 * @param string $chapter
1286
 *   Name of the part to display, valid values are:
1287
 *   'description', 'images', 'synonymy', 'specimens', 'all'.
1288
 *
1289
 * @return unknown_type
1290
 */
1291
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all', $synonym_uuid = NULL) {
1292 5ea8b301 Andreas Kohlbecker
1293 f26245c8 Andreas Kohlbecker
  cdm_check_valid_taxon_page($chapter);
1294 2dd59bb5 Andreas Kohlbecker
  cdm_dd("START OF TAXON PAGE [" . $chapter . "] " . $uuid . ' for ' . $_GET['q']);
1295 5ea8b301 Andreas Kohlbecker
  // show a warning in case the javascript development mode is anabled
1296
  if(variable_get('cdm_js_devel_mode', FALSE)) {
1297
    drupal_set_message(t('The !url1 is enabled.
1298
        WARNING: this is a performance penalty and must be turned off on production websites.', array(
1299
          '!url1' => l('java-script development mode', 'admin/config/cdm_dataportal/settings', array('fragment' => 'edit-cdm-js-devel-mode'))
1300
    )),
1301
    'warning'
1302
        );
1303
  }
1304
1305 6657531f Andreas Kohlbecker
  // Display the page for the taxon defined by $uuid.
1306
  // set_last_taxon_page_tab(arg(3));
1307
  $taxonpage = cdm_dataportal_taxon_view($uuid, $chapter, $synonym_uuid);
1308
  if (!empty($taxonpage)) {
1309 2dd59bb5 Andreas Kohlbecker
    cdm_dd("END OF TAXON PAGE [" . $chapter . "] " . $uuid);
1310 6657531f Andreas Kohlbecker
    return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title, $taxonpage->content);
1311
  }
1312
  else {
1313 2dd59bb5 Andreas Kohlbecker
    cdm_dd("END OF TAXON PAGE [" . $chapter . "] " . $uuid . ' !!! PAGE IS EMPTY !!!');
1314 6657531f Andreas Kohlbecker
    return '';
1315
  }
1316
}
1317
1318
/**
1319 59f6da42 Andreas Kohlbecker
 * This function will genreate the taxon page part ($chapter) and returns a taxonpage object
1320
 * which has two fields, one for the page title and one for the content. Later on in the
1321
 * process chain the value contained in these fields will be passed to the cdm_node_show()
1322
 * function as the function parameters $title and $content.
1323 6657531f Andreas Kohlbecker
 *
1324
 * @param string $uuid
1325 59f6da42 Andreas Kohlbecker
 *   the uuid of the taxon to show
1326 6657531f Andreas Kohlbecker
 * @param string $chapter
1327
 *   Name of the part to display, valid values are:
1328
 *   'description', 'images', 'synonymy', 'all'.
1329
 *
1330 59f6da42 Andreas Kohlbecker
 * @return taxonpage object with the following fields:
1331
 *   - title : the title of the page
1332
 *   - content: the content of the page
1333
 *
1334 6657531f Andreas Kohlbecker
 */
1335 8cf9238f Andreas Kohlbecker
function cdm_dataportal_taxon_view($uuid, $chapter = 'all') {
1336 6657531f Andreas Kohlbecker
  // Taxon object.
1337
  $taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $uuid);
1338
  if (empty($taxon)) {
1339
    drupal_set_title(t('Taxon does not exist'), PASS_THROUGH);
1340
    return FALSE;
1341
  }
1342
  $taxonpage = new stdClass();
1343
1344
  $taxonpage->title = theme('cdm_taxon_page_title', array(
1345 8cf9238f Andreas Kohlbecker
    'taxon' => $taxon
1346 6657531f Andreas Kohlbecker
  ));
1347
1348
  // Check if the taxon id contained in the currently selected tree.
1349 7663cd0b Andreas Kohlbecker
  $taxon_in_current_classification = taxon_in_current_classification($uuid);
1350 61b6ee11 Andreas Kohlbecker
1351 7663cd0b Andreas Kohlbecker
  if (!$taxon_in_current_classification) {
1352 61b6ee11 Andreas Kohlbecker
    $classifications = get_classifications_for_taxon($taxon);
1353
    if (count($classifications) == 0) {
1354 fba00a4f Andreas Kohlbecker
      drupal_set_message(t('This concept of the taxon !taxonname is not contained as an accepted taxon in currently chosen classification.',
1355
        array(
1356 63c3ac73 Andreas Kohlbecker
        '!taxonname' => render_taxon_or_name($taxon->name),
1357 fba00a4f Andreas Kohlbecker
        )
1358
      ), 'warning');
1359 6657531f Andreas Kohlbecker
    }
1360
    else {
1361
      $trees = '';
1362 61b6ee11 Andreas Kohlbecker
      foreach ($classifications as $classification) {
1363
        if (isset($classification->titleCache)) {
1364
          $trees .= ($trees ? ', ' : '') . '<strong>' . $classification->titleCache . '</strong>';
1365 6657531f Andreas Kohlbecker
        }
1366
      }
1367
1368 15b7c460 Andreas Kohlbecker
      drupal_set_message(format_plural(count($trees),
1369 50172c55 Andreas Kohlbecker
          'This concept of the taxon !taxonname is not contained as an accepted taxon in currently chosen classification, but in this one: !trees',
1370
          'This concept of the taxon !taxonname is not contained as an accepted taxon in currently chosen classification, but in one of these: !trees',
1371 fba00a4f Andreas Kohlbecker
          array('!taxonname' => render_taxon_or_name($taxon), '!trees' => $trees)
1372
        ) ,
1373
        'warning');
1374 6657531f Andreas Kohlbecker
    }
1375
  }
1376
1377
  // Render the taxon page.
1378 9a607f17 Andreas Kohlbecker
  $render_array = compose_cdm_taxon_page($taxon, $chapter);
1379
  $taxonpage->content = drupal_render($render_array);
1380 6657531f Andreas Kohlbecker
1381
  return $taxonpage;
1382
}
1383
1384 9d37c9d9 Patric Plitzner
/**
1385
 * Creates a specimen page view.
1386
 * @param string $uuid the UUID of the specimen
1387
 * @return array|string
1388
 */
1389
function cdm_dataportal_specimen_page_view($uuid) {
1390
1391
    //cdm_check_valid_taxon_page($chapter);
1392
    //cdm_dd("START OF TAXON PAGE [" . $chapter . "] " . $uuid . ' for ' . $_GET['q']);
1393
    // show a warning in case the javascript development mode is anabled
1394
    if(variable_get('cdm_js_devel_mode', FALSE)) {
1395
        drupal_set_message(t('The !url1 is enabled.
1396
        WARNING: this is a performance penalty and must be turned off on production websites.', array(
1397
            '!url1' => l('java-script development mode', 'admin/config/cdm_dataportal/settings', array('fragment' => 'edit-cdm-js-devel-mode'))
1398
        )),
1399
            'warning'
1400
        );
1401
    }
1402
1403
    // Display the page for the specimen defined by $uuid.
1404
    $specimenpage = cdm_dataportal_specimen_view($uuid);
1405
    if (!empty($specimenpage)) {
1406
        return cdm_node_show(NODETYPE_TAXON, $uuid, $specimenpage->title, $specimenpage->content);
1407
    }
1408
    else {
1409
        return '';
1410
    }
1411
}
1412
1413
/**
1414
 *
1415
 * Creates a specimen view.
1416
 * @param string $uuid the UUID of the specimen
1417
 * @return array|string
1418
 */
1419
function cdm_dataportal_specimen_view($uuid) {
1420
    $specimen = cdm_ws_get(CDM_WS_OCCURRENCE, $uuid);
1421
    if (empty($specimen)) {
1422
        drupal_set_title(t('Specimen does not exist'), PASS_THROUGH);
1423
        return FALSE;
1424
    }
1425
    $specimenpage = new stdClass();
1426
1427
    $specimenpage->title = theme('cdm_specimen_page_title', array(
1428
        'specimen' => $specimen
1429
    ));
1430
1431 b810158e Patric Plitzner
    // Render the specimen page.
1432
    $render_array = compose_cdm_specimen_page($uuid);
1433 9d37c9d9 Patric Plitzner
    $specimenpage->content = drupal_render($render_array);
1434
1435
    return $specimenpage;
1436
}
1437
1438 6657531f Andreas Kohlbecker
/**
1439
 * Returns a name page as a Drupal node ready to be renderized by Drupal.
1440
 *
1441
 * The node page shows the taxon name title and the list of taxon related
1442
 * with such taxon. Name on the tree already in use.
1443
 *
1444 51b04faf Andreas Kohlbecker
 * @param UUID $taxon_name_uuid
1445
 *   The uuid of the CDM TaxonNameBase to show a name page for
1446
 * @param UUID $taxon_to_hide_uuid
1447
 *   A taxon which should not be displayed in the taxon list
1448
 * @param UUID $highlite_synonym_uuid
1449
 *   Optinal parameter wich takes another taxon uuid, if given the
1450
 *   target taxon pages will show the syonymy tab where the taxon
1451
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1452
 *   in case it is found on this page.
1453 6657531f Andreas Kohlbecker
 *
1454
 * @return mixed
1455
 *   The formatted name page as node.
1456
 */
1457
function cdm_dataportal_name_page_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid = NULL) {
1458
1459 f26245c8 Andreas Kohlbecker
  cdm_check_valid_portal_page();
1460
1461 6657531f Andreas Kohlbecker
  $taxonname_page = cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid);
1462
  if (!empty($taxonname_page)) {
1463
    return cdm_node_show(NODETYPE_NAME, $taxon_name_uuid, $taxonname_page->title, $taxonname_page->content);
1464
  }
1465
  else {
1466
    return '';
1467
  }
1468
}
1469
1470
/**
1471 e3001c8d Andreas Kohlbecker
 * View function for a TaxonNameBase page.
1472 6657531f Andreas Kohlbecker
 *
1473 e3001c8d Andreas Kohlbecker
 * The name page lists all taxa for which the name specified by the
1474
 * $taxon_name_uuid is being used. I case there is only one name the
1475
 * page automatically redirects ti the according taxon page. Otherwise
1476
 * the list of names is displayed.
1477 6657531f Andreas Kohlbecker
 *
1478 e3001c8d Andreas Kohlbecker
 * The parameter $taxon_to_hide_uuid allows to exclude a taxon from the
1479
 * list of taxa. This is useful for example when referencing from a taxon
1480
 * to the name page and the referring taxon should not be repeaded in the
1481
 * name page.
1482 6657531f Andreas Kohlbecker
 *
1483 e3001c8d Andreas Kohlbecker
 *
1484
 * @param UUID $taxon_name_uuid
1485
 *   The uuid of the CDM TaxonNameBase to show a name page for
1486
 * @param UUID $taxon_to_hide_uuid
1487
 *   A taxon which should not be displayed in the taxon list
1488
 * @param UUID $highlite_synonym_uuid
1489
 *   Optinal parameter wich takes another taxon uuid, if given the
1490
 *   target taxon pages will show the syonymy tab where the taxon
1491
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1492
 *   in case it is found on this page.
1493
 *
1494
 * @return object
1495
 *   An object with two fields:
1496
 *     - title: the page title
1497
 *     - content: the page content
1498 6657531f Andreas Kohlbecker
 */
1499 e3001c8d Andreas Kohlbecker
function cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $highlite_synonym_uuid = NULL) {
1500 6657531f Andreas Kohlbecker
  // Getting the full taxonname object from the server.
1501
  $taxon_name = cdm_ws_get(CDM_WS_NAME, array($taxon_name_uuid));
1502
  if (!$taxon_name) {
1503
    drupal_set_title(t('Taxon name does not exist'), PASS_THROUGH);
1504
    return FALSE;
1505
  }
1506
  // Searching for all the taxa connected with the taxon name on the tree
1507
  // in use.
1508
  $name_cache = cdm_ws_get(CDM_WS_NAME_NAMECAHE, array($taxon_name_uuid));
1509
  $request_params = array();
1510
  $request_params['query'] = $name_cache;
1511 7663cd0b Andreas Kohlbecker
  $request_params['tree'] = get_current_classification_uuid();
1512 6657531f Andreas Kohlbecker
  $request_params['doTaxa'] = 1;
1513
  $request_params['doSynonyms'] = 1;
1514
  $request_params['doTaxaByCommonNames'] = 0;
1515
  $request_params['matchMode'] = "EXACT";
1516
  $taxon_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_FIND, NULL, queryString($request_params));
1517
1518
  // Removing the name where we come from.
1519
  foreach ($taxon_pager->records as $k => &$taxon) {
1520
    if ($taxon->uuid == $taxon_to_hide_uuid) {
1521
      unset($taxon_pager->records[$k]);
1522
    }
1523
  }
1524
  // Show the taxa list or go to the singular taxon.
1525
  if (sizeof($taxon_pager->records) == 1) {// Single taxon case.
1526 e3001c8d Andreas Kohlbecker
    $singleTaxon = array_pop($taxon_pager->records);
1527 6657531f Andreas Kohlbecker
    if ($singleTaxon->class != "Taxon") {
1528
      // It is a Synonym -> look for the accepted.
1529 7663cd0b Andreas Kohlbecker
      $accepted_taxa = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, array($singleTaxon->uuid, get_current_classification_uuid()));
1530 e3001c8d Andreas Kohlbecker
      if (!empty($highlite_synonym_uuid)) {
1531 f0bf24e7 Andreas Kohlbecker
        drupal_goto('cdm_dataportal/taxon/' . $accepted_taxa[0]->uuid . '/synonymy', array('query' => array('highlite' => $highlite_synonym_uuid)));
1532 6657531f Andreas Kohlbecker
      }
1533
      else {
1534 f0bf24e7 Andreas Kohlbecker
        drupal_goto('cdm_dataportal/taxon/' . $accepted_taxa[0]->uuid . '/synonymy', array('query' => array('highlite' => $singleTaxon->uuid)));
1535 6657531f Andreas Kohlbecker
      }
1536
    }
1537
    else {
1538
      // It is an accepted taxon.
1539 e3001c8d Andreas Kohlbecker
      if (!empty($highlite_synonym_uuid)) {
1540
        drupal_goto('cdm_dataportal/taxon/' . $singleTaxon->uuid . '/synonymy', array('query' => array('highlite' => $highlite_synonym_uuid)));
1541 6657531f Andreas Kohlbecker
      }
1542
      else {
1543
        drupal_goto('cdm_dataportal/taxon/' . $singleTaxon->uuid);
1544
      }
1545
    }
1546
  }
1547
  else {// More than one taxa case.
1548
    $taxon_name_page = new stdClass();
1549 4f225a0f Andreas Kohlbecker
    $taxon_name_page->title = theme('cdm_name_page_title', array('taxon_name' => $taxon_name));
1550 6657531f Andreas Kohlbecker
    if ($taxon_pager->records) {
1551
      $taxon_name_page->content = theme_cdm_list_of_taxa($taxon_pager->records, FALSE);
1552
    }
1553
    else {
1554
      $taxon_name_page->content = 'This name has no taxa';
1555
    }
1556
    return $taxon_name_page;
1557
  }
1558
}
1559
1560
/**
1561 84b13813 Andreas Kohlbecker
 * Creates a page with the advance search form.
1562
 *
1563
 * NOTE: The advance search form allows searching for taxa.
1564 6657531f Andreas Kohlbecker
 */
1565
function cdm_dataportal_view_search_advanced() {
1566
  drupal_set_title(t('Advanced search'), PASS_THROUGH);
1567
  return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
1568
}
1569
1570
/**
1571 84b13813 Andreas Kohlbecker
 * Creates a page with the search form for searching by taxon descriptions.
1572 6657531f Andreas Kohlbecker
 */
1573
function cdm_dataportal_view_search_taxon_by_description() {
1574 6441ddfb Andreas Kohlbecker
  drupal_set_title(t('Search by factual data'), PASS_THROUGH);
1575 6657531f Andreas Kohlbecker
  return drupal_get_form('cdm_dataportal_search_taxon_by_description_form');
1576
}
1577
1578
/**
1579
 * Executes the search and generates the result list of taxa.
1580
 */
1581
function cdm_dataportal_view_search_results_taxon() {
1582
1583
  $taxonPager = cdm_dataportal_search_execute();
1584
1585 f19f47fa Andreas Kohlbecker
  $showThumbnails = do_showThumbnails();
1586
1587
  $setSessionUri = url('cdm_api/setvalue/session', array(
1588
      'query' => array('var' => '[pageoption][searchtaxa][showThumbnails]', 'val' => ''),
1589
  ));
1590
1591
  drupal_add_js('jQuery(document).ready(function() {
1592
1593
      // init
1594
      if(' . $showThumbnails . ' == 1){
1595 1d69a96c Andreas Kohlbecker
          jQuery(\'.media_gallery\').show(20);
1596
      } else {
1597
          jQuery(\'.media_gallery\').hide(20);
1598
      }
1599
1600
      // add change handler
1601
      jQuery(\'#showThumbnails input.showThumbnails\').change(
1602 f19f47fa Andreas Kohlbecker
      function(event){
1603 1d69a96c Andreas Kohlbecker
        var state = 0;
1604
        if(jQuery(this).is(\':checked\')){
1605
          jQuery(\'.media_gallery\').show(20);
1606
          state = 1;
1607
        } else {
1608
          jQuery(\'.media_gallery\').hide(20);
1609
        }
1610
        // store state in session variable
1611
        var uri = \'' . $setSessionUri . '\' + state;
1612
        jQuery.get(uri);
1613
      });
1614 78e178e2 Andreas Kohlbecker
  });',
1615 f19f47fa Andreas Kohlbecker
  array('type' => "inline", 'scope' => JS_DEFAULT));
1616
1617 78e178e2 Andreas Kohlbecker
  drupal_set_title(t('Search results'), PASS_THROUGH);
1618
1619 6657531f Andreas Kohlbecker
  return theme('cdm_search_results', array(
1620
    'pager' => $taxonPager,
1621
    'path' => 'cdm_dataportal/search/results/taxon',
1622
    ));
1623
}
1624
1625 94550ff9 Andreas Kohlbecker
/**
1626
 * Provides the standart image wich indicated a loading process
1627
 *
1628
 * @return string
1629
 *  The img html tag
1630
 */
1631
function loading_image_html() {
1632
  return '<img class="loading" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal')
1633
    . '/images/loading_circle_grey_16.gif" style="display:none;">';
1634
}
1635
1636 78e178e2 Andreas Kohlbecker
/**
1637
 * Returns the state of the the showThumbnails flag set in the
1638
 * users session ($_SESSION['pageoption']['searchtaxa']['showThumbnails']).
1639
 *
1640 c5ed6357 Andreas Kohlbecker
 * @return boolean
1641 78e178e2 Andreas Kohlbecker
 *    returns 1 if the flag is set
1642
 */
1643
function do_showThumbnails() {
1644
  static $showThumbnails = null;
1645
1646
  if($showThumbnails == null) {
1647 c5ed6357 Andreas Kohlbecker
    $showThumbnails = 0;
1648
    if (!isset($_SESSION['pageoption']['searchtaxa']['showThumbnails'])) {
1649
      $showThumbnails = 0;
1650
      $search_gallery_settings = variable_get(CDM_DATAPORTAL_SEARCH_GALLERY_NAME, null);
1651
      $showThumbnails = is_array($search_gallery_settings)
1652
        && isset($search_gallery_settings['cdm_dataportal_show_taxon_thumbnails'])
1653
        && (
1654
            $search_gallery_settings['cdm_dataportal_show_taxon_thumbnails'] +
1655
            $search_gallery_settings['cdm_dataportal_show_synonym_thumbnails'] +
1656
            $search_gallery_settings['cdm_dataportal_show_thumbnail_captions'] > 0
1657
            )
1658
         ? 1 : 0;
1659 f19f47fa Andreas Kohlbecker
1660 c5ed6357 Andreas Kohlbecker
       drupal_array_set_nested_value($_SESSION, array('pageoption', 'searchtaxa', 'showThumbnails'), $showThumbnails);
1661
    }
1662 f19f47fa Andreas Kohlbecker
    $showThumbnails = $_SESSION['pageoption']['searchtaxa']['showThumbnails'];
1663
    if (!is_numeric($showThumbnails)) {
1664
      $showThumbnails = 1;
1665
    }
1666 78e178e2 Andreas Kohlbecker
  }
1667
1668
  return $showThumbnails;
1669
}
1670
1671 6657531f Andreas Kohlbecker
/**
1672
 * View which transforms XML output from a given webservice endpoint into JSON.
1673
 */
1674
/*
1675
 function cdm_view_xml2json(){
1676
 $file = arg(2);
1677
 $datastr = file_get_contents(variable_get('cdm_webservice_url', '').$file);
1678
 return  xml2json::transformXmlStringToJson($datastr);
1679
 }
1680
 */
1681
1682
/* ====================== other functions ====================== */
1683
/**
1684
 * Creates a URL to the taxon page specified by the $uuid parameter.
1685
 *
1686
 * The URL will be prepended with a path element to a specific taxon page tab.
1687
 *
1688
 * This tab is either taken from the CDM_DATAPORTAL_DEFAULT_TAXON_TAB which can
1689
 * be set globally in the administrative settings or individually in the user
1690
 * profile. If the CDM_DATAPORTAL_DEFAULT_TAXON_TAB value is set to LAST_VISITED_TAB
1691
 * the last portal will stay on this last tab.
1692
 *
1693
 * A third option is offerered by the $page_tab parameter which allows overwriting this
1694
 * internal mechanism by a specific value.
1695
 *
1696
 * @param string $uuid
1697
 *   The UUID of the taxon.
1698
 * @param string $page_tab
1699
 *   Overwriting the preset mechanism by defining specific value for the
1700
 *   taxon page tab.
1701
 *
1702
 * @return string
1703
 *   The created URL.
1704
 */
1705
function path_to_taxon($uuid, $page_tab = FALSE) {
1706
1707
  $tab = get_default_taxon_tab();
1708
  $values = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
1709
1710
  if (!$uuid) {
1711
    return FALSE;
1712
  }
1713
1714
  if ($page_tab) {
1715
    return 'cdm_dataportal/taxon/' . $uuid . '/' . $page_tab;
1716
  }
1717
  elseif (!$tab || strtolower($tab) == 'general') {
1718
    return 'cdm_dataportal/taxon/' . $uuid;
1719
  }
1720
  elseif (get_last_taxon_page_tab() &&   $tab == $values[CDM_DATAPORTAL_LAST_VISITED_TAB_ARRAY_INDEX]) {
1721
    return 'cdm_dataportal/taxon/' . $uuid . '/' . get_last_taxon_page_tab();
1722
  }
1723
  else {
1724
    return 'cdm_dataportal/taxon/' . $uuid . '/' . strtolower($tab);
1725
  }
1726
}
1727
1728 9d37c9d9 Patric Plitzner
function path_to_specimen($uuid) {
1729
1730
    if (!$uuid) {
1731
        return FALSE;
1732
    }
1733
    else {
1734
        return 'cdm_dataportal/specimen/' . $uuid;
1735
    }
1736
}
1737
1738 6657531f Andreas Kohlbecker
/**
1739 630c7044 Andreas Kohlbecker
 * Creates a URL to show a synonmy in the according taxon page.
1740
 *
1741
 * The URL will point to the synonymy tab of the taxon page of the accepted taxon given as parameter $acceptedUuid.
1742
 * The resulting URI will include query parameters to highlight the synonym, and to optionally display
1743
 * the accepted taxons name in aform like "Foo bar is accepted taxon for Ree doo". The URI will also
1744
 * include the sysnonym uuid as fragment in order to let the browser scroll to the according location
1745
 * in the page
1746
 *
1747
 * @param string $synonymUuid
1748
 *    The uuid of the synonym
1749
 * @param string $acceptedUuid
1750
 *    The uuid of the according accepted taxon
1751
 * @return string
1752
 *    The URL to show a synonmy in the according taxon page
1753 6657531f Andreas Kohlbecker
 */
1754
function uri_to_synonym($synonymUuid, $acceptedUuid) {
1755
  $acceptedPath = path_to_taxon($acceptedUuid, "synonymy");
1756 d071bed7 Andreas Kohlbecker
  return url($acceptedPath, array(
1757
      'query' => array(
1758
        // highlite the synony in the synonymy
1759
        'highlite' => $synonymUuid,
1760
        // the taxon page is refered from a synonym and the synonym can optionally be named in the page title
1761
        // see theme_taxon_page_title()
1762
        'acceptedFor' => $synonymUuid
1763 630c7044 Andreas Kohlbecker
      ),
1764
      'fragment' => $synonymUuid
1765 d071bed7 Andreas Kohlbecker
  ));
1766
1767 6657531f Andreas Kohlbecker
}
1768
1769
/**
1770 b8bfa4bd Andreas Kohlbecker
 * Compses the drupal path to the key identified by the uuid.
1771
 *
1772
 * @param string $keyType
1773
 *    the key typer corresponds to the specific class of the CDM
1774
 *    IdentificationKey. Possible values are
1775
 *      -PolytomousKey
1776
 *      -MultimediaKey
1777
 *      - ...
1778
 * @param UUID $keyUuid
1779
 *   The UUID of the key
1780 6657531f Andreas Kohlbecker
 */
1781
function path_to_key($keyType, $keyUuid) {
1782
  if (!$keyUuid || !$keyType) {
1783
    return FALSE;
1784
  }
1785
  $keyType{0} = strtolower($keyType{0});
1786
  return "cdm_dataportal/" . $keyType . "/$keyUuid";
1787
}
1788
1789
/**
1790
 * @todo Please document this function.
1791
 * @see http://drupal.org/node/1354
1792
 */
1793
function path_to_reference($uuid) {
1794
  if (!$uuid) {
1795
    return FALSE;
1796
  }
1797
  return 'cdm_dataportal/reference/' . $uuid;
1798
}
1799
1800
/**
1801 51b04faf Andreas Kohlbecker
 * Creates the path to a cdm_dataportal taxon name page.
1802
 *
1803
 * @param UUID $taxon_name_uuid
1804
 *   The uuid of the CDM TaxonNameBase to show a name page for
1805
 * @param UUID $taxon_to_hide_uuid
1806
 *   A taxon which should not be displayed in the taxon list
1807
 * @param UUID $highlite_synonym_uuid
1808
 *   Optinal parameter wich takes another taxon uuid, if given the
1809
 *   target taxon pages will show the syonymy tab where the taxon
1810
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1811
 *   in case it is found on this page.
1812
 *
1813
 * @return a URI path element as string
1814 6657531f Andreas Kohlbecker
 */
1815 51b04faf Andreas Kohlbecker
function path_to_name($name_uuid, $taxon_to_hide_uuid = NULL, $synonym_uuid  = NULL) {
1816 6657531f Andreas Kohlbecker
  $res = FALSE;
1817
  if ($name_uuid) {
1818
    $res = 'cdm_dataportal/name/' . $name_uuid;
1819
  }
1820 51b04faf Andreas Kohlbecker
  if($taxon_to_hide_uuid){
1821
    $res .= '/' . $taxon_to_hide_uuid;
1822
    if($synonym_uuid){
1823
      $res .= '/' . $synonym_uuid;
1824
    }
1825
  }
1826 6657531f Andreas Kohlbecker
  return $res;
1827
}
1828
1829
/**
1830
 * @todo Please document this function.
1831
 * @see http://drupal.org/node/1354
1832
 */
1833
function path_to_media($uuid, $representaion_uuid = FALSE, $partId = FALSE) {
1834
  if (!$uuid) {
1835
    return FALSE;
1836
  }
1837
  $out = 'cdm_dataportal/media/' . $uuid;
1838
  if ($representaion_uuid) {
1839
    $out .= '/' . $representaion_uuid;
1840
    if ($partId !== FALSE) {
1841
      $out .= '/' . $partId;
1842
    }
1843
  }
1844
  return $out;
1845
}
1846
1847
/**
1848
 * Compares thisRank with thatRank.
1849
 *
1850
 * Returns a negative integer, zero, or a positive integer
1851
 * as the of thisRank is higher than, equal to, or lower than thatRank.
1852
 * e.g:
1853
 * <ul>
1854
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
1855
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
1856
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
1857
 * </ul>
1858
 * <p>
1859
 * This compare logic of the underlying webservice is the
1860
 * <b>inverse logic</b> of the the one implemented in
1861
 * java.lang.Comparable#compareTo(java.lang.Object)
1862
 *
1863
 * @param $thisRankUuid
1864
 * @param $thatRankUuid
1865
 *
1866
 * @return int
1867
 *   A negative integer, zero, or a positive integer
1868
 *   as the thisRank is lower than, equal to, or higher than thatRank.
1869
 */
1870
function rank_compare($thisRankUuid, $thatRankUuid) {
1871
  $result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
1872
  return $result->Integer;
1873
}
1874
1875
/**
1876
 * Composes an HTML element class attribute value composed of
1877
 * the shortname of the cdm class and the uuid of the entity.
1878
 * This class attribute should be used whereever an cdm-entity is rendered.
1879
 *
1880
 * These according class selectors in css must be escaped, eg:
1881
 *    .cdm\:TextData
1882
 *
1883
 * @param $cdmEntity
1884
 */
1885 f19f47fa Andreas Kohlbecker
function html_class_attribute_ref($cdmEntity) {
1886 6657531f Andreas Kohlbecker
1887
  if (is_cdm_entity($cdmEntity)) {
1888
    return "cdm:" . $cdmEntity->class . " uuid:" . $cdmEntity->uuid;
1889
  }
1890
}
1891
1892
1893
/**
1894
 * Creates a short taxonname.
1895
 *
1896 e90899ac Andreas Kohlbecker
 * The short name is created by using the taggedTitle field of
1897 6657531f Andreas Kohlbecker
 * NameSTO or NameTO instances.
1898 e90899ac Andreas Kohlbecker
 * If the taggedTitle if empty the fullname will be returned.
1899 6657531f Andreas Kohlbecker
 *
1900
 * @param unknown_type $name
1901
 *   Name or TreeNode.
1902
 *
1903
 * @return string
1904
 */
1905
function cdm_dataportal_shortname_of($name) {
1906
  $nameStr = '';
1907
1908 e90899ac Andreas Kohlbecker
  normalize_tagged_text($name->taggedTitle);
1909 6657531f Andreas Kohlbecker
1910
  // Get all tagged text tokens of the scientific name.
1911
  foreach ($name->taggedTitle as $tagtxt) {
1912
    if ($tagtxt->type == 'name' || $tagtxt->type == 'rank') {
1913
      $nameStr .= ($nameStr ? ' ' : '') . $tagtxt->text;
1914
    }
1915
  }
1916
  $nameStr = trim($nameStr);
1917
1918
  if ($nameStr) {
1919
1920
    // Do not return short names for these.
1921
    if ($name->unplaced || $name->excluded) {
1922
      return $nameStr;
1923
    }
1924
1925
    /*
1926
    1st capture group (^[a-zA-Z]): First letter of uninomial.
1927
    Second capture group ([\p{L}]+): remaining letters of uninomial ([\p{L} = an UTF-8 letter).
1928
    Third capture group (\s+[^(\x2E]+\s+.+$|\s+[a-zA-Z]+$): letters of name,
1929
    but only matching if no '(' or '.' in second word of name,        ( \x2E = '.')
1930
    OR only one specific epithet \s+[\p{L}\x22\x2D\xD7]+$             (\x22= '"', \x2D='-', \xD7='×' )
1931
    */
1932
    $pattern = '/(^[a-zA-Z])([\p{L}]+)(\s+[^(\x2E]+\s+.+$|\s+[\p{L}\x22\x2D\xD7]+$)/u';
1933
    if (preg_match($pattern, $nameStr, $matches, PREG_OFFSET_CAPTURE)) {
1934
      return $matches[1][0] . "." . $matches[3][0];
1935
    }
1936
    else {
1937
      return $nameStr;
1938
    }
1939
  }
1940
  else {
1941
    return $name->titleCache;
1942
  }
1943
}
1944
1945
/**
1946
 * Check if a taxon is accepted by the current taxonomic tree.
1947
 *
1948
 * @param mixed $taxon
1949
 *   The Taxon obkect to check.
1950
 *
1951
 * @return bool
1952
 *   Returns TRUE if $taxon is accepted, FALSE otherwise.
1953
 */
1954
function _cdm_dataportal_acceptedByCurrentView($taxon) {
1955
1956 7663cd0b Andreas Kohlbecker
  $defaultTreeUuid = get_current_classification_uuid();
1957 6657531f Andreas Kohlbecker
1958
  if (isset($taxon->taxonNodes)) {
1959
    $taxonNodes = $taxon->taxonNodes;
1960
  }
1961
  else {
1962
    $taxonNodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, $taxon->uuid);
1963
  }
1964
1965
  if ($taxon->class == "Taxon" && isset($taxonNodes)) {
1966
    foreach ($taxonNodes as $node) {
1967 586fc780 Andreas Kohlbecker
      if (isset($node->classification)){
1968
        if(is_object($node->classification)) {
1969
          if ($node->classification->uuid == $defaultTreeUuid) {
1970
            return TRUE;
1971
          }
1972 6657531f Andreas Kohlbecker
        }
1973 586fc780 Andreas Kohlbecker
        else {
1974
          if ($node->classification == $defaultTreeUuid) {
1975
            return TRUE;
1976
          }
1977 6657531f Andreas Kohlbecker
        }
1978
      }
1979
    }
1980
  }
1981
1982
  return FALSE;
1983
}
1984
1985
/**
1986 54cfa718 Andreas Kohlbecker
 * Checks is the source has one of the given types.
1987
 *
1988
 * @param object $source
1989
 *   The original source entity
1990
 * @param array $types
1991
 *   An array of elementd of the OriginalSourceType enumeration
1992
 *   If not set the default will be used which is:
1993
 *    - Lineage
1994
 *    - PrimaryMediaSource
1995
 *    - PrimaryTaxonomicSource
1996
 *    - Unknown
1997
 *    - Other
1998
 * @return boolean
1999 6657531f Andreas Kohlbecker
 */
2000 54cfa718 Andreas Kohlbecker
  function _is_original_source_type($source, $types = null) {
2001
    // this is the default
2002
    // maybe this should also be put into the settings
2003
    static $default = array(
2004
      OriginalSourceType::Lineage,
2005
      OriginalSourceType::PrimaryMediaSource,
2006
      OriginalSourceType::PrimaryTaxonomicSource,
2007
      OriginalSourceType::Unknown,
2008
      OriginalSourceType::Other,
2009
    );
2010
2011
    if(!$types){
2012
      $types = $default;
2013
    }
2014
    return isset($source->type) && in_array($source->type, $types);
2015
  }
2016 6657531f Andreas Kohlbecker
2017
/**
2018
 * @todo Please document this function.
2019
 * @see http://drupal.org/node/1354
2020
 */
2021
function _is_invers_taxonRelationship($taxonRelationship, $focusedTaxon) {
2022
  return $taxonRelationship->toTaxon->uuid == $focusedTaxon->uuid;
2023
}
2024
2025
2026
/**
2027
 * Collects all the media from a list of description elements.
2028
 *
2029
 * @param array $descriptionElements
2030
 *   The description elements from which to collect the media.
2031
 *
2032
 * @return array
2033
 *   The output with all the collected media.
2034
 */
2035
function cdm_dataportal_media_from_descriptionElements($descriptionElements) {
2036
2037
  $outArrayOfMedia = array();
2038
2039
  // Avoiding a warning box in Drupal for Flora Malesiana.
2040
  if (isset($descriptionElements) && is_array($descriptionElements)) {
2041
    foreach ($descriptionElements as $descriptionElement) {
2042
      if (isset($descriptionElement->media) && is_array($descriptionElement->media)) {
2043
        foreach ($descriptionElement->media as $media) {
2044
          if (is_object($media)) {
2045
            $outArrayOfMedia[] = $media;
2046
          }
2047
        }
2048
      }
2049
    }
2050
  }
2051
  return $outArrayOfMedia;
2052
}
2053
2054
/**
2055
 * @todo Please document this function.
2056
 * @see http://drupal.org/node/1354
2057
 *
2058 65345976 Andreas Kohlbecker
 * @param array $cdm_entities
2059 6657531f Andreas Kohlbecker
 *   An array of CdmBase instances or a single instance.
2060 28c5c87a Andreas Kohlbecker
 * @param string $footnote_list_key_suggestion
2061 6657531f Andreas Kohlbecker
 *
2062
 * @return unknown
2063
 */
2064 65345976 Andreas Kohlbecker
function cdm_annotations_as_footnotekeys($cdm_entities, $footnote_list_key_suggestion = NULL) {
2065 90597e37 Andreas Kohlbecker
2066
   static $annotations_types_filter = null;
2067 6657531f Andreas Kohlbecker
   if(!$annotations_types_filter) {
2068 90597e37 Andreas Kohlbecker
     $annotations_types_filter = unserialize(ANNOTATIONS_TYPES_AS_FOOTNOTES_DEFAULT);
2069 6657531f Andreas Kohlbecker
   }
2070
2071
  $footNoteKeys = array();
2072
2073
  // Is argument cdmBase an array?
2074 65345976 Andreas Kohlbecker
  if (!is_array($cdm_entities)) {
2075 6657531f Andreas Kohlbecker
    $cdmBase_array = array();
2076 65345976 Andreas Kohlbecker
    $cdmBase_array[] = $cdm_entities;
2077 6657531f Andreas Kohlbecker
  }
2078
  else {
2079 65345976 Andreas Kohlbecker
    $cdmBase_array = $cdm_entities;
2080 6657531f Andreas Kohlbecker
  }
2081
2082
  // Getting the key for the footnotemanager.
2083 28c5c87a Andreas Kohlbecker
  if (isset($footnote_list_key_suggestion)) {
2084
    $footnote_list_key = $footnote_list_key_suggestion;
2085 6657531f Andreas Kohlbecker
  }
2086
  else {
2087 28c5c87a Andreas Kohlbecker
    $footnote_list_key = RenderHints::getFootnoteListKey() . '-annotations';
2088 6657531f Andreas Kohlbecker
  }
2089
2090
  // Adding the footnotes keys.
2091
  foreach ($cdmBase_array as $cdmBase_element) {
2092
    $annotations = cdm_ws_getAnnotationsFor($cdmBase_element, variable_get('annotations_types_as_footnotes', $annotations_types_filter));
2093
    if (is_array($annotations)) {
2094
      foreach ($annotations as $annotation) {
2095 28c5c87a Andreas Kohlbecker
        $footNoteKeys[] = FootnoteManager::addNewFootnote($footnote_list_key, $annotation->text);
2096 6657531f Andreas Kohlbecker
      }
2097
    }
2098
  }
2099
2100
  return $footNoteKeys;
2101
}
2102
2103 d9763fd3 Andreas Kohlbecker
2104 6657531f Andreas Kohlbecker
/**
2105 d9763fd3 Andreas Kohlbecker
 * Creates a CDM Dynabox.
2106
 *
2107 c1f5d897 Andreas Kohlbecker
 * @param string $dynabox_id
2108 d9763fd3 Andreas Kohlbecker
 *   a uninque name for tha dynabox, using a cdm entity uuid as id is good practice.
2109 6657531f Andreas Kohlbecker
 * @param string $label
2110
 *   The clickable text to show.
2111
 * @param string $content_url
2112
 *   The cdm REST service request url wich will deliver the content to be shown
2113
 *   once the dynabox toggles open.
2114
 * @param string $theme
2115 1ce9afb7 Patric Plitzner
 *   The theme to be used for rendering the cdm REST service response with is
2116 6657531f Andreas Kohlbecker
 *   returned from the $content_url.
2117
 * @param string $link_alt_text
2118
 *   The value for the alt attribute of the dynabox link.
2119
 * @param array $enclosingtags
2120
 *   An array with two elements: $enclosingtags[0] will be used for the dynabox
2121
 *   element itself, $enclosingtags[1] is the tag to be used for the
2122 c1f5d897 Andreas Kohlbecker
 *   dynabox_content (optional)
2123 d9763fd3 Andreas Kohlbecker
 * @param array $attributes
2124
 * @param $content_element_selector
2125
 *   Optional jQuery selector which can be used to reference a dom element which should
2126
 *   be used as container for the content to be shown. The dynabox-<dynabox id>-content
2127
 *  element will be placed in this container.
2128
 *
2129 c1f5d897 Andreas Kohlbecker
 * @param string $open_callback
2130 1ce9afb7 Patric Plitzner
 *   optional javascript call back function to be triggered after toggling the box to
2131 c1f5d897 Andreas Kohlbecker
 *   the open state.
2132
 * @param string $close_callback
2133 1ce9afb7 Patric Plitzner
 *   optional javascript call back function to be triggered after toggling the box to
2134 c1f5d897 Andreas Kohlbecker
 *   the closed state.
2135 d9763fd3 Andreas Kohlbecker
 * @return string Returns HTML for a dynabox.
2136
 * Returns HTML for a dynabox.
2137 6657531f Andreas Kohlbecker
 */
2138 d9763fd3 Andreas Kohlbecker
function cdm_dynabox($dynabox_id, $label, $content_url, $theme, $link_alt_text,
2139
                     $enclosingtags = array('li', 'ul'), $attributes = array(),
2140
                     $content_element_selector = null,
2141
                     $open_callback = 'function(){}', $close_callback = 'function(){}' ) {
2142 6657531f Andreas Kohlbecker
  $out = '';
2143 c1f5d897 Andreas Kohlbecker
2144
  // check for plain class attribute string
2145
  $dynabox_id = preg_replace('/[^a-zA-Z0-9\-]/', '', $dynabox_id);
2146
2147 9c9d11f7 Andreas Kohlbecker
  if(!array_key_exists('class', $attributes)) {
2148
    $attributes['class'] = array();
2149
  }
2150 d9763fd3 Andreas Kohlbecker
  $attributes['id'][] = 'dynabox-' . $dynabox_id;
2151 9c9d11f7 Andreas Kohlbecker
  $dynabox_attributes = drupal_attributes($attributes);
2152
2153
2154 b386ae48 Andreas Kohlbecker
  _add_js_domEvent(); // requires domEvent.js
2155 6657531f Andreas Kohlbecker
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cdm_dynabox.js');
2156 c1f5d897 Andreas Kohlbecker
  drupal_add_js("
2157 94bb8db6 Andreas Kohlbecker
  jQuery(document).ready(
2158
      function() {
2159
        dynabox('". $dynabox_id ."',
2160
          {
2161
            open_callback: " . $open_callback .",
2162 d9763fd3 Andreas Kohlbecker
            close_callback: " . $close_callback .
2163
            ($content_element_selector ? ",\n content_container_selector: '" . $content_element_selector . "'" : "") . "
2164 94bb8db6 Andreas Kohlbecker
          }
2165
        );
2166
      }
2167
   );",
2168
   array(
2169
    'type'=>'inline',
2170
    'scope'=>'footer'
2171
    )
2172
  );
2173 6657531f Andreas Kohlbecker
2174 9c9d11f7 Andreas Kohlbecker
2175 6657531f Andreas Kohlbecker
  $cdm_proxy_url = url('cdm_api/proxy/' . urlencode($content_url) . "/$theme");
2176 9c9d11f7 Andreas Kohlbecker
  $out .= '<!-- dynabox for ' . $content_url . ' -->';
2177 dcd1f0e0 Andreas Kohlbecker
  $out .= '<' . $enclosingtags[0] . ' ' .  $dynabox_attributes. '><a href="' . $cdm_proxy_url . '" class="label" alt="' . t($link_alt_text) . '">' . $label . '</a>';
2178 d9763fd3 Andreas Kohlbecker
  $out .= '  <' . $enclosingtags[1] . ' id="dynabox-' . $dynabox_id . '-content">';
2179
  $out .= '    <' . $enclosingtags[0] . ' class="dynabox-content-inner">' . loading_image_html() . '</' . $enclosingtags[0] . '>';
2180
  $out .= '    </' . $enclosingtags[1] . '>';
2181
  $out .= '  </' . $enclosingtags[0] . '>';
2182 9c9d11f7 Andreas Kohlbecker
  $out .= '<!-- dynabox end -->';
2183 6657531f Andreas Kohlbecker
  return $out;
2184
}
2185
2186
/**
2187
 * Checks whether a feature has any description elements.
2188
 *
2189
 * @param mixed $featureNode
2190
 *   A feature node as produced by the function _mergeFeatureTreeDescriptions().
2191
 *
2192
 * @see _mergeFeatureTreeDescriptions()
2193
 *
2194
 * @return bool
2195
 *   Returns TRUE if the given $featureNode or any of its subordinate nodes
2196
 *   contains at least one non empty TextData or at least one DescriptionElement
2197
 *   of an other type. A TextData element holding a multilanguageText or a
2198
 *   source reference is considered to be not empty.
2199 159cd9bb Andreas Kohlbecker
 *
2200
 * @TODO this function may have become obsolete by the new method of detecting empty blocks,
2201 aa26f9f0 Andreas Kohlbecker
 *       see $block_content_is_not_empty in compose_feature_blocks() and
2202 0ebb6efc Andreas Kohlbecker
 *       $feature_block_has_content in compose_feature_block_items_generic
2203 6657531f Andreas Kohlbecker
 */
2204 f19f47fa Andreas Kohlbecker
function has_feature_node_description_elements($featureNode) {
2205 6657531f Andreas Kohlbecker
2206
  if (isset($featureNode->descriptionElements) && is_array($featureNode->descriptionElements) && count($featureNode->descriptionElements) > 0) {
2207 092744e1 Andreas Kohlbecker
    if(!isset($featureNode->descriptionElements['#type'])){ // #type is used to identify e.g. DTO elements: '#type' => 'DTO'
2208
      foreach ($featureNode->descriptionElements as $descriptionElement) {
2209 159cd9bb Andreas Kohlbecker
        if ($descriptionElement->class != "TextData" || isset($descriptionElement->multilanguageText_L10n->text)
2210
          && $descriptionElement->multilanguageText_L10n->text != ''
2211
          || isset($descriptionElement->sources[0])
2212
          || isset($descriptionElement->media[0]) ) {
2213 092744e1 Andreas Kohlbecker
          return TRUE;
2214
        }
2215 6657531f Andreas Kohlbecker
      }
2216
    }
2217
  }
2218 092744e1 Andreas Kohlbecker
  else if (isset($featureNode->childNodes) && is_array($featureNode->childNodes)) {
2219 fae36a2a Andreas Kohlbecker
    foreach ($featureNode->childNodes as $child) {
2220 f19f47fa Andreas Kohlbecker
      if (has_feature_node_description_elements($child)) {
2221 6657531f Andreas Kohlbecker
        return TRUE;
2222
      }
2223
    }
2224
  }
2225
  return FALSE;
2226
}
2227 f26245c8 Andreas Kohlbecker
2228
/**
2229
 * Checks if the current page is a valid taxon portal page and responds with HTTP status 404 (not found) otherwise
2230
 *
2231
 * @param $chapter
2232
 *   The taxon page chapter or part
2233
 */
2234
function cdm_check_valid_taxon_page($chapter){
2235
  static $taxon_tabs = null;
2236
2237
  cdm_check_valid_portal_page();
2238
2239
  if($taxon_tabs == null){
2240
    $taxon_tabs = array('all', 'description');
2241
    foreach(get_taxon_tabs_list() as $tab){
2242
      $taxon_tabs[] = strtolower($tab);
2243
    }
2244
  }
2245
2246
  if(!in_array($chapter, $taxon_tabs)){
2247
    // oops this is not a valid chapter name
2248
    http_send_status(404); // 404 = Not Found
2249
  }
2250
2251
}
2252
2253
/**
2254
 * Checks if the current page is a valid portal page and responds with HTTP status 404 (not found) otherwise
2255
 *
2256
 * @param $chapter
2257
 *   The taxon page chapter or part
2258
 */
2259
function cdm_check_valid_portal_page(){
2260 595de9f0 Andreas Kohlbecker
  $ends_with_file_suffix_pattern = '/\/[^\.\/]*[\.][^\.\/]*$/';
2261 f26245c8 Andreas Kohlbecker
  if(preg_match($ends_with_file_suffix_pattern, $_GET['q'])){
2262
    // oops this urls ends with a file_suffix and thus does not refer to a portal page
2263
    http_send_status(404); // 404 = Not Found
2264 595de9f0 Andreas Kohlbecker
    exit('HTTP 404');
2265 f26245c8 Andreas Kohlbecker
  }
2266
}
2267 b2b9e057 Andreas Kohlbecker
2268
/**
2269
 * Generates the diff of the texts and presents it in a HTML diff viewer.
2270
 *
2271
 * @param $text_a
2272
 * @param $text_b
2273
 * @return string
2274
 */
2275
function diff_viewer($text_a, $text_b) {
2276
2277
  static $diff_viewer_count = 0;
2278
2279
  $element_id = 'part_definitions_diff_' . $diff_viewer_count++;
2280
2281
  // http://code.stephenmorley.org/php/diff-implementation/
2282
  module_load_include('php', 'cdm_dataportal', 'lib/class.Diff');
2283
  drupal_add_css(drupal_get_path('module',
2284
      'cdm_dataportal') . '/css/diff.css');
2285 087bb473 Andreas Kohlbecker
  _add_jquery_ui();
2286 b2b9e057 Andreas Kohlbecker
  drupal_add_js(
2287
    'jQuery(document).ready( function(){
2288
        jQuery(\'#' . $element_id . '\').accordion({
2289
        collapsible: true,
2290
        active: false,
2291
        fillSpace: true,
2292
        }).children(\'div\').css({ \'height\': \'auto\' });
2293
        jQuery(\'#' . $element_id . ' table.diff\').prepend(\'<thead><tr><th>Default</th><th>User defined<th></th><tr></thead>\');
2294
     });'
2295
    , array(
2296
    'type' => 'inline',
2297
    'scope' => 'footer'
2298
  ));
2299
2300
  $diff = Diff::compare($text_a,
2301
    $text_b);
2302
  $diff_viewer_markup = '<div id="' . $element_id . '"><h3>View Diff</h3><div>'
2303
    . Diff::toTable($diff, '', '')
2304
    . '</div></div>';
2305
  return $diff_viewer_markup;
2306
}
2307