1 | <?php |
---|
2 | /** |
---|
3 | * Render a mathematical expression as a numeric value |
---|
4 | * |
---|
5 | * Definition terms: |
---|
6 | * - float: If true this field contains a decimal value. If unset this field |
---|
7 | * will be assumed to be integer. |
---|
8 | * |
---|
9 | * @ingroup views_field_handlers |
---|
10 | */ |
---|
11 | class views_handler_field_math extends views_handler_field_numeric { |
---|
12 | function option_definition() { |
---|
13 | $options = parent::option_definition(); |
---|
14 | $options['expression'] = array('default' => ''); |
---|
15 | |
---|
16 | return $options; |
---|
17 | } |
---|
18 | |
---|
19 | function options_form(&$form, &$form_state) { |
---|
20 | $form['expression'] = array( |
---|
21 | '#type' => 'textarea', |
---|
22 | '#title' => t('Expression'), |
---|
23 | '#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).'), |
---|
24 | '#default_value' => $this->options['expression'], |
---|
25 | ); |
---|
26 | |
---|
27 | // Create a place for the help |
---|
28 | $form['expression_help'] = array(); |
---|
29 | parent::options_form($form, $form_state); |
---|
30 | |
---|
31 | // Then move the existing help: |
---|
32 | $form['expression_help'] = $form['alter']['help']; |
---|
33 | unset($form['expression_help']['#dependency']); |
---|
34 | unset($form['expression_help']['#process']); |
---|
35 | unset($form['alter']['help']); |
---|
36 | } |
---|
37 | |
---|
38 | function render($values) { |
---|
39 | ctools_include('math-expr'); |
---|
40 | $value = strtr($this->options['expression'], $this->get_render_tokens(array())); |
---|
41 | $expressions = explode(';', $value); |
---|
42 | $math = new ctools_math_expr; |
---|
43 | foreach ($expressions as $expression) { |
---|
44 | if ($expression !== '') { |
---|
45 | $value = $math->evaluate($expression); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | // The rest is directly from views_handler_field_numeric but because it |
---|
50 | // does not allow the value to be passed in, it is copied. |
---|
51 | if (!empty($this->options['set_precision'])) { |
---|
52 | $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']); |
---|
53 | } |
---|
54 | else { |
---|
55 | $remainder = abs($value) - intval(abs($value)); |
---|
56 | $value = $value > 0 ? floor($value) : ceil($value); |
---|
57 | $value = number_format($value, 0, '', $this->options['separator']); |
---|
58 | if ($remainder) { |
---|
59 | // The substr may not be locale safe. |
---|
60 | $value .= $this->options['decimal'] . substr($remainder, 2); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | // Check to see if hiding should happen before adding prefix and suffix. |
---|
65 | if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) { |
---|
66 | return ''; |
---|
67 | } |
---|
68 | |
---|
69 | // Should we format as a plural. |
---|
70 | if (!empty($this->options['format_plural']) && ($value != 0 || !$this->options['empty_zero'])) { |
---|
71 | $value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']); |
---|
72 | } |
---|
73 | |
---|
74 | return $this->sanitize_value($this->options['prefix'] . $value . $this->options['suffix']); |
---|
75 | } |
---|
76 | |
---|
77 | function query() { } |
---|
78 | } |
---|