50be5face099e9a1ad242b227774316223704df2
[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\Backend\Utility\IconUtility;
20 use TYPO3\CMS\Core\DataHandling\DataHandler;
21 use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
22 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\MathUtility;
25 use TYPO3\CMS\Frontend\Page\PageRepository;
26
27 /**
28 * TypoScript Constant editor
29 *
30 * Module Include-file
31 *
32 * $GLOBALS['TYPO3_CONF_VARS']['MODS']['web_ts']['onlineResourceDir'] = 'fileadmin/fonts/';
33 * // This is the path (must be in "fileadmin/" !!) where the web_ts/constant-editor submodule fetches online resources.
34 * Put fonts (ttf) and standard images here!
35 *
36 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
37 */
38 class TypoScriptTemplateConstantEditorModuleFunctionController extends AbstractFunctionModule {
39
40 /**
41 * @var TypoScriptTemplateModuleController
42 */
43 public $pObj;
44
45 /**
46 * Initialize editor
47 *
48 * Initializes the module.
49 * Done in this function because we may need to re-initialize if data is submitted!
50 *
51 * @param int $pageId
52 * @param int $template_uid
53 * @return bool
54 */
55 public function initialize_editor($pageId, $template_uid = 0) {
56 $templateService = GeneralUtility::makeInstance(ExtendedTemplateService::class);
57 $GLOBALS['tmpl'] = $templateService;
58
59 // Do not log time-performance information
60 $templateService->tt_track = FALSE;
61
62 $templateService->init();
63 $templateService->ext_localGfxPrefix = ExtensionManagementUtility::extPath('tstemplate');
64 $templateService->ext_localWebGfxPrefix = $GLOBALS['BACK_PATH'] . ExtensionManagementUtility::extRelPath('tstemplate') . 'Resources/Public/';
65
66 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
67 $GLOBALS['tplRow'] = $templateService->ext_getFirstTemplate($pageId, $template_uid);
68 // IF there was a template...
69 if (is_array($GLOBALS['tplRow'])) {
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 $templateService->runThroughTemplates($rootLine, $template_uid);
75 // The editable constants are returned in an array.
76 $GLOBALS['theConstants'] = $templateService->generateConfig_constants();
77 // The returned constants are sorted in categories, that goes into the $tmpl->categories array
78 $templateService->ext_categorizeEditableConstants($GLOBALS['theConstants']);
79 // This array will contain key=[expanded constant name], value=line number in template. (after edit_divider, if any)
80 $templateService->ext_regObjectPositions($GLOBALS['tplRow']['constants']);
81 return TRUE;
82 }
83 return FALSE;
84 }
85
86 /**
87 * Display example
88 *
89 * @param string $theOutput
90 * @return string
91 */
92 public function displayExample($theOutput) {
93 $templateService = $this->getExtendedTemplateService();
94 if ($templateService->helpConfig['imagetag'] || $templateService->helpConfig['description'] || $templateService->helpConfig['header']) {
95 $theOutput .= $this->pObj->doc->spacer(30);
96 $theOutput .= $this->pObj->doc->section($templateService->helpConfig['header'], '<div align="center">' . $templateService->helpConfig['imagetag'] . '</div><BR>' . ($templateService->helpConfig['description'] ? implode(explode('//', $templateService->helpConfig['description']), '<BR>') . '<BR>' : '') . ($templateService->helpConfig['bulletlist'] ? '<ul><li>' . implode(explode('//', $templateService->helpConfig['bulletlist']), '<li>') . '</ul>' : '<BR>'));
97 }
98 return $theOutput;
99 }
100
101 /**
102 * Main
103 *
104 * @return string
105 */
106 public function main() {
107 $lang = $this->getLanguageService();
108
109 $lang->includeLLFile('EXT:tstemplate/Resources/Private/Language/locallang_ceditor.xlf');
110 $theOutput = '';
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 $templateService = $this->getExtendedTemplateService();
124 $tplRow = $this->getTemplateRow();
125 $theConstants = $this->getConstants();
126
127 $this->pObj->doc->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Tstemplate/ConstantEditor');
128 $saveId = $tplRow['_ORIG_uid'] ? $tplRow['_ORIG_uid'] : $tplRow['uid'];
129 // Update template ?
130 if (GeneralUtility::_POST('submit') || MathUtility::canBeInterpretedAsInteger(GeneralUtility::_POST('submit_x')) && MathUtility::canBeInterpretedAsInteger(GeneralUtility::_POST('submit_y'))) {
131 $templateService->changed = 0;
132 $templateService->ext_procesInput(GeneralUtility::_POST(), array(), $theConstants, $tplRow);
133 if ($templateService->changed) {
134 // Set the data to be saved
135 $recData = array();
136 $recData['sys_template'][$saveId]['constants'] = implode($templateService->raw, LF);
137 // Create new tce-object
138 $tce = GeneralUtility::makeInstance(DataHandler::class);
139 $tce->stripslashes_values = FALSE;
140 $tce->start($recData, array());
141 $tce->process_datamap();
142 // Clear the cache (note: currently only admin-users can clear the cache in tce_main.php)
143 $tce->clear_cacheCmd('all');
144 // re-read the template ...
145 $this->initialize_editor($this->pObj->id, $template_uid);
146 // re-read the constants as they have changed
147 $theConstants = $this->getConstants();
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'] = $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 $content = IconUtility::getSpriteIconForRecord('sys_template', $tplRow) . '<strong>' . $this->pObj->linkWrapTemplateTitle($tplRow['title'], 'constants') . '</strong>' . htmlspecialchars((trim($tplRow['sitetitle']) ? ' (' . $tplRow['sitetitle'] . ')' : ''));
155 $theOutput .= $this->pObj->doc->section($lang->getLL('editConstants', TRUE), $content, FALSE, TRUE);
156 if ($manyTemplatesMenu) {
157 $theOutput .= $this->pObj->doc->section('', $manyTemplatesMenu);
158 }
159 $theOutput .= $this->pObj->doc->spacer(10);
160 if (!empty($this->pObj->MOD_MENU['constant_editor_cat'])) {
161 $menu = BackendUtility::getFuncMenu($this->pObj->id, 'SET[constant_editor_cat]', $this->pObj->MOD_SETTINGS['constant_editor_cat'], $this->pObj->MOD_MENU['constant_editor_cat']);
162 $theOutput .= $this->pObj->doc->section($lang->getLL('category', TRUE), '<span class="text-nowrap">' . $menu . '</span>', FALSE);
163 } else {
164 $theOutput .= $this->pObj->doc->section($lang->getLL('noConstants', TRUE), $lang->getLL('noConstantsDescription', TRUE), FALSE, FALSE, 1);
165 }
166 $theOutput .= $this->pObj->doc->spacer(15);
167 // Category and constant editor config:
168 $category = $this->pObj->MOD_SETTINGS['constant_editor_cat'];
169 $templateService->ext_getTSCE_config($category);
170
171 $printFields = trim($templateService->ext_printFields($theConstants, $category));
172 if ($printFields) {
173 $theOutput .= $this->pObj->doc->section('', $printFields);
174 }
175 $BE_USER_modOptions = BackendUtility::getModTSconfig(0, 'mod.' . $this->pObj->MCONF['name']);
176 if ($BE_USER_modOptions['properties']['constantEditor.']['example'] != 'top') {
177 $theOutput = $this->displayExample($theOutput);
178 }
179 } else {
180 $theOutput .= $this->pObj->noTemplate(1);
181 }
182 return $theOutput;
183 }
184
185 /**
186 * @return ExtendedTemplateService
187 */
188 protected function getExtendedTemplateService() {
189 return $GLOBALS['tmpl'];
190 }
191
192 /**
193 * @return array
194 */
195 protected function getTemplateRow() {
196 return $GLOBALS['tplRow'];
197 }
198
199 /**
200 * @return array
201 */
202 protected function getConstants() {
203 return $GLOBALS['theConstants'];
204 }
205
206 }