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