Passed
Push — master ( 51ccae...7bd827 )
by Andrea
07:15 queued 11s
created

TabellaOpzioniTrait::getOpzionitabellaCampiExtra()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 15
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 17
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.7666
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Tabella;
4
5
use Cdf\BiCoreBundle\Utils\Arrays\ArrayUtils;
6
use Cdf\BiCoreBundle\Utils\Entity\EntityUtils;
7
use function count;
8
9
trait TabellaOpzioniTrait
10
{
11
    use TabellaOpzioniFromCoreTrait, TabellaOpzioniFromModelloColonneTrait;
12
13 12
    protected function getAllOpzioniTabella()
14
    {
15 12
        $opzionibuilder = array();
16 12
        foreach ($this->colonnedatabase as $colonnadatabase) {
17
            // Inserire dati da definizione entity
18 12
            $this->setOpzioniTabellaDefault($colonnadatabase, $opzionibuilder, null, false);
19
        }
20
        //TODO: verify if is an issue or it's wanted that colonnadatabase is the last item of iterated columns
21 12
        if (isset($colonnadatabase)) {
22 12
            $this->setOpzioniTabellaFromCore($colonnadatabase, $opzionibuilder);
23
        }
24 12
        $this->setOpzioniTabellaFromModellocolonne($opzionibuilder);
25 12
        $this->setOrdinaColonneTabella($opzionibuilder);
26 12
        $this->setLarghezzaColonneTabella($opzionibuilder);
27
28 12
        return $opzionibuilder;
29
    }
30
31 5
    private function getOpzionitabellaCampiExtra($campo, $modellocolonna, &$opzionibuilder)
32
    {
33 5
        if ((isset($modellocolonna['campoextra']) && true == $modellocolonna['campoextra'])) {
34
            $opzionibuilder[$campo] = array(
35 1
                'tipocampo' => $modellocolonna['tipocampo'],
36 1
                'nomecampo' => $campo,
37 1
                'nometabella' => $modellocolonna['nometabella'],
38
                'entityclass' => null,
39
                'sourceentityclass' => null,
40
                'ordine' => null,
41 1
                'etichetta' => $campo,
42 1
                'larghezza' => 5,
43
                'editabile' => false,
44
                'campoextra' => true,
45
                'association' => null,
46
                'associationtable' => null,
47
                'escluso' => false,
48
            );
49
        }
50 5
    }
51
52 12
    protected function setOpzioniTabellaDefault($infoentity, &$opzionibuilder, $jointable = null, $ricursione = false, $ancestors = array())
53
    {
54 12
        $nometabella = ((isset($jointable)) ? $jointable : $this->tablename);
55 12
        if (!in_array($nometabella, $ancestors)) {
56 12
            $ancestors[] = $nometabella;
57
        }
58 12
        $nomecolonna = ucfirst(implode('.', $ancestors)).'.'.$infoentity['fieldName'];
59
60 12
        $this->elaboraColonneOpzioniTabellaMancanti($opzionibuilder, $infoentity, $nometabella, $nomecolonna, $ricursione);
61
62 12
        if (isset($infoentity['association'])) {
63 6
            $this->elaboraJoin($opzionibuilder, $infoentity, $ancestors);
64
        }
65 12
    }
66
67 12
    private function elaboraColonneOpzioniTabellaMancanti(&$opzionibuilder, $colonnadatabase, $nometabella, $nomecolonna, $ricursione)
68
    {
69
        $opzionibuilder[$nomecolonna] = array(
70 12
            'tipocampo' => isset($colonnadatabase['association']) ? 'join' : $colonnadatabase['type'],
71 12
            'nomecampo' => $nomecolonna,
72 12
            'nometabella' => $nometabella,
73 12
            'entityclass' => $colonnadatabase['entityClass'],
74 12
            'sourceentityclass' => isset($colonnadatabase['sourceEntityClass']) ? $colonnadatabase['sourceEntityClass'] : null,
75
            'ordine' => null,
76 12
            'etichetta' => ucfirst($colonnadatabase['columnName']),
77 12
            'larghezza' => 10,
78
            'editabile' => true,
79
            'campoextra' => false,
80 12
            'association' => isset($colonnadatabase['association']) ? $colonnadatabase['association'] : false,
81 12
            'associationtable' => isset($colonnadatabase['associationtable']) ? $colonnadatabase['associationtable'] : null,
82
            'decodifiche' => null,
83 12
            'escluso' => (true === $ricursione ? true : ('_id' == substr($colonnadatabase['fieldName'], -3) ? true : false)),
84
        );
85 12
    }
86
87 12
    private function getLarghezzaColonneTabellaTotalePercentuale($opzionibuilder)
88
    {
89 12
        $larghezzatotalepercentuale = 1;
90 12
        foreach ($opzionibuilder as $opzione) {
91 12
            if (false === $opzione['escluso']) {
92 12
                $larghezzatotalepercentuale += $opzione['larghezza'];
93
            }
94
        }
95
96 12
        return $larghezzatotalepercentuale;
97
    }
98
99 12
    private function getLarghezzaColonneTabellaPercentualeFinale()
100
    {
101
        // il 5% si lascia per la ruzzolina in fondo alla riga, il 3% si lascia per il checkbox in testa alla riga,
102
        // quindi per le colonne dati resta il 92%
103 12
        $percentualefinale = 95; // il 5% si lascia per la ruzzolina in fondo
104 12
        if (true === $this->getTabellaParameter('multiselezione')) {
0 ignored issues
show
Bug introduced by
It seems like getTabellaParameter() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

104
        if (true === $this->/** @scrutinizer ignore-call */ getTabellaParameter('multiselezione')) {
Loading history...
105
            $percentualefinale -= 3;
106
        }
107
108 12
        return $percentualefinale;
109
    }
110
111 12
    private function setLarghezzaColonneTabella(&$opzionibuilder)
112
    {
113 12
        $larghezzatotalepercentuale = $this->getLarghezzaColonneTabellaTotalePercentuale($opzionibuilder);
114 12
        $percentualefinale = $this->getLarghezzaColonneTabellaPercentualeFinale();
115 12
        $percentualerelativatotale = $percentualefinale * 100 / $larghezzatotalepercentuale;
116 12
        if (0 != $percentualefinale - $larghezzatotalepercentuale) {
117 12
            foreach ($opzionibuilder as $key => $opzione) {
118 12
                if (false === $opzione['escluso']) {
119 12
                    $larghezzapercentualericalcolata = ceil($opzione['larghezza'] * $percentualerelativatotale / 100);
120 12
                    $opzionibuilder[$key]['larghezza'] = ($larghezzapercentualericalcolata < 5 ? 5 : $larghezzapercentualericalcolata);
121
                }
122
            }
123
        }
124 12
    }
125
126 6
    private function elaboraJoin(&$opzionibuilder, $colonnadatabase, $ancestors)
127
    {
128 6
        $entitycollegata = $colonnadatabase['associationtable']['targetEntity'];
129 6
        $utils = new EntityUtils($this->em);
130 6
        $tablecollegataname = $this->em->getClassMetadata($entitycollegata)->getTableName();
131 6
        $colonnecollegate = $utils->getEntityColumns($entitycollegata);
132 6
        foreach ($colonnecollegate as $colonnacorrente) {
133 6
            if (!isset($colonnacorrente['type'])) {
134 5
                $this->setOpzioniTabellaDefault($colonnacorrente, $opzionibuilder, $tablecollegataname, true, $ancestors);
135 5
                continue;
136
            }
137
138 6
            if (!in_array($tablecollegataname, $ancestors)) {
139 6
                $ancestors[] = $tablecollegataname;
140
            }
141 6
            $nomecampo = ucfirst(implode('.', $ancestors)).'.'.$colonnacorrente['fieldName'];
142
            $opzionibuilder[$nomecampo] = array(
143 6
                'tipocampo' => $colonnacorrente['type'],
144 6
                'nomecampo' => $nomecampo,
145 6
                'nometabella' => $tablecollegataname,
146 6
                'entityclass' => $colonnadatabase['entityClass'],
147 6
                'sourceentityclass' => isset($colonnadatabase['sourceEntityClass']) ? $colonnadatabase['sourceEntityClass'] : null,
148
                'ordine' => null,
149 6
                'etichetta' => ucfirst($colonnacorrente['columnName']),
150 6
                'larghezza' => 0,
151
                'editabile' => false,
152
                'campoextra' => false,
153
                'association' => null,
154
                'associationtable' => null,
155
                'escluso' => true,
156
            );
157
        }
158 6
    }
159
160 12
    protected function setOrdinaColonneTabella(&$opzionibuilder)
161
    {
162 12
        foreach ($opzionibuilder as $key => $opzione) {
163 12
            if (null === $opzione['ordine']) {
164 12
                $newordine = $this->getMaxOrdine() + 10;
0 ignored issues
show
Bug introduced by
It seems like getMaxOrdine() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

164
                $newordine = $this->/** @scrutinizer ignore-call */ getMaxOrdine() + 10;
Loading history...
165 12
                $opzionibuilder[$key]['ordine'] = $newordine;
166 12
                $this->setMaxOrdine($newordine);
0 ignored issues
show
Bug introduced by
It seems like setMaxOrdine() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

166
                $this->/** @scrutinizer ignore-call */ 
167
                       setMaxOrdine($newordine);
Loading history...
167
            }
168
        }
169
        // Ordinamento per colonna ordine
170 12
        ArrayUtils::sortMultiAssociativeArray($opzionibuilder, 'ordine', true);
171 12
    }
172
173 6
    private function bonificaNomeCampo($nomecampo)
174
    {
175 6
        $parti = explode('.', $nomecampo);
176 6
        $campo = '';
177 6
        for ($index = 0; $index < count($parti); ++$index) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
178 6
            if ($index == count($parti) - 1) {
179 6
                $campo .= '.'.lcfirst($parti[$index]);
180
            } else {
181 6
                $campo .= '.'.ucfirst($parti[$index]);
182
            }
183
        }
184
185 6
        return substr($campo, 1);
186
    }
187
}
188