Project

General

Profile

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

    
4
/*
5
 * @file
6
 * cdm_taxontree.module
7
 *
8
 * Copyright (C) 2007 EDIT
9
 * European Distributed Institute of Taxonomy
10
 * http://www.e-taxonomy.eu
11
 */
12

    
13
/**
14
 * Implementation of hook_menu()
15
 */
16
function cdm_taxontree_menu($may_cache) {
17

    
18
  $items = array();
19
  if ($may_cache) {
20

    
21
    $items[] = array(
22
      'path' => 'cdm_taxontree/set',
23
      'callback' => 'cdm_taxontree_set',
24
      'access' => true,
25
      'type' => MENU_CALLBACK, 
26
    );
27
    
28
     $items[] = array(
29

    
30
      'path' => 'cdm_taxontree/filter', 
31
      'callback' => 'cdm_taxontree_view_filter',
32
      'access' => true,
33
      'type' => MENU_CALLBACK, 
34
    );
35
    
36
    $items[] = array(
37
      'path' => 'cdm_taxontree/taxonomy/children', 
38
      'callback' => 'cdm_taxontree_taxonomy_children',
39
      'access' => true,
40
      'type' => MENU_CALLBACK, 
41
    );
42

    
43
  }
44
  return $items;
45
}
46

    
47

    
48
/**
49
 * Implementation of hook_block()
50
 *
51
 * @param String $op
52
 * @param int $delta
53
 */
54
function cdm_taxontree_block($op='list', $delta=0, $edit = array()) {
55
  if ($op == "list") {
56
    $block['cdm_tree']["info"] = t('CDM Taxon Tree');
57
    $block['filters']["info"] = t('Active Filters');
58
    $block[1]["info"] = t('Dupal Taxonomy Tree');
59
    return $block;
60
  }
61
  else if ($op == 'view') {
62
    switch($delta){
63
      case 'cdm_tree':
64
        $block['subject'] = t('CDM Taxon Tree');
65
        $taxonUuid_inFocus = _cdm_get_taxonuuid();
66
        $tree = cdm_taxontree_build_tree($taxonUuid_inFocus);
67
        $block['content'] = theme('cdm_taxontree_block', $tree, TRUE, TRUE, 'cdm_taxontree_node_concept_switch');
68
        theme('cdm_taxontree_add_scripts');
69
        drupal_add_js('
70
        if (Drupal.jsEnabled) {
71
        $(document).ready(function() 
72
        {
73
          $(\'ul.cdm_taxontree\').cdm_taxontree();
74
          //var activeElement = $(\'div.cdm_taxontree_scroller_x .active\');
75
          $(\'div.cdm_taxontree_scroller_x\').scrollTo($(\'.active\'));});
76
        }', 'inline');
77
         return $block;
78
        
79
      case 'filters':
80
        $block['subject'] = t('Active Filters');
81
        $block['content'] = cdm_taxontree_view_filter('list');
82
        return $block;
83
        
84
      case 1:
85
        $block['subject'] = t('Dupal Taxonomy Tree ');
86
        $term_inFocus = arg(0) == 'taxonomy' && arg(1) == 'term' ? arg(2) : 0;
87
        $tree = cdm_taxontree_build_tree($term_inFocus, true, variable_get('cdm_taxontree_block_1_vid', 0));
88
        $block['content'] = theme('cdm_taxontree_block', $tree);
89
        theme('cdm_taxontree_add_scripts');
90
        drupal_add_js('
91
        if (Drupal.jsEnabled) {
92
        $(document).ready(function() 
93
        {
94
          $(\'ul.cdm_taxontree\').cdm_taxontree();
95
          //var activeElement = $(\'div.cdm_taxontree_scroller_x .active\');
96
          $(\'div.cdm_taxontree_scroller_x\').scrollTo($(\'.active\'));});
97
        }', 'inline');
98

    
99
        return $block;
100
      }
101
    } else if ($op == 'configure') {
102
      switch($delta){
103
        case 1:
104
          $vocs = taxonomy_get_vocabularies();
105
          $options = array();
106
          foreach($vocs as $voc){
107
            $options[$voc->vid] = $voc->name;
108
          }
109
          $form['vid'] = array(
110
            '#type' => 'select',
111
            '#title' => t('Category'),
112
            '#default_value' => variable_get('cdm_taxontree_block_1_vid', 0),
113
            '#options' => $options,
114
          );
115
          return $form;
116
      }
117
    } else if ($op == 'save') {
118
      switch($delta){
119
        case 1:
120
         variable_set('cdm_taxontree_block_1_vid', $edit['vid']);
121
         return;
122
      }
123
    }
124
}
125

    
126
function cdm_taxontree_set($key, $value){
127

    
128
  if(is_string($key)){
129
    $_SESSION['cdm']['taxontree'][$key] = $value;
130
  }
131

    
132
  if($_REQUEST['destination']){
133
    $destination = $_REQUEST['destination'];
134
    unset($_REQUEST['destination']);
135
    drupal_goto($destination);
136
  }
137
}
138

    
139
function _get_compact_mode(){
140

    
141
  if(!isset($_SESSION['cdm']['taxontree']['compact_mode'])){
142
    $_SESSION['cdm']['taxontree']['compact_mode'] = 'expanded';
143
  }
144
  return  $_SESSION['cdm']['taxontree']['compact_mode'];
145
}
146

    
147

    
148
function _is_uuid($str){
149
  return strlen($str) == 36 && strpos($str, '-');
150
}
151

    
152
/**
153
 * Converts an array of drupal taxonomy terms into an 
154
 * array of partially instantiated cdm tree nodes by adding the fields 
155
 * relevant for tree node processing in cdm_taxontree
156
 * 
157
 * 
158
 * term       =>      cdm tree node
159
 * ------------------------------------
160
 * tid       ->     uuid
161
   name      ->     fullname
162
                    taggedName 
163
                    secUuid
164
                    isAccepted
165
                    hasChildren
166
                    alternativeConceptRefs
167
 *
168
 * @param unknown_type $terms
169
 */
170
function cdm_taxontree_terms2treenodes(&$terms){
171
  foreach($terms as &$term){
172
    $term->uuid = $term->tid;
173
    $term->fullname = $term->name;
174
    $term->hasChildren = count(taxonomy_get_children($term->tid, $term->vid));
175
  }
176
  return $terms;
177
}
178

    
179
function cdm_taxontree_taxonomy_children($tid, $vid, $theme){
180
  $args = func_get_args();
181
  $tid = array_shift($args);
182
  $vid = array_shift($args);
183
  $theme = array_shift($args);
184
  
185
  $children = cdm_taxontree_get_children($tid, $vid);
186
  $children = cdm_taxontree_terms2treenodes($children);
187
  array_unshift($args, $theme, $children);
188
  print call_user_func_array('theme', $args);
189
}
190

    
191
function cdm_taxontree_get_root($vid = null, $secRefUuuid = null){
192
  if(is_numeric($vid)){
193
    // vid, $parent = 0, $depth = -1, $max_depth = NULL) {
194
    $terms = taxonomy_get_tree($vid, 0, -1, 1);
195
    return cdm_taxontree_terms2treenodes($terms);
196
  } else {
197
    return cdm_ws_get(CDM_WS_TREENODE_ROOT, $secRefUuuid);
198
  }
199
}
200

    
201
function cdm_taxontree_get_children($uuid, $vid = null){
202

    
203
  if(is_numeric($vid)){
204
    $terms = taxonomy_get_children($uuid, $vid);
205
    return cdm_taxontree_terms2treenodes($terms);
206
  } else {
207
    return cdm_ws_get(CDM_WS_TREENODE_CHILDREN, $uuid);
208
  }
209
}
210

    
211
function cdm_taxontree_get_parents($uuid){
212

    
213
  if(!_is_uuid($uuid)){
214
    $terms = taxonomy_get_parents($uuid);
215
    array_push($terms, taxonomy_get_term($uuid));
216
    $terms = array_reverse($terms);
217
    return cdm_taxontree_terms2treenodes($terms);
218
  } else {
219
    return cdm_ws_get(CDM_WS_TREENODE_PARENTS, $uuid);
220
  }
221
}
222

    
223
/**
224
 * builds a tree of TaxonNode instances, whereas the instances are extended by some fields:
225
 *
226
 *  - $node->filter: values ( 'on', 'excluded', 'included' )
227
 *  - $node->expanded: values ( 'expanded', 'collapsed' )
228
 *    $node->focused: values ( TRUE, FALSE )
229
 *
230
 * @param unknown_type $taxonUuid
231
 * @return unknown
232
 */
233
function cdm_taxontree_build_tree($taxonUuid = null, $hideOtherConcepts = true, $vid = null){
234

    
235
  // find the secRefUuid
236
  $secRefUuid = null;
237
  if(is_null($vid)){
238
    if($taxonUuid){
239
      //TODO poor performance here:
240
      $taxon =  cdm_ws_get(CDM_WS_TAXON, $taxonUuid);
241
      $secRefUuid = $taxon->sec->uuid;
242
    }
243
  
244
    if(!$secRefUuid){
245
      $secRefUuid = variable_get('cdm_secUuid_default',false);
246
    }
247
    
248
    $compact_tree = cdm_taxontree_filters_active() && _get_compact_mode() != 'expanded';
249
  }
250
  // compact_modes: 'expanded', 'compact', 'flattened'
251

    
252
  // get the root level
253
  $root_tree = cdm_taxontree_get_root($vid, ($hideOtherConcepts ? $secRefUuid : null));
254
  $root_tree = _cdm_resultset2nodelist($root_tree, cdm_taxontree_filters_active());
255

    
256
  if(cdm_taxontree_filters_active()){
257
    // the paths up to active filters are inactive in the user interface and
258
    // thus cannot be browsed by expanding nodes
259
    // therefore we need to build up the branches for all nodes which are set as filters
260
    // the branches are merged with the root
261
    foreach(cdm_taxontree_filters_get() as $uuid=>$filter){
262
      $branch = cdm_taxontree_build_path($uuid, TRUE, ($compact_tree === false ? true :null));
263
      $root_tree = _cdm_taxontree_merge($root_tree, $branch);
264
    }
265
  }
266

    
267
  // build the the branch for the focused node and merge it with the root
268
  if($taxonUuid){
269
    $branch = cdm_taxontree_build_path($taxonUuid, NULL, (cdm_taxontree_filters_active() ? NULL : TRUE), TRUE);
270
    $root_tree = _cdm_taxontree_merge($root_tree, $branch);
271
  }
272

    
273
  //reorder siblings & populate expanded nodes with children and propagate the filter attribute
274
  $root_tree = cdm_taxontree_populate($root_tree, $compact_tree === false);
275

    
276
  // flatten tree
277
  if($compact_tree){
278
    if( _get_compact_mode() == 'flattened'){
279
      $root_tree = cdm_taxontree_flatten($root_tree);
280
    } else if(_get_compact_mode() == 'compact') {
281
      foreach($root_tree as $uuid => $node){
282
        if( $node->filter == 'excluded' && !$node->children){
283
          unset($root_tree[$uuid]);
284
        }
285
      }
286
    }
287
  }
288

    
289
  return $root_tree;
290
}
291

    
292
/**
293
 * Builds the specific branch path for $taxonUuid.
294
 * The branch path reaches from the parent root node of $taxonUuid up to $taxonUuid.
295
 *
296
 * @param unknown_type $taxonUuid
297
 * @param $is_filter_path whether the upmost node of this path is mapped by an active filter
298
 * @param $is_expanded whether all nodes along the tree are expanded
299
 * @param $is_focused whether to upper most element of this branch is set as filter
300
 * @return a subtree
301
 */
302
function cdm_taxontree_build_path($taxonUuid, $is_filter_path = null, $is_expanded = null, $is_focused = FALSE){
303

    
304
  $branch_path = array();
305
  
306
  $parents = cdm_taxontree_get_parents($taxonUuid);
307
  if(!$parents){
308
    if($is_filter_path){
309
      // remove invalid filter
310
      cdm_taxontree_filters_remove($taxonUuid);
311
    }
312
    return false;
313
  }
314

    
315
  $parents = _cdm_resultset2nodelist($parents, NULL);
316
  $lastParent = null;
317
  foreach($parents as $pnode){
318
    $pnode->focused = false;
319
    if($lastParent){
320
      $pnode->children = array($lastParent->uuid => $lastParent);
321
      if(!is_null($is_filter_path)){
322
        $pnode->filter = ($is_filter_path ? 'excludes' : 'included');
323
      }
324
      if(!is_null($is_expanded)){
325
        $pnode->expanded = ($is_expanded ? 'expanded' : 'collapsed');
326
      }
327
    } else {
328
      // the uppermost node of branch
329
      if(!is_null($is_filter_path)){
330
        $pnode->filter = ($is_filter_path ? 'on' : 'includes');
331
      }
332
      // uppermost node is always expanded if it has children
333
      $pnode->focused = $is_focused;
334
      $pnode->expanded = ($pnode->hasChildren ? 'expanded' : 'collapsed');
335
    }
336
    $lastParent = $pnode;
337
  }
338
  $branch_path[$pnode->uuid] = $pnode;
339
  return $branch_path;
340
}
341

    
342
/**
343
 * Performs two steps on each level of the tree:
344
 *  1. reorder siblings except root (is expected to be ordered jet) alphabetically
345
 *  2. populate children of expanded nodes  & propagate the filter attribute
346
 *
347
 * @param unknown_type $tree
348
 * @return unknown
349
 */
350
function cdm_taxontree_populate($tree, $expand_excluded, $filter_default = null){
351

    
352
  if(!is_array($tree)){
353
    return false;
354
  }
355
  foreach(array_keys($tree) as $uuid){
356

    
357
    if(!isset($tree[$uuid]->filter) && !is_null($filter_default)){
358
      $tree[$uuid]->filter = $filter_default;
359
    }
360

    
361
    if( $tree[$uuid]->expanded == 'expanded' && ($expand_excluded || $tree[$uuid]->filter != 'excluded')){
362
      $children = cdm_taxontree_get_children($uuid, $tree[$uuid]->vid);
363
      //$children = cdm_ws_get(CDM_WS_TREENODE_CHILDREN, $uuid);
364
      $children = _cdm_resultset2nodelist($children, ($tree[$uuid]->filter == 'excludes'));
365

    
366
      // store the children of the node for later processing
367
      if(is_array($tree[$uuid]->children)){
368
        $pnode_children = $tree[$uuid]->children;
369
      } else {
370
        $pnode_children = false;
371
      }
372
      // replace the children by the newly retrieved child nodes
373
      $tree[$uuid]->children = $children;
374
       
375
      if($pnode_children){
376
        // recurse into the childtree which was stored before
377
        $pnode_children =  cdm_taxontree_populate($pnode_children, $expand_excluded ,$tree[$uuid]->filter);
378
        // recombine
379
        foreach($pnode_children as $childUuid=>$cnode){
380
          $tree[$uuid]->children[$childUuid] = $cnode;
381
        }
382
      }
383
    } else {
384
      // reorder nodes which are not expanded, expanded nodes are reordered implicitly above
385
      if(isset($tree[$uuid]->children) && count($tree[$uuid]->children) > 1) {
386
        // copy the children into an array which can be sorted by its keys
387
        $ordered = array();
388
        foreach($tree[$uuid]->children as $cnode){
389
          // concatenate full name and uid
390
          $reordered[str_pad($cnode->fullname, 255, '-').$cnode->uuid] = $cnode;
391
        }
392
        // sort
393
        ksort($reordered);
394
        // move the children back into the parent node
395
        $tree[$uuid]->children = array();
396
        foreach($reordered as $cnode){
397
          $tree[$uuid]->children[$cnode->uuid] = $cnode;
398
        }
399
      }
400
      $tree[$uuid]->children = cdm_taxontree_populate($tree[$uuid]->children, $expand_excluded, $tree[$uuid]->filter);
401
    }
402
  }
403
  return $tree;
404
}
405

    
406
function cdm_taxontree_flatten($tree, &$new_root = null){
407
  if(!$new_root){
408
    $new_root = array();
409
  }
410
  foreach($tree as $node){
411
    if($node->filter == 'on'){
412
      $new_root[$node->uuid] = $node;
413
    } else if(is_array($node->children)){
414
      cdm_taxontree_flatten($node->children, $new_root);
415
    }
416
  }
417
  return $new_root;
418
}
419

    
420

    
421
/**
422
 * Merge a branch into a tree whereas the tree dominated the branch except
423
 * nodes having property filter set to "on". These always dominate
424
 * nevertheless if they are in tree or branch.
425
 *
426
 * @param unknown_type $tree the dominant tree
427
 * @param unknown_type $branch the tree to be merged in
428
 * @return the merged $tree
429
 */
430
function _cdm_taxontree_merge($tree, $branch) {
431

    
432
  if(!$branch){
433
    return $tree;
434
  }
435

    
436
  foreach(array_keys($tree) as $uuid) {
437
    // check if node exists in $branch
438
    if(!empty($branch[$uuid])) {
439
      // preserve filter property
440
      if(isset($tree[$uuid]->filter) && !(isset($branch[$uuid]->filter) && $branch[$uuid]->filter == 'on') ){
441
        $branch[$uuid]->filter = $tree[$uuid]->filter;
442
      } else if(isset($branch[$uuid]->filter)){
443
        $tree[$uuid]->filter = $branch[$uuid]->filter;
444
      }
445
      // preserve expanded property
446
      if(isset($tree[$uuid]->expanded)){
447
        $branch[$uuid]->expanded = $tree[$uuid]->expanded;
448
      } else if(isset($branch[$uuid]->expanded)){
449
        $tree[$uuid]->expanded = $branch[$uuid]->expanded;
450
      }
451
      // $Uuid exists check if the node in tree1 or tree2 contains children
452
      if(is_array($branch[$uuid]->children) && is_array($tree[$uuid]->children)) {
453
        // merge recursive
454
        $tree[$uuid]->children = _cdm_taxontree_merge($tree[$uuid]->children, $branch[$uuid]->children);
455
      } else if(is_array($branch[$uuid]->children)){
456
        $tree[$uuid] =  $branch[$uuid];
457
      }
458
      unset($branch[$uuid]);
459
    }
460
  }
461
  // append remaining items from branch to tree
462
  foreach(array_keys($branch) as $uuid){
463
    $tree[$uuid] =  $branch[$uuid];
464
  }
465
  return $tree;
466
}
467

    
468

    
469
/**
470
 * Replaces the keys of an array of TreeNode instances
471
 * by the $treeNode->uuid of the single array elements.
472
 * An sets additional fields
473
 *
474
 * @param $resultset array of TreeNode instances as +returned by the cdm web service
475
 * @param $excluded  whether the $resultset is included by a active filter. Is ignored if NULL.
476
 * @param $expanded  whether the children of the nodes in the $resultset are expanded or not. Is ignored if NULL.
477
 */
478
function _cdm_resultset2nodelist($resultset, $excluded = null, $expanded = null){
479

    
480
  if(! is_array($resultset)) {
481
    return false;
482
  }
483

    
484
  $tree = array();
485
  foreach($resultset as $treeNode){
486
    if(!is_null($excluded)){
487
      $treeNode->filter = ($excluded ? 'excluded': 'included');
488
    }
489
    if(!is_null($expanded)){
490
      $treeNode->expanded = ($expanded ? 'expanded': 'collapsed');
491
    }
492
    $tree[$treeNode->uuid] = $treeNode;
493
  }
494
  return $tree;
495
}
496

    
497
function theme_cdm_taxontree_add_scripts(){
498
  $path_cdm_taxontree = drupal_get_path('module', 'cdm_taxontree');
499
  $path_preferred_module = drupal_get_path('module', 'cdm_dataportal') ? drupal_get_path('module', 'cdm_dataportal') : $path_cdm_taxontree;
500
  drupal_add_css($path_cdm_taxontree.'/cdm_taxontree.css');
501
  drupal_add_js($path_preferred_module.'/js/jquery.dimensions.js');
502
  drupal_add_js($path_cdm_taxontree.'/js/cdm_taxontree.js');
503
  drupal_add_js($path_cdm_taxontree.'/js/jquery.scrollTo.js');
504
}
505

    
506
/**
507
 * @param $tree the tree of TreeNode to be displayed
508
 * @param $magicbox if true, the tree will be embedded into a set of div tags which allow the 
509
 *            tree to expand and overlap other content. This is useful if the node titles are 
510
 *            quite long or if the tree is nested deeply.   
511
 * @param $show_filter_switch The tree can offer buttons to add a node to a set of filters
512
 *            which can then be applied to the tree to limit the visible subtrees and thus 
513
 *            to compact the tree. Three different compact modes are available.
514
 * @param $tree_node_callback name of a callback method which will be called for each node 
515
 *            in theme_cdm_taxontree_node(). The output of this callback, which takes the 
516
 *            $node object as single arument, is appended to the end of the redered node. 
517
 */
518
function theme_cdm_taxontree_block($tree, $magicbox = false, $show_filter_switch = false, $tree_node_callback = false){
519

    
520
  $out = '';
521
  if(cdm_taxontree_filters_active()){
522
    $out .= theme('cdm_taxontree_contoller', _get_compact_mode());
523
  }
524
  if($magicbox){
525
    $out .= '<div class="cdm_taxontree_scroller_x"><div class="cdm_taxontree_container"><div class="cdm_taxontree_scroller_y">';
526
  }
527
  $out .= theme('cdm_taxontree', $tree, !cdm_taxontree_filters_active(), $show_filter_switch , $tree_node_callback);
528
  if($magicbox){
529
    $out .= '</div></div></div>';
530
  }
531
  return $out;
532
}
533

    
534
function theme_cdm_taxontree_contoller($compact_mode){
535

    
536
  static $modes = array('expanded', 'compact', 'flattened');
537

    
538
  $out = '<div class="settings">';
539
  foreach($modes as $mode){
540
    if($compact_mode == $mode){
541
      $out .= t($mode);
542
    } else {
543
      $out .= l(t($mode), 'cdm_taxontree/set/compact_mode/'.$mode, array(), drupal_get_destination());
544
    }
545
    $out .= ' ';
546
  }
547

    
548
  return $out.'</div>';
549
}
550

    
551
function theme_cdm_taxontree($tree, $filterIncludes = null, $show_filter_switch = false, $tree_node_callback = false){
552

    
553
  if(!is_array($tree)) {
554
    return false;
555
  }
556

    
557
  if(is_null($filterIncludes)){
558
    // set $filterIncludes true if no filters are set.
559
    $filterIncludes = !cdm_taxontree_filters_active();
560
  }
561

    
562
  $out = '<ul class="cdm_taxontree">';
563
  foreach($tree as $node){
564
    $out .= theme('cdm_taxontree_node', $node, $filterIncludes, $show_filter_switch, $tree_node_callback);
565
  }
566
  $out .= '</ul>';
567
  return $out;
568
}
569

    
570
function theme_cdm_taxontree_node($node, $filterIncludes, $show_filter_switch = false, $tree_node_callback = false){
571

    
572
  $is_leaf = !$node->hasChildren || $node->hasChildren == 0;
573
  $is_expanded = isset($node->expanded) && $node->expanded = 'expanded';
574
  
575
  if($node->tid){
576
    $node_name = $node->name;
577
    $path = "taxonomy/term/".$node->tid;
578
    // disable filterswitch
579
    $show_filter_switch = false;
580
    
581
  } else {
582
    $node_name = cdm_dataportal_shortname_of($node);
583
    $path = cdm_dataportal_taxon_path($node->uuid);
584
  }
585

    
586
  if($filterIncludes){
587
    $name = l($node_name, $path);
588
    $filter_class = 'filter_included';
589
  } else {
590
    if($node->filter == 'on') {
591
      $name = l($node_name,  $path);
592
      $filter_class = 'filter_on';
593
    } else {
594
      $name .= $node_name;
595
      $filter_class = 'filter_excluded';
596
    }
597
  }
598
  $nextLevelIncluded = $node->filter == 'on' || $filterIncludes;
599

    
600
  $ahah_url = false;
601
  if(!$is_leaf && !$is_expanded && $filter_class != 'filter_excluded'){
602
    if($node->tid){
603
      $ahah_url = url('cdm_taxontree/taxonomy/children/'.$node->tid.'/'.$node->vid.'/cdm_taxontree/'.($nextLevelIncluded ? 1 : 0).'/'.($show_filter_switch ? 1 : 0).'/'.$tree_node_callback);      
604
    } else {
605
      $ws_url = cdm_compose_url(CDM_WS_TREENODE_CHILDREN, array($node->uuid));
606
      $ahah_url = url('cdm_api/proxy/'.urlencode($ws_url).'/cdm_taxontree/'.($nextLevelIncluded ? 1 : 0).'/'.($show_filter_switch ? 1 : 0).'/'.$tree_node_callback);      
607
    }
608
  }
609

    
610
  // list item
611
  $out = '<li class="'
612
  .($node->focused ? 'focused ' : '')
613
  .($is_leaf ? 'leaf ':($is_expanded ?'expanded ':'collapsed '))
614
  .$filter_class.'"'
615
  .($ahah_url ? 'title="'.$ahah_url.'"' : '')
616
  .'>';
617

    
618
  if($show_filter_switch){
619
    // filter icon
620
    $out .= theme('cdm_taxontree_node_filter_switch', $node, $filter_class);
621
  }
622

    
623
  // taxon name
624
  $out .= $name;
625

    
626
  // concept_switch or other theme callbacks
627
  if($tree_node_callback){
628
    $out .= theme($tree_node_callback, $node);
629
  }
630

    
631
  if($node->children && is_array($node->children)){
632
    $out .= theme('cdm_taxontree', $node->children, $nextLevelIncluded, $show_filter_switch, $tree_node_callback);
633
  }
634
  $out .= '</li>';
635

    
636
  return $out;
637
}
638

    
639
function theme_cdm_taxontree_node_filter_switch(&$node, $filter_class){
640
  $out = '';
641

    
642
  switch($filter_class){
643
    case 'filter_included':
644
      $filter_icon = 'visible_implicit_small.gif';
645
      break;
646
    case 'filter_excluded':
647
      $filter_icon = 'invisible_small.gif';
648
      break;
649
    case 'filter_on':
650
      $filter_icon = 'visible_small.gif';
651
      break;
652
  }
653

    
654
  $filter_op = $node->filter == 'on' ? 'remove' : 'add';
655

    
656
  $out .= '&nbsp;'
657
  .l('<img src="'.drupal_get_path('module', 'cdm_taxontree').'/'.$filter_icon.'" alt="[f]" />',
658
    'cdm_taxontree/filter/'.$filter_op.'/'.$node->uuid, array('class'=>'filter_'.$filter_op), 
659
    'destination='.cdm_dataportal_taxon_path($node->uuid), 
660
  null, false, true);
661
   
662
  return $out;
663
}
664

    
665
function theme_cdm_taxontree_node_concept_switch(&$node){
666
  $out = '';
667

    
668
  if(isset($node->alternativeConceptRefs[0])){
669
    $out = l(
670
      '<img src="'.drupal_get_path('module', 'cdm_taxontree').'/concept_switch.gif" alt="[-&gt;]" />', 
671
      'cdm_dataportal/taxon/alternative/'.$node->uuid, 
672
    array('rel'=>'cdm_dataportal/taxon/alternative/'.$node->uuid, 'class'=>'concept_switch'),
673
    null, null, false, true);
674
  }
675
  return $out;
676
}
677

    
678

    
679
function cdm_taxontree_secRefTitle_for($secUuid){
680
  
681
  $refSTO = cdm_api_secref_cache_get($secUuid);
682
  if($refSTO && isset($refSTO->fullCitation)){
683
    $cit = $refSTO->fullCitation;
684
  } else {
685
    $cit = '[no title for:'.$secUuid.']';
686
  }
687
  return $cit;
688
}
689

    
690

    
691
function theme_cdm_taxontree_node_reference(&$node){
692
  
693
  $secRefTitle = cdm_taxontree_secRefTitle_for($node->secUuid);
694
  $out = ' <span class="sec_ref widget_select" title="'.check_plain($secRefTitle).'" style="background-color:#'._uuid_to_rgbhex($node->secUuid).'" alt="'.$node->secUuid.'">'
695
      .check_plain($secRefTitle).'</span>';
696
  return $out;
697
}
698

    
699
// ----------------- filters -------------------------- // 
700

    
701
/**
702
 * filters on children override already set parent filters and vice verca
703
 *
704
 * @param unknown_type $op
705
 * @param unknown_type $taxonUuid
706
 * @return unknown
707
 */
708
function cdm_taxontree_view_filter($op, $taxonUuid = null){
709
  
710
  if(!isset($_SESSION['cdm']['filters'])){
711
    $_SESSION['cdm']['filters'] = array();
712
  }
713
  if($taxonUuid || $op == 'list'){
714
    switch($op){
715
      case 'add':
716
        cdm_taxontree_filters_add($taxonUuid);
717
        break;
718
      case 'remove':
719
        cdm_taxontree_filters_remove($taxonUuid);
720
        break;
721
      case 'list':
722
        //TODO put in cdm_dataportal_theme to decouple both modules by this!!!
723
        $out = '<ul>';
724
        foreach($_SESSION['cdm']['filters'] as $uuid=>$node){
725
          $out .= '<li>'.cdm_dataportal_shortname_of($node).' '.l('[x]', 'cdm_dataportal/filter/remove/'.$uuid, array(), drupal_get_destination()).'</li>';
726
        }
727
        $out .= '</ul>';
728
        return $out;
729
    }
730
  }
731
  if($_REQUEST['destination']){
732
    $destination = $_REQUEST['destination'];
733
    unset($_REQUEST['destination']);
734
    drupal_goto($destination);
735
  }
736
}
737

    
738
/**
739
 * filters are set in cdm_dataportal_view_filter()
740
 * functions using filters should remove invalid filters
741
 * @return true if any filter is active
742
 */
743
function cdm_taxontree_filters_active(){
744
 return isset($_SESSION['cdm']['filters']) && count($_SESSION['cdm']['filters']) > 0;
745
}
746

    
747
/**
748
 * filters are set in cdm_dataportal_view_filter()
749
 * @return a reference on the filters array stored in the SESSION
750
 */
751
function &cdm_taxontree_filters_get(){
752
  if(!isset($_SESSION['cdm']['filters'])){
753
    $_SESSION['cdm']['filters'] = array();
754
  }
755
 return $_SESSION['cdm']['filters'];
756
}
757

    
758

    
759
function cdm_taxontree_filters_add($taxonUuid){
760
  $parents = cdm_ws_get(CDM_WS_TREENODE_PARENTS, $taxonUuid);
761
  
762
  $parents = array_reverse($parents);
763
  
764
  // pop off last element since this is the TreeNode object for $taxonUuid!
765
  $this_node = array_pop($parents);
766
  // will contain the uuid of the parent nodes excluding the $taxonUuid node itself
767
  $parent_uuids = array();
768
  
769
  // children override parents rule: remove all parent filters, 
770
  foreach($parents as $pnode){
771
    unset($_SESSION['cdm']['filters'][$pnode->uuid]);
772
    $parent_uuids[] = $pnode->uuid;
773
  }
774
  
775
  // search for potential children of this $taxonUuid
776
  foreach($_SESSION['cdm']['filters'] as $uuid=>$node){
777
    if(in_array($taxonUuid, $node->parentUuids)){
778
      unset($_SESSION['cdm']['filters'][$node->uuid]);
779
    }
780
  }
781
  // finally add this $taxonUuid as new filter
782
  $this_node->parentUuids = $parent_uuids;
783
  $_SESSION['cdm']['filters'][$taxonUuid] = $this_node;
784
}
785

    
786

    
787
function cdm_taxontree_filters_remove($taxonUuid){
788
  unset($_SESSION['cdm']['filters'][$taxonUuid]);
789
}
790

    
791

    
792
// --------------------------------------------------- //
793
function _cdm_get_taxonuuid(){
794

    
795
  //TODO make the path configurable
796
  if (arg(0)=="cdm_dataportal" && arg(1)=="taxon" && arg(2)!==0){
797
    $taxon_uuid = arg(2);
798
  } else {
799
    $taxon_uuid = $_SESSION['cdm_dataportal']['tree']['taxon_uuid'];
800
  }
801

    
802
  return $taxon_uuid;
803
}
804

    
805
function _uuid_to_rgbhex($uuid){
806

    
807
  $xfoot = _str_crossfoot($uuid);
808
  $h = $xfoot / 255;
809
  $h = $h - floor($h);
810
  $RGB = _hsv_2_rgb($h, 0.45, 1);
811
  return dechex($RGB['R']).dechex($RGB['G']).dechex($RGB['B']);
812
}
813

    
814
function _str_crossfoot($str)
815
{
816
  $xfoot = 0;
817
  for($i=0; $i<strlen($str); $i++)
818
  {
819
    $xfoot = $xfoot + ord($str[$i]);
820
  }
821
  return $xfoot;
822
}
823

    
824

    
825
function _hsv_2_rgb($H, $S, $V) // HSV Values:Number 0-1
826
{ // RGB Results:Number 0-255
827
$RGB = array();
828

    
829
if($S == 0)
830
{
831
  $R = $G = $B = $V * 255;
832
}
833
else
834
{
835
  $var_H = $H * 6;
836
  $var_i = floor( $var_H );
837
  $var_1 = $V * ( 1 - $S );
838
  $var_2 = $V * ( 1 - $S * ( $var_H - $var_i ) );
839
  $var_3 = $V * ( 1 - $S * (1 - ( $var_H - $var_i ) ) );
840

    
841
  if ($var_i == 0) { $var_R = $V ; $var_G = $var_3 ; $var_B = $var_1 ; }
842
  else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $V ; $var_B = $var_1 ; }
843
  else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $V ; $var_B = $var_3 ; }
844
  else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $V ; }
845
  else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $V ; }
846
  else { $var_R = $V ; $var_G = $var_1 ; $var_B = $var_2 ; }
847

    
848
  $R = $var_R * 255;
849
  $G = $var_G * 255;
850
  $B = $var_B * 255;
851
}
852

    
853
$RGB['R'] = $R;
854
$RGB['G'] = $G;
855
$RGB['B'] = $B;
856

    
857
return $RGB;
858
}
(4-4/15)