views_handler_field_numeric.inc

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

Definition of views_handler_field_numeric.

File

handlers/views_handler_field_numeric.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_numeric.
  5. */
  6. /**
  7. * Render a field 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_numeric extends views_handler_field {
  16. function option_definition() {
  17. $options = parent::option_definition();
  18. $options['set_precision'] = array('default' => FALSE, 'bool' => TRUE);
  19. $options['precision'] = array('default' => 0);
  20. $options['decimal'] = array('default' => '.', 'translatable' => TRUE);
  21. $options['separator'] = array('default' => ',', 'translatable' => TRUE);
  22. $options['format_plural'] = array('default' => FALSE, 'bool' => TRUE);
  23. $options['format_plural_singular'] = array('default' => '1');
  24. $options['format_plural_plural'] = array('default' => '@count');
  25. $options['prefix'] = array('default' => '', 'translatable' => TRUE);
  26. $options['suffix'] = array('default' => '', 'translatable' => TRUE);
  27. return $options;
  28. }
  29. function options_form(&$form, &$form_state) {
  30. if (!empty($this->definition['float'])) {
  31. $form['set_precision'] = array(
  32. '#type' => 'checkbox',
  33. '#title' => t('Round'),
  34. '#description' => t('If checked, the number will be rounded.'),
  35. '#default_value' => $this->options['set_precision'],
  36. );
  37. $form['precision'] = array(
  38. '#type' => 'textfield',
  39. '#title' => t('Precision'),
  40. '#default_value' => $this->options['precision'],
  41. '#description' => t('Specify how many digits to print after the decimal point.'),
  42. '#dependency' => array('edit-options-set-precision' => array(TRUE)),
  43. '#size' => 2,
  44. );
  45. $form['decimal'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Decimal point'),
  48. '#default_value' => $this->options['decimal'],
  49. '#description' => t('What single character to use as a decimal point.'),
  50. '#size' => 2,
  51. );
  52. }
  53. $form['separator'] = array(
  54. '#type' => 'select',
  55. '#title' => t('Thousands marker'),
  56. '#options' => array(
  57. '' => t('- None -'),
  58. ',' => t('Comma'),
  59. ' ' => t('Space'),
  60. '.' => t('Decimal'),
  61. '\'' => t('Apostrophe'),
  62. ),
  63. '#default_value' => $this->options['separator'],
  64. '#description' => t('What single character to use as the thousands separator.'),
  65. '#size' => 2,
  66. );
  67. $form['format_plural'] = array(
  68. '#type' => 'checkbox',
  69. '#title' => t('Format plural'),
  70. '#description' => t('If checked, special handling will be used for plurality.'),
  71. '#default_value' => $this->options['format_plural'],
  72. );
  73. $form['format_plural_singular'] = array(
  74. '#type' => 'textfield',
  75. '#title' => t('Singular form'),
  76. '#default_value' => $this->options['format_plural_singular'],
  77. '#description' => t('Text to use for the singular form.'),
  78. '#dependency' => array('edit-options-format-plural' => array(TRUE)),
  79. );
  80. $form['format_plural_plural'] = array(
  81. '#type' => 'textfield',
  82. '#title' => t('Plural form'),
  83. '#default_value' => $this->options['format_plural_plural'],
  84. '#description' => t('Text to use for the plural form, @count will be replaced with the value.'),
  85. '#dependency' => array('edit-options-format-plural' => array(TRUE)),
  86. );
  87. $form['prefix'] = array(
  88. '#type' => 'textfield',
  89. '#title' => t('Prefix'),
  90. '#default_value' => $this->options['prefix'],
  91. '#description' => t('Text to put before the number, such as currency symbol.'),
  92. );
  93. $form['suffix'] = array(
  94. '#type' => 'textfield',
  95. '#title' => t('Suffix'),
  96. '#default_value' => $this->options['suffix'],
  97. '#description' => t('Text to put after the number, such as currency symbol.'),
  98. );
  99. parent::options_form($form, $form_state);
  100. }
  101. function render($values) {
  102. $value = $this->get_value($values);
  103. if (!empty($this->options['set_precision'])) {
  104. $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
  105. }
  106. else {
  107. $remainder = abs($value) - intval(abs($value));
  108. $value = $value > 0 ? floor($value) : ceil($value);
  109. $value = number_format($value, 0, '', $this->options['separator']);
  110. if ($remainder) {
  111. // The substr may not be locale safe.
  112. $value .= $this->options['decimal'] . substr($remainder, 2);
  113. }
  114. }
  115. // Check to see if hiding should happen before adding prefix and suffix.
  116. if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
  117. return '';
  118. }
  119. // Should we format as a plural.
  120. if (!empty($this->options['format_plural'])) {
  121. $value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
  122. }
  123. return $this->sanitize_value($this->options['prefix'], 'xss')
  124. . $this->sanitize_value($value)
  125. . $this->sanitize_value($this->options['suffix'], 'xss');
  126. }
  127. }