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() {
|
2009-09-30 00:00:27 +02:00
|
|
|
$parts=Array('datatype'=>'tinyint', 'precision'=>1, 'sign'=>'unsigned', 'null'=>'not null', 'default'=>$this->defaultVal, 'arrayValue'=>$this->arrayValue);
|
2008-11-23 02:01:03 +01:00
|
|
|
$values=Array('type'=>'boolean', 'parts'=>$parts);
|
|
|
|
DB::requireField($this->tableName, $this->name, $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Nice() {
|
2009-04-29 03:20:24 +02:00
|
|
|
return ($this->value) ? _t('Boolean.YES', 'Yes') : _t('Boolean.NO', 'No');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
|
|
|
|
function NiceAsBoolean() {
|
2009-04-29 03:20:24 +02:00
|
|
|
return ($this->value) ? 'true' : 'false';
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves this field to the given data object.
|
|
|
|
*/
|
|
|
|
function saveInto($dataObject) {
|
|
|
|
$fieldName = $this->name;
|
|
|
|
if($fieldName) {
|
2009-04-29 03:20:24 +02:00
|
|
|
$dataObject->$fieldName = ($this->value) ? 1 : 0;
|
2007-07-19 12:40:28 +02:00
|
|
|
} 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);
|
|
|
|
}
|
2009-04-29 03:20:24 +02:00
|
|
|
|
|
|
|
public function scaffoldSearchField($title = null) {
|
|
|
|
$anyText = _t('Boolean.ANY', 'Any');
|
|
|
|
$source = array(
|
|
|
|
1 => _t('Boolean.YES', 'Yes'),
|
|
|
|
0 => _t('Boolean.NO', 'No')
|
|
|
|
);
|
|
|
|
|
|
|
|
return new DropdownField($this->name, $title, $source, '', null, "($anyText)");
|
|
|
|
}
|
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) {
|
2009-10-01 23:03:52 +02:00
|
|
|
if(strpos($value, '[')!==false)
|
|
|
|
return addslashes($value);
|
|
|
|
else {
|
|
|
|
if($value && strtolower($value) != 'f') {
|
|
|
|
return "'1'";
|
|
|
|
} else {
|
|
|
|
return "'0'";
|
|
|
|
}
|
2008-09-11 08:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
?>
|