|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AtDataGrid; |
|
4
|
|
|
|
|
5
|
|
|
use AtDataGrid\Filter\FilterInterface; |
|
6
|
|
|
use AtDataGrid\Form\FormBuilder; |
|
7
|
|
|
use AtDataGrid\Renderer\RendererInterface; |
|
8
|
|
|
use AtDataGrid\Row\Action; |
|
9
|
|
|
use Zend\Cache\Storage\StorageInterface; |
|
10
|
|
|
use Zend\Form\Element; |
|
11
|
|
|
use Zend\Form\Form; |
|
12
|
|
|
|
|
13
|
|
|
class Manager |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var DataGrid |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $grid; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $rawData; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $data; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var RendererInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $renderer; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var FormBuilder |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $formBuilder; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Form |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $filtersForm; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var StorageInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $cache; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var bool |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $allowCreate = true; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var bool |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $allowDelete = true; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var bool |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $allowEdit = true; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Row actions |
|
67
|
|
|
* |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $actions = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param DataGrid $grid |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __construct(DataGrid $grid) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->grid = $grid; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return DataGrid |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getGrid() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->grid; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param RendererInterface $renderer |
|
90
|
|
|
* @return $this |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setRenderer(RendererInterface $renderer) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->renderer = $renderer; |
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return RendererInterface |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getRenderer() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->renderer; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param StorageInterface $cache |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setCache(StorageInterface $cache) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->cache = $cache; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return StorageInterface |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getCache() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->cache; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param bool $flag |
|
124
|
|
|
* @return DataGrid |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setAllowCreate($flag = true) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->allowCreate = $flag; |
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Alias for setAllowCreate |
|
134
|
|
|
* |
|
135
|
|
|
* @param bool $flag |
|
136
|
|
|
* @return DataGrid |
|
137
|
|
|
*/ |
|
138
|
|
|
public function allowCreate($flag = true) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->setAllowCreate($flag); |
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
public function isAllowCreate() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->allowCreate; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param bool $flag |
|
154
|
|
|
* @return DataGrid |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setAllowDelete($flag = true) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->allowDelete = $flag; |
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Alias for setAllowDelete |
|
164
|
|
|
* |
|
165
|
|
|
* @param bool $flag |
|
166
|
|
|
* @return DataGrid |
|
167
|
|
|
*/ |
|
168
|
|
|
public function allowDelete($flag = true) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->setAllowDelete($flag); |
|
171
|
|
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return bool |
|
176
|
|
|
*/ |
|
177
|
|
|
public function isAllowDelete() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->allowDelete; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param bool $flag |
|
184
|
|
|
* @return DataGrid |
|
185
|
|
|
*/ |
|
186
|
|
|
public function setAllowEdit($flag = true) |
|
187
|
|
|
{ |
|
188
|
|
|
$this->allowEdit = $flag; |
|
189
|
|
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Alias for setAllowEdit |
|
194
|
|
|
* |
|
195
|
|
|
* @param bool $flag |
|
196
|
|
|
* @return DataGrid |
|
197
|
|
|
*/ |
|
198
|
|
|
public function allowEdit($flag = true) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->setAllowEdit($flag); |
|
201
|
|
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
|
|
public function isAllowEdit() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->allowEdit; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return Form |
|
214
|
|
|
*/ |
|
215
|
|
|
public function getFiltersForm() |
|
216
|
|
|
{ |
|
217
|
|
|
if (!$this->filtersForm) { |
|
218
|
|
|
$this->buildFiltersForm(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $this->filtersForm; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @return FormBuilder |
|
226
|
|
|
*/ |
|
227
|
|
|
public function getFormBuilder() |
|
228
|
|
|
{ |
|
229
|
|
|
if (! $this->formBuilder) { |
|
230
|
|
|
$this->formBuilder = new FormBuilder(); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return $this->formBuilder; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param FormBuilder $formBuilder |
|
238
|
|
|
*/ |
|
239
|
|
|
public function setFormBuilder($formBuilder) |
|
240
|
|
|
{ |
|
241
|
|
|
$this->formBuilder = $formBuilder; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @return Form |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function buildFiltersForm() |
|
248
|
|
|
{ |
|
249
|
|
|
$grid = $this->getGrid(); |
|
250
|
|
|
$form = new Form('at-datagrid-filters-form'); |
|
251
|
|
|
|
|
252
|
|
|
/** @var FilterInterface $filter */ |
|
253
|
|
|
foreach ($grid->getFilters() as $filter) { |
|
254
|
|
|
$element = $filter->getFormElement(); |
|
255
|
|
|
if (! $element) { |
|
256
|
|
|
$column = $grid->getColumn($filter->getName()); |
|
257
|
|
|
$element = clone $column->getFormElement(); |
|
258
|
|
|
|
|
259
|
|
|
if ($element instanceof Element\Select) { |
|
260
|
|
|
$element->setEmptyOption(''); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$element->setName($filter->getName()); |
|
264
|
|
|
$filter->setFormElement($element); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
$form->add($element); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
// Apply button |
|
271
|
|
|
$form->add(new Element\Submit('apply', ['label' => 'Search'])); |
|
272
|
|
|
|
|
273
|
|
|
$this->filtersForm = $form; |
|
274
|
|
|
|
|
275
|
|
|
return $this->filtersForm; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @param Action $action |
|
280
|
|
|
* @return $this |
|
281
|
|
|
* @throws \Exception |
|
282
|
|
|
*/ |
|
283
|
|
|
public function addAction(Action $action) |
|
284
|
|
|
{ |
|
285
|
|
|
$this->actions[$action->getName()] = $action; |
|
286
|
|
|
return $this; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @param array $actions |
|
291
|
|
|
* @return $this |
|
292
|
|
|
*/ |
|
293
|
|
|
public function addActions($actions = []) |
|
294
|
|
|
{ |
|
295
|
|
|
foreach ($actions as $action) { |
|
296
|
|
|
$this->addAction($action); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
return $this; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @param $name |
|
304
|
|
|
* @return $this |
|
305
|
|
|
*/ |
|
306
|
|
|
public function removeAction($name) |
|
307
|
|
|
{ |
|
308
|
|
|
if (array_key_exists($name, $this->actions)) { |
|
309
|
|
|
unset($this->actions[$name]); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
return $this; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* @param $name |
|
317
|
|
|
* @return bool |
|
318
|
|
|
*/ |
|
319
|
|
|
public function getAction($name) |
|
320
|
|
|
{ |
|
321
|
|
|
if (array_key_exists($name, $this->actions)) { |
|
322
|
|
|
return $this->actions[$name]; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
return false; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @return array |
|
330
|
|
|
*/ |
|
331
|
|
|
public function getActions() |
|
332
|
|
|
{ |
|
333
|
|
|
return $this->actions; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* @return array |
|
338
|
|
|
*/ |
|
339
|
|
|
public function getBulkActions() |
|
340
|
|
|
{ |
|
341
|
|
|
$bulkActions = []; |
|
342
|
|
|
foreach ($this->getActions() as $action) { |
|
343
|
|
|
if ($action->isBulk()) { |
|
344
|
|
|
$bulkActions[] = $action; |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
return $bulkActions; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
|
|
352
|
|
|
public function getRawData() |
|
353
|
|
|
{ |
|
354
|
|
|
return $this->rawData; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* @return array |
|
359
|
|
|
* @throws \Exception |
|
360
|
|
|
*/ |
|
361
|
|
|
public function getData() |
|
362
|
|
|
{ |
|
363
|
|
|
$grid = $this->getGrid(); |
|
364
|
|
|
$data = $grid->getData(); |
|
365
|
|
|
|
|
366
|
|
|
$this->rawData = $data; |
|
|
|
|
|
|
367
|
|
|
|
|
368
|
|
|
// Add all columns (not only from source) from grid |
|
369
|
|
|
foreach (array_keys($grid->getColumns()) as $name) { |
|
370
|
|
|
foreach ($data as &$row) { |
|
371
|
|
|
if (!array_key_exists($name, $row)) { |
|
372
|
|
|
$row[$name] = ''; |
|
373
|
|
|
} |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
unset($row); |
|
377
|
|
|
|
|
378
|
|
|
// Apply decorators only for visible columns |
|
379
|
|
|
$decoratedData = []; |
|
380
|
|
|
foreach ($data as $row) { |
|
381
|
|
|
$decoratedRow = []; |
|
382
|
|
|
foreach ($row as $colName => $value) { |
|
383
|
|
|
$column = $grid->getColumn($colName); |
|
384
|
|
|
$decoratedRow[$colName] = $value; |
|
385
|
|
|
|
|
386
|
|
|
if ($column->isVisible()) { |
|
387
|
|
|
$decoratedRow[$colName] = $column->render($value, $row); |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
$decoratedData[] = $decoratedRow; |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
return $decoratedData; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* @return mixed |
|
398
|
|
|
*/ |
|
399
|
|
|
public function render() |
|
400
|
|
|
{ |
|
401
|
|
|
return $this->getRenderer()->render([ |
|
402
|
|
|
'gridManager' => $this |
|
403
|
|
|
]); |
|
404
|
|
|
} |
|
405
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..