1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Utils\Tabella; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
6
|
|
|
use Cdf\BiCoreBundle\Utils\Tabella\ParametriQueryTabellaDecoder; |
7
|
|
|
|
8
|
|
|
trait TabellaQueryTrait |
9
|
|
|
{ |
10
|
|
|
|
11
|
12 |
|
protected function biQueryBuilder() |
12
|
|
|
{ |
13
|
12 |
|
$nometabellaalias = $this->generaAlias($this->tablename); |
|
|
|
|
14
|
12 |
|
$qb = $this->em->createQueryBuilder() |
|
|
|
|
15
|
12 |
|
->select(array($nometabellaalias)) |
16
|
12 |
|
->from($this->entityname, $nometabellaalias); |
17
|
12 |
|
$campi = array_keys($this->em->getMetadataFactory()->getMetadataFor($this->entityname)->reflFields); |
|
|
|
|
18
|
12 |
|
$this->recursiveJoin($qb, $campi, $this->tablename, $nometabellaalias); |
19
|
12 |
|
$this->buildWhere($qb); |
20
|
12 |
|
$this->orderByBuilder($qb); |
21
|
12 |
|
return $qb; |
22
|
|
|
} |
23
|
12 |
|
protected function recursiveJoin(&$qb, $campi, $nometabella, $alias, $ancestors = array()) |
24
|
|
|
{ |
25
|
12 |
|
foreach ($campi as $campo) { |
26
|
12 |
|
if (!in_array($nometabella, $ancestors)) { |
27
|
12 |
|
$ancestors[] = $nometabella; |
28
|
|
|
} |
29
|
|
|
|
30
|
12 |
|
$configurazionecampo = isset($this->configurazionecolonnetabella[ucfirst(implode(".", $ancestors)) . "." . $campo]) ? |
|
|
|
|
31
|
12 |
|
$this->configurazionecolonnetabella[ucfirst(implode(".", $ancestors)) . "." . $campo] : false; |
|
|
|
|
32
|
12 |
|
if ($configurazionecampo && $configurazionecampo["association"] === true) { |
|
|
|
|
33
|
|
|
// crea la relazione con $padre = $nometabella in corso e figlio = $nomecampo con $alias generato |
34
|
6 |
|
if ((isset($configurazionecampo["sourceentityclass"])) && ($configurazionecampo["sourceentityclass"] !== null)) { |
|
|
|
|
35
|
6 |
|
$entitysrc = $configurazionecampo["sourceentityclass"]; |
|
|
|
|
36
|
6 |
|
$nometabellasrc = $this->em->getClassMetadata($entitysrc)->getTableName(); |
37
|
|
|
} else { |
38
|
|
|
$nometabellasrc = $nometabella; |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
$entitytarget = $configurazionecampo["associationtable"]["targetEntity"]; |
|
|
|
|
42
|
6 |
|
$nometabellatarget = $this->em->getClassMetadata($entitytarget)->getTableName(); |
43
|
6 |
|
$aliastarget = $this->generaAlias($nometabellatarget, $nometabellasrc, $ancestors); |
|
|
|
|
44
|
|
|
//$qb->leftJoin($alias . "." . $configurazionecampo["nomecampo"], $aliastarget); |
|
|
|
|
45
|
|
|
//$camporelazionejoin = strtolower(substr($configurazionecampo["nomecampo"], strpos($configurazionecampo["nomecampo"], ".") + 1)); |
|
|
|
|
46
|
6 |
|
$parti = explode(".", $configurazionecampo["nomecampo"]); |
|
|
|
|
47
|
|
|
|
48
|
6 |
|
$camporelazionejoin = strtolower($parti[count($parti) - 1]); |
49
|
6 |
|
$qb->leftJoin($alias . "." . $camporelazionejoin, $aliastarget); |
|
|
|
|
50
|
6 |
|
$campitarget = array_keys($this->em->getMetadataFactory()->getMetadataFor($entitytarget)->reflFields); |
51
|
12 |
|
$this->recursiveJoin($qb, $campitarget, $nometabellatarget, $aliastarget, $ancestors); |
52
|
|
|
|
53
|
|
|
// lancia rescursiveJoin su questo campo con padre = $aliasgenerato |
54
|
|
|
// --- figlio = $nomecampo |
55
|
|
|
// --- alias = alias generato nuovo |
56
|
|
|
} |
57
|
|
|
} |
58
|
12 |
|
} |
59
|
12 |
|
protected function buildWhere(&$qb) |
60
|
|
|
{ |
61
|
12 |
|
$filtro = ""; |
|
|
|
|
62
|
12 |
|
$prefiltro = ""; |
|
|
|
|
63
|
12 |
|
foreach ($this->prefiltri as $key => $prefiltro) { |
64
|
2 |
|
$this->prefiltri[$key]["prefiltro"] = true; |
|
|
|
|
65
|
|
|
} |
66
|
12 |
|
foreach ($this->filtri as $key => $filtro) { |
67
|
2 |
|
$this->filtri[$key]["prefiltro"] = false; |
|
|
|
|
68
|
|
|
} |
69
|
12 |
|
$tuttifiltri = array_merge($this->filtri, $this->prefiltri); |
|
|
|
|
70
|
12 |
|
$parametribag = array(); |
71
|
12 |
|
if (count($tuttifiltri)) { |
72
|
3 |
|
$descrizionefiltri = ""; |
|
|
|
|
73
|
3 |
|
foreach ($tuttifiltri as $num => $filtrocorrente) { |
74
|
3 |
|
$tablename = substr($filtrocorrente["nomecampo"], 0, strripos($filtrocorrente["nomecampo"], ".")); |
|
|
|
|
75
|
3 |
|
$alias = $this->findAliasByTablename($tablename); |
|
|
|
|
76
|
3 |
|
$fieldname = $alias . "." . (substr($filtrocorrente["nomecampo"], strripos($filtrocorrente["nomecampo"], ".") + 1)); |
|
|
|
|
77
|
3 |
|
$fieldvalue = $this->getFieldValue($filtrocorrente["valore"]); |
|
|
|
|
78
|
3 |
|
$fieldoperator = $this->getOperator($filtrocorrente["operatore"]); |
|
|
|
|
79
|
3 |
|
$fitrocorrenteqp = "fitrocorrente" . $num; |
|
|
|
|
80
|
3 |
|
$filtronomecampocorrente = $this->findFieldnameByAlias($filtrocorrente["nomecampo"]); |
|
|
|
|
81
|
3 |
|
$criteria = new ParametriQueryTabellaDecoder( |
|
|
|
|
82
|
3 |
|
$fieldname, |
83
|
3 |
|
$fieldoperator, |
84
|
3 |
|
$fieldvalue, |
85
|
3 |
|
$fitrocorrenteqp, |
86
|
3 |
|
$filtronomecampocorrente |
87
|
|
|
); |
88
|
|
|
|
89
|
3 |
|
$querycriteria = $criteria->getQueryCriteria(); |
|
|
|
|
90
|
3 |
|
$queryparameter = $criteria->getQueryParameters(); |
91
|
|
|
|
92
|
3 |
|
if ($querycriteria) { |
93
|
3 |
|
$qb->andWhere($querycriteria); |
94
|
3 |
|
$parametribag = array_merge($queryparameter, $parametribag); |
95
|
|
|
} else { |
96
|
2 |
|
$qb->andWhere($fieldname . " " . $fieldoperator . " " . ":$fitrocorrenteqp"); |
|
|
|
|
97
|
2 |
|
$parametribag = array_merge(array($fitrocorrenteqp => $fieldvalue), $parametribag); |
98
|
|
|
} |
99
|
3 |
|
$this->getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria); |
|
|
|
|
100
|
|
|
} |
101
|
3 |
|
$this->traduzionefiltri = substr($descrizionefiltri, 2); |
|
|
|
|
102
|
|
|
} |
103
|
12 |
|
$qb->setParameters($parametribag); |
104
|
|
|
|
105
|
12 |
|
if (isset($this->wheremanuale)) { |
106
|
|
|
$qb->andWhere($this->wheremanuale); |
107
|
|
|
} |
108
|
12 |
|
} |
109
|
12 |
|
protected function orderByBuilder(&$qb) |
110
|
|
|
{ |
111
|
12 |
|
foreach ($this->colonneordinamento as $nomecampo => $tipoordinamento) { |
112
|
10 |
|
$tablename = substr($nomecampo, 0, strripos($nomecampo, ".")); |
|
|
|
|
113
|
10 |
|
$alias = $this->getAliasGenerato($tablename); |
|
|
|
|
114
|
10 |
|
$fieldname = $alias . "." . (substr($nomecampo, strripos($nomecampo, ".") + 1)); |
|
|
|
|
115
|
10 |
|
$qb->addOrderBy($fieldname, $tipoordinamento); |
116
|
|
|
} |
117
|
12 |
|
} |
118
|
12 |
|
public function getRecordstabella() |
119
|
|
|
{ |
120
|
|
|
|
121
|
12 |
|
$qb = $this->biQueryBuilder(); |
122
|
|
|
|
123
|
12 |
|
if ($this->estraituttirecords === false) { |
|
|
|
|
124
|
12 |
|
$paginator = new Paginator($qb, true); |
|
|
|
|
125
|
12 |
|
$this->righetotali = count($paginator); |
|
|
|
|
126
|
12 |
|
$this->paginetotali = (int) $this->calcolaPagineTotali($this->getRigheperpagina()); |
|
|
|
|
127
|
|
|
/* imposta l'offset, ovvero il record dal quale iniziare a visualizzare i dati */ |
128
|
12 |
|
$offsetrecords = ($this->getRigheperpagina() * ($this->getPaginacorrente() - 1)); |
|
|
|
|
129
|
|
|
|
130
|
|
|
/* Imposta il limite ai record da estrarre */ |
131
|
12 |
|
if ($this->getRigheperpagina()) { |
132
|
12 |
|
$qb = $qb->setMaxResults($this->getRigheperpagina()); |
133
|
|
|
} |
134
|
|
|
/* E imposta il primo record da visualizzare (per la paginazione) */ |
135
|
12 |
|
if ($offsetrecords) { |
136
|
|
|
$qb = $qb->setFirstResult($offsetrecords); |
137
|
|
|
} |
138
|
|
|
/* Dall'oggetto querybuilder si ottiene la query da eseguire */ |
139
|
12 |
|
$recordsets = $qb->getQuery()->getResult(); |
140
|
|
|
} else { |
141
|
|
|
/* Dall'oggetto querybuilder si ottiene la query da eseguire */ |
142
|
4 |
|
$recordsets = $qb->getQuery()->getResult(); |
|
|
|
|
143
|
4 |
|
$this->righetotali = count($recordsets); |
|
|
|
|
144
|
4 |
|
$this->paginetotali = 1; |
145
|
|
|
} |
146
|
|
|
|
147
|
12 |
|
$this->records = array(); |
|
|
|
|
148
|
12 |
|
$rigatabellahtml = array(); |
149
|
12 |
|
foreach ($recordsets as $record) { |
150
|
12 |
|
$this->records[$record->getId()] = $record; |
151
|
12 |
|
unset($rigatabellahtml); |
152
|
|
|
} |
153
|
12 |
|
return $this->records; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|