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