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