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