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