Completed
Push — master ( b572ab...c6735e )
by Andrea
08:40
created

TabelleController::scriviDB()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 10
nc 5
nop 4
crap 4
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\HttpFoundation\JsonResponse;
8
use Fi\CoreBundle\Entity\Tabelle;
9
10
/**
11
 * Tabelle controller.
12
 */
13
class TabelleController extends FiCoreController
14
{
15
16
    public function aggiornaAction(Request $request)
17
    {
18
        if ($request->get('oper') == 'edit') {
19
            $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
20
            $operatore = $gestionepermessi->utentecorrente();
21
22
            $id = $request->get('id');
23
24
            $em = $this->getDoctrine()->getManager();
25
            $tabelle = $em->getRepository('FiCoreBundle:Tabelle')->find($id);
26
            if (!$tabelle) {
27
                throw new AccessDeniedException("Oggetto non trovato");
28
            }
29
            $tabelle->setOperatoriId($operatore['id']);
30
            $nometabella = $this->getRequestValue($request, 'nometabella');
31
            if ($nometabella) {
32
                $tabelle->setNometabella($nometabella);
33
            }
34
            $nomecampo = $this->getRequestValue($request, 'nomecampo');
35
            if ($nomecampo) {
36
                $tabelle->setNomecampo($nomecampo);
37
            }
38
            $mostraindex = $this->getRequestValue($request, 'mostraindex');
39
            $tabelle->setMostraindex($mostraindex);
40
            $ordineindex = $this->getRequestValue($request, 'ordineindex');
41
            $tabelle->setOrdineindex($ordineindex);
42
            $etichettaindex = $this->getRequestValue($request, 'etichettaindex');
43
            $tabelle->setEtichettaindex($etichettaindex);
44
45
            $larghezzaindex = $this->getRequestValue($request, 'larghezzaindex');
46
            $tabelle->setLarghezzaindex($larghezzaindex);
47
48
            $mostrastampa = $this->getRequestValue($request, 'mostrastampa');
49
            $tabelle->setMostrastampa($mostrastampa);
50
51
            $ordinestampa = $this->getRequestValue($request, 'ordinestampa');
52
            $tabelle->setOrdinestampa($ordinestampa);
53
54
            $etichettastampa = $this->getRequestValue($request, 'etichettastampa');
55
            $tabelle->setEtichettastampa($etichettastampa);
56
57
            $larghezzastampa = $this->getRequestValue($request, 'larghezzastampa');
58
            $tabelle->setLarghezzastampa($larghezzastampa);
59
            $em->persist($tabelle);
60
            $em->flush();
61
        }
62
63
        return new Response('OK');
64
    }
65
66
    private function getRequestValue($request, $attribute)
67
    {
68
        if (($request->get($attribute) !== null) && ($request->get($attribute) !== '')) {
69
            return $request->get($attribute);
70
        } else {
71
            return null;
72
        }
73
    }
74
75 1
    public function configuraAction(Request $request, $nometabella)
76
    {
77 1
        $this->setup($request);
78 1
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
79 1
        $operatore = $gestionepermessi->utentecorrente();
80 1
        $this->generaDB(array('tabella' => $nometabella), $request);
81 1
        $this->generaDB(array('tabella' => $nometabella, 'operatore' => $operatore['id']), $request);
82
83 1
        $namespace = $this->getNamespace();
84 1
        $bundle = $this->getBundle();
85 1
        $controller = $this->getController();
86 1
        $container = $this->container;
87
88 1
        $nomebundle = $namespace . $bundle . 'Bundle';
89
90 1
        $em = $this->getDoctrine()->getManager();
91 1
        $entities = $em->getRepository($nomebundle . ':' . $controller)->findAll();
92
93
        $dettaglij = array(
94 1
            'nomecampo' => array(
95
                array('nomecampo' => 'nomecampo', 'lunghezza' => '150', 'descrizione' => 'Campo', 'tipo' => 'text', 'editable' => false),),
96
            'mostraindex' => array(
97
                array('nomecampo' => 'mostraindex', 'lunghezza' => '100', 'descrizione' => 'Vedi in griglia', 'tipo' => 'boolean'),
98
            ),
99
            'ordineindex' => array(
100
                array('nomecampo' => 'ordineindex', 'lunghezza' => '100', 'descrizione' => 'Ordine in griglia', 'tipo' => 'text'),
101
            ),
102
            'etichettaindex' => array(
103
                array('nomecampo' => 'etichettaindex', 'lunghezza' => '150', 'descrizione' => 'Label in griglia', 'tipo' => 'text'),
104
            ),
105
            'larghezzaindex' => array(
106
                array('nomecampo' => 'larghezzaindex', 'lunghezza' => '100', 'descrizione' => 'Largh. in griglia', 'tipo' => 'text'),
107
            ),
108
            'mostrastampa' => array(
109
                array('nomecampo' => 'mostrastampa', 'lunghezza' => '100', 'descrizione' => 'Vedi in stampa', 'tipo' => 'boolean'),
110
            ),
111
            'ordinestampa' => array(
112
                array('nomecampo' => 'ordinestampa', 'lunghezza' => '100', 'descrizione' => 'Ordine in stampa', 'tipo' => 'text'),
113
            ),
114
            'etichettastampa' => array(
115
                array('nomecampo' => 'etichettastampa', 'lunghezza' => '150', 'descrizione' => 'Label in stampa', 'tipo' => 'text'),
116
            ),
117
            'larghezzastampa' => array(
118
                array('nomecampo' => 'larghezzastampa', 'lunghezza' => '100', 'descrizione' => 'Largh. in stampa', 'tipo' => 'text'),
119
            ),
120
        );
121
122
        $paricevuti = array(
123 1
            'doctrine' => $em,
124 1
            'nomebundle' => $nomebundle,
125 1
            'nometabella' => $controller,
126 1
            'dettaglij' => $dettaglij,
127 1
            'container' => $container,
128
        );
129
130 1
        $paricevuti['escludere'] = array('nometabella', 'operatori_id');
131
132 1
        $testata = Griglia::testataPerGriglia($paricevuti);
133
134 1
        $testata['titolo'] = "Configurazione colonne per tabella $nometabella";
135 1
        $testata['multisearch'] = 0;
136 1
        $testata['showdel'] = 0;
137 1
        $testata['showadd'] = 0;
138 1
        $testata['showedit'] = 0;
139 1
        $testata['showprint'] = 0;
140 1
        $testata['editinline'] = 1;
141 1
        $testata['nomelist'] = '#listconfigura';
142 1
        $testata['nomepager'] = '#pagerconfigura';
143 1
        $testata['tastochiudi'] = 1;
144 1
        $testata['div'] = '#dettaglioconf';
145 1
        $testata['chiamante'] = $nometabella;
146 1
        $testata['percorsogriglia'] = $nometabella . '/grigliapopup';
147 1
        $testata['altezzagriglia'] = '300';
148 1
        $testata['larghezzagriglia'] = '900';
149
150 1
        $testata['permessiedit'] = 1;
151 1
        $testata['permessidelete'] = 1;
152 1
        $testata['permessicreate'] = 1;
153 1
        $testata['permessiread'] = 1;
154
        $twigparm = array(
155 1
            'entities' => $entities,
156 1
            'nomecontroller' => $controller,
157 1
            'testata' => json_encode($testata),
158 1
            'chiamante' => $nometabella,
159
        );
160
161 1
        return $this->render($nomebundle . ':' . $controller . ':configura.html.twig', $twigparm);
162
    }
163
164 1
    public function generaDB($parametri, Request $request)
165
    {
166 1
        if (!isset($parametri['tabella'])) {
167
            return false;
168
        }
169
170 1
        $this->setup($request);
171 1
        $namespace = $this->getNamespace();
172 1
        $bundle = $this->getBundle();
173
174 1
        $nomebundle = $namespace . $bundle . 'Bundle';
175
176 1
        $nometabella = $parametri['tabella'];
177 1
        $em = $this->getDoctrine()->getManager();
178
179 1
        $bundles = $this->get('kernel')->getBundles();
180 1
        $tableClassName = "";
181 1
        $entityClass = "";
182 1
        foreach ($bundles as $bundle) {
183 1
            $className = get_class($bundle);
184 1
            $entityClass = substr($className, 0, strrpos($className, '\\'));
185 1
            $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella;
186 1
            if (!class_exists($tableClassName)) {
187 1
                $tableClassName = '';
188 1
                continue;
189
            } else {
190 1
                break;
191
            }
192
        }
193
194 1
        if (!$tableClassName) {
195
            throw new \Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1');
196
        }
197
198 1
        if (!$entityClass) {
199
            throw new \Exception('Entity class per la tabella ' . $nometabella . ' non trovata', '-1');
200
        }
201
202 1
        $bundleClass = str_replace('\\', '', $entityClass);
203
204 1
        $c = $em->getClassMetadata($bundleClass . ':' . $nometabella);
205
206 1
        $colonne = $c->getColumnNames();
207 1
        $this->scriviDB($colonne, $nometabella, $nomebundle, $parametri);
208 1
    }
209
210 1
    private function scriviDB($colonne, $nometabella, $nomebundle, $parametri)
211
    {
212 1
        foreach ($colonne as $colonna) {
213
            $vettorericerca = array(
214 1
                'nometabella' => $nometabella,
215 1
                'nomecampo' => $colonna,
216
            );
217
218 1
            if (isset($parametri['operatore'])) {
219 1
                $vettorericerca['operatori_id'] = $parametri['operatore'];
220
            }
221
222 1
            $trovato = $this->getDoctrine()->getRepository($nomebundle . ':Tabelle')->findBy($vettorericerca, array());
223
224 1
            if (empty($trovato)) {
225 1
                $this->creaRecordTabelle($nometabella, $colonna, $vettorericerca, $parametri);
226
            }
227
        }
228 1
    }
229
230 1
    private function creaRecordTabelle($nometabella, $colonna, $vettorericerca, $parametri)
231
    {
232 1
        $crea = new Tabelle();
233 1
        $crea->setNometabella($nometabella);
234 1
        $crea->setNomecampo($colonna);
235
236 1
        if (isset($parametri['operatore'])) {
237 1
            $idOperatore = $parametri['operatore'];
238 1
            $creaoperatore = $this->getDoctrine()->getRepository('FiCoreBundle:Operatori')->find($idOperatore);
239 1
            if ($creaoperatore instanceof \Fi\CoreBundle\Entity\Operatori) {
240 1
                $crea->setOperatori($creaoperatore);
241
            }
242
243 1
            $vettorericerca['operatori_id'] = null;
244 1
            $ritrovato = $this->getDoctrine()->getRepository('FiCoreBundle:Tabelle')->findOneBy($vettorericerca);
245
246 1
            if (!empty($ritrovato)) {
247 1
                $crea->setMostrastampa($ritrovato->hasMostrastampa() ? true : false);
248 1
                $crea->setMostraindex($ritrovato->hasMostraindex() ? true : false);
249
            }
250
        } else {
251 1
            $crea->setMostrastampa(true);
252 1
            $crea->setMostraindex(true);
253
        }
254
255 1
        $ma = $this->getDoctrine()->getManager();
256 1
        $ma->persist($crea);
257 1
        $ma->flush();
258 1
    }
259
260
    public function grigliapopupAction(Request $request, $chiamante)
261
    {
262
        $this->setup($request);
263
        $namespace = $this->getNamespace();
264
        $bundle = $this->getBundle();
265
        $controller = $this->getController();
266
267
        $nomebundle = $namespace . $bundle . 'Bundle';
268
        $em = $this->getDoctrine()->getManager();
269
270
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
271
        $operatore = $gestionepermessi->utentecorrente();
272
        $tabellej = array();
273
        $tabellej['operatori_id'] = array('tabella' => 'operatori', 'campi' => array('username', 'operatore'));
274
275
        $paricevuti = array(
276
            'request' => $request,
277
            'doctrine' => $em,
278
            'container' => $this->container,
279
            'nomebundle' => $nomebundle,
280
            'nometabella' => $controller,
281
            'tabellej' => $tabellej,);
282
283
        $paricevuti['escludere'] = array('nometabella', 'operatori_id');
284
        $paricevuti['precondizioni'] = array('Tabelle.nometabella' => $chiamante, 'Tabelle.operatori_id' => $operatore['id']);
285
286
        return new Response(Griglia::datiPerGriglia($paricevuti));
287
    }
288
289
    public function grigliaAction(Request $request)
290
    {
291
        $this->setParametriGriglia(array('request' => $request));
292
        $paricevuti = self::$parametrigriglia;
293
294
        return new Response(Griglia::datiPerGriglia($paricevuti));
295
    }
296
297 1
    protected function setParametriGriglia($prepar = array())
298
    {
299 1
        $this->setup($prepar['request']);
300 1
        $namespace = $this->getNamespace();
301 1
        $bundle = $this->getBundle();
302 1
        $controller = $this->getController();
303
304 1
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
305 1
        $canRead = ($gestionepermessi->leggere(array('modulo' => $controller)) ? 1 : 0);
306 1
        if (!$canRead) {
307
            throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto");
308
        }
309
310 1
        $nomebundle = $namespace . $bundle . 'Bundle';
311 1
        $tabellej = array();
312 1
        $tabellej['operatori_id'] = array('tabella' => 'operatori', 'campi' => array('username'));
313 1
        $escludi = array("operatori"); //'operatori_id'
314
315
        $paricevuti = array(
316 1
            'container' => $this->container,
317 1
            'nomebundle' => $nomebundle,
318 1
            'nometabella' => $controller,
319 1
            'tabellej' => $tabellej,
320 1
            'escludere' => $escludi
321
        );
322
323 1
        if (!empty($prepar)) {
324 1
            $paricevuti = array_merge($paricevuti, $prepar);
325
        }
326
327 1
        self::$parametrigriglia = $paricevuti;
328 1
    }
329
330
    public function listacampitabellaAction(Request $request)
331
    {
332
        $this->setup($request);
333
        $namespace = $this->getNamespace();
334
        $bundle = $this->getBundle();
335
        $controller = $this->getController();
336
        $nomebundle = $namespace . $bundle . 'Bundle';
337
338
        $nometabella = trim($request->get('tabella'));
339
        if (!isset($nometabella)) {
340
            return false;
341
        }
342
343
        $escludiid = $request->get('escludiid');
344
        if (!isset($escludiid)) {
345
            $escludiid = 0;
346
        }
347
348
        $em = $this->getDoctrine()->getManager();
349
350
        $bundles = $this->get('kernel')->getBundles();
351
        foreach ($bundles as $bundle) {
352
            $className = get_class($bundle);
353
            $entityClass = substr($className, 0, strrpos($className, '\\'));
354
            $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella;
355
            if (!class_exists($tableClassName)) {
356
                $tableClassName = '';
357
                continue;
358
            } else {
359
                break;
360
            }
361
        }
362
363
        if (!$tableClassName) {
0 ignored issues
show
Bug introduced by
The variable $tableClassName 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...
364
            throw new \Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1');
365
        }
366
367
        $bundleClass = str_replace('\\', '', $entityClass);
0 ignored issues
show
Bug introduced by
The variable $entityClass 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...
368
        $c = $em->getClassMetadata($bundleClass . ':' . $nometabella);
369
        $colonne = $c->getColumnNames();
370
371
        $risposta = $this->listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller);
372
        //natcasesort($risposta);
373
        asort($risposta, SORT_NATURAL | SORT_FLAG_CASE);
374
375
        return new JsonResponse($risposta);
376
    }
377
378
    private function listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller)
379
    {
380
        $risposta = array();
381
        $nometabella = $controller;
382
        if ($escludiid == 1) {
383
            $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
384
            $operatore = $gestionepermessi->utentecorrente();
385
            foreach ($colonne as $colonna) {
386
                $nomecampo = trim(strtolower($colonna));
387
                if (($nomecampo !== 'id') && (strpos($colonna, '_id') === false)) {
388
                    $qb = $this->getDoctrine()->getRepository("$nomebundle:$controller")
389
                            ->createQueryBuilder('t')
390
                            ->where('LOWER(t.nometabella) = :nometabella')
391
                            ->andWhere('LOWER(t.nomecampo) = :nomecampo')
392
                            ->andWhere('t.operatori_id = :operatori_id')
393
                            ->setParameter('nometabella', $nometabella)
394
                            ->setParameter('nomecampo', $nomecampo)
395
                            ->setParameter('operatori_id', $operatore['id'])
396
                            ->getQuery();
397
                    $labeltrovata = $qb->getResult();
398
                    if (!$labeltrovata) {
399
                        $qb = $this->getDoctrine()->getRepository("$nomebundle:$controller")
400
                                ->createQueryBuilder('t')
401
                                ->where('LOWER(t.nometabella) = :nometabella')
402
                                ->andWhere('LOWER(t.nomecampo) = :nomecampo')
403
                                ->andWhere('t.operatori_id IS NULL')
404
                                ->setParameter('nometabella', $nometabella)
405
                                ->setParameter('nomecampo', $nomecampo)
406
                                ->getQuery();
407
                        $labeltrovata = $qb->getResult();
408
                        if (!$labeltrovata) {
409
                            $risposta[$colonna] = $colonna;
410
                        } else {
411
                            if (($labeltrovata[0]->getEtichettaindex()) && ($labeltrovata[0]->getEtichettaindex() != '')) {
412
                                $risposta[$colonna] = trim($labeltrovata[0]->getEtichettaindex());
413
                            } else {
414
                                $risposta[$colonna] = $colonna;
415
                            }
416
                        }
417
                    } else {
418
                        if (($labeltrovata[0]->getEtichettaindex()) && ($labeltrovata[0]->getEtichettaindex() != '')) {
419
                            $risposta[$colonna] = trim($labeltrovata[0]->getEtichettaindex());
420
                        } else {
421
                            $risposta[$colonna] = $colonna;
422
                        }
423
                    }
424
                }
425
            }
426
        } else {
427
            foreach ($colonne as $colonna) {
428
                $risposta[$colonna] = $colonna;
429
            }
430
        }
431
432
        return $risposta;
433
    }
434
}
435