[!!!][TASK] Trees do not use global SOBE anymore
[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\ServerRequestInterface;
18 use Psr\Http\Message\ResponseInterface;
19 use TYPO3\CMS\Backend\Template\DocumentTemplate;
20 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21 use TYPO3\CMS\Core\Http\Response;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Lang\LanguageService;
24 use TYPO3\CMS\Recordlist\Browser\ElementBrowser;
25
26 /**
27 * Script class for the Element Browser window.
28 */
29 class ElementBrowserController implements \TYPO3\CMS\Core\Http\ControllerInterface {
30
31 /**
32 * The mode determines the main kind of output of the element browser.
33 * There are these options for values:
34 * - "rte" will show the link selector for the Rich Text Editor (see main_rte())
35 * - "wizard" will allow you to browse for links (like "rte") which are passed back to FormEngine (see main_rte(TRUE))
36 * - "db" will allow you to browse for pages or records in the page tree for FormEngine select fields (see main_db())
37 * - "file"/"filedrag" will allow you to browse for files in the folder mounts for FormEngine file selections (main_file())
38 * - "folder" will allow you to browse for folders in the folder mounts for FormEngine folder selecitons (see main_folder())
39 *
40 * @var string
41 */
42 public $mode;
43
44 /**
45 * Document template object
46 *
47 * @var DocumentTemplate
48 */
49 public $doc;
50
51 /**
52 * @var string
53 */
54 public $content = '';
55
56 /**
57 * Constructor
58 */
59 public function __construct() {
60 $GLOBALS['SOBE'] = $this;
61
62 // Creating backend template object:
63 // this might not be needed but some classes refer to $GLOBALS['SOBE']->doc, so ...
64 $this->doc = GeneralUtility::makeInstance(DocumentTemplate::class);
65 // Apply the same styles as those of the base script
66 $this->doc->bodyTagId = 'typo3-browse-links-php';
67
68 $this->init();
69 }
70
71 protected function init() {
72 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_browse_links.xlf');
73
74 $this->mode = GeneralUtility::_GP('mode');
75 if (!$this->mode) {
76 $this->mode = 'rte';
77 }
78 }
79
80 /**
81 * Injects the request object for the current request or subrequest
82 * As this controller goes only through the main() method, it is rather simple for now
83 *
84 * @param ServerRequestInterface $request
85 * @return ResponseInterface $response
86 */
87 public function processRequest(ServerRequestInterface $request) {
88 $this->main();
89
90 /** @var Response $response */
91 $response = GeneralUtility::makeInstance(Response::class);
92 $response->getBody()->write($this->content);
93 return $response;
94 }
95
96 /**
97 * Main function, detecting the current mode of the element browser and branching out to internal methods.
98 *
99 * @return void
100 */
101 public function main() {
102 $this->setTemporaryDbMounts();
103
104 $this->content = '';
105
106 // Render type by user func
107 $browserRendered = FALSE;
108 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/browse_links.php']['browserRendering'])) {
109 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/browse_links.php']['browserRendering'] as $classRef) {
110 $browserRenderObj = GeneralUtility::getUserObj($classRef);
111 if (is_object($browserRenderObj) && method_exists($browserRenderObj, 'isValid') && method_exists($browserRenderObj, 'render')) {
112 if ($browserRenderObj->isValid($this->mode, $this)) {
113 $this->content .= $browserRenderObj->render($this->mode, $this);
114 $browserRendered = TRUE;
115 break;
116 }
117 }
118 }
119 }
120 // if type was not rendered use default rendering functions
121 if (!$browserRendered) {
122 $browser = $this->getElementBrowserInstance();
123 $browser->init();
124 $backendUser = $this->getBackendUser();
125 $modData = $backendUser->getModuleData('browse_links.php', 'ses');
126 list($modData) = $browser->processSessionData($modData);
127 $backendUser->pushModuleData('browse_links.php', $modData);
128
129 // Output the correct content according to $this->mode
130 switch ((string)$this->mode) {
131 case 'rte':
132 $this->content = $browser->main_rte();
133 break;
134 case 'db':
135 $this->content = $browser->main_db();
136 break;
137 case 'file':
138 case 'filedrag':
139 $this->content = $browser->main_file();
140 break;
141 case 'folder':
142 $this->content = $browser->main_folder();
143 break;
144 case 'wizard':
145 $this->content = $browser->main_rte(TRUE);
146 break;
147 }
148 }
149 }
150
151 /**
152 * @return void
153 */
154 protected function setTemporaryDbMounts() {
155 $backendUser = $this->getBackendUser();
156
157 // Clear temporary DB mounts
158 $tmpMount = GeneralUtility::_GET('setTempDBmount');
159 if (isset($tmpMount)) {
160 $backendUser->setAndSaveSessionData('pageTree_temporaryMountPoint', (int)$tmpMount);
161 }
162 // Set temporary DB mounts
163 $alternativeWebmountPoint = (int)$backendUser->getSessionData('pageTree_temporaryMountPoint');
164 if ($alternativeWebmountPoint) {
165 $alternativeWebmountPoint = GeneralUtility::intExplode(',', $alternativeWebmountPoint);
166 $backendUser->setWebmounts($alternativeWebmountPoint);
167 } else {
168 switch ((string)$this->mode) {
169 case 'rte':
170 case 'db':
171 case 'wizard':
172 // Setting alternative browsing mounts (ONLY local to browse_links.php this script so they stay "read-only")
173 $alternativeWebmountPoints = trim($backendUser->getTSConfigVal('options.pageTree.altElementBrowserMountPoints'));
174 $appendAlternativeWebmountPoints = $backendUser->getTSConfigVal('options.pageTree.altElementBrowserMountPoints.append');
175 if ($alternativeWebmountPoints) {
176 $alternativeWebmountPoints = GeneralUtility::intExplode(',', $alternativeWebmountPoints);
177 $this->getBackendUser()->setWebmounts($alternativeWebmountPoints, $appendAlternativeWebmountPoints);
178 }
179 }
180 }
181 }
182
183 /**
184 * Get instance of ElementBrowser
185 *
186 * This method shall be overwritten in subclasses
187 *
188 * @return ElementBrowser
189 */
190 protected function getElementBrowserInstance() {
191 return GeneralUtility::makeInstance(ElementBrowser::class);
192 }
193
194 /**
195 * Print module content
196 *
197 * @return void
198 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use processRequest() instead
199 */
200 public function printContent() {
201 GeneralUtility::logDeprecatedFunction();
202 echo $this->content;
203 }
204
205 /**
206 * @return LanguageService
207 */
208 protected function getLanguageService() {
209 return $GLOBALS['LANG'];
210 }
211
212 /**
213 * @return BackendUserAuthentication
214 */
215 protected function getBackendUser() {
216 return $GLOBALS['BE_USER'];
217 }
218
219 }