1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AtAdmin\Controller; |
4
|
|
|
|
5
|
|
|
use AtDataGrid\DataGrid; |
6
|
|
|
use AtDataGrid\Form\FormBuilder; |
7
|
|
|
use AtDataGrid\Manager as GridManager; |
8
|
|
|
use AtDataGrid\Manager; |
9
|
|
|
use Zend\EventManager\EventManager; |
10
|
|
|
use Zend\Form\Form; |
11
|
|
|
use Zend\View\Model\ViewModel; |
12
|
|
|
|
13
|
|
|
class GridController extends AbstractAdminController |
14
|
|
|
{ |
15
|
|
|
// Events |
16
|
|
|
const EVENT_CHECK_PERMISSIONS_LIST = 'at-admin.check-permissions.list'; |
17
|
|
|
const EVENT_CHECK_PERMISSIONS_CREATE = 'at-admin.check-permissions.create'; |
18
|
|
|
const EVENT_CHECK_PERMISSIONS_EDIT = 'at-admin.check-permissions.edit'; |
19
|
|
|
const EVENT_CHECK_PERMISSIONS_DELETE = 'at-admin.check-permissions.delete'; |
20
|
|
|
|
21
|
|
|
const EVENT_VALIDATION_EDIT_PRE = 'at-admin.validation.edit.pre'; |
22
|
|
|
|
23
|
|
|
const EVENT_SAVE_PRE = 'at-admin.save.pre'; |
24
|
|
|
const EVENT_SAVE_POST = 'at-admin.save.post'; |
25
|
|
|
const EVENT_DELETE_PRE = 'at-admin.delete.pre'; |
26
|
|
|
const EVENT_DELETE_POST = 'at-admin.delete.post'; |
27
|
|
|
|
28
|
|
|
const EVENT_SET_TEMPLATE_CREATE = 'at-admin.set-template.create'; |
29
|
|
|
const EVENT_SET_TEMPLATE_EDIT = 'at-admin.set-template.edit'; |
30
|
|
|
|
31
|
|
|
// Page titles |
32
|
|
|
// @todo Remove |
33
|
|
|
const TITLE_ACTION_LIST = ''; |
34
|
|
|
const TITLE_ACTION_CREATE = 'Create'; |
35
|
|
|
const TITLE_ACTION_EDIT = 'Edit'; |
36
|
|
|
const TITLE_ACTION_DELETE = 'Delete'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var GridManager |
40
|
|
|
*/ |
41
|
|
|
protected $gridManager; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function getAction() |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$this->getEventManager()->trigger(self::EVENT_CHECK_PERMISSIONS_LIST, $this); |
49
|
|
|
|
50
|
|
|
// Save back url to redirect after actions |
51
|
|
|
$this->backTo()->setBackUrl(); |
|
|
|
|
52
|
|
|
|
53
|
|
|
// Check for mass actions |
54
|
|
|
if (isset($_POST['cmd'])) { |
55
|
|
|
$this->forward($_POST['cmd']); // @todo refactor this |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$gridManager = $this->getGridManager(); |
59
|
|
|
$grid = $gridManager->getGrid(); |
60
|
|
|
|
61
|
|
|
if ($this->request->getQuery('order')) { |
|
|
|
|
62
|
|
|
$order = explode('~', $this->request->getQuery('order')); |
|
|
|
|
63
|
|
|
$grid->setOrder([$order[0] => $order[1]]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->request->getQuery('page')) { |
|
|
|
|
67
|
|
|
$grid->setCurrentPage($this->request->getQuery('page')); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($this->request->getQuery('show_items')) { |
|
|
|
|
71
|
|
|
$grid->setItemsPerPage($this->request->getQuery('show_items')); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$filtersForm = $gridManager->getFiltersForm(); |
75
|
|
|
$filtersForm->setData($this->request->getQuery()); |
|
|
|
|
76
|
|
|
if (!$filtersForm->isValid()) { |
|
|
|
|
77
|
|
|
//$this->flashMessenger()->addMessage($filtersForm->getMessages()); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$grid->setFiltersData($filtersForm->getData()); |
81
|
|
|
|
82
|
|
|
return $gridManager->render(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return mixed|ViewModel |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
public function createAction() |
90
|
|
|
{ |
91
|
|
|
/** @var EventManager $eventManager */ |
92
|
|
|
$eventManager = $this->getEventManager(); |
93
|
|
|
$eventManager->trigger(self::EVENT_CHECK_PERMISSIONS_CREATE, $this); |
94
|
|
|
|
95
|
|
|
/** @var GridManager $gridManager */ |
96
|
|
|
$gridManager = $this->getGridManager(); |
97
|
|
|
if (! $gridManager->isAllowCreate()) { |
98
|
|
|
throw new \Exception('Creating is disabled'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** @var FormBuilder $formManager */ |
102
|
|
|
$formBuilder = $gridManager->getFormBuilder(); |
103
|
|
|
|
104
|
|
|
/** @var DataGrid $grid */ |
105
|
|
|
$grid = $gridManager->getGrid(); |
106
|
|
|
|
107
|
|
|
/** @var Form $form */ |
108
|
|
|
$form = $formBuilder->build($grid); |
109
|
|
|
|
110
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
111
|
|
|
$post = $this->getRequest()->getPost(); |
|
|
|
|
112
|
|
|
$form->setData($post); |
113
|
|
|
|
114
|
|
View Code Duplication |
if ($form->isValid()) { |
|
|
|
|
115
|
|
|
// Replace POST data with filtered and validated form values |
116
|
|
|
// POST data may contains not only form data |
117
|
|
|
$data = array_replace($post->toArray(), $form->getData()); |
118
|
|
|
|
119
|
|
|
$eventManager->trigger(self::EVENT_SAVE_PRE, null, $data); |
120
|
|
|
$item = $grid->save($data); |
121
|
|
|
$eventManager->trigger(self::EVENT_SAVE_POST, $item, $data); |
122
|
|
|
|
123
|
|
|
return $this->backTo()->previous('Record created'); |
|
|
|
|
124
|
|
|
} else { |
125
|
|
|
$this->flashMessenger()->addMessage('Check form data.'); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$viewModel = new ViewModel([ |
130
|
|
|
'title' => static::TITLE_ACTION_CREATE, |
131
|
|
|
'form' => $form, |
132
|
|
|
'customJs' => $formBuilder->getCustomJs(), |
133
|
|
|
'formSections' => $formBuilder->getFormSections(), |
134
|
|
|
'backUrl' => $this->backTo()->getBackUrl(false), |
|
|
|
|
135
|
|
|
]); |
136
|
|
|
|
137
|
|
|
$viewModel->setTemplate('at-admin/create.phtml'); |
138
|
|
|
$eventResult = $eventManager->trigger(self::EVENT_SET_TEMPLATE_CREATE, $viewModel)->last(); |
139
|
|
|
if ($eventResult) { |
140
|
|
|
$viewModel = $eventResult; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $viewModel; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return array|mixed|ViewModel |
148
|
|
|
* @throws \Exception |
149
|
|
|
*/ |
150
|
|
|
public function editAction() |
151
|
|
|
{ |
152
|
|
|
/** @var Manager $gridManager */ |
153
|
|
|
$gridManager = $this->getGridManager(); |
154
|
|
|
if (!$gridManager->isAllowEdit()) { |
155
|
|
|
throw new \Exception('Editing is disabled'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** @var DataGrid $grid */ |
159
|
|
|
$grid = $gridManager->getGrid(); |
160
|
|
|
|
161
|
|
|
$id = $this->params($grid->getIdentifierColumnName()); |
162
|
|
|
if (!$id) { |
163
|
|
|
return $this->notFoundAction(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$item = $grid->getRow($id); |
167
|
|
|
if (!$item) { |
168
|
|
|
return $this->notFoundAction(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** @var EventManager $eventManager */ |
172
|
|
|
$eventManager = $this->getEventManager(); |
173
|
|
|
$eventManager->trigger(self::EVENT_CHECK_PERMISSIONS_EDIT, $item); |
174
|
|
|
|
175
|
|
|
/** @var FormBuilder $formBuilder */ |
176
|
|
|
$formBuilder = $gridManager->getFormBuilder(); |
177
|
|
|
|
178
|
|
|
/** @var Form $form */ |
179
|
|
|
$form = $formBuilder->build($grid, FormBuilder::FORM_CONTEXT_EDIT, $item); |
180
|
|
|
|
181
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
182
|
|
|
$post = $this->getRequest()->getPost(); |
|
|
|
|
183
|
|
|
$form->setData($post); |
184
|
|
|
|
185
|
|
|
$this->getEventManager()->trigger(self::EVENT_VALIDATION_EDIT_PRE, $form, ['oldData' => $item, 'newData' => $post]); |
186
|
|
|
|
187
|
|
View Code Duplication |
if ($form->isValid()) { |
|
|
|
|
188
|
|
|
// Replace POST data with filtered and validated form values |
189
|
|
|
// POST data may contains not only form data (extra data from sections) |
190
|
|
|
$data = array_replace($post->toArray(), $form->getData()); |
191
|
|
|
|
192
|
|
|
$eventManager->trigger(self::EVENT_SAVE_PRE, $item, $data); |
193
|
|
|
$item = $grid->save($data, $id); |
194
|
|
|
$eventManager->trigger(self::EVENT_SAVE_POST, $item, $data); |
195
|
|
|
|
196
|
|
|
$this->backTo()->previous('Record was updated'); |
|
|
|
|
197
|
|
|
} else { |
198
|
|
|
$this->flashMessenger()->addMessage('Check form data'); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$viewModel = new ViewModel([ |
203
|
|
|
'title' => static::TITLE_ACTION_EDIT, |
204
|
|
|
'item' => $item, |
205
|
|
|
'form' => $form, |
206
|
|
|
'customJs' => $formBuilder->getCustomJs(), |
207
|
|
|
'formSections' => $formBuilder->getFormSections(), |
208
|
|
|
'backUrl' => $this->backTo()->getBackUrl(false), |
|
|
|
|
209
|
|
|
]); |
210
|
|
|
|
211
|
|
|
$viewModel->setTemplate('at-admin/edit.phtml'); |
212
|
|
|
$eventResult = $this->getEventManager()->trigger(self::EVENT_SET_TEMPLATE_EDIT, $viewModel, ['item' => $item])->last(); |
213
|
|
|
if ($eventResult) { |
214
|
|
|
$viewModel = $eventResult; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $viewModel; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return array |
222
|
|
|
* @throws \Exception |
223
|
|
|
*/ |
224
|
|
|
public function deleteAction() |
225
|
|
|
{ |
226
|
|
|
$gridManager = $this->getGridManager(); |
227
|
|
|
if (!$gridManager->isAllowDelete()) { |
228
|
|
|
throw new \Exception('Deleting is disabled.'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$id = $this->params('id'); |
232
|
|
|
if (!$id) { |
233
|
|
|
return $this->notFoundAction(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$grid = $gridManager->getGrid(); |
237
|
|
|
$item = $grid->getRow($id); |
238
|
|
|
if (!$item) { |
239
|
|
|
return $this->notFoundAction(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$evm = $this->getEventManager(); |
243
|
|
|
$evm->trigger(self::EVENT_CHECK_PERMISSIONS_DELETE, $item); |
244
|
|
|
|
245
|
|
|
// Get additional params |
246
|
|
|
$params = array_merge_recursive( |
247
|
|
|
$this->params()->fromQuery(), |
248
|
|
|
$this->params()->fromPost() |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
$evm->trigger(self::EVENT_DELETE_PRE, $item, $params); |
252
|
|
|
$grid->delete($id); |
253
|
|
|
$evm->trigger(self::EVENT_DELETE_POST, $item, $params); |
254
|
|
|
|
255
|
|
|
$this->backTo()->previous('Record deleted.'); |
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param Manager $manager |
260
|
|
|
*/ |
261
|
|
|
public function setGridManager(GridManager $manager) |
262
|
|
|
{ |
263
|
|
|
$this->gridManager = $manager; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return Manager |
268
|
|
|
*/ |
269
|
|
|
public function getGridManager() |
270
|
|
|
{ |
271
|
|
|
if (!$this->gridManager) { |
272
|
|
|
throw new \RuntimeException('Grid manager was not set'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this->gridManager; |
276
|
|
|
} |
277
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: