Completed
Push — master ( 175c72...a3c945 )
by Andrea
08:57 queued 10s
created

GenerateForm::generateFormWiew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\DependencyInjection;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
7
class GenerateForm
8
{
9
10
    private $container;
11
    private $apppath;
12
13
    public function __construct($container)
14
    {
15
        $this->container = $container;
16
        $this->apppath = $container->get("pannelloamministrazione.projectpath");
17
    }
18
19
    public function generateFormsTemplates($bundlename, $entityform)
20
    {
21
        $fs = new Filesystem();
22
        //Controller
23
        $controlleFile = $this->apppath->getSrcPath() . DIRECTORY_SEPARATOR .
24
                $bundlename . DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR .
25
                $entityform . 'Controller.php';
26
        $code = $this->getControllerCode(str_replace('/', '\\', $bundlename), $entityform);
27
        $fs->dumpFile($controlleFile, $code);
28
29
        //Routing
30
        $retmsg = $this->generateFormRouting($bundlename, $entityform);
31
        //Twig template (Crea i template per new edit show)
32
        $this->generateFormWiew($bundlename, $entityform, 'edit');
33
        $this->generateFormWiew($bundlename, $entityform, 'index');
34
        $this->generateFormWiew($bundlename, $entityform, 'new');
35
36
        return $retmsg;
37
    }
38
39
    public function generateFormRouting($bundlename, $entityform)
40
    {
41
        //Routing del form
42
        $fs = new Filesystem();
43
        $routingFile = $this->apppath->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename .
44
                DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' .
45
                DIRECTORY_SEPARATOR . 'routing' . DIRECTORY_SEPARATOR . strtolower($entityform) . '.yml';
46
47
        $code = $this->getRoutingCode(str_replace('/', '', $bundlename), $entityform);
48
        $fs->dumpFile($routingFile, $code);
49
50
        //Fixed: Adesso questa parte la fa da solo symfony (05/2015)
51
        //Refixed dalla versione 2.8 non lo fa più (04/2016)
52
53
        $dest = $this->apppath->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
54
                'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'routing.yml';
55
56
        $routingContext = "\n" . str_replace('/', '', $bundlename) . '_' . $entityform . ': ' . "\n" .
57
                '  resource: "@' . str_replace('/', '', $bundlename) . '/Resources/config/routing/' . strtolower($entityform) . '.yml"' . "\n" .
58
                '  prefix: /' . $entityform . "\n";
59
60
        //Si fa l'append nel file routing del bundle per aggiungerci le rotte della tabella che stiamo gestendo
61
        $fh = fopen($dest, 'a');
62
        fwrite($fh, $routingContext);
63
        fclose($fh);
64
        $retmsg = 'Routing ' . $dest . " generato automaticamente da pannelloammonistrazionebundle\n\n* * * * CLEAR CACHE * * * *\n";
65
66
        return $retmsg;
67
    }
68
69
    public function generateFormWiew($bundlename, $entityform, $view)
70
    {
71
        $fs = new Filesystem();
72
        $folderview = $this->apppath->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
73
                'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR .
74
                $entityform . DIRECTORY_SEPARATOR;
75
        $dest = $folderview . $view . '.html.twig';
76
        $fs->mkdir($folderview);
77
        file_put_contents($dest, "{% include 'FiCoreBundle:Standard:" . $view . ".html.twig' %}");
78
    }
79
80
    public function generateFormsDefaultTableValues($entityform)
81
    {
82
        //Si inserisce il record di default nella tabella permessi
83
        $em = $this->container->get('doctrine')->getManager();
84
        $ruoloAmm = $em->getRepository('FiCoreBundle:ruoli')->findOneBy(array('is_superadmin' => true)); //SuperAdmin
85
86
        $newPermesso = new \Fi\CoreBundle\Entity\Permessi();
87
        $newPermesso->setCrud('crud');
88
        $newPermesso->setModulo($entityform);
89
        $newPermesso->setRuoli($ruoloAmm);
90
        $em->persist($newPermesso);
91
        $em->flush();
92
93
        $tabelle = new \Fi\CoreBundle\Entity\Tabelle();
94
        $tabelle->setNometabella($entityform);
95
        $em->persist($tabelle);
96
        $em->flush();
97
    }
98
99
    public function getControllerCode($bundlename, $tabella)
100
    {
101
        $codeTemplate = <<<EOF
102
<?php
103
namespace [bundle]\Controller;
104
105
use Fi\CoreBundle\Controller\FiController;
106
use Symfony\Component\HttpFoundation\Request;
107
use Symfony\Component\HttpFoundation\Response;
108
use Fi\CoreBundle\Controller\Griglia;
109
use [bundle]\Entity\[tabella];
110
use [bundle]\Form\[tabella]Type;
111
112
113
/**
114
* [tabella] controller.
115
*
116
*/
117
118
class [tabella]Controller extends FiController {
119
120
}
121
EOF;
122
        $codebundle = str_replace('[bundle]', $bundlename, $codeTemplate);
123
        $code = str_replace('[tabella]', $tabella, $codebundle);
124
125
        return $code;
126
    }
127
128
    public function getRoutingCode($bundlename, $tabella)
129
    {
130
        $codeTemplate = <<<'EOF'
131
[tabella]_container:
132
    path:  /
133
    defaults: { _controller: "[bundle]:[tabella]:index" }
134
135
[tabella]_new:
136
    path:  /new
137
    defaults: { _controller: "[bundle]:[tabella]:new" }
138
139
[tabella]_create:
140
    path:  /create
141
    defaults: { _controller: "[bundle]:[tabella]:create" }
142
    requirements: { methods: post }
143
144
[tabella]_edit:
145
    path:  /{id}/edit
146
    defaults: { _controller: "[bundle]:[tabella]:edit" }
147
148
[tabella]_update:
149
    path:  /{id}/update
150
    defaults: { _controller: "[bundle]:[tabella]:update" }
151
    requirements: { methods: post|put }
152
153
[tabella]_aggiorna:
154
    path:  /aggiorna
155
    defaults: { _controller: "[bundle]:[tabella]:aggiorna" }
156
    requirements: { methods: post|put }
157
158
[tabella]_delete:
159
    path:  /{id}/delete
160
    defaults: { _controller: "[bundle]:[tabella]:delete" }
161
    requirements: { methods: post|delete }
162
163
[tabella]_deletemultiple:
164
    path:  /delete
165
    defaults: { _controller: "[bundle]:[tabella]:delete" }
166
    requirements: { methods: post|delete }
167
168
[tabella]_griglia:
169
    path:  /griglia
170
    defaults: { _controller: "[bundle]:[tabella]:Griglia" }
171
    requirements: { methods: get|post }
172
EOF;
173
        $codebundle = str_replace('[bundle]', $bundlename, $codeTemplate);
174
        $code = str_replace('[tabella]', $tabella, $codebundle);
175
176
        return $code;
177
    }
178
}
179