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