d5fa13f3dcf88f88b859e47f79b380d70d6e0e8c
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / RenameFileController.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\Template\Components\ButtonBar;
20 use TYPO3\CMS\Backend\Template\ModuleTemplate;
21 use TYPO3\CMS\Core\Imaging\Icon;
22 use TYPO3\CMS\Core\Resource\DuplicationBehavior;
23 use TYPO3\CMS\Core\Resource\File;
24 use TYPO3\CMS\Core\Resource\Folder;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Fluid\View\StandaloneView;
27
28 /**
29 * Script Class for the rename-file form.
30 */
31 class RenameFileController
32 {
33 /**
34 * Name of the filemount
35 *
36 * @var string
37 */
38 public $title;
39
40 /**
41 * Target path
42 *
43 * @var string
44 * @internal
45 */
46 public $target;
47
48 /**
49 * The file or folder object that should be renamed
50 *
51 * @var File|Folder $fileOrFolderObject
52 */
53 protected $fileOrFolderObject;
54
55 /**
56 * Return URL of list module.
57 *
58 * @var string
59 */
60 public $returnUrl;
61
62 /**
63 * Accumulating content
64 *
65 * @var string
66 * @internal
67 */
68 public $content;
69
70 /**
71 * ModuleTemplate object
72 *
73 * @var ModuleTemplate
74 */
75 protected $moduleTemplate;
76
77 /**
78 * Constructor
79 */
80 public function __construct()
81 {
82 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
83 $GLOBALS['SOBE'] = $this;
84 $this->init();
85 }
86
87 /**
88 * Initialize
89 *
90 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
91 */
92 protected function init()
93 {
94 // Initialize GPvars:
95 $this->target = GeneralUtility::_GP('target');
96 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
97 // Cleaning and checking target
98 if ($this->target) {
99 $this->fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->target);
100 }
101 if (!$this->fileOrFolderObject) {
102 $title = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
103 $message = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
104 throw new \RuntimeException($title . ': ' . $message, 1294586844);
105 }
106 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
107 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889840);
108 }
109
110 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
111 // so the redirect will NOT end in an error message
112 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
113 // rename the folder
114 if ($this->fileOrFolderObject instanceof Folder) {
115 $parsedUrl = parse_url($this->returnUrl);
116 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
117 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
118 $this->returnUrl = str_replace(
119 urlencode($queryParts['id']),
120 urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
121 $this->returnUrl
122 );
123 }
124 }
125
126 // building pathInfo for metaInformation
127 $pathInfo = [
128 'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
129 ];
130 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
131
132 // Setting up the context sensitive menu
133 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
134 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/RenameFile');
135
136 // Add javaScript
137 $this->moduleTemplate->addJavaScriptCode(
138 'RenameFileInlineJavaScript',
139 'function backToList() {top.goToModule("file_FilelistList");}'
140 );
141 }
142
143 /**
144 * Main function, rendering the content of the rename form
145 */
146 public function main()
147 {
148 $assigns = [];
149 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
150 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
151 $assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
152 $assigns['returnUrl'] = $this->returnUrl;
153
154 if ($this->fileOrFolderObject instanceof Folder) {
155 $fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
156 $targetLabel = 'file_rename.php.label.target.folder';
157 } else {
158 $fileIdentifier = $this->fileOrFolderObject->getUid();
159 $targetLabel = 'file_rename.php.label.target.file';
160 $assigns['conflictMode'] = DuplicationBehavior::cast(DuplicationBehavior::RENAME);
161 $assigns['destination'] = substr($this->fileOrFolderObject->getCombinedIdentifier(), 0, -strlen($this->fileOrFolderObject->getName()));
162 }
163
164 $assigns['fileName'] = $this->fileOrFolderObject->getName();
165 $assigns['fileIdentifier'] = $fileIdentifier;
166 $assigns['fieldLabel'] = $targetLabel;
167
168 // Create buttons
169 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
170
171 // csh button
172 $cshButton = $buttonBar->makeHelpButton()
173 ->setModuleName('xMOD_csh_corebe')
174 ->setFieldName('file_rename');
175 $buttonBar->addButton($cshButton);
176
177 // back button
178 if ($this->returnUrl) {
179 $backButton = $buttonBar->makeLinkButton()
180 ->setHref($this->returnUrl)
181 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
182 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-close', Icon::SIZE_SMALL));
183 $buttonBar->addButton($backButton);
184 }
185
186 // Save and Close button
187 $saveAndCloseButton = $buttonBar->makeInputButton()
188 ->setName('_saveandclose')
189 ->setValue('1')
190 ->setShowLabelText(true)
191 ->setClasses('t3js-submit-file-rename')
192 ->setForm('RenameFileController')
193 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_edit.php.saveAndClose'))
194 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-save-close', Icon::SIZE_SMALL));
195
196 $buttonBar->addButton($saveAndCloseButton, ButtonBar::BUTTON_POSITION_LEFT, 20);
197
198 $this->moduleTemplate->getPageRenderer()->addInlineLanguageLabelArray([
199 'file_rename.actions.cancel' => $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_rename.actions.cancel'),
200 'file_rename.actions.rename' => $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_rename.actions.rename'),
201 'file_rename.actions.override' => $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_rename.actions.override'),
202 'file_rename.exists.title' => $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_rename.exists.title'),
203 'file_rename.exists.description' => $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:file_rename.exists.description'),
204 ]);
205
206 // Rendering of the output via fluid
207 $view = GeneralUtility::makeInstance(StandaloneView::class);
208 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
209 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
210 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
211 'EXT:backend/Resources/Private/Templates/File/RenameFile.html'
212 ));
213 $view->assignMultiple($assigns);
214 $this->content = $view->render();
215 $this->moduleTemplate->setContent($this->content);
216 }
217
218 /**
219 * Processes the request, currently everything is handled and put together via "main()"
220 *
221 * @param ServerRequestInterface $request the current request
222 * @param ResponseInterface $response
223 * @return ResponseInterface the response with the content
224 */
225 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
226 {
227 $this->main();
228 $response->getBody()->write($this->moduleTemplate->renderContent());
229 return $response;
230 }
231
232 /**
233 * Returns LanguageService
234 *
235 * @return \TYPO3\CMS\Core\Localization\LanguageService
236 */
237 protected function getLanguageService()
238 {
239 return $GLOBALS['LANG'];
240 }
241
242 /**
243 * Returns an instance of DocumentTemplate
244 *
245 * @return \TYPO3\CMS\Backend\Template\DocumentTemplate
246 */
247 protected function getDocumentTemplate()
248 {
249 return $GLOBALS['TBE_TEMPLATE'];
250 }
251 }