[TASK] Replace sprite icon "apps-filetree-root" with the new IconFactory
[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 TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use TYPO3\CMS\Core\Http\Response;
24
25 /**
26 * Script Class for the create-new script; Displays a form for creating up to 10 folders or one new text file
27 */
28 class CreateFolderController implements \TYPO3\CMS\Core\Http\ControllerInterface {
29
30 /**
31 * @var int
32 */
33 public $folderNumber = 10;
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 * @var int
51 */
52 public $number;
53
54 /**
55 * Set with the target path inputted in &target
56 *
57 * @var string
58 */
59 public $target;
60
61 /**
62 * The folder object which is the target directory
63 *
64 * @var \TYPO3\CMS\Core\Resource\Folder $folderObject
65 */
66 protected $folderObject;
67
68 /**
69 * Return URL of list module.
70 *
71 * @var string
72 */
73 public $returnUrl;
74
75 /**
76 * Accumulating content
77 *
78 * @var string
79 */
80 public $content;
81
82 /**
83 * Constructor
84 */
85 public function __construct() {
86 $GLOBALS['SOBE'] = $this;
87 $this->init();
88 }
89
90 /**
91 * Initialize
92 *
93 * @throws \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
94 * @return void
95 */
96 protected function init() {
97 // Initialize GPvars:
98 $this->number = GeneralUtility::_GP('number');
99 $this->target = ($combinedIdentifier = GeneralUtility::_GP('target'));
100 $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
101 // create the folder object
102 if ($combinedIdentifier) {
103 $this->folderObject = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
104 }
105 // Cleaning and checking target directory
106 if (!$this->folderObject) {
107 $title = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', TRUE);
108 $message = $this->getLanguageService()->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', TRUE);
109 throw new \RuntimeException($title . ': ' . $message, 1294586845);
110 }
111 if ($this->folderObject->getStorage()->getUid() === 0) {
112 throw new \TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException('You are not allowed to access folders outside your storages', 1375889838);
113 }
114
115 // Setting the title and the icon
116 /** @var IconFactory $iconFactory */
117 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
118 $icon = $iconFactory->getIcon('apps-filetree-root', Icon::SIZE_SMALL);
119 $this->title = $icon . htmlspecialchars($this->folderObject->getStorage()->getName()) . ': ' . htmlspecialchars($this->folderObject->getIdentifier());
120 // Setting template object
121 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
122 $this->doc->setModuleTemplate('EXT:backend/Resources/Private/Templates/file_newfolder.html');
123 $this->doc->JScode = $this->doc->wrapScriptTags('
124 var path = "' . $this->target . '";
125
126 function reload(a) {
127 if (!changed || (changed && confirm(' . GeneralUtility::quoteJSvalue($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:mess.redraw')) . '))) {
128 var params = "&target="+encodeURIComponent(path)+"&number="+a+"&returnUrl=' . rawurlencode($this->returnUrl) . '";
129 window.location.href = ' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('file_newfolder')) . '+params;
130 }
131 }
132 function backToList() {
133 top.goToModule("file_FilelistList");
134 }
135
136 var changed = 0;
137 ');
138 }
139
140 /**
141 * Main function, rendering the main module content
142 *
143 * @return void
144 */
145 public function main() {
146 $lang = $this->getLanguageService();
147 // Start content compilation
148 $this->content .= $this->doc->startPage($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.pagetitle'));
149 // Make page header:
150 $pageContent = $this->doc->header($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.pagetitle'));
151 if ($this->folderObject->checkActionPermission('add')) {
152 $code = '<form role="form" action="' . htmlspecialchars(BackendUtility::getModuleUrl('tce_file')) . '" method="post" name="editform">';
153 // Making the selector box for the number of concurrent folder-creations
154 $this->number = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->number, 1, 10);
155 $code .= '
156 <div class="form-group">
157 <div class="form-section">
158 <div class="form-group">
159 <label for="number-of-new-folders">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.number_of_folders') . ' ' . BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfolder') . '</label>
160 <div class="form-control-wrap">
161 <div class="input-group">
162 <select class="form-control form-control-adapt" name="number" id="number-of-new-folders" onchange="reload(this.options[this.selectedIndex].value);">';
163 for ($a = 1; $a <= $this->folderNumber; $a++) {
164 $code .= '<option value="' . $a . '"' . ($this->number == $a ? ' selected="selected"' : '') . '>' . $a . '</option>';
165 }
166 $code .= '
167 </select>
168 </div>
169 </div>
170 </div>
171 </div>
172 ';
173 // Making the number of new-folder boxes needed:
174 for ($a = 0; $a < $this->number; $a++) {
175 $code .= '
176 <div class="form-section">
177 <div class="form-group">
178 <label for="folder_new_' . $a . '">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.label_newfolder') . ' ' . ($a + 1) . ':</label>
179 <div class="form-control-wrap">
180 <input type="text" class="form-control" id="folder_new_' . $a . '" name="file[newfolder][' . $a . '][data]" onchange="changed=true;" />
181 <input type="hidden" name="file[newfolder][' . $a . '][target]" value="' . htmlspecialchars($this->target) . '" />
182 </div>
183 </div>
184 </div>';
185 }
186 // Making submit button for folder creation:
187 $code .= '
188 </div><div class="form-group">
189 <input class="btn btn-default" type="submit" value="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.submit', TRUE) . '" />
190 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
191 </div>
192 ';
193 // Switching form tags:
194 $pageContent .= $this->doc->section($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.newfolders'), $code);
195 $pageContent .= $this->doc->sectionEnd() . '</form>';
196 }
197
198 if ($this->folderObject->getStorage()->checkUserActionPermission('add', 'File')) {
199 $pageContent .= '<form action="' . BackendUtility::getModuleUrl('tce_file') . '" method="post" name="editform2">';
200 // Create a list of allowed file extensions with the nice format "*.jpg, *.gif" etc.
201 $fileExtList = array();
202 $textFileExt = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], TRUE);
203 foreach ($textFileExt as $fileExt) {
204 if (!preg_match(('/' . $GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'] . '/i'), ('.' . $fileExt))) {
205 $fileExtList[] = '<span class="label label-success">' . strtoupper(htmlspecialchars($fileExt)) . '</span>';
206 }
207 }
208 // Add form fields for creation of a new, blank text file:
209 $code = '
210 <div class="form-group">
211 <div class="form-section">
212 <div class="form-group">
213 <label for="newfile">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.label_newfile') . ' ' . BackendUtility::cshItem('xMOD_csh_corebe', 'file_newfile') . '</label>
214 <div class="form-control-wrap">
215 <input class="form-control" type="text" id="newfile" name="file[newfile][0][data]" onchange="changed=true;" />
216 <input type="hidden" name="file[newfile][0][target]" value="' . htmlspecialchars($this->target) . '" />
217 </div>
218 <div class="help-block">
219 ' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:cm.allowedFileExtensions') . '<br>
220 ' . implode(' ', $fileExtList) . '
221 </div>
222 </div>
223 </div>
224 </div>
225 ';
226 // Submit button for creation of a new file:
227 $code .= '
228 <div class="form-group">
229 <input class="btn btn-default" type="submit" value="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.newfile_submit', TRUE) . '" />
230 <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
231 </div>
232 ';
233 $pageContent .= $this->doc->section($lang->sL('LLL:EXT:lang/locallang_core.xlf:file_newfolder.php.newfile'), $code);
234 $pageContent .= $this->doc->sectionEnd();
235 $pageContent .= '</form>';
236 }
237
238 $docHeaderButtons = array(
239 'back' => ''
240 );
241 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
242 // Back
243 if ($this->returnUrl) {
244 $docHeaderButtons['back'] = '<a href="' . htmlspecialchars(GeneralUtility::linkThisUrl($this->returnUrl)) . '" class="typo3-goBack" title="' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', TRUE) . '">' . $iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL) . '</a>';
245 }
246 // Add the HTML as a section:
247 $markerArray = array(
248 'CSH' => $docHeaderButtons['csh'],
249 'FUNC_MENU' => '',
250 'CONTENT' => $pageContent,
251 'PATH' => $this->title
252 );
253 $this->content .= $this->doc->moduleBody(array(), $docHeaderButtons, $markerArray);
254 $this->content .= $this->doc->endPage();
255 $this->content = $this->doc->insertStylesAndJS($this->content);
256 }
257
258 /**
259 * Processes the request, currently everything is handled and put together via "main()"
260 *
261 * @param ServerRequestInterface $request The request object
262 * @return ResponseInterface $response The response, created by the controller
263 */
264 public function processRequest(ServerRequestInterface $request) {
265 $this->main();
266 /** @var Response $response */
267 $response = GeneralUtility::makeInstance(Response::class);
268 $response->getBody()->write($this->content);
269 return $response;
270 }
271
272 /**
273 * Outputting the accumulated content to screen
274 *
275 * @return void
276 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use the processRequest() method instead
277 */
278 public function printContent() {
279 GeneralUtility::logDeprecatedFunction();
280 echo $this->content;
281 }
282
283 /**
284 * Returns LanguageService
285 *
286 * @return \TYPO3\CMS\Lang\LanguageService
287 */
288 protected function getLanguageService() {
289 return $GLOBALS['LANG'];
290 }
291
292 }