Passed
Push — master ( 11cbe7...4169a6 )
by
unknown
16:03
created

TypoScriptTemplateInformationModuleFunctionController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 71
dl 0
loc 176
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getLanguageService() 0 3 1
A initialize_editor() 0 10 2
A tableRowData() 0 19 1
B main() 0 61 8
A init() 0 5 1
A __construct() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Tstemplate\Controller;
17
18
use Psr\Http\Message\ServerRequestInterface;
19
use TYPO3\CMS\Backend\Routing\UriBuilder;
20
use TYPO3\CMS\Core\Http\PropagateResponseException;
21
use TYPO3\CMS\Core\Http\RedirectResponse;
22
use TYPO3\CMS\Core\Localization\LanguageService;
23
use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Fluid\View\StandaloneView;
26
27
/**
28
 * This class displays the Info/Modify screen of the Web > Template module
29
 * @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
30
 */
31
class TypoScriptTemplateInformationModuleFunctionController
32
{
33
34
    /**
35
     * @var TypoScriptTemplateModuleController
36
     */
37
    protected $pObj;
38
39
    /**
40
     * The currently selected sys_template record
41
     * @var array
42
     */
43
    protected $templateRow;
44
45
    /**
46
     * @var ExtendedTemplateService
47
     */
48
    protected $templateService;
49
50
    /**
51
     * @var int GET/POST var 'id'
52
     */
53
    protected $id;
54
55
    /**
56
     * @var ServerRequestInterface
57
     */
58
    protected $request;
59
60
    protected UriBuilder $uriBuilder;
61
62
    public function __construct(UriBuilder $uriBuilder)
63
    {
64
        $this->uriBuilder = $uriBuilder;
65
    }
66
    /**
67
     * Init, called from parent object
68
     *
69
     * @param TypoScriptTemplateModuleController $pObj A reference to the parent (calling) object
70
     * @param ServerRequestInterface $request
71
     */
72
    public function init($pObj, ServerRequestInterface $request)
73
    {
74
        $this->pObj = $pObj;
75
        $this->request = $request;
76
        $this->id = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);
77
    }
78
79
    /**
80
     * Gets the data for a row of a HTML table in the fluid template
81
     *
82
     * @param string $label The label to be shown (e.g. 'Title:')
83
     * @param string $data The data/information to be shown (e.g. 'Template for my site')
84
     * @param string $field The field/variable to be sent on clicking the edit icon (e.g. 'title')
85
     * @param int $id The field/variable to be sent on clicking the edit icon (e.g. 'title')
86
     * @return array Data for a row of a HTML table
87
     */
88
    protected function tableRowData($label, $data, $field, $id)
89
    {
90
        $urlParameters = [
91
            'id' => $this->id,
92
            'edit' => [
93
                'sys_template' => [
94
                    $id => 'edit'
95
                ]
96
            ],
97
            'columnsOnly' => $field,
98
            'createExtension' => 0,
99
            'returnUrl' => $this->request->getAttribute('normalizedParams')->getRequestUri()
100
        ];
101
        $url = (string)$this->uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
102
103
        return [
104
            'url' => $url,
105
            'data' => $data,
106
            'label' => $label
107
        ];
108
    }
109
110
    /**
111
     * Create an instance of \TYPO3\CMS\Core\TypoScript\ExtendedTemplateService
112
     * and looks for the first (visible) template
113
     * record. If $template_uid was given and greater than zero, this record will be checked.
114
     *
115
     * Initializes the module. Done in this function because we may need to re-initialize if data is submitted!
116
     *
117
     * @param int $pageId The uid of the current page
118
     * @param int $template_uid The uid of the template record to be rendered (only if more than one template on the current page)
119
     * @return bool Returns TRUE if a template record was found, otherwise FALSE
120
     */
121
    protected function initialize_editor($pageId, $template_uid = 0)
122
    {
123
        $this->templateService = GeneralUtility::makeInstance(ExtendedTemplateService::class);
124
125
        // Get the row of the first VISIBLE template of the page. where clause like the frontend.
126
        $this->templateRow = $this->templateService->ext_getFirstTemplate($pageId, $template_uid);
127
        if (is_array($this->templateRow)) {
128
            return true;
129
        }
130
        return false;
131
    }
132
133
    /**
134
     * Main, called from parent object
135
     *
136
     * @return string Information of the template status or the taken actions as HTML string
137
     */
138
    public function main()
139
    {
140
        // Checking for more than one template an if, set a menu...
141
        $manyTemplatesMenu = $this->pObj->templateMenu($this->request);
142
        $template_uid = 0;
143
        if ($manyTemplatesMenu) {
144
            $template_uid = $this->pObj->MOD_SETTINGS['templatesOnPage'];
145
        }
146
        // Initialize
147
        $existTemplate = $this->initialize_editor($this->id, $template_uid);
148
        $saveId = 0;
149
        if ($existTemplate) {
150
            $saveId = $this->templateRow['_ORIG_uid'] ?: $this->templateRow['uid'];
151
        }
152
        // Create extension template
153
        $newId = $this->pObj->createTemplate($this->id, (int)$saveId);
154
        if ($newId) {
155
            // Switch to new template
156
            $urlParameters = [
157
                'id' => $this->id,
158
                'SET[templatesOnPage]' => $newId
159
            ];
160
            $url = $this->uriBuilder->buildUriFromRoute('web_ts', $urlParameters);
161
            throw new PropagateResponseException(new RedirectResponse($url, 303), 1607271781);
162
        }
163
        if ($existTemplate) {
164
            $lang = $this->getLanguageService();
165
            $lang->includeLLFile('EXT:tstemplate/Resources/Private/Language/locallang_info.xlf');
166
            $assigns = [];
167
            $assigns['templateRecord'] = $this->templateRow;
168
            $assigns['manyTemplatesMenu'] = $manyTemplatesMenu;
169
170
            // Processing:
171
            $tableRows = [];
172
            $tableRows[] = $this->tableRowData($lang->getLL('title'), $this->templateRow['title'], 'title', $this->templateRow['uid']);
173
            $tableRows[] = $this->tableRowData($lang->getLL('description'), $this->templateRow['description'], 'description', $this->templateRow['uid']);
174
            $tableRows[] = $this->tableRowData($lang->getLL('constants'), sprintf($lang->getLL('editToView'), trim($this->templateRow['constants']) ? count(explode(LF, $this->templateRow['constants'])) : 0), 'constants', $this->templateRow['uid']);
175
            $tableRows[] = $this->tableRowData($lang->getLL('setup'), sprintf($lang->getLL('editToView'), trim($this->templateRow['config']) ? count(explode(LF, $this->templateRow['config'])) : 0), 'config', $this->templateRow['uid']);
176
            $assigns['tableRows'] = $tableRows;
177
178
            // Edit all icon:
179
            $urlParameters = [
180
                'edit' => [
181
                    'sys_template' => [
182
                        $this->templateRow['uid'] => 'edit'
183
                    ]
184
                ],
185
                'createExtension' => 0,
186
                'returnUrl' => $this->request->getAttribute('normalizedParams')->getRequestUri()
187
            ];
188
            $assigns['editAllUrl'] = (string)$this->uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
189
190
            // Rendering of the output via fluid
191
            $view = GeneralUtility::makeInstance(StandaloneView::class);
192
            $view->setTemplatePathAndFilename('EXT:tstemplate/Resources/Private/Templates/InformationModule.html');
193
            $view->assignMultiple($assigns);
194
            $theOutput = $view->render();
195
        } else {
196
            $theOutput = $this->pObj->noTemplate(1);
197
        }
198
        return $theOutput;
199
    }
200
201
    /**
202
     * @return LanguageService
203
     */
204
    protected function getLanguageService(): LanguageService
205
    {
206
        return $GLOBALS['LANG'];
207
    }
208
}
209