1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
9
|
|
|
use Fi\CoreBundle\Utils\Entity\Finder; |
10
|
|
|
use Fi\CoreBundle\Utils\Entity\EntityUtils; |
11
|
|
|
use Fi\CoreBundle\Utils\Tabella\ParametriTabella; |
12
|
|
|
use Symfony\Component\Asset\Packages; |
13
|
|
|
|
14
|
|
|
class FiCrudController extends AbstractController |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected $bundle; |
18
|
|
|
protected $controller; |
19
|
|
|
protected $permessi; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Lists all tables entities. |
24
|
|
|
*/ |
25
|
4 |
|
public function index(Request $request, Packages $assetsmanager) |
26
|
|
|
{ |
27
|
4 |
|
$bundle = $this->getBundle(); |
|
|
|
|
28
|
4 |
|
$controller = $this->getController(); |
29
|
4 |
|
$idpassato = $request->get('id'); |
|
|
|
|
30
|
|
|
|
31
|
4 |
|
if (!$this->getPermessi()->canRead()) { |
32
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto"); |
|
|
|
|
33
|
|
|
} |
34
|
4 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
35
|
|
|
|
36
|
4 |
|
$entityclassnotation = $this->getEntityClassNotation(); |
37
|
4 |
|
$entityclass = $this->getEntityClassName(); |
|
|
|
|
38
|
|
|
|
39
|
4 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$modellocolonne = array( |
42
|
|
|
/* |
|
|
|
|
43
|
|
|
$controller . ".nominativo" => array( |
44
|
|
|
"nometabella" => $controller, |
45
|
|
|
"nomecampo" => "nominativo", |
46
|
|
|
"etichetta" => "Nominativo", |
47
|
|
|
"ordine" => 10, |
48
|
|
|
"larghezza" => 200, |
49
|
|
|
"escluso" => false |
50
|
|
|
), |
51
|
|
|
$controller . ".datanascita" => array( |
52
|
|
|
"nometabella" => $controller, |
53
|
|
|
"nomecampo" => "datanascita", |
54
|
|
|
"etichetta" => "Data di nascita", |
55
|
|
|
"ordine" => 20, |
56
|
|
|
"larghezza" => 100, |
57
|
|
|
"escluso" => false |
58
|
|
|
), |
59
|
|
|
|
60
|
|
|
*/ |
61
|
4 |
|
); |
62
|
|
|
|
63
|
4 |
|
$colonneordinamento = array($controller . '.id' => "DESC"); |
|
|
|
|
64
|
4 |
|
$filtri = array(); |
|
|
|
|
65
|
4 |
|
$prefiltri = array(); |
|
|
|
|
66
|
4 |
|
$entityutils = new \Fi\CoreBundle\Utils\Entity\EntityUtils($this->get("doctrine")->getManager()); |
|
|
|
|
67
|
4 |
|
$tablenamefromentity = $entityutils->getTableFromEntity($entityclass); |
68
|
4 |
|
$colonneordinamento = array($tablenamefromentity . '.id' => "DESC"); |
|
|
|
|
69
|
4 |
|
$parametritabella = array("em" => ParametriTabella::setParameter("default"), |
|
|
|
|
70
|
4 |
|
'tablename' => ParametriTabella::setParameter($tablenamefromentity), |
71
|
4 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
72
|
4 |
|
'bundle' => ParametriTabella::setParameter($bundle), |
73
|
4 |
|
'entityname' => ParametriTabella::setParameter($entityclassnotation), |
74
|
4 |
|
'entityclass' => ParametriTabella::setParameter($entityclass), |
75
|
4 |
|
'formclass' => ParametriTabella::setParameter($formclass), |
76
|
4 |
|
'modellocolonne' => ParametriTabella::setParameter(json_encode($modellocolonne)), |
77
|
4 |
|
'permessi' => ParametriTabella::setParameter(json_encode($this->getPermessi())), |
78
|
4 |
|
'urltabella' => ParametriTabella::setParameter($assetsmanager->getUrl('/') . $controller . '/' . 'tabella'), |
79
|
4 |
|
'baseurl' => ParametriTabella::setParameter($assetsmanager->getUrl('/')), |
80
|
4 |
|
'idpassato' => ParametriTabella::setParameter($idpassato), |
81
|
4 |
|
'titolotabella' => ParametriTabella::setParameter("Elenco " . $controller), |
|
|
|
|
82
|
4 |
|
'paginacorrente' => ParametriTabella::setParameter("1"), |
|
|
|
|
83
|
4 |
|
'paginetotali' => ParametriTabella::setParameter(""), |
|
|
|
|
84
|
4 |
|
'righetotali' => ParametriTabella::setParameter("0"), |
|
|
|
|
85
|
4 |
|
'righeperpagina' => ParametriTabella::setParameter("15"), |
|
|
|
|
86
|
4 |
|
'estraituttirecords' => ParametriTabella::setParameter("0"), |
|
|
|
|
87
|
4 |
|
'colonneordinamento' => ParametriTabella::setParameter(json_encode($colonneordinamento)), |
88
|
4 |
|
'filtri' => ParametriTabella::setParameter(json_encode($filtri)), |
89
|
4 |
|
'prefiltri' => ParametriTabella::setParameter(json_encode($prefiltri)), |
90
|
4 |
|
'traduzionefiltri' => ParametriTabella::setParameter(""), |
|
|
|
|
91
|
|
|
); |
92
|
|
|
|
93
|
4 |
|
return $this->render($crudtemplate, array('parametritabella' => $parametritabella, )); |
94
|
|
|
} |
95
|
|
|
/** |
96
|
|
|
* Displays a form to create a new table entity. |
97
|
|
|
*/ |
98
|
5 |
|
public function new(Request $request) |
99
|
|
|
{ |
100
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
101
|
5 |
|
$bundle = $this->getBundle(); |
|
|
|
|
102
|
5 |
|
$controller = $this->getController(); |
103
|
5 |
|
if (!$this->getPermessi()->canCreate()) { |
104
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
5 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
|
|
|
108
|
5 |
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
109
|
|
|
|
110
|
5 |
|
$entityclass = $this->getEntityClassName(); |
111
|
5 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
112
|
|
|
|
113
|
5 |
|
$entity = new $entityclass(); |
|
|
|
|
114
|
5 |
|
$formType = $formclass . 'Type'; |
115
|
5 |
|
$form = $this->createForm($formType, $entity, array('attr' => array( |
|
|
|
|
116
|
5 |
|
'id' => 'formdati' . $controller, |
117
|
|
|
), |
118
|
5 |
|
'action' => $this->generateUrl($controller . '_new'), |
119
|
|
|
)); |
120
|
|
|
|
121
|
5 |
|
$form->handleRequest($request); |
122
|
|
|
|
123
|
|
|
$twigparms = array( |
124
|
5 |
|
'form' => $form->createView(), |
125
|
5 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
126
|
5 |
|
'tabellatemplate' => $tabellatemplate |
127
|
|
|
); |
128
|
|
|
|
129
|
5 |
|
if ($form->isSubmitted()) { |
130
|
5 |
|
if ($form->isValid()) { |
131
|
5 |
|
$entity = $form->getData(); |
132
|
|
|
|
133
|
5 |
|
$entityManager = $this->getDoctrine()->getManager(); |
134
|
5 |
|
$entityManager->persist($entity); |
135
|
5 |
|
$entityManager->flush(); |
136
|
5 |
|
return new Response( |
137
|
5 |
|
$this->renderView($crudtemplate, $twigparms), |
138
|
5 |
|
200 |
139
|
|
|
); |
140
|
|
|
} else { |
141
|
|
|
//Quando non passa la validazione |
142
|
|
|
return new Response( |
143
|
|
|
$this->renderView($crudtemplate, $twigparms), |
144
|
|
|
400 |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
} else { |
148
|
|
|
//Quando viene richiesta una "nuova" new |
149
|
5 |
|
return new Response( |
150
|
5 |
|
$this->renderView($crudtemplate, $twigparms), |
151
|
5 |
|
200 |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Displays a form to edit an existing table entity. |
158
|
|
|
*/ |
159
|
6 |
|
public function edit(Request $request, $id) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
162
|
6 |
|
$bundle = $this->getBundle(); |
|
|
|
|
163
|
6 |
|
$controller = $this->getController(); |
164
|
|
|
|
165
|
6 |
|
if (!$this->getPermessi()->canUpdate()) { |
166
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
167
|
|
|
} |
168
|
6 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
|
|
|
169
|
6 |
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
170
|
|
|
|
171
|
6 |
|
$entityclass = $this->getEntityClassName(); |
172
|
6 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
173
|
|
|
|
174
|
6 |
|
$formType = $formclass . 'Type'; |
175
|
|
|
|
176
|
6 |
|
$elencomodifiche = $this->elencoModifiche($controller, $id); |
177
|
|
|
|
178
|
6 |
|
$em = $this->getDoctrine()->getManager(); |
179
|
|
|
|
180
|
6 |
|
$entity = $em->getRepository($entityclass)->find($id); |
181
|
|
|
|
182
|
6 |
|
if (!$entity) { |
183
|
|
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' del record con id ' . $id . '.'); |
184
|
|
|
} |
185
|
|
|
|
186
|
6 |
|
$editForm = $this->createForm( |
187
|
6 |
|
$formType, |
188
|
6 |
|
$entity, |
189
|
|
|
array('attr' => array( |
190
|
6 |
|
'id' => 'formdati' . $controller, |
191
|
|
|
), |
192
|
6 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
193
|
|
|
) |
194
|
|
|
); |
195
|
|
|
|
196
|
6 |
|
return $this->render( |
197
|
6 |
|
$crudtemplate, |
198
|
|
|
array( |
199
|
6 |
|
'entity' => $entity, |
200
|
6 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
201
|
6 |
|
'tabellatemplate' => $tabellatemplate, |
202
|
6 |
|
'edit_form' => $editForm->createView(), |
203
|
6 |
|
'elencomodifiche' => $elencomodifiche, |
204
|
|
|
) |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Edits an existing table entity. |
210
|
|
|
*/ |
211
|
6 |
|
public function update(Request $request, $id) |
212
|
|
|
{ |
213
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
214
|
6 |
|
$bundle = $this->getBundle(); |
|
|
|
|
215
|
6 |
|
$controller = $this->getController(); |
216
|
6 |
|
if (!$this->getPermessi()->canUpdate()) { |
217
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
218
|
|
|
} |
219
|
6 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, "edit"); |
|
|
|
|
220
|
|
|
|
221
|
6 |
|
$entityclass = $this->getEntityClassName(); |
222
|
6 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
223
|
6 |
|
$formType = $formclass . 'Type'; |
|
|
|
|
224
|
|
|
|
225
|
6 |
|
$em = $this->getDoctrine()->getManager(); |
226
|
|
|
|
227
|
6 |
|
$entity = $em->getRepository($entityclass)->find($id); |
228
|
|
|
|
229
|
6 |
|
if (!$entity) { |
230
|
|
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' per il record con id ' . $id); |
231
|
|
|
} |
232
|
|
|
|
233
|
6 |
|
$editForm = $this->createForm( |
234
|
6 |
|
$formType, |
235
|
6 |
|
$entity, |
236
|
|
|
array('attr' => array( |
237
|
6 |
|
'id' => 'formdati' . $controller, |
238
|
|
|
), |
239
|
6 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
240
|
|
|
) |
241
|
|
|
); |
242
|
|
|
|
243
|
6 |
|
$editForm->submit($request->request->get($editForm->getName())); |
244
|
|
|
|
245
|
6 |
|
if ($editForm->isValid()) { |
246
|
6 |
|
$originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
247
|
|
|
|
248
|
6 |
|
$em->persist($entity); |
249
|
6 |
|
$em->flush(); |
250
|
|
|
|
251
|
6 |
|
$newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
|
|
|
|
252
|
6 |
|
$repoStorico = $em->getRepository("CoreBundle:Storicomodifiche"); |
|
|
|
|
253
|
6 |
|
$changes = $repoStorico->isRecordChanged($controller, $originalData, $newData); |
|
|
|
|
254
|
|
|
|
255
|
6 |
|
if ($changes) { |
256
|
2 |
|
$repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
257
|
|
|
} |
258
|
|
|
|
259
|
6 |
|
$continua = (int) $request->get('continua'); |
260
|
6 |
|
if ($continua === 0) { |
261
|
6 |
|
return new Response('OK'); |
262
|
|
|
} else { |
263
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $this->render( |
268
|
|
|
$crudtemplate, |
269
|
|
|
array( |
270
|
|
|
'entity' => $entity, |
271
|
|
|
'edit_form' => $editForm->createView(), |
272
|
|
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
273
|
|
|
) |
274
|
|
|
); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Deletes a table entity. |
279
|
|
|
*/ |
280
|
5 |
|
public function delete(Request $request) |
281
|
|
|
{ |
282
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
283
|
5 |
|
if (!$this->getPermessi()->canDelete()) { |
284
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per eliminare questo contenuto"); |
|
|
|
|
285
|
|
|
} |
286
|
5 |
|
$entityclass = $this->getEntityClassName(); |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
try { |
290
|
5 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
291
|
5 |
|
$qb = $em->createQueryBuilder(); |
|
|
|
|
292
|
5 |
|
$ids = explode(', ', $request->get('id')); |
293
|
5 |
|
$qb->delete($entityclass, 'u') |
294
|
5 |
|
->andWhere('u.id IN (:ids)') |
295
|
5 |
|
->setParameter('ids', $ids); |
296
|
|
|
|
297
|
5 |
|
$query = $qb->getQuery(); |
298
|
5 |
|
$query->execute(); |
299
|
|
|
} catch (\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException $e) { |
300
|
|
|
$response = new Response($e->getMessage()); |
301
|
|
|
$response->setStatusCode('501'); |
302
|
|
|
return $response; |
303
|
|
|
} catch (\Exception $e) { |
304
|
|
|
$response = new Response($e->getMessage()); |
305
|
|
|
$response->setStatusCode('200'); |
306
|
|
|
return $response; |
307
|
|
|
} |
308
|
|
|
|
309
|
5 |
|
return new Response('Operazione eseguita con successo'); |
310
|
|
|
} |
311
|
|
|
|
312
|
6 |
|
private function elencoModifiche($controller, $id) |
313
|
|
|
{ |
314
|
6 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
315
|
6 |
|
$risultato = $em->getRepository('CoreBundle:Storicomodifiche')->findBy( |
316
|
|
|
array( |
317
|
6 |
|
'nometabella' => $controller, |
318
|
6 |
|
'idtabella' => $id, |
319
|
|
|
), |
320
|
6 |
|
array('giorno' => 'DESC') |
321
|
|
|
); |
322
|
|
|
|
323
|
6 |
|
return $risultato; |
324
|
|
|
} |
325
|
|
|
|
326
|
6 |
|
protected function getTabellaTemplate($controller) |
327
|
|
|
{ |
328
|
6 |
|
$tabellatemplate = $controller . '/Tabella/tabellaform.html.twig'; |
329
|
6 |
|
if (!$this->get('templating')->exists($tabellatemplate)) { |
330
|
6 |
|
$tabellatemplate = 'CoreBundle:' . $controller . ':Tabella/tabellaform.html.twig'; |
331
|
6 |
|
if (!$this->get('templating')->exists($tabellatemplate)) { |
332
|
6 |
|
$tabellatemplate = 'CoreBundle:Standard:Tabella/tabellaform.html.twig'; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
6 |
|
return $tabellatemplate; |
337
|
|
|
} |
338
|
|
|
|
339
|
6 |
|
protected function getCrudTemplate($bundle, $controller, $operation) |
340
|
|
|
{ |
341
|
6 |
|
$crudtemplate = $bundle . ':' . $controller . ':Crud/' . $this->getThisFunctionName() . '.html.twig'; |
342
|
6 |
|
if (!$this->get('templating')->exists($crudtemplate)) { |
343
|
6 |
|
$crudtemplate = $controller . '/Crud/' . $operation . '.html.twig'; |
344
|
6 |
|
if (!$this->get('templating')->exists($crudtemplate)) { |
345
|
5 |
|
$crudtemplate = 'CoreBundle:Standard:Crud/' . $operation . '.html.twig'; |
346
|
|
|
} |
347
|
|
|
} |
348
|
6 |
|
return $crudtemplate; |
349
|
|
|
} |
350
|
6 |
|
protected function getBundle() |
351
|
|
|
{ |
352
|
6 |
|
return $this->bundle; |
353
|
|
|
} |
354
|
6 |
|
protected function getController() |
355
|
|
|
{ |
356
|
6 |
|
return $this->controller; |
357
|
|
|
} |
358
|
6 |
|
protected function getPermessi() |
359
|
|
|
{ |
360
|
6 |
|
return $this->permessi; |
361
|
|
|
} |
362
|
|
|
/** |
363
|
|
|
* Returns the calling function through a backtrace |
364
|
|
|
*/ |
365
|
6 |
|
protected function getThisFunctionName() |
366
|
|
|
{ |
367
|
|
|
// a funciton x has called a function y which called this |
368
|
|
|
// see stackoverflow.com/questions/190421 |
369
|
6 |
|
$caller = debug_backtrace(); |
370
|
6 |
|
$caller = $caller[1]; |
371
|
6 |
|
return $caller['function']; |
372
|
|
|
} |
373
|
6 |
|
protected function getEntityClassNotation() |
374
|
|
|
{ |
375
|
6 |
|
$em = $this->get("doctrine")->getManager(); |
|
|
|
|
376
|
6 |
|
$entityfinder = new Finder($em, $this->controller); |
377
|
6 |
|
$entityutils = new EntityUtils($em); |
|
|
|
|
378
|
6 |
|
return $entityutils->getClassNameToShortcutNotations($entityfinder->getClassNameFromEntityName()); |
379
|
|
|
} |
380
|
6 |
|
protected function getEntityClassName() |
381
|
|
|
{ |
382
|
6 |
|
$em = $this->get("doctrine")->getManager(); |
|
|
|
|
383
|
6 |
|
$entityfinder = new Finder($em, $this->controller); |
384
|
6 |
|
return $entityfinder->getClassNameFromEntityName(); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
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.