2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM\FieldType;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\NumericField;
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Represents a Decimal field.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-08-30 07:02:55 +02:00
|
|
|
class DBDecimal extends DBField {
|
2013-04-06 12:20:20 +02:00
|
|
|
|
2010-04-13 03:53:13 +02:00
|
|
|
protected $wholeSize, $decimalSize, $defaultValue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Create a new Decimal field.
|
2013-04-06 12:20:20 +02:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param int $wholeSize
|
|
|
|
* @param int $decimalSize
|
2016-07-01 04:26:18 +02:00
|
|
|
* @param float|int $defaultValue
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name = null, $wholeSize = 9, $decimalSize = 2, $defaultValue = 0) {
|
2013-04-06 12:20:20 +02:00
|
|
|
$this->wholeSize = is_int($wholeSize) ? $wholeSize : 9;
|
|
|
|
$this->decimalSize = is_int($decimalSize) ? $decimalSize : 2;
|
|
|
|
|
|
|
|
$this->defaultValue = number_format((float) $defaultValue, $decimalSize);;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct($name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:20:20 +02:00
|
|
|
/**
|
|
|
|
* @return float
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Nice() {
|
2013-04-06 12:20:20 +02:00
|
|
|
return number_format($this->value, $this->decimalSize);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:20:20 +02:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Int() {
|
2013-04-06 12:20:20 +02:00
|
|
|
return floor($this->value);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requireField() {
|
2013-04-06 12:20:20 +02:00
|
|
|
$parts = array(
|
|
|
|
'datatype' => 'decimal',
|
|
|
|
'precision' => "$this->wholeSize,$this->decimalSize",
|
|
|
|
'default' => $this->defaultValue,
|
|
|
|
'arrayValue' => $this->arrayValue
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:20:20 +02:00
|
|
|
$values = array(
|
|
|
|
'type' => 'decimal',
|
|
|
|
'parts' => $parts
|
|
|
|
);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::require_field($this->tableName, $this->name, $values);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function saveInto($dataObject) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$fieldName = $this->name;
|
2013-04-06 12:20:20 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
if($fieldName) {
|
2008-11-05 05:10:48 +01:00
|
|
|
$dataObject->$fieldName = (float)preg_replace('/[^0-9.\-\+]/', '', $this->value);
|
2008-02-25 03:10:37 +01:00
|
|
|
} else {
|
|
|
|
user_error("DBField::saveInto() Called on a nameless '" . get_class($this) . "' object", E_USER_ERROR);
|
|
|
|
}
|
2008-08-06 04:43:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-04-06 12:20:20 +02:00
|
|
|
/**
|
|
|
|
* @param string $title
|
|
|
|
* @param array $params
|
|
|
|
*
|
|
|
|
* @return NumericField
|
|
|
|
*/
|
2008-10-14 00:20:41 +02:00
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
2008-08-06 04:43:46 +02:00
|
|
|
return new NumericField($this->name, $title);
|
|
|
|
}
|
2013-04-06 12:20:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return float
|
|
|
|
*/
|
2009-02-02 00:49:53 +01:00
|
|
|
public function nullValue() {
|
2013-06-21 00:32:08 +02:00
|
|
|
return 0;
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2008-09-11 08:22:33 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function prepValueForDB($value) {
|
2008-09-11 08:22:33 +02:00
|
|
|
if($value === true) {
|
|
|
|
return 1;
|
2013-06-21 00:32:08 +02:00
|
|
|
} elseif(empty($value) || !is_numeric($value)) {
|
|
|
|
return 0;
|
2008-09-11 08:22:33 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
return $value;
|
2008-09-11 08:22:33 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|