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