1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Checkbox input field. |
8
|
|
|
* |
9
|
|
|
* @package Formgenerator |
10
|
|
|
* @author Stefanius <[email protected]> |
11
|
|
|
* @copyright MIT License - see the LICENSE file for details |
12
|
|
|
*/ |
13
|
|
|
class FormCheck extends FormInput |
14
|
|
|
{ |
15
|
|
|
/** @var string value for the button, if checked (default = 'on') */ |
16
|
|
|
protected string $strBtnValue = 'on'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create a checkbox. |
20
|
|
|
* As default, the value 'on' is submitted for a checked element. This value can be |
21
|
|
|
* changed with the setBtnValue() method.<br/> |
22
|
|
|
* @param string $strName name AND id of the element |
23
|
|
|
* @param int $wFlags (default: 0) |
24
|
|
|
* @param string $strSuffix Text after the checkbox (default: '') |
25
|
|
|
*/ |
26
|
|
|
public function __construct(string $strName, int $wFlags = 0, string $strSuffix = '') |
27
|
|
|
{ |
28
|
|
|
$this->oFlags = new FormFlags($wFlags); |
29
|
|
|
$this->strName = $strName; |
30
|
|
|
$this->strSuffix = $strSuffix; |
31
|
|
|
if ($this->oFlags->isSet(FormFlags::READ_ONLY | FormFlags::DISABLED)) { |
32
|
|
|
$this->addAttribute('disabled'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the value that is posted, when the element is checked. |
38
|
|
|
* @param string $strBtnValue |
39
|
|
|
*/ |
40
|
|
|
public function setBtnValue(string $strBtnValue) : void |
41
|
|
|
{ |
42
|
|
|
$this->strBtnValue = $strBtnValue; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Build the HTML-markup for the checkbox. |
47
|
|
|
* Checkbox is a special case, as it behaves particularly with regard to the transferred |
48
|
|
|
* value and does not support read-only mode. |
49
|
|
|
* There is special treatment for both cases, which is explained in more detail in the |
50
|
|
|
* respective code areas bolow. |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getHTML() : string |
54
|
|
|
{ |
55
|
|
|
$this->processFlags(); |
56
|
|
|
$bChecked = $this->getBoolValue(); |
57
|
|
|
if ($bChecked) { |
58
|
|
|
$this->addAttribute('checked'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$strHTML = $this->buildContainerDiv(); |
62
|
|
|
|
63
|
|
|
$strHTML .= $this->buildUncheckedSurrogate(); |
64
|
|
|
$strHTML .= $this->buildCheckBox(); |
65
|
|
|
$strHTML .= $this->buildReadonlySurrogate($bChecked); |
66
|
|
|
|
67
|
|
|
if (!empty($this->strSuffix)) { |
68
|
|
|
$strHTML .= ' ' . $this->strSuffix; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$strHTML .= '</div>' . PHP_EOL; |
72
|
|
|
|
73
|
|
|
return $strHTML; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the value as bool. <ul> |
78
|
|
|
* <li> 'real' bool values as is </li> |
79
|
|
|
* <li> numeric: 0 -> false, all other -> true </li> |
80
|
|
|
* <li> string: '1', 'on', 'true', 'yes' (case insensitive) -> true, all other -> false </li></ul> |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
protected function getBoolValue() : bool |
84
|
|
|
{ |
85
|
|
|
$bChecked = false; |
86
|
|
|
$value = $this->oFG->getData()->getValue($this->strName); |
87
|
|
|
if (is_bool($value)) { |
88
|
|
|
$bChecked = $value; |
89
|
|
|
} else { |
90
|
|
|
if (is_numeric($value)) { |
91
|
|
|
$bChecked = ($value !== 0); |
92
|
|
|
} else { |
93
|
|
|
$value = strtolower($value); |
94
|
|
|
$bChecked = in_array($value, ['1', 'on', 'true', 'yes']); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return $bChecked; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Build the checkbox. |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
protected function buildCheckBox() : string |
105
|
|
|
{ |
106
|
|
|
$strHTML = '<input type="checkbox"'; |
107
|
|
|
$strHTML .= $this->buildStyle(); |
108
|
|
|
$strHTML .= $this->buildAttributes(); |
109
|
|
|
$strHTML .= $this->buildTabindex(); |
110
|
|
|
$strHTML .= ' id="' . $this->strName . '"'; |
111
|
|
|
if (!$this->oFlags->isSet(FormFlags::READ_ONLY | FormFlags::DISABLED)) { |
112
|
|
|
$strHTML .= ' name="' . $this->strName . '"'; |
113
|
|
|
$strHTML .= ' value="' . $this->strBtnValue . '"'; |
114
|
|
|
} |
115
|
|
|
$strHTML .= '>'; |
116
|
|
|
|
117
|
|
|
return $strHTML; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Build the surrogate for unchecked box. |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
protected function buildUncheckedSurrogate() : string |
125
|
|
|
{ |
126
|
|
|
// We insert a hidden edit field with same name/id BEFORE we create the checkbox |
127
|
|
|
// and alwas set its value to 'off'. |
128
|
|
|
// If the checkbox is activated, 'on' (or the value set with BtnValue) is posted as normal. |
129
|
|
|
// However, since the value of a non-activated checkbox is not transferred at all, in this |
130
|
|
|
// case the hidden field defined in the previous order comes into play and the fixed value |
131
|
|
|
// 'off' is transferred. |
132
|
|
|
// This is particularly helpful/important if a database operation is to be carried out |
133
|
|
|
// dynamically on the basis of the transferred fields: If the 'off' were not transferred, |
134
|
|
|
// the field would not be taken into account either and would therefore never be set from |
135
|
|
|
// 'on' to 'off'!! |
136
|
|
|
$strHTML = '<input type="hidden" value="off" name="' . $this->strName . '">'; |
137
|
|
|
|
138
|
|
|
return $strHTML; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Build the surrogate in case of readonly checkbox. |
143
|
|
|
* @param bool $bChecked |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
protected function buildReadonlySurrogate(bool $bChecked) : string |
147
|
|
|
{ |
148
|
|
|
// NOTE: because checkboxes don't support readonly-attribute and disabled checkboxes |
149
|
|
|
// are not posted to reciever, we insert an hidden field with name and id to keep |
150
|
|
|
// value 'alive' |
151
|
|
|
// -> so we dont set name and id for readonly or disabled checkbox! |
152
|
|
|
$strHTML = ''; |
153
|
|
|
if ($this->oFlags->isSet(FormFlags::READ_ONLY | FormFlags::DISABLED)) { |
154
|
|
|
$strHTML .= '<input'; |
155
|
|
|
$strHTML .= ' type="hidden"'; |
156
|
|
|
$strHTML .= ' name="' . $this->strName . '"'; |
157
|
|
|
$strValue = ($bChecked) ? $this->strBtnValue : 'off'; |
158
|
|
|
$strHTML .= ' value="' . $strValue . '">'; |
159
|
|
|
} |
160
|
|
|
return $strHTML; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|