views_handler_field_math.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /**
  17. * {@inheritdoc}
  18. */
  19. public function option_definition() {
  20. $options = parent::option_definition();
  21. $options['expression'] = array('default' => '');
  22. return $options;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function options_form(&$form, &$form_state) {
  28. $form['expression'] = array(
  29. '#type' => 'textarea',
  30. '#title' => t('Expression'),
  31. '#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). The result of the previous row's mathematical expression can be accessed by using the [expression] token itself."),
  32. '#default_value' => $this->options['expression'],
  33. );
  34. // Create a place for the help.
  35. $form['expression_help'] = array();
  36. parent::options_form($form, $form_state);
  37. // Then move the existing help.
  38. $form['expression_help'] = $form['alter']['help'];
  39. unset($form['expression_help']['#dependency']);
  40. unset($form['alter']['help']);
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function render($values) {
  46. ctools_include('math-expr');
  47. $tokens = array_map('floatval', $this->get_render_tokens(array()));
  48. $value = strtr($this->options['expression'], $tokens);
  49. $expressions = explode(';', $value);
  50. $math = new ctools_math_expr;
  51. foreach ($expressions as $expression) {
  52. if ($expression !== '') {
  53. $value = $math->evaluate($expression);
  54. }
  55. }
  56. // The rest is directly from views_handler_field_numeric but because it
  57. // does not allow the value to be passed in, it is copied.
  58. if (!empty($this->options['set_precision'])) {
  59. $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
  60. }
  61. else {
  62. $remainder = abs($value) - intval(abs($value));
  63. $value = $value > 0 ? floor($value) : ceil($value);
  64. $value = number_format($value, 0, '', $this->options['separator']);
  65. if ($remainder) {
  66. // The substr may not be locale safe.
  67. $value .= $this->options['decimal'] . substr($remainder, 2);
  68. }
  69. }
  70. // Check to see if hiding should happen before adding prefix and suffix.
  71. if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
  72. return '';
  73. }
  74. // Should we format as a plural.
  75. if (!empty($this->options['format_plural']) && ($value != 0 || !$this->options['empty_zero'])) {
  76. $value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
  77. }
  78. return $this->sanitize_value($this->options['prefix'] . $value . $this->options['suffix']);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function query() {
  84. }
  85. }