[TASK] update the comments regarding ajax.php
[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 use Psr\Http\Message\ResponseInterface;
17 use Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Loads TSref information from a XML file an responds to an AJAX call.
22 */
23 class TypoScriptReferenceLoader {
24
25 /**
26 * @var \DOMDocument
27 */
28 protected $xmlDoc;
29
30 /**
31 * Default constructor
32 */
33 public function __construct() {
34 $GLOBALS['LANG']->includeLLFile('EXT:t3editor/Resources/Private/Language/locallang.xlf');
35 }
36
37 /**
38 * General processor for AJAX requests.
39 * Called by AjaxRequestHandler
40 *
41 * @param ServerRequestInterface $request
42 * @param ResponseInterface $response
43 * @return ResponseInterface
44 */
45 public function processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response) {
46 $parsedBody = $request->getParsedBody();
47 $queryParams = $request->getQueryParams();
48
49 // Load the TSref XML information:
50 $this->loadFile(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('t3editor') . 'Resources/Private/tsref.xml');
51 $fetch = isset($parsedBody['fetch']) ? $parsedBody['fetch'] : $queryParams['fetch'];
52 $content = null;
53
54 switch ($fetch) {
55 case 'types':
56 $response->getBody()->write(json_encode($this->getTypes()));
57 break;
58 case 'description':
59 $typeId = isset($parsedBody['typeId']) ? $parsedBody['typeId'] : $queryParams['typeId'];
60 $parameterName = isset($parsedBody['parameterName']) ? $parsedBody['parameterName'] : $queryParams['parameterName'];
61 $response = $this->getDescription($typeId, $parameterName);
62 $response->withHeader('Content-Type', 'text/html; charset=utf8');
63 break;
64 }
65 return $response;
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 ResponseInterface
119 */
120 protected function getDescription($typeId, $parameterName = '') {
121 /** @var \TYPO3\CMS\Core\Http\Response $response */
122 $response = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Http\Response::class);
123 if (!$typeId) {
124 $response->getBody()->write($GLOBALS['LANG']->getLL('typeIDMissing'));
125 $response = $response->withStatus(500);
126 return $response;
127 }
128 // getElementById does only work with schema
129 $type = $this->getType($typeId);
130 // Retrieve propertyDescription
131 if ($parameterName) {
132 $properties = $type->getElementsByTagName('property');
133 foreach ($properties as $propery) {
134 $propName = $propery->getAttribute('name');
135 if ($propName == $parameterName) {
136 $descriptions = $propery->getElementsByTagName('description');
137 if ($descriptions->length) {
138 $description = $descriptions->item(0)->textContent;
139 $description = htmlspecialchars($description);
140 $description = nl2br($description);
141 $response->getBody()->write($description);
142 break;
143 }
144 }
145 }
146 }
147 return $response;
148 }
149
150 /**
151 * Get type
152 *
153 * @param string $typeId
154 * @return \DOMNode
155 */
156 protected function getType($typeId) {
157 $types = $this->xmlDoc->getElementsByTagName('type');
158 foreach ($types as $type) {
159 if ($type->getAttribute('id') == $typeId) {
160 return $type;
161 }
162 }
163 }
164
165 }