1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\PannelloAmministrazioneBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Cdf\PannelloAmministrazioneBundle\Utils\ProjectPath; |
12
|
|
|
use Cdf\PannelloAmministrazioneBundle\Utils\Utility; |
13
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
14
|
|
|
|
15
|
|
|
class GenerateFormCommand extends Command |
16
|
|
|
{ |
17
|
|
|
protected static $defaultName = 'pannelloamministrazione:generateformcrud'; |
18
|
|
|
|
19
|
|
|
protected $apppaths; |
20
|
|
|
protected $em; |
21
|
|
|
protected $pammutils; |
22
|
|
|
private $generatemplate; |
23
|
|
|
private $kernel; |
24
|
|
|
|
25
|
1 |
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
1 |
|
->setDescription('Genera le views per il crud') |
29
|
1 |
|
->setHelp('Genera le views per il crud, <br/>bi.mwb AppBundle default [--schemaupdate]<br/>') |
30
|
1 |
|
->addArgument('entityform', InputArgument::REQUIRED, 'Il nome entity del form da creare') |
31
|
1 |
|
->addOption('generatemplate', InputOption::VALUE_OPTIONAL); |
32
|
1 |
|
} |
33
|
|
|
|
34
|
1 |
|
public function __construct($kernel, ProjectPath $projectpath, Utility $pammutils, ObjectManager $em) |
35
|
|
|
{ |
36
|
1 |
|
$this->kernel = $kernel; |
|
|
|
|
37
|
1 |
|
$this->apppaths = $projectpath; |
|
|
|
|
38
|
1 |
|
$this->pammutils = $pammutils; |
39
|
1 |
|
$this->em = $em; |
|
|
|
|
40
|
|
|
|
41
|
|
|
// you *must* call the parent constructor |
42
|
1 |
|
parent::__construct(); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
1 |
|
set_time_limit(0); |
48
|
|
|
|
49
|
1 |
|
$bundlename = 'App'; |
|
|
|
|
50
|
1 |
|
$entityform = $input->getArgument('entityform'); |
|
|
|
|
51
|
1 |
|
$this->generatemplate = $input->getOption('generatemplate'); |
52
|
|
|
|
53
|
1 |
|
$command = $this->apppaths->getConsole().' --env=dev'.' make:form '.$entityform.'Type '.$entityform; |
|
|
|
|
54
|
1 |
|
$resultcrud = $this->pammutils->runCommand($command); |
55
|
1 |
|
if (0 == $resultcrud['errcode']) { |
56
|
1 |
|
$fs = new Filesystem(); |
57
|
|
|
//Controller |
58
|
1 |
|
$controlleFile = $this->apppaths->getSrcPath().'/Controller/'.$entityform.'Controller.php'; |
59
|
|
|
|
60
|
1 |
|
$formFile = $this->apppaths->getSrcPath().'/Form/'.$entityform.'Type.php'; |
61
|
|
|
|
62
|
1 |
|
$lines = file($formFile, FILE_IGNORE_NEW_LINES); |
63
|
|
|
|
64
|
1 |
|
array_splice($lines, 8, 0, 'use Symfony\Component\Form\Extension\Core\Type\SubmitType;'); |
|
|
|
|
65
|
|
|
|
66
|
1 |
|
array_splice($lines, 14, 0, ' $submitparms = array(' |
67
|
1 |
|
."'label' => 'Salva','attr' => array(\"class\" => \"btn-outline-primary bisubmit\"));"); |
68
|
|
|
|
69
|
1 |
|
array_splice($lines, 16, 0, " ->add('submit', SubmitType::class, \$submitparms)"); |
70
|
|
|
|
71
|
1 |
|
array_splice($lines, count($lines) - 3, 0, " 'parametriform' => array()"); |
72
|
1 |
|
file_put_contents($formFile, implode("\n", $lines)); |
73
|
|
|
|
74
|
1 |
|
$code = $this->getControllerCode(str_replace('/', '\\', $bundlename), $entityform); |
75
|
1 |
|
$fs->dumpFile($controlleFile, $code); |
76
|
1 |
|
$output->writeln('<info>Creato '.$controlleFile.'</info>'); |
77
|
|
|
|
78
|
|
|
//Routing |
79
|
1 |
|
$retmsg = $this->generateFormRouting($entityform); |
80
|
|
|
//Twig template |
81
|
1 |
|
$this->copyTableStructureWiew($entityform); |
82
|
|
|
|
83
|
1 |
|
$this->generateFormsDefaultTableValues($entityform); |
84
|
1 |
|
$output->writeln('<info>'.$retmsg.'</info>'); |
85
|
|
|
|
86
|
1 |
|
return 0; |
87
|
|
|
} else { |
88
|
|
|
$output->writeln('<error>'.$resultcrud['message'].'</error>'); |
89
|
|
|
|
90
|
|
|
return 1; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
private function generateFormRouting($entityform) |
95
|
|
|
{ |
96
|
|
|
//Routing del form |
97
|
1 |
|
$bundlename = 'App'; |
|
|
|
|
98
|
1 |
|
$fs = new Filesystem(); |
|
|
|
|
99
|
1 |
|
$routingFile = $this->apppaths->getSrcPath().'/../config/routes/'.strtolower($entityform).'.yml'; |
100
|
|
|
|
101
|
1 |
|
$code = $this->getRoutingCode(str_replace('/', '', $bundlename), $entityform); |
102
|
1 |
|
$fs->dumpFile($routingFile, $code); |
103
|
|
|
|
104
|
1 |
|
$dest = $this->apppaths->getSrcPath().'/../config/routes.yaml'; |
105
|
|
|
|
106
|
1 |
|
$routingContext = str_replace('/', '', $bundlename).'_'.$entityform.':'."\n". |
107
|
1 |
|
' resource: routes/'.strtolower($entityform).'.yml'."\n". |
108
|
1 |
|
' prefix: /'.$entityform."\n\n"; |
109
|
|
|
|
110
|
|
|
//Si fa l'append nel file routing del bundle per aggiungerci le rotte della tabella che stiamo gestendo |
111
|
1 |
|
$fh = file_get_contents($dest); |
112
|
1 |
|
if (false !== $fh) { |
113
|
1 |
|
file_put_contents($dest, $routingContext.$fh); |
114
|
1 |
|
$retmsg = 'Routing '.$dest." generato automaticamente da pannelloammonistrazionebundle\n\n* * * * CLEAR CACHE * * * *\n"; |
115
|
|
|
} else { |
116
|
|
|
$retmsg = 'Impossibile generare il ruoting automaticamente da pannelloammonistrazionebundle\n'; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return $retmsg; |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
private function copyTableStructureWiew($entityform) |
123
|
|
|
{ |
124
|
1 |
|
$fs = new Filesystem(); |
125
|
|
|
/* $publicfolder = $this->apppaths->getPublicPath(); |
|
|
|
|
126
|
|
|
|
127
|
|
|
if (!$fs->exists($publicfolder . "/js")) { |
128
|
|
|
$fs->mkdir($publicfolder . "/js", 0777); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (!$fs->exists($publicfolder . "/css")) { |
132
|
|
|
$fs->mkdir($publicfolder . "/css", 0777); |
133
|
|
|
} */ |
134
|
|
|
|
135
|
1 |
|
$templatetablefolder = $this->apppaths->getTemplatePath().DIRECTORY_SEPARATOR.$entityform; |
136
|
1 |
|
$crudfolder = $this->kernel->locateResource('@BiCoreBundle') |
|
|
|
|
137
|
1 |
|
.DIRECTORY_SEPARATOR.'Resources/views/Standard/Crud'; |
138
|
1 |
|
$tabellafolder = $this->kernel->locateResource('@BiCoreBundle') |
|
|
|
|
139
|
1 |
|
.DIRECTORY_SEPARATOR.'Resources/views/Standard/Tabella'; |
140
|
|
|
|
141
|
1 |
|
$fs->mirror($crudfolder, $templatetablefolder.'/Crud'); |
142
|
1 |
|
if ($this->generatemplate) { |
143
|
|
|
$fs->mirror($tabellafolder, $templatetablefolder.'/Tabella'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
//$fs->touch($publicfolder . DIRECTORY_SEPARATOR . "js" . DIRECTORY_SEPARATOR . $entityform . ".js"); |
147
|
|
|
//$fs->touch($publicfolder . DIRECTORY_SEPARATOR . "css" . DIRECTORY_SEPARATOR . $entityform . ".css"); |
148
|
1 |
|
} |
149
|
|
|
|
150
|
1 |
|
private function generateFormsDefaultTableValues($entityform) |
151
|
|
|
{ |
152
|
|
|
//Si inserisce il record di default nella tabella permessi |
153
|
1 |
|
$ruoloAmm = $this->em->getRepository('BiCoreBundle:Ruoli')->findOneBy(array('superadmin' => true)); //SuperAdmin |
154
|
|
|
|
155
|
1 |
|
$newPermesso = new \Cdf\BiCoreBundle\Entity\Permessi(); |
156
|
1 |
|
$newPermesso->setCrud('crud'); |
157
|
1 |
|
$newPermesso->setModulo($entityform); |
158
|
1 |
|
$newPermesso->setRuoli($ruoloAmm); |
159
|
1 |
|
$this->em->persist($newPermesso); |
160
|
1 |
|
$this->em->flush(); |
161
|
|
|
|
162
|
1 |
|
$tabelle = new \Cdf\BiCoreBundle\Entity\Colonnetabelle(); |
163
|
1 |
|
$tabelle->setNometabella($entityform); |
164
|
1 |
|
$this->em->persist($tabelle); |
165
|
1 |
|
$this->em->flush(); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
1 |
|
private function getControllerCode($bundlename, $tabella) |
169
|
|
|
{ |
170
|
|
|
$codeTemplate = <<<EOF |
171
|
1 |
|
<?php |
172
|
|
|
namespace [bundle]\Controller; |
173
|
|
|
|
174
|
|
|
use Symfony\Component\HttpFoundation\Request; |
175
|
|
|
use Symfony\Component\HttpFoundation\Response; |
176
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
177
|
|
|
use Cdf\BiCoreBundle\Controller\FiController; |
178
|
|
|
use Cdf\BiCoreBundle\Utils\Tabella\ParametriTabella; |
179
|
|
|
use [bundle]\Entity\[tabella]; |
180
|
|
|
use [bundle]\Form\[tabella]Type; |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* [tabella] controller. |
184
|
|
|
* |
185
|
|
|
*/ |
186
|
|
|
|
187
|
|
|
class [tabella]Controller extends FiController { |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
EOF; |
191
|
1 |
|
$codebundle = str_replace('[bundle]', $bundlename, $codeTemplate); |
|
|
|
|
192
|
1 |
|
$code = str_replace('[tabella]', $tabella, $codebundle); |
|
|
|
|
193
|
|
|
|
194
|
1 |
|
return $code; |
195
|
|
|
} |
196
|
|
|
|
197
|
1 |
|
private function getRoutingCode($bundlename, $tabella) |
198
|
|
|
{ |
199
|
|
|
$codeTemplate = <<<'EOF' |
200
|
1 |
|
[tabella]_container: |
201
|
|
|
path: / |
202
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::index' } |
203
|
|
|
|
204
|
|
|
[tabella]_lista: |
205
|
|
|
path: /lista |
206
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::lista' } |
207
|
|
|
options: |
208
|
|
|
expose: true |
209
|
|
|
|
210
|
|
|
[tabella]_indexdettaglio: |
211
|
|
|
path: /indexDettaglio |
212
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::indexDettaglio' } |
213
|
|
|
options: |
214
|
|
|
expose: true |
215
|
|
|
|
216
|
|
|
[tabella]_new: |
217
|
|
|
path: /new |
218
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::new' } |
219
|
|
|
requirements: { methods: get|post } |
220
|
|
|
|
221
|
|
|
[tabella]_edit: |
222
|
|
|
path: /{id}/edit |
223
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::edit' } |
224
|
|
|
|
225
|
|
|
[tabella]_update: |
226
|
|
|
path: /{id}/update |
227
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::update' } |
228
|
|
|
requirements: { methods: post|put } |
229
|
|
|
|
230
|
|
|
[tabella]_aggiorna: |
231
|
|
|
path: /{id}/{token}/aggiorna |
232
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::aggiorna' } |
233
|
|
|
requirements: { methods: post|put } |
234
|
|
|
options: |
235
|
|
|
expose: true |
236
|
|
|
|
237
|
|
|
[tabella]_delete: |
238
|
|
|
path: /{id}/{token}/delete |
239
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::delete' } |
240
|
|
|
requirements: { methods: post|delete } |
241
|
|
|
|
242
|
|
|
[tabella]_deletemultiple: |
243
|
|
|
path: /{token}/delete |
244
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::delete' } |
245
|
|
|
requirements: { methods: post|delete } |
246
|
|
|
|
247
|
|
|
[tabella]_tabella: |
248
|
|
|
path: /tabella |
249
|
|
|
defaults: { _controller: '[bundle]\Controller\[tabella]Controller::tabella' } |
250
|
|
|
requirements: { methods: post } |
251
|
|
|
EOF; |
252
|
1 |
|
$codebundle = str_replace('[bundle]', $bundlename, $codeTemplate); |
|
|
|
|
253
|
1 |
|
$code = str_replace('[tabella]', $tabella, $codebundle); |
|
|
|
|
254
|
|
|
|
255
|
1 |
|
return $code; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
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.