‪TYPO3CMS  9.5
FormResultCompiler.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 
21 
28 {
32  protected ‪$hiddenFieldAccum = [];
33 
41  protected ‪$doSaveFieldName = '';
42 
46  protected ‪$inlineData = [];
47 
53  protected ‪$stylesheetFiles = [];
54 
60  protected ‪$additionalJavaScriptPost = [];
61 
69 
76 
83  protected ‪$requireJsModules = [];
84 
88  protected ‪$pageRenderer;
89 
96  public function ‪mergeResult(array $resultArray)
97  {
98  $this->doSaveFieldName = $resultArray['doSaveFieldName'];
99  foreach ($resultArray['additionalJavaScriptPost'] as $element) {
100  $this->additionalJavaScriptPost[] = $element;
101  }
102  foreach ($resultArray['additionalJavaScriptSubmit'] as $element) {
103  $this->additionalJavaScriptSubmit[] = $element;
104  }
105  if (!empty($resultArray['requireJsModules'])) {
106  foreach ($resultArray['requireJsModules'] as $module) {
107  $moduleName = null;
108  $callback = null;
109  if (is_string($module)) {
110  // if $module is a string, no callback
111  $moduleName = $module;
112  $callback = null;
113  } elseif (is_array($module)) {
114  // if $module is an array, callback is possible
115  foreach ($module as $key => $value) {
116  $moduleName = $key;
117  $callback = $value;
118  break;
119  }
120  }
121  if ($moduleName !== null) {
122  if (!empty($this->requireJsModules[$moduleName]) && $callback !== null) {
123  $existingValue = $this->requireJsModules[$moduleName];
124  if (!is_array($existingValue)) {
125  $existingValue = [$existingValue];
126  }
127  $existingValue[] = $callback;
128  $this->requireJsModules[$moduleName] = $existingValue;
129  } else {
130  $this->requireJsModules[$moduleName] = $callback;
131  }
132  }
133  }
134  }
135 
136  foreach ($resultArray['additionalHiddenFields'] as $element) {
137  $this->hiddenFieldAccum[] = $element;
138  }
139  foreach ($resultArray['stylesheetFiles'] as $stylesheetFile) {
140  if (!in_array($stylesheetFile, $this->stylesheetFiles)) {
141  $this->stylesheetFiles[] = $stylesheetFile;
142  }
143  }
144 
145  if (!empty($resultArray['inlineData'])) {
146  $resultArrayInlineData = ‪$this->inlineData;
147  $resultInlineData = $resultArray['inlineData'];
148  ‪ArrayUtility::mergeRecursiveWithOverrule($resultArrayInlineData, $resultInlineData);
149  $this->inlineData = $resultArrayInlineData;
150  }
151 
152  if (!empty($resultArray['additionalInlineLanguageLabelFiles'])) {
153  foreach ($resultArray['additionalInlineLanguageLabelFiles'] as $additionalInlineLanguageLabelFile) {
154  $this->additionalInlineLanguageLabelFiles[] = $additionalInlineLanguageLabelFile;
155  }
156  }
157  }
158 
164  public function ‪addCssFiles()
165  {
167  foreach ($this->stylesheetFiles as $stylesheetFile) {
168  ‪$pageRenderer->‪addCssFile($stylesheetFile);
169  }
170  return '';
171  }
172 
180  public function ‪printNeededJSFunctions()
181  {
182  // set variables to be accessible for JS
184  ‪$pageRenderer->‪addInlineSetting('FormEngine', 'formName', 'editform');
185 
186  return $this->‪JSbottom();
187  }
188 
194  protected function ‪JSbottom()
195  {
198  $uriBuilder = GeneralUtility::makeInstance(\‪TYPO3\CMS\Backend\Routing\UriBuilder::class);
199 
200  // @todo: this is messy here - "additional hidden fields" should be handled elsewhere
201  $html = implode(LF, $this->hiddenFieldAccum);
202  ‪$pageRenderer->‪addJsFile('EXT:backend/Resources/Public/JavaScript/md5.js');
203  // load the main module for FormEngine with all important JS functions
204  $this->requireJsModules['TYPO3/CMS/Backend/FormEngine'] = 'function(FormEngine) {
205  FormEngine.initialize(
206  ' . GeneralUtility::quoteJSvalue((string)$uriBuilder->buildUriFromRoute('wizard_element_browser')) . ',
207  ' . (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? '1' : '0') . '
208  );
209  }';
210  $this->requireJsModules['TYPO3/CMS/Backend/FormEngineReview'] = null;
211 
212  foreach ($this->requireJsModules as $moduleName => $callbacks) {
213  if (!is_array($callbacks)) {
214  $callbacks = [$callbacks];
215  }
216  foreach ($callbacks as $callback) {
217  ‪$pageRenderer->‪loadRequireJsModule($moduleName, $callback);
218  }
219  }
220  $backendUser = $this->‪getBackendUserAuthentication();
221 
222  // define the window size of the element browser etc.
223  $popupWindowWidth = 800;
224  $popupWindowHeight = 600;
225  $popupWindowSize = trim($backendUser->getTSConfig()['options.']['popupWindowSize'] ?? '');
226  if (!empty($popupWindowSize)) {
227  list($popupWindowWidth, $popupWindowHeight) = GeneralUtility::intExplode('x', $popupWindowSize);
228  }
229 
230  // define the window size of the popups within the RTE
231  $rtePopupWindowSize = trim($backendUser->getTSConfig()['options.']['rte.']['popupWindowSize'] ?? '');
232  if (!empty($rtePopupWindowSize)) {
233  list($rtePopupWindowWidth, $rtePopupWindowHeight) = GeneralUtility::trimExplode('x', $rtePopupWindowSize);
234  }
235  $rtePopupWindowWidth = !empty($rtePopupWindowWidth) ? (int)$rtePopupWindowWidth : $popupWindowWidth;
236  $rtePopupWindowHeight = !empty($rtePopupWindowHeight) ? (int)$rtePopupWindowHeight : $popupWindowHeight;
237 
238  // Make textareas resizable and flexible ("autogrow" in height)
239  $textareaSettings = [
240  'autosize' => (bool)$backendUser->uc['resizeTextareas_Flexible'],
241  'RTEPopupWindow' => [
242  'width' => $rtePopupWindowWidth,
243  'height' => $rtePopupWindowHeight
244  ]
245  ];
246  ‪$pageRenderer->‪addInlineSettingArray('Textarea', $textareaSettings);
247 
248  $popupSettings = [
249  'PopupWindow' => [
250  'width' => $popupWindowWidth,
251  'height' => $popupWindowHeight
252  ]
253  ];
254  ‪$pageRenderer->‪addInlineSettingArray('Popup', $popupSettings);
255 
256  ‪$pageRenderer->‪addJsFile('EXT:backend/Resources/Public/JavaScript/jsfunc.tbe_editor.js');
257  // Needed for FormEngine manipulation (date picker)
258  $dateFormat = (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ? ['MM-DD-YYYY', 'HH:mm MM-DD-YYYY'] : ['DD-MM-YYYY', 'HH:mm DD-MM-YYYY']);
259  ‪$pageRenderer->‪addInlineSetting('DateTimePicker', 'DateFormat', $dateFormat);
260 
261  ‪$pageRenderer->‪loadRequireJsModule('TYPO3/CMS/Filelist/FileListLocalisation');
262 
263  ‪$pageRenderer->‪addInlineLanguageLabelFile('EXT:core/Resources/Private/Language/locallang_core.xlf', 'file_upload');
264  if (!empty($this->additionalInlineLanguageLabelFiles)) {
265  foreach ($this->additionalInlineLanguageLabelFiles as $additionalInlineLanguageLabelFile) {
266  ‪$pageRenderer->‪addInlineLanguageLabelFile($additionalInlineLanguageLabelFile);
267  }
268  }
269  // @TODO: Refactor jsfunc.inline.js to an AMD module / TypsScript. If this is done, remove loadJquery() call.
271  ‪$pageRenderer->‪addJsFile('EXT:backend/Resources/Public/JavaScript/jsfunc.inline.js');
272 
273  // todo: change these things in JS
275  'FormEngine.noRecordTitle' => $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.no_title'),
276  'FormEngine.fieldsChanged' => $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.fieldsChanged'),
277  'FormEngine.fieldsMissing' => $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.fieldsMissing'),
278  'FormEngine.maxItemsAllowed' => $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.maxItemsAllowed'),
279  'FormEngine.refreshRequiredTitle' => $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:mess.refreshRequired.title'),
280  'FormEngine.refreshRequiredContent' => $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:mess.refreshRequired.content'),
281  'FormEngine.remainingCharacters' => $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.remainingCharacters'),
282  ]);
283 
284  $out = LF . 'TBE_EDITOR.doSaveFieldName = "' . ($this->doSaveFieldName ? addslashes($this->doSaveFieldName) : '') . '";';
285 
286  // Add JS required for inline fields
287  if (!empty($this->inlineData)) {
288  $out .= LF . 'inline.addToDataArray(' . json_encode($this->inlineData) . ');';
289  }
290  // $this->additionalJS_submit:
291  if ($this->additionalJavaScriptSubmit) {
292  $additionalJS_submit = implode('', $this->additionalJavaScriptSubmit);
293  $additionalJS_submit = str_replace([CR, LF], '', $additionalJS_submit);
294  $out .= 'TBE_EDITOR.addActionChecks("submit", "' . addslashes($additionalJS_submit) . '");';
295  }
296  $out .= LF . implode(LF, $this->additionalJavaScriptPost);
297 
298  return $html . LF . "\t" . GeneralUtility::wrapJS($out);
299  }
300 
304  protected function ‪getBackendUserAuthentication()
305  {
306  return ‪$GLOBALS['BE_USER'];
307  }
308 
314  protected function ‪getLanguageService()
315  {
316  return ‪$GLOBALS['LANG'];
317  }
318 
324  protected function ‪getPageRenderer()
325  {
326  if ($this->pageRenderer === null) {
327  $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
328  }
329  return ‪$this->pageRenderer;
330  }
331 }
‪TYPO3\CMS\Backend\Form\FormResultCompiler\$inlineData
‪array $inlineData
Definition: FormResultCompiler.php:43
‪TYPO3\CMS\Core\Page\PageRenderer\addInlineLanguageLabelFile
‪addInlineLanguageLabelFile($fileRef, $selectionPrefix='', $stripFromSelectionName='')
Definition: PageRenderer.php:1678
‪TYPO3\CMS\Backend\Form\FormResultCompiler\getPageRenderer
‪TYPO3 CMS Core Page PageRenderer getPageRenderer()
Definition: FormResultCompiler.php:315
‪TYPO3\CMS\Backend\Form\FormResultCompiler
Definition: FormResultCompiler.php:28
‪TYPO3\CMS\Backend\Form\FormResultCompiler\$additionalInlineLanguageLabelFiles
‪array $additionalInlineLanguageLabelFiles
Definition: FormResultCompiler.php:68
‪TYPO3\CMS\Backend\Form
Definition: AbstractNode.php:3
‪TYPO3\CMS\Backend\Form\FormResultCompiler\JSbottom
‪string JSbottom()
Definition: FormResultCompiler.php:185
‪TYPO3\CMS\Backend\Form\FormResultCompiler\mergeResult
‪mergeResult(array $resultArray)
Definition: FormResultCompiler.php:87
‪TYPO3
‪TYPO3\CMS\Backend\Form\FormResultCompiler\getLanguageService
‪TYPO3 CMS Core Localization LanguageService getLanguageService()
Definition: FormResultCompiler.php:305
‪TYPO3\CMS\Core\Utility\ArrayUtility\mergeRecursiveWithOverrule
‪static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
Definition: ArrayUtility.php:614
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:158
‪TYPO3\CMS\Core\Page\PageRenderer\loadRequireJsModule
‪loadRequireJsModule($mainModuleName, $callBackFunction=null)
Definition: PageRenderer.php:1593
‪TYPO3\CMS\Core\Page\PageRenderer\addInlineSettingArray
‪addInlineSettingArray($namespace, array $array)
Definition: PageRenderer.php:1724
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:35
‪TYPO3\CMS\Core\Page\PageRenderer\addCssFile
‪addCssFile($file, $rel='stylesheet', $media='all', $title='', $compress=true, $forceOnTop=false, $allWrap='', $excludeFromConcatenation=false, $splitChar='|', $inline=false)
Definition: PageRenderer.php:1272
‪TYPO3\CMS\Backend\Form\FormResultCompiler\addCssFiles
‪string addCssFiles()
Definition: FormResultCompiler.php:155
‪TYPO3\CMS\Backend\Form\FormResultCompiler\$stylesheetFiles
‪array $stylesheetFiles
Definition: FormResultCompiler.php:49
‪TYPO3\CMS\Backend\Form\FormResultCompiler\$hiddenFieldAccum
‪array $hiddenFieldAccum
Definition: FormResultCompiler.php:31
‪TYPO3\CMS\Backend\Form\FormResultCompiler\$doSaveFieldName
‪string $doSaveFieldName
Definition: FormResultCompiler.php:39
‪TYPO3\CMS\Backend\Form\FormResultCompiler\$additionalJavaScriptSubmit
‪array $additionalJavaScriptSubmit
Definition: FormResultCompiler.php:62
‪TYPO3\CMS\Core\Page\PageRenderer\addInlineLanguageLabelArray
‪addInlineLanguageLabelArray(array $array, $parseWithLanguageService=null)
Definition: PageRenderer.php:1653
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Backend\Form\FormResultCompiler\$requireJsModules
‪array $requireJsModules
Definition: FormResultCompiler.php:75
‪TYPO3\CMS\Core\Page\PageRenderer\JQUERY_NAMESPACE_NONE
‪const JQUERY_NAMESPACE_NONE
Definition: PageRenderer.php:45
‪TYPO3\CMS\Backend\Form\FormResultCompiler\$additionalJavaScriptPost
‪array $additionalJavaScriptPost
Definition: FormResultCompiler.php:55
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Page\PageRenderer\addInlineSetting
‪addInlineSetting($namespace, $key, $value)
Definition: PageRenderer.php:1698
‪TYPO3\CMS\Core\Page\PageRenderer\loadJquery
‪loadJquery($version=null, $source=null, $namespace=self::JQUERY_NAMESPACE_NONE, bool $isCoreCall=false)
Definition: PageRenderer.php:1351
‪TYPO3\CMS\Backend\Form\FormResultCompiler\$pageRenderer
‪PageRenderer $pageRenderer
Definition: FormResultCompiler.php:79
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Form\FormResultCompiler\printNeededJSFunctions
‪string printNeededJSFunctions()
Definition: FormResultCompiler.php:171
‪TYPO3\CMS\Core\Page\PageRenderer\addJsFile
‪addJsFile($file, $type='text/javascript', $compress=true, $forceOnTop=false, $allWrap='', $excludeFromConcatenation=false, $splitChar='|', $async=false, $integrity='', $defer=false, $crossorigin='')
Definition: PageRenderer.php:1157
‪TYPO3\CMS\Backend\Form\FormResultCompiler\getBackendUserAuthentication
‪BackendUserAuthentication getBackendUserAuthentication()
Definition: FormResultCompiler.php:295