1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2015 ublaboo <[email protected]> |
5
|
|
|
* @author Pavel Janda <[email protected]> |
6
|
|
|
* @package Ublaboo |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ublaboo\DataGrid\GroupAction; |
10
|
|
|
|
11
|
|
|
use Nette; |
12
|
|
|
use Nette\Application\UI\Form; |
13
|
|
|
use Ublaboo\DataGrid\DataGrid; |
14
|
|
|
use Ublaboo\DataGrid\Exception\DataGridGroupActionException; |
15
|
|
|
|
16
|
|
|
class GroupActionCollection extends Nette\Object |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
const ID_ATTRIBUTE_PREFIX = 'group_action_item_'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var GroupAction[] |
23
|
|
|
*/ |
24
|
|
|
protected $group_actions = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var DataGrid |
28
|
|
|
*/ |
29
|
|
|
protected $datagrid; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function __construct(DataGrid $datagrid) |
33
|
|
|
{ |
34
|
|
|
$this->datagrid = $datagrid; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get assambled form |
40
|
|
|
* @param Nette\Forms\Container $container |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function addToFormContainer($container) |
44
|
|
|
{ |
45
|
|
|
/** @var Nette\Application\UI\Form $form */ |
46
|
|
|
$form = $container->lookup('Nette\Application\UI\Form'); |
47
|
|
|
$translator = $form->getTranslator(); |
48
|
|
|
$main_options = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* First foreach for filling "main" select |
52
|
|
|
*/ |
53
|
|
|
foreach ($this->group_actions as $id => $action) { |
54
|
|
|
$main_options[$id] = $action->getTitle(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$container->addSelect('group_action', '', $main_options) |
58
|
|
|
->setPrompt('ublaboo_datagrid.choose'); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Second for creating select for each "sub"-action |
62
|
|
|
*/ |
63
|
|
|
foreach ($this->group_actions as $id => $action) { |
64
|
|
|
$control = NULL; |
65
|
|
|
|
66
|
|
|
if ($action instanceof GroupSelectAction) { |
67
|
|
|
if ($action->hasOptions()) { |
68
|
|
|
$control = $container->addSelect($id, '', $action->getOptions()); |
69
|
|
|
|
70
|
|
|
$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} else if ($action instanceof GroupTextAction) { |
74
|
|
|
$control = $container->addText($id, ''); |
75
|
|
|
|
76
|
|
|
$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id) |
|
|
|
|
77
|
|
|
->addConditionOn($container['group_action'], Form::EQUAL, $id) |
78
|
|
|
->setRequired($translator->translate('ublaboo_datagrid.choose_input_required')) |
|
|
|
|
79
|
|
|
->endCondition(); |
80
|
|
|
|
81
|
|
|
} else if ($action instanceof GroupTextareaAction) { |
82
|
|
|
$control = $container->addTextarea($id, ''); |
83
|
|
|
|
84
|
|
|
$control->setAttribute('id', static::ID_ATTRIBUTE_PREFIX . $id) |
|
|
|
|
85
|
|
|
->addConditionOn($container['group_action'], Form::EQUAL, $id) |
86
|
|
|
->setRequired($translator->translate('ublaboo_datagrid.choose_input_required')); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($control) { |
90
|
|
|
/** |
91
|
|
|
* User may set a class to the form control |
92
|
|
|
*/ |
93
|
|
|
if ($class = $action->getClass()) { |
94
|
|
|
$control->setAttribute('class', $class); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* User may set additional attribtues to the form control |
99
|
|
|
*/ |
100
|
|
|
foreach ($action->getAttributes() as $name => $value) { |
101
|
|
|
$control->setAttribute($name, $value); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
foreach ($this->group_actions as $id => $action) { |
107
|
|
|
$container['group_action']->addCondition(Form::EQUAL, $id) |
108
|
|
|
->toggle(static::ID_ATTRIBUTE_PREFIX.$id); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$container['group_action']->addCondition(Form::FILLED) |
112
|
|
|
->toggle(strtolower($this->datagrid->getName()) . 'group_action_submit'); |
113
|
|
|
|
114
|
|
|
$container->addSubmit('submit', 'ublaboo_datagrid.execute') |
115
|
|
|
->setAttribute('id', strtolower($this->datagrid->getName()) . 'group_action_submit'); |
|
|
|
|
116
|
|
|
|
117
|
|
|
if ($form instanceof Nette\ComponentModel\IComponent) { |
118
|
|
|
$form->onSubmit[] = [$this, 'submitted']; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Pass "sub"-form submission forward to custom submit function |
125
|
|
|
* @param Form $form |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
public function submitted(Form $form) |
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 || is_null($values->group_action)) { |
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, isset($values->{$id}) ? $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 string $title |
158
|
|
|
* @param array $options |
159
|
|
|
* |
160
|
|
|
* @return GroupAction |
161
|
|
|
*/ |
162
|
|
|
public function addGroupSelectAction($title, $options) |
163
|
|
|
{ |
164
|
|
|
$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1; |
165
|
|
|
|
166
|
|
|
return $this->group_actions[$id] = new GroupSelectAction($title, $options); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Add one group action (text input) to collection of actions |
172
|
|
|
* |
173
|
|
|
* @param string $title |
174
|
|
|
* |
175
|
|
|
* @return GroupAction |
176
|
|
|
*/ |
177
|
|
|
public function addGroupTextAction($title) |
178
|
|
|
{ |
179
|
|
|
$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1; |
180
|
|
|
|
181
|
|
|
return $this->group_actions[$id] = new GroupTextAction($title); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Add one group action (textarea) to collection of actions |
187
|
|
|
* |
188
|
|
|
* @param string $title |
189
|
|
|
* |
190
|
|
|
* @return GroupAction |
191
|
|
|
*/ |
192
|
|
|
public function addGroupTextareaAction($title) |
193
|
|
|
{ |
194
|
|
|
$id = ($s = sizeof($this->group_actions)) ? ($s + 1) : 1; |
195
|
|
|
|
196
|
|
|
return $this->group_actions[$id] = new GroupTextareaAction($title); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $title |
202
|
|
|
* @return GroupAction |
203
|
|
|
*/ |
204
|
|
|
public function getGroupAction($title) |
205
|
|
|
{ |
206
|
|
|
foreach ($this->group_actions as $action) { |
207
|
|
|
if ($action->getTitle() === $title) { |
208
|
|
|
return $action; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
throw new DataGridGroupActionException("Group action $title does not exist."); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
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: