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