1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Utils\Tabella; |
4
|
|
|
|
5
|
|
|
use Cdf\BiCoreBundle\Utils\Entity\EntityUtils; |
6
|
|
|
use Cdf\BiCoreBundle\Entity\Opzionitabelle; |
7
|
|
|
use Cdf\BiCoreBundle\Entity\Colonnetabelle; |
8
|
|
|
use Cdf\BiCoreBundle\Utils\Arrays\ArrayUtils; |
9
|
|
|
|
10
|
|
|
trait TabellaOpzioniTrait |
11
|
|
|
{ |
12
|
12 |
|
protected function getOpzionitabellaFromCore() |
13
|
|
|
{ |
14
|
12 |
|
$repoopzionitabelle = $this->em->getRepository(Opzionitabelle::class); |
15
|
12 |
|
$repocolonnetabelle = $this->em->getRepository(Colonnetabelle::class); |
16
|
12 |
|
$opzionitabella = $repoopzionitabelle->findOpzioniTabella($this->tablename); |
|
|
|
|
17
|
12 |
|
$colonnetabella = $repocolonnetabelle->findOpzioniColonnetabella($this->tablename, $this->user); |
|
|
|
|
18
|
|
|
|
19
|
12 |
|
return array("opzionitabella" => $opzionitabella, "colonnetabella" => $colonnetabella); |
|
|
|
|
20
|
|
|
} |
21
|
12 |
|
protected function getAllOpzioniTabella() |
22
|
|
|
{ |
23
|
12 |
|
$opzionibuilder = array(); |
24
|
12 |
|
foreach ($this->colonnedatabase as $colonnadatabase) { |
25
|
|
|
// Inserire dati da definizione entity |
26
|
12 |
|
$this->setOpzioniTabellaDefault($colonnadatabase, $opzionibuilder, null, false); |
27
|
|
|
} |
28
|
12 |
|
$this->setOpzioniTabellaFromModellocolonne($opzionibuilder); |
29
|
12 |
|
$this->setOpzioniTabellaFromCore($colonnadatabase, $opzionibuilder); |
|
|
|
|
30
|
12 |
|
$this->setOrdinaColonneTabella($opzionibuilder); |
31
|
12 |
|
return $opzionibuilder; |
32
|
|
|
} |
33
|
12 |
|
protected function setOpzioniTabellaFromModellocolonne(&$opzionibuilder) |
34
|
|
|
{ |
35
|
12 |
|
foreach ($this->modellocolonne as $modellocolonna) { |
36
|
4 |
|
$campo = $this->bonificaNomeCampo($modellocolonna["nomecampo"]); |
|
|
|
|
37
|
4 |
|
foreach ($modellocolonna as $key => $value) { |
38
|
4 |
|
if (!array_key_exists($campo, $opzionibuilder)) { |
39
|
|
|
$ex = "Fifree: " . $campo . " field table option not found, did you mean one of these:\n" . |
|
|
|
|
40
|
|
|
implode("\n", array_keys($opzionibuilder)) . |
41
|
|
|
" ?"; |
|
|
|
|
42
|
|
|
throw new \Exception($ex); |
43
|
|
|
} |
44
|
4 |
|
if ($key == 'ordine') { |
45
|
3 |
|
$this->setMaxOrdine($value); |
|
|
|
|
46
|
|
|
} |
47
|
4 |
|
$opzionibuilder[$campo][$key] = $value; |
48
|
|
|
} |
49
|
|
|
} |
50
|
12 |
|
} |
51
|
12 |
|
protected function setOpzioniTabellaFromCore($colonnadatabase, &$opzionibuilder) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
|
54
|
12 |
|
$colonnetabellacore = $this->opzionitabellacore["colonnetabella"]; |
|
|
|
|
55
|
|
|
//$nomecolonna = $this->tablename . "." . $colonnadatabase["fieldName"]; |
|
|
|
|
56
|
|
|
/* @var $colonnatabellacore \Cdf\BiCoreBundle\Entity\Colonnetabelle */ |
57
|
12 |
|
foreach ($colonnetabellacore as $colonnatabellacore) { |
58
|
3 |
|
$campodabonificare = $colonnatabellacore->getNometabella() . "." . $colonnatabellacore->getNomecampo(); |
|
|
|
|
59
|
3 |
|
$campo = $this->bonificaNomeCampo($campodabonificare); |
|
|
|
|
60
|
3 |
|
if (null !== ($colonnatabellacore->getEtichettaindex())) { |
61
|
3 |
|
$opzionibuilder[$campo]["etichetta"] = $colonnatabellacore->getEtichettaindex(); |
|
|
|
|
62
|
|
|
} |
63
|
3 |
|
if (null !== ($colonnatabellacore->getLarghezzaindex())) { |
64
|
3 |
|
$opzionibuilder[$campo]["larghezza"] = $colonnatabellacore->getLarghezzaindex(); |
|
|
|
|
65
|
|
|
} |
66
|
3 |
|
if (null !== ($colonnatabellacore->getMostraindex())) { |
67
|
3 |
|
$opzionibuilder[$campo]["escluso"] = !$colonnatabellacore->getMostraindex(); |
|
|
|
|
68
|
|
|
} |
69
|
3 |
|
if (null !== ($colonnatabellacore->getOrdineindex())) { |
70
|
3 |
|
$opzionibuilder[$campo]["ordine"] = $colonnatabellacore->getOrdineindex(); |
|
|
|
|
71
|
3 |
|
$this->setMaxOrdine($colonnatabellacore->getOrdineindex()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
12 |
|
} |
75
|
12 |
|
protected function setOpzioniTabellaDefault($infoentity, &$opzionibuilder, $jointable = null, $ricursione = false, $ancestors = array()) |
76
|
|
|
{ |
77
|
12 |
|
$nometabella = ((isset($jointable)) ? $jointable : $this->tablename); |
78
|
12 |
|
if (!in_array($nometabella, $ancestors)) { |
79
|
12 |
|
$ancestors[] = $nometabella; |
80
|
|
|
} |
81
|
12 |
|
$nomecolonna = ucfirst(implode(".", $ancestors)) . "." . $infoentity["fieldName"]; |
|
|
|
|
82
|
|
|
|
83
|
12 |
|
$this->elaboraColonneOpzioniTabellaMancanti($opzionibuilder, $infoentity, $nometabella, $nomecolonna, $ricursione); |
84
|
|
|
|
85
|
12 |
|
if (isset($infoentity["association"])) { |
|
|
|
|
86
|
6 |
|
$this->elaboraJoin($opzionibuilder, $infoentity, $ancestors); |
87
|
|
|
} |
88
|
12 |
|
} |
89
|
12 |
|
private function elaboraColonneOpzioniTabellaMancanti(&$opzionibuilder, $colonnadatabase, $nometabella, $nomecolonna, $ricursione) |
90
|
|
|
{ |
91
|
12 |
|
$opzionibuilder[$nomecolonna] = array( |
92
|
12 |
|
"tipocampo" => isset($colonnadatabase["association"]) ? 'join' : $colonnadatabase["type"], |
|
|
|
|
93
|
12 |
|
"nomecampo" => $nomecolonna, |
|
|
|
|
94
|
12 |
|
"nometabella" => $nometabella, |
|
|
|
|
95
|
12 |
|
"entityclass" => $colonnadatabase["entityClass"], |
|
|
|
|
96
|
12 |
|
"sourceentityclass" => isset($colonnadatabase["sourceEntityClass"]) ? $colonnadatabase["sourceEntityClass"] : null, |
|
|
|
|
97
|
|
|
"ordine" => null, |
|
|
|
|
98
|
12 |
|
"etichetta" => ucfirst($colonnadatabase["columnName"]), |
|
|
|
|
99
|
12 |
|
"larghezza" => 100, |
|
|
|
|
100
|
12 |
|
"association" => isset($colonnadatabase["association"]) ? $colonnadatabase["association"] : false, |
|
|
|
|
101
|
12 |
|
"associationtable" => isset($colonnadatabase["associationtable"]) ? $colonnadatabase["associationtable"] : null, |
|
|
|
|
102
|
|
|
"decodifiche" => null, |
|
|
|
|
103
|
12 |
|
"escluso" => ($ricursione === true) ? true : substr($colonnadatabase["fieldName"], -3) == "_id" ? true : false, |
|
|
|
|
104
|
|
|
); |
105
|
12 |
|
} |
106
|
6 |
|
private function elaboraJoin(&$opzionibuilder, $colonnadatabase, $ancestors) |
107
|
|
|
{ |
108
|
6 |
|
$entitycollegata = $colonnadatabase["associationtable"]["targetEntity"]; |
|
|
|
|
109
|
6 |
|
$utils = new EntityUtils($this->em, $entitycollegata); |
|
|
|
|
110
|
6 |
|
$tablecollegataname = $this->em->getClassMetadata($entitycollegata)->getTableName(); |
111
|
6 |
|
$colonnecollegate = $utils->getEntityColumns($entitycollegata); |
|
|
|
|
112
|
6 |
|
foreach ($colonnecollegate as $colonnacorrente) { |
113
|
6 |
|
if (!isset($colonnacorrente["type"])) { |
|
|
|
|
114
|
5 |
|
$this->setOpzioniTabellaDefault($colonnacorrente, $opzionibuilder, $tablecollegataname, true, $ancestors); |
115
|
5 |
|
continue; |
116
|
|
|
} |
117
|
|
|
|
118
|
6 |
|
if (!in_array($tablecollegataname, $ancestors)) { |
119
|
6 |
|
$ancestors[] = $tablecollegataname; |
120
|
|
|
} |
121
|
6 |
|
$nomecampo = ucfirst(implode(".", $ancestors)) . "." . $colonnacorrente["fieldName"]; |
|
|
|
|
122
|
6 |
|
$opzionibuilder[$nomecampo] = array( |
123
|
6 |
|
"tipocampo" => $colonnacorrente["type"], |
|
|
|
|
124
|
6 |
|
"nomecampo" => $nomecampo, |
|
|
|
|
125
|
6 |
|
"nometabella" => $tablecollegataname, |
|
|
|
|
126
|
6 |
|
"entityclass" => $colonnadatabase["entityClass"], |
|
|
|
|
127
|
6 |
|
"sourceentityclass" => isset($colonnadatabase["sourceEntityClass"]) ? $colonnadatabase["sourceEntityClass"] : null, |
|
|
|
|
128
|
|
|
"ordine" => null, |
|
|
|
|
129
|
6 |
|
"etichetta" => ucfirst($colonnacorrente["columnName"]), |
|
|
|
|
130
|
6 |
|
"larghezza" => 0, |
|
|
|
|
131
|
|
|
"association" => null, |
|
|
|
|
132
|
|
|
"associationtable" => null, |
|
|
|
|
133
|
|
|
"escluso" => true, |
|
|
|
|
134
|
|
|
); |
135
|
|
|
} |
136
|
6 |
|
} |
137
|
12 |
|
protected function setOrdinaColonneTabella(&$opzionibuilder) |
138
|
|
|
{ |
139
|
12 |
|
foreach ($opzionibuilder as $key => $opzione) { |
140
|
12 |
|
if ($opzione["ordine"] === null) { |
|
|
|
|
141
|
12 |
|
$newordine = $this->getMaxOrdine() + 10; |
|
|
|
|
142
|
12 |
|
$opzionibuilder[$key]["ordine"] = $newordine; |
|
|
|
|
143
|
12 |
|
$this->setMaxOrdine($newordine); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
// Ordinamento per colonna ordine |
147
|
12 |
|
ArrayUtils::sortMultiAssociativeArray($opzionibuilder, "ordine", true); |
|
|
|
|
148
|
12 |
|
} |
149
|
5 |
|
private function bonificaNomeCampo($nomecampo) |
150
|
|
|
{ |
151
|
5 |
|
$parti = explode(".", $nomecampo); |
|
|
|
|
152
|
5 |
|
$campo = ""; |
|
|
|
|
153
|
5 |
|
for ($index = 0; $index < count($parti); $index++) { |
|
|
|
|
154
|
5 |
|
if ($index == count($parti) - 1) { |
155
|
5 |
|
$campo .= "." . lcfirst($parti[$index]); |
|
|
|
|
156
|
|
|
} else { |
157
|
5 |
|
$campo .= "." . ucfirst($parti[$index]); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
} |
160
|
5 |
|
return substr($campo, 1); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.