|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
11
|
|
|
use Fi\CoreBundle\Utils\FieldTypeUtility; |
|
12
|
|
|
|
|
13
|
|
|
class Fifree2configuratorimportCommand extends ContainerAwareCommand |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
private $forceupdate = false; |
|
17
|
|
|
private $verboso = false; |
|
18
|
|
|
private $dbutility; |
|
19
|
|
|
private $systementity; |
|
20
|
|
|
private $entityutility; |
|
21
|
|
|
private $em; |
|
22
|
|
|
private $truncatetables; |
|
23
|
|
|
private $output; |
|
24
|
|
|
|
|
25
|
6 |
|
protected function configure() |
|
26
|
|
|
{ |
|
27
|
|
|
$this |
|
28
|
6 |
|
->setName('fifree2:configuratorimport') |
|
29
|
6 |
|
->setDescription('Configuratore per Fifree') |
|
30
|
6 |
|
->setHelp('Importa la configurazione di fifree da file fixtures.yml') |
|
31
|
6 |
|
->addOption('forceupdate', null, InputOption::VALUE_NONE, 'Forza update di record con id già presente') |
|
32
|
6 |
|
->addOption('truncatetables', null, InputOption::VALUE_NONE, 'Esegue una truncate della tabelle') |
|
33
|
6 |
|
->addOption('verboso', null, InputOption::VALUE_NONE, 'Visualizza tutti i messaggi di importazione'); |
|
34
|
6 |
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
37
|
|
|
{ |
|
38
|
2 |
|
$this->output = $output; |
|
39
|
2 |
|
$this->forceupdate = $input->getOption('forceupdate'); |
|
40
|
2 |
|
$this->verboso = $input->getOption('verboso'); |
|
41
|
2 |
|
$this->truncatetables = $input->getOption('truncatetables'); |
|
42
|
2 |
|
$this->dbutility = $this->getContainer()->get("ficorebundle.database.utility"); |
|
43
|
2 |
|
$this->entityutility = $this->getContainer()->get("ficorebundle.entity.utility"); |
|
44
|
2 |
|
$this->systementity = $this->getContainer()->get("ficorebundle.entity.system"); |
|
45
|
2 |
|
$this->em = $this->getContainer()->get("doctrine")->getManager(); |
|
46
|
|
|
|
|
47
|
2 |
|
$this->checkSchemaStatus(); |
|
48
|
2 |
|
$this->checkSysstemTables(); |
|
49
|
|
|
|
|
50
|
2 |
|
$fixturefile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "fixtures.yml"; |
|
51
|
2 |
|
$ret = $this->import($fixturefile); |
|
52
|
2 |
|
return $ret; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
protected function import($fixturefile) |
|
56
|
|
|
{ |
|
57
|
2 |
|
$fs = new Filesystem; |
|
58
|
2 |
|
if ($fs->exists($fixturefile)) { |
|
59
|
2 |
|
$fixtures = Yaml::parse(file_get_contents($fixturefile)); |
|
60
|
2 |
|
$msg = "<info>Trovate " . count($fixtures) . " entities nel file " . $fixturefile . "</info>"; |
|
61
|
2 |
|
$this->output->writeln($msg); |
|
62
|
|
|
|
|
63
|
2 |
|
if ($this->truncatetables) { |
|
64
|
1 |
|
foreach ($fixtures as $entityclass => $fixture) { |
|
65
|
1 |
|
$this->truncateTable($entityclass); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
2 |
|
$sortedEntities = $this->getSortedEntities($fixtures); |
|
69
|
2 |
|
foreach ($sortedEntities as $entityclass => $fixture) { |
|
70
|
2 |
|
$ret = $this->executeImport($entityclass, $fixture); |
|
71
|
2 |
|
if ($ret == 1) { |
|
72
|
|
|
return 1; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
2 |
|
return 0; |
|
76
|
|
|
} else { |
|
77
|
1 |
|
$msgerr = "<error>Non trovato file " . $fixturefile . "</error>"; |
|
78
|
1 |
|
$this->output->writeln($msgerr); |
|
79
|
1 |
|
return 1; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
private function checkSysstemTables() |
|
84
|
|
|
{ |
|
85
|
2 |
|
$this->systementity->dumpSystemEntities(); |
|
86
|
2 |
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
private function checkSchemaStatus() |
|
89
|
|
|
{ |
|
90
|
2 |
|
$schemachanged = $this->dbutility->isSchemaChanged(); |
|
91
|
|
|
|
|
92
|
2 |
|
if ($schemachanged) { |
|
93
|
2 |
|
$msgerr = "<error>Attenzione, lo schema database non è aggiornato, verrà comunque tentata l'importazione</error>"; |
|
94
|
2 |
|
$this->output->writeln($msgerr); |
|
95
|
2 |
|
sleep(3); |
|
96
|
|
|
} |
|
97
|
2 |
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
private function truncateTable($entityclass) |
|
100
|
|
|
{ |
|
101
|
1 |
|
$tablename = $this->entityutility->getTableFromEntity($entityclass); |
|
102
|
1 |
|
$msg = "<info>TRUNCATE della tabella " . $tablename . " (" . $entityclass . ")</info>"; |
|
103
|
1 |
|
$this->output->writeln($msg); |
|
104
|
1 |
|
$this->dbutility->truncatetable($entityclass, true); |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
|
109
|
|
|
*/ |
|
110
|
2 |
|
private function getSortedEntities($fixtures) |
|
111
|
|
|
{ |
|
112
|
2 |
|
$entities = array(); |
|
113
|
2 |
|
$sortedEntities = $this->systementity->getSystemEntities(); |
|
114
|
2 |
|
foreach ($sortedEntities as $fixture => $details) { |
|
115
|
2 |
|
if (isset($fixtures[$fixture])) { |
|
116
|
2 |
|
$entities[$fixture] = $fixtures[$fixture]; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
2 |
|
return $entities; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
private function executeImport($entityclass, $fixture) |
|
123
|
|
|
{ |
|
124
|
2 |
|
$msg = "<info>Trovati " . count($fixture) . " record per l'entity " . $entityclass . "</info>"; |
|
125
|
2 |
|
$this->output->writeln($msg); |
|
126
|
2 |
|
foreach ($fixture as $record) { |
|
127
|
2 |
|
$objrecord = $this->em->getRepository($entityclass)->find($record["id"]); |
|
128
|
2 |
|
$ret = $this->switchInsertUpdate($entityclass, $record, $objrecord); |
|
129
|
2 |
|
if ($ret !== 0) { |
|
130
|
|
|
return 1; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
2 |
|
return 0; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
2 |
|
private function switchInsertUpdate($entityclass, $record, $objrecord) |
|
137
|
|
|
{ |
|
138
|
2 |
|
if (!$objrecord) { |
|
139
|
2 |
|
return $this->executeInsert($entityclass, $record); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
if ($this->forceupdate) { |
|
143
|
1 |
|
return $this->executeUpdate($entityclass, $record, $objrecord); |
|
144
|
|
|
} else { |
|
145
|
|
|
$msgerr = "<error>" . $entityclass . " con id " . $record["id"] |
|
146
|
|
|
. " non modificata, specificare l'opzione --forceupdate " |
|
147
|
|
|
. "per sovrascrivere record presenti</error>"; |
|
148
|
|
|
$this->output->writeln($msgerr); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
2 |
|
private function executeInsert($entityclass, $record) |
|
153
|
|
|
{ |
|
154
|
2 |
|
$objrecord = new $entityclass(); |
|
155
|
2 |
|
foreach ($record as $key => $value) { |
|
156
|
2 |
|
if ($key !== 'id') { |
|
157
|
2 |
|
$propertyEntity = $this->entityutility->getEntityProperties($key, $objrecord); |
|
158
|
2 |
|
$getfieldname = $propertyEntity["get"]; |
|
159
|
2 |
|
$setfieldname = $propertyEntity["set"]; |
|
160
|
2 |
|
$fieldtype = $this->dbutility->getFieldType($objrecord, $key); |
|
161
|
2 |
|
if ($fieldtype === 'boolean') { |
|
162
|
2 |
|
$newval = FieldTypeUtility::getBooleanValue($value); |
|
163
|
2 |
|
$msgok = "<info>Inserimento " . $entityclass . " con id " . $record["id"] |
|
164
|
2 |
|
. " per campo " . $key . " con valore " |
|
165
|
2 |
|
. var_export($newval, true) . " in formato Boolean</info>"; |
|
166
|
2 |
|
$this->output->writeln($msgok); |
|
167
|
2 |
|
$objrecord->$setfieldname($newval); |
|
168
|
2 |
|
continue; |
|
169
|
|
|
} |
|
170
|
|
|
//Si prende in considerazione solo il null del boolean, gli altri non si toccano |
|
171
|
2 |
|
if (!$value) { |
|
172
|
2 |
|
continue; |
|
173
|
|
|
} |
|
174
|
2 |
|
if ($fieldtype === 'datetime' || $fieldtype === 'date') { |
|
175
|
2 |
|
$date = FieldTypeUtility::getDateTimeValueFromTimestamp($value); |
|
176
|
2 |
|
$msgok = "<info>Inserimento " . $entityclass . " con id " . $record["id"] |
|
177
|
2 |
|
. " per campo " . $key . " cambio valore da " |
|
178
|
2 |
|
. ($objrecord->$getfieldname() ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "NULL") |
|
179
|
2 |
|
. " a " . $date->format("Y-m-d H:i:s") . " in formato DateTime</info>"; |
|
180
|
2 |
|
$this->output->writeln($msgok); |
|
181
|
2 |
|
$objrecord->$setfieldname($date); |
|
182
|
2 |
|
continue; |
|
183
|
|
|
} |
|
184
|
2 |
|
if (is_array($value)) { |
|
185
|
2 |
|
$msgarray = "<info>Inserimento " . $entityclass . " con id " . $record["id"] |
|
186
|
2 |
|
. " per campo " . $key . " cambio valore da " |
|
187
|
2 |
|
. json_encode($objrecord->$getfieldname()) . " a " |
|
188
|
2 |
|
. json_encode($value) . " in formato array" . "</info>"; |
|
189
|
2 |
|
$this->output->writeln($msgarray); |
|
190
|
2 |
|
$objrecord->$setfieldname($value); |
|
191
|
2 |
|
continue; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
2 |
|
$joincolumn = $this->entityutility->getJoinTableField($entityclass, $key); |
|
195
|
2 |
|
$joincolumnproperty = $this->entityutility->getJoinTableFieldProperty($entityclass, $key); |
|
196
|
2 |
|
if ($joincolumn && $joincolumnproperty) { |
|
197
|
2 |
|
$joincolumnobj = $this->em->getRepository($joincolumn)->find($value); |
|
198
|
2 |
|
$msgok = "<info>Inserimento " . $entityclass . " con id " . $record["id"] |
|
199
|
2 |
|
. " per campo " . $key |
|
200
|
2 |
|
. " con valore " . print_r($value, true) . " tramite entity find</info>"; |
|
|
|
|
|
|
201
|
2 |
|
$this->output->writeln($msgok); |
|
202
|
2 |
|
$joinobj = $this->entityutility->getEntityProperties($joincolumnproperty, new $entityclass()); |
|
203
|
2 |
|
$setfieldname = $joinobj["set"]; |
|
204
|
2 |
|
$objrecord->$setfieldname($joincolumnobj); |
|
205
|
2 |
|
continue; |
|
206
|
|
|
} |
|
207
|
2 |
|
$objrecord->$setfieldname($value); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
2 |
|
$this->em->persist($objrecord); |
|
211
|
2 |
|
$this->em->flush(); |
|
212
|
|
|
|
|
213
|
2 |
|
$infomsg = "<info>" . $entityclass . " con id " . $objrecord->getId() . " aggiunta</info>"; |
|
214
|
2 |
|
$this->output->writeln($infomsg); |
|
215
|
2 |
|
$checkid = $this->changeRecordId($entityclass, $record, $objrecord); |
|
216
|
2 |
|
return $checkid; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
2 |
|
private function changeRecordId($entityclass, $record, $objrecord) |
|
220
|
|
|
{ |
|
221
|
2 |
|
if ($record["id"] !== $objrecord->getId()) { |
|
222
|
|
|
try { |
|
223
|
1 |
|
$qb = $this->em->createQueryBuilder(); |
|
224
|
1 |
|
$q = $qb->update($entityclass, 'u') |
|
225
|
1 |
|
->set('u.id', ":newid") |
|
226
|
1 |
|
->where('u.id = :oldid') |
|
227
|
1 |
|
->setParameter("newid", $record["id"]) |
|
228
|
1 |
|
->setParameter("oldid", $objrecord->getId()) |
|
229
|
1 |
|
->getQuery(); |
|
230
|
1 |
|
$q->execute(); |
|
231
|
1 |
|
$msgok = "<info>" . $entityclass . " con id " . $objrecord->getId() . " sistemata</info>"; |
|
232
|
1 |
|
$this->output->writeln($msgok); |
|
233
|
|
|
} catch (\Exception $exc) { |
|
234
|
|
|
echo $exc->getMessage(); |
|
235
|
|
|
return 1; |
|
236
|
|
|
} |
|
237
|
1 |
|
$this->em->flush(); |
|
238
|
|
|
} |
|
239
|
2 |
|
return 0; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
1 |
|
private function executeUpdate($entityclass, $record, $objrecord) |
|
243
|
|
|
{ |
|
244
|
1 |
|
unset($record["id"]); |
|
245
|
1 |
|
foreach ($record as $key => $value) { |
|
246
|
1 |
|
$propertyEntity = $this->entityutility->getEntityProperties($key, $objrecord); |
|
247
|
1 |
|
$getfieldname = $propertyEntity["get"]; |
|
248
|
1 |
|
$setfieldname = $propertyEntity["set"]; |
|
249
|
1 |
|
$cambiato = $this->dbutility->isRecordChanged($entityclass, $key, $objrecord->$getfieldname(), $value); |
|
250
|
1 |
|
if (!$cambiato) { |
|
251
|
1 |
|
if ($this->verboso) { |
|
252
|
1 |
|
$msginfo = "<info>" . $entityclass . " con id " . $objrecord->getId() |
|
253
|
1 |
|
. " per campo " . $key . " non modificato perchè già " |
|
254
|
1 |
|
. $value . "</info>"; |
|
255
|
1 |
|
$this->output->writeln($msginfo); |
|
256
|
|
|
} |
|
257
|
|
|
} else { |
|
258
|
|
|
try { |
|
259
|
1 |
|
$fieldtype = $this->dbutility->getFieldType($objrecord, $key); |
|
260
|
1 |
|
if ($fieldtype === 'boolean') { |
|
261
|
|
|
$newval = FieldTypeUtility::getBooleanValue($value); |
|
262
|
|
|
|
|
263
|
|
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
264
|
|
|
. " per campo " . $key . " cambio valore da " |
|
265
|
|
|
. var_export($objrecord->$getfieldname(), true) |
|
266
|
|
|
. " a " . var_export($newval, true) . " in formato Boolean</info>"; |
|
267
|
|
|
$this->output->writeln($msgok); |
|
268
|
|
|
$objrecord->$setfieldname($newval); |
|
269
|
|
|
continue; |
|
270
|
|
|
} |
|
271
|
|
|
//Si prende in considerazione solo il null del boolean, gli altri non si toccano |
|
272
|
1 |
|
if (!$value) { |
|
273
|
1 |
|
continue; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
1 |
|
if ($fieldtype === 'datetime' || $fieldtype === 'date') { |
|
277
|
|
|
$date = FieldTypeUtility::getDateTimeValueFromTimestamp($value); |
|
278
|
|
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
279
|
|
|
. " per campo " . $key . " cambio valore da " |
|
280
|
|
|
//. (!is_null($objrecord->$getfieldname())) ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "(null)" |
|
281
|
|
|
. ($objrecord->$getfieldname() ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "NULL") |
|
282
|
|
|
. " a " . $date->format("Y-m-d H:i:s") . " in formato DateTime</info>"; |
|
283
|
|
|
$this->output->writeln($msgok); |
|
284
|
|
|
$objrecord->$setfieldname($date); |
|
285
|
|
|
continue; |
|
286
|
|
|
} |
|
287
|
1 |
|
if (is_array($value)) { |
|
288
|
1 |
|
$msgarray = "<info>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
289
|
1 |
|
. " per campo " . $key . " cambio valore da " |
|
290
|
1 |
|
. json_encode($objrecord->$getfieldname()) . " a " |
|
291
|
1 |
|
. json_encode($value) . " in formato array" . "</info>"; |
|
292
|
1 |
|
$this->output->writeln($msgarray); |
|
293
|
1 |
|
$objrecord->$setfieldname($value); |
|
294
|
1 |
|
continue; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
1 |
|
$joincolumn = $this->entityutility->getJoinTableField($entityclass, $key); |
|
298
|
1 |
|
$joincolumnproperty = $this->entityutility->getJoinTableFieldProperty($entityclass, $key); |
|
299
|
1 |
|
if ($joincolumn && $joincolumnproperty) { |
|
300
|
|
|
$joincolumnobj = $this->em->getRepository($joincolumn)->find($value); |
|
301
|
|
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
302
|
|
|
. " per campo " . $key . " cambio valore da " . print_r($objrecord->$getfieldname(), true) |
|
|
|
|
|
|
303
|
|
|
. " a " . print_r($value, true) . " tramite entity find</info>"; |
|
|
|
|
|
|
304
|
|
|
$this->output->writeln($msgok); |
|
305
|
|
|
$joinobj = $this->entityutility->getEntityProperties($joincolumnproperty, new $entityclass()); |
|
306
|
|
|
$setfieldname = $joinobj["set"]; |
|
307
|
|
|
$objrecord->$setfieldname($joincolumnobj); |
|
308
|
|
|
continue; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
1 |
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
312
|
1 |
|
. " per campo " . $key . " cambio valore da " . print_r($objrecord->$getfieldname(), true) |
|
313
|
1 |
|
. " a " . print_r($value, true) . "</info>"; |
|
314
|
1 |
|
$this->output->writeln($msgok); |
|
315
|
1 |
|
$objrecord->$setfieldname($value); |
|
316
|
|
|
} catch (\Exception $exc) { |
|
317
|
|
|
$msgerr = "<error>Modifica " . $entityclass . " con id " . $objrecord->getId() |
|
318
|
|
|
. " per campo " . $key . ", ERRORE: " . $exc->getMessage() |
|
319
|
|
|
. " alla riga " . $exc->getLine() . "</error>"; |
|
320
|
|
|
$this->output->writeln($msgerr); |
|
321
|
|
|
//dump($exc); |
|
322
|
|
|
return 1; |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
} |
|
326
|
1 |
|
$this->em->persist($objrecord); |
|
327
|
1 |
|
$this->em->flush(); |
|
328
|
|
|
|
|
329
|
1 |
|
return 0; |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|