[TASK] Create own response instance in controller actions
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / FileSystemNavigationFrameController.php
1 <?php
2 namespace TYPO3\CMS\Backend\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\Routing\UriBuilder;
20 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
21 use TYPO3\CMS\Backend\Template\ModuleTemplate;
22 use TYPO3\CMS\Backend\Tree\View\ElementBrowserFolderTreeView;
23 use TYPO3\CMS\Core\Http\HtmlResponse;
24 use TYPO3\CMS\Core\Http\JsonResponse;
25 use TYPO3\CMS\Core\Imaging\Icon;
26 use TYPO3\CMS\Core\Imaging\IconFactory;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Filelist\FileListFolderTree;
29 use TYPO3\CMS\Recordlist\Tree\View\DummyLinkParameterProvider;
30
31 /**
32 * Main script class for rendering of the folder tree
33 */
34 class FileSystemNavigationFrameController
35 {
36 /**
37 * Content accumulates in this variable.
38 *
39 * @var string
40 */
41 public $content;
42
43 /**
44 * @var \TYPO3\CMS\Backend\Tree\View\FolderTreeView
45 */
46 public $foldertree;
47
48 /**
49 * @var string
50 */
51 public $currentSubScript;
52
53 /**
54 * @var bool
55 */
56 public $cMR;
57
58 /**
59 * @var array
60 */
61 protected $scopeData;
62
63 /**
64 * ModuleTemplate Container
65 *
66 * @var ModuleTemplate
67 */
68 protected $moduleTemplate;
69
70 /**
71 * Constructor
72 */
73 public function __construct()
74 {
75 $GLOBALS['SOBE'] = $this;
76 $this->init();
77 }
78
79 /**
80 * @param ServerRequestInterface $request the current request
81 * @return ResponseInterface the response with the content
82 */
83 public function mainAction(ServerRequestInterface $request): ResponseInterface
84 {
85 $this->initPage();
86 $this->main();
87 return new HtmlResponse($this->content);
88 }
89
90 /**
91 * Initialiation of the script class
92 */
93 protected function init()
94 {
95 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
96
97 // Setting GPvars:
98 $this->currentSubScript = GeneralUtility::_GP('currentSubScript');
99 $this->cMR = GeneralUtility::_GP('cMR');
100
101 $scopeData = (string)GeneralUtility::_GP('scopeData');
102 $scopeHash = (string)GeneralUtility::_GP('scopeHash');
103
104 if (!empty($scopeData) && hash_equals(GeneralUtility::hmac($scopeData), $scopeHash)) {
105 $this->scopeData = unserialize($scopeData);
106 }
107
108 // Create folder tree object:
109 if (!empty($this->scopeData)) {
110 $this->foldertree = GeneralUtility::makeInstance($this->scopeData['class']);
111 $this->foldertree->thisScript = $this->scopeData['script'];
112 $this->foldertree->ext_noTempRecyclerDirs = $this->scopeData['ext_noTempRecyclerDirs'];
113 if ($this->foldertree instanceof ElementBrowserFolderTreeView) {
114 // create a fake provider to pass link data along properly
115 $linkParamProvider = GeneralUtility::makeInstance(
116 DummyLinkParameterProvider::class,
117 $this->scopeData['browser'],
118 $this->scopeData['script']
119 );
120 $this->foldertree->setLinkParameterProvider($linkParamProvider);
121 }
122 } else {
123 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
124 $this->foldertree = GeneralUtility::makeInstance(FileListFolderTree::class);
125 $this->foldertree->thisScript = (string)$uriBuilder->buildUriFromRoute('file_navframe');
126 }
127 }
128
129 /**
130 * initialization for the visual parts of the class
131 * Use template rendering only if this is a non-AJAX call
132 */
133 public function initPage()
134 {
135 $this->moduleTemplate->setBodyTag('<body id="ext-backend-Modules-FileSystemNavigationFrame-index-php">');
136
137 // Adding javascript code for drag&drop and the file tree as well as the click menu code
138 $hlClass = $this->getBackendUser()->workspace === 0 ? 'active' : 'active active-ws wsver' . $GLOBALS['BE_USER']->workspace;
139 $dragDropCode = '
140 Tree.highlightClass = "' . $hlClass . '";
141 Tree.highlightActiveItem("", top.fsMod.navFrameHighlightedID["file"]);
142 ';
143
144 // Adding javascript for drag & drop activation and highlighting
145 $pageRenderer = $this->moduleTemplate->getPageRenderer();
146 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
147 $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/LegacyTree', 'function() {
148 DragDrop.table = "folders";
149 Tree.registerDragDropHandlers();
150 ' . $dragDropCode . '
151 }');
152
153 // Setting JavaScript for menu.
154 $inlineJs = ($this->currentSubScript ? 'top.currentSubScript=unescape("' . rawurlencode($this->currentSubScript) . '");' : '') . '
155 // Function, loading the list frame from navigation tree:
156 function jumpTo(id, linkObj, highlightID, bank) {
157 var theUrl = top.currentSubScript;
158 if (theUrl.indexOf("?") != -1) {
159 theUrl += "&id=" + id
160 } else {
161 theUrl += "?id=" + id
162 }
163 top.fsMod.currentBank = bank;
164 top.TYPO3.Backend.ContentContainer.setUrl(theUrl);
165
166 Tree.highlightActiveItem("file", highlightID + "_" + bank);
167 if (linkObj) { linkObj.blur(); }
168 return false;
169 }
170 ' . ($this->cMR ? ' jumpTo(top.fsMod.recentIds[\'file\'],\'\');' : '');
171
172 $this->moduleTemplate->getPageRenderer()->addJsInlineCode(
173 'FileSystemNavigationFrame',
174 $inlineJs
175 );
176 }
177
178 /**
179 * Main function, rendering the folder tree
180 */
181 public function main()
182 {
183 // Produce browse-tree:
184 $tree = $this->foldertree->getBrowsableTree();
185 // Outputting page tree:
186 $this->moduleTemplate->setContent($tree);
187 // Setting up the buttons
188 $this->getButtons();
189 // Build the <body> for the module
190 $this->moduleTemplate->setTitle('TYPO3 Folder Tree');
191 $this->content = $this->moduleTemplate->renderContent();
192 }
193
194 /**
195 * Register docHeader buttons
196 */
197 protected function getButtons()
198 {
199 /** @var ButtonBar $buttonBar */
200 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
201
202 /** @var IconFactory $iconFactory */
203 $iconFactory = $this->moduleTemplate->getIconFactory();
204
205 // Refresh
206 $refreshButton = $buttonBar->makeLinkButton()
207 ->setHref(GeneralUtility::getIndpEnv('REQUEST_URI'))
208 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.reload'))
209 ->setIcon($iconFactory->getIcon('actions-refresh', Icon::SIZE_SMALL));
210 $buttonBar->addButton($refreshButton, ButtonBar::BUTTON_POSITION_RIGHT);
211
212 // CSH
213 $cshButton = $buttonBar->makeHelpButton()
214 ->setModuleName('xMOD_csh_corebe')
215 ->setFieldName('filetree');
216 $buttonBar->addButton($cshButton);
217 }
218
219 /**********************************
220 * AJAX Calls
221 **********************************/
222 /**
223 * Makes the AJAX call to expand or collapse the foldertree.
224 * Called by an AJAX Route, see AjaxRequestHandler
225 *
226 * @param ServerRequestInterface $request
227 * @return ResponseInterface
228 */
229 public function ajaxExpandCollapse(ServerRequestInterface $request): ResponseInterface
230 {
231 $this->init();
232 $tree = $this->foldertree->getBrowsableTree();
233 if ($this->foldertree->getAjaxStatus() === false) {
234 return new HtmlResponse('', 500);
235 }
236 return new JsonResponse([$tree]);
237 }
238
239 /**
240 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
241 */
242 protected function getBackendUser()
243 {
244 return $GLOBALS['BE_USER'];
245 }
246
247 /**
248 * Returns an instance of LanguageService
249 *
250 * @return \TYPO3\CMS\Core\Localization\LanguageService
251 */
252 protected function getLanguageService()
253 {
254 return $GLOBALS['LANG'];
255 }
256 }