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