-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaseGrid.php
More file actions
executable file
·357 lines (302 loc) · 9.46 KB
/
Copy pathBaseGrid.php
File metadata and controls
executable file
·357 lines (302 loc) · 9.46 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
declare(strict_types=1);
namespace ADT\Datagrid\Component;
use ADT\Application\BasePresenter;
use ADT\Datagrid\Model\Service\DatagridService;
use ADT\DoctrineComponents\QueryObject\Filters\IsActiveFilter;
use ADT\DoctrineComponents\QueryObject\QueryObject;
use Closure;
use Contributte\Datagrid\Column\Action\Confirmation\StringConfirmation;
use Contributte\Datagrid\Exception\DatagridException;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Exception;
use Kdyby\Autowired\Attributes\Autowire;
use Kdyby\Autowired\AutowireComponentFactories;
use Kdyby\Autowired\AutowireProperties;
use Nette\Application\AbortException;
use Nette\Application\BadRequestException;
use Nette\Application\UI\Control;
use Nette\Application\UI\InvalidLinkException;
use Nette\Application\UI\Presenter;
use Nette\DI\Container;
use ReflectionClass;
use ReflectionException;
use TypeError;
/**
* @method BasePresenter getPresenter()
*/
abstract class BaseGrid extends Control
{
use AutowireProperties;
use AutowireComponentFactories;
use BaseGridDependencies;
#[Autowire]
protected DatagridService $datagridService;
/** @var callable */
protected $onDelete;
protected bool $withoutIsActiveColumn = false;
public function __construct()
{
$this->monitor(Presenter::class, function () {
$grid = $this->getGrid();
if (!method_exists($this, 'initGrid')) {
throw new Exception('Define initGrid method!');
}
if (isset($grid->getParameter('filter')[DataGrid::SELECTED_GRID_FILTER_KEY])) {
if (!$this->gridFilterQueryFactory->create()->byId($grid->getParameter('filter')[DataGrid::SELECTED_GRID_FILTER_KEY])->fetchOneOrNull()) {
$grid->handleResetAdvancedFilter();
}
}
$this->initGrid($grid);
$this->addIsActive($grid);
});
}
protected function createQueryObject(): QueryObject
{
return $this->getDic()->getByType($this->getQueryObjectFactoryClass())->create();
}
protected function getDic(): Container
{
return $this->autowirePropertiesLocator;
}
/**
* @throws DatagridException
* @throws Exception
*/
final protected function createComponentGrid(): DataGrid
{
/** @var DataGrid $grid */
$grid = new ($this->getDataGridClass())();
$grid->setTranslator($this->getTranslator());
$grid->setEntityManager($this->getEntityManager());
$grid->setDatagridService($this->datagridService);
$grid->setGridFilterQueryFactory($this->getGridFilterQueryFactory());
$grid->setOuterFilterRendering();
$grid->setEmail($this->getEmail());
$grid->setGridName($this->getGridName());
$this->setGridDataSource($grid);
if ($this->allowEdit() && (!$this->allowEdit()->acl || $this->getSecurityUser()->isAllowed($this->allowEdit()->acl))) {
$grid->addAction('edit', '')
->setIcon('edit')
->setClass('ajax datagrid-edit');
}
if ($this->allowDelete() && (!$this->allowDelete()->acl || $this->getSecurityUser()->isAllowed($this->allowDelete()->acl))) {
$grid->addAction('delete', $this->getTranslator()->translate('ublaboo_datagrid.delete.label'), 'delete!')
->setIcon('trash')
->setClass('ajax datagrid-delete')
->setConfirmation(new StringConfirmation($this->getTranslator()->translate('ublaboo_datagrid.delete.confirm')));
}
$grid->setSortableHandler($this->getName() . '-sortRows!');
$grid->setParentTemplate($this->getTemplateFile());
return $grid;
}
public function getYesNoOptions(): array
{
$translator = $this->getTranslator();
return [
'1' => $translator->translate('ublaboo_datagrid.yes'),
'0' => $translator->translate('ublaboo_datagrid.no'),
];
}
public function getGrid(): DataGrid
{
return $this['grid'];
}
protected function setGridDataSource(DataGrid $grid): void
{
$queryObject = $this->createQueryObject();
$this->initQueryObject($queryObject);
$queryObjectDataSource = $this->getQueryObjectDataSourceFactory()->create($queryObject);
if ($this->getDataSourceFilterCallback()) {
$queryObjectDataSource->setFilterCallback($this->getDataSourceFilterCallback());
}
$grid->setDataSource($queryObjectDataSource);
}
protected function initQueryObject($queryObject): void
{
}
public function render(): void
{
$this->getTemplate()->templateFile = $this->getTemplateFile();
$this->getTemplate()->setFile(__DIR__ . '/BaseGrid.latte')->render();
}
protected function getDataSourceFilterCallback(): ?Closure
{
return null;
}
/**
* @throws AbortException
* @throws ReflectionException
* @throws NonUniqueResultException
* @throws NoResultException|InvalidLinkException
*/
final public function handleEdit(int $id): void
{
if (!$this->allowEdit()) {
$this->getPresenter()->error();
}
if ($this->allowEdit()->acl && !$this->getSecurityUser()->isAllowed($this->allowEdit()->acl)) {
$this->getPresenter()->error();
}
if (str_ends_with($this->allowEdit()->redirect, '!')) {
$methodName = rtrim('handle' . ucfirst($this->allowEdit()->redirect), '!');
try {
$this->getPresenter()->{$methodName}($id);
} catch (InvalidLinkException|TypeError) {
$queryObject = $this->createQueryObject();
$this->initQueryObject($queryObject);
$row = $queryObject->byId($id)->getQuery()->getSingleResult();
if (is_array($row)) {
$entity = $row[0];
} else {
$entity = $row;
}
$this->getPresenter()->{$methodName}($entity);
}
} else {
// because of "Argument $order passed to App\Modules\SystemModule\Orders\OrdersPresenter::actionEdit() must be App\Model\Entity\Order, integer given."
// method Presenter::argsToParams doesn't respect router
try {
$this->getPresenter()->redirect($this->allowEdit()->redirect, $id);
} catch (InvalidLinkException) {
$queryObject = $this->createQueryObject();
$this->initQueryObject($queryObject);
$this->getPresenter()->redirect($this->allowEdit()->redirect, $queryObject->byId($id)->fetchOne());
}
}
}
/**
* @throws ReflectionException
* @throws Exception
*/
public function handleSortRows(): void
{
$itemId = $this->getParameter('item_id');
$nextId = $this->getParameter('next_id');
$previousId = $this->getParameter('prev_id');
$item = $this->createQueryObject()->byId($itemId)->fetchOne();
if ($previousId) {
$previousItem = $this->createQueryObject()->byId($previousId)->fetchOne();
$newPosition = $previousItem->getPosition() + 1;
} else if ($nextId) {
$nextItem = $this->createQueryObject()->byId($nextId)->fetchOne();
$newPosition = $nextItem->getPosition() - 1;
} else {
$newPosition = 0;
}
if ($newPosition < 0) {
$newPosition = 0;
}
$item->setPosition($newPosition);
$this->getEntityManager()->flush();
}
/**
* @throws BadRequestException
* @throws ReflectionException
* @throws NonUniqueResultException
* @throws Exception
*/
final public function handleDelete($id): void
{
if (!$this->allowDelete()) {
$this->getPresenter()->error();
}
if ($this->allowDelete()->acl && !$this->getSecurityUser()->isAllowed($this->allowDelete()->acl)) {
$this->getPresenter()->error();
}
$queryObject = $this->createQueryObject();
$this->initQueryObject($queryObject);
$row = $queryObject->byId($id)->getQuery()->getSingleResult();
if (is_array($row)) {
$entity = $row[0];
} else {
$entity = $row;
}
if ($this->allowDelete()->onDelete && !($this->allowDelete()->onDelete)($entity)) {
return;
}
if ($this->getEntityManager()->isPossibleToDeleteEntity($entity)) {
$this->getEntityManager()->remove($entity);
$this->getEntityManager()->flush();
$this->getPresenter()->flashMessageSuccess('action.delete.yes');
} else {
$this->getPresenter()->flashMessageError('app.grids.flashes.cantDelete');
}
}
final public function addFilterQuery(DataGrid $grid): void
{
$grid->addFilterText('q', '')
->setTemplate('datagrid_filter_q.latte')
->setCondition(function ($query, $value) {
$query->byQuery($value);
});
}
protected function allowEdit(): ?EditParams
{
return null;
}
protected function allowDelete(): ?DeleteParams
{
return null;
}
/**
* @throws DatagridException
*/
protected function addIsActive(DataGrid $grid): void
{
if ($this->withoutIsActiveColumn) {
$grid->setIsActiveValue(false);
return;
}
$class = $this->createQueryObject()->getEntityClass();
$metadata = $this->getEntityManager()->getClassMetadata($class);
if (
isset($grid->getColumns()['isActive'])
|| !$metadata->hasField('isActive')
) {
$grid->setIsActiveValue(false);
return;
}
$i = 1;
$order = [];
foreach ($grid->getColumns() as $key => $column) {
if ($i === 2) {
$order[] = 'isActive';
}
$order[] = $key;
$i++;
}
if (count($order) === 1) {
$order[] = 'isActive';
}
$grid->addColumnText('isActive', 'app.forms.global.isActive.label');
$grid->setColumnsOrder($order);
$grid->addIsActiveSwitcher();
if ($grid->getIsActiveValue()) {
$grid->getColumns()['isActive']
->addCellAttributes(['class' => 'd-none']);
}
}
/**
* @throws Exception
*/
public function handleDeleteGridFilter(): void
{
if (!$gridFilter = $this->getGridFilterQueryFactory()->create()->byId($this->getParameter('deleteId'))->fetchOneOrNull()) {
$this->error();
}
$this->getEntityManager()->remove($gridFilter);
$this->getEntityManager()->flush();
$this->getPresenter()->flashMessageSuccess('action.delete.yes');
$this->getGrid()->handleResetAdvancedFilter();
}
public function getGridName(): string
{
return $this->getPresenter()->getName() . '-' . $this->getName();
}
protected function getTemplateFile(): ?string
{
return null;
}
}