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
|
|
|
/** |
16
|
|
|
* Creates a new table entity. |
17
|
|
|
*/ |
18
|
|
|
public function createAction(Request $request) |
19
|
|
|
{ |
20
|
|
|
$this->setup($request); |
21
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
22
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
23
|
|
|
$controller = $this->getController(); |
24
|
|
|
|
25
|
|
|
if (!self::$canCreate) { |
26
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto"); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
|
|
|
|
30
|
|
|
$classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
31
|
|
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
|
|
|
|
32
|
|
|
|
33
|
|
|
$entity = new $classbundle(); |
|
|
|
|
34
|
|
|
$formType = $formbundle . 'Type'; |
35
|
|
|
|
36
|
|
|
$form = $this->createForm( |
37
|
|
|
$formType, |
38
|
|
|
$entity, |
39
|
|
|
array('attr' => array( |
40
|
|
|
'id' => 'formdati' . $controller, |
41
|
|
|
), |
42
|
|
|
'action' => $this->generateUrl($controller . '_create'), |
43
|
|
|
) |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$form->submit($request->request->get($form->getName())); |
47
|
|
|
|
48
|
|
|
if ($form->isValid()) { |
49
|
|
|
$em = $this->getDoctrine()->getManager(); |
50
|
|
|
$em->persist($entity); |
51
|
|
|
$em->flush(); |
52
|
|
|
$em->getConfiguration()->getResultCacheImpl()->delete("menu"); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$continua = (int) $request->get('continua'); |
55
|
|
|
if ($continua === 0) { |
56
|
|
|
return new Response('OK'); |
57
|
|
|
} else { |
58
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $entity->getId()))); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->render( |
63
|
|
|
$nomebundle . ':' . $controller . ':new.html.twig', |
64
|
|
|
array( |
65
|
|
|
'nomecontroller' => $controller, |
66
|
|
|
'entity' => $entity, |
67
|
|
|
'form' => $form->createView(), |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Edits an existing table entity. |
74
|
|
|
*/ |
75
|
|
|
public function updateAction(Request $request, $id) |
76
|
|
|
{ |
77
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
78
|
|
|
$this->setup($request); |
79
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
80
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
81
|
|
|
$controller = $this->getController(); |
82
|
|
|
|
83
|
|
|
if (!self::$canUpdate) { |
84
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per aggiornare questo contenuto"); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
88
|
|
|
$formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
89
|
|
|
$formType = $formbundle . 'Type'; |
|
|
|
|
90
|
|
|
|
91
|
|
|
$repoStorico = $this->container->get('Storicomodifiche_repository'); |
92
|
|
|
|
93
|
|
|
$em = $this->getDoctrine()->getManager(); |
94
|
|
|
|
95
|
|
|
$entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
96
|
|
|
|
97
|
|
|
if (!$entity) { |
98
|
|
|
throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$deleteForm = $this->createDeleteForm($id); |
102
|
|
|
|
103
|
|
|
$editForm = $this->createForm( |
104
|
|
|
$formType, |
105
|
|
|
$entity, |
106
|
|
|
array('attr' => array( |
107
|
|
|
'id' => 'formdati' . $controller, |
108
|
|
|
), |
109
|
|
|
'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$editForm->submit($request->request->get($editForm->getName())); |
114
|
|
|
|
115
|
|
|
if ($editForm->isValid()) { |
116
|
|
|
$originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
117
|
|
|
|
118
|
|
|
$em->persist($entity); |
119
|
|
|
$em->flush(); |
120
|
|
|
$em->getConfiguration()->getResultCacheImpl()->delete("menu"); |
|
|
|
|
121
|
|
|
|
122
|
|
|
$newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
123
|
|
|
$changes = $repoStorico->isRecordChanged($nomebundle, $controller, $originalData, $newData); |
124
|
|
|
|
125
|
|
|
if ($changes) { |
126
|
|
|
$repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$continua = (int) $request->get('continua'); |
130
|
|
|
if ($continua === 0) { |
131
|
|
|
return new Response('OK'); |
132
|
|
|
} else { |
133
|
|
|
return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->render( |
138
|
|
|
$nomebundle . ':' . $controller . ':edit.html.twig', |
139
|
|
|
array( |
140
|
|
|
'entity' => $entity, |
141
|
|
|
'edit_form' => $editForm->createView(), |
142
|
|
|
'delete_form' => $deleteForm->createView(), |
143
|
|
|
'nomecontroller' => $controller, |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Deletes a table entity. |
150
|
|
|
*/ |
151
|
|
|
public function deleteAction(Request $request) |
152
|
|
|
{ |
153
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
154
|
|
|
$this->setup($request); |
155
|
|
|
if (!self::$canDelete) { |
156
|
|
|
throw new AccessDeniedException("Non si hanno i permessi per aggiornare questo contenuto"); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
$namespace = $this->getNamespace(); |
|
|
|
|
159
|
|
|
$bundle = $this->getBundle(); |
|
|
|
|
160
|
|
|
$controller = $this->getController(); |
161
|
|
|
|
162
|
|
|
$nomebundle = $namespace . $bundle . 'Bundle'; |
163
|
|
|
|
164
|
|
|
try { |
165
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
166
|
|
|
$qb = $em->createQueryBuilder(); |
|
|
|
|
167
|
|
|
$ids = explode(',', $request->get('id')); |
168
|
|
|
$qb->delete($nomebundle . ':' . $controller, 'u') |
169
|
|
|
->andWhere('u.id IN (:ids)') |
170
|
|
|
->setParameter('ids', $ids); |
171
|
|
|
|
172
|
|
|
$query = $qb->getQuery(); |
173
|
|
|
$query->execute(); |
174
|
|
|
$em->getConfiguration()->getResultCacheImpl()->delete("menu"); |
|
|
|
|
175
|
|
|
} catch (\Exception $e) { |
176
|
|
|
$response = new Response(); |
177
|
|
|
$response->setStatusCode('200'); |
178
|
|
|
|
179
|
|
|
return new Response('404'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return new Response('OK'); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
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.