[BUGFIX] Filelist: Only show paste buttons if clipObj is set
[Packages/TYPO3.CMS.git] / typo3 / sysext / filelist / Classes / Controller / FileListController.php
1 <?php
2 namespace TYPO3\CMS\Filelist\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Script Class for creating the list of files in the File > Filelist module
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 class FileListController {
36
37 /**
38 * Module configuration
39 *
40 * @todo Define visibility
41 */
42 public $MCONF = array();
43
44 /**
45 * @todo Define visibility
46 */
47 public $MOD_MENU = array();
48
49 /**
50 * @todo Define visibility
51 */
52 public $MOD_SETTINGS = array();
53
54 /**
55 * Accumulated HTML output
56 *
57 * @todo Define visibility
58 */
59 public $content;
60
61 /**
62 * Document template object
63 *
64 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
65 * @todo Define visibility
66 */
67 public $doc;
68
69 /**
70 * "id" -> the path to list.
71 *
72 * @todo Define visibility
73 */
74 public $id;
75
76 /**
77 * @var \TYPO3\CMS\Core\Resource\Folder
78 */
79 protected $folderObject;
80
81 /**
82 * @var \TYPO3\CMS\Core\Messaging\FlashMessage
83 */
84 protected $errorMessage;
85
86 /**
87 * Pointer to listing
88 *
89 * @todo Define visibility
90 */
91 public $pointer;
92
93 /**
94 * "Table"
95 *
96 * @todo Define visibility
97 */
98 public $table;
99
100 /**
101 * Thumbnail mode.
102 *
103 * @todo Define visibility
104 */
105 public $imagemode;
106
107 /**
108 * @todo Define visibility
109 */
110 public $cmd;
111
112 /**
113 * @todo Define visibility
114 */
115 public $overwriteExistingFiles;
116
117 /**
118 * The file list object
119 *
120 * @var \TYPO3\CMS\Filelist\FileList
121 */
122 public $filelist = NULL;
123
124 /**
125 * Initialize variables, file object
126 * Incoming GET vars include id, pointer, table, imagemode
127 *
128 * @return void
129 * @todo Define visibility
130 */
131 public function init() {
132 // Setting GPvars:
133 $this->id = ($combinedIdentifier = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('id'));
134 $this->pointer = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('pointer');
135 $this->table = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('table');
136 $this->imagemode = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('imagemode');
137 $this->cmd = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('cmd');
138 $this->overwriteExistingFiles = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('overwriteExistingFiles');
139 // Setting module name:
140 $this->MCONF = $GLOBALS['MCONF'];
141 try {
142 if ($combinedIdentifier) {
143 /** @var $fileFactory \TYPO3\CMS\Core\Resource\ResourceFactory */
144 $fileFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
145 $this->folderObject = $fileFactory->getFolderObjectFromCombinedIdentifier($combinedIdentifier);
146 // Disallow the rendering of the processing folder (e.g. could be called manually)
147 // and all folders without any defined storage
148 if ($this->folderObject && ($this->folderObject->getStorage()->getUid() == 0 || trim($this->folderObject->getStorage()->getProcessingFolder()->getIdentifier(), '/') === trim($this->folderObject->getIdentifier(), '/'))) {
149 $storage = $fileFactory->getStorageObjectFromCombinedIdentifier($combinedIdentifier);
150 $this->folderObject = $storage->getRootLevelFolder();
151 }
152 } else {
153 // Take the first object of the first storage
154 $fileStorages = $GLOBALS['BE_USER']->getFileStorages();
155 $fileStorage = reset($fileStorages);
156 if ($fileStorage) {
157 // Validating the input "id" (the path, directory!) and
158 // checking it against the mounts of the user. - now done in the controller
159 $this->folderObject = $fileStorage->getRootLevelFolder();
160 } else {
161 throw new \RuntimeException('Could not find any folder to be displayed.', 1349276894);
162 }
163 }
164 } catch (\TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException $fileException) {
165 // Set folder object to null and throw a message later on
166 $this->folderObject = NULL;
167 $this->errorMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
168 sprintf($GLOBALS['LANG']->getLL('folderNotFoundMessage', TRUE),
169 htmlspecialchars($this->id)
170 ),
171 $GLOBALS['LANG']->getLL('folderNotFoundTitle', TRUE),
172 \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE
173 );
174 }
175 // Configure the "menu" - which is used internally to save the values of sorting, displayThumbs etc.
176 $this->menuConfig();
177 }
178
179 /**
180 * Setting the menu/session variables
181 *
182 * @return void
183 * @todo Define visibility
184 */
185 public function menuConfig() {
186 // MENU-ITEMS:
187 // If array, then it's a selector box menu
188 // If empty string it's just a variable, that'll be saved.
189 // Values NOT in this array will not be saved in the settings-array for the module.
190 $this->MOD_MENU = array(
191 'sort' => '',
192 'reverse' => '',
193 'displayThumbs' => '',
194 'clipBoard' => '',
195 'bigControlPanel' => ''
196 );
197 // CLEANSE SETTINGS
198 $this->MOD_SETTINGS = \TYPO3\CMS\Backend\Utility\BackendUtility::getModuleData(
199 $this->MOD_MENU,
200 \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('SET'),
201 $this->MCONF['name']
202 );
203 }
204
205 /**
206 * Main function, creating the listing
207 *
208 * @return void
209 * @todo Define visibility
210 */
211 public function main() {
212 // Initialize the template object
213 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Template\\DocumentTemplate');
214 $this->doc->backPath = $GLOBALS['BACK_PATH'];
215 $this->doc->setModuleTemplate('EXT:filelist/Resources/Private/Templates/file_list.html');
216 $this->doc->getPageRenderer()->loadPrototype();
217 // There there was access to this file path, continue, make the list
218 if ($this->folderObject) {
219
220 // Create filelisting object
221 $this->filelist = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Filelist\\FileList');
222 $this->filelist->backPath = $GLOBALS['BACK_PATH'];
223 // Apply predefined values for hidden checkboxes
224 // Set predefined value for DisplayBigControlPanel:
225 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'activated') {
226 $this->MOD_SETTINGS['bigControlPanel'] = TRUE;
227 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'deactivated') {
228 $this->MOD_SETTINGS['bigControlPanel'] = FALSE;
229 }
230 // Set predefined value for DisplayThumbnails:
231 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'activated') {
232 $this->MOD_SETTINGS['displayThumbs'] = TRUE;
233 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'deactivated') {
234 $this->MOD_SETTINGS['displayThumbs'] = FALSE;
235 }
236 // Set predefined value for Clipboard:
237 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'activated') {
238 $this->MOD_SETTINGS['clipBoard'] = TRUE;
239 } elseif ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'deactivated') {
240 $this->MOD_SETTINGS['clipBoard'] = FALSE;
241 }
242 // If user never opened the list module, set the value for displayThumbs
243 if (!isset($this->MOD_SETTINGS['displayThumbs'])) {
244 $this->MOD_SETTINGS['displayThumbs'] = $GLOBALS['BE_USER']->uc['thumbnailsByDefault'];
245 }
246 $this->filelist->thumbs = $this->MOD_SETTINGS['displayThumbs'];
247 // Create clipboard object and initialize that
248 $this->filelist->clipObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Clipboard\\Clipboard');
249 $this->filelist->clipObj->fileMode = 1;
250 $this->filelist->clipObj->initializeClipboard();
251 $CB = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('CB');
252 if ($this->cmd == 'setCB') {
253 $CB['el'] = $this->filelist->clipObj->cleanUpCBC(array_merge(\TYPO3\CMS\Core\Utility\GeneralUtility::_POST('CBH'), \TYPO3\CMS\Core\Utility\GeneralUtility::_POST('CBC')), '_FILE');
254 }
255 if (!$this->MOD_SETTINGS['clipBoard']) {
256 $CB['setP'] = 'normal';
257 }
258 $this->filelist->clipObj->setCmd($CB);
259 $this->filelist->clipObj->cleanCurrent();
260 // Saves
261 $this->filelist->clipObj->endClipboard();
262 // If the "cmd" was to delete files from the list (clipboard thing), do that:
263 if ($this->cmd == 'delete') {
264 $items = $this->filelist->clipObj->cleanUpCBC(\TYPO3\CMS\Core\Utility\GeneralUtility::_POST('CBC'), '_FILE', 1);
265 if (count($items)) {
266 // Make command array:
267 $FILE = array();
268 foreach ($items as $v) {
269 $FILE['delete'][] = array('data' => $v);
270 }
271 // Init file processing object for deleting and pass the cmd array.
272 $fileProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtility');
273 $fileProcessor->init($GLOBALS['FILEMOUNTS'], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
274 $fileProcessor->init_actionPerms($GLOBALS['BE_USER']->getFileoperationPermissions());
275 $fileProcessor->dontCheckForUnique = $this->overwriteExistingFiles ? 1 : 0;
276 $fileProcessor->start($FILE);
277 $fileProcessor->processData();
278 $fileProcessor->printLogErrorMessages();
279 }
280 }
281 if (!isset($this->MOD_SETTINGS['sort'])) {
282 // Set default sorting
283 $this->MOD_SETTINGS['sort'] = 'file';
284 $this->MOD_SETTINGS['reverse'] = 0;
285 }
286 // Start up filelisting object, include settings.
287 $this->pointer = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($this->pointer, 0, 100000);
288 $this->filelist->start($this->folderObject, $this->pointer, $this->MOD_SETTINGS['sort'], $this->MOD_SETTINGS['reverse'], $this->MOD_SETTINGS['clipBoard'], $this->MOD_SETTINGS['bigControlPanel']);
289 // Generate the list
290 $this->filelist->generateList();
291 // Write the footer
292 $this->filelist->writeBottom();
293 // Set top JavaScript:
294 $this->doc->JScode = $this->doc->wrapScriptTags('
295
296 if (top.fsMod) top.fsMod.recentIds["file"] = unescape("' . rawurlencode($this->id) . '");
297 function jumpToUrl(URL) { //
298 window.location.href = URL;
299 }
300
301 ' . $this->filelist->CBfunctions());
302 // This will return content necessary for the context sensitive clickmenus to work: bodytag events, JavaScript functions and DIV-layers.
303 $this->doc->getContextMenuCode();
304 // Setting up the buttons and markers for docheader
305 list($buttons, $otherMarkers) = $this->filelist->getButtonsAndOtherMarkers($this->folderObject);
306 // add the folder info to the marker array
307 $otherMarkers['FOLDER_INFO'] = $this->filelist->getFolderInfo();
308 $docHeaderButtons = array_merge($this->getButtons(), $buttons);
309
310 // Build the <body> for the module
311 $moduleHeadline = $this->getModuleHeadline();
312 // Create output
313 $pageContent = $moduleHeadline !== '' ? '<h1>' . $moduleHeadline . '</h1>' : '';
314
315 $pageContent .= '<form action="' . htmlspecialchars($this->filelist->listURL()) . '" method="post" name="dblistForm">';
316 $pageContent .= $this->filelist->HTMLcode;
317 $pageContent .= '<input type="hidden" name="cmd" /></form>';
318
319 // Making listing options:
320 if ($this->filelist->HTMLcode) {
321 $pageContent .= '
322
323 <!--
324 Listing options for extended view, clipboard and thumbnails
325 -->
326 <div id="typo3-listOptions">
327 ';
328 // Add "display bigControlPanel" checkbox:
329 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayBigControlPanel') === 'selectable') {
330 $pageContent .= \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->id, 'SET[bigControlPanel]', $this->MOD_SETTINGS['bigControlPanel'], '', '', 'id="bigControlPanel"') . '<label for="bigControlPanel"> ' . $GLOBALS['LANG']->getLL('bigControlPanel', TRUE) . '</label><br />';
331 }
332 // Add "display thumbnails" checkbox:
333 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableDisplayThumbnails') === 'selectable') {
334 $pageContent .= \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->id, 'SET[displayThumbs]', $this->MOD_SETTINGS['displayThumbs'], '', '', 'id="checkDisplayThumbs"') . ' <label for="checkDisplayThumbs">' . $GLOBALS['LANG']->getLL('displayThumbs', TRUE) . '</label><br />';
335 }
336 // Add "clipboard" checkbox:
337 if ($GLOBALS['BE_USER']->getTSConfigVal('options.file_list.enableClipBoard') === 'selectable') {
338 $pageContent .= \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncCheck($this->id, 'SET[clipBoard]', $this->MOD_SETTINGS['clipBoard'], '', '', 'id="checkClipBoard"') . ' <label for="checkClipBoard">' . $GLOBALS['LANG']->getLL('clipBoard', TRUE) . '</label>';
339 }
340 $pageContent .= '
341 </div>
342 ';
343 // Set clipboard:
344 if ($this->MOD_SETTINGS['clipBoard']) {
345 $pageContent .= $this->filelist->clipObj->printClipboard();
346 $pageContent .= \TYPO3\CMS\Backend\Utility\BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_clipboard', $GLOBALS['BACK_PATH']);
347 }
348 }
349 $markerArray = array(
350 'CSH' => $docHeaderButtons['csh'],
351 'FUNC_MENU' => \TYPO3\CMS\Backend\Utility\BackendUtility::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function']),
352 'CONTENT' => ($this->errorMessage ? $this->errorMessage->render() : '') . $pageContent
353 );
354 $this->content = $this->doc->moduleBody(array(), $docHeaderButtons, array_merge($markerArray, $otherMarkers));
355 // Renders the module page
356 $this->content = $this->doc->render($GLOBALS['LANG']->getLL('files'), $this->content);
357 } else {
358 $content = '';
359 if ($this->errorMessage) {
360 $this->errorMessage->setSeverity(\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
361 $content = $this->doc->moduleBody(array(), array_merge(array('LEVEL_UP' => '', 'REFRESH' => ''), $this->getButtons()), array('CSH' => '', 'TITLE' => '', 'FOLDER_INFO' => '', 'PAGE_ICON' => '', 'FUNC_MENU' => '', 'CONTENT' => $this->errorMessage->render()));
362 }
363 // Create output - no access (no warning though)
364 $this->content = $this->doc->render($GLOBALS['LANG']->getLL('files'), $content);
365 }
366 }
367
368 /**
369 * Get main headline based on active folder or storage for backend module
370 *
371 * Folder names are resolved to their special names like done in the tree view.
372 *
373 * @return string
374 */
375 protected function getModuleHeadline() {
376 $name = $this->folderObject->getName();
377 if ($name === '') {
378 // Show storage name on storage root
379 if ($this->folderObject->getIdentifier() === '/') {
380 $name = $this->folderObject->getStorage()->getName();
381 }
382 } else {
383 $name = key(\TYPO3\CMS\Core\Resource\Utility\ListUtility::resolveSpecialFolderNames(
384 array($name => $this->folderObject)
385 ));
386 }
387 return $name;
388 }
389
390 /**
391 * Outputting the accumulated content to screen
392 *
393 * @return void
394 * @todo Define visibility
395 */
396 public function printContent() {
397 echo $this->content;
398 }
399
400 /**
401 * Create the panel of buttons for submitting the form or otherwise perform operations.
402 *
403 * @return array All available buttons as an assoc. array
404 * @todo Define visibility
405 */
406 public function getButtons() {
407 $buttons = array(
408 'csh' => '',
409 'shortcut' => '',
410 'upload' => '',
411 'new' => ''
412 );
413 // Add shortcut
414 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
415 $buttons['shortcut'] = $this->doc->makeShortcutIcon('pointer,id,target,table', implode(',', array_keys($this->MOD_MENU)), $this->MCONF['name']);
416 }
417 // FileList Module CSH:
418 $buttons['csh'] = \TYPO3\CMS\Backend\Utility\BackendUtility::cshItem('xMOD_csh_corebe', 'filelist_module', $GLOBALS['BACK_PATH'], '', TRUE);
419 // Upload button (only if upload to this directory is allowed)
420 if ($this->folderObject && $this->folderObject->getStorage()->checkUserActionPermission('upload', 'File') && $this->folderObject->checkActionPermission('write')) {
421 $buttons['upload'] = '<a href="' . $GLOBALS['BACK_PATH'] . 'file_upload.php?target=' . rawurlencode($this->folderObject->getCombinedIdentifier()) . '&amp;returnUrl=' . rawurlencode($this->filelist->listURL()) . '" id="button-upload" title="' . $GLOBALS['LANG']->makeEntities($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:cm.upload', 1)) . '">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-edit-upload') . '</a>';
422 }
423 // New folder button
424 if ($this->folderObject && $this->folderObject->checkActionPermission('add')) {
425 $buttons['new'] = '<a href="' . $GLOBALS['BACK_PATH'] . 'file_newfolder.php?target=' . rawurlencode($this->folderObject->getCombinedIdentifier()) . '&amp;returnUrl=' . rawurlencode($this->filelist->listURL()) . '" title="' . $GLOBALS['LANG']->makeEntities($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:cm.new', 1)) . '">' . \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new') . '</a>';
426 }
427 return $buttons;
428 }
429
430 }
431
432 ?>