2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-08-27 23:07:55 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Model\FieldType;
|
|
|
|
|
|
|
|
use DB;
|
|
|
|
use NumericField;
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Represents a floating point field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-08-27 23:07:55 +02:00
|
|
|
class DBFloat extends DBField {
|
2010-04-14 05:45:20 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name = null, $defaultVal = 0) {
|
2010-10-13 03:45:24 +02:00
|
|
|
$this->defaultVal = is_float($defaultVal) ? $defaultVal : (float) 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-14 05:45:20 +02:00
|
|
|
parent::__construct($name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requireField() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$parts = Array(
|
2012-09-26 23:34:00 +02:00
|
|
|
'datatype'=>'float',
|
|
|
|
'null'=>'not null',
|
|
|
|
'default'=>$this->defaultVal,
|
2013-06-21 00:32:08 +02:00
|
|
|
'arrayValue'=>$this->arrayValue
|
|
|
|
);
|
|
|
|
$values = Array('type'=>'float', 'parts'=>$parts);
|
|
|
|
DB::require_field($this->tableName, $this->name, $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 01:58:09 +02:00
|
|
|
/**
|
|
|
|
* Returns the number, with commas and decimal places as appropriate, eg “1,000.00”.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
* @uses number_format()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Nice() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return number_format($this->value, 2);
|
2008-08-06 04:43:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Round($precision = 3) {
|
2008-08-13 00:47:08 +02:00
|
|
|
return round($this->value, $precision);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function NiceRound($precision = 3) {
|
2008-08-13 00:47:08 +02:00
|
|
|
return number_format(round($this->value, $precision), $precision);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-06 04:43:46 +02:00
|
|
|
public function scaffoldFormField($title = null) {
|
|
|
|
return new NumericField($this->name, $title);
|
|
|
|
}
|
2008-09-11 08:22:33 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function nullValue() {
|
2010-10-13 03:43:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2010-10-13 03:43:58 +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
|
|
|
}
|
2010-10-13 03:43:58 +02:00
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|