Project

General

Profile

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