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
|
|
|
/** |
15
|
|
|
* Displays a form to create a new table entity. |
16
|
|
|
*/ |
17
|
7 |
|
public function new(Request $request) |
18
|
|
|
{ |
19
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
20
|
7 |
|
$bundle = $this->getBundle(); |
|
|
|
|
21
|
7 |
|
$controller = $this->getController(); |
|
|
|
|
22
|
7 |
|
if (!$this->getPermessi()->canCreate()) { |
|
|
|
|
23
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
6 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
|
|
|
27
|
6 |
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
|
|
|
|
28
|
|
|
|
29
|
6 |
|
$parametriform = $request->get("parametriform") ? json_decode($request->get("parametriform"), true) : array(); |
|
|
|
|
30
|
|
|
|
31
|
6 |
|
$entityclass = $this->getEntityClassName(); |
|
|
|
|
32
|
6 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
33
|
|
|
|
34
|
6 |
|
$entity = new $entityclass(); |
|
|
|
|
35
|
6 |
|
$formType = $formclass . 'Type'; |
36
|
6 |
|
$form = $this->createForm($formType, $entity, array('attr' => array( |
|
|
|
|
37
|
6 |
|
'id' => 'formdati' . $controller, |
38
|
|
|
), |
39
|
6 |
|
'action' => $this->generateUrl($controller . '_new'), "parametriform" => $parametriform |
|
|
|
|
40
|
|
|
)); |
41
|
|
|
|
42
|
6 |
|
$form->handleRequest($request); |
43
|
|
|
|
44
|
|
|
$twigparms = array( |
45
|
6 |
|
'form' => $form->createView(), |
46
|
6 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
47
|
6 |
|
'tabellatemplate' => $tabellatemplate |
48
|
|
|
); |
49
|
|
|
|
50
|
6 |
|
if ($form->isSubmitted()) { |
51
|
6 |
|
if ($form->isValid()) { |
52
|
5 |
|
$entity = $form->getData(); |
53
|
|
|
|
54
|
5 |
|
$entityManager = $this->getDoctrine()->getManager(); |
|
|
|
|
55
|
5 |
|
$entityManager->persist($entity); |
56
|
5 |
|
$entityManager->flush(); |
57
|
5 |
|
return new Response( |
58
|
5 |
|
$this->renderView($crudtemplate, $twigparms), |
|
|
|
|
59
|
5 |
|
200 |
60
|
|
|
); |
61
|
|
|
} else { |
62
|
|
|
//Quando non passa la validazione |
63
|
1 |
|
return new Response( |
64
|
1 |
|
$this->renderView($crudtemplate, $twigparms), |
65
|
1 |
|
400 |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
|
|
//Quando viene richiesta una "nuova" new |
70
|
6 |
|
return new Response( |
71
|
6 |
|
$this->renderView($crudtemplate, $twigparms), |
72
|
6 |
|
200 |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Displays a form to edit an existing table entity. |
79
|
|
|
*/ |
80
|
8 |
|
public function edit(Request $request, $id) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
83
|
8 |
|
$bundle = $this->getBundle(); |
|
|
|
|
84
|
8 |
|
$controller = $this->getController(); |
85
|
|
|
|
86
|
8 |
|
if (!$this->getPermessi()->canUpdate()) { |
87
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
88
|
|
|
} |
89
|
7 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
|
|
|
90
|
7 |
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
91
|
|
|
|
92
|
7 |
|
$entityclass = $this->getEntityClassName(); |
93
|
7 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
94
|
|
|
|
95
|
7 |
|
$formType = $formclass . 'Type'; |
96
|
|
|
|
97
|
7 |
|
$elencomodifiche = $this->elencoModifiche($controller, $id); |
98
|
|
|
|
99
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
100
|
|
|
|
101
|
7 |
|
$entity = $em->getRepository($entityclass)->find($id); |
102
|
|
|
|
103
|
7 |
|
if (!$entity) { |
104
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' del record con id ' . $id . '.'); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
7 |
|
$editForm = $this->createForm( |
108
|
7 |
|
$formType, |
109
|
7 |
|
$entity, |
110
|
|
|
array('attr' => array( |
111
|
7 |
|
'id' => 'formdati' . $controller, |
112
|
|
|
), |
113
|
7 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
|
117
|
7 |
|
return $this->render( |
|
|
|
|
118
|
7 |
|
$crudtemplate, |
119
|
|
|
array( |
120
|
7 |
|
'entity' => $entity, |
121
|
7 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
122
|
7 |
|
'tabellatemplate' => $tabellatemplate, |
123
|
7 |
|
'edit_form' => $editForm->createView(), |
124
|
7 |
|
'elencomodifiche' => $elencomodifiche, |
125
|
|
|
) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Edits an existing table entity. |
131
|
|
|
*/ |
132
|
9 |
|
public function update(Request $request, $id) |
133
|
|
|
{ |
134
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
135
|
9 |
|
$bundle = $this->getBundle(); |
|
|
|
|
136
|
9 |
|
$controller = $this->getController(); |
137
|
9 |
|
if (!$this->getPermessi()->canUpdate()) { |
138
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
139
|
|
|
} |
140
|
8 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, "edit"); |
|
|
|
|
141
|
|
|
|
142
|
8 |
|
$entityclass = $this->getEntityClassName(); |
143
|
8 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
144
|
8 |
|
$formType = $formclass . 'Type'; |
|
|
|
|
145
|
|
|
|
146
|
8 |
|
$em = $this->getDoctrine()->getManager(); |
147
|
|
|
|
148
|
8 |
|
$entity = $em->getRepository($entityclass)->find($id); |
149
|
|
|
|
150
|
8 |
|
if (!$entity) { |
151
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' per il record con id ' . $id); |
152
|
|
|
} |
153
|
|
|
|
154
|
7 |
|
$editForm = $this->createForm( |
155
|
7 |
|
$formType, |
156
|
7 |
|
$entity, |
157
|
|
|
array('attr' => array( |
158
|
7 |
|
'id' => 'formdati' . $controller, |
159
|
|
|
), |
160
|
7 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
161
|
|
|
) |
162
|
|
|
); |
163
|
|
|
|
164
|
7 |
|
$editForm->submit($request->request->get($editForm->getName())); |
165
|
|
|
|
166
|
7 |
|
if ($editForm->isValid()) { |
167
|
7 |
|
$originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
168
|
|
|
|
169
|
7 |
|
$em->persist($entity); |
170
|
7 |
|
$em->flush(); |
171
|
|
|
|
172
|
7 |
|
$newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
|
|
|
|
173
|
7 |
|
$repoStorico = $em->getRepository("BiCoreBundle:Storicomodifiche"); |
|
|
|
|
174
|
7 |
|
$changes = $repoStorico->isRecordChanged($controller, $originalData, $newData); |
|
|
|
|
175
|
|
|
|
176
|
7 |
|
if ($changes) { |
177
|
2 |
|
$repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
7 |
|
$continua = (int) $request->get('continua'); |
181
|
7 |
|
if ($continua === 0) { |
182
|
7 |
|
return new Response('OK'); |
183
|
|
|
} else { |
184
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
return $this->render( |
189
|
1 |
|
$crudtemplate, |
190
|
|
|
array( |
191
|
1 |
|
'entity' => $entity, |
192
|
1 |
|
'edit_form' => $editForm->createView(), |
193
|
1 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
194
|
|
|
) |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
2 |
|
private function checkAggiornaRight($id) |
199
|
|
|
{ |
200
|
2 |
|
if ($id === 0) { |
201
|
|
|
if (!$this->getPermessi()->canCreate()) { |
202
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
} else { |
205
|
2 |
|
if (!$this->getPermessi()->canUpdate()) { |
206
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
} |
209
|
1 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Inline existing table entity. |
213
|
|
|
*/ |
214
|
2 |
|
public function aggiorna(Request $request, $id, $token) |
215
|
|
|
{ |
216
|
2 |
|
$this->checkAggiornaRight($id); |
217
|
|
|
|
218
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
219
|
1 |
|
$controller = $this->getController(); |
|
|
|
|
220
|
1 |
|
$entityclass = $this->getEntityClassName(); |
221
|
|
|
|
222
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
223
|
1 |
|
$queryBuilder = $em->createQueryBuilder(); |
224
|
1 |
|
$insert = ($id === 0); |
|
|
|
|
225
|
|
|
|
226
|
1 |
|
if ($insert) { |
227
|
|
|
//Insert |
228
|
|
|
$entity = new $entityclass(); |
229
|
|
|
$queryBuilder |
230
|
|
|
->insert($entityclass) |
231
|
|
|
; |
232
|
|
|
} else { |
233
|
|
|
//Update |
234
|
1 |
|
$entity = $em->getRepository($entityclass)->find($id); |
235
|
1 |
|
if (!$entity) { |
236
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' per il record con id ' . $id); |
237
|
|
|
} |
238
|
|
|
$queryBuilder |
239
|
1 |
|
->update($entityclass, "u") |
|
|
|
|
240
|
1 |
|
->where("u.id = :id") |
|
|
|
|
241
|
1 |
|
->setParameter("id", $id); |
|
|
|
|
242
|
|
|
} |
243
|
1 |
|
$values = $request->get("values"); |
|
|
|
|
244
|
1 |
|
$isValidToken = $this->isCsrfTokenValid($id, $token); |
|
|
|
|
245
|
|
|
|
246
|
1 |
|
if (!$isValidToken) { |
247
|
1 |
|
throw $this->createNotFoundException('Token non valido'); |
248
|
|
|
} |
249
|
|
|
|
250
|
1 |
|
$querydaeseguire = false; |
251
|
|
|
|
252
|
1 |
|
foreach ($values as $value) { |
253
|
1 |
|
$fieldpieces = explode(".", $value["fieldname"]); |
|
|
|
|
254
|
1 |
|
$table = $fieldpieces[0]; |
|
|
|
|
255
|
|
|
//Si prende in considerazione solo i campi strettamente legati a questa entity |
256
|
1 |
|
if ($table == $controller && count($fieldpieces) == 2) { |
257
|
1 |
|
$field = $fieldpieces[1]; |
258
|
1 |
|
if ($value["fieldtype"] == 'join') { |
|
|
|
|
259
|
|
|
$field = lcfirst($field . "_id"); |
|
|
|
|
260
|
|
|
} |
261
|
1 |
|
if ($insert) { |
262
|
|
|
$queryBuilder->setValue($field, ':' . $field); |
263
|
|
|
$fieldvalue = $this->getValueAggiorna($value); |
264
|
|
|
$queryBuilder->setParameter($field, $fieldvalue); |
265
|
|
|
$querydaeseguire = true; |
266
|
|
|
} else { |
267
|
1 |
|
$entityutils = new EntityUtils($em); |
|
|
|
|
268
|
1 |
|
$property = $entityutils->getEntityProperties($field, $entity); |
|
|
|
|
269
|
1 |
|
$nomefunzioneget = $property["get"]; |
|
|
|
|
270
|
1 |
|
if ($nomefunzioneget != $value["fieldvalue"]) { |
|
|
|
|
271
|
1 |
|
$querydaeseguire = true; |
272
|
1 |
|
$fieldvalue = $this->getValueAggiorna($value); |
|
|
|
|
273
|
1 |
|
$queryBuilder->set("u." . $field, ':' . $field); |
|
|
|
|
274
|
1 |
|
$queryBuilder->setParameter($field, $fieldvalue); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
} else { |
278
|
1 |
|
continue; |
279
|
|
|
} |
280
|
|
|
} |
281
|
1 |
|
if ($querydaeseguire) { |
282
|
1 |
|
$queryBuilder->getQuery()->execute(); |
283
|
|
|
} |
284
|
|
|
|
285
|
1 |
|
return new \Symfony\Component\HttpFoundation\JsonResponse(array("errcode" => 0, "message" => "Registrazione eseguita")); |
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
1 |
|
private function getValueAggiorna($field) |
289
|
|
|
{ |
290
|
1 |
|
$fieldvalue = $field["fieldvalue"]; |
|
|
|
|
291
|
1 |
|
$fieldtype = $field["fieldtype"]; |
|
|
|
|
292
|
1 |
|
if ($fieldtype == "date") { |
|
|
|
|
293
|
|
|
$fieldvalue = \DateTime::createFromFormat("d/m/Y", $field["fieldvalue"]); |
|
|
|
|
294
|
|
|
} |
295
|
1 |
|
if ($fieldtype == "datetime") { |
|
|
|
|
296
|
|
|
$fieldvalue = \DateTime::createFromFormat("d/m/Y H:i", $field["fieldvalue"]); |
|
|
|
|
297
|
|
|
} |
298
|
1 |
|
return $fieldvalue; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Deletes a table entity. |
303
|
|
|
*/ |
304
|
8 |
|
public function delete(Request $request, $token) |
305
|
|
|
{ |
306
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
307
|
8 |
|
if (!$this->getPermessi()->canDelete()) { |
308
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per eliminare questo contenuto"); |
|
|
|
|
309
|
|
|
} |
310
|
7 |
|
$entityclass = $this->getEntityClassName(); |
311
|
|
|
|
312
|
7 |
|
$isValidToken = $this->isCsrfTokenValid($this->getController(), $token); |
313
|
|
|
|
314
|
7 |
|
if (!$isValidToken) { |
315
|
|
|
throw $this->createNotFoundException('Token non valido'); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
try { |
319
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
320
|
7 |
|
$qb = $em->createQueryBuilder(); |
|
|
|
|
321
|
7 |
|
$ids = explode(',', $request->get('id')); |
322
|
7 |
|
$qb->delete($entityclass, 'u') |
323
|
7 |
|
->andWhere('u.id IN (:ids)') |
324
|
7 |
|
->setParameter('ids', $ids); |
325
|
|
|
|
326
|
7 |
|
$query = $qb->getQuery(); |
327
|
7 |
|
$query->execute(); |
328
|
2 |
|
} catch (\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException $e) { |
329
|
2 |
|
$response = new Response($e->getMessage()); |
330
|
2 |
|
$response->setStatusCode('501'); |
331
|
2 |
|
return $response; |
332
|
|
|
} catch (\Exception $e) { |
333
|
|
|
$response = new Response($e->getMessage()); |
334
|
|
|
$response->setStatusCode('200'); |
335
|
|
|
return $response; |
336
|
|
|
} |
337
|
|
|
|
338
|
5 |
|
return new Response('Operazione eseguita con successo'); |
339
|
|
|
} |
340
|
|
|
|
341
|
7 |
|
private function elencoModifiche($controller, $id) |
342
|
|
|
{ |
343
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
344
|
7 |
|
$risultato = $em->getRepository('BiCoreBundle:Storicomodifiche')->findBy( |
345
|
|
|
array( |
346
|
7 |
|
'nometabella' => $controller, |
347
|
7 |
|
'idtabella' => $id, |
348
|
|
|
), |
349
|
7 |
|
array('giorno' => 'DESC') |
350
|
|
|
); |
351
|
|
|
|
352
|
7 |
|
return $risultato; |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|