[BUGFIX] lineNumber count in Template Analyzer
[Packages/TYPO3.CMS.git] / typo3 / sysext / tstemplate / Classes / Controller / TemplateAnalyzerModuleFunctionController.php
1 <?php
2 namespace TYPO3\CMS\TsTemplate\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * TypoScript template analyzer
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 class TemplateAnalyzerModuleFunctionController extends \TYPO3\CMS\Backend\Module\AbstractFunctionModule {
36
37 /**
38 * Init
39 *
40 * @param object $pObj
41 * @param array $conf
42 * @return void
43 * @todo Define visibility
44 */
45 public function init(&$pObj, $conf) {
46 parent::init($pObj, $conf);
47 $GLOBALS['LANG']->includeLLFile('EXT:tstemplate/Resources/Private/Language/locallang_analyzer.xlf');
48 $this->pObj->modMenu_setDefaultList .= ',ts_analyzer_checkLinenum,ts_analyzer_checkSyntax';
49 }
50
51 /**
52 * Mod menu
53 *
54 * @return array
55 * @todo Define visibility
56 */
57 public function modMenu() {
58 return array(
59 'ts_analyzer_checkSetup' => '1',
60 'ts_analyzer_checkConst' => '1',
61 'ts_analyzer_checkLinenum' => '1',
62 'ts_analyzer_checkComments' => '1',
63 'ts_analyzer_checkCrop' => '1',
64 'ts_analyzer_checkSyntax' => '1'
65 );
66 }
67
68 /**
69 * Initialize editor
70 *
71 * @param integer $pageId
72 * @param integer $template_uid
73 * @return integer
74 * @todo Define visibility
75 */
76 public function initialize_editor($pageId, $template_uid = 0) {
77 // Initializes the module. Done in this function because we may need to re-initialize if data is submitted!
78 $GLOBALS['tmpl'] = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\ExtendedTemplateService');
79 // Do not log time-performance information
80 $GLOBALS['tmpl']->tt_track = 0;
81 $GLOBALS['tmpl']->init();
82 // Gets the rootLine
83 $sys_page = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
84 $GLOBALS['rootLine'] = $sys_page->getRootLine($pageId);
85 // This generates the constants/config + hierarchy info for the template.
86 $GLOBALS['tmpl']->runThroughTemplates($GLOBALS['rootLine'], $template_uid);
87 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
88 $GLOBALS['tplRow'] = $GLOBALS['tmpl']->ext_getFirstTemplate($pageId, $template_uid);
89 if (is_array($GLOBALS['tplRow'])) {
90 // IF there was a template...
91 return 1;
92 }
93 }
94
95 /**
96 * Main
97 *
98 * @return string
99 * @todo Define visibility
100 */
101 public function main() {
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 // BUGBUG: Should we check if the uset may at all read and write template-records???
113 $existTemplate = $this->initialize_editor($this->pObj->id, $template_uid);
114
115 // initialize
116 if ($existTemplate) {
117 $theOutput .= $this->pObj->doc->section(
118 $GLOBALS['LANG']->getLL('currentTemplate', TRUE),
119 \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'] . ')' : ''))
120 );
121 }
122 if ($manyTemplatesMenu) {
123 $theOutput .= $this->pObj->doc->section('', $manyTemplatesMenu);
124 }
125 $GLOBALS['tmpl']->clearList_const_temp = array_flip($GLOBALS['tmpl']->clearList_const);
126 $GLOBALS['tmpl']->clearList_setup_temp = array_flip($GLOBALS['tmpl']->clearList_setup);
127 $pointer = count($GLOBALS['tmpl']->hierarchyInfo);
128 $hierarchyInfo = $GLOBALS['tmpl']->ext_process_hierarchyInfo(array(), $pointer);
129 $head = '<thead><tr>';
130 $head .= '<th>' . $GLOBALS['LANG']->getLL('title', TRUE) . '</th>';
131 $head .= '<th>' . $GLOBALS['LANG']->getLL('rootlevel', TRUE) . '</th>';
132 $head .= '<th>' . $GLOBALS['LANG']->getLL('clearSetup', TRUE) . '</th>';
133 $head .= '<th>' . $GLOBALS['LANG']->getLL('clearConstants', TRUE) . '</th>';
134 $head .= '<th>' . $GLOBALS['LANG']->getLL('pid', TRUE) . '</th>';
135 $head .= '<th>' . $GLOBALS['LANG']->getLL('rootline', TRUE) . '</th>';
136 $head .= '<th>' . $GLOBALS['LANG']->getLL('nextLevel', TRUE) . '</th>';
137 $head .= '</tr></thead>';
138 $hierar = implode(array_reverse($GLOBALS['tmpl']->ext_getTemplateHierarchyArr($hierarchyInfo, '', array(), 1)), '');
139 $hierar = '<table class="t3-table" id="ts-analyzer">' . $head . $hierar . '</table>';
140 $theOutput .= $this->pObj->doc->spacer(5);
141 $theOutput .= $this->pObj->doc->section($GLOBALS['LANG']->getLL('templateHierarchy', TRUE), $hierar, 0, 1);
142 $urlParameters = array(
143 'id' => $GLOBALS['SOBE']->id,
144 'template' => 'all'
145 );
146 $aHref = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleUrl('web_ts', $urlParameters);
147 $completeLink = '<p><a href="' . htmlspecialchars($aHref) . '">' . $GLOBALS['LANG']->getLL('viewCompleteTS', TRUE) . '</a></p>';
148 $theOutput .= $this->pObj->doc->spacer(5);
149 $theOutput .= $this->pObj->doc->section($GLOBALS['LANG']->getLL('completeTS', TRUE), $completeLink, 0, 1);
150 $theOutput .= $this->pObj->doc->spacer(15);
151 // Output options
152 $theOutput .= $this->pObj->doc->section($GLOBALS['LANG']->getLL('displayOptions', TRUE), '', FALSE, TRUE);
153 $addParams = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') ? '&template=' . \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') : '';
154 $theOutput .= '<div class="tst-analyzer-options">' . \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->pObj->id, 'SET[ts_analyzer_checkLinenum]', $this->pObj->MOD_SETTINGS['ts_analyzer_checkLinenum'], '', $addParams, 'id="checkTs_analyzer_checkLinenum"') . '<label for="checkTs_analyzer_checkLinenum">' . $GLOBALS['LANG']->getLL('lineNumbers', TRUE) . '</label> ' . \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->pObj->id, 'SET[ts_analyzer_checkSyntax]', $this->pObj->MOD_SETTINGS['ts_analyzer_checkSyntax'], '', $addParams, 'id="checkTs_analyzer_checkSyntax"') . '<label for="checkTs_analyzer_checkSyntax">' . $GLOBALS['LANG']->getLL('syntaxHighlight', TRUE) . '</label> ' . (!$this->pObj->MOD_SETTINGS['ts_analyzer_checkSyntax'] ? \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->pObj->id, 'SET[ts_analyzer_checkComments]', $this->pObj->MOD_SETTINGS['ts_analyzer_checkComments'], '', $addParams, 'id="checkTs_analyzer_checkComments"') . '<label for="checkTs_analyzer_checkComments">' . $GLOBALS['LANG']->getLL('comments', TRUE) . '</label> ' . \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->pObj->id, 'SET[ts_analyzer_checkCrop]', $this->pObj->MOD_SETTINGS['ts_analyzer_checkCrop'], '', $addParams, 'id="checkTs_analyzer_checkCrop"') . '<label for="checkTs_analyzer_checkCrop">' . $GLOBALS['LANG']->getLL('cropLines', TRUE) . '</label> ' : '') . '</div>';
155 $theOutput .= $this->pObj->doc->spacer(25);
156 // Output Constants
157 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template')) {
158 $theOutput .= $this->pObj->doc->section($GLOBALS['LANG']->getLL('constants', TRUE), '', 0, 1);
159 $theOutput .= $this->pObj->doc->sectionEnd();
160 $theOutput .= '
161 <table class="t3-table ts-typoscript">
162 ';
163 // Don't know why -2 and not 0... :-) But works.
164 $GLOBALS['tmpl']->ext_lineNumberOffset = 0;
165 $GLOBALS['tmpl']->ext_lineNumberOffset_mode = 'const';
166 foreach ($GLOBALS['tmpl']->constants as $key => $val) {
167 $currentTemplateId = $GLOBALS['tmpl']->hierarchyInfo[$key]['templateID'];
168 if ($currentTemplateId == \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') || \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') == 'all') {
169 $theOutput .= '
170 <tr>
171 <td><strong>' . htmlspecialchars($GLOBALS['tmpl']->hierarchyInfo[$key]['title']) . '</strong></td>
172 </tr>
173 <tr>
174 <td>
175 <table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td nowrap="nowrap">' . $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) . '</td></tr></table>
176 </td>
177 </tr>
178 ';
179 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') != 'all') {
180 break;
181 }
182 }
183 $GLOBALS['tmpl']->ext_lineNumberOffset += count(explode(LF, $val)) + 1;
184 }
185 $theOutput .= '
186 </table>
187 ';
188 }
189 // Output setup
190 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template')) {
191 $theOutput .= $this->pObj->doc->spacer(15);
192 $theOutput .= $this->pObj->doc->section($GLOBALS['LANG']->getLL('setup', TRUE), '', 0, 1);
193 $theOutput .= $this->pObj->doc->sectionEnd();
194 $theOutput .= '
195 <table class="t3-table ts-typoscript">
196 ';
197 $GLOBALS['tmpl']->ext_lineNumberOffset = 0;
198 $GLOBALS['tmpl']->ext_lineNumberOffset_mode = 'setup';
199 foreach ($GLOBALS['tmpl']->config as $key => $val) {
200 $currentTemplateId = $GLOBALS['tmpl']->hierarchyInfo[$key]['templateID'];
201 if ($currentTemplateId == \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') || \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') == 'all') {
202 $theOutput .= '
203 <tr>
204 <td><strong>' . htmlspecialchars($GLOBALS['tmpl']->hierarchyInfo[$key]['title']) . '</strong></td></tr>
205 <tr>
206 <td><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td nowrap="nowrap">' . $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) . '</td></tr></table>
207 </td>
208 </tr>
209 ';
210 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') != 'all') {
211 break;
212 }
213 }
214 $GLOBALS['tmpl']->ext_lineNumberOffset += count(explode(LF, $val)) + 1;
215 }
216 $theOutput .= '
217 </table>
218 ';
219 }
220 return $theOutput;
221 }
222
223 }