[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/handlers/ -> views_handler_field_numeric.inc (source)

   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['format_plural'] = array('default' => FALSE);
  20      $options['format_plural_singular'] = array('default' => '1');
  21      $options['format_plural_plural'] = array('default' => '@count');
  22      $options['prefix'] = array('default' => '', 'translatable' => TRUE);
  23      $options['suffix'] = array('default' => '', 'translatable' => TRUE);
  24  
  25      return $options;
  26    }
  27  
  28    function options_form(&$form, &$form_state) {
  29      parent::options_form($form, $form_state);
  30  
  31      if (!empty($this->definition['float'])) {
  32        $form['set_precision'] = array(
  33          '#type' => 'checkbox',
  34          '#title' => t('Round'),
  35          '#description' => t('If checked, the number will be rounded.'),
  36          '#default_value' => $this->options['set_precision'],
  37        );
  38        $form['precision'] = array(
  39          '#type' => 'textfield',
  40          '#title' => t('Precision'),
  41          '#default_value' => $this->options['precision'],
  42          '#description' => t('Specify how many digits to print after the decimal point.'),
  43          '#process' => array('views_process_dependency'),
  44          '#dependency' => array('edit-options-set-precision' => array(TRUE)),
  45          '#size' => 2,
  46        );
  47        $form['decimal'] = array(
  48          '#type' => 'textfield',
  49          '#title' => t('Decimal point'),
  50          '#default_value' => $this->options['decimal'],
  51          '#description' => t('What single character to use as a decimal point.'),
  52          '#size' => 2,
  53        );
  54      }
  55      $form['separator'] = array(
  56        '#type' => 'textfield',
  57        '#title' => t('Thousands separator'),
  58        '#default_value' => $this->options['separator'],
  59        '#description' => t('What single character to use as the thousands separator.'),
  60        '#size' => 2,
  61      );
  62      $form['format_plural'] = array(
  63        '#type' => 'checkbox',
  64        '#title' => t('Format plural'),
  65        '#description' => t('If checked, special handling will be used for plurality.'),
  66        '#default_value' => $this->options['format_plural'],
  67      );
  68      $form['format_plural_singular'] = array(
  69        '#type' => 'textfield',
  70        '#title' => t('Singular form'),
  71        '#default_value' => $this->options['format_plural_singular'],
  72        '#description' => t('Text to use for the singular form.'),
  73        '#process' => array('views_process_dependency'),
  74        '#dependency' => array('edit-options-format-plural' => array(TRUE)),
  75      );
  76      $form['format_plural_plural'] = array(
  77        '#type' => 'textfield',
  78        '#title' => t('Plural form'),
  79        '#default_value' => $this->options['format_plural_plural'],
  80        '#description' => t('Text to use for the plural form, @count will be replaced with the value.'),
  81        '#process' => array('views_process_dependency'),
  82        '#dependency' => array('edit-options-format-plural' => array(TRUE)),
  83      );
  84      $form['prefix'] = array(
  85        '#type' => 'textfield',
  86        '#title' => t('Prefix'),
  87        '#default_value' => $this->options['prefix'],
  88        '#description' => t('Text to put before the number, such as currency symbol.'),
  89      );
  90      $form['suffix'] = array(
  91        '#type' => 'textfield',
  92        '#title' => t('Suffix'),
  93        '#default_value' => $this->options['suffix'],
  94        '#description' => t('Text to put after the number, such as currency symbol.'),
  95      );
  96    }
  97  
  98    function render($values) {
  99      $value = $values->{$this->field_alias};
 100      if (!empty($this->options['set_precision'])) {
 101        $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
 102      }
 103      else {
 104        $remainder = abs($value) - intval(abs($value));
 105        $value = $value > 0 ? floor($value) : ceil($value);
 106        $value = number_format($value, 0, '', $this->options['separator']);
 107        if ($remainder) {
 108          // The substr may not be locale safe.
 109          $value .= $this->options['decimal'] . substr($remainder, 2);
 110        }
 111      }
 112  
 113      // Check to see if hiding should happen before adding prefix and suffix.
 114      if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
 115        return '';
 116      }
 117  
 118      // Should we format as a plural.
 119      if (!empty($this->options['format_plural'])) {
 120        $value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
 121      }
 122  
 123      return filter_xss($this->options['prefix']) . check_plain($value) . filter_xss($this->options['suffix']);
 124    }
 125  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7