[!!!][TASK] Remove deprecated code from EXT:tstemplate
[Packages/TYPO3.CMS.git] / typo3 / sysext / tstemplate / Classes / Controller / TypoScriptTemplateConstantEditorModuleFunctionController.php
1 <?php
2 namespace 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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\DataHandling\DataHandler;
19 use TYPO3\CMS\Core\Localization\LanguageService;
20 use TYPO3\CMS\Core\Page\PageRenderer;
21 use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\RootlineUtility;
24 use TYPO3\CMS\Fluid\View\StandaloneView;
25
26 /**
27 * TypoScript Constant editor
28 * @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
29 */
30 class TypoScriptTemplateConstantEditorModuleFunctionController
31 {
32
33 /**
34 * @var TypoScriptTemplateModuleController
35 */
36 protected $pObj;
37
38 /**
39 * The currently selected sys_template record
40 * @var array
41 */
42 protected $templateRow;
43
44 /**
45 * @var ExtendedTemplateService
46 */
47 protected $templateService;
48
49 /**
50 * @var array
51 */
52 protected $constants;
53
54 /**
55 * @var int GET/POST var 'id'
56 */
57 protected $id;
58
59 /**
60 * Init, called from parent object
61 *
62 * @param TypoScriptTemplateModuleController $pObj A reference to the parent (calling) object
63 */
64 public function init($pObj)
65 {
66 $this->pObj = $pObj;
67 $this->id = (int)GeneralUtility::_GP('id');
68 }
69
70 /**
71 * Initialize editor
72 *
73 * Initializes the module.
74 * Done in this function because we may need to re-initialize if data is submitted!
75 *
76 * @param int $pageId
77 * @param int $template_uid
78 * @return bool
79 */
80 protected function initialize_editor($pageId, $template_uid = 0)
81 {
82 $this->templateService = GeneralUtility::makeInstance(ExtendedTemplateService::class);
83
84 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
85 $this->templateRow = $this->templateService->ext_getFirstTemplate($pageId, $template_uid);
86 // IF there was a template...
87 if (is_array($this->templateRow)) {
88 // Gets the rootLine
89 $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId);
90 $rootLine = $rootlineUtility->get();
91 // This generates the constants/config + hierarchy info for the template.
92 $this->templateService->runThroughTemplates($rootLine, $template_uid);
93 // The editable constants are returned in an array.
94 $this->constants = $this->templateService->generateConfig_constants();
95 // The returned constants are sorted in categories, that goes into the $tmpl->categories array
96 $this->templateService->ext_categorizeEditableConstants($this->constants);
97 // This array will contain key=[expanded constant name], value=line number in template.
98 $this->templateService->ext_regObjectPositions($this->templateRow['constants']);
99 return true;
100 }
101 return false;
102 }
103
104 /**
105 * Main, called from parent object
106 *
107 * @return string
108 */
109 public function main()
110 {
111 $assigns = [];
112 $assigns['LLPrefix'] = 'LLL:EXT:tstemplate/Resources/Private/Language/locallang_ceditor.xlf:';
113 // Create extension template
114 $this->pObj->createTemplate($this->id);
115 // Checking for more than one template an if, set a menu...
116 $manyTemplatesMenu = $this->pObj->templateMenu();
117 $template_uid = 0;
118 if ($manyTemplatesMenu) {
119 $template_uid = $this->pObj->MOD_SETTINGS['templatesOnPage'];
120 }
121
122 // initialize
123 $existTemplate = $this->initialize_editor($this->id, $template_uid);
124 if ($existTemplate) {
125 $assigns['siteTitle'] = trim($this->templateRow['sitetitle']);
126 $assigns['templateRecord'] = $this->templateRow;
127 if ($manyTemplatesMenu) {
128 $assigns['manyTemplatesMenu'] = $manyTemplatesMenu;
129 }
130
131 $saveId = $this->templateRow['_ORIG_uid'] ?: $this->templateRow['uid'];
132 // Update template ?
133 if (GeneralUtility::_POST('_savedok')) {
134 $this->templateService->changed = 0;
135 $this->templateService->ext_procesInput(GeneralUtility::_POST(), [], $this->constants, $this->templateRow);
136 if ($this->templateService->changed) {
137 // Set the data to be saved
138 $recData = [];
139 $recData['sys_template'][$saveId]['constants'] = implode($this->templateService->raw, LF);
140 // Create new tce-object
141 $tce = GeneralUtility::makeInstance(DataHandler::class);
142 $tce->start($recData, []);
143 $tce->process_datamap();
144 // Clear the cache (note: currently only admin-users can clear the cache in tce_main.php)
145 $tce->clear_cacheCmd('all');
146 // re-read the template ...
147 // re-read the constants as they have changed
148 $this->initialize_editor($this->id, $template_uid);
149 }
150 }
151 // Resetting the menu (start). I wonder if this in any way is a violation of the menu-system. Haven't checked. But need to do it here, because the menu is dependent on the categories available.
152 $this->pObj->MOD_MENU['constant_editor_cat'] = $this->templateService->ext_getCategoryLabelArray();
153 $this->pObj->MOD_SETTINGS = BackendUtility::getModuleData($this->pObj->MOD_MENU, GeneralUtility::_GP('SET'), 'web_ts');
154 // Resetting the menu (stop)
155 $assigns['title'] = $this->pObj->linkWrapTemplateTitle($this->templateRow['title'], 'constants');
156 if (!empty($this->pObj->MOD_MENU['constant_editor_cat'])) {
157 $assigns['constantsMenu'] = BackendUtility::getDropdownMenu($this->id, 'SET[constant_editor_cat]', $this->pObj->MOD_SETTINGS['constant_editor_cat'], $this->pObj->MOD_MENU['constant_editor_cat']);
158 }
159 // Category and constant editor config:
160 $category = $this->pObj->MOD_SETTINGS['constant_editor_cat'];
161
162 $printFields = trim($this->templateService->ext_printFields($this->constants, $category));
163 foreach ($this->templateService->getInlineJavaScript() as $name => $inlineJavaScript) {
164 $this->getPageRenderer()->addJsInlineCode($name, $inlineJavaScript);
165 }
166
167 if ($printFields) {
168 $assigns['printFields'] = $printFields;
169 }
170 // Rendering of the output via fluid
171 $view = GeneralUtility::makeInstance(StandaloneView::class);
172 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
173 'EXT:tstemplate/Resources/Private/Templates/ConstantEditor.html'
174 ));
175 $view->assignMultiple($assigns);
176 $theOutput = $view->render();
177 } else {
178 $theOutput = $this->pObj->noTemplate(1);
179 }
180 return $theOutput;
181 }
182
183 /**
184 * @return LanguageService
185 */
186 protected function getLanguageService(): LanguageService
187 {
188 return $GLOBALS['LANG'];
189 }
190
191 /**
192 * @return PageRenderer
193 */
194 protected function getPageRenderer(): PageRenderer
195 {
196 return GeneralUtility::makeInstance(PageRenderer::class);
197 }
198 }