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