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