Completed
Push — master ( 6cfad8...0e4f31 )
by Andrea
08:49
created

FiController::indexAction()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 0
cts 28
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 29
nc 4
nop 1
crap 12
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 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)
20
    {
21 4
        $matches = array();
22 4
        $controllo = new \ReflectionClass(get_class($this));
23
24 4
        preg_match('/(.*)\\\(.*)Bundle\\\Controller\\\(.*)Controller/', $controllo->name, $matches);
25
26 4
        self::$namespace = $matches[1];
27 4
        self::$bundle = $matches[2];
28 4
        self::$controller = $matches[3];
29 4
        self::$action = substr($request->attributes->get('_controller'), strrpos($request->attributes->get('_controller'), ':') + 1);
30 4
    }
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)
106
    {
107 1
        $this->setParametriGriglia(array('request' => $request));
108 1
        $paricevuti = self::$parametrigriglia;
109
110 1
        return new Response(Griglia::datiPerGriglia($paricevuti));
111
    }
112
113
    /**
114
     * Creates a new table entity.
115
     */
116 1
    public function createAction(Request $request)
117
    {
118 1
        $this->setup($request);
119 1
        $namespace = $this->getNamespace();
120 1
        $bundle = $this->getBundle();
121 1
        $controller = $this->getController();
122
123 1
        $nomebundle = $namespace . $bundle . 'Bundle';
124 1
        $classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller;
125 1
        $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller;
126
127 1
        $entity = new $classbundle();
128 1
        $formType = $formbundle . 'Type';
129
130 1
        $form = $this->createForm(
131 1
            $formType,
132 1
            $entity,
133
            array('attr' => array(
134 1
                'id' => 'formdati' . $controller,
135
                ),
136 1
                'action' => $this->generateUrl($controller . '_create'),
137
                )
138
        );
139
140 1
        $form->submit($request->request->get($form->getName()));
141
142 1
        if ($form->isValid()) {
143 1
            $em = $this->getDoctrine()->getManager();
144 1
            $em->persist($entity);
145 1
            $em->flush();
146
147 1
            $continua = $request->get('continua');
148 1
            if ($continua == 0) {
149 1
                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 1
    public function newAction(Request $request)
169
    {
170 1
        $this->setup($request);
171 1
        $namespace = $this->getNamespace();
172 1
        $bundle = $this->getBundle();
173 1
        $controller = $this->getController();
174
175 1
        $nomebundle = $namespace . $bundle . 'Bundle';
176 1
        $classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller;
177 1
        $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller;
178 1
        $formType = $formbundle . 'Type';
179
180 1
        $entity = new $classbundle();
181
182 1
        $form = $this->createForm(
183 1
            $formType,
184 1
            $entity,
185
            array('attr' => array(
186 1
                'id' => 'formdati' . $controller,
187
                ),
188 1
                'action' => $this->generateUrl($controller . '_create'),
189
                )
190
        );
191
192 1
        return $this->render(
193 1
            $nomebundle . ':' . $controller . ':new.html.twig',
194
            array(
195 1
                    'nomecontroller' => $controller,
196 1
                    'entity' => $entity,
197 1
                    'form' => $form->createView(),
198
                        )
199
        );
200
    }
201
202
    protected function elencoModifiche($nomebundle, $controller, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $nomebundle is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 1
    public function editAction(Request $request, $id)
220
    {
221
        /* @var $em \Doctrine\ORM\EntityManager */
222 1
        $this->setup($request);
223 1
        $namespace = $this->getNamespace();
224 1
        $bundle = $this->getBundle();
225 1
        $controller = $this->getController();
226
227 1
        $nomebundle = $namespace . $bundle . 'Bundle';
228 1
        $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller;
229 1
        $formType = $formbundle . 'Type';
230
231 1
        $elencomodifiche = $this->elencoModifiche($nomebundle, $controller, $id);
232
233 1
        $em = $this->getDoctrine()->getManager();
234
235 1
        $entity = $em->getRepository($nomebundle . ':' . $controller)->find($id);
236
237 1
        if (!$entity) {
238
            throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.');
239
        }
240
241 1
        $editForm = $this->createForm(
242 1
            $formType,
243 1
            $entity,
244
            array('attr' => array(
245 1
                'id' => 'formdati' . $controller,
246
                ),
247 1
                'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())),
248
                )
249
        );
250
251 1
        $deleteForm = $this->createDeleteForm($id);
252
253 1
        return $this->render(
254 1
            $nomebundle . ':' . $controller . ':edit.html.twig',
255
            array(
256 1
                    'entity' => $entity,
257 1
                    'nomecontroller' => $controller,
258 1
                    'edit_form' => $editForm->createView(),
259 1
                    'delete_form' => $deleteForm->createView(),
260 1
                    'elencomodifiche' => $elencomodifiche,
261
                        )
262
        );
263
    }
264
265
    /**
266
     * Edits an existing table entity.
267
     */
268 1
    public function updateAction(Request $request, $id)
269
    {
270
        /* @var $em \Doctrine\ORM\EntityManager */
271 1
        $this->setup($request);
272 1
        $namespace = $this->getNamespace();
273 1
        $bundle = $this->getBundle();
274 1
        $controller = $this->getController();
275
276 1
        $nomebundle = $namespace . $bundle . 'Bundle';
277 1
        $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller;
278 1
        $formType = $formbundle . 'Type';
279
280 1
        $repoStorico = $this->container->get('Storicomodifiche_repository');
281
282 1
        $em = $this->getDoctrine()->getManager();
283
284 1
        $entity = $em->getRepository($nomebundle . ':' . $controller)->find($id);
285
286 1
        if (!$entity) {
287
            throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.');
288
        }
289
290 1
        $deleteForm = $this->createDeleteForm($id);
291
292 1
        $editForm = $this->createForm(
293 1
            $formType,
294 1
            $entity,
295
            array('attr' => array(
296 1
                'id' => 'formdati' . $controller,
297
                ),
298 1
                'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())),
299
                )
300
        );
301
302 1
        $editForm->submit($request->request->get($editForm->getName()));
303
304 1
        if ($editForm->isValid()) {
305 1
            $originalData = $em->getUnitOfWork()->getOriginalEntityData($entity);
306
307 1
            $em->persist($entity);
308 1
            $em->flush();
309
310 1
            $newData = $em->getUnitOfWork()->getOriginalEntityData($entity);
311 1
            $changes = $repoStorico->isRecordChanged($nomebundle, $controller, $originalData, $newData);
312
313 1
            if ($changes) {
314
                $repoStorico->saveHistory($controller, $changes, $id, $this->getUser());
315
            }
316
317 1
            $continua = $request->get('continua');
318 1
            if ($continua == 0) {
319 1
                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 1
    public function deleteAction(Request $request)
369
    {
370
        /* @var $em \Doctrine\ORM\EntityManager */
371 1
        $this->setup($request);
372 1
        $namespace = $this->getNamespace();
373 1
        $bundle = $this->getBundle();
374 1
        $controller = $this->getController();
375
376 1
        $nomebundle = $namespace . $bundle . 'Bundle';
377
378
        //if (!$request->isXmlHttpRequest()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
379
        //    $request->checkCSRFProtection();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
380
        //}
381
        try {
382 1
            $em = $this->getDoctrine()->getManager();
383 1
            $qb = $em->createQueryBuilder();
384 1
            $ids = explode(',', $request->get('id'));
385 1
            $qb->delete($nomebundle . ':' . $controller, 'u')
386 1
                    ->andWhere('u.id IN (:ids)')
387 1
                    ->setParameter('ids', $ids);
388
389 1
            $query = $qb->getQuery();
390 1
            $query->execute();
391
        } catch (\Exception $e) {
392
            $response = new Response();
393
            $response->setStatusCode('200');
394
395
            return new Response('404');
396
        }
397
398 1
        return new Response('OK');
399
    }
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)
409
    {
410
        return $this->createFormBuilder(array('id' => $id))
411
                        ->add('id', get_class(new \Symfony\Component\Form\Extension\Core\Type\HiddenType()))
412
                        ->getForm();
413
    }
414
415
    protected function getParametersTestataPerGriglia($request, $container, $em, $paricevuti)
416
    {
417
        $parametritestarequest = $request->get('parametritesta');
418
        $parametritesta = array();
419
        if ($parametritestarequest) {
420
            $jsonparms = json_decode($parametritestarequest);
421
            $parametritesta = get_object_vars($jsonparms);
422
            $parametritesta['container'] = $container;
423
            $parametritesta['doctrine'] = $em;
424
            $parametritesta['request'] = $request;
425
            $parametritesta['output'] = 'stampa';
426
        }
427
428
        return $parametritestarequest ? $parametritesta : $paricevuti;
429
    }
430
431
    protected function getParametersDatiPerGriglia($request, $container, $em, $paricevuti)
432
    {
433
        $parametrigriglia = $request->get('parametrigriglia');
434
        if ($parametrigriglia) {
435
            $jsonparms = json_decode($parametrigriglia);
436
            $parametrigriglia = get_object_vars($jsonparms);
437
            $parametrigriglia['container'] = $container;
438
            $parametrigriglia['doctrine'] = $em;
439
            $parametrigriglia['request'] = $request;
440
            $parametrigriglia['output'] = 'stampa';
441
        }
442
443
        return $parametrigriglia ? $parametrigriglia : $paricevuti;
444
    }
445
446 4
    protected function getNamespace()
447
    {
448 4
        return self::$namespace;
449
    }
450
451 4
    protected function getBundle()
452
    {
453 4
        return self::$bundle;
454
    }
455
456 4
    protected function getController()
457
    {
458 4
        return self::$controller;
459
    }
460
461
    protected function getAction()
462
    {
463
        return self::$action;
464
    }
465
}
466