|
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
|
|
|
|
|
12
|
|
|
class Fifree2configuratorimportCommand extends ContainerAwareCommand |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
private $forceupdate = false; |
|
16
|
|
|
private $verboso = false; |
|
|
|
|
|
|
17
|
|
|
private $dbutility; |
|
18
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
19
|
|
|
private $em; |
|
20
|
|
|
|
|
21
|
4 |
|
protected function configure() |
|
22
|
|
|
{ |
|
23
|
4 |
|
$this |
|
24
|
4 |
|
->setName('fifree2:configuratorimport') |
|
25
|
4 |
|
->setDescription('Configuratore per Fifree') |
|
26
|
4 |
|
->setHelp('Importa la configurazione di fifree da file fixtures.yml') |
|
27
|
4 |
|
->addOption('forceupdate', null, InputOption::VALUE_NONE, 'Forza update di record con id già presente') |
|
28
|
4 |
|
->addOption('verboso', null, InputOption::VALUE_NONE, 'Visualizza tutti i messaggi di importazione'); |
|
29
|
4 |
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->output = $output; |
|
|
|
|
|
|
34
|
1 |
|
$this->forceupdate = $input->getOption('forceupdate'); |
|
35
|
1 |
|
$this->verboso = $input->getOption('verboso'); |
|
|
|
|
|
|
36
|
1 |
|
$this->dbutility = $this->getContainer()->get("ficorebundle.database.utility"); |
|
|
|
|
|
|
37
|
1 |
|
$this->em = $this->getContainer()->get("doctrine")->getManager(); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
1 |
|
$fixturefile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "fixtures.yml"; |
|
|
|
|
|
|
40
|
1 |
|
return $this->import($fixturefile); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
protected function import($fixturefile) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$fs = new Filesystem; |
|
46
|
1 |
|
if ($fs->exists($fixturefile)) { |
|
47
|
1 |
|
$fixtures = Yaml::parse(file_get_contents($fixturefile)); |
|
48
|
1 |
|
$msg = "<info>Trovate " . count($fixtures) . " entities nel file " . $fixturefile . "</info>"; |
|
|
|
|
|
|
49
|
1 |
|
$this->output->writeln($msg); |
|
50
|
1 |
|
foreach ($fixtures as $entityclass => $fixture) { |
|
51
|
1 |
|
$this->executeImport($entityclass, $fixture); |
|
52
|
1 |
|
} |
|
53
|
1 |
|
return 0; |
|
54
|
|
|
} else { |
|
55
|
1 |
|
$msgerr = "<error>Non trovato file " . $fixturefile . "</error>"; |
|
|
|
|
|
|
56
|
1 |
|
$this->output->writeln($msgerr); |
|
57
|
1 |
|
return 1; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
private function executeImport($entityclass, $fixture) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$msg = "<info>Trovati " . count($fixture) . " record per l'entity " . $entityclass . "</info>"; |
|
|
|
|
|
|
64
|
1 |
|
$this->output->writeln($msg); |
|
65
|
1 |
|
foreach ($fixture as $record) { |
|
66
|
1 |
|
$objrecord = $this->em->getRepository($entityclass)->find($record["id"]); |
|
|
|
|
|
|
67
|
1 |
|
if ($objrecord) { |
|
68
|
1 |
|
if ($this->forceupdate) { |
|
69
|
1 |
|
$retcode = $this->executeUpdate($entityclass, $record, $objrecord); |
|
70
|
1 |
|
if ($retcode !== 0) { |
|
71
|
|
|
return 1; |
|
72
|
|
|
} |
|
73
|
1 |
|
} else { |
|
74
|
|
|
$msgerr = "<error>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
|
|
75
|
|
|
. " non modificata, specificare l'opzione --forceupdate " |
|
76
|
|
|
. "per sovrascrivere record presenti</error>"; |
|
|
|
|
|
|
77
|
|
|
$this->output->writeln($msgerr); |
|
78
|
|
|
} |
|
79
|
1 |
|
} else { |
|
80
|
1 |
|
$retcode = $this->executeInsert($entityclass, $record); |
|
81
|
1 |
|
if ($retcode !== 0) { |
|
82
|
|
|
return 1; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
1 |
|
} |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
private function executeInsert($entityclass, $record) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$objrecord = new $entityclass(); |
|
91
|
|
|
|
|
92
|
1 |
|
foreach ($record as $key => $value) { |
|
93
|
1 |
|
if ($key !== 'id' && $value) { |
|
94
|
1 |
|
$joincolumn = $this->dbutility->getJoinTableField($entityclass, $key); |
|
|
|
|
|
|
95
|
1 |
|
$joincolumnproperty = $this->dbutility->getJoinTableFieldProperty($entityclass, $key); |
|
96
|
|
|
|
|
97
|
1 |
|
if ($joincolumn && $joincolumnproperty) { |
|
98
|
|
|
$joincolumnobj = $this->em->getRepository($joincolumn)->find($value); |
|
99
|
|
|
$msgok = "<info>Inserimento " . $entityclass . " con id " . $record["id"] |
|
|
|
|
|
|
100
|
|
|
. " per campo " . $key |
|
|
|
|
|
|
101
|
|
|
. " con valore " . print_r($value, true) . " tramite entity find</info>"; |
|
|
|
|
|
|
102
|
|
|
$this->output->writeln($msgok); |
|
103
|
|
|
$joinobj = $this->dbutility->getEntityProperties($joincolumnproperty, new $entityclass()); |
|
|
|
|
|
|
104
|
|
|
$setfieldname = $joinobj["set"]; |
|
|
|
|
|
|
105
|
|
|
$objrecord->$setfieldname($joincolumnobj); |
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
$propertyEntity = $this->dbutility->getEntityProperties($key, $objrecord); |
|
110
|
1 |
|
$setfieldname = $propertyEntity["set"]; |
|
|
|
|
|
|
111
|
1 |
|
$objrecord->$setfieldname($value); |
|
112
|
1 |
|
} |
|
113
|
1 |
|
} |
|
114
|
1 |
|
$this->em->persist($objrecord); |
|
115
|
1 |
|
$this->em->flush(); |
|
116
|
1 |
|
$this->em->clear(); |
|
117
|
1 |
|
$infomsg = "<info>" . $entityclass . " con id " . $objrecord->getId() . " aggiunta</info>"; |
|
|
|
|
|
|
118
|
1 |
|
$this->output->writeln($infomsg); |
|
119
|
1 |
|
if ($record["id"] !== $objrecord->getId()) { |
|
|
|
|
|
|
120
|
|
|
try { |
|
121
|
1 |
|
$qb = $this->em->createQueryBuilder(); |
|
122
|
1 |
|
$q = $qb->update($entityclass, 'u') |
|
|
|
|
|
|
123
|
1 |
|
->set('u.id', ":newid") |
|
|
|
|
|
|
124
|
1 |
|
->where('u.id = :oldid') |
|
125
|
1 |
|
->setParameter("newid", $record["id"]) |
|
|
|
|
|
|
126
|
1 |
|
->setParameter("oldid", $objrecord->getId()) |
|
|
|
|
|
|
127
|
1 |
|
->getQuery(); |
|
128
|
1 |
|
$q->execute(); |
|
129
|
1 |
|
$msgok = "<info>" . $entityclass . " con id " . $objrecord->getId() . " sistemata</info>"; |
|
|
|
|
|
|
130
|
1 |
|
$this->output->writeln($msgok); |
|
131
|
1 |
|
} catch (\Exception $exc) { |
|
132
|
|
|
echo $exc->getMessage(); |
|
133
|
|
|
return 1; |
|
134
|
|
|
} |
|
135
|
1 |
|
} |
|
136
|
1 |
|
return 0; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
1 |
|
private function executeUpdate($entityclass, $record, $objrecord) |
|
140
|
|
|
{ |
|
141
|
1 |
|
foreach ($record as $key => $value) { |
|
142
|
1 |
|
if ($key !== 'id') { |
|
143
|
1 |
|
$propertyEntity = $this->dbutility->getEntityProperties($key, $objrecord); |
|
144
|
1 |
|
$getfieldname = $propertyEntity["get"]; |
|
|
|
|
|
|
145
|
1 |
|
$setfieldname = $propertyEntity["set"]; |
|
|
|
|
|
|
146
|
1 |
|
$cambiato = $this->dbutility->isRecordChanged($entityclass, $key, $objrecord->$getfieldname(), $value); |
|
|
|
|
|
|
147
|
1 |
|
if (!$cambiato) { |
|
148
|
1 |
|
if ($this->verboso) { |
|
149
|
1 |
|
$msginfo = "<info>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
|
|
150
|
1 |
|
. " per campo " . $key . " non modificato perchè già " |
|
|
|
|
|
|
151
|
1 |
|
. $value . "</info>"; |
|
|
|
|
|
|
152
|
1 |
|
$this->output->writeln($msginfo); |
|
153
|
1 |
|
} |
|
154
|
1 |
|
} else { |
|
155
|
|
|
try { |
|
156
|
1 |
|
$fieldtype = $this->dbutility->getFieldType($objrecord, $key); |
|
157
|
1 |
|
if ($fieldtype === 'datetime') { |
|
158
|
1 |
|
$date = new \DateTime(); |
|
159
|
1 |
|
$date->setTimestamp($value); |
|
160
|
1 |
|
$msgok = "<info>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
|
|
161
|
1 |
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
|
|
162
|
|
|
//. (!is_null($objrecord->$getfieldname())) ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "(null)" |
|
|
|
|
|
|
163
|
1 |
|
. ($objrecord->$getfieldname() ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "") |
|
|
|
|
|
|
164
|
1 |
|
. " a " . $date->format("Y-m-d H:i:s") . " in formato DateTime</info>"; |
|
|
|
|
|
|
165
|
1 |
|
$this->output->writeln($msgok); |
|
166
|
1 |
|
$objrecord->$setfieldname($date); |
|
167
|
1 |
|
continue; |
|
168
|
|
|
} |
|
169
|
1 |
|
if (is_array($value)) { |
|
170
|
1 |
|
$msgarray = "<info>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
|
|
171
|
1 |
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
|
|
172
|
1 |
|
. json_encode($objrecord->$getfieldname()) . " a " |
|
|
|
|
|
|
173
|
1 |
|
. json_encode($value) . " in formato array" . "</info>"; |
|
|
|
|
|
|
174
|
1 |
|
$this->output->writeln($msgarray); |
|
175
|
1 |
|
$objrecord->$setfieldname($value); |
|
176
|
1 |
|
continue; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
$joincolumn = $this->dbutility->getJoinTableField($entityclass, $key); |
|
|
|
|
|
|
180
|
1 |
|
$joincolumnproperty = $this->dbutility->getJoinTableFieldProperty($entityclass, $key); |
|
181
|
1 |
|
if ($joincolumn && $joincolumnproperty) { |
|
182
|
1 |
|
$joincolumnobj = $this->em->getRepository($joincolumn)->find($value); |
|
183
|
1 |
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $record["id"] |
|
|
|
|
|
|
184
|
1 |
|
. " per campo " . $key . " cambio valore da " . print_r($objrecord->$getfieldname(), true) |
|
|
|
|
|
|
185
|
1 |
|
. " a " . print_r($value, true) . " tramite entity find</info>"; |
|
|
|
|
|
|
186
|
1 |
|
$this->output->writeln($msgok); |
|
187
|
1 |
|
$joinobj = $this->dbutility->getEntityProperties($joincolumnproperty, new $entityclass()); |
|
|
|
|
|
|
188
|
1 |
|
$setfieldname = $joinobj["set"]; |
|
|
|
|
|
|
189
|
1 |
|
$objrecord->$setfieldname($joincolumnobj); |
|
190
|
1 |
|
continue; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $record["id"] |
|
|
|
|
|
|
194
|
1 |
|
. " per campo " . $key . " cambio valore da " . print_r($objrecord->$getfieldname(), true) |
|
|
|
|
|
|
195
|
1 |
|
. " a " . print_r($value, true) . "</info>"; |
|
|
|
|
|
|
196
|
1 |
|
$this->output->writeln($msgok); |
|
197
|
1 |
|
$objrecord->$setfieldname($value); |
|
198
|
1 |
|
} catch (\Exception $exc) { |
|
199
|
|
|
$msgerr = "<error>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
|
|
200
|
|
|
. " per campo " . $key . ", ERRORE: " . $exc->getMessage() |
|
|
|
|
|
|
201
|
|
|
. " alla riga " . $exc->getLine() . "</error>"; |
|
|
|
|
|
|
202
|
|
|
$this->output->writeln($msgerr); |
|
203
|
|
|
//dump($exc); |
|
204
|
|
|
return 1; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
1 |
|
} |
|
208
|
1 |
|
} |
|
209
|
1 |
|
$this->em->persist($objrecord); |
|
210
|
1 |
|
$this->em->flush(); |
|
211
|
1 |
|
$this->em->clear(); |
|
212
|
|
|
|
|
213
|
1 |
|
return 0; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
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.