TYPO3 CMS  TYPO3_7-6
FormResultCompiler.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\Form;
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 
31 {
35  protected $extJSCODE = '';
36 
40  protected $hiddenFieldAccum = [];
41 
49  protected $doSaveFieldName = '';
50 
54  protected $inlineData = [];
55 
61  protected $stylesheetFiles = [];
62 
68  protected $additionalJS_post = [];
69 
76  protected $additionalJS_submit = [];
77 
84 
91  protected $requireJsModules = [];
92 
96  protected $pageRenderer = null;
97 
105  public function mergeResult(array $resultArray)
106  {
107  $this->doSaveFieldName = $resultArray['doSaveFieldName'];
108  foreach ($resultArray['additionalJavaScriptPost'] as $element) {
109  $this->additionalJS_post[] = $element;
110  }
111  foreach ($resultArray['additionalJavaScriptSubmit'] as $element) {
112  $this->additionalJS_submit[] = $element;
113  }
114  if (!empty($resultArray['requireJsModules'])) {
115  foreach ($resultArray['requireJsModules'] as $module) {
116  $moduleName = null;
117  $callback = null;
118  if (is_string($module)) {
119  // if $module is a string, no callback
120  $moduleName = $module;
121  $callback = null;
122  } elseif (is_array($module)) {
123  // if $module is an array, callback is possible
124  foreach ($module as $key => $value) {
125  $moduleName = $key;
126  $callback = $value;
127  break;
128  }
129  }
130  if ($moduleName !== null) {
131  if (!empty($this->requireJsModules[$moduleName]) && $callback !== null) {
132  $existingValue = $this->requireJsModules[$moduleName];
133  if (!is_array($existingValue)) {
134  $existingValue = [$existingValue];
135  }
136  $existingValue[] = $callback;
137  $this->requireJsModules[$moduleName] = $existingValue;
138  } else {
139  $this->requireJsModules[$moduleName] = $callback;
140  }
141  }
142  }
143  }
144  $this->extJSCODE = $this->extJSCODE . LF . $resultArray['extJSCODE'];
145  foreach ($resultArray['additionalHiddenFields'] as $element) {
146  $this->hiddenFieldAccum[] = $element;
147  }
148  foreach ($resultArray['stylesheetFiles'] as $stylesheetFile) {
149  if (!in_array($stylesheetFile, $this->stylesheetFiles)) {
150  $this->stylesheetFiles[] = $stylesheetFile;
151  }
152  }
153 
154  if (!empty($resultArray['inlineData'])) {
155  $resultArrayInlineData = $this->inlineData;
156  $resultInlineData = $resultArray['inlineData'];
157  ArrayUtility::mergeRecursiveWithOverrule($resultArrayInlineData, $resultInlineData);
158  $this->inlineData = $resultArrayInlineData;
159  }
160 
161  if (!empty($resultArray['additionalInlineLanguageLabelFiles'])) {
162  foreach ($resultArray['additionalInlineLanguageLabelFiles'] as $additionalInlineLanguageLabelFile) {
163  $this->additionalInlineLanguageLabelFiles[] = $additionalInlineLanguageLabelFile;
164  }
165  }
166  }
167 
173  public function JStop()
174  {
175  $stylesheetHtml = [];
176  foreach ($this->stylesheetFiles as $stylesheetFile) {
177  $stylesheetHtml[] = '<link rel="stylesheet" type="text/css" href="' . $stylesheetFile . '" />';
178  }
179  return implode(LF, $stylesheetHtml);
180  }
181 
189  public function printNeededJSFunctions()
190  {
191  // set variables to be accessible for JS
192  $pageRenderer = $this->getPageRenderer();
193  $pageRenderer->addInlineSetting('FormEngine', 'formName', 'editform');
194  $pageRenderer->addInlineSetting('FormEngine', 'backPath', '');
195 
196  return $this->JSbottom('editform');
197  }
198 
205  protected function JSbottom($formname = 'forms[0]')
206  {
207  $languageService = $this->getLanguageService();
208  $jsFile = [];
209 
210  // @todo: this is messy here - "additional hidden fields" should be handled elsewhere
211  $html = implode(LF, $this->hiddenFieldAccum);
212  $backendRelPath = ExtensionManagementUtility::extRelPath('backend');
213  $this->loadJavascriptLib($backendRelPath . 'Resources/Public/JavaScript/md5.js');
214  // load the main module for FormEngine with all important JS functions
215  $this->requireJsModules['TYPO3/CMS/Backend/FormEngine'] = 'function(FormEngine) {
216  FormEngine.initialize(
217  ' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('wizard_element_browser')) . ',
218  ' . ($GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? '1' : '0') . '
219  );
220  }';
221 
222  $pageRenderer = $this->getPageRenderer();
223  foreach ($this->requireJsModules as $moduleName => $callbacks) {
224  if (!is_array($callbacks)) {
225  $callbacks = [$callbacks];
226  }
227  foreach ($callbacks as $callback) {
228  $pageRenderer->loadRequireJsModule($moduleName, $callback);
229  }
230  }
231  $pageRenderer->loadJquery();
232  $pageRenderer->loadExtJS();
233  // Load tree stuff here
234  $pageRenderer->addJsFile($backendRelPath . 'Resources/Public/JavaScript/tree.js');
235  $pageRenderer->addInlineLanguageLabelFile(ExtensionManagementUtility::extPath('lang') . 'locallang_csh_corebe.xlf', 'tcatree');
236  $pageRenderer->addJsFile($backendRelPath . 'Resources/Public/JavaScript/notifications.js');
237  if (ExtensionManagementUtility::isLoaded('rtehtmlarea')) {
238  // This js addition is hackish ... it will always load this file even if not RTE
239  // is added here. But this simplifies RTE initialization a lot and is thus kept for now.
240  $pageRenderer->addJsFile(ExtensionManagementUtility::extRelPath('rtehtmlarea') . 'Resources/Public/JavaScript/HTMLArea/NameSpace/NameSpace.js');
241  }
242 
243  $beUserAuth = $this->getBackendUserAuthentication();
244  // Make textareas resizable and flexible ("autogrow" in height)
245  $textareaSettings = [
246  'autosize' => (bool)$beUserAuth->uc['resizeTextareas_Flexible']
247  ];
248  $pageRenderer->addInlineSettingArray('Textarea', $textareaSettings);
249 
250  $this->loadJavascriptLib($backendRelPath . 'Resources/Public/JavaScript/jsfunc.tbe_editor.js');
251  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ValueSlider');
252  // Needed for FormEngine manipulation (date picker)
253  $dateFormat = ($GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? ['MM-DD-YYYY', 'HH:mm MM-DD-YYYY'] : ['DD-MM-YYYY', 'HH:mm DD-MM-YYYY']);
254  $pageRenderer->addInlineSetting('DateTimePicker', 'DateFormat', $dateFormat);
255 
256  // support placeholders for IE9 and lower
257  $clientInfo = GeneralUtility::clientInfo();
258  if ($clientInfo['BROWSER'] == 'msie' && $clientInfo['VERSION'] <= 9) {
259  $this->loadJavascriptLib('sysext/core/Resources/Public/JavaScript/Contrib/placeholders.min.js');
260  }
261 
262  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Filelist/FileListLocalisation');
263 
264  $pageRenderer->addInlineLanguagelabelFile(
265  \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('lang') . 'locallang_core.xlf',
266  'file_upload'
267  );
268  if (!empty($this->additionalInlineLanguageLabelFiles)) {
269  foreach ($this->additionalInlineLanguageLabelFiles as $additionalInlineLanguageLabelFile) {
270  $pageRenderer->addInlineLanguageLabelFile($additionalInlineLanguageLabelFile);
271  }
272  }
273  // Load codemirror for T3Editor
274  if (ExtensionManagementUtility::isLoaded('t3editor')) {
275  $this->loadJavascriptLib(ExtensionManagementUtility::extRelPath('t3editor') . 'Resources/Public/JavaScript/Contrib/codemirror/js/codemirror.js');
276  }
277  // We want to load jQuery-ui inside our js. Enable this using requirejs.
278  $this->loadJavascriptLib($backendRelPath . 'Resources/Public/JavaScript/jsfunc.inline.js');
279  $out = '
280  inline.setNoTitleString(' . GeneralUtility::quoteJSvalue(BackendUtility::getNoRecordTitle(true)) . ');
281  ';
282 
283  $out .= '
284  TBE_EDITOR.formname = "' . $formname . '";
285  TBE_EDITOR.formnameUENC = "' . rawurlencode($formname) . '";
286  TBE_EDITOR.backPath = "";
287  TBE_EDITOR.isPalettedoc = null;
288  TBE_EDITOR.doSaveFieldName = "' . ($this->doSaveFieldName ? addslashes($this->doSaveFieldName) : '') . '";
289  TBE_EDITOR.labels.fieldsChanged = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.fieldsChanged')) . ';
290  TBE_EDITOR.labels.fieldsMissing = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.fieldsMissing')) . ';
291  TBE_EDITOR.labels.maxItemsAllowed = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.maxItemsAllowed')) . ';
292  TBE_EDITOR.labels.refresh_login = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:mess.refresh_login')) . ';
293  TBE_EDITOR.labels.refreshRequired = {};
294  TBE_EDITOR.labels.refreshRequired.title = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:mess.refreshRequired.title')) . ';
295  TBE_EDITOR.labels.refreshRequired.content = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:mess.refreshRequired.content')) . ';
296  TBE_EDITOR.labels.remainingCharacters = ' . GeneralUtility::quoteJSvalue($languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.remainingCharacters')) . ';
297  TBE_EDITOR.customEvalFunctions = {};
298  ';
299 
300  // Add JS required for inline fields
301  if (!empty($this->inlineData)) {
302  $out .= '
303  inline.addToDataArray(' . json_encode($this->inlineData) . ');
304  ';
305  }
306  // $this->additionalJS_submit:
307  if ($this->additionalJS_submit) {
308  $additionalJS_submit = implode('', $this->additionalJS_submit);
309  $additionalJS_submit = str_replace([CR, LF], '', $additionalJS_submit);
310  $out .= '
311  TBE_EDITOR.addActionChecks("submit", "' . addslashes($additionalJS_submit) . '");
312  ';
313  }
314  $out .= LF . implode(LF, $this->additionalJS_post) . LF . $this->extJSCODE;
315 
316  $spacer = LF . TAB;
317  $out = $html . $spacer . implode($spacer, $jsFile) . GeneralUtility::wrapJS($out);
318 
319  return $out;
320  }
321 
329  protected function loadJavascriptLib($lib)
330  {
331  $pageRenderer = $this->getPageRenderer();
332  $pageRenderer->addJsFile($lib);
333  }
334 
338  protected function getBackendUserAuthentication()
339  {
340  return $GLOBALS['BE_USER'];
341  }
342 
346  protected function getLanguageService()
347  {
348  return $GLOBALS['LANG'];
349  }
350 
356  protected function getPageRenderer()
357  {
358  if ($this->pageRenderer === null) {
359  $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
360  }
361  return $this->pageRenderer;
362  }
363 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static wrapJS($string, $linebreak=true)