1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saxulum\Crud\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
7
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
8
|
|
|
use Knp\Component\Pager\Pagination\AbstractPagination; |
9
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
10
|
|
|
use Saxulum\Crud\Exception\ServiceNotFoundException; |
11
|
|
|
use Saxulum\Crud\Listing\Listing; |
12
|
|
|
use Saxulum\Crud\Listing\ListingFactory; |
13
|
|
|
use Saxulum\Crud\Repository\QueryBuilderForFilterFormInterface; |
14
|
|
|
use Saxulum\Crud\Util\Helper; |
15
|
|
|
use Symfony\Component\Form\FormInterface; |
16
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
17
|
|
|
use Symfony\Component\Form\FormTypeInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
22
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
23
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
24
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
25
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
26
|
|
|
use Symfony\Component\Security\Core\SecurityContextInterface; |
27
|
|
|
|
28
|
|
|
trait CrudTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param Request $request |
32
|
|
|
* @param array $templateVars |
33
|
|
|
* |
34
|
|
|
* @return Response |
35
|
|
|
* |
36
|
|
|
* @throws \Exception |
37
|
|
|
*/ |
38
|
|
|
public function crudListObjects(Request $request, array $templateVars = array()) |
39
|
|
|
{ |
40
|
|
|
$crudListRole = $this->crudListRole(); |
41
|
|
|
if (!$this->crudIsGranted($crudListRole)) { |
42
|
|
|
throw new AccessDeniedException(sprintf('You need the permission to list entities, role: %s!', $crudListRole)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$form = $this->crudListForm($request); |
46
|
|
|
if (null !== $form) { |
47
|
|
|
$form->handleRequest($request); |
48
|
|
|
$formData = $form->getData(); |
49
|
|
|
} else { |
50
|
|
|
$formData = array(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$formData = $this->crudListFormDataEnrich($request, $formData); |
54
|
|
|
|
55
|
|
|
$repo = $this->crudRepositoryForClass($this->crudObjectClass()); |
56
|
|
|
if (!$repo instanceof QueryBuilderForFilterFormInterface) { |
57
|
|
|
throw new \Exception(sprintf('A repo used for crudListObjects needs to implement: %s', QueryBuilderForFilterFormInterface::classname)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$qb = $repo->getQueryBuilderForFilterForm($formData); |
61
|
|
|
|
62
|
|
|
$pagination = $this->crudPaginate($qb, $request); |
63
|
|
|
|
64
|
|
|
$baseTemplateVars = array( |
65
|
|
|
'request' => $request, |
66
|
|
|
'pagination' => $pagination, |
67
|
|
|
'form' => isset($form) ? $form->createView() : null, |
68
|
|
|
'listing' => $this->crudListListing(), |
69
|
|
|
'listRoute' => $this->crudListRoute(), |
70
|
|
|
'createRoute' => $this->crudCreateRoute(), |
71
|
|
|
'editRoute' => $this->crudEditRoute(), |
72
|
|
|
'viewRoute' => $this->crudViewRoute(), |
73
|
|
|
'deleteRoute' => $this->crudDeleteRoute(), |
74
|
|
|
'listRole' => $this->crudListRole(), |
75
|
|
|
'createRole' => $this->crudCreateRole(), |
76
|
|
|
'editRole' => $this->crudEditRole(), |
77
|
|
|
'viewRole' => $this->crudViewRole(), |
78
|
|
|
'deleteRole' => $this->crudDeleteRole(), |
79
|
|
|
'identifier' => $this->crudIdentifier(), |
80
|
|
|
'transPrefix' => $this->crudTransPrefix(), |
81
|
|
|
'transDomain' => $this->crudTransDomain(), |
82
|
|
|
'objectClass' => $this->crudObjectClass(), |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
return $this->crudListRenderTemplateResponse( |
86
|
|
|
$request, |
87
|
|
|
$baseTemplateVars, |
88
|
|
|
$templateVars |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Request $request |
94
|
|
|
* @param array $templateVars |
95
|
|
|
* |
96
|
|
|
* @return Response|RedirectResponse |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function crudCreateObject(Request $request, array $templateVars = array()) |
99
|
|
|
{ |
100
|
|
|
$crudCreateRole = $this->crudCreateRole(); |
101
|
|
|
if (!$this->crudIsGranted($crudCreateRole)) { |
102
|
|
|
throw new AccessDeniedException(sprintf('You need the permission to create an object, role: %s!', $crudCreateRole)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$object = $this->crudCreateFactory($request); |
106
|
|
|
$form = $this->crudCreateForm($object, $request); |
107
|
|
|
|
108
|
|
|
if ('POST' === $request->getMethod()) { |
109
|
|
|
$form->handleRequest($request); |
110
|
|
|
if ($this->crudCreateIsSubmitted($object, $form, $request)) { |
111
|
|
|
if ($form->isValid()) { |
112
|
|
|
$this->crudCreatePrePersist($object, $form, $request); |
113
|
|
|
|
114
|
|
|
$em = $this->crudManagerForClass($this->crudObjectClass()); |
115
|
|
|
$em->persist($object); |
116
|
|
|
$em->flush(); |
117
|
|
|
|
118
|
|
|
$this->crudCreatePostFlush($object, $form, $request); |
119
|
|
|
$this->crudCreateSuccessFlashMesssage($object, $form, $request); |
120
|
|
|
$response = $this->crudCreateSuccessResponse($object, $form, $request); |
121
|
|
|
} else { |
122
|
|
|
$this->crudCreateErrorFlashMesssage($object, $form, $request); |
123
|
|
|
$response = $this->crudCreateErrorResponse($object, $form, $request); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (null !== $response) { |
127
|
|
|
return $response; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$baseTemplateVars = array( |
133
|
|
|
'request' => $request, |
134
|
|
|
'object' => $object, |
135
|
|
|
'form' => $form->createView(), |
136
|
|
|
'createRoute' => $this->crudCreateRoute(), |
137
|
|
|
'listRoute' => $this->crudListRoute(), |
138
|
|
|
'editRoute' => $this->crudEditRoute(), |
139
|
|
|
'viewRoute' => $this->crudViewRoute(), |
140
|
|
|
'deleteRoute' => $this->crudDeleteRoute(), |
141
|
|
|
'listRole' => $this->crudListRole(), |
142
|
|
|
'createRole' => $this->crudCreateRole(), |
143
|
|
|
'editRole' => $this->crudEditRole(), |
144
|
|
|
'viewRole' => $this->crudViewRole(), |
145
|
|
|
'deleteRole' => $this->crudDeleteRole(), |
146
|
|
|
'identifier' => $this->crudIdentifier(), |
147
|
|
|
'transPrefix' => $this->crudTransPrefix(), |
148
|
|
|
'transDomain' => $this->crudTransDomain(), |
149
|
|
|
'objectClass' => $this->crudObjectClass(), |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
return $this->crudCreateRenderTemplateResponse( |
153
|
|
|
$request, |
154
|
|
|
$baseTemplateVars, |
155
|
|
|
$templateVars |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param Request $request |
161
|
|
|
* @param object|string|int $object |
162
|
|
|
* @param array $templateVars |
163
|
|
|
* |
164
|
|
|
* @return Response|RedirectResponse |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function crudEditObject(Request $request, $object, array $templateVars = array()) |
167
|
|
|
{ |
168
|
|
|
$object = $this->crudEditLoadObject($object, $request); |
169
|
|
|
|
170
|
|
|
$crudEditRole = $this->crudEditRole(); |
171
|
|
|
if (!$this->crudIsGranted($crudEditRole, $object)) { |
172
|
|
|
throw new AccessDeniedException(sprintf('You need the permission to edit this object, role: %s!', $crudEditRole)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$form = $this->crudEditForm($object, $request); |
176
|
|
|
|
177
|
|
|
if ('POST' === $request->getMethod()) { |
178
|
|
|
$form->handleRequest($request); |
179
|
|
|
if ($this->crudEditIsSubmitted($object, $form, $request)) { |
180
|
|
|
if ($form->isValid()) { |
181
|
|
|
$this->crudEditPrePersist($object, $form, $request); |
182
|
|
|
|
183
|
|
|
$em = $this->crudManagerForClass($this->crudObjectClass()); |
184
|
|
|
$em->persist($object); |
185
|
|
|
$em->flush(); |
186
|
|
|
|
187
|
|
|
$this->crudEditPostFlush($object, $form, $request); |
188
|
|
|
$this->crudEditSuccessFlashMesssage($object, $form, $request); |
189
|
|
|
$response = $this->crudEditSuccessResponse($object, $form, $request); |
190
|
|
|
} else { |
191
|
|
|
$this->crudEditErrorFlashMesssage($object, $form, $request); |
192
|
|
|
$response = $this->crudEditErrorResponse($object, $form, $request); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (null !== $response) { |
196
|
|
|
return $response; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$baseTemplateVars = array( |
202
|
|
|
'request' => $request, |
203
|
|
|
'object' => $object, |
204
|
|
|
'form' => $form->createView(), |
205
|
|
|
'createRoute' => $this->crudCreateRoute(), |
206
|
|
|
'listRoute' => $this->crudListRoute(), |
207
|
|
|
'editRoute' => $this->crudEditRoute(), |
208
|
|
|
'viewRoute' => $this->crudViewRoute(), |
209
|
|
|
'deleteRoute' => $this->crudDeleteRoute(), |
210
|
|
|
'listRole' => $this->crudListRole(), |
211
|
|
|
'createRole' => $this->crudCreateRole(), |
212
|
|
|
'editRole' => $this->crudEditRole(), |
213
|
|
|
'viewRole' => $this->crudViewRole(), |
214
|
|
|
'deleteRole' => $this->crudDeleteRole(), |
215
|
|
|
'identifier' => $this->crudIdentifier(), |
216
|
|
|
'transPrefix' => $this->crudTransPrefix(), |
217
|
|
|
'transDomain' => $this->crudTransDomain(), |
218
|
|
|
'objectClass' => $this->crudObjectClass(), |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
return $this->crudEditRenderTemplateResponse( |
222
|
|
|
$request, |
223
|
|
|
$baseTemplateVars, |
224
|
|
|
$templateVars |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param Request $request |
230
|
|
|
* @param object|string|int $object |
231
|
|
|
* @param array $templateVars |
232
|
|
|
* |
233
|
|
|
* @return Response|RedirectResponse |
234
|
|
|
*/ |
235
|
|
|
public function crudViewObject(Request $request, $object, array $templateVars = array()) |
236
|
|
|
{ |
237
|
|
|
$object = $this->crudViewLoadObject($object, $request); |
238
|
|
|
|
239
|
|
|
$crudViewRole = $this->crudViewRole(); |
240
|
|
|
if (!$this->crudIsGranted($crudViewRole, $object)) { |
241
|
|
|
throw new AccessDeniedException(sprintf('You need the permission to view this object, role: %s!', $crudViewRole)); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$baseTemplateVars = array( |
245
|
|
|
'request' => $request, |
246
|
|
|
'object' => $object, |
247
|
|
|
'createRoute' => $this->crudCreateRoute(), |
248
|
|
|
'listRoute' => $this->crudListRoute(), |
249
|
|
|
'editRoute' => $this->crudEditRoute(), |
250
|
|
|
'viewRoute' => $this->crudViewRoute(), |
251
|
|
|
'deleteRoute' => $this->crudDeleteRoute(), |
252
|
|
|
'listRole' => $this->crudListRole(), |
253
|
|
|
'createRole' => $this->crudCreateRole(), |
254
|
|
|
'editRole' => $this->crudEditRole(), |
255
|
|
|
'viewRole' => $this->crudViewRole(), |
256
|
|
|
'deleteRole' => $this->crudDeleteRole(), |
257
|
|
|
'identifier' => $this->crudIdentifier(), |
258
|
|
|
'transPrefix' => $this->crudTransPrefix(), |
259
|
|
|
'transDomain' => $this->crudTransDomain(), |
260
|
|
|
'objectClass' => $this->crudObjectClass(), |
261
|
|
|
); |
262
|
|
|
|
263
|
|
|
return $this->crudViewRenderTemplateResponse( |
264
|
|
|
$request, |
265
|
|
|
$baseTemplateVars, |
266
|
|
|
$templateVars |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param Request $request |
272
|
|
|
* @param object|string|int $object |
273
|
|
|
* |
274
|
|
|
* @return Response|RedirectResponse |
275
|
|
|
*/ |
276
|
|
|
public function crudDeleteObject(Request $request, $object) |
277
|
|
|
{ |
278
|
|
|
$object = $this->crudDeleteLoadObject($object, $request); |
279
|
|
|
|
280
|
|
|
$crudDeleteRole = $this->crudDeleteRole(); |
281
|
|
|
if (!$this->crudIsGranted($crudDeleteRole, $object)) { |
282
|
|
|
throw new AccessDeniedException(sprintf('You need the permission to delete this object, role: %s!', $crudDeleteRole)); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$this->crudDeletePreRemove($object, $request); |
286
|
|
|
|
287
|
|
|
$em = $this->crudManagerForClass($this->crudObjectClass()); |
288
|
|
|
$em->remove($object); |
289
|
|
|
$em->flush(); |
290
|
|
|
|
291
|
|
|
$this->crudDeletePostFlush($object, $request); |
292
|
|
|
$this->crudDeleteSuccessFlashMesssage($object, $request); |
293
|
|
|
|
294
|
|
|
return $this->crudDeleteSuccessResponse($object, $request); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return int |
299
|
|
|
*/ |
300
|
|
|
protected function crudListPerPage() |
301
|
|
|
{ |
302
|
|
|
return 10; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @return Listing |
307
|
|
|
*/ |
308
|
|
|
protected function crudListListing() |
309
|
|
|
{ |
310
|
|
|
return $this |
311
|
|
|
->crudListingFactory() |
312
|
|
|
->create($this->crudObjectClass()) |
313
|
|
|
; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @return string |
318
|
|
|
*/ |
319
|
|
|
protected function crudListRoute() |
320
|
|
|
{ |
321
|
|
|
return strtolower(sprintf($this->crudRoutePattern(), $this->crudName(), 'list')); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return string |
326
|
|
|
*/ |
327
|
|
|
protected function crudListRole() |
328
|
|
|
{ |
329
|
|
|
return strtoupper(sprintf($this->crudRolePattern(), $this->crudName(), 'list')); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param Request $request |
334
|
|
|
* |
335
|
|
|
* @return FormInterface|null |
336
|
|
|
*/ |
337
|
|
|
protected function crudListForm(Request $request) |
338
|
|
|
{ |
339
|
|
|
if (null === $formType = $this->crudListFormType($request)) { |
340
|
|
|
return null; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return $this->crudForm($formType, array()); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @param Request $request |
348
|
|
|
* |
349
|
|
|
* @return FormTypeInterface|null |
350
|
|
|
*/ |
351
|
|
|
protected function crudListFormType(Request $request) |
352
|
|
|
{ |
353
|
|
|
return null; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @param Request $request |
358
|
|
|
* @param array $formData |
359
|
|
|
* |
360
|
|
|
* @return array |
361
|
|
|
*/ |
362
|
|
|
protected function crudListFormDataEnrich(Request $request, array $formData) |
363
|
|
|
{ |
364
|
|
|
return $formData; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @param Request $request |
369
|
|
|
* @param array $baseTemplateVars |
370
|
|
|
* @param array $templateVars |
371
|
|
|
* |
372
|
|
|
* @return Response |
373
|
|
|
*/ |
374
|
|
|
protected function crudListRenderTemplateResponse(Request $request, array $baseTemplateVars, array $templateVars) |
375
|
|
|
{ |
376
|
|
|
return $this->crudRender( |
377
|
|
|
$this->crudListTemplate(), |
378
|
|
|
array_replace_recursive($baseTemplateVars, $templateVars) |
379
|
|
|
); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @return string |
384
|
|
|
*/ |
385
|
|
|
protected function crudListTemplate() |
386
|
|
|
{ |
387
|
|
|
return sprintf($this->crudTemplatePattern(), ucfirst($this->crudName()), 'list'); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @return string |
392
|
|
|
*/ |
393
|
|
|
protected function crudCreateRoute() |
394
|
|
|
{ |
395
|
|
|
return strtolower(sprintf($this->crudRoutePattern(), $this->crudName(), 'create')); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
|
|
protected function crudCreateRole() |
402
|
|
|
{ |
403
|
|
|
return strtoupper(sprintf($this->crudRolePattern(), $this->crudName(), 'create')); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @param Request $request |
408
|
|
|
* |
409
|
|
|
* @return object |
410
|
|
|
*/ |
411
|
|
|
protected function crudCreateFactory(Request $request) |
412
|
|
|
{ |
413
|
|
|
$objectClass = $this->crudObjectClass(); |
414
|
|
|
|
415
|
|
|
return new $objectClass(); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* @param object $object |
420
|
|
|
* @param Request $request |
421
|
|
|
* |
422
|
|
|
* @return FormInterface |
423
|
|
|
*/ |
424
|
|
|
protected function crudCreateForm($object, Request $request) |
425
|
|
|
{ |
426
|
|
|
return $this->crudForm($this->crudCreateFormType($request, $object), $object); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* @param Request $request |
431
|
|
|
* @param object $object |
432
|
|
|
* @return FormTypeInterface |
433
|
|
|
* |
434
|
|
|
* @throws \Exception |
435
|
|
|
*/ |
436
|
|
|
protected function crudCreateFormType(Request $request, $object) |
437
|
|
|
{ |
438
|
|
|
throw new \Exception('You need to implement this method, if you use the createObject method!'); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @param object $object |
443
|
|
|
* @param FormInterface $form |
444
|
|
|
* @param Request $request |
445
|
|
|
* |
446
|
|
|
* @return bool |
447
|
|
|
*/ |
448
|
|
View Code Duplication |
protected function crudCreateIsSubmitted($object, FormInterface $form, Request $request) |
449
|
|
|
{ |
450
|
|
|
$buttonName = $this->crudCreateButtonName(); |
451
|
|
|
if (null !== $buttonName && !$form->get($buttonName)->isClicked()) { |
452
|
|
|
return false; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
return true; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @return string|null |
460
|
|
|
*/ |
461
|
|
|
protected function crudCreateButtonName() |
462
|
|
|
{ |
463
|
|
|
return null; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* @param object $object |
468
|
|
|
* @param FormInterface $form |
469
|
|
|
* @param Request $request |
470
|
|
|
*/ |
471
|
|
|
protected function crudCreateSuccessFlashMesssage($object, FormInterface $form, Request $request) |
472
|
|
|
{ |
473
|
|
|
$this->crudFlashMessage($request, 'success', sprintf('%s.create.flash.success', $this->crudTransPrefix())); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* @param object $object |
478
|
|
|
* @param FormInterface $form |
479
|
|
|
* @param Request $request |
480
|
|
|
*/ |
481
|
|
|
protected function crudCreateErrorFlashMesssage($object, FormInterface $form, Request $request) |
482
|
|
|
{ |
483
|
|
|
$this->crudFlashMessage($request, 'error', sprintf('%s.create.flash.error', $this->crudTransPrefix())); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* @param object $object |
488
|
|
|
* @param FormInterface $form |
489
|
|
|
* @param Request $request |
490
|
|
|
* |
491
|
|
|
* @return RedirectResponse|Response |
492
|
|
|
*/ |
493
|
|
View Code Duplication |
protected function crudCreateSuccessResponse($object, FormInterface $form, Request $request) |
494
|
|
|
{ |
495
|
|
|
$identifierMethod = $this->crudIdentifierMethod(); |
496
|
|
|
$url = $this->crudGenerateRoute($this->crudEditRoute(), array('id' => $object->$identifierMethod())); |
497
|
|
|
|
498
|
|
|
return new RedirectResponse($url, 302); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* @param object $object |
503
|
|
|
* @param FormInterface $form |
504
|
|
|
* @param Request $request |
505
|
|
|
* |
506
|
|
|
* @return RedirectResponse|Response|null |
507
|
|
|
*/ |
508
|
|
|
protected function crudCreateErrorResponse($object, FormInterface $form, Request $request) |
509
|
|
|
{ |
510
|
|
|
return null; |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* @param Request $request |
515
|
|
|
* @param array $baseTemplateVars |
516
|
|
|
* @param array $templateVars |
517
|
|
|
* |
518
|
|
|
* @return Response |
519
|
|
|
*/ |
520
|
|
|
protected function crudCreateRenderTemplateResponse(Request $request, array $baseTemplateVars, array $templateVars) |
521
|
|
|
{ |
522
|
|
|
return $this->crudRender( |
523
|
|
|
$this->crudCreateTemplate(), |
524
|
|
|
array_replace_recursive($baseTemplateVars, $templateVars) |
525
|
|
|
); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* @return string |
530
|
|
|
*/ |
531
|
|
|
protected function crudCreateTemplate() |
532
|
|
|
{ |
533
|
|
|
return sprintf($this->crudTemplatePattern(), ucfirst($this->crudName()), 'create'); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* @param object $object |
538
|
|
|
* @param FormInterface $form |
539
|
|
|
* @param Request $request |
540
|
|
|
*/ |
541
|
|
|
protected function crudCreatePrePersist($object, FormInterface $form, Request $request) |
542
|
|
|
{ |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* @param object $object |
547
|
|
|
* @param FormInterface $form |
548
|
|
|
* @param Request $request |
549
|
|
|
*/ |
550
|
|
|
protected function crudCreatePostFlush($object, FormInterface $form, Request $request) |
551
|
|
|
{ |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
/** |
555
|
|
|
* @return string |
556
|
|
|
*/ |
557
|
|
|
protected function crudEditRoute() |
558
|
|
|
{ |
559
|
|
|
return strtolower(sprintf($this->crudRoutePattern(), $this->crudName(), 'edit')); |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* @param object|string|int $object |
564
|
|
|
* @param Request $request |
565
|
|
|
* |
566
|
|
|
* @return object |
567
|
|
|
*/ |
568
|
|
|
protected function crudEditLoadObject($object, Request $request) |
569
|
|
|
{ |
570
|
|
|
return $this->crudLoadObject($object, $request); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* @return string |
575
|
|
|
*/ |
576
|
|
|
protected function crudEditRole() |
577
|
|
|
{ |
578
|
|
|
return strtoupper(sprintf($this->crudRolePattern(), $this->crudName(), 'edit')); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* @param object $object |
583
|
|
|
* @param Request $request |
584
|
|
|
* |
585
|
|
|
* @return FormInterface |
586
|
|
|
*/ |
587
|
|
|
protected function crudEditForm($object, Request $request) |
588
|
|
|
{ |
589
|
|
|
return $this->crudForm($this->crudEditFormType($request, $object), $object); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
/** |
593
|
|
|
* @param Request $request |
594
|
|
|
* @param object $object |
595
|
|
|
* |
596
|
|
|
* @return FormTypeInterface |
597
|
|
|
* |
598
|
|
|
* @throws \Exception |
599
|
|
|
*/ |
600
|
|
|
protected function crudEditFormType(Request $request, $object) |
601
|
|
|
{ |
602
|
|
|
throw new \Exception('You need to implement this method, if you use the editObject method!'); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
/** |
606
|
|
|
* @param object $object |
607
|
|
|
* @param FormInterface $form |
608
|
|
|
* @param Request $request |
609
|
|
|
* |
610
|
|
|
* @return bool |
611
|
|
|
*/ |
612
|
|
View Code Duplication |
protected function crudEditIsSubmitted($object, FormInterface $form, Request $request) |
613
|
|
|
{ |
614
|
|
|
$buttonName = $this->crudEditButtonName(); |
615
|
|
|
if (null !== $buttonName && !$form->get($buttonName)->isClicked()) { |
616
|
|
|
return false; |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
return true; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* @return string|null |
624
|
|
|
*/ |
625
|
|
|
protected function crudEditButtonName() |
626
|
|
|
{ |
627
|
|
|
return null; |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* @param object $object |
632
|
|
|
* @param FormInterface $form |
633
|
|
|
* @param Request $request |
634
|
|
|
*/ |
635
|
|
|
protected function crudEditSuccessFlashMesssage($object, FormInterface $form, Request $request) |
636
|
|
|
{ |
637
|
|
|
$this->crudFlashMessage($request, 'success', sprintf('%s.edit.flash.success', $this->crudTransPrefix())); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* @param object $object |
642
|
|
|
* @param FormInterface $form |
643
|
|
|
* @param Request $request |
644
|
|
|
*/ |
645
|
|
|
protected function crudEditErrorFlashMesssage($object, FormInterface $form, Request $request) |
646
|
|
|
{ |
647
|
|
|
$this->crudFlashMessage($request, 'error', sprintf('%s.edit.flash.error', $this->crudTransPrefix())); |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
/** |
651
|
|
|
* @param object $object |
652
|
|
|
* @param FormInterface $form |
653
|
|
|
* @param Request $request |
654
|
|
|
* |
655
|
|
|
* @return RedirectResponse|Response |
656
|
|
|
*/ |
657
|
|
View Code Duplication |
protected function crudEditSuccessResponse($object, FormInterface $form, Request $request) |
658
|
|
|
{ |
659
|
|
|
$identifierMethod = $this->crudIdentifierMethod(); |
660
|
|
|
$url = $this->crudGenerateRoute($this->crudEditRoute(), array('id' => $object->$identifierMethod())); |
661
|
|
|
|
662
|
|
|
return new RedirectResponse($url, 302); |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* @param object $object |
667
|
|
|
* @param FormInterface $form |
668
|
|
|
* @param Request $request |
669
|
|
|
* |
670
|
|
|
* @return RedirectResponse|Response|null |
671
|
|
|
*/ |
672
|
|
|
protected function crudEditErrorResponse($object, FormInterface $form, Request $request) |
673
|
|
|
{ |
674
|
|
|
return null; |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* @param Request $request |
679
|
|
|
* @param array $baseTemplateVars |
680
|
|
|
* @param array $templateVars |
681
|
|
|
* |
682
|
|
|
* @return Response |
683
|
|
|
*/ |
684
|
|
|
protected function crudEditRenderTemplateResponse(Request $request, array $baseTemplateVars, array $templateVars) |
685
|
|
|
{ |
686
|
|
|
return $this->crudRender( |
687
|
|
|
$this->crudEditTemplate(), |
688
|
|
|
array_replace_recursive($baseTemplateVars, $templateVars) |
689
|
|
|
); |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* @return string |
694
|
|
|
*/ |
695
|
|
|
protected function crudEditTemplate() |
696
|
|
|
{ |
697
|
|
|
return sprintf($this->crudTemplatePattern(), ucfirst($this->crudName()), 'edit'); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* @param object $object |
702
|
|
|
* @param FormInterface $form |
703
|
|
|
* @param Request $request |
704
|
|
|
*/ |
705
|
|
|
protected function crudEditPrePersist($object, FormInterface $form, Request $request) |
706
|
|
|
{ |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
/** |
710
|
|
|
* @param object $object |
711
|
|
|
* @param FormInterface $form |
712
|
|
|
* @param Request $request |
713
|
|
|
*/ |
714
|
|
|
protected function crudEditPostFlush($object, FormInterface $form, Request $request) |
715
|
|
|
{ |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
/** |
719
|
|
|
* @return string |
720
|
|
|
*/ |
721
|
|
|
protected function crudViewRoute() |
722
|
|
|
{ |
723
|
|
|
return strtolower(sprintf($this->crudRoutePattern(), $this->crudName(), 'view')); |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* @param object|string|int $object |
728
|
|
|
* @param Request $request |
729
|
|
|
* |
730
|
|
|
* @return object |
731
|
|
|
*/ |
732
|
|
|
protected function crudViewLoadObject($object, Request $request) |
733
|
|
|
{ |
734
|
|
|
return $this->crudLoadObject($object, $request); |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* @return string |
739
|
|
|
*/ |
740
|
|
|
protected function crudViewRole() |
741
|
|
|
{ |
742
|
|
|
return strtoupper(sprintf($this->crudRolePattern(), $this->crudName(), 'view')); |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
/** |
746
|
|
|
* @param Request $request |
747
|
|
|
* @param array $baseTemplateVars |
748
|
|
|
* @param array $templateVars |
749
|
|
|
* |
750
|
|
|
* @return Response |
751
|
|
|
*/ |
752
|
|
|
protected function crudViewRenderTemplateResponse(Request $request, array $baseTemplateVars, array $templateVars) |
753
|
|
|
{ |
754
|
|
|
return $this->crudRender( |
755
|
|
|
$this->crudViewTemplate(), |
756
|
|
|
array_replace_recursive($baseTemplateVars, $templateVars) |
757
|
|
|
); |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* @return string |
762
|
|
|
*/ |
763
|
|
|
protected function crudViewTemplate() |
764
|
|
|
{ |
765
|
|
|
return sprintf($this->crudTemplatePattern(), ucfirst($this->crudName()), 'view'); |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
/** |
769
|
|
|
* @return string |
770
|
|
|
*/ |
771
|
|
|
protected function crudDeleteRoute() |
772
|
|
|
{ |
773
|
|
|
return strtolower(sprintf($this->crudRoutePattern(), $this->crudName(), 'delete')); |
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
/** |
777
|
|
|
* @param object|string|int $object |
778
|
|
|
* @param Request $request |
779
|
|
|
* |
780
|
|
|
* @return object |
781
|
|
|
*/ |
782
|
|
|
protected function crudDeleteLoadObject($object, Request $request) |
783
|
|
|
{ |
784
|
|
|
return $this->crudLoadObject($object, $request); |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
/** |
788
|
|
|
* @return string |
789
|
|
|
*/ |
790
|
|
|
protected function crudDeleteRole() |
791
|
|
|
{ |
792
|
|
|
return strtoupper(sprintf($this->crudRolePattern(), $this->crudName(), 'delete')); |
793
|
|
|
} |
794
|
|
|
|
795
|
|
|
/** |
796
|
|
|
* @param object $object |
797
|
|
|
* @param Request $request |
798
|
|
|
*/ |
799
|
|
|
protected function crudDeleteSuccessFlashMesssage($object, Request $request) |
800
|
|
|
{ |
801
|
|
|
$this->crudFlashMessage($request, 'success', sprintf('%s.delete.flash.success', $this->crudTransPrefix())); |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
/** |
805
|
|
|
* @param object $object |
806
|
|
|
* @param Request $request |
807
|
|
|
* |
808
|
|
|
* @return RedirectResponse|Response |
809
|
|
|
*/ |
810
|
|
|
protected function crudDeleteSuccessResponse($object, Request $request) |
811
|
|
|
{ |
812
|
|
|
return new RedirectResponse($this->crudGenerateRoute($this->crudListRoute()), 302); |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
/** |
816
|
|
|
* @param object $object |
817
|
|
|
* @param Request $request |
818
|
|
|
*/ |
819
|
|
|
protected function crudDeletePreRemove($object, Request $request) |
820
|
|
|
{ |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
/** |
824
|
|
|
* @param object $object |
825
|
|
|
* @param Request $request |
826
|
|
|
*/ |
827
|
|
|
protected function crudDeletePostFlush($object, Request $request) |
828
|
|
|
{ |
829
|
|
|
} |
830
|
|
|
|
831
|
|
|
/** |
832
|
|
|
* @return string |
833
|
|
|
*/ |
834
|
|
|
protected function crudRoutePattern() |
835
|
|
|
{ |
836
|
|
|
return '%s_%s'; |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
/** |
840
|
|
|
* @return string |
841
|
|
|
*/ |
842
|
|
|
protected function crudRolePattern() |
843
|
|
|
{ |
844
|
|
|
return 'role_%s_%s'; |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
/** |
848
|
|
|
* @return string |
849
|
|
|
*/ |
850
|
|
|
protected function crudTransPrefix() |
851
|
|
|
{ |
852
|
|
|
return Helper::camelCaseToUnderscore($this->crudName()); |
853
|
|
|
} |
854
|
|
|
|
855
|
|
|
/** |
856
|
|
|
* @return string |
857
|
|
|
*/ |
858
|
|
|
protected function crudTransDomain() |
859
|
|
|
{ |
860
|
|
|
return 'messages'; |
861
|
|
|
} |
862
|
|
|
|
863
|
|
|
/** |
864
|
|
|
* @return string |
865
|
|
|
* |
866
|
|
|
* @throws \Exception |
867
|
|
|
*/ |
868
|
|
|
protected function crudTemplatePattern() |
869
|
|
|
{ |
870
|
|
|
throw new \Exception(sprintf( |
871
|
|
|
'For actions using a template you need to define the template pattern like this: %s', |
872
|
|
|
'@SaxulumCrud/%s/%s.html.twig' |
873
|
|
|
)); |
874
|
|
|
} |
875
|
|
|
|
876
|
|
|
/** |
877
|
|
|
* @return string |
878
|
|
|
*/ |
879
|
|
|
abstract protected function crudName(); |
880
|
|
|
|
881
|
|
|
/** |
882
|
|
|
* @return string |
883
|
|
|
*/ |
884
|
|
|
abstract protected function crudObjectClass(); |
885
|
|
|
|
886
|
|
|
/** |
887
|
|
|
* @return AuthorizationCheckerInterface |
888
|
|
|
* |
889
|
|
|
* @throws ServiceNotFoundException |
890
|
|
|
*/ |
891
|
|
|
protected function crudAuthorizationChecker() |
892
|
|
|
{ |
893
|
|
|
throw new ServiceNotFoundException(sprintf( |
894
|
|
|
'For actions using authorization checker you need: %s', |
895
|
|
|
'Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface' |
896
|
|
|
)); |
897
|
|
|
} |
898
|
|
|
|
899
|
|
|
/** |
900
|
|
|
* @return SecurityContextInterface |
901
|
|
|
* |
902
|
|
|
* @throws ServiceNotFoundException |
903
|
|
|
*/ |
904
|
|
|
protected function crudSecurity() |
905
|
|
|
{ |
906
|
|
|
throw new ServiceNotFoundException(sprintf( |
907
|
|
|
'For actions using security you need: %s', |
908
|
|
|
'Symfony\Component\Security\Core\SecurityContextInterface' |
909
|
|
|
)); |
910
|
|
|
} |
911
|
|
|
|
912
|
|
|
/** |
913
|
|
|
* @return ManagerRegistry |
914
|
|
|
* |
915
|
|
|
* @throws ServiceNotFoundException |
916
|
|
|
*/ |
917
|
|
|
protected function crudDoctrine() |
918
|
|
|
{ |
919
|
|
|
throw new ServiceNotFoundException(sprintf( |
920
|
|
|
'For actions using doctrine you need: %s', |
921
|
|
|
'Doctrine\Common\Persistence\ManagerRegistry' |
922
|
|
|
)); |
923
|
|
|
} |
924
|
|
|
|
925
|
|
|
/** |
926
|
|
|
* @return FormFactoryInterface |
927
|
|
|
* |
928
|
|
|
* @throws ServiceNotFoundException |
929
|
|
|
*/ |
930
|
|
|
protected function crudFormFactory() |
931
|
|
|
{ |
932
|
|
|
throw new ServiceNotFoundException(sprintf( |
933
|
|
|
'For actions using form you need: %s', |
934
|
|
|
'Symfony\Component\Form\FormFactoryInterface' |
935
|
|
|
)); |
936
|
|
|
} |
937
|
|
|
|
938
|
|
|
/** |
939
|
|
|
* @return PaginatorInterface |
940
|
|
|
* |
941
|
|
|
* @throws ServiceNotFoundException |
942
|
|
|
*/ |
943
|
|
|
protected function crudPaginator() |
944
|
|
|
{ |
945
|
|
|
throw new ServiceNotFoundException(sprintf( |
946
|
|
|
'For actions using pagination you need: %s', |
947
|
|
|
'Saxulum\Crud\Pagination\PaginatorInterface' |
948
|
|
|
)); |
949
|
|
|
} |
950
|
|
|
|
951
|
|
|
/** |
952
|
|
|
* @return UrlGeneratorInterface |
953
|
|
|
* |
954
|
|
|
* @throws ServiceNotFoundException |
955
|
|
|
*/ |
956
|
|
|
protected function crudUrlGenerator() |
957
|
|
|
{ |
958
|
|
|
throw new ServiceNotFoundException(sprintf( |
959
|
|
|
'For actions using url generation you need: %s', |
960
|
|
|
'Symfony\Component\Routing\Generator\UrlGeneratorInterface' |
961
|
|
|
)); |
962
|
|
|
} |
963
|
|
|
|
964
|
|
|
/** |
965
|
|
|
* @return \Twig_Environment |
966
|
|
|
* |
967
|
|
|
* @throws ServiceNotFoundException |
968
|
|
|
*/ |
969
|
|
|
protected function crudTwig() |
970
|
|
|
{ |
971
|
|
|
throw new ServiceNotFoundException(sprintf( |
972
|
|
|
'For actions using twig you need: %s', |
973
|
|
|
'\Twig_Environment' |
974
|
|
|
)); |
975
|
|
|
} |
976
|
|
|
|
977
|
|
|
/** |
978
|
|
|
* @return ListingFactory |
979
|
|
|
* |
980
|
|
|
* @throws ServiceNotFoundException |
981
|
|
|
*/ |
982
|
|
|
protected function crudListingFactory() |
983
|
|
|
{ |
984
|
|
|
throw new ServiceNotFoundException(sprintf( |
985
|
|
|
'For actions using listing factory you need: %s', |
986
|
|
|
'Saxulum\Crud\Listing\ListingFactory' |
987
|
|
|
)); |
988
|
|
|
} |
989
|
|
|
|
990
|
|
|
/** |
991
|
|
|
* @param mixed $attributes |
992
|
|
|
* @param mixed $object |
993
|
|
|
* |
994
|
|
|
* @return bool |
995
|
|
|
* |
996
|
|
|
* @throws \Exception |
997
|
|
|
*/ |
998
|
|
|
protected function crudIsGranted($attributes, $object = null) |
999
|
|
|
{ |
1000
|
|
|
try { |
1001
|
|
|
return $this->crudAuthorizationChecker()->isGranted($attributes, $object); |
1002
|
|
|
} catch (ServiceNotFoundException $e) { |
1003
|
|
|
return $this->crudSecurity()->isGranted($attributes, $object); |
1004
|
|
|
} |
1005
|
|
|
} |
1006
|
|
|
|
1007
|
|
|
/** |
1008
|
|
|
* @param object|string|int $object |
1009
|
|
|
* @param Request $request |
1010
|
|
|
* |
1011
|
|
|
* @return object object |
1012
|
|
|
*/ |
1013
|
|
|
protected function crudLoadObject($object, Request $request) |
1014
|
|
|
{ |
1015
|
|
|
if (is_object($object)) { |
1016
|
|
|
return $object; |
1017
|
|
|
} |
1018
|
|
|
|
1019
|
|
|
/** @var ObjectRepository $repo */ |
1020
|
|
|
$repo = $this->crudRepositoryForClass($this->crudObjectClass()); |
1021
|
|
|
$object = $repo->find($object); |
1022
|
|
|
|
1023
|
|
|
if (null === $object) { |
1024
|
|
|
throw new NotFoundHttpException('There is no object with this id'); |
1025
|
|
|
} |
1026
|
|
|
|
1027
|
|
|
return $object; |
1028
|
|
|
} |
1029
|
|
|
|
1030
|
|
|
/** |
1031
|
|
|
* @param string $class |
1032
|
|
|
* |
1033
|
|
|
* @return ObjectManager |
1034
|
|
|
* |
1035
|
|
|
* @throws \Exception |
1036
|
|
|
*/ |
1037
|
|
|
protected function crudManagerForClass($class) |
1038
|
|
|
{ |
1039
|
|
|
$om = $this->crudDoctrine()->getManagerForClass($class); |
1040
|
|
|
|
1041
|
|
|
if (null === $om) { |
1042
|
|
|
throw new \Exception(sprintf('There is no object manager for class: %s', $class)); |
1043
|
|
|
} |
1044
|
|
|
|
1045
|
|
|
return $om; |
1046
|
|
|
} |
1047
|
|
|
|
1048
|
|
|
/** |
1049
|
|
|
* @param string $class |
1050
|
|
|
* |
1051
|
|
|
* @return ObjectRepository |
1052
|
|
|
*/ |
1053
|
|
|
protected function crudRepositoryForClass($class) |
1054
|
|
|
{ |
1055
|
|
|
return $this->crudManagerForClass($class)->getRepository($class); |
1056
|
|
|
} |
1057
|
|
|
|
1058
|
|
|
/** |
1059
|
|
|
* @return string |
1060
|
|
|
* |
1061
|
|
|
* @throws \Exception |
1062
|
|
|
*/ |
1063
|
|
|
protected function crudIdentifier() |
1064
|
|
|
{ |
1065
|
|
|
$em = $this->crudManagerForClass($this->crudObjectClass()); |
1066
|
|
|
$meta = $em->getClassMetadata($this->crudObjectClass()); |
1067
|
|
|
|
1068
|
|
|
$identifier = $meta->getIdentifier(); |
1069
|
|
|
|
1070
|
|
|
if (1 !== count($identifier)) { |
1071
|
|
|
throw new \Exception('There are multiple fields define the identifier, which is not supported!'); |
1072
|
|
|
} |
1073
|
|
|
|
1074
|
|
|
return reset($identifier); |
1075
|
|
|
} |
1076
|
|
|
|
1077
|
|
|
/** |
1078
|
|
|
* @return string |
1079
|
|
|
* |
1080
|
|
|
* @throws \Exception |
1081
|
|
|
*/ |
1082
|
|
|
protected function crudIdentifierMethod() |
1083
|
|
|
{ |
1084
|
|
|
$identifier = $this->crudIdentifier(); |
1085
|
|
|
|
1086
|
|
|
return 'get'.ucfirst($identifier); |
1087
|
|
|
} |
1088
|
|
|
|
1089
|
|
|
/** |
1090
|
|
|
* @param FormTypeInterface $type |
1091
|
|
|
* @param mixed $data |
1092
|
|
|
* @param array $options |
1093
|
|
|
* |
1094
|
|
|
* @return FormInterface |
1095
|
|
|
*/ |
1096
|
|
|
protected function crudForm(FormTypeInterface $type, $data = null, array $options = array()) |
1097
|
|
|
{ |
1098
|
|
|
return $this->crudFormFactory()->create($type, $data, $options); |
1099
|
|
|
} |
1100
|
|
|
|
1101
|
|
|
/** |
1102
|
|
|
* @param object $target |
1103
|
|
|
* @param Request $request |
1104
|
|
|
* |
1105
|
|
|
* @return AbstractPagination |
1106
|
|
|
*/ |
1107
|
|
|
protected function crudPaginate($target, Request $request) |
1108
|
|
|
{ |
1109
|
|
|
return $this->crudPaginator()->paginate( |
1110
|
|
|
$target, |
1111
|
|
|
$request->query->get('page', 1), |
1112
|
|
|
$request->query->get('perPage', $this->crudListPerPage()) |
1113
|
|
|
); |
1114
|
|
|
} |
1115
|
|
|
|
1116
|
|
|
/** |
1117
|
|
|
* @param string $name |
1118
|
|
|
* @param array $parameters |
1119
|
|
|
* |
1120
|
|
|
* @return string |
1121
|
|
|
*/ |
1122
|
|
|
protected function crudGenerateRoute($name, array $parameters = array()) |
1123
|
|
|
{ |
1124
|
|
|
return $this->crudUrlGenerator()->generate($name, $parameters, UrlGeneratorInterface::ABSOLUTE_URL); |
1125
|
|
|
} |
1126
|
|
|
|
1127
|
|
|
/** |
1128
|
|
|
* @param string $view |
1129
|
|
|
* @param array $parameters |
1130
|
|
|
* |
1131
|
|
|
* @return Response |
1132
|
|
|
*/ |
1133
|
|
|
protected function crudRender($view, array $parameters = array()) |
1134
|
|
|
{ |
1135
|
|
|
return new Response($this->crudTwig()->render($view, $parameters)); |
1136
|
|
|
} |
1137
|
|
|
|
1138
|
|
|
/** |
1139
|
|
|
* @param Request $request |
1140
|
|
|
* @param string $type |
1141
|
|
|
* @param string $message |
1142
|
|
|
*/ |
1143
|
|
|
protected function crudFlashMessage(Request $request, $type, $message) |
1144
|
|
|
{ |
1145
|
|
|
/** @var Session $session */ |
1146
|
|
|
$session = $request->getSession(); |
1147
|
|
|
$session->getFlashBag()->add($type, $message); |
1148
|
|
|
} |
1149
|
|
|
} |
1150
|
|
|
|