1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Utils\Tabella; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
6
|
|
|
use Fi\CoreBundle\Utils\Tabella\ParametriQueryTabellaDecoder; |
7
|
|
|
|
8
|
|
|
class Tabella extends TabellaOpzioni |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
protected $entityname; |
12
|
|
|
protected $tablename; |
13
|
|
|
protected $modellocolonne; |
14
|
|
|
protected $paginacorrente; |
15
|
|
|
protected $righeperpagina; |
16
|
|
|
protected $estraituttirecords; |
17
|
|
|
protected $prefiltri; |
18
|
|
|
protected $filtri; |
19
|
|
|
protected $wheremanuale; |
20
|
|
|
protected $colonneordinamento; |
21
|
|
|
|
22
|
|
|
protected function getTabellaParameter($name, $default = null) |
23
|
|
|
{ |
24
|
|
|
$risposta = null; |
25
|
|
|
if (isset($this->parametri[$name])) { |
26
|
|
|
$risposta = ParametriTabella::getParameter($this->parametri[$name]); |
27
|
|
|
} else { |
28
|
|
|
$risposta = $default; |
29
|
|
|
} |
30
|
|
|
return $risposta; |
31
|
|
|
} |
32
|
|
|
|
33
|
12 |
|
protected function fifreeQueryBuilder() |
34
|
|
|
{ |
35
|
|
|
//$nometabellaalias = $this->em->getClassMetadata($this->entityname)->getTableName(); |
|
|
|
|
36
|
|
|
|
37
|
12 |
|
$nometabellaalias = $this->generaAlias($this->tablename); |
38
|
12 |
|
$qb = $this->em->createQueryBuilder() |
|
|
|
|
39
|
12 |
|
->select(array($nometabellaalias)) |
40
|
12 |
|
->from($this->entityname, $nometabellaalias); |
41
|
12 |
|
$campi = array_keys($this->em->getMetadataFactory()->getMetadataFor($this->entityname)->reflFields); |
|
|
|
|
42
|
12 |
|
$this->recursiveJoin($qb, $campi, $this->tablename, $nometabellaalias); |
43
|
12 |
|
$this->buildWhere($qb); |
44
|
12 |
|
$this->orderByBuilder($qb); |
45
|
12 |
|
return $qb; |
46
|
|
|
} |
47
|
12 |
|
protected function recursiveJoin(&$qb, $campi, $nometabella, $alias, $ancestors = array()) |
48
|
|
|
{ |
49
|
12 |
|
foreach ($campi as $campo) { |
50
|
12 |
|
if (!in_array($nometabella, $ancestors)) { |
51
|
12 |
|
$ancestors[] = $nometabella; |
52
|
|
|
} |
53
|
|
|
|
54
|
12 |
|
$configurazionecampo = isset($this->configurazionecolonnetabella[ucfirst(implode(".", $ancestors)) . "." . $campo]) ? |
|
|
|
|
55
|
12 |
|
$this->configurazionecolonnetabella[ucfirst(implode(".", $ancestors)) . "." . $campo] : false; |
|
|
|
|
56
|
12 |
|
if ($configurazionecampo && $configurazionecampo["association"] === true) { |
|
|
|
|
57
|
|
|
// crea la relazione con $padre = $nometabella in corso e figlio = $nomecampo con $alias generato |
58
|
6 |
|
if ((isset($configurazionecampo["sourceentityclass"])) && ($configurazionecampo["sourceentityclass"] !== null)) { |
|
|
|
|
59
|
6 |
|
$entitysrc = $configurazionecampo["sourceentityclass"]; |
|
|
|
|
60
|
6 |
|
$nometabellasrc = $this->em->getClassMetadata($entitysrc)->getTableName(); |
61
|
|
|
} else { |
62
|
|
|
$nometabellasrc = $nometabella; |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
$entitytarget = $configurazionecampo["associationtable"]["targetEntity"]; |
|
|
|
|
66
|
6 |
|
$nometabellatarget = $this->em->getClassMetadata($entitytarget)->getTableName(); |
67
|
6 |
|
$aliastarget = $this->generaAlias($nometabellatarget, $nometabellasrc, $ancestors); |
|
|
|
|
68
|
|
|
//$qb->leftJoin($alias . "." . $configurazionecampo["nomecampo"], $aliastarget); |
|
|
|
|
69
|
|
|
//$camporelazionejoin = strtolower(substr($configurazionecampo["nomecampo"], strpos($configurazionecampo["nomecampo"], ".") + 1)); |
|
|
|
|
70
|
6 |
|
$parti = explode(".", $configurazionecampo["nomecampo"]); |
|
|
|
|
71
|
|
|
|
72
|
6 |
|
$camporelazionejoin = strtolower($parti[count($parti) - 1]); |
73
|
6 |
|
$qb->leftJoin($alias . "." . $camporelazionejoin, $aliastarget); |
|
|
|
|
74
|
6 |
|
$campitarget = array_keys($this->em->getMetadataFactory()->getMetadataFor($entitytarget)->reflFields); |
75
|
12 |
|
$this->recursiveJoin($qb, $campitarget, $nometabellatarget, $aliastarget, $ancestors); |
76
|
|
|
|
77
|
|
|
// lancia rescursiveJoin su questo campo con padre = $aliasgenerato |
78
|
|
|
// --- figlio = $nomecampo |
79
|
|
|
// --- alias = alias generato nuovo |
80
|
|
|
} |
81
|
|
|
} |
82
|
12 |
|
} |
83
|
12 |
|
protected function buildWhere(&$qb) |
84
|
|
|
{ |
85
|
12 |
|
$filtro = ""; |
|
|
|
|
86
|
12 |
|
$prefiltro = ""; |
|
|
|
|
87
|
12 |
|
foreach ($this->prefiltri as $key => $prefiltro) { |
88
|
2 |
|
$this->prefiltri[$key]["prefiltro"] = true; |
|
|
|
|
89
|
|
|
} |
90
|
12 |
|
foreach ($this->filtri as $key => $filtro) { |
91
|
2 |
|
$this->filtri[$key]["prefiltro"] = false; |
|
|
|
|
92
|
|
|
} |
93
|
12 |
|
$tuttifiltri = array_merge($this->filtri, $this->prefiltri); |
|
|
|
|
94
|
12 |
|
$parametribag = array(); |
95
|
12 |
|
if (count($tuttifiltri)) { |
96
|
3 |
|
$descrizionefiltri = ""; |
|
|
|
|
97
|
3 |
|
foreach ($tuttifiltri as $num => $filtrocorrente) { |
98
|
3 |
|
$tablename = substr($filtrocorrente["nomecampo"], 0, strripos($filtrocorrente["nomecampo"], ".")); |
|
|
|
|
99
|
3 |
|
$alias = $this->findAliasByTablename($tablename); |
|
|
|
|
100
|
3 |
|
$fieldname = $alias . "." . (substr($filtrocorrente["nomecampo"], strripos($filtrocorrente["nomecampo"], ".") + 1)); |
|
|
|
|
101
|
3 |
|
$fieldvalue = $this->getFieldValue($filtrocorrente["valore"]); |
|
|
|
|
102
|
3 |
|
$fieldoperator = $this->getOperator($filtrocorrente["operatore"]); |
|
|
|
|
103
|
3 |
|
$fitrocorrenteqp = "fitrocorrente" . $num; |
|
|
|
|
104
|
3 |
|
$filtronomecampocorrente = $this->findFieldnameByAlias($filtrocorrente["nomecampo"]); |
|
|
|
|
105
|
|
|
|
106
|
3 |
|
$criteria = new ParametriQueryTabellaDecoder( |
107
|
3 |
|
$fieldname, |
108
|
3 |
|
$fieldoperator, |
109
|
3 |
|
$fieldvalue, |
110
|
3 |
|
$fitrocorrenteqp, |
111
|
3 |
|
$filtronomecampocorrente |
112
|
|
|
); |
113
|
|
|
|
114
|
3 |
|
$querycriteria = $criteria->getQueryCriteria(); |
|
|
|
|
115
|
3 |
|
$queryparameter = $criteria->getQueryParameters(); |
116
|
|
|
|
117
|
3 |
|
if ($querycriteria) { |
118
|
3 |
|
$qb->andWhere($querycriteria); |
119
|
3 |
|
$parametribag = array_merge($queryparameter, $parametribag); |
120
|
|
|
} else { |
121
|
2 |
|
$qb->andWhere($fieldname . " " . $fieldoperator . " " . ":$fitrocorrenteqp"); |
|
|
|
|
122
|
2 |
|
$parametribag = array_merge(array($fitrocorrenteqp => $fieldvalue), $parametribag); |
123
|
|
|
} |
124
|
3 |
|
$this->getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria); |
125
|
|
|
} |
126
|
3 |
|
$this->traduzionefiltri = substr($descrizionefiltri, 2); |
127
|
|
|
} |
128
|
12 |
|
$qb->setParameters($parametribag); |
129
|
|
|
|
130
|
12 |
|
if (isset($this->wheremanuale)) { |
131
|
|
|
$qb->andWhere($this->wheremanuale); |
132
|
|
|
} |
133
|
12 |
|
} |
134
|
12 |
|
protected function orderByBuilder(&$qb) |
135
|
|
|
{ |
136
|
12 |
|
foreach ($this->colonneordinamento as $nomecampo => $tipoordinamento) { |
137
|
10 |
|
$tablename = substr($nomecampo, 0, strripos($nomecampo, ".")); |
|
|
|
|
138
|
10 |
|
$alias = $this->getAliasGenerato($tablename); |
|
|
|
|
139
|
10 |
|
$fieldname = $alias . "." . (substr($nomecampo, strripos($nomecampo, ".") + 1)); |
|
|
|
|
140
|
10 |
|
$qb->addOrderBy($fieldname, $tipoordinamento); |
141
|
|
|
} |
142
|
12 |
|
} |
143
|
12 |
|
public function getRecordstabella() |
144
|
|
|
{ |
145
|
|
|
|
146
|
12 |
|
$qb = $this->fifreeQueryBuilder(); |
147
|
|
|
|
148
|
12 |
|
if ($this->estraituttirecords === false) { |
149
|
12 |
|
$paginator = new Paginator($qb, true); |
|
|
|
|
150
|
12 |
|
$this->righetotali = count($paginator); |
|
|
|
|
151
|
12 |
|
$this->paginetotali = (int) $this->calcolaPagineTotali($this->getRigheperpagina()); |
152
|
|
|
/* imposta l'offset, ovvero il record dal quale iniziare a visualizzare i dati */ |
153
|
12 |
|
$offsetrecords = ($this->getRigheperpagina() * ($this->getPaginacorrente() - 1)); |
154
|
|
|
|
155
|
|
|
/* Imposta il limite ai record da estrarre */ |
156
|
12 |
|
if ($this->getRigheperpagina()) { |
157
|
12 |
|
$qb = $qb->setMaxResults($this->getRigheperpagina()); |
158
|
|
|
} |
159
|
|
|
/* E imposta il primo record da visualizzare (per la paginazione) */ |
160
|
12 |
|
if ($offsetrecords) { |
161
|
|
|
$qb = $qb->setFirstResult($offsetrecords); |
162
|
|
|
} |
163
|
|
|
/* Dall'oggetto querybuilder si ottiene la query da eseguire */ |
164
|
12 |
|
$recordsets = $qb->getQuery()->getResult(); |
165
|
|
|
} else { |
166
|
|
|
/* Dall'oggetto querybuilder si ottiene la query da eseguire */ |
167
|
4 |
|
$recordsets = $qb->getQuery()->getResult(); |
|
|
|
|
168
|
4 |
|
$this->righetotali = count($recordsets); |
|
|
|
|
169
|
4 |
|
$this->paginetotali = 1; |
170
|
|
|
} |
171
|
|
|
|
172
|
12 |
|
$this->records = array(); |
|
|
|
|
173
|
12 |
|
$rigatabellahtml = array(); |
174
|
12 |
|
foreach ($recordsets as $record) { |
175
|
12 |
|
$this->records[$record->getId()] = $record; |
176
|
12 |
|
unset($rigatabellahtml); |
177
|
|
|
} |
178
|
12 |
|
return $this->records; |
179
|
|
|
} |
180
|
12 |
|
public function calcolaPagineTotali($limit) |
181
|
|
|
{ |
182
|
12 |
|
if ($this->righetotali == 0) { |
183
|
1 |
|
return 1; |
184
|
|
|
} |
185
|
|
|
/* calcola in mumero di pagine totali necessarie */ |
186
|
12 |
|
return ceil($this->righetotali / ($limit == 0 ? 1 : $limit)); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.