• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

handlers/views_handler_field_math.inc

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     // Create a place for the help
00028     $form['expression_help'] = array();
00029     parent::options_form($form, $form_state);
00030 
00031     // Then move the existing help:
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     // The rest is directly from views_handler_field_numeric but because it
00050     // does not allow the value to be passed in, it is copied.
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         // The substr may not be locale safe.
00060         $value .= $this->options['decimal'] . substr($remainder, 2);
00061       }
00062     }
00063 
00064     // Check to see if hiding should happen before adding prefix and suffix.
00065     if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
00066       return '';
00067     }
00068 
00069     // Should we format as a plural.
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 }

Generated on Sun Feb 26 2012 12:52:51 for Views by  doxygen 1.7.1