views_handler_field_math.inc

  1. 3.x handlers/views_handler_field_math.inc
  2. 2.x handlers/views_handler_field_math.inc

Definition of views_handler_field_math.

File

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