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