1<?php
2namespace TYPO3\CMS\Tstemplate\Controller;
3
4/*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17use TYPO3\CMS\Backend\Utility\BackendUtility;
18use TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait;
19use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
20use TYPO3\CMS\Core\Localization\LanguageService;
21use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
22use TYPO3\CMS\Core\Utility\GeneralUtility;
23use TYPO3\CMS\Core\Utility\HttpUtility;
24use TYPO3\CMS\Fluid\View\StandaloneView;
25
26/**
27 * This class displays the Info/Modify screen of the Web > Template module
28 * @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
29 */
30class TypoScriptTemplateInformationModuleFunctionController
31{
32    use PublicPropertyDeprecationTrait;
33    use PublicMethodDeprecationTrait;
34
35    /**
36     * @var array
37     */
38    private $deprecatedPublicProperties = [
39        'pObj' => 'Using TypoScriptTemplateInformationModuleFunctionController::$pObj is deprecated and will not be possible anymore in TYPO3 v10.0.',
40        'function_key' => 'Using TypoScriptTemplateInformationModuleFunctionController::$function_key is deprecated, property will be removed in TYPO3 v10.0.',
41        'extClassConf' => 'Using TypoScriptTemplateInformationModuleFunctionController::$extClassConf is deprecated, property will be removed in TYPO3 v10.0.',
42        'localLangFile' => 'Using TypoScriptTemplateInformationModuleFunctionController::$localLangFile is deprecated, property will be removed in TYPO3 v10.0.',
43        'tce_processed' => 'Using TypoScriptTemplateInformationModuleFunctionController::$tce_processed is deprecated, property will be removed in TYPO3 v10.0.',
44    ];
45
46    /**
47     * @var array
48     */
49    private $deprecatedPublicMethods = [
50        'initialize_editor' => 'Using TypoScriptTemplateInformationModuleFunctionController::initialize_editor() is deprecated and will not be possible anymore in TYPO3 v10.0.',
51        'tableRowData' => 'Using TypoScriptTemplateInformationModuleFunctionController::tableRowData() is deprecated and will not be possible anymore in TYPO3 v10.0.',
52        'handleExternalFunctionValue' => 'Using TypoScriptTemplateInformationModuleFunctionController::handleExternalFunctionValue() is deprecated, method will be removed in TYPO3 v10.0.',
53    ];
54
55    /**
56     * Indicator for t3editor, whether data is stored
57     *
58     * @var bool
59     * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0.
60     */
61    protected $tce_processed = false;
62
63    /**
64     * @var TypoScriptTemplateModuleController
65     */
66    protected $pObj;
67
68    /**
69     * The currently selected sys_template record
70     * @var array
71     */
72    protected $templateRow;
73
74    /**
75     * @var ExtendedTemplateService
76     */
77    protected $templateService;
78
79    /**
80     * @var int GET/POST var 'id'
81     */
82    protected $id;
83
84    /**
85     * Can be hardcoded to the name of a locallang.xlf file (from the same directory as the class file) to use/load
86     * and is included / added to $GLOBALS['LOCAL_LANG']
87     *
88     * @see init()
89     * @var string
90     * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0.
91     */
92    protected $localLangFile = '';
93
94    /**
95     * Contains module configuration parts from TBE_MODULES_EXT if found
96     *
97     * @see handleExternalFunctionValue()
98     * @var array
99     * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0.
100     */
101    protected $extClassConf;
102
103    /**
104     * If this value is set it points to a key in the TBE_MODULES_EXT array (not on the top level..) where another classname/filepath/title can be defined for sub-subfunctions.
105     * This is a little hard to explain, so see it in action; it used in the extension 'func_wizards' in order to provide yet a layer of interfacing with the backend module.
106     * The extension 'func_wizards' has this description: 'Adds the 'Wizards' item to the function menu in Web>Func. This is just a framework for wizard extensions.' - so as you can see it is designed to allow further connectivity - 'level 2'
107     *
108     * @var string
109     * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0.
110     */
111    protected $function_key = '';
112
113    /**
114     * Init, called from parent object
115     *
116     * @param TypoScriptTemplateModuleController $pObj A reference to the parent (calling) object
117     */
118    public function init($pObj)
119    {
120        $this->pObj = $pObj;
121        // Local lang:
122        if (!empty($this->localLangFile)) {
123            // @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0.
124            $this->getLanguageService()->includeLLFile($this->localLangFile);
125        }
126        $this->id = (int)GeneralUtility::_GP('id');
127    }
128
129    /**
130     * Gets the data for a row of a HTML table in the fluid template
131     *
132     * @param string $label The label to be shown (e.g. 'Title:', 'Sitetitle:')
133     * @param string $data The data/information to be shown (e.g. 'Template for my site')
134     * @param string $field The field/variable to be sent on clicking the edit icon (e.g. 'title', 'sitetitle')
135     * @param int $id The field/variable to be sent on clicking the edit icon (e.g. 'title', 'sitetitle')
136     * @return array Data for a row of a HTML table
137     */
138    protected function tableRowData($label, $data, $field, $id)
139    {
140        $urlParameters = [
141            'id' => $this->id,
142            'edit' => [
143                'sys_template' => [
144                    $id => 'edit'
145                ]
146            ],
147            'columnsOnly' => $field,
148            'createExtension' => 0,
149            'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
150        ];
151        /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
152        $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
153        $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
154
155        return [
156            'url' => $url,
157            'data' => $data,
158            'label' => $label
159        ];
160    }
161
162    /**
163     * Create an instance of \TYPO3\CMS\Core\TypoScript\ExtendedTemplateService
164     * and looks for the first (visible) template
165     * record. If $template_uid was given and greater than zero, this record will be checked.
166     *
167     * Initializes the module. Done in this function because we may need to re-initialize if data is submitted!
168     *
169     * @param int $pageId The uid of the current page
170     * @param int $template_uid The uid of the template record to be rendered (only if more than one template on the current page)
171     * @return bool Returns TRUE if a template record was found, otherwise FALSE
172     */
173    protected function initialize_editor($pageId, $template_uid = 0)
174    {
175        $this->templateService = GeneralUtility::makeInstance(ExtendedTemplateService::class);
176
177        // Get the row of the first VISIBLE template of the page. where clause like the frontend.
178        $this->templateRow = $this->templateService->ext_getFirstTemplate($pageId, $template_uid);
179        if (is_array($this->templateRow)) {
180            return true;
181        }
182        return false;
183    }
184
185    /**
186     * Main, called from parent object
187     *
188     * @return string Information of the template status or the taken actions as HTML string
189     */
190    public function main()
191    {
192        // Checking for more than one template an if, set a menu...
193        $manyTemplatesMenu = $this->pObj->templateMenu();
194        $template_uid = 0;
195        if ($manyTemplatesMenu) {
196            $template_uid = $this->pObj->MOD_SETTINGS['templatesOnPage'];
197        }
198        // Initialize
199        $existTemplate = $this->initialize_editor($this->id, $template_uid);
200        $saveId = 0;
201        if ($existTemplate) {
202            $saveId = $this->templateRow['_ORIG_uid'] ?: $this->templateRow['uid'];
203        }
204        /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
205        $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
206        // Create extension template
207        $newId = $this->pObj->createTemplate($this->id, $saveId);
208        if ($newId) {
209            // Switch to new template
210            $urlParameters = [
211                'id' => $this->id,
212                'SET[templatesOnPage]' => $newId
213            ];
214            $url = (string)$uriBuilder->buildUriFromRoute('web_ts', $urlParameters);
215            HttpUtility::redirect($url);
216        }
217        $tce = null;
218        if ($existTemplate) {
219            $lang = $this->getLanguageService();
220            $lang->includeLLFile('EXT:tstemplate/Resources/Private/Language/locallang_info.xlf');
221            $assigns = [];
222            $assigns['LLPrefix'] = 'LLL:EXT:tstemplate/Resources/Private/Language/locallang_info.xlf:';
223
224            $assigns['title'] = trim($this->templateRow['title']);
225            $assigns['siteTitle'] = trim($this->templateRow['sitetitle']);
226            $assigns['templateRecord'] = $this->templateRow;
227            if ($manyTemplatesMenu) {
228                $assigns['manyTemplatesMenu'] = $manyTemplatesMenu;
229            }
230
231            // Processing:
232            $tableRows = [];
233            $tableRows[] = $this->tableRowData($lang->getLL('title'), $this->templateRow['title'], 'title', $this->templateRow['uid']);
234            $tableRows[] = $this->tableRowData($lang->getLL('sitetitle'), $this->templateRow['sitetitle'], 'sitetitle', $this->templateRow['uid']);
235            $tableRows[] = $this->tableRowData($lang->getLL('description'), $this->templateRow['description'], 'description', $this->templateRow['uid']);
236            $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']);
237            $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']);
238            $assigns['tableRows'] = $tableRows;
239
240            // Edit all icon:
241            $urlParameters = [
242                'edit' => [
243                    'sys_template' => [
244                        $this->templateRow['uid'] => 'edit'
245                    ]
246                ],
247                'createExtension' => 0,
248                'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
249            ];
250            $assigns['editAllUrl'] = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
251
252            // Rendering of the output via fluid
253            $view = GeneralUtility::makeInstance(StandaloneView::class);
254            $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
255                'EXT:tstemplate/Resources/Private/Templates/InformationModule.html'
256            ));
257            $view->assignMultiple($assigns);
258            $theOutput = $view->render();
259        } else {
260            $theOutput = $this->pObj->noTemplate(1);
261        }
262        return $theOutput;
263    }
264
265    /**
266     * If $this->function_key is set (which means there are two levels of object connectivity) then
267     * $this->extClassConf is loaded with the TBE_MODULES_EXT configuration for that sub-sub-module
268     *
269     * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.0.
270     */
271    protected function handleExternalFunctionValue()
272    {
273        // Must clean first to make sure the correct key is set...
274        $this->pObj->MOD_SETTINGS = BackendUtility::getModuleData($this->pObj->MOD_MENU, GeneralUtility::_GP('SET'), 'web_ts');
275        if ($this->function_key) {
276            $this->extClassConf = $this->pObj->getExternalItemConfig('web_ts', $this->function_key, $this->pObj->MOD_SETTINGS[$this->function_key]);
277        }
278    }
279
280    /**
281     * @return LanguageService
282     */
283    protected function getLanguageService(): LanguageService
284    {
285        return $GLOBALS['LANG'];
286    }
287}
288