|
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 $paginacorrente; |
|
15
|
|
|
protected $paginetotali; |
|
16
|
|
|
protected $righeperpagina; |
|
17
|
|
|
protected $righetotali; |
|
18
|
|
|
protected $maxordine; |
|
19
|
|
|
protected $traduzionefiltri; |
|
20
|
|
|
private $aliasGenerati; |
|
21
|
|
|
private $decodificaAlias; |
|
22
|
|
|
|
|
23
|
8 |
|
public function __construct($doctrine, $parametri = '{}') |
|
24
|
|
|
{ |
|
25
|
8 |
|
$this->parametri = $parametri; |
|
26
|
|
|
|
|
27
|
8 |
|
if (isset($this->parametri['em'])) { |
|
28
|
8 |
|
$this->em = $doctrine->getManager(ParametriTabella::getParameter($this->parametri['em'])); |
|
29
|
|
|
} else { |
|
30
|
|
|
$this->em = $doctrine->getManager(); |
|
31
|
|
|
} |
|
32
|
8 |
|
$this->parseParameters(); |
|
33
|
8 |
|
$this->colonnedatabase = $this->getColonneDatabase(); |
|
|
|
|
|
|
34
|
8 |
|
$this->opzionitabellacore = $this->getOpzionitabellaFromCore(); |
|
|
|
|
|
|
35
|
8 |
|
$this->configurazionecolonnetabella = $this->getAllOpzioniTabella(); |
|
36
|
8 |
|
} |
|
37
|
|
|
|
|
38
|
8 |
|
public function getRecordstabella() |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
8 |
|
$qb = $this->fifreeQueryBuilder(); |
|
42
|
|
|
|
|
43
|
8 |
|
$paginator = new Paginator($qb, true); |
|
|
|
|
|
|
44
|
8 |
|
$this->righetotali = count($paginator); |
|
|
|
|
|
|
45
|
8 |
|
$this->paginetotali = (int) $this->calcolaPagineTotali($this->getRigheperpagina()); |
|
46
|
|
|
/* imposta l'offset, ovvero il record dal quale iniziare a visualizzare i dati */ |
|
47
|
8 |
|
$offsetrecords = ($this->getRigheperpagina() * ($this->getPaginacorrente() - 1)); |
|
48
|
|
|
|
|
49
|
|
|
/* Imposta il limite ai record da estrarre */ |
|
50
|
8 |
|
if ($this->getRigheperpagina()) { |
|
51
|
8 |
|
$qb = $qb->setMaxResults($this->getRigheperpagina()); |
|
52
|
|
|
} |
|
53
|
|
|
/* E imposta il primo record da visualizzare (per la paginazione) */ |
|
54
|
8 |
|
if ($offsetrecords) { |
|
55
|
|
|
$qb = $qb->setFirstResult($offsetrecords); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/* Dall'oggetto querybuilder si ottiene la query da eseguire */ |
|
59
|
8 |
|
$recordsets = $qb->getQuery()->getResult(); |
|
60
|
|
|
|
|
61
|
8 |
|
$this->records = array(); |
|
|
|
|
|
|
62
|
8 |
|
$rigatabellahtml = array(); |
|
63
|
8 |
|
foreach ($recordsets as $record) { |
|
64
|
8 |
|
$this->records[$record->getId()] = $record; |
|
65
|
8 |
|
unset($rigatabellahtml); |
|
66
|
|
|
} |
|
67
|
8 |
|
return $this->records; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
8 |
|
private function orderByBuilder(&$qb) |
|
71
|
|
|
{ |
|
72
|
8 |
|
foreach ($this->colonneordinamento as $nomecampo => $tipoordinamento) { |
|
73
|
7 |
|
$tablename = substr($nomecampo, 0, strripos($nomecampo, ".")); |
|
|
|
|
|
|
74
|
7 |
|
$alias = $this->decodificaAlias[$tablename]["alias"]; |
|
|
|
|
|
|
75
|
7 |
|
$fieldname = $alias . "." . (substr($nomecampo, strripos($nomecampo, ".") + 1)); |
|
|
|
|
|
|
76
|
7 |
|
$qb->addOrderBy($fieldname, $tipoordinamento); |
|
77
|
|
|
} |
|
78
|
8 |
|
} |
|
79
|
|
|
|
|
80
|
8 |
|
private function fifreeQueryBuilder() |
|
81
|
|
|
{ |
|
82
|
|
|
//$nometabellaalias = $this->em->getClassMetadata($this->entityname)->getTableName(); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
8 |
|
$nometabellaalias = $this->generaAlias($this->tablename); |
|
85
|
8 |
|
$qb = $this->em->createQueryBuilder() |
|
|
|
|
|
|
86
|
8 |
|
->select(array($nometabellaalias)) |
|
87
|
8 |
|
->from($this->entityname, $nometabellaalias); |
|
88
|
8 |
|
$campi = array_keys($this->em->getMetadataFactory()->getMetadataFor($this->entityname)->reflFields); |
|
|
|
|
|
|
89
|
8 |
|
$this->recursiveJoin($qb, $campi, $this->tablename, $nometabellaalias); |
|
90
|
8 |
|
$this->buildWhere($qb); |
|
91
|
8 |
|
$this->orderByBuilder($qb); |
|
92
|
8 |
|
return $qb; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
8 |
|
private function recursiveJoin(&$qb, $campi, $nometabella, $alias, $ancestors = array()) |
|
96
|
|
|
{ |
|
97
|
8 |
|
foreach ($campi as $campo) { |
|
98
|
8 |
|
if (!in_array($nometabella, $ancestors)) { |
|
99
|
8 |
|
$ancestors[] = $nometabella; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
8 |
|
$configurazionecampo = isset($this->configurazionecolonnetabella[ucfirst(implode(".", $ancestors)) . "." . $campo]) ? |
|
|
|
|
|
|
103
|
8 |
|
$this->configurazionecolonnetabella[ucfirst(implode(".", $ancestors)) . "." . $campo] : false; |
|
|
|
|
|
|
104
|
8 |
|
if ($configurazionecampo && $configurazionecampo["association"] === true) { |
|
|
|
|
|
|
105
|
|
|
// crea la relazione con $padre = $nometabella in corso e figlio = $nomecampo con $alias generato |
|
106
|
2 |
|
if ((isset($configurazionecampo["sourceentityclass"])) && ($configurazionecampo["sourceentityclass"] !== null)) { |
|
|
|
|
|
|
107
|
2 |
|
$entitysrc = $configurazionecampo["sourceentityclass"]; |
|
|
|
|
|
|
108
|
2 |
|
$nometabellasrc = $this->em->getClassMetadata($entitysrc)->getTableName(); |
|
109
|
|
|
} else { |
|
110
|
|
|
$nometabellasrc = $nometabella; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
$entitytarget = $configurazionecampo["associationtable"]["targetEntity"]; |
|
|
|
|
|
|
114
|
2 |
|
$nometabellatarget = $this->em->getClassMetadata($entitytarget)->getTableName(); |
|
115
|
2 |
|
$aliastarget = $this->generaAlias($nometabellatarget, $nometabellasrc, $ancestors); |
|
|
|
|
|
|
116
|
|
|
//$qb->leftJoin($alias . "." . $configurazionecampo["nomecampo"], $aliastarget); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
//$camporelazionejoin = strtolower(substr($configurazionecampo["nomecampo"], strpos($configurazionecampo["nomecampo"], ".") + 1)); |
|
|
|
|
|
|
119
|
2 |
|
$parti = explode(".", $configurazionecampo["nomecampo"]); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
2 |
|
$camporelazionejoin = strtolower($parti[count($parti)-1]); |
|
122
|
2 |
|
$qb->leftJoin($alias . "." . $camporelazionejoin, $aliastarget); |
|
|
|
|
|
|
123
|
2 |
|
$campitarget = array_keys($this->em->getMetadataFactory()->getMetadataFor($entitytarget)->reflFields); |
|
124
|
8 |
|
$this->recursiveJoin($qb, $campitarget, $nometabellatarget, $aliastarget, $ancestors); |
|
125
|
|
|
|
|
126
|
|
|
// lancia rescursiveJoin su questo campo con padre = $aliasgenerato |
|
127
|
|
|
// --- figlio = $nomecampo |
|
128
|
|
|
// --- alias = alias generato nuovo |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
8 |
|
} |
|
132
|
|
|
|
|
133
|
8 |
|
private function generaAlias($nometabella, $nomepadre = false, $ancestors = array()) |
|
134
|
|
|
{ |
|
135
|
8 |
|
$nometabellapulito = preg_replace('/[^a-z0-9\.]/i', '', $nometabella); |
|
136
|
8 |
|
$primalettera = strtolower(substr($nometabellapulito, 0, 1)); |
|
|
|
|
|
|
137
|
8 |
|
if ($nomepadre && !in_array($nomepadre, $ancestors)) { |
|
138
|
|
|
$ancestors[] = $nomepadre; |
|
139
|
|
|
} |
|
140
|
8 |
|
if (!in_array($nometabella, $ancestors)) { |
|
141
|
8 |
|
$ancestors[] = $nometabella; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
8 |
|
if (isset($this->aliasGenerati[$primalettera])) { |
|
145
|
2 |
|
$risposta = $primalettera . $this->aliasGenerati[$primalettera]; |
|
|
|
|
|
|
146
|
2 |
|
$this->aliasGenerati[$primalettera] = $this->aliasGenerati[$primalettera] + 1; |
|
147
|
|
|
} else { |
|
148
|
8 |
|
$risposta = $primalettera; |
|
|
|
|
|
|
149
|
8 |
|
$this->aliasGenerati[$primalettera] = 1; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
8 |
|
$this->decodificaAlias[ucfirst(implode(".", $ancestors))] = array("alias" => $risposta); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
8 |
|
return $risposta; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
2 |
|
private function findAliasByTablename($tablename) |
|
158
|
|
|
{ |
|
159
|
2 |
|
if (!array_key_exists($tablename, $this->decodificaAlias)) { |
|
160
|
|
|
$ex = "Fifree: table or association " . $tablename . " not found, did you mean one of these:\n" . |
|
|
|
|
|
|
161
|
|
|
implode("\n", array_keys($this->decodificaAlias)) . |
|
162
|
|
|
" ?"; |
|
|
|
|
|
|
163
|
|
|
throw new \Exception($ex); |
|
164
|
|
|
} |
|
165
|
2 |
|
return $this->decodificaAlias[$tablename]["alias"]; |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
2 |
|
private function findFieldnameByAlias($nomecampo) |
|
169
|
|
|
{ |
|
170
|
2 |
|
if (!array_key_exists($nomecampo, $this->configurazionecolonnetabella)) { |
|
171
|
|
|
$ex = "Fifree: field or association " . $nomecampo . " not found, did you mean one of these:\n" . |
|
|
|
|
|
|
172
|
|
|
implode("\n", array_keys($this->configurazionecolonnetabella)) . |
|
173
|
|
|
" ?"; |
|
|
|
|
|
|
174
|
|
|
throw new \Exception($ex); |
|
175
|
|
|
} |
|
176
|
2 |
|
return $this->configurazionecolonnetabella[$nomecampo]; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
8 |
|
private function buildWhere(&$qb) |
|
180
|
|
|
{ |
|
181
|
8 |
|
$filtro = ""; |
|
|
|
|
|
|
182
|
8 |
|
$prefiltro = ""; |
|
|
|
|
|
|
183
|
8 |
|
foreach ($this->prefiltri as $key => $prefiltro) { |
|
184
|
1 |
|
$this->prefiltri[$key]["prefiltro"] = true; |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
8 |
|
foreach ($this->filtri as $key => $filtro) { |
|
187
|
2 |
|
$this->filtri[$key]["prefiltro"] = false; |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
8 |
|
$tuttifiltri = array_merge($this->filtri, $this->prefiltri); |
|
|
|
|
|
|
190
|
8 |
|
$parametribag = array(); |
|
191
|
8 |
|
if (count($tuttifiltri)) { |
|
192
|
2 |
|
$descrizionefiltri = ""; |
|
|
|
|
|
|
193
|
2 |
|
foreach ($tuttifiltri as $num => $filtrocorrente) { |
|
194
|
2 |
|
$tablename = substr($filtrocorrente["nomecampo"], 0, strripos($filtrocorrente["nomecampo"], ".")); |
|
|
|
|
|
|
195
|
2 |
|
$alias = $this->findAliasByTablename($tablename); |
|
|
|
|
|
|
196
|
2 |
|
$fieldname = $alias . "." . (substr($filtrocorrente["nomecampo"], strripos($filtrocorrente["nomecampo"], ".") + 1)); |
|
|
|
|
|
|
197
|
2 |
|
$fieldvalue = $this->getFieldValue($filtrocorrente["valore"]); |
|
|
|
|
|
|
198
|
2 |
|
$fieldoperator = $filtrocorrente["operatore"]; |
|
|
|
|
|
|
199
|
2 |
|
$fitrocorrenteqp = "fitrocorrente" . $num; |
|
|
|
|
|
|
200
|
2 |
|
$filtronomecampocorrente = $this->findFieldnameByAlias($filtrocorrente["nomecampo"]); |
|
|
|
|
|
|
201
|
|
|
|
|
202
|
2 |
|
$criteria = new ParametriQueryTabellaDecoder( |
|
203
|
2 |
|
$fieldname, |
|
204
|
2 |
|
$fieldoperator, |
|
205
|
2 |
|
$fieldvalue, |
|
206
|
2 |
|
$fitrocorrenteqp, |
|
207
|
2 |
|
$filtronomecampocorrente |
|
208
|
|
|
); |
|
209
|
|
|
|
|
210
|
2 |
|
$querycriteria = $criteria->getQueryCriteria(); |
|
|
|
|
|
|
211
|
2 |
|
$queryparameter = $criteria->getQueryParameters(); |
|
212
|
|
|
|
|
213
|
2 |
|
if ($querycriteria) { |
|
214
|
2 |
|
$qb->andWhere($querycriteria); |
|
215
|
2 |
|
$parametribag = array_merge($queryparameter, $parametribag); |
|
216
|
|
|
} else { |
|
217
|
1 |
|
$qb->andWhere($fieldname . " " . $fieldoperator . " " . ":$fitrocorrenteqp"); |
|
|
|
|
|
|
218
|
1 |
|
$parametribag = array_merge(array($fitrocorrenteqp => $fieldvalue), $parametribag); |
|
219
|
|
|
} |
|
220
|
2 |
|
$this->getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria); |
|
221
|
|
|
} |
|
222
|
2 |
|
$this->traduzionefiltri = substr($descrizionefiltri, 2); |
|
223
|
|
|
} |
|
224
|
8 |
|
$qb->setParameters($parametribag); |
|
225
|
|
|
|
|
226
|
8 |
|
if (isset($this->wheremanuale)) { |
|
227
|
|
|
$qb->andWhere($this->wheremanuale); |
|
228
|
|
|
} |
|
229
|
8 |
|
} |
|
230
|
|
|
|
|
231
|
2 |
|
private function getDescrizioneFiltro(&$descrizionefiltri, $filtrocorrente, $criteria) |
|
232
|
|
|
{ |
|
233
|
2 |
|
if ($filtrocorrente["prefiltro"] === false) { |
|
|
|
|
|
|
234
|
2 |
|
$descrizionefiltri = $descrizionefiltri . ", " . $criteria->getDescrizioneFiltro(); |
|
|
|
|
|
|
235
|
|
|
} |
|
236
|
2 |
|
} |
|
237
|
|
|
|
|
238
|
2 |
|
private function getFieldValue($fieldvalue) |
|
239
|
|
|
{ |
|
240
|
2 |
|
if (isset($fieldvalue["date"])) { |
|
|
|
|
|
|
241
|
|
|
//StdClassUtils::arrayToObject($fieldvalue, DatetimeTabella::class); |
|
|
|
|
|
|
242
|
1 |
|
return new \Datetime($fieldvalue["date"]); |
|
|
|
|
|
|
243
|
|
|
} else { |
|
244
|
2 |
|
return $fieldvalue; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
8 |
|
public function calcolaPagineTotali($limit) |
|
249
|
|
|
{ |
|
250
|
8 |
|
if ($this->righetotali == 0) { |
|
251
|
1 |
|
return 1; |
|
252
|
|
|
} |
|
253
|
|
|
/* calcola in mumero di pagine totali necessarie */ |
|
254
|
8 |
|
return ceil($this->righetotali / ($limit == 0 ? 1 : $limit)); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
8 |
|
public function getPaginacorrente() |
|
258
|
|
|
{ |
|
259
|
8 |
|
return $this->paginacorrente; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
6 |
|
public function getPaginetotali() |
|
263
|
|
|
{ |
|
264
|
6 |
|
return $this->paginetotali; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
8 |
|
public function getRigheperpagina() |
|
268
|
|
|
{ |
|
269
|
8 |
|
return $this->righeperpagina; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
8 |
|
public function getRighetotali() |
|
273
|
|
|
{ |
|
274
|
8 |
|
return $this->righetotali; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
6 |
|
public function getTraduzionefiltri() |
|
278
|
|
|
{ |
|
279
|
6 |
|
return $this->traduzionefiltri; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
8 |
|
public function getColonneDatabase() |
|
283
|
|
|
{ |
|
284
|
8 |
|
$utils = new EntityUtils($this->em, $this->entityname); |
|
|
|
|
|
|
285
|
8 |
|
return $utils->getEntityColumns($this->entityname); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
7 |
|
public function getConfigurazionecolonnetabella() |
|
289
|
|
|
{ |
|
290
|
7 |
|
return $this->configurazionecolonnetabella; |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
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.