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