[!!!][FEATURE] Refactor and streamline click menu / context menu
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / File / CreateFolderController.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\Resource\Exception\InsufficientFolderAccessPermissionsException;
23 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
24 use TYPO3\CMS\Core\Resource\ResourceFactory;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Core\Utility\MathUtility;
27 use TYPO3\CMS\Fluid\View\StandaloneView;
28
29 /**
30 * Script class for the create-new script
31 *
32 * Displays forms for creating folders (1 to 10), a media asset or a new file.
33 */
34 class CreateFolderController extends AbstractModule
35 {
36 /**
37 * @var int
38 */
39 public $folderNumber = 10;
40
41 /**
42 * Name of the filemount
43 *
44 * @var string
45 */
46 public $title;
47
48 /**
49 * @var int
50 */
51 public $number;
52
53 /**
54 * Set with the target path inputted in &target
55 *
56 * @var string
57 */
58 public $target;
59
60 /**
61 * The folder object which is the target directory
62 *
63 * @var \TYPO3\CMS\Core\Resource\Folder $folderObject
64 */
65 protected $folderObject;
66
67 /**
68 * Return URL of list module.
69 *
70 * @var string
71 */
72 public $returnUrl;
73
74 /**
75 * @var array
76 */
77 protected $pathInfo;
78
79 /**
80 * Accumulating content
81 *
82 * @var string
83 */
84 public $content;
85
86 /**
87 * Constructor
88 */
89 public function __construct()
90 {
91 parent::__construct();
92 $GLOBALS['SOBE'] = $this;
93 $this->init();
94 }
95
96 /**
97 * @throws InsufficientFolderAccessPermissionsException
98 * @throws \RuntimeException
99 */
100 protected function init()
101 {
102 // Initialize GPvars:
103 $this->number = GeneralUtility::_GP('number');
104 $this->target = ($combinedIdentifier = GeneralUtility::_GP('target'));
105 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
106 // create the folder object
107 if ($combinedIdentifier) {
108 $this->folderObject = ResourceFactory::getInstance()
109 ->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
110 }
111 // Cleaning and checking target directory
112 if (!$this->folderObject) {
113 $title = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_file_list.xlf:paramError');
114 $message = $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_file_list.xlf:targetNoDir');
115 throw new \RuntimeException($title . ': ' . $message, 1294586845);
116 }
117 if ($this->folderObject->getStorage()->getUid() === 0) {
118 throw new InsufficientFolderAccessPermissionsException(
119 'You are not allowed to access folders outside your storages',
120 1375889838
121 );
122 }
123
124 $pathInfo = [
125 'combined_identifier' => $this->folderObject->getCombinedIdentifier(),
126 ];
127 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
128 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
129 $this->moduleTemplate->addJavaScriptCode(
130 'CreateFolderInlineJavaScript',
131 'var path = "' . $this->target . '";
132 var confirmTitle = '
133 . GeneralUtility::quoteJSvalue(
134 $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_common.xlf:pleaseConfirm')
135 )
136 . ';
137 var confirmText = '
138 . GeneralUtility::quoteJSvalue(
139 $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:mess.redraw')
140 )
141 . ';
142 function reload(a) {
143 var params = "&target="+encodeURIComponent(path)+"&number="+a+"&returnUrl=' . rawurlencode($this->returnUrl) . '";
144 var url = \'' . BackendUtility::getModuleUrl('file_newfolder') . '\';
145 if (!changed) {
146 window.location.href = url + params;
147 } else {
148 var modal = top.TYPO3.Modal.confirm(confirmTitle, confirmText);
149 modal.on(\'confirm.button.cancel\', function(e) {
150 top.TYPO3.Modal.currentModal.trigger(\'modal-dismiss\');
151 });
152 modal.on(\'confirm.button.ok\', function(e) {
153 top.TYPO3.Modal.currentModal.trigger(\'modal-dismiss\');
154 window.location.href = url + params;
155 });
156 }
157 }
158 function backToList() {
159 top.goToModule("file_FilelistList");
160 }
161 var changed = 0;'
162 );
163 }
164
165 /**
166 * Main function, rendering the main module content
167 *
168 * @return void
169 */
170 public function main()
171 {
172 $lang = $this->getLanguageService();
173 $assigns = [];
174 $assigns['target'] = $this->target;
175 if ($this->folderObject->checkActionPermission('add')) {
176 $assigns['moduleUrlTceFile'] = BackendUtility::getModuleUrl('tce_file');
177 $assigns['cshFileNewFolder'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfolder');
178 // Making the selector box for the number of concurrent folder-creations
179 $this->number = MathUtility::forceIntegerInRange($this->number, 1, 10);
180 for ($a = 1; $a <= $this->folderNumber; $a++) {
181 $options = [];
182 $options['value'] = $a;
183 $options['selected'] = ($this->number == $a ? ' selected="selected"' : '');
184 $assigns['options'][] = $options;
185 }
186 // Making the number of new-folder boxes needed:
187 for ($a = 0; $a < $this->number; $a++) {
188 $folder = [];
189 $folder['this'] = $a;
190 $folder['next'] = $a + 1;
191 $assigns['folders'][] = $folder;
192 }
193 // Making submit button for folder creation:
194 $assigns['returnUrl'] = $this->returnUrl;
195 }
196
197 if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')) {
198 $assigns['moduleUrlOnlineMedia'] = BackendUtility::getModuleUrl('online_media');
199 $assigns['cshFileNewMedia'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newMedia');
200 // Create a list of allowed file extensions with the readable format "youtube, vimeo" etc.
201 $fileExtList = [];
202 $onlineMediaFileExt = OnlineMediaHelperRegistry::getInstance()->getSupportedFileExtensions();
203 foreach ($onlineMediaFileExt as $fileExt) {
204 if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)) {
205 $fileExtList[] = strtoupper(htmlspecialchars($fileExt));
206 }
207 }
208 $assigns['fileExtList'] = $fileExtList;
209
210 $assigns['moduleUrlTceFile'] = BackendUtility::getModuleUrl('tce_file');
211 $assigns['cshFileNewFile'] = BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfile');
212 // Create a list of allowed file extensions with a text format "*.txt, *.css" etc.
213 $fileExtList = [];
214 $textFileExt = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], true);
215 foreach ($textFileExt as $fileExt) {
216 if (GeneralUtility::verifyFilenameAgainstDenyPattern('.' . $fileExt)) {
217 $fileExtList[] = strtoupper(htmlspecialchars($fileExt));
218 }
219 }
220 $assigns['txtFileExtList'] = $fileExtList;
221 }
222
223 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
224 // CSH button
225 $helpButton = $buttonBar->makeHelpButton()
226 ->setFieldName('file_new')
227 ->setModuleName('xMOD_csh_corebe');
228 $buttonBar->addButton($helpButton);
229
230 // Back
231 if ($this->returnUrl) {
232 $backButton = $buttonBar->makeLinkButton()
233 ->setHref($this->returnUrl)
234 ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
235 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
236 $buttonBar->addButton($backButton);
237 }
238
239 // Rendering of the output via fluid
240 $view = GeneralUtility::makeInstance(StandaloneView::class);
241 $view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
242 $view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
243 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
244 'EXT:backend/Resources/Private/Templates/File/CreateFolder.html'
245 ));
246 $view->assignMultiple($assigns);
247 $this->content = $view->render();
248 $this->moduleTemplate->setContent($this->content);
249 }
250
251 /**
252 * Processes the request, currently everything is handled and put together via "main()"
253 *
254 * @param ServerRequestInterface $request the current request
255 * @param ResponseInterface $response
256 * @return ResponseInterface the response with the content
257 */
258 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
259 {
260 $this->main();
261 $response->getBody()->write($this->moduleTemplate->renderContent());
262 return $response;
263 }
264
265 /**
266 * Returns LanguageService
267 *
268 * @return \TYPO3\CMS\Lang\LanguageService
269 */
270 protected function getLanguageService()
271 {
272 return $GLOBALS['LANG'];
273 }
274 }