1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Fields; |
4
|
|
|
|
5
|
|
|
use WebTheory\Saveyour\Contracts\FormFieldInterface; |
6
|
|
|
|
7
|
|
|
class ToggleChecklist extends AbstractChecklist implements FormFieldInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Value for hidden input that facilitates unsetting single value on the server |
11
|
|
|
* |
12
|
|
|
* @var mixed |
13
|
|
|
*/ |
14
|
|
|
protected $toggleControl = '0'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get value for hidden input that facilitates unsetting single value on the server |
18
|
|
|
* |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public function getToggleControl() |
22
|
|
|
{ |
23
|
|
|
return $this->toggleControl; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set value for hidden input that facilitates unsetting single value on the server |
28
|
|
|
* |
29
|
|
|
* @param mixed $toggleControl |
30
|
|
|
* |
31
|
|
|
* @return self |
32
|
|
|
*/ |
33
|
|
|
public function setToggleControl($toggleControl) |
34
|
|
|
{ |
35
|
|
|
$this->toggleControl = $toggleControl; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
protected function createItemToggleControl(array $values): Hidden |
44
|
|
|
{ |
45
|
|
|
$basename = $values['name'] ?? ''; |
46
|
|
|
|
47
|
|
|
return (new Hidden()) |
48
|
|
|
->setName($this->name . "[{$basename}]") |
49
|
|
|
->setValue($this->toggleControl); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
*/ |
55
|
|
|
protected function createItemCheckBox(array $values): Checkbox |
56
|
|
|
{ |
57
|
|
|
$basename = $values['name']; |
58
|
|
|
|
59
|
|
|
return (new Checkbox()) |
60
|
|
|
->setId($values['id'] ?? '') |
61
|
|
|
->setValue($values['value'] ?? '') |
62
|
|
|
->setName($this->name . "[{$basename}]"); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
protected function defineChecklistItem(string $item, array $values) |
69
|
|
|
{ |
70
|
|
|
$html = ''; |
71
|
|
|
$html .= $this->createItemToggleControl($values); |
72
|
|
|
$html .= $this->createItemCheckBox($values)->setChecked($this->isItemChecked($item)); |
73
|
|
|
$html .= $this->createItemLabel($values)->setFor($values['id'] ?? ''); |
74
|
|
|
|
75
|
|
|
return $html; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
*/ |
81
|
|
|
public function renderHtmlMarkup(): string |
82
|
|
|
{ |
83
|
|
|
$html = ''; |
84
|
|
|
$html .= $this->open('div', $this->attributes ?? null); |
85
|
|
|
$html .= $this->open('ul'); |
86
|
|
|
|
87
|
|
|
foreach ($this->getItemsToRender() as $item => $values) { |
88
|
|
|
$html .= $this->open('li'); |
89
|
|
|
$html .= $this->defineChecklistItem($item, $values); |
90
|
|
|
$html .= $this->close('li'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$html .= $this->close('ul'); |
94
|
|
|
$html .= $this->close('div'); |
95
|
|
|
|
96
|
|
|
return $html; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|