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