This repository was archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDependentSelectBox.php
More file actions
198 lines (173 loc) Β· 4.62 KB
/
DependentSelectBox.php
File metadata and controls
198 lines (173 loc) Β· 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php declare (strict_types = 1);
namespace Wavevision\DependentSelectBox;
use Nette\Forms\Controls\BaseControl;
use Nette\Forms\Controls\SelectBox;
use Nette\Utils\Html;
use Wavevision\Utils\Arrays;
use function is_callable;
use function sprintf;
class DependentSelectBox extends SelectBox
{
use DependentSelectBoxUtils;
private bool $autoSelectSingleValue = false;
/**
* @var ConditionalParent[]
*/
private array $conditionalParents = [];
/**
* @var callable
*/
private $dependentCallback;
private bool $disabledWhenEmpty = false;
private bool $disallowSubmitWhenDisabled = false;
private bool $hidePromptWhenFilled = false;
/**
* @var BaseControl[]
*/
private array $parents;
/**
* @param string|object $label
* @param BaseControl[] $parents
*/
public function __construct($label, array $parents)
{
parent::__construct($label);
$this->parents = $parents;
}
public function getControl(): Html
{
$this->hidePrompt($this->hidePromptWhenFilled);
$control = parent::getControl();
$control->addAttributes(
[
'data-dependent-select-box' => true,
'data-parents' => $this->createParentsAttribute(),
]
);
if ($this->disallowSubmitWhenDisabled) {
$control->setAttribute('data-disallow-submit-when-disabled', true);
}
return $control;
}
/**
* @param mixed $onAncestorValue
*/
public function addConditionalParent(
BaseControl $parent,
BaseControl $ancestor,
$onAncestorValue
): DependentSelectBox {
if (!isset($this->conditionalParents[$parent->getHtmlId()])) {
$this->conditionalParents[$parent->getHtmlId()] = new ConditionalParent(
$parent,
$ancestor,
$onAncestorValue
);
}
return $this;
}
/**
* @return ConditionalParent[]
*/
public function getConditionalParents(): array
{
return $this->conditionalParents;
}
/**
* @param mixed[] $parentsValues
* @param mixed $selectedValue
* @throws InvalidDependentCallback
*/
public function getDependentData(array $parentsValues = [], $selectedValue = null): DependentData
{
if (!is_callable($this->dependentCallback)) {
throw new InvalidDependentCallback(
sprintf('Dependent callback for "%s" must be set.', $this->getHtmlId())
);
}
if (!$this->isDependentDataReady($parentsValues)) {
return $this->getDefaultData();
}
$dependentData = ($this->dependentCallback)(
new DependentValues($parentsValues, $this->getCurrentContainerValues($parentsValues), $selectedValue)
);
if (!($dependentData instanceof DependentData)) {
throw new InvalidDependentCallback(
sprintf(
'Dependent callback for "%s" must return an instance of %s.',
$this->getHtmlId(),
DependentData::class
)
);
}
$this->setItems($dependentData->getItems());
if ($dependentData->isDisabled() === null) {
$dependentData->setDisabled($this->isDisabled());
}
if ($dependentData->getPrompt() === null) {
$dependentData->setPrompt($this->getCurrentPrompt());
}
if ($dependentData->getValue() === null) {
$dependentData->setValue($selectedValue);
}
if ($dependentData->getPrompt() !== null) {
$this->setPrompt($dependentData->getPrompt());
}
$this->resolveValue($dependentData, $this->autoSelectSingleValue);
$dependentData->setOptions($this->getOptionsHtml());
return $dependentData;
}
/**
* @return int|string|null
*/
public function getValue()
{
if ($this->isSubmitted()) {
$this->loadHttpData();
return $this->getRawValue();
}
return parent::getValue();
}
public function isDisabled(): bool
{
if ($this->disabledWhenEmpty) {
if ($this->isSubmitted()) {
return $this->getValue() === null;
}
return Arrays::isEmpty($this->getItems());
}
return parent::isDisabled();
}
/**
* @return BaseControl[]
*/
public function getParents(): array
{
return $this->parents;
}
public function setAutoSelectSingleValue(bool $autoSelectSingleValue = true): DependentSelectBox
{
$this->autoSelectSingleValue = $autoSelectSingleValue;
return $this;
}
public function setDependentCallback(callable $dependentCallback): DependentSelectBox
{
$this->dependentCallback = $dependentCallback;
return $this;
}
public function setDisabledWhenEmpty(bool $disabledWhenEmpty = true): DependentSelectBox
{
$this->disabledWhenEmpty = $disabledWhenEmpty;
return $this;
}
public function setDisallowSubmitWhenDisabled(bool $disallowSubmitWhenDisabled = true): DependentSelectBox
{
$this->disallowSubmitWhenDisabled = $disallowSubmitWhenDisabled;
return $this;
}
public function setHidePromptWhenFilled(bool $hidePromptWhenFilled = true): DependentSelectBox
{
$this->hidePromptWhenFilled = $hidePromptWhenFilled;
return $this;
}
}