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