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