[BUGFIX] SQL error in install tool if no root page available
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / TypoScriptReferenceLoader.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 * Loads TSref information from a XML file an responds to an AJAX call.
32 *
33 * @author Stephan Petzl <spetzl@gmx.at>
34 * @author Christian Kartnig <office@hahnepeter.de>
35 */
36 class TypoScriptReferenceLoader {
37
38 /**
39 * @var \DOMDocument
40 */
41 protected $xmlDoc;
42
43 /**
44 * @var \TYPO3\CMS\Core\Http\AjaxRequestHandler
45 */
46 protected $ajaxObj;
47
48 /**
49 * General processor for AJAX requests.
50 * (called by typo3/ajax.php)
51 *
52 * @param array $params Additional parameters (not used here)
53 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj The AjaxRequestHandler object of this request
54 * @return void
55 * @author Oliver Hader <oliver@typo3.org>
56 */
57 public function processAjaxRequest($params, \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj) {
58 $this->ajaxObj = $ajaxObj;
59 // Load the TSref XML information:
60 $this->loadFile(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('t3editor') . 'res/tsref/tsref.xml');
61 $ajaxIdParts = explode('::', $ajaxObj->getAjaxID(), 2);
62 $ajaxMethod = $ajaxIdParts[1];
63 $response = array();
64 // Process the AJAX requests:
65 if ($ajaxMethod == 'getTypes') {
66 $ajaxObj->setContent($this->getTypes());
67 $ajaxObj->setContentFormat('jsonbody');
68 } elseif ($ajaxMethod == 'getDescription') {
69 $ajaxObj->addContent('', $this->getDescription(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('typeId'), \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('parameterName')));
70 $ajaxObj->setContentFormat('plain');
71 }
72 }
73
74 /**
75 * Load XML file
76 *
77 * @param string $filepath
78 * @return void
79 */
80 protected function loadFile($filepath) {
81 $this->xmlDoc = new \DOMDocument('1.0', 'utf-8');
82 $this->xmlDoc->load($filepath);
83 // @TODO: oliver@typo3.org: I guess this is not required here
84 $this->xmlDoc->saveXML();
85 }
86
87 /**
88 * Get types from XML
89 *
90 * @return array
91 */
92 protected function getTypes() {
93 $types = $this->xmlDoc->getElementsByTagName('type');
94 $typeArr = array();
95 foreach ($types as $type) {
96 $typeId = $type->getAttribute('id');
97 $typeName = $type->getAttribute('name');
98 if (!$typeName) {
99 $typeName = $typeId;
100 }
101 $properties = $type->getElementsByTagName('property');
102 $propArr = array();
103 foreach ($properties as $property) {
104 $p = array();
105 $p['name'] = $property->getAttribute('name');
106 $p['type'] = $property->getAttribute('type');
107 $propArr[$property->getAttribute('name')] = $p;
108 }
109 $typeArr[$typeId] = array();
110 $typeArr[$typeId]['properties'] = $propArr;
111 $typeArr[$typeId]['name'] = $typeName;
112 if ($type->hasAttribute('extends')) {
113 $typeArr[$typeId]['extends'] = $type->getAttribute('extends');
114 }
115 }
116 return $typeArr;
117 }
118
119 /**
120 * Get description
121 *
122 * @param string $typeId
123 * @param string $parameterName
124 * @return string
125 */
126 protected function getDescription($typeId, $parameterName = '') {
127 if (!$typeId) {
128 $this->ajaxObj->setError($GLOBALS['LANG']->getLL('typeIDMissing'));
129 return '';
130 }
131 // getElementById does only work with schema
132 $type = $this->getType($typeId);
133 // Retrieve propertyDescription
134 if ($parameterName) {
135 $properties = $type->getElementsByTagName('property');
136 foreach ($properties as $propery) {
137 $propName = $propery->getAttribute('name');
138 if ($propName == $parameterName) {
139 $descriptions = $propery->getElementsByTagName('description');
140 if ($descriptions->length) {
141 $description = $descriptions->item(0)->textContent;
142 $description = htmlspecialchars($description);
143 $description = nl2br($description);
144 return $description;
145 }
146 }
147 }
148 }
149 return '';
150 }
151
152 /**
153 * Get type
154 *
155 * @param string $typeId
156 * @return \DOMNode
157 */
158 protected function getType($typeId) {
159 $types = $this->xmlDoc->getElementsByTagName('type');
160 foreach ($types as $type) {
161 if ($type->getAttribute('id') == $typeId) {
162 return $type;
163 }
164 }
165 }
166
167 }
168
169
170 ?>