Project

General

Profile

Download (77 KB) Statistics
| Branch: | Tag: | Revision:
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/name');
27
  module_load_include('inc', 'cdm_dataportal', 'includes/taxon');
28
  module_load_include('inc', 'cdm_dataportal', 'includes/references');
29
  module_load_include('inc', 'cdm_dataportal', 'includes/pages');
30
  module_load_include('inc', 'cdm_dataportal', 'includes/media');
31
  module_load_include('inc', 'cdm_dataportal', 'includes/maps');
32
  module_load_include('inc', 'cdm_dataportal', 'includes/occurrences');
33
  module_load_include('inc', 'cdm_dataportal', 'includes/descriptions');
34
  module_load_include('inc', 'cdm_dataportal', 'includes/pre-drupal8');
35

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

    
46
  module_load_include('php', 'cdm_dataportal', 'classes/footnotemanager');
47
  module_load_include('php', 'cdm_dataportal', 'classes/footnote');
48
  module_load_include('php', 'cdm_dataportal', 'classes/footnotekey');
49
  module_load_include('php', 'cdm_dataportal', 'classes/renderhints');
50

    
51

    
52
  /* ============================ java script functions ============================= */
53

    
54

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

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

    
76
  /**
77
   */
78
  function drupal_add_js_rowToggle($tableId){
79

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

    
87
  /**
88
   * @param unknown_type $link_element_selector
89
   * @param unknown_type $progress_element_selector
90
   */
91
  function _add_js_cdm_ws_progressbar($link_element_selector, $progress_element_selector){
92

    
93
    $callback = "jQuery('" . $link_element_selector . "').cdm_ws_progress('" . $progress_element_selector . "');";
94

    
95
    drupal_add_js_async(variable_get('cdm_webservice_url', '').'js/cdm_ws_progress.js', $callback);
96

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

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

    
117
        });
118
      ",
119
      array(
120
        'type' => 'inline',
121
        'scope' => 'footer'
122
      )
123
    );
124
  }
125

    
126
  function _add_js_openlayers() {
127

    
128
    $openlayers = '/js/map/OpenLayers-2.13.1/OpenLayers.js';
129
    $proj4js = '/js/map/proj4js-1.1.0/proj4js-compressed.js';
130

    
131
    if(variable_get('cdm_js_devel_mode', FALSE)){
132
      // develooper mode libs
133
  //     $openlayers = '/js/map/OpenLayers-2.13.1/lib/OpenLayers.js';
134
      $openlayers = '/js/map/OpenLayers-2.13.1/OpenLayers.debug.js';
135
      $proj4js = '/js/map/proj4js-1.1.0/proj4js-combined.js';
136
    }
137

    
138
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . $openlayers,
139
      array(
140
        'type' => 'file',
141
        'weight' => JS_LIBRARY,
142
        'cache' => TRUE,
143
        'preprocess' => FALSE
144
      )
145
    );
146

    
147
    // see https://github.com/proj4js/proj4js
148
    // http://openlayers.org/dev/examples/using-proj4js.html
149
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . $proj4js,
150
      array(
151
        'type' => 'file',
152
        'weight' => JS_LIBRARY,
153
        'cache' => TRUE,
154
      )
155
    );
156

    
157
    // configure the theme
158
    $openlayers_theme_path = drupal_get_path('module', 'cdm_dataportal') . '/js/map/OpenLayers-2.13.1/theme/default/';
159
    $openlayers_imp_path = drupal_get_path('module', 'cdm_dataportal') . '/js/map/img/dark/';
160
    drupal_add_js('OpenLayers.ImgPath="' . base_path() . $openlayers_imp_path . '";', array(
161
        'type' => 'inline',
162
        'weight' => JS_LIBRARY,
163
        'cache' => TRUE,
164
        'preprocess' => FALSE
165
      ));
166

    
167
    drupal_add_css($openlayers_theme_path . 'style.tidy.css',
168
      array(
169
        'type' => 'file',
170
        'cache' => TRUE,
171
        'preprocess' => FALSE
172
      )
173
    );
174

    
175
  }
176

    
177
  /**
178
   * @todo Please document this function.
179
   * @see http://drupal.org/node/1354
180
   */
181
  function _add_js_thickbox() {
182
    // ---- jQuery thickbox:
183
    /*
184
    * bug: compat-1.0.js && thickbox.js line 237 .trigger("unload") -> event is
185
    * not triggered because of problems with compat-1.0.js' see INSTALL.txt
186
    */
187
    // drupal_add_js(drupal_get_path('module',
188
    // 'cdm_dataportal').'/js/jquery.imagetool.min.js');
189
    //
190
    // Add a setting for the path to cdm_dataportal module, used to find the path
191
    // for the loading animation image in thickbox.
192
    drupal_add_js(array(
193
    'cdm_dataportal' => array(
194
    'cdm_dataportal_path' => base_path() . drupal_get_path('module', 'cdm_dataportal'),
195
    )
196
    ),
197
    'setting'
198
        );
199
        drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/thickbox/thickbox.js');
200
        drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/js/thickbox/cdm_thickbox.css');
201
  }
202

    
203
  /**
204
   * @todo Please document this function.
205
   * @see http://drupal.org/node/1354
206
   */
207
  function _add_js_lightbox($galleryID) {
208
    /*
209
     * Important Notice: The jquery.lightbox-0.5.js has been modified in order to
210
    * allow using the "alt" attribute for captions instead of the "title"
211
    * attribute
212
    */
213
    $lightbox_base_path =  drupal_get_path('module', 'cdm_dataportal') . '/js/jquery-lightbox-0.5';
214
    $lightbox_image_path = base_path() . $lightbox_base_path . '/images/';
215
    drupal_add_js($lightbox_base_path . '/js/jquery.lightbox-0.5.js');
216
    drupal_add_css($lightbox_base_path . '/css/jquery.lightbox-0.5.css');
217
    drupal_add_js('jQuery(document).ready(function() {
218
        jQuery(\'#' . $galleryID . ' a.lightbox\').lightBox({
219
          fixedNavigation:  true,
220
          imageLoading:     \'' . $lightbox_image_path . 'lightbox-ico-loading.gif\',
221
          imageBtnPrev:     \'' . $lightbox_image_path . 'lightbox-btn-prev.gif\',
222
          imageBtnNext:     \'' . $lightbox_image_path . 'lightbox-btn-next.gif\',
223
          imageBtnClose:    \'' . $lightbox_image_path . 'lightbox-btn-close.gif\',
224
          imageBlank:       \'' . $lightbox_image_path . 'lightbox-blank.gif\',
225
          adjustToWindow: true
226
        });
227
      });
228
      ', array('type' => 'inline'));
229
  }
230

    
231
  /**
232
   * @todo Please document this function.
233
   * @see http://drupal.org/node/1354
234
   */
235
  function _add_js_footnotes() {
236
    _add_js_domEvent();
237
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/footnotes.js');
238
  }
239

    
240
  /**
241
   * @todo Please document this function.
242
   * @see http://drupal.org/node/1354
243
   */
244
  function _add_js_cluetip() {
245

    
246
    // TODO replace by
247
    // @see http://www.socialembedded.com/labs/jQuery-Tooltip-Plugin/jQuery-Tooltip-Plugin.html
248
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.cluetip.min.js');
249
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/jquery.dimensions.js');
250
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.hoverIntent.js');
251
    if(variable_get('cdm_js_devel_mode', FALSE)){
252
      drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.cluetip.js');
253
    }
254
    drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.cluetip.css');
255
    drupal_add_js("jQuery(document).ready(function(){
256
        jQuery('.cluetip').css({color: '#0062C2'}).cluetip({
257
          splitTitle: '|',
258
          showTitle: true,
259
          activation: 'hover',
260
          sicky: true,
261
          arrows: true,
262
          dropShadow: false,
263
          cluetipClass: 'rounded'
264
        });
265
      });",
266
      array(
267
        'type' => 'inline',
268
        'scope' => 'footer'
269
      )
270
    );
271
  }
272

    
273
  /**
274
   * @todo Please document this function.
275
   * @see http://drupal.org/node/1354
276
   */
277
  function _add_js_ahah() {
278

    
279
    _add_js_domEvent(); // requires domEvent.js
280
    drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/ahah-content.js');
281
  }
282

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

    
289
  global $base_url;
290

    
291

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

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

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

    
331
  /**
332
   * Provides the markup for an font awesome icon.
333
   *
334
   * The icons is created in default size without any extra features.
335
   *
336
   * The available icons are listed here http://fontawesome.io/cheatsheet/
337
   * fontawesome icons have much more features than implemented here in this function,
338
   * for spinning icons, fixed width icons, rotation, etc please checkout the
339
   * examples at http://fontawesome.io/examples/
340
   *
341
   * @parameter $icon_name
342
   *  The name of the icon which starts with 'fa-'
343
   *
344
   * @return String
345
   *    the markup for the icon in an <i> tag
346
   *
347
   */
348
  function font_awesome_icon_markup($icon_name = NULL, $attributes = array()){
349

    
350

    
351
    //<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
352

    
353
    $font_awesome_css_uri=drupal_get_path('module', 'cdm_dataportal').'/font-awesome/4.5.0/css/font-awesome.min.css';
354
    $font_awesome_css_uri="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css";
355

    
356
    drupal_add_html_head_link(
357
      array(
358
        'href' => $font_awesome_css_uri,
359
        'rel' => 'stylesheet'
360
      )
361
    );
362

    
363
    if($icon_name){
364
      if(!isset($attributes['class'])){
365
        $attributes['class'] = array();
366
      }
367
      $attributes['class'][] = 'fa';
368
      $attributes['class'][] = $icon_name;
369

    
370
      return '<i ' . drupal_attributes($attributes) . '></i>';
371
    }
372

    
373
    return '';
374
  }
375

    
376

    
377
  /* ====================== hook implementations ====================== */
378
  /**
379
   * Implements hook_permission().
380
   *
381
   * Valid permissions for this module.
382
   *
383
   * @return array
384
   *   An array of valid permissions for the portfolio module.
385
   */
386
  function cdm_dataportal_permission() {
387
    return array(
388
      'administer cdm_dataportal' => array(
389
        'title' => t('administer cdm_dataportal'),
390
        'description' => t("TODO Add a description for 'administer cdm_dataportal'"),
391
      ),
392
      'cdm_dataportal view notes' => array(
393
        'title' => t('cdm_dataportal view notes'),
394
        'description' => t("TODO Add a description for 'cdm_dataportal view notes'"),
395
      ),
396
      // TODO Which other permissions are required?
397
      // -> check the WP6 requirements document.
398
    );
399
  }
400

    
401
/**
402
 * Implements hook_menu().
403
 */
404
function cdm_dataportal_menu() {
405
  $items = array();
406

    
407
  // @see settings.php.
408
  cdm_dataportal_menu_admin($items);
409
  cdm_dataportal_menu_help($items);
410

    
411
  $items['cdm_dataportal/names'] = array(
412
    'page callback' => 'cdm_dataportal_view_names',
413
    'access arguments' => array('access content'),
414
    'type' => MENU_CALLBACK,
415
  );
416

    
417
  // Optional callback arguments: page.
418
  $items['cdm_dataportal/taxon'] = array(
419
    'page callback' => 'cdm_dataportal_taxon_page_view',
420
    'access arguments' => array('access content'),
421
    'type' => MENU_CALLBACK,
422
    // Expected callback arguments: uuid.
423
  );
424

    
425
   // Optional callback arguments: page.
426
    //FIXME point to view/page method in this module
427
    $items['cdm_dataportal/specimen'] = array(
428
        'page callback' => 'cdm_dataportal_specimen_page_view',
429
        'access arguments' => array('access content'),
430
        'type' => MENU_CALLBACK,
431
        // Expected callback arguments: uuid.
432
    );
433

    
434
  $items['cdm_dataportal/name'] = array(
435
    'page callback' => 'cdm_dataportal_name_page_view',
436
      /*
437
    'page arguments' => array(
438
       'taxon_name_uuid',
439
       'taxon_to_hide_uuid',
440
       'synonym_uuid' => NULL
441
      ),
442
      */
443
    'access arguments' => array('access content'),
444
    'type' => MENU_CALLBACK,
445
    // Expected callback arguments: uuid.
446
  );
447

    
448
  $items['cdm_dataportal/reference'] = array(
449
    'page callback' => 'cdm_dataportal_view_reference',
450
    'access arguments' => array('access content'),
451
    'type' => MENU_CALLBACK,
452
    // Expected callback arguments: uuid.
453
  );
454

    
455
  $items['cdm_dataportal/reference/list'] = array(
456
    'page callback' => 'cdm_dataportal_view_reference_list',
457
    'access arguments' => array('access content'),
458
    'type' => MENU_CALLBACK,
459
    // Expected callback arguments: uuid.
460
  );
461

    
462
  $items['cdm_dataportal/media'] = array(
463
    'page callback' => 'cdm_dataportal_view_media',
464
    'access arguments' => array('access content'),
465
    'type' => MENU_CALLBACK,
466
    // Expected callback arguments:
467
    // uuid, mediarepresentation_uuid, part_uuid or part#.
468
  );
469

    
470
  $items['cdm_dataportal/polytomousKey'] = array(
471
    'page callback' => 'cdm_dataportal_view_polytomousKey',
472
    'access arguments' => array('access content'),
473
    'type' => MENU_CALLBACK,
474
    // Expected callback arguments: polytomousKey->uuid.
475
  );
476

    
477
  $items['cdm_dataportal/search'] = array(
478
    'page callback' => 'cdm_dataportal_view_search_advanced',
479
    'access arguments' => array('access content'),
480
    'type' => MENU_CALLBACK,
481
  );
482

    
483
  $items['cdm_dataportal/search/advanced'] = array(
484
    'title' => 'Advanced', // will be passed through t()
485
    'page callback' => 'cdm_dataportal_view_search_advanced',
486
    'access arguments' => array('access content'),
487
    'type' => MENU_DEFAULT_LOCAL_TASK,
488
  );
489

    
490
  $items['cdm_dataportal/search/taxon_by_description'] = array(
491
    'title' => 'By factual data', // will be passed through t()
492
    'page callback' => 'cdm_dataportal_view_search_taxon_by_description',
493
    'access arguments' => array('access content'),
494
    'type' => MENU_LOCAL_TASK,
495
  );
496

    
497
  $items['cdm_dataportal/search/results/taxon'] = array(
498
    'page callback' => 'cdm_dataportal_view_search_results_taxon',
499
    'access arguments' => array('access content'),
500
    'type' => MENU_CALLBACK,
501
  );
502
  /*
503
   $items['cdm/xml2json'] = array(
504
   'page callback' => 'cdm_view_xml2json',
505
   'access arguments' => array('access content'),
506
   'type' => MENU_CALLBACK,
507
   );
508
   */
509

    
510
  // if (arg(0)=='user' && ($uid=arg(1)) && is_numeric($uid)) {
511
  // User configuration of cdm_dataportal.
512
  $items['user/%/cdm_dataportal'] = array(
513
    'title' => 'cdm_dataportal',
514
    'access arguments' => array('access content'),
515
    'page callback' => 'drupal_get_form',
516
    'page arguments' => array('cdm_dataportal_user_form'),
517
    'type' => MENU_LOCAL_TASK,
518
    'weight' => 10,
519
  );
520
  // }
521

    
522
  // 'May not cache' in D5.
523
  $items['cdm_dataportal/name/%'] = array(
524
    // 'page callback' => 'cdm_dataportal_view_name',
525
    'page callback' => 'cdm_dataportal_name_page_view',
526
    'page arguments' => array(2, 3, 4),
527
    'access arguments' => array('access content'),
528
    'type' => MENU_CALLBACK,
529
  );
530

    
531
  // --- Local tasks for Taxon.
532
  // --- tabbed taxon page
533
  if (variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
534
    $items['cdm_dataportal/taxon/%'] = array(
535
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'General')),
536
      'page callback' => 'cdm_dataportal_taxon_page_view',
537
      'access arguments' => array('access content'),
538
      'type' => MENU_CALLBACK,
539
      'weight' => 1,
540
      'page arguments' => array(2, "description")
541
      , // Expected callback arguments: taxon_uuid.
542
    );
543

    
544
    $items['cdm_dataportal/taxon/%/all'] = array(
545
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'General')),
546
      'page callback' => 'cdm_dataportal_taxon_page_view',
547
      'access arguments' => array('access content'),
548
      'type' => MENU_CALLBACK,
549
      'weight' => 2,
550
      'page arguments' => array(2, "all")
551
      , // Expected callback arguments: taxon_uuid.
552
    );
553

    
554
    $items['cdm_dataportal/taxon/%/description'] = array(
555
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'General')),
556
      'page callback' => 'cdm_dataportal_taxon_page_view',
557
      'access arguments' => array('access content'),
558
      'type' => MENU_DEFAULT_LOCAL_TASK,
559
      'weight' => 2,
560
      'page arguments' => array(2, "description")
561
      , // Expected callback arguments: taxon_uuid.
562
    );
563

    
564
    $items['cdm_dataportal/taxon/%/synonymy'] = array(
565
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Synonymy')),
566
      'page callback' => 'cdm_dataportal_taxon_page_view',
567
      'access arguments' => array('access content'),
568
      'type' => MENU_LOCAL_TASK,
569
      'weight' => 4,
570
      'page arguments' => array(2, "synonymy", 4)
571
      , // Expected callback arguments: taxon_uuid and ...
572
    );
573
    $items['cdm_dataportal/taxon/%/images'] = array(
574
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Images')),
575
      'page callback' => 'cdm_dataportal_taxon_page_view',
576
      'access arguments' => array('access content'),
577
      'type' => MENU_LOCAL_TASK,
578
      'weight' => 5,
579
      'page arguments' => array(2, "images")
580
      , // Expected callback arguments: taxon_uuid.
581
    );
582

    
583
    $items['cdm_dataportal/taxon/%/specimens'] = array(
584
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Specimens')),
585
      'page callback' => 'cdm_dataportal_taxon_page_view',
586
      'access arguments' => array('access content'),
587
      'type' => MENU_LOCAL_TASK,
588
      'weight' => 6,
589
      'page arguments' => array(2, "specimens")
590
      , // Expected callback arguments: taxon_uuid.
591
    );
592

    
593
    $items['cdm_dataportal/taxon/%/keys'] = array(
594
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Keys')),
595
      'page callback' => 'cdm_dataportal_taxon_page_view',
596
      'access arguments' => array('access content'),
597
      'type' => MENU_LOCAL_TASK,
598
      'weight' => 6,
599
      'page arguments' => array(2, "keys")
600
      , // Expected callback arguments: taxon_uuid.
601
    );
602

    
603
    $items['cdm_dataportal/taxon/%/experts'] = array(
604
        'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Experts')),
605
        'page callback' => 'cdm_dataportal_taxon_page_view',
606
        'access arguments' => array('access content'),
607
        'type' => MENU_LOCAL_TASK,
608
        'weight' => 6,
609
        'page arguments' => array(2, "experts")
610
    , // Expected callback arguments: taxon_uuid.
611
    );
612
  }
613

    
614
  // --- refresh link for all cdmnode types
615
  foreach (cdm_get_nodetypes() as $type=>$name) {
616
    $items['cdm_dataportal/' . $name . '/%/refresh'] = array(
617
        'title' => t('Refresh'),
618
        'page callback' => 'cdm_dataportal_refresh_node',
619
        'access arguments' => array('administer cdm_dataportal'),
620
        'type' => MENU_LOCAL_TASK,
621
        'weight' => 100,
622
        'page arguments' => array($name, 2)
623
    );
624
  }
625

    
626
  return $items;
627
}
628

    
629
/**
630
 * Implements hook_init().
631
 *
632
 */
633
function cdm_dataportal_init() {
634
  //FIXME To add CSS or JS that should be present on all pages, modules
635
  //      should not implement this hook, but declare these files in their .info file.
636
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal.css');
637
  // drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
638
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal_screen.css', array('type' => 'screen'));
639

    
640
  if(variable_get('cdm_debug_mode', FALSE)){
641
    $file = 'temporary://drupal_debug.txt';
642
    file_put_contents($file, 'CDM DEBUG LOG for ' . $_GET['q']. "\n"); // will overwrite the file
643
  }
644

    
645
  $bibliography_settings = get_bibliography_settings();
646
  $enclosing_tag = $bibliography_settings['enabled'] == 1 ? 'div' : 'span';
647
  FootnoteManager::registerFootnoteSet('BIBLIOGRAPHY', $enclosing_tag, $bibliography_settings['key_format']);
648
}
649

    
650
function cdm_dataportal_refresh_node($cdm_node_name, $uuid, $parameters = array()){
651

    
652
  $base_path = 'cdm_dataportal/' . $cdm_node_name . '/' . $uuid;
653

    
654
  if($cdm_node_name == 'taxon' && variable_get('cdm_dataportal_taxonpage_tabs', 1)){
655
    // force reloading of all and notify user about this special loading
656
    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: ')
657
        . l('Back to tabbed taxon page', $base_path));
658
    $base_path .= '/all';
659
  } else {
660
    drupal_set_message(t('The level 2 cache has been cleared for this page'));
661
  }
662

    
663
  $parameters['cacheL2_refresh'] ='1';
664

    
665

    
666
  drupal_goto($base_path, array('query' => $parameters));
667
}
668

    
669
/**
670
 * The function generate form for own user cdm dataportal configurations.
671
 */
672
function cdm_dataportal_user_form($form, &$form_state) {
673

    
674
  global $user;
675
  $checkbox_value = 'cdm_dataportal_' . $user->uid . '_default_tab_active';
676

    
677
  $form['taxon_page_tabs'] = array(
678
      '#type' => 'fieldset',
679
      '#tree' => true,
680
      '#title' => t('Taxon page tabs'),
681
  );
682

    
683
  $form['taxon_page_tabs']['user_defined'] = array(
684
    '#type' => 'checkbox',
685
    '#title' => t('Activate user default configuration'),
686
    '#default_value' => variable_get($checkbox_value, 0),
687
    '#description' => t('Check this if you want configure your own default tab from the below menu.'),
688
  );
689

    
690
  $form['taxon_page_tabs']['default_tab'] = array(
691
    '#type' => 'select',
692
    '#title' => t('Default tab to display'),
693
    '#default_value' => get_default_taxon_tab(TRUE),
694
    '#options' => unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB),
695
    '#description' => t('<p>Select the default tab to display when visiting a taxon page. Only available if Tabbed Taxon Page is enable.</p>
696
              <strong>Note:</strong> After performing a search and clicking in any synonym, the taxon tab
697
              to be rendered will be the synonymy of the accepted taxon and not the above selected tab.'),
698
  );
699

    
700

    
701
  if(false){
702
    $form['developer_options'] = array(
703
        '#type' => 'fieldset',
704
        '#tree' => true,
705
        '#title' => t('Developer options'),
706
    );
707

    
708
    $form['developer_options']['show_render_path'] = array(
709
      '#type' => 'checkbox',
710
      '#title' => t('Display the render path for each taxon name.'),
711
      '#default_value' => variable_get($checkbox_value, 0),
712
      '#description' => t('This option is very helpful if you are editing the !link for taxon names.',
713
        array(
714
            '!link' => l(
715
              'render template', 'admin/config/cdm_dataportal/settings/layout', array('fragment' => 'edit-cdm-name-render-templates'))
716
            )
717
        ),
718
    );
719
  }
720

    
721
  $form['submit'] = array(
722
    '#type' => 'submit',
723
    '#value' => t('Submit'),
724
  );
725

    
726
  return $form;
727
}
728

    
729
/**
730
 * Form submission handler for user_form().
731
 *
732
 * Submits the user cdm dataportal configurations.
733
 */
734
function cdm_dataportal_user_form_submit($form, &$form_state) {
735
  global $user;
736
  $msg_type = 'status';
737
  $username = $user->name;
738
  $variable_to_use = 'cdm_dataportal_' . $user->uid . '_default_tab';
739

    
740
  // FIXME: this is completely wrong, see user_profile_form_submit()
741

    
742
  // it is only possible to change own user settings
743
  if (arg(0) == 'user' && is_numeric(arg(1)) && $user->uid == arg(1)) {
744

    
745
    // DEFAULT_TAXON_TAB
746
    $variable = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
747
    variable_set($variable_to_use . '_active', $form_state['values']['taxon_page_tabs']['user_defined']);
748
    variable_set($variable_to_use, $form_state['values']['taxon_page_tabs']['default_tab']);
749
    if ($form_state['values']['taxon_page_tabs']['user_defined']) {
750
      drupal_set_message(check_plain(t('The user default tab will be used for the next taxon site visit.')));
751
      drupal_set_message(check_plain(t('The user default tab has been changed to: !tab for the user !user', array(
752
        '!tab' => $variable[variable_get($variable_to_use, 0)],
753
        '!user' => $username,
754
      ))), $msg_type);
755
    }
756
    else {
757
      drupal_set_message(check_plain(t('The user default tab wont be used for
758
        the next taxon site, check the box if you want to use the user default configuration.')));
759
    }
760

    
761
  }
762
  else {
763
    // Problem with the user id => variables wont be saved.
764
    $msg_type = 'warning';
765
    drupal_set_message(check_plain(t('Default tab has not been saved due to user id problems')), $msg_type);
766
  }
767
}
768

    
769
/**
770
 * Implements hook_block_info().
771
 */
772
function cdm_dataportal_block_info() {
773

    
774
    // $block[0]["info"] = t("CDM DataPortal DevLinks");
775
    // $block[1]["info"] = t("CDM DataPortal Credits");
776
    $block["2"] = array(
777
        "info" => t("CDM Search Taxa"),
778
        "cache" => DRUPAL_NO_CACHE
779
      );
780
    // $block[3]["info"] = t("CDM Filters");
781
    $block["4"]["info"] = t("CDM Dataportal Print");
782
    $block["keys"]["info"] = t("CDM identification keys");
783
    $block["fundedByEDIT"]["info"] = t('Funded by EDIT');
784
    $block["classification_breadcrumbs"] =  array(
785
        'info' => t('Classification breadcrumbs'),
786
        'cache' => DRUPAL_CACHE_PER_PAGE
787
      );
788

    
789
    return $block;
790
}
791

    
792
/**
793
 * Implements hook_block_view().
794
 */
795
function cdm_dataportal_block_view($delta) {
796
  // TODO Rename block deltas (e.g. '2') to readable strings.
797
  switch ($delta) {
798
    // case 'delta-1':
799
    // $block['subject'] = t('Credits');
800
    // $block['content'] = theme('cdm_credits');
801
    // return $block;
802
    case '2':
803
      $block['subject'] = t('Search taxa');
804
      $form = drupal_get_form('cdm_dataportal_search_taxon_form');
805
      $block['content'] = drupal_render($form);
806

    
807
      if (variable_get('cdm_dataportal_show_advanced_search', 1)) {
808
        $block['content'] .= '<div>' . l(t('Advanced Search'), 'cdm_dataportal/search') . '</div>';
809
      }
810
      return $block;
811
    case '4':
812
      $block['subject'] = '';
813
      $block['content'] = theme('cdm_print_button');
814
      return $block;
815
    case "keys":
816
      $block['subject'] = t('Identification Keys');
817
      $block['content'] = theme('cdm_block_IdentificationKeys', array('taxonUuid' => NULL));
818
      return $block;
819
    case "fundedByEDIT":
820
      // t('Funded by EDIT');
821
      $text = '<none>';
822
      $block['subject'] = $text;
823
      $img_tag = '<img src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/powered_by_edit.png' . '" alt="' . $text . '"/>';
824
      $block['content'] = l($img_tag, "http://cybertaxonomy.org/", array(
825
        'attributes' => array("target" => "EDIT"),
826
        'absolute' => TRUE,
827
        'html' => TRUE,
828
      ));
829
      return $block;
830
    case 'classification_breadcrumbs':
831
      if (arg(1) == 'taxon' && is_uuid(arg(2))) {
832
        $block['subject'] = '<none>';
833
        $block['content'] = compose_classification_breadcrumbs(arg(2));
834
        return $block;
835
      }
836
      break;
837
  }
838
}
839

    
840
/*
841
 function cdm_dataportal_session_clear($cdm_ws_uri_update = FALSE){
842
 $_SESSION['cdm'] = NULL;
843
 if(is_string($cdm_ws_uri_update)){
844
 $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
845
 }
846
 }
847

    
848
 function cdm_dataportal_session_validate(){
849
 if(!isset($_SESSION['cdm']['ws_uri'])){
850
 $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', FALSE));
851
 } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', FALSE)){
852
 cdm_dataportal_session_clear(variable_get('cdm_webservice_url', FALSE));
853
 }
854
 }
855
 */
856

    
857
/**
858
 * creates a  selector form for taxonomic trees.
859
 *
860
 * @return a drupal form array
861
 */
862
function cdm_taxonomictree_selector() {
863
  _add_js_treeselector();
864

    
865
  $form = drupal_get_form('cdm_taxonomictree_selector_form');
866
  return $form;
867
}
868

    
869
/**
870
 * @todo Please document this function.
871
 * @see http://drupal.org/node/1354
872
 */
873
function cdm_taxonomictree_selector_form($form, &$form_state) {
874

    
875
  $url = url('cdm_api/setvalue/session', array('query' => NULL));
876
  $form['#action'] = $url;
877

    
878
  $form['var'] = array(
879
    '#weight' => -3,
880
    '#type' => 'hidden',
881
    '#value' => '[cdm][taxonomictree_uuid]',
882
  );
883

    
884
  $destination_array = drupal_get_destination();
885
  $destination = $destination_array['destination'];
886

    
887
  $form['destination'] = array(
888
    '#weight' => -3,
889
    '#type' => 'hidden',
890
    '#value' =>  $destination,
891
  );
892

    
893
  $options = cdm_get_taxontrees_as_options();
894
  $taxontree_includes = variable_get(CDM_TAXONTREE_INCLUDES, null);
895
  if($taxontree_includes){
896
    $filtered_options = array();
897
    foreach($options as $uuid=>$label){
898
      if(!empty($taxontree_includes[$uuid])){
899
        $filtered_options[$uuid] = $label;
900
      }
901
    }
902
    $options = $filtered_options;
903
  }
904

    
905
  $form['val'] = array(
906
    '#type' => 'select',
907
    '#title' => t('Available classifications'),
908
    '#default_value' => get_current_classification_uuid(),
909
    '#options' => $options,
910
    '#attributes' => array('class' => array('highlite-first-child'))
911
  );
912

    
913
  return $form;
914

    
915
}
916

    
917
/* UNREACHABLE since action of form directly links to view.
918
 function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
919

    
920
 $_SESSION['cdm']['search'] = $form_values;
921
 //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
922
 return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
923
 //$paramstr = compose_url_prameterstr($form_values);
924
 //return url('/cdm_dataportal/search/taxon/', array('query' => $paramstr));
925
 }
926
 */
927
/* ====================== menu callback functions ====================== */
928
/**
929
 * @todo Please document this function.
930
 * @see http://drupal.org/node/1354
931
 */
932
/*
933
function cdm_dataportal_form_alter(&$form, &$form_state, $form_id) {
934
  static $comment_node_disabled =  0;
935
  static $comment_node_read_only =  1;
936
  static $comment_node_read_write =  2;
937

    
938
  if ($form_id == 'node_type_form'
939
   && isset($form['identity']['type'])
940
   && array_key_exists($form['#node_type']->type, cdm_get_nodetypes())
941
  ) {
942
    $form['workflow']['comment'] = array(
943
      '#type' => 'radios',
944
      '#title' => t('Default comment setting'),
945
      '#default_value' => variable_get('comment__' . $node->type . $form['#node_type']->type, $comment_node_disabled),
946
      '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
947
      '#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
948
    );
949
  }
950
}
951
*/
952

    
953
/**
954
 * Displays a list of the known taxonomic names.
955
 *
956
 * When the list of taxonomic names is displayed, long lists are split up into
957
 * multiple pages.
958
 *
959
 * TODO: Parameters are still preliminary.
960
 *
961
 * @param string $beginsWith
962
 * @param string $page
963
 *   Page number to diplay defaults to page 1.
964
 * @param bool $onlyAccepted
965
 */
966
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = FALSE) {
967

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

    
970
  /*
971
  // FIXME the filter for accepted names will be a form element, thus this
972
  // widget should be generated via form api preferably as block.
973
  $out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
974
  $out .= theme('cdm_dataportal_widget_names_list', $names, $page);
975
  $out .= theme('cdm_listof_taxa', $taxonPager);
976
  return $out;
977
  */
978
}
979

    
980
/**
981
 * @todo Please document this function.
982
 * @see http://drupal.org/node/1354
983
 */
984
function cdm_dataportal_view_reference($uuid, $arg2 = NULL) {
985

    
986
  cdm_check_valid_portal_page();
987

    
988
  $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
989
  return theme('cdm_reference_page', array('reference' => $reference));
990
}
991

    
992
/**
993
 * Creates a view on a all references contained in the portal.
994
 *
995
 * This function is used at the path cdm_dataportal/reference/list
996
 */
997
function cdm_dataportal_view_reference_list($pageNumber) {
998
  $referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE), $pageNumber);
999
  cdm_reference_pager($referencePager, 'cdm_dataportal/reference/list/');
1000
}
1001

    
1002
/**
1003
 * @todo Please document this function.
1004
 * @see http://drupal.org/node/1354
1005
 */
1006
function cdm_dataportal_view_media($mediaUuid, $mediarepresentation_uuid = FALSE, $part = 0) {
1007

    
1008
  cdm_check_valid_portal_page();
1009

    
1010
  $media = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $mediaUuid);
1011
  return theme('cdm_media_page', array(
1012
    'media' => $media,
1013
    'mediarepresentation_uuid' => $mediarepresentation_uuid,
1014
    'partId' => $part,
1015
    ));
1016
}
1017

    
1018
/**
1019
 * @todo Please document this function.
1020
 * @see http://drupal.org/node/1354
1021
 */
1022
function _load_taxonBase(&$taxonBase) {
1023
  if (isset($taxonBase->uuid)) {
1024
    $taxonBase->name = cdm_ws_get(CDM_WS_TAXON, array($taxonBase->uuid, "name"));
1025
    $taxonBase->name->taggedName = cdm_ws_get(CDM_WS_NAME, array($taxonBase->name->uuid, "taggedName"));
1026
    $taxonBase->name->nomenclaturalReference = cdm_ws_get(CDM_WS_NAME, array($taxonBase->name->uuid, "nomenclaturalReference"));
1027
  }
1028
}
1029

    
1030
/**
1031
 * Loads the media associated to the given taxon from the cdm server.
1032
 * The aggregation settings regarding taxon relathionships and
1033
 * taxonnomic childen are taken into account.
1034
 *
1035
 * The media lists are cached in a static variable.
1036
 *
1037
 * @param Taxon $taxon
1038
 *   A CDM Taxon entitiy
1039
 *
1040
 * @return array
1041
 *   An array of CDM Media entities
1042
 *
1043
 */
1044
function _load_media_for_taxon($taxon) {
1045

    
1046
  static $media = NULL;
1047

    
1048
  if(!isset($media)) {
1049
    $media = array();
1050
  }
1051
  if (!isset($media[$taxon->uuid])) {
1052

    
1053
    // --- GET Images --- //
1054
    $mediaQueryParameters = array(
1055
        "type" => "ImageFile",
1056
    );
1057

    
1058
    $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
1059
    $mediaQueryParameters['relationships'] = implode(',', get_selection($relationship_choice['direct']));
1060
    $mediaQueryParameters['relationshipsInvers'] = implode(',', get_selection($relationship_choice['invers']));
1061

    
1062
    $taxon_media_filter_choice = variable_get(CDM_TAXON_MEDIA_FILTER, unserialize(CDM_TAXON_MEDIA_FILTER_DEFAULT));
1063
    $mediaQueryParameters['includeTaxonDescriptions'] = (boolean) $taxon_media_filter_choice['includeTaxonDescriptions'] != 0;
1064
    $mediaQueryParameters['includeOccurrences'] = (boolean) $taxon_media_filter_choice['includeOccurrences'] != 0;
1065
    $mediaQueryParameters['includeTaxonNameDescriptions'] = (boolean) $taxon_media_filter_choice['includeTaxonNameDescriptions'] != 0;
1066

    
1067
    $ws_endpoint = NULL;
1068
    if ( variable_get('cdm_images_include_children', 0) == 0) {
1069
      $ws_endpoint = CDM_WS_PORTAL_TAXON_MEDIA;
1070
    } else {
1071
      $ws_endpoint = CDM_WS_PORTAL_TAXON_SUBTREE_MEDIA;
1072
    }
1073

    
1074
    $media[$taxon->uuid] = cdm_ws_get($ws_endpoint,
1075
        array(
1076
            $taxon->uuid,
1077
        ),
1078
        queryString($mediaQueryParameters)
1079
       );
1080
  }
1081

    
1082
  return $media[$taxon->uuid];
1083
}
1084

    
1085
/**
1086
 *
1087
 * @param Taxon $taxon
1088
 *   A CDM Taxon entitiy
1089
 *
1090
 * @return array
1091
 *   An array of CDM SpecimenOrObservation entities
1092
 *
1093
function _load_occurences_for_taxon($taxon){
1094

    
1095
  static $occurences = NULL;
1096

    
1097
  if(!isset($occurences)) {
1098
    $occurences = array();
1099
  }
1100

    
1101
  if (!isset($occurences[$taxon->uuid])){
1102

    
1103
    $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
1104
    $relationship_choice['direct'] = get_selection($relationship_choice['direct']);
1105
    $relationship_choice['invers'] = get_selection($relationship_choice['invers']);
1106

    
1107
    $by_associatedtaxon_query = http_build_query(array(
1108
        'relationshipsInvers' => implode(',', $relationship_choice['invers']),
1109
        'relationships' => implode(',', $relationship_choice['direct']),
1110
        'pageSize' => null // all hits in one page
1111
    )
1112
    );
1113

    
1114
    $pager = cdm_ws_get(CDM_WS_OCCURRENCE_BY_ASSOCIATEDTAXON,
1115
        null,
1116
        $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
1117
    );
1118

    
1119

    
1120
    if(isset($pager->records[0])){
1121
      $occurences[$taxon->uuid] =  $pager->records;
1122
    }
1123
  }
1124
  return $occurences[$taxon->uuid];
1125
}
1126
 */
1127

    
1128
/**
1129
 * Gets a Drupal variable, string or array and returns it.
1130
 *
1131
 * Similar to the variable_get() function of Drupal, except that this function
1132
 * is able to handle arrays correctly. This function is especially useful
1133
 * when dealing with collections of settings form elements (#tree = TRUE).
1134
 *
1135
 * @param string $variableKey
1136
 *   The Unique key of the Drupal variable in the Drupal variables table.
1137
 * @param string $defaultValueString
1138
 *   A string as for example derived from a CONSTANT.
1139
 *
1140
 * @return mixed
1141
 *   usually an array, depending on the nature of the variable.
1142
 *
1143
 * TODO compare with get_array_variable_merged() duplicate functions?
1144
 * @deprecated rather use get_array_variable_merged() since this function
1145
 * used an array as second parameter
1146
 */
1147
function mixed_variable_get($variableKey, $defaultValueString) {
1148
  $systemDefaults = unserialize($defaultValueString);
1149
  $storedSettings = variable_get($variableKey, array());
1150
  if (is_array($storedSettings)) {
1151
    // TODO better use drupal_array_merge_deep() ?
1152
    $settings = array_merge($systemDefaults, $storedSettings);
1153
  }
1154
  else {
1155
    $settings = $systemDefaults;
1156
  }
1157
  return $settings;
1158
}
1159

    
1160
/**
1161
 * Recursive function to convert an object into an array.
1162
 * also subordinate objects will be converted.
1163
 *
1164
 * @param object $object
1165
 * @return the array
1166
 */
1167
function convert_to_array($object) {
1168
  if(is_object($object) || is_array($object)) {
1169
    $array = (array)$object;
1170
    foreach ($array as $key=>$value){
1171
      $array[$key] = convert_to_array($value);
1172
    }
1173
    return $array;
1174
  } else {
1175
    return $object;
1176
  }
1177
}
1178

    
1179
/**
1180
 * Searches the $collection for the cdm entitiy given as $element.
1181
 *
1182
 * The elements are compared by their UUID.
1183
 *
1184
 * @param $element
1185
 *  the CDM entitiy to search for
1186
 * @param $collection
1187
 *  the list of CDM entities to search in
1188
 *
1189
 * @return boolean TRUE if the $collection contains the $element, otheriwse FALSE
1190
 *
1191
 */
1192
function contains_cdm_entitiy($element, $collection) {
1193
  $result = FALSE;
1194
  foreach ($collection as $a) {
1195
    if ($a->uuid == $element->uuid) {
1196
      $result = TRUE;
1197
    }
1198
  }
1199
  return $result;
1200
}
1201

    
1202
/**
1203
 * Fiters the array $entity_list of CDM entities by the list
1204
 * of $excludes. Any element contained in the $excludes will be removed
1205
 * from included int the retuned list.
1206
 *
1207
 * If the $entity_list is not an array the $excludes will be returned.
1208
 */
1209
function filter_cdm_entity_list($entity_list, $excludes) {
1210
  if (is_array($entity_list)) {
1211
    $result = $entity_list;
1212
    if ($excludes) {
1213
      foreach ($excludes as $exclude) {
1214
        if (!contains_cdm_entitiy($exclude, $entity_list)) {
1215
          $result[] = $exclude;
1216
        }
1217
      }
1218
    }
1219
  }
1220
  else {
1221
    $result = $excludes;
1222
  }
1223
  return $result;
1224
}
1225

    
1226
/**
1227
 * Wraps the given $html string into a render array suitable for drupal_render()
1228
 *
1229
 * @param $html
1230
 *   the html string, see
1231
 *   http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#markup
1232
 * @param $weight
1233
 *   A positive or negative number (integer or decimal).
1234
 *   see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#weightval
1235
 * @param $prefix
1236
 *   Optional markup for the '#prefix' element of the render array
1237
 * @param $suffix
1238
 *   Optional markup for the '#suffix' element of the render array
1239
 *
1240
 * @return array
1241
 *   A render array
1242
 *
1243
 */
1244
function markup_to_render_array($html, $weight = FALSE, $prefix = NULL, $suffix = NULL) {
1245
  $render_array = array(
1246
    '#markup' => $html
1247
      );
1248
  if (is_numeric($weight)) {
1249
    $render_array['#weight'] = $weight;
1250
  }
1251
  if($prefix){
1252
    $render_array['#prefix'] = $prefix;
1253
  }
1254
  if($suffix) {
1255
    $render_array['#suffix'] = $suffix;
1256
  }
1257
  return $render_array;
1258
}
1259

    
1260
/**
1261
 * Loads the subgraph of a given PolytomousKeyNode.
1262
 *
1263
 * Loads the subgraph of the given PolytomousKeyNode recursively from
1264
 * the CDM REST service.
1265
 *
1266
 * @param mixed $polytomousKeyNode
1267
 *   PolytomousKeyNode passed by reference.
1268
 *
1269
 * @return void
1270
 */
1271
function _load_polytomousKeySubGraph(&$polytomousKeyNode) {
1272

    
1273
  if (!$polytomousKeyNode) {
1274
    return;
1275
  }
1276
  if ($polytomousKeyNode->class != "PolytomousKeyNode") {
1277
    drupal_set_message('_load_polytomousKeySubGraph(): ' . t('invalid type given.'), 'error');
1278
    return;
1279
  }
1280
  if (!is_uuid($polytomousKeyNode->uuid)) {
1281
    drupal_set_message('_load_polytomousKeySubGraph(): ' . t('invalid type given.'), 'error');
1282
    return;
1283
  }
1284

    
1285
  $polytomousKeyNode = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, $polytomousKeyNode->uuid);
1286

    
1287
  if (!$polytomousKeyNode) {
1288
    // drupal_set_message("_load_polytomousKeyChildNodes() : could not load polytomousKeyNode", "error");
1289
    return;
1290
  }
1291

    
1292
  // Load children.
1293
  foreach ($polytomousKeyNode->children as &$childNode) {
1294
    _load_polytomousKeySubGraph($childNode);
1295
  }
1296

    
1297
  // Load subkey.
1298
  $polytomousKeyNode->subkey = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, array($polytomousKeyNode->uuid, "subkey"));
1299

    
1300
  // Load taxon.
1301
  $polytomousKeyNode->taxon = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, array($polytomousKeyNode->uuid, "taxon"));
1302
  _load_taxonBase($polytomousKeyNode->taxon);
1303
  return;
1304
}
1305

    
1306
/**
1307
 * @todo Please document this function.
1308
 * @see http://drupal.org/node/1354
1309
 */
1310
function cdm_dataportal_view_polytomousKey($polytomousKeyUuid) {
1311

    
1312
  cdm_check_valid_portal_page();
1313

    
1314
  $polytomousKey = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, $polytomousKeyUuid);
1315

    
1316
  $sourcePager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'sources'));
1317
  if (is_array($sourcePager->records)) {
1318
    $polytomousKey->sources = $sourcePager->records;
1319
    // $polytomousKey->sources->citation = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'sources'));
1320
  }
1321

    
1322
  $annotationPager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'annotations'));
1323
  if (is_array($annotationPager->records)) {
1324
    $polytomousKey->annotations = $annotationPager->records;
1325
  }
1326

    
1327
  _load_polytomousKeySubGraph($polytomousKey->root);
1328
  return theme('cdm_polytomousKey_page', array('polytomousKey' => $polytomousKey));
1329
}
1330

    
1331
/**
1332
 * Creates a taxon page view or a chapter of it.
1333
 *
1334
 * The taxon page gives detailed information on a taxon, it shows:
1335
 *  - Taxon name.
1336
 *  - Full list of synonyms homotypic synonyms on top, followed by the
1337
 *    heterotypic and finally followed by misapplied names.
1338
 *    The list is ordered historically.
1339
 *  - All description associated with the taxon.
1340
 *
1341
 * @param string $uuid
1342
 * @param string $chapter
1343
 *   Name of the part to display, valid values are:
1344
 *   'description', 'images', 'synonymy', 'specimens', 'all'.
1345
 *
1346
 * @return unknown_type
1347
 */
1348
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all', $synonym_uuid = NULL) {
1349

    
1350
  cdm_check_valid_taxon_page($chapter);
1351
  cdm_dd("START OF TAXON PAGE [" . $chapter . "] " . $uuid . ' for ' . $_GET['q']);
1352
  // show a warning in case the javascript development mode is anabled
1353
  if(variable_get('cdm_js_devel_mode', FALSE)) {
1354
    drupal_set_message(t('The !url1 is enabled.
1355
        WARNING: this is a performance penalty and must be turned off on production websites.', array(
1356
          '!url1' => l('java-script development mode', 'admin/config/cdm_dataportal/settings', array('fragment' => 'edit-cdm-js-devel-mode'))
1357
    )),
1358
    'warning'
1359
        );
1360
  }
1361

    
1362
  // Display the page for the taxon defined by $uuid.
1363
  // set_last_taxon_page_tab(arg(3));
1364
  $taxonpage = cdm_dataportal_taxon_view($uuid, $chapter, $synonym_uuid);
1365
  if (!empty($taxonpage)) {
1366
    cdm_dd("END OF TAXON PAGE [" . $chapter . "] " . $uuid);
1367
    return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title, $taxonpage->content);
1368
  }
1369
  else {
1370
    cdm_dd("END OF TAXON PAGE [" . $chapter . "] " . $uuid . ' !!! PAGE IS EMPTY !!!');
1371
    return '';
1372
  }
1373
}
1374

    
1375
/**
1376
 * This function will genreate the taxon page part ($chapter) and returns a taxonpage object
1377
 * which has two fields, one for the page title and one for the content. Later on in the
1378
 * process chain the value contained in these fields will be passed to the cdm_node_show()
1379
 * function as the function parameters $title and $content.
1380
 *
1381
 * @param string $uuid
1382
 *   the uuid of the taxon to show
1383
 * @param string $chapter
1384
 *   Name of the part to display, valid values are:
1385
 *   'description', 'images', 'synonymy', 'all'.
1386
 *
1387
 * @return taxonpage object with the following fields:
1388
 *   - title : the title of the page
1389
 *   - content: the content of the page
1390
 *
1391
 */
1392
function cdm_dataportal_taxon_view($uuid, $chapter = 'all') {
1393
  // Taxon object.
1394
  $taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $uuid);
1395
  if (empty($taxon)) {
1396
    drupal_set_title(t('Taxon does not exist'), PASS_THROUGH);
1397
    return FALSE;
1398
  }
1399
  $taxonpage = new stdClass();
1400

    
1401
  $taxonpage->title = theme('cdm_taxon_page_title', array(
1402
    'taxon' => $taxon
1403
  ));
1404

    
1405
  // Check if the taxon id contained in the currently selected tree.
1406
  $taxon_in_current_classification = taxon_in_current_classification($uuid);
1407

    
1408
  if (!$taxon_in_current_classification) {
1409
    $classifications = get_classifications_for_taxon($taxon);
1410
    RenderHints::pushToRenderStack('not_in_current_classification');
1411
    $taxon_name_markup = render_taxon_or_name($taxon);
1412

    
1413
    if (count($classifications) == 0) {
1414
      drupal_set_message(t('This concept of the taxon !taxonname is not contained as an accepted taxon in the currently chosen classification.',
1415
        array(
1416
        '!taxonname' => $taxon_name_markup,
1417
        )
1418
      ), 'warning');
1419
    }
1420
    else {
1421
      $trees = '';
1422
      foreach ($classifications as $classification) {
1423
        if (isset($classification->titleCache)) {
1424
          $trees .= ($trees ? ', ' : '') . '<strong>' . $classification->titleCache . '</strong>';
1425
        }
1426
      }
1427

    
1428
      drupal_set_message(format_plural(count($trees),
1429
          'This concept of the taxon !taxonname is not contained as an accepted taxon in the currently chosen classification, but in this one: !trees',
1430
          'This concept of the taxon !taxonname is not contained as an accepted taxon in the currently chosen classification, but in one of these: !trees',
1431
          array('!taxonname' => $taxon_name_markup, '!trees' => $trees)
1432
        ) ,
1433
        'warning');
1434
    }
1435
    RenderHints::popFromRenderStack();
1436
  }
1437

    
1438
  // Render the taxon page.
1439
  $render_array = compose_cdm_taxon_page($taxon, $chapter);
1440
  $taxonpage->content = drupal_render($render_array);
1441

    
1442
  return $taxonpage;
1443
}
1444

    
1445
/**
1446
 * Creates a specimen page view.
1447
 * @param string $uuid the UUID of the specimen
1448
 * @return array|string
1449
 */
1450
function cdm_dataportal_specimen_page_view($uuid) {
1451

    
1452
    //cdm_check_valid_taxon_page($chapter);
1453
    //cdm_dd("START OF TAXON PAGE [" . $chapter . "] " . $uuid . ' for ' . $_GET['q']);
1454
    // show a warning in case the javascript development mode is anabled
1455
    if(variable_get('cdm_js_devel_mode', FALSE)) {
1456
        drupal_set_message(t('The !url1 is enabled.
1457
        WARNING: this is a performance penalty and must be turned off on production websites.', array(
1458
            '!url1' => l('java-script development mode', 'admin/config/cdm_dataportal/settings', array('fragment' => 'edit-cdm-js-devel-mode'))
1459
        )),
1460
            'warning'
1461
        );
1462
    }
1463

    
1464
    // Display the page for the specimen defined by $uuid.
1465
    $specimenpage = cdm_dataportal_specimen_view($uuid);
1466
    if (!empty($specimenpage)) {
1467
        return cdm_node_show(NODETYPE_TAXON, $uuid, $specimenpage->title, $specimenpage->content);
1468
    }
1469
    else {
1470
        return '';
1471
    }
1472
}
1473

    
1474
/**
1475
 *
1476
 * Creates a specimen view.
1477
 * @param string $uuid the UUID of the specimen
1478
 * @return array|string
1479
 */
1480
function cdm_dataportal_specimen_view($uuid) {
1481
    $specimen = cdm_ws_get(CDM_WS_OCCURRENCE, $uuid);
1482
    if (empty($specimen)) {
1483
        drupal_set_title(t('Specimen does not exist'), PASS_THROUGH);
1484
        return FALSE;
1485
    }
1486
    $specimenpage = new stdClass();
1487

    
1488
    $specimenpage->title = theme('cdm_specimen_page_title', array(
1489
        'specimen' => $specimen
1490
    ));
1491

    
1492
    // Render the specimen page.
1493
    $render_array = compose_cdm_specimen_page($uuid);
1494
    $specimenpage->content = drupal_render($render_array);
1495

    
1496
    return $specimenpage;
1497
}
1498

    
1499
/**
1500
 * Returns a name page as a Drupal node ready to be renderized by Drupal.
1501
 *
1502
 * The node page shows the taxon name title and the list of taxon related
1503
 * with such taxon. Name on the tree already in use.
1504
 *
1505
 * @param UUID $taxon_name_uuid
1506
 *   The uuid of the CDM TaxonNameBase to show a name page for
1507
 * @param UUID $taxon_to_hide_uuid
1508
 *   A taxon which should not be displayed in the taxon list
1509
 * @param UUID $highlite_synonym_uuid
1510
 *   Optinal parameter wich takes another taxon uuid, if given the
1511
 *   target taxon pages will show the syonymy tab where the taxon
1512
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1513
 *   in case it is found on this page.
1514
 *
1515
 * @return mixed
1516
 *   The formatted name page as node.
1517
 */
1518
function cdm_dataportal_name_page_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid = NULL) {
1519

    
1520
  cdm_check_valid_portal_page();
1521

    
1522
  $taxonname_page = cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid);
1523
  if (!empty($taxonname_page)) {
1524
    return cdm_node_show(NODETYPE_NAME, $taxon_name_uuid, $taxonname_page->title, $taxonname_page->content);
1525
  }
1526
  else {
1527
    return '';
1528
  }
1529
}
1530

    
1531
/**
1532
 * View function for a TaxonNameBase page.
1533
 *
1534
 * The name page lists all taxa for which the name specified by the
1535
 * $taxon_name_uuid is being used. I case there is only one name the
1536
 * page automatically redirects ti the according taxon page. Otherwise
1537
 * the list of names is displayed.
1538
 *
1539
 * The parameter $taxon_to_hide_uuid allows to exclude a taxon from the
1540
 * list of taxa. This is useful for example when referencing from a taxon
1541
 * to the name page and the referring taxon should not be repeaded in the
1542
 * name page.
1543
 *
1544
 *
1545
 * @param UUID $taxon_name_uuid
1546
 *   The uuid of the CDM TaxonNameBase to show a name page for
1547
 * @param UUID $taxon_to_hide_uuid
1548
 *   A taxon which should not be displayed in the taxon list
1549
 * @param UUID $highlite_synonym_uuid
1550
 *   Optinal parameter wich takes another taxon uuid, if given the
1551
 *   target taxon pages will show the syonymy tab where the taxon
1552
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1553
 *   in case it is found on this page.
1554
 *
1555
 * @return object
1556
 *   An object with two fields:
1557
 *     - title: the page title
1558
 *     - content: the page content
1559
 */
1560
function cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $highlite_synonym_uuid = NULL) {
1561
  // Getting the full taxonname object from the server.
1562
  $taxon_name = cdm_ws_get(CDM_WS_PORTAL_NAME, array($taxon_name_uuid));
1563
  if (!$taxon_name) {
1564
    drupal_set_title(t('Taxon name does not exist'), PASS_THROUGH);
1565
    return FALSE;
1566
  }
1567
  // Searching for all the taxa connected with the taxon name on the tree
1568
  // in use.
1569
  $name_cache = cdm_ws_get(CDM_WS_NAME_NAMECAHE, array($taxon_name_uuid));
1570
  $request_params = array();
1571
  $request_params['query'] = $name_cache;
1572
  $request_params['tree'] = get_current_classification_uuid();
1573
  $request_params['doTaxa'] = 1;
1574
  $request_params['doSynonyms'] = 1;
1575
  $request_params['doTaxaByCommonNames'] = 0;
1576
  $request_params['matchMode'] = "EXACT";
1577
  $taxon_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_FIND, NULL, queryString($request_params));
1578

    
1579
  // Removing the name where we came from.
1580
  foreach ($taxon_pager->records as $k => &$taxon) {
1581
    if ($taxon->uuid == $taxon_to_hide_uuid) {
1582
      unset($taxon_pager->records[$k]);
1583
    }
1584
  }
1585
  // Show the taxa list or go to the singular taxon.
1586
  if (sizeof($taxon_pager->records) == 1) {// Single taxon case.
1587
    $singleTaxon = array_pop($taxon_pager->records);
1588
    if ($singleTaxon->class != "Taxon") {
1589
      // It is a Synonym -> look for the accepted.
1590
      $accepted_taxa = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, array($singleTaxon->uuid, get_current_classification_uuid()));
1591
      if (!empty($highlite_synonym_uuid)) {
1592
        drupal_goto('cdm_dataportal/taxon/' . $accepted_taxa[0]->uuid . '/synonymy', array('query' => array('highlite' => $highlite_synonym_uuid)));
1593
      }
1594
      else {
1595
        drupal_goto('cdm_dataportal/taxon/' . $accepted_taxa[0]->uuid . '/synonymy', array('query' => array('highlite' => $singleTaxon->uuid)));
1596
      }
1597
    }
1598
    else {
1599
      // It is an accepted taxon.
1600
      if (!empty($highlite_synonym_uuid)) {
1601
        drupal_goto('cdm_dataportal/taxon/' . $singleTaxon->uuid . '/synonymy', array('query' => array('highlite' => $highlite_synonym_uuid)));
1602
      }
1603
      else {
1604
        drupal_goto('cdm_dataportal/taxon/' . $singleTaxon->uuid);
1605
      }
1606
    }
1607
  }
1608
  else {// More than one taxa case.
1609
    $taxon_name_page = new stdClass();
1610
    $taxon_name_page->title = theme('cdm_name_page_title', array('taxon_name' => $taxon_name));
1611
    if ($taxon_pager->records) {
1612
      $taxon_name_page->content = compose_list_of_taxa($taxon_pager->records);
1613
    }
1614
    else {
1615
      $taxon_name_page->content = 'This name has no taxa';
1616
    }
1617
    return $taxon_name_page;
1618
  }
1619
}
1620

    
1621
/**
1622
 * Creates a page with the advance search form.
1623
 *
1624
 * NOTE: The advance search form allows searching for taxa.
1625
 */
1626
function cdm_dataportal_view_search_advanced() {
1627
  drupal_set_title(t('Advanced search'), PASS_THROUGH);
1628
  return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
1629
}
1630

    
1631
/**
1632
 * Creates a page with the search form for searching by taxon descriptions.
1633
 */
1634
function cdm_dataportal_view_search_taxon_by_description() {
1635
  drupal_set_title(t('Search by factual data'), PASS_THROUGH);
1636
  return drupal_get_form('cdm_dataportal_search_taxon_by_description_form');
1637
}
1638

    
1639
/**
1640
 * Executes the search and generates the result list of taxa.
1641
 */
1642
function cdm_dataportal_view_search_results_taxon() {
1643

    
1644
  $taxonPager = cdm_dataportal_search_execute();
1645

    
1646
  $showThumbnails = do_showThumbnails();
1647

    
1648
  $setSessionUri = url('cdm_api/setvalue/session', array(
1649
      'query' => array('var' => '[pageoption][searchtaxa][showThumbnails]', 'val' => ''),
1650
  ));
1651

    
1652
  drupal_add_js('jQuery(document).ready(function() {
1653

    
1654
      // init
1655
      if(' . $showThumbnails . ' == 1){
1656
          jQuery(\'.media_gallery\').show(20);
1657
      } else {
1658
          jQuery(\'.media_gallery\').hide(20);
1659
      }
1660

    
1661
      // add change handler
1662
      jQuery(\'#showThumbnails input.showThumbnails\').change(
1663
      function(event){
1664
        var state = 0;
1665
        if(jQuery(this).is(\':checked\')){
1666
          jQuery(\'.media_gallery\').show(20);
1667
          state = 1;
1668
        } else {
1669
          jQuery(\'.media_gallery\').hide(20);
1670
        }
1671
        // store state in session variable
1672
        var uri = \'' . $setSessionUri . '\' + state;
1673
        jQuery.get(uri);
1674
      });
1675
  });',
1676
  array('type' => "inline", 'scope' => JS_DEFAULT));
1677

    
1678
  drupal_set_title(t('Search results'), PASS_THROUGH);
1679

    
1680
  return theme('cdm_search_results', array(
1681
    'pager' => $taxonPager,
1682
    'path' => 'cdm_dataportal/search/results/taxon',
1683
    ));
1684
}
1685

    
1686
/**
1687
 * Provides the standart image wich indicated a loading process
1688
 *
1689
 * @return string
1690
 *  The img html tag
1691
 */
1692
function loading_image_html() {
1693
  return '<img class="loading" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal')
1694
    . '/images/loading_circle_grey_16.gif" style="display:none;">';
1695
}
1696

    
1697
/**
1698
 * Returns the state of the the showThumbnails flag set in the
1699
 * users session ($_SESSION['pageoption']['searchtaxa']['showThumbnails']).
1700
 *
1701
 * @return boolean
1702
 *    returns 1 if the flag is set
1703
 */
1704
function do_showThumbnails() {
1705
  static $showThumbnails = null;
1706

    
1707
  if($showThumbnails == null) {
1708
    $showThumbnails = 0;
1709
    if (!isset($_SESSION['pageoption']['searchtaxa']['showThumbnails'])) {
1710
      $showThumbnails = 0;
1711
      $search_gallery_settings = variable_get(CDM_DATAPORTAL_SEARCH_GALLERY_NAME, null);
1712
      $showThumbnails = is_array($search_gallery_settings)
1713
        && isset($search_gallery_settings['cdm_dataportal_show_taxon_thumbnails'])
1714
        && (
1715
            $search_gallery_settings['cdm_dataportal_show_taxon_thumbnails'] +
1716
            $search_gallery_settings['cdm_dataportal_show_synonym_thumbnails'] +
1717
            $search_gallery_settings['cdm_dataportal_show_thumbnail_captions'] > 0
1718
            )
1719
         ? 1 : 0;
1720

    
1721
       drupal_array_set_nested_value($_SESSION, array('pageoption', 'searchtaxa', 'showThumbnails'), $showThumbnails);
1722
    }
1723
    $showThumbnails = $_SESSION['pageoption']['searchtaxa']['showThumbnails'];
1724
    if (!is_numeric($showThumbnails)) {
1725
      $showThumbnails = 1;
1726
    }
1727
  }
1728

    
1729
  return $showThumbnails;
1730
}
1731

    
1732
/**
1733
 * View which transforms XML output from a given webservice endpoint into JSON.
1734
 */
1735
/*
1736
 function cdm_view_xml2json(){
1737
 $file = arg(2);
1738
 $datastr = file_get_contents(variable_get('cdm_webservice_url', '').$file);
1739
 return  xml2json::transformXmlStringToJson($datastr);
1740
 }
1741
 */
1742

    
1743
/* ====================== other functions ====================== */
1744
/**
1745
 * Creates a URL to the taxon page specified by the $uuid parameter.
1746
 *
1747
 * The URL will be prepended with a path element to a specific taxon page tab.
1748
 *
1749
 * This tab is either taken from the CDM_DATAPORTAL_DEFAULT_TAXON_TAB which can
1750
 * be set globally in the administrative settings or individually in the user
1751
 * profile. If the CDM_DATAPORTAL_DEFAULT_TAXON_TAB value is set to LAST_VISITED_TAB
1752
 * the last portal will stay on this last tab.
1753
 *
1754
 * A third option is offerered by the $page_tab parameter which allows overwriting this
1755
 * internal mechanism by a specific value.
1756
 *
1757
 * @param string $uuid
1758
 *   The UUID of the taxon.
1759
 * @param string $page_tab
1760
 *   Overwriting the preset mechanism by defining specific value for the
1761
 *   taxon page tab.
1762
 *
1763
 * @return string
1764
 *   The created URL.
1765
 */
1766
function path_to_taxon($uuid, $page_tab = FALSE) {
1767

    
1768
  $tab = get_default_taxon_tab();
1769
  $values = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
1770

    
1771
  if (!$uuid) {
1772
    return FALSE;
1773
  }
1774

    
1775
  if ($page_tab) {
1776
    return 'cdm_dataportal/taxon/' . $uuid . '/' . $page_tab;
1777
  }
1778
  elseif (!$tab || strtolower($tab) == 'general') {
1779
    return 'cdm_dataportal/taxon/' . $uuid;
1780
  }
1781
  elseif (get_last_taxon_page_tab() &&   $tab == $values[CDM_DATAPORTAL_LAST_VISITED_TAB_ARRAY_INDEX]) {
1782
    return 'cdm_dataportal/taxon/' . $uuid . '/' . get_last_taxon_page_tab();
1783
  }
1784
  else {
1785
    return 'cdm_dataportal/taxon/' . $uuid . '/' . strtolower($tab);
1786
  }
1787
}
1788

    
1789
function path_to_specimen($uuid) {
1790

    
1791
    if (!$uuid) {
1792
        return FALSE;
1793
    }
1794
    else {
1795
        return 'cdm_dataportal/specimen/' . $uuid;
1796
    }
1797
}
1798

    
1799
/**
1800
 * Creates a URL to show a synonmy in the according taxon page.
1801
 *
1802
 * The URL will point to the synonymy tab of the taxon page of the accepted taxon given as parameter $acceptedUuid.
1803
 * The resulting URI will include query parameters to highlight the synonym, and to optionally display
1804
 * the accepted taxons name in aform like "Foo bar is accepted taxon for Ree doo". The URI will also
1805
 * include the sysnonym uuid as fragment in order to let the browser scroll to the according location
1806
 * in the page
1807
 *
1808
 * @param string $synonymUuid
1809
 *    The uuid of the synonym
1810
 * @param string $acceptedUuid
1811
 *    The uuid of the according accepted taxon
1812
 * @return string
1813
 *    The URL to show a synonmy in the according taxon page
1814
 */
1815
function uri_to_synonym($synonymUuid, $acceptedUuid) {
1816
  $acceptedPath = path_to_taxon($acceptedUuid, "synonymy");
1817
  return url($acceptedPath, array(
1818
      'query' => array(
1819
        // highlite the synony in the synonymy
1820
        'highlite' => $synonymUuid,
1821
        // the taxon page is refered from a synonym and the synonym can optionally be named in the page title
1822
        // see theme_taxon_page_title()
1823
        'acceptedFor' => $synonymUuid
1824
      ),
1825
      'fragment' => $synonymUuid
1826
  ));
1827

    
1828
}
1829

    
1830
/**
1831
 * Compses the drupal path to the key identified by the uuid.
1832
 *
1833
 * @param string $keyType
1834
 *    the key typer corresponds to the specific class of the CDM
1835
 *    IdentificationKey. Possible values are
1836
 *      -PolytomousKey
1837
 *      -MultimediaKey
1838
 *      - ...
1839
 * @param UUID $keyUuid
1840
 *   The UUID of the key
1841
 */
1842
function path_to_key($keyType, $keyUuid) {
1843
  if (!$keyUuid || !$keyType) {
1844
    return FALSE;
1845
  }
1846
  $keyType{0} = strtolower($keyType{0});
1847
  return "cdm_dataportal/" . $keyType . "/$keyUuid";
1848
}
1849

    
1850
/**
1851
 * @todo Please document this function.
1852
 * @see http://drupal.org/node/1354
1853
 */
1854
function path_to_reference($uuid) {
1855
  if (!$uuid) {
1856
    return FALSE;
1857
  }
1858
  return 'cdm_dataportal/reference/' . $uuid;
1859
}
1860

    
1861
/**
1862
 * Creates the path to a cdm_dataportal taxon name page.
1863
 *
1864
 * @param UUID $taxon_name_uuid
1865
 *   The uuid of the CDM TaxonNameBase to show a name page for
1866
 * @param UUID $taxon_to_hide_uuid
1867
 *   A taxon which should not be displayed in the taxon list
1868
 * @param UUID $highlite_synonym_uuid
1869
 *   Optinal parameter wich takes another taxon uuid, if given the
1870
 *   target taxon pages will show the syonymy tab where the taxon
1871
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1872
 *   in case it is found on this page.
1873
 *
1874
 * @return a URI path element as string
1875
 */
1876
function path_to_name($name_uuid, $taxon_to_hide_uuid = NULL, $synonym_uuid  = NULL) {
1877
  $res = FALSE;
1878
  if ($name_uuid) {
1879
    $res = 'cdm_dataportal/name/' . $name_uuid;
1880
  }
1881
  if($taxon_to_hide_uuid){
1882
    $res .= '/' . $taxon_to_hide_uuid;
1883
    if($synonym_uuid){
1884
      $res .= '/' . $synonym_uuid;
1885
    }
1886
  }
1887
  return $res;
1888
}
1889

    
1890
/**
1891
 * @todo Please document this function.
1892
 * @see http://drupal.org/node/1354
1893
 */
1894
function path_to_media($uuid, $representaion_uuid = FALSE, $partId = FALSE) {
1895
  if (!$uuid) {
1896
    return FALSE;
1897
  }
1898
  $out = 'cdm_dataportal/media/' . $uuid;
1899
  if ($representaion_uuid) {
1900
    $out .= '/' . $representaion_uuid;
1901
    if ($partId !== FALSE) {
1902
      $out .= '/' . $partId;
1903
    }
1904
  }
1905
  return $out;
1906
}
1907

    
1908
/**
1909
 * Compares thisRank with thatRank.
1910
 *
1911
 * Returns a negative integer, zero, or a positive integer
1912
 * as the of thisRank is higher than, equal to, or lower than thatRank.
1913
 * e.g:
1914
 * <ul>
1915
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
1916
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
1917
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
1918
 * </ul>
1919
 * <p>
1920
 * This compare logic of the underlying webservice is the
1921
 * <b>inverse logic</b> of the the one implemented in
1922
 * java.lang.Comparable#compareTo(java.lang.Object)
1923
 *
1924
 * @param $thisRankUuid
1925
 * @param $thatRankUuid
1926
 *
1927
 * @return int
1928
 *   A negative integer, zero, or a positive integer
1929
 *   as the thisRank is lower than, equal to, or higher than thatRank.
1930
 */
1931
function rank_compare($thisRankUuid, $thatRankUuid) {
1932
  $result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
1933
  return $result->Integer;
1934
}
1935

    
1936
/**
1937
 * Composes an HTML element class attribute value composed of
1938
 * the shortname of the cdm class and the uuid of the entity.
1939
 * This class attribute should be used whereever an cdm-entity is rendered.
1940
 *
1941
 * These according class selectors in css must be escaped, eg:
1942
 *    .cdm\:TextData
1943
 *
1944
 * @param $cdmEntity
1945
 */
1946
function html_class_attribute_ref($cdmEntity) {
1947

    
1948
  if (is_cdm_entity($cdmEntity)) {
1949
    return "cdm:" . $cdmEntity->class . " uuid:" . $cdmEntity->uuid;
1950
  }
1951
}
1952

    
1953

    
1954
/**
1955
 * Creates a short taxonname.
1956
 *
1957
 * The short name is created by using the taggedTitle field of
1958
 * NameSTO or NameTO instances.
1959
 * If the taggedTitle if empty the fullname will be returned.
1960
 *
1961
 * @param unknown_type $name
1962
 *   Name or TreeNode.
1963
 *
1964
 * @return string
1965
 */
1966
function cdm_dataportal_shortname_of($name) {
1967
  $nameStr = '';
1968

    
1969
  normalize_tagged_text($name->taggedTitle);
1970

    
1971
  // Get all tagged text tokens of the scientific name.
1972
  foreach ($name->taggedTitle as $tagtxt) {
1973
    if ($tagtxt->type == 'name' || $tagtxt->type == 'rank') {
1974
      $nameStr .= ($nameStr ? ' ' : '') . $tagtxt->text;
1975
    }
1976
  }
1977
  $nameStr = trim($nameStr);
1978

    
1979
  if ($nameStr) {
1980

    
1981
    // Do not return short names for these.
1982
    if ($name->unplaced || $name->excluded) {
1983
      return $nameStr;
1984
    }
1985

    
1986
    /*
1987
    1st capture group (^[a-zA-Z]): First letter of uninomial.
1988
    Second capture group ([\p{L}]+): remaining letters of uninomial ([\p{L} = an UTF-8 letter).
1989
    Third capture group (\s+[^(\x2E]+\s+.+$|\s+[a-zA-Z]+$): letters of name,
1990
    but only matching if no '(' or '.' in second word of name,        ( \x2E = '.')
1991
    OR only one specific epithet \s+[\p{L}\x22\x2D\xD7]+$             (\x22= '"', \x2D='-', \xD7='×' )
1992
    */
1993
    $pattern = '/(^[a-zA-Z])([\p{L}]+)(\s+[^(\x2E]+\s+.+$|\s+[\p{L}\x22\x2D\xD7]+$)/u';
1994
    if (preg_match($pattern, $nameStr, $matches, PREG_OFFSET_CAPTURE)) {
1995
      return $matches[1][0] . "." . $matches[3][0];
1996
    }
1997
    else {
1998
      return $nameStr;
1999
    }
2000
  }
2001
  else {
2002
    return $name->titleCache;
2003
  }
2004
}
2005

    
2006
/**
2007
 * Check if a taxon is accepted by the current taxonomic tree.
2008
 *
2009
 * @param mixed $taxon
2010
 *   The Taxon obkect to check.
2011
 *
2012
 * @return bool
2013
 *   Returns TRUE if $taxon is accepted, FALSE otherwise.
2014
 */
2015
function _cdm_dataportal_acceptedByCurrentView($taxon) {
2016

    
2017
  $defaultTreeUuid = get_current_classification_uuid();
2018

    
2019
  if (isset($taxon->taxonNodes)) {
2020
    $taxonNodes = $taxon->taxonNodes;
2021
  }
2022
  else {
2023
    $taxonNodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, $taxon->uuid);
2024
  }
2025

    
2026
  if ($taxon->class == "Taxon" && isset($taxonNodes)) {
2027
    foreach ($taxonNodes as $node) {
2028
      if (isset($node->classification)){
2029
        if(is_object($node->classification)) {
2030
          if ($node->classification->uuid == $defaultTreeUuid) {
2031
            return TRUE;
2032
          }
2033
        }
2034
        else {
2035
          if ($node->classification == $defaultTreeUuid) {
2036
            return TRUE;
2037
          }
2038
        }
2039
      }
2040
    }
2041
  }
2042

    
2043
  return FALSE;
2044
}
2045

    
2046
/**
2047
 * Checks is the source has one of the given types.
2048
 *
2049
 * @param object $source
2050
 *   The original source entity
2051
 * @param array $types
2052
 *   An array of elementd of the OriginalSourceType enumeration
2053
 *   If not set the default will be used which is:
2054
 *    - Lineage
2055
 *    - PrimaryMediaSource
2056
 *    - PrimaryTaxonomicSource
2057
 *    - Unknown
2058
 *    - Other
2059
 * @return boolean
2060
 */
2061
  function _is_original_source_type($source, $types = null) {
2062
    // this is the default
2063
    // maybe this should also be put into the settings
2064
    static $default = array(
2065
      OriginalSourceType::Lineage,
2066
      OriginalSourceType::PrimaryMediaSource,
2067
      OriginalSourceType::PrimaryTaxonomicSource,
2068
      OriginalSourceType::Unknown,
2069
      OriginalSourceType::Other,
2070
    );
2071

    
2072
    if(!$types){
2073
      $types = $default;
2074
    }
2075
    return isset($source->type) && in_array($source->type, $types);
2076
  }
2077

    
2078
/**
2079
 * @todo Please document this function.
2080
 * @see http://drupal.org/node/1354
2081
 */
2082
function _is_invers_taxonRelationship($taxonRelationship, $focusedTaxon) {
2083
  return $taxonRelationship->toTaxon->uuid == $focusedTaxon->uuid;
2084
}
2085

    
2086

    
2087
/**
2088
 * Collects all the media from a list of description elements.
2089
 *
2090
 * @param array $descriptionElements
2091
 *   The description elements from which to collect the media.
2092
 *
2093
 * @return array
2094
 *   The output with all the collected media.
2095
 */
2096
function cdm_dataportal_media_from_descriptionElements($descriptionElements) {
2097

    
2098
  $outArrayOfMedia = array();
2099

    
2100
  // Avoiding a warning box in Drupal for Flora Malesiana.
2101
  if (isset($descriptionElements) && is_array($descriptionElements)) {
2102
    foreach ($descriptionElements as $descriptionElement) {
2103
      if (isset($descriptionElement->media) && is_array($descriptionElement->media)) {
2104
        foreach ($descriptionElement->media as $media) {
2105
          if (is_object($media)) {
2106
            $outArrayOfMedia[] = $media;
2107
          }
2108
        }
2109
      }
2110
    }
2111
  }
2112
  return $outArrayOfMedia;
2113
}
2114

    
2115
/**
2116
 * @todo Please document this function.
2117
 * @see http://drupal.org/node/1354
2118
 *
2119
 * @param array $cdm_entities
2120
 *   An array of CdmBase instances or a single instance.
2121
 * @param string $footnote_list_key_suggestion
2122
 *
2123
 * @return unknown
2124
 */
2125
function cdm_annotations_as_footnotekeys($cdm_entities, $footnote_list_key_suggestion = NULL) {
2126

    
2127
   static $annotations_types_filter = null;
2128
   if(!$annotations_types_filter) {
2129
     $annotations_types_filter = unserialize(ANNOTATIONS_TYPES_AS_FOOTNOTES_DEFAULT);
2130
   }
2131

    
2132
  $footNoteKeys = array();
2133

    
2134
  // Is argument cdmBase an array?
2135
  if (!is_array($cdm_entities)) {
2136
    $cdmBase_array = array();
2137
    $cdmBase_array[] = $cdm_entities;
2138
  }
2139
  else {
2140
    $cdmBase_array = $cdm_entities;
2141
  }
2142

    
2143
  // Getting the key for the footnotemanager.
2144
  if (isset($footnote_list_key_suggestion)) {
2145
    $footnote_list_key = $footnote_list_key_suggestion;
2146
  }
2147
  else {
2148
    $footnote_list_key = RenderHints::getFootnoteListKey() . '-annotations';
2149
  }
2150

    
2151
  // Adding the footnotes keys.
2152
  foreach ($cdmBase_array as $cdmBase_element) {
2153
    $annotations = cdm_ws_getAnnotationsFor($cdmBase_element, variable_get('annotations_types_as_footnotes', $annotations_types_filter));
2154
    if (is_array($annotations)) {
2155
      foreach ($annotations as $annotation) {
2156
        $footNoteKeys[] = FootnoteManager::addNewFootnote($footnote_list_key, $annotation->text);
2157
      }
2158
    }
2159
  }
2160

    
2161
  return $footNoteKeys;
2162
}
2163

    
2164

    
2165
/**
2166
 * Creates a CDM Dynabox.
2167
 *
2168
 * @param string $dynabox_id
2169
 *   a uninque name for tha dynabox, using a cdm entity uuid as id is good practice.
2170
 * @param string $label
2171
 *   The clickable text to show.
2172
 * @param string $content_url
2173
 *   The cdm REST service request url wich will deliver the content to be shown
2174
 *   once the dynabox toggles open.
2175
 * @param string $theme
2176
 *   The theme to be used for rendering the cdm REST service response with is
2177
 *   returned from the $content_url.
2178
 * @param string $link_alt_text
2179
 *   The value for the alt attribute of the dynabox link.
2180
 * @param array $enclosingtags
2181
 *   An array with two elements: $enclosingtags[0] will be used for the dynabox
2182
 *   element itself, $enclosingtags[1] is the tag to be used for the
2183
 *   dynabox_content (optional)
2184
 * @param array $attributes
2185
 * @param $content_element_selector
2186
 *   Optional jQuery selector which can be used to reference a dom element which should
2187
 *   be used as container for the content to be shown. The dynabox-<dynabox id>-content
2188
 *  element will be placed in this container.
2189
 *
2190
 * @param string $open_callback
2191
 *   optional javascript call back function to be triggered after toggling the box to
2192
 *   the open state.
2193
 * @param string $close_callback
2194
 *   optional javascript call back function to be triggered after toggling the box to
2195
 *   the closed state.
2196
 * @return string Returns HTML for a dynabox.
2197
 * Returns HTML for a dynabox.
2198
 */
2199
function cdm_dynabox($dynabox_id, $label, $content_url, $theme, $link_alt_text,
2200
                     $enclosingtags = array('li', 'ul'), $attributes = array(),
2201
                     $content_element_selector = null,
2202
                     $open_callback = 'function(){}', $close_callback = 'function(){}' ) {
2203
  $out = '';
2204

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

    
2208
  if(!array_key_exists('class', $attributes)) {
2209
    $attributes['class'] = array();
2210
  }
2211
  $attributes['id'][] = 'dynabox-' . $dynabox_id;
2212
  $dynabox_attributes = drupal_attributes($attributes);
2213

    
2214

    
2215
  _add_js_domEvent(); // requires domEvent.js
2216
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cdm_dynabox.js');
2217
  drupal_add_js("
2218
  jQuery(document).ready(
2219
      function() {
2220
        dynabox('". $dynabox_id ."',
2221
          {
2222
            open_callback: " . $open_callback .",
2223
            close_callback: " . $close_callback .
2224
            ($content_element_selector ? ",\n content_container_selector: '" . $content_element_selector . "'" : "") . "
2225
          }
2226
        );
2227
      }
2228
   );",
2229
   array(
2230
    'type'=>'inline',
2231
    'scope'=>'footer'
2232
    )
2233
  );
2234

    
2235

    
2236
  $cdm_proxy_url = url('cdm_api/proxy/' . urlencode($content_url) . "/$theme");
2237
  $out .= '<!-- dynabox for ' . $content_url . ' -->';
2238
  $out .= '<' . $enclosingtags[0] . ' ' .  $dynabox_attributes. '><a href="' . $cdm_proxy_url . '" class="label" alt="' . t($link_alt_text) . '">' . $label . '</a>';
2239
  $out .= '  <' . $enclosingtags[1] . ' id="dynabox-' . $dynabox_id . '-content">';
2240
  $out .= '    <' . $enclosingtags[0] . ' class="dynabox-content-inner">' . loading_image_html() . '</' . $enclosingtags[0] . '>';
2241
  $out .= '    </' . $enclosingtags[1] . '>';
2242
  $out .= '  </' . $enclosingtags[0] . '>';
2243
  $out .= '<!-- dynabox end -->';
2244
  return $out;
2245
}
2246

    
2247
/**
2248
 * Checks whether a feature has any description elements.
2249
 *
2250
 * @param mixed $featureNode
2251
 *   A feature node as produced by the function _mergeFeatureTreeDescriptions().
2252
 *
2253
 * @see _mergeFeatureTreeDescriptions()
2254
 *
2255
 * @return bool
2256
 *   Returns TRUE if the given $featureNode or any of its subordinate nodes
2257
 *   contains at least one non empty TextData or at least one DescriptionElement
2258
 *   of an other type. A TextData element holding a multilanguageText or a
2259
 *   source reference is considered to be not empty.
2260
 *
2261
 * @TODO this function may have become obsolete by the new method of detecting empty blocks,
2262
 *       see $block_content_is_not_empty in compose_feature_blocks() and
2263
 *       $feature_block_has_content in compose_feature_block_items_generic
2264
 */
2265
function has_feature_node_description_elements($featureNode) {
2266

    
2267
  if (isset($featureNode->descriptionElements) && is_array($featureNode->descriptionElements) && count($featureNode->descriptionElements) > 0) {
2268
    if(!isset($featureNode->descriptionElements['#type'])){ // #type is used to identify e.g. DTO elements: '#type' => 'DTO'
2269
      foreach ($featureNode->descriptionElements as $descriptionElement) {
2270
        if ($descriptionElement->class != "TextData" || isset($descriptionElement->multilanguageText_L10n->text)
2271
          && $descriptionElement->multilanguageText_L10n->text != ''
2272
          || isset($descriptionElement->sources[0])
2273
          || isset($descriptionElement->media[0]) ) {
2274
          return TRUE;
2275
        }
2276
      }
2277
    }
2278
  }
2279
  else if (isset($featureNode->childNodes) && is_array($featureNode->childNodes)) {
2280
    foreach ($featureNode->childNodes as $child) {
2281
      if (has_feature_node_description_elements($child)) {
2282
        return TRUE;
2283
      }
2284
    }
2285
  }
2286
  return FALSE;
2287
}
2288

    
2289
/**
2290
 * Checks if the current page is a valid taxon portal page and responds with HTTP status 404 (not found) otherwise
2291
 *
2292
 * @param $chapter
2293
 *   The taxon page chapter or part
2294
 */
2295
function cdm_check_valid_taxon_page($chapter){
2296
  static $taxon_tabs = null;
2297

    
2298
  cdm_check_valid_portal_page();
2299

    
2300
  if($taxon_tabs == null){
2301
    $taxon_tabs = array('all', 'description');
2302
    foreach(get_taxon_tabs_list() as $tab){
2303
      $taxon_tabs[] = strtolower($tab);
2304
    }
2305
  }
2306

    
2307
  if(!in_array($chapter, $taxon_tabs)){
2308
    // oops this is not a valid chapter name
2309
    http_response_code(404); // 404 = Not Found
2310
  }
2311

    
2312
}
2313

    
2314
/**
2315
 * Checks if the current page is a valid portal page and responds with HTTP status 404 (not found) otherwise
2316
 *
2317
 * @param $chapter
2318
 *   The taxon page chapter or part
2319
 */
2320
function cdm_check_valid_portal_page(){
2321
  $ends_with_file_suffix_pattern = '/\/[^\.\/]*[\.][^\.\/]*$/';
2322
  if(preg_match($ends_with_file_suffix_pattern, $_GET['q'])){
2323
    // oops this urls ends with a file_suffix and thus does not refer to a portal page
2324
    http_response_code(404); // 404 = Not Found
2325
    exit('HTTP 404');
2326
  }
2327
}
2328

    
2329
/**
2330
 * Generates the diff of the texts and presents it in a HTML diff viewer.
2331
 *
2332
 * @param $text_a
2333
 * @param $text_b
2334
 * @return string
2335
 */
2336
function diff_viewer($text_a, $text_b) {
2337

    
2338
  static $diff_viewer_count = 0;
2339

    
2340
  $element_id = 'part_definitions_diff_' . $diff_viewer_count++;
2341

    
2342
  // http://code.stephenmorley.org/php/diff-implementation/
2343
  module_load_include('php', 'cdm_dataportal', 'lib/class.Diff');
2344
  drupal_add_css(drupal_get_path('module',
2345
      'cdm_dataportal') . '/css/diff.css');
2346
  _add_jquery_ui();
2347
  drupal_add_js(
2348
    'jQuery(document).ready( function(){
2349
        jQuery(\'#' . $element_id . '\').accordion({
2350
        collapsible: true,
2351
        active: false,
2352
        fillSpace: true,
2353
        }).children(\'div\').css({ \'height\': \'auto\' });
2354
        jQuery(\'#' . $element_id . ' table.diff\').prepend(\'<thead><tr><th>Default</th><th>User defined<th></th><tr></thead>\');
2355
     });'
2356
    , array(
2357
    'type' => 'inline',
2358
    'scope' => 'footer'
2359
  ));
2360

    
2361
  $diff = Diff::compare($text_a,
2362
    $text_b);
2363
  $diff_viewer_markup = '<div id="' . $element_id . '"><h3>View Diff</h3><div>'
2364
    . Diff::toTable($diff, '', '')
2365
    . '</div></div>';
2366
  return $diff_viewer_markup;
2367
}
2368

    
2369

    
(10-10/16)