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