|
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\StdClass\StdClassUtils; |
|
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
|
|
|
|
|
21
|
2 |
|
public function __construct($doctrine, $parametri = '{}') |
|
22
|
|
|
{ |
|
23
|
2 |
|
$this->parametri = $parametri; |
|
24
|
|
|
|
|
25
|
2 |
|
if (isset($this->parametri['em'])) { |
|
26
|
2 |
|
$this->em = $doctrine->getManager(ParametriTabella::getParameter($this->parametri['em'])); |
|
27
|
|
|
} else { |
|
28
|
|
|
$this->em = $doctrine->getManager(); |
|
29
|
|
|
} |
|
30
|
2 |
|
$this->parseParameters(); |
|
31
|
2 |
|
$this->colonnedatabase = $this->getColonneDatabase(); |
|
|
|
|
|
|
32
|
2 |
|
$this->opzionitabellacore = $this->getOpzionitabellaFromCore(); |
|
|
|
|
|
|
33
|
2 |
|
$this->configurazionecolonnetabella = $this->getAllOpzioniTabella(); |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
public function getRecordstabella() |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
2 |
|
$qb = $this->fifreeQueryBuilder(); |
|
40
|
|
|
|
|
41
|
2 |
|
$paginator = new Paginator($qb, true); |
|
|
|
|
|
|
42
|
2 |
|
$this->righetotali = count($paginator); |
|
|
|
|
|
|
43
|
2 |
|
$this->paginetotali = (int) $this->calcolaPagineTotali($this->getRigheperpagina()); |
|
44
|
|
|
/* imposta l'offset, ovvero il record dal quale iniziare a visualizzare i dati */ |
|
45
|
2 |
|
$offsetrecords = ($this->getRigheperpagina() * ($this->getPaginacorrente() - 1)); |
|
46
|
|
|
|
|
47
|
|
|
/* Imposta il limite ai record da estrarre */ |
|
48
|
2 |
|
if ($this->getRigheperpagina()) { |
|
49
|
2 |
|
$qb = $qb->setMaxResults($this->getRigheperpagina()); |
|
50
|
|
|
} |
|
51
|
|
|
/* E imposta il primo record da visualizzare (per la paginazione) */ |
|
52
|
2 |
|
if ($offsetrecords) { |
|
53
|
|
|
$qb = $qb->setFirstResult($offsetrecords); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
$this->orderByBuilder($qb); |
|
57
|
|
|
|
|
58
|
|
|
/* Dall'oggetto querybuilder si ottiene la query da eseguire */ |
|
59
|
2 |
|
$recordsets = $qb->getQuery()->getResult(); |
|
60
|
|
|
|
|
61
|
2 |
|
$this->records = array(); |
|
|
|
|
|
|
62
|
2 |
|
$rigatabellahtml = array(); |
|
63
|
2 |
|
foreach ($recordsets as $record) { |
|
64
|
2 |
|
$this->records[$record->getId()] = $record; |
|
65
|
2 |
|
unset($rigatabellahtml); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
return $this->records; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
private function orderByBuilder(&$qb) |
|
72
|
|
|
{ |
|
73
|
2 |
|
foreach ($this->colonneordinamento as $nomecampo => $tipoordinamento) { |
|
74
|
1 |
|
$qb->addOrderBy($nomecampo, $tipoordinamento); |
|
75
|
|
|
} |
|
76
|
2 |
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
private function fifreeQueryBuilder() |
|
79
|
|
|
{ |
|
80
|
|
|
/* @var $this->em \Doctrine\ORM\EntityManager */ |
|
81
|
|
|
/* @var $qb \Doctrine\ORM\QueryBuilder */ |
|
82
|
2 |
|
$qb = $this->em->createQueryBuilder() |
|
83
|
2 |
|
->select(array($this->tablename)) |
|
84
|
2 |
|
->from($this->entityname, $this->tablename) |
|
85
|
|
|
//->where("a.id = :valoreid") |
|
86
|
|
|
//->setParameter('valoreid', 1) |
|
|
|
|
|
|
87
|
|
|
; |
|
88
|
2 |
|
$this->buildLeftJoin($qb); |
|
89
|
|
|
|
|
90
|
2 |
|
$this->buildWhere($qb); |
|
91
|
|
|
|
|
92
|
|
|
//dump($qb->getQuery()->getSQL());exit; |
|
|
|
|
|
|
93
|
2 |
|
return $qb; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
private function buildLeftJoin(&$qb) |
|
97
|
|
|
{ |
|
98
|
2 |
|
$fieldname = ""; |
|
|
|
|
|
|
99
|
|
|
//Aggiungere left join per tabelle collegate nel caso ci siano |
|
100
|
2 |
|
foreach ($this->configurazionecolonnetabella as $fieldname => $configurazionecampo) { |
|
101
|
2 |
|
if ($configurazionecampo["association"] === true) { |
|
|
|
|
|
|
102
|
|
|
$nometabellasrc = $this->em->getClassMetadata($configurazionecampo["associationtable"]["targetEntity"])->getTableName(); |
|
|
|
|
|
|
103
|
|
|
//$this->getEntitynameFromEntity $nometabellasrc |
|
104
|
|
|
//Attenzione: si presuppone che la tabella collegata sia mappata nelle entity (default con mysqlworkbench exporter) |
|
105
|
|
|
//con la lettera minuscola |
|
106
|
|
|
if (false !== strpos($configurazionecampo["associationtable"]["targetEntity"], 'Fi\CoreBundle')) { |
|
|
|
|
|
|
107
|
|
|
$qb->leftJoin($this->tablename . "." . $configurazionecampo["nomecampo"], $configurazionecampo["nomecampo"]); |
|
|
|
|
|
|
108
|
|
|
} else { |
|
109
|
2 |
|
$qb->leftJoin($this->tablename . "." . strtolower($nometabellasrc), $nometabellasrc); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
//$qb->leftJoin($this->tablename . "." . strtolower($nometabellasrc), $nometabellasrc); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
2 |
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
private function buildWhere(&$qb) |
|
118
|
|
|
{ |
|
119
|
2 |
|
$filtro = ""; |
|
|
|
|
|
|
120
|
2 |
|
$prefiltro = ""; |
|
|
|
|
|
|
121
|
2 |
|
foreach ($this->prefiltri as $key => $prefiltro) { |
|
122
|
1 |
|
$this->prefiltri[$key]["prefiltro"] = true; |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
2 |
|
foreach ($this->filtri as $key => $filtro) { |
|
125
|
1 |
|
$this->filtri[$key]["prefiltro"] = false; |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
2 |
|
$tuttifiltri = array_merge($this->filtri, $this->prefiltri); |
|
|
|
|
|
|
128
|
2 |
|
$parametribag = array(); |
|
129
|
|
|
//dump($tuttifiltri);exit; |
|
|
|
|
|
|
130
|
2 |
|
if (count($tuttifiltri)) { |
|
131
|
1 |
|
$descrizionefiltri = ""; |
|
|
|
|
|
|
132
|
1 |
|
foreach ($tuttifiltri as $num => $filtrocorrente) { |
|
133
|
|
|
//dump($filtrocorrente);exit; |
|
|
|
|
|
|
134
|
1 |
|
$fieldname = $filtrocorrente["nomecampo"]; |
|
|
|
|
|
|
135
|
1 |
|
$fieldvalue = $this->getFieldValue($filtrocorrente["valore"]); |
|
|
|
|
|
|
136
|
1 |
|
$fieldoperator = $filtrocorrente["operatore"]; |
|
|
|
|
|
|
137
|
1 |
|
$fitrocorrenteqp = "fitrocorrente" . $num; |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
1 |
|
$criteria = new ParametriQueryTabellaDecoder( |
|
140
|
1 |
|
$fieldname, |
|
141
|
1 |
|
$fieldoperator, |
|
142
|
1 |
|
$fieldvalue, |
|
143
|
1 |
|
$fitrocorrenteqp, |
|
144
|
1 |
|
$this->configurazionecolonnetabella[$fieldname] |
|
145
|
|
|
); |
|
146
|
|
|
|
|
147
|
1 |
|
$querycriteria = $criteria->getQueryCriteria(); |
|
|
|
|
|
|
148
|
1 |
|
$queryparameter = $criteria->getQueryParameters(); |
|
149
|
|
|
//dump($querycriteria); |
|
150
|
|
|
//dump($queryparameter); |
|
151
|
|
|
|
|
152
|
1 |
|
if ($querycriteria) { |
|
153
|
1 |
|
$qb->andWhere($querycriteria); |
|
154
|
|
|
//if (count($queryparameter) > 0) { |
|
|
|
|
|
|
155
|
1 |
|
$parametribag = array_merge($queryparameter, $parametribag); |
|
156
|
|
|
//} |
|
157
|
|
|
} else { |
|
158
|
1 |
|
$qb->andWhere($fieldname . " " . $fieldoperator . " " . ":$fitrocorrenteqp"); |
|
|
|
|
|
|
159
|
1 |
|
$parametribag = array_merge(array($fitrocorrenteqp => $fieldvalue), $parametribag); |
|
160
|
|
|
//$qb->setParameter($fitrocorrenteqp, $fieldvalue); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
1 |
|
$this->getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria); |
|
163
|
|
|
} |
|
164
|
1 |
|
$this->traduzionefiltri = substr($descrizionefiltri, 2); |
|
165
|
|
|
} |
|
166
|
|
|
//if (count($parametribag) > 0) { |
|
|
|
|
|
|
167
|
2 |
|
$qb->setParameters($parametribag); |
|
168
|
|
|
//} |
|
169
|
|
|
|
|
170
|
2 |
|
if (isset($this->wheremanuale)) { |
|
171
|
|
|
$qb->andWhere($this->wheremanuale); |
|
172
|
|
|
} |
|
173
|
2 |
|
} |
|
174
|
|
|
|
|
175
|
1 |
|
private function getDescrizioneFiltro(&$descrizionefiltri, $filtrocorrente, $criteria) |
|
176
|
|
|
{ |
|
177
|
1 |
|
if ($filtrocorrente["prefiltro"] === false) { |
|
|
|
|
|
|
178
|
1 |
|
$descrizionefiltri = $descrizionefiltri . ", " . $criteria->getDescrizioneFiltro(); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
1 |
|
} |
|
181
|
|
|
|
|
182
|
1 |
|
private function getFieldValue($fieldvalue) |
|
183
|
|
|
{ |
|
184
|
1 |
|
if (isset($fieldvalue["date"])) { |
|
|
|
|
|
|
185
|
|
|
//StdClassUtils::arrayToObject($fieldvalue, DatetimeTabella::class); |
|
|
|
|
|
|
186
|
|
|
//dump(new \Datetime($fieldvalue["date"]));exit; |
|
|
|
|
|
|
187
|
1 |
|
return new \Datetime($fieldvalue["date"]); |
|
|
|
|
|
|
188
|
|
|
} else { |
|
189
|
1 |
|
return $fieldvalue; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
2 |
|
public function calcolaPagineTotali($limit) |
|
194
|
|
|
{ |
|
195
|
2 |
|
if ($this->righetotali == 0) { |
|
196
|
1 |
|
return 1; |
|
197
|
|
|
} |
|
198
|
|
|
/* calcola in mumero di pagine totali necessarie */ |
|
199
|
2 |
|
return ceil($this->righetotali / ($limit == 0 ? 1 : $limit)); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
2 |
|
public function getPaginacorrente() |
|
203
|
|
|
{ |
|
204
|
2 |
|
return $this->paginacorrente; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function getPaginetotali() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->paginetotali; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
2 |
|
public function getRigheperpagina() |
|
213
|
|
|
{ |
|
214
|
2 |
|
return $this->righeperpagina; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
2 |
|
public function getRighetotali() |
|
218
|
|
|
{ |
|
219
|
2 |
|
return $this->righetotali; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function getTraduzionefiltri() |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->traduzionefiltri; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
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.