Passed
Branch dev (406128)
by Sergey
51:41
created

TemplatesController   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 316
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 142
c 2
b 0
f 0
dl 0
loc 316
rs 10
wmc 27

10 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 29 1
A deleteProcess() 0 15 2
A editProcess() 0 14 2
A _type_location() 0 9 2
A rename() 0 29 5
A renameProcess() 0 21 3
A edit() 0 37 5
A add() 0 26 1
A addProcess() 0 29 4
A duplicateProcess() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype;
6
7
use Flextype\Component\Filesystem\Filesystem;
0 ignored issues
show
Bug introduced by
The type Flextype\Component\Filesystem\Filesystem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Psr\Http\Message\ResponseInterface as Response;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Psr\Http\Message\ServerRequestInterface as Request;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use function date;
11
use function Flextype\Component\I18n\__;
0 ignored issues
show
introduced by
The function Flextype\Component\I18n\__ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
12
13
/**
14
 * @property twig $twig
15
 * @property Router $router
16
 * @property Cache $cache
17
 * @property Themes $themes
18
 * @property Slugify $slugify
19
 * @property Flash $flash
20
 */
21
class TemplatesController extends Container
0 ignored issues
show
Bug introduced by
The type Flextype\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
{
23
    /**
24
     * Index page
25
     *
26
     * @param Request  $request  PSR7 request
27
     * @param Response $response PSR7 response
28
     */
29
    public function index(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response
30
    {
31
        // Get theme from request query params
32
        $theme = $request->getQueryParams()['theme'];
33
34
        return $this->twig->render(
35
            $response,
36
            'plugins/themes-admin/templates/extends/themes/templates/index.html',
37
            [
38
                'menu_item' => 'themes',
39
                'theme' => $theme,
40
                'templates_list' => $this->themes->getTemplates($theme),
41
                'partials_list' => $this->themes->getPartials($theme),
42
                'links' =>  [
43
                    'themes' => [
44
                        'link' => $this->router->pathFor('admin.themes.index'),
45
                        'title' => __('themes_admin_themes'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
46
47
                    ],
48
                    'templates' => [
49
                        'link' => $this->router->pathFor('admin.templates.index') . '?theme=' . $theme,
50
                        'title' => __('themes_admin_templates'),
51
                        'active' => true
52
                    ],
53
                ],
54
                'buttons' => [
55
                    'templates_create' => [
56
                        'link' => $this->router->pathFor('admin.templates.add') . '?theme=' . $theme,
57
                        'title' => __('admin_create_new_template'),
58
59
                    ],
60
                ],
61
            ]
62
        );
63
    }
64
65
    /**
66
     * Add template
67
     *
68
     * @param Request  $request  PSR7 request
69
     * @param Response $response PSR7 response
70
     */
71
    public function add(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response
72
    {
73
        // Get theme from request query params
74
        $theme = $request->getQueryParams()['theme'];
75
76
        return $this->twig->render(
77
            $response,
78
            'plugins/themes-admin/templates/extends/themes/templates/add.html',
79
            [
80
                'menu_item' => 'themes',
81
                'theme' => $theme,
82
                'links' =>  [
83
                    'themes' => [
84
                        'link' => $this->router->pathFor('admin.themes.index'),
85
                        'title' => __('themes_admin_themes'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
86
87
                    ],
88
                    'templates' => [
89
                        'link' => $this->router->pathFor('admin.templates.index') . '?theme=' . $theme,
90
                        'title' => __('themes_admin_templates'),
91
92
                    ],
93
                    'templates_add' => [
94
                        'link' => $this->router->pathFor('admin.templates.add') . '?theme=' . $theme,
95
                        'title' => __('admin_create_new_template'),
96
                        'active' => true
97
                    ],
98
                ],
99
            ]
100
        );
101
    }
102
103
    /**
104
     * Add template process
105
     *
106
     * @param Request  $request  PSR7 request
107
     * @param Response $response PSR7 response
108
     */
109
    public function addProcess(Request $request, Response $response) : Response
110
    {
111
        // Get data from POST
112
        $post_data = $request->getParsedBody();
113
114
        $id    = $post_data['id'];
115
        $type  = $post_data['type'];
116
        $theme = $post_data['theme'];
117
118
        $file = PATH['site'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $this->slugify->slugify($id) . '.html';
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
119
120
        if (! Filesystem::has($file)) {
121
            if (Filesystem::write(
122
                $file,
123
                ''
124
            )) {
125
                $this->flash->addMessage('success', __('admin_message_' . $type . '_created'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
                $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_' . $type . '_created'));
Loading history...
126
            } else {
127
                $this->flash->addMessage('error', __('admin_message_' . $type . '_was_not_created'));
128
            }
129
        } else {
130
            $this->flash->addMessage('error', __('admin_message_' . $type . '_was_not_created'));
131
        }
132
133
        if (isset($post_data['create-and-edit'])) {
134
            return $response->withRedirect($this->router->pathFor('admin.templates.edit') . '?theme=' . $theme . '&type=' . $type . '&id=' . $id);
135
        }
136
137
        return $response->withRedirect($this->router->pathFor('admin.templates.index') . '?theme=' . $theme);
138
    }
139
140
    /**
141
     * Edit template
142
     *
143
     * @param Request  $request  PSR7 request
144
     * @param Response $response PSR7 response
145
     */
146
    public function edit(Request $request, Response $response) : Response
147
    {
148
        // Get type and theme from request query params
149
        $type  = $request->getQueryParams()['type'];
150
        $theme = $request->getQueryParams()['theme'];
151
152
        return $this->twig->render(
153
            $response,
154
            'plugins/themes-admin/templates/extends/themes/templates/edit.html',
155
            [
156
                'menu_item' => 'themes',
157
                'theme' => $theme,
158
                'id' => $request->getQueryParams()['id'],
159
                'data' => Filesystem::read(PATH['site'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getQueryParams()['id'] . '.html'),
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
160
                'type' => ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template'),
161
                'links' => [
162
                    'themes' => [
163
                        'link' => $this->router->pathFor('admin.themes.index'),
164
                        'title' => __('themes_admin_themes'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
                        'title' => /** @scrutinizer ignore-call */ __('themes_admin_themes'),
Loading history...
165
166
                    ],
167
                    'templates' => [
168
                        'link' => $this->router->pathFor('admin.templates.index') . '?theme=' . $theme,
169
                        'title' => __('themes_admin_templates'),
170
171
                    ],
172
                    'templates_editor' => [
173
                        'link' => $this->router->pathFor('admin.templates.edit') . '?id=' . $request->getQueryParams()['id'] . '&type=' . ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template') . '&theme=' . $theme,
174
                        'title' => __('admin_editor'),
175
                        'active' => true
176
                    ],
177
                ],
178
                'buttons' => [
179
                    'save_template' => [
180
                        'link'       => 'javascript:;',
181
                        'title'      => __('admin_save'),
182
                        'type' => 'action',
183
                    ],
184
                ],
185
            ]
186
        );
187
    }
188
189
    /**
190
     * Edit template process
191
     *
192
     * @param Request  $request  PSR7 request
193
     * @param Response $response PSR7 response
194
     */
195
    public function editProcess(Request $request, Response $response) : Response
196
    {
197
        // Get theme and type and id from request query params
198
        $theme = $request->getParsedBody()['theme'];
199
        $id    = $request->getParsedBody()['id'];
200
        $type  = $request->getParsedBody()['type'];
201
202
        if (Filesystem::write(PATH['site'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()['id'] . '.html', $request->getParsedBody()['data'])) {
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
203
            $this->flash->addMessage('success', __('admin_message_' . $type . '_saved'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

203
            $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_' . $type . '_saved'));
Loading history...
204
        } else {
205
            $this->flash->addMessage('error', __('admin_message_' . $type . '_was_not_saved'));
206
        }
207
208
        return $response->withRedirect($this->router->pathFor('admin.templates.edit') . '?id=' . $id . '&type=' . $type . '&theme=' . $theme);
209
    }
210
211
    /**
212
     * Rename template
213
     *
214
     * @param Request  $request  PSR7 request
215
     * @param Response $response PSR7 response
216
     */
217
    public function rename(Request $request, Response $response) : Response
218
    {
219
        // Get theme from request query params
220
        $theme = $request->getQueryParams()['theme'];
221
222
        return $this->twig->render(
223
            $response,
224
            'plugins/themes-admin/templates/extends/themes/templates/rename.html',
225
            [
226
                'menu_item' => 'themes',
227
                'theme' => $theme,
228
                'types' => ['partial' => __('admin_partial'), 'template' => __('admin_template')],
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

228
                'types' => ['partial' => /** @scrutinizer ignore-call */ __('admin_partial'), 'template' => __('admin_template')],
Loading history...
229
                'id_current' => $request->getQueryParams()['id'],
230
                'type_current' => ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template'),
231
                'links' => [
232
                    'themes' => [
233
                        'link' => $this->router->pathFor('admin.themes.index'),
234
                        'title' => __('themes_admin_themes'),
235
236
                    ],
237
                    'templates' => [
238
                        'link' => $this->router->pathFor('admin.templates.index') . '?theme=' . $theme,
239
                        'title' => __('themes_admin_templates'),
240
241
                    ],
242
                    'templates_rename' => [
243
                        'link' => $this->router->pathFor('admin.templates.rename') . '?id=' . $request->getQueryParams()['id'] . '&type=' . ($request->getQueryParams()['type'] && $request->getQueryParams()['type'] === 'partial' ? 'partial' : 'template') . '&theme=' . $theme,
244
                        'title' => __('admin_rename'),
245
                        'active' => true
246
                    ],
247
                ],
248
            ]
249
        );
250
    }
251
252
    /**
253
     * Rename template process
254
     *
255
     * @param Request  $request  PSR7 request
256
     * @param Response $response PSR7 response
257
     */
258
    public function renameProcess(Request $request, Response $response) : Response
259
    {
260
        // Get theme and type from request query params
261
        $theme = $request->getParsedBody()['theme'];
262
        $type  = $request->getParsedBody()['type_current'];
263
264
        if (! Filesystem::has(PATH['site'] . '/themes/' . $this->registry->get('plugins.site.settings.theme') . '/' . $this->_type_location($type) . $request->getParsedBody()['id'] . '.html')) {
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
265
            if (Filesystem::rename(
266
                PATH['site'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()['id_current'] . '.html',
267
                PATH['site'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()['id'] . '.html'
268
            )
269
            ) {
270
                $this->flash->addMessage('success', __('admin_message_' . $type . '_renamed'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

270
                $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_' . $type . '_renamed'));
Loading history...
271
            } else {
272
                $this->flash->addMessage('error', __('admin_message_' . $type . '_was_not_renamed'));
273
            }
274
        } else {
275
            $this->flash->addMessage('error', __('admin_message_' . $type . '_was_not_renamed'));
276
        }
277
278
        return $response->withRedirect($this->router->pathFor('admin.templates.index') . '?theme=' . $theme);
279
    }
280
281
    /**
282
     * Delete template process
283
     *
284
     * @param Request  $request  PSR7 request
285
     * @param Response $response PSR7 response
286
     */
287
    public function deleteProcess(Request $request, Response $response) : Response
288
    {
289
        // Get theme and type from request query params
290
        $theme = $request->getParsedBody()['theme'];
291
        $type  = $request->getParsedBody()['type'];
292
293
        $file_path = PATH['site'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()[$type . '-id'] . '.html';
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
294
295
        if (Filesystem::delete($file_path)) {
296
            $this->flash->addMessage('success', __('admin_message_' . $type . '_deleted'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

296
            $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_' . $type . '_deleted'));
Loading history...
297
        } else {
298
            $this->flash->addMessage('error', __('admin_message_' . $type . '_was_not_deleted'));
299
        }
300
301
        return $response->withRedirect($this->router->pathFor('admin.templates.index') . '?theme=' . $theme);
302
    }
303
304
    /**
305
     * Duplicate template process
306
     *
307
     * @param Request  $request  PSR7 request
308
     * @param Response $response PSR7 response
309
     */
310
    public function duplicateProcess(Request $request, Response $response) : Response
311
    {
312
        // Get theme and type from request query params
313
        $theme = $request->getParsedBody()['theme'];
314
        $type  = $request->getParsedBody()['type'];
315
316
        $file_path     = PATH['site'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()[$type . '-id'] . '.html';
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
317
        $file_path_new = PATH['site'] . '/themes/' . $theme . '/' . $this->_type_location($type) . $request->getParsedBody()[$type . '-id'] . '-duplicate-' . date('Ymd_His') . '.html';
318
319
        if (Filesystem::copy($file_path, $file_path_new)) {
320
            $this->flash->addMessage('success', __('admin_message_' . $type . '_duplicated'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

320
            $this->flash->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_' . $type . '_duplicated'));
Loading history...
321
        } else {
322
            $this->flash->addMessage('error', __('admin_message_' . $type . '_was_not_duplicated'));
323
        }
324
325
        return $response->withRedirect($this->router->pathFor('admin.templates.index') . '?theme=' . $theme);
326
    }
327
328
    private function _type_location($type)
329
    {
330
        if ($type === 'partial') {
331
            $_type = '/templates/partials/';
332
        } else {
333
            $_type = '/templates/';
334
        }
335
336
        return $_type;
337
    }
338
}
339