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