2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Represents a boolean 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
|
|
|
*/
|
|
|
|
class Boolean extends DBField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($name = null, $defaultVal = 0) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->defaultVal = ($defaultVal) ? 1 : 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct($name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requireField() {
|
2012-09-26 23:34:00 +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);
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::require_field($this->tableName, $this->name, $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Nice() {
|
2013-04-12 01:09:53 +02:00
|
|
|
return ($this->value) ? _t('Boolean.YESANSWER', 'Yes') : _t('Boolean.NOANSWER', 'No');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function saveInto($dataObject) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$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);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
public function scaffoldSearchField($title = null) {
|
|
|
|
$anyText = _t('Boolean.ANY', 'Any');
|
|
|
|
$source = array(
|
2013-04-12 01:09:53 +02:00
|
|
|
1 => _t('Boolean.YESANSWER', 'Yes'),
|
|
|
|
0 => _t('Boolean.NOANSWER', 'No')
|
2009-04-29 03:20:24 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-23 12:45:04 +02:00
|
|
|
$field = new DropdownField($this->name, $title, $source);
|
|
|
|
$field->setEmptyString("($anyText)");
|
2012-05-24 04:13:11 +02:00
|
|
|
return $field;
|
2009-04-29 03:20:24 +02:00
|
|
|
}
|
2008-09-11 08:22:33 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
public function nullValue() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function prepValueForDB($value) {
|
2013-06-21 00:32:08 +02:00
|
|
|
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;
|
2009-10-01 23:03:52 +02:00
|
|
|
}
|
2008-09-11 08:22:33 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
return $value ? 1 : 0;
|
2008-09-11 08:22:33 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|