1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
9
|
|
|
|
10
|
|
|
class FiCrudController extends Controller |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public static $namespace; |
14
|
|
|
public static $bundle; |
15
|
|
|
public static $controller; |
16
|
|
|
public static $action; |
17
|
|
|
public static $parametrigriglia; |
18
|
|
|
public static $canRead; |
19
|
|
|
public static $canDelete; |
20
|
|
|
public static $canCreate; |
21
|
|
|
public static $canUpdate; |
22
|
|
|
|
23
|
15 |
|
protected function setup(Request $request) |
24
|
|
|
{ |
25
|
15 |
|
$matches = array(); |
|
|
|
|
26
|
15 |
|
$controllo = new \ReflectionClass(get_class($this)); |
27
|
|
|
|
28
|
15 |
|
preg_match('/(.*)\\\(.*)Bundle\\\Controller\\\(.*)Controller/', $controllo->name, $matches); |
29
|
|
|
|
30
|
15 |
|
self::$namespace = $matches[1]; |
|
|
|
|
31
|
15 |
|
self::$bundle = $matches[2]; |
|
|
|
|
32
|
15 |
|
self::$controller = $matches[3]; |
33
|
15 |
|
self::$action = substr($request->attributes->get('_controller'), strrpos($request->attributes->get('_controller'), ':') + 1); |
|
|
|
|
34
|
|
|
} |
35
|
15 |
|
|
36
|
15 |
|
/** |
37
|
15 |
|
* Lists all tables entities. |
38
|
15 |
|
*/ |
39
|
15 |
|
public function indexAction(Request $request) |
40
|
|
|
{ |
41
|
15 |
|
/* @var $em \Doctrine\ORM\EntityManager */ |
42
|
1 |
|
$this->setup($request); |
43
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
44
|
14 |
|
$bundle = $this->getBundle(); |
|
|
|
|
45
|
|
|
$controller = $this->getController(); |
46
|
|
|
|
47
|
|
|
if (!self::$canRead) { |
48
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto"); |
|
|
|
|
49
|
4 |
|
} |
50
|
|
|
|
51
|
|
|
$container = $this->container; |
52
|
4 |
|
|
53
|
4 |
|
$idpassato = $request->get('id'); |
54
|
4 |
|
|
55
|
4 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
56
|
|
|
|
57
|
4 |
|
$repotabelle = $this->get('OpzioniTabella_repository'); |
58
|
|
|
|
59
|
|
|
$paricevuti = array('nomebundle' => $nomebundle, 'nometabella' => $controller, 'container' => $container); |
60
|
|
|
|
61
|
4 |
|
$testatagriglia = Griglia::testataPerGriglia($paricevuti); |
62
|
|
|
|
63
|
4 |
|
$testatagriglia['multisearch'] = 1; |
|
|
|
|
64
|
|
|
$testatagriglia['showconfig'] = 1; |
|
|
|
|
65
|
4 |
|
$testatagriglia['overlayopen'] = 1; |
|
|
|
|
66
|
|
|
$testatagriglia['showadd'] = self::$canCreate; |
|
|
|
|
67
|
4 |
|
$testatagriglia['showedit'] = self::$canUpdate; |
|
|
|
|
68
|
|
|
$testatagriglia['showdel'] = self::$canDelete; |
|
|
|
|
69
|
4 |
|
$testatagriglia["filterToolbar_searchOnEnter"] = true; |
|
|
|
|
70
|
|
|
$testatagriglia["filterToolbar_searchOperators"] = true; |
|
|
|
|
71
|
4 |
|
|
72
|
|
|
$testatagriglia['parametritesta'] = json_encode($paricevuti); |
73
|
4 |
|
|
74
|
4 |
|
$this->setParametriGriglia(array('request' => $request)); |
|
|
|
|
75
|
4 |
|
$testatagriglia['parametrigriglia'] = json_encode(self::$parametrigriglia); |
76
|
4 |
|
|
77
|
4 |
|
$testata = $repotabelle->editTestataFormTabelle($testatagriglia, $controller, $container); |
78
|
4 |
|
return $this->render( |
79
|
4 |
|
$nomebundle . ':' . $controller . ':index.html.twig', |
80
|
4 |
|
array( |
81
|
|
|
'nomecontroller' => $controller, |
82
|
4 |
|
'testata' => $testata, |
83
|
|
|
'canread' => self::$canRead, |
84
|
4 |
|
'idpassato' => $idpassato, |
85
|
4 |
|
) |
86
|
|
|
); |
87
|
4 |
|
} |
88
|
4 |
|
|
89
|
4 |
|
/** |
90
|
|
|
* Creates a new table entity. |
91
|
4 |
|
*/ |
92
|
4 |
|
public function createAction(Request $request) |
93
|
4 |
|
{ |
94
|
4 |
|
$this->setup($request); |
95
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
96
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
97
|
|
|
$controller = $this->getController(); |
98
|
|
|
|
99
|
|
|
if (!self::$canCreate) { |
100
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
101
|
|
|
} |
102
|
2 |
|
|
103
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
|
|
|
104
|
2 |
|
$classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
105
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
|
|
|
|
106
|
2 |
|
|
107
|
2 |
|
$entity = new $classbundle(); |
|
|
|
|
108
|
|
|
$formType = $formbundle . 'Type'; |
109
|
2 |
|
|
110
|
|
|
$form = $this->createForm( |
111
|
|
|
$formType, |
112
|
|
|
$entity, |
113
|
2 |
|
array('attr' => array( |
114
|
2 |
|
'id' => 'formdati' . $controller, |
115
|
2 |
|
), |
116
|
|
|
'action' => $this->generateUrl($controller . '_create'), |
117
|
2 |
|
) |
118
|
2 |
|
); |
119
|
|
|
|
120
|
2 |
|
$form->submit($request->request->get($form->getName())); |
121
|
2 |
|
|
122
|
2 |
|
if ($form->isValid()) { |
123
|
|
|
$em = $this->getDoctrine()->getManager(); |
124
|
2 |
|
$em->persist($entity); |
125
|
|
|
$em->flush(); |
126
|
2 |
|
|
127
|
|
|
$continua = $request->get('continua'); |
128
|
|
|
if ($continua == 0) { |
|
|
|
|
129
|
|
|
return new Response('OK'); |
130
|
2 |
|
} else { |
131
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $entity->getId()))); |
132
|
2 |
|
} |
133
|
2 |
|
} |
134
|
2 |
|
|
135
|
2 |
|
return $this->render( |
136
|
|
|
$nomebundle . ':' . $controller . ':new.html.twig', |
137
|
2 |
|
array( |
138
|
2 |
|
'nomecontroller' => $controller, |
139
|
2 |
|
'entity' => $entity, |
140
|
|
|
'form' => $form->createView(), |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Displays a form to create a new table entity. |
147
|
|
|
*/ |
148
|
|
|
public function newAction(Request $request) |
149
|
|
|
{ |
150
|
|
|
$this->setup($request); |
151
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
152
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
153
|
|
|
$controller = $this->getController(); |
154
|
|
|
|
155
|
|
|
if (!self::$canCreate) { |
156
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
157
|
|
|
} |
158
|
2 |
|
|
159
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
|
|
|
160
|
2 |
|
$classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
161
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
|
|
|
|
162
|
2 |
|
$formType = $formbundle . 'Type'; |
|
|
|
|
163
|
2 |
|
|
164
|
|
|
$entity = new $classbundle(); |
165
|
2 |
|
|
166
|
|
|
$form = $this->createForm( |
167
|
|
|
$formType, |
168
|
|
|
$entity, |
169
|
2 |
|
array('attr' => array( |
170
|
2 |
|
'id' => 'formdati' . $controller, |
171
|
2 |
|
), |
172
|
2 |
|
'action' => $this->generateUrl($controller . '_create'), |
173
|
|
|
) |
174
|
2 |
|
); |
175
|
|
|
|
176
|
2 |
|
return $this->render( |
177
|
2 |
|
$nomebundle . ':' . $controller . ':new.html.twig', |
178
|
2 |
|
array( |
179
|
|
|
'nomecontroller' => $controller, |
180
|
2 |
|
'entity' => $entity, |
181
|
|
|
'form' => $form->createView(), |
182
|
2 |
|
) |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
2 |
|
/** |
187
|
2 |
|
* Displays a form to edit an existing table entity. |
188
|
|
|
*/ |
189
|
2 |
|
public function editAction(Request $request, $id) |
190
|
2 |
|
{ |
191
|
2 |
|
/* @var $em \Doctrine\ORM\EntityManager */ |
192
|
|
|
$this->setup($request); |
193
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
194
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
195
|
|
|
$controller = $this->getController(); |
196
|
|
|
|
197
|
|
|
if (!self::$canUpdate) { |
198
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
199
|
2 |
|
} |
200
|
|
|
|
201
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
202
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
203
|
2 |
|
$formType = $formbundle . 'Type'; |
|
|
|
|
204
|
2 |
|
|
205
|
2 |
|
$elencomodifiche = $this->elencoModifiche($controller, $id); |
|
|
|
|
206
|
|
|
|
207
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
208
|
|
|
|
209
|
|
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
210
|
|
|
|
211
|
2 |
|
if (!$entity) { |
212
|
2 |
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
213
|
2 |
|
} |
214
|
|
|
|
215
|
2 |
|
$editForm = $this->createForm( |
216
|
|
|
$formType, |
217
|
2 |
|
$entity, |
218
|
|
|
array('attr' => array( |
219
|
2 |
|
'id' => 'formdati' . $controller, |
220
|
|
|
), |
221
|
2 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
222
|
|
|
) |
223
|
|
|
); |
224
|
|
|
|
225
|
2 |
|
$deleteForm = $this->createDeleteForm($id); |
226
|
2 |
|
|
227
|
2 |
|
return $this->render( |
228
|
|
|
$nomebundle . ':' . $controller . ':edit.html.twig', |
229
|
2 |
|
array( |
230
|
|
|
'entity' => $entity, |
231
|
2 |
|
'nomecontroller' => $controller, |
232
|
|
|
'edit_form' => $editForm->createView(), |
233
|
|
|
'delete_form' => $deleteForm->createView(), |
234
|
|
|
'elencomodifiche' => $elencomodifiche, |
235
|
2 |
|
) |
236
|
|
|
); |
237
|
2 |
|
} |
238
|
2 |
|
|
239
|
|
|
/** |
240
|
2 |
|
* Edits an existing table entity. |
241
|
2 |
|
*/ |
242
|
2 |
|
public function updateAction(Request $request, $id) |
243
|
2 |
|
{ |
244
|
2 |
|
/* @var $em \Doctrine\ORM\EntityManager */ |
245
|
|
|
$this->setup($request); |
246
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
247
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
248
|
|
|
$controller = $this->getController(); |
249
|
|
|
|
250
|
|
|
if (!self::$canUpdate) { |
251
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per aggiornare questo contenuto"); |
|
|
|
|
252
|
2 |
|
} |
253
|
|
|
|
254
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
255
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
256
|
2 |
|
$formType = $formbundle . 'Type'; |
|
|
|
|
257
|
2 |
|
|
258
|
2 |
|
$repoStorico = $this->container->get('Storicomodifiche_repository'); |
259
|
|
|
|
260
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
261
|
|
|
|
262
|
|
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
263
|
|
|
|
264
|
2 |
|
if (!$entity) { |
265
|
2 |
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
266
|
2 |
|
} |
267
|
|
|
|
268
|
2 |
|
$deleteForm = $this->createDeleteForm($id); |
269
|
|
|
|
270
|
2 |
|
$editForm = $this->createForm( |
271
|
|
|
$formType, |
272
|
2 |
|
$entity, |
273
|
|
|
array('attr' => array( |
274
|
2 |
|
'id' => 'formdati' . $controller, |
275
|
|
|
), |
276
|
|
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
277
|
|
|
) |
278
|
2 |
|
); |
279
|
|
|
|
280
|
2 |
|
$editForm->submit($request->request->get($editForm->getName())); |
281
|
2 |
|
|
282
|
2 |
|
if ($editForm->isValid()) { |
283
|
|
|
$originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
284
|
2 |
|
|
285
|
|
|
$em->persist($entity); |
286
|
2 |
|
$em->flush(); |
287
|
|
|
|
288
|
|
|
$newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
289
|
|
|
$changes = $repoStorico->isRecordChanged($nomebundle, $controller, $originalData, $newData); |
290
|
2 |
|
|
291
|
|
|
if ($changes) { |
292
|
2 |
|
$repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
293
|
2 |
|
} |
294
|
|
|
|
295
|
2 |
|
$continua = $request->get('continua'); |
296
|
2 |
|
if ($continua == 0) { |
|
|
|
|
297
|
|
|
return new Response('OK'); |
298
|
2 |
|
} else { |
299
|
2 |
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
300
|
|
|
} |
301
|
2 |
|
} |
302
|
1 |
|
|
303
|
|
|
return $this->render( |
304
|
|
|
$nomebundle . ':' . $controller . ':edit.html.twig', |
305
|
2 |
|
array( |
306
|
2 |
|
'entity' => $entity, |
307
|
2 |
|
'edit_form' => $editForm->createView(), |
308
|
|
|
'delete_form' => $deleteForm->createView(), |
309
|
|
|
'nomecontroller' => $controller, |
310
|
|
|
) |
311
|
|
|
); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Edits an existing table entity. |
316
|
|
|
*/ |
317
|
|
|
public function aggiornaAction(Request $request) |
318
|
|
|
{ |
319
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
320
|
|
|
$this->setup($request); |
321
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
322
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
323
|
|
|
$controller = $this->getController(); |
324
|
|
|
|
325
|
|
|
if (!self::$canUpdate) { |
326
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per aggiornare questo contenuto"); |
|
|
|
|
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
330
|
|
|
|
331
|
|
|
$id = $this->get('request')->request->get('id'); |
332
|
|
|
|
333
|
|
|
$em = $this->getDoctrine()->getManager(); |
334
|
|
|
|
335
|
|
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
336
|
|
|
|
337
|
|
|
if (!$entity) { |
338
|
|
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
throw $this->createNotFoundException("Implementare a seconda dell'esigenza 'aggiornaAction' del controller " |
342
|
|
|
. $nomebundle |
343
|
|
|
. '/' |
344
|
|
|
. $controller); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Deletes a table entity. |
349
|
|
|
*/ |
350
|
|
|
public function deleteAction(Request $request) |
351
|
|
|
{ |
352
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
353
|
|
|
$this->setup($request); |
354
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
355
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
356
|
|
|
$controller = $this->getController(); |
357
|
|
|
|
358
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
359
|
|
|
|
360
|
2 |
|
try { |
361
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
362
|
|
|
$qb = $em->createQueryBuilder(); |
|
|
|
|
363
|
2 |
|
$ids = explode(',', $request->get('id')); |
364
|
2 |
|
$qb->delete($nomebundle . ':' . $controller, 'u') |
365
|
2 |
|
->andWhere('u.id IN (:ids)') |
366
|
2 |
|
->setParameter('ids', $ids); |
367
|
|
|
|
368
|
2 |
|
$query = $qb->getQuery(); |
369
|
|
|
$query->execute(); |
370
|
|
|
} catch (\Exception $e) { |
371
|
2 |
|
$response = new Response(); |
372
|
2 |
|
$response->setStatusCode('200'); |
373
|
2 |
|
|
374
|
2 |
|
return new Response('404'); |
375
|
2 |
|
} |
376
|
2 |
|
|
377
|
|
|
return new Response('OK'); |
378
|
2 |
|
} |
379
|
2 |
|
|
380
|
|
|
/** |
381
|
|
|
* Creates a form to delete a table entity by id. |
382
|
|
|
* |
383
|
|
|
* @param mixed $id The entity id |
384
|
|
|
* |
385
|
|
|
* @return \Symfony\Component\Form\Form The form |
386
|
|
|
*/ |
387
|
2 |
|
protected function createDeleteForm($id) |
388
|
|
|
{ |
389
|
|
|
return $this->createFormBuilder(array('id' => $id)) |
390
|
|
|
->add('id', get_class(new \Symfony\Component\Form\Extension\Core\Type\HiddenType())) |
391
|
|
|
->getForm(); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
protected function getNamespace() |
395
|
|
|
{ |
396
|
|
|
return self::$namespace; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
protected function getBundle() |
400
|
|
|
{ |
401
|
|
|
return self::$bundle; |
402
|
|
|
} |
403
|
|
|
|
404
|
14 |
|
protected function getController() |
405
|
|
|
{ |
406
|
14 |
|
return self::$controller; |
407
|
|
|
} |
408
|
|
|
|
409
|
14 |
|
protected function getAction() |
410
|
|
|
{ |
411
|
14 |
|
return self::$action; |
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.