123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- /**
- * @file
- * Definition of views_handler_field_numeric.
- */
- /**
- * Render a field as a numeric value.
- *
- * Definition terms:
- * - float: If true this field contains a decimal value. If unset this field
- * will be assumed to be integer.
- *
- * @ingroup views_field_handlers
- */
- class views_handler_field_numeric extends views_handler_field {
- /**
- * {@inheritdoc}
- */
- public function option_definition() {
- $options = parent::option_definition();
- $options['set_precision'] = array('default' => FALSE, 'bool' => TRUE);
- $options['precision'] = array('default' => 0);
- $options['decimal'] = array('default' => '.', 'translatable' => TRUE);
- $options['separator'] = array('default' => ',', 'translatable' => TRUE);
- $options['format_plural'] = array('default' => FALSE, 'bool' => TRUE);
- $options['format_plural_singular'] = array('default' => '1');
- $options['format_plural_plural'] = array('default' => '@count');
- $options['prefix'] = array('default' => '', 'translatable' => TRUE);
- $options['suffix'] = array('default' => '', 'translatable' => TRUE);
- return $options;
- }
- /**
- * {@inheritdoc}
- */
- public function options_form(&$form, &$form_state) {
- if (!empty($this->definition['float'])) {
- $form['set_precision'] = array(
- '#type' => 'checkbox',
- '#title' => t('Round'),
- '#description' => t('If checked, the number will be rounded.'),
- '#default_value' => $this->options['set_precision'],
- );
- $form['precision'] = array(
- '#type' => 'textfield',
- '#title' => t('Precision'),
- '#default_value' => $this->options['precision'],
- '#description' => t('Specify how many digits to print after the decimal point.'),
- '#dependency' => array('edit-options-set-precision' => array(TRUE)),
- '#size' => 2,
- );
- $form['decimal'] = array(
- '#type' => 'textfield',
- '#title' => t('Decimal point'),
- '#default_value' => $this->options['decimal'],
- '#description' => t('What single character to use as a decimal point.'),
- '#size' => 2,
- );
- }
- $form['separator'] = array(
- '#type' => 'select',
- '#title' => t('Thousands marker'),
- '#options' => array(
- '' => t('- None -'),
- ',' => t('Comma'),
- ' ' => t('Space'),
- '.' => t('Decimal'),
- '\'' => t('Apostrophe'),
- ),
- '#default_value' => $this->options['separator'],
- '#description' => t('What single character to use as the thousands separator.'),
- '#size' => 2,
- );
- $form['format_plural'] = array(
- '#type' => 'checkbox',
- '#title' => t('Format plural'),
- '#description' => t('If checked, special handling will be used for plurality.'),
- '#default_value' => $this->options['format_plural'],
- );
- $form['format_plural_singular'] = array(
- '#type' => 'textfield',
- '#title' => t('Singular form'),
- '#default_value' => $this->options['format_plural_singular'],
- '#description' => t('Text to use for the singular form.'),
- '#dependency' => array('edit-options-format-plural' => array(TRUE)),
- );
- $form['format_plural_plural'] = array(
- '#type' => 'textfield',
- '#title' => t('Plural form'),
- '#default_value' => $this->options['format_plural_plural'],
- '#description' => t('Text to use for the plural form, @count will be replaced with the value.'),
- '#dependency' => array('edit-options-format-plural' => array(TRUE)),
- );
- $form['prefix'] = array(
- '#type' => 'textfield',
- '#title' => t('Prefix'),
- '#default_value' => $this->options['prefix'],
- '#description' => t('Text to put before the number, such as currency symbol.'),
- );
- $form['suffix'] = array(
- '#type' => 'textfield',
- '#title' => t('Suffix'),
- '#default_value' => $this->options['suffix'],
- '#description' => t('Text to put after the number, such as currency symbol.'),
- );
- parent::options_form($form, $form_state);
- }
- /**
- * {@inheritdoc}
- */
- public function render($values) {
- $value = $this->get_value($values);
- // Output nothing if the value is null.
- if (is_null($value)) {
- return '';
- }
- // Hiding should happen before rounding or adding prefix/suffix.
- if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
- return '';
- }
- if (!empty($this->options['set_precision'])) {
- $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
- }
- else {
- $point_position = strpos($value, '.');
- $remainder = ($point_position === FALSE) ? '' : substr($value, $point_position + 1);
- $value = $value > 0 ? floor($value) : ceil($value);
- $value = number_format($value, 0, '', $this->options['separator']);
- if ($remainder) {
- // The substr may not be locale safe.
- $value .= $this->options['decimal'] . $remainder;
- }
- }
- // Should we format as a plural.
- if (!empty($this->options['format_plural'])) {
- $value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
- }
- return $this->sanitize_value($this->options['prefix'], 'xss')
- . $this->sanitize_value($value)
- . $this->sanitize_value($this->options['suffix'], 'xss');
- }
- }
|