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