1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
9
|
|
|
|
10
|
|
|
class FiCrudController extends Controller |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public static $namespace; |
14
|
|
|
public static $bundle; |
15
|
|
|
public static $controller; |
16
|
|
|
public static $action; |
17
|
|
|
public static $parametrigriglia; |
18
|
|
|
|
19
|
13 |
|
protected function setup(Request $request) |
20
|
|
|
{ |
21
|
13 |
|
$matches = array(); |
22
|
13 |
|
$controllo = new \ReflectionClass(get_class($this)); |
23
|
|
|
|
24
|
13 |
|
preg_match('/(.*)\\\(.*)Bundle\\\Controller\\\(.*)Controller/', $controllo->name, $matches); |
25
|
|
|
|
26
|
13 |
|
self::$namespace = $matches[1]; |
27
|
13 |
|
self::$bundle = $matches[2]; |
28
|
13 |
|
self::$controller = $matches[3]; |
29
|
13 |
|
self::$action = substr($request->attributes->get('_controller'), strrpos($request->attributes->get('_controller'), ':') + 1); |
30
|
13 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Lists all tables entities. |
34
|
|
|
*/ |
35
|
4 |
|
public function indexAction(Request $request) |
36
|
|
|
{ |
37
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
38
|
4 |
|
$this->setup($request); |
39
|
4 |
|
$namespace = $this->getNamespace(); |
40
|
4 |
|
$bundle = $this->getBundle(); |
41
|
4 |
|
$controller = $this->getController(); |
42
|
4 |
|
$container = $this->container; |
43
|
|
|
|
44
|
4 |
|
$gestionepermessi = $this->get('ficorebundle.gestionepermessi'); |
45
|
4 |
|
$canRead = ($gestionepermessi->leggere(array('modulo' => $controller)) ? 1 : 0); |
46
|
4 |
|
$canDelete = ($gestionepermessi->cancellare(array('modulo' => $controller)) ? 1 : 0); |
47
|
4 |
|
$canCreare = ($gestionepermessi->creare(array('modulo' => $controller)) ? 1 : 0); |
48
|
4 |
|
$canAggiornare = ($gestionepermessi->aggiornare(array('modulo' => $controller)) ? 1 : 0); |
49
|
4 |
|
if (!$canRead) { |
50
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto"); |
51
|
|
|
} |
52
|
4 |
|
$idpassato = $request->get('id'); |
53
|
|
|
|
54
|
4 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
55
|
|
|
|
56
|
4 |
|
$repotabelle = $this->get('OpzioniTabella_repository'); |
57
|
|
|
|
58
|
4 |
|
$paricevuti = array('nomebundle' => $nomebundle, 'nometabella' => $controller, 'container' => $container); |
59
|
|
|
|
60
|
4 |
|
$testatagriglia = Griglia::testataPerGriglia($paricevuti); |
61
|
|
|
|
62
|
4 |
|
$testatagriglia['multisearch'] = 1; |
63
|
4 |
|
$testatagriglia['showconfig'] = 1; |
64
|
4 |
|
$testatagriglia['overlayopen'] = 1; |
65
|
4 |
|
$testatagriglia['showadd'] = $canCreare; |
66
|
4 |
|
$testatagriglia['showedit'] = $canAggiornare; |
67
|
4 |
|
$testatagriglia['showdel'] = $canDelete; |
68
|
4 |
|
$testatagriglia["filterToolbar_searchOnEnter"] = true; |
69
|
4 |
|
$testatagriglia["filterToolbar_searchOperators"] = true; |
70
|
|
|
|
71
|
4 |
|
$testatagriglia['parametritesta'] = json_encode($paricevuti); |
72
|
|
|
|
73
|
4 |
|
$this->setParametriGriglia(array('request' => $request)); |
|
|
|
|
74
|
4 |
|
$testatagriglia['parametrigriglia'] = json_encode(self::$parametrigriglia); |
75
|
|
|
|
76
|
4 |
|
$testata = $repotabelle->editTestataFormTabelle($testatagriglia, $controller, $container); |
77
|
4 |
|
return $this->render( |
78
|
4 |
|
$nomebundle . ':' . $controller . ':index.html.twig', |
79
|
|
|
array( |
80
|
4 |
|
'nomecontroller' => $controller, |
81
|
4 |
|
'testata' => $testata, |
82
|
4 |
|
'canread' => $canRead, |
83
|
4 |
|
'idpassato' => $idpassato, |
84
|
|
|
) |
85
|
4 |
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Creates a new table entity. |
90
|
|
|
*/ |
91
|
2 |
|
public function createAction(Request $request) |
92
|
|
|
{ |
93
|
2 |
|
$this->setup($request); |
94
|
2 |
|
$namespace = $this->getNamespace(); |
95
|
2 |
|
$bundle = $this->getBundle(); |
96
|
2 |
|
$controller = $this->getController(); |
97
|
|
|
|
98
|
2 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
99
|
2 |
|
$classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
100
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
101
|
|
|
|
102
|
2 |
|
$entity = new $classbundle(); |
103
|
2 |
|
$formType = $formbundle . 'Type'; |
104
|
|
|
|
105
|
2 |
|
$form = $this->createForm( |
106
|
2 |
|
$formType, |
107
|
2 |
|
$entity, |
108
|
|
|
array('attr' => array( |
109
|
2 |
|
'id' => 'formdati' . $controller, |
110
|
2 |
|
), |
111
|
2 |
|
'action' => $this->generateUrl($controller . '_create'), |
112
|
|
|
) |
113
|
2 |
|
); |
114
|
|
|
|
115
|
2 |
|
$form->submit($request->request->get($form->getName())); |
116
|
|
|
|
117
|
2 |
|
if ($form->isValid()) { |
118
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
119
|
2 |
|
$em->persist($entity); |
120
|
2 |
|
$em->flush(); |
121
|
|
|
|
122
|
2 |
|
$continua = $request->get('continua'); |
123
|
2 |
|
if ($continua == 0) { |
124
|
2 |
|
return new Response('OK'); |
125
|
|
|
} else { |
126
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $entity->getId()))); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->render( |
131
|
|
|
$nomebundle . ':' . $controller . ':new.html.twig', |
132
|
|
|
array( |
133
|
|
|
'nomecontroller' => $controller, |
134
|
|
|
'entity' => $entity, |
135
|
|
|
'form' => $form->createView(), |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Displays a form to create a new table entity. |
142
|
|
|
*/ |
143
|
2 |
|
public function newAction(Request $request) |
144
|
|
|
{ |
145
|
2 |
|
$this->setup($request); |
146
|
2 |
|
$namespace = $this->getNamespace(); |
147
|
2 |
|
$bundle = $this->getBundle(); |
148
|
2 |
|
$controller = $this->getController(); |
149
|
|
|
|
150
|
2 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
151
|
2 |
|
$classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
152
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
153
|
2 |
|
$formType = $formbundle . 'Type'; |
154
|
|
|
|
155
|
2 |
|
$entity = new $classbundle(); |
156
|
|
|
|
157
|
2 |
|
$form = $this->createForm( |
158
|
2 |
|
$formType, |
159
|
2 |
|
$entity, |
160
|
|
|
array('attr' => array( |
161
|
2 |
|
'id' => 'formdati' . $controller, |
162
|
2 |
|
), |
163
|
2 |
|
'action' => $this->generateUrl($controller . '_create'), |
164
|
|
|
) |
165
|
2 |
|
); |
166
|
|
|
|
167
|
2 |
|
return $this->render( |
168
|
2 |
|
$nomebundle . ':' . $controller . ':new.html.twig', |
169
|
|
|
array( |
170
|
2 |
|
'nomecontroller' => $controller, |
171
|
2 |
|
'entity' => $entity, |
172
|
2 |
|
'form' => $form->createView(), |
173
|
|
|
) |
174
|
2 |
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Displays a form to edit an existing table entity. |
179
|
|
|
*/ |
180
|
2 |
|
public function editAction(Request $request, $id) |
181
|
|
|
{ |
182
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
183
|
2 |
|
$this->setup($request); |
184
|
2 |
|
$namespace = $this->getNamespace(); |
185
|
2 |
|
$bundle = $this->getBundle(); |
186
|
2 |
|
$controller = $this->getController(); |
187
|
|
|
|
188
|
2 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
189
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
190
|
2 |
|
$formType = $formbundle . 'Type'; |
191
|
|
|
|
192
|
2 |
|
$elencomodifiche = $this->elencoModifiche($nomebundle, $controller, $id); |
|
|
|
|
193
|
|
|
|
194
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
195
|
|
|
|
196
|
2 |
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
197
|
|
|
|
198
|
2 |
|
if (!$entity) { |
199
|
|
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
200
|
|
|
} |
201
|
|
|
|
202
|
2 |
|
$editForm = $this->createForm( |
203
|
2 |
|
$formType, |
204
|
2 |
|
$entity, |
205
|
|
|
array('attr' => array( |
206
|
2 |
|
'id' => 'formdati' . $controller, |
207
|
2 |
|
), |
208
|
2 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
209
|
|
|
) |
210
|
2 |
|
); |
211
|
|
|
|
212
|
2 |
|
$deleteForm = $this->createDeleteForm($id); |
213
|
|
|
|
214
|
2 |
|
return $this->render( |
215
|
2 |
|
$nomebundle . ':' . $controller . ':edit.html.twig', |
216
|
|
|
array( |
217
|
2 |
|
'entity' => $entity, |
218
|
2 |
|
'nomecontroller' => $controller, |
219
|
2 |
|
'edit_form' => $editForm->createView(), |
220
|
2 |
|
'delete_form' => $deleteForm->createView(), |
221
|
2 |
|
'elencomodifiche' => $elencomodifiche, |
222
|
|
|
) |
223
|
2 |
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Edits an existing table entity. |
228
|
|
|
*/ |
229
|
2 |
|
public function updateAction(Request $request, $id) |
230
|
|
|
{ |
231
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
232
|
2 |
|
$this->setup($request); |
233
|
2 |
|
$namespace = $this->getNamespace(); |
234
|
2 |
|
$bundle = $this->getBundle(); |
235
|
2 |
|
$controller = $this->getController(); |
236
|
|
|
|
237
|
2 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
238
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
239
|
2 |
|
$formType = $formbundle . 'Type'; |
240
|
|
|
|
241
|
2 |
|
$repoStorico = $this->container->get('Storicomodifiche_repository'); |
242
|
|
|
|
243
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
244
|
|
|
|
245
|
2 |
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
246
|
|
|
|
247
|
2 |
|
if (!$entity) { |
248
|
|
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
249
|
|
|
} |
250
|
|
|
|
251
|
2 |
|
$deleteForm = $this->createDeleteForm($id); |
252
|
|
|
|
253
|
2 |
|
$editForm = $this->createForm( |
254
|
2 |
|
$formType, |
255
|
2 |
|
$entity, |
256
|
|
|
array('attr' => array( |
257
|
2 |
|
'id' => 'formdati' . $controller, |
258
|
2 |
|
), |
259
|
2 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
260
|
|
|
) |
261
|
2 |
|
); |
262
|
|
|
|
263
|
2 |
|
$editForm->submit($request->request->get($editForm->getName())); |
264
|
|
|
|
265
|
2 |
|
if ($editForm->isValid()) { |
266
|
2 |
|
$originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
267
|
|
|
|
268
|
2 |
|
$em->persist($entity); |
269
|
2 |
|
$em->flush(); |
270
|
|
|
|
271
|
2 |
|
$newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
272
|
2 |
|
$changes = $repoStorico->isRecordChanged($nomebundle, $controller, $originalData, $newData); |
273
|
|
|
|
274
|
2 |
|
if ($changes) { |
275
|
1 |
|
$repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
276
|
1 |
|
} |
277
|
|
|
|
278
|
2 |
|
$continua = $request->get('continua'); |
279
|
2 |
|
if ($continua == 0) { |
280
|
2 |
|
return new Response('OK'); |
281
|
|
|
} else { |
282
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $this->render( |
287
|
|
|
$nomebundle . ':' . $controller . ':edit.html.twig', |
288
|
|
|
array( |
289
|
|
|
'entity' => $entity, |
290
|
|
|
'edit_form' => $editForm->createView(), |
291
|
|
|
'delete_form' => $deleteForm->createView(), |
292
|
|
|
'nomecontroller' => $controller, |
293
|
|
|
) |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Edits an existing table entity. |
299
|
|
|
*/ |
300
|
|
|
public function aggiornaAction(Request $request) |
301
|
|
|
{ |
302
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
303
|
|
|
$this->setup($request); |
304
|
|
|
$namespace = $this->getNamespace(); |
305
|
|
|
$bundle = $this->getBundle(); |
306
|
|
|
$controller = $this->getController(); |
307
|
|
|
|
308
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
309
|
|
|
|
310
|
|
|
$id = $this->get('request')->request->get('id'); |
311
|
|
|
|
312
|
|
|
$em = $this->getDoctrine()->getManager(); |
313
|
|
|
|
314
|
|
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
315
|
|
|
|
316
|
|
|
if (!$entity) { |
317
|
|
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
throw $this->createNotFoundException("Implementare a seconda dell'esigenza 'aggiornaAction' del controller " |
321
|
|
|
. $nomebundle |
322
|
|
|
. '/' |
323
|
|
|
. $controller); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Deletes a table entity. |
328
|
|
|
*/ |
329
|
2 |
|
public function deleteAction(Request $request) |
330
|
|
|
{ |
331
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
332
|
2 |
|
$this->setup($request); |
333
|
2 |
|
$namespace = $this->getNamespace(); |
334
|
2 |
|
$bundle = $this->getBundle(); |
335
|
2 |
|
$controller = $this->getController(); |
336
|
|
|
|
337
|
2 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
338
|
|
|
|
339
|
|
|
//if (!$request->isXmlHttpRequest()) { |
|
|
|
|
340
|
|
|
// $request->checkCSRFProtection(); |
|
|
|
|
341
|
|
|
//} |
342
|
|
|
try { |
343
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
344
|
2 |
|
$qb = $em->createQueryBuilder(); |
345
|
2 |
|
$ids = explode(',', $request->get('id')); |
346
|
2 |
|
$qb->delete($nomebundle . ':' . $controller, 'u') |
347
|
2 |
|
->andWhere('u.id IN (:ids)') |
348
|
2 |
|
->setParameter('ids', $ids); |
349
|
|
|
|
350
|
2 |
|
$query = $qb->getQuery(); |
351
|
2 |
|
$query->execute(); |
352
|
2 |
|
} catch (\Exception $e) { |
353
|
|
|
$response = new Response(); |
354
|
|
|
$response->setStatusCode('200'); |
355
|
|
|
|
356
|
|
|
return new Response('404'); |
357
|
|
|
} |
358
|
|
|
|
359
|
2 |
|
return new Response('OK'); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Creates a form to delete a table entity by id. |
364
|
|
|
* |
365
|
|
|
* @param mixed $id The entity id |
366
|
|
|
* |
367
|
|
|
* @return \Symfony\Component\Form\Form The form |
368
|
|
|
*/ |
369
|
|
|
protected function createDeleteForm($id) |
370
|
|
|
{ |
371
|
|
|
return $this->createFormBuilder(array('id' => $id)) |
372
|
|
|
->add('id', get_class(new \Symfony\Component\Form\Extension\Core\Type\HiddenType())) |
373
|
|
|
->getForm(); |
374
|
|
|
} |
375
|
|
|
|
376
|
13 |
|
protected function getNamespace() |
377
|
|
|
{ |
378
|
13 |
|
return self::$namespace; |
379
|
|
|
} |
380
|
|
|
|
381
|
13 |
|
protected function getBundle() |
382
|
|
|
{ |
383
|
13 |
|
return self::$bundle; |
384
|
|
|
} |
385
|
|
|
|
386
|
13 |
|
protected function getController() |
387
|
|
|
{ |
388
|
13 |
|
return self::$controller; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
protected function getAction() |
392
|
|
|
{ |
393
|
|
|
return self::$action; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: