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