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