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