Version: current

     1 <?php
     2 /**
     3  * Represents a Decimal field.
     4  * @package sapphire
     5  * @subpackage model
     6  */
     7
     8 //require_once 'Zend/Currency.php';
     9
    10 class Decimal extends DBField {
    11     protected $wholeSize, $decimalSize, $defaultValue;
    12
    13     public static $thousand_sep = ' ';
    14     public static $decimal_sep = '.';
    15
    16     /**
    17      * Create a new Decimal field.
    18      */
    19     function __construct($name, $wholeSize = 9, $decimalSize = 2, $defaultValue = 0) {
    20         $this->wholeSize = isset($wholeSize) ? $wholeSize : 9;
    21         $this->decimalSize = isset($decimalSize) ? $decimalSize : 2;
    22         $this->defaultValue = $defaultValue;
    23         parent::__construct($name);
    24     }
    25     function setDecimal($value) {
    26         $this->decimalSize = $value;
    27     }
    28
    29     function Nice($decimalSize = false) {
    30         if ($decimalSize === false)
    31             $decimalSize = $this->decimalSize;
    32         return number_format($this->value, $decimalSize, self::$decimal_sep, self::$thousand_sep);
    33     }
    34
    35     function Int() {
    36         return ceil( $this->value );
    37     }
    38
    39     function hasValue() {
    40         return ($this->value != 0);
    41     }
    42
    43     function requireField() {
    44         $parts=Array('datatype'=>'decimal', 'precision'=>"$this->wholeSize,$this->decimalSize", 'default'=>sprintf("%.{$this->decimalSize}f", $this->defaultValue), 'arrayValue'=>$this->arrayValue);
    45         $values=Array('type'=>'decimal', 'parts'=>$parts);
    46         DB::requireField($this->tableName, $this->name, $values);
    47     }
    48
    49     function saveInto($dataObject) {
    50         $fieldName = $this->name;
    51         if($fieldName) {
    52             $dataObject->$fieldName = (float)preg_replace('/[^0-9.\-\+]/', '', $this->value);
    53         } else {
    54             user_error("DBField::saveInto() Called on a nameless '" . get_class($this) . "' object", E_USER_ERROR);
    55         }
    56     }
    57
    58     public function scaffoldFormField($title = null, $params = null) {
    59         return new NumericField($this->name, $title);
    60     }
    61
    62     public function nullValue() {
    63         return "0.00";
    64     }
    65
    66     /**
    67      * Return an encoding of the given value suitable for inclusion in a SQL statement.
    68      * If necessary, this should include quotes.
    69      */
    70     function prepValueForDB($value) {
    71         if($value === true) {
    72             return 1;
    73         } if(!$value || !is_numeric($value)) {
    74             if(strpos($value, '[')===false)
    75                 return '0';
    76             else
    77                 return addslashes($value);
    78         } else {
    79                         $value = str_replace(',', '.', $value);
    80             return '\''.addslashes($value).'\'';
    81         }
    82     }
    83
    84 }
    85
    86 ?>
    87

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -