1 | <?php |
||
10 | class FiController 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 | 4 | protected function setup(Request $request) |
|
31 | |||
32 | 1 | protected function setParametriGriglia($prepar = array()) |
|
33 | { |
||
34 | 1 | $this->setup($prepar['request']); |
|
35 | 1 | $namespace = $this->getNamespace(); |
|
36 | 1 | $bundle = $this->getBundle(); |
|
37 | 1 | $controller = $this->getController(); |
|
38 | |||
39 | 1 | $gestionepermessi = $this->get("ficorebundle.gestionepermessi"); |
|
40 | 1 | $canRead = ($gestionepermessi->leggere(array('modulo' => $controller)) ? 1 : 0); |
|
41 | 1 | if (!$canRead) { |
|
42 | throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto"); |
||
43 | } |
||
44 | |||
45 | 1 | $nomebundle = $namespace . $bundle . 'Bundle'; |
|
46 | 1 | $escludi = array(); |
|
47 | |||
48 | 1 | $paricevuti = array('container' => $this->container, 'nomebundle' => $nomebundle, 'nometabella' => $controller, 'escludere' => $escludi); |
|
49 | |||
50 | 1 | if (! empty($prepar)) { |
|
51 | 1 | $paricevuti = array_merge($paricevuti, $prepar); |
|
52 | } |
||
53 | |||
54 | 1 | self::$parametrigriglia = $paricevuti; |
|
55 | 1 | } |
|
56 | |||
57 | /** |
||
58 | * Lists all tables entities. |
||
59 | */ |
||
60 | public function indexAction(Request $request) |
||
61 | { |
||
62 | /* @var $em \Doctrine\ORM\EntityManager */ |
||
63 | $this->setup($request); |
||
64 | $namespace = $this->getNamespace(); |
||
65 | $bundle = $this->getBundle(); |
||
66 | $controller = $this->getController(); |
||
67 | $container = $this->container; |
||
68 | |||
69 | $gestionepermessi = $this->get('ficorebundle.gestionepermessi'); |
||
70 | $canRead = ($gestionepermessi->leggere(array('modulo' => $controller)) ? 1 : 0); |
||
71 | if (!$canRead) { |
||
72 | throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto"); |
||
73 | } |
||
74 | $idpassato = $request->get('id'); |
||
75 | |||
76 | $nomebundle = $namespace . $bundle . 'Bundle'; |
||
77 | |||
78 | $repotabelle = $this->get('OpzioniTabella_repository'); |
||
79 | |||
80 | $paricevuti = array('nomebundle' => $nomebundle, 'nometabella' => $controller, 'container' => $container); |
||
81 | |||
82 | $testatagriglia = Griglia::testataPerGriglia($paricevuti); |
||
83 | |||
84 | $testatagriglia['multisearch'] = 1; |
||
85 | $testatagriglia['showconfig'] = 1; |
||
86 | $testatagriglia['overlayopen'] = 1; |
||
87 | |||
88 | $testatagriglia['parametritesta'] = json_encode($paricevuti); |
||
89 | |||
90 | $this->setParametriGriglia(array('request' => $request)); |
||
91 | $testatagriglia['parametrigriglia'] = json_encode(self::$parametrigriglia); |
||
92 | |||
93 | $testata = $repotabelle->editTestataFormTabelle($testatagriglia, $controller, $container); |
||
94 | return $this->render( |
||
95 | $nomebundle . ':' . $controller . ':index.html.twig', |
||
96 | array( |
||
97 | 'nomecontroller' => $controller, |
||
98 | 'testata' => $testata, |
||
99 | 'canread' => $canRead, |
||
100 | 'idpassato' => $idpassato, |
||
101 | ) |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | 1 | public function grigliaAction(Request $request) |
|
112 | |||
113 | /** |
||
114 | * Creates a new table entity. |
||
115 | */ |
||
116 | public function createAction(Request $request) |
||
117 | { |
||
118 | $this->setup($request); |
||
119 | $namespace = $this->getNamespace(); |
||
120 | $bundle = $this->getBundle(); |
||
121 | $controller = $this->getController(); |
||
122 | |||
123 | $nomebundle = $namespace . $bundle . 'Bundle'; |
||
124 | $classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
||
125 | $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
||
126 | |||
127 | $entity = new $classbundle(); |
||
128 | $formType = $formbundle . 'Type'; |
||
129 | |||
130 | $form = $this->createForm( |
||
131 | $formType, |
||
132 | $entity, |
||
133 | array('attr' => array( |
||
134 | 'id' => 'formdati' . $controller, |
||
135 | ), |
||
136 | 'action' => $this->generateUrl($controller . '_create'), |
||
137 | ) |
||
138 | ); |
||
139 | |||
140 | $form->submit($request->request->get($form->getName())); |
||
141 | |||
142 | if ($form->isValid()) { |
||
143 | $em = $this->getDoctrine()->getManager(); |
||
144 | $em->persist($entity); |
||
145 | $em->flush(); |
||
146 | |||
147 | $continua = $request->get('continua'); |
||
148 | if ($continua == 0) { |
||
149 | return new Response('OK'); |
||
150 | } else { |
||
151 | return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $entity->getId()))); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | return $this->render( |
||
156 | $nomebundle . ':' . $controller . ':new.html.twig', |
||
157 | array( |
||
158 | 'nomecontroller' => $controller, |
||
159 | 'entity' => $entity, |
||
160 | 'form' => $form->createView(), |
||
161 | ) |
||
162 | ); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Displays a form to create a new table entity. |
||
167 | */ |
||
168 | public function newAction(Request $request) |
||
169 | { |
||
170 | $this->setup($request); |
||
171 | $namespace = $this->getNamespace(); |
||
172 | $bundle = $this->getBundle(); |
||
173 | $controller = $this->getController(); |
||
174 | |||
175 | $nomebundle = $namespace . $bundle . 'Bundle'; |
||
176 | $classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller; |
||
177 | $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
||
178 | $formType = $formbundle . 'Type'; |
||
179 | |||
180 | $entity = new $classbundle(); |
||
181 | |||
182 | $form = $this->createForm( |
||
183 | $formType, |
||
184 | $entity, |
||
185 | array('attr' => array( |
||
186 | 'id' => 'formdati' . $controller, |
||
187 | ), |
||
188 | 'action' => $this->generateUrl($controller . '_create'), |
||
189 | ) |
||
190 | ); |
||
191 | |||
192 | return $this->render( |
||
193 | $nomebundle . ':' . $controller . ':new.html.twig', |
||
194 | array( |
||
195 | 'nomecontroller' => $controller, |
||
196 | 'entity' => $entity, |
||
197 | 'form' => $form->createView(), |
||
198 | ) |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | protected function elencoModifiche($nomebundle, $controller, $id) |
||
203 | { |
||
204 | $controllerStorico = 'Storicomodifiche'; |
||
205 | $em = $this->getDoctrine()->getManager(); |
||
206 | $risultato = $em->getRepository('FiCoreBundle:' . $controllerStorico)->findBy( |
||
207 | array( |
||
208 | 'nometabella' => $controller, |
||
209 | 'idtabella' => $id, |
||
210 | ) |
||
211 | ); |
||
212 | |||
213 | return $risultato; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Displays a form to edit an existing table entity. |
||
218 | */ |
||
219 | public function editAction(Request $request, $id) |
||
220 | { |
||
221 | /* @var $em \Doctrine\ORM\EntityManager */ |
||
222 | $this->setup($request); |
||
223 | $namespace = $this->getNamespace(); |
||
224 | $bundle = $this->getBundle(); |
||
225 | $controller = $this->getController(); |
||
226 | |||
227 | $nomebundle = $namespace . $bundle . 'Bundle'; |
||
228 | $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
||
229 | $formType = $formbundle . 'Type'; |
||
230 | |||
231 | $elencomodifiche = $this->elencoModifiche($nomebundle, $controller, $id); |
||
232 | |||
233 | $em = $this->getDoctrine()->getManager(); |
||
234 | |||
235 | $entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
||
236 | |||
237 | if (!$entity) { |
||
238 | throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
||
239 | } |
||
240 | |||
241 | $editForm = $this->createForm( |
||
242 | $formType, |
||
243 | $entity, |
||
244 | array('attr' => array( |
||
245 | 'id' => 'formdati' . $controller, |
||
246 | ), |
||
247 | 'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
||
248 | ) |
||
249 | ); |
||
250 | |||
251 | $deleteForm = $this->createDeleteForm($id); |
||
252 | |||
253 | return $this->render( |
||
254 | $nomebundle . ':' . $controller . ':edit.html.twig', |
||
255 | array( |
||
256 | 'entity' => $entity, |
||
257 | 'nomecontroller' => $controller, |
||
258 | 'edit_form' => $editForm->createView(), |
||
259 | 'delete_form' => $deleteForm->createView(), |
||
260 | 'elencomodifiche' => $elencomodifiche, |
||
261 | ) |
||
262 | ); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Edits an existing table entity. |
||
267 | */ |
||
268 | public function updateAction(Request $request, $id) |
||
269 | { |
||
270 | /* @var $em \Doctrine\ORM\EntityManager */ |
||
271 | $this->setup($request); |
||
272 | $namespace = $this->getNamespace(); |
||
273 | $bundle = $this->getBundle(); |
||
274 | $controller = $this->getController(); |
||
275 | |||
276 | $nomebundle = $namespace . $bundle . 'Bundle'; |
||
277 | $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller; |
||
278 | $formType = $formbundle . 'Type'; |
||
279 | |||
280 | $repoStorico = $this->container->get('Storicomodifiche_repository'); |
||
281 | |||
282 | $em = $this->getDoctrine()->getManager(); |
||
283 | |||
284 | $entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
||
285 | |||
286 | if (!$entity) { |
||
287 | throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
||
288 | } |
||
289 | |||
290 | $deleteForm = $this->createDeleteForm($id); |
||
291 | |||
292 | $editForm = $this->createForm( |
||
293 | $formType, |
||
294 | $entity, |
||
295 | array('attr' => array( |
||
296 | 'id' => 'formdati' . $controller, |
||
297 | ), |
||
298 | 'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())), |
||
299 | ) |
||
300 | ); |
||
301 | |||
302 | $editForm->submit($request->request->get($editForm->getName())); |
||
303 | |||
304 | if ($editForm->isValid()) { |
||
305 | $originalData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
||
306 | |||
307 | $em->persist($entity); |
||
308 | $em->flush(); |
||
309 | |||
310 | $newData = $em->getUnitOfWork()->getOriginalEntityData($entity); |
||
311 | $changes = $repoStorico->isRecordChanged($nomebundle, $controller, $originalData, $newData); |
||
312 | |||
313 | if ($changes) { |
||
314 | $repoStorico->saveHistory($controller, $changes, $id, $this->getUser()); |
||
315 | } |
||
316 | |||
317 | $continua = $request->get('continua'); |
||
318 | if ($continua == 0) { |
||
319 | return new Response('OK'); |
||
320 | } else { |
||
321 | return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id))); |
||
322 | } |
||
323 | } |
||
324 | |||
325 | return $this->render( |
||
326 | $nomebundle . ':' . $controller . ':edit.html.twig', |
||
327 | array( |
||
328 | 'entity' => $entity, |
||
329 | 'edit_form' => $editForm->createView(), |
||
330 | 'delete_form' => $deleteForm->createView(), |
||
331 | 'nomecontroller' => $controller, |
||
332 | ) |
||
333 | ); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Edits an existing table entity. |
||
338 | */ |
||
339 | public function aggiornaAction(Request $request) |
||
340 | { |
||
341 | /* @var $em \Doctrine\ORM\EntityManager */ |
||
342 | $this->setup($request); |
||
343 | $namespace = $this->getNamespace(); |
||
344 | $bundle = $this->getBundle(); |
||
345 | $controller = $this->getController(); |
||
346 | |||
347 | $nomebundle = $namespace . $bundle . 'Bundle'; |
||
348 | |||
349 | $id = $this->get('request')->request->get('id'); |
||
350 | |||
351 | $em = $this->getDoctrine()->getManager(); |
||
352 | |||
353 | $entity = $em->getRepository($nomebundle . ':' . $controller)->find($id); |
||
354 | |||
355 | if (!$entity) { |
||
356 | throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.'); |
||
357 | } |
||
358 | |||
359 | throw $this->createNotFoundException("Implementare a seconda dell'esigenza 'aggiornaAction' del controller " |
||
360 | . $nomebundle |
||
361 | . '/' |
||
362 | . $controller); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Deletes a table entity. |
||
367 | */ |
||
368 | public function deleteAction(Request $request) |
||
400 | |||
401 | /** |
||
402 | * Creates a form to delete a table entity by id. |
||
403 | * |
||
404 | * @param mixed $id The entity id |
||
405 | * |
||
406 | * @return \Symfony\Component\Form\Form The form |
||
407 | */ |
||
408 | protected function createDeleteForm($id) |
||
414 | |||
415 | protected function getParametersTestataPerGriglia($request, $container, $em, $paricevuti) |
||
416 | { |
||
417 | if ($request->get('parametritesta')) { |
||
418 | $jsonparms = json_decode($request->get('parametritesta')); |
||
419 | $parametritesta = get_object_vars($jsonparms); |
||
420 | $parametritesta['container'] = $container; |
||
421 | $parametritesta['doctrine'] = $em; |
||
422 | $parametritesta['request'] = $request; |
||
423 | $parametritesta['output'] = 'stampa'; |
||
424 | } |
||
425 | |||
426 | return $request->get('parametritesta') ? $parametritesta : $paricevuti; |
||
427 | } |
||
428 | |||
429 | protected function getParametersDatiPerGriglia($request, $container, $em, $paricevuti) |
||
430 | { |
||
431 | if ($request->get('parametrigriglia')) { |
||
432 | $jsonparms = json_decode($request->get('parametrigriglia')); |
||
433 | $parametrigriglia = get_object_vars($jsonparms); |
||
434 | $parametrigriglia['container'] = $container; |
||
435 | $parametrigriglia['doctrine'] = $em; |
||
436 | $parametrigriglia['request'] = $request; |
||
437 | $parametrigriglia['output'] = 'stampa'; |
||
438 | } |
||
439 | |||
440 | return $request->get('parametrigriglia') ? $parametrigriglia : $paricevuti; |
||
441 | } |
||
442 | |||
443 | 4 | protected function getNamespace() |
|
447 | |||
448 | 4 | protected function getBundle() |
|
452 | |||
453 | 4 | protected function getController() |
|
457 | |||
458 | protected function getAction() |
||
462 | } |
||
463 |