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