|
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\Utils\Api\ApiUtils; |
|
12
|
|
|
use Cdf\BiCoreBundle\Utils\Entity\ModelUtils; |
|
13
|
|
|
|
|
14
|
|
|
trait FiApiCoreCrudControllerTrait |
|
15
|
|
|
{ |
|
16
|
|
|
use FiApiCoreCrudInlineControllerTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Displays a form to create a new table entity. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function new(Request $request) |
|
22
|
|
|
{ |
|
23
|
|
|
/* @var $em EntityManager */ |
|
24
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
25
|
|
|
$controller = $this->getController(); |
|
|
|
|
|
|
26
|
|
|
if (!$this->getPermessi()->canCreate($this->getController())) { |
|
|
|
|
|
|
27
|
|
|
throw new AccessDeniedException('Non si hanno i permessi per creare questo contenuto'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
|
|
|
|
|
31
|
|
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
$parametriform = $request->get('parametriform') ? json_decode($request->get('parametriform'), true) : []; |
|
34
|
|
|
|
|
35
|
|
|
//$entityclass = $this->getModelClassName(); |
|
36
|
|
|
$entityclass = $this->getControllerItemName(); |
|
|
|
|
|
|
37
|
|
|
$entity = new $entityclass(); |
|
38
|
|
|
|
|
39
|
|
|
//$formclass = str_replace('Entity', 'Form', $entityclass); |
|
40
|
|
|
$formclass = $this->getFormName(); |
|
|
|
|
|
|
41
|
|
|
$formType = $formclass.'Type'; |
|
42
|
|
|
|
|
43
|
|
|
$attrArray = ['attr' => [ |
|
44
|
|
|
'id' => 'formdati'.$controller, |
|
45
|
|
|
], |
|
46
|
|
|
'action' => $this->generateUrl($controller.'_new'), 'parametriform' => $parametriform, |
|
|
|
|
|
|
47
|
|
|
'extra-options' => [] |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
foreach ($this->options as $key => $option) { |
|
51
|
|
|
$attrArray['extra-options'][$key] = $option; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$form = $this->createForm( |
|
|
|
|
|
|
55
|
|
|
$formType, |
|
56
|
|
|
$entity, |
|
57
|
|
|
$attrArray |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$form->handleRequest($request); |
|
61
|
|
|
|
|
62
|
|
|
$twigparms = [ |
|
63
|
|
|
'form' => $form->createView(), |
|
64
|
|
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
|
65
|
|
|
'tabellatemplate' => $tabellatemplate, |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
if ($form->isSubmitted()) { |
|
69
|
|
|
if ($form->isValid()) { |
|
70
|
|
|
//TODO: evaluate if this part can be improved |
|
71
|
|
|
$parameters = $request->request->get($form->getName()); |
|
72
|
|
|
$entity = $form->getData(); |
|
73
|
|
|
$this->setIdObjectfromSelect($entity, $parameters); |
|
74
|
|
|
|
|
75
|
|
|
$apiClass = $this->apiController; |
|
76
|
|
|
$apiObject = new $apiClass(); |
|
77
|
|
|
$apiBook = new ApiUtils($this->collection); |
|
78
|
|
|
$createMethod = $apiBook->getCreate(); |
|
79
|
|
|
|
|
80
|
|
|
/*$response = */ |
|
81
|
|
|
$apiObject->$createMethod($entity); |
|
82
|
|
|
|
|
83
|
|
|
return new Response( |
|
84
|
|
|
$this->renderView($crudtemplate, $twigparms), |
|
|
|
|
|
|
85
|
|
|
200 |
|
86
|
|
|
); |
|
87
|
|
|
} else { |
|
88
|
|
|
//Quando non passa la validazione |
|
89
|
|
|
return new Response( |
|
90
|
|
|
$this->renderView($crudtemplate, $twigparms), |
|
91
|
|
|
400 |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} else { |
|
95
|
|
|
//Quando viene richiesta una "nuova" new |
|
96
|
|
|
return new Response( |
|
97
|
|
|
$this->renderView($crudtemplate, $twigparms), |
|
98
|
|
|
200 |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Displays a form to edit an existing table entity. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function edit(Request $request, $id) |
|
107
|
|
|
{ |
|
108
|
|
|
$bundle = $this->getBundle(); |
|
109
|
|
|
$controller = $this->getController(); |
|
110
|
|
|
|
|
111
|
|
|
if (!$this->getPermessi()->canUpdate($this->getController())) { |
|
112
|
|
|
throw new AccessDeniedException('Non si hanno i permessi per modificare questo contenuto'); |
|
113
|
|
|
} |
|
114
|
|
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
115
|
|
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
|
116
|
|
|
|
|
117
|
|
|
$formclass = $this->getFormName(); |
|
118
|
|
|
$formType = $formclass.'Type'; |
|
119
|
|
|
|
|
120
|
|
|
$apiClass = $this->apiController; |
|
121
|
|
|
$apiObject = new $apiClass(); |
|
122
|
|
|
$apiBook = new ApiUtils($this->collection); |
|
123
|
|
|
$getMethod = $apiBook->getItem(); |
|
124
|
|
|
|
|
125
|
|
|
//TODO: response belongs to last operation |
|
126
|
|
|
$entityorig = $apiObject->$getMethod($id); |
|
127
|
|
|
|
|
128
|
|
|
$elencomodifiche = $this->elencoModifiche($controller, $id); |
|
129
|
|
|
|
|
130
|
|
|
$modelutils = new ModelUtils(); |
|
131
|
|
|
$entity = $modelutils->setApiValues($entityorig); |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
$attrArray = ['attr' => [ |
|
135
|
|
|
'id' => 'formdati'.$controller, |
|
136
|
|
|
], |
|
137
|
|
|
'action' => $this->generateUrl($controller.'_update', ['id' => $entity->getId()]), |
|
138
|
|
|
'extra-options' => [] |
|
139
|
|
|
]; |
|
140
|
|
|
foreach ($this->options as $key => $option) { |
|
141
|
|
|
$attrArray['extra-options'][$key] = $option; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$editForm = $this->createForm( |
|
145
|
|
|
$formType, |
|
146
|
|
|
$entity, |
|
147
|
|
|
$attrArray |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
|
|
return $this->render( |
|
|
|
|
|
|
151
|
|
|
$crudtemplate, |
|
152
|
|
|
[ |
|
153
|
|
|
'entity' => $entity, |
|
154
|
|
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
|
155
|
|
|
'tabellatemplate' => $tabellatemplate, |
|
156
|
|
|
'edit_form' => $editForm->createView(), |
|
157
|
|
|
'elencomodifiche' => $elencomodifiche, |
|
158
|
|
|
] |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Update value of _id field with value selected on select list. |
|
164
|
|
|
* //TODO: review duplicated code |
|
165
|
|
|
*/ |
|
166
|
|
|
private function setIdfromSelect(&$parameters) |
|
167
|
|
|
{ |
|
168
|
|
|
foreach (array_keys($parameters) as $key) { |
|
169
|
|
|
if (\str_contains($key, '_id')) { |
|
170
|
|
|
$sourceKey = substr($key, 0, strpos($key, '_id')); |
|
171
|
|
|
if (isset($parameters[$sourceKey])) { |
|
172
|
|
|
$parameters[$key] = $parameters[$sourceKey]; |
|
173
|
|
|
} |
|
174
|
|
|
} elseif (\str_contains($key, '_enum')) { |
|
175
|
|
|
$sourceKey = substr($key, 0, strpos($key, '_enum')); |
|
176
|
|
|
if (isset($parameters[$sourceKey])) { |
|
177
|
|
|
$parameters[$key] = $parameters[$sourceKey]; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Update value of _id fields of an object with value selected on select list. |
|
185
|
|
|
* It forces the received field to be an INT (It applies a cast) |
|
186
|
|
|
*/ |
|
187
|
|
|
private function setIdObjectfromSelect(&$classItem, &$parameters) |
|
188
|
|
|
{ |
|
189
|
|
|
//TODO: (int) cast that is fixed |
|
190
|
|
|
$setters = $classItem::setters(); |
|
191
|
|
|
foreach (array_keys($parameters) as $key) { |
|
192
|
|
|
if (\str_contains($key, '_id')) { |
|
193
|
|
|
$setMethod = $setters[$key]; |
|
194
|
|
|
$sourceKey = substr($key, 0, strpos($key, '_id')); |
|
195
|
|
|
$classItem->$setMethod((int)$parameters[$sourceKey]); |
|
196
|
|
|
} elseif (\str_contains($key, '_enum')) { |
|
197
|
|
|
$setMethod = $setters[$key]; |
|
198
|
|
|
$sourceKey = substr($key, 0, strpos($key, '_enum')); |
|
199
|
|
|
$classItem->$setMethod((int)$parameters[$sourceKey]); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Edits an existing table entity. |
|
206
|
|
|
*/ |
|
207
|
|
|
public function update(Request $request, $id) |
|
208
|
|
|
{ |
|
209
|
|
|
$bundle = $this->getBundle(); |
|
210
|
|
|
$controller = $this->getController(); |
|
211
|
|
|
if (!$this->getPermessi()->canUpdate($this->getController())) { |
|
212
|
|
|
throw new AccessDeniedException('Non si hanno i permessi per modificare questo contenuto'); |
|
213
|
|
|
} |
|
214
|
|
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, 'edit'); |
|
215
|
|
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
|
216
|
|
|
$elencomodifiche = $this->elencoModifiche($controller, $id); |
|
217
|
|
|
|
|
218
|
|
|
$formclass = $this->getFormName(); |
|
219
|
|
|
$formType = $formclass.'Type'; |
|
220
|
|
|
|
|
221
|
|
|
$apiClass = $this->apiController; |
|
222
|
|
|
$apiObject = new $apiClass(); |
|
223
|
|
|
$apiBook = new ApiUtils($this->collection); |
|
224
|
|
|
$getMethod = $apiBook->getItem(); |
|
225
|
|
|
|
|
226
|
|
|
//TODO: response belongs to last operation |
|
227
|
|
|
$entityorig = $apiObject->$getMethod($id); |
|
228
|
|
|
|
|
229
|
|
|
$modelutils = new ModelUtils(); |
|
230
|
|
|
$entity = $modelutils->setApiValues($entityorig); |
|
231
|
|
|
|
|
232
|
|
|
$attrArray = ['attr' => [ |
|
233
|
|
|
'id' => 'formdati'.$controller, |
|
234
|
|
|
], |
|
235
|
|
|
'action' => $this->generateUrl($controller.'_update', ['id' => $entity->getId()]), |
|
236
|
|
|
'extra-options' => [] |
|
237
|
|
|
]; |
|
238
|
|
|
|
|
239
|
|
|
foreach ($this->options as $key => $option) { |
|
240
|
|
|
$attrArray['extra-options'][$key] = $option; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
$editForm = $this->createForm( |
|
244
|
|
|
$formType, |
|
245
|
|
|
$entity, |
|
246
|
|
|
$attrArray |
|
247
|
|
|
); |
|
248
|
|
|
|
|
249
|
|
|
$parameters = $request->request->get($editForm->getName()); |
|
250
|
|
|
|
|
251
|
|
|
$this->setIdfromSelect($parameters); |
|
252
|
|
|
$editForm->submit($parameters); |
|
253
|
|
|
|
|
254
|
|
|
if ($editForm->isValid()) { |
|
255
|
|
|
$entityItem = $editForm->getData(); |
|
256
|
|
|
|
|
257
|
|
|
//$entityItem = $modelutils->getControllerItem($modelEntity , $this->getControllerItemName()); |
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
$apiClass = $this->apiController; |
|
261
|
|
|
$apiObject = new $apiClass(); |
|
262
|
|
|
$apiBook = new ApiUtils($this->collection); |
|
263
|
|
|
$updateMethod = $apiBook->getUpdateItem(); |
|
264
|
|
|
|
|
265
|
|
|
/*$responseMessage = */ |
|
266
|
|
|
$apiObject->$updateMethod($entityItem, $id); |
|
267
|
|
|
|
|
268
|
|
|
$continua = (int) $request->get('continua'); |
|
269
|
|
|
if (0 === $continua) { |
|
270
|
|
|
return new Response('OK'); |
|
271
|
|
|
} else { |
|
272
|
|
|
return $this->redirect($this->generateUrl($controller.'_edit', ['id' => $id])); |
|
|
|
|
|
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return new Response($this->renderView( |
|
277
|
|
|
$crudtemplate, |
|
278
|
|
|
[ |
|
279
|
|
|
'entity' => $entity, |
|
280
|
|
|
'edit_form' => $editForm->createView(), |
|
281
|
|
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
|
282
|
|
|
'tabellatemplate' => $tabellatemplate, |
|
283
|
|
|
'elencomodifiche' => $elencomodifiche, |
|
284
|
|
|
] |
|
285
|
|
|
), 400); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Deletes a table entity. |
|
290
|
|
|
*/ |
|
291
|
|
|
public function delete(Request $request, $token) |
|
292
|
|
|
{ |
|
293
|
|
|
/* @var $em EntityManager */ |
|
294
|
|
|
if (!$this->getPermessi()->canDelete($this->getController())) { |
|
295
|
|
|
throw new AccessDeniedException('Non si hanno i permessi per eliminare questo contenuto'); |
|
296
|
|
|
} |
|
297
|
|
|
//$entityclass = $this->getEntityClassName(); |
|
298
|
|
|
|
|
299
|
|
|
$isValidToken = $this->isCsrfTokenValid($this->getController(), $token); |
|
|
|
|
|
|
300
|
|
|
|
|
301
|
|
|
if (!$isValidToken) { |
|
302
|
|
|
throw $this->createNotFoundException('Token non valido'); |
|
|
|
|
|
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
try { |
|
306
|
|
|
$ids = explode(',', $request->get('id')); |
|
307
|
|
|
|
|
308
|
|
|
$apiClass = $this->apiController; |
|
309
|
|
|
$apiObject = new $apiClass(); |
|
310
|
|
|
$apiBook = new ApiUtils($this->collection); |
|
311
|
|
|
$deleteMethod = $apiBook->getDelete(); |
|
312
|
|
|
|
|
313
|
|
|
foreach ($ids as $id) { |
|
314
|
|
|
//TODO: response belongs to last operation |
|
315
|
|
|
$response = $apiObject->$deleteMethod($id); |
|
|
|
|
|
|
316
|
|
|
} |
|
317
|
|
|
} catch (\Exception $e) { |
|
318
|
|
|
$response = new Response($e->getMessage()); |
|
319
|
|
|
$response->setStatusCode('200'); |
|
320
|
|
|
|
|
321
|
|
|
return $response; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
return new Response('Operazione eseguita con successo'); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
public function elencoModifiche($controller, $id) |
|
328
|
|
|
{ |
|
329
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
|
|
330
|
|
|
$risultato = $em->getRepository('BiCoreBundle:Storicomodifiche')->findBy( |
|
331
|
|
|
[ |
|
332
|
|
|
'nometabella' => $controller, |
|
333
|
|
|
'idtabella' => $id, |
|
334
|
|
|
], |
|
335
|
|
|
['giorno' => 'DESC'] |
|
336
|
|
|
); |
|
337
|
|
|
|
|
338
|
|
|
return $risultato; |
|
339
|
|
|
} |
|
340
|
|
|
} |
|
341
|
|
|
|