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 removing all values on the server |
20
|
|
|
* if no values are selected in the form. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $clearControl = '0'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ChecklistItemsProviderInterface |
28
|
|
|
*/ |
29
|
|
|
protected $selectionProvider; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the value of selectionProvider |
33
|
|
|
* |
34
|
|
|
* @return ChecklistItemsProviderInterface |
35
|
|
|
*/ |
36
|
|
|
public function getSelectionProvider(): ChecklistItemsProviderInterface |
37
|
|
|
{ |
38
|
|
|
return $this->selectionProvider; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set the value of selectionProvider |
43
|
|
|
* |
44
|
|
|
* @param ChecklistItemsProviderInterface $selectionProvider |
45
|
|
|
* |
46
|
|
|
* @return self |
47
|
|
|
*/ |
48
|
|
|
public function setSelectionProvider(ChecklistItemsProviderInterface $selectionProvider) |
49
|
|
|
{ |
50
|
|
|
$this->selectionProvider = $selectionProvider; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
|
|
protected function getSelectionItemsName(): string |
59
|
|
|
{ |
60
|
|
|
return $this->name . '[]'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
|
|
protected function renderHtmlMarkup(): string |
67
|
|
|
{ |
68
|
|
|
$clearControl = $this->createClearControlField() |
69
|
|
|
->setName($this->getSelectionItemsName()) |
70
|
|
|
->setValue($this->clearControl); |
71
|
|
|
|
72
|
|
|
$list = $this->tag('ul', [], $this->renderSelection()); |
73
|
|
|
|
74
|
|
|
return $this->tag('div', $this->attributes, $clearControl . $list); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* |
79
|
|
|
*/ |
80
|
|
|
protected function createClearControlField(): Hidden |
81
|
|
|
{ |
82
|
|
|
return new Hidden(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
*/ |
88
|
|
|
protected function renderSelection(): string |
89
|
|
|
{ |
90
|
|
|
$html = ''; |
91
|
|
|
|
92
|
|
|
foreach ($this->getSelectionData() as $selection) { |
93
|
|
|
$id = $this->defineSelectionId($selection); |
94
|
|
|
$value = $this->defineSelectionValue($selection); |
95
|
|
|
|
96
|
|
|
$checkbox = $this->createSelectionCheckbox($selection) |
97
|
|
|
->setChecked($this->isSelectionSelected($value)) |
98
|
|
|
->setDisabled($this->disabled) |
99
|
|
|
->setName($this->getSelectionItemsName()) |
100
|
|
|
->setValue($value) |
101
|
|
|
->setId($id); |
102
|
|
|
|
103
|
|
|
$label = $this->createSelectionLabel($selection)->setFor($id); |
104
|
|
|
|
105
|
|
|
$html .= $this->tag('li', [], $checkbox . $label); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $html; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* |
113
|
|
|
*/ |
114
|
|
|
protected function createSelectionCheckbox($selection): Checkbox |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
return new Checkbox(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.