Completed
Push — master ( 2ff726...1b8ace )
by Andrea
09:31
created

TabelleController::setParametriGriglia()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4.0012

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 22
cts 23
cp 0.9565
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 22
nc 6
nop 1
crap 4.0012
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
            'nomecampo' => array(
95 1
                array('nomecampo' => 'nomecampo', 'lunghezza' => '150', 'descrizione' => 'Campo', 'tipo' => 'text', 'editable' => false),),
96
            'mostraindex' => array(
97 1
                array('nomecampo' => 'mostraindex', 'lunghezza' => '100', 'descrizione' => 'Vedi in griglia', 'tipo' => 'boolean'),
98 1
            ),
99
            'ordineindex' => array(
100 1
                array('nomecampo' => 'ordineindex', 'lunghezza' => '100', 'descrizione' => 'Ordine in griglia', 'tipo' => 'text'),
101 1
            ),
102
            'etichettaindex' => array(
103 1
                array('nomecampo' => 'etichettaindex', 'lunghezza' => '150', 'descrizione' => 'Label in griglia', 'tipo' => 'text'),
104 1
            ),
105
            'larghezzaindex' => array(
106 1
                array('nomecampo' => 'larghezzaindex', 'lunghezza' => '100', 'descrizione' => 'Largh. in griglia', 'tipo' => 'text'),
107 1
            ),
108
            'mostrastampa' => array(
109 1
                array('nomecampo' => 'mostrastampa', 'lunghezza' => '100', 'descrizione' => 'Vedi in stampa', 'tipo' => 'boolean'),
110 1
            ),
111
            'ordinestampa' => array(
112 1
                array('nomecampo' => 'ordinestampa', 'lunghezza' => '100', 'descrizione' => 'Ordine in stampa', 'tipo' => 'text'),
113 1
            ),
114
            'etichettastampa' => array(
115 1
                array('nomecampo' => 'etichettastampa', 'lunghezza' => '150', 'descrizione' => 'Label in stampa', 'tipo' => 'text'),
116 1
            ),
117
            'larghezzastampa' => array(
118 1
                array('nomecampo' => 'larghezzastampa', 'lunghezza' => '100', 'descrizione' => 'Largh. in stampa', 'tipo' => 'text'),
119 1
            ),
120 1
        );
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 1
        );
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 1
        );
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 1
        }
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 1
            );
217
218 1
            if (isset($parametri['operatore'])) {
219 1
                $vettorericerca['operatori_id'] = $parametri['operatore'];
220 1
            }
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 1
            }
227 1
        }
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 1
            }
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 1
            }
250 1
        } 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 1
    protected function setParametriGriglia($prepar = array())
290
    {
291 1
        $this->setup($prepar['request']);
292 1
        $namespace = $this->getNamespace();
293 1
        $bundle = $this->getBundle();
294 1
        $controller = $this->getController();
295
296 1
        $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
297 1
        $canRead = ($gestionepermessi->leggere(array('modulo' => $controller)) ? 1 : 0);
298 1
        if (!$canRead) {
299
            throw new AccessDeniedException("Non si hanno i permessi per visualizzare questo contenuto");
300
        }
301
302 1
        $nomebundle = $namespace . $bundle . 'Bundle';
303 1
        $tabellej = array();
304 1
        $tabellej['operatori_id'] = array('tabella' => 'operatori', 'campi' => array('username'));
305 1
        $escludi = array("operatori"); //'operatori_id'
306
307
        $paricevuti = array(
308 1
            'container' => $this->container,
309 1
            'nomebundle' => $nomebundle,
310 1
            'nometabella' => $controller,
311 1
            'tabellej' => $tabellej,
312
            'escludere' => $escludi
313 1
        );
314
315 1
        if (!empty($prepar)) {
316 1
            $paricevuti = array_merge($paricevuti, $prepar);
317 1
        }
318
319 1
        self::$parametrigriglia = $paricevuti;
320 1
    }
321
322
    public function listacampitabellaAction(Request $request)
323
    {
324
        $this->setup($request);
325
        $namespace = $this->getNamespace();
326
        $bundle = $this->getBundle();
327
        $controller = $this->getController();
328
        $nomebundle = $namespace . $bundle . 'Bundle';
329
330
        $nometabella = trim($request->get('tabella'));
331
        if (!isset($nometabella)) {
332
            return false;
333
        }
334
335
        $escludiid = $request->get('escludiid');
336
        if (!isset($escludiid)) {
337
            $escludiid = 0;
338
        }
339
340
        $em = $this->getDoctrine()->getManager();
341
        $tableClassName = "";
342
        $entityClass = "";
343
        $bundles = $this->get('kernel')->getBundles();
344
        foreach ($bundles as $bundle) {
345
            $className = get_class($bundle);
346
            $entityClass = substr($className, 0, strrpos($className, '\\'));
347
            $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella;
348
            if (!class_exists($tableClassName)) {
349
                $tableClassName = '';
350
                continue;
351
            } else {
352
                break;
353
            }
354
        }
355
356
        if (!$tableClassName) {
357
            throw new \Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1');
358
        }
359
360
        if (!$entityClass) {
361
            throw new \Exception('Entity class per la tabella ' . $nometabella . ' non trovata', '-1');
362
        }
363
364
        $bundleClass = str_replace('\\', '', $entityClass);
365
        $c = $em->getClassMetadata($bundleClass . ':' . $nometabella);
366
        $colonne = $c->getColumnNames();
367
368
        $risposta = $this->listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller);
369
        //natcasesort($risposta);
370
        asort($risposta, SORT_NATURAL | SORT_FLAG_CASE);
371
372
        return new JsonResponse($risposta);
373
    }
374
375
    private function listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller)
376
    {
377
        $risposta = array();
378
        $nometabella = $controller;
379
        if ($escludiid == 1) {
380
            $gestionepermessi = $this->get("ficorebundle.gestionepermessi");
381
            $operatore = $gestionepermessi->utentecorrente();
382
            foreach ($colonne as $colonna) {
383
                $nomecampo = trim(strtolower($colonna));
384
                if (($nomecampo !== 'id') && (strpos($colonna, '_id') === false)) {
385
                    $qb = $this->getDoctrine()->getRepository("$nomebundle:$controller")
386
                            ->createQueryBuilder('t')
387
                            ->where('LOWER(t.nometabella) = :nometabella')
388
                            ->andWhere('LOWER(t.nomecampo) = :nomecampo')
389
                            ->andWhere('t.operatori_id = :operatori_id')
390
                            ->setParameter('nometabella', $nometabella)
391
                            ->setParameter('nomecampo', $nomecampo)
392
                            ->setParameter('operatori_id', $operatore['id'])
393
                            ->getQuery();
394
                    $labeltrovata = $qb->getResult();
395
                    if (!$labeltrovata) {
396
                        $qb = $this->getDoctrine()->getRepository("$nomebundle:$controller")
397
                                ->createQueryBuilder('t')
398
                                ->where('LOWER(t.nometabella) = :nometabella')
399
                                ->andWhere('LOWER(t.nomecampo) = :nomecampo')
400
                                ->andWhere('t.operatori_id IS NULL')
401
                                ->setParameter('nometabella', $nometabella)
402
                                ->setParameter('nomecampo', $nomecampo)
403
                                ->getQuery();
404
                        $labeltrovata = $qb->getResult();
405
                        if (!$labeltrovata) {
406
                            $risposta[$colonna] = $colonna;
407
                        } else {
408
                            if (($labeltrovata[0]->getEtichettaindex()) && ($labeltrovata[0]->getEtichettaindex() != '')) {
409
                                $risposta[$colonna] = trim($labeltrovata[0]->getEtichettaindex());
410
                            } else {
411
                                $risposta[$colonna] = $colonna;
412
                            }
413
                        }
414
                    } else {
415
                        if (($labeltrovata[0]->getEtichettaindex()) && ($labeltrovata[0]->getEtichettaindex() != '')) {
416
                            $risposta[$colonna] = trim($labeltrovata[0]->getEtichettaindex());
417
                        } else {
418
                            $risposta[$colonna] = $colonna;
419
                        }
420
                    }
421
                }
422
            }
423
        } else {
424
            foreach ($colonne as $colonna) {
425
                $risposta[$colonna] = $colonna;
426
            }
427
        }
428
429
        return $risposta;
430
    }
431
}
432