1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* RadioSet definition - add group of radios into your form |
8
|
|
|
* |
9
|
|
|
* <b>Usage:</b> |
10
|
|
|
* <code> |
11
|
|
|
* $radio1 = new Controls\Radio(); |
12
|
|
|
* $radio1->set(0, 'yes', 'NO'); |
13
|
|
|
* $radios[] = $radio1; |
14
|
|
|
* $radio2 = new Controls\Radio(); |
15
|
|
|
* $radio2->set(1, 'no', 'YES'); |
16
|
|
|
* $radios[] = $radio2; |
17
|
|
|
* |
18
|
|
|
* $radio = new Controls\RadioSet(); |
19
|
|
|
* $radio->set('alias', 1, null, $radios); |
20
|
|
|
* echo $radio->getValue() // no |
21
|
|
|
* |
22
|
|
|
* $form->addRadios('accessKey')->addOption(111, 'yes')->addOption(222, 'no'); |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class RadioSet extends AControl |
26
|
|
|
{ |
27
|
|
|
protected string $templateInput = '%3$s'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Add group of elements of form entries Radio |
31
|
|
|
* @param string $key |
32
|
|
|
* @param string|int|float|null $value |
33
|
|
|
* @param string $label |
34
|
|
|
* @param iterable<string, string|int|Radio> $children |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
2 |
|
public function set(string $key, $value = null, string $label = '', iterable $children = []): self |
38
|
|
|
{ |
39
|
2 |
|
$this->setEntry($key, '', $label); |
40
|
2 |
|
foreach ($children as $alias => $child) { |
41
|
1 |
|
if ($child instanceof Radio) { |
42
|
1 |
|
$child->setParent($this); |
43
|
1 |
|
$this->addChild($child); |
44
|
1 |
|
} elseif (is_string($child)) { |
45
|
1 |
|
$this->addOption($key, $alias, $child, strval(intval(strval($value) == $alias))); |
46
|
|
|
} |
47
|
|
|
} |
48
|
2 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add radio option into current set |
53
|
|
|
* @param string $alias |
54
|
|
|
* @param string|int|float|null $value |
55
|
|
|
* @param string $label |
56
|
|
|
* @param string $selected |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
1 |
|
public function addOption(string $alias, $value, string $label = '', string $selected = ''): self |
60
|
|
|
{ |
61
|
1 |
|
$radio = new Radio(); |
62
|
1 |
|
$this->addChild($radio->set($alias, $value, $label, $selected)); |
63
|
1 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set checked for selected alias; rest will be unchecked |
68
|
|
|
* @param string|int|float|bool|null $value |
69
|
|
|
*/ |
70
|
1 |
|
public function setValue($value): void |
71
|
|
|
{ |
72
|
1 |
|
foreach ($this->children as $child) { |
73
|
1 |
|
if ($child instanceof Radio) { |
74
|
1 |
|
$child->setValue($child->getOriginalValue() == strval($value)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns value of selected radio from set |
81
|
|
|
* @return string|int|float|bool|null |
82
|
|
|
*/ |
83
|
1 |
|
public function getValue() |
84
|
|
|
{ |
85
|
1 |
|
foreach ($this->children as $child) { |
86
|
1 |
|
if (($child instanceof Radio) && $child->getValue()) { |
87
|
1 |
|
return $child->getOriginalValue(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
1 |
|
return ''; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|