[TASK] Fluidification of ReplaceFileController
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / ReplaceFileController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\File;
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\Module\AbstractModule;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Imaging\Icon;
22 use TYPO3\CMS\Core\Imaging\IconFactory;
23 use TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException;
24 use TYPO3\CMS\Core\Resource\Folder;
25 use TYPO3\CMS\Core\Resource\ResourceFactory;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Fluid\View\StandaloneView;
28 use TYPO3\CMS\Lang\LanguageService;
29
30 /**
31 * Script Class for the rename-file form
32 */
33 class ReplaceFileController extends AbstractModule
34 {
35 /**
36 * Document template object
37 *
38 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
39 */
40 public $doc;
41
42 /**
43 * Name of the filemount
44 *
45 * @var string
46 */
47 public $title;
48
49 /**
50 * sys_file uid
51 *
52 * @var int
53 */
54 public $uid;
55
56 /**
57 * The file or folder object that should be renamed
58 *
59 * @var \TYPO3\CMS\Core\Resource\ResourceInterface $fileOrFolderObject
60 */
61 protected $fileOrFolderObject;
62
63 /**
64 * Return URL of list module.
65 *
66 * @var string
67 */
68 public $returnUrl;
69
70 /**
71 * Accumulating content
72 *
73 * @var string
74 */
75 public $content;
76
77 /**
78 * Constructor
79 */
80 public function __construct()
81 {
82 parent::__construct();
83 $GLOBALS['SOBE'] = $this;
84 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
85 $this->init();
86 }
87
88 /**
89 * Init
90 *
91 * @return void
92 * @throws \RuntimeException
93 * @throws InsufficientFileAccessPermissionsException
94 */
95 protected function init()
96 {
97 // Initialize GPvars:
98 $this->uid = (int)GeneralUtility::_GP('uid');
99 $lang = $this->getLanguageService();
100
101 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
102 // Cleaning and checking uid
103 if ($this->uid > 0) {
104 $this->fileOrFolderObject = ResourceFactory::getInstance()
105 ->retrieveFileOrFolderObject('file:' . $this->uid);
106 }
107 if (!$this->fileOrFolderObject) {
108 $title = $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
109 $message = $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
110 throw new \RuntimeException($title . ': ' . $message, 1436895930);
111 }
112 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
113 throw new InsufficientFileAccessPermissionsException(
114 'You are not allowed to access files outside your storages',
115 1436895931
116 );
117 }
118
119 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
120 // so the redirect will NOT end in an error message
121 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
122 // rename the folder
123 if ($this->fileOrFolderObject instanceof Folder) {
124 $parsedUrl = parse_url($this->returnUrl);
125 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
126 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
127 $this->returnUrl = str_replace(
128 urlencode($queryParts['id']),
129 urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
130 $this->returnUrl
131 );
132 }
133 }
134
135 $pathInfo = [
136 'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
137 ];
138 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
139 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ClickMenu');
140 $this->moduleTemplate->addJavaScriptCode(
141 'ReplaceFileOnlineJavaScript',
142 'function backToList() {top.goToModule("file_FilelistList");}'
143 );
144 }
145
146 /**
147 * Main function, rendering the content of the rename form
148 *
149 * @return void
150 */
151 public function main()
152 {
153 // Assign variables used by the fluid template
154 $assigns = [];
155 $assigns['moduleUrlTceFile'] = BackendUtility::getModuleUrl('tce_file');
156 $assigns['uid'] = $this->uid;
157 $assigns['returnUrl'] = $this->returnUrl;
158
159 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
160 // csh button
161 $cshButton = $buttonBar->makeHelpButton()
162 ->setModuleName('xMOD_csh_corebe')
163 ->setFieldName('file_rename');
164 $buttonBar->addButton($cshButton);
165
166 // Back button
167 if ($this->returnUrl) {
168 $returnButton = $buttonBar->makeLinkButton()
169 ->setHref($this->returnUrl)
170 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
171 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
172 $buttonBar->addButton($returnButton);
173 }
174
175 // Rendering of the output via fluid
176 $view = GeneralUtility::makeInstance(StandaloneView::class);
177 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
178 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
179 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
180 'EXT:backend/Resources/Private/Templates/File/ReplaceFile.html'
181 ));
182 $view->assignMultiple($assigns);
183 $this->content = $view->render();
184
185 $this->moduleTemplate->setContent($this->content);
186 }
187
188 /**
189 * Processes the request, currently everything is handled and put together via "main()"
190 *
191 * @param ServerRequestInterface $request the current request
192 * @param ResponseInterface $response
193 * @return ResponseInterface the response with the content
194 */
195 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
196 {
197 $this->main();
198
199 $response->getBody()->write($this->moduleTemplate->renderContent());
200 return $response;
201 }
202
203 /**
204 * @return LanguageService
205 */
206 protected function getLanguageService()
207 {
208 return $GLOBALS['LANG'];
209 }
210 }