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