[TASK] Fix CGL errors
[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 = $this->pObj->doc->header($this->getLanguageService()->getLL('tsconf_title'));
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 $pTitle = $this->pObj->doc->getHeader('pages', $row, '', false);
115 $editIdList = substr($k, 4);
116 $urlParameters = [
117 'edit' => [
118 'pages' => [
119 $editIdList => 'edit',
120 ]
121 ],
122 'columnsOnly' => 'TSconfig',
123 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
124 ];
125 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
126 $editIcon = '<a href="' . htmlspecialchars($url) . '" title="' . $this->getLanguageService()->getLL('editTSconfig', true) . '">' . $this->iconFactory->getIcon('actions-document-open', Icon::SIZE_SMALL)->render() . '</a>';
127 }
128 $TScontent = nl2br(htmlspecialchars(trim($v) . LF));
129 $tsparser = GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::class);
130 $tsparser->lineNumberOffset = 0;
131 $TScontent = $tsparser->doSyntaxHighlight(trim($v) . LF);
132 $lines[] = '
133 <tr><td nowrap="nowrap" class="bgColor5">' . $pTitle . '</td></tr>
134 <tr><td nowrap="nowrap" class="bgColor4">' . $TScontent . $editIcon . '</td></tr>
135 <tr><td>&nbsp;</td></tr>
136 ';
137 }
138 }
139 if (!empty($pUids)) {
140 $urlParameters = [
141 'edit' => [
142 'pages' => [
143 implode(',', $pUids) => 'edit',
144 ]
145 ],
146 'columnsOnly' => 'TSconfig',
147 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
148 ];
149 $url = BackendUtility::getModuleUrl('record_edit', $urlParameters);
150 $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>';
151 } else {
152 $editIcon = '';
153 }
154 $theOutput .= '<div>';
155 $theOutput .= BackendUtility::cshItem('_MOD_web_info', 'tsconfig_edit', null) . $menu . '
156 <!-- Edit fields: -->
157 <table border="0" cellpadding="0" cellspacing="1">' . implode('', $lines) . '</table><br />' . $editIcon;
158 $theOutput .= '</div>';
159 } else {
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 $csh = BackendUtility::cshItem('_MOD_web_info', 'tsconfig_hierarchy', null);
222 $tree = $tmpl->ext_getObjTree($modTSconfig, '', '', '', '', $this->pObj->MOD_SETTINGS['tsconf_alphaSort']);
223
224 $theOutput .= '<div>';
225 $theOutput .= $csh . $menu . '<div class="nowrap">' . $tree . '</div>';
226 $theOutput .= '</div>';
227 }
228 }
229
230 return $theOutput;
231 }
232
233 /**
234 * Renders table rows of all pages containing TSConfig together with its rootline
235 *
236 * @return array
237 */
238 protected function getOverviewOfPagesUsingTSConfig()
239 {
240 $db = $this->getDatabaseConnection();
241 $res = $db->exec_SELECTquery(
242 'uid, TSconfig',
243 'pages',
244 'TSconfig != \'\''
245 . BackendUtility::deleteClause('pages')
246 . BackendUtility::versioningPlaceholderClause('pages'), 'pages.uid');
247 $pageArray = array();
248 while ($row = $db->sql_fetch_assoc($res)) {
249 $this->setInPageArray($pageArray, BackendUtility::BEgetRootLine($row['uid'], 'AND 1=1'), $row);
250 }
251 return $this->renderList($pageArray);
252 }
253
254 /**
255 * Set page in array
256 * This function is called recursively and builds a multi-dimensional array that reflects the page
257 * hierarchy.
258 *
259 * @param array $hierarchicArray The hierarchic array (passed by reference)
260 * @param array $rootlineArray The rootline array
261 * @param array $row The row from the database containing the uid and TSConfig fields
262 * @return void
263 */
264 protected function setInPageArray(&$hierarchicArray, $rootlineArray, $row)
265 {
266 ksort($rootlineArray);
267 reset($rootlineArray);
268 if (!$rootlineArray[0]['uid']) {
269 array_shift($rootlineArray);
270 }
271 $currentElement = current($rootlineArray);
272 $hierarchicArray[$currentElement['uid']] = htmlspecialchars($currentElement['title']);
273 array_shift($rootlineArray);
274 if (!empty($rootlineArray)) {
275 if (!isset($hierarchicArray[($currentElement['uid'] . '.')])) {
276 $hierarchicArray[$currentElement['uid'] . '.'] = array();
277 }
278 $this->setInPageArray($hierarchicArray[$currentElement['uid'] . '.'], $rootlineArray, $row);
279 } else {
280 $hierarchicArray[$currentElement['uid'] . '_'] = $this->extractLinesFromTSConfig($row);
281 }
282 }
283
284 /**
285 * Extract the lines of TSConfig from a given pages row
286 *
287 * @param array $row The row from the database containing the uid and TSConfig fields
288 * @return array
289 */
290 protected function extractLinesFromTSConfig(array $row)
291 {
292 $out = array();
293 $includeLines = 0;
294 $out['uid'] = $row['uid'];
295 $lines = GeneralUtility::trimExplode("\r\n", $row['TSconfig']);
296 foreach ($lines as $line) {
297 if (strpos($line, '<INCLUDE_TYPOSCRIPT:') !== false) {
298 $includeLines++;
299 }
300 }
301 $out['includeLines'] = $includeLines;
302 $out['writtenLines'] = (count($lines) - $includeLines);
303 return $out;
304 }
305
306 /**
307 * Render the list of pages to show.
308 * This function is called recursively
309 *
310 * @param array $pageArray The Page Array
311 * @param array $lines Lines that have been processed up to this point
312 * @param int $pageDepth The level of the current $pageArray being processed
313 * @return array
314 */
315 protected function renderList($pageArray, $lines = array(), $pageDepth = 0)
316 {
317 $cellStyle = 'padding-left: ' . ($pageDepth * 20) . 'px';
318 if (!is_array($pageArray)) {
319 return $lines;
320 }
321
322 foreach ($pageArray as $identifier => $_) {
323 if (!MathUtility::canBeInterpretedAsInteger($identifier)) {
324 continue;
325 }
326 if (isset($pageArray[$identifier . '_'])) {
327 $lines[] = '
328 <tr>
329 <td nowrap style="' . $cellStyle . '">
330 <a href="'
331 . htmlspecialchars(GeneralUtility::linkThisScript(array('id' => $identifier)))
332 . '" title="' . htmlspecialchars('ID: ' . $identifier) . '">'
333 . $this->iconFactory->getIconForRecord('pages', BackendUtility::getRecordWSOL('pages', $identifier), Icon::SIZE_SMALL)->render()
334 . GeneralUtility::fixed_lgd_cs($pageArray[$identifier], 30) . '</a></td>
335 <td>' . ($pageArray[($identifier . '_')]['includeLines'] === 0 ? '' : $pageArray[($identifier . '_')]['includeLines']) . '</td>
336 <td>' . ($pageArray[$identifier . '_']['writtenLines'] === 0 ? '' : $pageArray[$identifier . '_']['writtenLines']) . '</td>
337 </tr>';
338 } else {
339 $lines[] = '<tr>
340 <td nowrap style="' . $cellStyle . '">'
341 . $this->iconFactory->getIconForRecord('pages', BackendUtility::getRecordWSOL('pages', $identifier), Icon::SIZE_SMALL)->render()
342 . GeneralUtility::fixed_lgd_cs($pageArray[$identifier], 30) . '</td>
343 <td></td>
344 <td></td>
345 </tr>';
346 }
347 $lines = $this->renderList($pageArray[$identifier . '.'], $lines, $pageDepth + 1);
348 }
349 return $lines;
350 }
351
352 /**
353 * Returns LanguageService
354 *
355 * @return \TYPO3\CMS\Lang\LanguageService
356 */
357 protected function getLanguageService()
358 {
359 return $GLOBALS['LANG'];
360 }
361
362 /**
363 * Returns the current BE user.
364 *
365 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
366 */
367 protected function getBackendUser()
368 {
369 return $GLOBALS['BE_USER'];
370 }
371 }