1 <?php
2 3 4 5 6
7 class Boolean extends DBField {
8
9 function __construct($name, $defaultVal = 0) {
10 $this->defaultVal = ($defaultVal) ? 1 : 0;
11
12 parent::__construct($name);
13 }
14
15 function requireField() {
16 $parts=Array('datatype'=>'tinyint', 'precision'=>1, 'sign'=>'unsigned', 'null'=>'not null', 'default'=>$this->defaultVal, 'arrayValue'=>$this->arrayValue);
17 $values=Array('type'=>'boolean', 'parts'=>$parts);
18 DB::requireField($this->tableName, $this->name, $values);
19 }
20
21 function Nice() {
22 return ($this->value) ? _t('Boolean.YES', 'Yes') : _t('Boolean.NO', 'No');
23 }
24
25 function NiceAsBoolean() {
26 return ($this->value) ? 'true' : 'false';
27 }
28
29 30 31
32 function saveInto($dataObject) {
33 $fieldName = $this->name;
34 if($fieldName) {
35 $dataObject->$fieldName = ($this->value) ? 1 : 0;
36 } else {
37 user_error("DBField::saveInto() Called on a nameless '$this->class' object", E_USER_ERROR);
38 }
39 }
40
41 public function scaffoldFormField($title = null, $params = null) {
42 return new CheckboxField($this->name, $title);
43 }
44
45 public function scaffoldSearchField($title = null) {
46 $anyText = _t('Boolean.ANY', 'Any');
47 $source = array(
48 1 => _t('Boolean.YES', 'Yes'),
49 0 => _t('Boolean.NO', 'No')
50 );
51
52 return new DropdownField($this->name, $title, $source, '', null, "($anyText)");
53 }
54
55 56 57 58
59 function prepValueForDB($value) {
60 if(strpos($value, '[')!==false)
61 return addslashes($value);
62 else {
63 if($value && strtolower($value) != 'f') {
64 return "'1'";
65 } else {
66 return "'0'";
67 }
68 }
69 }
70
71 function nullValue() {
72 return "'0'";
73 }
74
75 }
76
77 ?>
78
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-