[TASK] Use ServerRequestInterface in LogoutController
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / AbstractFormEngineAjaxController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Localization\LocalizationFactory;
19 use TYPO3\CMS\Core\Utility\ArrayUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\PathUtility;
22
23 /**
24 * Abstract class for a couple of FormEngine controllers triggered by
25 * ajax calls. The class containers some helpers to for instance prepare
26 * the form render result for json output.
27 *
28 * @internal Marked as internal for now, methods in this class may change any time.
29 */
30 abstract class AbstractFormEngineAjaxController
31 {
32 /**
33 * Gets result array from FormEngine and returns string with js modules
34 * that need to be loaded and evaluated by JavaScript.
35 *
36 * @param array $result
37 * @return array
38 */
39 protected function createExecutableStringRepresentationOfRegisteredRequireJsModules(array $result): array
40 {
41 if (empty($result['requireJsModules'])) {
42 return [];
43 }
44 $requireJs = [];
45 foreach ($result['requireJsModules'] as $module) {
46 $moduleName = null;
47 $callback = null;
48 if (is_string($module)) {
49 // if $module is a string, no callback
50 $moduleName = $module;
51 $callback = null;
52 } elseif (is_array($module)) {
53 // if $module is an array, callback is possible
54 foreach ($module as $key => $value) {
55 $moduleName = $key;
56 $callback = $value;
57 break;
58 }
59 }
60 if ($moduleName !== null) {
61 $inlineCodeKey = $moduleName;
62 $javaScriptCode = 'require(["' . $moduleName . '"]';
63 if ($callback !== null) {
64 $inlineCodeKey .= sha1($callback);
65 $javaScriptCode .= ', ' . $callback;
66 }
67 $javaScriptCode .= ');';
68 $requireJs[] = '/*RequireJS-Module-' . $inlineCodeKey . '*/' . LF . $javaScriptCode;
69 }
70 }
71 return $requireJs;
72 }
73
74 /**
75 * Resolve a CSS file position, possibly prefixed with 'EXT:'
76 *
77 * @param string $stylesheetFile Given file, possibly prefixed with EXT:
78 * @return string Web root relative position to file
79 */
80 protected function getRelativePathToStylesheetFile(string $stylesheetFile): string
81 {
82 if (strpos($stylesheetFile, 'EXT:') === 0) {
83 $stylesheetFile = GeneralUtility::getFileAbsFileName($stylesheetFile);
84 $stylesheetFile = PathUtility::getRelativePathTo($stylesheetFile);
85 $stylesheetFile = rtrim($stylesheetFile, '/');
86 } else {
87 $stylesheetFile = GeneralUtility::resolveBackPath($stylesheetFile);
88 }
89 $stylesheetFile = GeneralUtility::createVersionNumberedFilename($stylesheetFile);
90 return PathUtility::getAbsoluteWebPath($stylesheetFile);
91 }
92
93 /**
94 * Parse a language file and get a label/value array from it.
95 *
96 * @param string $file EXT:path/to/file
97 * @return array Label/value array
98 */
99 protected function getLabelsFromLocalizationFile($file)
100 {
101 /** @var $languageFactory LocalizationFactory */
102 $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
103 $language = $GLOBALS['LANG']->lang;
104 $localizationArray = $languageFactory->getParsedData($file, $language);
105 if (is_array($localizationArray) && !empty($localizationArray)) {
106 if (!empty($localizationArray[$language])) {
107 $xlfLabelArray = $localizationArray['default'];
108 ArrayUtility::mergeRecursiveWithOverrule($xlfLabelArray, $localizationArray[$language], true, false);
109 } else {
110 $xlfLabelArray = $localizationArray['default'];
111 }
112 } else {
113 $xlfLabelArray = [];
114 }
115 $labelArray = [];
116 foreach ($xlfLabelArray as $key => $value) {
117 if (isset($value[0]['target'])) {
118 $labelArray[$key] = $value[0]['target'];
119 } else {
120 $labelArray[$key] = '';
121 }
122 }
123 return $labelArray;
124 }
125 }