TYPO3  7.6
RenameFileController.php
Go to the documentation of this file.
1 <?php
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 
23 
28 {
29 
35  public $title;
36 
43  public $target;
44 
51 
57  public $returnUrl;
58 
65  public $content;
66 
70  public function __construct()
71  {
72  parent::__construct();
73  $GLOBALS['SOBE'] = $this;
74  $this->init();
75  }
76 
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 
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 .= '<div>' . $pageContent . '</div>';
188  $this->moduleTemplate->setContent($this->content);
189  }
190 
199  {
200  $this->main();
201  $response->getBody()->write($this->moduleTemplate->renderContent());
202  return $response;
203  }
204 
210  protected function getLanguageService()
211  {
212  return $GLOBALS['LANG'];
213  }
214 
220  protected function getDocumentTemplate()
221  {
222  return $GLOBALS['TBE_TEMPLATE'];
223  }
224 }