‪TYPO3CMS  10.4
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;
32 
38 {
44  protected ‪$uid;
45 
51  protected ‪$fileOrFolderObject;
52 
58  protected ‪$returnUrl;
59 
65  protected ‪$moduleTemplate;
66 
73  public function ‪mainAction(ServerRequestInterface $request): ResponseInterface
74  {
75  $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
76  $this->‪init($request);
77  $this->‪renderContent();
78  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
79  }
80 
88  protected function ‪init(ServerRequestInterface $request): void
89  {
90  $parsedBody = $request->getParsedBody();
91  $queryParams = $request->getQueryParams();
92  $lang = $this->‪getLanguageService();
93 
94  // Initialize GPvars:
95  $this->uid = (int)($parsedBody['uid'] ?? $queryParams['uid'] ?? 0);
96  $this->returnUrl = GeneralUtility::sanitizeLocalUrl($parsedBody['returnUrl'] ?? $queryParams['returnUrl'] ?? '');
97 
98  // Cleaning and checking uid
99  if ($this->uid > 0) {
100  $this->fileOrFolderObject = GeneralUtility::makeInstance(ResourceFactory::class)
101  ->retrieveFileOrFolderObject('file:' . $this->uid);
102  }
103  if (!$this->fileOrFolderObject) {
104  $title = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
105  $message = $lang->sL('LLL:EXT:filelist/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
106  throw new \RuntimeException($title . ': ' . $message, 1436895930);
107  }
108  if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
110  'You are not allowed to access files outside your storages',
111  1436895931
112  );
113  }
114 
115  // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
116  // so the redirect will NOT end in an error message
117  // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
118  // rename the folder
119  if ($this->fileOrFolderObject instanceof ‪Folder) {
120  $parsedUrl = parse_url($this->returnUrl);
121  $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
122  if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
123  $this->returnUrl = str_replace(
124  urlencode($queryParts['id']),
125  urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
126  $this->returnUrl
127  );
128  }
129  }
130 
131  $pathInfo = [
132  'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
133  ];
134  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
135  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
136  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Filelist/FileReplace');
137  }
138 
142  protected function ‪renderContent(): void
143  {
144  // Assign variables used by the fluid template
145  $assigns = [];
147  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
148  $assigns['moduleUrlTceFile'] = (string)$uriBuilder->buildUriFromRoute('tce_file');
149  $assigns['uid'] = ‪$this->uid;
150  $assigns['returnUrl'] = ‪$this->returnUrl;
151 
152  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
153  // csh button
154  $cshButton = $buttonBar->makeHelpButton()
155  ->setModuleName('xMOD_csh_corebe')
156  ->setFieldName('file_rename');
157  $buttonBar->addButton($cshButton);
158 
159  // Back button
160  if ($this->returnUrl) {
161  $returnButton = $buttonBar->makeLinkButton()
162  ->setHref($this->returnUrl)
163  ->setTitle($this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
164  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', ‪Icon::SIZE_SMALL));
165  $buttonBar->addButton($returnButton);
166  }
167 
168  // Rendering of the output via fluid
169  $view = GeneralUtility::makeInstance(StandaloneView::class);
170  $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
171  $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
172  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
173  'EXT:filelist/Resources/Private/Templates/File/ReplaceFile.html'
174  ));
175  $view->assignMultiple($assigns);
176  $this->moduleTemplate->setContent($view->render());
177  }
178 
182  protected function ‪getLanguageService(): ‪LanguageService
183  {
184  return ‪$GLOBALS['LANG'];
185  }
186 }
‪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:61
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$uid
‪int $uid
Definition: ReplaceFileController.php:43
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\getLanguageService
‪LanguageService getLanguageService()
Definition: ReplaceFileController.php:178
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:194
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$fileOrFolderObject
‪TYPO3 CMS Core Resource ResourceInterface $fileOrFolderObject
Definition: ReplaceFileController.php:49
‪TYPO3\CMS\Filelist\Controller\File
Definition: CreateFolderController.php:18
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\init
‪init(ServerRequestInterface $request)
Definition: ReplaceFileController.php:84
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:43
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
Definition: InsufficientFileAccessPermissionsException.php:24
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪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:69
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\$returnUrl
‪string $returnUrl
Definition: ReplaceFileController.php:55
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController\renderContent
‪renderContent()
Definition: ReplaceFileController.php:138
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Filelist\Controller\File\ReplaceFileController
Definition: ReplaceFileController.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26