‪TYPO3CMS  11.5
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;
30 use TYPO3\CMS\Core\Page\PageRenderer;
38 
44 {
51  protected ‪$target;
52 
58  protected ‪$fileOrFolderObject;
59 
65  protected ‪$returnUrl;
66 
72  protected ‪$moduleTemplate;
73 
75  protected PageRenderer ‪$pageRenderer;
79 
80  public function ‪__construct(
82  PageRenderer ‪$pageRenderer,
86  ) {
87  $this->iconFactory = ‪$iconFactory;
88  $this->pageRenderer = ‪$pageRenderer;
89  $this->uriBuilder = ‪$uriBuilder;
90  $this->resourceFactory = ‪$resourceFactory;
91  $this->moduleTemplateFactory = ‪$moduleTemplateFactory;
92  }
93 
100  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
101  {
102  $this->moduleTemplate = $this->moduleTemplateFactory->create($request);
103  $this->‪init($request);
104  $this->‪renderContent();
105  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
106  }
107 
114  protected function ‪init(ServerRequestInterface $request): void
115  {
116  $parsedBody = $request->getParsedBody();
117  $queryParams = $request->getQueryParams();
118 
119  // Initialize GPvars:
120  $this->target = $parsedBody['target'] ?? $queryParams['target'] ?? null;
121  $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
122  // Cleaning and checking target
123  if ($this->target) {
124  $this->fileOrFolderObject = $this->resourceFactory->retrieveFileOrFolderObject($this->target);
125  }
126  if (!$this->fileOrFolderObject) {
127  $title = $this->‪getLanguageService()->‪sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
128  $message = $this->‪getLanguageService()->‪sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
129  throw new \RuntimeException($title . ': ' . $message, 1294586844);
130  }
131  if ($this->fileOrFolderObject->getStorage()->isFallbackStorage()) {
132  throw new InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889840);
133  }
134 
135  // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
136  // so the redirect will NOT end in an error message
137  // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
138  // rename the folder
139  if ($this->fileOrFolderObject instanceof Folder) {
140  $parsedUrl = parse_url($this->returnUrl);
141  $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query'] ?? ''));
142  $queryPartsId = $queryParts['id'] ?? null;
143  if ($queryPartsId === $this->fileOrFolderObject->getCombinedIdentifier()) {
144  $this->returnUrl = str_replace(
145  urlencode($queryPartsId),
146  urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
147  $this->returnUrl
148  );
149  }
150  }
151 
152  // building pathInfo for metaInformation
153  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformationForResource($this->fileOrFolderObject);
154 
155  // Setting up the context sensitive menu
156  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
157  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Filelist/RenameFile');
158  }
159 
163  protected function ‪renderContent(): void
164  {
165  $assigns = [];
166  $assigns['moduleUrlTceFile'] = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
167  $assigns['returnUrl'] = ‪$this->returnUrl;
168 
169  if ($this->fileOrFolderObject instanceof ‪Folder) {
170  $fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
171  $targetLabel = 'file_rename.php.label.target.folder';
172  } else {
173  $fileIdentifier = $this->fileOrFolderObject->getUid();
174  $targetLabel = 'file_rename.php.label.target.file';
176  $assigns['destination'] = $this->fileOrFolderObject->getParentFolder()->getCombinedIdentifier();
177  }
178 
179  $assigns['fileName'] = $this->fileOrFolderObject->getName();
180  $assigns['fileIdentifier'] = $fileIdentifier;
181  $assigns['fieldLabel'] = $targetLabel;
182 
183  // Create buttons
184  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
185 
186  // csh button
187  $cshButton = $buttonBar->makeHelpButton()
188  ->setModuleName('xMOD_csh_corebe')
189  ->setFieldName('file_rename');
190  $buttonBar->addButton($cshButton);
191 
192  // back button
193  if ($this->returnUrl) {
194  $backButton = $buttonBar->makeLinkButton()
195  ->setHref($this->returnUrl)
196  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
197  ->setIcon($this->iconFactory->getIcon('actions-close', ‪Icon::SIZE_SMALL));
198  $buttonBar->addButton($backButton);
199  }
200 
201  // Save and Close button
202  $saveAndCloseButton = $buttonBar->makeInputButton()
203  ->setName('_saveandclose')
204  ->setValue('1')
205  ->setShowLabelText(true)
206  ->setClasses('t3js-submit-file-rename')
207  ->setForm('RenameFileController')
208  ->setTitle($this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_edit.php.saveAndClose'))
209  ->setIcon($this->iconFactory->getIcon('actions-document-save-close', ‪Icon::SIZE_SMALL));
210 
211  $buttonBar->addButton($saveAndCloseButton, ‪ButtonBar::BUTTON_POSITION_LEFT, 20);
212 
213  $this->pageRenderer->addInlineLanguageLabelArray([
214  'file_rename.actions.cancel' => $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_rename.actions.cancel'),
215  'file_rename.actions.rename' => $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_rename.actions.rename'),
216  'file_rename.actions.override' => $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_rename.actions.override'),
217  'file_rename.exists.title' => $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_rename.exists.title'),
218  'file_rename.exists.description' => $this->‪getLanguageService()->sL('LLL:EXT:filelist/Resources/Private/Language/locallang.xlf:file_rename.exists.description'),
219  ]);
220 
221  // Rendering of the output via fluid
222  $view = GeneralUtility::makeInstance(StandaloneView::class);
223  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
224  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
225  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
226  'EXT:filelist/Resources/Private/Templates/File/RenameFile.html'
227  ));
228  $view->assignMultiple($assigns);
229  $this->moduleTemplate->setContent($view->render());
230  }
231 
235  protected function ‪getLanguageService(): ‪LanguageService
236  {
237  return ‪$GLOBALS['LANG'];
238  }
239 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$uriBuilder
‪UriBuilder $uriBuilder
Definition: RenameFileController.php:72
‪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:62
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\getLanguageService
‪LanguageService getLanguageService()
Definition: RenameFileController.php:231
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:29
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$moduleTemplateFactory
‪ModuleTemplateFactory $moduleTemplateFactory
Definition: RenameFileController.php:74
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: RenameFileController.php:96
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$resourceFactory
‪ResourceFactory $resourceFactory
Definition: RenameFileController.php:73
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:161
‪TYPO3\CMS\Filelist\Controller\File
Definition: CreateFolderController.php:18
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:23
‪TYPO3\CMS\Core\Type\Enumeration\cast
‪static static cast($value)
Definition: Enumeration.php:186
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$pageRenderer
‪PageRenderer $pageRenderer
Definition: RenameFileController.php:71
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController
Definition: RenameFileController.php:44
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\init
‪init(ServerRequestInterface $request)
Definition: RenameFileController.php:110
‪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\Filelist\Controller\File\RenameFileController\$fileOrFolderObject
‪File Folder null $fileOrFolderObject
Definition: RenameFileController.php:56
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\__construct
‪__construct(IconFactory $iconFactory, PageRenderer $pageRenderer, UriBuilder $uriBuilder, ResourceFactory $resourceFactory, ModuleTemplateFactory $moduleTemplateFactory)
Definition: RenameFileController.php:76
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: RenameFileController.php:68
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$target
‪string $target
Definition: RenameFileController.php:50
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\renderContent
‪renderContent()
Definition: RenameFileController.php:159
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Filelist\Controller\File\RenameFileController\$iconFactory
‪IconFactory $iconFactory
Definition: RenameFileController.php:70
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26