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\Entity\EntityUtils; |
9
|
|
|
use Cdf\BiCoreBundle\Utils\Tabella\ParametriTabella; |
10
|
|
|
|
11
|
|
|
trait FiCoreCrudControllerTrait |
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
|
|
|
* Displays a form to edit an existing table entity. |
77
|
|
|
*/ |
78
|
8 |
|
public function edit(Request $request, $id) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
81
|
8 |
|
$bundle = $this->getBundle(); |
|
|
|
|
82
|
8 |
|
$controller = $this->getController(); |
83
|
|
|
|
84
|
8 |
|
if (!$this->getPermessi()->canUpdate()) { |
85
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
86
|
|
|
} |
87
|
7 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
|
|
|
88
|
7 |
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
89
|
|
|
|
90
|
7 |
|
$entityclass = $this->getEntityClassName(); |
91
|
7 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
92
|
|
|
|
93
|
7 |
|
$formType = $formclass . 'Type'; |
94
|
|
|
|
95
|
7 |
|
$elencomodifiche = $this->elencoModifiche($controller, $id); |
96
|
|
|
|
97
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
98
|
|
|
|
99
|
7 |
|
$entity = $em->getRepository($entityclass)->find($id); |
100
|
|
|
|
101
|
7 |
|
if (!$entity) { |
102
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' del record con id ' . $id . '.'); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
7 |
|
$editForm = $this->createForm( |
106
|
7 |
|
$formType, |
107
|
7 |
|
$entity, |
108
|
|
|
array('attr' => array( |
109
|
7 |
|
'id' => 'formdati' . $controller, |
110
|
|
|
), |
111
|
7 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
112
|
|
|
) |
113
|
|
|
); |
114
|
|
|
|
115
|
7 |
|
return $this->render( |
|
|
|
|
116
|
7 |
|
$crudtemplate, |
117
|
|
|
array( |
118
|
7 |
|
'entity' => $entity, |
119
|
7 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
120
|
7 |
|
'tabellatemplate' => $tabellatemplate, |
121
|
7 |
|
'edit_form' => $editForm->createView(), |
122
|
7 |
|
'elencomodifiche' => $elencomodifiche, |
123
|
|
|
) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
/** |
127
|
|
|
* Edits an existing table entity. |
128
|
|
|
*/ |
129
|
9 |
|
public function update(Request $request, $id) |
130
|
|
|
{ |
131
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
132
|
9 |
|
$bundle = $this->getBundle(); |
|
|
|
|
133
|
9 |
|
$controller = $this->getController(); |
134
|
9 |
|
if (!$this->getPermessi()->canUpdate()) { |
135
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
136
|
|
|
} |
137
|
8 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, "edit"); |
|
|
|
|
138
|
|
|
|
139
|
8 |
|
$entityclass = $this->getEntityClassName(); |
140
|
8 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
141
|
8 |
|
$formType = $formclass . 'Type'; |
|
|
|
|
142
|
|
|
|
143
|
8 |
|
$em = $this->getDoctrine()->getManager(); |
144
|
|
|
|
145
|
8 |
|
$entity = $em->getRepository($entityclass)->find($id); |
146
|
|
|
|
147
|
8 |
|
if (!$entity) { |
148
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' per il record con id ' . $id); |
149
|
|
|
} |
150
|
|
|
|
151
|
7 |
|
$editForm = $this->createForm( |
152
|
7 |
|
$formType, |
153
|
7 |
|
$entity, |
154
|
|
|
array('attr' => array( |
155
|
7 |
|
'id' => 'formdati' . $controller, |
156
|
|
|
), |
157
|
7 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
158
|
|
|
) |
159
|
|
|
); |
160
|
|
|
|
161
|
7 |
|
$editForm->submit($request->request->get($editForm->getName())); |
162
|
|
|
|
163
|
7 |
|
if ($editForm->isValid()) { |
164
|
7 |
|
$originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
165
|
|
|
|
166
|
7 |
|
$em->persist($entity); |
167
|
7 |
|
$em->flush(); |
168
|
|
|
|
169
|
7 |
|
$newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
|
|
|
|
170
|
7 |
|
$repoStorico = $em->getRepository("BiCoreBundle:Storicomodifiche"); |
|
|
|
|
171
|
7 |
|
$changes = $repoStorico->isRecordChanged($controller, $originalData, $newData); |
|
|
|
|
172
|
|
|
|
173
|
7 |
|
if ($changes) { |
174
|
2 |
|
$repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
7 |
|
$continua = (int) $request->get('continua'); |
178
|
7 |
|
if ($continua === 0) { |
179
|
7 |
|
return new Response('OK'); |
180
|
|
|
} else { |
181
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
return $this->render( |
186
|
1 |
|
$crudtemplate, |
187
|
|
|
array( |
188
|
1 |
|
'entity' => $entity, |
189
|
1 |
|
'edit_form' => $editForm->createView(), |
190
|
1 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
191
|
|
|
) |
192
|
|
|
); |
193
|
|
|
} |
194
|
2 |
|
private function checkAggiornaRight($id) |
195
|
|
|
{ |
196
|
2 |
|
if ($id === 0) { |
197
|
|
|
if (!$this->getPermessi()->canCreate()) { |
198
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
} else { |
201
|
2 |
|
if (!$this->getPermessi()->canUpdate()) { |
202
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
} |
205
|
1 |
|
} |
206
|
|
|
/** |
207
|
|
|
* Inline existing table entity. |
208
|
|
|
*/ |
209
|
2 |
|
public function aggiorna(Request $request, $id, $token) |
210
|
|
|
{ |
211
|
2 |
|
$this->checkAggiornaRight($id); |
212
|
|
|
|
213
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
214
|
1 |
|
$controller = $this->getController(); |
|
|
|
|
215
|
1 |
|
$entityclass = $this->getEntityClassName(); |
216
|
|
|
|
217
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
218
|
1 |
|
$queryBuilder = $em->createQueryBuilder(); |
219
|
1 |
|
$insert = ($id === 0); |
|
|
|
|
220
|
|
|
|
221
|
1 |
|
if ($insert) { |
222
|
|
|
//Insert |
223
|
|
|
$entity = new $entityclass(); |
224
|
|
|
$queryBuilder |
225
|
|
|
->insert($entityclass) |
226
|
|
|
; |
227
|
|
|
} else { |
228
|
|
|
//Update |
229
|
1 |
|
$entity = $em->getRepository($entityclass)->find($id); |
230
|
1 |
|
if (!$entity) { |
231
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' per il record con id ' . $id); |
232
|
|
|
} |
233
|
|
|
$queryBuilder |
234
|
1 |
|
->update($entityclass, "u") |
|
|
|
|
235
|
1 |
|
->where("u.id = :id") |
|
|
|
|
236
|
1 |
|
->setParameter("id", $id); |
|
|
|
|
237
|
|
|
} |
238
|
1 |
|
$values = $request->get("values"); |
|
|
|
|
239
|
1 |
|
$isValidToken = $this->isCsrfTokenValid($id, $token); |
|
|
|
|
240
|
|
|
|
241
|
1 |
|
if (!$isValidToken) { |
242
|
1 |
|
throw $this->createNotFoundException('Token non valido'); |
243
|
|
|
} |
244
|
|
|
|
245
|
1 |
|
$querydaeseguire = false; |
246
|
|
|
|
247
|
1 |
|
foreach ($values as $value) { |
248
|
1 |
|
$fieldpieces = explode(".", $value["fieldname"]); |
|
|
|
|
249
|
1 |
|
$table = $fieldpieces[0]; |
|
|
|
|
250
|
|
|
//Si prende in considerazione solo i campi strettamente legati a questa entity |
251
|
1 |
|
if ($table == $controller && count($fieldpieces) == 2 && $value["fieldtype"] != 'join') { |
|
|
|
|
252
|
1 |
|
$field = $fieldpieces[1]; |
253
|
1 |
|
if ($insert) { |
254
|
|
|
$queryBuilder->setValue($field, ':' . $field); |
255
|
|
|
$queryBuilder->setParameter($field, $value["fieldvalue"]); |
|
|
|
|
256
|
|
|
$querydaeseguire = true; |
257
|
|
|
} else { |
258
|
1 |
|
$entityutils = new EntityUtils($em); |
|
|
|
|
259
|
1 |
|
$property = $entityutils->getEntityProperties($field, $entity); |
|
|
|
|
260
|
1 |
|
$nomefunzioneget = $property["get"]; |
|
|
|
|
261
|
1 |
|
if ($nomefunzioneget != $value["fieldvalue"]) { |
|
|
|
|
262
|
1 |
|
$querydaeseguire = true; |
263
|
1 |
|
$queryBuilder->set("u." . $field, ':' . $field); |
|
|
|
|
264
|
1 |
|
$queryBuilder->setParameter($field, $value["fieldvalue"]); |
|
|
|
|
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} else { |
268
|
1 |
|
continue; |
269
|
|
|
} |
270
|
|
|
} |
271
|
1 |
|
if ($querydaeseguire) { |
272
|
1 |
|
$queryBuilder->getQuery()->execute(); |
273
|
|
|
} |
274
|
|
|
|
275
|
1 |
|
return new \Symfony\Component\HttpFoundation\JsonResponse(array("errcode" => 0, "message" => "Registrazione eseguita")); |
|
|
|
|
276
|
|
|
} |
277
|
|
|
/** |
278
|
|
|
* Deletes a table entity. |
279
|
|
|
*/ |
280
|
8 |
|
public function delete(Request $request, $token) |
281
|
|
|
{ |
282
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
283
|
8 |
|
if (!$this->getPermessi()->canDelete()) { |
284
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per eliminare questo contenuto"); |
|
|
|
|
285
|
|
|
} |
286
|
7 |
|
$entityclass = $this->getEntityClassName(); |
287
|
|
|
|
288
|
7 |
|
$isValidToken = $this->isCsrfTokenValid($this->getController(), $token); |
289
|
|
|
|
290
|
7 |
|
if (!$isValidToken) { |
291
|
|
|
throw $this->createNotFoundException('Token non valido'); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
try { |
295
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
296
|
7 |
|
$qb = $em->createQueryBuilder(); |
|
|
|
|
297
|
7 |
|
$ids = explode(',', $request->get('id')); |
298
|
7 |
|
$qb->delete($entityclass, 'u') |
299
|
7 |
|
->andWhere('u.id IN (:ids)') |
300
|
7 |
|
->setParameter('ids', $ids); |
301
|
|
|
|
302
|
7 |
|
$query = $qb->getQuery(); |
303
|
7 |
|
$query->execute(); |
304
|
2 |
|
} catch (\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException $e) { |
305
|
2 |
|
$response = new Response($e->getMessage()); |
306
|
2 |
|
$response->setStatusCode('501'); |
307
|
2 |
|
return $response; |
308
|
|
|
} catch (\Exception $e) { |
309
|
|
|
$response = new Response($e->getMessage()); |
310
|
|
|
$response->setStatusCode('200'); |
311
|
|
|
return $response; |
312
|
|
|
} |
313
|
|
|
|
314
|
5 |
|
return new Response('Operazione eseguita con successo'); |
315
|
|
|
} |
316
|
7 |
|
private function elencoModifiche($controller, $id) |
317
|
|
|
{ |
318
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
319
|
7 |
|
$risultato = $em->getRepository('BiCoreBundle:Storicomodifiche')->findBy( |
320
|
|
|
array( |
321
|
7 |
|
'nometabella' => $controller, |
322
|
7 |
|
'idtabella' => $id, |
323
|
|
|
), |
324
|
7 |
|
array('giorno' => 'DESC') |
325
|
|
|
); |
326
|
|
|
|
327
|
7 |
|
return $risultato; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|