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