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