1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Ublaboo\DataGrid\GroupAction; |
4
|
|
|
|
5
|
|
|
use Nette; |
6
|
|
|
use Nette\Application\UI\Form; |
7
|
|
|
use Nette\SmartObject; |
8
|
|
|
use Ublaboo\DataGrid\DataGrid; |
9
|
|
|
use Ublaboo\DataGrid\Exception\DataGridGroupActionException; |
10
|
|
|
|
11
|
|
|
class GroupActionCollection |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
use SmartObject; |
15
|
|
|
|
16
|
|
|
public const ID_ATTRIBUTE_PREFIX = 'group_action_item_'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var GroupAction[] |
20
|
|
|
*/ |
21
|
|
|
protected $group_actions = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var DataGrid |
25
|
|
|
*/ |
26
|
|
|
protected $datagrid; |
27
|
|
|
|
28
|
|
|
public function __construct(DataGrid $datagrid) |
29
|
|
|
{ |
30
|
|
|
$this->datagrid = $datagrid; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get assambled form |
36
|
|
|
*/ |
37
|
|
|
public function addToFormContainer(Nette\Forms\Container $container): void |
38
|
|
|
{ |
39
|
|
|
/** @var Nette\Application\UI\Form $form */ |
40
|
|
|
$form = $container->lookup('Nette\Application\UI\Form'); |
41
|
|
|
$translator = $form->getTranslator(); |
42
|
|
|
$main_options = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* First foreach for filling "main" select |
46
|
|
|
*/ |
47
|
|
|
foreach ($this->group_actions as $id => $action) { |
48
|
|
|
$main_options[$id] = $action->getTitle(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$container->addSelect('group_action', '', $main_options) |
52
|
|
|
->setPrompt('ublaboo_datagrid.choose'); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Second for creating select for each "sub"-action |
56
|
|
|
*/ |
57
|
|
|
foreach ($this->group_actions as $id => $action) { |
58
|
|
|
$control = null; |
59
|
|
|
|
60
|
|
|
if ($action instanceof GroupSelectAction) { |
61
|
|
|
if ($action->hasOptions()) { |
62
|
|
|
if ($action instanceof GroupMultiSelectAction) { |
63
|
|
|
$control = $container->addMultiSelect($id, '', $action->getOptions()); |
64
|
|
|
$control->setAttribute('data-datagrid-multiselect-id', static::ID_ATTRIBUTE_PREFIX . $id); |
|
|
|
|
65
|
|
|
$control->setAttribute('data-style', 'hidden'); |
|
|
|
|
66
|
|
|
$control->setAttribute('data-selected-icon-check', DataGrid::$icon_prefix . 'check'); |
|
|
|
|
67
|
|
|
} else { |
68
|
|
|
$control = $container->addSelect($id, '', $action->getOptions()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} elseif ($action instanceof GroupTextAction) { |
75
|
|
|
$control = $container->addText($id, ''); |
76
|
|
|
|
77
|
|
|
$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id) |
|
|
|
|
78
|
|
|
->addConditionOn($container['group_action'], Form::EQUAL, $id) |
79
|
|
|
->setRequired($translator->translate('ublaboo_datagrid.choose_input_required')) |
80
|
|
|
->endCondition(); |
81
|
|
|
|
82
|
|
|
} elseif ($action instanceof GroupTextareaAction) { |
83
|
|
|
$control = $container->addTextarea($id, ''); |
84
|
|
|
|
85
|
|
|
$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id) |
|
|
|
|
86
|
|
|
->addConditionOn($container['group_action'], Form::EQUAL, $id) |
87
|
|
|
->setRequired($translator->translate('ublaboo_datagrid.choose_input_required')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($control) { |
91
|
|
|
/** |
92
|
|
|
* User may set a class to the form control |
93
|
|
|
*/ |
94
|
|
|
if ($class = $action->getClass()) { |
95
|
|
|
$control->setAttribute('class', $class); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* User may set additional attribtues to the form control |
100
|
|
|
*/ |
101
|
|
|
foreach ($action->getAttributes() as $name => $value) { |
102
|
|
|
$control->setAttribute($name, $value); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
foreach ($this->group_actions as $id => $action) { |
108
|
|
|
$container['group_action']->addCondition(Form::EQUAL, $id) |
109
|
|
|
->toggle(static::ID_ATTRIBUTE_PREFIX . $id); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$container['group_action']->addCondition(Form::FILLED) |
113
|
|
|
->toggle(strtolower($this->datagrid->getName()) . 'group_action_submit'); |
114
|
|
|
|
115
|
|
|
$container->addSubmit('submit', 'ublaboo_datagrid.execute') |
116
|
|
|
->setValidationScope([$container]) |
117
|
|
|
->setAttribute('id', strtolower($this->datagrid->getName()) . 'group_action_submit'); |
|
|
|
|
118
|
|
|
|
119
|
|
|
if ($form instanceof Nette\ComponentModel\IComponent) { |
120
|
|
|
$form->onSubmit[] = [$this, 'submitted']; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Pass "sub"-form submission forward to custom submit function |
127
|
|
|
*/ |
128
|
|
|
public function submitted(Form $form): void |
129
|
|
|
{ |
130
|
|
|
if (!isset($form['group_action']['submit']) || !$form['group_action']['submit']->isSubmittedBy()) { |
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$values = $form->getValues(); |
135
|
|
|
$values = $values['group_action']; |
136
|
|
|
|
137
|
|
|
if ($values->group_action === 0 || $values->group_action === null) { |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @todo Define items IDs |
143
|
|
|
*/ |
144
|
|
|
$http_ids = $form->getHttpData(Form::DATA_LINE | Form::DATA_KEYS, strtolower($this->datagrid->getName()) . '_group_action_item[]'); |
145
|
|
|
$ids = array_keys($http_ids); |
146
|
|
|
|
147
|
|
|
$id = $values->group_action; |
148
|
|
|
$this->group_actions[$id]->onSelect($ids, $values->{$id} ?? null); |
149
|
|
|
|
150
|
|
|
$form['group_action']['group_action']->setValue(null); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Add one group action (select box) to collection of actions |
156
|
|
|
* |
157
|
|
|
* @param array $options |
158
|
|
|
*/ |
159
|
|
|
public function addGroupSelectAction(string $title, array $options): GroupAction |
160
|
|
|
{ |
161
|
|
|
$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1; |
162
|
|
|
|
163
|
|
|
return $this->group_actions[$id] = new GroupSelectAction($title, $options); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Add one group action (multiselect box) to collection of actions |
169
|
|
|
* |
170
|
|
|
* @param array $options |
171
|
|
|
*/ |
172
|
|
|
public function addGroupMultiSelectAction(string $title, array $options): GroupAction |
173
|
|
|
{ |
174
|
|
|
$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1; |
175
|
|
|
|
176
|
|
|
return $this->group_actions[$id] = new GroupMultiSelectAction($title, $options); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Add one group action (text input) to collection of actions |
182
|
|
|
*/ |
183
|
|
|
public function addGroupTextAction(string $title): GroupAction |
184
|
|
|
{ |
185
|
|
|
$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1; |
186
|
|
|
|
187
|
|
|
return $this->group_actions[$id] = new GroupTextAction($title); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Add one group action (textarea) to collection of actions |
193
|
|
|
*/ |
194
|
|
|
public function addGroupTextareaAction(string $title): GroupAction |
195
|
|
|
{ |
196
|
|
|
$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1; |
197
|
|
|
|
198
|
|
|
return $this->group_actions[$id] = new GroupTextareaAction($title); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
public function getGroupAction(string $title): GroupAction |
203
|
|
|
{ |
204
|
|
|
foreach ($this->group_actions as $action) { |
205
|
|
|
if ($action->getTitle() === $title) { |
206
|
|
|
return $action; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
throw new DataGridGroupActionException("Group action $title does not exist."); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: