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