|
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
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Lists all tables entities. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function indexAction(Request $request) |
|
23
|
|
|
{ |
|
24
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
25
|
|
|
$this->setup($request); |
|
|
|
|
|
|
26
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
|
|
27
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
28
|
|
|
$controller = $this->getController(); |
|
|
|
|
|
|
29
|
|
|
$container = $this->container; |
|
30
|
|
|
|
|
31
|
|
|
$gestionepermessi = $this->get('ficorebundle.gestionepermessi'); |
|
32
|
|
|
$canRead = ($gestionepermessi->leggere(array('modulo' => $controller)) ? 1 : 0); |
|
33
|
|
|
if (!$canRead) { |
|
34
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto"); |
|
35
|
|
|
} |
|
36
|
|
|
$idpassato = $request->get('id'); |
|
37
|
|
|
|
|
38
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
39
|
|
|
|
|
40
|
|
|
$repotabelle = $this->get('OpzioniTabella_repository'); |
|
41
|
|
|
|
|
42
|
|
|
$paricevuti = array('nomebundle' => $nomebundle, 'nometabella' => $controller, 'container' => $container); |
|
43
|
|
|
|
|
44
|
|
|
$testatagriglia = Griglia::testataPerGriglia($paricevuti); |
|
45
|
|
|
|
|
46
|
|
|
$testatagriglia['multisearch'] = 1; |
|
47
|
|
|
$testatagriglia['showconfig'] = 1; |
|
48
|
|
|
$testatagriglia['overlayopen'] = 1; |
|
49
|
|
|
|
|
50
|
|
|
$testatagriglia['parametritesta'] = json_encode($paricevuti); |
|
51
|
|
|
|
|
52
|
|
|
$this->setParametriGriglia(array('request' => $request)); |
|
|
|
|
|
|
53
|
|
|
$testatagriglia['parametrigriglia'] = json_encode(self::$parametrigriglia); |
|
54
|
|
|
|
|
55
|
|
|
$testata = $repotabelle->editTestataFormTabelle($testatagriglia, $controller, $container); |
|
56
|
|
|
return $this->render( |
|
57
|
|
|
$nomebundle . ':' . $controller . ':index.html.twig', |
|
58
|
|
|
array( |
|
59
|
|
|
'nomecontroller' => $controller, |
|
60
|
|
|
'testata' => $testata, |
|
61
|
|
|
'canread' => $canRead, |
|
62
|
|
|
'idpassato' => $idpassato, |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Creates a new table entity. |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function createAction(Request $request) |
|
71
|
|
|
{ |
|
72
|
2 |
|
$this->setup($request); |
|
|
|
|
|
|
73
|
2 |
|
$namespace = $this->getNamespace(); |
|
|
|
|
|
|
74
|
2 |
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
75
|
2 |
|
$controller = $this->getController(); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
2 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
78
|
2 |
|
$classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
|
79
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
|
80
|
|
|
|
|
81
|
2 |
|
$entity = new $classbundle(); |
|
82
|
2 |
|
$formType = $formbundle . 'Type'; |
|
83
|
|
|
|
|
84
|
2 |
|
$form = $this->createForm( |
|
85
|
2 |
|
$formType, |
|
86
|
2 |
|
$entity, |
|
87
|
|
|
array('attr' => array( |
|
88
|
2 |
|
'id' => 'formdati' . $controller, |
|
89
|
|
|
), |
|
90
|
2 |
|
'action' => $this->generateUrl($controller . '_create'), |
|
91
|
|
|
) |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
2 |
|
$form->submit($request->request->get($form->getName())); |
|
95
|
|
|
|
|
96
|
2 |
|
if ($form->isValid()) { |
|
97
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
|
98
|
2 |
|
$em->persist($entity); |
|
99
|
2 |
|
$em->flush(); |
|
100
|
|
|
|
|
101
|
2 |
|
$continua = $request->get('continua'); |
|
102
|
2 |
|
if ($continua == 0) { |
|
103
|
2 |
|
return new Response('OK'); |
|
104
|
|
|
} else { |
|
105
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $entity->getId()))); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this->render( |
|
110
|
|
|
$nomebundle . ':' . $controller . ':new.html.twig', |
|
111
|
|
|
array( |
|
112
|
|
|
'nomecontroller' => $controller, |
|
113
|
|
|
'entity' => $entity, |
|
114
|
|
|
'form' => $form->createView(), |
|
115
|
|
|
) |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Displays a form to create a new table entity. |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function newAction(Request $request) |
|
123
|
|
|
{ |
|
124
|
2 |
|
$this->setup($request); |
|
|
|
|
|
|
125
|
2 |
|
$namespace = $this->getNamespace(); |
|
|
|
|
|
|
126
|
2 |
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
127
|
2 |
|
$controller = $this->getController(); |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
2 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
130
|
2 |
|
$classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
|
131
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
|
132
|
2 |
|
$formType = $formbundle . 'Type'; |
|
133
|
|
|
|
|
134
|
2 |
|
$entity = new $classbundle(); |
|
135
|
|
|
|
|
136
|
2 |
|
$form = $this->createForm( |
|
137
|
2 |
|
$formType, |
|
138
|
2 |
|
$entity, |
|
139
|
|
|
array('attr' => array( |
|
140
|
2 |
|
'id' => 'formdati' . $controller, |
|
141
|
|
|
), |
|
142
|
2 |
|
'action' => $this->generateUrl($controller . '_create'), |
|
143
|
|
|
) |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
2 |
|
return $this->render( |
|
147
|
2 |
|
$nomebundle . ':' . $controller . ':new.html.twig', |
|
148
|
|
|
array( |
|
149
|
2 |
|
'nomecontroller' => $controller, |
|
150
|
2 |
|
'entity' => $entity, |
|
151
|
2 |
|
'form' => $form->createView(), |
|
152
|
|
|
) |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Displays a form to edit an existing table entity. |
|
158
|
|
|
*/ |
|
159
|
2 |
|
public function editAction(Request $request, $id) |
|
160
|
|
|
{ |
|
161
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
162
|
2 |
|
$this->setup($request); |
|
|
|
|
|
|
163
|
2 |
|
$namespace = $this->getNamespace(); |
|
|
|
|
|
|
164
|
2 |
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
165
|
2 |
|
$controller = $this->getController(); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
2 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
168
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
|
169
|
2 |
|
$formType = $formbundle . 'Type'; |
|
170
|
|
|
|
|
171
|
2 |
|
$elencomodifiche = $this->elencoModifiche($nomebundle, $controller, $id); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
|
174
|
|
|
|
|
175
|
2 |
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
|
176
|
|
|
|
|
177
|
2 |
|
if (!$entity) { |
|
178
|
|
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
2 |
|
$editForm = $this->createForm( |
|
182
|
2 |
|
$formType, |
|
183
|
2 |
|
$entity, |
|
184
|
|
|
array('attr' => array( |
|
185
|
2 |
|
'id' => 'formdati' . $controller, |
|
186
|
|
|
), |
|
187
|
2 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
|
188
|
|
|
) |
|
189
|
|
|
); |
|
190
|
|
|
|
|
191
|
2 |
|
$deleteForm = $this->createDeleteForm($id); |
|
192
|
|
|
|
|
193
|
2 |
|
return $this->render( |
|
194
|
2 |
|
$nomebundle . ':' . $controller . ':edit.html.twig', |
|
195
|
|
|
array( |
|
196
|
2 |
|
'entity' => $entity, |
|
197
|
2 |
|
'nomecontroller' => $controller, |
|
198
|
2 |
|
'edit_form' => $editForm->createView(), |
|
199
|
2 |
|
'delete_form' => $deleteForm->createView(), |
|
200
|
2 |
|
'elencomodifiche' => $elencomodifiche, |
|
201
|
|
|
) |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Edits an existing table entity. |
|
207
|
|
|
*/ |
|
208
|
2 |
|
public function updateAction(Request $request, $id) |
|
209
|
|
|
{ |
|
210
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
211
|
2 |
|
$this->setup($request); |
|
|
|
|
|
|
212
|
2 |
|
$namespace = $this->getNamespace(); |
|
|
|
|
|
|
213
|
2 |
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
214
|
2 |
|
$controller = $this->getController(); |
|
|
|
|
|
|
215
|
|
|
|
|
216
|
2 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
217
|
2 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
|
218
|
2 |
|
$formType = $formbundle . 'Type'; |
|
219
|
|
|
|
|
220
|
2 |
|
$repoStorico = $this->container->get('Storicomodifiche_repository'); |
|
221
|
|
|
|
|
222
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
|
223
|
|
|
|
|
224
|
2 |
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
|
225
|
|
|
|
|
226
|
2 |
|
if (!$entity) { |
|
227
|
|
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
2 |
|
$deleteForm = $this->createDeleteForm($id); |
|
231
|
|
|
|
|
232
|
2 |
|
$editForm = $this->createForm( |
|
233
|
2 |
|
$formType, |
|
234
|
2 |
|
$entity, |
|
235
|
|
|
array('attr' => array( |
|
236
|
2 |
|
'id' => 'formdati' . $controller, |
|
237
|
|
|
), |
|
238
|
2 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
|
239
|
|
|
) |
|
240
|
|
|
); |
|
241
|
|
|
|
|
242
|
2 |
|
$editForm->submit($request->request->get($editForm->getName())); |
|
243
|
|
|
|
|
244
|
2 |
|
if ($editForm->isValid()) { |
|
245
|
2 |
|
$originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
|
246
|
|
|
|
|
247
|
2 |
|
$em->persist($entity); |
|
248
|
2 |
|
$em->flush(); |
|
249
|
|
|
|
|
250
|
2 |
|
$newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
|
251
|
2 |
|
$changes = $repoStorico->isRecordChanged($nomebundle, $controller, $originalData, $newData); |
|
252
|
|
|
|
|
253
|
2 |
|
if ($changes) { |
|
254
|
1 |
|
$repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
2 |
|
$continua = $request->get('continua'); |
|
258
|
2 |
|
if ($continua == 0) { |
|
259
|
2 |
|
return new Response('OK'); |
|
260
|
|
|
} else { |
|
261
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
return $this->render( |
|
266
|
|
|
$nomebundle . ':' . $controller . ':edit.html.twig', |
|
267
|
|
|
array( |
|
268
|
|
|
'entity' => $entity, |
|
269
|
|
|
'edit_form' => $editForm->createView(), |
|
270
|
|
|
'delete_form' => $deleteForm->createView(), |
|
271
|
|
|
'nomecontroller' => $controller, |
|
272
|
|
|
) |
|
273
|
|
|
); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Edits an existing table entity. |
|
278
|
|
|
*/ |
|
279
|
|
|
public function aggiornaAction(Request $request) |
|
280
|
|
|
{ |
|
281
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
282
|
|
|
$this->setup($request); |
|
|
|
|
|
|
283
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
|
|
284
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
285
|
|
|
$controller = $this->getController(); |
|
|
|
|
|
|
286
|
|
|
|
|
287
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
288
|
|
|
|
|
289
|
|
|
$id = $this->get('request')->request->get('id'); |
|
290
|
|
|
|
|
291
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
292
|
|
|
|
|
293
|
|
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
|
294
|
|
|
|
|
295
|
|
|
if (!$entity) { |
|
296
|
|
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
throw $this->createNotFoundException("Implementare a seconda dell'esigenza 'aggiornaAction' del controller " |
|
300
|
|
|
. $nomebundle |
|
301
|
|
|
. '/' |
|
302
|
|
|
. $controller); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Deletes a table entity. |
|
307
|
|
|
*/ |
|
308
|
2 |
|
public function deleteAction(Request $request) |
|
309
|
|
|
{ |
|
310
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
311
|
2 |
|
$this->setup($request); |
|
|
|
|
|
|
312
|
2 |
|
$namespace = $this->getNamespace(); |
|
|
|
|
|
|
313
|
2 |
|
$bundle = $this->getBundle(); |
|
|
|
|
|
|
314
|
2 |
|
$controller = $this->getController(); |
|
|
|
|
|
|
315
|
|
|
|
|
316
|
2 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
317
|
|
|
|
|
318
|
|
|
//if (!$request->isXmlHttpRequest()) { |
|
|
|
|
|
|
319
|
|
|
// $request->checkCSRFProtection(); |
|
|
|
|
|
|
320
|
|
|
//} |
|
321
|
|
|
try { |
|
322
|
2 |
|
$em = $this->getDoctrine()->getManager(); |
|
323
|
2 |
|
$qb = $em->createQueryBuilder(); |
|
324
|
2 |
|
$ids = explode(',', $request->get('id')); |
|
325
|
2 |
|
$qb->delete($nomebundle . ':' . $controller, 'u') |
|
326
|
2 |
|
->andWhere('u.id IN (:ids)') |
|
327
|
2 |
|
->setParameter('ids', $ids); |
|
328
|
|
|
|
|
329
|
2 |
|
$query = $qb->getQuery(); |
|
330
|
2 |
|
$query->execute(); |
|
331
|
|
|
} catch (\Exception $e) { |
|
332
|
|
|
$response = new Response(); |
|
333
|
|
|
$response->setStatusCode('200'); |
|
334
|
|
|
|
|
335
|
|
|
return new Response('404'); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
2 |
|
return new Response('OK'); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Creates a form to delete a table entity by id. |
|
343
|
|
|
* |
|
344
|
|
|
* @param mixed $id The entity id |
|
345
|
|
|
* |
|
346
|
|
|
* @return \Symfony\Component\Form\Form The form |
|
347
|
|
|
*/ |
|
348
|
|
|
protected function createDeleteForm($id) |
|
349
|
|
|
{ |
|
350
|
|
|
return $this->createFormBuilder(array('id' => $id)) |
|
351
|
|
|
->add('id', get_class(new \Symfony\Component\Form\Extension\Core\Type\HiddenType())) |
|
352
|
|
|
->getForm(); |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: