00001 <?php
00011 class views_handler_field_math extends views_handler_field_numeric {
00012 function option_definition() {
00013 $options = parent::option_definition();
00014 $options['expression'] = array('default' => '');
00015
00016 return $options;
00017 }
00018
00019 function options_form(&$form, &$form_state) {
00020 $form['expression'] = array(
00021 '#type' => 'textarea',
00022 '#title' => t('Expression'),
00023 '#description' => t('Enter mathematical expressions such as 2 + 2 or sqrt(5). You my assign variables and create mathematical functions and evaluate them. Use the ; to separate these. For example: f(x) = x + 2; f(2).'),
00024 '#default_value' => $this->options['expression'],
00025 );
00026
00027
00028 $form['expression_help'] = array();
00029 parent::options_form($form, $form_state);
00030
00031
00032 $form['expression_help'] = $form['alter']['help'];
00033 unset($form['expression_help']['#dependency']);
00034 unset($form['alter']['help']);
00035 }
00036
00037 function render($values) {
00038 ctools_include('math-expr');
00039 $tokens = array_map('floatval', $this->get_render_tokens(array()));
00040 $value = strtr($this->options['expression'], $tokens);
00041 $expressions = explode(';', $value);
00042 $math = new ctools_math_expr;
00043 foreach ($expressions as $expression) {
00044 if ($expression !== '') {
00045 $value = $math->evaluate($expression);
00046 }
00047 }
00048
00049
00050
00051 if (!empty($this->options['set_precision'])) {
00052 $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
00053 }
00054 else {
00055 $remainder = abs($value) - intval(abs($value));
00056 $value = $value > 0 ? floor($value) : ceil($value);
00057 $value = number_format($value, 0, '', $this->options['separator']);
00058 if ($remainder) {
00059
00060 $value .= $this->options['decimal'] . substr($remainder, 2);
00061 }
00062 }
00063
00064
00065 if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
00066 return '';
00067 }
00068
00069
00070 if (!empty($this->options['format_plural']) && ($value != 0 || !$this->options['empty_zero'])) {
00071 $value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
00072 }
00073
00074 return $this->sanitize_value($this->options['prefix'] . $value . $this->options['suffix']);
00075 }
00076
00077 function query() { }
00078 }