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
|
|
|
$token = $request->get("token"); |
|
|
|
|
255
|
|
|
$isValidToken = $this->isCsrfTokenValid($id, $token); |
256
|
|
|
|
257
|
|
|
if (!$isValidToken) { |
258
|
|
|
throw $this->createNotFoundException('Token non valido'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$querydaeseguire = false; |
262
|
|
|
|
263
|
|
|
foreach ($values as $value) { |
264
|
|
|
$fieldpieces = explode(".", $value["fieldname"]); |
|
|
|
|
265
|
|
|
$table = $fieldpieces[0]; |
|
|
|
|
266
|
|
|
//Si prende in considerazione solo i campi strettamente legati a questa entity |
267
|
|
|
if ($table == $controller && count($fieldpieces) == 2 && $value["fieldtype"] != 'join') { |
|
|
|
|
268
|
|
|
$field = $fieldpieces[1]; |
269
|
|
|
if ($insert) { |
270
|
|
|
$queryBuilder->setValue($field, ':' . $field); |
|
|
|
|
271
|
|
|
$queryBuilder->setParameter($field, $value["fieldvalue"]); |
|
|
|
|
272
|
|
|
$querydaeseguire = true; |
273
|
|
|
} else { |
274
|
|
|
$entityutils = new EntityUtils($em); |
|
|
|
|
275
|
|
|
$property = $entityutils->getEntityProperties($field, $entity); |
|
|
|
|
276
|
|
|
$nomefunzioneget = $property["get"]; |
|
|
|
|
277
|
|
|
if ($nomefunzioneget != $value["fieldvalue"]) { |
|
|
|
|
278
|
|
|
$querydaeseguire = true; |
279
|
|
|
$queryBuilder->set("u." . $field, ':' . $field); |
|
|
|
|
280
|
|
|
$queryBuilder->setParameter($field, $value["fieldvalue"]); |
|
|
|
|
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} else { |
284
|
|
|
continue; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
if ($querydaeseguire) { |
288
|
|
|
$queryBuilder->getQuery()->execute(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return new \Symfony\Component\HttpFoundation\JsonResponse(array("errcode" => 0, "message" => "Registrazione eseguita")); |
|
|
|
|
292
|
|
|
} |
293
|
|
|
/** |
294
|
|
|
* Deletes a table entity. |
295
|
|
|
*/ |
296
|
8 |
|
public function delete(Request $request) |
297
|
|
|
{ |
298
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
299
|
8 |
|
if (!$this->getPermessi()->canDelete()) { |
300
|
1 |
|
throw new AccessDeniedException("Non si hanno i permessi per eliminare questo contenuto"); |
|
|
|
|
301
|
|
|
} |
302
|
7 |
|
$entityclass = $this->getEntityClassName(); |
303
|
|
|
|
304
|
|
|
/*$token = $request->get("token"); |
|
|
|
|
305
|
|
|
$isValidToken = $this->isCsrfTokenValid($this->getController(), $token); |
306
|
|
|
|
307
|
|
|
if (!$isValidToken){ |
308
|
|
|
throw $this->createNotFoundException('Token non valido'); |
309
|
|
|
}*/ |
310
|
|
|
|
311
|
|
|
try { |
312
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
313
|
7 |
|
$qb = $em->createQueryBuilder(); |
|
|
|
|
314
|
7 |
|
$ids = explode(',', $request->get('id')); |
315
|
7 |
|
$qb->delete($entityclass, 'u') |
316
|
7 |
|
->andWhere('u.id IN (:ids)') |
317
|
7 |
|
->setParameter('ids', $ids); |
318
|
|
|
|
319
|
7 |
|
$query = $qb->getQuery(); |
320
|
7 |
|
$query->execute(); |
321
|
2 |
|
} catch (\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException $e) { |
322
|
2 |
|
$response = new Response($e->getMessage()); |
323
|
2 |
|
$response->setStatusCode('501'); |
324
|
2 |
|
return $response; |
325
|
|
|
} catch (\Exception $e) { |
326
|
|
|
$response = new Response($e->getMessage()); |
327
|
|
|
$response->setStatusCode('200'); |
328
|
|
|
return $response; |
329
|
|
|
} |
330
|
|
|
|
331
|
5 |
|
return new Response('Operazione eseguita con successo'); |
332
|
|
|
} |
333
|
|
|
|
334
|
7 |
|
private function elencoModifiche($controller, $id) |
335
|
|
|
{ |
336
|
7 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
337
|
7 |
|
$risultato = $em->getRepository('BiCoreBundle:Storicomodifiche')->findBy( |
338
|
|
|
array( |
339
|
7 |
|
'nometabella' => $controller, |
340
|
7 |
|
'idtabella' => $id, |
341
|
|
|
), |
342
|
7 |
|
array('giorno' => 'DESC') |
343
|
|
|
); |
344
|
|
|
|
345
|
7 |
|
return $risultato; |
346
|
|
|
} |
347
|
|
|
|
348
|
7 |
|
protected function getTabellaTemplate($controller) |
349
|
|
|
{ |
350
|
7 |
|
$tabellatemplate = $controller . '/Tabella/tabellaform.html.twig'; |
351
|
7 |
|
if (!$this->get('templating')->exists($tabellatemplate)) { |
352
|
6 |
|
$tabellatemplate = 'BiCoreBundle:' . $controller . ':Tabella/tabellaform.html.twig'; |
353
|
6 |
|
if (!$this->get('templating')->exists($tabellatemplate)) { |
354
|
5 |
|
$tabellatemplate = 'BiCoreBundle:Standard:Tabella/tabellaform.html.twig'; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
358
|
7 |
|
return $tabellatemplate; |
359
|
|
|
} |
360
|
|
|
|
361
|
9 |
|
protected function getCrudTemplate($bundle, $controller, $operation) |
362
|
|
|
{ |
363
|
9 |
|
$crudtemplate = $bundle . ':' . $controller . ':Crud/' . $operation . '.html.twig'; |
364
|
9 |
|
if (!$this->get('templating')->exists($crudtemplate)) { |
365
|
3 |
|
$crudtemplate = $controller . '/Crud/' . $operation . '.html.twig'; |
366
|
3 |
|
if (!$this->get('templating')->exists($crudtemplate)) { |
367
|
|
|
$crudtemplate = 'BiCoreBundle:Standard:Crud/' . $operation . '.html.twig'; |
368
|
|
|
} |
369
|
|
|
} |
370
|
9 |
|
return $crudtemplate; |
371
|
|
|
} |
372
|
|
|
|
373
|
11 |
|
protected function getBundle() |
374
|
|
|
{ |
375
|
11 |
|
return $this->bundle; |
376
|
|
|
} |
377
|
|
|
|
378
|
11 |
|
protected function getController() |
379
|
|
|
{ |
380
|
11 |
|
return $this->controller; |
381
|
|
|
} |
382
|
|
|
|
383
|
11 |
|
protected function getPermessi() |
384
|
|
|
{ |
385
|
11 |
|
return $this->permessi; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Returns the calling function through a backtrace |
390
|
|
|
*/ |
391
|
10 |
|
protected function getThisFunctionName() |
392
|
|
|
{ |
393
|
|
|
// a funciton x has called a function y which called this |
394
|
|
|
// see stackoverflow.com/questions/190421 |
395
|
10 |
|
$caller = debug_backtrace(); |
396
|
10 |
|
$caller = $caller[1]; |
397
|
10 |
|
return $caller['function']; |
398
|
|
|
} |
399
|
|
|
|
400
|
10 |
|
protected function getEntityClassNotation() |
401
|
|
|
{ |
402
|
10 |
|
$em = $this->get("doctrine")->getManager(); |
|
|
|
|
403
|
10 |
|
$entityutils = new EntityUtils($em); |
404
|
10 |
|
return $entityutils->getClassNameToShortcutNotations($this->getEntityClassName()); |
405
|
|
|
} |
406
|
|
|
|
407
|
10 |
|
protected function getEntityClassName() |
408
|
|
|
{ |
409
|
10 |
|
$em = $this->get("doctrine")->getManager(); |
|
|
|
|
410
|
10 |
|
$entityfinder = new Finder($em, $this->controller); |
411
|
10 |
|
return $entityfinder->getClassNameFromEntityName(); |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
|
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.