[TASK] Unify element- and linkbrowser styling
[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\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
62 $this->init();
63 }
64
65 /**
66 * Initialize the controller
67 */
68 protected function init()
69 {
70 $this->getLanguageService()->includeLLFile('EXT:lang/Resources/Private/Language/locallang_browse_links.xlf');
71
72 $this->mode = GeneralUtility::_GP('mode');
73 }
74
75 /**
76 * Injects the request object for the current request or sub-request
77 * As this controller goes only through the main() method, it is rather simple for now
78 *
79 * @param ServerRequestInterface $request the current request
80 * @param ResponseInterface $response the prepared response object
81 * @return ResponseInterface the response with the content
82 */
83 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
84 {
85 // Fallback for old calls, which use mode "wizard" or "rte" for link selection
86 if ($this->mode === 'wizard' || $this->mode === 'rte') {
87 return $response->withStatus(303)->withHeader('Location', BackendUtility::getModuleUrl('wizard_link', $_GET));
88 }
89
90 $response->getBody()->write($this->main());
91 return $response;
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 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/browse_links.php']['browserRendering'])) {
106 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/browse_links.php']['browserRendering'] as $classRef) {
107 $browserRenderObj = GeneralUtility::getUserObj($classRef);
108 if (is_object($browserRenderObj) && method_exists($browserRenderObj, 'isValid') && method_exists($browserRenderObj, 'render')) {
109 if ($browserRenderObj->isValid($this->mode, $this)) {
110 $content = $browserRenderObj->render($this->mode, $this);
111 $browserRendered = true;
112 break;
113 }
114 }
115 }
116 }
117
118 // if type was not rendered use default rendering functions
119 if (!$browserRendered) {
120 $browser = $this->getElementBrowserInstance();
121
122 $backendUser = $this->getBackendUser();
123 $modData = $backendUser->getModuleData('browse_links.php', 'ses');
124 list($modData) = $browser->processSessionData($modData);
125 $backendUser->pushModuleData('browse_links.php', $modData);
126
127 $content = $browser->render();
128 }
129
130 return $content;
131 }
132
133 /**
134 * Get instance of the actual element browser
135 *
136 * This method shall be overwritten in subclasses
137 *
138 * @return ElementBrowserInterface
139 * @throws \UnexpectedValueException
140 */
141 protected function getElementBrowserInstance()
142 {
143 $className = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ElementBrowsers'][$this->mode];
144 $browser = GeneralUtility::makeInstance($className);
145 if (!$browser instanceof ElementBrowserInterface) {
146 throw new \UnexpectedValueException('The specified element browser "' . $className . '" does not implement the required ElementBrowserInterface', 1442763890);
147 }
148 return $browser;
149 }
150
151 /**
152 * @return LanguageService
153 */
154 protected function getLanguageService()
155 {
156 return $GLOBALS['LANG'];
157 }
158
159 /**
160 * @return BackendUserAuthentication
161 */
162 protected function getBackendUser()
163 {
164 return $GLOBALS['BE_USER'];
165 }
166 }