1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* MenuApplicazione controller. |
11
|
|
|
*/ |
12
|
|
|
class MenuApplicazioneController extends FiCoreController |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Displays a form to create a new table entity. |
16
|
|
|
*/ |
17
|
1 |
|
public function newAction(Request $request) |
18
|
|
|
{ |
19
|
1 |
|
$this->setup($request); |
20
|
1 |
|
$namespace = $this->getNamespace(); |
|
|
|
|
21
|
1 |
|
$bundle = $this->getBundle(); |
|
|
|
|
22
|
1 |
|
$controller = $this->getController(); |
23
|
|
|
|
24
|
1 |
|
if (!self::$canCreate) { |
25
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
|
|
|
29
|
1 |
|
$classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
30
|
1 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
|
|
|
|
31
|
1 |
|
$formType = $formbundle . 'Type'; |
|
|
|
|
32
|
|
|
|
33
|
1 |
|
$entity = new $classbundle(); |
34
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
35
|
1 |
|
$form = $this->createForm( |
|
|
|
|
36
|
1 |
|
$formType, |
37
|
1 |
|
$entity, |
38
|
1 |
|
array('entity_manager' => $em, 'attr' => array( |
39
|
1 |
|
'id' => 'formdati' . $controller, |
40
|
|
|
), |
41
|
1 |
|
'action' => $this->generateUrl($controller . '_create'), |
42
|
|
|
) |
43
|
|
|
); |
44
|
|
|
|
45
|
1 |
|
return $this->render( |
46
|
1 |
|
$nomebundle . ':' . $controller . ':new.html.twig', |
47
|
|
|
array( |
48
|
1 |
|
'nomecontroller' => $controller, |
49
|
1 |
|
'entity' => $entity, |
50
|
1 |
|
'form' => $form->createView(), |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
/** |
55
|
|
|
* Displays a form to edit an existing table entity. |
56
|
|
|
*/ |
57
|
1 |
|
public function editAction(Request $request, $id) |
58
|
|
|
{ |
59
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
60
|
1 |
|
$this->setup($request); |
61
|
1 |
|
$namespace = $this->getNamespace(); |
|
|
|
|
62
|
1 |
|
$bundle = $this->getBundle(); |
|
|
|
|
63
|
1 |
|
$controller = $this->getController(); |
64
|
|
|
|
65
|
1 |
|
if (!self::$canUpdate) { |
66
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto"); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
70
|
1 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
71
|
1 |
|
$formType = $formbundle . 'Type'; |
|
|
|
|
72
|
|
|
|
73
|
1 |
|
$elencomodifiche = $this->elencoModifiche($controller, $id); |
74
|
|
|
|
75
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
76
|
|
|
|
77
|
1 |
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
78
|
|
|
|
79
|
1 |
|
if (!$entity) { |
80
|
|
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$editForm = $this->createForm( |
84
|
1 |
|
$formType, |
85
|
1 |
|
$entity, |
86
|
1 |
|
array('entity_manager' => $em, 'attr' => array( |
87
|
1 |
|
'id' => 'formdati' . $controller, |
88
|
|
|
), |
89
|
1 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
|
93
|
1 |
|
$deleteForm = $this->createDeleteForm($id); |
94
|
|
|
|
95
|
1 |
|
return $this->render( |
96
|
1 |
|
$nomebundle . ':' . $controller . ':edit.html.twig', |
97
|
|
|
array( |
98
|
1 |
|
'entity' => $entity, |
99
|
1 |
|
'nomecontroller' => $controller, |
100
|
1 |
|
'edit_form' => $editForm->createView(), |
101
|
1 |
|
'delete_form' => $deleteForm->createView(), |
102
|
1 |
|
'elencomodifiche' => $elencomodifiche, |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
/** |
107
|
|
|
* Creates a new table entity. |
108
|
|
|
*/ |
109
|
1 |
|
public function createAction(Request $request) |
110
|
|
|
{ |
111
|
1 |
|
$this->setup($request); |
112
|
1 |
|
$namespace = $this->getNamespace(); |
|
|
|
|
113
|
1 |
|
$bundle = $this->getBundle(); |
|
|
|
|
114
|
1 |
|
$controller = $this->getController(); |
115
|
|
|
|
116
|
1 |
|
if (!self::$canCreate) { |
117
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
|
|
|
121
|
1 |
|
$classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
122
|
1 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
|
|
|
|
123
|
|
|
|
124
|
1 |
|
$entity = new $classbundle(); |
|
|
|
|
125
|
1 |
|
$formType = $formbundle . 'Type'; |
126
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
127
|
1 |
|
$form = $this->createForm( |
|
|
|
|
128
|
1 |
|
$formType, |
129
|
1 |
|
$entity, |
130
|
1 |
|
array('entity_manager' => $em, 'attr' => array( |
131
|
1 |
|
'id' => 'formdati' . $controller, |
132
|
|
|
), |
133
|
1 |
|
'action' => $this->generateUrl($controller . '_create'), |
134
|
|
|
) |
135
|
|
|
); |
136
|
|
|
|
137
|
1 |
|
$form->submit($request->request->get($form->getName())); |
138
|
|
|
|
139
|
1 |
|
if ($form->isValid()) { |
140
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
141
|
1 |
|
$em->persist($entity); |
142
|
1 |
|
$em->flush(); |
143
|
1 |
|
$em->getConfiguration()->getResultCacheImpl()->delete($controller); |
144
|
|
|
|
145
|
1 |
|
$continua = (int) $request->get('continua'); |
146
|
1 |
|
if ($continua === 0) { |
147
|
1 |
|
return new Response('OK'); |
148
|
|
|
} else { |
149
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $entity->getId()))); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this->render( |
154
|
|
|
$nomebundle . ':' . $controller . ':new.html.twig', |
155
|
|
|
array( |
156
|
|
|
'nomecontroller' => $controller, |
157
|
|
|
'entity' => $entity, |
158
|
|
|
'form' => $form->createView(), |
159
|
|
|
) |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
/** |
163
|
|
|
* Edits an existing table entity. |
164
|
|
|
*/ |
165
|
1 |
|
public function updateAction(Request $request, $id) |
166
|
|
|
{ |
167
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
168
|
1 |
|
$this->setup($request); |
169
|
1 |
|
$namespace = $this->getNamespace(); |
|
|
|
|
170
|
1 |
|
$bundle = $this->getBundle(); |
|
|
|
|
171
|
1 |
|
$controller = $this->getController(); |
172
|
|
|
|
173
|
1 |
|
if (!self::$canUpdate) { |
174
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per aggiornare questo contenuto"); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
178
|
1 |
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
179
|
1 |
|
$formType = $formbundle . 'Type'; |
|
|
|
|
180
|
|
|
|
181
|
1 |
|
$repoStorico = $this->container->get('Storicomodifiche_repository'); |
182
|
|
|
|
183
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
184
|
|
|
|
185
|
1 |
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
186
|
|
|
|
187
|
1 |
|
if (!$entity) { |
188
|
|
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
$deleteForm = $this->createDeleteForm($id); |
192
|
|
|
|
193
|
1 |
|
$editForm = $this->createForm( |
194
|
1 |
|
$formType, |
195
|
1 |
|
$entity, |
196
|
1 |
|
array('entity_manager' => $em, 'attr' => array( |
197
|
1 |
|
'id' => 'formdati' . $controller, |
198
|
|
|
), |
199
|
1 |
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
200
|
|
|
) |
201
|
|
|
); |
202
|
|
|
|
203
|
1 |
|
$editForm->submit($request->request->get($editForm->getName())); |
204
|
|
|
|
205
|
1 |
|
if ($editForm->isValid()) { |
206
|
1 |
|
$originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
207
|
|
|
|
208
|
1 |
|
$em->persist($entity); |
209
|
1 |
|
$em->flush(); |
210
|
1 |
|
$em->getConfiguration()->getResultCacheImpl()->delete($controller); |
211
|
|
|
|
212
|
1 |
|
$newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
213
|
1 |
|
$changes = $repoStorico->isRecordChanged($nomebundle, $controller, $originalData, $newData); |
214
|
|
|
|
215
|
1 |
|
if ($changes) { |
216
|
|
|
$repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
217
|
|
|
} |
218
|
|
|
|
219
|
1 |
|
$continua = (int) $request->get('continua'); |
220
|
1 |
|
if ($continua === 0) { |
221
|
1 |
|
return new Response('OK'); |
222
|
|
|
} else { |
223
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $this->render( |
228
|
|
|
$nomebundle . ':' . $controller . ':edit.html.twig', |
229
|
|
|
array( |
230
|
|
|
'entity' => $entity, |
231
|
|
|
'edit_form' => $editForm->createView(), |
232
|
|
|
'delete_form' => $deleteForm->createView(), |
233
|
|
|
'nomecontroller' => $controller, |
234
|
|
|
) |
235
|
|
|
); |
236
|
|
|
} |
237
|
|
|
/** |
238
|
|
|
* Deletes a table entity. |
239
|
|
|
*/ |
240
|
1 |
|
public function deleteAction(Request $request) |
241
|
|
|
{ |
242
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
243
|
1 |
|
$this->setup($request); |
244
|
1 |
|
if (!self::$canDelete) { |
245
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per aggiornare questo contenuto"); |
|
|
|
|
246
|
|
|
} |
247
|
1 |
|
$namespace = $this->getNamespace(); |
|
|
|
|
248
|
1 |
|
$bundle = $this->getBundle(); |
|
|
|
|
249
|
1 |
|
$controller = $this->getController(); |
250
|
|
|
|
251
|
1 |
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
252
|
|
|
|
253
|
|
|
try { |
254
|
1 |
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
255
|
1 |
|
$qb = $em->createQueryBuilder(); |
|
|
|
|
256
|
1 |
|
$ids = explode(', ', $request->get('id')); |
257
|
1 |
|
$qb->delete($nomebundle . ':' . $controller, 'u') |
258
|
1 |
|
->andWhere('u.id IN (:ids)') |
259
|
1 |
|
->setParameter('ids', $ids); |
260
|
|
|
|
261
|
1 |
|
$query = $qb->getQuery(); |
262
|
1 |
|
$query->execute(); |
263
|
1 |
|
$em->getConfiguration()->getResultCacheImpl()->delete($controller); |
264
|
|
|
} catch (\Exception $e) { |
265
|
|
|
$response = new Response(); |
266
|
|
|
$response->setStatusCode('200'); |
267
|
|
|
|
268
|
|
|
return new Response('404'); |
269
|
|
|
} |
270
|
|
|
|
271
|
1 |
|
return new Response('OK'); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
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.