2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Represents a boolean field.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Boolean extends DBField {
|
|
|
|
|
|
|
|
function __construct($name, $defaultVal = 0) {
|
|
|
|
$this->defaultVal = ($defaultVal) ? 1 : 0;
|
|
|
|
|
|
|
|
parent::__construct($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
function requireField() {
|
2008-11-23 02:01:03 +01:00
|
|
|
$parts=Array('datatype'=>'tinyint', 'precision'=>1, 'sign'=>'unsigned', 'null'=>'not null', 'default'=>$this->defaultVal);
|
|
|
|
$values=Array('type'=>'boolean', 'parts'=>$parts);
|
|
|
|
DB::requireField($this->tableName, $this->name, $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Nice() {
|
|
|
|
return ($this->value) ? "yes" : "no";
|
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
|
|
|
|
function NiceAsBoolean() {
|
|
|
|
return ($this->value) ? "true" : "false";
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves this field to the given data object.
|
|
|
|
*/
|
|
|
|
function saveInto($dataObject) {
|
|
|
|
$fieldName = $this->name;
|
|
|
|
if($fieldName) {
|
|
|
|
$dataObject->$fieldName = $this->value ? 1 : 0;
|
|
|
|
} else {
|
|
|
|
user_error("DBField::saveInto() Called on a nameless '$this->class' object", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2008-11-24 00:20:43 +01:00
|
|
|
|
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 CheckboxField($this->name, $title);
|
|
|
|
}
|
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) {
|
2008-11-24 20:28:46 +01:00
|
|
|
if($value === true || $value === 1 || $value === '1') {
|
2008-11-24 00:20:43 +01:00
|
|
|
return "'1'";
|
2008-09-11 08:22:33 +02:00
|
|
|
} if(!$value || !is_numeric($value)) {
|
2008-11-24 00:20:43 +01:00
|
|
|
return "'0'";
|
2008-09-11 08:22:33 +02:00
|
|
|
} else {
|
|
|
|
return addslashes($value);
|
|
|
|
}
|
|
|
|
}
|
2008-11-24 00:20:43 +01:00
|
|
|
|
|
|
|
function nullValue() {
|
|
|
|
return "'0'";
|
|
|
|
}
|
2008-09-11 08:22:33 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-11-23 02:01:03 +01:00
|
|
|
?>
|