Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / modules / cdm_dataportal / cdm_dataportal.module @ a9a297f6

History | View | Annotate | Download (96.4 KB)

1
<?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
  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
  module_load_include('inc', 'cdm_dataportal', 'includes/footnotes');
27
  module_load_include('inc', 'cdm_dataportal', 'includes/agent');
28
  module_load_include('inc', 'cdm_dataportal', 'includes/name');
29
  module_load_include('inc', 'cdm_dataportal', 'includes/taxon');
30
  module_load_include('inc', 'cdm_dataportal', 'includes/taxon-node');
31
  module_load_include('inc', 'cdm_dataportal', 'includes/references');
32
  module_load_include('inc', 'cdm_dataportal', 'includes/pages');
33
  module_load_include('inc', 'cdm_dataportal', 'includes/media');
34
  module_load_include('inc', 'cdm_dataportal', 'includes/maps');
35
  module_load_include('inc', 'cdm_dataportal', 'includes/occurrences');
36
  module_load_include('inc', 'cdm_dataportal', 'includes/descriptions');
37
  module_load_include('inc', 'cdm_dataportal', 'includes/pre-drupal8');
38

    
39
  module_load_include('inc', 'cdm_dataportal', 'theme/theme_registry');
40
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.common');
41
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.descriptions');
42
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.media');
43
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.occurrence');
44
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.page');
45
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.taxon');
46
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.name');
47
  module_load_include('theme', 'cdm_dataportal', 'theme/cdm_dataportal.references');
48

    
49
  module_load_include('php', 'cdm_dataportal', 'classes/footnotemanager');
50
  module_load_include('php', 'cdm_dataportal', 'classes/footnote');
51
  module_load_include('php', 'cdm_dataportal', 'classes/footnotekey');
52
  module_load_include('php', 'cdm_dataportal', 'classes/renderhints');
53

    
54

    
55
  /* ============================ java script functions ============================= */
56

    
57

    
58
  /**
59
  * loads external java script files asynchronously.
60
  *
61
  * @param unknown_type $script_url
62
  */
63
  function drupal_add_js_async($script_url, $callback){
64

    
65
    drupal_add_js("
66
          jQuery(document).ready(function() {
67
            jQuery.ajax({
68
              url: '" . $script_url . "',
69
              dataType: 'script',
70
              cache: true, // otherwise will get fresh copy every page load
71
              success: function() {
72
                    " . $callback . "
73
              }
74
            });
75
          });"
76
    , 'inline');
77
  }
78

    
79
  /**
80
   */
81
  function drupal_add_js_rowToggle($tableId){
82

    
83
      drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/table_modification.js');
84
      drupal_add_js('jQuery(document).ready(function(){
85
          addRowToggle("' . $tableId . '");
86
      });
87
      ', array('type' => 'inline'));
88
  }
89

    
90
  /**
91
   * @param unknown_type $link_element_selector
92
   * @param unknown_type $progress_element_selector
93
   */
94
  function _add_js_cdm_ws_progressbar($link_element_selector, $progress_element_selector){
95

    
96
    $callback = "jQuery('" . $link_element_selector . "').cdm_ws_progress('" . $progress_element_selector . "');";
97

    
98
    drupal_add_js_async(variable_get('cdm_webservice_url', '').'js/cdm_ws_progress.js', $callback);
99

    
100
    //   drupal_add_js("
101
    //   	  if (Drupal.jsEnabled) {
102
    //         $(document).ready(function() {
103
    //       		$('" . $link_element_selector . "').cdm_ws_progress('" . $progress_element_selector . "');
104
    //         });
105
    //       }", 'inline');
106
    }
107

    
108
  /**
109
   * @todo Please document this function.
110
   * @see http://drupal.org/node/1354
111
   */
112
  function _add_js_treeselector() {
113
    // drupal_add_js(drupal_get_path('module', 'cdm_dataportal').'/js/treeselector.js');
114
    drupal_add_js("
115
        jQuery(document).ready(function() {
116
           jQuery('#cdm-taxonomictree-selector-form #edit-val').change(function () {
117
                jQuery('#cdm-taxonomictree-selector-form').submit();
118
            });
119

    
120
        });
121
      ",
122
      array(
123
        'type' => 'inline',
124
        'scope' => 'footer'
125
      )
126
    );
127
  }
128

    
129
  function _add_js_resizable_element($selector, $y_axis_only) {
130

    
131
    _add_jquery_ui();
132
    $options = "";
133
    if($y_axis_only) {
134
      $options = "resize: function(event, ui) {
135
        ui.size.width = ui.originalSize.width;
136
        },
137
        handles: \"s\"";
138

    
139
    }
140
    drupal_add_js("
141
          jQuery(document).ready(function() {
142
             jQuery('" . $selector . "').resizable({". $options ."});
143
          });
144
        ",
145
      array(
146
        'type' => 'inline',
147
        'scope' => 'footer'
148
      )
149
    );
150
  }
151

    
152
  function _add_js_openlayers() {
153

    
154
    $openlayers = '/js/map/OpenLayers-2.13.1/OpenLayers.js';
155
    $proj4js = '/js/map/proj4js-1.1.0/proj4js-compressed.js';
156

    
157
    if(variable_get('cdm_js_devel_mode', FALSE)){
158
      // develooper mode libs
159
  //     $openlayers = '/js/map/OpenLayers-2.13.1/lib/OpenLayers.js';
160
      $openlayers = '/js/map/OpenLayers-2.13.1/OpenLayers.debug.js';
161
      $proj4js = '/js/map/proj4js-1.1.0/proj4js-combined.js';
162
    }
163

    
164
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . $openlayers,
165
      array(
166
        'type' => 'file',
167
        'group' => JS_LIBRARY,
168
        'weight' => 0,
169
        'cache' => TRUE,
170
        'preprocess' => FALSE
171
      )
172
    );
173

    
174

    
175
    // see https://github.com/proj4js/proj4js
176
    // http://openlayers.org/dev/examples/using-proj4js.html
177
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . $proj4js,
178
      array(
179
        'type' => 'file',
180
        'group' => JS_LIBRARY,
181
        'weight' => -1, // before open layers
182
        'cache' => TRUE,
183
      )
184
    );
185

    
186
    // configure the theme
187
    $openlayers_theme_path = drupal_get_path('module', 'cdm_dataportal') . '/js/map/OpenLayers-2.13.1/theme/default/';
188
    $openlayers_imp_path = drupal_get_path('module', 'cdm_dataportal') . '/js/map/img/dark/';
189
    drupal_add_js('OpenLayers.ImgPath="' . base_path() . $openlayers_imp_path . '";', array(
190
        'type' => 'inline',
191
        'group' => JS_LIBRARY,
192
        'weight' => 1, // after openlayers
193
        'cache' => TRUE,
194
        'preprocess' => FALSE
195
      ));
196

    
197
    drupal_add_css($openlayers_theme_path . 'style.tidy.css',
198
      array(
199
        'type' => 'file',
200
        'cache' => TRUE,
201
        'preprocess' => FALSE
202
      )
203
    );
204

    
205
  }
206

    
207
  /**
208
   * @todo Please document this function.
209
   * @see http://drupal.org/node/1354
210
   */
211
  function _add_js_thickbox() {
212
    // ---- jQuery thickbox:
213
    /*
214
    * bug: compat-1.0.js && thickbox.js line 237 .trigger("unload") -> event is
215
    * not triggered because of problems with compat-1.0.js' see INSTALL.txt
216
    */
217
    // drupal_add_js(drupal_get_path('module',
218
    // 'cdm_dataportal').'/js/jquery.imagetool.min.js');
219
    //
220
    // Add a setting for the path to cdm_dataportal module, used to find the path
221
    // for the loading animation image in thickbox.
222
    drupal_add_js(array(
223
    'cdm_dataportal' => array(
224
    'cdm_dataportal_path' => base_path() . drupal_get_path('module', 'cdm_dataportal'),
225
    )
226
    ),
227
    'setting'
228
        );
229
        drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/thickbox/thickbox.js');
230
        drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/js/thickbox/cdm_thickbox.css');
231
  }
232

    
233
  /**
234
   * @todo Please document this function.
235
   * @see http://drupal.org/node/1354
236
   */
237
  function _add_js_lightbox($galleryID) {
238
    /*
239
     * Important Notice: The jquery.lightbox-0.5.js has been modified in order to
240
     * allow using the "alt" attribute for captions instead of the "title"
241
     * attribute
242
     */
243
    $lightbox_base_path =  drupal_get_path('module', 'cdm_dataportal') . '/js/jquery-lightbox-0.5';
244
    $lightbox_image_path = base_path() . $lightbox_base_path . '/images/';
245
    drupal_add_js($lightbox_base_path . '/js/jquery.lightbox-0.5.js');
246
    drupal_add_css($lightbox_base_path . '/css/jquery.lightbox-0.5.css');
247
    drupal_add_js('jQuery(document).ready(function() {
248
        jQuery(\'#' . $galleryID . ' a.lightbox\').lightBox({
249
          fixedNavigation:  true,
250
          imageLoading:     \'' . $lightbox_image_path . 'lightbox-ico-loading.gif\',
251
          imageBtnPrev:     \'' . $lightbox_image_path . 'lightbox-btn-prev.gif\',
252
          imageBtnNext:     \'' . $lightbox_image_path . 'lightbox-btn-next.gif\',
253
          imageBtnClose:    \'' . $lightbox_image_path . 'lightbox-btn-close.gif\',
254
          imageBlank:       \'' . $lightbox_image_path . 'lightbox-blank.gif\',
255
          adjustToWindow: true
256
        });
257
      });
258
      ', array('type' => 'inline'));
259
  }
260

    
261
  /**
262
   * @todo Please document this function.
263
   * @see http://drupal.org/node/1354
264
   */
265
  function _add_js_footnotes() {
266
    _add_js_domEvent();
267
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/footnotes.js');
268
  }
269

    
270
  /**
271
   * @todo Please document this function.
272
   * @see http://drupal.org/node/1354
273
   */
274
  function _add_js_ahah() {
275

    
276
    _add_js_domEvent(); // requires domEvent.js
277
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/ahah-content.js');
278
  }
279

    
280
/**
281
 * @todo Please document this function.
282
 * @see http://drupal.org/node/1354
283
 */
284
function _add_js_taxonomic_children($jquery_selector) {
285

    
286
  global $base_url;
287

    
288

    
289
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/jquery.cdm.taxonomic_children.js');
290
  
291
  drupal_add_js('jQuery(document).ready(function() {
292
        jQuery(\'' . $jquery_selector . '\').taxonomic_children({
293
          // hoverClass: "fa-rotate-90",
294
          // activeClass: "fa-rotate-90",
295
          classificationUuid: "' . get_current_classification_uuid() . '",
296
          taxonUuid: "' . get_current_taxon_uuid() . '",
297
          cdmWebappBaseUri: "' . variable_get('cdm_webservice_url', '') . '",
298
          proxyBaseUri: "' . $base_url . '",
299
          
300
        });
301
      });
302
      ', array('type' => 'inline'));
303
}
304

    
305
  /**
306
   * Adds the external javascript file for domEvent.js.
307
   *
308
   * @see drupal_add_js()
309
   */
310
  function _add_js_domEvent() {
311
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/domEvent.js');
312
  }
313

    
314
  function _add_jquery_ui()
315
  {
316
    drupal_add_css(drupal_get_path('module',
317
        'cdm_dataportal') . '/js/jquery-ui-1.8.24/themes/base/jquery.ui.all.css');
318
    drupal_add_js(drupal_get_path('module',
319
        'cdm_dataportal') . '/js/jquery-ui-1.8.24/ui/jquery-ui.js',
320
      array(
321
        'type' => 'file',
322
        'weight' => JS_LIBRARY,
323
        'cache' => TRUE,
324
        'preprocess' => FALSE
325
      )
326
    );
327
  }
328

    
329
  function _add_js_ui_accordion(){
330
    _add_jquery_ui();
331
    drupal_add_js('jQuery(document).ready(function() {
332
        jQuery( "#accordion" ).accordion();
333
      });',
334
      array('type' => 'inline')
335
    );
336
  }
337

    
338

    
339
function _add_js_utis_client($jquery_selector){
340

    
341
  drupal_add_js(drupal_get_path('module',
342
      'cdm_dataportal') . '/js/utis-client/utis-client.js',
343
    array(
344
      'type' => 'file',
345
      'weight' => JS_LIBRARY,
346
      'cache' => TRUE,
347
      'preprocess' => FALSE
348
    )
349
  );
350

    
351
  drupal_add_js('jQuery(document).ready(function() {
352
        jQuery( "' . $jquery_selector . '" ).utis_client(
353
        {
354
          spinnerIcon: \'' . font_awesome_icon_markup('fa-sync', array('class' => array('fa-3x',  'fa-spin'), 'style' => array('opacity: 0.3;'))) . '\',
355
          externalLinkIcon: \'' . font_awesome_icon_markup('fa-external-link-alt') . '\'
356
        });
357
      });',
358
    array('type' => 'inline')
359
  );
360
}
361

    
362
function _add_js_universalviewer($jquery_selector, $manifest_uri){
363

    
364
  $universalviewer_path  = drupal_get_path('module',    'cdm_dataportal') . '/js/universalviewer';
365

    
366
  drupal_add_css($universalviewer_path . '/uv/uv.css');
367
  drupal_add_css($universalviewer_path . '/uv-fix.css');
368

    
369
  // offline.js is all of these dependencies concatenated (jQuery, CoreJS, and JSViews)
370
  drupal_add_js($universalviewer_path . '/uv/lib/offline.js',
371
      array(
372
      'type' => 'file',
373
      'group' => JS_LIBRARY,
374
      'cache' => TRUE,
375
      'preprocess' => FALSE
376
    )
377
  );
378
  drupal_add_js($universalviewer_path . '/uv/helpers.js',
379
    array(
380
      'type' => 'file',
381
      'group' => JS_LIBRARY,
382
      'cache' => TRUE,
383
      'preprocess' => FALSE
384
    )
385
  );
386
  // from UV 3.x documentation:
387
  // "root:
388
  // The path to the uv directory. Use a relative path e.g. root: '../../uv'
389
  // Don't start your path with a / otherwise requirejs will not append .js to
390
  // dependencies and they won't load."
391
  //
392
  // universalviewerPath will be used to build the root config variable.
393
  $iCleanUrlRequest = strpos($_SERVER['REQUEST_URI'], 'q=cdm_dataportal/') === false;
394
  if($iCleanUrlRequest){
395
    $folders_up = preg_replace('/[^\/]+/', '..', $_GET['q']);
396
    $folders_up = preg_replace('/\.\.$/', '', $folders_up);
397
  } else {
398
    $folders_up = '';
399
  }
400
  $basePath = base_path();
401
  $config_file = 'uv-config.json';
402
  drupal_add_js('
403
      console.log(\'jQuery inline: \' + jQuery.fn.jquery);
404
      jQuery(window).on(\'uvLoaded\', function() {
405
        jQuery( "' . $jquery_selector . '" ).jqUniversalviewer(
406
        {
407
          root: \'' . $folders_up . $universalviewer_path . '/uv\',
408
          configUri: \'' . $basePath . $universalviewer_path . '/'. $config_file . '\',
409
          manifestUri: \'' . $manifest_uri . '\'
410
        }
411
        );
412
      });',
413
    array(
414
      'type' => 'inline',
415
      'scope' => 'footer',
416
      'group' => JS_DEFAULT,
417
      'weight' => 0,
418
    )
419
  );
420
  drupal_add_js($universalviewer_path . '/jq-universalviewer.js',
421
    array(
422
      'type' => 'file',
423
      'scope' => 'footer',
424
      'group' => JS_DEFAULT,
425
      'weight' => 10,
426
      'cache' => TRUE,
427
      'preprocess' => FALSE
428
    )
429
  );
430
  // ui.js must be last
431
  drupal_add_js($universalviewer_path . '/uv/uv.js',
432
    array(
433
      'type' => 'file',
434
      'scope' => 'footer',
435
      'group' => 300, // IMPORTANT!
436
      'cache' => TRUE,
437
      'preprocess' => FALSE
438
    )
439
  );
440
}
441

    
442
  /**
443
   * Provides the markup for an font awesome icon.
444
   *
445
   * The icons is created in default size without any extra features.
446
   *
447
   * The available icons are listed here http://fontawesome.io/cheatsheet/
448
   * fontawesome icons have much more features than implemented here in this function,
449
   * for spinning icons, fixed width icons, rotation, etc please checkout the
450
   * examples at http://fontawesome.io/examples/
451
   *
452
   * @parameter $icon_name
453
   *  The name of the icon which starts with 'fa-'
454
   *
455
   * @return String
456
   *    the markup for the icon in an <i> tag
457
   *
458
   */
459
  function font_awesome_icon_markup($icon_name = NULL, $attributes = array()){
460
    _add_font_awesome_font();
461

    
462

    
463
    if($icon_name){
464
      if(!isset($attributes['class'])){
465
        $attributes['class'] = array();
466
      }
467
      $attributes['class'][] = 'fa';
468
      $attributes['class'][] = $icon_name;
469

    
470
      return '<i ' . drupal_attributes($attributes) . '></i>';
471
    }
472

    
473
    return '';
474
  }
475

    
476
/**
477
 * @param string $glyph_name
478
 *   The name of the gloyph (e.g. 'icon-interal-link-alt-solid') for the foll list please
479
 *   refer to modules/cdm_dataportal/fonts/custom-icon-font
480
 * @param array $attributes
481
 * @return string
482
 */
483
  function custom_icon_font_markup($glyph_name = NULL, $attributes = array()){
484
    _add_font_custom_icon_font();
485

    
486

    
487
    if($glyph_name){
488
      if(!isset($attributes['class'])){
489
        $attributes['class'] = array();
490
      }
491
      $attributes['class'][] = $glyph_name;
492

    
493
      return '<i ' . drupal_attributes($attributes) . '></i>';
494
    }
495

    
496
    return '';
497
  }
498

    
499
/**
500
 * Adds the css  containing the font awesome icons to the html header.
501
 */
502
function _add_font_awesome_font()
503
{
504
  // $fa_font_version = 'font-awesome-4.6.3/css/font-awesome.min.css';
505
  $fa_font_version = 'fontawesome-free-5.9.0-web/css/all.css';
506
  $font_awesome_css_uri = base_path() . drupal_get_path('module', 'cdm_dataportal') . '/fonts/' . $fa_font_version;
507

    
508
  drupal_add_html_head_link(
509
    array(
510
      'href' => $font_awesome_css_uri,
511
      'rel' => 'stylesheet'
512
    )
513
  );
514
}
515

    
516
/**
517
 * Adds the css  containing the font awesome icons to the html header.
518
 */
519
function _add_font_custom_icon_font()
520
{
521

    
522
  $custom_icon_font_css_uri = base_path() . drupal_get_path('module', 'cdm_dataportal') . '/fonts/custom-icon-font/style.css';
523

    
524
  drupal_add_html_head_link(
525
    array(
526
      'href' => $custom_icon_font_css_uri,
527
      'rel' => 'stylesheet'
528
    )
529
  );
530
}
531

    
532
/* ====================== hook implementations ====================== */
533
  /**
534
   * Implements hook_permission().
535
   *
536
   * Valid permissions for this module.
537
   *
538
   * @return array
539
   *   An array of valid permissions for the cdm_dataportal module.
540
   */
541
  function cdm_dataportal_permission() {
542
    return array(
543
      'administer cdm_dataportal' => array(
544
        'title' => t('Administer CDM DataPortal settings'),
545
        'description' => t("Access the settings pages specific for the cdm_dataportal module"),
546
      ),
547
      'access cdm content' => array(
548
        'title' => t('Access CDM content'),
549
        'description' => t("Access content (taxa, names, specimens, etc.) served by the CDM web service."),
550
      ),
551
    );
552
  }
553

    
554
/**
555
 * Implements hook_menu().
556
 */
557
function cdm_dataportal_menu() {
558
  $items = array();
559

    
560
  // @see settings.php.
561
  cdm_dataportal_menu_admin($items);
562
  cdm_dataportal_menu_help($items);
563

    
564
  $items['cdm_dataportal/names'] = array(
565
    'page callback' => 'cdm_dataportal_view_names',
566
    'access arguments' => array('access cdm content'),
567
    'type' => MENU_CALLBACK,
568
  );
569

    
570
  // Optional callback arguments: page.
571
  $items['cdm_dataportal/taxon'] = array(
572
    'page callback' => 'cdm_dataportal_taxon_page_view',
573
    'access arguments' => array('access cdm content'),
574
    'type' => MENU_CALLBACK,
575
    // Expected callback arguments: uuid.
576
  );
577

    
578
  $items['cdm_dataportal/occurrence'] = array(
579
        'page callback' => 'cdm_dataportal_specimen_page_view',
580
        'access arguments' => array('access cdm content'),
581
        'type' => MENU_CALLBACK,
582
        // Expected callback arguments: uuid.
583
    );
584

    
585
  $items['cdm_dataportal/description'] = array(
586
        'page callback' => 'cdm_dataportal_description_page_view',
587
        'access arguments' => array('access cdm content'),
588
        'type' => MENU_CALLBACK,
589
        // Expected callback arguments: uuid.
590
    );
591

    
592
   $items['cdm_dataportal/specimen/accession_number'] = array(
593
        'page callback' => 'cdm_dataportal_specimen_by_accession_number_page_view',
594
        'access arguments' => array('access cdm content'),
595
        'type' => MENU_CALLBACK,
596
        // Expected callback arguments: accession number.
597
    );
598
  $items['cdm_dataportal/named_area'] = array(
599
    'page callback' => 'cdm_dataportal_named_area_page_view',
600
    'access arguments' => array('access cdm content'),
601
    'type' => MENU_CALLBACK,
602
    // Expected callback arguments: uuid.
603
  );
604

    
605
  $items['cdm_dataportal/name'] = array(
606
    'page callback' => 'cdm_dataportal_name_page_view',
607
      /*
608
    'page arguments' => array(
609
       'taxon_name_uuid',
610
       'taxon_to_hide_uuid',
611
       'synonym_uuid' => NULL
612
      ),
613
      */
614
    'access arguments' => array('access cdm content'),
615
    'type' => MENU_CALLBACK,
616
    // Expected callback arguments: uuid.
617
  );
618

    
619
  $items['cdm_dataportal/reference'] = array(
620
    'page callback' => 'cdm_dataportal_view_reference',
621
    'access arguments' => array('access cdm content'),
622
    'type' => MENU_CALLBACK,
623
    // Expected callback arguments: uuid.
624
  );
625

    
626
  $items['cdm_dataportal/reference/list'] = array(
627
    'page callback' => 'cdm_dataportal_view_reference_list',
628
    'access arguments' => array('access cdm content'),
629
    'type' => MENU_CALLBACK,
630
    // Expected callback arguments: uuid.
631
  );
632

    
633
  $items['cdm_dataportal/media'] = array(
634
    'page callback' => 'cdm_dataportal_view_media',
635
    'access arguments' => array('access cdm content'),
636
    'type' => MENU_CALLBACK,
637
    // Expected callback arguments:
638
    // uuid, mediarepresentation_uuid, part_uuid or part#.
639
  );
640

    
641
  $items['cdm_dataportal/polytomousKey'] = array(
642
    'page callback' => 'cdm_dataportal_view_polytomousKey',
643
    'access arguments' => array('access cdm content'),
644
    'type' => MENU_CALLBACK,
645
    // Expected callback arguments: polytomousKey->uuid.
646
  );
647

    
648
  $items['cdm_dataportal/search'] = array(
649
    'page callback' => 'cdm_dataportal_view_search_advanced',
650
    'access arguments' => array('access cdm content'),
651
    'type' => MENU_CALLBACK,
652
  );
653

    
654
  // Optional callback arguments: page.
655
  $items['cdm_dataportal/registration'] = array(
656
    'page callback' => 'cdm_dataportal_registration_page_view',
657
    'access arguments' => array('access cdm content'),
658
    'type' => MENU_CALLBACK,
659
    // Expected callback arguments: uuid.
660
  );
661

    
662
  // ------------ SEARCH -----------
663
  $items['cdm_dataportal/search/advanced'] = array(
664
    'title' => 'Advanced', // will be passed through t()
665
    'page callback' => 'cdm_dataportal_view_search_advanced',
666
    'access arguments' => array('access cdm content'),
667
    'type' => MENU_DEFAULT_LOCAL_TASK,
668
  );
669
  $items['cdm_dataportal/search/blast'] = array(
670
    'title' => 'Blast', // will be passed through t()
671
    'page callback' => 'cdm_dataportal_view_search_blast',
672
    'access arguments' => array('access cdm content'),
673
    'type' => MENU_LOCAL_TASK,
674
  );
675

    
676
  $items['cdm_dataportal/search/taxon_by_description'] = array(
677
    'title' => 'By content category', // will be passed through t()
678
    'page callback' => 'cdm_dataportal_view_search_taxon_by_description',
679
    'access arguments' => array('access cdm content'),
680
    'type' => MENU_LOCAL_TASK,
681
  );
682
  $items['cdm_dataportal/search/results/taxon'] = array(
683
    'page callback' => 'cdm_dataportal_view_search_results_taxon',
684
    'access arguments' => array('access cdm content'),
685
    'type' => MENU_CALLBACK,
686
  );
687

    
688
  $items['cdm_dataportal/search/results/specimen'] = array(
689
      'page callback' => 'cdm_dataportal_view_search_results_specimen',
690
      'access arguments' => array('access cdm content'),
691
      'type' => MENU_CALLBACK,
692
  );
693

    
694
  /*
695
   * MENU_CALLBACK at cdm_dataportal/registration-search is needed to make the
696
   * tabs in the subordinate paths work, accessing this 'page' will cause the
697
   * MENU_DEFAULT_LOCAL_TASK being displayed
698
   */
699
  $items['cdm_dataportal/registration-search'] = array(
700
    'title' => 'Search', // will be passed through t()
701
    'page callback' => 'cdm_dataportal_view_search_registrations_results',
702
    'page arguments' => array("filter"),
703
    'access arguments' => array('access cdm content'),
704
    'type' => MENU_CALLBACK,
705
  );
706
  /*
707
   * the MENU_DEFAULT_LOCAL_TASK creates a tab for the MENU_CALLBACK
708
   * defined at a higher level of the path (cdm_dataportal/registration-search)
709
   */
710
  $items['cdm_dataportal/registration-search/filter'] = array(
711
    'title' => 'Search', // will be passed through t()
712
    'page callback' => 'cdm_dataportal_view_search_registrations_results',
713
    'page arguments' => array("filter"),
714
    'access arguments' => array('access cdm content'),
715
    'type' => MENU_DEFAULT_LOCAL_TASK,
716
  );
717
  /*
718
   * the MENU_LOCAL_TASK creates another tab
719
   */
720
  $items['cdm_dataportal/registration-search/taxongraph'] = array(
721
    'title' => 'Taxon graph search', // will be passed through t()
722
    'page callback' => 'cdm_dataportal_view_search_registrations_results',
723
    'page arguments' => array("taxongraph"),
724
    'access arguments' => array('access cdm content'),
725
    'type' => MENU_LOCAL_TASK,
726
  );
727

    
728
  // Optional callback arguments: page.
729
  $items['cdm_dataportal/search/agent'] = array(
730
    'page callback' => 'cdm_dataportal_view_search_agent',
731
    'access arguments' => array('access cdm content'),
732
    'type' => MENU_CALLBACK,
733
    // Expected callback arguments: uuid.
734
  );
735

    
736
  // ------------ menu items with variable path elements -----------
737

    
738
  // 'May not cache' in D5.
739
  $items['cdm_dataportal/name/%'] = array(
740
    // 'page callback' => 'cdm_dataportal_view_name',
741
    'page callback' => 'cdm_dataportal_name_page_view',
742
    'page arguments' => array(2, 3, 4, 5),
743
    'access arguments' => array('access cdm content'),
744
    'type' => MENU_CALLBACK,
745
  );
746

    
747
  // --- Local tasks for Taxon.
748
  // --- tabbed taxon page
749
  if (variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
750
    $items['cdm_dataportal/taxon/%'] = array(
751
      'title' => cdm_taxonpage_tab_label('General'),
752
      'page callback' => 'cdm_dataportal_taxon_page_view',
753
      'access arguments' => array('access cdm content'),
754
      'type' => MENU_CALLBACK,
755
      'weight' => 1,
756
      'page arguments' => array(2, "description")
757
      , // Expected callback arguments: taxon_uuid.
758
    );
759

    
760
    $items['cdm_dataportal/taxon/%/all'] = array(
761
      'title' => cdm_taxonpage_tab_label('General'),
762
      'page callback' => 'cdm_dataportal_taxon_page_view',
763
      'access arguments' => array('access cdm content'),
764
      'type' => MENU_CALLBACK,
765
      'weight' => 2,
766
      'page arguments' => array(2, "all")
767
      , // Expected callback arguments: taxon_uuid.
768
    );
769

    
770
    $items['cdm_dataportal/taxon/%/description'] = array(
771
      'title' => cdm_taxonpage_tab_label('General'),
772
      'page callback' => 'cdm_dataportal_taxon_page_view',
773
      'access arguments' => array('access cdm content'),
774
      'type' => MENU_DEFAULT_LOCAL_TASK,
775
      'weight' => 2,
776
      'page arguments' => array(2, "description")
777
      , // Expected callback arguments: taxon_uuid.
778
    );
779

    
780
    $items['cdm_dataportal/taxon/%/synonymy'] = array(
781
      'title' => cdm_taxonpage_tab_label('Synonymy'),
782
      'page callback' => 'cdm_dataportal_taxon_page_view',
783
      'access arguments' => array('access cdm content'),
784
      'type' => MENU_LOCAL_TASK,
785
      'weight' => 4,
786
      'page arguments' => array(2, "synonymy", 4)
787
      , // Expected callback arguments: taxon_uuid and ...
788
    );
789
    $items['cdm_dataportal/taxon/%/images'] = array( // Images
790
      'title' => cdm_taxonpage_tab_label('Images'),
791
      'page callback' => 'cdm_dataportal_taxon_page_view',
792
      'access arguments' => array('access cdm content'),
793
      'type' => MENU_LOCAL_TASK,
794
      'weight' => 5,
795
      'page arguments' => array(2, "images")
796
      , // Expected callback arguments: taxon_uuid.
797
    );
798

    
799
    $items['cdm_dataportal/taxon/%/specimens'] = array( // Specimens
800
      'title' => cdm_taxonpage_tab_label('Specimens'),
801
      'page callback' => 'cdm_dataportal_taxon_page_view',
802
      'access arguments' => array('access cdm content'),
803
      'type' => MENU_LOCAL_TASK,
804
      'weight' => 6,
805
      'page arguments' => array(2, "specimens")
806
      , // Expected callback arguments: taxon_uuid.
807
    );
808

    
809
    $items['cdm_dataportal/taxon/%/keys'] = array( // Keys
810
      'title' => cdm_taxonpage_tab_label('Keys'),
811
      'page callback' => 'cdm_dataportal_taxon_page_view',
812
      'access arguments' => array('access cdm content'),
813
      'type' => MENU_LOCAL_TASK,
814
      'weight' => 6,
815
      'page arguments' => array(2, "keys")
816
      , // Expected callback arguments: taxon_uuid.
817
    );
818

    
819
    $items['cdm_dataportal/taxon/%/experts'] = array( // Experts
820
      'title' => cdm_taxonpage_tab_label('Experts'),
821
        'page callback' => 'cdm_dataportal_taxon_page_view',
822
        'access arguments' => array('access cdm content'),
823
        'type' => MENU_LOCAL_TASK,
824
        'weight' => 6,
825
        'page arguments' => array(2, "experts")
826
    , // Expected callback arguments: taxon_uuid.
827
    );
828

    
829
    $items['cdm_dataportal/taxon/autosuggest/%/%/%/'] = array(
830
        'page callback' => 'cdm_dataportal_taxon_autosuggest',
831
        'access arguments' => array('access cdm content'),
832
        'page arguments' => array(3,4,5),
833
        'type' => MENU_CALLBACK
834
    );
835
  }
836

    
837
  // --- refresh link for all cdmnode types
838
  foreach (cdm_get_nodetypes() as $type=>$name) {
839
    $items['cdm_dataportal/' . $name . '/%/refresh'] = array(
840
        'title' => 'Refresh',
841
        'page callback' => 'cdm_dataportal_refresh_node',
842
        'access arguments' => array('administer cdm_dataportal'),
843
        'type' => MENU_LOCAL_TASK,
844
        'weight' => 100,
845
        'page arguments' => array($name, 2)
846
    );
847
  }
848

    
849
  return $items;
850
}
851

    
852
/**
853
 * Implements hook_init().
854
 *
855
 */
856
function cdm_dataportal_init() {
857
  if (!path_is_admin(current_path())) {
858
    //FIXME To add CSS or JS that should be present on all pages, modules
859
    //      should not implement this hook, but declare these files in their .info file.
860
    drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal.css');
861
    // drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
862
    drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal_screen.css', array('type' => 'screen'));
863
  } else {
864
    drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal_settings.css');
865
  }
866

    
867
  if(variable_get('cdm_debug_mode', FALSE)){
868
    $file = 'temporary://drupal_debug.txt';
869
    file_put_contents($file, 'CDM DEBUG LOG for ' . $_GET['q']. "\n"); // will overwrite the file
870
  }
871

    
872
  $bibliography_settings = get_bibliography_settings();
873
  $enclosing_tag = $bibliography_settings['enabled'] == 1 ? 'div' : 'span';
874
  FootnoteManager::registerFootnoteSet('BIBLIOGRAPHY', $enclosing_tag, $bibliography_settings['key_format']);
875
}
876

    
877
function cdm_dataportal_refresh_node($cdm_node_name, $uuid, $parameters = array()){
878

    
879
  $base_path = 'cdm_dataportal/' . $cdm_node_name . '/' . $uuid;
880

    
881
  if($cdm_node_name == 'taxon' && variable_get('cdm_dataportal_taxonpage_tabs', 1)){
882
    // force reloading of all and notify user about this special loading
883
    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: ')
884
        . l('Back to tabbed taxon page', $base_path));
885
    $base_path .= '/all';
886
  } else {
887
    drupal_set_message(t('The level 2 cache has been cleared for this page'));
888
  }
889

    
890
  $parameters['cacheL2_refresh'] ='1';
891

    
892

    
893
  drupal_goto($base_path, array('query' => $parameters));
894
}
895

    
896
/**
897
 * Implements hook_requirements($phase)
898
 */
899
function cdm_dataportal_requirements($phase) {
900
    $requirements = array();
901
    if($phase == 'runtime'){
902

    
903
    }
904
    return $requirements;
905
}
906

    
907
/**
908
 * Implements hook_block_info().
909
 */
910
function cdm_dataportal_block_info() {
911

    
912
    // $block[0]["info"] = t("CDM DataPortal DevLinks");
913
    // $block[1]["info"] = t("CDM DataPortal Credits");
914
    $block["2"] = array(
915
        "info" => t("CDM - Search Taxa"),
916
        "cache" => DRUPAL_NO_CACHE
917
      );
918
    // $block[3]["info"] = t("CDM Filters");
919
    $block["4"]["info"] = t("CDM  - Dataportal Print");
920
    $block["keys"]["info"] = t("CDM - Identification keys");
921
    $block["fundedByEDIT"]["info"] = t('CDM - Powered by EDIT');
922
    $block["classification_breadcrumbs"] =  array(
923
        'info' => t('CDM - Classification breadcrumbs'),
924
        'cache' => DRUPAL_CACHE_PER_PAGE
925
      );
926
    $block["taxonomic_children"] =  array(
927
      'info' => t('CDM - Taxonomic children'),
928
      'cache' => DRUPAL_CACHE_PER_PAGE
929
    );
930
    $block["back_to_search_results"] =  array(
931
      'title' => '<none>',
932
      'info' => t('CDM - Back to search Results'),
933
      'cache' => DRUPAL_CACHE_PER_PAGE,
934
      'visibility' => BLOCK_VISIBILITY_LISTED,
935
      'pages' => "cdm_dataportal/taxon/*", // multiple page paths separated by "\n"!!!
936
    );
937
  $block['registrations_search_filter'] =  array(
938
    'title' => 'Filter registrations',
939
    'info' => t('CDM - Registrations search filter'),
940
    'cache' => DRUPAL_CACHE_PER_PAGE,
941
    'visibility' => BLOCK_VISIBILITY_NOTLISTED,
942
    'pages' => "cdm_dataportal/registration-search\ncdm_dataportal/registration-search/*", // multiple page paths separated by "\n"!!!
943
  );
944
  $block['registrations_search_taxongraph'] =  array(
945
    'title' => 'Taxonomic registration search',
946
    'info' => t('CDM - Registrations search by taxon graph'),
947
    'cache' => DRUPAL_CACHE_PER_PAGE,
948
    'visibility' => BLOCK_VISIBILITY_NOTLISTED,
949
    'pages' => "cdm_dataportal/registration-search\ncdm_dataportal/registration-search/*", // multiple page paths separated by "\n"!!!
950
  );
951
  $block['registrations_search'] =  array(
952
    'title' => 'Search',
953
    'info' => t('CDM - Registrations search combining filter and taxon graph search' ),
954
    'cache' => DRUPAL_CACHE_PER_PAGE,
955
    'visibility' => BLOCK_VISIBILITY_NOTLISTED,
956
    'pages' => "cdm_dataportal/registration-search\ncdm_dataportal/registration-search/*", // multiple page paths separated by "\n"!!!
957
  );
958
  $block['utis_search'] =  array(
959
    'title' => 'UTIS Search',
960
    'info' => t('Query the Unified Taxonomic Information Service (UTIS)' ),
961
    'cache' => DRUPAL_CACHE_PER_PAGE,
962
    'visibility' => BLOCK_VISIBILITY_NOTLISTED
963
  );
964
  return $block;
965
}
966

    
967
/**
968
 * Implements hook_block_view().
969
 */
970
function cdm_dataportal_block_view($delta) {
971
  // TODO Rename block deltas (e.g. '2') to readable strings.
972
  switch ($delta) {
973
    // case 'delta-1':
974
    // $block['subject'] = t('Credits');
975
    // $block['content'] = theme('cdm_credits');
976
    // return $block;
977
    case '2':
978
      $block['subject'] = t('Search taxa');
979
      $form = drupal_get_form('cdm_dataportal_search_taxon_form');
980
      $block['content'] = drupal_render($form);
981

    
982
      if (variable_get('cdm_dataportal_show_advanced_search', 1)) {
983
        $block['content'] .= '<div>' . l(t('Advanced Search'), 'cdm_dataportal/search') . '</div>';
984
      }
985
      if (variable_get(CDM_SEARCH_BLAST_ENABLED)){
986
          $block['content'] .= '<div>' . l(t('Blast Search'), 'cdm_dataportal/search/blast') . '</div>';
987
      }
988
      return $block;
989
    case '4':
990
      $block['subject'] = '';
991
      $block['content'] = theme('cdm_print_button');
992
      return $block;
993
    case "keys":
994
      $block['subject'] = t('Identification Keys');
995
      $block['content'] = theme('cdm_block_IdentificationKeys', array('taxonUuid' => NULL));
996
      return $block;
997
    case "fundedByEDIT":
998
      // t('Funded by EDIT');
999
      $text = '<none>';
1000
      $block['subject'] = $text;
1001
      $img_tag = '<img src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/powered_by_edit.png' . '" alt="' . $text . '"/>';
1002
      $block['content'] = l($img_tag, "http://cybertaxonomy.org/", array(
1003
        'attributes' => array("target" => "EDIT"),
1004
        'absolute' => TRUE,
1005
        'html' => TRUE,
1006
      ));
1007
      return $block;
1008
    case 'classification_breadcrumbs':
1009
      $taxon_uuid = get_current_taxon_uuid();
1010
      $block['subject'] = '<none>';
1011
      $block['content'] = compose_classification_breadcrumbs($taxon_uuid);
1012
      return $block;
1013
    case 'taxonomic_children':
1014
      $taxon_uuid = get_current_taxon_uuid();
1015
      $block['subject'] = '<none>';
1016
      $block['content'] = compose_taxonomic_children($taxon_uuid);
1017
      return $block;
1018
    case 'back_to_search_results':
1019
      $block['subject'] = '<none>';
1020
      if (isset($_SESSION['cdm']['search'])) {
1021
        $block['content'] = l(t('Back to search result'), "http://" . $_SERVER['SERVER_NAME'] . $_SESSION['cdm']['last_search']);
1022
      }
1023
      return $block;
1024
    case 'registrations_search_filter':
1025
      $block['subject'] = '<none>';
1026
      $block['content'] = drupal_get_form('cdm_dataportal_search_registration_filter_form'); // see cdm_dataportal_search_registration_filter_form($form, &$form_state)
1027
      return $block;
1028
    case 'registrations_search_taxongraph':
1029
      $block['subject'] = '<none>';
1030
      $block['content'] = drupal_get_form('cdm_dataportal_search_registration_taxongraph_form'); // see cdm_dataportal_search_registration_taxongraph_form($form, &$form_state)
1031
      return $block;
1032
    case 'registrations_search':
1033
      _add_js_ui_accordion();
1034
      _add_font_awesome_font();
1035
      $block['subject'] = '<none>';
1036
      $filter_form = drupal_get_form('cdm_dataportal_search_registration_filter_form');
1037
      $filter_form['#prefix'] = '<div>';
1038
      $filter_form['#suffix'] = '</div>';
1039
      $taxongraph_form = drupal_get_form('cdm_dataportal_search_registration_taxongraph_form');
1040
      $taxongraph_form['#prefix'] = '<div>';
1041
      $taxongraph_form['#suffix'] = '</div>';
1042
      $block['content'] = array(
1043
        'accordion' => array(
1044
          '#markup' => '',
1045
          '#prefix' => '<div id="accordion">',
1046
          '#suffix' => '</div>',
1047
          'content' => array(
1048
            array('#markup' => '<h3>Filter</h3>'),
1049
            $filter_form,
1050
            array('#markup' => '<h3>Taxon graph</h3>'),
1051
            $taxongraph_form,
1052
          )
1053
        )
1054
    );
1055
       return $block;
1056
    case 'utis_search':
1057
      _add_js_utis_client('.utis_client');
1058
      $block['subject'] = '<none>';
1059
      $block['content'] = '<div class="utis_client"></div>';
1060
      return $block;
1061
    default:
1062
      return null;
1063
  }
1064
}
1065

    
1066

    
1067
/**
1068
 * Provides the uuid of the taxon for pages with the path ./taxon/{taxon_uuid}
1069
 *
1070
 * @return string
1071
 *   the taxon uuid or NULL
1072
 */
1073
function get_current_taxon_uuid()
1074
{
1075
  static $taxon_uuid;
1076

    
1077
  if(!isset($taxon_uuid)){
1078
    if(isset($_REQUEST['currentTaxon']) && is_uuid($_REQUEST['currentTaxon'])) {
1079
      $taxon_uuid = $_REQUEST['currentTaxon'];
1080
    } else if (arg(1) == 'taxon' && is_uuid(arg(2))) {
1081
      $taxon_uuid = arg(2);
1082
    } else {
1083
      $taxon_uuid = null;
1084
    }
1085
  }
1086

    
1087
  return $taxon_uuid;
1088
}
1089

    
1090
/**
1091
 * Returns the currently classification tree in use.
1092
 *
1093
 * @return string
1094
 *   The uuid of the currently focused classification
1095
 */
1096
function get_current_classification_uuid() {
1097
  if (isset($_SESSION['cdm']['taxonomictree_uuid']) && is_uuid($_SESSION['cdm']['taxonomictree_uuid'])) {
1098
    return $_SESSION['cdm']['taxonomictree_uuid'];
1099
  }
1100
  else {
1101
    return variable_get(CDM_TAXONOMICTREE_UUID, FALSE);
1102
  }
1103
}
1104

    
1105
/*
1106
 function cdm_dataportal_session_clear($cdm_ws_uri_update = FALSE){
1107
 $_SESSION['cdm'] = NULL;
1108
 if(is_string($cdm_ws_uri_update)){
1109
 $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
1110
 }
1111
 }
1112

    
1113
 function cdm_dataportal_session_validate(){
1114
 if(!isset($_SESSION['cdm']['ws_uri'])){
1115
 $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', FALSE));
1116
 } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', FALSE)){
1117
 cdm_dataportal_session_clear(variable_get('cdm_webservice_url', FALSE));
1118
 }
1119
 }
1120
 */
1121

    
1122
/**
1123
 * creates a  selector form for taxonomic trees.
1124
 *
1125
 * @return array
1126
 *  a drupal form array
1127
 */
1128
function cdm_taxonomictree_selector() {
1129
  _add_js_treeselector();
1130

    
1131
  $form = drupal_get_form('cdm_taxonomictree_selector_form');
1132
  return $form;
1133
}
1134

    
1135
/**
1136
 * @todo Please document this function.
1137
 * @see http://drupal.org/node/1354
1138
 *
1139
 * @deprecated use compose_classification_selector instead
1140
 */
1141
function cdm_taxonomictree_selector_form($form, &$form_state) {
1142

    
1143
  $url = url('cdm_api/setvalue/session', array('query' => NULL));
1144
  $form['#action'] = $url;
1145

    
1146
  $form['var'] = array(
1147
    '#weight' => -3,
1148
    '#type' => 'hidden',
1149
    '#value' => '[cdm][taxonomictree_uuid]',
1150
  );
1151

    
1152
  $destination_array = drupal_get_destination();
1153
  $destination = $destination_array['destination'];
1154

    
1155
  $form['destination'] = array(
1156
    '#weight' => -3,
1157
    '#type' => 'hidden',
1158
    '#value' =>  $destination,
1159
  );
1160

    
1161
  $options = cdm_get_taxontrees_as_options();
1162
  $taxontree_includes = variable_get(CDM_TAXONTREE_INCLUDES, null);
1163
  if($taxontree_includes){
1164
    $filtered_options = array();
1165
    foreach($options as $uuid=>$label){
1166
      if(!empty($taxontree_includes[$uuid])){
1167
        $filtered_options[$uuid] = $label;
1168
      }
1169
    }
1170
    $options = $filtered_options;
1171
  }
1172

    
1173
  $form['val'] = array(
1174
    '#type' => 'select',
1175
    '#title' => t('Available classifications'),
1176
    '#default_value' => get_current_classification_uuid(),
1177
    '#options' => $options,
1178
    '#attributes' => array('class' => array('highlite-first-child')),
1179
  );
1180

    
1181
  return $form;
1182

    
1183
}
1184

    
1185
/**
1186
 *
1187
 * @ingroup compose
1188
 */
1189
function compose_classification_selector() {
1190

    
1191
  $destination_array = drupal_get_destination();
1192
  $destination = $destination_array['destination'];
1193

    
1194
  $options = cdm_get_taxontrees_as_options();
1195
  $items = array();
1196
  $taxontree_includes = variable_get(CDM_TAXONTREE_INCLUDES, null);
1197

    
1198
  $current_classification_uuid = get_current_classification_uuid();
1199

    
1200

    
1201
  foreach($options as $uuid=>$label){
1202
    if(!$taxontree_includes || !empty($taxontree_includes[$uuid])){
1203

    
1204
      $class_attributes = '';
1205
      if($current_classification_uuid == $uuid){
1206
        $class_attributes  = array('focused');
1207
      }
1208
      $items[] = array(
1209
        'data' => l($label,
1210
          'cdm_api/setvalue/session',
1211
          array(
1212
            'query' => array(
1213
              'destination' => $destination,
1214
              'val' => $uuid,
1215
              'var' => '[cdm][taxonomictree_uuid]'
1216
            ),
1217
          )
1218
        ),
1219
        'class' => $class_attributes
1220
      );
1221
    }
1222
  }
1223

    
1224
  $render_array = array(
1225
    '#theme' => 'item_list',
1226
    '#type' => 'ul',
1227
    '#items' => $items
1228
  );
1229

    
1230
  return $render_array;
1231
}
1232

    
1233

    
1234
/* UNREACHABLE since action of form directly links to view.
1235
 function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
1236

    
1237
 $_SESSION['cdm']['search'] = $form_values;
1238
 //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
1239
 return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
1240
 //$paramstr = compose_url_prameterstr($form_values);
1241
 //return url('/cdm_dataportal/search/taxon/', array('query' => $paramstr));
1242
 }
1243
 */
1244
/* ====================== menu callback functions ====================== */
1245
/**
1246
 * @todo Please document this function.
1247
 * @see http://drupal.org/node/1354
1248
 */
1249
/*
1250
function cdm_dataportal_form_alter(&$form, &$form_state, $form_id) {
1251
  static $comment_node_disabled =  0;
1252
  static $comment_node_read_only =  1;
1253
  static $comment_node_read_write =  2;
1254

    
1255
  if ($form_id == 'node_type_form'
1256
   && isset($form['identity']['type'])
1257
   && array_key_exists($form['#node_type']->type, cdm_get_nodetypes())
1258
  ) {
1259
    $form['workflow']['comment'] = array(
1260
      '#type' => 'radios',
1261
      '#title' => t('Default comment setting'),
1262
      '#default_value' => variable_get('comment__' . $node->type . $form['#node_type']->type, $comment_node_disabled),
1263
      '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
1264
      '#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
1265
    );
1266
  }
1267
}
1268
*/
1269

    
1270
/**
1271
 * Displays a list of the known taxonomic names.
1272
 *
1273
 * When the list of taxonomic names is displayed, long lists are split up into
1274
 * multiple pages.
1275
 *
1276
 * TODO: Parameters are still preliminary.
1277
 *
1278
 * @param string $beginsWith
1279
 * @param string $page
1280
 *   Page number to diplay defaults to page 1.
1281
 * @param bool $onlyAccepted
1282
 */
1283
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = FALSE) {
1284

    
1285
  $out = t('<h3>Sorry, the name list feature is not yet available in this version of the DataPortal software<h3>');
1286

    
1287
  /*
1288
  // FIXME the filter for accepted names will be a form element, thus this
1289
  // widget should be generated via form api preferably as block.
1290
  $out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
1291
  $out .= theme('cdm_dataportal_widget_names_list', $names, $page);
1292
  $out .= theme('cdm_listof_taxa', $taxonPager);
1293
  return $out;
1294
  */
1295
}
1296

    
1297
/**
1298
 * @todo Please document this function.
1299
 * @see http://drupal.org/node/1354
1300
 * @throws Exception
1301
 */
1302
function cdm_dataportal_view_reference($uuid, $arg2 = NULL) {
1303

    
1304
  cdm_check_valid_portal_page();
1305

    
1306
  return compose_cdm_reference_page($uuid);
1307
}
1308

    
1309
/**
1310
 * Creates a view on a all references contained in the portal.
1311
 *
1312
 * This function is used at the path cdm_dataportal/reference/list
1313
 */
1314
function cdm_dataportal_view_reference_list($pageNumber) {
1315
  $referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get(CDM_SEARCH_RESULT_PAGE_SIZE, CDM_SEARCH_RESULT_PAGE_SIZE_DEFAULT), $pageNumber);
1316
  cdm_reference_pager($referencePager, 'cdm_dataportal/reference/list/');
1317
}
1318

    
1319
/**
1320
 * @todo Please document this function.
1321
 * @see http://drupal.org/node/1354
1322
 */
1323
function cdm_dataportal_view_media($mediaUuid, $mediarepresentation_uuid = FALSE, $part = 0) {
1324

    
1325
  cdm_check_valid_portal_page();
1326

    
1327
  $media = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $mediaUuid);
1328
  if (!$media) {
1329
    drupal_set_title(t('Media does not exist'), PASS_THROUGH);
1330
    return "";
1331
  }
1332
  return theme('cdm_media_page', array(
1333
    'media' => $media,
1334
    'mediarepresentation_uuid' => $mediarepresentation_uuid,
1335
    'partId' => $part,
1336
    ));
1337
}
1338

    
1339
/**
1340
 * Loads the media associated to the given taxon from the cdm server.
1341
 * The aggregation settings regarding taxon relathionships and
1342
 * taxonnomic childen are taken into account.
1343
 *
1344
 * The media lists are cached in a static variable.
1345
 *
1346
 * @param Taxon $taxon
1347
 *   A CDM Taxon entity
1348
 *
1349
 * @return array
1350
 *   An array of CDM Media entities
1351
 *
1352
 */
1353
  function _load_media_for_taxon($taxon) {
1354

    
1355
  static $media = NULL;
1356

    
1357
  if(!isset($media)) {
1358
    $media = array();
1359
  }
1360
  if (!isset($media[$taxon->uuid])) {
1361

    
1362

    
1363
    $mediaQueryParameters = taxon_media_query_parameters();
1364

    
1365
    $ws_endpoint = NULL;
1366
    if ( $mediaQueryParameters['includeTaxonomicChildren']) {
1367
      $ws_endpoint = CDM_WS_PORTAL_TAXON_SUBTREE_MEDIA;
1368
    } else {
1369
      $ws_endpoint = CDM_WS_PORTAL_TAXON_MEDIA;
1370
    }
1371
    // this parameter is not yet used by the CDM_WS_PORTAL_TAXON_*MEDIA
1372
    // services
1373
    unset($mediaQueryParameters['includeTaxonomicChildren']);
1374

    
1375
    $media[$taxon->uuid] = cdm_ws_get($ws_endpoint,
1376
        array(
1377
            $taxon->uuid,
1378
        ),
1379
        queryString($mediaQueryParameters)
1380
       );
1381
  }
1382

    
1383
  return $media[$taxon->uuid];
1384
}
1385

    
1386
/**
1387
 *
1388
 * @param Taxon $taxon
1389
 *   A CDM Taxon entitiy
1390
 *
1391
 * @return array
1392
 *   An array of CDM SpecimenOrObservation entities
1393
 *
1394
function _load_occurences_for_taxon($taxon){
1395

    
1396
  static $occurences = NULL;
1397

    
1398
  if(!isset($occurences)) {
1399
    $occurences = array();
1400
  }
1401

    
1402
  if (!isset($occurences[$taxon->uuid])){
1403

    
1404
    $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
1405
    $relationship_choice['direct'] = get_selection($relationship_choice['direct']);
1406
    $relationship_choice['invers'] = get_selection($relationship_choice['invers']);
1407

    
1408
    $by_associatedtaxon_query = http_build_query(array(
1409
        'relationshipsInvers' => implode(',', $relationship_choice['invers']),
1410
        'relationships' => implode(',', $relationship_choice['direct']),
1411
        'pageSize' => null // all hits in one page
1412
    )
1413
    );
1414

    
1415
    $pager = cdm_ws_get(CDM_WS_OCCURRENCE_BY_ASSOCIATEDTAXON,
1416
        null,
1417
        $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
1418
    );
1419

    
1420

    
1421
    if(isset($pager->records[0])){
1422
      $occurences[$taxon->uuid] =  $pager->records;
1423
    }
1424
  }
1425
  return $occurences[$taxon->uuid];
1426
}
1427
 */
1428

    
1429
/**
1430
 * Gets a Drupal variable, string or array and returns it.
1431
 *
1432
 * Similar to the variable_get() function of Drupal, except that this function
1433
 * is able to handle arrays correctly. This function is especially useful
1434
 * when dealing with collections of settings form elements (#tree = TRUE).
1435
 *
1436
 * @param string $variableKey
1437
 *   The Unique key of the Drupal variable in the Drupal variables table.
1438
 * @param string $defaultValueString
1439
 *   A string as for example derived from a CONSTANT.
1440
 *
1441
 * @return mixed
1442
 *   usually an array, depending on the nature of the variable.
1443
 *
1444
 * TODO compare with get_array_variable_merged() duplicate functions?
1445
 * @deprecated rather use get_array_variable_merged() since this function
1446
 * used an array as second parameter
1447
 */
1448
function mixed_variable_get($variableKey, $defaultValueString) {
1449
  $systemDefaults = unserialize($defaultValueString);
1450
  $storedSettings = variable_get($variableKey, array());
1451
  if (is_array($storedSettings)) {
1452
    // TODO better use drupal_array_merge_deep() ?
1453
    $settings = array_merge($systemDefaults, $storedSettings);
1454
  }
1455
  else {
1456
    $settings = $systemDefaults;
1457
  }
1458
  return $settings;
1459
}
1460

    
1461
/**
1462
 * Recursive function to convert an object into an array.
1463
 * also subordinate objects will be converted.
1464
 *
1465
 * @param object $object
1466
 *  the object to be converted
1467
 * @return array
1468
 *  The array
1469
 */
1470
function object_to_array($object) {
1471
  if(is_object($object) || is_array($object)) {
1472
    $array = (array)$object;
1473
    foreach ($array as $key=>$value){
1474
      $array[$key] = object_to_array($value);
1475
    }
1476
    return $array;
1477
  } else {
1478
    return $object;
1479
  }
1480
}
1481

    
1482
/**
1483
 * Searches the $collection for the cdm entitiy given as $element.
1484
 *
1485
 * The elements are compared by their UUID.
1486
 *
1487
 * @param $element
1488
 *  the CDM entitiy to search for
1489
 * @param $collection
1490
 *  the list of CDM entities to search in
1491
 *
1492
 * @return boolean TRUE if the $collection contains the $element, otheriwse FALSE
1493
 *
1494
 */
1495
function contains_cdm_entitiy($element, $collection) {
1496
  $result = FALSE;
1497
  foreach ($collection as $a) {
1498
    if ($a->uuid == $element->uuid) {
1499
      $result = TRUE;
1500
    }
1501
  }
1502
  return $result;
1503
}
1504

    
1505
/**
1506
 * Filters the array $entity_list of CDM entities by the list
1507
 * of $excludes. Any element contained in the $excludes will be removed
1508
 * from included int the returned list.
1509
 *
1510
 * If the $entity_list is not an array the $excludes will be returned.
1511
 */
1512
function filter_cdm_entity_list($entity_list, $excludes) {
1513
  if (is_array($entity_list)) {
1514
    $result = $entity_list;
1515
    if ($excludes) {
1516
      foreach ($excludes as $exclude) {
1517
        if (!contains_cdm_entitiy($exclude, $entity_list)) {
1518
          $result[] = $exclude;
1519
        }
1520
      }
1521
    }
1522
  }
1523
  else {
1524
    $result = $excludes;
1525
  }
1526
  return $result;
1527
}
1528

    
1529
/**
1530
 * Wraps the given $html string into a render array suitable for drupal_render()
1531
 *
1532
 * @param $html
1533
 *   the html string, see
1534
 *   http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#markup
1535
 * @param $weight
1536
 *   A positive or negative number (integer or decimal).
1537
 *   see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#weightval
1538
 * @param $prefix
1539
 *   Optional markup for the '#prefix' element of the render array
1540
 * @param $suffix
1541
 *   Optional markup for the '#suffix' element of the render array
1542
 *
1543
 * @return array
1544
 *   A render array
1545
 *
1546
 */
1547
function markup_to_render_array($html, $weight = FALSE, $prefix = NULL, $suffix = NULL) {
1548
  $render_array = array(
1549
    '#markup' => $html
1550
      );
1551
  if (is_numeric($weight)) {
1552
    $render_array['#weight'] = $weight;
1553
  }
1554
  if($prefix){
1555
    $render_array['#prefix'] = $prefix;
1556
  }
1557
  if($suffix) {
1558
    $render_array['#suffix'] = $suffix;
1559
  }
1560
  return $render_array;
1561
}
1562

    
1563
/**
1564
 * Loads the subgraph of a given PolytomousKeyNode.
1565
 *
1566
 * Loads the subgraph of the given PolytomousKeyNode recursively from
1567
 * the CDM REST service.
1568
 *
1569
 * @param mixed $polytomousKeyNode
1570
 *   PolytomousKeyNode passed by reference.
1571
 *
1572
 * @return void
1573
 */
1574
function _load_polytomousKeySubGraph(&$polytomousKeyNode) {
1575

    
1576
  if (!$polytomousKeyNode) {
1577
    return;
1578
  }
1579
  if ($polytomousKeyNode->class != "PolytomousKeyNode") {
1580
    drupal_set_message('_load_polytomousKeySubGraph(): ' . t('invalid type given.'), 'error');
1581
    return;
1582
  }
1583
  if (!is_uuid($polytomousKeyNode->uuid)) {
1584
    drupal_set_message('_load_polytomousKeySubGraph(): ' . t('invalid type given.'), 'error');
1585
    return;
1586
  }
1587

    
1588
  $polytomousKeyNode = cdm_ws_get(CDM_WS_PORTAL_POLYTOMOUSKEY_NODE, $polytomousKeyNode->uuid);
1589

    
1590
  if (!$polytomousKeyNode) {
1591
    // drupal_set_message("_load_polytomousKeyChildNodes() : could not load polytomousKeyNode", "error");
1592
    return;
1593
  }
1594

    
1595
  // Load children.
1596
  foreach ($polytomousKeyNode->children as &$childNode) {
1597
    _load_polytomousKeySubGraph($childNode);
1598
  }
1599
  return;
1600
}
1601

    
1602
/**
1603
 * @todo Please document this function.
1604
 * @see http://drupal.org/node/1354
1605
 */
1606
function cdm_dataportal_view_polytomousKey($polytomousKeyUuid) {
1607

    
1608
  cdm_check_valid_portal_page();
1609

    
1610
  $polytomousKey = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, $polytomousKeyUuid);
1611
  if (!$polytomousKey) {
1612
    drupal_set_title(t('Polytomous key does not exist'), PASS_THROUGH);
1613
    return "";
1614
  }
1615

    
1616
  $sourcePager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'sources'));
1617
  if (is_array($sourcePager->records)) {
1618
    $polytomousKey->sources = $sourcePager->records;
1619
    // $polytomousKey->sources->citation = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'sources'));
1620
  }
1621

    
1622
  $annotationPager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'annotations'));
1623
  if (is_array($annotationPager->records)) {
1624
    $polytomousKey->annotations = $annotationPager->records;
1625
  }
1626

    
1627
  _load_polytomousKeySubGraph($polytomousKey->root);
1628
  return theme('cdm_polytomousKey_page', array('polytomousKey' => $polytomousKey));
1629
}
1630

    
1631
/**
1632
 * Creates a taxon page view or a chapter of it.
1633
 *
1634
 * The taxon page gives detailed information on a taxon, it shows:
1635
 *  - Taxon name.
1636
 *  - Full list of synonyms homotypic synonyms on top, followed by the
1637
 *    heterotypic and finally followed by misapplied names.
1638
 *    The list is ordered historically.
1639
 *  - All description associated with the taxon.
1640
 *
1641
 * @param string $uuid
1642
 * @param string $chapter
1643
 *   Name of the part to display, valid values are:
1644
 *   'description', 'images', 'synonymy', 'specimens', 'all'.
1645
 *
1646
 * @return string
1647
 */
1648
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all') {
1649

    
1650
  cdm_check_valid_taxon_page($chapter);
1651
  cdm_dd("START OF TAXON PAGE [" . $chapter . "] " . $uuid . ' for ' . $_GET['q']);
1652

    
1653

    
1654
  // Display the page for the taxon defined by $uuid.
1655
  // set_last_taxon_page_tab(arg(3));
1656
  $taxonpage = cdm_dataportal_taxon_view($uuid, $chapter);
1657
  if (!empty($taxonpage)) {
1658
    cdm_dd("END OF TAXON PAGE [" . $chapter . "] " . $uuid);
1659
    return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title, $taxonpage->content);
1660
  }
1661
  else {
1662
    cdm_dd("END OF TAXON PAGE [" . $chapter . "] " . $uuid . ' !!! PAGE IS EMPTY !!!');
1663
    return '';
1664
  }
1665
}
1666

    
1667
/**
1668
 * This function will generate the taxon page part ($chapter) and returns a taxonpage object
1669
 * which has two fields, one for the page title and one for the content. Later on in the
1670
 * process chain the value contained in these fields will be passed to the cdm_node_show()
1671
 * function as the function parameters $title and $content.
1672
 *
1673
 * @param string $uuid
1674
 *   the uuid of the taxon to show
1675
 * @param string $chapter
1676
 *   Name of the part to display, valid values are:
1677
 *   'description', 'images', 'synonymy', 'all'.
1678
 *
1679
 * @return object with the following fields:
1680
 *   - title : the title of the page
1681
 *   - content: the content of the page
1682
 *
1683
 * @throws Exception
1684
 *
1685
 */
1686
function cdm_dataportal_taxon_view($uuid, $chapter = 'all') {
1687
  // Taxon object.
1688
  $taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $uuid);
1689
  if (empty($taxon)) {
1690
    drupal_set_title(t('Taxon does not exist'), PASS_THROUGH);
1691
    return null;
1692
  }
1693
  $taxonpage = new stdClass();
1694

    
1695
  $taxonpage->title = theme('cdm_taxon_page_title', array(
1696
    'taxon' => $taxon
1697
  ));
1698

    
1699
  // Check if the taxon id contained in the currently selected tree.
1700
  $taxon_in_current_classification = taxon_in_current_classification($uuid);
1701

    
1702
  if (!$taxon_in_current_classification) {
1703
    $classifications = get_classifications_for_taxon($taxon);
1704
    RenderHints::pushToRenderStack('not_in_current_classification');
1705
    RenderHints::setFootnoteListKey('not_in_current_classification');
1706
    FootnoteManager::blockFootnotesFor('not_in_current_classification');
1707
    $taxon_name_markup = render_taxon_or_name($taxon);
1708
    FootnoteManager::unblockFootnotesFor('not_in_current_classification');
1709
    RenderHints::clearFootnoteListKey();
1710

    
1711
    if (count($classifications) == 0) {
1712
      drupal_set_message(t('This concept of the taxon !taxonname is not contained as an accepted taxon in the currently chosen classification.',
1713
        array(
1714
        '!taxonname' => $taxon_name_markup,
1715
        )
1716
      ), 'warning');
1717
    }
1718
    else {
1719
      $trees = '';
1720
      foreach ($classifications as $classification) {
1721
        if (isset($classification->titleCache)) {
1722
          $trees .= ($trees ? ', ' : '') . '<strong>' . $classification->titleCache . '</strong>';
1723
        }
1724
      }
1725

    
1726
      drupal_set_message(format_plural(count($trees),
1727
          'This concept of the taxon !taxonname is not contained as an accepted taxon in the currently chosen classification, but in this one: !trees',
1728
          'This concept of the taxon !taxonname is not contained as an accepted taxon in the currently chosen classification, but in one of these: !trees',
1729
          array('!taxonname' => $taxon_name_markup, '!trees' => $trees)
1730
        ) ,
1731
        'warning');
1732
    }
1733
    RenderHints::popFromRenderStack();
1734
  }
1735

    
1736
  // Render the taxon page.
1737
  $render_array = compose_cdm_taxon_page($taxon, $chapter);
1738
  $taxonpage->content = drupal_render($render_array);
1739

    
1740
  return $taxonpage;
1741
}
1742

    
1743

    
1744
function cdm_dataportal_specimen_by_accession_number_page_view($accession_number)
1745
{
1746
  cdm_check_valid_portal_page();
1747

    
1748
  $specimenpage = specimen_by_accession_number_view($accession_number);
1749

    
1750
  if (!empty($specimenpage)) {
1751
    return cdm_node_show_simulate($specimenpage->content);
1752
  }
1753
  else {
1754
    return '';
1755
  }
1756

    
1757

    
1758
}
1759

    
1760
/**
1761
 * Creates a specimen page view.
1762
 * @param string $uuid the UUID of the specimen
1763
 * @return array|string
1764
 */
1765
function cdm_dataportal_specimen_page_view($uuid) {
1766

    
1767
    cdm_check_valid_portal_page();
1768
    $specimenpage = cdm_dataportal_specimen_view($uuid);
1769
    if (!empty($specimenpage)) {
1770
        return cdm_node_show_simulate($specimenpage->content);
1771
    }
1772
    else {
1773
        return '';
1774
    }
1775
}
1776

    
1777

    
1778
/**
1779
 * Creates a specimen page view.
1780
 * @param string $uuid the UUID of the specimen
1781
 * @return object
1782
 *  The page object with the following fields:
1783
 *   - 'title': the page title
1784
 *   - 'content' : the page content rendered as markup
1785
 */
1786
function specimen_by_accession_number_view($accession_number = null) {
1787

    
1788
  $field_unit_dto = cdm_ws_get(CDM_WS_OCCURRENCE_ACCESSION_NUMBER, null, 'accessionNumber=' . $accession_number);
1789

    
1790
  // Display the page for the specimen defined by $uuid.
1791
  if (isset($field_unit_dto)){
1792
    $dto_array = array($field_unit_dto);
1793
    $specimen_array = specimen_render_array_items($dto_array);
1794
    $specimen_table = array(
1795
      '#theme' => 'table',
1796
      '#weight' => 2,
1797
      // prefix attributes and rows with '#' to let it pass toF the theme function,
1798
      // otherwise it is handled as child render array
1799
      '#attributes' => array('class' => 'specimens'),
1800
      '#rows' => array(),
1801
      '#prefix' => '<div id="specimens">',
1802
      '#suffix' => '</div>',
1803
    );
1804
    foreach($specimen_array as $value){
1805
      $renderArray = array(
1806
        '#theme' => 'item_list',
1807
        '#items' => array($value),
1808
        '#type' => 'ul');
1809
      $output = drupal_render($renderArray);
1810
      $specimen_table['#rows'][] = array(
1811
        // An array of table rows. Every row is an array of cells, or an associative array
1812
        'data' => array($output),
1813
        'class' =>  array(
1814
          'descriptionElement',
1815
          'descriptionElement_IndividualsAssociation'
1816
        ),
1817
      );
1818

    
1819

    
1820
    }
1821
    $specimenpage = new stdClass();
1822

    
1823
    $specimenpage->title = theme('theme_cdm_specimen_dto_page_title', array(
1824
      'specimen' => $field_unit_dto
1825
    ));
1826

    
1827
     $render_array['markup'] = $specimen_table;
1828
     $specimenpage->content = drupal_render($render_array);
1829
  }
1830

    
1831
  return $specimenpage;
1832
}
1833

    
1834

    
1835
/**
1836
 *
1837
 * Creates a specimen view.
1838
 * @param string $uuid the UUID of the specimen
1839
 * @return object
1840
 *    Page object with the fields
1841
 *    - title
1842
 *    - content
1843
 */
1844
function cdm_dataportal_specimen_view($uuid) {
1845
    //TODO: get the derivateDTO and don't call the webservice a second time in compose_cdm_specimen_page
1846
    $specimen = cdm_ws_get(CDM_WS_PORTAL_OCCURRENCE, $uuid);
1847
    if (empty($specimen)) {
1848
        drupal_set_title(t('Specimen does not exist'), PASS_THROUGH);
1849
        return FALSE;
1850
    }
1851
    $specimenpage = new stdClass();
1852

    
1853
    $specimenpage->title = specimen_page_title($specimen);
1854

    
1855
    // Render the specimen page.
1856
    $render_array = compose_cdm_specimen_page($specimen);
1857
    $specimenpage->content = drupal_render($render_array);
1858

    
1859
    return $specimenpage;
1860
}
1861

    
1862
/**
1863
 *
1864
 * Creates a description view.
1865
 *
1866
 * @param string $uuid the UUID of the description
1867
 * @param null $descriptive_dataset_uuid the uuid of the data set which defines
1868
 * the display of the description detail page
1869
 *
1870
 * @return object Page object with the fields
1871
 * Page object with the fields
1872
 * - title
1873
 * - content
1874
 */
1875
function cdm_dataportal_description_view($uuid, $descriptive_dataset_uuid = NULL) {
1876
    $description = cdm_ws_get(CDM_WS_PORTAL_DESCRIPTION, $uuid);
1877
    $description_page = new stdClass();
1878
    if (empty($description)) {
1879
      $description_page->title = t('Description does not exist');
1880
      drupal_set_title($description_page->title, PASS_THROUGH); // not necessary if correctly implemented as drupal node
1881
      $description_page->content = "";
1882

    
1883
    } else {
1884
      $description_page->title = $description->titleCache;
1885
      drupal_set_title($description_page->title); // not necessary if correctly implemented as drupal node
1886

    
1887
      // Render the description page.
1888
      $render_array = compose_description_table($description->uuid, $descriptive_dataset_uuid);
1889
      if($render_array['title']){
1890
        $page_title = strip_tags($render_array['title']['#markup']);
1891
        drupal_set_title($page_title);
1892
        unset($render_array['title']);
1893
      }
1894
      $description_page->content = drupal_render($render_array);
1895
    }
1896

    
1897
    return $description_page;
1898
}
1899

    
1900
function cdm_dataportal_description_page_view($uuid, $descriptive_dataset_uuid = NULL) {
1901

    
1902
  cdm_check_valid_portal_page();
1903
  $descriptionpage = cdm_dataportal_description_view($uuid, $descriptive_dataset_uuid);
1904
  if (!empty($descriptionpage)) {
1905
    return cdm_node_show_simulate($descriptionpage->content);
1906
  }
1907
  else {
1908
    return NULL;
1909
  }
1910
}
1911

    
1912
/**
1913
 *
1914
 * Creates a named area view.
1915
 * @param string $uuid the UUID of the specimen
1916
 *  * @return object
1917
 *   An object with two fields:
1918
 *     - title: the page title
1919
 *     - content: the page content
1920
 */
1921

    
1922
function cdm_dataportal_named_area_view($uuid) {
1923
  $named_area = cdm_ws_get(CDM_WS_PORTAL_TERM, $uuid);
1924
  if (empty($named_area) || $named_area->class !== 'NamedArea') {
1925
    drupal_set_title(t('Named area does not exist'), PASS_THROUGH);
1926
    return null;
1927
  }
1928
  $named_area_page = new stdClass();
1929

    
1930
  $named_area_page->title = $named_area->representation_L10n;
1931

    
1932
  // Render the specimen page.
1933
  $render_array = compose_cdm_named_area_page($uuid);
1934
  $named_area_page->content = drupal_render($render_array);
1935

    
1936
  return $named_area_page;
1937
}
1938

    
1939
function cdm_dataportal_named_area_page_view($uuid) {
1940

    
1941
  cdm_check_valid_portal_page();
1942

    
1943
  $named_area_page = cdm_dataportal_named_area_view($uuid);
1944
  if (!empty($named_area_page)) {
1945
    return cdm_node_show(NODETYPE_NAME, $uuid, $named_area_page->title, $named_area_page->content);
1946
  }
1947
  else {
1948
    return '';
1949
  }
1950

    
1951

    
1952
}
1953

    
1954
/**
1955
 * Returns a name page as a Drupal node ready to be renderized by Drupal.
1956
 *
1957
 * The node page shows the taxon name title and the list of taxon related
1958
 * with such taxon. Name on the tree already in use.
1959
 *
1960
 * @param string $taxon_name_uuid
1961
 *   The uuid of the CDM TaxonName to show a name page for
1962
 * @param string $taxon_to_hide_uuid
1963
 *   A taxon which should not be displayed in the taxon list
1964
 * @param $redirect_to_taxon
1965
 *   Automatically redirect to the related taxon if there is only one
1966
 *   accepted taxon for this name.
1967
 * @param string $highlite_synonym_uuid
1968
 *   Optinal parameter wich takes another taxon uuid, if given the
1969
 *   target taxon pages will show the syonymy tab where the taxon
1970
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1971
 *   in case it is found on this page.
1972
 *
1973
 * @return mixed
1974
 *   The formatted name page as node.
1975
 */
1976
function cdm_dataportal_name_page_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid = NULL, $redirect_to_taxon = false) {
1977

    
1978
  cdm_check_valid_portal_page();
1979

    
1980
  $taxonname_page = cdm_dataportal_name_view(
1981
    is_uuid($taxon_name_uuid) ? $taxon_name_uuid : null,
1982
    is_uuid($taxon_to_hide_uuid) ? $taxon_to_hide_uuid : null,
1983
    $redirect_to_taxon == 'redirect_to_taxon',
1984
    is_uuid($synonym_uuid) ? $synonym_uuid : null);
1985
  if (!empty($taxonname_page)) {
1986
    return cdm_node_show(NODETYPE_NAME, $taxon_name_uuid, $taxonname_page->title, $taxonname_page->content);
1987
  }
1988
  else {
1989
    return '';
1990
  }
1991
}
1992

    
1993
/**
1994
 * View function for a TaxonName page.
1995
 *
1996
 * The name page lists all taxa for which the name specified by the
1997
 * $taxon_name_uuid is being used. I case there is only one name the
1998
 * page automatically redirects ti the according taxon page. Otherwise
1999
 * the list of names is displayed.
2000
 *
2001
 * The parameter $taxon_to_hide_uuid allows to exclude a taxon from the
2002
 * list of taxa. This is useful for example when referencing from a taxon
2003
 * to the name page and the referring taxon should not be repeaded in the
2004
 * name page.
2005
 *
2006
 *
2007
 * @param string $taxon_name_uuid
2008
 *   The uuid of the CDM TaxonName to show a name page for
2009
 * @param string $taxon_to_hide_uuid
2010
 *   A taxon which should not be displayed in the taxon list
2011
 * @param $redirect_to_taxon
2012
 *   Automatically redirect to the related taxon if there is only one
2013
 *   accepted taxon for this name.
2014
 * @param string $highlite_synonym_uuid
2015
 *   Optional parameter which takes another taxon uuid, if given the
2016
 *   target taxon pages will show the synonymy tab where the taxon
2017
 *   referenced by the $highlite_synonym_uuid will be highlighted
2018
 *   in case it is found on this page.
2019
 *
2020
 * @return object|boolean
2021
 *   An object with two fields:
2022
 *     - title: the page title
2023
 *     - content: the page content
2024
 *   or FALSE
2025
 *
2026
 */
2027
function cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $redirect_to_taxon, $highlite_synonym_uuid = NULL) {
2028
  // Getting the full taxonname object from the server.
2029
  $taxon_name = cdm_ws_get(CDM_WS_PORTAL_NAME, array($taxon_name_uuid));
2030
  if (!$taxon_name) {
2031
    drupal_set_title(t('Taxon name does not exist'), PASS_THROUGH);
2032
    return FALSE;
2033
  }
2034
  cdm_load_tagged_full_title($taxon_name);
2035
  // Searching for all the taxa connected with the taxon name on the tree
2036
  // in use.
2037
  $name = cdm_ws_get(CDM_WS_NAME, array($taxon_name_uuid));
2038
  $restrictions = array(new Restriction("name.titleCache","EXACT", array($name->titleCache), 'AND'));
2039
  $init_strategy = array(
2040
    "name.titleCache",
2041
    "name.nomenclaturalReference.authorship",
2042
    "name.nomenclaturalReference.inReference.authorship",
2043
    "name.nomenclaturalReference.inReference.inReference.authorship",
2044
    "name.nomenclaturalReference.inReference.inReference.inReference.authorship"
2045
  );
2046
  $taxa = cdm_ws_fetch_all_by_restriction("Taxon", NULL, $restrictions, $init_strategy);
2047

    
2048
  // Removing the name where we came from.
2049
  foreach ($taxa as $k => &$taxon) {
2050
    if ($taxon->uuid == $taxon_to_hide_uuid) {
2051
      unset($taxa[$k]);
2052
    }
2053
  }
2054
  // Show the taxa list or go to the singular taxon.
2055
  if (sizeof($taxa) == 1 && $redirect_to_taxon) {
2056
    // redirect to the taxon if there is only one.
2057
    $singleTaxon = array_pop($taxa);
2058
    if ($singleTaxon->class != "Taxon") {
2059
      // It is a Synonym -> look for the accepted.
2060
      $accepted_taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, array($singleTaxon->uuid), 'classificationFilter=' . get_current_classification_uuid());
2061
      if (!empty($highlite_synonym_uuid)) {
2062
        drupal_goto('cdm_dataportal/taxon/' . $accepted_taxon->uuid . '/synonymy', array('query' => array('highlite' => $highlite_synonym_uuid)));
2063
      }
2064
      else {
2065
        drupal_goto('cdm_dataportal/taxon/' . $accepted_taxon->uuid . '/synonymy', array('query' => array('highlite' => $singleTaxon->uuid)));
2066
      }
2067
    }
2068
    else {
2069
      // It is an accepted taxon.
2070
      if (!empty($highlite_synonym_uuid)) {
2071
        drupal_goto('cdm_dataportal/taxon/' . $singleTaxon->uuid . '/synonymy', array('query' => array('highlite' => $highlite_synonym_uuid)));
2072
      }
2073
      else {
2074
        drupal_goto('cdm_dataportal/taxon/' . $singleTaxon->uuid);
2075
      }
2076
    }
2077
  }
2078
  else {
2079
    // display the name page content
2080
    $taxon_name_page = new stdClass();
2081
    $taxon_name_page->title = theme('cdm_name_page_title', array('taxon_name' => $taxon_name));
2082
    $content = array();
2083

    
2084
    RenderHints::pushToRenderStack('name_page');
2085
    RenderHints::setFootnoteListKey('name_page');
2086
    $content['taxon_name'] = markup_to_render_array(render_taxon_or_name($taxon_name), null, '<div class="name-page-name">', '</div>');
2087

    
2088

    
2089
    // name relationships
2090
    $name_relations = cdm_ws_fetch_all(str_replace("$0", $taxon_name->uuid, CDM_WS_PORTAL_NAME_NAME_RELATIONS));
2091
    if(count($name_relations) > 0){
2092
      $content['name_relationships'] = compose_name_relationships_list($name_relations, $taxon_name->uuid, null);
2093
    }
2094

    
2095
    // type designations
2096
    $type_designations_render_array = compose_type_designations($taxon_name->uuid);
2097
    $content = array_merge($content, $type_designations_render_array);
2098
    // registrations
2099
    $registrationDTOs= cdm_ws_fetch_all(CDM_WS_REGISTRATION_DTO,  array('nameUuid' => $taxon_name_uuid));
2100
    if(isset($registrationDTOs) && count($registrationDTOs ) > 0 ){
2101
      $content['registrations'][] = markup_to_render_array('<h2>' . t("Registrations") . '</h2>') ;
2102
      foreach ($registrationDTOs as $regDTO){
2103
        $content['registrations'][] = compose_registration_dto_compact($regDTO);
2104
      }
2105
    }
2106

    
2107

    
2108
    // related taxa
2109
    $show_taxa_section = variable_get(CDM_NAME_PAGE_SECTION_TAXA, CDM_NAME_PAGE_SECTION_TAXA_DEFAULT);
2110
    if($show_taxa_section){
2111
      if ($taxa) {
2112
        $content['related_taxa_header'] = markup_to_render_array("<h2>Taxa for this name</h2>");
2113
        $content['related_taxa'] = compose_list_of_taxa($taxa);
2114
      }
2115
      else {
2116
        $content['related_taxa'] = markup_to_render_array('This name is not assigned to a taxon.', null, '<div class="no-taxon-message">', '</div>');
2117
      }
2118
    }
2119

    
2120
    $content['footnotes'] = markup_to_render_array(render_footnotes());
2121

    
2122
    $taxon_name_page->content = $content;
2123
    RenderHints::popFromRenderStack();
2124
    RenderHints::clearFootnoteListKey();
2125
    return $taxon_name_page;
2126
  }
2127
}
2128

    
2129

    
2130
/**
2131
 * Returns a registration page as a Drupal node to be rendered by Drupal.
2132
 *
2133
 * @param string  $registration_identifie
2134
 *   The persistent identifier of the registration urlencoded.
2135
 * @return mixed
2136
 *   The formatted registration page as node.
2137
 */
2138
function cdm_dataportal_registration_page_view() {
2139

    
2140
  cdm_check_valid_portal_page("/\/cdm_dataportal\/registration/");
2141
  $registration_identifier = $_REQUEST['identifier'];
2142
  $registration_page = cdm_dataportal_registration_view($registration_identifier);
2143
  return cdm_node_show_simulate($registration_page);
2144
}
2145

    
2146
/**
2147
 * @param $registration_identifier
2148
 * @return array
2149
 *   The drupal render array for the registration view.
2150
 */
2151
function cdm_dataportal_registration_view($registration_identifier) {
2152

    
2153
  RenderHints::pushToRenderStack('registration_page');
2154
  RenderHints::setFootnoteListKey('registration_page');
2155

    
2156
  $render_array = array();
2157
  $registration_dto = cdm_ws_get(CDM_WS_REGISTRATION_DTO, null, 'identifier=' . $registration_identifier);
2158
  if($registration_dto){
2159

    
2160
    drupal_set_title(t('Registration Id:') . ' ' . $registration_identifier, PASS_THROUGH);
2161
    $render_array = compose_registration_dto_full($registration_dto, true);
2162

    
2163
  } else {
2164
    $status_text = cdm_ws_get(CDM_WS_REGISTRATION_STATUS, null, 'identifier=' . $registration_identifier);
2165
    if(isset($status_text->String)) {
2166
      $status_text = strtolower($status_text->String);
2167
      if($status_text == 'preparation' || $status_text == 'curation'){
2168
        $status_text = 'in ' . $status_text;
2169
      }
2170
      drupal_set_title(t('Registration ' . $status_text), PASS_THROUGH);
2171
      //$status_message;
2172
      drupal_set_message("A registration with the identifier  " . $registration_identifier . " is " . $status_text, "status");
2173
    } else {
2174
      drupal_set_title(t('Registration not found'), PASS_THROUGH);
2175
      drupal_set_message("A registration with the identifier  " . $registration_identifier . " does not exist", "warning");
2176
    }
2177
  }
2178

    
2179
  $render_array = array(
2180
    '#prefix' => '<div id="registration">',
2181
    'registration' => $render_array,
2182
    '#suffix' => '</div>',
2183
  );
2184

    
2185
  RenderHints::popFromRenderStack();
2186
  RenderHints::clearFootnoteListKey();
2187

    
2188
  return $render_array ;
2189
}
2190

    
2191

    
2192
/**
2193
 * Creates a page with the advance search form.
2194
 *
2195
 * NOTE: The advance search form allows searching for taxa.
2196
 */
2197
function cdm_dataportal_view_search_advanced() {
2198
  drupal_set_title(t('Advanced search'), PASS_THROUGH);
2199
  return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
2200
}
2201

    
2202
/**
2203
 * Creates a page with the blast search form.
2204
 *
2205
 * NOTE: The advance search form allows searching for specimen in blast DB.
2206
 */
2207
function cdm_dataportal_view_search_blast() {
2208
    drupal_set_title(t('Blast search'), PASS_THROUGH);
2209
    return drupal_get_form('cdm_dataportal_search_blast_form');
2210
}
2211

    
2212
/**
2213
 * Creates a page with the search form for searching by taxon descriptions.
2214
 */
2215
function cdm_dataportal_view_search_taxon_by_description() {
2216
  drupal_set_title(t('Search by factual data'), PASS_THROUGH);
2217
  return drupal_get_form('cdm_dataportal_search_taxon_by_description_form');
2218
}
2219

    
2220
/**
2221
 * Executes the search and generates the result list of taxa.
2222
 */
2223
function cdm_dataportal_view_search_results_taxon() {
2224

    
2225
  $taxonPager = cdm_dataportal_search_taxon_execute();
2226

    
2227
  $showThumbnails = do_showThumbnails();
2228

    
2229
  $setSessionUri = url('cdm_api/setvalue/session', array(
2230
      'query' => array('var' => '[pageoption][searchtaxa][showThumbnails]', 'val' => ''),
2231
  ));
2232

    
2233
  drupal_add_js('jQuery(document).ready(function() {
2234

    
2235
      // init
2236
      if(' . $showThumbnails . ' == 1){
2237
          jQuery(\'.media_gallery\').show(20);
2238
      } else {
2239
          jQuery(\'.media_gallery\').hide(20);
2240
      }
2241

    
2242
      // add change handler
2243
      jQuery(\'#showThumbnails input.showThumbnails\').change(
2244
      function(event){
2245
        var state = 0;
2246
        if(jQuery(this).is(\':checked\')){
2247
          jQuery(\'.media_gallery\').show(20);
2248
          state = 1;
2249
        } else {
2250
          jQuery(\'.media_gallery\').hide(20);
2251
        }
2252
        // store state in session variable
2253
        var uri = \'' . $setSessionUri . '\' + state;
2254
        jQuery.get(uri);
2255
      });
2256
  });',
2257
  array('type' => "inline", 'scope' => JS_DEFAULT));
2258

    
2259
  drupal_set_title(t('Search results'), PASS_THROUGH);
2260

    
2261
  return theme('cdm_search_taxa_results', array(
2262
    'pager' => $taxonPager,
2263
    'path' => 'cdm_dataportal/search/results/taxon',
2264
    ));
2265
}
2266

    
2267
/**
2268
 * Executes the blast search and generates the result list of specimen.
2269
 */
2270
function cdm_dataportal_view_search_results_specimen() {
2271

    
2272
    $specimenPager = cdm_dataportal_search_blast_execute();
2273

    
2274
    return theme('cdm_search_specimen_results', array(
2275
        'pager' => $specimenPager,
2276
        'path' => 'cdm_dataportal/search/results/specimen',
2277
    ));
2278
}
2279

    
2280

    
2281
/**
2282
 * Executes the search for registrations and generates the result list..
2283
 */
2284
function cdm_dataportal_view_search_registrations_results($mode = 'filter') {
2285

    
2286
  switch($mode ){
2287
    case 'taxongraph':
2288
      $block = block_load('cdm_dataportal', 'registrations_search_taxongraph');
2289
      $registration_pager = cdm_dataportal_search_registrations_taxongraph_execute();
2290
      break;
2291
    case 'filter':
2292
    default:
2293
      $block = block_load('cdm_dataportal', 'registrations_search_filter');
2294
      $registration_pager = cdm_dataportal_search_registrations_filter_execute();
2295
  }
2296
  $block->title = null;
2297

    
2298
  drupal_set_title(t('Search registrations'), PASS_THROUGH);
2299

    
2300
  $render_array = _block_get_renderable_array(_block_render_blocks(array($block)));
2301
  $registrations_pager_array = compose_search_results($registration_pager, new RegistrationDTOComposeHandler());
2302
  $render_array = array_merge($render_array, $registrations_pager_array);
2303

    
2304
  return $render_array;
2305
}
2306

    
2307
/**
2308
 * Executes the search for registrations and generates the result list..
2309
 */
2310
function cdm_dataportal_view_search_agent() {
2311

    
2312
  $render_array = [];
2313
  $title = isset($_REQUEST['class']) && $_REQUEST['class'] ? $_REQUEST['class'] : 'Persons & Teams';
2314
  drupal_set_title(t($title), PASS_THROUGH);
2315
  // TODO $block = block_load('cdm_dataportal', 'search_agent_filter');
2316
  // $block->title = null;
2317
  // $render_array = _block_get_renderable_array(_block_render_blocks(array($block)));
2318
  $pager = cdm_dataportal_search_agent_execute();
2319
  $pager_render_array = compose_search_results($pager, new AgentComposeHandler());
2320
  $render_array = array_merge($render_array, $pager_render_array);
2321
  return $render_array;
2322
}
2323

    
2324

    
2325
/**
2326
 * Provides the standard image which indicated a loading process
2327
 *
2328
 * @return string
2329
 *  The img html tag
2330
 */
2331
function loading_image_html() {
2332
  return '<img class="loading" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal')
2333
    . '/images/loading_circle_grey_16.gif" style="display:none;">';
2334
}
2335

    
2336
/**
2337
 * Returns the state of the the showThumbnails flag set in the
2338
 * users session ($_SESSION['pageoption']['searchtaxa']['showThumbnails']).
2339
 *
2340
 * @return boolean
2341
 *    returns 1 if the flag is set
2342
 */
2343
function do_showThumbnails() {
2344
  static $showThumbnails = null;
2345

    
2346
  if($showThumbnails == null) {
2347
    $showThumbnails = 0;
2348
    if (!isset($_SESSION['pageoption']['searchtaxa']['showThumbnails'])) {
2349
      $showThumbnails = 0;
2350
      $search_gallery_settings = variable_get(CDM_DATAPORTAL_SEARCH_GALLERY_NAME, null);
2351
      $showThumbnails = is_array($search_gallery_settings)
2352
        && isset($search_gallery_settings['cdm_dataportal_show_taxon_thumbnails'])
2353
        && (
2354
            $search_gallery_settings['cdm_dataportal_show_taxon_thumbnails'] +
2355
            $search_gallery_settings['cdm_dataportal_show_synonym_thumbnails'] +
2356
            $search_gallery_settings['cdm_dataportal_show_thumbnail_captions'] > 0
2357
            )
2358
         ? 1 : 0;
2359

    
2360
       drupal_array_set_nested_value($_SESSION, array('pageoption', 'searchtaxa', 'showThumbnails'), $showThumbnails);
2361
    }
2362
    $showThumbnails = $_SESSION['pageoption']['searchtaxa']['showThumbnails'];
2363
    if (!is_numeric($showThumbnails)) {
2364
      $showThumbnails = 1;
2365
    }
2366
  }
2367

    
2368
  return $showThumbnails;
2369
}
2370

    
2371

    
2372

    
2373
/* ====================== other functions ====================== */
2374
/**
2375
 * Creates a URL to the taxon page specified by the $uuid parameter.
2376
 *
2377
 * The URL will be prepended with a path element to a specific taxon page tab.
2378
 *
2379
 * This tab is either taken from the CDM_DATAPORTAL_DEFAULT_TAXON_TAB which can
2380
 * be set globally in the administrative settings or individually in the user
2381
 * profile. If the CDM_DATAPORTAL_DEFAULT_TAXON_TAB value is set to LAST_VISITED_TAB
2382
 * the last portal will stay on this last tab.
2383
 *
2384
 * A third option is offerered by the $page_tab parameter which allows overwriting this
2385
 * internal mechanism by a specific value.
2386
 *
2387
 * @param string $uuid
2388
 *   The UUID of the taxon.
2389
 * @param string $page_tab
2390
 *   Overwriting the preset mechanism by defining specific value for the
2391
 *   taxon page tab.
2392
 * @return string
2393
 *   The created URL.
2394
 */
2395
function path_to_taxon($uuid, $page_tab = FALSE) {
2396

    
2397
  $tab = get_default_taxon_tab();
2398
  $values = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
2399

    
2400
  if (!$uuid) {
2401
    return FALSE;
2402
  }
2403

    
2404
  if ($page_tab) {
2405
    $url = 'cdm_dataportal/taxon/' . $uuid . '/' . $page_tab;
2406
  }
2407
  elseif (!$tab || strtolower($tab) == 'general') {
2408
    $url = 'cdm_dataportal/taxon/' . $uuid;
2409
  }
2410
  elseif (get_last_taxon_page_tab() &&   $tab == $values[CDM_DATAPORTAL_LAST_VISITED_TAB_ARRAY_INDEX]) {
2411
    $url = 'cdm_dataportal/taxon/' . $uuid . '/' . get_last_taxon_page_tab();
2412
  }
2413
  else {
2414
    $url = 'cdm_dataportal/taxon/' . $uuid . '/' . strtolower($tab);
2415
  }
2416
  return $url;
2417
}
2418

    
2419
function path_to_description($uuid, $descriptive_dataset_uuid = NULL) {
2420

    
2421
    if (!$uuid) {
2422
        return FALSE;
2423
    }
2424
    else {
2425
      return 'cdm_dataportal/description/' . $uuid
2426
        . (is_uuid($descriptive_dataset_uuid) ? '/' . $descriptive_dataset_uuid : '');
2427
    }
2428
}
2429

    
2430
function path_to_specimen($uuid) {
2431

    
2432
    if (!$uuid) {
2433
        return FALSE;
2434
    }
2435
    else {
2436
        return 'cdm_dataportal/occurrence/' . $uuid;
2437
    }
2438
}
2439

    
2440
function path_to_specimen_by_accession_number($accession_number) {
2441

    
2442
    if (!$accession_number) {
2443
        return FALSE;
2444
    }
2445
    else {
2446
        return 'cdm_dataportal/specimen/accession_number/' . $accession_number;
2447
    }
2448
}
2449

    
2450
function path_to_named_area($uuid) {
2451

    
2452
  if (!$uuid) {
2453
    return FALSE;
2454
  }
2455
  else {
2456
    return 'cdm_dataportal/named_area/' . $uuid;
2457
  }
2458
}
2459

    
2460
/**
2461
 * Creates a URL to show a synonmy in the according taxon page.
2462
 *
2463
 * The URL will point to the synonymy tab of the taxon page of the accepted taxon given as parameter $acceptedUuid.
2464
 * The resulting URI will include query parameters to highlight the synonym, and to optionally display
2465
 * the accepted taxons name in aform like "Foo bar is accepted taxon for Ree doo". The URI will also
2466
 * include the sysnonym uuid as fragment in order to let the browser scroll to the according location
2467
 * in the page
2468
 *
2469
 * @param string $synonymUuid
2470
 *    The uuid of the synonym
2471
 * @param string $acceptedUuid
2472
 *    The uuid of the according accepted taxon
2473
 * @return string
2474
 *    The URL to show a synonmy in the according taxon page
2475
 */
2476
function uri_to_synonym($synonymUuid, $acceptedUuid) {
2477
  $acceptedPath = path_to_taxon($acceptedUuid, "synonymy");
2478
  return url($acceptedPath, array(
2479
      'query' => array(
2480
        // highlite the synony in the synonymy
2481
        'highlite' => $synonymUuid,
2482
        // the taxon page is refered from a synonym and the synonym can optionally be named in the page title
2483
        // see theme_taxon_page_title()
2484
        'acceptedFor' => $synonymUuid
2485
      ),
2486
      'fragment' => $synonymUuid
2487
  ));
2488

    
2489
}
2490

    
2491
/**
2492
 * Composes the drupal path to the key identified by the uuid.
2493
 *
2494
 * @param string $keyType
2495
 *    the key typer corresponds to the specific class of the CDM
2496
 *    IdentificationKey. Possible values are
2497
 *      -PolytomousKey
2498
 *      -MultimediaKey
2499
 *      - ...
2500
 * @param string $keyUuid
2501
 *   The UUID of the key
2502
 */
2503
function path_to_key($keyType, $keyUuid) {
2504
  if (!$keyUuid || !$keyType) {
2505
    return FALSE;
2506
  }
2507
  $keyType{0} = strtolower($keyType{0});
2508
  return "cdm_dataportal/" . $keyType . "/$keyUuid";
2509
}
2510

    
2511
/**
2512
 * Composes the drupal path to the reference identified by the uuid.
2513
 *
2514
 * @param $uuid string String representation of the registration entity uuid.
2515
 *
2516
 * @return string
2517
 *  The drupal path
2518
 *
2519
 */
2520
function path_to_reference($uuid) {
2521
  if (!$uuid) {
2522
    return FALSE;
2523
  }
2524
  return 'cdm_dataportal/reference/' . $uuid;
2525
}
2526

    
2527
/**
2528
 * Composes the drupal path to the reference identified by the uuid.
2529
 *
2530
 * @param string $identifier
2531
 *  The persistent identifier of the registration entity (Registration.identifier).
2532
 * @return string
2533
 *  The drupal path
2534
 */
2535
function path_to_registration($identifier) {
2536

    
2537
  if(variable_get(CDM_REGISTRATION_PRESISTENT_IDENTIFIER_AS_LINK)){
2538
    return $identifier;
2539
  } else {
2540
    return 'cdm_dataportal/registration/' . urlencode($identifier);
2541
  }
2542
}
2543

    
2544
/**
2545
 * Creates the path to a cdm_dataportal taxon name page.
2546
 *
2547
 * @param string $taxon_name_uuid
2548
 *   The uuid as string of the CDM TaxonName to show a name page for
2549
 * @param string $taxon_to_hide_uuid
2550
 *   The uuid as string of a taxon which should not be displayed in the taxon list
2551
 * @param string $highlited_synonym_uuid
2552
 *   Optional parameter which takes another taxon uuid, if given the
2553
 *   target taxon pages will show the synonymy tab where the taxon
2554
 *   referenced by the $highlite_synonym_uuid will be highlighted
2555
 *   in case it is found on this page.
2556
 * @param $redirect_to_taxon
2557
 *   If true, the name page will redirect to the related taxon if there is a single one
2558
 *   for this name only.
2559
 *
2560
 * @return string
2561
 *  URI path element as string
2562
 */
2563
function path_to_name($name_uuid, $taxon_to_hide_uuid = NULL, $highlited_synonym_uuid  = NULL, $redirect_to_taxon = false) {
2564
  $res = FALSE;
2565
  if ($name_uuid) {
2566
    $res = 'cdm_dataportal/name/' . $name_uuid .
2567
    '/' . (is_uuid($taxon_to_hide_uuid) ? $taxon_to_hide_uuid : "null") .
2568
    '/' . (is_uuid($highlited_synonym_uuid) ? $highlited_synonym_uuid : "null") .
2569
    '/' . ($redirect_to_taxon || variable_get(CDM_NAME_PAGE_AUTOREDIRECT, 0) ? "redirect_to_taxon" : "");
2570
  }
2571

    
2572
  return $res;
2573
}
2574

    
2575
/**
2576
 * Composes the drupal path to the media entity identified by the uuid.
2577
 *
2578
 * @param string $uuid
2579
 *  The persistent identifier of the entity entity
2580
 * @return string
2581
 *  The drupal path
2582
 */
2583
function path_to_media($uuid, $representaion_uuid = FALSE, $partId = FALSE) {
2584
  if (!$uuid) {
2585
    return FALSE;
2586
  }
2587
  $out = 'cdm_dataportal/media/' . $uuid;
2588
  if ($representaion_uuid) {
2589
    $out .= '/' . $representaion_uuid;
2590
    if (is_numeric($partId)) {
2591
      $out .= '/' . $partId;
2592
    }
2593
  }
2594
  return $out;
2595
}
2596

    
2597
/**
2598
 * Compares thisRank with thatRank.
2599
 *
2600
 * Returns a negative integer, zero, or a positive integer
2601
 * as the of thisRank is higher than, equal to, or lower than thatRank.
2602
 * e.g:
2603
 * <ul>
2604
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
2605
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
2606
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
2607
 * </ul>
2608
 * <p>
2609
 * This compare logic of the underlying webservice is the
2610
 * <b>inverse logic</b> of the the one implemented in
2611
 * java.lang.Comparable#compareTo(java.lang.Object)
2612
 *
2613
 * @param $thisRankUuid
2614
 * @param $thatRankUuid
2615
 *
2616
 * @return int
2617
 *   A negative integer, zero, or a positive integer
2618
 *   as the thisRank is lower than, equal to, or higher than thatRank.
2619
 */
2620
function rank_compare($thisRankUuid, $thatRankUuid) {
2621
  $result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
2622
  return $result->Integer;
2623
}
2624

    
2625
/**
2626
 * Creates a short version of a taxonname.
2627
 *
2628
 * The short name is created by using the taggedTitle field of
2629
 * TaxonNodeDTO instances.
2630
 * If the taggedTitle if empty the fullname will be returned.
2631
 *
2632
 * @param object $taxonNodeDTO
2633
 *   A TaxonNodeDTO object
2634
 *
2635
 * @return string
2636
 */
2637
function cdm_dataportal_shortname_of($taxonNodeDTO) {
2638

    
2639
  $nameStr = '';
2640

    
2641
  normalize_tagged_text($taxonNodeDTO->taggedTitle);
2642

    
2643
  // Get all tagged text tokens of the scientific name.
2644
  foreach ($taxonNodeDTO->taggedTitle as $tagtxt) {
2645
    if ($tagtxt->type == 'name' || $tagtxt->type == 'rank') {
2646
      $nameStr .= ($nameStr ? ' ' : '') . $tagtxt->text;
2647
    }
2648
  }
2649
  $nameStr = trim($nameStr);
2650

    
2651
  if ($nameStr) {
2652
    if(isset($taxonNodeDTO->status->symbol)){
2653
      $nameStr = $taxonNodeDTO->status->symbol . ' ' . $nameStr;
2654
    }
2655
    // Do not return short names for these.
2656
    if ($taxonNodeDTO->unplaced || $taxonNodeDTO->excluded) {
2657
      return $nameStr;
2658
    }
2659

    
2660
    /*
2661
    1st capture group (^[a-zA-Z]): First letter of uninomial.
2662
    Second capture group ([\p{L}]+): remaining letters of uninomial ([\p{L} = an UTF-8 letter).
2663
    Third capture group (\s+[^(\x2E]+\s+.+$|\s+[a-zA-Z]+$): letters of name,
2664
    but only matching if no '(' or '.' in second word of name,        ( \x2E = '.')
2665
    OR only one specific epithet \s+[\p{L}\x22\x2D\xD7]+$             (\x22= '"', \x2D='-', \xD7='×' )
2666
    */
2667
    $pattern = '/(^[a-zA-Z])([\p{L}]+)(\s+[^(\x2E]+\s+.+$|\s+[\p{L}\x22\x2D\xD7]+$)/u';
2668
    if (preg_match($pattern, $nameStr, $matches, PREG_OFFSET_CAPTURE)) {
2669
      return $matches[1][0] . "." . $matches[3][0];
2670
    }
2671
    else {
2672
      return $nameStr;
2673
    }
2674
  }
2675
  else {
2676
    return $taxonNodeDTO->titleCache;
2677
  }
2678
}
2679

    
2680
/**
2681
 * Check if a taxon is accepted by the current taxonomic tree.
2682
 *
2683
 * @param mixed $taxon
2684
 *   The Taxon obkect to check.
2685
 *
2686
 * @return bool
2687
 *   Returns TRUE if $taxon is accepted, FALSE otherwise.
2688
 */
2689
function _cdm_dataportal_acceptedByCurrentView($taxon) {
2690

    
2691
  $default_classification_uuid = get_current_classification_uuid();
2692

    
2693
  if (isset($taxon->taxonNodes)) {
2694
    $taxon_nodes = $taxon->taxonNodes;
2695
  }
2696
  else {
2697
    $taxon_nodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, $taxon->uuid);
2698
  }
2699

    
2700
  if ($taxon->class == "Taxon" && isset($taxon_nodes)) {
2701
    foreach ($taxon_nodes as $node) {
2702
      if($node->class == 'TaxonNodeDto' && isset($node->classificationUUID)){
2703
        // case of TaxonNodeDto
2704
        return $node->classificationUUID == $default_classification_uuid;
2705
      } else if($node->class == 'TaxonNode' && isset($node->classification->uuid)) {
2706
         return $node->classification->uuid == $default_classification_uuid;
2707
      }
2708
    }
2709
  }
2710
  return FALSE;
2711
}
2712

    
2713
/**
2714
 * Checks is the source has one of the given types.
2715
 *
2716
 * @param object $source
2717
 *   The original source entity
2718
 * @param array $types
2719
 *   An array of element of the OriginalSourceType enumeration
2720
 *   If not set the default will be used which is:
2721
 *    - Lineage
2722
 *    - PrimaryMediaSource
2723
 *    - PrimaryTaxonomicSource
2724
 *    - Unknown
2725
 *    - Other
2726
 * @return boolean
2727
 */
2728
  function _is_original_source_type($source, $types = null) {
2729
    // this is the default
2730
    // maybe this should also be put into the settings
2731
    static $default = array(
2732
      OriginalSourceType::Lineage,
2733
      OriginalSourceType::PrimaryMediaSource,
2734
      OriginalSourceType::PrimaryTaxonomicSource,
2735
      OriginalSourceType::Unknown,
2736
      OriginalSourceType::Other,
2737
      OriginalSourceType::Aggregation
2738
    );
2739

    
2740
    if(!$types){
2741
      $types = $default;
2742
    }
2743
    return isset($source->type) && in_array($source->type, $types);
2744
  }
2745

    
2746
/**
2747
 * Collects all the media from a list of description elements.
2748
 *
2749
 * @param array $descriptionElements
2750
 *   The description elements from which to collect the media.
2751
 *
2752
 * @return array
2753
 *   The output with all the collected media.
2754
 */
2755
function cdm_dataportal_media_from_descriptionElements($descriptionElements) {
2756

    
2757
  $outArrayOfMedia = array();
2758

    
2759
  // Avoiding a warning box in Drupal for Flora Malesiana.
2760
  if (isset($descriptionElements) && is_array($descriptionElements)) {
2761
    foreach ($descriptionElements as $descriptionElement) {
2762
      if (isset($descriptionElement->media) && is_array($descriptionElement->media)) {
2763
        foreach ($descriptionElement->media as $media) {
2764
          if (is_object($media)) {
2765
            $outArrayOfMedia[] = $media;
2766
          }
2767
        }
2768
      }
2769
    }
2770
  }
2771
  return $outArrayOfMedia;
2772
}
2773

    
2774

    
2775

    
2776

    
2777
/**
2778
 * Creates a CDM Dynabox.
2779
 *
2780
 * @param string $dynabox_id
2781
 *   a uninque name for tha dynabox, using a cdm entity uuid as id is good practice.
2782
 * @param string $label
2783
 *   The clickable text to show.
2784
 * @param string $content_url
2785
 *   The cdm REST service request url wich will deliver the content to be shown
2786
 *   once the dynabox toggles open.
2787
 * @param string $theme
2788
 *   The theme to be used for rendering the cdm REST service response with is
2789
 *   returned from the $content_url.
2790
 * @param string $link_alt_text
2791
 *   The value for the alt attribute of the dynabox link.
2792
 * @param array $enclosingtags
2793
 *   An array with two elements: $enclosingtags[0] will be used for the dynabox
2794
 *   element itself, $enclosingtags[1] is the tag to be used for the
2795
 *   dynabox_content (optional)
2796
 * @param array $attributes
2797
 * @param $content_element_selector
2798
 *   Optional jQuery selector which can be used to reference a dom element which should
2799
 *   be used as container for the content to be shown. The dynabox-<dynabox id>-content
2800
 *  element will be placed in this container.
2801
 *
2802
 * @param string $open_callback
2803
 *   optional javascript call back function to be triggered after toggling the box to
2804
 *   the open state.
2805
 * @param string $close_callback
2806
 *   optional javascript call back function to be triggered after toggling the box to
2807
 *   the closed state.
2808
 * @return string Returns HTML for a dynabox.
2809
 * Returns HTML for a dynabox.
2810
 */
2811
function cdm_dynabox($dynabox_id, $label, $content_url, $theme, $link_alt_text,
2812
                     $enclosingtags = array('li', 'ul'), $attributes = array(),
2813
                     $content_element_selector = null,
2814
                     $open_callback = 'function(){}', $close_callback = 'function(){}' ) {
2815
  $out = '';
2816

    
2817
  // check for plain class attribute string
2818
  $dynabox_id = preg_replace('/[^a-zA-Z0-9\-]/', '', $dynabox_id);
2819

    
2820
  if(!array_key_exists('class', $attributes)) {
2821
    $attributes['class'] = ["dynabox"];
2822
  } else {
2823
    $attributes['class'] = array_merge($attributes['class'], ["dynabox"]);
2824
  }
2825
  $attributes['id'][] = 'dynabox-' . $dynabox_id;
2826
  $dynabox_attributes = drupal_attributes($attributes);
2827

    
2828

    
2829
  _add_js_domEvent(); // requires domEvent.js
2830
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cdm_dynabox.js');
2831
  drupal_add_js("
2832
  jQuery(document).ready(
2833
      function() {
2834
        dynabox('". $dynabox_id ."',
2835
          {
2836
            open_callback: " . $open_callback .",
2837
            close_callback: " . $close_callback .
2838
            ($content_element_selector ? ",\n content_container_selector: '" . $content_element_selector . "'" : "") . "
2839
          }
2840
        );
2841
      }
2842
   );",
2843
   array(
2844
    'type'=>'inline',
2845
    'scope'=>'footer'
2846
    )
2847
  );
2848

    
2849

    
2850
  $cdm_proxy_url = url('cdm_api/proxy/' . urlencode($content_url) . "/$theme");
2851
  $out .= '<!-- dynabox for ' . $content_url . ' -->';
2852
  $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>';
2853
  $out .= '  <' . $enclosingtags[1] . ' id="dynabox-' . $dynabox_id . '-content">';
2854
  $out .= '    <' . $enclosingtags[0] . ' class="dynabox-content-inner">' . loading_image_html() . '</' . $enclosingtags[0] . '>';
2855
  $out .= '    </' . $enclosingtags[1] . '>';
2856
  $out .= '  </' . $enclosingtags[0] . '>';
2857
  $out .= '<!-- dynabox end -->';
2858
  return $out;
2859
}
2860

    
2861
/**
2862
 * Checks whether a feature has any description elements.
2863
 *
2864
 * @param mixed $featureNode
2865
 *   A feature node as produced by the function _mergeFeatureTreeDescriptions().
2866
 *
2867
 * @see _mergeFeatureTreeDescriptions()
2868
 *
2869
 * @return bool
2870
 *   Returns TRUE if the given $featureNode or any of its subordinate nodes
2871
 *   contains at least one non empty TextData or at least one DescriptionElement
2872
 *   of an other type. A TextData element holding a multilanguageText or a
2873
 *   source reference is considered to be not empty.
2874
 *
2875
 * @TODO this function may have become obsolete by the new method of detecting empty blocks,
2876
 *       see $block_content_is_not_empty in make_feature_block_list() and
2877
 *       $feature_block_has_content in compose_feature_block_items_generic
2878
 */
2879
function has_feature_node_description_elements($featureNode) {
2880

    
2881
  if (isset($featureNode->descriptionElements) && is_array($featureNode->descriptionElements) && count($featureNode->descriptionElements) > 0) {
2882
    if(!isset($featureNode->descriptionElements['#type'])){ // #type is used to identify e.g. DTO elements: '#type' => 'DTO'
2883
      foreach ($featureNode->descriptionElements as $descriptionElement) {
2884
        if ($descriptionElement->class != "TextData" || isset($descriptionElement->multilanguageText_L10n->text)
2885
          && $descriptionElement->multilanguageText_L10n->text != ''
2886
          || isset($descriptionElement->sources[0])
2887
          || isset($descriptionElement->media[0]) ) {
2888
          return TRUE;
2889
        }
2890
      }
2891
    }
2892
  }
2893
  else if (isset($featureNode->childNodes) && is_array($featureNode->childNodes)) {
2894
    foreach ($featureNode->childNodes as $child) {
2895
      if (has_feature_node_description_elements($child)) {
2896
        return TRUE;
2897
      }
2898
    }
2899
  }
2900
  return FALSE;
2901
}
2902

    
2903
/**
2904
 * Checks if the current page is a valid taxon portal page and responds with HTTP status 404 (not found) otherwise
2905
 *
2906
 * @param $chapter
2907
 *   The taxon page chapter or part
2908
 */
2909
function cdm_check_valid_taxon_page($chapter){
2910
  static $taxon_tabs = null;
2911

    
2912
  cdm_check_valid_portal_page();
2913

    
2914
  if($taxon_tabs == null){
2915
    $taxon_tabs = array('all', 'description');
2916
    foreach(get_taxon_tabs_list() as $tab){
2917
      $taxon_tabs[] = strtolower($tab);
2918
    }
2919
  }
2920

    
2921
  if(!in_array($chapter, $taxon_tabs)){
2922
    // oops this is not a valid chapter name
2923
    http_response_code(404); // 404 = Not Found
2924
  }
2925

    
2926
}
2927

    
2928
function check_js_devel_mode_disabled() {
2929
  if(variable_get('cdm_js_devel_mode', FALSE)) {
2930
    drupal_set_message(t('The !url1 is enabled.
2931
        WARNING: this is a performance penalty and must be turned off on production websites.', array(
2932
      '!url1' => l('java-script development mode', 'admin/config/cdm_dataportal/settings', array('fragment' => 'edit-cdm-js-devel-mode'))
2933
    )),
2934
      'warning'
2935
    );
2936
  }
2937
}
2938

    
2939
/**
2940
 * Checks if the current page is a valid portal page and responds with HTTP status 404 (not found) otherwise.
2941
 * The test applied by default it a check for the query parameter 'q' ending with a file suffix like '*.*'
2942
 *
2943
 * @param $preg_pattern
2944
 *   Optional regular expression pattern to be used in preg_match().
2945
 */
2946
function cdm_check_valid_portal_page($preg_pattern = null){
2947
  $ends_with_file_suffix_pattern = '/\/[^\.\/]*[\.][^\.\/]*$/';
2948
  if($preg_pattern === null){
2949
    $preg_pattern = $ends_with_file_suffix_pattern;
2950
  }
2951
  if(preg_match($preg_pattern, $_GET['q'])){
2952
    // oops this urls ends with a file_suffix and thus does not refer to a portal page
2953
    http_response_code(404); // 404 = Not Found
2954
    exit('HTTP 404');
2955
  }
2956
  check_js_devel_mode_disabled();
2957
}
2958

    
2959
/**
2960
 * Generates the diff of the texts and presents it in a HTML diff viewer.
2961
 *
2962
 * @param $text_a
2963
 * @param $text_b
2964
 * @return string
2965
 */
2966
function diff_viewer($text_a, $text_b) {
2967

    
2968
  static $diff_viewer_count = 0;
2969

    
2970
  $element_id = 'part_definitions_diff_' . $diff_viewer_count++;
2971

    
2972
  // http://code.stephenmorley.org/php/diff-implementation/
2973
  module_load_include('php', 'cdm_dataportal', 'lib/class.Diff');
2974
  drupal_add_css(drupal_get_path('module',
2975
      'cdm_dataportal') . '/css/diff.css');
2976
  _add_jquery_ui();
2977
  drupal_add_js(
2978
    'jQuery(document).ready( function(){
2979
        jQuery(\'#' . $element_id . '\').accordion({
2980
        collapsible: true,
2981
        active: false,
2982
        fillSpace: true,
2983
        }).children(\'div\').css({ \'height\': \'auto\' });
2984
        jQuery(\'#' . $element_id . ' table.diff\').prepend(\'<thead><tr><th>Default</th><th>User defined<th></th><tr></thead>\');
2985
     });'
2986
    , array(
2987
    'type' => 'inline',
2988
    'scope' => 'footer'
2989
  ));
2990

    
2991
  $diff = Diff::compare($text_a,
2992
    $text_b);
2993
  $diff_viewer_markup = '<div id="' . $element_id . '"><h3>View Diff</h3><div>'
2994
    . Diff::toTable($diff, '', '')
2995
    . '</div></div>';
2996
  return $diff_viewer_markup;
2997
}
2998

    
2999

    
Add picture from clipboard (Maximum size: 40 MB)