‪TYPO3CMS  10.4
RenameFileController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
35 
41 {
48  protected ‪$target;
49 
55  protected ‪$fileOrFolderObject;
56 
62  protected ‪$returnUrl;
63 
69  protected ‪$moduleTemplate;
70 
77  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
78  {
79  $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
80  $this->‪init($request);
81  $this->‪renderContent();
82  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
83  }
84 
91  protected function ‪init(ServerRequestInterface $request): void
92  {
93  $parsedBody = $request->getParsedBody();
94  $queryParams = $request->getQueryParams();
95 
96  // Initialize GPvars:
97  $this->target = $parsedBody['target'] ?? $queryParams['target'] ?? null;
98  $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
99  // Cleaning and checking target
100  if ($this->target) {
101  $this->fileOrFolderObject = GeneralUtility::makeInstance(ResourceFactory::class)->retrieveFileOrFolderObject($this->target);
102  }
103  if (!$this->fileOrFolderObject) {
104  $title = $this->‪getLanguageService()->‪sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
105  $message = $this->‪getLanguageService()->‪sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
106  throw new \RuntimeException($title . ': ' . $message, 1294586844);
107  }
108  if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
109  throw new ‪InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889840);
110  }
111 
112  // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
113  // so the redirect will NOT end in an error message
114  // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
115  // rename the folder
116  if ($this->fileOrFolderObject instanceof ‪Folder) {
117  $parsedUrl = parse_url($this->returnUrl);
118  $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
119  if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
120  $this->returnUrl = str_replace(
121  urlencode($queryParts['id']),
122  urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
123  $this->returnUrl
124  );
125  }
126  }
127 
128  // building pathInfo for metaInformation
129  $pathInfo = [
130  'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
131  ];
132  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
133 
134  // Setting up the context sensitive menu
135  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
136  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Filelist/RenameFile');
137  }
138 
142  protected function ‪renderContent(): void
143  {
144  $assigns = [];
146  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
147  $assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
148  $assigns['returnUrl'] = ‪$this->returnUrl;
149 
150  if ($this->fileOrFolderObject instanceof ‪Folder) {
151  $fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
152  $targetLabel = 'file_rename.php.label.target.folder';
153  } else {
154  $fileIdentifier = $this->fileOrFolderObject->getUid();
155  $targetLabel = 'file_rename.php.label.target.file';
157  $assigns['destination'] = $this->fileOrFolderObject->getParentFolder()->getCombinedIdentifier();
158  }
159 
160  $assigns['fileName'] = $this->fileOrFolderObject->getName();
161  $assigns['fileIdentifier'] = $fileIdentifier;
162  $assigns['fieldLabel'] = $targetLabel;
163 
164  // Create buttons
165  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
166 
167  // csh button
168  $cshButton = $buttonBar->makeHelpButton()
169  ->setModuleName('xMOD_csh_corebe')
170  ->setFieldName('file_rename');
171  $buttonBar->addButton($cshButton);
172 
173  // back button
174  if ($this->returnUrl) {
175  $backButton = $buttonBar->makeLinkButton()
176  ->setHref($this->returnUrl)
177  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
178  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-close', ‪Icon::SIZE_SMALL));
179  $buttonBar->addButton($backButton);
180  }
181 
182  // Save and Close button
183  $saveAndCloseButton = $buttonBar->makeInputButton()
184  ->setName('_saveandclose')
185  ->setValue('1')
186  ->setShowLabelText(true)
187  ->setClasses('t3js-submit-file-rename')
188  ->setForm('RenameFileController')
189  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_edit.php.saveAndClose'))
190  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-save-close', ‪Icon::SIZE_SMALL));
191 
192  $buttonBar->addButton($saveAndCloseButton, ‪ButtonBar::BUTTON_POSITION_LEFT, 20);
193 
194  $this->moduleTemplate->getPageRenderer()->addInlineLanguageLabelArray([
195  'file_rename.actions.cancel' => $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_rename.actions.cancel'),
196  'file_rename.actions.rename' => $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_rename.actions.rename'),
197  'file_rename.actions.override' => $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_rename.actions.override'),
198  'file_rename.exists.title' => $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_rename.exists.title'),
199  'file_rename.exists.description' => $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_rename.exists.description'),
200  ]);
201 
202  // Rendering of the output via fluid
203  $view = GeneralUtility::makeInstance(StandaloneView::class);
204  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
205  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
206  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
207  'EXT:filelist/Resources/Private/Templates/File/RenameFile.html'
208  ));
209  $view->assignMultiple($assigns);
210  $this->moduleTemplate->setContent($view->render());
211  }
212 
216  protected function ‪getLanguageService(): ‪LanguageService
217  {
218  return ‪$GLOBALS['LANG'];
219  }
220 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_LEFT
‪const BUTTON_POSITION_LEFT
Definition: ButtonBar.php:36
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:32
‪TYPO3\CMS\Core\Resource\DuplicationBehavior
Definition: DuplicationBehavior.php:24
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$returnUrl
‪string $returnUrl
Definition: RenameFileController.php:59
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\getLanguageService
‪LanguageService getLanguageService()
Definition: RenameFileController.php:212
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: RenameFileController.php:73
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$fileOrFolderObject
‪File Folder $fileOrFolderObject
Definition: RenameFileController.php:53
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:194
‪TYPO3\CMS\Filelist\Controller\File
Definition: CreateFolderController.php:18
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:43
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:24
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:186
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController
Definition: RenameFileController.php:41
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\init
‪init(ServerRequestInterface $request)
Definition: RenameFileController.php:87
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:32
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: RenameFileController.php:65
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$target
‪string $target
Definition: RenameFileController.php:47
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\renderContent
‪renderContent()
Definition: RenameFileController.php:138
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26