[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / TypoScriptReferenceLoader.php
1 <?php
2 namespace TYPO3\CMS\T3editor;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Loads TSref information from a XML file an responds to an AJAX call.
19 *
20 * @author Stephan Petzl <spetzl@gmx.at>
21 * @author Christian Kartnig <office@hahnepeter.de>
22 */
23 class TypoScriptReferenceLoader {
24
25 /**
26 * @var \DOMDocument
27 */
28 protected $xmlDoc;
29
30 /**
31 * @var \TYPO3\CMS\Core\Http\AjaxRequestHandler
32 */
33 protected $ajaxObj;
34
35 /**
36 * Default constructor
37 */
38 public function __construct() {
39 $GLOBALS['LANG']->includeLLFile('EXT:t3editor/locallang.xlf');
40 }
41
42 /**
43 * General processor for AJAX requests.
44 * (called by typo3/ajax.php)
45 *
46 * @param array $params Additional parameters (not used here)
47 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj The AjaxRequestHandler object of this request
48 * @return void
49 * @author Oliver Hader <oliver@typo3.org>
50 */
51 public function processAjaxRequest($params, \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj) {
52 $this->ajaxObj = $ajaxObj;
53 // Load the TSref XML information:
54 $this->loadFile(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('t3editor') . 'res/tsref/tsref.xml');
55 $ajaxIdParts = explode('::', $ajaxObj->getAjaxID(), 2);
56 $ajaxMethod = $ajaxIdParts[1];
57 $response = array();
58 // Process the AJAX requests:
59 if ($ajaxMethod == 'getTypes') {
60 $ajaxObj->setContent($this->getTypes());
61 $ajaxObj->setContentFormat('jsonbody');
62 } elseif ($ajaxMethod == 'getDescription') {
63 $ajaxObj->addContent('', $this->getDescription(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('typeId'), \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('parameterName')));
64 $ajaxObj->setContentFormat('plain');
65 }
66 }
67
68 /**
69 * Load XML file
70 *
71 * @param string $filepath
72 * @return void
73 */
74 protected function loadFile($filepath) {
75 $this->xmlDoc = new \DOMDocument('1.0', 'utf-8');
76 $this->xmlDoc->load($filepath);
77 // @TODO: oliver@typo3.org: I guess this is not required here
78 $this->xmlDoc->saveXML();
79 }
80
81 /**
82 * Get types from XML
83 *
84 * @return array
85 */
86 protected function getTypes() {
87 $types = $this->xmlDoc->getElementsByTagName('type');
88 $typeArr = array();
89 foreach ($types as $type) {
90 $typeId = $type->getAttribute('id');
91 $typeName = $type->getAttribute('name');
92 if (!$typeName) {
93 $typeName = $typeId;
94 }
95 $properties = $type->getElementsByTagName('property');
96 $propArr = array();
97 foreach ($properties as $property) {
98 $p = array();
99 $p['name'] = $property->getAttribute('name');
100 $p['type'] = $property->getAttribute('type');
101 $propArr[$property->getAttribute('name')] = $p;
102 }
103 $typeArr[$typeId] = array();
104 $typeArr[$typeId]['properties'] = $propArr;
105 $typeArr[$typeId]['name'] = $typeName;
106 if ($type->hasAttribute('extends')) {
107 $typeArr[$typeId]['extends'] = $type->getAttribute('extends');
108 }
109 }
110 return $typeArr;
111 }
112
113 /**
114 * Get description
115 *
116 * @param string $typeId
117 * @param string $parameterName
118 * @return string
119 */
120 protected function getDescription($typeId, $parameterName = '') {
121 if (!$typeId) {
122 $this->ajaxObj->setError($GLOBALS['LANG']->getLL('typeIDMissing'));
123 return '';
124 }
125 // getElementById does only work with schema
126 $type = $this->getType($typeId);
127 // Retrieve propertyDescription
128 if ($parameterName) {
129 $properties = $type->getElementsByTagName('property');
130 foreach ($properties as $propery) {
131 $propName = $propery->getAttribute('name');
132 if ($propName == $parameterName) {
133 $descriptions = $propery->getElementsByTagName('description');
134 if ($descriptions->length) {
135 $description = $descriptions->item(0)->textContent;
136 $description = htmlspecialchars($description);
137 $description = nl2br($description);
138 return $description;
139 }
140 }
141 }
142 }
143 return '';
144 }
145
146 /**
147 * Get type
148 *
149 * @param string $typeId
150 * @return \DOMNode
151 */
152 protected function getType($typeId) {
153 $types = $this->xmlDoc->getElementsByTagName('type');
154 foreach ($types as $type) {
155 if ($type->getAttribute('id') == $typeId) {
156 return $type;
157 }
158 }
159 }
160
161 }