1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Utils; |
4
|
|
|
|
5
|
|
|
use Fi\CoreBundle\Controller\FiUtilita; |
6
|
|
|
|
7
|
|
|
class GrigliaRegoleUtils |
8
|
|
|
{ |
9
|
|
|
|
10
|
2 |
|
public static function getTipoRegola(&$tipo, &$regola, $parametri) |
11
|
|
|
{ |
12
|
2 |
|
$doctrine = $parametri['doctrine']; |
|
|
|
|
13
|
2 |
|
$nometabella = $parametri['nometabella']; |
14
|
2 |
|
$entityName = $parametri['entityName']; |
|
|
|
|
15
|
2 |
|
$bundle = $parametri['bundle']; |
|
|
|
|
16
|
|
|
|
17
|
2 |
|
$elencocampi = $doctrine->getClassMetadata($entityName)->getFieldNames(); |
18
|
|
|
|
19
|
2 |
|
if (strrpos($regola['field'], '.') == 0) { |
20
|
2 |
|
if (in_array($regola['field'], $elencocampi) === true) { |
21
|
2 |
|
$type = $doctrine->getClassMetadata($entityName)->getFieldMapping($regola['field']); |
22
|
2 |
|
$tipo = $type['type']; |
23
|
|
|
|
24
|
|
|
//Si aggiunge l'alias al campo altrimenti da Doctrine2 fallisce la query |
25
|
2 |
|
$regola['field'] = $nometabella . '.' . $regola['field']; |
26
|
2 |
|
} |
27
|
2 |
|
} else { |
28
|
|
|
//Altrimenti stiamo analizzando il campo di una tabella in leftjoin pertanto si cercano le informazioni sul tipo |
29
|
|
|
//dei campi nella tabella "joinata" |
30
|
2 |
|
$tablejoined = substr($regola['field'], 0, strrpos($regola['field'], '.')); |
31
|
2 |
|
$fieldjoined = substr($regola['field'], strrpos($regola['field'], '.') + 1); |
32
|
|
|
|
33
|
2 |
|
$entityNametablejoined = $bundle . ':' . $tablejoined; |
34
|
|
|
|
35
|
2 |
|
$type = $doctrine->getClassMetadata($entityNametablejoined)->getFieldMapping($fieldjoined); |
36
|
2 |
|
$tipo = $type['type']; |
37
|
|
|
} |
38
|
2 |
|
} |
39
|
|
|
|
40
|
2 |
|
public static function getRegolaPerData(&$regola) |
41
|
|
|
{ |
42
|
2 |
|
if (isset($regola) && count($regola) > 0) { |
43
|
2 |
|
$regola['data'] = preg_replace('/^(\'(.*)\'|"(.*)")$/', '$2$3', $regola['data']); |
44
|
2 |
|
} |
45
|
2 |
|
} |
46
|
|
|
|
47
|
2 |
|
public static function setRegole(&$q, &$primo, $parametri = array()) |
48
|
|
|
{ |
49
|
2 |
|
$regole = $parametri['regole']; |
50
|
|
|
//file_put_contents("/tmp/appo.log", print_r($regole, true) . "\n", FILE_APPEND); |
|
|
|
|
51
|
|
|
//$tipof = $parametri['tipof']; |
|
|
|
|
52
|
2 |
|
$tipo = null; |
53
|
|
|
//file_put_contents("/tmp/appo.log", "\n\n" . . "\n\n", FILE_APPEND); |
|
|
|
|
54
|
2 |
|
foreach ($regole as $key => $regola) { |
55
|
|
|
//file_put_contents("/tmp/appo.log", "***1***" . "\n", FILE_APPEND); |
|
|
|
|
56
|
|
|
//Se il campo non ha il . significa che è necessario aggiungere il nometabella |
57
|
2 |
|
self::getTipoRegola($tipo, $regola, $parametri); |
58
|
2 |
|
$tipof = isset($regola["typof"]) ? $regola["typof"] : 'AND'; |
|
|
|
|
59
|
2 |
|
$regola = self::setSingolaRegola($tipo, $regola); |
60
|
2 |
|
if (!$regola) { |
61
|
1 |
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$conditions = self::getDoctrineConditions($regola, $key); |
65
|
|
|
|
66
|
2 |
|
$condition = $conditions["condition"]; |
|
|
|
|
67
|
2 |
|
$sqlparameter = $conditions["sqlparameter"]; |
|
|
|
|
68
|
2 |
|
$value = $conditions["value"]; |
|
|
|
|
69
|
|
|
|
70
|
2 |
|
if ($primo) { |
71
|
2 |
|
$primo = false; |
72
|
2 |
|
$q->where($condition); |
73
|
2 |
|
} else { |
74
|
|
|
//file_put_contents("/tmp/appo.log", $tipof . "\n", FILE_APPEND); |
|
|
|
|
75
|
2 |
|
if (strtoupper($tipof) == 'OR') { |
76
|
|
|
$q->orWhere($condition); |
77
|
|
|
} else { |
78
|
2 |
|
$q->andwhere($condition); |
79
|
|
|
} |
80
|
|
|
} |
81
|
2 |
|
if ($sqlparameter) { |
82
|
2 |
|
$q->setParameter($sqlparameter, $value); |
83
|
2 |
|
} |
84
|
|
|
/* $condizioneAND = $regola['field'] . ' ' . |
|
|
|
|
85
|
|
|
GrigliaUtils::$decodificaop[$regola['op']] . ' ' . |
86
|
|
|
GrigliaUtils::$precarattere[$regola['op']] . |
87
|
|
|
str_replace("'", "''", $regola['data']) . |
88
|
|
|
GrigliaUtils::$postcarattere[$regola['op']]; |
89
|
|
|
$q->andWhere($condizioneAND); */ |
90
|
|
|
//file_put_contents("/tmp/appo.log", dump($fieldparm) . "\n", FILE_APPEND); |
|
|
|
|
91
|
|
|
//file_put_contents("/tmp/appo.log", dump($sqlparameter) . "\n", FILE_APPEND); |
|
|
|
|
92
|
|
|
//file_put_contents("/tmp/appo.log", print_r($condition, true) . "\n", FILE_APPEND); |
|
|
|
|
93
|
|
|
//file_put_contents("/tmp/appo.log", print_r($value, true) . "\n", FILE_APPEND); |
|
|
|
|
94
|
2 |
|
} |
95
|
2 |
|
} |
96
|
|
|
|
97
|
2 |
|
private static function getDoctrineConditions($regola, $key) |
98
|
|
|
{ |
99
|
|
|
//file_put_contents("/tmp/appo.log", "!" . $tipo . "! " . print_r($regola,true) . "\n", FILE_APPEND); |
|
|
|
|
100
|
2 |
|
if (GrigliaUtils::$decodificaop[$regola['op']] == 'IS' || GrigliaUtils::$decodificaop[$regola['op']] == 'IS NOT') { |
101
|
2 |
|
$condition = GrigliaUtils::$precaratterecampo[$regola['op']] |
|
|
|
|
102
|
2 |
|
. $regola['field'] |
103
|
2 |
|
. GrigliaUtils::$postcaratterecampo[$regola['op']] . " " |
|
|
|
|
104
|
2 |
|
. GrigliaUtils::$decodificaop[$regola['op']] . " NULL"; |
|
|
|
|
105
|
2 |
|
$value = null; |
|
|
|
|
106
|
2 |
|
$sqlparameter = null; |
107
|
2 |
|
return array("condition" => $condition, "sqlparameter" => $sqlparameter, "value" => $value); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
if (GrigliaUtils::$decodificaop[$regola['op']] == 'IN' || GrigliaUtils::$decodificaop[$regola['op']] == 'NOT IN') { |
111
|
1 |
|
$fieldparm = GrigliaUtils::$precaratterecampo[$regola['op']] |
|
|
|
|
112
|
1 |
|
. str_replace(".", "", ":" . $regola['field'] . $key) |
|
|
|
|
113
|
1 |
|
. GrigliaUtils::$postcaratterecampo[$regola['op']]; |
114
|
1 |
|
$sqlparameter = str_replace(".", "", ":" . $regola['field'] . $key); |
|
|
|
|
115
|
1 |
|
$condition = GrigliaUtils::$precarattere[$regola['op']] |
|
|
|
|
116
|
1 |
|
. $regola['field'] |
117
|
1 |
|
. GrigliaUtils::$postcarattere[$regola['op']] |
118
|
1 |
|
. " " . GrigliaUtils::$decodificaop[$regola['op']] . " " . $fieldparm; |
|
|
|
|
119
|
1 |
|
$value = explode(",", str_replace(", ", ",", $regola['data'])); |
|
|
|
|
120
|
|
|
//file_put_contents("/tmp/appo.log", "|" . $regola['op'] . "| " . $regola['data'] . "\n", FILE_APPEND); |
|
|
|
|
121
|
1 |
|
return array("condition" => $condition, "sqlparameter" => $sqlparameter, "value" => $value); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
//file_put_contents("/tmp/appo.log", "|" . $regola['op'] . "| " . $regola['data'] . "\n", FILE_APPEND); |
|
|
|
|
125
|
2 |
|
$fieldparm = GrigliaUtils::$precaratterecampo[$regola['op']] |
|
|
|
|
126
|
2 |
|
. str_replace(".", "", ":" . $regola['field'] . $key) |
|
|
|
|
127
|
2 |
|
. GrigliaUtils::$postcaratterecampo[$regola['op']]; |
128
|
2 |
|
$sqlparameter = str_replace(".", "", $regola['field'] . $key); |
|
|
|
|
129
|
2 |
|
$condition = GrigliaUtils::$precaratterecampo[$regola['op']] |
|
|
|
|
130
|
2 |
|
. $regola['field'] |
131
|
2 |
|
. GrigliaUtils::$postcaratterecampo[$regola['op']] . " " |
|
|
|
|
132
|
2 |
|
. GrigliaUtils::$decodificaop[$regola['op']] . " " . $fieldparm; |
|
|
|
|
133
|
2 |
|
$value = GrigliaUtils::$precarattere[$regola['op']] |
|
|
|
|
134
|
2 |
|
. $regola['data'] |
135
|
2 |
|
. GrigliaUtils::$postcarattere[$regola['op']]; |
136
|
2 |
|
return array("condition" => $condition, "sqlparameter" => $sqlparameter, "value" => $value); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
2 |
|
public static function setSingolaRegola($tipo, $regola) |
140
|
|
|
{ |
141
|
|
|
//file_put_contents("/tmp/appo.log", "...". $tipo."-" .$regola['data']. "\n", FILE_APPEND); |
|
|
|
|
142
|
2 |
|
if (!$tipo || $tipo == "integer" || $tipo == "float") { |
|
|
|
|
143
|
1 |
|
GrigliaUtils::setVettoriPerNumero(); |
144
|
1 |
|
} |
145
|
2 |
|
if ($tipo == 'date' || $tipo == 'datetime') { |
146
|
|
|
GrigliaUtils::setVettoriPerData(); |
147
|
|
|
$regola['data'] = FiUtilita::data2db($regola['data']); |
148
|
2 |
|
} elseif ($tipo == 'string') { |
149
|
2 |
|
GrigliaUtils::setVettoriPerStringa(); |
150
|
|
|
//file_put_contents("/tmp/appo.log", "..." . $tipo . "-" . $regola['data'] . "->" . strlen($regola['data']) . "\n", FILE_APPEND); |
|
|
|
|
151
|
2 |
|
$regola['data'] = preg_replace('/^(\'(.*)\'|"(.*)")$/', '$2$3', $regola['data']); |
152
|
2 |
|
} |
153
|
2 |
|
if ($tipo == 'boolean') { |
154
|
1 |
|
self::setTipoBoolean($regola, $tipo); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
2 |
|
self::getRegolaPerData($regola); |
158
|
|
|
|
159
|
2 |
|
return $regola; |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
public static function setTipoBoolean(&$regola, $tipo) |
163
|
|
|
{ |
164
|
1 |
|
if ($regola['data'] === 'false' || $regola['data'] === false) { |
165
|
1 |
|
GrigliaUtils::setVettoriPerBoolean(); |
166
|
1 |
|
$regola['op'] = 'eq'; |
|
|
|
|
167
|
1 |
|
$regola['data'] = "0"; |
|
|
|
|
168
|
1 |
|
} |
169
|
1 |
|
if ($regola['data'] === 'true' || $regola['data'] === true) { |
170
|
1 |
|
GrigliaUtils::setVettoriPerBoolean(); |
171
|
1 |
|
$regola['op'] = 'eq'; |
|
|
|
|
172
|
1 |
|
$regola['data'] = "1"; |
|
|
|
|
173
|
1 |
|
} |
174
|
1 |
|
if ($tipo == 'boolean' && $regola['data'] == 'null') { |
175
|
1 |
|
$regola = array(); |
176
|
1 |
|
} |
177
|
1 |
|
} |
178
|
|
|
|
179
|
13 |
|
public static function campiesclusi($parametri = array()) |
180
|
|
|
{ |
181
|
13 |
|
if (!isset($parametri['nometabella'])) { |
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
13 |
|
$output = GrigliaParametriUtils::getOuputType($parametri); |
186
|
|
|
|
187
|
13 |
|
$nometabella = $parametri['nometabella']; |
188
|
|
|
|
189
|
13 |
|
$doctrine = GrigliaParametriUtils::getDoctrineByEm($parametri); |
|
|
|
|
190
|
13 |
|
$doctrineficore = GrigliaParametriUtils::getDoctrineFiCoreByEm($parametri, $doctrine); |
191
|
13 |
|
$container = $parametri['container']; |
|
|
|
|
192
|
|
|
|
193
|
13 |
|
$gestionepermessi = $container->get("ficorebundle.gestionepermessi"); |
|
|
|
|
194
|
13 |
|
$operatorecorrente = $gestionepermessi->utentecorrente(); |
195
|
|
|
|
196
|
13 |
|
$escludi = array(); |
197
|
13 |
|
$q = GrigliaUtils::getUserCustomTableFields($doctrineficore, $nometabella, $operatorecorrente); |
|
|
|
|
198
|
13 |
|
if (!$q) { |
199
|
9 |
|
return $escludi; |
200
|
|
|
} |
201
|
|
|
|
202
|
4 |
|
foreach ($q as $riga) { |
203
|
4 |
|
$campo = GrigliaUtils::getCampiEsclusi($riga, $output); |
204
|
4 |
|
if ($campo) { |
205
|
|
|
$escludi[] = $campo; |
206
|
|
|
} |
207
|
4 |
|
} |
208
|
|
|
|
209
|
4 |
|
return $escludi; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.