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