* Fixed #10382: Bugfixes for t3editor code completion (thanks to Stephan Petzl and...
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / lib / ts_codecompletion / class.tx_t3editor_codecompletion.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008-2009 Stephan Petzl <spetzl@gmx.at> and Christian Kartnig <office@hahnepeter.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 require_once(PATH_t3lib.'class.t3lib_page.php');
29
30 class tx_t3editor_codecompletion {
31 /** @var TYPO3AJAX */
32 protected $ajaxObj;
33
34 /**
35 * General processor for AJAX requests.
36 * (called by typo3/ajax.php)
37 *
38 * @param array $params: additional parameters (not used here)
39 * @param TYPO3AJAX &$ajaxObj: the TYPO3AJAX object of this request
40 * @return void
41 * @author Oliver Hader <oliver@typo3.org>
42 */
43 public function processAjaxRequest($params, TYPO3AJAX &$ajaxObj) {
44 $this->ajaxObj = $ajaxObj;
45
46 $ajaxIdParts = explode('::', $ajaxObj->getAjaxID(), 2);
47 $ajaxMethod = $ajaxIdParts[1];
48 $response = array();
49
50 // Process the AJAX requests:
51 if ($ajaxMethod == 'loadTemplates') {
52 $ajaxObj->setContent($this->loadTemplates(
53 intval(t3lib_div::_GP('pageId')))
54 );
55 $ajaxObj->setContentFormat('jsonbody');
56 }
57 }
58
59 /**
60 * Loads all templates up to a given page id (walking the rootline) and
61 * cleans parts that are not required for the t3editor codecompletion.
62 *
63 * @param integer $pageId: id of the page
64 * @param integer $templateId: currently unused (default: 0)
65 * @return array Cleaned array of TypoScript information
66 * @author Oliver Hader <oliver@typo3.org>
67 */
68 protected function loadTemplates($pageId, $templateId = 0) {
69 $templates = array();
70
71 // Check whether access is granted (only admin have access to sys_template records):
72 if ($GLOBALS['BE_USER']->isAdmin()) {
73 // Check whether there is a pageId given:
74 if ($pageId) {
75 $templates = $this->getMergedTemplates($pageId);
76 // Otherwise, set an error:
77 } else {
78 $this->ajaxObj->setError('Syntax error: Parameter pageId must be a valid integer.');
79 }
80 // Set an error if user has no access to sys_template records:
81 } else {
82 $this->ajaxObj->setError('Access denied: No permission to template records.');
83 }
84
85 return $templates;
86 }
87
88 /**
89 * Gets merged templates by walking the rootline to a given page id.
90 *
91 * @todo oliver@typo3.org: Refactor this method and comment what's going on there
92 * @param integer $pageId
93 * @param integer $templateId
94 * @return array Setup part of merged template records
95 */
96 protected function getMergedTemplates($pageId, $templateId = 0) {
97 $result = array();
98
99 /** @var $tsParser t3lib_tsparser_ext */
100 $tsParser = t3lib_div::makeInstance('t3lib_tsparser_ext');
101 $tsParser->tt_track = 0;
102 $tsParser->init();
103 // Gets the rootLine
104 $page = t3lib_div::makeInstance('t3lib_pageSelect');
105 $rootLine = $page->getRootLine($pageId);
106
107 // This generates the constants/config + hierarchy info for the template.
108 $tsParser->runThroughTemplates($rootLine);
109
110 // ts-setup & ts-constants of the currently edited template should not be included
111 // therefor we have to delete the last template from the stack
112 array_pop($tsParser->config);
113 array_pop($tsParser->constants);
114
115 // some of the lines are not clear to me... do we need them?
116 //$tsParser->matchAlternative[] = 'dummydummydummydummydummydummydummydummydummydummydummy'; // This is just here to make sure that at least one element is in the array so that the tsparser actually uses this array to match.
117 //$tsParser->regexMode = $this->pObj->MOD_SETTINGS["ts_browser_regexsearch"];
118 // ??
119 //$tsParser->fixedLgd=$this->pObj->MOD_SETTINGS["ts_browser_fixedLgd"];
120 //$tsParser->matchAlternative = $this->pObj->MOD_SETTINGS['tsbrowser_conditions'];
121 $tsParser->linkObjects = TRUE;
122 $tsParser->ext_regLinenumbers = FALSE;
123 $tsParser->bType=$bType;
124 $tsParser->resourceCheck=1;
125 $tsParser->uplPath = PATH_site . $tsParser->uplPath;
126 $tsParser->removeFromGetFilePath = PATH_site;
127 $tsParser->generateConfig();
128
129 $result = $this->treeWalkCleanup($tsParser->setup);
130
131 return $result;
132 }
133
134 /**
135 * Walks through a tree of TypoScript configuration an cleans it up.
136 *
137 * @TODO oliver@typo3.org: Define and comment why this is necessary and exactly happens below
138 * @param array $treeBranch: TypoScript configuration or sub branch of it
139 * @return array Cleaned TypoScript branch
140 */
141 private function treeWalkCleanup(array $treeBranch){
142 $cleanedTreeBranch = array();
143
144 foreach ($treeBranch as $key => $value) {
145 $dotCount = substr_count($key, '.');
146 if ($dotCount == 0){ //type definition or value-assignment
147 if ($value != '') {
148 if (strlen($value) > 20) {
149 $value = substr($value, 0, 20);
150 }
151 if (!isset($cleanedTreeBranch[$key])) {
152 $cleanedTreeBranch[$key] = array();
153 }
154 $cleanedTreeBranch[$key]['v'] = $value;
155 }
156 } else if ($dotCount == 1) { // subtree (definition of properties)
157 $subBranch = $this->treeWalkCleanup($value);
158 if ($subBranch) {
159 $key = str_replace('.', '', $key);
160 if (!isset($cleanedTreeBranch[$key])) {
161 $cleanedTreeBranch[$key] = array();
162 }
163 $cleanedTreeBranch[$key]['c'] = $subBranch;
164 }
165 } //in other cases do nothing (this information (lineNo,..) is not needed in the editor)
166 }
167
168 return $cleanedTreeBranch;
169 }
170 }
171
172 ?>