[BUGFIX] Use FAL object name directly in Filelist clipboard message
[Packages/TYPO3.CMS.git] / typo3 / sysext / filelist / Classes / FileList.php
1 <?php
2 namespace TYPO3\CMS\Filelist;
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\Clipboard\Clipboard;
18 use TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider;
19 use TYPO3\CMS\Backend\RecordList\AbstractRecordList;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Backend\Utility\IconUtility;
22 use TYPO3\CMS\Core\Database\DatabaseConnection;
23 use TYPO3\CMS\Core\Imaging\Icon;
24 use TYPO3\CMS\Core\Imaging\IconFactory;
25 use TYPO3\CMS\Core\Messaging\FlashMessage;
26 use TYPO3\CMS\Core\Resource\File;
27 use TYPO3\CMS\Core\Resource\Folder;
28 use TYPO3\CMS\Core\Resource\InaccessibleFolder;
29 use TYPO3\CMS\Core\Resource\ProcessedFile;
30 use TYPO3\CMS\Core\Resource\ResourceFactory;
31 use TYPO3\CMS\Core\Resource\Utility\ListUtility;
32 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
33 use TYPO3\CMS\Core\Utility\GeneralUtility;
34 use TYPO3\CMS\Core\Resource\FolderInterface;
35
36 /**
37 * Class for rendering of File>Filelist
38 */
39 class FileList extends AbstractRecordList {
40
41 /**
42 * Default Max items shown
43 *
44 * @var int
45 */
46 public $iLimit = 40;
47
48 /**
49 * Thumbnails on records containing files (pictures)
50 *
51 * @var bool
52 */
53 public $thumbs = FALSE;
54
55 /**
56 * Space icon used for alignment when no button is available
57 *
58 * @var string
59 */
60 public $spaceIcon;
61
62 /**
63 * Max length of strings
64 *
65 * @var int
66 */
67 public $fixedL = 30;
68
69 /**
70 * @var string
71 */
72 public $script = '';
73
74 /**
75 * If TRUE click menus are generated on files and folders
76 *
77 * @var bool
78 */
79 public $clickMenus = 1;
80
81 /**
82 * The field to sort by
83 *
84 * @var string
85 */
86 public $sort = '';
87
88 /**
89 * Reverse sorting flag
90 *
91 * @var bool
92 */
93 public $sortRev = 1;
94
95 /**
96 * @var int
97 */
98 public $firstElementNumber = 0;
99
100 /**
101 * @var bool
102 */
103 public $clipBoard = 0;
104
105 /**
106 * @var bool
107 */
108 public $bigControlPanel = 0;
109
110 /**
111 * @var string
112 */
113 public $JScode = '';
114
115 /**
116 * @var string
117 */
118 public $HTMLcode = '';
119
120 /**
121 * @var int
122 */
123 public $totalbytes = 0;
124
125 /**
126 * @var array
127 */
128 public $dirs = array();
129
130 /**
131 * @var array
132 */
133 public $files = array();
134
135 /**
136 * @var string
137 */
138 public $path = '';
139
140 /**
141 * @var Folder
142 */
143 protected $folderObject;
144
145 /**
146 * Counting the elements no matter what
147 *
148 * @var int
149 */
150 public $eCounter = 0;
151
152 /**
153 * @var string
154 */
155 public $totalItems = '';
156
157 /**
158 * @var array
159 */
160 public $CBnames = array();
161
162 /**
163 * @var Clipboard $clipObj
164 */
165 public $clipObj;
166
167 /**
168 * @var ResourceFactory
169 */
170 protected $resourceFactory;
171
172 /**
173 * @param ResourceFactory $resourceFactory
174 */
175 public function injectResourceFactory(ResourceFactory $resourceFactory) {
176 $this->resourceFactory = $resourceFactory;
177 }
178
179 /**
180 * @var IconFactory
181 */
182 protected $iconFactory;
183
184 /**
185 * Constructor
186 */
187 public function __construct() {
188 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
189 }
190
191 /**
192 * Initialization of class
193 *
194 * @param Folder $folderObject The folder to work on
195 * @param int $pointer Pointer
196 * @param bool $sort Sorting column
197 * @param bool $sortRev Sorting direction
198 * @param bool $clipBoard
199 * @param bool $bigControlPanel Show clipboard flag
200 * @return void
201 */
202 public function start(Folder $folderObject, $pointer, $sort, $sortRev, $clipBoard = FALSE, $bigControlPanel = FALSE) {
203 $this->script = BackendUtility::getModuleUrl('file_list');
204 $this->folderObject = $folderObject;
205 $this->counter = 0;
206 $this->totalbytes = 0;
207 $this->JScode = '';
208 $this->HTMLcode = '';
209 $this->path = $folderObject->getReadablePath();
210 $this->sort = $sort;
211 $this->sortRev = $sortRev;
212 $this->firstElementNumber = $pointer;
213 $this->clipBoard = $clipBoard;
214 $this->bigControlPanel = $bigControlPanel;
215 // Setting the maximum length of the filenames to the user's settings or minimum 30 (= $this->fixedL)
216 $this->fixedL = max($this->fixedL, $this->getBackendUser()->uc['titleLen']);
217 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_common.xlf');
218 $this->resourceFactory = ResourceFactory::getInstance();
219 }
220
221 /**
222 * Reading files and directories, counting elements and generating the list in ->HTMLcode
223 *
224 * @return void
225 */
226 public function generateList() {
227 $this->HTMLcode .= $this->getTable('fileext,tstamp,size,rw,_REF_');
228 }
229
230 /**
231 * Return the buttons used by the filelist to include in the top header
232 *
233 * @param Folder $folderObject
234 * @return array
235 */
236 public function getButtonsAndOtherMarkers(Folder $folderObject) {
237 $otherMarkers = array(
238 'PAGE_ICON' => '',
239 'TITLE' => ''
240 );
241 $buttons = array(
242 'level_up' => $this->getLinkToParentFolder($folderObject),
243 'refresh' => '',
244 'title' => '',
245 'page_icon' => '',
246 'PASTE' => ''
247 );
248 // Makes the code for the folder icon in the top
249 if ($folderObject) {
250 $title = htmlspecialchars($folderObject->getReadablePath());
251 // Start compiling the HTML
252 // If this is some subFolder under the mount root....
253 if ($folderObject->getStorage()->isWithinFileMountBoundaries($folderObject)) {
254 // The icon with link
255 $otherMarkers['PAGE_ICON'] = IconUtility::getSpriteIconForResource($folderObject, array('title' => $title));
256 // No HTML specialchars here - HTML like <strong> </strong> is allowed
257 $otherMarkers['TITLE'] .= GeneralUtility::removeXSS(GeneralUtility::fixed_lgd_cs($title, -($this->fixedL + 20)));
258 } else {
259 // This is the root folder
260 $otherMarkers['PAGE_ICON'] = IconUtility::getSpriteIconForResource($folderObject, array('title' => $title, 'mount-root' => TRUE));
261 $otherMarkers['TITLE'] .= htmlspecialchars(GeneralUtility::fixed_lgd_cs($title, -($this->fixedL + 20)));
262 }
263 if ($this->clickMenus) {
264 $otherMarkers['PAGE_ICON'] = $GLOBALS['SOBE']->doc->wrapClickMenuOnIcon($otherMarkers['PAGE_ICON'], $folderObject->getCombinedIdentifier());
265 }
266 // Add paste button if clipboard is initialized
267 if ($this->clipObj instanceof Clipboard && $folderObject->checkActionPermission('write')) {
268 $elFromTable = $this->clipObj->elFromTable('_FILE');
269 if (!empty($elFromTable)) {
270 $addPasteButton = TRUE;
271 $elToConfirm = array();
272 foreach ($elFromTable as $key => $element) {
273 $clipBoardElement = $this->resourceFactory->retrieveFileOrFolderObject($element);
274 if ($clipBoardElement instanceof Folder && $clipBoardElement->getStorage()->isWithinFolder($clipBoardElement, $folderObject)) {
275 $addPasteButton = FALSE;
276 }
277 $elToConfirm[$key] = $clipBoardElement->getName();
278 }
279 if ($addPasteButton) {
280 $buttons['PASTE'] = '<a href="' . htmlspecialchars($this->clipObj->pasteUrl('_FILE', $folderObject->getCombinedIdentifier())) . '" onclick="return ' . htmlspecialchars($this->clipObj->confirmMsg('_FILE', $this->path, 'into', $elToConfirm)) . '" title="' . $this->getLanguageService()->getLL('clip_paste', TRUE) . '">' . $this->iconFactory->getIcon('actions-document-paste-after', Icon::SIZE_SMALL) . '</a>';
281 }
282 }
283 }
284
285 }
286 $buttons['refresh'] = '<a href="' . htmlspecialchars($this->listURL()) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.reload', TRUE) . '">' . IconUtility::getSpriteIcon('actions-system-refresh') . '</a>';
287 return array($buttons, $otherMarkers);
288 }
289
290 /**
291 * Wrapping input string in a link with clipboard command.
292 *
293 * @param string $string String to be linked - must be htmlspecialchar'ed / prepared before.
294 * @param string $table table - NOT USED
295 * @param string $cmd "cmd" value
296 * @param string $warning Warning for JS confirm message
297 * @return string Linked string
298 */
299 public function linkClipboardHeaderIcon($string, $table, $cmd, $warning = '') {
300 $onClickEvent = 'document.dblistForm.cmd.value=\'' . $cmd . '\';document.dblistForm.submit();';
301 if ($warning) {
302 $onClickEvent = 'if (confirm(' . GeneralUtility::quoteJSvalue($warning) . ')){' . $onClickEvent . '}';
303 }
304 return '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($onClickEvent) . 'return false;">' . $string . '</a>';
305 }
306
307 /**
308 * Returns a table with directories and files listed.
309 *
310 * @param array $rowlist Array of files from path
311 * @return string HTML-table
312 */
313 public function getTable($rowlist) {
314 // prepare space icon
315 $this->spaceIcon = '<span class="btn btn-default disabled">' . IconUtility::getSpriteIcon('empty-empty') . '</span>';
316
317 // @todo use folder methods directly when they support filters
318 $storage = $this->folderObject->getStorage();
319 $storage->resetFileAndFolderNameFiltersToDefault();
320
321 // Only render the contents of a browsable storage
322 if ($this->folderObject->getStorage()->isBrowsable()) {
323 try {
324 $foldersCount = $storage->countFoldersInFolder($this->folderObject);
325 $filesCount = $storage->countFilesInFolder($this->folderObject);
326 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException $e) {
327 $foldersCount = 0;
328 $filesCount = 0;
329 }
330
331 if ($foldersCount <= $this->firstElementNumber) {
332 $foldersFrom = FALSE;
333 $foldersNum = FALSE;
334 } else {
335 $foldersFrom = $this->firstElementNumber;
336 if ($this->firstElementNumber + $this->iLimit > $foldersCount) {
337 $foldersNum = $foldersCount - $this->firstElementNumber;
338 } else {
339 $foldersNum = $this->iLimit;
340 }
341 }
342 if ($foldersCount >= $this->firstElementNumber + $this->iLimit) {
343 $filesFrom = FALSE;
344 $filesNum = FALSE;
345 } else {
346 if ($this->firstElementNumber <= $foldersCount) {
347 $filesFrom = 0;
348 $filesNum = $this->iLimit - $foldersNum;
349 } else {
350 $filesFrom = $this->firstElementNumber - $foldersCount;
351 if ($filesFrom + $this->iLimit > $filesCount) {
352 $filesNum = $filesCount - $filesFrom;
353 } else {
354 $filesNum = $this->iLimit;
355 }
356 }
357 }
358 $folders = $storage->getFoldersInFolder($this->folderObject, $foldersFrom, $foldersNum, TRUE, FALSE, trim($this->sort), (bool)$this->sortRev);
359 $files = $this->folderObject->getFiles($filesFrom, $filesNum, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, FALSE, trim($this->sort), (bool)$this->sortRev);
360 $this->totalItems = $foldersCount + $filesCount;
361 // Adds the code of files/dirs
362 $out = '';
363 $titleCol = 'file';
364 // Cleaning rowlist for duplicates and place the $titleCol as the first column always!
365 $rowlist = '_LOCALIZATION_,' . $rowlist;
366 $rowlist = GeneralUtility::rmFromList($titleCol, $rowlist);
367 $rowlist = GeneralUtility::uniqueList($rowlist);
368 $rowlist = $rowlist ? $titleCol . ',' . $rowlist : $titleCol;
369 if ($this->clipBoard) {
370 $rowlist = str_replace('_LOCALIZATION_,', '_LOCALIZATION_,_CLIPBOARD_,', $rowlist);
371 $this->addElement_tdCssClass['_CLIPBOARD_'] = 'col-clipboard';
372 }
373 if ($this->bigControlPanel) {
374 $rowlist = str_replace('_LOCALIZATION_,', '_LOCALIZATION_,_CONTROL_,', $rowlist);
375 $this->addElement_tdCssClass['_CONTROL_'] = 'col-control';
376 }
377 $this->fieldArray = explode(',', $rowlist);
378
379 // Add classes to table cells
380 $this->addElement_tdCssClass[$titleCol] = 'col-title';
381 $this->addElement_tdCssClass['_LOCALIZATION_'] = 'col-localizationa';
382
383 $folders = ListUtility::resolveSpecialFolderNames($folders);
384
385 $iOut = '';
386 // Directories are added
387 $this->eCounter = $this->firstElementNumber;
388 list($flag, $code) = $this->fwd_rwd_nav();
389 $iOut .= $code;
390
391 $iOut .= $this->formatDirList($folders);
392 // Files are added
393 $iOut .= $this->formatFileList($files);
394
395 $this->eCounter = $this->firstElementNumber + $this->iLimit <= $this->totalItems
396 ? $this->firstElementNumber + $this->iLimit
397 : $this->totalItems;
398 list($flag, $code) = $this->fwd_rwd_nav();
399 $iOut .= $code;
400
401 // Header line is drawn
402 $theData = array();
403 foreach ($this->fieldArray as $v) {
404 if ($v == '_CLIPBOARD_' && $this->clipBoard) {
405 $cells = array();
406 $table = '_FILE';
407 $elFromTable = $this->clipObj->elFromTable($table);
408 if (!empty($elFromTable) && $this->folderObject->checkActionPermission('write')) {
409 $addPasteButton = TRUE;
410 $elToConfirm = array();
411 foreach ($elFromTable as $key => $element) {
412 $clipBoardElement = $this->resourceFactory->retrieveFileOrFolderObject($element);
413 if ($clipBoardElement instanceof Folder && $clipBoardElement->getStorage()->isWithinFolder($clipBoardElement, $this->folderObject)) {
414 $addPasteButton = FALSE;
415 }
416 $elToConfirm[$key] = $clipBoardElement->getName();
417 }
418 if ($addPasteButton) {
419 $cells[] = '<a class="btn btn-default" href="' . htmlspecialchars($this->clipObj->pasteUrl('_FILE', $this->folderObject->getCombinedIdentifier())) . '" onclick="return ' . htmlspecialchars($this->clipObj->confirmMsg('_FILE', $this->path, 'into', $elToConfirm)) . '" title="' . $this->getLanguageService()->getLL('clip_paste', 1) . '">' . $this->iconFactory->getIcon('actions-document-paste-after', Icon::SIZE_SMALL) . '</a>';
420 }
421 }
422 if ($this->clipObj->current !== 'normal' && $iOut) {
423 $cells[] = $this->linkClipboardHeaderIcon(IconUtility::getSpriteIcon('actions-edit-copy', array('title' => $this->getLanguageService()->getLL('clip_selectMarked', TRUE))), $table, 'setCB');
424 $cells[] = $this->linkClipboardHeaderIcon(IconUtility::getSpriteIcon('actions-edit-delete', array('title' => $this->getLanguageService()->getLL('clip_deleteMarked'))), $table, 'delete', $this->getLanguageService()->getLL('clip_deleteMarkedWarning'));
425 $onClick = 'checkOffCB(\'' . implode(',', $this->CBnames) . '\', this); return false;';
426 $cells[] = '<a class="btn btn-default" rel="" href="#" onclick="' . htmlspecialchars($onClick) . '" title="' . $this->getLanguageService()->getLL('clip_markRecords', TRUE) . '">' . IconUtility::getSpriteIcon('actions-document-select') . '</a>';
427 }
428 $theData[$v] = implode('', $cells);
429 } else {
430 // Normal row:
431 $theT = $this->linkWrapSort($this->getLanguageService()->getLL('c_' . $v, TRUE), $this->folderObject->getCombinedIdentifier(), $v);
432 $theData[$v] = $theT;
433 }
434 }
435
436 $out .= '<thead>' . $this->addelement(1, '', $theData, '', '', '', 'th') . '</thead>';
437 $out .= '<tbody>' . $iOut . '</tbody>';
438 // half line is drawn
439 // finish
440 $out = '
441 <!--
442 Filelist table:
443 -->
444 <div class="table-fit">
445 <table class="table table-striped table-hover" id="typo3-filelist">
446 ' . $out . '
447 </table>
448 </div>';
449
450 } else {
451 /** @var $flashMessage FlashMessage */
452 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('storageNotBrowsableMessage'), $this->getLanguageService()->getLL('storageNotBrowsableTitle'), FlashMessage::INFO);
453 $out = $flashMessage->render();
454 }
455 return $out;
456 }
457
458 /**
459 * If there is a parent folder and user has access to it, return an icon
460 * which is linked to the filelist of the parent folder.
461 *
462 * @param Folder $currentFolder
463 * @return string
464 */
465 protected function getLinkToParentFolder(Folder $currentFolder) {
466 $levelUp = '';
467 try {
468 $currentStorage = $currentFolder->getStorage();
469 $parentFolder = $currentFolder->getParentFolder();
470 if ($parentFolder->getIdentifier() !== $currentFolder->getIdentifier() && $currentStorage->isWithinFileMountBoundaries($parentFolder)) {
471 $levelUp = $this->linkWrapDir(
472 IconUtility::getSpriteIcon(
473 'actions-view-go-up',
474 array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.php:labels.upOneLevel', TRUE))
475 ),
476 $parentFolder
477 );
478 }
479 } catch (\Exception $e) {}
480 return $levelUp;
481 }
482
483 /**
484 * Gets the number of files and total size of a folder
485 *
486 * @return string
487 */
488 public function getFolderInfo() {
489 if ($this->counter == 1) {
490 $fileLabel = $this->getLanguageService()->getLL('file', TRUE);
491 } else {
492 $fileLabel = $this->getLanguageService()->getLL('files', TRUE);
493 }
494 return $this->counter . ' ' . $fileLabel . ', ' . GeneralUtility::formatSize($this->totalbytes, $this->getLanguageService()->getLL('byteSizeUnits', TRUE));
495 }
496
497 /**
498 * This returns tablerows for the directories in the array $items['sorting'].
499 *
500 * @param Folder[] $folders Folders of \TYPO3\CMS\Core\Resource\Folder
501 * @return string HTML table rows.
502 */
503 public function formatDirList(array $folders) {
504 $out = '';
505 foreach ($folders as $folderName => $folderObject) {
506 $role = $folderObject->getRole();
507 if ($role === FolderInterface::ROLE_PROCESSING) {
508 // don't show processing-folder
509 continue;
510 }
511 if ($role !== FolderInterface::ROLE_DEFAULT) {
512 $displayName = '<strong>' . htmlspecialchars($folderName) . '</strong>';
513 } else {
514 $displayName = htmlspecialchars($folderName);
515 }
516
517 $isLocked = $folderObject instanceof InaccessibleFolder;
518 $isWritable = $folderObject->checkActionPermission('write');
519
520 // Initialization
521 $this->counter++;
522
523 // The icon with link
524 $theIcon = IconUtility::getSpriteIconForResource($folderObject, array('title' => $folderName));
525 if (!$isLocked && $this->clickMenus) {
526 $theIcon = $GLOBALS['SOBE']->doc->wrapClickMenuOnIcon($theIcon, $folderObject->getCombinedIdentifier());
527 }
528
529 // Preparing and getting the data-array
530 $theData = array();
531 if ($isLocked) {
532 foreach ($this->fieldArray as $field) {
533 $theData[$field] = '';
534 }
535 $theData['file'] = $displayName;
536 } else {
537 foreach ($this->fieldArray as $field) {
538 switch ($field) {
539 case 'size':
540 try {
541 $numFiles = $folderObject->getFileCount();
542 } catch (\TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException $e) {
543 $numFiles = 0;
544 }
545 $theData[$field] = $numFiles . ' ' . $this->getLanguageService()->getLL(($numFiles === 1 ? 'file' : 'files'), TRUE);
546 break;
547 case 'rw':
548 $theData[$field] = '<strong class="text-danger">' . $this->getLanguageService()->getLL('read', TRUE) . '</strong>' . (!$isWritable ? '' : '<strong class="text-danger">' . $this->getLanguageService()->getLL('write', TRUE) . '</strong>');
549 break;
550 case 'fileext':
551 $theData[$field] = $this->getLanguageService()->getLL('folder', TRUE);
552 break;
553 case 'tstamp':
554 // @todo: FAL: how to get the mtime info -- $theData[$field] = \TYPO3\CMS\Backend\Utility\BackendUtility::date($theFile['tstamp']);
555 $theData[$field] = '-';
556 break;
557 case 'file':
558 $theData[$field] = $this->linkWrapDir($displayName, $folderObject);
559 break;
560 case '_CONTROL_':
561 $theData[$field] = $this->makeEdit($folderObject);
562 break;
563 case '_CLIPBOARD_':
564 $theData[$field] = $this->makeClip($folderObject);
565 break;
566 case '_REF_':
567 $theData[$field] = $this->makeRef($folderObject);
568 break;
569 default:
570 $theData[$field] = GeneralUtility::fixed_lgd_cs($theData[$field], $this->fixedL);
571 }
572 }
573 }
574 $out .= $this->addelement(1, $theIcon, $theData);
575 }
576 return $out;
577 }
578
579 /**
580 * Wraps the directory-titles
581 *
582 * @param string $title String to be wrapped in links
583 * @param Folder $folderObject Folder to work on
584 * @return string HTML
585 */
586 public function linkWrapDir($title, Folder $folderObject) {
587 $href = $this->script . '&id=' . rawurlencode($folderObject->getCombinedIdentifier());
588 $onclick = ' onclick="' . htmlspecialchars(('top.document.getElementsByName("navigation")[0].contentWindow.Tree.highlightActiveItem("file","folder' . GeneralUtility::md5int($folderObject->getCombinedIdentifier()) . '_"+top.fsMod.currentBank)')) . '"';
589 // Sometimes $code contains plain HTML tags. In such a case the string should not be modified!
590 if ((string)$title === strip_tags($title)) {
591 return '<a href="' . htmlspecialchars($href) . '"' . $onclick . ' title="' . htmlspecialchars($title) . '">' . GeneralUtility::fixed_lgd_cs($title, $this->fixedL) . '</a>';
592 } else {
593 return '<a href="' . htmlspecialchars($href) . '"' . $onclick . '>' . $title . '</a>';
594 }
595 }
596
597 /**
598 * Wraps filenames in links which opens the metadata editor.
599 *
600 * @param string $code String to be wrapped in links
601 * @param File $fileObject File to be linked
602 * @return string HTML
603 */
604 public function linkWrapFile($code, File $fileObject) {
605 try {
606 if ($fileObject instanceof File && $fileObject->isIndexed() && $fileObject->checkActionPermission('write') && $this->getBackendUser()->check('tables_modify', 'sys_file_metadata')) {
607 $metaData = $fileObject->_getMetaData();
608 $data = array(
609 'sys_file_metadata' => array($metaData['uid'] => 'edit')
610 );
611 $editOnClick = BackendUtility::editOnClick(GeneralUtility::implodeArrayForUrl('edit', $data), '', $this->listUrl());
612 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.editMetadata'));
613 $code = '<a href="#" title="' . $title . '" onclick="' . htmlspecialchars($editOnClick) . '">' . GeneralUtility::fixed_lgd_cs($code, $this->fixedL) . '</a>';
614 }
615 } catch (\Exception $e) {
616 // intentional fall-through
617 }
618 return $code;
619 }
620
621 /**
622 * Returns list URL; This is the URL of the current script with id and imagemode parameters, that's all.
623 * The URL however is not relative, otherwise GeneralUtility::sanitizeLocalUrl() would say that
624 * the URL would be invalid
625 *
626 * @return string URL
627 */
628 public function listURL($altId = '') {
629 return GeneralUtility::linkThisScript(array(
630 'target' => rawurlencode($this->folderObject->getCombinedIdentifier()),
631 'imagemode' => $this->thumbs
632 ));
633 }
634
635 /**
636 * This returns tablerows for the files in the array $items['sorting'].
637 *
638 * @param File[] $files File items
639 * @return string HTML table rows.
640 */
641 public function formatFileList(array $files) {
642 $out = '';
643 // first two keys are "0" (default) and "-1" (multiple), after that comes the "other languages"
644 $allSystemLanguages = GeneralUtility::makeInstance(TranslationConfigurationProvider::class)->getSystemLanguages();
645 $systemLanguages = array_filter($allSystemLanguages, function($languageRecord) {
646 if ($languageRecord['uid'] === -1 || $languageRecord['uid'] === 0 || !$this->getBackendUser()->checkLanguageAccess($languageRecord['uid'])) {
647 return FALSE;
648 } else {
649 return TRUE;
650 }
651 });
652
653 foreach ($files as $fileObject) {
654 // Initialization
655 $this->counter++;
656 $this->totalbytes += $fileObject->getSize();
657 $ext = $fileObject->getExtension();
658 $fileName = trim($fileObject->getName());
659 // The icon with link
660 $theIcon = IconUtility::getSpriteIconForResource($fileObject, array('title' => $fileName . ' [' . (int)$fileObject->getUid() . ']'));
661 if ($this->clickMenus) {
662 $theIcon = $GLOBALS['SOBE']->doc->wrapClickMenuOnIcon($theIcon, $fileObject->getCombinedIdentifier());
663 }
664 // Preparing and getting the data-array
665 $theData = array();
666 foreach ($this->fieldArray as $field) {
667 switch ($field) {
668 case 'size':
669 $theData[$field] = GeneralUtility::formatSize($fileObject->getSize(), $this->getLanguageService()->getLL('byteSizeUnits', TRUE));
670 break;
671 case 'rw':
672 $theData[$field] = '' . (!$fileObject->checkActionPermission('read') ? ' ' : '<strong class="text-danger">' . $this->getLanguageService()->getLL('read', TRUE) . '</strong>') . (!$fileObject->checkActionPermission('write') ? '' : '<strong class="text-danger">' . $this->getLanguageService()->getLL('write', TRUE) . '</strong>');
673 break;
674 case 'fileext':
675 $theData[$field] = strtoupper($ext);
676 break;
677 case 'tstamp':
678 $theData[$field] = BackendUtility::date($fileObject->getModificationTime());
679 break;
680 case '_CONTROL_':
681 $theData[$field] = $this->makeEdit($fileObject);
682 break;
683 case '_CLIPBOARD_':
684 $theData[$field] = $this->makeClip($fileObject);
685 break;
686 case '_LOCALIZATION_':
687 if (!empty($systemLanguages) && $fileObject->isIndexed() && $fileObject->checkActionPermission('write') && $this->getBackendUser()->check('tables_modify', 'sys_file_metadata')) {
688 $metaDataRecord = $fileObject->_getMetaData();
689 $translations = $this->getTranslationsForMetaData($metaDataRecord);
690 $languageCode = '';
691
692 foreach ($systemLanguages as $language) {
693 $languageId = $language['uid'];
694 $flagIcon = $language['flagIcon'];
695 if (array_key_exists($languageId, $translations)) {
696 $flagButtonIcon = IconUtility::getSpriteIcon(
697 'actions-document-open',
698 array('title' => sprintf($GLOBALS['LANG']->getLL('editMetadataForLanguage'), $language['title'])),
699 array($flagIcon . '-overlay' => array()));
700 $data = array(
701 'sys_file_metadata' => array($translations[$languageId]['uid'] => 'edit')
702 );
703 $editOnClick = BackendUtility::editOnClick(GeneralUtility::implodeArrayForUrl('edit', $data), '', $this->listUrl());
704 $languageCode .= '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($editOnClick) . '">' . $flagButtonIcon . '</a>';
705 } else {
706 $parameters = [
707 'justLocalized' => 'sys_file_metadata:' . $metaDataRecord['uid'] . ':' . $languageId,
708 'returnUrl' => $this->listURL()
709 ];
710 $returnUrl = BackendUtility::getModuleUrl('record_edit', $parameters) . BackendUtility::getUrlToken('editRecord');
711 $href = $GLOBALS['SOBE']->doc->issueCommand(
712 '&cmd[sys_file_metadata][' . $metaDataRecord['uid'] . '][localize]=' . $languageId,
713 $returnUrl
714 );
715 $flagButtonIcon = IconUtility::getSpriteIcon(
716 $flagIcon,
717 array('title' => sprintf($GLOBALS['LANG']->getLL('createMetadataForLanguage'), $language['title'])),
718 array($flagIcon . '-overlay' => array())
719 );
720 $languageCode .= '<a href="' . htmlspecialchars($href) . '" class="btn btn-default">' . $flagButtonIcon . '</a> ';
721 }
722 }
723
724 // Hide flag button bar when not translated yet
725 $theData[$field] = ' <div class="localisationData btn-group" data-fileid="' . $fileObject->getUid() . '"' .
726 (empty($translations) ? ' style="display: none;"' : '') . '>' . $languageCode . '</div>';
727 $theData[$field] .= '<a class="btn btn-default filelist-translationToggler" data-fileid="' . $fileObject->getUid() . '">' .
728 IconUtility::getSpriteIcon(
729 'mimetypes-x-content-page-language-overlay',
730 array(
731 'title' => $GLOBALS['LANG']->getLL('translateMetadata')
732 )
733 ) . '</a>';
734 }
735 break;
736 case '_REF_':
737 $theData[$field] = $this->makeRef($fileObject);
738 break;
739 case 'file':
740 // Edit metadata of file
741 $theData[$field] = $this->linkWrapFile(htmlspecialchars($fileName), $fileObject);
742
743 if ($fileObject->isMissing()) {
744 $flashMessage = \TYPO3\CMS\Core\Resource\Utility\BackendUtility::getFlashMessageForMissingFile($fileObject);
745 $theData[$field] .= $flashMessage->render();
746 // Thumbnails?
747 } elseif ($this->thumbs && $this->isImage($ext)) {
748 $processedFile = $fileObject->process(ProcessedFile::CONTEXT_IMAGEPREVIEW, array());
749 if ($processedFile) {
750 $thumbUrl = $processedFile->getPublicUrl(TRUE);
751 $theData[$field] .= '<br /><img src="' . $thumbUrl . '" ' .
752 'width="' . $processedFile->getProperty('width') . '" ' .
753 'height="' . $processedFile->getProperty('height') . '" ' .
754 'title="' . htmlspecialchars($fileName) . '" alt="" />';
755 }
756 }
757 break;
758 default:
759 $theData[$field] = '';
760 if ($fileObject->hasProperty($field)) {
761 $theData[$field] = htmlspecialchars(GeneralUtility::fixed_lgd_cs($fileObject->getProperty($field), $this->fixedL));
762 }
763 }
764 }
765 $out .= $this->addelement(1, $theIcon, $theData);
766 }
767 return $out;
768 }
769
770 /**
771 * Fetch the translations for a sys_file_metadata record
772 *
773 * @param $metaDataRecord
774 * @return array keys are the sys_language uids, values are the $rows
775 */
776 protected function getTranslationsForMetaData($metaDataRecord) {
777 $where = $GLOBALS['TCA']['sys_file_metadata']['ctrl']['transOrigPointerField'] . '=' . (int)$metaDataRecord['uid'] .
778 ' AND ' . $GLOBALS['TCA']['sys_file_metadata']['ctrl']['languageField'] . '>0';
779 $translationRecords = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'sys_file_metadata', $where);
780 $translations = array();
781 foreach ($translationRecords as $record) {
782 $translations[$record[$GLOBALS['TCA']['sys_file_metadata']['ctrl']['languageField']]] = $record;
783 }
784 return $translations;
785 }
786
787 /**
788 * Returns TRUE if $ext is an image-extension according to $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
789 *
790 * @param string $ext File extension
791 * @return bool
792 */
793 public function isImage($ext) {
794 return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], strtolower($ext));
795 }
796
797 /**
798 * Wraps the directory-titles ($code) in a link to filelist/Modules/Filelist/index.php (id=$path) and sorting commands...
799 *
800 * @param string $code String to be wrapped
801 * @param string $folderIdentifier ID (path)
802 * @param string $col Sorting column
803 * @return string HTML
804 */
805 public function linkWrapSort($code, $folderIdentifier, $col) {
806 if ($this->sort === $col) {
807 // Check reverse sorting
808 $params = '&SET[sort]=' . $col . '&SET[reverse]=' . ($this->sortRev ? '0' : '1');
809 $sortArrow = IconUtility::getSpriteIcon('status-status-sorting-light-' . ($this->sortRev ? 'desc' : 'asc'));
810 } else {
811 $params = '&SET[sort]=' . $col . '&SET[reverse]=0';
812 $sortArrow = '';
813 }
814 $href = GeneralUtility::resolveBackPath($this->script) . '&id=' . rawurlencode($folderIdentifier) . $params;
815 return '<a href="' . htmlspecialchars($href) . '">' . $code . ' ' . $sortArrow . '</a>';
816 }
817
818 /**
819 * Creates the clipboard control pad
820 *
821 * @param File|Folder $fileOrFolderObject Array with information about the file/directory for which to make the clipboard panel for the listing.
822 * @return string HTML-table
823 */
824 public function makeClip($fileOrFolderObject) {
825 if (!$fileOrFolderObject->checkActionPermission('read')) {
826 return '';
827 }
828 $cells = array();
829 $fullIdentifier = $fileOrFolderObject->getCombinedIdentifier();
830 $fullName = $fileOrFolderObject->getName();
831 $md5 = GeneralUtility::shortmd5($fullIdentifier);
832 // For normal clipboard, add copy/cut buttons:
833 if ($this->clipObj->current == 'normal') {
834 $isSel = $this->clipObj->isSelected('_FILE', $md5);
835 $cells[] = '<a class="btn btn-default"" href="' . htmlspecialchars($this->clipObj->selUrlFile($fullIdentifier, 1, ($isSel == 'copy'))) . '">' . IconUtility::getSpriteIcon(('actions-edit-copy' . ($isSel == 'copy' ? '-release' : '')), array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.copy', TRUE))) . '</a>';
836 // we can only cut if file can be moved
837 if ($fileOrFolderObject->checkActionPermission('move')) {
838 $cells[] = '<a class="btn btn-default" href="' . htmlspecialchars($this->clipObj->selUrlFile($fullIdentifier, 0, ($isSel == 'cut'))) . '">' . IconUtility::getSpriteIcon(('actions-edit-cut' . ($isSel == 'cut' ? '-release' : '')), array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.cut', TRUE))) . '</a>';
839 } else {
840 $cells[] = $this->spaceIcon;
841 }
842 } else {
843 // For numeric pads, add select checkboxes:
844 $n = '_FILE|' . $md5;
845 $this->CBnames[] = $n;
846 $checked = $this->clipObj->isSelected('_FILE', $md5) ? ' checked="checked"' : '';
847 $cells[] = '<label class="btn btn-default btn-checkbox"><input type="hidden" name="CBH[' . $n . ']" value="0" /><input type="checkbox" name="CBC[' . $n . ']" value="' . htmlspecialchars($fullIdentifier) . '" ' . $checked . ' /><span class="t3-icon fa"></span></label>';
848 }
849 // Display PASTE button, if directory:
850 $elFromTable = $this->clipObj->elFromTable('_FILE');
851 if ($fileOrFolderObject instanceof Folder && !empty($elFromTable) && $fileOrFolderObject->checkActionPermission('write')) {
852 $addPasteButton = TRUE;
853 $elToConfirm = array();
854 foreach ($elFromTable as $key => $element) {
855 $clipBoardElement = $this->resourceFactory->retrieveFileOrFolderObject($element);
856 if ($clipBoardElement instanceof Folder && $clipBoardElement->getStorage()->isWithinFolder($clipBoardElement, $fileOrFolderObject)) {
857 $addPasteButton = FALSE;
858 }
859 $elToConfirm[$key] = $clipBoardElement->getName();
860 }
861 if ($addPasteButton) {
862 $cells[] = '<a class="btn btn-default" href="' . htmlspecialchars($this->clipObj->pasteUrl('_FILE', $fullIdentifier)) . '" onclick="return ' . htmlspecialchars($this->clipObj->confirmMsg('_FILE', $fullName, 'into', $elToConfirm)) . '" title="' . $this->getLanguageService()->getLL('clip_pasteInto', TRUE) . '">' . IconUtility::getSpriteIcon('actions-document-paste-into') . '</a>';
863 }
864 }
865 // Compile items into a DIV-element:
866 return ' <div class="btn-group" role="group">' . implode('', $cells) . '</div>';
867 }
868
869 /**
870 * Creates the edit control section
871 *
872 * @param File|Folder $fileOrFolderObject Array with information about the file/directory for which to make the edit control section for the listing.
873 * @return string HTML-table
874 */
875 public function makeEdit($fileOrFolderObject) {
876 $cells = array();
877 $fullIdentifier = $fileOrFolderObject->getCombinedIdentifier();
878
879 // Edit file content (if editable)
880 if ($fileOrFolderObject instanceof File && $fileOrFolderObject->checkActionPermission('write') && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $fileOrFolderObject->getExtension())) {
881 $url = BackendUtility::getModuleUrl('file_edit', array('target' => $fullIdentifier));
882 $editOnClick = 'top.content.list_frame.location.href=' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+top.rawurlencode(top.content.list_frame.document.location.pathname+top.content.list_frame.document.location.search);return false;';
883 $cells['edit'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($editOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.editcontent') . '">' . IconUtility::getSpriteIcon('actions-page-open') . '</a>';
884 } else {
885 $cells['edit'] = $this->spaceIcon;
886 }
887 if ($fileOrFolderObject instanceof File) {
888 $fileUrl = $fileOrFolderObject->getPublicUrl(TRUE);
889 if ($fileUrl) {
890 $aOnClick = 'return top.openUrlInWindow(' . GeneralUtility::quoteJSvalue($fileUrl) . ', \'WebFile\');';
891 $cells['view'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($aOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.view') . '">' . $this->iconFactory->getIcon('actions-document-view', Icon::SIZE_SMALL) . '</a>';
892 } else {
893 $cells['view'] = $this->spaceIcon;
894 }
895 } else {
896 $cells['view'] = $this->spaceIcon;
897 }
898
899 // replace file
900 if ($fileOrFolderObject instanceof File && $fileOrFolderObject->checkActionPermission('replace')) {
901 $url = BackendUtility::getModuleUrl('file_replace', array('target' => $fullIdentifier, 'uid' => $fileOrFolderObject->getUid()));
902 $replaceOnClick = 'top.content.list_frame.location.href = ' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+top.rawurlencode(top.content.list_frame.document.location.pathname+top.content.list_frame.document.location.search);return false;';
903 $cells['replace'] = '<a href="#" class="btn btn-default" onclick="' . $replaceOnClick . '" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:cm.replace') . '">' . IconUtility::getSpriteIcon('actions-edit-replace') . '</a>';
904 }
905
906 // rename the file
907 if ($fileOrFolderObject->checkActionPermission('rename')) {
908 $url = BackendUtility::getModuleUrl('file_rename', array('target' => $fullIdentifier));
909 $renameOnClick = 'top.content.list_frame.location.href = ' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+top.rawurlencode(top.content.list_frame.document.location.pathname+top.content.list_frame.document.location.search);return false;';
910 $cells['rename'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($renameOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.rename') . '">' . IconUtility::getSpriteIcon('actions-edit-rename') . '</a>';
911 } else {
912 $cells['rename'] = $this->spaceIcon;
913 }
914 if ($fileOrFolderObject->checkActionPermission('read')) {
915 $infoOnClick = '';
916 if ($fileOrFolderObject instanceof Folder) {
917 $infoOnClick = 'top.launchView( \'_FOLDER\', ' . GeneralUtility::quoteJSvalue($fullIdentifier) . ');return false;';
918 } elseif ($fileOrFolderObject instanceof File) {
919 $infoOnClick = 'top.launchView( \'_FILE\', ' . GeneralUtility::quoteJSvalue($fullIdentifier) . ');return false;';
920 }
921 $cells['info'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($infoOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.info') . '">' . IconUtility::getSpriteIcon('status-dialog-information') . '</a>';
922 } else {
923 $cells['info'] = $this->spaceIcon;
924 }
925
926 // delete the file
927 if ($fileOrFolderObject->checkActionPermission('delete')) {
928 $identifier = $fileOrFolderObject->getIdentifier();
929 if ($fileOrFolderObject instanceof Folder) {
930 $referenceCountText = BackendUtility::referenceCount('_FILE', $identifier, ' (There are %s reference(s) to this folder!)');
931 } else {
932 $referenceCountText = BackendUtility::referenceCount('sys_file', $fileOrFolderObject->getUid(), ' (There are %s reference(s) to this file!)');
933 }
934
935 if ($this->getBackendUser()->jsConfirmation(JsConfirmation::DELETE)) {
936 $confirmationCheck = 'confirm(' . GeneralUtility::quoteJSvalue(sprintf($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:mess.delete'), $fileOrFolderObject->getName()) . $referenceCountText) . ')';
937 } else {
938 $confirmationCheck = '1 == 1';
939 }
940
941 $removeOnClick = 'if (' . $confirmationCheck . ') { top.content.list_frame.location.href=' . GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('tce_file') .'&file[delete][0][data]=' . rawurlencode($fileOrFolderObject->getCombinedIdentifier()) . '&vC=' . $this->getBackendUser()->veriCode() . BackendUtility::getUrlToken('tceAction') . '&redirect=') . '+top.rawurlencode(top.content.list_frame.document.location.pathname+top.content.list_frame.document.location.search);};';
942
943 $cells['delete'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($removeOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:cm.delete') . '">' . IconUtility::getSpriteIcon('actions-edit-delete') . '</a>';
944 } else {
945 $cells['delete'] = $this->spaceIcon;
946 }
947
948 // Hook for manipulating edit icons.
949 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['fileList']['editIconsHook'])) {
950 $cells['__fileOrFolderObject'] = $fileOrFolderObject;
951 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['fileList']['editIconsHook'] as $classData) {
952 $hookObject = GeneralUtility::getUserObj($classData);
953 if (!$hookObject instanceof FileListEditIconHookInterface) {
954 throw new \UnexpectedValueException(
955 '$hookObject must implement interface \\TYPO3\\CMS\\Filelist\\FileListEditIconHookInterface',
956 1235225797
957 );
958 }
959 $hookObject->manipulateEditIcons($cells, $this);
960 }
961 unset($cells['__fileOrFolderObject']);
962 }
963 // Compile items into a DIV-element:
964 return '<div class="btn-group">' . implode('', $cells) . '</div>';
965 }
966
967 /**
968 * Make reference count
969 *
970 * @param File|Folder $fileOrFolderObject Array with information about the file/directory for which to make the clipboard panel for the listing.
971 * @return string HTML
972 */
973 public function makeRef($fileOrFolderObject) {
974 if ($fileOrFolderObject instanceof FolderInterface) {
975 return '-';
976 }
977 // Look up the file in the sys_refindex.
978 // Exclude sys_file_metadata records as these are no use references
979 $databaseConnection = $this->getDatabaseConnection();
980 $table = 'sys_refindex';
981 $referenceCount = $databaseConnection->exec_SELECTcountRows(
982 '*',
983 $table,
984 'ref_table=' . $databaseConnection->fullQuoteStr('sys_file', $table)
985 . ' AND ref_uid=' . (int)$fileOrFolderObject->getUid()
986 . ' AND deleted=0'
987 . ' AND tablename != ' . $databaseConnection->fullQuoteStr('sys_file_metadata', $table)
988 );
989 return $this->generateReferenceToolTip($referenceCount, '\'_FILE\', ' . GeneralUtility::quoteJSvalue($fileOrFolderObject->getCombinedIdentifier()));
990 }
991
992 /**
993 * Returns the database connection
994 * @return DatabaseConnection
995 */
996 protected function getDatabaseConnection() {
997 return $GLOBALS['TYPO3_DB'];
998 }
999
1000 /**
1001 * Returns an instance of LanguageService
1002 *
1003 * @return \TYPO3\CMS\Lang\LanguageService
1004 */
1005 protected function getLanguageService() {
1006 return $GLOBALS['LANG'];
1007 }
1008
1009 /**
1010 * Returns the current BE user.
1011 *
1012 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
1013 */
1014 protected function getBackendUser() {
1015 return $GLOBALS['BE_USER'];
1016 }
1017
1018 }