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