fa986e730bbaa2db99206186ca62d68981eb39b1
[Packages/TYPO3.CMS.git] / typo3 / sysext / tstemplate / Classes / Controller / TemplateAnalyzerModuleFunctionController.php
1 <?php
2 namespace TYPO3\CMS\Tstemplate\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\Module\AbstractFunctionModule;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Imaging\IconFactory;
21 use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Frontend\Page\PageRepository;
24
25 /**
26 * TypoScript template analyzer
27 */
28 class TemplateAnalyzerModuleFunctionController extends AbstractFunctionModule
29 {
30 /**
31 * @var TypoScriptTemplateModuleController
32 */
33 public $pObj;
34
35 /**
36 * Init
37 *
38 * @param TypoScriptTemplateModuleController $pObj
39 * @param array $conf
40 * @return void
41 */
42 public function init(&$pObj, $conf)
43 {
44 parent::init($pObj, $conf);
45 $this->getLanguageService()->includeLLFile('EXT:tstemplate/Resources/Private/Language/locallang_analyzer.xlf');
46 $this->pObj->modMenu_setDefaultList .= ',ts_analyzer_checkLinenum,ts_analyzer_checkSyntax';
47 }
48
49 /**
50 * Mod menu
51 *
52 * @return array
53 */
54 public function modMenu()
55 {
56 return array(
57 'ts_analyzer_checkSetup' => '1',
58 'ts_analyzer_checkConst' => '1',
59 'ts_analyzer_checkLinenum' => '1',
60 'ts_analyzer_checkComments' => '1',
61 'ts_analyzer_checkCrop' => '1',
62 'ts_analyzer_checkSyntax' => '1'
63 );
64 }
65
66 /**
67 * Initialize editor
68 *
69 * @param int $pageId
70 * @param int $template_uid
71 * @return bool
72 */
73 public function initialize_editor($pageId, $template_uid = 0)
74 {
75 // Initializes the module. Done in this function because we may need to re-initialize if data is submitted!
76 $templateService = GeneralUtility::makeInstance(ExtendedTemplateService::class);
77 $GLOBALS['tmpl'] = $templateService;
78
79 // Do not log time-performance information
80 $templateService->tt_track = false;
81 $templateService->init();
82
83 // Gets the rootLine
84 $sys_page = GeneralUtility::makeInstance(PageRepository::class);
85 $GLOBALS['rootLine'] = $sys_page->getRootLine($pageId);
86
87 // This generates the constants/config + hierarchy info for the template.
88 $templateService->runThroughTemplates($GLOBALS['rootLine'], $template_uid);
89
90 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
91 $GLOBALS['tplRow'] = $templateService->ext_getFirstTemplate($pageId, $template_uid);
92 return is_array($GLOBALS['tplRow']);
93 }
94
95 /**
96 * Main
97 *
98 * @return string
99 */
100 public function main()
101 {
102 $theOutput = '';
103
104 // Initializes the module. Done in this function because we may need to re-initialize if data is submitted!
105 // Checking for more than one template an if, set a menu...
106 $manyTemplatesMenu = $this->pObj->templateMenu();
107 $template_uid = 0;
108 if ($manyTemplatesMenu) {
109 $template_uid = $this->pObj->MOD_SETTINGS['templatesOnPage'];
110 }
111
112 $existTemplate = $this->initialize_editor($this->pObj->id, $template_uid);
113
114 // initialize
115 $lang = $this->getLanguageService();
116 if ($existTemplate) {
117 $siteTitle = trim($GLOBALS['tplRow']['sitetitle']);
118 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
119 $theOutput .= '<h3>' . htmlspecialchars($lang->getLL('currentTemplate')) . '</h3>';
120 $theOutput .= $iconFactory->getIconForRecord('sys_template', $GLOBALS['tplRow'], Icon::SIZE_SMALL)->render()
121 . '<strong>' . $this->pObj->linkWrapTemplateTitle($GLOBALS['tplRow']['title']) . '</strong>'
122 . htmlspecialchars($siteTitle ? ' (' . $siteTitle . ')' : '');
123 }
124 if ($manyTemplatesMenu) {
125 $theOutput .= '<div>' . $manyTemplatesMenu . '</div>';
126 }
127 $templateService = $this->getExtendedTemplateService();
128 $templateService->clearList_const_temp = array_flip($templateService->clearList_const);
129 $templateService->clearList_setup_temp = array_flip($templateService->clearList_setup);
130 $pointer = count($templateService->hierarchyInfo);
131 $hierarchyInfo = $templateService->ext_process_hierarchyInfo(array(), $pointer);
132 $head = '<thead><tr>';
133 $head .= '<th>' . htmlspecialchars($lang->getLL('title')) . '</th>';
134 $head .= '<th>' . htmlspecialchars($lang->getLL('rootlevel')) . '</th>';
135 $head .= '<th>' . htmlspecialchars($lang->getLL('clearSetup')) . '</th>';
136 $head .= '<th>' . htmlspecialchars($lang->getLL('clearConstants')) . '</th>';
137 $head .= '<th>' . htmlspecialchars($lang->getLL('pid')) . '</th>';
138 $head .= '<th>' . htmlspecialchars($lang->getLL('rootline')) . '</th>';
139 $head .= '<th>' . htmlspecialchars($lang->getLL('nextLevel')) . '</th>';
140 $head .= '</tr></thead>';
141 $hierar = implode(array_reverse($templateService->ext_getTemplateHierarchyArr($hierarchyInfo, '', array(), 1)), '');
142 $hierar = '<div class="table-fit"><table class="table table-striped table-hover" id="ts-analyzer">' . $head . $hierar . '</table></div>';
143 $theOutput .= '<div style="padding-top: 5px;"></div>';
144 $theOutput .= '<h2>' . htmlspecialchars($lang->getLL('templateHierarchy')) . '</h2>';
145 $theOutput .= '<div>' . $hierar . '</div>';
146 $urlParameters = array(
147 'id' => $GLOBALS['SOBE']->id,
148 'template' => 'all'
149 );
150 $aHref = BackendUtility::getModuleUrl('web_ts', $urlParameters);
151
152 $completeLink = '<p><a href="' . htmlspecialchars($aHref) . '" class="btn btn-default">' . htmlspecialchars($lang->getLL('viewCompleteTS')) . '</a></p>';
153 $theOutput .= '<div style="padding-top: 5px;"></div>';
154 $theOutput .= '<h2>' . htmlspecialchars($lang->getLL('completeTS')) . '</h2>';
155 $theOutput .= '<div>' . $completeLink . '</div>';
156 $theOutput .= '<div style="padding-top: 15px;"></div>';
157 // Output options
158 $theOutput .= '<h2>' . htmlspecialchars($lang->getLL('displayOptions')) . '</h2>';
159
160 $template = GeneralUtility::_GET('template');
161 $addParams = $template ? '&template=' . $template : '';
162 $theOutput .= '<div class="tst-analyzer-options">' .
163 '<div class="checkbox"><label for="checkTs_analyzer_checkLinenum">' .
164 BackendUtility::getFuncCheck($this->pObj->id, 'SET[ts_analyzer_checkLinenum]', $this->pObj->MOD_SETTINGS['ts_analyzer_checkLinenum'], '', $addParams, 'id="checkTs_analyzer_checkLinenum"') .
165 htmlspecialchars($lang->getLL('lineNumbers')) .
166 '</label></div>' .
167 '<div class="checkbox"><label for="checkTs_analyzer_checkSyntax">' .
168 BackendUtility::getFuncCheck($this->pObj->id, 'SET[ts_analyzer_checkSyntax]', $this->pObj->MOD_SETTINGS['ts_analyzer_checkSyntax'], '', $addParams, 'id="checkTs_analyzer_checkSyntax"') .
169 htmlspecialchars($lang->getLL('syntaxHighlight')) . '</label> ' .
170 '</label></div>';
171 if (!$this->pObj->MOD_SETTINGS['ts_analyzer_checkSyntax']) {
172 $theOutput .=
173 '<div class="checkbox"><label for="checkTs_analyzer_checkComments">' .
174 BackendUtility::getFuncCheck($this->pObj->id, 'SET[ts_analyzer_checkComments]', $this->pObj->MOD_SETTINGS['ts_analyzer_checkComments'], '', $addParams, 'id="checkTs_analyzer_checkComments"') .
175 htmlspecialchars($lang->getLL('comments')) .
176 '</label></div>' .
177 '<div class="checkbox"><label for="checkTs_analyzer_checkCrop">' .
178 BackendUtility::getFuncCheck($this->pObj->id, 'SET[ts_analyzer_checkCrop]', $this->pObj->MOD_SETTINGS['ts_analyzer_checkCrop'], '', $addParams, 'id="checkTs_analyzer_checkCrop"') .
179 htmlspecialchars($lang->getLL('cropLines')) .
180 '</label></div>';
181 }
182 $theOutput .= '</div>';
183 $theOutput .= '<div style="padding-top: 25px;"></div>';
184
185 if ($template) {
186 // Output Constants
187 $theOutput .= '<h2>' . htmlspecialchars($lang->getLL('constants')) . '</h2>';
188
189 $templateService->ext_lineNumberOffset = 0;
190 $templateService->ext_lineNumberOffset_mode = 'const';
191 foreach ($templateService->constants as $key => $val) {
192 $currentTemplateId = $templateService->hierarchyInfo[$key]['templateID'];
193 if ($currentTemplateId == $template || $template === 'all') {
194 $theOutput .= '
195 <h3>' . htmlspecialchars($templateService->hierarchyInfo[$key]['title']) . '</h3>
196 <div class="nowrap">' .
197 $templateService->ext_outputTS(array($val), $this->pObj->MOD_SETTINGS['ts_analyzer_checkLinenum'], $this->pObj->MOD_SETTINGS['ts_analyzer_checkComments'], $this->pObj->MOD_SETTINGS['ts_analyzer_checkCrop'], $this->pObj->MOD_SETTINGS['ts_analyzer_checkSyntax'], 0) .
198 '</div>
199 ';
200 if ($template !== 'all') {
201 break;
202 }
203 }
204 $templateService->ext_lineNumberOffset += count(explode(LF, $val)) + 1;
205 }
206
207 // Output Setup
208 $theOutput .= '<div style="padding-top: 15px;"></div>';
209 $theOutput .= '<h2>' . htmlspecialchars($lang->getLL('setup')) . '</h2>';
210 $templateService->ext_lineNumberOffset = 0;
211 $templateService->ext_lineNumberOffset_mode = 'setup';
212 foreach ($templateService->config as $key => $val) {
213 $currentTemplateId = $templateService->hierarchyInfo[$key]['templateID'];
214 if ($currentTemplateId == $template || $template == 'all') {
215 $theOutput .= '
216 <h3>' . htmlspecialchars($templateService->hierarchyInfo[$key]['title']) . '</h3>
217 <div class="nowrap">' .
218 $templateService->ext_outputTS(array($val), $this->pObj->MOD_SETTINGS['ts_analyzer_checkLinenum'], $this->pObj->MOD_SETTINGS['ts_analyzer_checkComments'], $this->pObj->MOD_SETTINGS['ts_analyzer_checkCrop'], $this->pObj->MOD_SETTINGS['ts_analyzer_checkSyntax'], 0) .
219 '</div>
220 ';
221 if ($template !== 'all') {
222 break;
223 }
224 }
225 $templateService->ext_lineNumberOffset += count(explode(LF, $val)) + 1;
226 }
227 }
228 return $theOutput;
229 }
230
231 /**
232 * @return ExtendedTemplateService
233 */
234 protected function getExtendedTemplateService()
235 {
236 return $GLOBALS['tmpl'];
237 }
238 }