[TASK] Fluidification of Info PageTSConfig
[Packages/TYPO3.CMS.git] / typo3 / sysext / info_pagetsconfig / Classes / Controller / InfoPageTyposcriptConfigController.php
1 <?php
2 namespace TYPO3\CMS\InfoPagetsconfig\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\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\MathUtility;
22 use TYPO3\CMS\Fluid\View\StandaloneView;
23
24 /**
25 * Page TSconfig viewer
26 */
27 class InfoPageTyposcriptConfigController extends \TYPO3\CMS\Backend\Module\AbstractFunctionModule
28 {
29 /**
30 * @var IconFactory
31 */
32 protected $iconFactory;
33
34 /**
35 * @var StandaloneView
36 */
37 protected $view;
38
39 /**
40 * Constructor
41 */
42 public function __construct()
43 {
44 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
45 $this->getLanguageService()->includeLLFile('EXT:info_pagetsconfig/Resources/Private/Language/locallang.xlf');
46
47 $this->view = $this->getFluidTemplateObject();
48 }
49
50 /**
51 * Function menu initialization
52 *
53 * @return array Menu array
54 */
55 public function modMenu()
56 {
57 $lang = $this->getLanguageService();
58 $modMenuAdd = array(
59 'tsconf_parts' => array(
60 0 => $lang->getLL('tsconf_parts_0'),
61 1 => $lang->getLL('tsconf_parts_1'),
62 '1a' => $lang->getLL('tsconf_parts_1a'),
63 '1b' => $lang->getLL('tsconf_parts_1b'),
64 '1c' => $lang->getLL('tsconf_parts_1c'),
65 '1d' => $lang->getLL('tsconf_parts_1d'),
66 '1e' => $lang->getLL('tsconf_parts_1e'),
67 '1f' => $lang->getLL('tsconf_parts_1f'),
68 '1g' => $lang->getLL('tsconf_parts_1g'),
69 2 => 'RTE.',
70 5 => 'TCEFORM.',
71 6 => 'TCEMAIN.',
72 3 => 'TSFE.',
73 4 => 'user.',
74 99 => $lang->getLL('tsconf_configFields')
75 ),
76 'tsconf_alphaSort' => '1'
77 );
78 if (!$this->getBackendUser()->isAdmin()) {
79 unset($modMenuAdd['tsconf_parts'][99]);
80 }
81 return $modMenuAdd;
82 }
83
84 /**
85 * Main function of class
86 *
87 * @return string HTML output
88 */
89 public function main()
90 {
91 $pageId = (int)(GeneralUtility::_GP('id'));
92
93 if ($pageId === 0) {
94 $this->view->assign('pageZero', 1);
95 $this->view->assign('overviewOfPagesUsingTSConfig', $this->getOverviewOfPagesUsingTSConfig());
96 } else {
97 if ($this->pObj->MOD_SETTINGS['tsconf_parts'] == 99) {
98 $TSparts = BackendUtility::getPagesTSconfig($this->pObj->id, null, true);
99 $lines = array();
100 $pUids = array();
101
102 foreach ($TSparts as $k => $v) {
103 if ($k != 'uid_0') {
104 $line = array();
105 if ($k == 'defaultPageTSconfig') {
106 $line['defaultPageTSconfig'] = 1;
107 } else {
108 $pUids[] = substr($k, 4);
109 $row = BackendUtility::getRecordWSOL('pages', substr($k, 4));
110
111 $icon = $this->iconFactory->getIconForRecord('pages', $row, Icon::SIZE_SMALL);
112 $editIdList = substr($k, 4);
113 $urlParameters = [
114 'edit' => [
115 'pages' => [
116 $editIdList => 'edit',
117 ]
118 ],
119 'columnsOnly' => 'TSconfig',
120 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
121 ];
122 $line['editIcon'] = BackendUtility::getModuleUrl('record_edit', $urlParameters);
123 $line['editTitle'] = 'editTSconfig';
124 $line['title'] = BackendUtility::wrapClickMenuOnIcon($icon, 'pages', $row['uid'])
125 . ' ' . htmlspecialchars(BackendUtility::getRecordTitle('pages', $row));
126 }
127 $tsparser = GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::class);
128 $tsparser->lineNumberOffset = 0;
129 $line['content'] = $tsparser->doSyntaxHighlight(trim($v) . LF);
130 $lines[] = $line;
131 }
132 }
133
134 if (!empty($pUids)) {
135 $urlParameters = [
136 'edit' => [
137 'pages' => [
138 implode(',', $pUids) => 'edit',
139 ]
140 ],
141 'columnsOnly' => 'TSconfig',
142 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
143 ];
144 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
145 $editIcon = htmlspecialchars($url);
146 $editTitle = 'editTSconfig_all';
147
148 } else {
149 $editIcon = '';
150 $editTitle = '';
151 }
152
153 $this->view->assign('tsconfParts99', 1);
154 $this->view->assign('csh', BackendUtility::cshItem('_MOD_web_info', 'tsconfig_edit', null, '|'));
155 $this->view->assign('lines', $lines);
156 $this->view->assign('editIcon', $editIcon);
157 $this->view->assign('editTitle', $editTitle);
158 } else {
159 $this->view->assign('tsconfParts99', 0);
160 // Defined global here!
161 $tmpl = GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\ExtendedTemplateService::class);
162
163 // Do not log time-performance information
164 $tmpl->tt_track = 0;
165 $tmpl->fixedLgd = 0;
166 $tmpl->linkObjects = 0;
167 $tmpl->bType = '';
168 $tmpl->ext_expandAllNotes = 1;
169 $tmpl->ext_noPMicons = 1;
170
171 $beUser = $this->getBackendUser();
172 switch ($this->pObj->MOD_SETTINGS['tsconf_parts']) {
173 case '1':
174 $modTSconfig = BackendUtility::getModTSconfig($this->pObj->id, 'mod');
175 break;
176 case '1a':
177 $modTSconfig = $beUser->getTSConfig('mod.web_layout', BackendUtility::getPagesTSconfig($this->pObj->id));
178 break;
179 case '1b':
180 $modTSconfig = $beUser->getTSConfig('mod.web_view', BackendUtility::getPagesTSconfig($this->pObj->id));
181 break;
182 case '1c':
183 $modTSconfig = $beUser->getTSConfig('mod.web_modules', BackendUtility::getPagesTSconfig($this->pObj->id));
184 break;
185 case '1d':
186 $modTSconfig = $beUser->getTSConfig('mod.web_list', BackendUtility::getPagesTSconfig($this->pObj->id));
187 break;
188 case '1e':
189 $modTSconfig = $beUser->getTSConfig('mod.web_info', BackendUtility::getPagesTSconfig($this->pObj->id));
190 break;
191 case '1f':
192 $modTSconfig = $beUser->getTSConfig('mod.web_func', BackendUtility::getPagesTSconfig($this->pObj->id));
193 break;
194 case '1g':
195 $modTSconfig = $beUser->getTSConfig('mod.web_ts', BackendUtility::getPagesTSconfig($this->pObj->id));
196 break;
197 case '2':
198 $modTSconfig = $beUser->getTSConfig('RTE', BackendUtility::getPagesTSconfig($this->pObj->id));
199 break;
200 case '5':
201 $modTSconfig = $beUser->getTSConfig('TCEFORM', BackendUtility::getPagesTSconfig($this->pObj->id));
202 break;
203 case '6':
204 $modTSconfig = $beUser->getTSConfig('TCEMAIN', BackendUtility::getPagesTSconfig($this->pObj->id));
205 break;
206 case '3':
207 $modTSconfig = $beUser->getTSConfig('TSFE', BackendUtility::getPagesTSconfig($this->pObj->id));
208 break;
209 case '4':
210 $modTSconfig = $beUser->getTSConfig('user', BackendUtility::getPagesTSconfig($this->pObj->id));
211 break;
212 default:
213 $modTSconfig['properties'] = BackendUtility::getPagesTSconfig($this->pObj->id);
214 }
215
216 $modTSconfig = $modTSconfig['properties'];
217 if (!is_array($modTSconfig)) {
218 $modTSconfig = array();
219 }
220
221 $this->view->assign('csh', BackendUtility::cshItem('_MOD_web_info', 'tsconfig_hierarchy', null, '|'));
222 $this->view->assign('tree', $tmpl->ext_getObjTree($modTSconfig, '', '', '', '', $this->pObj->MOD_SETTINGS['tsconf_alphaSort']));
223 }
224 $this->view->assign('alphaSort', BackendUtility::getFuncCheck($this->pObj->id, 'SET[tsconf_alphaSort]', $this->pObj->MOD_SETTINGS['tsconf_alphaSort'], '', '', 'id="checkTsconf_alphaSort"'));
225 $this->view->assign('dropdownMenu', BackendUtility::getDropdownMenu($this->pObj->id, 'SET[tsconf_parts]', $this->pObj->MOD_SETTINGS['tsconf_parts'], $this->pObj->MOD_MENU['tsconf_parts']));
226 }
227 return $this->view->render();
228 }
229
230 /**
231 * Renders table rows of all pages containing TSConfig together with its rootline
232 *
233 * @return array
234 */
235 protected function getOverviewOfPagesUsingTSConfig()
236 {
237 $db = $this->getDatabaseConnection();
238 $res = $db->exec_SELECTquery(
239 'uid, TSconfig',
240 'pages',
241 'TSconfig != \'\''
242 . BackendUtility::deleteClause('pages')
243 . BackendUtility::versioningPlaceholderClause('pages'), 'pages.uid');
244 $pageArray = array();
245 while ($row = $db->sql_fetch_assoc($res)) {
246 $this->setInPageArray($pageArray, BackendUtility::BEgetRootLine($row['uid'], 'AND 1=1'), $row);
247 }
248 return $this->getList($pageArray);
249 }
250
251 /**
252 * Set page in array
253 * This function is called recursively and builds a multi-dimensional array that reflects the page
254 * hierarchy.
255 *
256 * @param array $hierarchicArray The hierarchic array (passed by reference)
257 * @param array $rootlineArray The rootline array
258 * @param array $row The row from the database containing the uid and TSConfig fields
259 * @return void
260 */
261 protected function setInPageArray(&$hierarchicArray, $rootlineArray, $row)
262 {
263 ksort($rootlineArray);
264 reset($rootlineArray);
265 if (!$rootlineArray[0]['uid']) {
266 array_shift($rootlineArray);
267 }
268 $currentElement = current($rootlineArray);
269 $hierarchicArray[$currentElement['uid']] = htmlspecialchars($currentElement['title']);
270 array_shift($rootlineArray);
271 if (!empty($rootlineArray)) {
272 if (!isset($hierarchicArray[$currentElement['uid'] . '.'])) {
273 $hierarchicArray[$currentElement['uid'] . '.'] = array();
274 }
275 $this->setInPageArray($hierarchicArray[$currentElement['uid'] . '.'], $rootlineArray, $row);
276 } else {
277 $hierarchicArray[$currentElement['uid'] . '_'] = $this->extractLinesFromTSConfig($row);
278 }
279 }
280
281 /**
282 * Extract the lines of TSConfig from a given pages row
283 *
284 * @param array $row The row from the database containing the uid and TSConfig fields
285 * @return array
286 */
287 protected function extractLinesFromTSConfig(array $row)
288 {
289 $out = array();
290 $includeLines = 0;
291 $out['uid'] = $row['uid'];
292 $lines = GeneralUtility::trimExplode("\r\n", $row['TSconfig']);
293 foreach ($lines as $line) {
294 if (strpos($line, '<INCLUDE_TYPOSCRIPT:') !== false) {
295 $includeLines++;
296 }
297 }
298 $out['includeLines'] = $includeLines;
299 $out['writtenLines'] = (count($lines) - $includeLines);
300 return $out;
301 }
302
303 /**
304 * Get the list of pages to show.
305 * This function is called recursively
306 *
307 * @param array $pageArray The Page Array
308 * @param array $lines Lines that have been processed up to this point
309 * @param int $pageDepth The level of the current $pageArray being processed
310 * @return array
311 */
312 protected function getList($pageArray, $lines = array(), $pageDepth = 0)
313 {
314 if (!is_array($pageArray)) {
315 return $lines;
316 }
317
318 foreach ($pageArray as $identifier => $_) {
319 if (!MathUtility::canBeInterpretedAsInteger($identifier)) {
320 continue;
321 }
322 $line = array();
323 $line['padding'] = ($pageDepth * 20);
324 if (isset($pageArray[$identifier . '_'])) {
325 $line['link'] = htmlspecialchars(GeneralUtility::linkThisScript(array('id' => $identifier)));
326 $line['icon'] = $this->iconFactory->getIconForRecord('pages', BackendUtility::getRecordWSOL('pages', $identifier), Icon::SIZE_SMALL)->render();
327 $line['title'] = htmlspecialchars('ID: ' . $identifier);
328 $line['pageTitle'] = GeneralUtility::fixed_lgd_cs($pageArray[$identifier], 30);
329 $line['includedFiles'] = ($pageArray[$identifier . '_']['includeLines'] === 0 ? '' : $pageArray[($identifier . '_')]['includeLines']);
330 $line['lines'] = ($pageArray[$identifier . '_']['writtenLines'] === 0 ? '' : $pageArray[$identifier . '_']['writtenLines']);
331 } else {
332 $line['link'] = '';
333 $line['icon'] = $this->iconFactory->getIconForRecord('pages', BackendUtility::getRecordWSOL('pages', $identifier), Icon::SIZE_SMALL)->render();
334 $line['title'] = '';
335 $line['pageTitle'] = GeneralUtility::fixed_lgd_cs($pageArray[$identifier], 30);
336 $line['includedFiles'] = '';
337 $line['lines'] = '';
338 }
339 $lines[] = $line;
340 $lines = $this->getList($pageArray[$identifier . '.'], $lines, $pageDepth + 1);
341 }
342 return $lines;
343 }
344
345 /**
346 * Returns LanguageService
347 *
348 * @return \TYPO3\CMS\Lang\LanguageService
349 */
350 protected function getLanguageService()
351 {
352 return $GLOBALS['LANG'];
353 }
354
355 /**
356 * Returns the current BE user.
357 *
358 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
359 */
360 protected function getBackendUser()
361 {
362 return $GLOBALS['BE_USER'];
363 }
364
365 /**
366 * returns a new standalone view, shorthand function
367 *
368 * @return StandaloneView
369 */
370 protected function getFluidTemplateObject()
371 {
372 /** @var StandaloneView $view */
373 $view = GeneralUtility::makeInstance(StandaloneView::class);
374 $view->setLayoutRootPaths(array(GeneralUtility::getFileAbsFileName('EXT:info_pagetsconfig/Resources/Private/Layouts')));
375 $view->setPartialRootPaths(array(GeneralUtility::getFileAbsFileName('EXT:info_pagetsconfig/Resources/Private/Partials')));
376 $view->setTemplateRootPaths(array(GeneralUtility::getFileAbsFileName('EXT:info_pagetsconfig/Resources/Private/Templates')));
377
378 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:info_pagetsconfig/Resources/Private/Templates/Main.html'));
379
380 $view->getRequest()->setControllerExtensionName('info_pagetsconfig');
381 return $view;
382 }
383 }