silverstripe-framework/model/FieldType/DBBoolean.php
Sam Minnee aeccb8b8e0 API: Move DBField subclasses into SilverStripe\Model\FieldType namespace
API: Deprecate SS_Datetime.

The DBField subclasses are have all been renamed to start with “DB” and
be in the SilverStripe\Model\FieldType namespace. To keep DataObject
definitions concise, the original short variations of their names are
preserved as service definitions. Most of the field generation code
doesn’t need to change, but where field classes are referenced directly,
changes will be needed.

SS_Datetime, which is commonly referenced outside the model system
itself, has been preserved as a subclass of DBDatetime. This has been
marked as deprecated and can be removed in SilverStripe 5.

A few places that referred to $db and $casting values weren’t using
the Injector to instantiate the relevant classes. This meant that the
remapping we have created as part of moving classes into a namespace
didn’t work.
2016-03-22 18:09:30 +13:00

96 lines
2.0 KiB
PHP

<?php
namespace SilverStripe\Model\FieldType;
use DB;
use CheckboxField;
use DropdownField;
/**
* Represents a boolean field.
*
* @package framework
* @subpackage model
*/
class DBBoolean extends DBField {
public function __construct($name = null, $defaultVal = 0) {
$this->defaultVal = ($defaultVal) ? 1 : 0;
parent::__construct($name);
}
public function requireField() {
$parts=Array(
'datatype'=>'tinyint',
'precision'=>1,
'sign'=>'unsigned',
'null'=>'not null',
'default'=>$this->defaultVal,
'arrayValue'=>$this->arrayValue
);
$values=Array('type'=>'boolean', 'parts'=>$parts);
DB::require_field($this->tableName, $this->name, $values);
}
public function Nice() {
return ($this->value) ? _t('Boolean.YESANSWER', 'Yes') : _t('Boolean.NOANSWER', 'No');
}
public function NiceAsBoolean() {
return ($this->value) ? 'true' : 'false';
}
/**
* Saves this field to the given data object.
*/
public 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);
}
}
public function scaffoldFormField($title = null, $params = null) {
return new CheckboxField($this->name, $title);
}
public function scaffoldSearchField($title = null) {
$anyText = _t('Boolean.ANY', 'Any');
$source = array(
1 => _t('Boolean.YESANSWER', 'Yes'),
0 => _t('Boolean.NOANSWER', 'No')
);
$field = new DropdownField($this->name, $title, $source);
$field->setEmptyString("($anyText)");
return $field;
}
public function nullValue() {
return 0;
}
public function prepValueForDB($value) {
if(is_bool($value)) {
return $value ? 1 : 0;
} else if(empty($value)) {
return 0;
} else if(is_string($value)){
switch(strtolower($value)) {
case 'false':
case 'f':
return 0;
case 'true':
case 't':
return 1;
}
}
return $value ? 1 : 0;
}
}