1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Utils\Tabella; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
6
|
|
|
use Doctrine\Common\Collections\Expr\Comparison; |
7
|
|
|
use Cdf\BiCoreBundle\Utils\FieldType\FieldTypeUtils; |
8
|
|
|
use Exception; |
9
|
|
|
use function count; |
10
|
|
|
|
11
|
|
|
trait TabellaQueryTrait |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
* @return mixed |
17
|
|
|
*/ |
18
|
12 |
|
protected function biQueryBuilder() |
19
|
|
|
{ |
20
|
12 |
|
$nometabellaalias = $this->generaAlias($this->tablename); |
|
|
|
|
21
|
12 |
|
$qb = $this->em->createQueryBuilder() |
22
|
12 |
|
->select(array($nometabellaalias)) |
23
|
12 |
|
->from($this->entityname, $nometabellaalias); |
24
|
12 |
|
$campi = array_keys($this->em->getMetadataFactory()->getMetadataFor($this->entityname)->reflFields); |
25
|
12 |
|
$this->recursiveJoin($qb, $campi, $this->tablename, $nometabellaalias); |
26
|
12 |
|
$this->buildWhere($qb); |
27
|
12 |
|
$this->orderByBuilder($qb); |
28
|
|
|
|
29
|
12 |
|
return $qb; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @param mixed $qb |
35
|
|
|
* @param array<mixed> $campi |
36
|
|
|
* @param string $nometabella |
37
|
|
|
* @param string $alias |
38
|
|
|
* @param array<mixed> $ancestors |
39
|
|
|
*/ |
40
|
12 |
|
protected function recursiveJoin(&$qb, $campi, $nometabella, $alias, $ancestors = array()): void |
41
|
|
|
{ |
42
|
12 |
|
foreach ($campi as $campo) { |
43
|
12 |
|
if (false !== strpos(strtolower($campo), 'relatedby')) { |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
12 |
|
if (!in_array($nometabella, $ancestors)) { |
47
|
12 |
|
$ancestors[] = $nometabella; |
48
|
|
|
} |
49
|
|
|
|
50
|
12 |
|
$configurazionecampo = isset($this->configurazionecolonnetabella[ucfirst(implode('.', $ancestors)) . '.' . $campo]) ? |
51
|
12 |
|
$this->configurazionecolonnetabella[ucfirst(implode('.', $ancestors)) . '.' . $campo] : false; |
52
|
12 |
|
if ($configurazionecampo && true === $configurazionecampo['association']) { |
53
|
|
|
// crea la relazione con $padre = $nometabella in corso e figlio = $nomecampo con $alias generato |
54
|
6 |
|
if ((isset($configurazionecampo['sourceentityclass'])) && (null !== $configurazionecampo['sourceentityclass'])) { |
55
|
6 |
|
$entitysrc = $configurazionecampo['sourceentityclass']; |
56
|
6 |
|
$nometabellasrc = $this->em->getClassMetadata($entitysrc)->getTableName(); |
57
|
|
|
} else { |
58
|
|
|
$nometabellasrc = $nometabella; |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
$entitytarget = $configurazionecampo['associationtable']['targetEntity']; |
62
|
6 |
|
$nometabellatarget = $this->em->getClassMetadata($entitytarget)->getTableName(); |
63
|
6 |
|
$aliastarget = $this->generaAlias($nometabellatarget, $nometabellasrc, $ancestors); |
64
|
|
|
//$qb->leftJoin($alias . "." . $configurazionecampo["nomecampo"], $aliastarget); |
65
|
|
|
//$camporelazionejoin = strtolower(substr($configurazionecampo["nomecampo"], strpos($configurazionecampo["nomecampo"], ".") + 1)); |
66
|
6 |
|
$parti = explode('.', $configurazionecampo['nomecampo']); |
67
|
|
|
|
68
|
6 |
|
$camporelazionejoin = strtolower($parti[count($parti) - 1]); |
69
|
6 |
|
$qb->leftJoin($alias . '.' . $camporelazionejoin, $aliastarget); |
70
|
6 |
|
$campitarget = array_keys($this->em->getMetadataFactory()->getMetadataFor($entitytarget)->reflFields); |
71
|
6 |
|
$this->recursiveJoin($qb, $campitarget, $nometabellatarget, $aliastarget, $ancestors); |
72
|
|
|
|
73
|
|
|
// lancia rescursiveJoin su questo campo con padre = $aliasgenerato |
74
|
|
|
// --- figlio = $nomecampo |
75
|
|
|
// --- alias = alias generato nuovo |
76
|
|
|
} |
77
|
|
|
} |
78
|
12 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* |
82
|
|
|
* @param mixed $qb |
83
|
|
|
*/ |
84
|
12 |
|
protected function buildWhere(&$qb): void |
85
|
|
|
{ |
86
|
|
|
|
87
|
12 |
|
$filtro = ''; |
|
|
|
|
88
|
12 |
|
$prefiltro = ''; |
|
|
|
|
89
|
12 |
|
foreach ($this->prefiltri as $key => $prefiltro) { |
90
|
2 |
|
$this->prefiltri[$key]['prefiltro'] = true; |
|
|
|
|
91
|
|
|
} |
92
|
12 |
|
foreach ($this->filtri as $key => $filtro) { |
93
|
2 |
|
$this->filtri[$key]['prefiltro'] = false; |
|
|
|
|
94
|
|
|
} |
95
|
12 |
|
$tuttifiltri = array_merge($this->filtri, $this->prefiltri); |
96
|
12 |
|
$parametribag = array(); |
97
|
12 |
|
if (count($tuttifiltri)) { |
98
|
3 |
|
$descrizionefiltri = ''; |
99
|
3 |
|
foreach ($tuttifiltri as $num => $filtrocorrente) { |
100
|
3 |
|
$strpos = strripos($filtrocorrente['nomecampo'], '.'); |
101
|
3 |
|
if ($strpos === false) { |
102
|
|
|
throw new Exception("Impossibile trovare il . in " . $filtrocorrente['nomecampo']); |
103
|
|
|
} |
104
|
3 |
|
$tablename = substr($filtrocorrente['nomecampo'], 0, $strpos); |
105
|
3 |
|
$alias = $this->findAliasByTablename($tablename); |
|
|
|
|
106
|
3 |
|
$fieldname = $alias . '.' . (substr($filtrocorrente['nomecampo'], strripos($filtrocorrente['nomecampo'], '.') + 1)); |
107
|
3 |
|
$fieldvalue = $this->getFieldValue($filtrocorrente['valore']); |
|
|
|
|
108
|
3 |
|
$fieldoperator = $this->getOperator($filtrocorrente['operatore']); |
|
|
|
|
109
|
3 |
|
$fitrocorrenteqp = 'fitrocorrente' . $num; |
110
|
3 |
|
$filtronomecampocorrente = $this->findFieldnameByAlias($filtrocorrente['nomecampo']); |
|
|
|
|
111
|
2 |
|
$criteria = new ParametriQueryTabellaDecoder( |
112
|
2 |
|
$fieldname, |
113
|
|
|
$fieldoperator, |
114
|
|
|
$fieldvalue, |
115
|
|
|
$fitrocorrenteqp, |
116
|
|
|
$filtronomecampocorrente |
117
|
|
|
); |
118
|
|
|
|
119
|
2 |
|
$querycriteria = $criteria->getQueryCriteria(); |
120
|
2 |
|
$queryparameter = $criteria->getQueryParameters(); |
121
|
|
|
|
122
|
2 |
|
if ($querycriteria) { |
123
|
2 |
|
$qb->andWhere($querycriteria); |
124
|
2 |
|
$parametribag = array_merge($queryparameter, $parametribag); |
125
|
|
|
} else { |
126
|
2 |
|
$qb->andWhere($fieldname . ' ' . $fieldoperator . ' ' . ":$fitrocorrenteqp"); |
127
|
2 |
|
$parametribag = array_merge(array($fitrocorrenteqp => $fieldvalue), $parametribag); |
128
|
|
|
} |
129
|
2 |
|
$this->getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria); |
|
|
|
|
130
|
|
|
} |
131
|
2 |
|
$this->traduzionefiltri = substr($descrizionefiltri, 2); |
|
|
|
|
132
|
|
|
} |
133
|
12 |
|
$qb->setParameters($parametribag); |
134
|
|
|
|
135
|
12 |
|
if (isset($this->wheremanuale)) { |
136
|
|
|
$qb->andWhere($this->wheremanuale); |
137
|
|
|
} |
138
|
12 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* |
142
|
|
|
* @param mixed $qb |
143
|
|
|
*/ |
144
|
12 |
|
protected function orderByBuilder(&$qb): void |
145
|
|
|
{ |
146
|
12 |
|
foreach ($this->colonneordinamento as $nomecampo => $tipoordinamento) { |
147
|
10 |
|
$strpos = strripos($nomecampo, '.'); |
148
|
10 |
|
if ($strpos === false) { |
149
|
|
|
throw new Exception("Impossibile trovare il . in " . $nomecampo); |
150
|
|
|
} |
151
|
10 |
|
$tablename = substr($nomecampo, 0, $strpos); |
152
|
10 |
|
$alias = $this->getAliasGenerato($tablename); |
|
|
|
|
153
|
10 |
|
$fieldname = $alias . '.' . (substr($nomecampo, strripos($nomecampo, '.') + 1)); |
154
|
10 |
|
$qb->addOrderBy($fieldname, $tipoordinamento); |
155
|
|
|
} |
156
|
12 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Attempt to translate the user given value into a boolean valid field |
160
|
|
|
*/ |
161
|
|
|
private function translateBoolValue(string $fieldvalue): string |
162
|
|
|
{ |
163
|
|
|
switch (strtoupper($fieldvalue)) { |
164
|
|
|
case 'SI': |
165
|
|
|
$fieldvalue = 'true'; |
166
|
|
|
break; |
167
|
|
|
case '1': |
168
|
|
|
$fieldvalue = 'true'; |
169
|
|
|
break; |
170
|
|
|
case 'NO': |
171
|
|
|
$fieldvalue = 'false'; |
172
|
|
|
break; |
173
|
|
|
case '0': |
174
|
|
|
$fieldvalue = 'false'; |
175
|
|
|
break; |
176
|
|
|
default: |
177
|
|
|
$fieldvalue = 'false'; |
178
|
|
|
break; |
179
|
|
|
} |
180
|
|
|
return $fieldvalue; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* It appends the new filter string part to the given filter string ($filterString) |
185
|
|
|
*/ |
186
|
|
|
private function appendFilterString(string &$filterString, string $swaggerType, string $swaggerKind, string $fieldvalue): void |
187
|
|
|
{ |
188
|
|
|
if ($swaggerKind == 'bool') { |
189
|
|
|
$filterString .= $this->translateBoolValue($fieldvalue); |
190
|
|
|
} elseif ($swaggerType == null /* || $swaggerFormats[ $nomeCampo ] == 'datetime' */) { |
191
|
|
|
//"%" chars will be applied by insurance back-end API |
192
|
|
|
$filterString .= '"' . $fieldvalue . '"'; |
193
|
|
|
} elseif ($swaggerType == 'datetime' || $swaggerType == 'date') { |
194
|
|
|
$fieldvalue = \str_replace("/", "-", $fieldvalue); |
195
|
|
|
//does it contain an hour ? |
196
|
|
|
$hour = strpos($fieldvalue, ":"); |
197
|
|
|
$time = strtotime($fieldvalue); |
198
|
|
|
if ($time === false) { |
199
|
|
|
throw new Exception("time non valido: " . $time); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$backend_format = FieldTypeUtils::getEnvVar("BE_DATETIME_FORMAT", "Y-m-d\TH:i:sP"); |
203
|
|
|
if ($hour === false) { |
204
|
|
|
$backend_format = FieldTypeUtils::getEnvVar("BE_DATE_FORMAT", "Y-m-d"); |
205
|
|
|
} |
206
|
|
|
$filterString .= date($backend_format, $time); |
207
|
|
|
} else { |
208
|
|
|
$filterString .= $fieldvalue; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* It composes filtering string to be used with api rest services |
214
|
|
|
*/ |
215
|
|
|
protected function filterByApiBuilder(): ?string |
216
|
|
|
{ |
217
|
|
|
$filterString = null; |
218
|
|
|
$filtro = ''; |
|
|
|
|
219
|
|
|
$prefiltro = ''; |
|
|
|
|
220
|
|
|
foreach ($this->prefiltri as $key => $prefiltro) { |
221
|
|
|
$this->prefiltri[$key]['prefiltro'] = true; |
|
|
|
|
222
|
|
|
} |
223
|
|
|
foreach ($this->filtri as $key => $filtro) { |
224
|
|
|
$this->filtri[$key]['prefiltro'] = false; |
|
|
|
|
225
|
|
|
} |
226
|
|
|
$tuttifiltri = array_merge($this->filtri, $this->prefiltri); |
227
|
|
|
//$parametribag = array(); |
228
|
|
|
if (count($tuttifiltri)) { |
229
|
|
|
$attributeMap = $this->entityname::attributeMap(); |
230
|
|
|
$swaggerFormats = $this->entityname::swaggerFormats(); |
231
|
|
|
$swaggerTypes = $this->entityname::swaggerTypes(); |
232
|
|
|
//compose the string |
233
|
|
|
$descrizionefiltri = ''; |
234
|
|
|
foreach ($tuttifiltri as $num => $filtrocorrente) { |
235
|
|
|
$nomeCampo = substr($filtrocorrente['nomecampo'], strripos($filtrocorrente['nomecampo'], '.') + 1); |
236
|
|
|
$fieldname = ' ' . $nomeCampo; |
237
|
|
|
$filteringValues = $this->getFieldValue($filtrocorrente['valore']); |
238
|
|
|
$fieldoperator = $this->getOperator($filtrocorrente['operatore']); |
239
|
|
|
$fitrocorrenteqp = 'fitrocorrente' . $num; |
240
|
|
|
$filtronomecampocorrente = $this->findFieldnameByAlias($filtrocorrente['nomecampo']); |
241
|
|
|
|
242
|
|
|
$criteria = new ParametriQueryTabellaDecoder( |
243
|
|
|
$fieldname, |
244
|
|
|
$fieldoperator, |
245
|
|
|
$filteringValues, |
246
|
|
|
$fitrocorrenteqp, |
247
|
|
|
$filtronomecampocorrente |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
if (is_array($filteringValues)) { |
251
|
|
|
$fieldstring = '( '; |
252
|
|
|
foreach ($filteringValues as $num => $filterValue) { |
|
|
|
|
253
|
|
|
$fieldstring .= $attributeMap[$nomeCampo]; |
254
|
|
|
$fieldstring .= ' ' . $this->getApiOperator($filtrocorrente['operatore']) . ' '; |
255
|
|
|
$fieldvalue = urldecode($filterValue); |
256
|
|
|
$this->appendFilterString($fieldstring, $swaggerFormats[$nomeCampo], $swaggerTypes[$nomeCampo], $fieldvalue); |
257
|
|
|
if ($num < count($filteringValues) - 1) { |
258
|
|
|
$fieldstring .= ' OR '; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
$fieldstring .= ' )'; |
262
|
|
|
} else { |
263
|
|
|
$fieldstring = $attributeMap[$nomeCampo]; |
264
|
|
|
$fieldstring .= ' ' . $this->getApiOperator($filtrocorrente['operatore']) . ' '; |
265
|
|
|
$fieldvalue = urldecode($filteringValues); |
|
|
|
|
266
|
|
|
$this->appendFilterString($fieldstring, $swaggerFormats[$nomeCampo], $swaggerTypes[$nomeCampo], $filteringValues); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
if ($filterString != null) { |
270
|
|
|
$filterString .= ' AND '; |
271
|
|
|
} |
272
|
|
|
$filterString .= $fieldstring; |
273
|
|
|
$this->getDescrizioneFiltro($descrizionefiltri, $filtrocorrente, $criteria); |
274
|
|
|
} |
275
|
|
|
$this->traduzionefiltri = substr($descrizionefiltri, 2); |
|
|
|
|
276
|
|
|
} |
277
|
|
|
return $filterString; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Return the operator to be used |
282
|
|
|
*/ |
283
|
|
|
private function getApiOperator(string $operator): string |
284
|
|
|
{ |
285
|
|
|
switch (strtoupper($operator)) { |
286
|
|
|
case Comparison::CONTAINS: |
287
|
|
|
$operator = '~'; |
288
|
|
|
break; |
289
|
|
|
/* case 'IN': |
290
|
|
|
$operator = Comparison::IN; |
291
|
|
|
break; |
292
|
|
|
case 'NOT IN': |
293
|
|
|
$operator = Comparison::NIN; |
294
|
|
|
break; */ |
295
|
|
|
default: |
296
|
|
|
$operator = '='; |
297
|
|
|
break; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return $operator; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Build the ordering string compliant with API REST services |
305
|
|
|
*/ |
306
|
|
|
protected function orderByApiBuilder(): ?string |
307
|
|
|
{ |
308
|
|
|
$attributeMap = $this->entityname::attributeMap(); |
309
|
|
|
$orderingString = null; |
310
|
|
|
foreach ($this->colonneordinamento as $nomecampo => $tipoordinamento) { |
311
|
|
|
$fieldname = $attributeMap[substr($nomecampo, strripos($nomecampo, '.') + 1)]; |
312
|
|
|
$fieldname .= ':' . $tipoordinamento; |
313
|
|
|
if ($orderingString != null) { |
314
|
|
|
$orderingString .= ','; |
315
|
|
|
} |
316
|
|
|
$orderingString .= $fieldname; |
317
|
|
|
} |
318
|
|
|
return $orderingString; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* |
323
|
|
|
* @return array<mixed> |
324
|
|
|
*/ |
325
|
12 |
|
public function getRecordstabella() |
326
|
|
|
{ |
327
|
|
|
//Look for all tables |
328
|
12 |
|
$qb = $this->biQueryBuilder(); |
329
|
|
|
|
330
|
12 |
|
if (false === $this->estraituttirecords) { |
|
|
|
|
331
|
12 |
|
$paginator = new Paginator($qb, true); |
332
|
12 |
|
$this->righetotali = count($paginator); |
|
|
|
|
333
|
12 |
|
$this->paginetotali = (int) $this->calcolaPagineTotali($this->getRigheperpagina()); |
|
|
|
|
334
|
|
|
/* imposta l'offset, ovvero il record dal quale iniziare a visualizzare i dati */ |
335
|
12 |
|
$offsetrecords = ($this->getRigheperpagina() * ($this->getPaginacorrente() - 1)); |
|
|
|
|
336
|
|
|
|
337
|
|
|
/* Imposta il limite ai record da estrarre */ |
338
|
12 |
|
if ($this->getRigheperpagina()) { |
339
|
12 |
|
$qb = $qb->setMaxResults((int) $this->getRigheperpagina()); |
340
|
|
|
} |
341
|
|
|
/* E imposta il primo record da visualizzare (per la paginazione) */ |
342
|
12 |
|
if ($offsetrecords) { |
343
|
|
|
$qb = $qb->setFirstResult((int) $offsetrecords); |
344
|
|
|
} |
345
|
|
|
/* Dall'oggetto querybuilder si ottiene la query da eseguire */ |
346
|
12 |
|
$recordsets = $qb->getQuery()->getResult(); |
347
|
|
|
} else { |
348
|
|
|
/* Dall'oggetto querybuilder si ottiene la query da eseguire */ |
349
|
4 |
|
$recordsets = $qb->getQuery()->getResult(); |
350
|
4 |
|
$this->righetotali = count($recordsets); |
351
|
4 |
|
$this->paginetotali = 1; |
352
|
|
|
} |
353
|
|
|
|
354
|
12 |
|
$this->records = array(); |
|
|
|
|
355
|
12 |
|
$rigatabellahtml = array(); |
356
|
12 |
|
foreach ($recordsets as $record) { |
357
|
12 |
|
$this->records[$record->getId()] = $record; |
358
|
12 |
|
unset($rigatabellahtml); |
359
|
|
|
} |
360
|
|
|
|
361
|
12 |
|
return $this->records; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Read the API in order to obtains the pages features |
366
|
|
|
* |
367
|
|
|
* @return array<mixed> |
368
|
|
|
*/ |
369
|
|
|
public function getApiRecordstabella() |
370
|
|
|
{ |
371
|
|
|
$newApi = $this->apiController; |
372
|
|
|
$apiController = new $newApi(); |
373
|
|
|
|
374
|
|
|
if (false === $this->estraituttirecords) { |
|
|
|
|
375
|
|
|
$countMethod = $this->apiBook->getCount(); |
376
|
|
|
|
377
|
|
|
$count = $apiController->$countMethod(); |
378
|
|
|
$this->righetotali = $count; |
|
|
|
|
379
|
|
|
$this->paginetotali = (int) $this->calcolaPagineTotali($this->getRigheperpagina()); |
|
|
|
|
380
|
|
|
/* imposta l'offset, ovvero il record dal quale iniziare a visualizzare i dati */ |
381
|
|
|
$offsetrecords = ($this->getRigheperpagina() * ($this->getPaginacorrente() - 1)); |
382
|
|
|
|
383
|
|
|
/* $offset = null, $limit = null, $sort = null, $condition = null */ |
384
|
|
|
$paginationMethod = $this->apiBook->getAll(); |
385
|
|
|
//dump($this->filterByApiBuilder()); |
386
|
|
|
|
387
|
|
|
$recordsets = $apiController->$paginationMethod( |
388
|
|
|
$offsetrecords, |
389
|
|
|
$this->getRigheperpagina(), |
390
|
|
|
$this->orderByApiBuilder(), |
391
|
|
|
$this->filterByApiBuilder() |
392
|
|
|
); |
393
|
|
|
//dump($recordsets); |
394
|
|
|
} else { |
395
|
|
|
/* Dall'oggetto querybuilder si ottiene la query da eseguire */ |
396
|
|
|
$paginationMethod = $this->apiBook->getAll(); |
397
|
|
|
$recordsets = $apiController->$paginationMethod(0, null, $this->orderByApiBuilder(), $this->filterByApiBuilder()); |
398
|
|
|
$this->paginetotali = 1; |
399
|
|
|
$this->righetotali = count($recordsets); |
400
|
|
|
} |
401
|
|
|
$this->records = array(); |
|
|
|
|
402
|
|
|
$rigatabellahtml = array(); |
403
|
|
|
foreach ($recordsets as $record) { |
404
|
|
|
$this->records[$record->getId()] = $record; |
405
|
|
|
unset($rigatabellahtml); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
return $this->records; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|