GrigliaUtils::init()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 111
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 90
c 1
b 0
f 0
dl 0
loc 111
ccs 11
cts 11
cp 1
rs 8.2181
cc 1
nc 1
nop 0
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Fi\CoreBundle\Utils;
4
5
class GrigliaUtils
6
{
7
8
    public static $decodificaop = [];
9
    public static $precarattere = [];
10
    public static $postcarattere = [];
11
    public static $precaratterecampo = [];
12
    public static $postcaratterecampo = [];
13
14
    const LARGHEZZAMASSIMA = 500;
15
    const MOLTIPLICATORELARGHEZZA = 10;
16
17 6
    public static function init()
18
    {
19
        // i possibili operatori di ciascuna ricerca sono
20
        //questi: ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc', 'nu', 'nn']
21
        //significano questo
22
        //['equal','not equal', 'less', 'less or equal','greater','greater or equal',
23
        //'begins with','does not begin with','is in','is not in','ends with','does not end with',
24
        //'contains','does not contain', 'is null', 'is not null']
25
        //sulla base dell'operatore impostato per la singola ricerca si impostano tre vettori
26
        //il promo contiene l'operatore da usare in query
27
28 6
        self::$decodificaop = array(
29 6
            'eq' => '=',
30
            'ne' => '<>',
31
            'lt' => '<',
32
            'le' => '<=',
33
            'gt' => '>',
34
            'ge' => '>=',
35
            'bw' => 'LIKE',
36
            'bn' => 'NOT LIKE',
37
            'in' => 'IN',
38
            'ni' => 'NOT IN',
39
            'ew' => 'LIKE',
40
            'en' => 'NOT LIKE',
41
            'cn' => 'LIKE',
42
            'nc' => 'NOT LIKE',
43
            'nu' => 'IS',
44
            'nn' => 'IS NOT',
45
            'nt' => '<>'
46
        );
47
        // questo contiene il carattere da usare prima del campo dati in query dipendentemente dal tipo di operatore
48 6
        self::$precarattere = array(
49 6
            'eq' => '',
50
            'ne' => '',
51
            'lt' => '',
52
            'le' => '',
53
            'gt' => '',
54
            'ge' => '',
55
            'bw' => 'lower(\'',
56
            'bn' => 'lower(\'',
57
            'in' => '(',
58
            'ni' => '(',
59
            'ew' => 'lower(\'%',
60
            'en' => 'lower(\'%',
61
            'cn' => 'lower(\'%',
62
            'nc' => 'lower(\'%',
63
            'nu' => '',
64
            'nn' => '',
65
            'nt' => 'TRUE'
66
        );
67
68
        // questo contiene il carattere da usare dopo il campo dati in query dipendentemente dal tipo di operatore
69 6
        self::$postcarattere = array(
70 6
            'eq' => '',
71
            'ne' => '',
72
            'lt' => '',
73
            'le' => '',
74
            'gt' => '',
75
            'ge' => '',
76
            'bw' => '%\')',
77
            'bn' => '%\')',
78
            'in' => ')',
79
            'ni' => ')',
80
            'ew' => '\')',
81
            'en' => '\')',
82
            'cn' => '%\')',
83
            'nc' => '%\')',
84
            'nu' => '',
85
            'nn' => '',
86
            'nt' => '');
87
88
        // questo contiene il carattere da usare prima del campo dati in query dipendentemente dal tipo di operatore
89 6
        self::$precaratterecampo = array(
90 6
            'eq' => '',
91
            'ne' => '',
92
            'lt' => '',
93
            'le' => '',
94
            'gt' => '',
95
            'ge' => '',
96
            'bw' => 'lower(',
97
            'bn' => 'lower(',
98
            'in' => '(',
99
            'ni' => '(',
100
            'ew' => 'lower(',
101
            'en' => 'lower(',
102
            'cn' => 'lower(',
103
            'nc' => 'lower(',
104
            'nu' => '',
105
            'nn' => '',
106
            'nt' => 'TRUE',
107
        );
108
109
        // questo contiene il carattere da usare dopo il campo dati in query dipendentemente dal tipo di operatore
110 6
        self::$postcaratterecampo = array(
111 6
            'eq' => '',
112
            'ne' => '',
113
            'lt' => '',
114
            'le' => '',
115
            'gt' => '',
116
            'ge' => '',
117
            'bw' => ')',
118
            'bn' => ')',
119
            'in' => ')',
120
            'ni' => ')',
121
            'ew' => ')',
122
            'en' => ')',
123
            'cn' => ')',
124
            'nc' => ')',
125
            'nu' => '',
126
            'nn' => '',
127
            'nt' => '',);
128 6
    }
129
130 1
    public static function setVettoriPerData()
131
    {
132 1
        self::$precaratterecampo['eq'] = "";
133 1
        self::$postcaratterecampo['eq'] = "";
134 1
        self::$precaratterecampo['ne'] = "";
135 1
        self::$postcaratterecampo['ne'] = "";
136
137 1
        self::$precarattere['eq'] = "";
138 1
        self::$precarattere['ne'] = "";
139 1
        self::$precarattere['lt'] = "";
140 1
        self::$precarattere['le'] = "";
141 1
        self::$precarattere['gt'] = "";
142 1
        self::$precarattere['ge'] = "";
143 1
        self::$postcarattere['eq'] = "";
144 1
        self::$postcarattere['ne'] = "";
145 1
        self::$postcarattere['lt'] = "";
146 1
        self::$postcarattere['le'] = "";
147 1
        self::$postcarattere['gt'] = "";
148 1
        self::$postcarattere['ge'] = "";
149 1
    }
150
151 1
    public static function setVettoriPerBoolean()
152
    {
153 1
        self::setVettoriPerData();
154 1
    }
155
156 5
    public static function setVettoriPerStringa()
157
    {
158 5
        self::$precaratterecampo['eq'] = "lower(";
159 5
        self::$postcaratterecampo['eq'] = ")";
160 5
        self::$precaratterecampo['ne'] = "lower(";
161 5
        self::$postcaratterecampo['ne'] = ")";
162
163 5
        self::$precarattere['eq'] = "";
164 5
        self::$precarattere['ne'] = "";
165 5
        self::$precarattere['lt'] = "";
166 5
        self::$precarattere['le'] = "";
167 5
        self::$precarattere['gt'] = "";
168 5
        self::$precarattere['ge'] = "";
169 5
        self::$precarattere['cn'] = "%";
170 5
        self::$precarattere['nc'] = "%";
171 5
        self::$precarattere['bw'] = "";
172 5
        self::$precarattere['in'] = "lower(";
173 5
        self::$precarattere['ni'] = "lower(";
174 5
        self::$postcarattere['eq'] = "";
175 5
        self::$postcarattere['ne'] = "";
176 5
        self::$postcarattere['lt'] = "";
177 5
        self::$postcarattere['le'] = "";
178 5
        self::$postcarattere['gt'] = "";
179 5
        self::$postcarattere['ge'] = "";
180 5
        self::$postcarattere['cn'] = "%";
181 5
        self::$postcarattere['nc'] = "%";
182 5
        self::$postcarattere['bw'] = "%";
183 5
        self::$postcarattere['in'] = ")";
184 5
        self::$postcarattere['ni'] = ")";
185 5
    }
186
187 2
    public static function setVettoriPerNumero()
188
    {
189 2
        self::$precaratterecampo['eq'] = "";
190 2
        self::$postcaratterecampo['eq'] = "";
191 2
        self::$precaratterecampo['ne'] = "";
192 2
        self::$postcaratterecampo['ne'] = "";
193 2
        self::$precaratterecampo['in'] = "(";
194 2
        self::$postcaratterecampo['in'] = ")";
195 2
        self::$precaratterecampo['ni'] = "(";
196 2
        self::$postcaratterecampo['ni'] = ")";
197
198 2
        self::$precarattere['eq'] = '';
199 2
        self::$precarattere['ne'] = '';
200 2
        self::$precarattere['lt'] = '';
201 2
        self::$precarattere['le'] = '';
202 2
        self::$precarattere['gt'] = '';
203 2
        self::$precarattere['ge'] = '';
204 2
        self::$precarattere['in'] = '';
205 2
        self::$precarattere['ni'] = '';
206 2
        self::$postcarattere['eq'] = '';
207 2
        self::$postcarattere['ne'] = '';
208 2
        self::$postcarattere['lt'] = '';
209 2
        self::$postcarattere['le'] = '';
210 2
        self::$postcarattere['gt'] = '';
211 2
        self::$postcarattere['ge'] = '';
212 2
        self::$postcarattere['in'] = '';
213 2
        self::$postcarattere['ni'] = '';
214 2
    }
215
216 12
    public static function getCampiEsclusi($riga, $output)
217
    {
218 12
        $campoescluso = null;
219 12
        if ($output == 'stampa') {
220
            if ($riga->hasMostrastampa() == false) {
221
                $campoescluso = $riga->getNomecampo();
222
            }
223
        } else {
224 12
            if ($riga->hasMostraindex() == false) {
225 3
                $campoescluso = $riga->getNomecampo();
226
            }
227
        }
228
229 12
        return $campoescluso;
230
    }
231
232 21
    public static function getUserCustomTableFields($em, $nometabella, $operatore)
233
    {
234 21
        return TabelleSingletonUtility::instance($em, $nometabella, $operatore)->getTabelle();
235
    }
236
237 19
    public static function etichettecampi($parametri = array())
238
    {
239 19
        if (!isset($parametri['nometabella'])) {
240
            return false;
241
        }
242
243 19
        $output = GrigliaParametriUtils::getOuputType($parametri);
244
245 19
        $nometabella = $parametri['nometabella'];
246
247 19
        $doctrine = GrigliaParametriUtils::getDoctrineByEm($parametri);
248 19
        $doctrineficore = GrigliaParametriUtils::getDoctrineFiCoreByEm($parametri, $doctrine);
249 19
        $container = $parametri['container'];
250 19
        $gestionepermessi = $container->get("ficorebundle.gestionepermessi");
251
252 19
        $operatorecorrente = $gestionepermessi->utentecorrente();
253
254 19
        $etichette = array();
255
256 19
        $q = self::getUserCustomTableFields($doctrineficore, $nometabella, $operatorecorrente["id"]);
257
258 19
        if ($q) {
259 11
            foreach ($q as $riga) {
260 11
                if ($output == 'stampa') {
261
                    $etichette[$riga->getNomecampo()] = $riga->getEtichettastampa();
262
                } else {
263 11
                    $etichette[$riga->getNomecampo()] = $riga->getEtichettaindex();
264
                }
265
            }
266
        }
267
268 19
        return $etichette;
269
    }
270
271 19
    public static function larghezzecampi($parametri = array())
272
    {
273 19
        if (!isset($parametri['nometabella'])) {
274
            return false;
275
        }
276
277 19
        $output = GrigliaParametriUtils::getOuputType($parametri);
278
279 19
        $nometabella = $parametri['nometabella'];
280
281 19
        $doctrine = GrigliaParametriUtils::getDoctrineByEm($parametri);
282 19
        $doctrineficore = GrigliaParametriUtils::getDoctrineFiCoreByEm($parametri, $doctrine);
283
284 19
        $container = $parametri['container'];
285 19
        $gestionepermessi = $container->get("ficorebundle.gestionepermessi");
286 19
        $operatorecorrente = $gestionepermessi->utentecorrente();
287
288 19
        $etichette = array();
289
290 19
        $q = self::getUserCustomTableFields($doctrineficore, $nometabella, $operatorecorrente["id"]);
291
292 19
        if (!$q) {
293 8
            return $etichette;
294
        }
295
296 11
        foreach ($q as $riga) {
297 11
            if ($output == 'stampa') {
298
                $etichette[$riga->getNomecampo()] = $riga->getLarghezzastampa();
299
            } else {
300 11
                $etichette[$riga->getNomecampo()] = $riga->getLarghezzaindex();
301
            }
302
        }
303
304 11
        return $etichette;
305
    }
306
307 20
    public static function ordinecolonne($parametri = array())
308
    {
309 20
        if (!isset($parametri['nometabella'])) {
310
            return false;
311
        }
312
313 20
        $output = GrigliaParametriUtils::getOuputType($parametri);
314
315 20
        $nometabella = $parametri['nometabella'];
316
317 20
        $doctrine = GrigliaParametriUtils::getDoctrineByEm($parametri);
318 20
        $doctrineficore = GrigliaParametriUtils::getDoctrineFiCoreByEm($parametri, $doctrine);
319 20
        $container = $parametri['container'];
320
321 20
        $gestionepermessi = $container->get("ficorebundle.gestionepermessi");
322
323 20
        $operatorecorrente = $gestionepermessi->utentecorrente();
324
325 20
        $ordine = array();
326
327 20
        $q = self::getUserCustomTableFields($doctrineficore, $nometabella, $operatorecorrente["id"]);
328
329 20
        if ($q) {
330 11
            foreach ($q as $riga) {
331 11
                if ($output == 'stampa') {
332
                    if ($riga->getOrdinestampa()) {
333
                        $ordine[$riga->getOrdinestampa()] = $riga->getNomecampo();
334
                    }
335
                } else {
336 11
                    if ($riga->getOrdineindex()) {
337
                        $ordine[$riga->getOrdineindex()] = $riga->getNomecampo();
338
                    }
339
                }
340
            }
341 11
            if (count($ordine) > 0) {
342
                ksort($ordine);
343
            }
344
        }
345
346 20
        $ordinecolonne = GrigliaColonneUtils::getColonneOrdinate($ordine);
347
348 20
        return $ordinecolonne;
349
    }
350
351
    /**
352
     * Translates a string with underscores into camel case (e.g. first_name -&gt; firstName).
353
     *
354
     * @param array  $parametri
355
     *
356
     * @return string $str translated into camel caps
357
     */
358 21
    public static function toCamelCase($parametri = array())
359
    {
360 21
        $str = $parametri['str'];
361 21
        $capitalise_first_char = isset($parametri['primamaiuscola']) ? $parametri['primamaiuscola'] : false;
362
363 21
        if ($capitalise_first_char) {
364 21
            $str[0] = strtoupper($str[0]);
365
        }
366
        $func = function ($matches) {
367 6
            return strtoupper($matches[1]);
368 21
        };
369
370 21
        return preg_replace_callback('/_([a-z])/', $func, $str);
371
    }
372
373 19
    public static function getOpzioniTabella($doctrineficore, $nometabella, &$testata)
374
    {
375
        /* @var $qb \Doctrine\ORM\QueryBuilder */
376 19
        $qb = $doctrineficore->createQueryBuilder();
377 19
        $qb->select(array('a'));
378 19
        $qb->from('FiCoreBundle:OpzioniTabella', 'a');
379 19
        $qb->leftJoin('a.tabelle', 't');
380 19
        $qb->where("t.nometabella = '*' or t.nometabella = :tabella");
381 19
        $qb->andWhere("t.nomecampo is null or t.nomecampo = ''");
382 19
        $qb->orderBy('t.nometabella');
383 19
        $qb->setParameter('tabella', $nometabella);
384 19
        $opzioni = $qb->getQuery()->useQueryCache(true)->useResultCache(true, null, 'OpzioniTabella')->getResult();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\ORM\AbstractQuery::useResultCache() has been deprecated: 2.7 Use {@see enableResultCache} and {@see disableResultCache} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

384
        $opzioni = /** @scrutinizer ignore-deprecated */ $qb->getQuery()->useQueryCache(true)->useResultCache(true, null, 'OpzioniTabella')->getResult();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
385 19
        foreach ($opzioni as $opzione) {
386 19
            $testata[$opzione->getParametro()] = str_replace('%tabella%', $nometabella, $opzione->getValore());
387
        }
388 19
    }
389
390 19
    public static function getNomiColonne($nomicolonne)
391
    {
392 19
        ksort($nomicolonne);
393 19
        $nomicolonnesorted = array();
394 19
        foreach ($nomicolonne as $value) {
395 19
            $nomicolonnesorted[] = $value;
396
        }
397
398 19
        return $nomicolonnesorted;
399
    }
400
401 19
    public static function getModelloColonne($modellocolonne)
402
    {
403 19
        ksort($modellocolonne);
404 19
        $modellocolonnesorted = array();
405 19
        foreach ($modellocolonne as $value) {
406 19
            $modellocolonnesorted[] = $value;
407
        }
408
409 19
        return $modellocolonnesorted;
410
    }
411
412 19
    public static function getPermessiTabella($paricevuti, &$testata)
413
    {
414 19
        if (!isset($paricevuti['container'])) {
415
            return;
416
        }
417
418 19
        $container = $paricevuti['container'];
419 19
        $nometabella = $paricevuti['nometabella'];
420 19
        $gestionepermessi = $container->get("ficorebundle.gestionepermessi");
421
422 19
        $vettorepermessi = $gestionepermessi->impostaPermessi(array('modulo' => $nometabella));
423
424 19
        return array_merge($testata, $vettorepermessi);
425
    }
426
}
427