* Fixed #10380:
[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 class tx_t3editor_codecompletion {
29 /** @var TYPO3AJAX */
30 protected $ajaxObj;
31
32 /**
33 * General processor for AJAX requests.
34 * (called by typo3/ajax.php)
35 *
36 * @param array $params: additional parameters (not used here)
37 * @param TYPO3AJAX &$ajaxObj: the TYPO3AJAX object of this request
38 * @return void
39 * @author Oliver Hader <oliver@typo3.org>
40 */
41 public function processAjaxRequest($params, TYPO3AJAX &$ajaxObj) {
42 $this->ajaxObj = $ajaxObj;
43
44 $ajaxIdParts = explode('::', $ajaxObj->getAjaxID(), 2);
45 $ajaxMethod = $ajaxIdParts[1];
46 $response = array();
47
48 // Process the AJAX requests:
49 if ($ajaxMethod == 'loadTemplates') {
50 $ajaxObj->setContent($this->loadTemplates(
51 intval(t3lib_div::_GP('pageId')))
52 );
53 $ajaxObj->setContentFormat('jsonbody');
54 }
55 }
56
57 /**
58 * Loads all templates up to a given page id (walking the rootline) and
59 * cleans parts that are not required for the t3editor codecompletion.
60 *
61 * @param integer $pageId: id of the page
62 * @param integer $templateId: currently unused (default: 0)
63 * @return array Cleaned array of TypoScript information
64 * @author Oliver Hader <oliver@typo3.org>
65 */
66 protected function loadTemplates($pageId, $templateId = 0) {
67 $templates = array();
68
69 // Check whether access is granted (only admin have access to sys_template records):
70 if ($GLOBALS['BE_USER']->isAdmin()) {
71 // Check whether there is a pageId given:
72 if ($pageId) {
73 $templates = $this->getMergedTemplates($pageId);
74 // Otherwise, set an error:
75 } else {
76 $this->ajaxObj->setError('Syntax error: Parameter pageId must be a valid integer.');
77 }
78 // Set an error if user has no access to sys_template records:
79 } else {
80 $this->ajaxObj->setError('Access denied: No permission to template records.');
81 }
82
83 return $templates;
84 }
85
86 /**
87 * Gets merged templates by walking the rootline to a given page id.
88 *
89 * @todo oliver@typo3.org: Refactor this method and comment what's going on there
90 * @param integer $pageId
91 * @param integer $templateId
92 * @return array Setup part of merged template records
93 */
94 protected function getMergedTemplates($pageId, $templateId = 0) {
95 $result = array();
96
97 /** @var $tsParser t3lib_tsparser_ext */
98 $tsParser = t3lib_div::makeInstance('t3lib_tsparser_ext');
99 $tsParser->tt_track = 0;
100 $tsParser->init();
101 // Gets the rootLine
102 $page = t3lib_div::makeInstance('t3lib_pageSelect');
103 $rootLine = $page->getRootLine($pageId);
104
105 // This generates the constants/config + hierarchy info for the template.
106 $tsParser->runThroughTemplates($rootLine);
107
108 // ts-setup & ts-constants of the currently edited template should not be included
109 // therefor we have to delete the last template from the stack
110 array_pop($tsParser->config);
111 array_pop($tsParser->constants);
112
113 // some of the lines are not clear to me... do we need them?
114 //$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.
115 //$tsParser->regexMode = $this->pObj->MOD_SETTINGS["ts_browser_regexsearch"];
116 // ??
117 //$tsParser->fixedLgd=$this->pObj->MOD_SETTINGS["ts_browser_fixedLgd"];
118 //$tsParser->matchAlternative = $this->pObj->MOD_SETTINGS['tsbrowser_conditions'];
119 $tsParser->linkObjects = TRUE;
120 $tsParser->ext_regLinenumbers = FALSE;
121 $tsParser->bType=$bType;
122 $tsParser->resourceCheck=1;
123 $tsParser->uplPath = PATH_site . $tsParser->uplPath;
124 $tsParser->removeFromGetFilePath = PATH_site;
125 $tsParser->generateConfig();
126
127 $result = $this->treeWalkCleanup($tsParser->setup);
128
129 return $result;
130 }
131
132 /**
133 * Walks through a tree of TypoScript configuration an cleans it up.
134 *
135 * @TODO oliver@typo3.org: Define and comment why this is necessary and exactly happens below
136 * @param array $treeBranch: TypoScript configuration or sub branch of it
137 * @return array Cleaned TypoScript branch
138 */
139 private function treeWalkCleanup(array $treeBranch){
140 $cleanedTreeBranch = array();
141
142 foreach ($treeBranch as $key => $value) {
143 $dotCount = substr_count($key, '.');
144 if ($dotCount == 0){ //type definition or value-assignment
145 if ($value != '') {
146 if (strlen($value) > 20) {
147 $value = substr($value, 0, 20);
148 }
149 if (!isset($cleanedTreeBranch[$key])) {
150 $cleanedTreeBranch[$key] = array();
151 }
152 $cleanedTreeBranch[$key]['v'] = $value;
153 }
154 } else if ($dotCount == 1) { // subtree (definition of properties)
155 $subBranch = $this->treeWalkCleanup($value);
156 if ($subBranch) {
157 $key = str_replace('.', '', $key);
158 if (!isset($cleanedTreeBranch[$key])) {
159 $cleanedTreeBranch[$key] = array();
160 }
161 $cleanedTreeBranch[$key]['c'] = $subBranch;
162 }
163 } //in other cases do nothing (this information (lineNo,..) is not needed in the editor)
164 }
165
166 return $cleanedTreeBranch;
167 }
168 }
169
170 ?>