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