[TASK] Call explicit render() on icon objects
[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\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
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 {
28
29 /**
30 * Document template object
31 *
32 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
33 * @internal
34 */
35 public $doc;
36
37 /**
38 * Name of the filemount
39 *
40 * @var string
41 */
42 public $title;
43
44 /**
45 * Target path
46 *
47 * @var string
48 * @internal
49 */
50 public $target;
51
52 /**
53 * The file or folder object that should be renamed
54 *
55 * @var \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\Folder $fileOrFolderObject
56 */
57 protected $fileOrFolderObject;
58
59 /**
60 * Return URL of list module.
61 *
62 * @var string
63 */
64 public $returnUrl;
65
66 /**
67 * Accumulating content
68 *
69 * @var string
70 * @internal
71 */
72 public $content;
73
74 /**
75 * Constructor
76 */
77 public function __construct() {
78 $GLOBALS['SOBE'] = $this;
79 $this->init();
80 }
81
82 /**
83 * Initialize
84 *
85 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException
86 */
87 protected function init() {
88 // Initialize GPvars:
89 $this->target = GeneralUtility::_GP('target');
90 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
91 // Cleaning and checking target
92 if ($this->target) {
93 $this->fileOrFolderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->retrieveFileOrFolderObject($this->target);
94 }
95 if (!$this->fileOrFolderObject) {
96 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
97 $message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
98 throw new \RuntimeException($title . ': ' . $message, 1294586844);
99 }
100 if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
101 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException('You are not allowed to access files outside your storages', 1375889840);
102 }
103
104 // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
105 // so the redirect will NOT end in an error message
106 // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
107 // rename the folder
108 if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
109 $parsedUrl = parse_url($this->returnUrl);
110 $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
111 if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
112 $this->returnUrl = str_replace(urlencode($queryParts['id']), urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()), $this->returnUrl);
113 }
114 }
115 // Setting icon and title
116 /** @var IconFactory $iconFactory */
117 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
118 $icon = $iconFactory->getIcon('apps-filetree-root', Icon::SIZE_SMALL)->render();
119 $this->title = $icon . htmlspecialchars($this->fileOrFolderObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->fileOrFolderObject->getIdentifier());
120 // Setting template object
121 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
122 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_rename.html');
123 $this->doc->JScode = $this->doc->wrapScriptTags('
124 function backToList() {
125 top.goToModule("file_FilelistList");
126 }
127 ');
128 }
129
130 /**
131 * Main function, rendering the content of the rename form
132 *
133 * @return void
134 */
135 public function main() {
136 // Make page header:
137 $this->content = $this->doc->startPage($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle'));
138 $pageContent = $this->doc->header($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.pagetitle'));
139 if ($this->fileOrFolderObject instanceof \TYPO3\CMS\Core\Resource\Folder) {
140 $fileIdentifier = $this->fileOrFolderObject->getCombinedIdentifier();
141 } else {
142 $fileIdentifier = $this->fileOrFolderObject->getUid();
143 }
144 $pageContent .= '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform" role="form">';
145 // Making the formfields for renaming:
146 $pageContent .= '
147
148 <div class="form-group">
149 <input class="form-control" type="text" name="file[rename][0][target]" value="' . htmlspecialchars($this->fileOrFolderObject->getName()) . '" ' . $this->getDocumentTemplate()->formWidth(40) . ' />
150 <input type="hidden" name="file[rename][0][data]" value="' . htmlspecialchars($fileIdentifier) . '" />
151 </div>
152 ';
153 // Making submit button:
154 $pageContent .= '
155 <div class="form-group">
156 <input class="btn btn-primary" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:file_rename.php.submit', TRUE) . '" />
157 <input class="btn btn-danger" type="submit" value="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', TRUE) . '" onclick="backToList(); return false;" />
158 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
159 </div>
160 ';
161 $pageContent .= '</form>';
162 $docHeaderButtons = array(
163 'back' => ''
164 );
165 $docHeaderButtons['csh'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_rename');
166 // Back
167 if ($this->returnUrl) {
168 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
169 $docHeaderButtons['back'] = '<a href="' . htmlspecialchars(\TYPO3\CMS\Core\Utility\GeneralUtility::linkThisUrl($this->returnUrl)) . '" class="typo3-goBack" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', TRUE) . '">' . $iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL)->render() . '</a>';
170 }
171 // Add the HTML as a section:
172 $markerArray = array(
173 'CSH' => $docHeaderButtons['csh'],
174 'FUNC_MENU' => '',
175 'CONTENT' => $pageContent,
176 'PATH' => $this->title
177 );
178 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
179 $this->content .= $this->doc->endPage();
180 $this->content = $this->doc->insertStylesAndJS($this->content);
181 }
182
183 /**
184 * Processes the request, currently everything is handled and put together via "main()"
185 *
186 * @param ServerRequestInterface $request the current request
187 * @param ResponseInterface $response
188 * @return ResponseInterface the response with the content
189 */
190 public function mainAction(ServerRequestInterface $request, ResponseInterface $response) {
191 $this->main();
192
193 $response->getBody()->write($this->content);
194 return $response;
195 }
196
197 /**
198 * Outputting the accumulated content to screen
199 *
200 * @return void
201 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use the mainAction() method instead
202 */
203 public function printContent() {
204 GeneralUtility::logDeprecatedFunction();
205 echo $this->content;
206 }
207
208 /**
209 * Returns LanguageService
210 *
211 * @return \TYPO3\CMS\Lang\LanguageService
212 */
213 protected function getLanguageService() {
214 return $GLOBALS['LANG'];
215 }
216
217 /**
218 * Returns an instance of DocumentTemplate
219 *
220 * @return \TYPO3\CMS\Backend\Template\DocumentTemplate
221 */
222 protected function getDocumentTemplate() {
223 return $GLOBALS['TBE_TEMPLATE'];
224 }
225
226 }