Revert "[FEATURE] Use FormEngine in Template and Filelist modules"
[Packages/TYPO3.CMS.git] / typo3 / sysext / t3editor / Classes / Form / Element / T3editorElement.php
1 <?php
2 namespace TYPO3\CMS\T3editor\Form\Element;
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 use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\MathUtility;
22 use TYPO3\CMS\Lang\LanguageService;
23 use TYPO3\CMS\T3editor\T3editor;
24
25 /**
26 * t3editor FormEngine widget
27 */
28 class T3editorElement extends AbstractFormElement
29 {
30 const MODE_CSS = 'css';
31 const MODE_HTML = 'html';
32 const MODE_JAVASCRIPT = 'javascript';
33 const MODE_MIXED = 'mixed';
34 const MODE_PHP = 'php';
35 const MODE_SPARQL = 'sparql';
36 const MODE_TYPOSCRIPT = 'typoscript';
37 const MODE_XML = 'xml';
38
39 /**
40 * @var array
41 */
42 protected $allowedModes = [
43 self::MODE_CSS,
44 self::MODE_HTML,
45 self::MODE_JAVASCRIPT,
46 self::MODE_MIXED,
47 self::MODE_PHP,
48 self::MODE_SPARQL,
49 self::MODE_TYPOSCRIPT,
50 self::MODE_XML,
51 ];
52
53 /**
54 * @var array
55 */
56 protected $resultArray;
57
58 /**
59 * @var string
60 */
61 protected $mode = '';
62
63 /**
64 * Counts the editors on the current page
65 *
66 * @var int
67 */
68 protected $editorCounter = 0;
69
70 /**
71 * Relative path to EXT:t3editor
72 *
73 * @var string
74 */
75 protected $relExtPath = '';
76
77 /**
78 * @var string
79 */
80 protected $codemirrorPath = 'Resources/Public/JavaScript/Contrib/codemirror/js/';
81
82 /**
83 * RequireJS modules loaded for code completion
84 *
85 * @var array
86 */
87 protected $codeCompletionComponents = ['TsRef', 'CompletionResult', 'TsParser', 'TsCodeCompletion'];
88
89 /**
90 * Render t3editor element
91 *
92 * @return array As defined in initializeResultArray() of AbstractNode
93 */
94 public function render()
95 {
96 $this->getLanguageService()->includeLLFile('EXT:t3editor/Resources/Private/Language/locallang.xlf');
97 $this->relExtPath = ExtensionManagementUtility::extRelPath('t3editor');
98 $this->codemirrorPath = $this->relExtPath . $this->codemirrorPath;
99
100 $this->resultArray = $this->initializeResultArray();
101
102 $parameterArray = $this->data['parameterArray'];
103
104 $rows = MathUtility::forceIntegerInRange($parameterArray['fieldConf']['config']['rows'] ?: 10, 1, 40);
105 $this->setMode(isset($parameterArray['fieldConf']['config']['format']) ? $parameterArray['fieldConf']['config']['format'] : T3editor::MODE_MIXED);
106
107 $attributes = [];
108 $attributes['rows'] = $rows;
109 $attributes['wrap'] = 'off';
110 $attributes['style'] = 'width:100%;';
111 $attributes['onchange'] = GeneralUtility::quoteJSvalue($parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged']);
112
113 $attributeString = '';
114 foreach ($attributes as $param => $value) {
115 $attributeString .= $param . '="' . htmlspecialchars($value) . '" ';
116 }
117
118 $this->resultArray['html'] = $this->getHTMLCodeForEditor(
119 $parameterArray['itemFormElName'],
120 'text-monospace enable-tab',
121 $parameterArray['itemFormElValue'],
122 $attributeString,
123 $this->data['tableName'] . ' > ' . $this->data['fieldName'],
124 ['target' => 0]
125 );
126 $this->resultArray['additionalJavaScriptPost'][] = 'require(["TYPO3/CMS/T3editor/T3editor"], function(T3editor) {T3editor.findAndInitializeEditors();});';
127
128 $this->initJavascriptCode();
129 return $this->resultArray;
130 }
131
132 /**
133 * Sets the type of code to edit, use one of the predefined constants
134 *
135 * @param string $mode Expects one of the predefined constants
136 * @throws \InvalidArgumentException
137 */
138 public function setMode($mode)
139 {
140 if (!in_array($mode, $this->allowedModes, true)) {
141 throw new \InvalidArgumentException($mode . 'is not allowed', 1438352574);
142 }
143 $this->mode = $mode;
144 }
145
146 /**
147 * Get mode
148 *
149 * @return string
150 */
151 public function getMode()
152 {
153 return $this->mode;
154 }
155
156 /**
157 * Init the JavaScript code (header part) for editor
158 */
159 protected function initJavascriptCode()
160 {
161 $this->resultArray['stylesheetFiles'][] = 'EXT:t3editor/Resources/Public/Css/t3editor.css';
162 if ($this->mode === self::MODE_TYPOSCRIPT) {
163 foreach ($this->codeCompletionComponents as $codeCompletionComponent) {
164 $this->resultArray['requireJsModules'][] = 'TYPO3/CMS/T3editor/Plugins/CodeCompletion/' . $codeCompletionComponent;
165 }
166 }
167 }
168
169 /**
170 * Generates HTML with code editor
171 *
172 * @param string $name Name attribute of HTML tag
173 * @param string $class Class attribute of HTML tag
174 * @param string $content Content of the editor
175 * @param string $additionalParams Any additional editor parameters
176 * @param string $alt Alt attribute
177 * @param array $hiddenfields
178 * @return string Generated HTML code for editor
179 */
180 protected function getHTMLCodeForEditor($name, $class = '', $content = '', $additionalParams = '', $alt = '', array $hiddenfields = [])
181 {
182 $code = [];
183 $attributes = [];
184 $attributes['class'] = $class . ' t3editor';
185 $attributes['alt'] = $alt;
186 $attributes['id'] = 't3editor_' . $this->editorCounter;
187 $attributes['name'] = $name;
188 $attributes['data-labels'] = json_encode($this->getLanguageService()->getLabelsWithPrefix('js.', 'label_'));
189 $attributes['data-instance-number'] = $this->editorCounter;
190 $attributes['data-editor-path'] = $this->relExtPath;
191 $attributes['data-codemirror-path'] = $this->codemirrorPath;
192 $attributes['data-ajaxsavetype'] = ''; // no ajax save in FormEngine at the moment
193 $attributes['data-parserfile'] = $this->getParserfileByMode($this->mode);
194 $attributes['data-stylesheet'] = $this->getStylesheetByMode($this->mode);
195
196 $attributesString = '';
197 foreach ($attributes as $attribute => $value) {
198 $attributesString .= $attribute . '="' . htmlspecialchars($value) . '" ';
199 }
200 $attributesString .= $additionalParams;
201
202 $code[] = '<div class="t3editor">';
203 $code[] = ' <div class="t3e_wrap">';
204 $code[] = str_replace([CR, LF], '', file_get_contents(GeneralUtility::getFileAbsFileName('EXT:t3editor/Resources/Private/Templates/t3editor.html')));
205 $code[] = ' </div>';
206 $code[] = ' <textarea ' . $attributesString . '>' . htmlspecialchars($content) . '</textarea>';
207 $code[] = '</div>';
208
209 if (!empty($hiddenfields)) {
210 foreach ($hiddenfields as $name => $value) {
211 $code[] = '<input type="hidden" name="' . htmlspecialchars($name) . '" value="' . htmlspecialchars($value) . '" />';
212 }
213 }
214 $this->editorCounter++;
215 return implode(LF, $code);
216 }
217
218 /**
219 * Determine the correct parser js file for given mode
220 *
221 * @param string $mode
222 * @return string Parser file name
223 */
224 protected function getParserfileByMode($mode)
225 {
226 $parserfile = [];
227 switch ($mode) {
228 case self::MODE_TYPOSCRIPT:
229 $relPath = '../../../parse_typoscript/';
230 $parserfile = [$relPath . 'tokenizetyposcript.js', $relPath . 'parsetyposcript.js'];
231 break;
232 case self::MODE_JAVASCRIPT:
233 $parserfile = ['tokenizejavascript.js', 'parsejavascript.js'];
234 break;
235 case self::MODE_CSS:
236 $parserfile = ['parsecss.js'];
237 break;
238 case self::MODE_XML:
239 $parserfile = ['parsexml.js'];
240 break;
241 case self::MODE_SPARQL:
242 $parserfile = ['parsesparql.js'];
243 break;
244 case self::MODE_HTML:
245 $parserfile = ['tokenizejavascript.js', 'parsejavascript.js', 'parsecss.js', 'parsexml.js', 'parsehtmlmixed.js'];
246 break;
247 case self::MODE_PHP:
248 case self::MODE_MIXED:
249 $parserfile = ['tokenizejavascript.js', 'parsejavascript.js', 'parsecss.js', 'parsexml.js', '../contrib/php/js/tokenizephp.js', '../contrib/php/js/parsephp.js', '../contrib/php/js/parsephphtmlmixed.js'];
250 break;
251 }
252 return json_encode($parserfile);
253 }
254
255 /**
256 * Determine the correct css file for given mode
257 *
258 * @param string $mode
259 * @return string css file name
260 */
261 protected function getStylesheetByMode($mode)
262 {
263 switch ($mode) {
264 case self::MODE_TYPOSCRIPT:
265 $stylesheet = [$this->relExtPath . 'Resources/Public/Css/t3editor_typoscript_colors.css'];
266 break;
267 case self::MODE_JAVASCRIPT:
268 $stylesheet = [$this->codemirrorPath . '../css/jscolors.css'];
269 break;
270 case self::MODE_CSS:
271 $stylesheet = [$this->codemirrorPath . '../css/csscolors.css'];
272 break;
273 case self::MODE_XML:
274 $stylesheet = [$this->codemirrorPath . '../css/xmlcolors.css'];
275 break;
276 case self::MODE_HTML:
277 $stylesheet = [$this->codemirrorPath . '../css/xmlcolors.css', $this->codemirrorPath . '../css/jscolors.css', $this->codemirrorPath . '../css/csscolors.css'];
278 break;
279 case self::MODE_SPARQL:
280 $stylesheet = [$this->codemirrorPath . '../css/sparqlcolors.css'];
281 break;
282 case self::MODE_PHP:
283 $stylesheet = [$this->codemirrorPath . '../contrib/php/css/phpcolors.css'];
284 break;
285 case self::MODE_MIXED:
286 $stylesheet = [$this->codemirrorPath . '../css/xmlcolors.css', $this->codemirrorPath . '../css/jscolors.css', $this->codemirrorPath . '../css/csscolors.css', $this->codemirrorPath . '../contrib/php/css/phpcolors.css'];
287 break;
288 default:
289 $stylesheet = [];
290 }
291 $stylesheet[] = $this->relExtPath . 'Resources/Public/Css/t3editor_inner.css';
292 return json_encode($stylesheet);
293 }
294
295 /**
296 * @return LanguageService
297 */
298 protected function getLanguageService()
299 {
300 return $GLOBALS['LANG'];
301 }
302
303 /**
304 * @return BackendUserAuthentication
305 */
306 protected function getBackendUserAuthentication()
307 {
308 return $GLOBALS['BE_USER'];
309 }
310 }