-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathDependentMultiSelectBox.php
More file actions
146 lines (116 loc) · 3.35 KB
/
Copy pathDependentMultiSelectBox.php
File metadata and controls
146 lines (116 loc) · 3.35 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
<?php
/**
* This file is part of the NasExt extensions of Nette Framework
*
* Copyright (c) 2013 Dusan Hudak (http://dusan-hudak.com)
*
* For the full copyright and license information, please view
* the file license.md that was distributed with this source code.
*/
namespace NasExt\Forms\Controls;
use NasExt;
use Nette;
/**
* @author Jáchym Toušek
* @author Dusan Hudak
* @author Ales Wita
* @license MIT
*/
class DependentMultiSelectBox extends Nette\Forms\Controls\MultiSelectBox implements Nette\Application\UI\ISignalReceiver
{
use NasExt\Forms\DependentTrait {
getValue as protected traitGetValue;
}
/** @var string */
const SIGNAL_NAME = DependentSelectBox::SIGNAL_NAME;
/**
* @param string $label
* @param array<int, Nette\Forms\Control> $parents
*/
public function __construct($label, array $parents)
{
$this->parents = $parents;
$this->dependentCallbackParams = $this->parents;//default
parent::__construct($label);
}
/**
* @throws $value
* @param bool
* @return self
*/
public function setDisabled($value = true)
{
if (is_array($value)) {
throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!');
}
return parent::setDisabled($value);
}
/**
* @return array
*/
public function getValue(): array
{
return $this->traitGetValue();
}
/**
* @param string $signal
* @return void
*/
public function signalReceived(string $signal) : void
{
$presenter = $this->lookup('Nette\\Application\\UI\\Presenter');
if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) {
$cbParamNames = [];
foreach ($this->dependentCallbackParams as $param) {
$value = $presenter->getParameter($this->getNormalizeName($param));
if ($param instanceof Nette\Forms\Controls\MultiChoiceControl) {
if (is_string($value)) {
$value = explode(',', $value);
}
if ($value !== null) {
$value = array_filter($value, static function ($val) {return !in_array($val, [null, '', []], true);});
}
}
$param->setValue($value);
$cbParamNames[$param->getName()] = $param->getValue();
}
$data = $this->getDependentData([$cbParamNames]);
$presenter->payload->dependentselectbox = [
'id' => $this->getHtmlId(),
'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled),
'value' => $data->getValue(),
'prompt' => false,
'disabledWhenEmpty' => $this->disabledWhenEmpty,
];
$presenter->sendPayload();
}
}
/**
* @return void
*/
private function tryLoadItems()
{
if ($this->dependentCallbackParams === array_filter($this->dependentCallbackParams, function ($p) {return !$p->hasErrors();})) {
$cbParamValues = [];
foreach ($this->dependentCallbackParams as $param) {
$cbParamValues[$param->getName()] = $param->getValue();
}
$data = $this->getDependentData([$cbParamValues]);
$items = $data->getItems();
if ($this->getForm()->isSubmitted()) {
$this->setValue($this->value);
} elseif ($this->tempValue !== null) {
$this->setValue($this->tempValue);
} else {
$this->setValue($data->getValue());
}
$this->loadHttpData();
$this->setItems($items);
if (count($items) === 0) {
if ($this->disabledWhenEmpty === true && !$this->isDisabled()) {
$this->setDisabled();
}
}
}
}
}