Project

General

Profile

Download (95.6 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 b7a20282 Andreas Kohlbecker
  module_load_include('inc', 'cdm_dataportal', 'includes/taxon');
28
  module_load_include('inc', 'cdm_dataportal', 'includes/references');
29 38b27a36 Andreas Kohlbecker
  module_load_include('inc', 'cdm_dataportal', 'includes/pages');
30
  module_load_include('inc', 'cdm_dataportal', 'includes/media');
31
  module_load_include('inc', 'cdm_dataportal', 'includes/maps');
32
  module_load_include('inc', 'cdm_dataportal', 'includes/occurrences');
33
  module_load_include('inc', 'cdm_dataportal', 'includes/descriptions');
34
  module_load_include('inc', 'cdm_dataportal', 'includes/pre-drupal8');
35
36
  module_load_include('inc', 'cdm_dataportal', 'theme/theme_registry');
37
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.common');
38
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.descriptions');
39
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.media');
40
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.occurrence');
41
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.page');
42
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.taxon');
43
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.name');
44
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.references');
45
46
  module_load_include('php', 'cdm_dataportal', 'classes/footnotemanager');
47
  module_load_include('php', 'cdm_dataportal', 'classes/footnote');
48
  module_load_include('php', 'cdm_dataportal', 'classes/footnotekey');
49
  module_load_include('php', 'cdm_dataportal', 'classes/renderhints');
50
51
52
  /* ============================ java script functions ============================= */
53
54
55
  /**
56
  * loads external java script files asynchronously.
57
  *
58
  * @param unknown_type $script_url
59
  */
60
  function drupal_add_js_async($script_url, $callback){
61
62
    drupal_add_js("
63
          jQuery(document).ready(function() {
64
            jQuery.ajax({
65
              url: '" . $script_url . "',
66
              dataType: 'script',
67
              cache: true, // otherwise will get fresh copy every page load
68
              success: function() {
69
                    " . $callback . "
70
              }
71
            });
72
          });"
73
    , 'inline');
74 6657531f Andreas Kohlbecker
  }
75
76 38b27a36 Andreas Kohlbecker
  /**
77
   */
78
  function drupal_add_js_rowToggle($tableId){
79 6657531f Andreas Kohlbecker
80 38b27a36 Andreas Kohlbecker
      drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/table_modification.js');
81
      drupal_add_js('jQuery(document).ready(function(){
82
          addRowToggle("' . $tableId . '");
83 6657531f Andreas Kohlbecker
      });
84 38b27a36 Andreas Kohlbecker
      ', array('type' => 'inline'));
85
  }
86 d2fd2a4c Andreas Kohlbecker
87 38b27a36 Andreas Kohlbecker
  /**
88
   * @param unknown_type $link_element_selector
89
   * @param unknown_type $progress_element_selector
90
   */
91
  function _add_js_cdm_ws_progressbar($link_element_selector, $progress_element_selector){
92
93
    $callback = "jQuery('" . $link_element_selector . "').cdm_ws_progress('" . $progress_element_selector . "');";
94
95
    drupal_add_js_async(variable_get('cdm_webservice_url', '').'js/cdm_ws_progress.js', $callback);
96
97
    //   drupal_add_js("
98
    //   	  if (Drupal.jsEnabled) {
99
    //         $(document).ready(function() {
100
    //       		$('" . $link_element_selector . "').cdm_ws_progress('" . $progress_element_selector . "');
101
    //         });
102
    //       }", 'inline');
103
    }
104
105
  /**
106
   * @todo Please document this function.
107
   * @see http://drupal.org/node/1354
108
   */
109
  function _add_js_treeselector() {
110
    // drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/treeselector.js');
111
    drupal_add_js("
112
        jQuery(document).ready(function() {
113
           jQuery('#cdm-taxonomictree-selector-form #edit-val').change(function () {
114
                jQuery('#cdm-taxonomictree-selector-form').submit();
115
            });
116
117
        });
118
      ",
119
      array(
120
        'type' => 'inline',
121
        'scope' => 'footer'
122
      )
123
    );
124 5ea8b301 Andreas Kohlbecker
  }
125
126 6b605d3b Andreas Kohlbecker
  function _add_js_resizable_element($selector, $y_axis_only) {
127
128
    _add_jquery_ui();
129
    $options = "";
130
    if($y_axis_only) {
131
      $options = "resize: function(event, ui) {
132
        ui.size.width = ui.originalSize.width;
133
        },
134
        handles: \"s\"";
135
136
    }
137
    drupal_add_js("
138
          jQuery(document).ready(function() {
139
             jQuery('" . $selector . "').resizable({". $options ."});
140
          });
141
        ",
142
      array(
143
        'type' => 'inline',
144
        'scope' => 'footer'
145
      )
146
    );
147
  }
148
149 38b27a36 Andreas Kohlbecker
  function _add_js_openlayers() {
150
151
    $openlayers = '/js/map/OpenLayers-2.13.1/OpenLayers.js';
152
    $proj4js = '/js/map/proj4js-1.1.0/proj4js-compressed.js';
153
154
    if(variable_get('cdm_js_devel_mode', FALSE)){
155
      // develooper mode libs
156
  //     $openlayers = '/js/map/OpenLayers-2.13.1/lib/OpenLayers.js';
157
      $openlayers = '/js/map/OpenLayers-2.13.1/OpenLayers.debug.js';
158
      $proj4js = '/js/map/proj4js-1.1.0/proj4js-combined.js';
159
    }
160
161
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . $openlayers,
162
      array(
163
        'type' => 'file',
164 4c21ffdb Andreas Kohlbecker
        'group' => JS_LIBRARY,
165
        'weight' => 0,
166 38b27a36 Andreas Kohlbecker
        'cache' => TRUE,
167
        'preprocess' => FALSE
168
      )
169
    );
170
171
    // see https://github.com/proj4js/proj4js
172
    // http://openlayers.org/dev/examples/using-proj4js.html
173
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . $proj4js,
174
      array(
175
        'type' => 'file',
176 4c21ffdb Andreas Kohlbecker
        'group' => JS_LIBRARY,
177
        'weight' => -1, // before open layers
178 38b27a36 Andreas Kohlbecker
        'cache' => TRUE,
179
      )
180
    );
181
182
    // configure the theme
183
    $openlayers_theme_path = drupal_get_path('module', 'cdm_dataportal') . '/js/map/OpenLayers-2.13.1/theme/default/';
184
    $openlayers_imp_path = drupal_get_path('module', 'cdm_dataportal') . '/js/map/img/dark/';
185
    drupal_add_js('OpenLayers.ImgPath="' . base_path() . $openlayers_imp_path . '";', array(
186
        'type' => 'inline',
187 4c21ffdb Andreas Kohlbecker
        'group' => JS_LIBRARY,
188
        'weight' => 1, // after openlayers
189 38b27a36 Andreas Kohlbecker
        'cache' => TRUE,
190
        'preprocess' => FALSE
191
      ));
192
193
    drupal_add_css($openlayers_theme_path . 'style.tidy.css',
194
      array(
195
        'type' => 'file',
196
        'cache' => TRUE,
197
        'preprocess' => FALSE
198
      )
199
    );
200 e3026d72 Andreas Kohlbecker
201 38b27a36 Andreas Kohlbecker
  }
202 e3026d72 Andreas Kohlbecker
203 38b27a36 Andreas Kohlbecker
  /**
204
   * @todo Please document this function.
205
   * @see http://drupal.org/node/1354
206
   */
207
  function _add_js_thickbox() {
208
    // ---- jQuery thickbox:
209
    /*
210
    * bug: compat-1.0.js && thickbox.js line 237 .trigger("unload") -> event is
211
    * not triggered because of problems with compat-1.0.js' see INSTALL.txt
212
    */
213
    // drupal_add_js(drupal_get_path('module',
214
    // 'cdm_dataportal').'/js/jquery.imagetool.min.js');
215
    //
216
    // Add a setting for the path to cdm_dataportal module, used to find the path
217
    // for the loading animation image in thickbox.
218
    drupal_add_js(array(
219
    'cdm_dataportal' => array(
220
    'cdm_dataportal_path' => base_path() . drupal_get_path('module', 'cdm_dataportal'),
221 10f6aa4b Andreas Kohlbecker
    )
222 38b27a36 Andreas Kohlbecker
    ),
223
    'setting'
224
        );
225
        drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/thickbox/thickbox.js');
226
        drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/js/thickbox/cdm_thickbox.css');
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_lightbox($galleryID) {
234
    /*
235
     * Important Notice: The jquery.lightbox-0.5.js has been modified in order to
236 fe19a89b Andreas Kohlbecker
     * allow using the "alt" attribute for captions instead of the "title"
237
     * attribute
238
     */
239 38b27a36 Andreas Kohlbecker
    $lightbox_base_path =  drupal_get_path('module', 'cdm_dataportal') . '/js/jquery-lightbox-0.5';
240
    $lightbox_image_path = base_path() . $lightbox_base_path . '/images/';
241
    drupal_add_js($lightbox_base_path . '/js/jquery.lightbox-0.5.js');
242
    drupal_add_css($lightbox_base_path . '/css/jquery.lightbox-0.5.css');
243
    drupal_add_js('jQuery(document).ready(function() {
244
        jQuery(\'#' . $galleryID . ' a.lightbox\').lightBox({
245
          fixedNavigation:  true,
246
          imageLoading:     \'' . $lightbox_image_path . 'lightbox-ico-loading.gif\',
247
          imageBtnPrev:     \'' . $lightbox_image_path . 'lightbox-btn-prev.gif\',
248
          imageBtnNext:     \'' . $lightbox_image_path . 'lightbox-btn-next.gif\',
249
          imageBtnClose:    \'' . $lightbox_image_path . 'lightbox-btn-close.gif\',
250
          imageBlank:       \'' . $lightbox_image_path . 'lightbox-blank.gif\',
251
          adjustToWindow: true
252
        });
253 94550ff9 Andreas Kohlbecker
      });
254 38b27a36 Andreas Kohlbecker
      ', array('type' => 'inline'));
255
  }
256 94550ff9 Andreas Kohlbecker
257 38b27a36 Andreas Kohlbecker
  /**
258
   * @todo Please document this function.
259
   * @see http://drupal.org/node/1354
260
   */
261
  function _add_js_footnotes() {
262
    _add_js_domEvent();
263
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/footnotes.js');
264
  }
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 d0d23caa Andreas Kohlbecker
/**
277
 * @todo Please document this function.
278
 * @see http://drupal.org/node/1354
279
 */
280
function _add_js_taxonomic_children($jquery_selector) {
281 b0adfd32 Andreas Kohlbecker
282
  global $base_url;
283
284
285
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/jquery.cdm.taxonomic_children.js');
286 faf4f771 Andreas Kohlbecker
  
287 d0d23caa Andreas Kohlbecker
  drupal_add_js('jQuery(document).ready(function() {
288 b0adfd32 Andreas Kohlbecker
        jQuery(\'' . $jquery_selector . '\').taxonomic_children({
289 09edee9a Andreas Kohlbecker
          // hoverClass: "fa-rotate-90",
290
          // activeClass: "fa-rotate-90",
291 b0adfd32 Andreas Kohlbecker
          classificationUuid: "' . get_current_classification_uuid() . '",
292 0af3ce28 Andreas Kohlbecker
          taxonUuid: "' . get_current_taxon_uuid() . '",
293 38da4e54 Andreas Kohlbecker
          cdmWebappBaseUri: "' . variable_get('cdm_webservice_url', '') . '",
294 b0adfd32 Andreas Kohlbecker
          proxyBaseUri: "' . $base_url . '",
295
          
296
        });
297 d0d23caa Andreas Kohlbecker
      });
298
      ', array('type' => 'inline'));
299
}
300
301 38b27a36 Andreas Kohlbecker
  /**
302
   * Adds the external javascript file for domEvent.js.
303
   *
304
   * @see drupal_add_js()
305
   */
306
  function _add_js_domEvent() {
307
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/domEvent.js');
308
  }
309 b386ae48 Andreas Kohlbecker
310 087bb473 Andreas Kohlbecker
  function _add_jquery_ui()
311
  {
312
    drupal_add_css(drupal_get_path('module',
313
        'cdm_dataportal') . '/js/jquery-ui-1.8.24/themes/base/jquery.ui.all.css');
314
    drupal_add_js(drupal_get_path('module',
315
        'cdm_dataportal') . '/js/jquery-ui-1.8.24/ui/jquery-ui.js',
316
      array(
317
        'type' => 'file',
318
        'weight' => JS_LIBRARY,
319
        'cache' => TRUE,
320
        'preprocess' => FALSE
321
      )
322
    );
323
  }
324
325 b18d70f6 Andreas Kohlbecker
  function _add_js_ui_accordion(){
326
    _add_jquery_ui();
327
    drupal_add_js('jQuery(document).ready(function() {
328
        jQuery( "#accordion" ).accordion();
329
      });',
330
      array('type' => 'inline')
331
    );
332
  }
333
334 38b27a36 Andreas Kohlbecker
  /**
335
   * Provides the markup for an font awesome icon.
336
   *
337
   * The icons is created in default size without any extra features.
338
   *
339
   * The available icons are listed here http://fontawesome.io/cheatsheet/
340 ef12d0c6 Andreas Kohlbecker
   * fontawesome icons have much more features than implemented here in this function,
341
   * for spinning icons, fixed width icons, rotation, etc please checkout the
342 38b27a36 Andreas Kohlbecker
   * examples at http://fontawesome.io/examples/
343
   *
344
   * @parameter $icon_name
345
   *  The name of the icon which starts with 'fa-'
346
   *
347
   * @return String
348
   *    the markup for the icon in an <i> tag
349
   *
350
   */
351 b0aa9a8f Andreas Kohlbecker
  function font_awesome_icon_markup($icon_name = NULL, $attributes = array()){
352 6d9071b7 Andreas Kohlbecker
    _add_font_awesome_font();
353 38b27a36 Andreas Kohlbecker
354
355 b0aa9a8f Andreas Kohlbecker
    if($icon_name){
356
      if(!isset($attributes['class'])){
357
        $attributes['class'] = array();
358
      }
359
      $attributes['class'][] = 'fa';
360
      $attributes['class'][] = $icon_name;
361
362
      return '<i ' . drupal_attributes($attributes) . '></i>';
363 38b27a36 Andreas Kohlbecker
    }
364
365 b0aa9a8f Andreas Kohlbecker
    return '';
366 38b27a36 Andreas Kohlbecker
  }
367 94550ff9 Andreas Kohlbecker
368 6d9071b7 Andreas Kohlbecker
/**
369
 * Adds the css  containing the font awesome icons to the html header.
370
 */
371
function _add_font_awesome_font()
372
{
373
//<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
374
375
  $font_awesome_css_uri = base_path() . drupal_get_path('module', 'cdm_dataportal') . '/fonts/font-awesome-4.6.3/css/font-awesome.min.css';
376
  //$font_awesome_css_uri="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css";
377
378
  drupal_add_html_head_link(
379
    array(
380
      'href' => $font_awesome_css_uri,
381
      'rel' => 'stylesheet'
382
    )
383
  );
384
}
385
386 38b27a36 Andreas Kohlbecker
387 6d9071b7 Andreas Kohlbecker
/* ====================== hook implementations ====================== */
388 38b27a36 Andreas Kohlbecker
  /**
389
   * Implements hook_permission().
390
   *
391
   * Valid permissions for this module.
392
   *
393
   * @return array
394 8385a9f2 Andreas Kohlbecker
   *   An array of valid permissions for the cdm_dataportal module.
395 38b27a36 Andreas Kohlbecker
   */
396
  function cdm_dataportal_permission() {
397
    return array(
398
      'administer cdm_dataportal' => array(
399 8385a9f2 Andreas Kohlbecker
        'title' => t('Administer CDM DataPortal settings'),
400
        'description' => t("Access the settings pages specific for the cdm_dataportal module"),
401 c6fe437b Andreas Kohlbecker
      ),
402
      'access cdm content' => array(
403
        'title' => t('Access CDM content'),
404
        'description' => t("Access content (taxa, names, specimens, etc.) served by the CDM web service."),
405
      ),
406 38b27a36 Andreas Kohlbecker
    );
407
  }
408 6657531f Andreas Kohlbecker
409
/**
410 f19f47fa Andreas Kohlbecker
 * Implements hook_menu().
411
 */
412 6657531f Andreas Kohlbecker
function cdm_dataportal_menu() {
413
  $items = array();
414
415
  // @see settings.php.
416
  cdm_dataportal_menu_admin($items);
417
  cdm_dataportal_menu_help($items);
418
419
  $items['cdm_dataportal/names'] = array(
420
    'page callback' => 'cdm_dataportal_view_names',
421 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
422 6657531f Andreas Kohlbecker
    'type' => MENU_CALLBACK,
423
  );
424
425
  // Optional callback arguments: page.
426
  $items['cdm_dataportal/taxon'] = array(
427
    'page callback' => 'cdm_dataportal_taxon_page_view',
428 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
429 6657531f Andreas Kohlbecker
    'type' => MENU_CALLBACK,
430
    // Expected callback arguments: uuid.
431
  );
432
433 30845bda Andreas Kohlbecker
  $items['cdm_dataportal/specimen'] = array(
434
      'page callback' => 'cdm_dataportal_specimen_page_view',
435
      'access arguments' => array('access cdm content'),
436
      'type' => MENU_CALLBACK,
437
      // Expected callback arguments: uuid.
438
  );
439
440
  $items['cdm_dataportal/named_area'] = array(
441
    'page callback' => 'cdm_dataportal_named_area_page_view',
442
    'access arguments' => array('access cdm content'),
443
    'type' => MENU_CALLBACK,
444
    // Expected callback arguments: uuid.
445
  );
446 9d37c9d9 Patric Plitzner
447 6657531f Andreas Kohlbecker
  $items['cdm_dataportal/name'] = array(
448
    'page callback' => 'cdm_dataportal_name_page_view',
449
      /*
450
    'page arguments' => array(
451
       'taxon_name_uuid',
452
       'taxon_to_hide_uuid',
453
       'synonym_uuid' => NULL
454
      ),
455
      */
456 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
457 6657531f Andreas Kohlbecker
    'type' => MENU_CALLBACK,
458
    // Expected callback arguments: uuid.
459
  );
460
461
  $items['cdm_dataportal/reference'] = array(
462
    'page callback' => 'cdm_dataportal_view_reference',
463 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
464 6657531f Andreas Kohlbecker
    'type' => MENU_CALLBACK,
465
    // Expected callback arguments: uuid.
466
  );
467
468
  $items['cdm_dataportal/reference/list'] = array(
469
    'page callback' => 'cdm_dataportal_view_reference_list',
470 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
471 6657531f Andreas Kohlbecker
    'type' => MENU_CALLBACK,
472
    // Expected callback arguments: uuid.
473
  );
474
475
  $items['cdm_dataportal/media'] = array(
476
    'page callback' => 'cdm_dataportal_view_media',
477 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
478 6657531f Andreas Kohlbecker
    'type' => MENU_CALLBACK,
479
    // Expected callback arguments:
480
    // uuid, mediarepresentation_uuid, part_uuid or part#.
481
  );
482
483
  $items['cdm_dataportal/polytomousKey'] = array(
484
    'page callback' => 'cdm_dataportal_view_polytomousKey',
485 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
486 6657531f Andreas Kohlbecker
    'type' => MENU_CALLBACK,
487
    // Expected callback arguments: polytomousKey->uuid.
488
  );
489
490
  $items['cdm_dataportal/search'] = array(
491
    'page callback' => 'cdm_dataportal_view_search_advanced',
492 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
493 6657531f Andreas Kohlbecker
    'type' => MENU_CALLBACK,
494
  );
495
496
  $items['cdm_dataportal/search/advanced'] = array(
497 29077ee5 Andreas Kohlbecker
    'title' => 'Advanced', // will be passed through t()
498 6657531f Andreas Kohlbecker
    'page callback' => 'cdm_dataportal_view_search_advanced',
499 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
500 6657531f Andreas Kohlbecker
    'type' => MENU_DEFAULT_LOCAL_TASK,
501
  );
502
503
  $items['cdm_dataportal/search/taxon_by_description'] = array(
504 579c485d Andreas Kohlbecker
    'title' => 'By content category', // will be passed through t()
505 6657531f Andreas Kohlbecker
    'page callback' => 'cdm_dataportal_view_search_taxon_by_description',
506 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
507 6657531f Andreas Kohlbecker
    'type' => MENU_LOCAL_TASK,
508
  );
509
  $items['cdm_dataportal/search/results/taxon'] = array(
510
    'page callback' => 'cdm_dataportal_view_search_results_taxon',
511 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
512 6657531f Andreas Kohlbecker
    'type' => MENU_CALLBACK,
513
  );
514 efbff4b3 Andreas Kohlbecker
  /*
515
   * MENU_CALLBACK at cdm_dataportal/registration-search is needed to make the
516
   * tabs in the subordinate paths work, accessing this 'page' will cause the
517
   * MENU_DEFAULT_LOCAL_TASK being displayed
518
   */
519
  $items['cdm_dataportal/registration-search'] = array(
520
    'title' => 'Search', // will be passed through t()
521 a6ae799b Andreas Kohlbecker
    'page callback' => 'cdm_dataportal_view_search_registrations_results',
522 efbff4b3 Andreas Kohlbecker
    'page arguments' => array("filter"),
523 a6ae799b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
524
    'type' => MENU_CALLBACK,
525
  );
526 efbff4b3 Andreas Kohlbecker
  /*
527
   * the MENU_DEFAULT_LOCAL_TASK creates a tab for the MENU_CALLBACK
528
   * defined at a higher level of the path (cdm_dataportal/registration-search)
529
   */
530
  $items['cdm_dataportal/registration-search/filter'] = array(
531
    'title' => 'Search', // will be passed through t()
532
    'page callback' => 'cdm_dataportal_view_search_registrations_results',
533
    'page arguments' => array("filter"),
534
    'access arguments' => array('access cdm content'),
535
    'type' => MENU_DEFAULT_LOCAL_TASK,
536
  );
537
  /*
538
   * the MENU_LOCAL_TASK creates another tab
539
   */
540
  $items['cdm_dataportal/registration-search/taxongraph'] = array(
541
    'title' => 'Taxonomic search', // will be passed through t()
542
    'page callback' => 'cdm_dataportal_view_search_registrations_results',
543
    'page arguments' => array("taxongraph"),
544
    'access arguments' => array('access cdm content'),
545
    'type' => MENU_LOCAL_TASK,
546
  );
547
548 e0d3e148 Andreas Kohlbecker
  // Optional callback arguments: page.
549
  $items['cdm_dataportal/registration'] = array(
550
    'page callback' => 'cdm_dataportal_registration_page_view',
551
    'access arguments' => array('access cdm content'),
552
    'type' => MENU_CALLBACK,
553
    // Expected callback arguments: uuid.
554
  );
555 a6ae799b Andreas Kohlbecker
556 6657531f Andreas Kohlbecker
  /*
557
   $items['cdm/xml2json'] = array(
558
   'page callback' => 'cdm_view_xml2json',
559 c6fe437b Andreas Kohlbecker
   'access arguments' => array('access cdm content'),
560 6657531f Andreas Kohlbecker
   'type' => MENU_CALLBACK,
561
   );
562
   */
563
564
  // if (arg(0)=='user' && ($uid=arg(1)) && is_numeric($uid)) {
565
  // User configuration of cdm_dataportal.
566
  $items['user/%/cdm_dataportal'] = array(
567
    'title' => 'cdm_dataportal',
568 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
569 6657531f Andreas Kohlbecker
    'page callback' => 'drupal_get_form',
570
    'page arguments' => array('cdm_dataportal_user_form'),
571
    'type' => MENU_LOCAL_TASK,
572
    'weight' => 10,
573
  );
574
  // }
575
576
  // 'May not cache' in D5.
577
  $items['cdm_dataportal/name/%'] = array(
578
    // 'page callback' => 'cdm_dataportal_view_name',
579
    'page callback' => 'cdm_dataportal_name_page_view',
580
    'page arguments' => array(2, 3, 4),
581 c6fe437b Andreas Kohlbecker
    'access arguments' => array('access cdm content'),
582 6657531f Andreas Kohlbecker
    'type' => MENU_CALLBACK,
583
  );
584
585 6f1a217c Andreas Kohlbecker
  // --- Local tasks for Taxon.
586
  // --- tabbed taxon page
587 6657531f Andreas Kohlbecker
  if (variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
588
    $items['cdm_dataportal/taxon/%'] = array(
589 4feeabc7 Andreas Kohlbecker
      'title' => cdm_taxonpage_tab_label('General'),
590 6657531f Andreas Kohlbecker
      'page callback' => 'cdm_dataportal_taxon_page_view',
591 c6fe437b Andreas Kohlbecker
      'access arguments' => array('access cdm content'),
592 6657531f Andreas Kohlbecker
      'type' => MENU_CALLBACK,
593
      'weight' => 1,
594
      'page arguments' => array(2, "description")
595 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
596 6657531f Andreas Kohlbecker
    );
597
598
    $items['cdm_dataportal/taxon/%/all'] = array(
599 4feeabc7 Andreas Kohlbecker
      'title' => cdm_taxonpage_tab_label('General'),
600 6657531f Andreas Kohlbecker
      'page callback' => 'cdm_dataportal_taxon_page_view',
601 c6fe437b Andreas Kohlbecker
      'access arguments' => array('access cdm content'),
602 6657531f Andreas Kohlbecker
      'type' => MENU_CALLBACK,
603
      'weight' => 2,
604
      'page arguments' => array(2, "all")
605 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
606 6657531f Andreas Kohlbecker
    );
607
608
    $items['cdm_dataportal/taxon/%/description'] = array(
609 4feeabc7 Andreas Kohlbecker
      'title' => cdm_taxonpage_tab_label('General'),
610 6657531f Andreas Kohlbecker
      'page callback' => 'cdm_dataportal_taxon_page_view',
611 c6fe437b Andreas Kohlbecker
      'access arguments' => array('access cdm content'),
612 6657531f Andreas Kohlbecker
      'type' => MENU_DEFAULT_LOCAL_TASK,
613
      'weight' => 2,
614
      'page arguments' => array(2, "description")
615 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
616 6657531f Andreas Kohlbecker
    );
617
618
    $items['cdm_dataportal/taxon/%/synonymy'] = array(
619 4feeabc7 Andreas Kohlbecker
      'title' => cdm_taxonpage_tab_label('Synonymy'),
620 6657531f Andreas Kohlbecker
      'page callback' => 'cdm_dataportal_taxon_page_view',
621 c6fe437b Andreas Kohlbecker
      'access arguments' => array('access cdm content'),
622 6657531f Andreas Kohlbecker
      'type' => MENU_LOCAL_TASK,
623
      'weight' => 4,
624
      'page arguments' => array(2, "synonymy", 4)
625 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid and ...
626 6657531f Andreas Kohlbecker
    );
627 7cc085da Andreas Kohlbecker
    $items['cdm_dataportal/taxon/%/images'] = array( // Images
628 4feeabc7 Andreas Kohlbecker
      'title' => cdm_taxonpage_tab_label('Images'),
629 6657531f Andreas Kohlbecker
      'page callback' => 'cdm_dataportal_taxon_page_view',
630 c6fe437b Andreas Kohlbecker
      'access arguments' => array('access cdm content'),
631 6657531f Andreas Kohlbecker
      'type' => MENU_LOCAL_TASK,
632
      'weight' => 5,
633
      'page arguments' => array(2, "images")
634 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
635 6657531f Andreas Kohlbecker
    );
636
637 7cc085da Andreas Kohlbecker
    $items['cdm_dataportal/taxon/%/specimens'] = array( // Specimens
638 4feeabc7 Andreas Kohlbecker
      'title' => cdm_taxonpage_tab_label('Specimens'),
639 6657531f Andreas Kohlbecker
      'page callback' => 'cdm_dataportal_taxon_page_view',
640 c6fe437b Andreas Kohlbecker
      'access arguments' => array('access cdm content'),
641 6657531f Andreas Kohlbecker
      'type' => MENU_LOCAL_TASK,
642
      'weight' => 6,
643
      'page arguments' => array(2, "specimens")
644 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
645 6657531f Andreas Kohlbecker
    );
646
647 7cc085da Andreas Kohlbecker
    $items['cdm_dataportal/taxon/%/keys'] = array( // Keys
648 4feeabc7 Andreas Kohlbecker
      'title' => cdm_taxonpage_tab_label('Keys'),
649 6657531f Andreas Kohlbecker
      'page callback' => 'cdm_dataportal_taxon_page_view',
650 c6fe437b Andreas Kohlbecker
      'access arguments' => array('access cdm content'),
651 6657531f Andreas Kohlbecker
      'type' => MENU_LOCAL_TASK,
652
      'weight' => 6,
653
      'page arguments' => array(2, "keys")
654 7663cd0b Andreas Kohlbecker
      , // Expected callback arguments: taxon_uuid.
655
    );
656
657 7cc085da Andreas Kohlbecker
    $items['cdm_dataportal/taxon/%/experts'] = array( // Experts
658 4feeabc7 Andreas Kohlbecker
      'title' => cdm_taxonpage_tab_label('Experts'),
659 7663cd0b Andreas Kohlbecker
        'page callback' => 'cdm_dataportal_taxon_page_view',
660 c6fe437b Andreas Kohlbecker
        'access arguments' => array('access cdm content'),
661 7663cd0b Andreas Kohlbecker
        'type' => MENU_LOCAL_TASK,
662
        'weight' => 6,
663
        'page arguments' => array(2, "experts")
664
    , // Expected callback arguments: taxon_uuid.
665 6657531f Andreas Kohlbecker
    );
666 93ae67cc Patrick Plitzner
667 51cc509a Patrick Plitzner
    $items['cdm_dataportal/taxon/autosuggest/%/%/%/'] = array(
668 04a1d647 Patrick Plitzner
        'page callback' => 'cdm_dataportal_taxon_autosuggest',
669 c6fe437b Andreas Kohlbecker
        'access arguments' => array('access cdm content'),
670 51cc509a Patrick Plitzner
        'page arguments' => array(3,4,5),
671 93ae67cc Patrick Plitzner
        'type' => MENU_CALLBACK
672
    );
673 6657531f Andreas Kohlbecker
  }
674
675 6f1a217c Andreas Kohlbecker
  // --- refresh link for all cdmnode types
676
  foreach (cdm_get_nodetypes() as $type=>$name) {
677
    $items['cdm_dataportal/' . $name . '/%/refresh'] = array(
678 7cc085da Andreas Kohlbecker
        'title' => 'Refresh',
679 6f1a217c Andreas Kohlbecker
        'page callback' => 'cdm_dataportal_refresh_node',
680 3113db26 Andreas Kohlbecker
        'access arguments' => array('administer cdm_dataportal'),
681 6f1a217c Andreas Kohlbecker
        'type' => MENU_LOCAL_TASK,
682
        'weight' => 100,
683
        'page arguments' => array($name, 2)
684
    );
685
  }
686
687 6657531f Andreas Kohlbecker
  return $items;
688
}
689
690
/**
691 b85ab055 Andreas Kohlbecker
 * Implements hook_init().
692
 *
693 6657531f Andreas Kohlbecker
 */
694
function cdm_dataportal_init() {
695 8fc089e1 Saskia Gennrich
  if (!path_is_admin(current_path())) {
696
    //FIXME To add CSS or JS that should be present on all pages, modules
697
    //      should not implement this hook, but declare these files in their .info file.
698
    drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal.css');
699
    // drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
700
    drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal_screen.css', array('type' => 'screen'));
701 63f6d312 Saskia Gennrich
  } else {
702
    drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal_settings.css');
703 8fc089e1 Saskia Gennrich
  }
704 2dd59bb5 Andreas Kohlbecker
705
  if(variable_get('cdm_debug_mode', FALSE)){
706
    $file = 'temporary://drupal_debug.txt';
707
    file_put_contents($file, 'CDM DEBUG LOG for ' . $_GET['q']. "\n"); // will overwrite the file
708
  }
709 5611d467 Andreas Kohlbecker
710
  $bibliography_settings = get_bibliography_settings();
711 57d513cb Andreas Kohlbecker
  $enclosing_tag = $bibliography_settings['enabled'] == 1 ? 'div' : 'span';
712
  FootnoteManager::registerFootnoteSet('BIBLIOGRAPHY', $enclosing_tag, $bibliography_settings['key_format']);
713 6657531f Andreas Kohlbecker
}
714
715 6f1a217c Andreas Kohlbecker
function cdm_dataportal_refresh_node($cdm_node_name, $uuid, $parameters = array()){
716
717
  $base_path = 'cdm_dataportal/' . $cdm_node_name . '/' . $uuid;
718
719
  if($cdm_node_name == 'taxon' && variable_get('cdm_dataportal_taxonpage_tabs', 1)){
720
    // force reloading of all and notify user about this special loading
721
    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: ')
722
        . l('Back to tabbed taxon page', $base_path));
723
    $base_path .= '/all';
724
  } else {
725
    drupal_set_message(t('The level 2 cache has been cleared for this page'));
726
  }
727
728
  $parameters['cacheL2_refresh'] ='1';
729
730
731
  drupal_goto($base_path, array('query' => $parameters));
732
}
733
734 102f8c26 Andreas Kohlbecker
/**
735
 * Implements hook_requirements($phase)
736
 */
737
function cdm_dataportal_requirements($phase) {
738
    $requirements = array();
739
    if($phase == 'runtime'){
740
741
    }
742
    return $requirements;
743
}
744
745
746 6657531f Andreas Kohlbecker
/**
747
 * The function generate form for own user cdm dataportal configurations.
748
 */
749
function cdm_dataportal_user_form($form, &$form_state) {
750 36818236 Andreas Kohlbecker
751 6657531f Andreas Kohlbecker
  global $user;
752
  $checkbox_value = 'cdm_dataportal_' . $user->uid . '_default_tab_active';
753
754 8e1dc7ec Andreas Kohlbecker
  $form['taxon_page_tabs'] = array(
755
      '#type' => 'fieldset',
756 36818236 Andreas Kohlbecker
      '#tree' => true,
757 8e1dc7ec Andreas Kohlbecker
      '#title' => t('Taxon page tabs'),
758
  );
759 36818236 Andreas Kohlbecker
760
  $form['taxon_page_tabs']['user_defined'] = array(
761 6657531f Andreas Kohlbecker
    '#type' => 'checkbox',
762
    '#title' => t('Activate user default configuration'),
763
    '#default_value' => variable_get($checkbox_value, 0),
764
    '#description' => t('Check this if you want configure your own default tab from the below menu.'),
765
  );
766
767 36818236 Andreas Kohlbecker
  $form['taxon_page_tabs']['default_tab'] = array(
768 6657531f Andreas Kohlbecker
    '#type' => 'select',
769
    '#title' => t('Default tab to display'),
770
    '#default_value' => get_default_taxon_tab(TRUE),
771
    '#options' => unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB),
772
    '#description' => t('<p>Select the default tab to display when visiting a taxon page. Only available if Tabbed Taxon Page is enable.</p>
773
              <strong>Note:</strong> After performing a search and clicking in any synonym, the taxon tab
774 36818236 Andreas Kohlbecker
              to be rendered will be the synonymy of the accepted taxon and not the above selected tab.'),
775
  );
776
777
778 f695daf4 Andreas Kohlbecker
  if(false){
779
    $form['developer_options'] = array(
780
        '#type' => 'fieldset',
781
        '#tree' => true,
782
        '#title' => t('Developer options'),
783
    );
784
785
    $form['developer_options']['show_render_path'] = array(
786
      '#type' => 'checkbox',
787
      '#title' => t('Display the render path for each taxon name.'),
788
      '#default_value' => variable_get($checkbox_value, 0),
789
      '#description' => t('This option is very helpful if you are editing the !link for taxon names.',
790
        array(
791
            '!link' => l(
792
              'render template', 'admin/config/cdm_dataportal/settings/layout', array('fragment' => 'edit-cdm-name-render-templates'))
793
            )
794
        ),
795
    );
796
  }
797 6657531f Andreas Kohlbecker
798
  $form['submit'] = array(
799
    '#type' => 'submit',
800
    '#value' => t('Submit'),
801
  );
802
803
  return $form;
804
}
805
806
/**
807
 * Form submission handler for user_form().
808
 *
809
 * Submits the user cdm dataportal configurations.
810
 */
811
function cdm_dataportal_user_form_submit($form, &$form_state) {
812
  global $user;
813
  $msg_type = 'status';
814
  $username = $user->name;
815
  $variable_to_use = 'cdm_dataportal_' . $user->uid . '_default_tab';
816
817 36818236 Andreas Kohlbecker
  // FIXME: this is completely wrong, see user_profile_form_submit()
818
819
  // it is only possible to change own user settings
820
  if (arg(0) == 'user' && is_numeric(arg(1)) && $user->uid == arg(1)) {
821
822
    // DEFAULT_TAXON_TAB
823 6657531f Andreas Kohlbecker
    $variable = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
824 36818236 Andreas Kohlbecker
    variable_set($variable_to_use . '_active', $form_state['values']['taxon_page_tabs']['user_defined']);
825
    variable_set($variable_to_use, $form_state['values']['taxon_page_tabs']['default_tab']);
826
    if ($form_state['values']['taxon_page_tabs']['user_defined']) {
827 6657531f Andreas Kohlbecker
      drupal_set_message(check_plain(t('The user default tab will be used for the next taxon site visit.')));
828
      drupal_set_message(check_plain(t('The user default tab has been changed to: !tab for the user !user', array(
829
        '!tab' => $variable[variable_get($variable_to_use, 0)],
830
        '!user' => $username,
831
      ))), $msg_type);
832
    }
833
    else {
834
      drupal_set_message(check_plain(t('The user default tab wont be used for
835
        the next taxon site, check the box if you want to use the user default configuration.')));
836
    }
837 36818236 Andreas Kohlbecker
838 6657531f Andreas Kohlbecker
  }
839
  else {
840 36818236 Andreas Kohlbecker
    // Problem with the user id => variables wont be saved.
841 6657531f Andreas Kohlbecker
    $msg_type = 'warning';
842
    drupal_set_message(check_plain(t('Default tab has not been saved due to user id problems')), $msg_type);
843
  }
844
}
845
846
/**
847
 * Implements hook_block_info().
848
 */
849
function cdm_dataportal_block_info() {
850
851
    // $block[0]["info"] = t("CDM DataPortal DevLinks");
852
    // $block[1]["info"] = t("CDM DataPortal Credits");
853 fc13aa30 Andreas Kohlbecker
    $block["2"] = array(
854 beb914ef Andreas Kohlbecker
        "info" => t("CDM - Search Taxa"),
855 fc13aa30 Andreas Kohlbecker
        "cache" => DRUPAL_NO_CACHE
856
      );
857 6657531f Andreas Kohlbecker
    // $block[3]["info"] = t("CDM Filters");
858 beb914ef Andreas Kohlbecker
    $block["4"]["info"] = t("CDM  - Dataportal Print");
859
    $block["keys"]["info"] = t("CDM - Identification keys");
860 46a02272 Andreas Kohlbecker
    $block["fundedByEDIT"]["info"] = t('CDM - Powered by EDIT');
861 f9a3d0f6 Andreas Kohlbecker
    $block["classification_breadcrumbs"] =  array(
862 beb914ef Andreas Kohlbecker
        'info' => t('CDM - Classification breadcrumbs'),
863 f9a3d0f6 Andreas Kohlbecker
        'cache' => DRUPAL_CACHE_PER_PAGE
864
      );
865 bc400a17 Andreas Kohlbecker
    $block["taxonomic_children"] =  array(
866 beb914ef Andreas Kohlbecker
      'info' => t('CDM - Taxonomic children'),
867 bc400a17 Andreas Kohlbecker
      'cache' => DRUPAL_CACHE_PER_PAGE
868
    );
869 beb914ef Andreas Kohlbecker
    $block["back_to_search_results"] =  array(
870
      'title' => '<none>',
871
      'info' => t('CDM - Back to search Results'),
872
      'cache' => DRUPAL_CACHE_PER_PAGE,
873
      'visibility' => BLOCK_VISIBILITY_LISTED,
874
      'pages' => "cdm_dataportal/taxon/*", // multiple page paths separated by "\n"!!!
875
    );
876 a6ae799b Andreas Kohlbecker
  $block['registrations_search_filter'] =  array(
877 b18d70f6 Andreas Kohlbecker
    'title' => 'Filter registrations',
878 a6ae799b Andreas Kohlbecker
    'info' => t('CDM - Registrations search filter'),
879
    'cache' => DRUPAL_CACHE_PER_PAGE,
880 810710a0 Andreas Kohlbecker
    'visibility' => BLOCK_VISIBILITY_NOTLISTED,
881 b18d70f6 Andreas Kohlbecker
    'pages' => "cdm_dataportal/registration-search\ncdm_dataportal/registration-search/*", // multiple page paths separated by "\n"!!!
882 a6ae799b Andreas Kohlbecker
  );
883 efbff4b3 Andreas Kohlbecker
  $block['registrations_search_taxongraph'] =  array(
884
    'title' => 'Taxonomic registration search',
885
    'info' => t('CDM - Registrations search by taxon graph'),
886
    'cache' => DRUPAL_CACHE_PER_PAGE,
887
    'visibility' => BLOCK_VISIBILITY_NOTLISTED,
888 b18d70f6 Andreas Kohlbecker
    'pages' => "cdm_dataportal/registration-search\ncdm_dataportal/registration-search/*", // multiple page paths separated by "\n"!!!
889
  );
890
  $block['registrations_search'] =  array(
891
    'title' => 'Search',
892
    'info' => t('CDM - Registrations search combining filter and taxon graph search' ),
893
    'cache' => DRUPAL_CACHE_PER_PAGE,
894
    'visibility' => BLOCK_VISIBILITY_NOTLISTED,
895
    'pages' => "cdm_dataportal/registration-search\ncdm_dataportal/registration-search/*", // multiple page paths separated by "\n"!!!
896 efbff4b3 Andreas Kohlbecker
  );
897
  return $block;
898 6657531f Andreas Kohlbecker
}
899
900
/**
901
 * Implements hook_block_view().
902
 */
903
function cdm_dataportal_block_view($delta) {
904
  // TODO Rename block deltas (e.g. '2') to readable strings.
905
  switch ($delta) {
906
    // case 'delta-1':
907
    // $block['subject'] = t('Credits');
908
    // $block['content'] = theme('cdm_credits');
909
    // return $block;
910
    case '2':
911
      $block['subject'] = t('Search taxa');
912 191f88fd Andreas Kohlbecker
      $form = drupal_get_form('cdm_dataportal_search_taxon_form');
913
      $block['content'] = drupal_render($form);
914 6657531f Andreas Kohlbecker
915
      if (variable_get('cdm_dataportal_show_advanced_search', 1)) {
916
        $block['content'] .= '<div>' . l(t('Advanced Search'), 'cdm_dataportal/search') . '</div>';
917
      }
918
      return $block;
919
    case '4':
920 c8234bdf Andreas Kohlbecker
      $block['subject'] = '';
921 6657531f Andreas Kohlbecker
      $block['content'] = theme('cdm_print_button');
922
      return $block;
923
    case "keys":
924
      $block['subject'] = t('Identification Keys');
925
      $block['content'] = theme('cdm_block_IdentificationKeys', array('taxonUuid' => NULL));
926
      return $block;
927
    case "fundedByEDIT":
928
      // t('Funded by EDIT');
929
      $text = '<none>';
930
      $block['subject'] = $text;
931 889c74b7 Andreas Kohlbecker
      $img_tag = '<img src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/powered_by_edit.png' . '" alt="' . $text . '"/>';
932
      $block['content'] = l($img_tag, "http://cybertaxonomy.org/", array(
933 6657531f Andreas Kohlbecker
        'attributes' => array("target" => "EDIT"),
934
        'absolute' => TRUE,
935
        'html' => TRUE,
936
      ));
937
      return $block;
938 f9a3d0f6 Andreas Kohlbecker
    case 'classification_breadcrumbs':
939 0af3ce28 Andreas Kohlbecker
      $taxon_uuid = get_current_taxon_uuid();
940 38da4e54 Andreas Kohlbecker
      $block['subject'] = '<none>';
941
      $block['content'] = compose_classification_breadcrumbs($taxon_uuid);
942
      return $block;
943 bc400a17 Andreas Kohlbecker
    case 'taxonomic_children':
944
      $taxon_uuid = get_current_taxon_uuid();
945
      $block['subject'] = '<none>';
946
      $block['content'] = compose_taxonomic_children($taxon_uuid);
947
      return $block;
948 beb914ef Andreas Kohlbecker
    case 'back_to_search_results':
949
      $block['subject'] = '<none>';
950
      if (isset($_SESSION['cdm']['search'])) {
951
        $block['content'] = l(t('Back to search result'), "http://" . $_SERVER['SERVER_NAME'] . $_SESSION['cdm']['last_search']);
952
      }
953
      return $block;
954 a6ae799b Andreas Kohlbecker
    case 'registrations_search_filter':
955
      $block['subject'] = '<none>';
956 efbff4b3 Andreas Kohlbecker
      $block['content'] = drupal_get_form('cdm_dataportal_search_registration_filter_form'); // see cdm_dataportal_search_registration_filter_form($form, &$form_state)
957
      return $block;
958
    case 'registrations_search_taxongraph':
959
      $block['subject'] = '<none>';
960
      $block['content'] = drupal_get_form('cdm_dataportal_search_registration_taxongraph_form'); // see cdm_dataportal_search_registration_taxongraph_form($form, &$form_state)
961 a6ae799b Andreas Kohlbecker
      return $block;
962 b18d70f6 Andreas Kohlbecker
    case 'registrations_search':
963
      _add_js_ui_accordion();
964
      _add_font_awesome_font();
965
      $block['subject'] = '<none>';
966
      $filter_form = drupal_get_form('cdm_dataportal_search_registration_filter_form');
967
      $filter_form['#prefix'] = '<div>';
968
      $filter_form['#suffix'] = '</div>';
969
      $taxongraph_form = drupal_get_form('cdm_dataportal_search_registration_taxongraph_form');
970
      $taxongraph_form['#prefix'] = '<div>';
971
      $taxongraph_form['#suffix'] = '</div>';
972
      $block['content'] = array(
973
        'accordion' => array(
974
          '#markup' => '',
975
          '#prefix' => '<div id="accordion">',
976
          '#suffix' => '</div>',
977
          'content' => array(
978
            array('#markup' => '<h3>Filter</h3>'),
979
            $filter_form,
980
            array('#markup' => '<h3>Taxon graph</h3>'),
981
            $taxongraph_form,
982
          )
983
        )
984
    );
985
       return $block;
986 beb914ef Andreas Kohlbecker
    default:
987
      return null;
988 6657531f Andreas Kohlbecker
  }
989
}
990
991 0af3ce28 Andreas Kohlbecker
/**
992
 * Provides the uuid of the taxon for pages with the path ./taxon/{taxon_uuid}
993
 *
994
 * @return string
995
 *   the taxon uuid or NULL
996
 */
997
function get_current_taxon_uuid()
998
{
999
  static $taxon_uuid;
1000
1001
  if(!isset($taxon_uuid)){
1002
    if(isset($_REQUEST['currentTaxon']) && is_uuid($_REQUEST['currentTaxon'])) {
1003
      $taxon_uuid = $_REQUEST['currentTaxon'];
1004
    } else if (arg(1) == 'taxon' && is_uuid(arg(2))) {
1005
      $taxon_uuid = arg(2);
1006
    } else {
1007
      $taxon_uuid = null;
1008
    }
1009
  }
1010
1011
  return $taxon_uuid;
1012
}
1013
1014
/**
1015
 * Returns the currently classification tree in use.
1016
 *
1017
 * @return string
1018
 *   The uuid of the currently focused classification
1019
 */
1020
function get_current_classification_uuid() {
1021
  if (isset($_SESSION['cdm']['taxonomictree_uuid']) && is_uuid($_SESSION['cdm']['taxonomictree_uuid'])) {
1022
    return $_SESSION['cdm']['taxonomictree_uuid'];
1023
  }
1024
  else {
1025
    return variable_get(CDM_TAXONOMICTREE_UUID, FALSE);
1026
  }
1027
}
1028
1029 6657531f Andreas Kohlbecker
/*
1030
 function cdm_dataportal_session_clear($cdm_ws_uri_update = FALSE){
1031
 $_SESSION['cdm'] = NULL;
1032
 if(is_string($cdm_ws_uri_update)){
1033
 $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
1034
 }
1035
 }
1036
1037
 function cdm_dataportal_session_validate(){
1038
 if(!isset($_SESSION['cdm']['ws_uri'])){
1039
 $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', FALSE));
1040
 } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', FALSE)){
1041
 cdm_dataportal_session_clear(variable_get('cdm_webservice_url', FALSE));
1042
 }
1043
 }
1044
 */
1045
1046
/**
1047 d7588d36 Andreas Kohlbecker
 * creates a  selector form for taxonomic trees.
1048
 *
1049 0af3ce28 Andreas Kohlbecker
 * @return array
1050
 *  a drupal form array
1051 6657531f Andreas Kohlbecker
 */
1052
function cdm_taxonomictree_selector() {
1053
  _add_js_treeselector();
1054
1055 a8a8f23c Andreas Kohlbecker
  $form = drupal_get_form('cdm_taxonomictree_selector_form');
1056 d7588d36 Andreas Kohlbecker
  return $form;
1057 6657531f Andreas Kohlbecker
}
1058
1059
/**
1060
 * @todo Please document this function.
1061
 * @see http://drupal.org/node/1354
1062 0af3ce28 Andreas Kohlbecker
 *
1063
 * @deprecated use compose_classification_selector instead
1064 6657531f Andreas Kohlbecker
 */
1065
function cdm_taxonomictree_selector_form($form, &$form_state) {
1066
1067
  $url = url('cdm_api/setvalue/session', array('query' => NULL));
1068
  $form['#action'] = $url;
1069
1070
  $form['var'] = array(
1071
    '#weight' => -3,
1072
    '#type' => 'hidden',
1073
    '#value' => '[cdm][taxonomictree_uuid]',
1074
  );
1075
1076
  $destination_array = drupal_get_destination();
1077
  $destination = $destination_array['destination'];
1078
1079
  $form['destination'] = array(
1080
    '#weight' => -3,
1081
    '#type' => 'hidden',
1082
    '#value' =>  $destination,
1083
  );
1084
1085 26fb3778 Andreas Kohlbecker
  $options = cdm_get_taxontrees_as_options();
1086
  $taxontree_includes = variable_get(CDM_TAXONTREE_INCLUDES, null);
1087
  if($taxontree_includes){
1088
    $filtered_options = array();
1089
    foreach($options as $uuid=>$label){
1090
      if(!empty($taxontree_includes[$uuid])){
1091
        $filtered_options[$uuid] = $label;
1092
      }
1093
    }
1094
    $options = $filtered_options;
1095
  }
1096
1097 6657531f Andreas Kohlbecker
  $form['val'] = array(
1098
    '#type' => 'select',
1099
    '#title' => t('Available classifications'),
1100 7663cd0b Andreas Kohlbecker
    '#default_value' => get_current_classification_uuid(),
1101 26fb3778 Andreas Kohlbecker
    '#options' => $options,
1102 0af3ce28 Andreas Kohlbecker
    '#attributes' => array('class' => array('highlite-first-child')),
1103 6657531f Andreas Kohlbecker
  );
1104
1105
  return $form;
1106
1107
}
1108
1109 0af3ce28 Andreas Kohlbecker
/**
1110
 *
1111
 * @ingroup compose
1112
 */
1113
function compose_classification_selector() {
1114
1115
  $destination_array = drupal_get_destination();
1116
  $destination = $destination_array['destination'];
1117
1118
  $options = cdm_get_taxontrees_as_options();
1119
  $items = array();
1120
  $taxontree_includes = variable_get(CDM_TAXONTREE_INCLUDES, null);
1121
1122
  $current_classification_uuid = get_current_classification_uuid();
1123
1124
1125
  foreach($options as $uuid=>$label){
1126
    if(!$taxontree_includes || !empty($taxontree_includes[$uuid])){
1127
1128
      $class_attributes = '';
1129
      if($current_classification_uuid == $uuid){
1130
        $class_attributes  = array('focused');
1131
      }
1132
      $items[] = array(
1133
        'data' => l($label,
1134
          'cdm_api/setvalue/session',
1135
          array(
1136
            'query' => array(
1137
              'destination' => $destination,
1138
              'val' => $uuid,
1139
              'var' => '[cdm][taxonomictree_uuid]'
1140
            ),
1141
          )
1142
        ),
1143
        'class' => $class_attributes
1144
      );
1145
    }
1146
  }
1147
1148
  $render_array = array(
1149
    '#theme' => 'item_list',
1150
    '#type' => 'ul',
1151
    '#items' => $items
1152
  );
1153
1154
  return $render_array;
1155
}
1156
1157
1158 6657531f Andreas Kohlbecker
/* UNREACHABLE since action of form directly links to view.
1159
 function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
1160
1161
 $_SESSION['cdm']['search'] = $form_values;
1162
 //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
1163
 return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
1164
 //$paramstr = compose_url_prameterstr($form_values);
1165
 //return url('/cdm_dataportal/search/taxon/', array('query' => $paramstr));
1166
 }
1167
 */
1168
/* ====================== menu callback functions ====================== */
1169
/**
1170
 * @todo Please document this function.
1171
 * @see http://drupal.org/node/1354
1172
 */
1173
/*
1174
function cdm_dataportal_form_alter(&$form, &$form_state, $form_id) {
1175
  static $comment_node_disabled =  0;
1176
  static $comment_node_read_only =  1;
1177
  static $comment_node_read_write =  2;
1178
1179
  if ($form_id == 'node_type_form'
1180
   && isset($form['identity']['type'])
1181
   && array_key_exists($form['#node_type']->type, cdm_get_nodetypes())
1182
  ) {
1183
    $form['workflow']['comment'] = array(
1184
      '#type' => 'radios',
1185
      '#title' => t('Default comment setting'),
1186
      '#default_value' => variable_get('comment__' . $node->type . $form['#node_type']->type, $comment_node_disabled),
1187
      '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
1188
      '#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
1189
    );
1190
  }
1191
}
1192
*/
1193
1194
/**
1195
 * Displays a list of the known taxonomic names.
1196
 *
1197
 * When the list of taxonomic names is displayed, long lists are split up into
1198
 * multiple pages.
1199
 *
1200
 * TODO: Parameters are still preliminary.
1201
 *
1202
 * @param string $beginsWith
1203
 * @param string $page
1204
 *   Page number to diplay defaults to page 1.
1205
 * @param bool $onlyAccepted
1206
 */
1207
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = FALSE) {
1208
1209
  $out = t('<h3>Sorry, the name list feature is not yet available in this version of the DataPortal software<h3>');
1210
1211
  /*
1212
  // FIXME the filter for accepted names will be a form element, thus this
1213
  // widget should be generated via form api preferably as block.
1214
  $out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
1215
  $out .= theme('cdm_dataportal_widget_names_list', $names, $page);
1216
  $out .= theme('cdm_listof_taxa', $taxonPager);
1217
  return $out;
1218
  */
1219
}
1220
1221
/**
1222
 * @todo Please document this function.
1223
 * @see http://drupal.org/node/1354
1224 c0223d83 Andreas Kohlbecker
 * @throws Exception
1225 6657531f Andreas Kohlbecker
 */
1226
function cdm_dataportal_view_reference($uuid, $arg2 = NULL) {
1227 f26245c8 Andreas Kohlbecker
1228
  cdm_check_valid_portal_page();
1229
1230 6cbefddc Andreas Kohlbecker
  return compose_cdm_reference_page($uuid);
1231 6657531f Andreas Kohlbecker
}
1232
1233
/**
1234 b2ebf0d3 Andreas Kohlbecker
 * Creates a view on a all references contained in the portal.
1235 b5dca1e2 Andreas Kohlbecker
 *
1236
 * This function is used at the path cdm_dataportal/reference/list
1237 6657531f Andreas Kohlbecker
 */
1238
function cdm_dataportal_view_reference_list($pageNumber) {
1239
  $referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE), $pageNumber);
1240 b2ebf0d3 Andreas Kohlbecker
  cdm_reference_pager($referencePager, 'cdm_dataportal/reference/list/');
1241 6657531f Andreas Kohlbecker
}
1242
1243
/**
1244
 * @todo Please document this function.
1245
 * @see http://drupal.org/node/1354
1246
 */
1247
function cdm_dataportal_view_media($mediaUuid, $mediarepresentation_uuid = FALSE, $part = 0) {
1248 f26245c8 Andreas Kohlbecker
1249
  cdm_check_valid_portal_page();
1250
1251 6657531f Andreas Kohlbecker
  $media = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $mediaUuid);
1252
  return theme('cdm_media_page', array(
1253
    'media' => $media,
1254
    'mediarepresentation_uuid' => $mediarepresentation_uuid,
1255
    'partId' => $part,
1256
    ));
1257
}
1258
1259
/**
1260
 * @todo Please document this function.
1261
 * @see http://drupal.org/node/1354
1262
 */
1263
function _load_taxonBase(&$taxonBase) {
1264
  if (isset($taxonBase->uuid)) {
1265
    $taxonBase->name = cdm_ws_get(CDM_WS_TAXON, array($taxonBase->uuid, "name"));
1266
    $taxonBase->name->taggedName = cdm_ws_get(CDM_WS_NAME, array($taxonBase->name->uuid, "taggedName"));
1267
    $taxonBase->name->nomenclaturalReference = cdm_ws_get(CDM_WS_NAME, array($taxonBase->name->uuid, "nomenclaturalReference"));
1268
  }
1269
}
1270
1271 9438ad3a Andreas Kohlbecker
/**
1272
 * Loads the media associated to the given taxon from the cdm server.
1273
 * The aggregation settings regarding taxon relathionships and
1274
 * taxonnomic childen are taken into account.
1275
 *
1276
 * The media lists are cached in a static variable.
1277
 *
1278
 * @param Taxon $taxon
1279
 *   A CDM Taxon entitiy
1280
 *
1281
 * @return array
1282
 *   An array of CDM Media entities
1283
 *
1284
 */
1285
function _load_media_for_taxon($taxon) {
1286
1287
  static $media = NULL;
1288
1289
  if(!isset($media)) {
1290
    $media = array();
1291
  }
1292
  if (!isset($media[$taxon->uuid])) {
1293
1294 f19f47fa Andreas Kohlbecker
    // --- GET Images --- //
1295
    $mediaQueryParameters = array(
1296
        "type" => "ImageFile",
1297
    );
1298
1299
    $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
1300
    $mediaQueryParameters['relationships'] = implode(',', get_selection($relationship_choice['direct']));
1301 a950f2f9 Andreas Kohlbecker
    $mediaQueryParameters['relationshipsInvers'] = implode(',', get_selection($relationship_choice['invers']));
1302
1303
    $taxon_media_filter_choice = variable_get(CDM_TAXON_MEDIA_FILTER, unserialize(CDM_TAXON_MEDIA_FILTER_DEFAULT));
1304
    $mediaQueryParameters['includeTaxonDescriptions'] = (boolean) $taxon_media_filter_choice['includeTaxonDescriptions'] != 0;
1305
    $mediaQueryParameters['includeOccurrences'] = (boolean) $taxon_media_filter_choice['includeOccurrences'] != 0;
1306
    $mediaQueryParameters['includeTaxonNameDescriptions'] = (boolean) $taxon_media_filter_choice['includeTaxonNameDescriptions'] != 0;
1307 9438ad3a Andreas Kohlbecker
1308 f19f47fa Andreas Kohlbecker
    $ws_endpoint = NULL;
1309 9438ad3a Andreas Kohlbecker
    if ( variable_get('cdm_images_include_children', 0) == 0) {
1310
      $ws_endpoint = CDM_WS_PORTAL_TAXON_MEDIA;
1311
    } else {
1312
      $ws_endpoint = CDM_WS_PORTAL_TAXON_SUBTREE_MEDIA;
1313
    }
1314 f19f47fa Andreas Kohlbecker
1315
    $media[$taxon->uuid] = cdm_ws_get($ws_endpoint,
1316
        array(
1317
            $taxon->uuid,
1318
        ),
1319 9438ad3a Andreas Kohlbecker
        queryString($mediaQueryParameters)
1320
       );
1321
  }
1322
1323
  return $media[$taxon->uuid];
1324
}
1325
1326
/**
1327
 *
1328
 * @param Taxon $taxon
1329
 *   A CDM Taxon entitiy
1330
 *
1331
 * @return array
1332
 *   An array of CDM SpecimenOrObservation entities
1333
 *
1334
function _load_occurences_for_taxon($taxon){
1335
1336
  static $occurences = NULL;
1337 f19f47fa Andreas Kohlbecker
1338
  if(!isset($occurences)) {
1339
    $occurences = array();
1340
  }
1341 9438ad3a Andreas Kohlbecker
1342
  if (!isset($occurences[$taxon->uuid])){
1343
1344
    $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
1345
    $relationship_choice['direct'] = get_selection($relationship_choice['direct']);
1346 f19f47fa Andreas Kohlbecker
    $relationship_choice['invers'] = get_selection($relationship_choice['invers']);
1347
1348
    $by_associatedtaxon_query = http_build_query(array(
1349
        'relationshipsInvers' => implode(',', $relationship_choice['invers']),
1350
        'relationships' => implode(',', $relationship_choice['direct']),
1351
        'pageSize' => null // all hits in one page
1352
    )
1353
    );
1354
1355
    $pager = cdm_ws_get(CDM_WS_OCCURRENCE_BY_ASSOCIATEDTAXON,
1356
        null,
1357
        $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
1358
    );
1359
1360
1361
    if(isset($pager->records[0])){
1362
      $occurences[$taxon->uuid] =  $pager->records;
1363
    }
1364
  }
1365 9438ad3a Andreas Kohlbecker
  return $occurences[$taxon->uuid];
1366
}
1367
 */
1368
1369 6657531f Andreas Kohlbecker
/**
1370
 * Gets a Drupal variable, string or array and returns it.
1371
 *
1372
 * Similar to the variable_get() function of Drupal, except that this function
1373
 * is able to handle arrays correctly. This function is especially useful
1374 8c962983 Andreas Kohlbecker
 * when dealing with collections of settings form elements (#tree = TRUE).
1375 6657531f Andreas Kohlbecker
 *
1376
 * @param string $variableKey
1377
 *   The Unique key of the Drupal variable in the Drupal variables table.
1378
 * @param string $defaultValueString
1379
 *   A string as for example derived from a CONSTANT.
1380
 *
1381
 * @return mixed
1382 8c962983 Andreas Kohlbecker
 *   usually an array, depending on the nature of the variable.
1383
 *
1384
 * TODO compare with get_array_variable_merged() duplicate functions?
1385 0f7c9d15 Andreas Kohlbecker
 * @deprecated rather use get_array_variable_merged() since this function
1386
 * used an array as second parameter
1387 6657531f Andreas Kohlbecker
 */
1388
function mixed_variable_get($variableKey, $defaultValueString) {
1389
  $systemDefaults = unserialize($defaultValueString);
1390
  $storedSettings = variable_get($variableKey, array());
1391
  if (is_array($storedSettings)) {
1392 8c962983 Andreas Kohlbecker
    // TODO better use drupal_array_merge_deep() ?
1393 6657531f Andreas Kohlbecker
    $settings = array_merge($systemDefaults, $storedSettings);
1394
  }
1395
  else {
1396
    $settings = $systemDefaults;
1397
  }
1398
  return $settings;
1399
}
1400
1401 b5519d3a Andreas Kohlbecker
/**
1402 0f7c9d15 Andreas Kohlbecker
 * Recursive function to convert an object into an array.
1403 b5519d3a Andreas Kohlbecker
 * also subordinate objects will be converted.
1404
 *
1405
 * @param object $object
1406 db725031 Andreas Kohlbecker
 *  the object to be converted
1407
 * @return array
1408
 *  The array
1409 b5519d3a Andreas Kohlbecker
 */
1410 db725031 Andreas Kohlbecker
function object_to_array($object) {
1411 b5519d3a Andreas Kohlbecker
  if(is_object($object) || is_array($object)) {
1412
    $array = (array)$object;
1413
    foreach ($array as $key=>$value){
1414 db725031 Andreas Kohlbecker
      $array[$key] = object_to_array($value);
1415 b5519d3a Andreas Kohlbecker
    }
1416
    return $array;
1417
  } else {
1418
    return $object;
1419
  }
1420
}
1421
1422 2fd6da0b Andreas Kohlbecker
/**
1423
 * Searches the $collection for the cdm entitiy given as $element.
1424
 *
1425
 * The elements are compared by their UUID.
1426
 *
1427
 * @param $element
1428
 *  the CDM entitiy to search for
1429
 * @param $collection
1430
 *  the list of CDM entities to search in
1431
 *
1432
 * @return boolean TRUE if the $collection contains the $element, otheriwse FALSE
1433
 *
1434
 */
1435
function contains_cdm_entitiy($element, $collection) {
1436
  $result = FALSE;
1437
  foreach ($collection as $a) {
1438
    if ($a->uuid == $element->uuid) {
1439
      $result = TRUE;
1440
    }
1441
  }
1442
  return $result;
1443
}
1444
1445
/**
1446
 * Fiters the array $entity_list of CDM entities by the list
1447
 * of $excludes. Any element contained in the $excludes will be removed
1448
 * from included int the retuned list.
1449
 *
1450
 * If the $entity_list is not an array the $excludes will be returned.
1451
 */
1452
function filter_cdm_entity_list($entity_list, $excludes) {
1453
  if (is_array($entity_list)) {
1454
    $result = $entity_list;
1455
    if ($excludes) {
1456
      foreach ($excludes as $exclude) {
1457
        if (!contains_cdm_entitiy($exclude, $entity_list)) {
1458
          $result[] = $exclude;
1459
        }
1460
      }
1461
    }
1462
  }
1463
  else {
1464
    $result = $excludes;
1465
  }
1466
  return $result;
1467
}
1468
1469 0a1151a4 Andreas Kohlbecker
/**
1470 6f5c7f2a Andreas Kohlbecker
 * Wraps the given $html string into a render array suitable for drupal_render()
1471 0a1151a4 Andreas Kohlbecker
 *
1472 65345976 Andreas Kohlbecker
 * @param $html
1473 0a1151a4 Andreas Kohlbecker
 *   the html string, see
1474
 *   http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#markup
1475 65345976 Andreas Kohlbecker
 * @param $weight
1476 0a1151a4 Andreas Kohlbecker
 *   A positive or negative number (integer or decimal).
1477
 *   see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#weightval
1478 65345976 Andreas Kohlbecker
 * @param $prefix
1479
 *   Optional markup for the '#prefix' element of the render array
1480
 * @param $suffix
1481
 *   Optional markup for the '#suffix' element of the render array
1482 0a1151a4 Andreas Kohlbecker
 *
1483 65345976 Andreas Kohlbecker
 * @return array
1484 0a1151a4 Andreas Kohlbecker
 *   A render array
1485
 *
1486
 */
1487 65345976 Andreas Kohlbecker
function markup_to_render_array($html, $weight = FALSE, $prefix = NULL, $suffix = NULL) {
1488 6f5c7f2a Andreas Kohlbecker
  $render_array = array(
1489
    '#markup' => $html
1490 1ce9afb7 Patric Plitzner
      );
1491 6f5c7f2a Andreas Kohlbecker
  if (is_numeric($weight)) {
1492
    $render_array['#weight'] = $weight;
1493 65345976 Andreas Kohlbecker
  }
1494
  if($prefix){
1495
    $render_array['#prefix'] = $prefix;
1496
  }
1497
  if($suffix) {
1498
    $render_array['#suffix'] = $suffix;
1499
  }
1500 6f5c7f2a Andreas Kohlbecker
  return $render_array;
1501 0a1151a4 Andreas Kohlbecker
}
1502
1503 6657531f Andreas Kohlbecker
/**
1504
 * Loads the subgraph of a given PolytomousKeyNode.
1505
 *
1506
 * Loads the subgraph of the given PolytomousKeyNode recursively from
1507
 * the CDM REST service.
1508
 *
1509
 * @param mixed $polytomousKeyNode
1510
 *   PolytomousKeyNode passed by reference.
1511
 *
1512
 * @return void
1513
 */
1514
function _load_polytomousKeySubGraph(&$polytomousKeyNode) {
1515
1516
  if (!$polytomousKeyNode) {
1517
    return;
1518
  }
1519
  if ($polytomousKeyNode->class != "PolytomousKeyNode") {
1520 f19f47fa Andreas Kohlbecker
    drupal_set_message('_load_polytomousKeySubGraph(): ' . t('invalid type given.'), 'error');
1521 6657531f Andreas Kohlbecker
    return;
1522
  }
1523
  if (!is_uuid($polytomousKeyNode->uuid)) {
1524 f19f47fa Andreas Kohlbecker
    drupal_set_message('_load_polytomousKeySubGraph(): ' . t('invalid type given.'), 'error');
1525 6657531f Andreas Kohlbecker
    return;
1526
  }
1527
1528
  $polytomousKeyNode = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, $polytomousKeyNode->uuid);
1529
1530
  if (!$polytomousKeyNode) {
1531
    // drupal_set_message("_load_polytomousKeyChildNodes() : could not load polytomousKeyNode", "error");
1532
    return;
1533
  }
1534
1535
  // Load children.
1536
  foreach ($polytomousKeyNode->children as &$childNode) {
1537
    _load_polytomousKeySubGraph($childNode);
1538
  }
1539
1540
  // Load subkey.
1541
  $polytomousKeyNode->subkey = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, array($polytomousKeyNode->uuid, "subkey"));
1542
1543
  // Load taxon.
1544
  $polytomousKeyNode->taxon = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, array($polytomousKeyNode->uuid, "taxon"));
1545
  _load_taxonBase($polytomousKeyNode->taxon);
1546
  return;
1547
}
1548
1549
/**
1550
 * @todo Please document this function.
1551
 * @see http://drupal.org/node/1354
1552
 */
1553
function cdm_dataportal_view_polytomousKey($polytomousKeyUuid) {
1554 f26245c8 Andreas Kohlbecker
1555
  cdm_check_valid_portal_page();
1556
1557 6657531f Andreas Kohlbecker
  $polytomousKey = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, $polytomousKeyUuid);
1558
1559
  $sourcePager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'sources'));
1560
  if (is_array($sourcePager->records)) {
1561
    $polytomousKey->sources = $sourcePager->records;
1562
    // $polytomousKey->sources->citation = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'sources'));
1563
  }
1564
1565
  $annotationPager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'annotations'));
1566
  if (is_array($annotationPager->records)) {
1567
    $polytomousKey->annotations = $annotationPager->records;
1568
  }
1569
1570
  _load_polytomousKeySubGraph($polytomousKey->root);
1571
  return theme('cdm_polytomousKey_page', array('polytomousKey' => $polytomousKey));
1572
}
1573
1574
/**
1575
 * Creates a taxon page view or a chapter of it.
1576
 *
1577
 * The taxon page gives detailed information on a taxon, it shows:
1578
 *  - Taxon name.
1579
 *  - Full list of synonyms homotypic synonyms on top, followed by the
1580
 *    heterotypic and finally followed by misapplied names.
1581
 *    The list is ordered historically.
1582
 *  - All description associated with the taxon.
1583
 *
1584
 * @param string $uuid
1585
 * @param string $chapter
1586
 *   Name of the part to display, valid values are:
1587
 *   'description', 'images', 'synonymy', 'specimens', 'all'.
1588
 *
1589 793dae0a Andreas Kohlbecker
 * @return string
1590 6657531f Andreas Kohlbecker
 */
1591 0c5a23b6 Andreas Kohlbecker
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all') {
1592 5ea8b301 Andreas Kohlbecker
1593 f26245c8 Andreas Kohlbecker
  cdm_check_valid_taxon_page($chapter);
1594 2dd59bb5 Andreas Kohlbecker
  cdm_dd("START OF TAXON PAGE [" . $chapter . "] " . $uuid . ' for ' . $_GET['q']);
1595 5ea8b301 Andreas Kohlbecker
  // show a warning in case the javascript development mode is anabled
1596
  if(variable_get('cdm_js_devel_mode', FALSE)) {
1597
    drupal_set_message(t('The !url1 is enabled.
1598
        WARNING: this is a performance penalty and must be turned off on production websites.', array(
1599
          '!url1' => l('java-script development mode', 'admin/config/cdm_dataportal/settings', array('fragment' => 'edit-cdm-js-devel-mode'))
1600
    )),
1601
    'warning'
1602
        );
1603
  }
1604
1605 6657531f Andreas Kohlbecker
  // Display the page for the taxon defined by $uuid.
1606
  // set_last_taxon_page_tab(arg(3));
1607 0c5a23b6 Andreas Kohlbecker
  $taxonpage = cdm_dataportal_taxon_view($uuid, $chapter);
1608 6657531f Andreas Kohlbecker
  if (!empty($taxonpage)) {
1609 2dd59bb5 Andreas Kohlbecker
    cdm_dd("END OF TAXON PAGE [" . $chapter . "] " . $uuid);
1610 6657531f Andreas Kohlbecker
    return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title, $taxonpage->content);
1611
  }
1612
  else {
1613 2dd59bb5 Andreas Kohlbecker
    cdm_dd("END OF TAXON PAGE [" . $chapter . "] " . $uuid . ' !!! PAGE IS EMPTY !!!');
1614 6657531f Andreas Kohlbecker
    return '';
1615
  }
1616
}
1617
1618
/**
1619 59f6da42 Andreas Kohlbecker
 * This function will genreate the taxon page part ($chapter) and returns a taxonpage object
1620
 * which has two fields, one for the page title and one for the content. Later on in the
1621
 * process chain the value contained in these fields will be passed to the cdm_node_show()
1622
 * function as the function parameters $title and $content.
1623 6657531f Andreas Kohlbecker
 *
1624
 * @param string $uuid
1625 59f6da42 Andreas Kohlbecker
 *   the uuid of the taxon to show
1626 6657531f Andreas Kohlbecker
 * @param string $chapter
1627
 *   Name of the part to display, valid values are:
1628
 *   'description', 'images', 'synonymy', 'all'.
1629
 *
1630 0c5a23b6 Andreas Kohlbecker
 * @return object with the following fields:
1631 59f6da42 Andreas Kohlbecker
 *   - title : the title of the page
1632
 *   - content: the content of the page
1633
 *
1634 8a1b451e Andreas Kohlbecker
 * @throws Exception
1635
 *
1636 6657531f Andreas Kohlbecker
 */
1637 8cf9238f Andreas Kohlbecker
function cdm_dataportal_taxon_view($uuid, $chapter = 'all') {
1638 6657531f Andreas Kohlbecker
  // Taxon object.
1639
  $taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $uuid);
1640
  if (empty($taxon)) {
1641
    drupal_set_title(t('Taxon does not exist'), PASS_THROUGH);
1642 8a1b451e Andreas Kohlbecker
    return null;
1643 6657531f Andreas Kohlbecker
  }
1644
  $taxonpage = new stdClass();
1645
1646
  $taxonpage->title = theme('cdm_taxon_page_title', array(
1647 8cf9238f Andreas Kohlbecker
    'taxon' => $taxon
1648 6657531f Andreas Kohlbecker
  ));
1649
1650
  // Check if the taxon id contained in the currently selected tree.
1651 7663cd0b Andreas Kohlbecker
  $taxon_in_current_classification = taxon_in_current_classification($uuid);
1652 61b6ee11 Andreas Kohlbecker
1653 7663cd0b Andreas Kohlbecker
  if (!$taxon_in_current_classification) {
1654 61b6ee11 Andreas Kohlbecker
    $classifications = get_classifications_for_taxon($taxon);
1655 e24d373b Andreas Kohlbecker
    RenderHints::pushToRenderStack('not_in_current_classification');
1656
    $taxon_name_markup = render_taxon_or_name($taxon);
1657
1658 61b6ee11 Andreas Kohlbecker
    if (count($classifications) == 0) {
1659 e24d373b Andreas Kohlbecker
      drupal_set_message(t('This concept of the taxon !taxonname is not contained as an accepted taxon in the currently chosen classification.',
1660 fba00a4f Andreas Kohlbecker
        array(
1661 e24d373b Andreas Kohlbecker
        '!taxonname' => $taxon_name_markup,
1662 fba00a4f Andreas Kohlbecker
        )
1663
      ), 'warning');
1664 6657531f Andreas Kohlbecker
    }
1665
    else {
1666
      $trees = '';
1667 61b6ee11 Andreas Kohlbecker
      foreach ($classifications as $classification) {
1668
        if (isset($classification->titleCache)) {
1669
          $trees .= ($trees ? ', ' : '') . '<strong>' . $classification->titleCache . '</strong>';
1670 6657531f Andreas Kohlbecker
        }
1671
      }
1672
1673 15b7c460 Andreas Kohlbecker
      drupal_set_message(format_plural(count($trees),
1674 e24d373b Andreas Kohlbecker
          'This concept of the taxon !taxonname is not contained as an accepted taxon in the currently chosen classification, but in this one: !trees',
1675
          'This concept of the taxon !taxonname is not contained as an accepted taxon in the currently chosen classification, but in one of these: !trees',
1676
          array('!taxonname' => $taxon_name_markup, '!trees' => $trees)
1677 fba00a4f Andreas Kohlbecker
        ) ,
1678
        'warning');
1679 6657531f Andreas Kohlbecker
    }
1680 e24d373b Andreas Kohlbecker
    RenderHints::popFromRenderStack();
1681 6657531f Andreas Kohlbecker
  }
1682
1683
  // Render the taxon page.
1684 9a607f17 Andreas Kohlbecker
  $render_array = compose_cdm_taxon_page($taxon, $chapter);
1685
  $taxonpage->content = drupal_render($render_array);
1686 6657531f Andreas Kohlbecker
1687
  return $taxonpage;
1688
}
1689
1690 9d37c9d9 Patric Plitzner
/**
1691
 * Creates a specimen page view.
1692
 * @param string $uuid the UUID of the specimen
1693
 * @return array|string
1694
 */
1695
function cdm_dataportal_specimen_page_view($uuid) {
1696
1697
    //cdm_check_valid_taxon_page($chapter);
1698
    //cdm_dd("START OF TAXON PAGE [" . $chapter . "] " . $uuid . ' for ' . $_GET['q']);
1699
    // show a warning in case the javascript development mode is anabled
1700
    if(variable_get('cdm_js_devel_mode', FALSE)) {
1701
        drupal_set_message(t('The !url1 is enabled.
1702
        WARNING: this is a performance penalty and must be turned off on production websites.', array(
1703
            '!url1' => l('java-script development mode', 'admin/config/cdm_dataportal/settings', array('fragment' => 'edit-cdm-js-devel-mode'))
1704
        )),
1705
            'warning'
1706
        );
1707
    }
1708
1709
    // Display the page for the specimen defined by $uuid.
1710
    $specimenpage = cdm_dataportal_specimen_view($uuid);
1711
    if (!empty($specimenpage)) {
1712
        return cdm_node_show(NODETYPE_TAXON, $uuid, $specimenpage->title, $specimenpage->content);
1713
    }
1714
    else {
1715
        return '';
1716
    }
1717
}
1718
1719
/**
1720
 *
1721
 * Creates a specimen view.
1722
 * @param string $uuid the UUID of the specimen
1723
 * @return array|string
1724
 */
1725
function cdm_dataportal_specimen_view($uuid) {
1726 a2acff0a Katja Luther
    //TODO: get the derivateDTO and don't call the webservice a second time in compose_cdm_specimen_page
1727 907cc9b4 Patrick Plitzner
    $specimen = cdm_ws_get(CDM_WS_PORTAL_OCCURRENCE, $uuid);
1728 9d37c9d9 Patric Plitzner
    if (empty($specimen)) {
1729
        drupal_set_title(t('Specimen does not exist'), PASS_THROUGH);
1730
        return FALSE;
1731
    }
1732
    $specimenpage = new stdClass();
1733
1734
    $specimenpage->title = theme('cdm_specimen_page_title', array(
1735
        'specimen' => $specimen
1736
    ));
1737
1738 b810158e Patric Plitzner
    // Render the specimen page.
1739 a2acff0a Katja Luther
    $render_array = compose_cdm_specimen_page($specimen);
1740 9d37c9d9 Patric Plitzner
    $specimenpage->content = drupal_render($render_array);
1741
1742
    return $specimenpage;
1743
}
1744
1745 30845bda Andreas Kohlbecker
/**
1746
 *
1747
 * Creates a named area view.
1748
 * @param string $uuid the UUID of the specimen
1749
 *  * @return object
1750
 *   An object with two fields:
1751
 *     - title: the page title
1752
 *     - content: the page content
1753
 */
1754
1755
function cdm_dataportal_named_area_view($uuid) {
1756
  $named_area = cdm_ws_get(CDM_WS_PORTAL_TERM, $uuid);
1757
  if (empty($named_area) || $named_area->class !== 'NamedArea') {
1758
    drupal_set_title(t('Named area does not exist'), PASS_THROUGH);
1759
    return FALSE;
1760
  }
1761
  $named_area_page = new stdClass();
1762
1763
  $named_area_page->title = $named_area->representation_L10n;
1764
1765
  // Render the specimen page.
1766
  $render_array = compose_cdm_named_area_page($uuid);
1767
  $named_area_page->content = drupal_render($render_array);
1768
1769
  return $named_area_page;
1770
}
1771
1772
function cdm_dataportal_named_area_page_view($uuid) {
1773
1774
  cdm_check_valid_portal_page();
1775
1776
  $named_area_page = cdm_dataportal_named_area_view($uuid);
1777
  if (!empty($named_area_page)) {
1778
    return cdm_node_show(NODETYPE_NAME, $uuid, $named_area_page->title, $named_area_page->content);
1779
  }
1780
  else {
1781
    return '';
1782
  }
1783
1784
1785
}
1786
1787 6657531f Andreas Kohlbecker
/**
1788
 * Returns a name page as a Drupal node ready to be renderized by Drupal.
1789
 *
1790
 * The node page shows the taxon name title and the list of taxon related
1791
 * with such taxon. Name on the tree already in use.
1792
 *
1793 d4ea0dd9 Andreas Kohlbecker
 * @param string $taxon_name_uuid
1794 102f8c26 Andreas Kohlbecker
 *   The uuid of the CDM TaxonName to show a name page for
1795 d4ea0dd9 Andreas Kohlbecker
 * @param string $taxon_to_hide_uuid
1796 51b04faf Andreas Kohlbecker
 *   A taxon which should not be displayed in the taxon list
1797 d4ea0dd9 Andreas Kohlbecker
 * @param string $highlite_synonym_uuid
1798 51b04faf Andreas Kohlbecker
 *   Optinal parameter wich takes another taxon uuid, if given the
1799
 *   target taxon pages will show the syonymy tab where the taxon
1800
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1801
 *   in case it is found on this page.
1802 6657531f Andreas Kohlbecker
 *
1803
 * @return mixed
1804
 *   The formatted name page as node.
1805
 */
1806
function cdm_dataportal_name_page_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid = NULL) {
1807
1808 f26245c8 Andreas Kohlbecker
  cdm_check_valid_portal_page();
1809
1810 6657531f Andreas Kohlbecker
  $taxonname_page = cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid);
1811
  if (!empty($taxonname_page)) {
1812
    return cdm_node_show(NODETYPE_NAME, $taxon_name_uuid, $taxonname_page->title, $taxonname_page->content);
1813
  }
1814
  else {
1815
    return '';
1816
  }
1817
}
1818
1819
/**
1820 102f8c26 Andreas Kohlbecker
 * View function for a TaxonName page.
1821 6657531f Andreas Kohlbecker
 *
1822 e3001c8d Andreas Kohlbecker
 * The name page lists all taxa for which the name specified by the
1823
 * $taxon_name_uuid is being used. I case there is only one name the
1824
 * page automatically redirects ti the according taxon page. Otherwise
1825
 * the list of names is displayed.
1826 6657531f Andreas Kohlbecker
 *
1827 e3001c8d Andreas Kohlbecker
 * The parameter $taxon_to_hide_uuid allows to exclude a taxon from the
1828
 * list of taxa. This is useful for example when referencing from a taxon
1829
 * to the name page and the referring taxon should not be repeaded in the
1830
 * name page.
1831 6657531f Andreas Kohlbecker
 *
1832 e3001c8d Andreas Kohlbecker
 *
1833 d4ea0dd9 Andreas Kohlbecker
 * @param string $taxon_name_uuid
1834 102f8c26 Andreas Kohlbecker
 *   The uuid of the CDM TaxonName to show a name page for
1835 d4ea0dd9 Andreas Kohlbecker
 * @param string $taxon_to_hide_uuid
1836 e3001c8d Andreas Kohlbecker
 *   A taxon which should not be displayed in the taxon list
1837 d4ea0dd9 Andreas Kohlbecker
 * @param string $highlite_synonym_uuid
1838 e3001c8d Andreas Kohlbecker
 *   Optinal parameter wich takes another taxon uuid, if given the
1839
 *   target taxon pages will show the syonymy tab where the taxon
1840
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1841
 *   in case it is found on this page.
1842
 *
1843
 * @return object
1844
 *   An object with two fields:
1845
 *     - title: the page title
1846
 *     - content: the page content
1847 6657531f Andreas Kohlbecker
 */
1848 e3001c8d Andreas Kohlbecker
function cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $highlite_synonym_uuid = NULL) {
1849 6657531f Andreas Kohlbecker
  // Getting the full taxonname object from the server.
1850 d0d068e9 Andreas Kohlbecker
  $taxon_name = cdm_ws_get(CDM_WS_PORTAL_NAME, array($taxon_name_uuid));
1851 6657531f Andreas Kohlbecker
  if (!$taxon_name) {
1852
    drupal_set_title(t('Taxon name does not exist'), PASS_THROUGH);
1853
    return FALSE;
1854
  }
1855
  // Searching for all the taxa connected with the taxon name on the tree
1856
  // in use.
1857
  $name_cache = cdm_ws_get(CDM_WS_NAME_NAMECAHE, array($taxon_name_uuid));
1858
  $request_params = array();
1859
  $request_params['query'] = $name_cache;
1860 7663cd0b Andreas Kohlbecker
  $request_params['tree'] = get_current_classification_uuid();
1861 6657531f Andreas Kohlbecker
  $request_params['doTaxa'] = 1;
1862
  $request_params['doSynonyms'] = 1;
1863
  $request_params['doTaxaByCommonNames'] = 0;
1864
  $request_params['matchMode'] = "EXACT";
1865
  $taxon_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_FIND, NULL, queryString($request_params));
1866
1867 d0d068e9 Andreas Kohlbecker
  // Removing the name where we came from.
1868 6657531f Andreas Kohlbecker
  foreach ($taxon_pager->records as $k => &$taxon) {
1869
    if ($taxon->uuid == $taxon_to_hide_uuid) {
1870
      unset($taxon_pager->records[$k]);
1871
    }
1872
  }
1873
  // Show the taxa list or go to the singular taxon.
1874
  if (sizeof($taxon_pager->records) == 1) {// Single taxon case.
1875 e3001c8d Andreas Kohlbecker
    $singleTaxon = array_pop($taxon_pager->records);
1876 6657531f Andreas Kohlbecker
    if ($singleTaxon->class != "Taxon") {
1877
      // It is a Synonym -> look for the accepted.
1878 7635cc7d Andreas Kohlbecker
      $accepted_taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, array($singleTaxon->uuid), 'classificationFilter=' . get_current_classification_uuid());
1879 e3001c8d Andreas Kohlbecker
      if (!empty($highlite_synonym_uuid)) {
1880 7635cc7d Andreas Kohlbecker
        drupal_goto('cdm_dataportal/taxon/' . $accepted_taxon->uuid . '/synonymy', array('query' => array('highlite' => $highlite_synonym_uuid)));
1881 6657531f Andreas Kohlbecker
      }
1882
      else {
1883 7635cc7d Andreas Kohlbecker
        drupal_goto('cdm_dataportal/taxon/' . $accepted_taxon->uuid . '/synonymy', array('query' => array('highlite' => $singleTaxon->uuid)));
1884 6657531f Andreas Kohlbecker
      }
1885
    }
1886
    else {
1887
      // It is an accepted taxon.
1888 e3001c8d Andreas Kohlbecker
      if (!empty($highlite_synonym_uuid)) {
1889
        drupal_goto('cdm_dataportal/taxon/' . $singleTaxon->uuid . '/synonymy', array('query' => array('highlite' => $highlite_synonym_uuid)));
1890 6657531f Andreas Kohlbecker
      }
1891
      else {
1892
        drupal_goto('cdm_dataportal/taxon/' . $singleTaxon->uuid);
1893
      }
1894
    }
1895
  }
1896
  else {// More than one taxa case.
1897
    $taxon_name_page = new stdClass();
1898 4f225a0f Andreas Kohlbecker
    $taxon_name_page->title = theme('cdm_name_page_title', array('taxon_name' => $taxon_name));
1899 6657531f Andreas Kohlbecker
    if ($taxon_pager->records) {
1900 7272d19c Andreas Kohlbecker
      $taxon_name_page->content = compose_list_of_taxa($taxon_pager->records);
1901 6657531f Andreas Kohlbecker
    }
1902
    else {
1903 6b294c11 Andreas Kohlbecker
      $taxon_name_page->content = 'This name is not assigned to a taxon.';
1904 6657531f Andreas Kohlbecker
    }
1905
    return $taxon_name_page;
1906
  }
1907
}
1908
1909 e0d3e148 Andreas Kohlbecker
/**
1910
 * Returns a registration page as a Drupal node to be rendered by Drupal.
1911
 *
1912
 * @param string  $registration_identifier_encoded
1913
 *   The persistent identifier of the registration urlencoded.
1914
 * @return mixed
1915
 *   The formatted registration page as node.
1916
 */
1917
function cdm_dataportal_registration_page_view($registration_identifier_encoded) {
1918
1919
  cdm_check_valid_portal_page("/\/cdm_dataportal\/registration\/.*$/");
1920
  $registration_page = cdm_dataportal_registration_view($registration_identifier_encoded);
1921
  return cdm_node_show_simulate($registration_page);
1922
}
1923
1924
function cdm_dataportal_registration_view($registration_identifier_encoded) {
1925
1926
  $registration_identifier = urldecode($registration_identifier_encoded);
1927
1928 6cbefddc Andreas Kohlbecker
  $render_array = array();
1929 1e7459da Andreas Kohlbecker
  $registration_dto = cdm_ws_get(CDM_WS_REGISTRATION_DTO_BY_IDENTIFIER . $registration_identifier_encoded);
1930 e0d3e148 Andreas Kohlbecker
  if($registration_dto){
1931 18f73480 Andreas Kohlbecker
    drupal_set_title(t('Registration Id') . ' ' . $registration_identifier, PASS_THROUGH);
1932 e0d3e148 Andreas Kohlbecker
1933 5446f047 Andreas Kohlbecker
    $render_array = compose_registration_dto($registration_dto, true, false, false);
1934 e0d3e148 Andreas Kohlbecker
1935
  } else {
1936 1e7459da Andreas Kohlbecker
    $status_text = cdm_ws_get(CDM_WS_REGISTRATION_STATUS_BY_IDENTIFIER, array($registration_identifier));
1937 6c9d417d Andreas Kohlbecker
    if(isset($status_text->String)) {
1938 1e7459da Andreas Kohlbecker
      $status_text = strtolower($status_text->String);
1939
      if($status_text == 'preparation' || $status_text == 'curation'){
1940
        $status_text = 'in ' . $status_text;
1941
      }
1942
      drupal_set_title(t('Registration ' . $status_text), PASS_THROUGH);
1943
      //$status_message;
1944
      $render_array['error'] = markup_to_render_array("A registration with the identifier  " . $registration_identifier . " is " . $status_text);
1945
    } else {
1946
      drupal_set_title(t('Registration not found'), PASS_THROUGH);
1947
      $render_array['error'] = markup_to_render_array("A registration with the identifier  " . $registration_identifier . " does not exist.");
1948
    }
1949 e0d3e148 Andreas Kohlbecker
  }
1950
1951 5446f047 Andreas Kohlbecker
  $render_array = array(
1952
    '#prefix' => '<div class="registration">',
1953
    'registration' => $render_array,
1954
    '#suffix' => '</div>',
1955
  );
1956
1957 e0d3e148 Andreas Kohlbecker
  return $render_array ;
1958
}
1959
1960 6cbefddc Andreas Kohlbecker
/**
1961 5446f047 Andreas Kohlbecker
 * Composes information for a registration from a dto object
1962 5e4cae59 Andreas Kohlbecker
 *
1963 6cbefddc Andreas Kohlbecker
 * @param $registration_dto
1964 5446f047 Andreas Kohlbecker
 * @param $with_citation
1965
 *   Whether to show the citation.
1966
 * @param $with_identifier
1967
 *   Whether to add the identifier to the summary (in enabled)
1968
 * @param $with_compact_summary
1969
 *   Whether to display the name and type designations as compact summary or in extended form.
1970 5e4cae59 Andreas Kohlbecker
 *
1971 6cbefddc Andreas Kohlbecker
 * @return array
1972 bbeade6a Andreas Kohlbecker
 *    A drupal render array with the elements:
1973 5446f047 Andreas Kohlbecker
 *    - 'publication_date_and_office'
1974
 *    - 'citation'
1975
 *    - 'summary' OR  'name', 'name-relations', 'name_type_designations', 'specimen_table' (depending on parameter $with_compact_summary)
1976 5e4cae59 Andreas Kohlbecker
 * @ingroup compose
1977 6cbefddc Andreas Kohlbecker
 */
1978 5446f047 Andreas Kohlbecker
function compose_registration_dto($registration_dto, $with_citation = true, $with_identifier = false, $with_compact_summary = true)
1979 6cbefddc Andreas Kohlbecker
{
1980
  $render_array = array();
1981 723aa98c Andreas Kohlbecker
  $media_link_map = array();
1982 5446f047 Andreas Kohlbecker
1983 84e766b2 Andreas Kohlbecker
1984 5446f047 Andreas Kohlbecker
1985
  if($with_compact_summary){
1986
    // summary
1987
    $taggged_text_expanded = cdm_tagged_text_expand_entity_references($registration_dto->summaryTaggedText);
1988
    foreach ($taggged_text_expanded  as $tagged_text){
1989
      if(isset($tagged_text->entityReference->type) && $tagged_text->entityReference->type == 'SpecimenTypeDesignation') {
1990
        $mediaDTOs = cdm_ws_get('typedesignation/$0/media', array($tagged_text->entityReference->uuid));
1991
        if(isset($mediaDTOs[0]->uri)){
1992
            $media_url_key = '{link-' . $mediaDTOs[0]->uuid . '}';
1993
            $tagged_text->text = str_replace('[icon]', '[icon]' . $media_url_key, $tagged_text->text);
1994
            $media_link_map[$media_url_key] =  cdm_external_uri($mediaDTOs[0]->uri, true);
1995
        }
1996
      }
1997
    }
1998
    $registation_markup = cdm_tagged_text_to_markup($taggged_text_expanded);
1999
    foreach($media_link_map as $media_url_key => $link){
2000
      $registation_markup = str_replace($media_url_key, $link, $registation_markup);
2001
    }
2002
    if($with_identifier){
2003
      $registation_markup .= " " . l ($registration_dto->identifier, $registration_dto->identifier);
2004
    }
2005 88b26bc2 Andreas Kohlbecker
    $render_array['summary'] = markup_to_render_array('<p class="registration-summary">' . $registation_markup . "</p>", 0);
2006 5446f047 Andreas Kohlbecker
  } else {
2007
    // name and typedesignation in detail
2008
    if($registration_dto->nameRef){
2009
      $name = cdm_ws_get(CDM_WS_PORTAL_NAME, $registration_dto->nameRef->uuid);
2010 88b26bc2 Andreas Kohlbecker
      $render_array['name'] = markup_to_render_array('<p class="name">' . render_taxon_or_name($name) . '</p>', 0);
2011 5446f047 Andreas Kohlbecker
      $name_relations = cdm_ws_fetch_all(str_replace("$0", $registration_dto->nameRef->uuid, CDM_WS_PORTAL_NAME_NAME_RELATIONS));
2012
      $render_array['name_relations'] = markup_to_render_array(render_name_relationships_of($name_relations, $registration_dto->nameRef->uuid, null, false));
2013 ed4d07fb Andreas Kohlbecker
      $render_array['name_relations']['#weight'] = 10;
2014 5446f047 Andreas Kohlbecker
    }
2015
    if(is_object($registration_dto->orderdTypeDesignationWorkingSets)) {
2016
      $field_unit_uuids = array();
2017 f5e7f68e Andreas Kohlbecker
      $specimen_type_designation_refs = array();
2018 5446f047 Andreas Kohlbecker
      $name_type_designation_refs = array();
2019
      foreach ((array)$registration_dto->orderdTypeDesignationWorkingSets as $field_unit_ref => $obj) {
2020
        $tokens = explode("#", $field_unit_ref);
2021 f5e7f68e Andreas Kohlbecker
        foreach ($obj as $type_status => $entity_reference_list) {
2022
          // NOTE: there is always only one element, since we use the foreach to extract the objects field name and value
2023
          $entity_reference = $entity_reference_list[0];
2024
        }
2025 5446f047 Andreas Kohlbecker
        if ($tokens[0] == 'NameTypeDesignation') {
2026 f5e7f68e Andreas Kohlbecker
          foreach ($obj as $type_status => $entity_reference_list) {
2027
            $name_type_designation_refs[$type_status] = $entity_reference;
2028 5446f047 Andreas Kohlbecker
          }
2029 f5e7f68e Andreas Kohlbecker
        } else if ($tokens[0] == 'FieldUnit'){
2030 5446f047 Andreas Kohlbecker
          $field_unit_uuids[] = $tokens[1];
2031 f5e7f68e Andreas Kohlbecker
          $specimen_type_designation_refs[$type_status] = $entity_reference;
2032
        } else {
2033
          drupal_set_message("Unimplemented type: " . $tokens[0], 'error');
2034 5446f047 Andreas Kohlbecker
        }
2035
      }
2036
      if (count($name_type_designation_refs) > 0) {
2037
        $render_array['name_type_designations'] = compose_name_type_designations($name_type_designation_refs);
2038
        $render_array['name_type_designations']['#prefix'] = '<p class="name_type_designations">';
2039
        $render_array['name_type_designations']['#suffix'] = '</p>';
2040 ed4d07fb Andreas Kohlbecker
        $render_array['name_type_designations']['#weight'] = 20;
2041 5446f047 Andreas Kohlbecker
      }
2042
      if (count($field_unit_uuids) > 0) {
2043 f5e7f68e Andreas Kohlbecker
        $render_array['specimen_type_designations'] = compose_specimen_type_designations($specimen_type_designation_refs);
2044
        // $render_array['specimen_table'] = compose_specimen_table($field_unit_uuids);
2045 5446f047 Andreas Kohlbecker
        // below citation (weight=20)
2046 ed4d07fb Andreas Kohlbecker
        $render_array['specimen_table']['#weight'] = 21;
2047 5446f047 Andreas Kohlbecker
      }
2048
    }
2049 84e766b2 Andreas Kohlbecker
  }
2050
2051 ed4d07fb Andreas Kohlbecker
  // citation
2052
  if ($with_citation) {
2053
    $render_array['citation'] = markup_to_render_array(
2054
      "<p class=\"citation " . html_class_attribute_ref(new TypedEntityReference("Reference", $registration_dto->citationUuid)) . "\">"
2055
      . l($registration_dto->bibliographicInRefCitationString, path_to_reference($registration_dto->citationUuid))
2056
      . "</p>",
2057
      50);
2058
  }
2059
2060
  // registration date and office
2061
  if($registration_dto->registrationDate){
2062
    $date_string = format_datetime($registration_dto->registrationDate);
2063
    if(isset($registration_dto->institutionTitleCache) && $registration_dto->institutionTitleCache){
2064
      $registration_date_insitute_markup =
2065
        t("Registration on @date in @institution", array(
2066
          '@date' => $date_string,
2067
          '@institution' => $registration_dto->institutionTitleCache,
2068
        ));
2069
    } else {
2070
      $registration_date_insitute_markup =
2071
        t("Registration on @date", array(
2072
          '@date' => $date_string
2073
        ));
2074
    }
2075
    $render_array['publication_date_and_office'] = markup_to_render_array(
2076
      '<p class"date-and-institute">'.$registration_date_insitute_markup . '</p>',
2077
      100);
2078
  }
2079
2080 6cbefddc Andreas Kohlbecker
  return $render_array;
2081
}
2082 e0d3e148 Andreas Kohlbecker
2083
2084 6657531f Andreas Kohlbecker
/**
2085 84b13813 Andreas Kohlbecker
 * Creates a page with the advance search form.
2086
 *
2087
 * NOTE: The advance search form allows searching for taxa.
2088 6657531f Andreas Kohlbecker
 */
2089
function cdm_dataportal_view_search_advanced() {
2090
  drupal_set_title(t('Advanced search'), PASS_THROUGH);
2091
  return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
2092
}
2093
2094
/**
2095 84b13813 Andreas Kohlbecker
 * Creates a page with the search form for searching by taxon descriptions.
2096 6657531f Andreas Kohlbecker
 */
2097
function cdm_dataportal_view_search_taxon_by_description() {
2098 6441ddfb Andreas Kohlbecker
  drupal_set_title(t('Search by factual data'), PASS_THROUGH);
2099 6657531f Andreas Kohlbecker
  return drupal_get_form('cdm_dataportal_search_taxon_by_description_form');
2100
}
2101
2102
/**
2103
 * Executes the search and generates the result list of taxa.
2104
 */
2105
function cdm_dataportal_view_search_results_taxon() {
2106
2107 24797d7f Andreas Kohlbecker
  $taxonPager = cdm_dataportal_search_taxon_execute();
2108 6657531f Andreas Kohlbecker
2109 f19f47fa Andreas Kohlbecker
  $showThumbnails = do_showThumbnails();
2110
2111
  $setSessionUri = url('cdm_api/setvalue/session', array(
2112
      'query' => array('var' => '[pageoption][searchtaxa][showThumbnails]', 'val' => ''),
2113
  ));
2114
2115
  drupal_add_js('jQuery(document).ready(function() {
2116
2117
      // init
2118
      if(' . $showThumbnails . ' == 1){
2119 1d69a96c Andreas Kohlbecker
          jQuery(\'.media_gallery\').show(20);
2120
      } else {
2121
          jQuery(\'.media_gallery\').hide(20);
2122
      }
2123
2124
      // add change handler
2125
      jQuery(\'#showThumbnails input.showThumbnails\').change(
2126 f19f47fa Andreas Kohlbecker
      function(event){
2127 1d69a96c Andreas Kohlbecker
        var state = 0;
2128
        if(jQuery(this).is(\':checked\')){
2129
          jQuery(\'.media_gallery\').show(20);
2130
          state = 1;
2131
        } else {
2132
          jQuery(\'.media_gallery\').hide(20);
2133
        }
2134
        // store state in session variable
2135
        var uri = \'' . $setSessionUri . '\' + state;
2136
        jQuery.get(uri);
2137
      });
2138 78e178e2 Andreas Kohlbecker
  });',
2139 f19f47fa Andreas Kohlbecker
  array('type' => "inline", 'scope' => JS_DEFAULT));
2140
2141 78e178e2 Andreas Kohlbecker
  drupal_set_title(t('Search results'), PASS_THROUGH);
2142
2143 0a1dc066 Andreas Kohlbecker
  return theme('cdm_search_taxa_results', array(
2144 6657531f Andreas Kohlbecker
    'pager' => $taxonPager,
2145
    'path' => 'cdm_dataportal/search/results/taxon',
2146
    ));
2147
}
2148
2149 a6ae799b Andreas Kohlbecker
/**
2150
 * Executes the search for registrations and generates the result list..
2151
 */
2152 efbff4b3 Andreas Kohlbecker
function cdm_dataportal_view_search_registrations_results($mode = 'filter') {
2153
2154
  switch($mode ){
2155
    case 'taxongraph':
2156
      $block = block_load('cdm_dataportal', 'registrations_search_taxongraph');
2157
      $registration_pager = cdm_dataportal_search_registrations_taxongraph_execute();
2158
      break;
2159
    case 'filter':
2160
    default:
2161
      $block = block_load('cdm_dataportal', 'registrations_search_filter');
2162
      $registration_pager = cdm_dataportal_search_registrations_filter_execute();
2163
  }
2164
  $block->title = null;
2165 a6ae799b Andreas Kohlbecker
2166
  drupal_set_title(t('Search registrations'), PASS_THROUGH);
2167
2168 efbff4b3 Andreas Kohlbecker
  $render_array = _block_get_renderable_array(_block_render_blocks(array($block)));
2169 24797d7f Andreas Kohlbecker
  $registrations_pager_array = compose_registrations_search_results($registration_pager);
2170 a6ae799b Andreas Kohlbecker
  $render_array = array_merge($render_array, $registrations_pager_array);
2171
2172
  return $render_array;
2173
}
2174
2175
2176 94550ff9 Andreas Kohlbecker
/**
2177
 * Provides the standart image wich indicated a loading process
2178
 *
2179
 * @return string
2180
 *  The img html tag
2181
 */
2182
function loading_image_html() {
2183
  return '<img class="loading" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal')
2184
    . '/images/loading_circle_grey_16.gif" style="display:none;">';
2185
}
2186
2187 78e178e2 Andreas Kohlbecker
/**
2188
 * Returns the state of the the showThumbnails flag set in the
2189
 * users session ($_SESSION['pageoption']['searchtaxa']['showThumbnails']).
2190
 *
2191 c5ed6357 Andreas Kohlbecker
 * @return boolean
2192 78e178e2 Andreas Kohlbecker
 *    returns 1 if the flag is set
2193
 */
2194
function do_showThumbnails() {
2195
  static $showThumbnails = null;
2196
2197
  if($showThumbnails == null) {
2198 c5ed6357 Andreas Kohlbecker
    $showThumbnails = 0;
2199
    if (!isset($_SESSION['pageoption']['searchtaxa']['showThumbnails'])) {
2200
      $showThumbnails = 0;
2201
      $search_gallery_settings = variable_get(CDM_DATAPORTAL_SEARCH_GALLERY_NAME, null);
2202
      $showThumbnails = is_array($search_gallery_settings)
2203
        && isset($search_gallery_settings['cdm_dataportal_show_taxon_thumbnails'])
2204
        && (
2205
            $search_gallery_settings['cdm_dataportal_show_taxon_thumbnails'] +
2206
            $search_gallery_settings['cdm_dataportal_show_synonym_thumbnails'] +
2207
            $search_gallery_settings['cdm_dataportal_show_thumbnail_captions'] > 0
2208
            )
2209
         ? 1 : 0;
2210 f19f47fa Andreas Kohlbecker
2211 c5ed6357 Andreas Kohlbecker
       drupal_array_set_nested_value($_SESSION, array('pageoption', 'searchtaxa', 'showThumbnails'), $showThumbnails);
2212
    }
2213 f19f47fa Andreas Kohlbecker
    $showThumbnails = $_SESSION['pageoption']['searchtaxa']['showThumbnails'];
2214
    if (!is_numeric($showThumbnails)) {
2215
      $showThumbnails = 1;
2216
    }
2217 78e178e2 Andreas Kohlbecker
  }
2218
2219
  return $showThumbnails;
2220
}
2221
2222 6657531f Andreas Kohlbecker
2223 a6ae799b Andreas Kohlbecker
2224 6657531f Andreas Kohlbecker
/* ====================== other functions ====================== */
2225
/**
2226
 * Creates a URL to the taxon page specified by the $uuid parameter.
2227
 *
2228
 * The URL will be prepended with a path element to a specific taxon page tab.
2229
 *
2230
 * This tab is either taken from the CDM_DATAPORTAL_DEFAULT_TAXON_TAB which can
2231
 * be set globally in the administrative settings or individually in the user
2232
 * profile. If the CDM_DATAPORTAL_DEFAULT_TAXON_TAB value is set to LAST_VISITED_TAB
2233
 * the last portal will stay on this last tab.
2234
 *
2235
 * A third option is offerered by the $page_tab parameter which allows overwriting this
2236
 * internal mechanism by a specific value.
2237
 *
2238
 * @param string $uuid
2239
 *   The UUID of the taxon.
2240
 * @param string $page_tab
2241
 *   Overwriting the preset mechanism by defining specific value for the
2242
 *   taxon page tab.
2243
 *
2244
 * @return string
2245
 *   The created URL.
2246
 */
2247
function path_to_taxon($uuid, $page_tab = FALSE) {
2248
2249
  $tab = get_default_taxon_tab();
2250
  $values = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
2251
2252
  if (!$uuid) {
2253
    return FALSE;
2254
  }
2255
2256
  if ($page_tab) {
2257
    return 'cdm_dataportal/taxon/' . $uuid . '/' . $page_tab;
2258
  }
2259
  elseif (!$tab || strtolower($tab) == 'general') {
2260
    return 'cdm_dataportal/taxon/' . $uuid;
2261
  }
2262
  elseif (get_last_taxon_page_tab() &&   $tab == $values[CDM_DATAPORTAL_LAST_VISITED_TAB_ARRAY_INDEX]) {
2263
    return 'cdm_dataportal/taxon/' . $uuid . '/' . get_last_taxon_page_tab();
2264
  }
2265
  else {
2266
    return 'cdm_dataportal/taxon/' . $uuid . '/' . strtolower($tab);
2267
  }
2268
}
2269
2270 9d37c9d9 Patric Plitzner
function path_to_specimen($uuid) {
2271
2272
    if (!$uuid) {
2273
        return FALSE;
2274
    }
2275
    else {
2276
        return 'cdm_dataportal/specimen/' . $uuid;
2277
    }
2278
}
2279
2280 30845bda Andreas Kohlbecker
function path_to_named_area($uuid) {
2281
2282
  if (!$uuid) {
2283
    return FALSE;
2284
  }
2285
  else {
2286
    return 'cdm_dataportal/named_area/' . $uuid;
2287
  }
2288
}
2289
2290 6657531f Andreas Kohlbecker
/**
2291 630c7044 Andreas Kohlbecker
 * Creates a URL to show a synonmy in the according taxon page.
2292
 *
2293
 * The URL will point to the synonymy tab of the taxon page of the accepted taxon given as parameter $acceptedUuid.
2294
 * The resulting URI will include query parameters to highlight the synonym, and to optionally display
2295
 * the accepted taxons name in aform like "Foo bar is accepted taxon for Ree doo". The URI will also
2296
 * include the sysnonym uuid as fragment in order to let the browser scroll to the according location
2297
 * in the page
2298
 *
2299
 * @param string $synonymUuid
2300
 *    The uuid of the synonym
2301
 * @param string $acceptedUuid
2302
 *    The uuid of the according accepted taxon
2303
 * @return string
2304
 *    The URL to show a synonmy in the according taxon page
2305 6657531f Andreas Kohlbecker
 */
2306
function uri_to_synonym($synonymUuid, $acceptedUuid) {
2307
  $acceptedPath = path_to_taxon($acceptedUuid, "synonymy");
2308 d071bed7 Andreas Kohlbecker
  return url($acceptedPath, array(
2309
      'query' => array(
2310
        // highlite the synony in the synonymy
2311
        'highlite' => $synonymUuid,
2312
        // the taxon page is refered from a synonym and the synonym can optionally be named in the page title
2313
        // see theme_taxon_page_title()
2314
        'acceptedFor' => $synonymUuid
2315 630c7044 Andreas Kohlbecker
      ),
2316
      'fragment' => $synonymUuid
2317 d071bed7 Andreas Kohlbecker
  ));
2318
2319 6657531f Andreas Kohlbecker
}
2320
2321
/**
2322 e0d3e148 Andreas Kohlbecker
 * Composes the drupal path to the key identified by the uuid.
2323 b8bfa4bd Andreas Kohlbecker
 *
2324
 * @param string $keyType
2325
 *    the key typer corresponds to the specific class of the CDM
2326
 *    IdentificationKey. Possible values are
2327
 *      -PolytomousKey
2328
 *      -MultimediaKey
2329
 *      - ...
2330 e0d3e148 Andreas Kohlbecker
 * @param string $keyUuid
2331 b8bfa4bd Andreas Kohlbecker
 *   The UUID of the key
2332 6657531f Andreas Kohlbecker
 */
2333
function path_to_key($keyType, $keyUuid) {
2334
  if (!$keyUuid || !$keyType) {
2335
    return FALSE;
2336
  }
2337
  $keyType{0} = strtolower($keyType{0});
2338
  return "cdm_dataportal/" . $keyType . "/$keyUuid";
2339
}
2340
2341
/**
2342 e0d3e148 Andreas Kohlbecker
 * Composes the drupal path to the reference identified by the uuid.
2343
 *
2344
 * @param $uuid string String representation of the registration entity uuid.
2345
 *
2346
 * @return string
2347
 *  The drupal path
2348
 *
2349 6657531f Andreas Kohlbecker
 */
2350
function path_to_reference($uuid) {
2351
  if (!$uuid) {
2352
    return FALSE;
2353
  }
2354
  return 'cdm_dataportal/reference/' . $uuid;
2355
}
2356
2357 e0d3e148 Andreas Kohlbecker
/**
2358
 * Composes the drupal path to the reference identified by the uuid.
2359
 *
2360
 * @param string $identifier
2361
 *  The persistent identifier of the registration entity (Registration.identifier).
2362
 * @return string
2363
 *  The drupal path
2364
 */
2365
function path_to_registration($identifier) {
2366
2367
  return 'cdm_dataportal/registration/' . urlencode($identifier);
2368
}
2369
2370 6657531f Andreas Kohlbecker
/**
2371 51b04faf Andreas Kohlbecker
 * Creates the path to a cdm_dataportal taxon name page.
2372
 *
2373 e0d3e148 Andreas Kohlbecker
 * @param string $taxon_name_uuid
2374
 *   The uuid as string of the CDM TaxonName to show a name page for
2375
 * @param string $taxon_to_hide_uuid
2376
 *   The uuid as string of a taxon which should not be displayed in the taxon list
2377 d4ea0dd9 Andreas Kohlbecker
 * @param string $highlited_synonym_uuid
2378 e0d3e148 Andreas Kohlbecker
 *   Optional parameter which takes another taxon uuid, if given the
2379
 *   target taxon pages will show the synonymy tab where the taxon
2380
 *   referenced by the $highlite_synonym_uuid will be highlighted
2381 51b04faf Andreas Kohlbecker
 *   in case it is found on this page.
2382
 *
2383 e0d3e148 Andreas Kohlbecker
 * @return string
2384
 *  URI path element as string
2385 6657531f Andreas Kohlbecker
 */
2386 d4ea0dd9 Andreas Kohlbecker
function path_to_name($name_uuid, $taxon_to_hide_uuid = NULL, $highlited_synonym_uuid  = NULL) {
2387 6657531f Andreas Kohlbecker
  $res = FALSE;
2388
  if ($name_uuid) {
2389
    $res = 'cdm_dataportal/name/' . $name_uuid;
2390
  }
2391 51b04faf Andreas Kohlbecker
  if($taxon_to_hide_uuid){
2392
    $res .= '/' . $taxon_to_hide_uuid;
2393 d4ea0dd9 Andreas Kohlbecker
    if($highlited_synonym_uuid){
2394
      $res .= '/' . $highlited_synonym_uuid;
2395 51b04faf Andreas Kohlbecker
    }
2396
  }
2397 6657531f Andreas Kohlbecker
  return $res;
2398
}
2399
2400
/**
2401 e0d3e148 Andreas Kohlbecker
 * Composes the drupal path to the media entity identified by the uuid.
2402
 *
2403
 * @param string $uuid
2404
 *  The persistent identifier of the entity entity
2405
 * @return string
2406
 *  The drupal path
2407 6657531f Andreas Kohlbecker
 */
2408
function path_to_media($uuid, $representaion_uuid = FALSE, $partId = FALSE) {
2409
  if (!$uuid) {
2410
    return FALSE;
2411
  }
2412
  $out = 'cdm_dataportal/media/' . $uuid;
2413
  if ($representaion_uuid) {
2414
    $out .= '/' . $representaion_uuid;
2415 a867b774 Andreas Kohlbecker
    if (is_numeric($partId)) {
2416 6657531f Andreas Kohlbecker
      $out .= '/' . $partId;
2417
    }
2418
  }
2419
  return $out;
2420
}
2421
2422
/**
2423
 * Compares thisRank with thatRank.
2424
 *
2425
 * Returns a negative integer, zero, or a positive integer
2426
 * as the of thisRank is higher than, equal to, or lower than thatRank.
2427
 * e.g:
2428
 * <ul>
2429
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
2430
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
2431
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
2432
 * </ul>
2433
 * <p>
2434
 * This compare logic of the underlying webservice is the
2435
 * <b>inverse logic</b> of the the one implemented in
2436
 * java.lang.Comparable#compareTo(java.lang.Object)
2437
 *
2438
 * @param $thisRankUuid
2439
 * @param $thatRankUuid
2440
 *
2441
 * @return int
2442
 *   A negative integer, zero, or a positive integer
2443
 *   as the thisRank is lower than, equal to, or higher than thatRank.
2444
 */
2445
function rank_compare($thisRankUuid, $thatRankUuid) {
2446
  $result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
2447
  return $result->Integer;
2448
}
2449
2450
/**
2451
 * Composes an HTML element class attribute value composed of
2452 4b3be949 Andreas Kohlbecker
 * the short-name of the cdm class and the uuid of the entity.
2453
 * This class attribute should be used wherever an cdm-entity is rendered.
2454
 *
2455
 * In case of Taxon entities or TaxonNodeDTOs the secReference is also added
2456
 * to the class attributes as 'sec_uuid:<uuid>'. In case of TaxonNodeDTOs the
2457
 * Taxon uuid is added also as taxon_uuid:<uuid>
2458 6657531f Andreas Kohlbecker
 *
2459
 * These according class selectors in css must be escaped, eg:
2460
 *    .cdm\:TextData
2461
 *
2462 a6ae799b Andreas Kohlbecker
 * @param $cdm_entity
2463
 *    A CDM entity, TaxonNodeDTO or TypedEntityReference
2464 6657531f Andreas Kohlbecker
 */
2465 a6ae799b Andreas Kohlbecker
function html_class_attribute_ref($cdm_entity) {
2466 6657531f Andreas Kohlbecker
2467 4b3be949 Andreas Kohlbecker
  $attributes = '';
2468 a6ae799b Andreas Kohlbecker
  if (is_cdm_entity($cdm_entity)) {
2469
    $attributes =  "cdm:" . $cdm_entity->class . " uuid:" . $cdm_entity->uuid;
2470
  } else if($cdm_entity->class == 'TypedEntityReference') {
2471
    $attributes =  "cdm:" . $cdm_entity->type . " uuid:" . $cdm_entity->uuid;
2472 4b3be949 Andreas Kohlbecker
  }
2473 a6ae799b Andreas Kohlbecker
  if($cdm_entity->class == 'TaxonNodeDto'){
2474
    $attributes .= " taxon_uuid:"  . $cdm_entity->taxonUuid . " sec_uuid:"  . $cdm_entity->secUuid;;
2475 4b3be949 Andreas Kohlbecker
  }
2476 a6ae799b Andreas Kohlbecker
  if($cdm_entity->class == 'Taxon' && isset($cdm_entity->sec->uuid)){
2477
    $attributes .= " sec_uuid:"  . $cdm_entity->sec->uuid;
2478 6657531f Andreas Kohlbecker
  }
2479 4b3be949 Andreas Kohlbecker
  return $attributes;
2480 6657531f Andreas Kohlbecker
}
2481
2482
2483
/**
2484 a0e3ae5c Andreas Kohlbecker
 * Creates a short version of a taxonname.
2485 6657531f Andreas Kohlbecker
 *
2486 e90899ac Andreas Kohlbecker
 * The short name is created by using the taggedTitle field of
2487 a0e3ae5c Andreas Kohlbecker
 * TaxonNodeDTO instances.
2488 e90899ac Andreas Kohlbecker
 * If the taggedTitle if empty the fullname will be returned.
2489 6657531f Andreas Kohlbecker
 *
2490 a0e3ae5c Andreas Kohlbecker
 * @param object $taxonNodeDTO
2491
 *   A TaxonNodeDTO object
2492 6657531f Andreas Kohlbecker
 *
2493
 * @return string
2494
 */
2495 a0e3ae5c Andreas Kohlbecker
function cdm_dataportal_shortname_of($taxonNodeDTO) {
2496
2497 6657531f Andreas Kohlbecker
  $nameStr = '';
2498
2499 a0e3ae5c Andreas Kohlbecker
  normalize_tagged_text($taxonNodeDTO->taggedTitle);
2500 6657531f Andreas Kohlbecker
2501
  // Get all tagged text tokens of the scientific name.
2502 a0e3ae5c Andreas Kohlbecker
  foreach ($taxonNodeDTO->taggedTitle as $tagtxt) {
2503 6657531f Andreas Kohlbecker
    if ($tagtxt->type == 'name' || $tagtxt->type == 'rank') {
2504
      $nameStr .= ($nameStr ? ' ' : '') . $tagtxt->text;
2505
    }
2506
  }
2507
  $nameStr = trim($nameStr);
2508
2509
  if ($nameStr) {
2510
2511
    // Do not return short names for these.
2512 a0e3ae5c Andreas Kohlbecker
    if ($taxonNodeDTO->unplaced || $taxonNodeDTO->excluded) {
2513 6657531f Andreas Kohlbecker
      return $nameStr;
2514
    }
2515
2516
    /*
2517
    1st capture group (^[a-zA-Z]): First letter of uninomial.
2518
    Second capture group ([\p{L}]+): remaining letters of uninomial ([\p{L} = an UTF-8 letter).
2519
    Third capture group (\s+[^(\x2E]+\s+.+$|\s+[a-zA-Z]+$): letters of name,
2520
    but only matching if no '(' or '.' in second word of name,        ( \x2E = '.')
2521
    OR only one specific epithet \s+[\p{L}\x22\x2D\xD7]+$             (\x22= '"', \x2D='-', \xD7='×' )
2522
    */
2523
    $pattern = '/(^[a-zA-Z])([\p{L}]+)(\s+[^(\x2E]+\s+.+$|\s+[\p{L}\x22\x2D\xD7]+$)/u';
2524
    if (preg_match($pattern, $nameStr, $matches, PREG_OFFSET_CAPTURE)) {
2525
      return $matches[1][0] . "." . $matches[3][0];
2526
    }
2527
    else {
2528
      return $nameStr;
2529
    }
2530
  }
2531
  else {
2532 a0e3ae5c Andreas Kohlbecker
    return $taxonNodeDTO->titleCache;
2533 6657531f Andreas Kohlbecker
  }
2534
}
2535
2536
/**
2537
 * Check if a taxon is accepted by the current taxonomic tree.
2538
 *
2539
 * @param mixed $taxon
2540
 *   The Taxon obkect to check.
2541
 *
2542
 * @return bool
2543
 *   Returns TRUE if $taxon is accepted, FALSE otherwise.
2544
 */
2545
function _cdm_dataportal_acceptedByCurrentView($taxon) {
2546
2547 7663cd0b Andreas Kohlbecker
  $defaultTreeUuid = get_current_classification_uuid();
2548 6657531f Andreas Kohlbecker
2549
  if (isset($taxon->taxonNodes)) {
2550
    $taxonNodes = $taxon->taxonNodes;
2551
  }
2552
  else {
2553
    $taxonNodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, $taxon->uuid);
2554
  }
2555
2556
  if ($taxon->class == "Taxon" && isset($taxonNodes)) {
2557
    foreach ($taxonNodes as $node) {
2558 586fc780 Andreas Kohlbecker
      if (isset($node->classification)){
2559
        if(is_object($node->classification)) {
2560
          if ($node->classification->uuid == $defaultTreeUuid) {
2561
            return TRUE;
2562
          }
2563 6657531f Andreas Kohlbecker
        }
2564 586fc780 Andreas Kohlbecker
        else {
2565
          if ($node->classification == $defaultTreeUuid) {
2566
            return TRUE;
2567
          }
2568 6657531f Andreas Kohlbecker
        }
2569
      }
2570
    }
2571
  }
2572
2573
  return FALSE;
2574
}
2575
2576
/**
2577 54cfa718 Andreas Kohlbecker
 * Checks is the source has one of the given types.
2578
 *
2579
 * @param object $source
2580
 *   The original source entity
2581
 * @param array $types
2582
 *   An array of elementd of the OriginalSourceType enumeration
2583
 *   If not set the default will be used which is:
2584
 *    - Lineage
2585
 *    - PrimaryMediaSource
2586
 *    - PrimaryTaxonomicSource
2587
 *    - Unknown
2588
 *    - Other
2589
 * @return boolean
2590 6657531f Andreas Kohlbecker
 */
2591 54cfa718 Andreas Kohlbecker
  function _is_original_source_type($source, $types = null) {
2592
    // this is the default
2593
    // maybe this should also be put into the settings
2594
    static $default = array(
2595
      OriginalSourceType::Lineage,
2596
      OriginalSourceType::PrimaryMediaSource,
2597
      OriginalSourceType::PrimaryTaxonomicSource,
2598
      OriginalSourceType::Unknown,
2599
      OriginalSourceType::Other,
2600
    );
2601
2602
    if(!$types){
2603
      $types = $default;
2604
    }
2605
    return isset($source->type) && in_array($source->type, $types);
2606
  }
2607 6657531f Andreas Kohlbecker
2608
/**
2609
 * Collects all the media from a list of description elements.
2610
 *
2611
 * @param array $descriptionElements
2612
 *   The description elements from which to collect the media.
2613
 *
2614
 * @return array
2615
 *   The output with all the collected media.
2616
 */
2617
function cdm_dataportal_media_from_descriptionElements($descriptionElements) {
2618
2619
  $outArrayOfMedia = array();
2620
2621
  // Avoiding a warning box in Drupal for Flora Malesiana.
2622
  if (isset($descriptionElements) && is_array($descriptionElements)) {
2623
    foreach ($descriptionElements as $descriptionElement) {
2624
      if (isset($descriptionElement->media) && is_array($descriptionElement->media)) {
2625
        foreach ($descriptionElement->media as $media) {
2626
          if (is_object($media)) {
2627
            $outArrayOfMedia[] = $media;
2628
          }
2629
        }
2630
      }
2631
    }
2632
  }
2633
  return $outArrayOfMedia;
2634
}
2635
2636
/**
2637 7370f4f7 Andreas Kohlbecker
 * Fetches the list of visible annotations for each of the cdm entities and returns the footnote keys for them.
2638
 *
2639
 * The footnotes are passed to the FootnoteManager in order to store the annotations and to create the footnote keys.
2640
 *
2641
 * @see cdm_fetch_visible_annotations()
2642 6657531f Andreas Kohlbecker
 *
2643 65345976 Andreas Kohlbecker
 * @param array $cdm_entities
2644 6657531f Andreas Kohlbecker
 *   An array of CdmBase instances or a single instance.
2645 28c5c87a Andreas Kohlbecker
 * @param string $footnote_list_key_suggestion
2646 6657531f Andreas Kohlbecker
 *
2647 7370f4f7 Andreas Kohlbecker
 * @return array of footnote keys
2648 6657531f Andreas Kohlbecker
 */
2649 65345976 Andreas Kohlbecker
function cdm_annotations_as_footnotekeys($cdm_entities, $footnote_list_key_suggestion = NULL) {
2650 90597e37 Andreas Kohlbecker
2651 6657531f Andreas Kohlbecker
  $footNoteKeys = array();
2652
2653
  // Is argument cdmBase an array?
2654 65345976 Andreas Kohlbecker
  if (!is_array($cdm_entities)) {
2655 6657531f Andreas Kohlbecker
    $cdmBase_array = array();
2656 65345976 Andreas Kohlbecker
    $cdmBase_array[] = $cdm_entities;
2657 6657531f Andreas Kohlbecker
  }
2658
  else {
2659 65345976 Andreas Kohlbecker
    $cdmBase_array = $cdm_entities;
2660 6657531f Andreas Kohlbecker
  }
2661
2662
  // Getting the key for the footnotemanager.
2663 28c5c87a Andreas Kohlbecker
  if (isset($footnote_list_key_suggestion)) {
2664
    $footnote_list_key = $footnote_list_key_suggestion;
2665 6657531f Andreas Kohlbecker
  }
2666
  else {
2667 28c5c87a Andreas Kohlbecker
    $footnote_list_key = RenderHints::getFootnoteListKey() . '-annotations';
2668 6657531f Andreas Kohlbecker
  }
2669
2670
  // Adding the footnotes keys.
2671
  foreach ($cdmBase_array as $cdmBase_element) {
2672 7370f4f7 Andreas Kohlbecker
    $annotations = cdm_fetch_visible_annotations($cdmBase_element);
2673 6657531f Andreas Kohlbecker
    if (is_array($annotations)) {
2674
      foreach ($annotations as $annotation) {
2675 28c5c87a Andreas Kohlbecker
        $footNoteKeys[] = FootnoteManager::addNewFootnote($footnote_list_key, $annotation->text);
2676 6657531f Andreas Kohlbecker
      }
2677
    }
2678
  }
2679
2680
  return $footNoteKeys;
2681
}
2682
2683 d9763fd3 Andreas Kohlbecker
2684 6657531f Andreas Kohlbecker
/**
2685 d9763fd3 Andreas Kohlbecker
 * Creates a CDM Dynabox.
2686
 *
2687 c1f5d897 Andreas Kohlbecker
 * @param string $dynabox_id
2688 d9763fd3 Andreas Kohlbecker
 *   a uninque name for tha dynabox, using a cdm entity uuid as id is good practice.
2689 6657531f Andreas Kohlbecker
 * @param string $label
2690
 *   The clickable text to show.
2691
 * @param string $content_url
2692
 *   The cdm REST service request url wich will deliver the content to be shown
2693
 *   once the dynabox toggles open.
2694
 * @param string $theme
2695 1ce9afb7 Patric Plitzner
 *   The theme to be used for rendering the cdm REST service response with is
2696 6657531f Andreas Kohlbecker
 *   returned from the $content_url.
2697
 * @param string $link_alt_text
2698
 *   The value for the alt attribute of the dynabox link.
2699
 * @param array $enclosingtags
2700
 *   An array with two elements: $enclosingtags[0] will be used for the dynabox
2701
 *   element itself, $enclosingtags[1] is the tag to be used for the
2702 c1f5d897 Andreas Kohlbecker
 *   dynabox_content (optional)
2703 d9763fd3 Andreas Kohlbecker
 * @param array $attributes
2704
 * @param $content_element_selector
2705
 *   Optional jQuery selector which can be used to reference a dom element which should
2706
 *   be used as container for the content to be shown. The dynabox-<dynabox id>-content
2707
 *  element will be placed in this container.
2708
 *
2709 c1f5d897 Andreas Kohlbecker
 * @param string $open_callback
2710 1ce9afb7 Patric Plitzner
 *   optional javascript call back function to be triggered after toggling the box to
2711 c1f5d897 Andreas Kohlbecker
 *   the open state.
2712
 * @param string $close_callback
2713 1ce9afb7 Patric Plitzner
 *   optional javascript call back function to be triggered after toggling the box to
2714 c1f5d897 Andreas Kohlbecker
 *   the closed state.
2715 d9763fd3 Andreas Kohlbecker
 * @return string Returns HTML for a dynabox.
2716
 * Returns HTML for a dynabox.
2717 6657531f Andreas Kohlbecker
 */
2718 d9763fd3 Andreas Kohlbecker
function cdm_dynabox($dynabox_id, $label, $content_url, $theme, $link_alt_text,
2719
                     $enclosingtags = array('li', 'ul'), $attributes = array(),
2720
                     $content_element_selector = null,
2721
                     $open_callback = 'function(){}', $close_callback = 'function(){}' ) {
2722 6657531f Andreas Kohlbecker
  $out = '';
2723 c1f5d897 Andreas Kohlbecker
2724
  // check for plain class attribute string
2725
  $dynabox_id = preg_replace('/[^a-zA-Z0-9\-]/', '', $dynabox_id);
2726
2727 9c9d11f7 Andreas Kohlbecker
  if(!array_key_exists('class', $attributes)) {
2728
    $attributes['class'] = array();
2729
  }
2730 d9763fd3 Andreas Kohlbecker
  $attributes['id'][] = 'dynabox-' . $dynabox_id;
2731 9c9d11f7 Andreas Kohlbecker
  $dynabox_attributes = drupal_attributes($attributes);
2732
2733
2734 b386ae48 Andreas Kohlbecker
  _add_js_domEvent(); // requires domEvent.js
2735 6657531f Andreas Kohlbecker
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cdm_dynabox.js');
2736 c1f5d897 Andreas Kohlbecker
  drupal_add_js("
2737 94bb8db6 Andreas Kohlbecker
  jQuery(document).ready(
2738
      function() {
2739
        dynabox('". $dynabox_id ."',
2740
          {
2741
            open_callback: " . $open_callback .",
2742 d9763fd3 Andreas Kohlbecker
            close_callback: " . $close_callback .
2743
            ($content_element_selector ? ",\n content_container_selector: '" . $content_element_selector . "'" : "") . "
2744 94bb8db6 Andreas Kohlbecker
          }
2745
        );
2746
      }
2747
   );",
2748
   array(
2749
    'type'=>'inline',
2750
    'scope'=>'footer'
2751
    )
2752
  );
2753 6657531f Andreas Kohlbecker
2754 9c9d11f7 Andreas Kohlbecker
2755 6657531f Andreas Kohlbecker
  $cdm_proxy_url = url('cdm_api/proxy/' . urlencode($content_url) . "/$theme");
2756 9c9d11f7 Andreas Kohlbecker
  $out .= '<!-- dynabox for ' . $content_url . ' -->';
2757 7cc085da Andreas Kohlbecker
  $out .= '<' . $enclosingtags[0] . ' ' .  $dynabox_attributes. '><a href="' . $cdm_proxy_url . '" class="label" alt="' . t('@link-alt-text', array('@link-alt-text' => $link_alt_text)) . '">' . $label . '</a>';
2758 d9763fd3 Andreas Kohlbecker
  $out .= '  <' . $enclosingtags[1] . ' id="dynabox-' . $dynabox_id . '-content">';
2759
  $out .= '    <' . $enclosingtags[0] . ' class="dynabox-content-inner">' . loading_image_html() . '</' . $enclosingtags[0] . '>';
2760
  $out .= '    </' . $enclosingtags[1] . '>';
2761
  $out .= '  </' . $enclosingtags[0] . '>';
2762 9c9d11f7 Andreas Kohlbecker
  $out .= '<!-- dynabox end -->';
2763 6657531f Andreas Kohlbecker
  return $out;
2764
}
2765
2766
/**
2767
 * Checks whether a feature has any description elements.
2768
 *
2769
 * @param mixed $featureNode
2770
 *   A feature node as produced by the function _mergeFeatureTreeDescriptions().
2771
 *
2772
 * @see _mergeFeatureTreeDescriptions()
2773
 *
2774
 * @return bool
2775
 *   Returns TRUE if the given $featureNode or any of its subordinate nodes
2776
 *   contains at least one non empty TextData or at least one DescriptionElement
2777
 *   of an other type. A TextData element holding a multilanguageText or a
2778
 *   source reference is considered to be not empty.
2779 159cd9bb Andreas Kohlbecker
 *
2780
 * @TODO this function may have become obsolete by the new method of detecting empty blocks,
2781 1b756c5f Andreas Kohlbecker
 *       see $block_content_is_not_empty in make_feature_block_list() and
2782 0ebb6efc Andreas Kohlbecker
 *       $feature_block_has_content in compose_feature_block_items_generic
2783 6657531f Andreas Kohlbecker
 */
2784 f19f47fa Andreas Kohlbecker
function has_feature_node_description_elements($featureNode) {
2785 6657531f Andreas Kohlbecker
2786
  if (isset($featureNode->descriptionElements) && is_array($featureNode->descriptionElements) && count($featureNode->descriptionElements) > 0) {
2787 092744e1 Andreas Kohlbecker
    if(!isset($featureNode->descriptionElements['#type'])){ // #type is used to identify e.g. DTO elements: '#type' => 'DTO'
2788
      foreach ($featureNode->descriptionElements as $descriptionElement) {
2789 159cd9bb Andreas Kohlbecker
        if ($descriptionElement->class != "TextData" || isset($descriptionElement->multilanguageText_L10n->text)
2790
          && $descriptionElement->multilanguageText_L10n->text != ''
2791
          || isset($descriptionElement->sources[0])
2792
          || isset($descriptionElement->media[0]) ) {
2793 092744e1 Andreas Kohlbecker
          return TRUE;
2794
        }
2795 6657531f Andreas Kohlbecker
      }
2796
    }
2797
  }
2798 092744e1 Andreas Kohlbecker
  else if (isset($featureNode->childNodes) && is_array($featureNode->childNodes)) {
2799 fae36a2a Andreas Kohlbecker
    foreach ($featureNode->childNodes as $child) {
2800 f19f47fa Andreas Kohlbecker
      if (has_feature_node_description_elements($child)) {
2801 6657531f Andreas Kohlbecker
        return TRUE;
2802
      }
2803
    }
2804
  }
2805
  return FALSE;
2806
}
2807 f26245c8 Andreas Kohlbecker
2808
/**
2809
 * Checks if the current page is a valid taxon portal page and responds with HTTP status 404 (not found) otherwise
2810
 *
2811
 * @param $chapter
2812
 *   The taxon page chapter or part
2813
 */
2814
function cdm_check_valid_taxon_page($chapter){
2815
  static $taxon_tabs = null;
2816
2817
  cdm_check_valid_portal_page();
2818
2819
  if($taxon_tabs == null){
2820
    $taxon_tabs = array('all', 'description');
2821
    foreach(get_taxon_tabs_list() as $tab){
2822
      $taxon_tabs[] = strtolower($tab);
2823
    }
2824
  }
2825
2826
  if(!in_array($chapter, $taxon_tabs)){
2827
    // oops this is not a valid chapter name
2828 b4a5c2be Andreas Kohlbecker
    http_response_code(404); // 404 = Not Found
2829 f26245c8 Andreas Kohlbecker
  }
2830
2831
}
2832
2833
/**
2834 e0d3e148 Andreas Kohlbecker
 * Checks if the current page is a valid portal page and responds with HTTP status 404 (not found) otherwise.
2835
 * The test applied by default it a check for the query parameter 'q' ending with a file suffix like '*.*'
2836 f26245c8 Andreas Kohlbecker
 *
2837 e0d3e148 Andreas Kohlbecker
 * @param $preg_pattern
2838
 *   Optional regular expression pattern to be used in preg_match().
2839 f26245c8 Andreas Kohlbecker
 */
2840 e0d3e148 Andreas Kohlbecker
function cdm_check_valid_portal_page($preg_pattern = null){
2841 595de9f0 Andreas Kohlbecker
  $ends_with_file_suffix_pattern = '/\/[^\.\/]*[\.][^\.\/]*$/';
2842 e0d3e148 Andreas Kohlbecker
  if($preg_pattern === null){
2843
    $preg_pattern = $ends_with_file_suffix_pattern;
2844
  }
2845
  if(preg_match($preg_pattern, $_GET['q'])){
2846 f26245c8 Andreas Kohlbecker
    // oops this urls ends with a file_suffix and thus does not refer to a portal page
2847 b4a5c2be Andreas Kohlbecker
    http_response_code(404); // 404 = Not Found
2848 595de9f0 Andreas Kohlbecker
    exit('HTTP 404');
2849 f26245c8 Andreas Kohlbecker
  }
2850
}
2851 b2b9e057 Andreas Kohlbecker
2852
/**
2853
 * Generates the diff of the texts and presents it in a HTML diff viewer.
2854
 *
2855
 * @param $text_a
2856
 * @param $text_b
2857
 * @return string
2858
 */
2859
function diff_viewer($text_a, $text_b) {
2860
2861
  static $diff_viewer_count = 0;
2862
2863
  $element_id = 'part_definitions_diff_' . $diff_viewer_count++;
2864
2865
  // http://code.stephenmorley.org/php/diff-implementation/
2866
  module_load_include('php', 'cdm_dataportal', 'lib/class.Diff');
2867
  drupal_add_css(drupal_get_path('module',
2868
      'cdm_dataportal') . '/css/diff.css');
2869 087bb473 Andreas Kohlbecker
  _add_jquery_ui();
2870 b2b9e057 Andreas Kohlbecker
  drupal_add_js(
2871
    'jQuery(document).ready( function(){
2872
        jQuery(\'#' . $element_id . '\').accordion({
2873
        collapsible: true,
2874
        active: false,
2875
        fillSpace: true,
2876
        }).children(\'div\').css({ \'height\': \'auto\' });
2877
        jQuery(\'#' . $element_id . ' table.diff\').prepend(\'<thead><tr><th>Default</th><th>User defined<th></th><tr></thead>\');
2878
     });'
2879
    , array(
2880
    'type' => 'inline',
2881
    'scope' => 'footer'
2882
  ));
2883
2884
  $diff = Diff::compare($text_a,
2885
    $text_b);
2886
  $diff_viewer_markup = '<div id="' . $element_id . '"><h3>View Diff</h3><div>'
2887
    . Diff::toTable($diff, '', '')
2888
    . '</div></div>';
2889
  return $diff_viewer_markup;
2890
}
2891