‪TYPO3CMS  11.5
ReplaceFileController.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;
29 use TYPO3\CMS\Core\Page\PageRenderer;
36 
42 {
48  protected ‪$uid;
49 
55  protected ‪$fileOrFolderObject;
56 
62  protected ‪$returnUrl;
63 
69  protected ‪$moduleTemplate;
70 
72  protected PageRenderer ‪$pageRenderer;
76 
77  public function ‪__construct(
79  PageRenderer ‪$pageRenderer,
83  ) {
84  $this->iconFactory = ‪$iconFactory;
85  $this->pageRenderer = ‪$pageRenderer;
86  $this->uriBuilder = ‪$uriBuilder;
87  $this->resourceFactory = ‪$resourceFactory;
88  $this->moduleTemplateFactory = ‪$moduleTemplateFactory;
89  }
90 
97  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
98  {
99  $this->moduleTemplate = $this->moduleTemplateFactory->create($request);
100  $this->‪init($request);
101  $this->‪renderContent();
102  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
103  }
104 
112  protected function ‪init(ServerRequestInterface $request): void
113  {
114  $parsedBody = $request->getParsedBody();
115  $queryParams = $request->getQueryParams();
116  $lang = $this->‪getLanguageService();
117 
118  // Initialize GPvars:
119  $this->uid = (int)($parsedBody['uid'] ?? $queryParams['uid'] ?? 0);
120  $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
121 
122  // Cleaning and checking uid
123  if ($this->uid > 0) {
124  $this->fileOrFolderObject = $this->resourceFactory->retrieveFileOrFolderObject('file:' . $this->uid);
125  }
126  if (!$this->fileOrFolderObject) {
127  $title = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
128  $message = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
129  throw new \RuntimeException($title . ': ' . $message, 1436895930);
130  }
131  if ($this->fileOrFolderObject->getStorage()->isFallbackStorage()) {
132  throw new InsufficientFileAccessPermissionsException(
133  'You are not allowed to access files outside your storages',
134  1436895931
135  );
136  }
137 
138  // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
139  // so the redirect will NOT end in an error message
140  // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
141  // rename the folder
142  if ($this->fileOrFolderObject instanceof Folder) {
143  $parsedUrl = parse_url($this->returnUrl);
144  $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query'] ?? ''));
145  $queryPartsId = $queryParts['id'] ?? null;
146  if ($queryPartsId === $this->fileOrFolderObject->getCombinedIdentifier()) {
147  $this->returnUrl = str_replace(
148  urlencode($queryPartsId),
149  urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
150  $this->returnUrl
151  );
152  }
153  }
154 
155  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformationForResource($this->fileOrFolderObject);
156  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
157  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Filelist/FileReplace');
158  }
159 
163  protected function ‪renderContent(): void
164  {
165  // Assign variables used by the fluid template
166  $assigns = [];
167  $assigns['moduleUrlTceFile'] = (string)$this->uriBuilder->buildUriFromRoute('tce_file');
168  $assigns['uid'] = ‪$this->uid;
169  $assigns['returnUrl'] = ‪$this->returnUrl;
170 
171  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
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  $returnButton = $buttonBar->makeLinkButton()
181  ->setHref($this->returnUrl)
182  ->setTitle($this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
183  ->setIcon($this->iconFactory->getIcon('actions-view-go-back', ‪Icon::SIZE_SMALL));
184  $buttonBar->addButton($returnButton);
185  }
186 
187  // Rendering of the output via fluid
188  $view = GeneralUtility::makeInstance(StandaloneView::class);
189  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
190  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
191  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
192  'EXT:filelist/Resources/Private/Templates/File/ReplaceFile.html'
193  ));
194  $view->assignMultiple($assigns);
195  $this->moduleTemplate->setContent($view->render());
196  }
197 
201  protected function ‪getLanguageService(): ‪LanguageService
202  {
203  return ‪$GLOBALS['LANG'];
204  }
205 }
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$uriBuilder
‪UriBuilder $uriBuilder
Definition: ReplaceFileController.php:69
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$iconFactory
‪IconFactory $iconFactory
Definition: ReplaceFileController.php:67
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: ReplaceFileController.php:65
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$uid
‪int $uid
Definition: ReplaceFileController.php:47
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$pageRenderer
‪PageRenderer $pageRenderer
Definition: ReplaceFileController.php:68
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:29
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$moduleTemplateFactory
‪ModuleTemplateFactory $moduleTemplateFactory
Definition: ReplaceFileController.php:71
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\getLanguageService
‪LanguageService getLanguageService()
Definition: ReplaceFileController.php:197
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:161
‪TYPO3\CMS\Filelist\Controller\File
Definition: CreateFolderController.php:18
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\init
‪init(ServerRequestInterface $request)
Definition: ReplaceFileController.php:108
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:23
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$fileOrFolderObject
‪ResourceInterface null $fileOrFolderObject
Definition: ReplaceFileController.php:53
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$resourceFactory
‪ResourceFactory $resourceFactory
Definition: ReplaceFileController.php:70
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\mainAction
‪ResponseInterface mainAction(ServerRequestInterface $request)
Definition: ReplaceFileController.php:93
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$returnUrl
‪string $returnUrl
Definition: ReplaceFileController.php:59
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\__construct
‪__construct(IconFactory $iconFactory, PageRenderer $pageRenderer, UriBuilder $uriBuilder, ResourceFactory $resourceFactory, ModuleTemplateFactory $moduleTemplateFactory)
Definition: ReplaceFileController.php:73
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\renderContent
‪renderContent()
Definition: ReplaceFileController.php:159
‪TYPO3\CMS\Core\Resource\ResourceInterface
Definition: ResourceInterface.php:22
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController
Definition: ReplaceFileController.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26