|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_forms\Exceptions\RenderException; |
|
7
|
|
|
use kalanis\kw_forms\Interfaces\IMultiValue; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Definition of form controls Group of Checkboxes |
|
12
|
|
|
* |
|
13
|
|
|
* <b>Examples</b> |
|
14
|
|
|
* <code> |
|
15
|
|
|
* // render form for set 2 values, second will be empty |
|
16
|
|
|
* $form = new Form(); |
|
17
|
|
|
* $checkboxes = $form->addCheckboxes('fotos', 'Choose files'); |
|
18
|
|
|
* $checkboxes->addCheckbox('file1', 'File 1')); |
|
19
|
|
|
* $checkboxes->addCheckbox('file2', 'File 2', true)); |
|
20
|
|
|
* echo $form; |
|
21
|
|
|
* |
|
22
|
|
|
* // render form for setting 5 values, first two will be set |
|
23
|
|
|
* $form = new Form(); |
|
24
|
|
|
* for($i=1;$i<6;$i++) { |
|
25
|
|
|
* $files[] = $form->getControlFactory()->getCheckbox('', 1, 'File '.$i); |
|
26
|
|
|
* } |
|
27
|
|
|
* $checkboxes = $form->addCheckboxes('fotos', 'Select files', ['0', 1], $files); |
|
28
|
|
|
* echo $form; |
|
29
|
|
|
* </code> |
|
30
|
|
|
*/ |
|
31
|
|
|
class Checkboxes extends AControl implements IMultiValue |
|
32
|
|
|
{ |
|
33
|
|
|
use TShorterKey; |
|
34
|
|
|
|
|
35
|
|
|
protected string $templateLabel = '<label>%2$s</label>'; |
|
36
|
|
|
protected string $templateInput = '%3$s'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Create group of form elements Checkbox |
|
40
|
|
|
* @param string $alias |
|
41
|
|
|
* @param array<string, string|int|float|bool> $value |
|
42
|
|
|
* @param string|null $label |
|
43
|
|
|
* @param iterable<string, string|Checkbox> $children |
|
44
|
|
|
* @return $this |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function set(string $alias, array $value = [], ?string $label = null, iterable $children = array()): self |
|
47
|
|
|
{ |
|
48
|
2 |
|
$this->alias = $alias; |
|
49
|
2 |
|
$this->setLabel($label); |
|
50
|
|
|
|
|
51
|
2 |
|
foreach ($children as $childLabel => $childValue) { |
|
52
|
2 |
|
if ($childValue instanceof Checkbox) { |
|
53
|
1 |
|
$this->addChild($childValue, $childLabel); |
|
54
|
|
|
} else { |
|
55
|
2 |
|
$this->addCheckbox(strval($childLabel), strval($childValue), $childValue); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
if (!empty($value)) { |
|
60
|
2 |
|
$this->setValues($value); |
|
61
|
|
|
} |
|
62
|
2 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Create single Checkbox element |
|
67
|
|
|
* @param string $alias |
|
68
|
|
|
* @param string $label |
|
69
|
|
|
* @param string|int|float|null $value |
|
70
|
|
|
* @param boolean $checked |
|
71
|
|
|
* @return Checkbox |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function addCheckbox(string $alias, string $label, $value, ?bool $checked = null): Checkbox |
|
74
|
|
|
{ |
|
75
|
2 |
|
$checkbox = new Checkbox(); |
|
76
|
2 |
|
$checkbox->set($alias, $value, $label); |
|
77
|
2 |
|
$checkbox->setValue(strval(intval($checked))); |
|
78
|
2 |
|
$this->addChild($checkbox); |
|
79
|
2 |
|
return $checkbox; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get statuses of all children |
|
84
|
|
|
* @return array<string, string|int|float|bool|null> |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function getValues(): array |
|
87
|
|
|
{ |
|
88
|
1 |
|
$array = array(); |
|
89
|
1 |
|
foreach ($this->children as $child) { |
|
90
|
1 |
|
if ($child instanceof Checkbox) { |
|
91
|
1 |
|
$array[$child->getKey()] = $child->getValue(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
1 |
|
return $array; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set values to all children |
|
99
|
|
|
* !! UNDEFINED values will be SET too !! |
|
100
|
|
|
* @param array<string, string|int|float|bool|null> $data |
|
101
|
|
|
*/ |
|
102
|
2 |
|
public function setValues(array $data = []): void |
|
103
|
|
|
{ |
|
104
|
2 |
|
foreach ($this->children as $child) { |
|
105
|
2 |
|
if ($child instanceof Checkbox) { |
|
106
|
2 |
|
$shortKey = $this->shorterKey($child->getKey()); |
|
107
|
2 |
|
$child->setValue( |
|
108
|
2 |
|
$data[$shortKey] ?? ( |
|
109
|
2 |
|
$data[$child->getKey()] ?? '' |
|
110
|
|
|
) |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
2 |
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
public function renderInput($attributes = null): string |
|
117
|
|
|
{ |
|
118
|
1 |
|
return $this->wrapIt(sprintf($this->templateInput, '', '', $this->renderChildren()), $this->wrappersInput); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Render all children, add missing prefixes |
|
123
|
|
|
* @throws RenderException |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function renderChildren(): string |
|
127
|
|
|
{ |
|
128
|
1 |
|
$return = ''; |
|
129
|
1 |
|
foreach ($this->children as $child) { |
|
130
|
1 |
|
if ($child instanceof AControl) { |
|
131
|
1 |
|
$child->setAttribute('id', $this->getAlias() . '_' . $child->getKey()); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
$return .= $this->wrapIt($child->render(), $this->wrappersChild) . PHP_EOL; |
|
135
|
|
|
} |
|
136
|
1 |
|
return $this->wrapIt($return, $this->wrappersChildren); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|