views_handler_field_numeric.inc 5.3 KB

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