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