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