|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
8
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
9
|
|
|
use Cdf\BiCoreBundle\Utils\Entity\Finder; |
|
10
|
|
|
use Cdf\BiCoreBundle\Utils\Entity\EntityUtils; |
|
11
|
|
|
use Cdf\BiCoreBundle\Utils\Tabella\ParametriTabella; |
|
12
|
|
|
use Symfony\Component\Asset\Packages; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @property \Symfony\Component\Security\Core\Security $user |
|
16
|
|
|
*/ |
|
17
|
|
|
class FiCrudController extends AbstractController |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
protected $bundle; |
|
21
|
|
|
protected $controller; |
|
22
|
|
|
protected $permessi; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Displays a form to create a new table entity. |
|
26
|
|
|
*/ |
|
27
|
7 |
|
public function new(Request $request) |
|
28
|
|
|
{ |
|
29
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
30
|
7 |
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
31
|
7 |
|
$controller = $this->getController(); |
|
32
|
7 |
|
if (!$this->getPermessi()->canCreate()) { |
|
33
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
6 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
|
|
|
|
|
37
|
6 |
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
|
38
|
|
|
|
|
39
|
6 |
|
$parametriform = $request->get("parametriform") ? json_decode($request->get("parametriform"), true) : array(); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
6 |
|
$entityclass = $this->getEntityClassName(); |
|
42
|
6 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
6 |
|
$entity = new $entityclass(); |
|
|
|
|
|
|
45
|
6 |
|
$formType = $formclass . 'Type'; |
|
46
|
6 |
|
$form = $this->createForm($formType, $entity, array('attr' => array( |
|
|
|
|
|
|
47
|
6 |
|
'id' => 'formdati' . $controller, |
|
48
|
|
|
), |
|
49
|
6 |
|
'action' => $this->generateUrl($controller . '_new'), "parametriform" => $parametriform |
|
|
|
|
|
|
50
|
|
|
)); |
|
51
|
|
|
|
|
52
|
6 |
|
$form->handleRequest($request); |
|
53
|
|
|
|
|
54
|
|
|
$twigparms = array( |
|
55
|
6 |
|
'form' => $form->createView(), |
|
56
|
6 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
|
57
|
6 |
|
'tabellatemplate' => $tabellatemplate |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
6 |
|
if ($form->isSubmitted()) { |
|
61
|
6 |
|
if ($form->isValid()) { |
|
62
|
5 |
|
$entity = $form->getData(); |
|
63
|
|
|
|
|
64
|
5 |
|
$entityManager = $this->getDoctrine()->getManager(); |
|
65
|
5 |
|
$entityManager->persist($entity); |
|
66
|
5 |
|
$entityManager->flush(); |
|
67
|
5 |
|
return new Response( |
|
68
|
5 |
|
$this->renderView($crudtemplate, $twigparms), |
|
69
|
5 |
|
200 |
|
70
|
|
|
); |
|
71
|
|
|
} else { |
|
72
|
|
|
//Quando non passa la validazione |
|
73
|
1 |
|
return new Response( |
|
74
|
1 |
|
$this->renderView($crudtemplate, $twigparms), |
|
75
|
1 |
|
400 |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} else { |
|
79
|
|
|
//Quando viene richiesta una "nuova" new |
|
80
|
6 |
|
return new Response( |
|
81
|
6 |
|
$this->renderView($crudtemplate, $twigparms), |
|
82
|
6 |
|
200 |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Displays a form to edit an existing table entity. |
|
89
|
|
|
*/ |
|
90
|
8 |
|
public function edit(Request $request, $id) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
93
|
8 |
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
94
|
8 |
|
$controller = $this->getController(); |
|
95
|
|
|
|
|
96
|
8 |
|
if (!$this->getPermessi()->canUpdate()) { |
|
97
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
7 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, $this->getThisFunctionName()); |
|
|
|
|
|
|
100
|
7 |
|
$tabellatemplate = $this->getTabellaTemplate($controller); |
|
101
|
|
|
|
|
102
|
7 |
|
$entityclass = $this->getEntityClassName(); |
|
103
|
7 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
7 |
|
$formType = $formclass . 'Type'; |
|
106
|
|
|
|
|
107
|
7 |
|
$elencomodifiche = $this->elencoModifiche($controller, $id); |
|
108
|
|
|
|
|
109
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
110
|
|
|
|
|
111
|
7 |
|
$entity = $em->getRepository($entityclass)->find($id); |
|
112
|
|
|
|
|
113
|
7 |
|
if (!$entity) { |
|
114
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' del record con id ' . $id . '.'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
7 |
|
$editForm = $this->createForm( |
|
118
|
7 |
|
$formType, |
|
119
|
7 |
|
$entity, |
|
120
|
|
|
array('attr' => array( |
|
121
|
7 |
|
'id' => 'formdati' . $controller, |
|
122
|
|
|
), |
|
123
|
7 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
7 |
|
return $this->render( |
|
128
|
7 |
|
$crudtemplate, |
|
129
|
|
|
array( |
|
130
|
7 |
|
'entity' => $entity, |
|
131
|
7 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
|
132
|
7 |
|
'tabellatemplate' => $tabellatemplate, |
|
133
|
7 |
|
'edit_form' => $editForm->createView(), |
|
134
|
7 |
|
'elencomodifiche' => $elencomodifiche, |
|
135
|
|
|
) |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Edits an existing table entity. |
|
141
|
|
|
*/ |
|
142
|
9 |
|
public function update(Request $request, $id) |
|
143
|
|
|
{ |
|
144
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
145
|
9 |
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
146
|
9 |
|
$controller = $this->getController(); |
|
147
|
9 |
|
if (!$this->getPermessi()->canUpdate()) { |
|
148
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
8 |
|
$crudtemplate = $this->getCrudTemplate($bundle, $controller, "edit"); |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
8 |
|
$entityclass = $this->getEntityClassName(); |
|
153
|
8 |
|
$formclass = str_replace("Entity", "Form", $entityclass); |
|
|
|
|
|
|
154
|
8 |
|
$formType = $formclass . 'Type'; |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
8 |
|
$em = $this->getDoctrine()->getManager(); |
|
157
|
|
|
|
|
158
|
8 |
|
$entity = $em->getRepository($entityclass)->find($id); |
|
159
|
|
|
|
|
160
|
8 |
|
if (!$entity) { |
|
161
|
1 |
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' per il record con id ' . $id); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
7 |
|
$editForm = $this->createForm( |
|
165
|
7 |
|
$formType, |
|
166
|
7 |
|
$entity, |
|
167
|
|
|
array('attr' => array( |
|
168
|
7 |
|
'id' => 'formdati' . $controller, |
|
169
|
|
|
), |
|
170
|
7 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
|
171
|
|
|
) |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
7 |
|
$editForm->submit($request->request->get($editForm->getName())); |
|
175
|
|
|
|
|
176
|
7 |
|
if ($editForm->isValid()) { |
|
177
|
7 |
|
$originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
|
178
|
|
|
|
|
179
|
7 |
|
$em->persist($entity); |
|
180
|
7 |
|
$em->flush(); |
|
181
|
|
|
|
|
182
|
7 |
|
$newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
|
|
|
|
|
|
183
|
7 |
|
$repoStorico = $em->getRepository("BiCoreBundle:Storicomodifiche"); |
|
|
|
|
|
|
184
|
7 |
|
$changes = $repoStorico->isRecordChanged($controller, $originalData, $newData); |
|
|
|
|
|
|
185
|
|
|
|
|
186
|
7 |
|
if ($changes) { |
|
187
|
2 |
|
$repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
7 |
|
$continua = (int) $request->get('continua'); |
|
191
|
7 |
|
if ($continua === 0) { |
|
192
|
7 |
|
return new Response('OK'); |
|
193
|
|
|
} else { |
|
194
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
1 |
|
return $this->render( |
|
199
|
1 |
|
$crudtemplate, |
|
200
|
|
|
array( |
|
201
|
1 |
|
'entity' => $entity, |
|
202
|
1 |
|
'edit_form' => $editForm->createView(), |
|
203
|
1 |
|
'nomecontroller' => ParametriTabella::setParameter($controller), |
|
204
|
|
|
) |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
private function checkAggiornaRight($id) |
|
209
|
|
|
{ |
|
210
|
|
|
if ($id === 0) { |
|
211
|
|
|
if (!$this->getPermessi()->canCreate()) { |
|
212
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
} else { |
|
215
|
|
|
if (!$this->getPermessi()->canUpdate()) { |
|
216
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Inline existing table entity. |
|
223
|
|
|
*/ |
|
224
|
|
|
public function aggiorna(Request $request, $id) |
|
225
|
|
|
{ |
|
226
|
|
|
$this->checkAggiornaRight($id); |
|
227
|
|
|
|
|
228
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
229
|
|
|
$controller = $this->getController(); |
|
|
|
|
|
|
230
|
|
|
$entityclass = $this->getEntityClassName(); |
|
231
|
|
|
|
|
232
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
|
|
233
|
|
|
$queryBuilder = $em->createQueryBuilder(); |
|
234
|
|
|
$insert = ($id === 0); |
|
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
if ($insert) { |
|
237
|
|
|
//Insert |
|
238
|
|
|
$entity = new $entityclass(); |
|
239
|
|
|
$queryBuilder |
|
240
|
|
|
->insert($entityclass) |
|
|
|
|
|
|
241
|
|
|
; |
|
242
|
|
|
} else { |
|
243
|
|
|
//Update |
|
244
|
|
|
$entity = $em->getRepository($entityclass)->find($id); |
|
245
|
|
|
if (!$entity) { |
|
246
|
|
|
throw $this->createNotFoundException('Impossibile trovare l\'entità ' . $controller . ' per il record con id ' . $id); |
|
247
|
|
|
} |
|
248
|
|
|
$queryBuilder |
|
249
|
|
|
->update($entityclass, "u") |
|
|
|
|
|
|
250
|
|
|
->where("u.id = :id") |
|
|
|
|
|
|
251
|
|
|
->setParameter("id", $id); |
|
|
|
|
|
|
252
|
|
|
} |
|
253
|
|
|
$values = $request->get("values"); |
|
|
|
|
|
|
254
|
|
|
$querydaeseguire = false; |
|
255
|
|
|
|
|
256
|
|
|
foreach ($values as $value) { |
|
257
|
|
|
$fieldpieces = explode(".", $value["fieldname"]); |
|
|
|
|
|
|
258
|
|
|
$table = $fieldpieces[0]; |
|
|
|
|
|
|
259
|
|
|
//Si prende in considerazione solo i campi strettamente legati a questa entity |
|
260
|
|
|
if ($table == $controller && count($fieldpieces) == 2 && $value["fieldtype"] != 'join') { |
|
|
|
|
|
|
261
|
|
|
$field = $fieldpieces[1]; |
|
262
|
|
|
if ($insert) { |
|
263
|
|
|
$queryBuilder->setValue($field, ':' . $field); |
|
|
|
|
|
|
264
|
|
|
$queryBuilder->setParameter($field, $value["fieldvalue"]); |
|
|
|
|
|
|
265
|
|
|
$querydaeseguire = true; |
|
266
|
|
|
} else { |
|
267
|
|
|
$entityutils = new EntityUtils($em); |
|
|
|
|
|
|
268
|
|
|
$property = $entityutils->getEntityProperties($field, $entity); |
|
|
|
|
|
|
269
|
|
|
$nomefunzioneget = $property["get"]; |
|
|
|
|
|
|
270
|
|
|
if ($nomefunzioneget != $value["fieldvalue"]) { |
|
|
|
|
|
|
271
|
|
|
$querydaeseguire = true; |
|
272
|
|
|
$queryBuilder->set("u." . $field, ':' . $field); |
|
|
|
|
|
|
273
|
|
|
$queryBuilder->setParameter($field, $value["fieldvalue"]); |
|
|
|
|
|
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
} else { |
|
277
|
|
|
continue; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
if ($querydaeseguire) { |
|
281
|
|
|
$queryBuilder->getQuery()->execute(); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
return new \Symfony\Component\HttpFoundation\JsonResponse(array("errcode" => 0, "message" => "Registrazione eseguita")); |
|
|
|
|
|
|
285
|
|
|
} |
|
286
|
|
|
/** |
|
287
|
|
|
* Deletes a table entity. |
|
288
|
|
|
*/ |
|
289
|
8 |
|
public function delete(Request $request) |
|
290
|
|
|
{ |
|
291
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
292
|
8 |
|
if (!$this->getPermessi()->canDelete()) { |
|
293
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per eliminare questo contenuto"); |
|
|
|
|
|
|
294
|
|
|
} |
|
295
|
7 |
|
$entityclass = $this->getEntityClassName(); |
|
296
|
|
|
|
|
297
|
|
|
|
|
298
|
|
|
try { |
|
299
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
|
|
300
|
7 |
|
$qb = $em->createQueryBuilder(); |
|
|
|
|
|
|
301
|
7 |
|
$ids = explode(',', $request->get('id')); |
|
302
|
7 |
|
$qb->delete($entityclass, 'u') |
|
303
|
7 |
|
->andWhere('u.id IN (:ids)') |
|
304
|
7 |
|
->setParameter('ids', $ids); |
|
305
|
|
|
|
|
306
|
7 |
|
$query = $qb->getQuery(); |
|
307
|
7 |
|
$query->execute(); |
|
308
|
2 |
|
} catch (\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException $e) { |
|
309
|
2 |
|
$response = new Response($e->getMessage()); |
|
310
|
2 |
|
$response->setStatusCode('501'); |
|
311
|
2 |
|
return $response; |
|
312
|
|
|
} catch (\Exception $e) { |
|
313
|
|
|
$response = new Response($e->getMessage()); |
|
314
|
|
|
$response->setStatusCode('200'); |
|
315
|
|
|
return $response; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
5 |
|
return new Response('Operazione eseguita con successo'); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
7 |
|
private function elencoModifiche($controller, $id) |
|
322
|
|
|
{ |
|
323
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
|
|
324
|
7 |
|
$risultato = $em->getRepository('BiCoreBundle:Storicomodifiche')->findBy( |
|
325
|
|
|
array( |
|
326
|
7 |
|
'nometabella' => $controller, |
|
327
|
7 |
|
'idtabella' => $id, |
|
328
|
|
|
), |
|
329
|
7 |
|
array('giorno' => 'DESC') |
|
330
|
|
|
); |
|
331
|
|
|
|
|
332
|
7 |
|
return $risultato; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
7 |
|
protected function getTabellaTemplate($controller) |
|
336
|
|
|
{ |
|
337
|
7 |
|
$tabellatemplate = $controller . '/Tabella/tabellaform.html.twig'; |
|
338
|
7 |
|
if (!$this->get('templating')->exists($tabellatemplate)) { |
|
339
|
6 |
|
$tabellatemplate = 'BiCoreBundle:' . $controller . ':Tabella/tabellaform.html.twig'; |
|
340
|
6 |
|
if (!$this->get('templating')->exists($tabellatemplate)) { |
|
341
|
5 |
|
$tabellatemplate = 'BiCoreBundle:Standard:Tabella/tabellaform.html.twig'; |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
7 |
|
return $tabellatemplate; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
9 |
|
protected function getCrudTemplate($bundle, $controller, $operation) |
|
349
|
|
|
{ |
|
350
|
9 |
|
$crudtemplate = $bundle . ':' . $controller . ':Crud/' . $operation . '.html.twig'; |
|
351
|
9 |
|
if (!$this->get('templating')->exists($crudtemplate)) { |
|
352
|
3 |
|
$crudtemplate = $controller . '/Crud/' . $operation . '.html.twig'; |
|
353
|
3 |
|
if (!$this->get('templating')->exists($crudtemplate)) { |
|
354
|
|
|
$crudtemplate = 'BiCoreBundle:Standard:Crud/' . $operation . '.html.twig'; |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
9 |
|
return $crudtemplate; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
11 |
|
protected function getBundle() |
|
361
|
|
|
{ |
|
362
|
11 |
|
return $this->bundle; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
11 |
|
protected function getController() |
|
366
|
|
|
{ |
|
367
|
11 |
|
return $this->controller; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
11 |
|
protected function getPermessi() |
|
371
|
|
|
{ |
|
372
|
11 |
|
return $this->permessi; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* Returns the calling function through a backtrace |
|
377
|
|
|
*/ |
|
378
|
10 |
|
protected function getThisFunctionName() |
|
379
|
|
|
{ |
|
380
|
|
|
// a funciton x has called a function y which called this |
|
381
|
|
|
// see stackoverflow.com/questions/190421 |
|
382
|
10 |
|
$caller = debug_backtrace(); |
|
383
|
10 |
|
$caller = $caller[1]; |
|
384
|
10 |
|
return $caller['function']; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
10 |
|
protected function getEntityClassNotation() |
|
388
|
|
|
{ |
|
389
|
10 |
|
$em = $this->get("doctrine")->getManager(); |
|
|
|
|
|
|
390
|
10 |
|
$entityutils = new EntityUtils($em); |
|
391
|
10 |
|
return $entityutils->getClassNameToShortcutNotations($this->getEntityClassName()); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
10 |
|
protected function getEntityClassName() |
|
395
|
|
|
{ |
|
396
|
10 |
|
$em = $this->get("doctrine")->getManager(); |
|
|
|
|
|
|
397
|
10 |
|
$entityfinder = new Finder($em, $this->controller); |
|
398
|
10 |
|
return $entityfinder->getClassNameFromEntityName(); |
|
399
|
|
|
} |
|
400
|
|
|
} |
|
401
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.