Project

General

Profile

Paste
Download (5.05 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_numeric.inc @ 44d2e178

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_numeric.
6
 */
7

    
8
/**
9
 * Render a field as a numeric value
10
 *
11
 * Definition terms:
12
 * - float: If true this field contains a decimal value. If unset this field
13
 *          will be assumed to be integer.
14
 *
15
 * @ingroup views_field_handlers
16
 */
17
class views_handler_field_numeric extends views_handler_field {
18
  function option_definition() {
19
    $options = parent::option_definition();
20

    
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

    
31
    return $options;
32
  }
33

    
34
  function options_form(&$form, &$form_state) {
35
    if (!empty($this->definition['float'])) {
36
      $form['set_precision'] = array(
37
        '#type' => 'checkbox',
38
        '#title' => t('Round'),
39
        '#description' => t('If checked, the number will be rounded.'),
40
        '#default_value' => $this->options['set_precision'],
41
      );
42
      $form['precision'] = array(
43
        '#type' => 'textfield',
44
        '#title' => t('Precision'),
45
        '#default_value' => $this->options['precision'],
46
        '#description' => t('Specify how many digits to print after the decimal point.'),
47
        '#dependency' => array('edit-options-set-precision' => array(TRUE)),
48
        '#size' => 2,
49
      );
50
      $form['decimal'] = array(
51
        '#type' => 'textfield',
52
        '#title' => t('Decimal point'),
53
        '#default_value' => $this->options['decimal'],
54
        '#description' => t('What single character to use as a decimal point.'),
55
        '#size' => 2,
56
      );
57
    }
58
    $form['separator'] = array(
59
      '#type' => 'select',
60
      '#title' => t('Thousands marker'),
61
      '#options' => array(
62
        '' => t('- None -'),
63
        ',' => t('Comma'),
64
        ' ' => t('Space'),
65
        '.' => t('Decimal'),
66
        '\'' => t('Apostrophe'),
67
      ),
68
      '#default_value' => $this->options['separator'],
69
      '#description' => t('What single character to use as the thousands separator.'),
70
      '#size' => 2,
71
    );
72
    $form['format_plural'] = array(
73
      '#type' => 'checkbox',
74
      '#title' => t('Format plural'),
75
      '#description' => t('If checked, special handling will be used for plurality.'),
76
      '#default_value' => $this->options['format_plural'],
77
    );
78
    $form['format_plural_singular'] = array(
79
      '#type' => 'textfield',
80
      '#title' => t('Singular form'),
81
      '#default_value' => $this->options['format_plural_singular'],
82
      '#description' => t('Text to use for the singular form.'),
83
      '#dependency' => array('edit-options-format-plural' => array(TRUE)),
84
    );
85
    $form['format_plural_plural'] = array(
86
      '#type' => 'textfield',
87
      '#title' => t('Plural form'),
88
      '#default_value' => $this->options['format_plural_plural'],
89
      '#description' => t('Text to use for the plural form, @count will be replaced with the value.'),
90
      '#dependency' => array('edit-options-format-plural' => array(TRUE)),
91
    );
92
    $form['prefix'] = array(
93
      '#type' => 'textfield',
94
      '#title' => t('Prefix'),
95
      '#default_value' => $this->options['prefix'],
96
      '#description' => t('Text to put before the number, such as currency symbol.'),
97
    );
98
    $form['suffix'] = array(
99
      '#type' => 'textfield',
100
      '#title' => t('Suffix'),
101
      '#default_value' => $this->options['suffix'],
102
      '#description' => t('Text to put after the number, such as currency symbol.'),
103
    );
104

    
105
    parent::options_form($form, $form_state);
106
  }
107

    
108
  function render($values) {
109
    $value = $this->get_value($values);
110

    
111
    // Hiding should happen before rounding or adding prefix/suffix.
112
    if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
113
      return '';
114
    }
115

    
116
    if (!empty($this->options['set_precision'])) {
117
      $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
118
    }
119
    else {
120
      $remainder = abs($value) - intval(abs($value));
121
      $value = $value > 0 ? floor($value) : ceil($value);
122
      $value = number_format($value, 0, '', $this->options['separator']);
123
      if ($remainder) {
124
        // The substr may not be locale safe.
125
        $value .= $this->options['decimal'] . substr($remainder, 2);
126
      }
127
    }
128

    
129
    // Should we format as a plural.
130
    if (!empty($this->options['format_plural'])) {
131
      $value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
132
    }
133

    
134
    return $this->sanitize_value($this->options['prefix'], 'xss')
135
      . $this->sanitize_value($value)
136
      . $this->sanitize_value($this->options['suffix'], 'xss');
137
  }
138
}