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