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