[TASK] Streamline PHPDoc comment matches function/method signature
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / RenameFileController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller\File;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
21 use TYPO3\CMS\Backend\Template\DocumentTemplate;
22 use TYPO3\CMS\Backend\Template\ModuleTemplate;
23 use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
24 use TYPO3\CMS\Core\Http\HtmlResponse;
25 use TYPO3\CMS\Core\Imaging\Icon;
26 use TYPO3\CMS\Core\Localization\LanguageService;
27 use TYPO3\CMS\Core\Resource\DuplicationBehavior;
28 use TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException;
29 use TYPO3\CMS\Core\Resource\File;
30 use TYPO3\CMS\Core\Resource\Folder;
31 use TYPO3\CMS\Core\Utility\GeneralUtility;
32 use TYPO3\CMS\Fluid\View\StandaloneView;
33
34 /**
35 * Script Class for the rename-file form.
36 */
37 class RenameFileController
38 {
39 use PublicPropertyDeprecationTrait;
40
41 /**
42 * @var array
43 */
44 protected $deprecatedPublicProperties = [
45 'title' => 'Using $title of class RenameFileController from outside is discouraged as this variable is only used for internal storage.',
46 'target' => 'Using $target of class RenameFileController from outside is discouraged as this variable is only used for internal storage.',
47 'returnUrl' => 'Using $returnUrl of class RenameFileController from outside is discouraged as this variable is only used for internal storage.',
48 'content' => 'Using $content of class RenameFileController from outside is discouraged as this variable is only used for internal storage.',
49 ];
50
51 /**
52 * Name of the filemount
53 *
54 * @var string
55 */
56 protected $title;
57
58 /**
59 * Target path
60 *
61 * @var string
62 * @internal
63 */
64 protected $target;
65
66 /**
67 * The file or folder object that should be renamed
68 *
69 * @var File|Folder $fileOrFolderObject
70 */
71 protected $fileOrFolderObject;
72
73 /**
74 * Return URL of list module.
75 *
76 * @var string
77 */
78 protected $returnUrl;
79
80 /**
81 * Accumulating content
82 *
83 * @var string
84 * @internal
85 */
86 protected $content;
87
88 /**
89 * ModuleTemplate object
90 *
91 * @var ModuleTemplate
92 */
93 protected $moduleTemplate;
94
95 /**
96 * Constructor
97 */
98 public function __construct()
99 {
100 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
101
102 // @deprecated since v9, will be moved out of __construct() in v10
103 $this->init($GLOBALS['TYPO3_REQUEST']);
104 }
105
106 /**
107 * Processes the request, currently everything is handled and put together via "renderContent()"
108 *
109 * @param ServerRequestInterface $request
110 * @return ResponseInterface the response with the content
111 */
112 public function mainAction(ServerRequestInterface $request): ResponseInterface
113 {
114 $this->renderContent();
115
116 return new HtmlResponse($this->moduleTemplate->renderContent());
117 }
118
119 /**
120 * Main function, rendering the content of the rename form
121 *
122 * @deprecated since v9, will be removed in v10
123 */
124 public function main()
125 {
126 trigger_error('Method main() will be replaced by protected method renderContent() in v10. Do not call from other extension', E_USER_DEPRECATED);
127 $this->renderContent();
128 }
129
130 /**
131 * Initialize
132 *
133 * @param ServerRequestInterface $request
134 * @throws InsufficientFileAccessPermissionsException
135 */
136 protected function init(ServerRequestInterface $request): void
137 {
138 $parsedBody = $request->getParsedBody();
139 $queryParams = $request->getQueryParams();
140
141 // Initialize GPvars:
142 $this->target = $parsedBody['target'] ?? $queryParams['target'] ?? null;
143 $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
144 // Cleaning and checking target
145 if ($this->target) {
146 $this->fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->target);
147 }
148 if (!$this->fileOrFolderObject) {
149 $title = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
150 $message = $this->getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
151 throw new \RuntimeException($title . ': ' . $message, 1294586844);
152 }
153 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
154 throw new InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889840);
155 }
156
157 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
158 // so the redirect will NOT end in an error message
159 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
160 // rename the folder
161 if ($this->fileOrFolderObject instanceof Folder) {
162 $parsedUrl = parse_url($this->returnUrl);
163 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
164 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
165 $this->returnUrl = str_replace(
166 urlencode($queryParts['id']),
167 urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
168 $this->returnUrl
169 );
170 }
171 }
172
173 // building pathInfo for metaInformation
174 $pathInfo = [
175 'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
176 ];
177 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
178
179 // Setting up the context sensitive menu
180 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
181 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/RenameFile');
182
183 // Add javaScript
184 $this->moduleTemplate->addJavaScriptCode(
185 'RenameFileInlineJavaScript',
186 'function backToList() {top.goToModule("file_FilelistList");}'
187 );
188 }
189
190 /**
191 * Render module content
192 */
193 protected function renderContent(): void
194 {
195 $assigns = [];
196 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
197 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
198 $assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
199 $assigns['returnUrl'] = $this->returnUrl;
200
201 if ($this->fileOrFolderObject instanceof Folder) {
202 $fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
203 $targetLabel = 'file_rename.php.label.target.folder';
204 } else {
205 $fileIdentifier = $this->fileOrFolderObject->getUid();
206 $targetLabel = 'file_rename.php.label.target.file';
207 $assigns['conflictMode'] = DuplicationBehavior::cast(DuplicationBehavior::RENAME);
208 $assigns['destination'] = $this->fileOrFolderObject->getParentFolder()->getCombinedIdentifier();
209 }
210
211 $assigns['fileName'] = $this->fileOrFolderObject->getName();
212 $assigns['fileIdentifier'] = $fileIdentifier;
213 $assigns['fieldLabel'] = $targetLabel;
214
215 // Create buttons
216 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
217
218 // csh button
219 $cshButton = $buttonBar->makeHelpButton()
220 ->setModuleName('xMOD_csh_corebe')
221 ->setFieldName('file_rename');
222 $buttonBar->addButton($cshButton);
223
224 // back button
225 if ($this->returnUrl) {
226 $backButton = $buttonBar->makeLinkButton()
227 ->setHref($this->returnUrl)
228 ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
229 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-close', Icon::SIZE_SMALL));
230 $buttonBar->addButton($backButton);
231 }
232
233 // Save and Close button
234 $saveAndCloseButton = $buttonBar->makeInputButton()
235 ->setName('_saveandclose')
236 ->setValue('1')
237 ->setShowLabelText(true)
238 ->setClasses('t3js-submit-file-rename')
239 ->setForm('RenameFileController')
240 ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_edit.php.saveAndClose'))
241 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-save-close', Icon::SIZE_SMALL));
242
243 $buttonBar->addButton($saveAndCloseButton, ButtonBar::BUTTON_POSITION_LEFT, 20);
244
245 $this->moduleTemplate->getPageRenderer()->addInlineLanguageLabelArray([
246 'file_rename.actions.cancel' => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_rename.actions.cancel'),
247 'file_rename.actions.rename' => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_rename.actions.rename'),
248 'file_rename.actions.override' => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_rename.actions.override'),
249 'file_rename.exists.title' => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_rename.exists.title'),
250 'file_rename.exists.description' => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:file_rename.exists.description'),
251 ]);
252
253 // Rendering of the output via fluid
254 $view = GeneralUtility::makeInstance(StandaloneView::class);
255 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
256 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
257 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
258 'EXT:backend/Resources/Private/Templates/File/RenameFile.html'
259 ));
260 $view->assignMultiple($assigns);
261 $this->content = $view->render();
262 $this->moduleTemplate->setContent($this->content);
263 }
264
265 /**
266 * @return LanguageService
267 */
268 protected function getLanguageService(): LanguageService
269 {
270 return $GLOBALS['LANG'];
271 }
272
273 /**
274 * @return DocumentTemplate
275 */
276 protected function getDocumentTemplate(): DocumentTemplate
277 {
278 return $GLOBALS['TBE_TEMPLATE'];
279 }
280 }