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