Completed
Push — master ( 9b53e2...2758ff )
by Andrea
19:08 queued 08:29
created

FiUtilita::getDataFormattata()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 4
nop 4
crap 3
1
<?php
2
3
namespace Fi\CoreBundle\Controller;
4
5
/*
6
 * Insieme di funzioni utili
7
 * FiUtilita.
8
 *
9
 * @author Emidio Picariello
10
 */
11
12
use Fi\CoreBundle\DependencyInjection\PercentualiStringhe;
13
14
class FiUtilita
15
{
16
17
    public function percentualiConfrontoStringheVettore($parametri = array())
18
    {
19
        $percentuali = new PercentualiStringhe();
20
21
        return $percentuali->percentualiConfrontoStringheVettore($parametri);
22
    }
23
24 1
    public function percentualiConfrontoStringhe($parametri = array())
25
    {
26 1
        $percentuali = new PercentualiStringhe();
27
28 1
        return $percentuali->percentualiConfrontoStringhe($parametri);
29
    }
30
31
    public function sommaMinuti($parametri = array())
32
    {
33
        //parametri obbligatori
34
        if (!isset($parametri['minuti'])) {
35
            return false;
36
        }
37
        $minuti = $parametri['minuti'];
38
39
        $totminuti = array_sum($minuti);
40
        $resminuti = $totminuti % 60;
41
        $resore = ($totminuti - $resminuti) / 60;
42
43
        $restotminuti = array('ore' => $resore, 'minuti' => $resminuti);
44
45
        return $restotminuti;
46
    }
47
48
    /**
49
     * @param array  $parametri
50
     * string $parametri["tipo"]
51
     *
52
     * @return Array("segnouno"=>"xx", "segnodue"=>"yy") dove segnodue non obbligatorio
0 ignored issues
show
Documentation introduced by
The doc-type Array("segnouno"=>"xx", could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
53
     */
54
    public function operatoreQuery($parametri = array())
55
    {
56
        $risposta = array();
57
58
        if (isset($parametri['tipo'])) {
59
            $tipocampo = $parametri['tipo'];
60
        } else {
61
            return array('segnouno' => '=');
62
        }
63
64
        switch ($tipocampo) {
65
            case 'date':
66
            case 'integer':
67
            case 'double':
68
                $operatore = '>=';
69
                $operatoredue = '<=';
70
                break;
71
            case 'string':
72
            case 'text':
73
                $operatore = 'LIKE';
74
                break;
75
            default:
76
                $operatore = '=';
77
                break;
78
        }
79
80
        $risposta['segnouno'] = $operatore;
81
        if (isset($operatoredue)) {
82
            $risposta['segnodue'] = $operatoredue;
83
        }
84
85
        return $risposta;
86
    }
87
88 2
    public static function data2db($giorno, $invertito = false, $senzalinea = false)
89
    {
90 2
        if ($giorno == '') {
91
            return null;
92
        }
93
94 2
        if (substr($giorno, 4, 1) == '-') {
95 1
            return $giorno;
96
        }
97
98 1
        $barragiorno = strpos($giorno, '/');
99 1
        $gg = substr($giorno, 0, $barragiorno);
100 1
        $restante = substr($giorno, $barragiorno + 1);
101
102 1
        $barra = strpos($restante, '/');
103 1
        $mm = substr($restante, 0, $barra);
104 1
        $aaaa = substr($restante, $barra + 1);
105
106 1
        $appogg = ($invertito ? $mm : $gg);
107 1
        $mm = ($invertito ? $gg : $mm);
108 1
        $gg = $appogg;
109
110 1
        $formattata = self::getDataFormattata($aaaa, $mm, $gg, $senzalinea);
111
112 1
        return $formattata;
113
    }
114
115 1
    private static function getDataFormattata($aaaa, $mm, $gg, $senzalinea)
116
    {
117 1
        $separatore = ($senzalinea ? '' : '-');
118
119 1
        $nuovadata = $aaaa . $separatore . $mm . $separatore . $gg;
120
121 1
        return strlen($gg) == 0 ? '' : $nuovadata;
122
    }
123
124
    public static function db2data($giorno, $senzalinea = false)
125
    {
126
        if (substr($giorno, 2, 1) == '/') {
127
            return $giorno;
128
        }
129
130
        if ($senzalinea) {
131
            $formattata = self::senzalinea($giorno);
132
        } else {
133
            $barra = strpos($giorno, '-');
134
            $aaaa = substr($giorno, 0, $barra);
135
            $restante = substr($giorno, $barra + 1);
136
            $barra = strpos($restante, '-');
137
            $mm = substr($restante, 0, $barra);
138
            $gg = substr($restante, $barra + 1);
139
140
            $formattata = (strlen($gg) == 0 ? '' : "$gg/$mm/$aaaa");
141
        }
142
143
        return $formattata;
144
    }
145
146
    private static function senzalinea($giorno)
147
    {
148
        $aaaa = substr($giorno, 0, 4);
149
        $mm = substr($giorno, 4, 2);
150
        $gg = substr($giorno, 6, 2);
151
152
        $formattata = (strlen($gg) == 0 ? '' : "$gg/$mm/$aaaa");
153
154
        return $formattata;
155
    }
156
157
    /**
158
     * @param array  $parametri
159
     * string $parametri["nomecodice"]      default = "codice"
160
     * string $parametri["nomedescrizione"] default = "descrizione"
161
     * array  $parametri["elementi"]        Array([0]=>("codice"=>1, "descrizione"=>"blaa"), [1]=>...)
162
     * string $parametri["selezionato"]     opzionale
163
     *
164
     * @return string
165
     */
166 1
    public function proSelect($parametri = array())
167
    {
168 1
        $stringaproselect = '';
169 1
        if (!isset($parametri['elementi'])) {
170
            return false;
171
        }
172
173
        //parametri obbligatori
174 1
        $elementi = $parametri['elementi'];
175 1
        $attributi = $this->getProSelectAttribute($parametri);
176 1
        $selezionato = $attributi['selezionato'];
177 1
        $nomecodice = $attributi['nomecodice'];
178 1
        $nomedescrizione = $attributi['nomedescrizione'];
179
180 1
        foreach ($elementi as $elemento) {
181 1
            $elementonomecodice = $elemento[$nomecodice];
182 1
            $elementonomedescrizione = $elemento[$nomedescrizione];
183 1
            $elementoselezionato = ($elementonomecodice === $selezionato ? " selected='yes'" : '');
184 1
            $stringaproselect .= '<option value="' . $elementonomecodice . '"' . $elementoselezionato . '>' . $elementonomedescrizione . '</option>';
185 1
        }
186
187 1
        return $stringaproselect;
188
    }
189
190 1
    public function getProSelectAttribute($parametri)
191
    {
192 1
        $arrayritorno = array();
193 1
        $arrayritorno['selezionato'] = (isset($parametri['selezionato']) ? $parametri['selezionato'] : false);
194 1
        $arrayritorno['nomecodice'] = (isset($parametri['nomecodice']) ? $parametri['nomecodice'] : 'codice');
195 1
        $arrayritorno['nomedescrizione'] = (isset($parametri['nomedescrizione']) ? $parametri['nomedescrizione'] : 'descrizione');
196
197 1
        return $arrayritorno;
198
    }
199
200
    /**
201
     * @param $parametri["vettore"]
202
     * @param $parametri["chiave"]
203
     * @param $parametri["valore"]
204
     *
205
     * @return $vettorenuovo
0 ignored issues
show
Documentation introduced by
The doc-type $vettorenuovo could not be parsed: Unknown type name "$vettorenuovo" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
206
     */
207
    public function cancellaDaVettore($parametri = array())
208
    {
209
210
        //parametri obbligatori
211
        if (isset($parametri['vettore'])) {
212
            $vettore = $parametri['vettore'];
213
        } else {
214
            return false;
215
        }
216
217
        //parametri obbligatori
218
        if (isset($parametri['chiave'])) {
219
            $chiave = $parametri['chiave'];
220
        } else {
221
            return $vettore;
222
        }
223
224
        //parametri obbligatori
225
        if (isset($parametri['valore'])) {
226
            $valore = $parametri['valore'];
227
        } else {
228
            return $vettore;
229
        }
230
231
        $vettorenuovo = array();
232
233
        foreach ($vettore as $elemento) {
234
            if (!($elemento[$chiave] == $valore)) {
235
                $vettorenuovo[] = $elemento;
236
            }
237
        }
238
        return $vettorenuovo;
239
    }
240
}
241