1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
8
|
|
|
use Cdf\BiCoreBundle\Utils\Tabella\ParametriTabella; |
9
|
|
|
|
10
|
|
|
trait FiCoreCrudControllerTrait |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Displays a form to create a new table entity. |
15
|
|
|
*/ |
16
|
7 |
|
public function new(Request $request) |
17
|
|
|
{ |
18
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
19
|
7 |
|
$bundle = $this->getBundle(); |
|
|
|
|
20
|
7 |
|
$controller = $this->getController(); |
|
|
|
|
21
|
7 |
|
if (!$this->getPermessi()->canCreate()) { |
|
|
|
|
22
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
6 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
|
|
|
26
|
6 |
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
|
|
|
|
27
|
|
|
|
28
|
6 |
|
$parametriform = $request->get("parametriform") ? json_decode($request->get("parametriform"), true) : array(); |
|
|
|
|
29
|
|
|
|
30
|
6 |
|
$entityclass = $this->getEntityClassName(); |
|
|
|
|
31
|
6 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
32
|
|
|
|
33
|
6 |
|
$entity = new $entityclass(); |
|
|
|
|
34
|
6 |
|
$formType = $formclass . 'Type'; |
35
|
6 |
|
$form = $this->createForm($formType, $entity, array('attr' => array( |
|
|
|
|
36
|
6 |
|
'id' => 'formdati' . $controller, |
37
|
|
|
), |
38
|
6 |
|
'action' => $this->generateUrl($controller . '_new'), "parametriform" => $parametriform |
|
|
|
|
39
|
|
|
)); |
40
|
|
|
|
41
|
6 |
|
$form->handleRequest($request); |
42
|
|
|
|
43
|
|
|
$twigparms = array( |
44
|
6 |
|
'form' => $form->createView(), |
45
|
6 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
46
|
6 |
|
'tabellatemplate' => $tabellatemplate |
47
|
|
|
); |
48
|
|
|
|
49
|
6 |
|
if ($form->isSubmitted()) { |
50
|
6 |
|
if ($form->isValid()) { |
51
|
5 |
|
$entity = $form->getData(); |
52
|
|
|
|
53
|
5 |
|
$entityManager = $this->getDoctrine()->getManager(); |
|
|
|
|
54
|
5 |
|
$entityManager->persist($entity); |
55
|
5 |
|
$entityManager->flush(); |
56
|
5 |
|
return new Response( |
57
|
5 |
|
$this->renderView($crudtemplate, $twigparms), |
|
|
|
|
58
|
5 |
|
200 |
59
|
|
|
); |
60
|
|
|
} else { |
61
|
|
|
//Quando non passa la validazione |
62
|
1 |
|
return new Response( |
63
|
1 |
|
$this->renderView($crudtemplate, $twigparms), |
64
|
1 |
|
400 |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} else { |
68
|
|
|
//Quando viene richiesta una "nuova" new |
69
|
6 |
|
return new Response( |
70
|
6 |
|
$this->renderView($crudtemplate, $twigparms), |
71
|
6 |
|
200 |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Displays a form to edit an existing table entity. |
78
|
|
|
*/ |
79
|
8 |
|
public function edit(Request $request, $id) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
82
|
8 |
|
$bundle = $this->getBundle(); |
|
|
|
|
83
|
8 |
|
$controller = $this->getController(); |
84
|
|
|
|
85
|
8 |
|
if (!$this->getPermessi()->canUpdate()) { |
86
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
87
|
|
|
} |
88
|
7 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
|
|
|
89
|
7 |
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
90
|
|
|
|
91
|
7 |
|
$entityclass = $this->getEntityClassName(); |
92
|
7 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
93
|
|
|
|
94
|
7 |
|
$formType = $formclass . 'Type'; |
95
|
|
|
|
96
|
7 |
|
$elencomodifiche = $this->elencoModifiche($controller, $id); |
97
|
|
|
|
98
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
99
|
|
|
|
100
|
7 |
|
$entity = $em->getRepository($entityclass)->find($id); |
101
|
|
|
|
102
|
7 |
|
if (!$entity) { |
103
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' del record con id ' . $id . '.'); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
7 |
|
$editForm = $this->createForm( |
107
|
7 |
|
$formType, |
108
|
7 |
|
$entity, |
109
|
|
|
array('attr' => array( |
110
|
7 |
|
'id' => 'formdati' . $controller, |
111
|
|
|
), |
112
|
7 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
|
116
|
7 |
|
return $this->render( |
|
|
|
|
117
|
7 |
|
$crudtemplate, |
118
|
|
|
array( |
119
|
7 |
|
'entity' => $entity, |
120
|
7 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
121
|
7 |
|
'tabellatemplate' => $tabellatemplate, |
122
|
7 |
|
'edit_form' => $editForm->createView(), |
123
|
7 |
|
'elencomodifiche' => $elencomodifiche, |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Edits an existing table entity. |
130
|
|
|
*/ |
131
|
9 |
|
public function update(Request $request, $id) |
132
|
|
|
{ |
133
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
134
|
9 |
|
$bundle = $this->getBundle(); |
|
|
|
|
135
|
9 |
|
$controller = $this->getController(); |
136
|
9 |
|
if (!$this->getPermessi()->canUpdate()) { |
137
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
138
|
|
|
} |
139
|
8 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, "edit"); |
|
|
|
|
140
|
|
|
|
141
|
8 |
|
$entityclass = $this->getEntityClassName(); |
142
|
8 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
143
|
8 |
|
$formType = $formclass . 'Type'; |
|
|
|
|
144
|
|
|
|
145
|
8 |
|
$em = $this->getDoctrine()->getManager(); |
146
|
|
|
|
147
|
8 |
|
$entity = $em->getRepository($entityclass)->find($id); |
148
|
|
|
|
149
|
8 |
|
if (!$entity) { |
150
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' per il record con id ' . $id); |
151
|
|
|
} |
152
|
|
|
|
153
|
7 |
|
$editForm = $this->createForm( |
154
|
7 |
|
$formType, |
155
|
7 |
|
$entity, |
156
|
|
|
array('attr' => array( |
157
|
7 |
|
'id' => 'formdati' . $controller, |
158
|
|
|
), |
159
|
7 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
160
|
|
|
) |
161
|
|
|
); |
162
|
|
|
|
163
|
7 |
|
$editForm->submit($request->request->get($editForm->getName())); |
164
|
|
|
|
165
|
7 |
|
if ($editForm->isValid()) { |
166
|
7 |
|
$originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
167
|
|
|
|
168
|
7 |
|
$em->persist($entity); |
169
|
7 |
|
$em->flush(); |
170
|
|
|
|
171
|
7 |
|
$newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
|
|
|
|
172
|
7 |
|
$repoStorico = $em->getRepository("BiCoreBundle:Storicomodifiche"); |
|
|
|
|
173
|
7 |
|
$changes = $repoStorico->isRecordChanged($controller, $originalData, $newData); |
|
|
|
|
174
|
|
|
|
175
|
7 |
|
if ($changes) { |
176
|
2 |
|
$repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
7 |
|
$continua = (int) $request->get('continua'); |
180
|
7 |
|
if ($continua === 0) { |
181
|
7 |
|
return new Response('OK'); |
182
|
|
|
} else { |
183
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
1 |
|
return $this->render( |
188
|
1 |
|
$crudtemplate, |
189
|
|
|
array( |
190
|
1 |
|
'entity' => $entity, |
191
|
1 |
|
'edit_form' => $editForm->createView(), |
192
|
1 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
193
|
|
|
) |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Deletes a table entity. |
199
|
|
|
*/ |
200
|
8 |
|
public function delete(Request $request, $token) |
201
|
|
|
{ |
202
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
203
|
8 |
|
if (!$this->getPermessi()->canDelete()) { |
204
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per eliminare questo contenuto"); |
|
|
|
|
205
|
|
|
} |
206
|
7 |
|
$entityclass = $this->getEntityClassName(); |
207
|
|
|
|
208
|
7 |
|
$isValidToken = $this->isCsrfTokenValid($this->getController(), $token); |
|
|
|
|
209
|
|
|
|
210
|
7 |
|
if (!$isValidToken) { |
211
|
|
|
throw $this->createNotFoundException('Token non valido'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
try { |
215
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
216
|
7 |
|
$qb = $em->createQueryBuilder(); |
|
|
|
|
217
|
7 |
|
$ids = explode(',', $request->get('id')); |
218
|
7 |
|
$qb->delete($entityclass, 'u') |
219
|
7 |
|
->andWhere('u.id IN (:ids)') |
220
|
7 |
|
->setParameter('ids', $ids); |
221
|
|
|
|
222
|
7 |
|
$query = $qb->getQuery(); |
223
|
7 |
|
$query->execute(); |
224
|
2 |
|
} catch (\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException $e) { |
225
|
2 |
|
$response = new Response($e->getMessage()); |
226
|
2 |
|
$response->setStatusCode('501'); |
227
|
2 |
|
return $response; |
228
|
|
|
} catch (\Exception $e) { |
229
|
|
|
$response = new Response($e->getMessage()); |
230
|
|
|
$response->setStatusCode('200'); |
231
|
|
|
return $response; |
232
|
|
|
} |
233
|
|
|
|
234
|
5 |
|
return new Response('Operazione eseguita con successo'); |
235
|
|
|
} |
236
|
|
|
|
237
|
7 |
|
private function elencoModifiche($controller, $id) |
238
|
|
|
{ |
239
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
240
|
7 |
|
$risultato = $em->getRepository('BiCoreBundle:Storicomodifiche')->findBy( |
241
|
|
|
array( |
242
|
7 |
|
'nometabella' => $controller, |
243
|
7 |
|
'idtabella' => $id, |
244
|
|
|
), |
245
|
7 |
|
array('giorno' => 'DESC') |
246
|
|
|
); |
247
|
|
|
|
248
|
7 |
|
return $risultato; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|