[TASK] Use BE Routing / PSR-7 instead of BackendUtility::getModuleUrl
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Classes / Controller / ElementBrowserController.php
1 <?php
2 namespace TYPO3\CMS\Recordlist\Controller;
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
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Template\DocumentTemplate;
20 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21 use TYPO3\CMS\Core\Localization\LanguageService;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Recordlist\Browser\ElementBrowserInterface;
24
25 /**
26 * Script class for the Element Browser window.
27 */
28 class ElementBrowserController
29 {
30 /**
31 * The mode determines the main kind of output of the element browser.
32 *
33 * There are these options for values:
34 * - "db" will allow you to browse for pages or records in the page tree for FormEngine select fields
35 * - "file" will allow you to browse for files in the folder mounts for FormEngine file selections
36 * - "folder" will allow you to browse for folders in the folder mounts for FormEngine folder selections
37 * - Other options may be registered via extensions
38 *
39 * @var string
40 */
41 protected $mode;
42
43 /**
44 * Document template object
45 *
46 * @var DocumentTemplate
47 */
48 public $doc;
49
50 /**
51 * Constructor
52 */
53 public function __construct()
54 {
55 $GLOBALS['SOBE'] = $this;
56
57 // Creating backend template object:
58 // this might not be needed but some classes refer to $GLOBALS['SOBE']->doc, so ...
59 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
60
61 $this->init();
62 }
63
64 /**
65 * Initialize the controller
66 */
67 protected function init()
68 {
69 $this->getLanguageService()->includeLLFile('EXT:lang/Resources/Private/Language/locallang_browse_links.xlf');
70
71 $this->mode = GeneralUtility::_GP('mode');
72 }
73
74 /**
75 * Injects the request object for the current request or sub-request
76 * As this controller goes only through the main() method, it is rather simple for now
77 *
78 * @param ServerRequestInterface $request the current request
79 * @param ResponseInterface $response the prepared response object
80 * @return ResponseInterface the response with the content
81 */
82 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
83 {
84 // Fallback for old calls, which use mode "wizard" or "rte" for link selection
85 if ($this->mode === 'wizard' || $this->mode === 'rte') {
86 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
87 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
88 return $response->withStatus(303)->withHeader('Location', (string)$uriBuilder->buildUriFromRoute('wizard_link', $_GET));
89 }
90
91 $response->getBody()->write($this->main());
92 return $response;
93 }
94
95 /**
96 * Main function, detecting the current mode of the element browser and branching out to internal methods.
97 *
98 * @return string HTML content
99 */
100 public function main()
101 {
102 $content = '';
103
104 // Render type by user func
105 $browserRendered = false;
106 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/browse_links.php']['browserRendering'] ?? [] as $className) {
107 $browserRenderObj = GeneralUtility::makeInstance($className);
108 if (is_object($browserRenderObj) && method_exists($browserRenderObj, 'isValid') && method_exists($browserRenderObj, 'render')) {
109 if ($browserRenderObj->isValid($this->mode, $this)) {
110 $content = $browserRenderObj->render($this->mode, $this);
111 $browserRendered = true;
112 break;
113 }
114 }
115 }
116
117 // if type was not rendered use default rendering functions
118 if (!$browserRendered) {
119 $browser = $this->getElementBrowserInstance();
120
121 $backendUser = $this->getBackendUser();
122 $modData = $backendUser->getModuleData('browse_links.php', 'ses');
123 list($modData) = $browser->processSessionData($modData);
124 $backendUser->pushModuleData('browse_links.php', $modData);
125
126 $content = $browser->render();
127 }
128
129 return $content;
130 }
131
132 /**
133 * Get instance of the actual element browser
134 *
135 * This method shall be overwritten in subclasses
136 *
137 * @return ElementBrowserInterface
138 * @throws \UnexpectedValueException
139 */
140 protected function getElementBrowserInstance()
141 {
142 $className = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ElementBrowsers'][$this->mode];
143 $browser = GeneralUtility::makeInstance($className);
144 if (!$browser instanceof ElementBrowserInterface) {
145 throw new \UnexpectedValueException('The specified element browser "' . $className . '" does not implement the required ElementBrowserInterface', 1442763890);
146 }
147 return $browser;
148 }
149
150 /**
151 * @return LanguageService
152 */
153 protected function getLanguageService()
154 {
155 return $GLOBALS['LANG'];
156 }
157
158 /**
159 * @return BackendUserAuthentication
160 */
161 protected function getBackendUser()
162 {
163 return $GLOBALS['BE_USER'];
164 }
165 }