1e54dec6cfc7884730fe3d2f33086f7d3c384137
[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\Imaging\Icon;
21 use TYPO3\CMS\Core\Imaging\IconFactory;
22 use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
23 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
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 class TypoScriptTemplateConstantEditorModuleFunctionController extends AbstractFunctionModule
37 {
38 /**
39 * @var TypoScriptTemplateModuleController
40 */
41 public $pObj;
42
43 /**
44 * Initialize editor
45 *
46 * Initializes the module.
47 * Done in this function because we may need to re-initialize if data is submitted!
48 *
49 * @param int $pageId
50 * @param int $template_uid
51 * @return bool
52 */
53 public function initialize_editor($pageId, $template_uid = 0)
54 {
55 $templateService = GeneralUtility::makeInstance(ExtendedTemplateService::class);
56 $GLOBALS['tmpl'] = $templateService;
57
58 // Do not log time-performance information
59 $templateService->tt_track = false;
60
61 $templateService->init();
62 $templateService->ext_localGfxPrefix = ExtensionManagementUtility::extPath('tstemplate');
63 $templateService->ext_localWebGfxPrefix = ExtensionManagementUtility::extRelPath('tstemplate') . 'Resources/Public/';
64
65 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
66 $GLOBALS['tplRow'] = $templateService->ext_getFirstTemplate($pageId, $template_uid);
67 // IF there was a template...
68 if (is_array($GLOBALS['tplRow'])) {
69 // Gets the rootLine
70 $sys_page = GeneralUtility::makeInstance(PageRepository::class);
71 $rootLine = $sys_page->getRootLine($pageId);
72 // This generates the constants/config + hierarchy info for the template.
73 $templateService->runThroughTemplates($rootLine, $template_uid);
74 // The editable constants are returned in an array.
75 $GLOBALS['theConstants'] = $templateService->generateConfig_constants();
76 // The returned constants are sorted in categories, that goes into the $tmpl->categories array
77 $templateService->ext_categorizeEditableConstants($GLOBALS['theConstants']);
78 // This array will contain key=[expanded constant name], value=line number in template. (after edit_divider, if any)
79 $templateService->ext_regObjectPositions($GLOBALS['tplRow']['constants']);
80 return true;
81 }
82 return false;
83 }
84
85 /**
86 * Display example
87 *
88 * @param string $theOutput
89 * @return string
90 */
91 public function displayExample($theOutput)
92 {
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 {
108 $lang = $this->getLanguageService();
109
110 $lang->includeLLFile('EXT:tstemplate/Resources/Private/Language/locallang_ceditor.xlf');
111 $theOutput = '';
112 // Create extension template
113 $this->pObj->createTemplate($this->pObj->id);
114 // Checking for more than one template an if, set a menu...
115 $manyTemplatesMenu = $this->pObj->templateMenu();
116 $template_uid = 0;
117 if ($manyTemplatesMenu) {
118 $template_uid = $this->pObj->MOD_SETTINGS['templatesOnPage'];
119 }
120
121 // initialize
122 $existTemplate = $this->initialize_editor($this->pObj->id, $template_uid);
123 if ($existTemplate) {
124 $templateService = $this->getExtendedTemplateService();
125 $tplRow = $this->getTemplateRow();
126 $theConstants = $this->getConstants();
127
128 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Tstemplate/ConstantEditor');
129 $saveId = $tplRow['_ORIG_uid'] ? $tplRow['_ORIG_uid'] : $tplRow['uid'];
130 // Update template ?
131 if (GeneralUtility::_POST('_savedok')) {
132 $templateService->changed = 0;
133 $templateService->ext_procesInput(GeneralUtility::_POST(), array(), $theConstants, $tplRow);
134 if ($templateService->changed) {
135 // Set the data to be saved
136 $recData = array();
137 $recData['sys_template'][$saveId]['constants'] = implode($templateService->raw, LF);
138 // Create new tce-object
139 $tce = GeneralUtility::makeInstance(DataHandler::class);
140 $tce->stripslashes_values = false;
141 $tce->start($recData, array());
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 $this->initialize_editor($this->pObj->id, $template_uid);
147 // re-read the constants as they have changed
148 $templateService = $this->getExtendedTemplateService();
149 $tplRow = $this->getTemplateRow();
150 $theConstants = $this->getConstants();
151 }
152 }
153 // 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.
154 $this->pObj->MOD_MENU['constant_editor_cat'] = $templateService->ext_getCategoryLabelArray();
155 $this->pObj->MOD_SETTINGS = BackendUtility::getModuleData($this->pObj->MOD_MENU, GeneralUtility::_GP('SET'), $this->pObj->MCONF['name']);
156 // Resetting the menu (stop)
157 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
158 $content = $iconFactory->getIconForRecord('sys_template', $tplRow, Icon::SIZE_SMALL)->render() . '<strong>' . $this->pObj->linkWrapTemplateTitle($tplRow['title'], 'constants') . '</strong>' . (trim($tplRow['sitetitle']) ? htmlspecialchars(' (' . $tplRow['sitetitle'] . ')') : '');
159 $theOutput .= $this->pObj->doc->section($lang->getLL('editConstants', true), $content, false, true);
160 if ($manyTemplatesMenu) {
161 $theOutput .= $this->pObj->doc->section('', $manyTemplatesMenu);
162 }
163 $theOutput .= $this->pObj->doc->spacer(10);
164 if (!empty($this->pObj->MOD_MENU['constant_editor_cat'])) {
165 $menu = '<div class="form-inline form-inline-spaced">';
166 $menu .= BackendUtility::getDropdownMenu($this->pObj->id, 'SET[constant_editor_cat]', $this->pObj->MOD_SETTINGS['constant_editor_cat'], $this->pObj->MOD_MENU['constant_editor_cat']);
167 $menu .= '</div>';
168 $theOutput .= $this->pObj->doc->section($lang->getLL('category', true), '<span class="text-nowrap">' . $menu . '</span>', false);
169 } else {
170 $theOutput .= $this->pObj->doc->section($lang->getLL('noConstants', true), $lang->getLL('noConstantsDescription', true), false, false, 1);
171 }
172 $theOutput .= $this->pObj->doc->spacer(15);
173 // Category and constant editor config:
174 $category = $this->pObj->MOD_SETTINGS['constant_editor_cat'];
175 $templateService->ext_getTSCE_config($category);
176
177 $printFields = trim($templateService->ext_printFields($theConstants, $category));
178 if ($printFields) {
179 $theOutput .= $this->pObj->doc->section('', $printFields);
180 }
181 $BE_USER_modOptions = BackendUtility::getModTSconfig(0, 'mod.' . $this->pObj->MCONF['name']);
182 if ($BE_USER_modOptions['properties']['constantEditor.']['example'] != 'top') {
183 $theOutput = $this->displayExample($theOutput);
184 }
185 } else {
186 $theOutput .= $this->pObj->noTemplate(1);
187 }
188 return $theOutput;
189 }
190
191 /**
192 * @return ExtendedTemplateService
193 */
194 protected function getExtendedTemplateService()
195 {
196 return $GLOBALS['tmpl'];
197 }
198
199 /**
200 * @return array
201 */
202 protected function getTemplateRow()
203 {
204 return $GLOBALS['tplRow'];
205 }
206
207 /**
208 * @return array
209 */
210 protected function getConstants()
211 {
212 return $GLOBALS['theConstants'];
213 }
214 }