Stripping CVS keywords
[project/views.git] / handlers / views_handler_field_numeric.inc
1 <?php
2 /**
3 * Render a field as a numeric value
4 *
5 * Definition terms:
6 * - float: If true this field contains a decimal value. If unset this field
7 * will be assumed to be integer.
8 *
9 * @ingroup views_field_handlers
10 */
11 class views_handler_field_numeric extends views_handler_field {
12 function option_definition() {
13 $options = parent::option_definition();
14
15 $options['set_precision'] = array('default' => FALSE);
16 $options['precision'] = array('default' => 0);
17 $options['decimal'] = array('default' => '.', 'translatable' => TRUE);
18 $options['separator'] = array('default' => ',', 'translatable' => TRUE);
19 $options['prefix'] = array('default' => '', 'translatable' => TRUE);
20 $options['suffix'] = array('default' => '', 'translatable' => TRUE);
21
22 return $options;
23 }
24
25 function options_form(&$form, &$form_state) {
26 parent::options_form($form, $form_state);
27
28 if (!empty($this->definition['float'])) {
29 $form['set_precision'] = array(
30 '#type' => 'checkbox',
31 '#title' => t('Round'),
32 '#description' => t('If checked, the number will be rounded.'),
33 '#default_value' => $this->options['set_precision'],
34 );
35 $form['precision'] = array(
36 '#type' => 'textfield',
37 '#title' => t('Precision'),
38 '#default_value' => $this->options['precision'],
39 '#description' => t('Specify how many digits to print after the decimal point.'),
40 '#process' => array('views_process_dependency'),
41 '#dependency' => array('edit-options-set-precision' => array(TRUE)),
42 '#size' => 2,
43 );
44 $form['decimal'] = array(
45 '#type' => 'textfield',
46 '#title' => t('Decimal point'),
47 '#default_value' => $this->options['decimal'],
48 '#description' => t('What single character to use as a decimal point.'),
49 '#size' => 2,
50 );
51 }
52 $form['separator'] = array(
53 '#type' => 'textfield',
54 '#title' => t('Thousands separator'),
55 '#default_value' => $this->options['separator'],
56 '#description' => t('What single character to use as the thousands separator.'),
57 '#size' => 2,
58 );
59 $form['prefix'] = array(
60 '#type' => 'textfield',
61 '#title' => t('Prefix'),
62 '#default_value' => $this->options['prefix'],
63 '#description' => t('Text to put before the number, such as currency symbol.'),
64 );
65 $form['suffix'] = array(
66 '#type' => 'textfield',
67 '#title' => t('Suffix'),
68 '#default_value' => $this->options['suffix'],
69 '#description' => t('Text to put after the number, such as currency symbol.'),
70 );
71 }
72
73 function render($values) {
74 $value = $values->{$this->field_alias};
75 if (!empty($this->options['set_precision'])) {
76 $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
77 }
78 else {
79 $remainder = abs($value) - intval(abs($value));
80 $value = $value > 0 ? floor($value) : ceil($value);
81 $value = number_format($value, 0, '', $this->options['separator']);
82 if ($remainder) {
83 // The substr may not be locale safe.
84 $value .= $this->options['decimal'] . substr($remainder, 2);
85 }
86 }
87
88 // Check to see if hiding should happen before adding prefix and suffix.
89 if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
90 return '';
91 }
92
93 return check_plain($this->options['prefix'] . $value . $this->options['suffix']);
94 }
95 }