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