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