|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use WebTheory\Saveyour\Concerns\MultiValueSelectionTrait; |
|
6
|
|
|
use WebTheory\Saveyour\Contracts\ChecklistItemsProviderInterface; |
|
7
|
|
|
use WebTheory\Saveyour\Contracts\FormFieldInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Checklist extends AbstractCompositeSelectionField implements FormFieldInterface |
|
10
|
|
|
{ |
|
11
|
|
|
use MultiValueSelectionTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $value = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Value for hidden input that facilitates unsetting all values on the server |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $clearControl = '0'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ChecklistItemsProviderInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $selectionProvider; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get value for hidden input that facilitates unsetting all values on the server |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getClearControl(): string |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->clearControl; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Set value for hidden input that facilitates unsetting all values on the server |
|
42
|
|
|
* |
|
43
|
|
|
* @param string |
|
44
|
|
|
* |
|
45
|
|
|
* @return self |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setClearControl(string $clearControl) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->clearControl = $clearControl; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the value of selectionProvider |
|
56
|
|
|
* |
|
57
|
|
|
* @return ChecklistItemsProviderInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getSelectionProvider(): ChecklistItemsProviderInterface |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->selectionProvider; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set the value of selectionProvider |
|
66
|
|
|
* |
|
67
|
|
|
* @param ChecklistItemsProviderInterface $selectionProvider |
|
68
|
|
|
* |
|
69
|
|
|
* @return self |
|
70
|
|
|
*/ |
|
71
|
|
|
public function setSelectionProvider(ChecklistItemsProviderInterface $selectionProvider) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->selectionProvider = $selectionProvider; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getSelectionItemsName() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->name . '[]'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function renderHtmlMarkup(): string |
|
90
|
|
|
{ |
|
91
|
|
|
$clearControl = $this->createClearControlField() |
|
92
|
|
|
->setName($this->getSelectionItemsName()) |
|
93
|
|
|
->setValue($this->clearControl); |
|
94
|
|
|
|
|
95
|
|
|
$list = $this->tag('ul', $this->renderSelection()); |
|
96
|
|
|
|
|
97
|
|
|
return $this->tag('div', $clearControl . $list, $this->attributes); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function createClearControlField(): Hidden |
|
104
|
|
|
{ |
|
105
|
|
|
return new Hidden(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function renderSelection(): string |
|
112
|
|
|
{ |
|
113
|
|
|
$html = ''; |
|
114
|
|
|
|
|
115
|
|
|
foreach ($this->getSelectionData() as $selection) { |
|
116
|
|
|
$id = $this->defineSelectionId($selection); |
|
117
|
|
|
$value = $this->defineSelectionValue($selection); |
|
118
|
|
|
|
|
119
|
|
|
$checkbox = $this->createSelectionCheckbox($selection) |
|
120
|
|
|
->setChecked($this->isSelectionSelected($value)) |
|
121
|
|
|
->setName($this->getSelectionItemsName()) |
|
122
|
|
|
->setValue($value) |
|
123
|
|
|
->setId($id); |
|
124
|
|
|
|
|
125
|
|
|
$label = $this->createSelectionLabel($selection)->setFor($id); |
|
126
|
|
|
|
|
127
|
|
|
$html .= $this->tag('li', $checkbox . $label); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $html; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function createSelectionCheckbox($selection): Checkbox |
|
|
|
|
|
|
137
|
|
|
{ |
|
138
|
|
|
return new Checkbox(); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.