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