views_handler_field_numeric.inc

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

File

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