Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

cdm-dataportal / 7.x / modules / cdm_dataportal / cdm_dataportal.module @ 3eb8fbfb

History | View | Annotate | Download (63.7 KB)

1
<?php
2
/**
3
 * @file
4
 * Module to provide a CDM Dataportal.
5
 *
6
 * @copyright
7
 *   (C) 2007-2012 EDIT
8
 *   European Distributed Institute of Taxonomy
9
 *   http://www.e-taxonomy.eu
10
 *
11
 *   The contents of this module are subject to the Mozilla
12
 *   Public License Version 1.1.
13
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
14
 *
15
 * @author
16
 *   - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
17
 *   - Wouter Addink <w.addink@eti.uva.nl> (migration from Drupal 5 to Drupal7)
18
 */
19

    
20
module_load_include('php', 'cdm_dataportal', 'node_types');
21
module_load_include('php', 'cdm_dataportal', 'settings');
22
module_load_include('php', 'cdm_dataportal', 'help');
23
module_load_include('php', 'cdm_dataportal', 'cdm_dataportal.search');
24

    
25
module_load_include('inc', 'cdm_dataportal', 'includes/common');
26
module_load_include('inc', 'cdm_dataportal', 'includes/pages');
27
module_load_include('inc', 'cdm_dataportal', 'includes/media');
28
module_load_include('inc', 'cdm_dataportal', 'includes/maps');
29
module_load_include('inc', 'cdm_dataportal', 'includes/occurrences');
30
module_load_include('inc', 'cdm_dataportal', 'includes/descriptions');
31
module_load_include('inc', 'cdm_dataportal', 'includes/pre-drupal8');
32

    
33
module_load_include('inc', 'cdm_dataportal', 'theme/theme_registry');
34
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

    
50
/* ============================ java script functions ============================= */
51

    
52

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

    
60
  drupal_add_js("
61
        jQuery(document).ready(function() {
62
          jQuery.ajax({
63
            url: '" . $script_url . "',
64
            dataType: 'script',
65
            cache: true, // otherwise will get fresh copy every page load
66
            success: function() {
67
                  " . $callback . "
68
            }
69
          });
70
        });"
71
  , 'inline');
72
}
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
    ",
106
    array(
107
      'type' => 'inline',
108
      'scope' => 'footer'
109
    )
110
  );
111
}
112

    
113
function _add_js_openlayers() {
114
  $openlayers = '/js/OpenLayers-2.11/OpenLayers.js';
115

    
116
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . $openlayers, array(
117
  'type' => 'file',
118
  'weight' => JS_LIBRARY,
119
  'cache' => FALSE,
120
  ));
121
}
122

    
123
/**
124
 * @todo Please document this function.
125
 * @see http://drupal.org/node/1354
126
 */
127
function _add_js_thickbox() {
128
  // ---- jQuery thickbox:
129
  /*
130
  * bug: compat-1.0.js && thickbox.js line 237 .trigger("unload") -> event is
131
  * not triggered because of problems with compat-1.0.js' see INSTALL.txt
132
  */
133
  // drupal_add_js(drupal_get_path('module',
134
  // 'cdm_dataportal').'/js/jquery.imagetool.min.js');
135
  //
136
  // Add a setting for the path to cdm_dataportal module, used to find the path
137
  // for the loading animation image in thickbox.
138
  drupal_add_js(array(
139
  'cdm_dataportal' => array(
140
  'cdm_dataportal_path' => base_path() . drupal_get_path('module', 'cdm_dataportal'),
141
  )
142
  ),
143
  'setting'
144
      );
145
      drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/thickbox/thickbox.js');
146
      drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/js/thickbox/cdm_thickbox.css');
147
}
148

    
149
/**
150
 * @todo Please document this function.
151
 * @see http://drupal.org/node/1354
152
 */
153
function _add_js_lightbox($galleryID) {
154
  /*
155
   * Important Notice: The jquery.lightbox-0.5.js has been modified in order to
156
  * allow using the "alt" attribute for captions instead of the "title"
157
  * attribute
158
  */
159
  $lightbox_base_path =  drupal_get_path('module', 'cdm_dataportal') . '/js/jquery-lightbox-0.5';
160
  $lightbox_image_path = base_path() . $lightbox_base_path . '/images/';
161
  drupal_add_js($lightbox_base_path . '/js/jquery.lightbox-0.5.js');
162
  drupal_add_css($lightbox_base_path . '/css/jquery.lightbox-0.5.css');
163
  drupal_add_js('jQuery(document).ready(function() {
164
      jQuery(\'#' . $galleryID . ' a.lightbox\').lightBox({
165
        fixedNavigation:  true,
166
        imageLoading:     \'' . $lightbox_image_path . 'lightbox-ico-loading.gif\',
167
        imageBtnPrev:     \'' . $lightbox_image_path . 'lightbox-btn-prev.gif\',
168
        imageBtnNext:     \'' . $lightbox_image_path . 'lightbox-btn-next.gif\',
169
        imageBtnClose:    \'' . $lightbox_image_path . 'lightbox-btn-close.gif\',
170
        imageBlank:       \'' . $lightbox_image_path . 'lightbox-blank.gif\',
171
        adjustToWindow: true
172
      });
173
    });
174
    ', array('type' => 'inline'));
175
}
176

    
177
/**
178
 * @todo Please document this function.
179
 * @see http://drupal.org/node/1354
180
 */
181
function _add_js_footnotes() {
182
  _add_js_domEvent();
183
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/footnotes.js');
184
}
185

    
186
/**
187
 * @todo Please document this function.
188
 * @see http://drupal.org/node/1354
189
 */
190
function _add_js_cluetip() {
191

    
192
  // TODO replace by
193
  // @see http://www.socialembedded.com/labs/jQuery-Tooltip-Plugin/jQuery-Tooltip-Plugin.html
194
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.cluetip.js');
195
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/jquery.dimensions.js');
196
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.hoverIntent.js');
197
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/js/cluetip/jquery.cluetip.css');
198
  drupal_add_js("jQuery(document).ready(function(){
199
      jQuery('.cluetip').css({color: '#0062C2'}).cluetip({
200
        splitTitle: '|',
201
        showTitle: true,
202
        activation: 'hover',
203
        sicky: true,
204
        arrows: true,
205
        dropShadow: false,
206
        cluetipClass: 'rounded'
207
      });
208
    });", array('type' => 'inline'));
209
}
210

    
211
/**
212
 * @todo Please document this function.
213
 * @see http://drupal.org/node/1354
214
 */
215
function _add_js_ahah() {
216

    
217
  _add_js_domEvent(); // requires domEvent.js
218
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/ahah-content.js');
219
}
220

    
221
/**
222
 * Adds the external javascript file for domEvent.js.
223
 *
224
 * @see drupal_add_js()
225
 */
226
function _add_js_domEvent() {
227
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/domEvent.js');
228
}
229

    
230

    
231
/* ====================== hook implementations ====================== */
232
/**
233
 * Implements hook_permission().
 *
234
 * Valid permissions for this module.
235
 *
236
 * @return array
237
 *   An array of valid permissions for the portfolio module.
238
 */
239
function cdm_dataportal_permission() {
240
  return array(
241
    'administer cdm_dataportal' => array(
242
      'title' => t('administer cdm_dataportal'),
243
      'description' => t("TODO Add a description for 'administer cdm_dataportal'"),
244
    ),
245
    'cdm_dataportal view notes' => array(
246
      'title' => t('cdm_dataportal view notes'),
247
      'description' => t("TODO Add a description for 'cdm_dataportal view notes'"),
248
    ),
249
    // TODO Which other permissions are required?
250
    // -> check the WP6 requirements document.
251
  );
252
}
253

    
254
/**
255
 * Implements hook_menu().
 */
256
function cdm_dataportal_menu() {
257
  $items = array();
258

    
259
  // @see settings.php.
260
  cdm_dataportal_menu_admin($items);
261
  cdm_dataportal_menu_help($items);
262

    
263
  $items['cdm_dataportal/names'] = array(
264
    'page callback' => 'cdm_dataportal_view_names',
265
    'access arguments' => array('access content'),
266
    'type' => MENU_CALLBACK,
267
  );
268

    
269
  // Optional callback arguments: page.
270
  $items['cdm_dataportal/taxon'] = array(
271
    'page callback' => 'cdm_dataportal_taxon_page_view',
272
    'access arguments' => array('access content'),
273
    'type' => MENU_CALLBACK,
274
    // Expected callback arguments: uuid.
275
  );
276

    
277
  $items['cdm_dataportal/name'] = array(
278
    'page callback' => 'cdm_dataportal_name_page_view',
279
      /*
280
    'page arguments' => array(
281
       'taxon_name_uuid',
282
       'taxon_to_hide_uuid',
283
       'synonym_uuid' => NULL
284
      ),
285
      */
286
    'access arguments' => array('access content'),
287
    'type' => MENU_CALLBACK,
288
    // Expected callback arguments: uuid.
289
  );
290

    
291
  $items['cdm_dataportal/reference'] = array(
292
    'page callback' => 'cdm_dataportal_view_reference',
293
    'access arguments' => array('access content'),
294
    'type' => MENU_CALLBACK,
295
    // Expected callback arguments: uuid.
296
  );
297

    
298
  $items['cdm_dataportal/reference/list'] = array(
299
    'page callback' => 'cdm_dataportal_view_reference_list',
300
    'access arguments' => array('access content'),
301
    'type' => MENU_CALLBACK,
302
    // Expected callback arguments: uuid.
303
  );
304

    
305
  $items['cdm_dataportal/media'] = array(
306
    'page callback' => 'cdm_dataportal_view_media',
307
    'access arguments' => array('access content'),
308
    'type' => MENU_CALLBACK,
309
    // Expected callback arguments:
310
    // uuid, mediarepresentation_uuid, part_uuid or part#.
311
  );
312

    
313
  $items['cdm_dataportal/polytomousKey'] = array(
314
    'page callback' => 'cdm_dataportal_view_polytomousKey',
315
    'access arguments' => array('access content'),
316
    'type' => MENU_CALLBACK,
317
    // Expected callback arguments: polytomousKey->uuid.
318
  );
319

    
320
  $items['cdm_dataportal/search'] = array(
321
    'page callback' => 'cdm_dataportal_view_search_advanced',
322
    'access arguments' => array('access content'),
323
    'type' => MENU_CALLBACK,
324
  );
325

    
326
  $items['cdm_dataportal/search/advanced'] = array(
327
    'title' => 'Advanced',
328
    'page callback' => 'cdm_dataportal_view_search_advanced',
329
    'access arguments' => array('access content'),
330
    'type' => MENU_DEFAULT_LOCAL_TASK,
331
  );
332

    
333
  $items['cdm_dataportal/search/taxon_by_description'] = array(
334
    'title' => 'By description full text',
335
    'page callback' => 'cdm_dataportal_view_search_taxon_by_description',
336
    'access arguments' => array('access content'),
337
    'type' => MENU_LOCAL_TASK,
338
  );
339

    
340
  $items['cdm_dataportal/search/results/taxon'] = array(
341
    'page callback' => 'cdm_dataportal_view_search_results_taxon',
342
    'access arguments' => array('access content'),
343
    'type' => MENU_CALLBACK,
344
  );
345
  /*
346
   $items['cdm/xml2json'] = array(
347
   'page callback' => 'cdm_view_xml2json',
348
   'access arguments' => array('access content'),
349
   'type' => MENU_CALLBACK,
350
   );
351
   */
352

    
353
  // if (arg(0)=='user' && ($uid=arg(1)) && is_numeric($uid)) {
354
  // User configuration of cdm_dataportal.
355
  $items['user/%/cdm_dataportal'] = array(
356
    'title' => 'cdm_dataportal',
357
    'access arguments' => array('access content'),
358
    'page callback' => 'drupal_get_form',
359
    'page arguments' => array('cdm_dataportal_user_form'),
360
    'type' => MENU_LOCAL_TASK,
361
    'weight' => 10,
362
  );
363
  // }
364

    
365
  // 'May not cache' in D5.
366
  // --- Local tasks for Taxon.
367
  $items['cdm_dataportal/name/%'] = array(
368
    // 'page callback' => 'cdm_dataportal_view_name',
369
    'page callback' => 'cdm_dataportal_name_page_view',
370
    'page arguments' => array(2, 3, 4),
371
    'access arguments' => array('access content'),
372
    'type' => MENU_CALLBACK,
373
  );
374

    
375
  if (variable_get('cdm_dataportal_taxonpage_tabs', 1)) {
376
    $items['cdm_dataportal/taxon/%'] = array(
377
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'General')),
378
      'page callback' => 'cdm_dataportal_taxon_page_view',
379
      'access arguments' => array('access content'),
380
      'type' => MENU_CALLBACK,
381
      'weight' => 1,
382
      'page arguments' => array(2, "description")
383
      , // Expected callback arguments: name_uuid.
384
    );
385

    
386
    $items['cdm_dataportal/taxon/%/all'] = array(
387
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'General')),
388
      'page callback' => 'cdm_dataportal_taxon_page_view',
389
      'access arguments' => array('access content'),
390
      'type' => MENU_CALLBACK,
391
      'weight' => 2,
392
      'page arguments' => array(2, "all")
393
      , // Expected callback arguments: name_uuid.
394
    );
395

    
396
    $items['cdm_dataportal/taxon/%/description'] = array(
397
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'General')),
398
      'page callback' => 'cdm_dataportal_taxon_page_view',
399
      'access arguments' => array('access content'),
400
      'type' => MENU_DEFAULT_LOCAL_TASK,
401
      'weight' => 2,
402
      'page arguments' => array(2, "description")
403
      , // Expected callback arguments: name_uuid.
404
    );
405

    
406
    $items['cdm_dataportal/taxon/%/synonymy'] = array(
407
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Synonymy')),
408
      'page callback' => 'cdm_dataportal_taxon_page_view',
409
      'access arguments' => array('access content'),
410
      'type' => MENU_LOCAL_TASK,
411
      'weight' => 4,
412
      'page arguments' => array(2, "synonymy", 4)
413
      , // Expected callback arguments: name_uuid.
414
    );
415
    $items['cdm_dataportal/taxon/%/images'] = array(
416
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Images')),
417
      'page callback' => 'cdm_dataportal_taxon_page_view',
418
      'access arguments' => array('access content'),
419
      'type' => MENU_LOCAL_TASK,
420
      'weight' => 5,
421
      'page arguments' => array(2, "images")
422
      , // Expected callback arguments: name_uuid.
423
    );
424

    
425
    $items['cdm_dataportal/taxon/%/specimens'] = array(
426
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Specimens')),
427
      'page callback' => 'cdm_dataportal_taxon_page_view',
428
      'access arguments' => array('access content'),
429
      'type' => MENU_LOCAL_TASK,
430
      'weight' => 6,
431
      'page arguments' => array(2, "specimens")
432
      , // Expected callback arguments: name_uuid.
433
    );
434

    
435
    $items['cdm_dataportal/taxon/%/keys'] = array(
436
      'title' => theme('cdm_taxonpage_tab', array('tabname' => 'Keys')),
437
      'page callback' => 'cdm_dataportal_taxon_page_view',
438
      'access arguments' => array('access content'),
439
      'type' => MENU_LOCAL_TASK,
440
      'weight' => 6,
441
      'page arguments' => array(2, "keys")
442
      , // Expected callback arguments: name_uuid.
443
    );
444
  }
445

    
446
  return $items;
447
}
448

    
449
/**
450
 * Implements hook_init().
451
 *
452
 * can be removed after fixing the FIXME below
453
 */
454
function cdm_dataportal_init() {
455
  //FIXME To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.
456
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal.css');
457
  // drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
458
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal') . '/cdm_dataportal_screen.css', array('type' => 'screen'));
459
}
460

    
461
/**
462
 * The function generate form for own user cdm dataportal configurations.
463
 */
464
function cdm_dataportal_user_form($form, &$form_state) {
465
  global $user;
466
  $checkbox_value = 'cdm_dataportal_' . $user->uid . '_default_tab_active';
467

    
468
  $form['check'] = array(
469
    '#type' => 'checkbox',
470
    '#title' => t('Activate user default configuration'),
471
    '#default_value' => variable_get($checkbox_value, 0),
472
    '#description' => t('Check this if you want configure your own default tab from the below menu.'),
473
  );
474

    
475
  $form['cdm_dataportal_user_form'] = array(
476
    '#type' => 'select',
477
    '#title' => t('Default tab to display'),
478
    '#default_value' => get_default_taxon_tab(TRUE),
479
    '#options' => unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB),
480
    '#description' => t('<p>Select the default tab to display when visiting a taxon page. Only available if Tabbed Taxon Page is enable.</p>
481
              <strong>Note:</strong> After performing a search and clicking in any synonym, the taxon tab
482
              to be renderized will be the synonymy of the accepted taxon and not the above selected tab.'),
483
  );
484

    
485
  $form['submit'] = array(
486
    '#type' => 'submit',
487
    '#value' => t('Submit'),
488
  );
489

    
490
  return $form;
491
}
492

    
493
/**
494
 * Form submission handler for user_form().
495
 *
496
 * Submits the user cdm dataportal configurations.
497
 */
498
function cdm_dataportal_user_form_submit($form, &$form_state) {
499
  global $user;
500
  $msg_type = 'status';
501
  $username = $user->name;
502
  $variable_to_use = 'cdm_dataportal_' . $user->uid . '_default_tab';
503

    
504
  // If is the right user the variables are setted.
505
  if (arg(0) == 'user' && ($uid = arg(1)) && is_numeric($uid) && $user->uid == $uid) {
506
    $variable = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
507
    variable_set($variable_to_use . '_active', $form_state['values']['check']);
508
    variable_set($variable_to_use, $form_state['values']['cdm_dataportal_user_form']);
509
    if ($form_state['values']['check']) {
510
      drupal_set_message(check_plain(t('The user default tab will be used for the next taxon site visit.')));
511
      drupal_set_message(check_plain(t('The user default tab has been changed to: !tab for the user !user', array(
512
        '!tab' => $variable[variable_get($variable_to_use, 0)],
513
        '!user' => $username,
514
      ))), $msg_type);
515
    }
516
    else {
517
      drupal_set_message(check_plain(t('The user default tab wont be used for
518
        the next taxon site, check the box if you want to use the user default configuration.')));
519
    }
520
    // Problem with the user id => variables wont be saved.
521
  }
522
  else {
523
    $msg_type = 'warning';
524
    drupal_set_message(check_plain(t('Default tab has not been saved due to user id problems')), $msg_type);
525
  }
526
}
527

    
528
/**
529
 * Implements hook_block_info().
530
 */
531
function cdm_dataportal_block_info() {
532

    
533
    // $block[0]["info"] = t("CDM DataPortal DevLinks");
534
    // $block[1]["info"] = t("CDM DataPortal Credits");
535
    $block["2"] = array(
536
        "info" => t("CDM Search Taxa"),
537
        "cache" => DRUPAL_NO_CACHE
538
      );
539
    // $block[3]["info"] = t("CDM Filters");
540
    $block["4"]["info"] = t("CDM Dataportal Print");
541
    $block["keys"]["info"] = t("CDM identification keys");
542
    $block["uses"]["info"] = t("CDM Uses");
543
    $block["fundedByEDIT"]["info"] = t('Funded by EDIT');
544

    
545
    return $block;
546
}
547

    
548
/**
549
 * Implements hook_block_view().
550
 */
551
function cdm_dataportal_block_view($delta) {
552
  // TODO Rename block deltas (e.g. '2') to readable strings.
553
  switch ($delta) {
554
    // case 'delta-1':
555
    // $block['subject'] = t('Credits');
556
    // $block['content'] = theme('cdm_credits');
557
    // return $block;
558
    case '2':
559
      $block['subject'] = t('Search taxa');
560
      $form = drupal_get_form('cdm_dataportal_search_taxon_form');
561
      $block['content'] = drupal_render($form);
562

    
563
      if (variable_get('cdm_dataportal_show_advanced_search', 1)) {
564
        $block['content'] .= '<div>' . l(t('Advanced Search'), 'cdm_dataportal/search') . '</div>';
565
      }
566
      return $block;
567
    case '4':
568
      $block['subject'] = '';
569
      $block['content'] = theme('cdm_print_button');
570
      return $block;
571
    case "keys":
572
      $block['subject'] = t('Identification Keys');
573
      $block['content'] = theme('cdm_block_IdentificationKeys', array('taxonUuid' => NULL));
574
      return $block;
575
      // Creating a block specific for the Use Interface.
576
    case "uses":
577
      $block['subject'] = t('Uses');
578
      $block['content'] = theme('cdm_block_uses', array('taxonUuid' => NULL));
579
      return $block;
580
    case "fundedByEDIT":
581
      // t('Funded by EDIT');
582
      $text = '<none>';
583
      $block['subject'] = $text;
584
      $img_tag = '<img src="' . base_path() . drupal_get_path('module', 'cdm_dataportal') . '/images/funded_by_EDIT.png' . '" alt="' . $text . '"/>';
585
      $block['content'] = l($img_tag, "http://www.e-taxonomy.eu/", array(
586
        'attributes' => array("target" => "EDIT"),
587
        'absolute' => TRUE,
588
        'html' => TRUE,
589
      ));
590
      return $block;
591
  }
592
}
593

    
594
/*
595
 function cdm_dataportal_session_clear($cdm_ws_uri_update = FALSE){
596
 $_SESSION['cdm'] = NULL;
597
 if(is_string($cdm_ws_uri_update)){
598
 $_SESSION['cdm'] = array('ws_uri'=>$cdm_ws_uri_update);
599
 }
600
 }
601

    
602
 function cdm_dataportal_session_validate(){
603
 if(!isset($_SESSION['cdm']['ws_uri'])){
604
 $_SESSION['cdm'] = array('ws_uri'=>variable_get('cdm_webservice_url', FALSE));
605
 } else if($_SESSION['cdm']['ws_uri'] != variable_get('cdm_webservice_url', FALSE)){
606
 cdm_dataportal_session_clear(variable_get('cdm_webservice_url', FALSE));
607
 }
608
 }
609
 */
610

    
611
/**
612
 * @todo Please document this function.
613
 * @see http://drupal.org/node/1354
614
 */
615
function cdm_taxonomictree_selector() {
616
  _add_js_treeselector();
617

    
618
  $form = drupal_get_form('cdm_taxonomictree_selector_form');
619
  $out = drupal_render($form);
620
  return $out;
621
}
622

    
623
/**
624
 * @todo Please document this function.
625
 * @see http://drupal.org/node/1354
626
 */
627
function cdm_taxonomictree_selector_form($form, &$form_state) {
628

    
629
  $url = url('cdm_api/setvalue/session', array('query' => NULL));
630
  $form['#action'] = $url;
631

    
632
  $form['var'] = array(
633
    '#weight' => -3,
634
    '#type' => 'hidden',
635
    '#value' => '[cdm][taxonomictree_uuid]',
636
  );
637

    
638
  $destination_array = drupal_get_destination();
639
  $destination = $destination_array['destination'];
640

    
641
  $form['destination'] = array(
642
    '#weight' => -3,
643
    '#type' => 'hidden',
644
    '#value' =>  $destination,
645
  );
646

    
647
  $form['val'] = array(
648
    '#type' => 'select',
649
    '#title' => t('Available classifications'),
650
    '#default_value' => get_taxonomictree_uuid_selected(),
651
    '#options' => cdm_get_taxontrees_as_options(),
652
  );
653

    
654
  return $form;
655

    
656
}
657

    
658
/* UNREACHABLE since action of form directly links to view.
659
 function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
660

    
661
 $_SESSION['cdm']['search'] = $form_values;
662
 //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];
663
 return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');
664
 //$paramstr = compose_url_prameterstr($form_values);
665
 //return url('/cdm_dataportal/search/taxon/', array('query' => $paramstr));
666
 }
667
 */
668
/* ====================== menu callback functions ====================== */
669
/**
670
 * @todo Please document this function.
671
 * @see http://drupal.org/node/1354
672
 */
673
/*
674
function cdm_dataportal_form_alter(&$form, &$form_state, $form_id) {
675
  static $comment_node_disabled =  0;
676
  static $comment_node_read_only =  1;
677
  static $comment_node_read_write =  2;
678

    
679
  if ($form_id == 'node_type_form'
680
   && isset($form['identity']['type'])
681
   && array_key_exists($form['#node_type']->type, cdm_get_nodetypes())
682
  ) {
683
    $form['workflow']['comment'] = array(
684
      '#type' => 'radios',
685
      '#title' => t('Default comment setting'),
686
      '#default_value' => variable_get('comment__' . $node->type . $form['#node_type']->type, $comment_node_disabled),
687
      '#options' => array(t('Disabled'), t('Read only'), t('Read/Write')),
688
      '#description' => t('Users with the <em>administer comments</em> permission will be able to override this setting.'),
689
    );
690
  }
691
}
692
*/
693

    
694
/**
695
 * Displays a list of the known taxonomic names.
696
 *
697
 * When the list of taxonomic names is displayed, long lists are split up into
698
 * multiple pages.
699
 *
700
 * TODO: Parameters are still preliminary.
701
 *
702
 * @param string $beginsWith
703
 * @param string $page
704
 *   Page number to diplay defaults to page 1.
705
 * @param bool $onlyAccepted
706
 */
707
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = FALSE) {
708

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

    
711
  /*
712
  // FIXME the filter for accepted names will be a form element, thus this
713
  // widget should be generated via form api preferably as block.
714
  $out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
715
  $out .= theme('cdm_dataportal_widget_names_list', $names, $page);
716
  $out .= theme('cdm_listof_taxa', $taxonPager);
717
  return $out;
718
  */
719
}
720

    
721
/**
722
 * @todo Please document this function.
723
 * @see http://drupal.org/node/1354
724
 */
725
function cdm_dataportal_view_reference($uuid, $arg2 = NULL) {
726
  $reference = cdm_ws_get(CDM_WS_REFERENCE, $uuid);
727
  return theme('cdm_reference_page', array('reference' => $reference));
728
}
729

    
730
/**
731
 * Created a view on a all references contained in the portal.
732
 *
733
 * This function is used at the path cdm_dataportal/reference/list
734
 */
735
function cdm_dataportal_view_reference_list($pageNumber) {
736
  $referencePager = cdm_ws_page(CDM_WS_REFERENCE, variable_get('cdm_dataportal_search_items_on_page', CDM_DATAPORTAL_SEARCH_ITEMS_ON_PAGE), $pageNumber);
737
  return theme('cdm_reference_pager', array(
738
    'referencePager' => $referencePager,
739
    'path' => 'cdm_dataportal/reference/list/',
740
    ));
741
}
742

    
743
/**
744
 * @todo Please document this function.
745
 * @see http://drupal.org/node/1354
746
 */
747
function cdm_dataportal_view_media($mediaUuid, $mediarepresentation_uuid = FALSE, $part = 0) {
748
  $media = cdm_ws_get(CDM_WS_PORTAL_MEDIA, $mediaUuid);
749
  return theme('cdm_media_page', array(
750
    'media' => $media,
751
    'mediarepresentation_uuid' => $mediarepresentation_uuid,
752
    'partId' => $part,
753
    ));
754
}
755

    
756
/**
757
 * @todo Please document this function.
758
 * @see http://drupal.org/node/1354
759
 */
760
function _load_taxonBase(&$taxonBase) {
761
  if (isset($taxonBase->uuid)) {
762
    $taxonBase->name = cdm_ws_get(CDM_WS_TAXON, array($taxonBase->uuid, "name"));
763
    $taxonBase->name->taggedName = cdm_ws_get(CDM_WS_NAME, array($taxonBase->name->uuid, "taggedName"));
764
    $taxonBase->name->nomenclaturalReference = cdm_ws_get(CDM_WS_NAME, array($taxonBase->name->uuid, "nomenclaturalReference"));
765
  }
766
}
767

    
768
/**
769
 * Loads the media associated to the given taxon from the cdm server.
770
 * The aggregation settings regarding taxon relathionships and
771
 * taxonnomic childen are taken into account.
772
 *
773
 * The media lists are cached in a static variable.
774
 *
775
 * @param Taxon $taxon
776
 *   A CDM Taxon entitiy
777
 *
778
 * @return array
779
 *   An array of CDM Media entities
780
 *
781
 */
782
function _load_media_for_taxon($taxon) {
783

    
784
  static $media = NULL;
785

    
786
  if(!isset($media)) {
787
    $media = array();
788
  }
789
  if (!isset($media[$taxon->uuid])) {
790

    
791
    // --- GET Images --- //
792
    $mediaQueryParameters = array(
793
        "type" => "ImageFile",
794
    );
795

    
796
    $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
797
    $mediaQueryParameters['relationships'] = implode(',', get_selection($relationship_choice['direct']));
798
    $mediaQueryParameters['relationshipsInvers'] = implode(',', get_selection($relationship_choice['invers']));
799

    
800
    $taxon_media_filter_choice = variable_get(CDM_TAXON_MEDIA_FILTER, unserialize(CDM_TAXON_MEDIA_FILTER_DEFAULT));
801
    $mediaQueryParameters['includeTaxonDescriptions'] = (boolean) $taxon_media_filter_choice['includeTaxonDescriptions'] != 0;
802
    $mediaQueryParameters['includeOccurrences'] = (boolean) $taxon_media_filter_choice['includeOccurrences'] != 0;
803
    $mediaQueryParameters['includeTaxonNameDescriptions'] = (boolean) $taxon_media_filter_choice['includeTaxonNameDescriptions'] != 0;
804

    
805
    $ws_endpoint = NULL;
806
    if ( variable_get('cdm_images_include_children', 0) == 0) {
807
      $ws_endpoint = CDM_WS_PORTAL_TAXON_MEDIA;
808
    } else {
809
      $ws_endpoint = CDM_WS_PORTAL_TAXON_SUBTREE_MEDIA;
810
    }
811

    
812
    $media[$taxon->uuid] = cdm_ws_get($ws_endpoint,
813
        array(
814
            $taxon->uuid,
815
        ),
816
        queryString($mediaQueryParameters)
817
       );
818
  }
819

    
820
  return $media[$taxon->uuid];
821
}
822

    
823
/**
824
 *
825
 * @param Taxon $taxon
826
 *   A CDM Taxon entitiy
827
 *
828
 * @return array
829
 *   An array of CDM SpecimenOrObservation entities
830
 *
831
function _load_occurences_for_taxon($taxon){
832

    
833
  static $occurences = NULL;
834

    
835
  if(!isset($occurences)) {
836
    $occurences = array();
837
  }
838

    
839
  if (!isset($occurences[$taxon->uuid])){
840

    
841
    $relationship_choice = variable_get(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS, unserialize(CDM_AGGREGATE_BY_TAXON_RELATIONSHIPS_DEFAULT));
842
    $relationship_choice['direct'] = get_selection($relationship_choice['direct']);
843
    $relationship_choice['invers'] = get_selection($relationship_choice['invers']);
844

    
845
    $by_associatedtaxon_query = http_build_query(array(
846
        'relationshipsInvers' => implode(',', $relationship_choice['invers']),
847
        'relationships' => implode(',', $relationship_choice['direct']),
848
        'pageSize' => null // all hits in one page
849
    )
850
    );
851

    
852
    $pager = cdm_ws_get(CDM_WS_OCCURRENCE_BY_ASSOCIATEDTAXON,
853
        null,
854
        $by_associatedtaxon_query . '&taxonUuid=' . $taxon->uuid
855
    );
856

    
857

    
858
    if(isset($pager->records[0])){
859
      $occurences[$taxon->uuid] =  $pager->records;
860
    }
861
  }
862
  return $occurences[$taxon->uuid];
863
}
864
 */
865

    
866
/**
867
 * Gets a Drupal variable, string or array and returns it.
868
 *
869
 * Similar to the variable_get() function of Drupal, except that this function
870
 * is able to handle arrays correctly. This function is especially useful
871
 * when dealing with collections of setting form elements (#tree = TRUE).
872
 *
873
 * @param string $variableKey
874
 *   The Unique key of the Drupal variable in the Drupal variables table.
875
 * @param string $defaultValueString
876
 *   A string as for example derived from a CONSTANT.
877
 *
878
 * @return mixed
879
 *   Returns usally an array, depending on the nature of the variable.
880
 */
881
function mixed_variable_get($variableKey, $defaultValueString) {
882
  $systemDefaults = unserialize($defaultValueString);
883
  $storedSettings = variable_get($variableKey, array());
884
  if (is_array($storedSettings)) {
885
    $settings = array_merge($systemDefaults, $storedSettings);
886
  }
887
  else {
888
    $settings = $systemDefaults;
889
  }
890
  return $settings;
891
}
892

    
893
/**
894
 * Recusive funciton to convert an object into an array.
895
 * also subordinate objects will be converted.
896
 *
897
 * @param object $object
898
 * @return the array
899
 */
900
function convert_to_array($object) {
901
  if(is_object($object) || is_array($object)) {
902
    $array = (array)$object;
903
    foreach ($array as $key=>$value){
904
      $array[$key] = convert_to_array($value);
905
    }
906
    return $array;
907
  } else {
908
    return $object;
909
  }
910
}
911

    
912
/**
913
 * Wrapps the given $html string into a reder array suitable for drupal_render()
914
 *
915
 * @param string $html
916
 *   the html string, see
917
 *   http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#markup
918
 * @param number $weight
919
 *   A positive or negative number (integer or decimal).
920
 *   see http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#weightval
921
 *
922
 * @return
923
 *   A render array
924
 *
925
 */
926
function markup_to_render_array($html, $weight = 0) {
927
  return array(
928
          '#markup' => $html,
929
          '#weight' => $weight,
930
      );
931
}
932

    
933
/**
934
 * Loads the subgraph of a given PolytomousKeyNode.
935
 *
936
 * Loads the subgraph of the given PolytomousKeyNode recursively from
937
 * the CDM REST service.
938
 *
939
 * @param mixed $polytomousKeyNode
940
 *   PolytomousKeyNode passed by reference.
941
 *
942
 * @return void
943
 */
944
function _load_polytomousKeySubGraph(&$polytomousKeyNode) {
945

    
946
  if (!$polytomousKeyNode) {
947
    return;
948
  }
949
  if ($polytomousKeyNode->class != "PolytomousKeyNode") {
950
    drupal_set_message(_load_polytomousKeySubGraph() . t('invalid type given.'), 'error');
951
    return;
952
  }
953
  if (!is_uuid($polytomousKeyNode->uuid)) {
954
    drupal_set_message(_load_polytomousKeySubGraph() . t('invalid type given.'), 'error');
955
    return;
956
  }
957

    
958
  $polytomousKeyNode = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, $polytomousKeyNode->uuid);
959

    
960
  if (!$polytomousKeyNode) {
961
    // drupal_set_message("_load_polytomousKeyChildNodes() : could not load polytomousKeyNode", "error");
962
    return;
963
  }
964

    
965
  // Load children.
966
  foreach ($polytomousKeyNode->children as &$childNode) {
967
    _load_polytomousKeySubGraph($childNode);
968
  }
969

    
970
  // Load subkey.
971
  $polytomousKeyNode->subkey = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, array($polytomousKeyNode->uuid, "subkey"));
972

    
973
  // Load taxon.
974
  $polytomousKeyNode->taxon = cdm_ws_get(CDM_WS_POLYTOMOUSKEY_NODE, array($polytomousKeyNode->uuid, "taxon"));
975
  _load_taxonBase($polytomousKeyNode->taxon);
976
  return;
977
}
978

    
979
/**
980
 * @todo Please document this function.
981
 * @see http://drupal.org/node/1354
982
 */
983
function cdm_dataportal_view_polytomousKey($polytomousKeyUuid) {
984
  $polytomousKey = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, $polytomousKeyUuid);
985

    
986
  $sourcePager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'sources'));
987
  if (is_array($sourcePager->records)) {
988
    $polytomousKey->sources = $sourcePager->records;
989
    // $polytomousKey->sources->citation = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'sources'));
990
  }
991

    
992
  $annotationPager = cdm_ws_get(CDM_WS_POLYTOMOUSKEY, array($polytomousKeyUuid, 'annotations'));
993
  if (is_array($annotationPager->records)) {
994
    $polytomousKey->annotations = $annotationPager->records;
995
  }
996

    
997
  _load_polytomousKeySubGraph($polytomousKey->root);
998
  return theme('cdm_polytomousKey_page', array('polytomousKey' => $polytomousKey));
999
}
1000

    
1001
/**
1002
 * Creates a taxon page view or a chapter of it.
1003
 *
1004
 * The taxon page gives detailed information on a taxon, it shows:
1005
 *  - Taxon name.
1006
 *  - Full list of synonyms homotypic synonyms on top, followed by the
1007
 *    heterotypic and finally followed by misapplied names.
1008
 *    The list is ordered historically.
1009
 *  - All description associated with the taxon.
1010
 *
1011
 * @param string $uuid
1012
 * @param string $chapter
1013
 *   Name of the part to display, valid values are:
1014
 *   'description', 'images', 'synonymy', 'specimens', 'all'.
1015
 *
1016
 * @return unknown_type
1017
 */
1018
function cdm_dataportal_taxon_page_view($uuid, $chapter = 'all', $synonym_uuid = NULL) {
1019
  // Display the page for the taxon defined by $uuid.
1020
  // set_last_taxon_page_tab(arg(3));
1021
  $taxonpage = cdm_dataportal_taxon_view($uuid, $chapter, $synonym_uuid);
1022
  if (!empty($taxonpage)) {
1023
    return cdm_node_show(NODETYPE_TAXON, $uuid, $taxonpage->title, $taxonpage->content);
1024
  }
1025
  else {
1026
    return '';
1027
  }
1028
}
1029

    
1030
/**
1031
 * This function will genreate the taxon page part ($chapter) and returns a taxonpage object
1032
 * which has two fields, one for the page title and one for the content. Later on in the
1033
 * process chain the value contained in these fields will be passed to the cdm_node_show()
1034
 * function as the function parameters $title and $content.
1035
 *
1036
 * @param string $uuid
1037
 *   the uuid of the taxon to show
1038
 * @param string $chapter
1039
 *   Name of the part to display, valid values are:
1040
 *   'description', 'images', 'synonymy', 'all'.
1041
 *
1042
 * @return taxonpage object with the following fields:
1043
 *   - title : the title of the page
1044
 *   - content: the content of the page
1045
 *
1046
 */
1047
function cdm_dataportal_taxon_view($uuid, $chapter = 'all') {
1048
  // Taxon object.
1049
  $taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON, $uuid);
1050
  if (empty($taxon)) {
1051
    drupal_set_title(t('Taxon does not exist'), PASS_THROUGH);
1052
    return FALSE;
1053
  }
1054
  $taxonpage = new stdClass();
1055

    
1056
  $taxonpage->title = theme('cdm_taxon_page_title', array(
1057
    'taxon' => $taxon
1058
  ));
1059

    
1060
  // Check if the taxon id contained in the currently selected tree.
1061
  $taxon_in_current_tree = taxon_in_current_tree($uuid);
1062

    
1063
  if (!$taxon_in_current_tree) {
1064
    $classifications = get_classifications_for_taxon($taxon);
1065
    if (count($classifications) == 0) {
1066
      drupal_set_message(check_plain(t('This concept of the taxon !taxonname is not contained as an accepted taxon in currently chosen classification.', array(
1067
        '!taxonname' => theme('cdm_taxonName', array('taxonName' => $taxon->name)),
1068
      ))), 'warning');
1069
    }
1070
    else {
1071
      $trees = '';
1072
      foreach ($classifications as $classification) {
1073
        if (isset($classification->titleCache)) {
1074
          $trees .= ($trees ? ', ' : '') . '<strong>' . $classification->titleCache . '</strong>';
1075
        }
1076
      }
1077

    
1078
      drupal_set_message(format_plural(count($trees),
1079
          'This concept of the taxon !taxonname is not contained as an accepted taxon in currently chosen classification, but in this one: !trees',
1080
          'This concept of the taxon !taxonname is not contained as an accepted taxon in currently chosen classification, but in one of these: !trees',
1081
          array('!taxonname' => theme('cdm_taxonName', array('taxonName' => $taxon->name)), '!trees' => $trees)) , 'warning');
1082
    }
1083
  }
1084

    
1085
  // Render the taxon page.
1086
  $taxonpage->content = theme('cdm_taxon_page', array(
1087
    'taxon' => $taxon,
1088
    'page_part' => $chapter,
1089
  ));
1090

    
1091
  return $taxonpage;
1092
}
1093

    
1094
/**
1095
 * Returns a name page as a Drupal node ready to be renderized by Drupal.
1096
 *
1097
 * The node page shows the taxon name title and the list of taxon related
1098
 * with such taxon. Name on the tree already in use.
1099
 *
1100
 * @param string $taxon_name_uuid
1101
 *   A taxon name UUID.
1102
 *
1103
 * @return mixed
1104
 *   The formatted name page as node.
1105
 */
1106
function cdm_dataportal_name_page_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid = NULL) {
1107

    
1108
  $taxonname_page = cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid);
1109
  if (!empty($taxonname_page)) {
1110
    return cdm_node_show(NODETYPE_NAME, $taxon_name_uuid, $taxonname_page->title, $taxonname_page->content);
1111
  }
1112
  else {
1113
    return '';
1114
  }
1115
}
1116

    
1117
/**
1118
 * View function for a TaxonNameBase page.
1119
 *
1120
 * The name page lists all taxa for which the name specified by the
1121
 * $taxon_name_uuid is being used. I case there is only one name the
1122
 * page automatically redirects ti the according taxon page. Otherwise
1123
 * the list of names is displayed.
1124
 *
1125
 * The parameter $taxon_to_hide_uuid allows to exclude a taxon from the
1126
 * list of taxa. This is useful for example when referencing from a taxon
1127
 * to the name page and the referring taxon should not be repeaded in the
1128
 * name page.
1129
 *
1130
 *
1131
 * @param UUID $taxon_name_uuid
1132
 *   The uuid of the CDM TaxonNameBase to show a name page for
1133
 * @param UUID $taxon_to_hide_uuid
1134
 *   A taxon which should not be displayed in the taxon list
1135
 * @param UUID $highlite_synonym_uuid
1136
 *   Optinal parameter wich takes another taxon uuid, if given the
1137
 *   target taxon pages will show the syonymy tab where the taxon
1138
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1139
 *   in case it is found on this page.
1140
 *
1141
 * @return object
1142
 *   An object with two fields:
1143
 *     - title: the page title
1144
 *     - content: the page content
1145
 */
1146
function cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $highlite_synonym_uuid = NULL) {
1147
  // Getting the full taxonname object from the server.
1148
  $taxon_name = cdm_ws_get(CDM_WS_NAME, array($taxon_name_uuid));
1149
  if (!$taxon_name) {
1150
    drupal_set_title(t('Taxon name does not exist'), PASS_THROUGH);
1151
    return FALSE;
1152
  }
1153
  // Searching for all the taxa connected with the taxon name on the tree
1154
  // in use.
1155
  $name_cache = cdm_ws_get(CDM_WS_NAME_NAMECAHE, array($taxon_name_uuid));
1156
  $request_params = array();
1157
  $request_params['query'] = $name_cache;
1158
  $request_params['tree'] = get_taxonomictree_uuid_selected();
1159
  $request_params['doTaxa'] = 1;
1160
  $request_params['doSynonyms'] = 1;
1161
  $request_params['doTaxaByCommonNames'] = 0;
1162
  $request_params['matchMode'] = "EXACT";
1163
  $taxon_pager = cdm_ws_get(CDM_WS_PORTAL_TAXON_FIND, NULL, queryString($request_params));
1164

    
1165
  // Removing the name where we come from.
1166
  foreach ($taxon_pager->records as $k => &$taxon) {
1167
    if ($taxon->uuid == $taxon_to_hide_uuid) {
1168
      unset($taxon_pager->records[$k]);
1169
    }
1170
  }
1171
  // Show the taxa list or go to the singular taxon.
1172
  if (sizeof($taxon_pager->records) == 1) {// Single taxon case.
1173
    $singleTaxon = array_pop($taxon_pager->records);
1174
    if ($singleTaxon->class != "Taxon") {
1175
      // It is a Synonym -> look for the accepted.
1176
      $taxon = cdm_ws_get(CDM_WS_PORTAL_TAXON_ACCEPTED, array($singleTaxon->uuid, get_taxonomictree_uuid_selected()));
1177
      if (!empty($highlite_synonym_uuid)) {
1178
        drupal_goto('cdm_dataportal/taxon/' . $taxon[0]->uuid . '/synonymy', array('query' => array('highlite' => $highlite_synonym_uuid)));
1179
      }
1180
      else {
1181
        drupal_goto('cdm_dataportal/taxon/' . $taxon[0]->uuid . '/synonymy', array('query' => array('highlite' => $singleTaxon->uuid)));
1182
      }
1183
    }
1184
    else {
1185
      // It is an accepted taxon.
1186
      if (!empty($highlite_synonym_uuid)) {
1187
        drupal_goto('cdm_dataportal/taxon/' . $singleTaxon->uuid . '/synonymy', array('query' => array('highlite' => $highlite_synonym_uuid)));
1188
      }
1189
      else {
1190
        drupal_goto('cdm_dataportal/taxon/' . $singleTaxon->uuid);
1191
      }
1192
    }
1193
  }
1194
  else {// More than one taxa case.
1195
    $taxon_name_page = new stdClass();
1196
    $taxon_name_page->title = theme('cdm_name_page_title', array('taxon_name' => $taxon_name));
1197
    if ($taxon_pager->records) {
1198
      $taxon_name_page->content = theme_cdm_list_of_taxa($taxon_pager->records, FALSE);
1199
    }
1200
    else {
1201
      $taxon_name_page->content = 'This name has no taxa';
1202
    }
1203
    return $taxon_name_page;
1204
  }
1205
}
1206

    
1207
/**
1208
 * @todo Please document this function.
1209
 * @see http://drupal.org/node/1354
1210
 */
1211
function cdm_dataportal_view_search_advanced() {
1212

    
1213
  drupal_set_title(t('Advanced search'), PASS_THROUGH);
1214
  $form = array();
1215
  $form_state = array();
1216
  $searchForm = cdm_dataportal_search_taxon_form($form, $form_state, TRUE);
1217

    
1218
  return drupal_get_form('cdm_dataportal_search_taxon_form_advanced');
1219

    
1220
}
1221

    
1222
/**
1223
 * @todo Please document this function.
1224
 * @see http://drupal.org/node/1354
1225
 */
1226
function cdm_dataportal_view_search_taxon_by_description() {
1227
  drupal_set_title(t('Search by description full text'), PASS_THROUGH);
1228
  // $form_state = false;
1229
  // $searchForm = cdm_dataportal_search_taxon_by_description_form($form_state);
1230
  $searchForm = cdm_dataportal_search_taxon_by_description_form();
1231

    
1232
  return drupal_get_form('cdm_dataportal_search_taxon_by_description_form');
1233
}
1234

    
1235
/**
1236
 * Executes the search and generates the result list of taxa.
1237
 */
1238
function cdm_dataportal_view_search_results_taxon() {
1239

    
1240
  $taxonPager = cdm_dataportal_search_execute();
1241

    
1242
  $showThumbnails = do_showThumbnails();
1243

    
1244
  $setSessionUri = url('cdm_api/setvalue/session', array(
1245
      'query' => array('var' => '[pageoption][searchtaxa][showThumbnails]', 'val' => ''),
1246
  ));
1247

    
1248
  drupal_add_js('jQuery(document).ready(function() {
1249

    
1250
      // init
1251
      if(' . $showThumbnails . ' == 1){
1252
      jQuery(\'.media_gallery\').show(20);
1253
  } else {
1254
      jQuery(\'.media_gallery\').hide(20);
1255
  }
1256
      // add change hander
1257
      jQuery(\'#showThumbnails\').change(
1258
      function(event){
1259
      var state = 0;
1260
      if(jQuery(this).is(\':checked\')){
1261
      jQuery(\'.media_gallery\').show(20);
1262
      state = 1;
1263
  } else {
1264
      jQuery(\'.media_gallery\').hide(20);
1265
  }
1266
      // store state in session variable
1267
      var uri = \'' . $setSessionUri . '\' + state;
1268
      jQuery.get(uri);
1269
  });
1270
  });',
1271
  array('type' => "inline", 'scope' => JS_DEFAULT));
1272

    
1273
  drupal_set_title(t('Search results'), PASS_THROUGH);
1274

    
1275
  return theme('cdm_search_results', array(
1276
    'pager' => $taxonPager,
1277
    'path' => 'cdm_dataportal/search/results/taxon',
1278
    ));
1279
}
1280

    
1281
/**
1282
 * Provides the standart image wich indicated a loading process
1283
 *
1284
 * @return string
1285
 *  The img html tag
1286
 */
1287
function loading_image_html() {
1288
  return '<img class="loading" src="' . base_path() . drupal_get_path('module', 'cdm_dataportal')
1289
    . '/images/loading_circle_grey_16.gif" style="display:none;">';
1290
}
1291

    
1292
/**
1293
 * Returns the state of the the showThumbnails flag set in the
1294
 * users session ($_SESSION['pageoption']['searchtaxa']['showThumbnails']).
1295
 *
1296
 * @return Ambigous <boolean, number>
1297
 *    returns 1 if the flag is set
1298
 */
1299
function do_showThumbnails() {
1300
  static $showThumbnails = null;
1301

    
1302
  if($showThumbnails == null) {
1303
    $showThumbnails = FALSE;
1304
    if (isset($_SESSION['pageoption']['searchtaxa']['showThumbnails'])) {
1305
      $showThumbnails = $_SESSION['pageoption']['searchtaxa']['showThumbnails'];
1306
    }
1307
    if (!is_numeric($showThumbnails)) {
1308
      $showThumbnails = 1;
1309
    }
1310
  }
1311

    
1312
  return $showThumbnails;
1313
}
1314

    
1315
/**
1316
 * View which transforms XML output from a given webservice endpoint into JSON.
1317
 */
1318
/*
1319
 function cdm_view_xml2json(){
1320
 $file = arg(2);
1321
 $datastr = file_get_contents(variable_get('cdm_webservice_url', '').$file);
1322
 return  xml2json::transformXmlStringToJson($datastr);
1323
 }
1324
 */
1325

    
1326
/* ====================== other functions ====================== */
1327
/**
1328
 * Creates a URL to the taxon page specified by the $uuid parameter.
1329
 *
1330
 * The URL will be prepended with a path element to a specific taxon page tab.
1331
 *
1332
 * This tab is either taken from the CDM_DATAPORTAL_DEFAULT_TAXON_TAB which can
1333
 * be set globally in the administrative settings or individually in the user
1334
 * profile. If the CDM_DATAPORTAL_DEFAULT_TAXON_TAB value is set to LAST_VISITED_TAB
1335
 * the last portal will stay on this last tab.
1336
 *
1337
 * A third option is offerered by the $page_tab parameter which allows overwriting this
1338
 * internal mechanism by a specific value.
1339
 *
1340
 * @param string $uuid
1341
 *   The UUID of the taxon.
1342
 * @param string $page_tab
1343
 *   Overwriting the preset mechanism by defining specific value for the
1344
 *   taxon page tab.
1345
 *
1346
 * @return string
1347
 *   The created URL.
1348
 */
1349
function path_to_taxon($uuid, $page_tab = FALSE) {
1350

    
1351
  $tab = get_default_taxon_tab();
1352
  $values = unserialize(CDM_DATAPORTAL_DEFAULT_TAXON_TAB);
1353

    
1354
  if (!$uuid) {
1355
    return FALSE;
1356
  }
1357

    
1358
  if ($page_tab) {
1359
    return 'cdm_dataportal/taxon/' . $uuid . '/' . $page_tab;
1360
  }
1361
  elseif (!$tab || strtolower($tab) == 'general') {
1362
    return 'cdm_dataportal/taxon/' . $uuid;
1363
  }
1364
  elseif (get_last_taxon_page_tab() &&   $tab == $values[CDM_DATAPORTAL_LAST_VISITED_TAB_ARRAY_INDEX]) {
1365
    return 'cdm_dataportal/taxon/' . $uuid . '/' . get_last_taxon_page_tab();
1366
  }
1367
  else {
1368
    return 'cdm_dataportal/taxon/' . $uuid . '/' . strtolower($tab);
1369
  }
1370
}
1371

    
1372
/**
1373
 * Creates a URL to show a synonmy in the according taxon page.
1374
 *
1375
 * The URL will point to the synonymy tab of the taxon page of the accepted taxon given as parameter $acceptedUuid.
1376
 * The resulting URI will include query parameters to highlight the synonym, and to optionally display
1377
 * the accepted taxons name in aform like "Foo bar is accepted taxon for Ree doo". The URI will also
1378
 * include the sysnonym uuid as fragment in order to let the browser scroll to the according location
1379
 * in the page
1380
 *
1381
 * @param string $synonymUuid
1382
 *    The uuid of the synonym
1383
 * @param string $acceptedUuid
1384
 *    The uuid of the according accepted taxon
1385
 * @return string
1386
 *    The URL to show a synonmy in the according taxon page
1387
 */
1388
function uri_to_synonym($synonymUuid, $acceptedUuid) {
1389
  $acceptedPath = path_to_taxon($acceptedUuid, "synonymy");
1390
  return url($acceptedPath, array(
1391
      'query' => array(
1392
        // highlite the synony in the synonymy
1393
        'highlite' => $synonymUuid,
1394
        // the taxon page is refered from a synonym and the synonym can optionally be named in the page title
1395
        // see theme_taxon_page_title()
1396
        'acceptedFor' => $synonymUuid
1397
      ),
1398
      'fragment' => $synonymUuid
1399
  ));
1400

    
1401
}
1402

    
1403
/**
1404
 * Compses the drupal path to the key identified by the uuid.
1405
 *
1406
 * @param string $keyType
1407
 *    the key typer corresponds to the specific class of the CDM
1408
 *    IdentificationKey. Possible values are
1409
 *      -PolytomousKey
1410
 *      -MultimediaKey
1411
 *      - ...
1412
 * @param UUID $keyUuid
1413
 *   The UUID of the key
1414
 */
1415
function path_to_key($keyType, $keyUuid) {
1416
  if (!$keyUuid || !$keyType) {
1417
    return FALSE;
1418
  }
1419
  $keyType{0} = strtolower($keyType{0});
1420
  return "cdm_dataportal/" . $keyType . "/$keyUuid";
1421
}
1422

    
1423
/**
1424
 * @todo Please document this function.
1425
 * @see http://drupal.org/node/1354
1426
 */
1427
function path_to_reference($uuid) {
1428
  if (!$uuid) {
1429
    return FALSE;
1430
  }
1431
  return 'cdm_dataportal/reference/' . $uuid;
1432
}
1433

    
1434
/**
1435
 * @todo Please document this function.
1436
 * @see http://drupal.org/node/1354
1437
 */
1438
function path_to_name($name_uuid) {
1439
  $res = FALSE;
1440
  if ($name_uuid) {
1441
    $res = 'cdm_dataportal/name/' . $name_uuid;
1442
  }
1443
  return $res;
1444
}
1445

    
1446
/**
1447
 * @todo Please document this function.
1448
 * @see http://drupal.org/node/1354
1449
 */
1450
function path_to_media($uuid, $representaion_uuid = FALSE, $partId = FALSE) {
1451
  if (!$uuid) {
1452
    return FALSE;
1453
  }
1454
  $out = 'cdm_dataportal/media/' . $uuid;
1455
  if ($representaion_uuid) {
1456
    $out .= '/' . $representaion_uuid;
1457
    if ($partId !== FALSE) {
1458
      $out .= '/' . $partId;
1459
    }
1460
  }
1461
  return $out;
1462
}
1463

    
1464
/**
1465
 * Compares thisRank with thatRank.
1466
 *
1467
 * Returns a negative integer, zero, or a positive integer
1468
 * as the of thisRank is higher than, equal to, or lower than thatRank.
1469
 * e.g:
1470
 * <ul>
1471
 * <li>rank_compare({species_uuid}, {genus_uuid}) = -1</li>
1472
 * <li>rank_compare({genus_uuid}, {genus_uuid}) = 0</li>
1473
 * <li>rank_compare({genus_uuid}, {tribus_uuid}) = 1</li>
1474
 * </ul>
1475
 * <p>
1476
 * This compare logic of the underlying webservice is the
1477
 * <b>inverse logic</b> of the the one implemented in
1478
 * java.lang.Comparable#compareTo(java.lang.Object)
1479
 *
1480
 * @param $thisRankUuid
1481
 * @param $thatRankUuid
1482
 *
1483
 * @return int
1484
 *   A negative integer, zero, or a positive integer
1485
 *   as the thisRank is lower than, equal to, or higher than thatRank.
1486
 */
1487
function rank_compare($thisRankUuid, $thatRankUuid) {
1488
  $result = cdm_ws_get(CDM_WS_TERM_COMPARE, array($thisRankUuid, $thatRankUuid));
1489
  return $result->Integer;
1490
}
1491

    
1492
/**
1493
 * Composes an HTML element class attribute value composed of
1494
 * the shortname of the cdm class and the uuid of the entity.
1495
 * This class attribute should be used whereever an cdm-entity is rendered.
1496
 *
1497
 * These according class selectors in css must be escaped, eg:
1498
 *    .cdm\:TextData
1499
 *
1500
 * @param $cdmEntity
1501
 */
1502
function html_class_atttibute_ref($cdmEntity) {
1503

    
1504
  if (is_cdm_entity($cdmEntity)) {
1505
    return "cdm:" . $cdmEntity->class . " uuid:" . $cdmEntity->uuid;
1506
  }
1507
}
1508

    
1509
/**
1510
 * Preprocess the taggedName to normalize newly introduced
1511
 * tagtypes like hybridSign, separator, .. more?
1512
 *
1513
 * @param unknown_type $taggedTextList
1514
 */
1515
function normalize_TaggedName(&$taggedTextList) {
1516

    
1517
  if (is_array($taggedTextList)) {
1518

    
1519
    // First pass: rename.
1520
    for ($i = 0; $i < count($taggedTextList); $i++) {
1521

    
1522
      if ($taggedTextList[$i]->type == "hybridSign") {
1523
        $taggedTextList[$i]->type = "name";
1524
      }
1525
    }
1526

    
1527
    // Second pass: resolve separators.
1528
    $taggedNameListNew = array();
1529
    for ($i = 0; $i < count($taggedTextList); $i++) {
1530

    
1531
      if ($i + 1 < count($taggedTextList) && $taggedTextList[$i + 1]->type == "separator") {
1532

    
1533
        if ($taggedTextList[$i]->type == $taggedTextList[$i + 2]->type) {
1534
          $taggedName = $taggedTextList[$i];
1535
          $taggedName->text = $taggedName->text . $taggedTextList[$i + 1]->text . $taggedTextList[$i + 2]->text;
1536
          $taggedNameListNew[] = $taggedName;
1537
          ++$i;
1538
          ++$i;
1539
        }
1540
      }
1541
      else {
1542
        $taggedNameListNew[] = $taggedTextList[$i];
1543
      }
1544
    }
1545
    $taggedTextList = $taggedNameListNew;
1546
  }
1547
}
1548

    
1549
/**
1550
 * Creates a short taxonname.
1551
 *
1552
 * The short name is created by using the taggename field of
1553
 * NameSTO or NameTO instances.
1554
 * If the taggename if empty the fullname will be returned.
1555
 *
1556
 * @param unknown_type $name
1557
 *   Name or TreeNode.
1558
 *
1559
 * @return string
1560
 */
1561
function cdm_dataportal_shortname_of($name) {
1562
  $nameStr = '';
1563

    
1564
  normalize_TaggedName($name->taggedTitle);
1565

    
1566
  // Get all tagged text tokens of the scientific name.
1567
  foreach ($name->taggedTitle as $tagtxt) {
1568
    if ($tagtxt->type == 'name' || $tagtxt->type == 'rank') {
1569
      $nameStr .= ($nameStr ? ' ' : '') . $tagtxt->text;
1570
    }
1571
  }
1572
  $nameStr = trim($nameStr);
1573

    
1574
  if ($nameStr) {
1575

    
1576
    // Do not return short names for these.
1577
    if ($name->unplaced || $name->excluded) {
1578
      return $nameStr;
1579
    }
1580

    
1581
    /*
1582
    1st capture group (^[a-zA-Z]): First letter of uninomial.
1583
    Second capture group ([\p{L}]+): remaining letters of uninomial ([\p{L} = an UTF-8 letter).
1584
    Third capture group (\s+[^(\x2E]+\s+.+$|\s+[a-zA-Z]+$): letters of name,
1585
    but only matching if no '(' or '.' in second word of name,        ( \x2E = '.')
1586
    OR only one specific epithet \s+[\p{L}\x22\x2D\xD7]+$             (\x22= '"', \x2D='-', \xD7='×' )
1587
    */
1588
    $pattern = '/(^[a-zA-Z])([\p{L}]+)(\s+[^(\x2E]+\s+.+$|\s+[\p{L}\x22\x2D\xD7]+$)/u';
1589
    if (preg_match($pattern, $nameStr, $matches, PREG_OFFSET_CAPTURE)) {
1590
      return $matches[1][0] . "." . $matches[3][0];
1591
    }
1592
    else {
1593
      return $nameStr;
1594
    }
1595
  }
1596
  else {
1597
    return $name->titleCache;
1598
  }
1599
}
1600

    
1601
/**
1602
 * Check if a taxon is accepted by the current taxonomic tree.
1603
 *
1604
 * @param mixed $taxon
1605
 *   The Taxon obkect to check.
1606
 *
1607
 * @return bool
1608
 *   Returns TRUE if $taxon is accepted, FALSE otherwise.
1609
 */
1610
function _cdm_dataportal_acceptedByCurrentView($taxon) {
1611

    
1612
  $defaultTreeUuid = get_taxonomictree_uuid_selected();
1613

    
1614
  if (isset($taxon->taxonNodes)) {
1615
    $taxonNodes = $taxon->taxonNodes;
1616
  }
1617
  else {
1618
    $taxonNodes = cdm_ws_get(CDM_WS_PORTAL_TAXON_TAXONNODES, $taxon->uuid);
1619
  }
1620

    
1621
  if ($taxon->class == "Taxon" && isset($taxonNodes)) {
1622
    foreach ($taxonNodes as $node) {
1623
      if (is_object($node->classification)) {
1624
        if ($node->classification->uuid == $defaultTreeUuid) {
1625
          return TRUE;
1626
        }
1627
      }
1628
      else {
1629
        if ($node->classification == $defaultTreeUuid) {
1630
          return TRUE;
1631
        }
1632
      }
1633
    }
1634
  }
1635

    
1636
  return FALSE;
1637
}
1638

    
1639
/**
1640
 * Workaround for missing original source type.
1641
 *
1642
 * idNamespace is always set in these cases.
1643
 * @todo improve documentation of this function.
1644
 *
1645
 * @param unknown_type $source
1646
 *
1647
 * @return unknown_type
1648
 */
1649
function _is_original_source_type($source) {
1650
  return !$source->idNamespace && strlen($source->idNamespace) == 0;
1651
}
1652

    
1653
/**
1654
 * @todo Please document this function.
1655
 * @see http://drupal.org/node/1354
1656
 */
1657
function _is_invers_taxonRelationship($taxonRelationship, $focusedTaxon) {
1658
  return $taxonRelationship->toTaxon->uuid == $focusedTaxon->uuid;
1659
}
1660

    
1661
/**
1662
 * Gets the metadata info such as title or artist of a media file.
1663
 *
1664
 * The function tries at first to get all the info from the file metadata
1665
 * and if it is not avaible look at the media file info stored at the database.
1666
 *
1667
 * @param mixed $media
1668
 *   The media file object for which to get the metadata.
1669
 *
1670
 * @return array
1671
 *   The array with the available specified metadata info.
1672
 * TODO rename to read_media_metadata() and move to *.module.
1673
 */
1674
function cdm_read_media_metadata($media) {
1675

    
1676
  $metadata_caption = array(
1677
    'title' => '',// Media_metadata and media.
1678
    'artist' => '',// Media_metadata and media.
    'rights',// Media_metadata and media.
    'location',// Media_metadata.
1679
    'filename' => '',// Media.
1680
    'mediacreated' => '',// Media.
1681
    'description' => '',
1682
  );// Media.
1683

    
1684
  // Getting the media metadata.
1685
  $media_metadata = cdm_ws_get(CDM_WS_MEDIA_METADATA, array($media->uuid));
1686
  $media_metadata_aux = (array) $media_metadata;
1687

    
1688
  // Filename.
1689
  if (!empty($media->representations[0]->parts[0]->uri)) {
1690
    $fileUri = $media->representations[0]->parts[0]->uri;
1691
    $filename = substr($fileUri, strrpos($fileUri, "/") + 1);
1692
    $metadata_caption['filename'] = $filename;
1693
  }
1694
  else {
1695
    $metadata_caption['filename'] = '';
1696
  }
1697

    
1698
  // Title.
1699
  if (!empty($media_metadata->ObjectName)) {
1700
    $metadata_caption['title'] = $media_metadata->ObjectName;
1701
  }
1702
  elseif (!empty($media_metadata_aux['Object Name'])) {
1703
    $metadata_caption['title'] = $media_metadata_aux['Object Name'];
1704
  }
1705
  elseif (!empty($media->title_L10n)) {
1706
    $metadata_caption['title'] = $media->title_L10n;
1707
  }
1708
  elseif (!empty($media->titleCache)) {
1709
    $metadata_caption['title'] = $media->titleCache;
1710
  }
1711

    
1712
  // Append description to title.
1713
  if (!empty($media->description_L10n)) {
1714
    $metadata_caption['title'] .= '<span class="media-description">' . $media->description_L10n . '<span>';
1715
  }
1716

    
1717
  // Artist.
1718
  if (!empty($media_metadata->Artist)) {
1719
    $metadata_caption['artist'] = '' . $media_metadata->Artist;
1720
  }
1721
  elseif (!empty($media->artist->titleCache)) {
1722
    $metadata_caption['artist'] = $media->artist->titleCache;
1723
  }
1724

    
1725
  // Copyright.
1726
  $metadata_caption['rights'] = array(
1727
    'copyright' => array('agentNames' => array()),
1728
    'license' => array(
1729
      'agentNames' => array(),
1730
      'types' => array(),
1731
      'abbreviatedTexts' => array(),
1732
      'uris' => array(),
1733
    ),
1734
  );
1735
  if (!empty($media_metadata->Copyright)) {
1736
    $metadata_caption['rights']['copyright']['agentNames'][] = $media_metadata->Copyright;
1737
  }
1738
  elseif (isset($media->rights) && is_array($media->rights)) {
1739
    foreach ($media->rights as $right) {
1740
      switch ($right->term->uuid) {
1741
        case UUID_RIGHTS_LICENCE:
1742
          $metadata_caption['rights']['license']['agentNames'][] = ($right->agent ? '' . $right->agent->firstname . ' ' . $right->agent->lastname : '');
1743
          $metadata_caption['rights']['license']['types'][] = ($right->representation_L10n ? '' . $right->representation_L10n : '');
1744
          $metadata_caption['rights']['license']['abbreviatedTexts'][] = ($right->abbreviatedText ? '' . $right->abbreviatedText : '');
1745
          $metadata_caption['rights']['license']['uris'][] = ($right->uri ? '' . $right->uri : '');
1746
          break;
1747
        case UUID_RIGHTS_COPYRIGHT:
1748
          $metadata_caption['rights']['copyright']['agentNames'][] = $right->agent->firstname . ' ' . $right->agent->lastname;
1749
          break;
1750
      }
1751
    }
1752
  }
1753
  else {
1754
    $metadata_caption['rights']['agentNames'][] = '';
1755
  }
1756

    
1757
  // Filling the description (though there is no description in the db???).
1758
  // $metadata_caption['description'] = $media->description_L10n;
1759

    
1760
  // Location.
1761
  $metadata_caption['location'] = array();
1762
  $metadata_caption['location']['sublocation'] = !empty($media_metadata->Sublocation) ? $media_metadata->Sublocation : FALSE;
1763
  $metadata_caption['location']['city'] = !empty($media_metadata->City) ? $media_metadata->City : FALSE;
1764
  $metadata_caption['location']['province'] = !empty($media_metadata->Province) ? $media_metadata->Province : FALSE;
1765
  $metadata_caption['location']['country'] = !empty($media_metadata->Country)? $media_metadata->Country : FALSE;
1766

    
1767
  /*
1768
   // Creation date.
1769
   if($media_metadata["Modify Date"])
1770
   $metadata_caption['mediacreated'] = $media_metadata["Modify Date"];
1771
   else
1772
   $metadata_caption['mediacreated'] = $media->created;
1773
   */
1774

    
1775
  // Returned value.
1776
  return $metadata_caption;
1777
}
1778

    
1779
/**
1780
 * Collects all the media from a list of description elements.
1781
 *
1782
 * @param array $descriptionElements
1783
 *   The description elements from which to collect the media.
1784
 *
1785
 * @return array
1786
 *   The output with all the collected media.
1787
 */
1788
function cdm_dataportal_media_from_descriptionElements($descriptionElements) {
1789

    
1790
  $outArrayOfMedia = array();
1791

    
1792
  // Avoiding a warning box in Drupal for Flora Malesiana.
1793
  if (isset($descriptionElements) && is_array($descriptionElements)) {
1794
    foreach ($descriptionElements as $descriptionElement) {
1795
      if (isset($descriptionElement->media) && is_array($descriptionElement->media)) {
1796
        foreach ($descriptionElement->media as $media) {
1797
          if (is_object($media)) {
1798
            $outArrayOfMedia[] = $media;
1799
          }
1800
        }
1801
      }
1802
    }
1803
  }
1804
  return $outArrayOfMedia;
1805
}
1806

    
1807
/**
1808
 * @todo Please document this function.
1809
 * @see http://drupal.org/node/1354
1810
 *
1811
 * @param array $cdmBase_list
1812
 *   An array of CdmBase instances or a single instance.
1813
 * @param string $footnote_list_key
1814
 *
1815
 * @return unknown
1816
 */
1817
function cdm_annotations_as_footnotekeys($cdmBase_list, $footnote_list_key = NULL) {
1818

    
1819
   static $annotations_types_filter = null;
1820
   if(!$annotations_types_filter) {
1821
     $annotations_types_filter = unserialize(ANNOTATIONS_TYPES_AS_FOOTNOTES_DEFAULT);
1822
   }
1823

    
1824
  $footNoteKeys = array();
1825

    
1826
  // Is argument cdmBase an array?
1827
  if (!is_array($cdmBase_list)) {
1828
    $cdmBase_array = array();
1829
    $cdmBase_array[] = $cdmBase_list;
1830
  }
1831
  else {
1832
    $cdmBase_array = $cdmBase_list;
1833
  }
1834

    
1835
  // Getting the key for the footnotemanager.
1836
  if (isset($footnote_list_key)) {
1837
    $footnoteListKey = $footnote_list_key;
1838
  }
1839
  else {
1840
    $footnoteListKey = RenderHints::getFootnoteListKey() . '-annotations';
1841
  }
1842

    
1843
  // Adding the footnotes keys.
1844
  foreach ($cdmBase_array as $cdmBase_element) {
1845
    $annotations = cdm_ws_getAnnotationsFor($cdmBase_element, variable_get('annotations_types_as_footnotes', $annotations_types_filter));
1846
    if (is_array($annotations)) {
1847
      foreach ($annotations as $annotation) {
1848
        $footNoteKeys[] = FootnoteManager::addNewFootnote($footnoteListKey, $annotation->text);
1849
      }
1850
    }
1851
  }
1852

    
1853
  return $footNoteKeys;
1854
}
1855

    
1856
// @WA Comment: should this not be a theme function?
1857
/**
1858
 *  Creates a CDM Dynabox.
1859
 * @param string $dynabox_id
1860
 *   a uninque name for tha dynabox, using a cdm entity uuid herefore is good pratice
1861
 * @param string $label
1862
 *   The clickable text to show.
1863
 * @param string $content_url
1864
 *   The cdm REST service request url wich will deliver the content to be shown
1865
 *   once the dynabox toggles open.
1866
 * @param string $theme
1867
 *   The theme to be used for rendering the cdm REST service reponse with is
1868
 *   returned from the $content_url.
1869
 * @param string $link_alt_text
1870
 *   The value for the alt attribute of the dynabox link.
1871
 * @param array $enclosingtags
1872
 *   An array with two elements: $enclosingtags[0] will be used for the dynabox
1873
 *   element itself, $enclosingtags[1] is the tag to be used for the
1874
 *   dynabox_content (optional)
1875
 * @param string $open_callback
1876
 *   optional javascript call back function to be triggered after toggeling the box to
1877
 *   the open state.
1878
 * @param string $close_callback
1879
 *   optional javascript call back function to be triggered after toggeling the box to
1880
 *   the closed state.
1881
 *
1882
 * @return string
1883
 *   Returns HTML for a dynabox.
1884
 */
1885
function cdm_dynabox($dynabox_id, $label, $content_url, $theme, $link_alt_text, $enclosingtags = array('li', 'ul'), $open_callback = 'function(){}', $close_callback = 'function(){}') {
1886
  $out = '';
1887

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

    
1891
  _add_js_domEvent(); // requires domEvent.js
1892
  drupal_add_js(drupal_get_path('module', 'cdm_dataportal') . '/js/cdm_dynabox.js');
1893
  drupal_add_js("
1894
  jQuery(document).ready(
1895
      function() {
1896
        dynabox('". $dynabox_id ."',
1897
          {
1898
            open_callback: " . $open_callback .",
1899
            close_callback: " . $close_callback ."
1900
          }
1901
        );
1902
      }
1903
   );",
1904
   array(
1905
    'type'=>'inline',
1906
    'scope'=>'footer'
1907
    )
1908
  );
1909

    
1910
  $cdm_proxy_url = url('cdm_api/proxy/' . urlencode($content_url) . "/$theme");
1911
  $out .= '<!-- ' . $content_url . ' -->';
1912
  $out .= '<' . $enclosingtags[0] . ' class="dynabox-' . $dynabox_id . '"><a href="#" class="label" alt="' . t($link_alt_text) . '">' . $label . '</a>';
1913
  $out .= '<' . $enclosingtags[1] . ' class="dynabox-' . $dynabox_id . '-content" title="' . $cdm_proxy_url . '"><' . $enclosingtags[0] . '>' . loading_image_html() . '</' . $enclosingtags[0] . '></' . $enclosingtags[1] . '>';
1914
  $out .= '</' . $enclosingtags[0] . '>';
1915
  return $out;
1916
}
1917

    
1918
/**
1919
 * Checks whether a feature has any description elements.
1920
 *
1921
 * @param mixed $featureNode
1922
 *   A feature node as produced by the function _mergeFeatureTreeDescriptions().
1923
 *
1924
 * @see _mergeFeatureTreeDescriptions()
1925
 *
1926
 * @return bool
1927
 *   Returns TRUE if the given $featureNode or any of its subordinate nodes
1928
 *   contains at least one non empty TextData or at least one DescriptionElement
1929
 *   of an other type. A TextData element holding a multilanguageText or a
1930
 *   source reference is considered to be not empty.
1931
 */
1932
function hasFeatureNodeDescriptionElements($featureNode) {
1933

    
1934
  if (isset($featureNode->descriptionElements) && is_array($featureNode->descriptionElements) && count($featureNode->descriptionElements) > 0) {
1935
    foreach ($featureNode->descriptionElements as $descriptionElement) {
1936
      if ($descriptionElement->class != "TextData" || isset($descriptionElement->multilanguageText_L10n->text) && $descriptionElement->multilanguageText_L10n->text != '' || isset($descriptionElement->sources[0]) || isset($descriptionElement->media[0]) ) {
1937
        return TRUE;
1938
      }
1939
    }
1940
  }
1941
  elseif (isset($featureNode->children) && is_array($featureNode->children)) {
1942
    foreach ($featureNode->children as $child) {
1943
      if (hasFeatureNodeDescriptionElements($child)) {
1944
        return TRUE;
1945
      }
1946
    }
1947
  }
1948
  return FALSE;
1949
}
Add picture from clipboard (Maximum size: 40 MB)