1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Utils\Tabella; |
4
|
|
|
|
5
|
|
|
use Fi\CoreBundle\Utils\Tabella\ParametriTabella; |
6
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
7
|
|
|
use Fi\CoreBundle\Utils\Entity\EntityUtils; |
8
|
|
|
use Fi\CoreBundle\Utils\Tabella\ParametriQueryTabellaDecoder; |
9
|
|
|
|
10
|
|
|
class Tabella extends BaseTabella |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected $permessi; |
14
|
|
|
protected $records; |
15
|
|
|
protected $paginacorrente; |
16
|
|
|
protected $paginetotali; |
17
|
|
|
protected $righeperpagina; |
18
|
|
|
protected $righetotali; |
19
|
|
|
protected $traduzionefiltri; |
20
|
|
|
|
21
|
11 |
|
public function __construct($doctrine, $parametri = '{}') |
22
|
|
|
{ |
23
|
11 |
|
$this->parametri = $parametri; |
24
|
|
|
|
25
|
11 |
|
if (isset($this->parametri['em'])) { |
26
|
11 |
|
$this->em = $doctrine->getManager(ParametriTabella::getParameter($this->parametri['em'])); |
27
|
|
|
} else { |
28
|
|
|
$this->em = $doctrine->getManager(); |
29
|
|
|
} |
30
|
11 |
|
$this->parseParameters(); |
31
|
11 |
|
$this->colonnedatabase = $this->getColonneDatabase(); |
|
|
|
|
32
|
11 |
|
$this->opzionitabellacore = $this->getOpzionitabellaFromCore(); |
|
|
|
|
33
|
11 |
|
$this->configurazionecolonnetabella = $this->getAllOpzioniTabella(); |
34
|
11 |
|
} |
35
|
11 |
|
protected function fifreeQueryBuilder() |
36
|
|
|
{ |
37
|
|
|
//$nometabellaalias = $this->em->getClassMetadata($this->entityname)->getTableName(); |
|
|
|
|
38
|
|
|
|
39
|
11 |
|
$nometabellaalias = $this->generaAlias($this->tablename); |
40
|
11 |
|
$qb = $this->em->createQueryBuilder() |
|
|
|
|
41
|
11 |
|
->select(array($nometabellaalias)) |
42
|
11 |
|
->from($this->entityname, $nometabellaalias); |
43
|
11 |
|
$campi = array_keys($this->em->getMetadataFactory()->getMetadataFor($this->entityname)->reflFields); |
|
|
|
|
44
|
11 |
|
$this->recursiveJoin($qb, $campi, $this->tablename, $nometabellaalias); |
45
|
11 |
|
$this->buildWhere($qb); |
46
|
11 |
|
$this->orderByBuilder($qb); |
47
|
11 |
|
return $qb; |
48
|
|
|
} |
49
|
11 |
|
protected function recursiveJoin(&$qb, $campi, $nometabella, $alias, $ancestors = array()) |
50
|
|
|
{ |
51
|
11 |
|
foreach ($campi as $campo) { |
52
|
11 |
|
if (!in_array($nometabella, $ancestors)) { |
53
|
11 |
|
$ancestors[] = $nometabella; |
54
|
|
|
} |
55
|
|
|
|
56
|
11 |
|
$configurazionecampo = isset($this->configurazionecolonnetabella[ucfirst(implode(".", $ancestors)) . "." . $campo]) ? |
|
|
|
|
57
|
11 |
|
$this->configurazionecolonnetabella[ucfirst(implode(".", $ancestors)) . "." . $campo] : false; |
|
|
|
|
58
|
11 |
|
if ($configurazionecampo && $configurazionecampo["association"] === true) { |
|
|
|
|
59
|
|
|
// crea la relazione con $padre = $nometabella in corso e figlio = $nomecampo con $alias generato |
60
|
5 |
|
if ((isset($configurazionecampo["sourceentityclass"])) && ($configurazionecampo["sourceentityclass"] !== null)) { |
|
|
|
|
61
|
5 |
|
$entitysrc = $configurazionecampo["sourceentityclass"]; |
|
|
|
|
62
|
5 |
|
$nometabellasrc = $this->em->getClassMetadata($entitysrc)->getTableName(); |
63
|
|
|
} else { |
64
|
|
|
$nometabellasrc = $nometabella; |
65
|
|
|
} |
66
|
|
|
|
67
|
5 |
|
$entitytarget = $configurazionecampo["associationtable"]["targetEntity"]; |
|
|
|
|
68
|
5 |
|
$nometabellatarget = $this->em->getClassMetadata($entitytarget)->getTableName(); |
69
|
5 |
|
$aliastarget = $this->generaAlias($nometabellatarget, $nometabellasrc, $ancestors); |
|
|
|
|
70
|
|
|
//$qb->leftJoin($alias . "." . $configurazionecampo["nomecampo"], $aliastarget); |
|
|
|
|
71
|
|
|
//$camporelazionejoin = strtolower(substr($configurazionecampo["nomecampo"], strpos($configurazionecampo["nomecampo"], ".") + 1)); |
|
|
|
|
72
|
5 |
|
$parti = explode(".", $configurazionecampo["nomecampo"]); |
|
|
|
|
73
|
|
|
|
74
|
5 |
|
$camporelazionejoin = strtolower($parti[count($parti) - 1]); |
75
|
5 |
|
$qb->leftJoin($alias . "." . $camporelazionejoin, $aliastarget); |
|
|
|
|
76
|
5 |
|
$campitarget = array_keys($this->em->getMetadataFactory()->getMetadataFor($entitytarget)->reflFields); |
77
|
11 |
|
$this->recursiveJoin($qb, $campitarget, $nometabellatarget, $aliastarget, $ancestors); |
78
|
|
|
|
79
|
|
|
// lancia rescursiveJoin su questo campo con padre = $aliasgenerato |
80
|
|
|
// --- figlio = $nomecampo |
81
|
|
|
// --- alias = alias generato nuovo |
82
|
|
|
} |
83
|
|
|
} |
84
|
11 |
|
} |
85
|
11 |
|
protected function buildWhere(&$qb) |
86
|
|
|
{ |
87
|
11 |
|
$filtro = ""; |
|
|
|
|
88
|
11 |
|
$prefiltro = ""; |
|
|
|
|
89
|
11 |
|
foreach ($this->prefiltri as $key => $prefiltro) { |
90
|
2 |
|
$this->prefiltri[$key]["prefiltro"] = true; |
|
|
|
|
91
|
|
|
} |
92
|
11 |
|
foreach ($this->filtri as $key => $filtro) { |
93
|
2 |
|
$this->filtri[$key]["prefiltro"] = false; |
|
|
|
|
94
|
|
|
} |
95
|
11 |
|
$tuttifiltri = array_merge($this->filtri, $this->prefiltri); |
|
|
|
|
96
|
11 |
|
$parametribag = array(); |
97
|
11 |
|
if (count($tuttifiltri)) { |
98
|
3 |
|
$descrizionefiltri = ""; |
|
|
|
|
99
|
3 |
|
foreach ($tuttifiltri as $num => $filtrocorrente) { |
100
|
3 |
|
$tablename = substr($filtrocorrente["nomecampo"], 0, strripos($filtrocorrente["nomecampo"], ".")); |
|
|
|
|
101
|
3 |
|
$alias = $this->findAliasByTablename($tablename); |
|
|
|
|
102
|
3 |
|
$fieldname = $alias . "." . (substr($filtrocorrente["nomecampo"], strripos($filtrocorrente["nomecampo"], ".") + 1)); |
|
|
|
|
103
|
3 |
|
$fieldvalue = $this->getFieldValue($filtrocorrente["valore"]); |
|
|
|
|
104
|
3 |
|
$fieldoperator = $this->getOperator($filtrocorrente["operatore"]); |
|
|
|
|
105
|
3 |
|
$fitrocorrenteqp = "fitrocorrente" . $num; |
|
|
|
|
106
|
3 |
|
$filtronomecampocorrente = $this->findFieldnameByAlias($filtrocorrente["nomecampo"]); |
|
|
|
|
107
|
|
|
|
108
|
3 |
|
$criteria = new ParametriQueryTabellaDecoder( |
109
|
3 |
|
$fieldname, |
110
|
3 |
|
$fieldoperator, |
111
|
3 |
|
$fieldvalue, |
112
|
3 |
|
$fitrocorrenteqp, |
113
|
3 |
|
$filtronomecampocorrente |
114
|
|
|
); |
115
|
|
|
|
116
|
3 |
|
$querycriteria = $criteria->getQueryCriteria(); |
|
|
|
|
117
|
3 |
|
$queryparameter = $criteria->getQueryParameters(); |
118
|
|
|
|
119
|
3 |
|
if ($querycriteria) { |
120
|
3 |
|
$qb->andWhere($querycriteria); |
121
|
3 |
|
$parametribag = array_merge($queryparameter, $parametribag); |
122
|
|
|
} else { |
123
|
2 |
|
$qb->andWhere($fieldname . " " . $fieldoperator . " " . ":$fitrocorrenteqp"); |
|
|
|
|
124
|
2 |
|
$parametribag = array_merge(array($fitrocorrenteqp => $fieldvalue), $parametribag); |
125
|
|
|
} |
126
|
3 |
|
$this->getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria); |
127
|
|
|
} |
128
|
3 |
|
$this->traduzionefiltri = substr($descrizionefiltri, 2); |
129
|
|
|
} |
130
|
11 |
|
$qb->setParameters($parametribag); |
131
|
|
|
|
132
|
11 |
|
if (isset($this->wheremanuale)) { |
133
|
|
|
$qb->andWhere($this->wheremanuale); |
134
|
|
|
} |
135
|
11 |
|
} |
136
|
11 |
|
protected function orderByBuilder(&$qb) |
137
|
|
|
{ |
138
|
11 |
|
foreach ($this->colonneordinamento as $nomecampo => $tipoordinamento) { |
139
|
9 |
|
$tablename = substr($nomecampo, 0, strripos($nomecampo, ".")); |
|
|
|
|
140
|
9 |
|
$alias = $this->getAliasGenerato($tablename); |
|
|
|
|
141
|
9 |
|
$fieldname = $alias . "." . (substr($nomecampo, strripos($nomecampo, ".") + 1)); |
|
|
|
|
142
|
9 |
|
$qb->addOrderBy($fieldname, $tipoordinamento); |
143
|
|
|
} |
144
|
11 |
|
} |
145
|
11 |
|
public function getRecordstabella() |
146
|
|
|
{ |
147
|
|
|
|
148
|
11 |
|
$qb = $this->fifreeQueryBuilder(); |
149
|
|
|
|
150
|
11 |
|
$paginator = new Paginator($qb, true); |
|
|
|
|
151
|
11 |
|
$this->righetotali = count($paginator); |
|
|
|
|
152
|
11 |
|
$this->paginetotali = (int) $this->calcolaPagineTotali($this->getRigheperpagina()); |
153
|
|
|
/* imposta l'offset, ovvero il record dal quale iniziare a visualizzare i dati */ |
154
|
11 |
|
$offsetrecords = ($this->getRigheperpagina() * ($this->getPaginacorrente() - 1)); |
155
|
|
|
|
156
|
|
|
/* Imposta il limite ai record da estrarre */ |
157
|
11 |
|
if ($this->getRigheperpagina()) { |
158
|
11 |
|
$qb = $qb->setMaxResults($this->getRigheperpagina()); |
159
|
|
|
} |
160
|
|
|
/* E imposta il primo record da visualizzare (per la paginazione) */ |
161
|
11 |
|
if ($offsetrecords) { |
162
|
|
|
$qb = $qb->setFirstResult($offsetrecords); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/* Dall'oggetto querybuilder si ottiene la query da eseguire */ |
166
|
11 |
|
$recordsets = $qb->getQuery()->getResult(); |
167
|
|
|
|
168
|
11 |
|
$this->records = array(); |
|
|
|
|
169
|
11 |
|
$rigatabellahtml = array(); |
170
|
11 |
|
foreach ($recordsets as $record) { |
171
|
11 |
|
$this->records[$record->getId()] = $record; |
172
|
11 |
|
unset($rigatabellahtml); |
173
|
|
|
} |
174
|
11 |
|
return $this->records; |
175
|
|
|
} |
176
|
11 |
|
public function calcolaPagineTotali($limit) |
177
|
|
|
{ |
178
|
11 |
|
if ($this->righetotali == 0) { |
179
|
1 |
|
return 1; |
180
|
|
|
} |
181
|
|
|
/* calcola in mumero di pagine totali necessarie */ |
182
|
11 |
|
return ceil($this->righetotali / ($limit == 0 ? 1 : $limit)); |
183
|
|
|
} |
184
|
11 |
|
public function getPaginacorrente() |
185
|
|
|
{ |
186
|
11 |
|
return $this->paginacorrente; |
187
|
|
|
} |
188
|
6 |
|
public function getPaginetotali() |
189
|
|
|
{ |
190
|
6 |
|
return $this->paginetotali; |
191
|
|
|
} |
192
|
11 |
|
public function getRigheperpagina() |
193
|
|
|
{ |
194
|
11 |
|
return $this->righeperpagina; |
195
|
|
|
} |
196
|
11 |
|
public function getRighetotali() |
197
|
|
|
{ |
198
|
11 |
|
return $this->righetotali; |
199
|
|
|
} |
200
|
6 |
|
public function getTraduzionefiltri() |
201
|
|
|
{ |
202
|
6 |
|
return $this->traduzionefiltri; |
203
|
|
|
} |
204
|
11 |
|
public function getColonneDatabase() |
205
|
|
|
{ |
206
|
11 |
|
$utils = new EntityUtils($this->em, $this->entityname); |
|
|
|
|
207
|
11 |
|
return $utils->getEntityColumns($this->entityname); |
208
|
|
|
} |
209
|
9 |
|
public function getConfigurazionecolonnetabella() |
210
|
|
|
{ |
211
|
9 |
|
return $this->configurazionecolonnetabella; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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.