[TASK] Remove dummy files in typo3/classes/
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / CodeCompletion.php
1 <?php
2 namespace TYPO3\CMS\T3Editor;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008-2013 Stephan Petzl <spetzl@gmx.at> and Christian Kartnig <office@hahnepeter.de>
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 textfile 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 * Code completion for t3editor
32 *
33 * @author Stephan Petzl <spetzl@gmx.at>
34 * @author Christian Kartnig <office@hahnepeter.de>
35 */
36 class CodeCompletion {
37
38 /**
39 * @var \TYPO3\CMS\Core\Http\AjaxRequestHandler
40 */
41 protected $ajaxObj;
42
43 /**
44 * General processor for AJAX requests.
45 * (called by typo3/ajax.php)
46 *
47 * @param array $params Additional parameters (not used here)
48 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj The AjaxRequestHandler object of this request
49 * @return void
50 * @author Oliver Hader <oliver@typo3.org>
51 */
52 public function processAjaxRequest($params, \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj) {
53 $this->ajaxObj = $ajaxObj;
54 $ajaxIdParts = explode('::', $ajaxObj->getAjaxID(), 2);
55 $ajaxMethod = $ajaxIdParts[1];
56 $response = array();
57 // Process the AJAX requests:
58 if ($ajaxMethod == 'loadTemplates') {
59 $ajaxObj->setContent($this->loadTemplates(intval(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('pageId'))));
60 $ajaxObj->setContentFormat('jsonbody');
61 }
62 }
63
64 /**
65 * Loads all templates up to a given page id (walking the rootline) and
66 * cleans parts that are not required for the t3editor codecompletion.
67 *
68 * @param integer $pageId ID of the page
69 * @param integer $templateId Currently unused (default: 0)
70 * @return array Cleaned array of TypoScript information
71 * @author Oliver Hader <oliver@typo3.org>
72 */
73 protected function loadTemplates($pageId, $templateId = 0) {
74 $templates = array();
75 // Check whether access is granted (only admin have access to sys_template records):
76 if ($GLOBALS['BE_USER']->isAdmin()) {
77 // Check whether there is a pageId given:
78 if ($pageId) {
79 $templates = $this->getMergedTemplates($pageId);
80 } else {
81 $this->ajaxObj->setError($GLOBALS['LANG']->getLL('pageIDInteger'));
82 }
83 } else {
84 $this->ajaxObj->setError($GLOBALS['LANG']->getLL('noPermission'));
85 }
86 return $templates;
87 }
88
89 /**
90 * Gets merged templates by walking the rootline to a given page id.
91 *
92 * @todo oliver@typo3.org: Refactor this method and comment what's going on there
93 * @param integer $pageId
94 * @param integer $templateId
95 * @return array Setup part of merged template records
96 */
97 protected function getMergedTemplates($pageId, $templateId = 0) {
98 $result = array();
99 /** @var $tsParser \TYPO3\CMS\Core\TypoScript\ExtendedTemplateService */
100 $tsParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\ExtendedTemplateService');
101 $tsParser->tt_track = 0;
102 $tsParser->init();
103 // Gets the rootLine
104 $page = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
105 $rootLine = $page->getRootLine($pageId);
106 // This generates the constants/config + hierarchy info for the template.
107 $tsParser->runThroughTemplates($rootLine);
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 $tsParser->linkObjects = TRUE;
113 $tsParser->ext_regLinenumbers = FALSE;
114 $tsParser->bType = $bType;
115 $tsParser->resourceCheck = 1;
116 $tsParser->removeFromGetFilePath = PATH_site;
117 $tsParser->generateConfig();
118 $result = $this->treeWalkCleanup($tsParser->setup);
119 return $result;
120 }
121
122 /**
123 * Walks through a tree of TypoScript configuration an cleans it up.
124 *
125 * @TODO oliver@typo3.org: Define and comment why this is necessary and exactly happens below
126 * @param array $treeBranch TypoScript configuration or sub branch of it
127 * @return array Cleaned TypoScript branch
128 */
129 private function treeWalkCleanup(array $treeBranch) {
130 $cleanedTreeBranch = array();
131 foreach ($treeBranch as $key => $value) {
132 $dotCount = substr_count($key, '.');
133 //type definition or value-assignment
134 if ($dotCount == 0) {
135 if ($value != '') {
136 if (strlen($value) > 20) {
137 $value = substr($value, 0, 20);
138 }
139 if (!isset($cleanedTreeBranch[$key])) {
140 $cleanedTreeBranch[$key] = array();
141 }
142 $cleanedTreeBranch[$key]['v'] = $value;
143 }
144 } elseif ($dotCount == 1) {
145 // subtree (definition of properties)
146 $subBranch = $this->treeWalkCleanup($value);
147 if ($subBranch) {
148 $key = str_replace('.', '', $key);
149 if (!isset($cleanedTreeBranch[$key])) {
150 $cleanedTreeBranch[$key] = array();
151 }
152 $cleanedTreeBranch[$key]['c'] = $subBranch;
153 }
154 }
155 }
156 return $cleanedTreeBranch;
157 }
158
159 }
160
161
162 ?>