Projet

Général

Profil

Paste
Télécharger (2,88 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_math.inc @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_math.
6
 */
7

    
8
/**
9
 * Render a mathematical expression as a numeric value
10
 *
11
 * Definition terms:
12
 * - float: If true this field contains a decimal value. If unset this field
13
 *   will be assumed to be integer.
14
 *
15
 * @ingroup views_field_handlers
16
 */
17
class views_handler_field_math extends views_handler_field_numeric {
18
  function option_definition() {
19
    $options = parent::option_definition();
20
    $options['expression'] = array('default' => '');
21

    
22
    return $options;
23
  }
24

    
25
  function options_form(&$form, &$form_state) {
26
    $form['expression'] = array(
27
      '#type' => 'textarea',
28
      '#title' => t('Expression'),
29
      '#description' => t('Enter mathematical expressions such as 2 + 2 or sqrt(5). You may assign variables and create mathematical functions and evaluate them. Use the ; to separate these. For example: f(x) = x + 2; f(2).'),
30
      '#default_value' => $this->options['expression'],
31
    );
32

    
33
    // Create a place for the help
34
    $form['expression_help'] = array();
35
    parent::options_form($form, $form_state);
36

    
37
    // Then move the existing help:
38
    $form['expression_help'] = $form['alter']['help'];
39
    unset($form['expression_help']['#dependency']);
40
    unset($form['alter']['help']);
41
  }
42

    
43
  function render($values) {
44
    ctools_include('math-expr');
45
    $tokens = array_map('floatval', $this->get_render_tokens(array()));
46
    $value = strtr($this->options['expression'], $tokens);
47
    $expressions = explode(';', $value);
48
    $math = new ctools_math_expr;
49
    foreach ($expressions as $expression) {
50
      if ($expression !== '') {
51
        $value = $math->evaluate($expression);
52
      }
53
    }
54

    
55
    // The rest is directly from views_handler_field_numeric but because it
56
    // does not allow the value to be passed in, it is copied.
57
    if (!empty($this->options['set_precision'])) {
58
      $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
59
    }
60
    else {
61
      $remainder = abs($value) - intval(abs($value));
62
      $value = $value > 0 ? floor($value) : ceil($value);
63
      $value = number_format($value, 0, '', $this->options['separator']);
64
      if ($remainder) {
65
        // The substr may not be locale safe.
66
        $value .= $this->options['decimal'] . substr($remainder, 2);
67
      }
68
    }
69

    
70
    // Check to see if hiding should happen before adding prefix and suffix.
71
    if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
72
      return '';
73
    }
74

    
75
    // Should we format as a plural.
76
    if (!empty($this->options['format_plural']) && ($value != 0 || !$this->options['empty_zero'])) {
77
      $value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
78
    }
79

    
80
    return $this->sanitize_value($this->options['prefix'] . $value . $this->options['suffix']);
81
  }
82

    
83
  function query() { }
84
}