• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

handlers/views_handler_field_numeric.inc

00001 <?php
00011 class views_handler_field_numeric extends views_handler_field {
00012   function option_definition() {
00013     $options = parent::option_definition();
00014 
00015     $options['set_precision'] = array('default' => FALSE);
00016     $options['precision'] = array('default' => 0);
00017     $options['decimal'] = array('default' => '.', 'translatable' => TRUE);
00018     $options['separator'] = array('default' => ',', 'translatable' => TRUE);
00019     $options['format_plural'] = array('default' => FALSE);
00020     $options['format_plural_singular'] = array('default' => '1');
00021     $options['format_plural_plural'] = array('default' => '@count');
00022     $options['prefix'] = array('default' => '', 'translatable' => TRUE);
00023     $options['suffix'] = array('default' => '', 'translatable' => TRUE);
00024 
00025     return $options;
00026   }
00027 
00028   function options_form(&$form, &$form_state) {
00029     if (!empty($this->definition['float'])) {
00030       $form['set_precision'] = array(
00031         '#type' => 'checkbox',
00032         '#title' => t('Round'),
00033         '#description' => t('If checked, the number will be rounded.'),
00034         '#default_value' => $this->options['set_precision'],
00035       );
00036       $form['precision'] = array(
00037         '#type' => 'textfield',
00038         '#title' => t('Precision'),
00039         '#default_value' => $this->options['precision'],
00040         '#description' => t('Specify how many digits to print after the decimal point.'),
00041         '#dependency' => array('edit-options-set-precision' => array(TRUE)),
00042         '#size' => 2,
00043       );
00044       $form['decimal'] = array(
00045         '#type' => 'textfield',
00046         '#title' => t('Decimal point'),
00047         '#default_value' => $this->options['decimal'],
00048         '#description' => t('What single character to use as a decimal point.'),
00049         '#size' => 2,
00050       );
00051     }
00052     $form['separator'] = array(
00053       '#type' => 'select',
00054       '#title' => t('Thousands marker'),
00055       '#options' => array(
00056         '' => t('- None -'),
00057         ',' => t('Comma'),
00058         ' ' => t('Space'),
00059         '.' => t('Decimal'),
00060         '\'' => t('Apostrophe'),
00061       ),
00062       '#default_value' => $this->options['separator'],
00063       '#description' => t('What single character to use as the thousands separator.'),
00064       '#size' => 2,
00065     );
00066     $form['format_plural'] = array(
00067       '#type' => 'checkbox',
00068       '#title' => t('Format plural'),
00069       '#description' => t('If checked, special handling will be used for plurality.'),
00070       '#default_value' => $this->options['format_plural'],
00071     );
00072     $form['format_plural_singular'] = array(
00073       '#type' => 'textfield',
00074       '#title' => t('Singular form'),
00075       '#default_value' => $this->options['format_plural_singular'],
00076       '#description' => t('Text to use for the singular form.'),
00077       '#dependency' => array('edit-options-format-plural' => array(TRUE)),
00078     );
00079     $form['format_plural_plural'] = array(
00080       '#type' => 'textfield',
00081       '#title' => t('Plural form'),
00082       '#default_value' => $this->options['format_plural_plural'],
00083       '#description' => t('Text to use for the plural form, @count will be replaced with the value.'),
00084       '#dependency' => array('edit-options-format-plural' => array(TRUE)),
00085     );
00086     $form['prefix'] = array(
00087       '#type' => 'textfield',
00088       '#title' => t('Prefix'),
00089       '#default_value' => $this->options['prefix'],
00090       '#description' => t('Text to put before the number, such as currency symbol.'),
00091     );
00092     $form['suffix'] = array(
00093       '#type' => 'textfield',
00094       '#title' => t('Suffix'),
00095       '#default_value' => $this->options['suffix'],
00096       '#description' => t('Text to put after the number, such as currency symbol.'),
00097     );
00098 
00099     parent::options_form($form, $form_state);
00100   }
00101 
00102   function render($values) {
00103     $value = $this->get_value($values);
00104     if (!empty($this->options['set_precision'])) {
00105       $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
00106     }
00107     else {
00108       $remainder = abs($value) - intval(abs($value));
00109       $value = $value > 0 ? floor($value) : ceil($value);
00110       $value = number_format($value, 0, '', $this->options['separator']);
00111       if ($remainder) {
00112         // The substr may not be locale safe.
00113         $value .= $this->options['decimal'] . substr($remainder, 2);
00114       }
00115     }
00116 
00117     // Check to see if hiding should happen before adding prefix and suffix.
00118     if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
00119       return '';
00120     }
00121 
00122     // Should we format as a plural.
00123     if (!empty($this->options['format_plural'])) {
00124       $value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
00125     }
00126 
00127     return $this->sanitize_value($this->options['prefix'], 'xss')
00128       . $this->sanitize_value($value)
00129       . $this->sanitize_value($this->options['suffix'], 'xss');
00130   }
00131 }

Generated on Sun Feb 26 2012 12:52:51 for Views by  doxygen 1.7.1