9e999ed02bbc724a64f4a1e56a660e9368ff940e
[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\Template\Components\ButtonBar;
20 use TYPO3\CMS\Backend\Template\ModuleTemplate;
21 use TYPO3\CMS\Backend\Tree\View\ElementBrowserFolderTreeView;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
23 use TYPO3\CMS\Core\Imaging\Icon;
24 use TYPO3\CMS\Core\Imaging\IconFactory;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Filelist\FileListFolderTree;
27 use TYPO3\CMS\Recordlist\Tree\View\DummyLinkParameterProvider;
28
29 /**
30 * Main script class for rendering of the folder tree
31 */
32 class FileSystemNavigationFrameController
33 {
34 /**
35 * Content accumulates in this variable.
36 *
37 * @var string
38 */
39 public $content;
40
41 /**
42 * @var \TYPO3\CMS\Backend\Tree\View\FolderTreeView
43 */
44 public $foldertree;
45
46 /**
47 * @var string
48 */
49 public $currentSubScript;
50
51 /**
52 * @var bool
53 */
54 public $cMR;
55
56 /**
57 * @var array
58 */
59 protected $scopeData;
60
61 /**
62 * ModuleTemplate Container
63 *
64 * @var ModuleTemplate
65 */
66 protected $moduleTemplate;
67
68 /**
69 * Constructor
70 */
71 public function __construct()
72 {
73 $GLOBALS['SOBE'] = $this;
74 $this->init();
75 }
76
77 /**
78 * @param ServerRequestInterface $request the current request
79 * @param ResponseInterface $response
80 * @return ResponseInterface the response with the content
81 */
82 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
83 {
84 $this->initPage();
85 $this->main();
86
87 $response->getBody()->write($this->content);
88 return $response;
89 }
90
91 /**
92 * Initialiation of the script class
93 */
94 protected function init()
95 {
96 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
97
98 // Setting GPvars:
99 $this->currentSubScript = GeneralUtility::_GP('currentSubScript');
100 $this->cMR = GeneralUtility::_GP('cMR');
101
102 $scopeData = (string)GeneralUtility::_GP('scopeData');
103 $scopeHash = (string)GeneralUtility::_GP('scopeHash');
104
105 if (!empty($scopeData) && GeneralUtility::hmac($scopeData) === $scopeHash) {
106 $this->scopeData = unserialize($scopeData);
107 }
108
109 // Create folder tree object:
110 if (!empty($this->scopeData)) {
111 $this->foldertree = GeneralUtility::makeInstance($this->scopeData['class']);
112 $this->foldertree->thisScript = $this->scopeData['script'];
113 $this->foldertree->ext_noTempRecyclerDirs = $this->scopeData['ext_noTempRecyclerDirs'];
114 if ($this->foldertree instanceof ElementBrowserFolderTreeView) {
115 // create a fake provider to pass link data along properly
116 $linkParamProvider = GeneralUtility::makeInstance(
117 DummyLinkParameterProvider::class,
118 $this->scopeData['browser'],
119 $this->scopeData['script']
120 );
121 $this->foldertree->setLinkParameterProvider($linkParamProvider);
122 }
123 } else {
124 $this->foldertree = GeneralUtility::makeInstance(FileListFolderTree::class);
125 $this->foldertree->thisScript = BackendUtility::getModuleUrl('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 * @param ResponseInterface $response
228 * @return ResponseInterface
229 */
230 public function ajaxExpandCollapse(ServerRequestInterface $request, ResponseInterface $response)
231 {
232 $this->init();
233 $tree = $this->foldertree->getBrowsableTree();
234 if ($this->foldertree->getAjaxStatus() === false) {
235 $response = $response->withStatus(500);
236 } else {
237 $response->getBody()->write(json_encode($tree));
238 }
239
240 return $response;
241 }
242
243 /**
244 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
245 */
246 protected function getBackendUser()
247 {
248 return $GLOBALS['BE_USER'];
249 }
250
251 /**
252 * Returns an instance of LanguageService
253 *
254 * @return \TYPO3\CMS\Core\Localization\LanguageService
255 */
256 protected function getLanguageService()
257 {
258 return $GLOBALS['LANG'];
259 }
260 }