Newer
Older
Earl Miles
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
// $Id$
/**
* 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 {
function option_definition() {
$options = parent::option_definition();
$options['set_precision'] = array('default' => FALSE);
$options['precision'] = array('default' => 0);
$options['decimal'] = array('default' => '.', 'translatable' => TRUE);
$options['separator'] = array('default' => ',', 'translatable' => TRUE);
$options['prefix'] = array('default' => '', 'translatable' => TRUE);
$options['suffix'] = array('default' => '', 'translatable' => TRUE);
return $options;
}
function options_form(&$form, &$form_state) {
parent::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.'),
'#process' => array('views_process_dependency'),
'#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' => 'textfield',
'#title' => t('Thousands separator'),
'#default_value' => $this->options['separator'],
'#description' => t('What single character to use as the thousands separator.'),
'#size' => 2,
);
$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.'),
);
}
function render($values) {
$value = $values->{$this->field_alias};
if (!empty($this->options['set_precision'])) {
$value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
}
else {
$remainder = abs($value) - intval(abs($value));
Earl Miles
committed
$value = $value > 0 ? floor($value) : ceil($value);
$value = number_format($value, 0, '', $this->options['separator']);
Earl Miles
committed
if ($remainder) {
// The substr may not be locale safe.
$value .= $this->options['decimal'] . substr($remainder, 2);
Earl Miles
committed
}
}
Earl Miles
committed
// Check to see if hiding should happen before adding prefix and suffix.
if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
return '';
}
Earl Miles
committed
return check_plain($this->options['prefix'] . $value . $this->options['suffix']);
}
}