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