1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use BZIon\Form\Creator\ModelFormCreator; |
4
|
|
|
use Symfony\Component\Form\Form; |
5
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A controller with actions for creating, reading, updating and deleting models |
10
|
|
|
* @package BZiON\Controllers |
11
|
|
|
*/ |
12
|
|
|
abstract class CRUDController extends JSONController |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Make sure that the data of a form is valid, only called when creating a |
16
|
|
|
* new object |
17
|
|
|
* @param Form $form The submitted form |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
1 |
|
protected function validateNew($form) |
21
|
|
|
{ |
22
|
1 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Make sure that the data of a form is valid, only called when editing an |
26
|
|
|
* existing object |
27
|
|
|
* @param Form $form The submitted form |
28
|
|
|
* @param PermissionModel $model The model being edited |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
protected function validateEdit($form, $model) |
32
|
|
|
{ |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Make sure that the data of a form is valid |
37
|
|
|
* @param Form $form The submitted form |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
protected function validate($form) |
41
|
|
|
{ |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Delete a model |
46
|
|
|
* @param PermissionModel $model The model we want to delete |
47
|
|
|
* @param Player $me The user who wants to delete the model |
48
|
|
|
* @param Closure|null $onSuccess Something to do when the model is deleted |
49
|
|
|
* @throws ForbiddenException |
50
|
|
|
* @return mixed The response to show to the user |
51
|
|
|
*/ |
52
|
1 |
|
protected function delete(PermissionModel $model, Player $me, $onSuccess = null) |
53
|
|
|
{ |
54
|
1 |
|
if ($model->isDeleted()) { |
55
|
|
|
// We will have to hard delete the model |
56
|
|
|
$hard = true; |
57
|
|
|
$message = 'hardDelete'; |
58
|
|
|
$action = 'Erase forever'; |
59
|
|
|
} else { |
60
|
1 |
|
$hard = false; |
61
|
1 |
|
$message = 'softDelete'; |
62
|
1 |
|
$action = 'Delete'; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
if (!$this->canDelete($me, $model, $hard)) { |
66
|
|
|
throw new ForbiddenException($this->getMessage($model, $message, 'forbidden')); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$successMessage = $this->getMessage($model, $message, 'success'); |
70
|
1 |
|
$redirection = $this->redirectToList($model); |
71
|
|
|
|
72
|
1 |
|
return $this->showConfirmationForm(function () use ($model, $hard, $redirection, $onSuccess) { |
73
|
1 |
|
if ($hard) { |
74
|
|
|
$model->wipe(); |
75
|
|
|
} else { |
76
|
1 |
|
$model->delete(); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
if ($onSuccess) { |
80
|
1 |
|
$response = $onSuccess(); |
81
|
1 |
|
if ($response instanceof Response) { |
82
|
|
|
return $response; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return $redirection; |
87
|
1 |
|
}, $this->getMessage($model, $message, 'confirm'), $successMessage, $action); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Create a model |
92
|
|
|
* |
93
|
|
|
* This method requires that you have implemented enter() and a form creator |
94
|
|
|
* for the model |
95
|
|
|
* |
96
|
|
|
* @param Player $me The user who wants to create the model |
97
|
|
|
* @param Closure|null $onSuccess The function to call on success |
98
|
|
|
* @throws ForbiddenException |
99
|
|
|
* @return mixed The response to show to the user |
100
|
|
|
*/ |
101
|
1 |
|
protected function create(Player $me, $onSuccess = null) |
102
|
|
|
{ |
103
|
1 |
|
if (!$this->canCreate($me)) { |
104
|
1 |
|
throw new ForbiddenException($this->getMessage($this->getName(), 'create', 'forbidden')); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$creator = $this->getFormCreator(); |
108
|
1 |
|
$form = $creator->create()->handleRequest($this->getRequest()); |
109
|
|
|
|
110
|
1 |
|
if ($form->isSubmitted()) { |
111
|
1 |
|
$this->validate($form); |
|
|
|
|
112
|
1 |
|
$this->validateNew($form); |
|
|
|
|
113
|
1 |
|
if ($form->isValid()) { |
114
|
1 |
|
$model = $creator->enter($form); |
|
|
|
|
115
|
1 |
|
$this->getFlashBag()->add("success", |
116
|
1 |
|
$this->getMessage($model, 'create', 'success')); |
117
|
|
|
|
118
|
1 |
|
if ($onSuccess) { |
119
|
1 |
|
$response = $onSuccess($model); |
120
|
1 |
|
if ($response instanceof Response) { |
121
|
|
|
return $response; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return $this->redirectTo($model); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
return array("form" => $form->createView()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Edit a model |
134
|
|
|
* |
135
|
|
|
* This method requires that you have implemented update() and a form creator |
136
|
|
|
* for the model |
137
|
|
|
* |
138
|
|
|
* @param PermissionModel $model The model we want to edit |
139
|
|
|
* @param Player $me The user who wants to edit the model |
140
|
|
|
* @param string $type The name of the variable to pass to the view |
141
|
|
|
* @throws ForbiddenException |
142
|
|
|
* @return mixed The response to show to the user |
143
|
|
|
*/ |
144
|
|
|
protected function edit(PermissionModel $model, Player $me, $type) |
145
|
|
|
{ |
146
|
|
|
if (!$this->canEdit($me, $model)) { |
147
|
|
|
throw new ForbiddenException($this->getMessage($model, 'edit', 'forbidden')); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$creator = $this->getFormCreator($model); |
151
|
|
|
$form = $creator->create()->handleRequest($this->getRequest()); |
152
|
|
|
|
153
|
|
|
if ($form->isSubmitted()) { |
154
|
|
|
$this->validate($form); |
|
|
|
|
155
|
|
|
$this->validateEdit($form, $model); |
|
|
|
|
156
|
|
|
if ($form->isValid()) { |
157
|
|
|
$creator->update($form, $model); |
|
|
|
|
158
|
|
|
$this->getFlashBag()->add("success", |
159
|
|
|
$this->getMessage($model, 'edit', 'success')); |
160
|
|
|
|
161
|
|
|
return $this->redirectTo($model); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return array("form" => $form->createView(), $type => $model); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Find whether a player can delete a model |
170
|
|
|
* |
171
|
|
|
* @param Player $player The player who wants to delete the model |
172
|
|
|
* @param PermissionModel $model The model that will be deleted |
173
|
|
|
* @param bool $hard Whether to hard-delete the model instead of soft-deleting it |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
1 |
|
protected function canDelete($player, $model, $hard = false) |
177
|
|
|
{ |
178
|
1 |
|
return $player->canDelete($model, $hard); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Find whether a player can create a model |
183
|
|
|
* |
184
|
|
|
* @param Player $player The player who wants to create a model |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
1 |
|
protected function canCreate($player) |
188
|
|
|
{ |
189
|
1 |
|
$modelName = $this->getName(); |
190
|
|
|
|
191
|
1 |
|
return $player->canCreate($modelName); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Find whether a player can edit a model |
196
|
|
|
* |
197
|
|
|
* @param Player $player The player who wants to delete the model |
198
|
|
|
* @param PermissionModel $model The model which will be edited |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
protected function canEdit($player, $model) |
202
|
|
|
{ |
203
|
|
|
return $player->canEdit($model); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get a redirection response to a model |
208
|
|
|
* |
209
|
|
|
* Goes to a list of models of the same type if the provided model does not |
210
|
|
|
* have a URL |
211
|
|
|
* |
212
|
|
|
* @param ModelInterface $model The model to redirect to |
213
|
|
|
* @return Response |
214
|
|
|
*/ |
215
|
1 |
|
protected function redirectTo($model) |
216
|
|
|
{ |
217
|
1 |
|
if ($model instanceof UrlModel) { |
218
|
1 |
|
return new RedirectResponse($model->getUrl()); |
219
|
|
|
} else { |
220
|
|
|
return $this->redirectToList($model); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get a redirection response to a list of models |
226
|
|
|
* |
227
|
|
|
* @param ModelInterface $model The model to whose list we should redirect |
228
|
|
|
* @return Response |
229
|
|
|
*/ |
230
|
1 |
|
protected function redirectToList($model) |
231
|
|
|
{ |
232
|
1 |
|
$route = $model->getRouteName('list'); |
|
|
|
|
233
|
1 |
|
$url = Service::getGenerator()->generate($route); |
234
|
|
|
|
235
|
1 |
|
return new RedirectResponse($url); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Dynamically get the form to show to the user |
240
|
|
|
* |
241
|
|
|
* @param \Model|null $model The model being edited, `null` if we're creating one |
242
|
|
|
* @return ModelFormCreator |
243
|
|
|
*/ |
244
|
1 |
|
private function getFormCreator($model = null) |
245
|
|
|
{ |
246
|
1 |
|
$type = ($model instanceof Model) ? $model->getType() : $this->getName(); |
247
|
1 |
|
$type = ucfirst($type); |
248
|
|
|
|
249
|
1 |
|
$creatorClass = "\\BZIon\\Form\\Creator\\{$type}FormCreator"; |
250
|
1 |
|
$creator = new $creatorClass($model, $this->getMe(), $this); |
251
|
|
|
|
252
|
1 |
|
return $creator; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get a message to show to the user |
257
|
|
|
* @todo Use the $escape parameter |
258
|
|
|
* @param \ModelInterface|string $model The model (or type) to show a message for |
259
|
|
|
* @param string $action The action that will be performed (softDelete, hardDelete, create or edit) |
260
|
|
|
* @param string $status The message's status (confirm, error or success) |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
1 |
|
private function getMessage($model, $action, $status, $escape = true) |
|
|
|
|
264
|
|
|
{ |
265
|
1 |
|
if ($model instanceof Model) { |
266
|
1 |
|
$type = strtolower($model->getTypeForHumans()); |
267
|
|
|
|
268
|
1 |
|
if ($model instanceof NamedModel) { |
269
|
|
|
// Twig will not escape the message on confirmation forms |
270
|
1 |
|
$name = $model->getName(); |
271
|
1 |
|
if ($status == 'confirm') { |
272
|
1 |
|
$name = Model::escape($name); |
273
|
|
|
} |
274
|
|
|
|
275
|
1 |
|
$messages = $this->getMessages($type, $name); |
276
|
|
|
|
277
|
1 |
|
return $messages[$action][$status]['named']; |
278
|
|
|
} else { |
279
|
|
|
$messages = $this->getMessages($type); |
280
|
|
|
|
281
|
|
|
return $messages[$action][$status]['unnamed']; |
282
|
|
|
} |
283
|
|
|
} else { |
284
|
1 |
|
$messages = $this->getMessages(strtolower($model)); |
285
|
|
|
|
286
|
1 |
|
return $messages[$action][$status]; |
|
|
|
|
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Get a list of messages to show to the user |
292
|
|
|
* @param string $type The type of the model that the message refers to |
293
|
|
|
* @param string $name The name of the model |
294
|
|
|
* @return array |
295
|
|
|
*/ |
296
|
1 |
|
protected function getMessages($type, $name = '') |
297
|
|
|
{ |
298
|
|
|
return array( |
299
|
|
|
'hardDelete' => array( |
300
|
|
|
'confirm' => array( |
301
|
|
|
'named' => <<<"WARNING" |
302
|
1 |
|
Are you sure you want to wipe <strong>$name</strong>?<br /> |
303
|
|
|
<strong><em>DANGER</em></strong>: This action will <strong>permanently</strong> |
304
|
1 |
|
erase the $type from the database, including any objects directly related to it! |
305
|
|
|
WARNING |
306
|
|
|
, |
307
|
|
|
'unnamed' => <<<"WARNING" |
308
|
1 |
|
Are you sure you want to wipe this $type?<br /> |
309
|
|
|
<strong><em>DANGER</em></strong>: This action will <strong>permanently</strong> |
310
|
1 |
|
erase the $type from the database, including any objects directly related to it! |
311
|
|
|
WARNING |
312
|
|
|
), |
313
|
|
|
'forbidden' => array( |
314
|
1 |
|
'named' => "You are not allowed to delete the $type $name", |
315
|
1 |
|
'unnamed' => "You are not allowed to delete this $type", |
316
|
|
|
), |
317
|
|
|
'success' => array( |
318
|
1 |
|
'named' => "The $type $name was permanently erased from the database", |
319
|
1 |
|
'unnamed' => "The $type has been permanently erased from the database", |
320
|
|
|
), |
321
|
|
|
), |
322
|
|
|
'softDelete' => array( |
323
|
|
|
'confirm' => array( |
324
|
1 |
|
'named' => "Are you sure you want to delete <strong>$name</strong>?", |
325
|
1 |
|
'unnamed' => "Are you sure you want to delete this $type?", |
326
|
|
|
), |
327
|
|
|
'forbidden' => array( |
328
|
1 |
|
'named' => "You are not allowed to delete the $type $name", |
329
|
1 |
|
'unnamed' => "You aren't allowed to delete this $type", |
330
|
|
|
), |
331
|
|
|
'success' => array( |
332
|
1 |
|
'named' => "The $type $name was deleted successfully", |
333
|
1 |
|
'unnamed' => "The $type was deleted successfully", |
334
|
|
|
), |
335
|
|
|
), |
336
|
|
|
'edit' => array( |
337
|
|
|
'forbidden' => array( |
338
|
1 |
|
'named' => "You are not allowed to edit the $type $name", |
339
|
1 |
|
'unnamed' => "You aren't allowed to edit this $type", |
340
|
|
|
), |
341
|
|
|
'success' => array( |
342
|
1 |
|
'named' => "The $type $name has been successfully updated", |
343
|
1 |
|
'unnamed' => "The $type was updated successfully", |
344
|
|
|
), |
345
|
|
|
), |
346
|
|
|
'create' => array( |
347
|
1 |
|
'forbidden' => "You are not allowed to create a new $type", |
348
|
|
|
'success' => array( |
349
|
1 |
|
'named' => "The $type $name was created successfully", |
350
|
1 |
|
'unnamed' => "The $type was created successfully", |
351
|
|
|
), |
352
|
|
|
), |
353
|
|
|
); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.