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