annotate sites/all/modules/views/handlers/views_handler_field_math.inc @ 13:134d4b2e75f6

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