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