[TASK] Use ModuleTemplateAPI for Controller\File\RenameFileController
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / RenameFileController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Controller\File;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Module\AbstractModule;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23
24 /**
25 * Script Class for the rename-file form.
26 */
27 class RenameFileController extends AbstractModule
28 {
29
30 /**
31 * Name of the filemount
32 *
33 * @var string
34 */
35 public $title;
36
37 /**
38 * Target path
39 *
40 * @var string
41 * @internal
42 */
43 public $target;
44
45 /**
46 * The file or folder object that should be renamed
47 *
48 * @var \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder $fileOrFolderObject
49 */
50 protected $fileOrFolderObject;
51
52 /**
53 * Return URL of list module.
54 *
55 * @var string
56 */
57 public $returnUrl;
58
59 /**
60 * Accumulating content
61 *
62 * @var string
63 * @internal
64 */
65 public $content;
66
67 /**
68 * Constructor
69 */
70 public function __construct()
71 {
72 parent::__construct();
73 $GLOBALS['SOBE'] = $this;
74 $this->init();
75 }
76
77 /**
78 * Initialize
79 *
80 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
81 */
82 protected function init()
83 {
84 // Initialize GPvars:
85 $this->target = GeneralUtility::_GP('target');
86 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
87 // Cleaning and checking target
88 if ($this->target) {
89 $this->fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->target);
90 }
91 if (!$this->fileOrFolderObject) {
92 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', true);
93 $message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', true);
94 throw new \RuntimeException($title . ': ' . $message, 1294586844);
95 }
96 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
97 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889840);
98 }
99
100 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
101 // so the redirect will NOT end in an error message
102 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
103 // rename the folder
104 if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
105 $parsedUrl = parse_url($this->returnUrl);
106 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
107 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
108 $this->returnUrl = str_replace(urlencode($queryParts['id']),
109 urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
110 $this->returnUrl);
111 }
112 }
113
114 // building pathInfo for metaInformation
115 $pathInfo = [
116 'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
117 ];
118 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
119
120 // Setting up the context sensitive menu
121 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ClickMenu');
122
123 // Add javaScript
124 $this->moduleTemplate->addJavaScriptCode(
125 'RenameFileInlineJavaScript',
126 'function backToList() {top.goToModule("file_FilelistList");}'
127 );
128
129 }
130
131 /**
132 * Main function, rendering the content of the rename form
133 *
134 * @return void
135 */
136 public function main()
137 {
138 if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
139 $fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
140 } else {
141 $fileIdentifier = $this->fileOrFolderObject->getUid();
142 }
143 $pageContent = '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform" role="form">';
144 // Making the formfields for renaming:
145 $pageContent .= '
146
147 <div class="form-group">
148 <input class="form-control" type="text" name="file[rename][0][target]" value="' . htmlspecialchars($this->fileOrFolderObject->getName()) . '" ' . $this->getDocumentTemplate()->formWidth(40) . ' />
149 <input type="hidden" name="file[rename][0][data]" value="' . htmlspecialchars($fileIdentifier) . '" />
150 </div>
151 ';
152 // Making submit button:
153 $pageContent .= '
154 <div class="form-group">
155 <input class="btn btn-primary" type="submit" value="' .
156 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.submit', true) . '" />
157 <input class="btn btn-danger" type="submit" value="' .
158 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', true) .
159 '" onclick="backToList(); return false;" />
160 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
161 </div>
162 ';
163 $pageContent .= '</form>';
164
165 // Create buttons
166 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
167
168 // csh button
169 $cshButton = $buttonBar->makeHelpButton()
170 ->setModuleName('xMOD_csh_corebe')
171 ->setFieldName('file_rename');
172 $buttonBar->addButton($cshButton);
173
174 // back button
175 if ($this->returnUrl) {
176 $backButton = $buttonBar->makeLinkButton()
177 ->sethref(GeneralUtility::linkThisUrl($this->returnUrl))
178 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', true))
179 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
180 $buttonBar->addButton($backButton);
181 }
182
183 // set header
184 $this->content = '<h1>' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle') . '</h1>';
185
186 // add section
187 $this->content .= $this->moduleTemplate->section('', $pageContent);
188 $this->moduleTemplate->setContent($this->content);
189 }
190
191 /**
192 * Processes the request, currently everything is handled and put together via "main()"
193 *
194 * @param ServerRequestInterface $request the current request
195 * @param ResponseInterface $response
196 * @return ResponseInterface the response with the content
197 */
198 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
199 {
200 $this->main();
201 $response->getBody()->write($this->moduleTemplate->renderContent());
202 return $response;
203 }
204
205 /**
206 * Returns LanguageService
207 *
208 * @return \TYPO3\CMS\Lang\LanguageService
209 */
210 protected function getLanguageService()
211 {
212 return $GLOBALS['LANG'];
213 }
214
215 /**
216 * Returns an instance of DocumentTemplate
217 *
218 * @return \TYPO3\CMS\Backend\Template\DocumentTemplate
219 */
220 protected function getDocumentTemplate()
221 {
222 return $GLOBALS['TBE_TEMPLATE'];
223 }
224 }