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