Completed
Push — master ( d3a1aa...33af6b )
by Andrea
08:15
created

FiController::setParametriGriglia()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 16
cp 0.9375
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 15
nc 6
nop 1
crap 4.0039
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
    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)
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
    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)
369
    {
370
        /* @var $em \Doctrine\ORM\EntityManager */
371
        $this->setup($request);
372
        $namespace = $this->getNamespace();
373
        $bundle = $this->getBundle();
374
        $controller = $this->getController();
375
376
        $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
            $em = $this->getDoctrine()->getManager();
383
            $qb = $em->createQueryBuilder();
384
            $ids = explode(',', $request->get('id'));
385
            $qb->delete($nomebundle . ':' . $controller, 'u')
386
                    ->andWhere('u.id IN (:ids)')
387
                    ->setParameter('ids', $ids);
388
389
            $query = $qb->getQuery();
390
            $query->execute();
391
        } catch (\Exception $e) {
392
            $response = new Response();
393
            $response->setStatusCode('200');
394
395
            return new Response('404');
396
        }
397
398
        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
        if ($request->get('parametrigriglia')) {
434
            $jsonparms = json_decode($request->get('parametrigriglia'));
435
            $parametrigriglia = get_object_vars($jsonparms);
436
            $parametrigriglia['container'] = $container;
437
            $parametrigriglia['doctrine'] = $em;
438
            $parametrigriglia['request'] = $request;
439
            $parametrigriglia['output'] = 'stampa';
440
        }
441
442
        return $request->get('parametrigriglia') ? $parametrigriglia : $paricevuti;
0 ignored issues
show
Bug introduced by
The variable $parametrigriglia does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
443
    }
444
445 4
    protected function getNamespace()
446
    {
447 4
        return self::$namespace;
448
    }
449
450 4
    protected function getBundle()
451
    {
452 4
        return self::$bundle;
453
    }
454
455 4
    protected function getController()
456
    {
457 4
        return self::$controller;
458
    }
459
460
    protected function getAction()
461
    {
462
        return self::$action;
463
    }
464
}
465