[TASK] Remove inline JavaScript from Install Tool
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Classes / Controller / ElementBrowserController.php
1 <?php
2
3 /*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16 namespace TYPO3\CMS\Recordlist\Controller;
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Backend\Routing\UriBuilder;
21 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22 use TYPO3\CMS\Core\Http\HtmlResponse;
23 use TYPO3\CMS\Core\Http\RedirectResponse;
24 use TYPO3\CMS\Core\Localization\LanguageService;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Recordlist\Browser\ElementBrowserInterface;
27
28 /**
29 * Script class for the Element Browser window.
30 * @internal This class is a specific Backend controller implementation and is not part of the TYPO3's Core API.
31 */
32 class ElementBrowserController
33 {
34 /**
35 * The mode determines the main kind of output of the element browser.
36 *
37 * There are these options for values:
38 * - "db" will allow you to browse for pages or records in the page tree for FormEngine select fields
39 * - "file" will allow you to browse for files in the folder mounts for FormEngine file selections
40 * - "folder" will allow you to browse for folders in the folder mounts for FormEngine folder selections
41 * - Other options may be registered via extensions
42 *
43 * @var string
44 */
45 protected string $mode = '';
46
47 /**
48 * Injects the request object for the current request or sub-request
49 * As this controller goes only through the main() method, it is rather simple for now
50 *
51 * @param ServerRequestInterface $request the current request
52 * @return ResponseInterface the response with the content
53 */
54 public function mainAction(ServerRequestInterface $request): ResponseInterface
55 {
56 $this->getLanguageService()->includeLLFile('EXT:recordlist/Resources/Private/Language/locallang_browse_links.xlf');
57 $this->mode = $request->getQueryParams()['mode'] ?? $request->getQueryParams()['mode'] ?? '';
58 // Fallback for old calls, which use mode "wizard" or "rte" for link selection
59 if ($this->mode === 'wizard' || $this->mode === 'rte') {
60 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
61 return new RedirectResponse((string)$uriBuilder->buildUriFromRoute('wizard_link', $_GET), 303);
62 }
63 return new HtmlResponse($this->main($request));
64 }
65
66 /**
67 * Main function, detecting the current mode of the element browser and branching out to internal methods.
68 *
69 * @return string HTML content
70 */
71 protected function main(ServerRequestInterface $request)
72 {
73 $content = '';
74
75 // Render type by user func
76 $browserRendered = false;
77 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/browse_links.php']['browserRendering'] ?? [] as $className) {
78 $browserRenderObj = GeneralUtility::makeInstance($className);
79 if (is_object($browserRenderObj) && method_exists($browserRenderObj, 'isValid') && method_exists($browserRenderObj, 'render')) {
80 if ($browserRenderObj->isValid($this->mode, $this)) {
81 $content = $browserRenderObj->render($this->mode, $this);
82 $browserRendered = true;
83 break;
84 }
85 }
86 }
87
88 // if type was not rendered use default rendering functions
89 if (!$browserRendered) {
90 $browser = $this->getElementBrowserInstance();
91 if (is_callable([$browser, 'setRequest'])) {
92 $browser->setRequest($request);
93 }
94
95 $backendUser = $this->getBackendUser();
96 $modData = $backendUser->getModuleData('browse_links.php', 'ses');
97 [$modData] = $browser->processSessionData($modData);
98 $backendUser->pushModuleData('browse_links.php', $modData);
99
100 $content = $browser->render();
101 }
102
103 return $content;
104 }
105
106 /**
107 * Get instance of the actual element browser
108 *
109 * This method shall be overwritten in subclasses
110 *
111 * @return ElementBrowserInterface
112 * @throws \UnexpectedValueException
113 */
114 protected function getElementBrowserInstance()
115 {
116 $className = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ElementBrowsers'][$this->mode];
117 $browser = GeneralUtility::makeInstance($className);
118 if (!$browser instanceof ElementBrowserInterface) {
119 throw new \UnexpectedValueException('The specified element browser "' . $className . '" does not implement the required ElementBrowserInterface', 1442763890);
120 }
121 return $browser;
122 }
123
124 /**
125 * @return LanguageService
126 */
127 protected function getLanguageService()
128 {
129 return $GLOBALS['LANG'];
130 }
131
132 /**
133 * @return BackendUserAuthentication
134 */
135 protected function getBackendUser()
136 {
137 return $GLOBALS['BE_USER'];
138 }
139 }