Project

General

Profile

Download (26.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
// $Id$
3

    
4
/*
5
 * @file
6
 * cdm_dataportal
7
 *
8
 * Copyright (C) 2007 EDIT
9
 * European Distributed Institute of Taxonomy
10
 * http://www.e-taxonomy.eu
11
 *
12
 * The contents of this file are subject to the Mozilla Public License Version 1.1
13
 * See LICENSE.TXT at the top of this package for the full license terms.
14
 */
15

    
16
require_once('cdm_dataportal.theme.php');
17

    
18
/* ====================== hook implementations ====================== */
19

    
20
/**
21
 * Implementation of hook_help()
22
 *
23
 * Display help and module information
24
 * @param section which section of the site we're displaying help
25
 * @return help text for section
26
 */
27
function cdm_dataportal_help($section='') {
28

    
29
  $out = '';
30
  switch ($section) {
31
    case "admin/modules#description":
32
      $out = t("The dataportal publishes CDM data hosted in a CommunityStore on the web.");
33
      break;
34
  }
35
  return $out;
36
}
37

    
38

    
39
/**
40
 * Implementation of hook_perm()
41
 *
42
 * Valid permissions for this module
43
 * @return array An array of valid permissions for the portfolio module
44
 */
45
function cdm_dataportal_perm() {
46
  return array(
47
    	'administer cdm_dataportal',
48
        'cdm_dataportal view notes',
49
  //TODO which else permission are required? -> check the WP6 requirements document
50
  );
51
}
52

    
53

    
54
/**
55
 * Implementation of hook_menu()
56
 */
57
function cdm_dataportal_menu($may_cache) {
58
  $items = array();
59
  if ($may_cache) {
60

    
61
    $items[] = array(
62
      'path' => 'admin/settings/cdm_dataportal',
63
      'title' => t('CDM Dataportal'),
64
      'description' => t('Setting for the CDM Dataportal'),
65
      'access' => user_access('administer cdm_dataportal'),
66
      'callback' => 'drupal_get_form',
67
      'callback arguments' => 'cdm_dataportal_settings',
68
    );
69
    
70
       $items[] = array(
71
      'path' => 'admin/settings/cdm_dataportal/ws',
72
      'title' => t('Web Service'),
73
      'description' => t('Setting for the CDM Dataportal'),
74
      'access' => user_access('administer cdm_dataportal'),
75
      'callback' => 'drupal_get_form',
76
      'callback arguments' => 'cdm_dataportal_settings',
77
      'weight' => 1,
78
      'type' => MENU_DEFAULT_LOCAL_TASK,
79
    );
80
    
81
    $items[] = array(
82
      'path' => 'admin/settings/cdm_dataportal/rules',
83
      'title' => t('Media URI Conversion'),
84
      'description' => t('Define Media URI Conversion Rules'),
85
      'access' => user_access('administer cdm_dataportal'),
86
      'callback' => 'drupal_get_form',
87
      'callback arguments' => 'cdm_dataportal_settings_rules',
88
      'weight' => 2,
89
      'type' => MENU_LOCAL_TASK,
90
    );
91

    
92
    $items[] = array(
93
	    'path' => 'cdm_dataportal/names',
94
	    'callback' => 'cdm_dataportal_view_names',
95
	    'access' => true,
96
	    'type' => MENU_CALLBACK, 
97
    );
98
    // optional callback arguments: page
99
     
100
    $items[] = array(
101
	    'path' => 'cdm_dataportal/taxon',
102
	    'callback' => 'cdm_dataportal_view_taxon',
103
	    'access' => true,
104
	    'type' => MENU_CALLBACK, 
105
    // expected callback arguments: name_uuid
106
    );
107
    
108
    $items[] = array(
109
      'path' => 'cdm_dataportal/search/taxon',
110
      'callback' => 'cdm_dataportal_view_search_taxon',
111
      'access' => true,
112
      'type' => MENU_CALLBACK, 
113
    );
114
    
115
    $items[] = array(
116
      'path' => 'cdm_dataportal/filter',
117
      'callback' => 'cdm_dataportal_view_filter',
118
      'access' => true,
119
      'type' => MENU_CALLBACK, 
120
    );
121
    
122
    $items[] = array(
123
      'path' => 'cdm/xml2json',
124
      'callback' => 'cdm_view_xml2json',
125
      'access' => true,
126
      'type' => MENU_CALLBACK, 
127
    );
128
     
129
  }
130
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal.css');
131
  //drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_print.css', 'print');
132
  drupal_add_css(drupal_get_path('module', 'cdm_dataportal').'/cdm_dataportal_screen.css', 'screen');
133
  return $items;
134
  
135
}
136

    
137
/**
138
 * Implementation of hook_block()
139
 *
140
 * Provides the following blocks:
141
 *  0: list of links useful during development
142
 *
143
 * @param String $op
144
 * @param int $delta
145
 */
146
function cdm_dataportal_block($op='list', $delta=0) {
147
  // listing of blocks, such as on the admin/block page
148
  if ($op == "list") {
149
    $block[0]["info"] = t("CDM DataPortal DevLinks");
150
    $block[1]["info"] = t("CDM DataPortal Credits");
151
    $block[2]["info"] = t("CDM Search Taxa");
152
    $block[3]["info"] = t("CDM Filters");
153
    return $block;
154
  }
155
  else if ($op == 'view') {
156
    switch($delta){
157
      case 0:
158
        $block['subject'] = t('CDM DataPortal DevLinks');
159
        $block['content'] = '<ul>
160
        <li>'.l('A Taxon Page', cdm_dataportal_taxon_path('5000001-stub')).'</li>
161
        <li>'.l('Name List - page1 of A', 'cdm_dataportal/names/A/1')
162
        //.'</li><li>'.l('xml2json', 'cdm/xml2json/namelist;startwith=A.xml')
163
        .'</li>
164
        </ul>';
165
        return $block;
166
      case 1:
167
        $block['subject'] = t('Credits');
168
        $block['content'] = theme('cdm_credits');
169
        return $block;
170
      case 2:
171
        $block['subject'] = t('Search Taxa');
172
        $block['content'] = drupal_get_form('cdm_dataportal_search_taxon_form');
173
        return $block;   
174
      case 3:
175
        $block['subject'] = t('Active Filters');
176
        $block['content'] = cdm_dataportal_view_filter('list');
177
        return $block;
178
    }
179
  }
180
}
181

    
182

    
183
/**
184
 * Implementation of hook_validate()
185
 *
186
 * @param $element
187
 */
188
function cdm_dataportal_settings_validate($form_id, $form_values){
189
  if (!str_endsWith($form_values['cdm_webservice_url'], '/')) {
190
    form_set_error('cdm_webservice_url', t("The URL to the CDM Web Service must end with a slash: '/'."));
191
  }
192
}
193

    
194
function cdm_dataportal_search_taxon_form(){
195
  
196
  $url = 'cdm_dataportal/search/taxon';
197
  $form['#method'] = 'get';
198
  $form['#process'] = array('cdm_dataportal_search_process' => array());
199
  $form['#action'] = url($url, NULL, NULL, true);
200
  
201
  $form['queryString'] = array(
202
    '#type' => 'textfield',
203
    '#size' => 20,
204
    '#attributes' => array('title' => t('Enter the name or part of a name you wish to search for.')),
205
    '#default_value' => (isset($_SESSION['cdm']['search']['queryString']) ? $_SESSION['cdm']['search']['queryString'] : ''),
206
  );
207
  $form['onlyAccepted'] = array(
208
    '#type' => 'checkbox',
209
    '#title' => t('Only Accepted'),
210
    '#attributes' => array('title' => t('Search only for taxa which are accepted by the current treatment.')),
211
    '#default_value' => isset($_SESSION['cdm']['search']['onlyAccepted']) && $_SESSION['cdm']['search']['onlyAccepted'],
212
  );
213
  /*
214
  $form['vernacular'] = array(
215
    '#type' => 'checkbox',
216
    '#title' => t('Vernacular Names'),
217
    '#attributes' => array('title' => t('Search for vernacular names.')),
218
    '#default_value' => isset($_SESSION['cdm']['search']['vernacular']) && $_SESSION['cdm']['search']['vernacular'],
219
  );
220
  $form['language'] =  array(
221
    '#type' => 'select',
222
    '#title' => t('Language'),
223
    '#options' => array(
224
        '*' => t('ALL')
225
        ),
226
    '#description'   => t('The language of the vernacular name'),
227
    '#default_value' => (isset($_SESSION['cdm']['search']['language']) ? $_SESSION['cdm']['search']['language'] : ''),
228
  );
229
  */
230
  $form['page'] = array(
231
  '#type' => 'hidden',
232
  '#value' => '1',
233
  );
234
  
235
  $form['submit'] = array(
236
  '#type' => 'submit',
237
  '#name' => '', 
238
  '#value' => t('Search'),
239
  );
240
  
241
  // clean URL get forms breaks if we don't give it a 'q'.
242
  if (!(bool)variable_get('clean_url', '0')) {
243
    $form['q'] = array(
244
      '#type' => 'hidden',
245
      '#value' => $url,
246
      '#name' => 'q',
247
    );
248
  }
249
  return $form;
250
}
251

    
252

    
253
function cdm_dataportal_search_process($form) {
254
  unset($form['form_id']);
255
  unset($form['form_token']);
256
  return $form;
257
}
258

    
259
/**
260
 * Filters $_REQUEST by a list of valid request  parameters and also sets defaults if required.
261
 * returns the processed request parameters submitted by the search form.
262
 */
263
function cdm_dataportal_search_form_request(){
264
  // keys map $REQUEST Keys values are optional default parameters if not null
265
  static $search_form_params = array('queryString'=>null, 'onlyAccepted' =>'0', 'page'=>null);
266
  
267
  $form_params = array();
268
  foreach($search_form_params as $key=>$default ){
269
    $form_params[$key] = ( isset($_REQUEST[$key]) && $_REQUEST[$key] != '' ? $_REQUEST[$key] : $default);
270
  }
271
  return $form_params;
272
}
273
  
274

    
275
/*
276
function cdm_dataportal_search_taxon_form_submit($form_id, $form_values) {
277
  
278
  $_SESSION['cdm']['search'] = $form_values;
279
  //return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['vernacular']?'1':'0').'/'.$form_values['language'];      
280
  return '/cdm_dataportal/search/taxon/'.$form_values['queryString'].'/'.($form_values['onlyAccepted']?'1':'0');  
281
  //$paramstr = compose_url_prameterstr($form_values);
282
  //return url('/cdm_dataportal/search/taxon/', $paramstr);
283
}
284
*/
285
/* ====================== menu callback functions ====================== */
286

    
287
/**
288
 * Generate main administration form.
289
 *
290
 * @return
291
 *   An array containing form items to place on the module settings page.
292
 */
293
function cdm_dataportal_settings(){
294

    
295
  $form = cdm_api_settings_form();
296

    
297
  //TODO: settings are still incomplete, compare with trunk/dataportal/inc/config_default.php.inc
298
  $form['cdm_dataportal'] = array(
299
      '#type' => 'fieldset',
300
      '#title' => t('CDM DataPortal'),
301
      '#collapsible' => FALSE,
302
      '#collapsed' => TRUE,
303
  );
304
  $allSecRefs = cdm_ws_get(CDM_WS_SEC_REFERENCES_ALL);
305
  $secRef_default = isset($allSecRefs[0]) ? $allSecRefs[0] : false;
306
  $secUuid_options = array();
307
  foreach($allSecRefs as $ref){
308
    $secUuid_options[$ref->uuid] =  $ref->citation.($ref->year ? ' ['.$ref->year.']' : '');
309
  }
310
  $form['cdm_dataportal']['cdm_secUuid_default'] =  array(
311
    '#type' => 'select',
312
    '#title'         => t('Default Concept Reference'),
313
    '#options' => $secUuid_options,
314
    '#default_value' => variable_get('cdm_secUuid_default', $secRef_default->uuid),
315
    '#description'   => t('The default \'sensu\' reference to start the cdm_dataportal with. Per user choices are possible and may override this value.'),
316
  );
317
   
318
  return system_settings_form($form);
319
}
320

    
321
function cdm_dataportal_settings_rules(){
322
  $form = array();
323

    
324
  $rules = cdm_dataportal_mediaUri_converion_rules();
325
   /*
326
   * rules
327
   * 
328
   *  ['type']: values(preview, webapp, media)
329
   *  ['size_x'], ['size_y']: dimensions of preview image in px
330
   *  ['pattern']: php PCRE regex pattern (see: http://www.php.net/manual/reference.pcre.pattern.syntax.php)
331
   *  ['replace']: replacement string 
332
   *  ['prefix']: string by which the converted url is to be prefixed
333
   *  ['affix']: string by which the converted url is to be affixed
334
   *  ['url_encode']: values(TRUE, FALSE) if the converted url is to be url encoded
335
   *  ['embed_html']: embes the construct of prefix + url + affix into the page as plain html
336
   * 
337
   */
338
  $new_rule = array();
339
  $new_rule['type'] = 'empty';
340
  $new_rule['pattern'] = '';
341
  $new_rule['replace'] = '';
342
  $new_rule['prefix'] = ''; 
343
  $new_rule['affix'] = '';
344
  $new_rule['url_encode'] = 0; 
345
  $new_rule['embed_html'] = 0;
346
  
347
  $rules[] = $new_rule;
348
  
349
  $cnt = -1;
350
  
351
  $form['cdm_dataportal_rules'] = array(
352
      '#type' => 'fieldset',
353
      '#tree' => true,
354
    );
355
    
356
  foreach($rules  as $rule){
357
    $cnt++;
358
    
359
    $form['cdm_dataportal_rules'][$cnt] = array(
360
      '#type' => 'fieldset',
361
      '#title' => ($rule['type'] == 'empty' ? t('New Rule') : t('Rule '.$cnt)),
362
      '#collapsible' => TRUE,
363
      '#collapsed' => TRUE,
364
      '#tree' => TRUE,
365
    );
366
    
367
    $form['cdm_dataportal_rules'][$cnt]['type'] =  array(
368
      '#type'  => 'select',
369
      '#title' => t('Type'),
370
      '#default_value' => $rule['type'],
371
      '#options' => array(
372
          'preview' => t('Preview Quality'),
373
          'webapp' => t('Web Application'),
374
          ),
375
      '#description'   => t('The type media representation.'),
376
    );
377
    
378
   $form['cdm_dataportal_rules'][$cnt]['pattern'] =  array(
379
    '#type' => 'textarea',
380
    '#title'         => t('Pattern'),
381
    '#description'   => t('php PCRE regex pattern (see: ').l('php manual', 'http://www.php.net/manual/reference.pcre.pattern.syntax.php').')',
382
    '#default_value' => $rule['pattern'],
383
    '#rows' => 1
384
  );
385
  
386
  $form['cdm_dataportal_rules'][$cnt]['replace'] =  array(
387
    '#type' => 'textarea',
388
    '#title'         => t('Replacement'),
389
    '#default_value' => $rule['replace'],
390
    '#rows' => 1,
391
   );
392
  
393
  $form['cdm_dataportal_rules'][$cnt]['url_encode'] =  array(
394
    '#type' => 'checkbox',
395
    '#title'         => t('Url Encode'),
396
    '#default_value' => $rule['url_encode'],
397
    '#description'   => t('Whether the converted url is to be url encoded')
398
   );
399
  
400
  //  ['embed_html'] = 1;
401
  
402
  $form['cdm_dataportal_rules'][$cnt]['embed_html'] =  array(
403
    '#type' => 'checkbox',
404
    '#title'         => t('Embed'),
405
    '#default_value' => $rule['embed_html'],
406
    '#description'   => t('Embed the construct of prefix + url + affix into the page as plain html')
407
   );
408
  
409
   
410
  $form['cdm_dataportal_rules'][$cnt]['prefix'] =  array(
411
    '#type' => 'textarea',
412
    '#title'   => t('Prefix'),
413
    '#default_value' => $rule['prefix'],
414
    '#rows' => 1,
415
  );
416
  
417
    $form['cdm_dataportal_rules'][$cnt]['affix'] =  array(
418
    '#type' => 'textarea',
419
    '#title'   => t('Affix'),
420
    '#default_value' => $rule['affix'],
421
    '#rows' => 1,
422
  );
423
  
424
   $form['cdm_dataportal_rules'][$cnt]['size_x'] =  array(
425
    '#type' => 'textfield',
426
    '#title'         => t('Preview Image Width'),
427
    '#default_value' => $rule['size_x'],
428
    '#disabled' => $rule['type'] != 'preview',
429
    '#description'   => t('Width of preview image in px'),
430
    '#size' => 4, 
431
  );
432
  
433
  $form['cdm_dataportal_rules'][$cnt]['size_y'] =  array(
434
    '#type' => 'textfield',
435
    '#title'         => t('Preview Image Height'),
436
    '#default_value' => $rule['size_y'],
437
    '#disabled' => $rule['type'] != 'preview',
438
    '#description'   => t('Height of preview image in px'),
439
    '#size' => 4, 
440
  );
441
    
442
  }
443
  
444
  return system_settings_form($form);
445
}
446

    
447
/**
448
 * Displays a list of the known taxonomic names. Long lists are split up into multiple pages
449
 *
450
 * TODO: parameters are still preliminar
451
 * @param String $page page number to diplay defaults to page 1
452
 * @param boolean $hide_unaccepted whether to hide nams which are not accepted by the current view
453
 */
454
function cdm_dataportal_view_names($beginsWith = 'A', $page = 1, $onlyAccepted = false ){
455

    
456
  $request_params  = array(
457
    'q' => $beginsWith, 
458
         //'sec' = '',
459
         //'higherTaxa' => getFiters(),
460
          // 'matchAnywhere' => false, // default is false
461
    'page' => $page, 
462
    'onlyAccepted' => $onlyAccepted, 
463
    'pagesize' => 20  /*$_SESSION['cdm']['namelist_pagesize'] */);
464
  
465
  $taxonSTOs = cdm_ws_get(CDM_WS_FIND_TAXA, $request_params);
466
  /*
467
   * FIXME the filter for accepted names will be a form element, thus this widget
468
   * should be generated via form api preferably as block
469
   */
470
  //$out  = theme('cdm_dataportal_widget_filter_accepted', $onlyAccepted);
471
  //$out .= theme('cdm_dataportal_widget_names_list', $names, $page);
472
  $out .= theme('cdm_listof_taxa', $taxonSTOs);
473
  return $out;
474
}
475

    
476

    
477
/**
478
 * The taxon page gives detailed information on a taxon, it shows:
479
 *  - Taxon name
480
 *  - Full list of synonyms homotypic synonyms on top, followed by the
481
 *    heterotypic and finally followed by misapplied names.
482
 *    The list is ordered historically.
483
 *  - All facts associated with the very taxon concept and taxon name.
484
 *
485
 * @param String $uuid the UUID of the taxon
486
 */
487
function cdm_dataportal_view_taxon($uuid, $arg2 = null){
488

    
489

    
490
  if(isset($arg2) && !is_numeric($arg2)){
491
   
492
    switch ($args[0]){
493
      case 'alternatives':
494
        $alternatives = cdm_ws_get(CDM_WS_TAXON_ALTERNATIVES, $uuid);
495
        return theme('cdm_alternative_taxa', $alternatives);
496
    }
497
    
498
  } else {
499
  
500
    // display the page for the taxon defined by $uuid
501
    $taxonTO = cdm_ws_get(CDM_WS_TAXON ,$uuid);
502
  
503
    _cdm_dataportal_set_currentSecUuid($taxonTO->secUuid);
504
  
505
    drupal_set_title(theme('cdm_taxon', $taxonTO, true));
506
    //TODO retrieve complete synonymy and other data
507
  
508
    $out = theme('cdm_taxon_page', $taxonTO);
509
    return $out;
510
  }
511
}
512

    
513

    
514

    
515
/**
516
 * Expected URL request parameters:
517
 *  - text
518
 *  - language
519
 *  - vernacular
520
 * 
521
 * future extensions to meet palmweb mockup requirements:
522
 *  - habitat
523
 *  - uses
524
 *  - conservation status
525
 *  - locality / tdwg region
526
 */
527
function cdm_dataportal_view_search_taxon(){
528
  
529
  $request_params = cdm_dataportal_search_form_request();
530
  $sw_params = array_replace_key($request_params, array('queryString'=> 10 ));
531
  //FIXME remove debug code:
532
  $sw_params['page'] = '1'; // DEBUG code
533
  $resultPageSTO = cdm_ws_get(CDM_WS_FIND_TAXA, $sw_params);
534
  $resultPageSTO->pageNumber = $request_params['page'];
535
  return theme('cdm_search_results', $resultPageSTO, 'cdm_dataportal/search/taxon', $request_params);
536
}
537

    
538
/**
539
 * filters on children override already set parent filters and vice verca
540
 *
541
 * @param unknown_type $op
542
 * @param unknown_type $taxonUuid
543
 * @return unknown
544
 */
545
function cdm_dataportal_view_filter($op, $taxonUuid = null){
546
  
547
  if(!isset($_SESSION['cdm']['filters'])){
548
    $_SESSION['cdm']['filters'] = array();
549
  }
550
  if($taxonUuid || $op == 'list'){
551
    switch($op){
552
      case 'add':
553
        $parents = cdm_ws_get(CDM_WS_TREENODE_PARENTS, $taxonUuid);
554
        // pop off last element since this is the TreeNode object for $taxonUuid!
555
        $this_node = array_pop($parents);
556
        // will contain the uuid of the parent nodes excluding the $taxonUuid node itself
557
        $parent_uuids = array();
558
        
559
        // children override parents rule: remove all parent filters, 
560
        foreach($parents as $pnode){
561
          unset($_SESSION['cdm']['filters'][$pnode->uuid]);
562
          $parent_uuids[] = $pnode->uuid;
563
        }
564
        
565
        // search for potential children of this $taxonUuid
566
        foreach($_SESSION['cdm']['filters'] as $uuid=>$node){
567
          if(in_array($taxonUuid, $node->parentUuids)){
568
            unset($_SESSION['cdm']['filters'][$node->uuid]);
569
          }
570
        }
571
        // finally add this $taxonUuid as new filter
572
        $this_node->parentUuids = $parent_uuids;
573
        $_SESSION['cdm']['filters'][$taxonUuid] = $this_node;
574
        break;
575
      case 'remove':
576
        unset($_SESSION['cdm']['filters'][$taxonUuid]);
577
        break;
578
      case 'list':
579
        //TODO put in theme!!!
580
        $out = '<ul>';
581
        foreach($_SESSION['cdm']['filters'] as $uuid=>$node){
582
          $out .= '<li>'.cdm_dataportal_shortname_of($node).' '.l('[x]', 'cdm_dataportal/filter/remove/'.$uuid, array(), drupal_get_destination()).'</li>';
583
        }
584
        $out .= '</ul>';
585
        return $out;
586
    }
587
  }
588
  if($_REQUEST['destination']){
589
    $destination = $_REQUEST['destination'];
590
    unset($_REQUEST['destination']);
591
    drupal_goto($destination);
592
  }
593
}
594

    
595

    
596
function cdm_view_xml2json(){
597
  $file = arg(2);
598
  $datastr = get_content(variable_get('cdm_webservice_url', '').$file);
599
  return  xml2json::transformXmlStringToJson($datastr);
600

    
601
}
602

    
603
/**
604
 * Get a term for the current locale from the $localised_terms array.
605
 * Uses global variable $locale from drupal (see drupal functions locale_initialize() for details)
606
 * Function name is adapted from the drupal function t().
607
 *
608
 */
609
function cdm_dataportal_t($localised_terms){
610
  global $locale;  // drupal variable containing the current locale
611
  return cdm_get_localised_term($localised_terms, $locale);
612
}
613

    
614
/* ====================== other functions ====================== */
615

    
616
/**
617
 * Enter description here...
618
 *
619
 * @param String $uuid the UUID of the taxon
620
 * @return the URL
621
 */
622
function cdm_dataportal_taxon_path($uuid){
623
  return 'cdm_dataportal/taxon/'.$uuid;
624
}
625

    
626
/**
627
 * Creates a short taxonname by using the taggename field of NameSTO or NameTO instances.
628
 * If the taggename if empty the fullname will be returned.
629
 *
630
 * @param unknown_type $NameSTO a NameSTO, TreeNode or NameTO instance
631
 * @return string
632
 */
633
function cdm_dataportal_shortname_of($NameSTO){
634

    
635
  $name = trim(cdm_taggedtext_value($NameSTO->taggedName, 'name'));
636
  if($name){
637
    if( $pos = stripos($name, ' ')){
638
      return substr($name, 0, 1).'. '.substr($name, $pos);
639
    } else {
640
      return $name;
641
    }
642
  } else {
643
    return $NameSTO->fullname;
644
  }
645
}
646

    
647
/**
648
 * Sets the secUuid of the $taxonTO parameter to the current secUuid which is stored in the users session
649
 *
650
 * @param TaxonTO $taxonTO
651
 */
652
function _cdm_dataportal_set_currentSecUuid($secUuid){
653

    
654
  if( !isset($_SESSION['cdm']['currentSecRef']['uuid']) ||  $_SESSION['cdm']['currentSecRef']['uuid'] != $secUuid){
655
    $secRef = cdm_ws_get(CDM_WS_REFERENCE ,$secUuid);
656
    $_SESSION['cdm']['currentSecRef'] = (array)$secRef;
657
  }
658
}
659

    
660
/**
661
 * returns the current secRef array from the users session. 
662
 * If the according session variable is not jet set the default
663
 * as configured in the setting is used.
664
 * 
665
 * currentSecRef['uuid']
666
 * currentSecRef[....
667
 *
668
 * @return array
669
 */
670
function _cdm_dataportal_currentSecRef_array(){
671
  
672
  if( !isset($_SESSION['cdm']['currentSecRef']['uuid'])){
673
     $secUuid = variable_get('cdm_secUuid_default', false);
674
     _cdm_dataportal_set_currentSecUuid($secUuid);
675
  }
676
  return  $_SESSION['cdm']['currentSecRef'];
677
}
678

    
679
function cdm_dataportal_filters_areset(){
680
 return isset($_SESSION['cdm']['filters']) && count($_SESSION['cdm']['filters']) > 0;
681
}
682

    
683
/**
684
 * @return a reference on the filters array stored in the SESSION
685
 */
686
function &cdm_dataportal_filters_get(){
687
  if(!isset($_SESSION['cdm']['filters'])){
688
    $_SESSION['cdm']['filters'] = array();
689
  }
690
 return $_SESSION['cdm']['filters'];
691
}
692

    
693

    
694
function cdm_dataportal_mediaUri_converion_rules(){
695
  /*
696
   * rules
697
   * 
698
   *  ['type']: values(preview, webapp, media)
699
   *  ['size_x'], ['size_y']: dimensions of preview image in px
700
   *  ['pattern']: php PCRE regex pattern (see: http://www.php.net/manual/reference.pcre.pattern.syntax.php)
701
   *  ['replace']: replacement string 
702
   *  ['prefix']: string by which the converted url is to be prefixed
703
   *  ['affix']: string by which the converted url is to be affixed
704
   *  ['url_encode']: values(TRUE, FALSE) if the converted url to be url encoded
705
   *  ['embed_html']: embes the construct of prefix + url + affix into the page as plain html
706
   * 
707
   */
708
  // default rules
709
  /*
710
  $rule1 = array();
711
  $rule1['type'] = 'preview';
712
  $rule1['pattern'] = '/^(http:\/\/ww2.bgbm.org\/herbarium\/images(?:\/\w+)(?:\/\d+){4})(\/)(.*)$/';
713
  $rule1['replace'] = '$1/thumbs/$3';
714
  $rule1['size_x'] = 125;
715
  $rule1['size_Y'] = 200;
716
  
717
  $rule2 = array();
718
  $rule2['type'] = 'webapp';
719
  $rule2['pattern'] = '/^http:\/\/ww2.bgbm.org\/herbarium\/images\/((?:\w+\/)(?:\d+\/){4}[\w_]+?\.)(.*)$/';
720
  $rule2['replace'] = '$1fpx';
721
  $rule2['prefix'] = '<embed width="490" height="700" align="middle" type="application/x-shockwave-flash" 
722
       pluginspage="http://www.macromedia.com/go/getflashplayer" name="FSIViewer" 
723
       bgcolor="black" quality="high" play="true" 
724
       src="http://ww2.bgbm.org/fsi/fsi.swf?cfg=plugin2&FPXSrc='; 
725
  $rule2['affix'] = '&FPXWidth=2801&FPXHeight=4478&Effects=qlt%3D85&InitialView=&Measure_ImageWidth=270.0&Measure_Suffix=%20mm"/>';
726
  $rule2['url_encode'] = 1; 
727
  $rule2['embed_html'] = 1;
728
  return array($rule1, $rule2);
729
  */
730
  
731
  return variable_get('cdm_dataportal_rules', array());
732
  
733
  
734
}
735

    
736
/**
737
 * Converts a media uri according to all matching conversion rules which are currently defined.
738
 * 
739
 * Format of a returned array element:
740
 * 
741
 *  $return_array[{type}] { // values for {type}:(preview, webapp, media, original)
742
 *      ['size_x'],         // dimensions of preview image in px
743
 *      ['size_y'],         // dimensions of preview image in px
744
 *      ['uri']             //the uri 
745
 *  }
746
 * 
747
 * @return returns an associative array of the original media uri and all convered 
748
 *    whereas the rule type becomes the array key of the array entries.
749
 *    All uris are again packed into associative arrays by means to submit 
750
 *    additional information as defined in the rules applied. 
751
 */
752
function cdm_dataportal_mediaUri_conversion($mediaUri){
753
  
754
  $rules = cdm_dataportal_mediaUri_converion_rules();
755
  
756
  $muris = array();
757
  $muris['original'] = array('uri' => $mediaUri, 'size_x'=>null, 'size_y'=>null);
758
  
759
  foreach($rules as $rule){
760
      $uri_converted = preg_replace($rule['pattern'], $rule['replace'], $mediaUri);
761
      if($uri_converted != $mediaUri){
762
        if(isset($rule['url_encode']) && $rule['url_encode']){
763
          $uri_converted = urlencode($uri_converted);
764
        }
765
        $muris[$rule['type']] = array('uri'=>$uri_converted, 'size_x'=>$rule['size_x'], 'size_y'=>$rule['size_y']);
766
      }
767
  }
768
  return $muris;
769
}
770

    
771
/**
772
 * @param $str the string to truncate
773
 * @param $len the maximun length
774
 * @param $appendix an optional appendix.
775
 *
776
 * @return the string truncated to the specified length or the original string as given as parameter.
777
 * if an appendix has been defined the resulting string
778
 * will have the specified length inculding the the appendix.
779
 */
780
function str_trunk(&$str, $len, $appendix=''){
781
  if(strlen($str) >= $len )
782
  return  substr($str, 0, $len - strlen($appendix)).$appendix;
783
  else
784
  return $str;
785
}
786

    
787
/**
788
 * @param string $str
789
 * @param string $sub
790
 * @return boolean
791
 */
792
function str_beginsWith( $str, $sub ) {
793
  return ( substr( $str, 0, strlen( $sub ) ) === $sub );
794
}
795

    
796
/**
797
 *
798
 * @param string $str
799
 * @param string $sub
800
 * @return boolean
801
 */
802
function str_endsWith( $str, $sub ) {
803
  return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
804
}
805

    
806

    
807
function array_replace_key($array, $replace_map){
808
  foreach($replace_map as $key=>$newkey){
809
    if(isset($array[$key])){
810
      $array[$newkey] = $array[$key];
811
      unset($array[$key]);
812
    }
813
  }
814
  return $array;
815
}
816

    
817

    
818
function compose_url_prameterstr($parameters = array()){
819
  $pstr = '';
820
  foreach($parameters as $key=>$value){
821
     $pstr .= ($pstr ? '&' :'').$key.'='.urlencode($value);
822
  }
823
  return $pstr;
824
}
(4-4/8)