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