Project

General

Profile

Download (25.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3

    
4
/*
5
 * Definition of privacy levels
6
 */
7
define('TAXONPRIVACY_PRIVATE', 'Private');
8
define('TAXONPRIVACY_PUBLIC', 'Public');
9

    
10
/**
11
 * Implementation of hook_perm()
12
 *
13
 * Valid permissions for this module
14
 * @return array An array of valid permissions for the taxon_experts module
15
 */
16
function cdm_taxontree_perm() {
17
    return array(
18
        'view private taxoninterests',
19
    //TODO which else permission are required?
20
    );
21
}
22

    
23

    
24
/**
25
 * Implementation of hook_field()
26
 *
27
 */
28
function cdm_taxontree_field($op, &$node, $field, &$items, $teaser, $page) {
29
    switch ($op) {
30

    
31
        case 'view':
32
            $context = $teaser ? 'teaser' : 'full';
33
            $formatter = isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default';
34
            foreach ($items as $delta => $item) {
35
                $items[$delta]['view'] = content_format($field, $item, $formatter, $node);
36
            }
37
            return theme('field', $node, $field, $items, $teaser, $page);
38
    }
39
}
40

    
41

    
42
/**
43
 * Implementation of hook_field_settings().
44
 */
45
function cdm_taxontree_field_settings($op, $field) {
46
    switch ($op) {
47
        case 'form':
48
            $form = array(
49
        '#theme' => 'cdm_taxontree_field_settings',
50
            );
51
            return $form;
52

    
53
        case 'save':
54
            break;
55

    
56
        case 'database columns':
57
            return array(
58
        'tid' => array('type' => 'int', 'length' => 10, 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
59
            );
60
            break;
61

    
62
        case 'filters':
63
            return array(
64
             'default' => array(
65
       'operator' => 'views_handler_operator_like',
66
       'handler' => 'cdm_taxontree_views_filter_handler',
67
            ),
68
            );
69
            break;
70

    
71
        case 'callbacks': //pairs up with cck_fullname_field::view
72
            return array(
73
        'view' => CONTENT_CALLBACK_CUSTOM,
74
            );
75
            break;
76

    
77
    }
78
}
79

    
80
/**
81
 * implementation of hook_widget_info()
82
 *
83
 */
84
function cdm_taxontree_widget_info(){
85
    $vocabularies = taxonomy_get_vocabularies();
86
    $vocabs = array();
87
    foreach ($vocabularies as $vid => $vocab) {
88
        $vocabs['cdm_taxontree_' .$vocab->vid] = array(
89
        'label' => $vocab->name,
90
        'field types' => array('cdm_taxontree'),
91
        );
92
    }
93
    return $vocabs;
94
}
95

    
96
/**
97
 * implementation of hook_widget()
98
 *
99
 */
100
function cdm_taxontree_widget($op, &$node, $field, &$node_field){
101
    switch($op){
102
        case 'prepare form values':
103
            // get posted values in both node edit and profile edit mode
104
            if ($_POST[$field['field_name']] || $_POST[$field['type_name'].'_node_form'][$field['field_name']]) {
105
                $node_field = ($_POST['form_id'] == 'user_edit') ?  $_POST[$field['type_name'].'_node_form'][$field['field_name']] : $_POST[$field['field_name']];
106
            }else{
107
                $node_field_transposed = content_transpose_array_rows_cols($node_field);
108
                $node_field = array();
109
                $node_field[0]['cdm_taxontree_select'] = $node_field_transposed['tid'];
110
            }
111
            return;
112

    
113
        case 'form':
114
            $form = array();
115
            $form[$field['field_name']] = array(
116
        '#tree' => TRUE,
117
        '#theme' => NULL,
118
        '#type' => 'markup',
119
        '#title' => t($field['widget']['label']),
120
        '#description' => t($field['widget']['description']),
121
        '#weight' => $field['widget']['weight'],
122
            );
123

    
124
            // add the form
125
            $delta = 0;
126
            _cdm_taxontree_widget_form($form[$field['field_name']][$delta], $field, $node_field);
127

    
128
            return $form;
129

    
130
        case 'validate':
131
            if($field['required'] && count($node_field[0]['cdm_taxontree_select']) < 1){
132
                form_set_error('cdm_taxontree_select', t('Please select at least one item from %type.', array('%type' => $field['widget']['label'])));
133
            }
134
         return;
135
      }
136
    }
137

    
138
function _cdm_taxontree_widget_form(&$form_item, $field = array(), $node_field = array(), $delta = 0) {
139

    
140
  // get vocabulary id
141
  $vid = substr($field['widget']['type'], strlen($field['type'])+1);
142

    
143
  // build the tree
144
  $tree = cdm_taxontree_build_tree(NULL, NULL, $vid);
145

    
146
  $options = array();
147
  if (!empty($node_field[$delta]['cdm_taxontree_select'])){
148
    $options = _cdm_taxontree_recreate_options($node_field[$delta]['cdm_taxontree_select']);
149
  }
150

    
151
  // prefix and suffix: render the taxon tree div structure around the select field
152
  $prefix .= '<div class="cdm_taxontree_widget">';
153
  // hide labels in filter view
154
  if(substr($field['field_name'],-7,7) !== '_filter'){
155
    $prefix .= '<div class="taxontree_header">';
156
    $prefix .= '<div class="field-label column-left">' . t($field['widget']['label']) . ':' . ($field['required'] == 1 ? ' <span class="form-required" title="' .t('This field is required.'). '">*</span>' : '') . '</div>';
157
    $prefix .= '<div class="field-label column-right">' . t('Your Selection') . ':</div>';
158
    $prefix .= '</div>';
159
  }
160
  $prefix .= '<div class="taxontree">';
161
  $prefix .= '<div class="cdm_taxontree_scroller_x">';
162
  $prefix .= '<div class="cdm_taxontree_container">';
163
  $prefix .= '<div class="cdm_taxontree_scroller_y">';
164
  // inject the taxon tree
165
  $prefix .= theme('cdm_taxontree',$tree, NULL, FALSE, 'cdm_taxontree_node_reference_widget', $field['field_name']);
166
  $prefix .= '</div></div></div></div>';
167
  $prefix .= '<div class="taxontree_devider"><span class="hidden">&gt;&gt;</span></div>';
168
  // this is the place where to put the select box without disturbing the scrollers
169
  $suffix = '</div>';
170

    
171
  // pull in the default value
172
  $default_value = (!empty($field['widget']['default_value'][$delta]['cdm_taxontree_select'])) ? $field['widget']['default_value'][$delta]['cdm_taxontree_select'] : array();
173
  // $default_show_childnodes = ($field['widget']['default_value']['include_childnodes']['include_childnodes'] === 'include_childnodes') ? 'checked="checked"' : '';
174
  $default_show_childnodes = 'checked="checked"';
175

    
176
  // add the select box and set to multiple, if appropriate
177
  $form_item['cdm_taxontree_select'] = array(
178
        '#tree' => TRUE,
179
        '#type' => 'select',
180
        '#default_value' => !empty($node_field[$delta]['cdm_taxontree_select']) ? $node_field[$delta]['cdm_taxontree_select'] : $default_value ,
181
        '#multiple' => $field['multiple'] ? TRUE : FALSE,
182
        '#options' => $options,
183
        '#size' => $field['multiple'] ? 12 : 2,
184
        '#prefix' => $prefix,
185
        '#suffix' => $suffix,
186
        '#attributes' => array('class' => 'taxontree_select'),
187
  );
188

    
189
  // add the scripts for multiple widgets, if not already present
190
  theme('cdm_taxontree_add_scripts');
191

    
192
  global $taxontree_script_present;
193
  if(!$taxontree_script_present[$field['field_name']]){
194
    drupal_add_js('$(document).ready(function() {$(\'ul.cdm_taxontree.' . $field['field_name'] . '\').cdm_taxontree(
195
      {
196
        widget:                 true,
197
        element_name:           \''.$field['field_name'] .'\',  //
198
        multiselect:            '.($field['multiple']?'true':'false').'         //
199
      }
200
      );});', 'inline');
201
    $taxontree_script_present[$field['field_name']] = TRUE;
202
  }
203

    
204
  // add flag for including child nodes if selected in settings
205
  // this is only tirggered in filter mode
206
  if($field['show_include_childnodes']){
207

    
208
    $default_value = (count($_GET)==1) ? $default_show_childnodes : ($field['include_childnodes_flag']) ? 'checked="checked"' : '';
209

    
210
    $include_label = 'Include Childnodes';
211
    $include_childnode_message = 'Include childnodes and parent node';
212
    // HACK! Replace the text to display as 'include childnodes text'
213
    if(substr($field['field_name'],-7,7) == '_filter'){
214
      $vocabulary = substr($field['widget']['type'],strlen($field['type'])+1);
215
      switch($vocabulary){
216

    
217
        case 8:
218
          $include_label = 'Include Childtaxa';
219
          $include_childnode_message = 'Check, if all child taxa of the selected taxa shall be included';
220
          break;
221
        case 3:
222
          $include_label = 'Include Subregions';
223
          $include_childnode_message = 'Check, if all subregions of the selected region should be included';
224
          break;
225

    
226
      }
227
    }
228

    
229
    $form_item['include_childnodes'] = array(
230
      '#tree' => TRUE,
231
      '#type' => 'markup',
232
      '#prefix' => '<div class="include-options">',
233
      '#value' => '<label class="field-label" for="' . $field['field_name'] . $delta . '-include">' . t($include_label) . '</label><div class="field-description">' . t($include_childnode_message) . '<input type="checkbox" name="'  .$field['field_name'] . $delta . '_include" id="' . $field['field_name'] . $delta . '-include" value="include_childnodes" ' . $default_value . '/></div>',
234
      '#suffix' => '</div>',
235
    );
236

    
237
  }
238

    
239
}
240

    
241

    
242
/**
243
 * implementation of hook_filters_alter(), a *views modification*
244
 * to enable injection of more filter values
245
 *
246
 * @param object $view, needed to find cdm_taxontree filters by $delta
247
 * @param array $filters, incoming filter values
248
 * @return array $filters extended filter values
249
 */
250
function cdm_taxontree_views_filters_alter($view, $filters){
251

    
252
    if($view->name == 'search_interest'){
253
        // find the cdm_taxontree_filter
254
        foreach($view->exposed_filter as $delta => $filter){
255
            if(preg_match('@^cdm_taxontree@',$filter['field'])){
256

    
257
                // include child terms and parent term
258
                if($_GET['cdm_taxontree_filter' . $delta . '_include'] == TRUE && is_array($filters[$delta]['filter'])){
259
                    foreach($filters[$delta]['filter'] as $key => $val){
260

    
261
                        if(is_numeric($val)){
262
                            $child_terms[] = $val;
263
                        }
264
                    }
265

    
266
                    $term_collection = array();
267
                    // get child terms from cache
268
                    foreach($child_terms as $key => $term){
269
                        if(!$children = cdm_taxontree_cache_get($term,'cache_cdm_taxontree')){
270
                            // no data in cache? add some!
271
                            $child_terms = _cdm_taxontree_get_all_children($child_terms,TRUE);
272
                            // add parent term
273
                            $parent_terms = taxonomy_get_parents($term);
274
                            foreach($parent_terms as $key => $term_obj){
275
                                $child_terms[$key] = $key;
276
                            }
277

    
278
                            cdm_taxontree_cache_set($term,'cache_cdm_taxontree',serialize($child_terms));
279
                            $children = $child_terms;
280
                        }
281
                        else{
282
                            $children = unserialize($children->data);
283
                        }
284

    
285
                        $term_collection = array_merge($term_collection, $children);
286
                    }
287

    
288
                    // re-add filter values
289
                    $filters[$delta]['filter'] = $term_collection;
290

    
291
                }
292
            }
293
        }
294
    }
295
                
296
    /*
297
     * React on taxonprivacy settings
298
     * Remove taxon_privacy from taxonomy / filters, if user is allowed to view private taxon interests
299
     */
300
    global $user;
301

    
302
    // show information only to roles with access to private interests
303
    if($user->uid && user_access('view private taxoninterests',$user)){
304
            foreach($view->filter as $delta => $filter){
305
                if(preg_match('@^term_node_@',$filter['field'])){
306
                    $tid = intval(str_replace('.tid','',str_replace('term_node_','',$filter['field'])));
307
                    if($tid == 9){
308
                        unset($view->filter[$delta]);
309
                    }
310
                }
311
            }
312
    }
313

    
314
    
315
    return $filters;
316
}
317

    
318

    
319
/*
320
 * implementation of hook_form_alter()
321
 *
322
 * used to replace the standard taxonomy selectbox
323
 * with our nice cdm_taxontree_widget form.
324
 *
325
 */
326
function cdm_taxontree_form_alter($form_id, &$form){
327

    
328
    if($form_id == 'views_filters')
329
    {
330
        global $user;
331
        $view = $form['view']['#value'];
332

    
333
        // find the cdm_taxontree_filter
334
        foreach($view->exposed_filter as $delta => $filter){
335
            if(preg_match('@^cdm_taxontree@',$filter['field'])){
336

    
337
                // simulate field settings to invoke the widget form correctly
338
                $field = array(
339
                    'field_name' => 'cdm_taxontree_filter',
340
                    'type' => 'cdm_taxontree',
341
                  'multiple' => $form['filter'.$delta]['#multiple'],
342
                    'show_include_childnodes' => 1,
343
                    'include_childnodes_flag' => $_GET['cdm_taxontree_filter' . $delta . '_include'] ? 1 : 0,
344
                    'widget' => array(
345
                        'type' => 'cdm_taxontree_' . $form['filter'.$delta]['#vocab'],
346
                        'label' => $filter['label'],
347
                ),
348
                );
349

    
350
                // simulate the node_field data from filter form values
351
                $filter_field = array();
352
                $filter_values = $form['filter'.$delta]['#default_value'];
353

    
354
                // transpose the filter values to a form understood by the widget
355
                if(is_array($filter_values)){
356
                    foreach($filter_values as $key => $val){
357
                        $filter_field[$delta]['cdm_taxontree_select'][] = $val;
358
                    }
359
                }else if(is_numeric($key)){
360
                    $filter_field[$delta]['cdm_taxontree_select'][] = $filter_values;
361
                }
362

    
363
                // create the widget
364
                $subform = array();
365
                _cdm_taxontree_widget_form($subform['cdm_taxontree_filter'], $field, $filter_field, $delta);
366

    
367
                // add some important attributes
368
                $subform['cdm_taxontree_filter']['cdm_taxontree_select']['#name'] = 'filter'.$delta;
369
                $subform['cdm_taxontree_filter']['cdm_taxontree_select']['#id'] = 'edit-filter'.$delta;
370

    
371
                // replace original form element with widget
372
                $form['filter'.$delta] = $subform['cdm_taxontree_filter'];
373

    
374
            }
375
        }
376
    }
377
}
378

    
379

    
380
/**
381
 * Implematation of hook_views_tables()
382
 *
383
 *
384
 * @return onject views related db table data
385
 */
386
function cdm_taxontree_views_tables() {
387
    $tables = array();
388
    // field_name, type_name, widget_type
389
    $result = db_query("SELECT nfi.*, nf.db_storage FROM {node_field_instance} nfi INNER JOIN {node_field} nf ON nfi.field_name = nf.field_name WHERE widget_type LIKE 'cdm_taxontree_%'");
390
    while ($row = db_fetch_object($result)) {
391

    
392
        // Build the list of options
393
        $vid = substr($row->widget_type, 14); // 14 = strlen('cdm_taxontree)!
394

    
395
        $options = array();
396
        $tree = taxonomy_form($vid);
397
        foreach ($tree['#options'] as $index => $option) {
398
            if (is_object($option)) {
399
                foreach($option->option as $key => $value) {
400
                    $options[$key] = $value;
401
                }
402
            }
403
        }
404

    
405
        // Rely on CCK to provide us with the correct table name.
406
        if ($row->db_storage == CONTENT_DB_STORAGE_PER_FIELD) {
407
            $table_name = _content_tablename($row->field_name, CONTENT_DB_STORAGE_PER_FIELD);
408
        }
409
        else {
410
            $table_name = _content_tablename($row->type_name, CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
411
        }
412

    
413
        # Get the taxonomy multi select property
414
        $vocab = taxonomy_get_vocabulary($vid);
415
        $multiple = $vocab->multiple;
416

    
417
        $table = array(
418
      'name' => $table_name,
419
      'provider' => 'cdm_taxontree',
420
      'join' => array(
421
        'left' => array('table' => 'node', 'field' => 'vid',),
422
        'right' => array('field' => 'vid'),
423
        ),
424
      'filters' => array(
425
        $row->field_name .'_tid' => array(
426
          'name' => t('CDM Taxontree: @field_name', array('@field_name' => $row->field_name)),
427
          'help' => t('Filter on @field_name terms.', array('@field_name' => $row->field_name)),
428
          'operator' => 'views_handler_operator_or',
429
                    'handler' => 'cdm_taxontree_views_handler_filter_default',
430
          'value' => array(
431
            '#type' => 'select',
432
        // pass the vocabulary id
433
                        '#vocab' => $vid,
434
            '#options' => $options,
435
            '#multiple' => $multiple,
436
        ),
437
        ),
438
        ),
439
        );
440
        $tables['cdm_taxontree'. $row->field_name] = $table;
441
    }
442
    return $tables;
443
}
444

    
445
function cdm_taxontree_views_handler_filter_default($op, $filter, $filterinfo, &$query) {
446
    $table = $filterinfo['table'];
447
    $field = $filterinfo['field'];
448

    
449
    if (is_array($filter['value']) && count($filter['value'])) {
450
        if ($filter['operator'] == 'OR' || $filter['operator'] == 'NOR') {
451
            $query->ensure_table($table);
452
            $where_args = array_merge(array($query->use_alias_prefix . $table, $field), $filter['value']);
453
            $placeholder = array_fill(0, count($filter['value']), '%s');
454
            if ($filter['operator'] == 'OR') {
455
                $query->add_where("%s.%s IN (". implode(", ", $placeholder) .")", $where_args);
456
            }
457
            else {
458
                $where_args[] = $where_args[0];
459
                $where_args[] = $where_args[1];
460
                $query->add_where("(%s.%s NOT IN (". implode(", ", $placeholder) .") OR %s.%s IS NULL)", $where_args);
461
            }
462
        }
463
        else {
464
            $howmany = count($filter['value']);
465
            $high_table = $query->add_table($table, true, $howmany);
466
            if (!$high_table) { // couldn't add the table
467
                return;
468
            }
469

    
470
            $table_num = $high_table - $howmany;
471
            foreach ($filter['value'] as $item) {
472
                $table_num++;
473
                $tn = $query->get_table_name($table, $table_num);
474
                $query->add_where("%s.%s = '%s'", $tn, $field, $item);
475
            }
476
        }
477
    }
478
    else {
479
        $query->ensure_table("$table");
480
        $query->add_where("%s.%s %s '%s'", $query->use_alias_prefix . $table, $field, $filter['operator'], $filter['value']);
481
    }
482
}
483

    
484
/**
485
 * implematation of hook_token_list()
486
 *
487
 */
488
function cdm_taxontree_token_list($type = 'all') {
489
    if ($type == 'field' || $type == 'all') {
490
        $tokens = array();
491

    
492
        $tokens['cdm_taxontree']['tid'] = t("Term ID - first item only.");
493
        $tokens['cdm_taxontree']['tid-all'] = t("Term ID - all items comma separated");
494
        $tokens['cdm_taxontree']['tid-n'] = t("Term ID - where n = the value to select if more than one value in the field.");
495
        $tokens['cdm_taxontree']['raw'] = t("Raw, unfiltered term - first item only");
496
        $tokens['cdm_taxontree']['raw-all'] = t("Raw, unfiltered terms - all items comma separated");
497
        $tokens['cdm_taxontree']['raw-n'] = t('Raw, unfiltered terms - where n = the value to select if more than one value in the field');
498
        $tokens['cdm_taxontree']['formatted'] = t("Formatted and filtered terms - first item only");
499
        $tokens['cdm_taxontree']['formatted-all'] = t("Formatted and filtered terms - all items with line break separated");
500
        $tokens['cdm_taxontree']['formatted-n'] = t('Formatted and filtered terms - where n = the value to select if more than one value in the field');
501

    
502
        return $tokens;
503
    }
504
}
505

    
506

    
507
/**
508
 * implementation of hook_token_values()
509
 *
510
 */
511
function cdm_taxontree_token_values($type, $object = NULL) {
512
    if ($type == 'field') {
513
        $i = 0;
514
        foreach ($object as $value) {
515
            $term = taxonomy_get_term($value['tid']);
516
            $values[] = $term->name;
517
            $views[] = $value['view'];
518
            $tokens['raw-' . $i] = $term->name;
519
            $tokens['formatted-' . $i] = $value['view'];
520
            $i++;
521
        }
522

    
523
        //for backwards compat
524
        $tokens['raw-all']  = implode(', ' , $values);
525
        $tokens['formatted-all'] = implode("\n", $views);
526

    
527
        return $tokens;
528
    }
529
}
530

    
531
/**
532
 * Helper function to transpose a list of term ids into an
533
 * array with key = tid and value = term name;
534
 * used to recreate cdm_taxontree_widgets select list options.
535
 *
536
 * @param array $options
537
 * @return array transposed options
538
 */
539
function _cdm_taxontree_recreate_options($options = array()){
540
    $transposed = array();
541
    foreach($options as $key => $tid){
542
        if(is_numeric($tid)){
543
            $parents = taxonomy_get_parents_all($tid);
544
            $path = theme('cdm_taxontree_taxon_fullpath',array_reverse($parents),3);
545
            $transposed[$tid] = $path;
546
        }
547
    }
548
    return $transposed;
549
}
550

    
551

    
552
/**
553
 * Theme function that returns a <span> element containing the secundum of a given TreeNode> element
554
 *
555
 * @param array $node The <TreeNode> element as returned by the webservice
556
 *
557
 * @return String html formatted title of the secundum
558
 * @deprecated replace by according taxonomictree function
559
 */
560
function theme_cdm_taxontree_node_reference(&$node){
561

    
562
  $secRefTitle = cdm_taxontree_secRefTitle_for($node->secUuid);
563
  // encode any special characters
564
  $secRefTitle = check_plain($secRefTitle);
565
  $out = ' <span class="sec_ref widget_select" title="'.$secRefTitle.'" style="background-color:#'._uuid_to_rgbhex($node->secUuid).'" alt="'.$node->secUuid.'">'
566
      .$secRefTitle.'</span>';
567
  return $out;
568
}
569

    
570

    
571
/**
572
 * theme function to display terms in the cd,_taxontree_widget with additional data.
573
 *
574
 */
575
function theme_cdm_taxontree_node_reference_widget(&$node){
576
    $title = ($node->titleCache) ? check_plain($node->titleCache) : check_plain($node->name);
577
    $parents = taxonomy_get_parents_all($node->tid);
578
    $path = theme('cdm_taxontree_taxon_fullpath',array_reverse($parents),3);
579
    $out = ' <span class="sec_ref widget_select" title="' . (!empty($path) ? $path : $title) . '" alt="'.$node->tid.'">' . $title . '</span>';
580
    return $out;
581
}
582

    
583
/**
584
 * theme function to replace term names with full path to term name
585
 *
586
 * @param array $tids path->to->term
587
 * @param integer $max_levels, 0 is unlimited
588
 * @return text representation of path->to->term
589
 */
590
function theme_cdm_taxontree_taxon_fullpath($tids = array(), $max_levels = 3){
591
    $path = array();
592
    foreach ($tids as $tid){
593
        $path[] = $tid->name;
594
    }
595
    if(count($path) > $max_levels){
596
        $abbr_path = array_slice($path,0,$max_levels-1);
597
        array_push($abbr_path,'…');
598
        array_push($abbr_path,array_pop($path));
599
        $path = $abbr_path;
600
    }
601
    return implode(' » ',$path);
602
}
603

    
604

    
605
function cdm_taxontree_cache_get($term){
606
  if(variable_get('cdm_taxontree_cache', 0)){
607
    return cache_get($term,'cache_cdm_taxontree');
608
  }
609
}
610

    
611
function cdm_taxontree_cache_set($term, $child_terms){
612
  if(variable_get('cdm_taxontree_cache', 0)){
613
    return cache_set($term,'cache_cdm_taxontree', serialize($child_terms));
614
  }
615
}
616

    
617

    
618
/**
619
 * function to empty the cdm_taxontree cache table
620
 *
621
 */
622
function cdm_taxontree_cache_clear() {
623

    
624
    // clear core tables
625
    $alltables = array('cache_cdm_taxontree');
626
    foreach ($alltables as $table) {
627
        cache_clear_all('*', $table, TRUE);
628
    }
629
    drupal_set_message('CDM Taxontree Cache cleared.');
630
    drupal_goto('admin/settings/cdm_dataportal');
631
}
632

    
633

    
634

    
635
/**
636
 * Converts a UUID into a hexadecimal RGB colour code.
637
 *
638
 * @param UUID $uuid
639
 * @return Number hexadecimal
640
 */
641
function _uuid_to_rgbhex($uuid){
642

    
643
  $xfoot = _str_crossfoot($uuid);
644
  $h = $xfoot / 255;
645
  $h = $h - floor($h);
646
  $RGB = _hsv_2_rgb($h, 0.45, 1);
647
  return dechex($RGB['R']).dechex($RGB['G']).dechex($RGB['B']);
648
}
649

    
650

    
651
/**
652
 * Sums up ASCII values of the character in the given string.
653
 *
654
 * @param String $str
655
 * @return Number
656
 */
657
function _str_crossfoot($str){
658
  $xfoot = 0;
659
  for($i=0; $i<strlen($str); $i++){
660
    $xfoot = $xfoot + ord($str[$i]);
661
  }
662
  return $xfoot;
663
}
664

    
665
/**
666
 * Converts HSV colour codes into their RGB counterpart
667
 *
668
 * @param Number $H value 0-1
669
 * @param Number $S value 0-1
670
 * @param Number $V value 0-1
671
 * @return array three values  0-255
672
 */
673
function _hsv_2_rgb($H, $S, $V) // HSV Values:Number 0-1
674
{ //
675
  $RGB = array();
676
  
677
  if($S == 0){
678
    $R = $G = $B = $V * 255;
679
  } else {
680
    $var_H = $H * 6;
681
    $var_i = floor( $var_H );
682
    $var_1 = $V * ( 1 - $S );
683
    $var_2 = $V * ( 1 - $S * ( $var_H - $var_i ) );
684
    $var_3 = $V * ( 1 - $S * (1 - ( $var_H - $var_i ) ) );
685
  
686
    if ($var_i == 0) { $var_R = $V ; $var_G = $var_3 ; $var_B = $var_1 ; }
687
    else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $V ; $var_B = $var_1 ; }
688
    else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $V ; $var_B = $var_3 ; }
689
    else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $V ; }
690
    else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $V ; }
691
    else { $var_R = $V ; $var_G = $var_1 ; $var_B = $var_2 ; }
692
  
693
    $R = $var_R * 255;
694
    $G = $var_G * 255;
695
    $B = $var_B * 255;
696
  }
697
  
698
  $RGB['R'] = $R;
699
  $RGB['G'] = $G;
700
  $RGB['B'] = $B;
701
  
702
  return $RGB;
703
}
(7-7/18)