558716040d0113adcb2192942d65c41f7417f42e
[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 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 $templateService = GeneralUtility::makeInstance(ExtendedTemplateService::class);
55 $GLOBALS['tmpl'] = $templateService;
56
57 // Do not log time-performance information
58 $templateService->tt_track = FALSE;
59
60 $templateService->init();
61 $templateService->ext_localGfxPrefix = ExtensionManagementUtility::extPath('tstemplate');
62 $templateService->ext_localWebGfxPrefix = ExtensionManagementUtility::extRelPath('tstemplate') . 'Resources/Public/';
63
64 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
65 $GLOBALS['tplRow'] = $templateService->ext_getFirstTemplate($pageId, $template_uid);
66 // IF there was a template...
67 if (is_array($GLOBALS['tplRow'])) {
68 // Gets the rootLine
69 $sys_page = GeneralUtility::makeInstance(PageRepository::class);
70 $rootLine = $sys_page->getRootLine($pageId);
71 // This generates the constants/config + hierarchy info for the template.
72 $templateService->runThroughTemplates($rootLine, $template_uid);
73 // The editable constants are returned in an array.
74 $GLOBALS['theConstants'] = $templateService->generateConfig_constants();
75 // The returned constants are sorted in categories, that goes into the $tmpl->categories array
76 $templateService->ext_categorizeEditableConstants($GLOBALS['theConstants']);
77 // This array will contain key=[expanded constant name], value=line number in template. (after edit_divider, if any)
78 $templateService->ext_regObjectPositions($GLOBALS['tplRow']['constants']);
79 return TRUE;
80 }
81 return FALSE;
82 }
83
84 /**
85 * Display example
86 *
87 * @param string $theOutput
88 * @return string
89 */
90 public function displayExample($theOutput) {
91 $templateService = $this->getExtendedTemplateService();
92 if ($templateService->helpConfig['imagetag'] || $templateService->helpConfig['description'] || $templateService->helpConfig['header']) {
93 $theOutput .= $this->pObj->doc->spacer(30);
94 $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>'));
95 }
96 return $theOutput;
97 }
98
99 /**
100 * Main
101 *
102 * @return string
103 */
104 public function main() {
105 $lang = $this->getLanguageService();
106
107 $lang->includeLLFile('EXT:tstemplate/Resources/Private/Language/locallang_ceditor.xlf');
108 $theOutput = '';
109 // Create extension template
110 $this->pObj->createTemplate($this->pObj->id);
111 // Checking for more than one template an if, set a menu...
112 $manyTemplatesMenu = $this->pObj->templateMenu();
113 $template_uid = 0;
114 if ($manyTemplatesMenu) {
115 $template_uid = $this->pObj->MOD_SETTINGS['templatesOnPage'];
116 }
117
118 // initialize
119 $existTemplate = $this->initialize_editor($this->pObj->id, $template_uid);
120 if ($existTemplate) {
121 $templateService = $this->getExtendedTemplateService();
122 $tplRow = $this->getTemplateRow();
123 $theConstants = $this->getConstants();
124
125 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Tstemplate/ConstantEditor');
126 $saveId = $tplRow['_ORIG_uid'] ? $tplRow['_ORIG_uid'] : $tplRow['uid'];
127 // Update template ?
128 if (GeneralUtility::_POST('submit') || MathUtility::canBeInterpretedAsInteger(GeneralUtility::_POST('submit_x')) && MathUtility::canBeInterpretedAsInteger(GeneralUtility::_POST('submit_y'))) {
129 $templateService->changed = 0;
130 $templateService->ext_procesInput(GeneralUtility::_POST(), array(), $theConstants, $tplRow);
131 if ($templateService->changed) {
132 // Set the data to be saved
133 $recData = array();
134 $recData['sys_template'][$saveId]['constants'] = implode($templateService->raw, LF);
135 // Create new tce-object
136 $tce = GeneralUtility::makeInstance(DataHandler::class);
137 $tce->stripslashes_values = FALSE;
138 $tce->start($recData, array());
139 $tce->process_datamap();
140 // Clear the cache (note: currently only admin-users can clear the cache in tce_main.php)
141 $tce->clear_cacheCmd('all');
142 // re-read the template ...
143 $this->initialize_editor($this->pObj->id, $template_uid);
144 // re-read the constants as they have changed
145 $theConstants = $this->getConstants();
146 }
147 }
148 // 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.
149 $this->pObj->MOD_MENU['constant_editor_cat'] = $templateService->ext_getCategoryLabelArray();
150 $this->pObj->MOD_SETTINGS = BackendUtility::getModuleData($this->pObj->MOD_MENU, GeneralUtility::_GP('SET'), $this->pObj->MCONF['name']);
151 // Resetting the menu (stop)
152 $content = IconUtility::getSpriteIconForRecord('sys_template', $tplRow) . '<strong>' . $this->pObj->linkWrapTemplateTitle($tplRow['title'], 'constants') . '</strong>' . htmlspecialchars((trim($tplRow['sitetitle']) ? ' (' . $tplRow['sitetitle'] . ')' : ''));
153 $theOutput .= $this->pObj->doc->section($lang->getLL('editConstants', TRUE), $content, FALSE, TRUE);
154 if ($manyTemplatesMenu) {
155 $theOutput .= $this->pObj->doc->section('', $manyTemplatesMenu);
156 }
157 $theOutput .= $this->pObj->doc->spacer(10);
158 if (!empty($this->pObj->MOD_MENU['constant_editor_cat'])) {
159 $menu = '<div class="form-inline form-inline-spaced">';
160 $menu .= BackendUtility::getDropdownMenu($this->pObj->id, 'SET[constant_editor_cat]', $this->pObj->MOD_SETTINGS['constant_editor_cat'], $this->pObj->MOD_MENU['constant_editor_cat']);
161 $menu .= '</div>';
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 }