2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Represents a floating point field.
|
2010-05-05 08:50:54 +02:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Float extends DBField {
|
2010-02-12 02:35:26 +01:00
|
|
|
|
|
|
|
function __construct($name, $defaultVal = 0) {
|
2010-04-13 06:05:03 +02:00
|
|
|
$this->defaultVal = is_float($defaultVal) ? $defaultVal : (float) 0;
|
2010-02-12 02:35:26 +01:00
|
|
|
|
|
|
|
parent::__construct($name);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
function requireField() {
|
2010-02-12 02:35:26 +01:00
|
|
|
$parts=Array('datatype'=>'float', 'null'=>'not null', 'default'=>$this->defaultVal, 'arrayValue'=>$this->arrayValue);
|
2008-11-23 02:01:03 +01:00
|
|
|
$values=Array('type'=>'float', 'parts'=>$parts);
|
|
|
|
DB::requireField($this->tableName, $this->name, $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2010-05-05 08:50:54 +02:00
|
|
|
/**
|
|
|
|
* Returns the number, with commas and decimal places as appropriate, eg “1,000.00”.
|
|
|
|
*
|
|
|
|
* @uses number_format()
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Nice() {
|
|
|
|
return number_format($this->value, 2);
|
2008-08-06 04:43:46 +02:00
|
|
|
}
|
|
|
|
|
2008-08-13 00:47:08 +02:00
|
|
|
function Round($precision = 3) {
|
|
|
|
return round($this->value, $precision);
|
|
|
|
}
|
|
|
|
|
|
|
|
function NiceRound($precision = 3) {
|
|
|
|
return number_format(round($this->value, $precision), $precision);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-04-12 08:02:22 +02:00
|
|
|
/**
|
|
|
|
* Returns the value to be set in the database to blank this field.
|
|
|
|
* Usually it's a choice between null, 0, and ''
|
|
|
|
*/
|
|
|
|
function nullValue() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-11 08:22:33 +02:00
|
|
|
/**
|
|
|
|
* Return an encoding of the given value suitable for inclusion in a SQL statement.
|
|
|
|
* If necessary, this should include quotes.
|
|
|
|
*/
|
|
|
|
function prepValueForDB($value) {
|
|
|
|
if($value === true) {
|
|
|
|
return 1;
|
2010-04-12 08:02:22 +02:00
|
|
|
}
|
|
|
|
if(!$value || !is_numeric($value)) {
|
|
|
|
if(strpos($value, '[') === false) {
|
2009-10-01 23:03:52 +02:00
|
|
|
return '0';
|
2010-04-12 08:02:22 +02:00
|
|
|
} else {
|
|
|
|
return Convert::raw2sql($value);
|
|
|
|
}
|
2008-09-11 08:22:33 +02:00
|
|
|
} else {
|
2010-04-12 08:02:22 +02:00
|
|
|
return Convert::raw2sql($value);
|
2008-09-11 08:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-12 08:02:22 +02:00
|
|
|
|
|
|
|
}
|