TYPO3 CMS  TYPO3_7-6
T3editorElement.php
Go to the documentation of this file.
1 <?php
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 
24 
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 
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 
56  protected $resultArray;
57 
61  protected $mode = '';
62 
68  protected $editorCounter = 0;
69 
75  protected $relExtPath = '';
76 
80  protected $codemirrorPath = 'Resources/Public/JavaScript/Contrib/codemirror/js/';
81 
87  protected $codeCompletionComponents = ['TsRef', 'CompletionResult', 'TsParser', 'TsCodeCompletion'];
88 
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 
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 
151  public function getMode()
152  {
153  return $this->mode;
154  }
155 
159  protected function initJavascriptCode()
160  {
161  $this->resultArray['stylesheetFiles'][] = $this->relExtPath . '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 
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], '', GeneralUtility::getUrl(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 
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 
261  protected function getStylesheetByMode($mode)
262  {
263  switch ($mode) {
264  case self::MODE_TYPOSCRIPT:
265  $stylesheet = [$this->relExtPath . 'Resources/Public/Css/typoscriptcolors.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 
298  protected function getLanguageService()
299  {
300  return $GLOBALS['LANG'];
301  }
302 
306  protected function getBackendUserAuthentication()
307  {
308  return $GLOBALS['BE_USER'];
309  }
310 }
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
getHTMLCodeForEditor($name, $class='', $content='', $additionalParams='', $alt='', array $hiddenfields=[])
static getUrl($url, $includeHeader=0, $requestHeaders=false, &$report=null)
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']