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