[TASK] Move md5.js into an AMD module
[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\Routing\UriBuilder;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Database\ConnectionPool;
22 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
23 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
24 use TYPO3\CMS\Core\Imaging\Icon;
25 use TYPO3\CMS\Core\Imaging\IconFactory;
26 use TYPO3\CMS\Core\Messaging\FlashMessage;
27 use TYPO3\CMS\Core\Messaging\FlashMessageService;
28 use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException;
29 use TYPO3\CMS\Core\Resource\File;
30 use TYPO3\CMS\Core\Resource\Folder;
31 use TYPO3\CMS\Core\Resource\FolderInterface;
32 use TYPO3\CMS\Core\Resource\InaccessibleFolder;
33 use TYPO3\CMS\Core\Resource\ResourceFactory;
34 use TYPO3\CMS\Core\Resource\Utility\ListUtility;
35 use TYPO3\CMS\Core\Type\Bitmask\JsConfirmation;
36 use TYPO3\CMS\Core\Utility\GeneralUtility;
37 use TYPO3\CMS\Core\Utility\MathUtility;
38 use TYPO3\CMS\Filelist\Configuration\ThumbnailConfiguration;
39
40 /**
41 * Class for rendering of File>Filelist
42 * @internal this is a concrete TYPO3 controller implementation and solely used for EXT:filelist and not part of TYPO3's Core API.
43 */
44 class FileList
45 {
46 /**
47 * Default Max items shown
48 *
49 * @var int
50 */
51 public $iLimit = 40;
52
53 /**
54 * Thumbnails on records containing files (pictures)
55 *
56 * @var bool
57 */
58 public $thumbs = false;
59
60 /**
61 * Space icon used for alignment when no button is available
62 *
63 * @var string
64 */
65 public $spaceIcon;
66
67 /**
68 * Max length of strings
69 *
70 * @var int
71 */
72 public $fixedL = 30;
73
74 /**
75 * The field to sort by
76 *
77 * @var string
78 */
79 public $sort = '';
80
81 /**
82 * Reverse sorting flag
83 *
84 * @var bool
85 */
86 public $sortRev = 1;
87
88 /**
89 * @var int
90 */
91 public $firstElementNumber = 0;
92
93 /**
94 * @var bool
95 */
96 public $clipBoard = 0;
97
98 /**
99 * @var bool
100 */
101 public $bigControlPanel = 0;
102
103 /**
104 * @var string
105 */
106 public $JScode = '';
107
108 /**
109 * String with accumulated HTML content
110 *
111 * @var string
112 */
113 public $HTMLcode = '';
114
115 /**
116 * @var int
117 */
118 public $totalbytes = 0;
119
120 /**
121 * @var array
122 */
123 public $dirs = [];
124
125 /**
126 * @var array
127 */
128 public $files = [];
129
130 /**
131 * @var string
132 */
133 public $path = '';
134
135 /**
136 * OBSOLETE - NOT USED ANYMORE. leftMargin
137 *
138 * @var int
139 */
140 public $leftMargin = 0;
141
142 /**
143 * This could be set to the total number of items. Used by the fwd_rew_navigation...
144 *
145 * @var string
146 */
147 public $totalItems = '';
148
149 /**
150 * Decides the columns shown. Filled with values that refers to the keys of the data-array. $this->fieldArray[0] is the title column.
151 *
152 * @var array
153 */
154 public $fieldArray = [];
155
156 /**
157 * Set to zero, if you don't want a left-margin with addElement function
158 *
159 * @var int
160 */
161 public $setLMargin = 1;
162
163 /**
164 * Contains page translation languages
165 *
166 * @var array
167 */
168 public $pageOverlays = [];
169
170 /**
171 * Counter increased for each element. Used to index elements for the JavaScript-code that transfers to the clipboard
172 *
173 * @var int
174 */
175 public $counter = 0;
176
177 /**
178 * Contains sys language icons and titles
179 *
180 * @var array
181 */
182 public $languageIconTitles = [];
183
184 /**
185 * Script URL
186 *
187 * @var string
188 */
189 public $thisScript = '';
190
191 /**
192 * If set this is <td> CSS-classname for odd columns in addElement. Used with db_layout / pages section
193 *
194 * @var string
195 */
196 public $oddColumnsCssClass = '';
197
198 /**
199 * Counting the elements no matter what
200 *
201 * @var int
202 */
203 public $eCounter = 0;
204
205 /**
206 * @var TranslationConfigurationProvider
207 */
208 public $translateTools;
209
210 /**
211 * Keys are fieldnames and values are td-parameters to add in addElement(), please use $addElement_tdCSSClass for CSS-classes;
212 *
213 * @var array
214 */
215 public $addElement_tdParams = [];
216
217 /**
218 * @var int
219 */
220 public $no_noWrap = 0;
221
222 /**
223 * @var int
224 */
225 public $showIcon = 1;
226
227 /**
228 * Keys are fieldnames and values are td-css-classes to add in addElement();
229 *
230 * @var array
231 */
232 public $addElement_tdCssClass = [];
233
234 /**
235 * @var Folder
236 */
237 protected $folderObject;
238
239 /**
240 * @var array
241 */
242 public $CBnames = [];
243
244 /**
245 * @var Clipboard $clipObj
246 */
247 public $clipObj;
248
249 /**
250 * @var ResourceFactory
251 */
252 protected $resourceFactory;
253
254 /**
255 * @var IconFactory
256 */
257 protected $iconFactory;
258
259 /**
260 * @var int
261 */
262 protected $id = 0;
263
264 /**
265 * @var ThumbnailConfiguration
266 */
267 protected $thumbnailConfiguration;
268
269 /**
270 * Construct
271 */
272 public function __construct()
273 {
274 $backendUser = $this->getBackendUser();
275 if (isset($backendUser->uc['titleLen']) && $backendUser->uc['titleLen'] > 0) {
276 $this->fixedL = $backendUser->uc['titleLen'];
277 }
278 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
279 $this->getTranslateTools();
280 $this->determineScriptUrl();
281 $this->thumbnailConfiguration = GeneralUtility::makeInstance(ThumbnailConfiguration::class);
282 $this->iLimit = MathUtility::forceIntegerInRange(
283 $backendUser->getTSConfig()['options.']['file_list.']['filesPerPage'] ?? $this->iLimit,
284 1
285 );
286 }
287
288 /**
289 * @param ResourceFactory $resourceFactory
290 */
291 public function injectResourceFactory(ResourceFactory $resourceFactory)
292 {
293 $this->resourceFactory = $resourceFactory;
294 }
295
296 /**
297 * Initialization of class
298 *
299 * @param Folder $folderObject The folder to work on
300 * @param int $pointer Pointer
301 * @param bool $sort Sorting column
302 * @param bool $sortRev Sorting direction
303 * @param bool $clipBoard
304 * @param bool $bigControlPanel Show clipboard flag
305 */
306 public function start(Folder $folderObject, $pointer, $sort, $sortRev, $clipBoard = false, $bigControlPanel = false)
307 {
308 $this->folderObject = $folderObject;
309 $this->counter = 0;
310 $this->totalbytes = 0;
311 $this->JScode = '';
312 $this->HTMLcode = '';
313 $this->path = $folderObject->getReadablePath();
314 $this->sort = $sort;
315 $this->sortRev = $sortRev;
316 $this->firstElementNumber = $pointer;
317 $this->clipBoard = $clipBoard;
318 $this->bigControlPanel = $bigControlPanel;
319 // Setting the maximum length of the filenames to the user's settings or minimum 30 (= $this->fixedL)
320 $this->fixedL = max($this->fixedL, $this->getBackendUser()->uc['titleLen']);
321 $this->getLanguageService()->includeLLFile('EXT:core/Resources/Private/Language/locallang_common.xlf');
322 $this->resourceFactory = ResourceFactory::getInstance();
323 }
324
325 /**
326 * Reading files and directories, counting elements and generating the list in ->HTMLcode
327 */
328 public function generateList()
329 {
330 $this->HTMLcode .= $this->getTable('fileext,tstamp,size,rw,_REF_');
331 }
332
333 /**
334 * Wrapping input string in a link with clipboard command.
335 *
336 * @param string $string String to be linked - must be htmlspecialchar'ed / prepared before.
337 * @param string $_ unused
338 * @param string $cmd "cmd" value
339 * @param string $warning Warning for JS confirm message
340 * @return string Linked string
341 */
342 public function linkClipboardHeaderIcon($string, $_, $cmd, $warning = '')
343 {
344 $jsCode = 'document.dblistForm.cmd.value=' . GeneralUtility::quoteJSvalue($cmd)
345 . ';document.dblistForm.submit();';
346
347 $attributes = [];
348 if ($warning) {
349 $attributes['class'] = 'btn btn-default t3js-modal-trigger';
350 $attributes['data-href'] = 'javascript:' . $jsCode;
351 $attributes['data-severity'] = 'warning';
352 $attributes['data-content'] = $warning;
353 } else {
354 $attributes['class'] = 'btn btn-default';
355 $attributes['onclick'] = $jsCode . 'return false;';
356 }
357
358 $attributesString = '';
359 foreach ($attributes as $key => $value) {
360 $attributesString .= ' ' . $key . '="' . htmlspecialchars($value) . '"';
361 }
362 return '<a href="#" ' . $attributesString . '>' . $string . '</a>';
363 }
364
365 /**
366 * Returns a table with directories and files listed.
367 *
368 * @param array $rowlist Array of files from path
369 * @return string HTML-table
370 */
371 public function getTable($rowlist)
372 {
373 // prepare space icon
374 $this->spaceIcon = '<span class="btn btn-default disabled">' . $this->iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render() . '</span>';
375
376 // @todo use folder methods directly when they support filters
377 $storage = $this->folderObject->getStorage();
378 $storage->resetFileAndFolderNameFiltersToDefault();
379
380 // Only render the contents of a browsable storage
381 if ($this->folderObject->getStorage()->isBrowsable()) {
382 try {
383 $foldersCount = $storage->countFoldersInFolder($this->folderObject);
384 $filesCount = $storage->countFilesInFolder($this->folderObject);
385 } catch (InsufficientFolderAccessPermissionsException $e) {
386 $foldersCount = 0;
387 $filesCount = 0;
388 }
389
390 if ($foldersCount <= $this->firstElementNumber) {
391 $foldersFrom = false;
392 $foldersNum = false;
393 } else {
394 $foldersFrom = $this->firstElementNumber;
395 if ($this->firstElementNumber + $this->iLimit > $foldersCount) {
396 $foldersNum = $foldersCount - $this->firstElementNumber;
397 } else {
398 $foldersNum = $this->iLimit;
399 }
400 }
401 if ($foldersCount >= $this->firstElementNumber + $this->iLimit) {
402 $filesFrom = false;
403 $filesNum = false;
404 } else {
405 if ($this->firstElementNumber <= $foldersCount) {
406 $filesFrom = 0;
407 $filesNum = $this->iLimit - $foldersNum;
408 } else {
409 $filesFrom = $this->firstElementNumber - $foldersCount;
410 if ($filesFrom + $this->iLimit > $filesCount) {
411 $filesNum = $filesCount - $filesFrom;
412 } else {
413 $filesNum = $this->iLimit;
414 }
415 }
416 }
417
418 $folders = $storage->getFoldersInFolder($this->folderObject, $foldersFrom, $foldersNum, true, false, trim($this->sort), (bool)$this->sortRev);
419 $files = $this->folderObject->getFiles($filesFrom, $filesNum, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, false, trim($this->sort), (bool)$this->sortRev);
420 $this->totalItems = $foldersCount + $filesCount;
421 // Adds the code of files/dirs
422 $out = '';
423 $titleCol = 'file';
424 // Cleaning rowlist for duplicates and place the $titleCol as the first column always!
425 $rowlist = '_LOCALIZATION_,' . $rowlist;
426 $rowlist = GeneralUtility::rmFromList($titleCol, $rowlist);
427 $rowlist = GeneralUtility::uniqueList($rowlist);
428 $rowlist = $rowlist ? $titleCol . ',' . $rowlist : $titleCol;
429 if ($this->clipBoard) {
430 $rowlist = str_replace('_LOCALIZATION_,', '_LOCALIZATION_,_CLIPBOARD_,', $rowlist);
431 $this->addElement_tdCssClass['_CLIPBOARD_'] = 'col-clipboard';
432 }
433 if ($this->bigControlPanel) {
434 $rowlist = str_replace('_LOCALIZATION_,', '_LOCALIZATION_,_CONTROL_,', $rowlist);
435 $this->addElement_tdCssClass['_CONTROL_'] = 'col-control';
436 }
437 $this->fieldArray = explode(',', $rowlist);
438
439 // Add classes to table cells
440 $this->addElement_tdCssClass[$titleCol] = 'col-title col-responsive';
441 $this->addElement_tdCssClass['_LOCALIZATION_'] = 'col-localizationa';
442
443 $folders = ListUtility::resolveSpecialFolderNames($folders);
444
445 $iOut = '';
446 // Directories are added
447 $this->eCounter = $this->firstElementNumber;
448 list(, $code) = $this->fwd_rwd_nav();
449 $iOut .= $code;
450
451 $iOut .= $this->formatDirList($folders);
452 // Files are added
453 $iOut .= $this->formatFileList($files);
454
455 $this->eCounter = $this->firstElementNumber + $this->iLimit < $this->totalItems
456 ? $this->firstElementNumber + $this->iLimit
457 : -1;
458 list(, $code) = $this->fwd_rwd_nav();
459 $iOut .= $code;
460
461 // Header line is drawn
462 $theData = [];
463 foreach ($this->fieldArray as $v) {
464 if ($v === '_CLIPBOARD_' && $this->clipBoard) {
465 $cells = [];
466 $table = '_FILE';
467 $elFromTable = $this->clipObj->elFromTable($table);
468 if (!empty($elFromTable) && $this->folderObject->checkActionPermission('write')) {
469 $addPasteButton = true;
470 $elToConfirm = [];
471 foreach ($elFromTable as $key => $element) {
472 $clipBoardElement = $this->resourceFactory->retrieveFileOrFolderObject($element);
473 if ($clipBoardElement instanceof Folder && $clipBoardElement->getStorage()->isWithinFolder($clipBoardElement, $this->folderObject)) {
474 $addPasteButton = false;
475 }
476 $elToConfirm[$key] = $clipBoardElement->getName();
477 }
478 if ($addPasteButton) {
479 $cells[] = '<a class="btn btn-default t3js-modal-trigger"' .
480 ' href="' . htmlspecialchars($this->clipObj->pasteUrl(
481 '_FILE',
482 $this->folderObject->getCombinedIdentifier()
483 )) . '"'
484 . ' data-content="' . htmlspecialchars($this->clipObj->confirmMsgText(
485 '_FILE',
486 $this->path,
487 'into',
488 $elToConfirm
489 )) . '"'
490 . ' data-severity="warning"'
491 . ' data-title="' . htmlspecialchars($this->getLanguageService()->getLL('clip_paste')) . '"'
492 . ' title="' . htmlspecialchars($this->getLanguageService()->getLL('clip_paste')) . '">'
493 . $this->iconFactory->getIcon('actions-document-paste-into', Icon::SIZE_SMALL)
494 ->render()
495 . '</a>';
496 }
497 }
498 if ($this->clipObj->current !== 'normal' && $iOut) {
499 $cells[] = $this->linkClipboardHeaderIcon('<span title="' . htmlspecialchars($this->getLanguageService()->getLL('clip_selectMarked')) . '">' . $this->iconFactory->getIcon('actions-edit-copy', Icon::SIZE_SMALL)->render() . '</span>', $table, 'setCB');
500 $cells[] = $this->linkClipboardHeaderIcon('<span title="' . htmlspecialchars($this->getLanguageService()->getLL('clip_deleteMarked')) . '">' . $this->iconFactory->getIcon('actions-edit-delete', Icon::SIZE_SMALL)->render(), $table, 'delete', $this->getLanguageService()->getLL('clip_deleteMarkedWarning'));
501 $onClick = 'checkOffCB(' . GeneralUtility::quoteJSvalue(implode(',', $this->CBnames)) . ', this); return false;';
502 $cells[] = '<a class="btn btn-default" rel="" href="#" onclick="' . htmlspecialchars($onClick) . '" title="' . htmlspecialchars($this->getLanguageService()->getLL('clip_markRecords')) . '">' . $this->iconFactory->getIcon('actions-document-select', Icon::SIZE_SMALL)->render() . '</a>';
503 }
504 $theData[$v] = implode('', $cells);
505 } elseif ($v === '_REF_') {
506 $theData[$v] = htmlspecialchars($this->getLanguageService()->getLL('c_' . $v));
507 } else {
508 // Normal row:
509 $theT = $this->linkWrapSort(htmlspecialchars($this->getLanguageService()->getLL('c_' . $v)), $this->folderObject->getCombinedIdentifier(), $v);
510 $theData[$v] = $theT;
511 }
512 }
513
514 $out .= '<thead>' . $this->addElement(1, '', $theData, '', '', '', 'th') . '</thead>';
515 $out .= '<tbody>' . $iOut . '</tbody>';
516 // half line is drawn
517 // finish
518 $out = '
519 <!--
520 Filelist table:
521 -->
522 <div class="panel panel-default">
523 <div class="table-fit">
524 <table class="table table-striped table-hover" id="typo3-filelist">
525 ' . $out . '
526 </table>
527 </div>
528 </div>';
529 } else {
530 /** @var FlashMessage $flashMessage */
531 $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $this->getLanguageService()->getLL('storageNotBrowsableMessage'), $this->getLanguageService()->getLL('storageNotBrowsableTitle'), FlashMessage::INFO);
532 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageService $flashMessageService */
533 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
534 /** @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue $defaultFlashMessageQueue */
535 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
536 $defaultFlashMessageQueue->enqueue($flashMessage);
537 $out = '';
538 }
539 return $out;
540 }
541
542 /**
543 * Returns a table-row with the content from the fields in the input data array.
544 * OBS: $this->fieldArray MUST be set! (represents the list of fields to display)
545 *
546 * @param int $h Is an integer >=0 and denotes how tall an element is. Set to '0' makes a halv line, -1 = full line, set to 1 makes a 'join' and above makes 'line'
547 * @param string $icon Is the <img>+<a> of the record. If not supplied the first 'join'-icon will be a 'line' instead
548 * @param array $data Is the dataarray, record with the fields. Notice: These fields are (currently) NOT htmlspecialchar'ed before being wrapped in <td>-tags
549 * @param string $rowParams Is insert in the <tr>-tags. Must carry a ' ' as first character
550 * @param string $_ OBSOLETE - NOT USED ANYMORE. $lMargin is the leftMargin (int)
551 * @param string $_2 OBSOLETE - NOT USED ANYMORE. Is the HTML <img>-tag for an alternative 'gfx/ol/line.gif'-icon (used in the top)
552 * @param string $colType Defines the tag being used for the columns. Default is td.
553 *
554 * @return string HTML content for the table row
555 */
556 public function addElement($h, $icon, $data, $rowParams = '', $_ = '', $_2 = '', $colType = 'td')
557 {
558 $colType = ($colType === 'th') ? 'th' : 'td';
559 $noWrap = $this->no_noWrap ? '' : ' nowrap';
560 // Start up:
561 $l10nParent = (int)($data['_l10nparent_'] ?? 0);
562 $out = '
563 <!-- Element, begin: -->
564 <tr ' . $rowParams . ' data-uid="' . (int)($data['uid'] ?? 0) . '" data-l10nparent="' . $l10nParent . '">';
565 // Show icon and lines
566 if ($this->showIcon) {
567 $out .= '
568 <' . $colType . ' class="col-icon nowrap">';
569 if (!$h) {
570 $out .= '&nbsp;';
571 } else {
572 for ($a = 0; $a < $h; $a++) {
573 if (!$a) {
574 if ($icon) {
575 $out .= $icon;
576 }
577 }
578 }
579 }
580 $out .= '</' . $colType . '>
581 ';
582 }
583 // Init rendering.
584 $colsp = '';
585 $lastKey = '';
586 $c = 0;
587 $ccount = 0;
588 // __label is used as the label key to circumvent problems with uid used as label (see #67756)
589 // as it was introduced later on, check if it really exists before using it
590 $fields = $this->fieldArray;
591 if ($colType === 'td' && array_key_exists('__label', $data)) {
592 $fields[0] = '__label';
593 }
594 // Traverse field array which contains the data to present:
595 foreach ($fields as $vKey) {
596 if (isset($data[$vKey])) {
597 if ($lastKey) {
598 $cssClass = $this->addElement_tdCssClass[$lastKey] ?? '';
599 if ($this->oddColumnsCssClass && $ccount % 2 == 0) {
600 $cssClass = implode(' ', [$cssClass, $this->oddColumnsCssClass]);
601 }
602 $out .= '
603 <' . $colType . ' class="' . $cssClass . $noWrap . '"' . $colsp . ($this->addElement_tdParams[$lastKey] ?? '') . '>' . $data[$lastKey] . '</' . $colType . '>';
604 }
605 $lastKey = $vKey;
606 $c = 1;
607 $ccount++;
608 } else {
609 if (!$lastKey) {
610 $lastKey = $vKey;
611 }
612 $c++;
613 }
614 if ($c > 1) {
615 $colsp = ' colspan="' . $c . '"';
616 } else {
617 $colsp = '';
618 }
619 }
620 if ($lastKey) {
621 $cssClass = $this->addElement_tdCssClass[$lastKey] ?? '';
622 if ($this->oddColumnsCssClass) {
623 $cssClass = implode(' ', [$cssClass, $this->oddColumnsCssClass]);
624 }
625 $out .= '
626 <' . $colType . ' class="' . $cssClass . $noWrap . '"' . $colsp . ($this->addElement_tdParams[$lastKey] ?? '') . '>' . $data[$lastKey] . '</' . $colType . '>';
627 }
628 // End row
629 $out .= '
630 </tr>';
631 // Return row.
632 return $out;
633 }
634
635 /**
636 * Dummy function, used to write the top of a table listing.
637 */
638 public function writeTop()
639 {
640 }
641
642 /**
643 * Creates a forward/reverse button based on the status of ->eCounter, ->firstElementNumber, ->iLimit
644 *
645 * @param string $table Table name
646 * @return array array([boolean], [HTML]) where [boolean] is 1 for reverse element, [HTML] is the table-row code for the element
647 */
648 public function fwd_rwd_nav($table = '')
649 {
650 $code = '';
651 if ($this->eCounter >= $this->firstElementNumber && $this->eCounter < $this->firstElementNumber + $this->iLimit) {
652 if ($this->firstElementNumber && $this->eCounter == $this->firstElementNumber) {
653 // Reverse
654 $theData = [];
655 $titleCol = $this->fieldArray[0];
656 $theData[$titleCol] = $this->fwd_rwd_HTML('fwd', $this->eCounter, $table);
657 $code = $this->addElement(1, '', $theData, 'class="fwd_rwd_nav"');
658 }
659 return [1, $code];
660 }
661 if ($this->eCounter == $this->firstElementNumber + $this->iLimit) {
662 // Forward
663 $theData = [];
664 $titleCol = $this->fieldArray[0];
665 $theData[$titleCol] = $this->fwd_rwd_HTML('rwd', $this->eCounter, $table);
666 $code = $this->addElement(1, '', $theData, 'class="fwd_rwd_nav"');
667 }
668 return [0, $code];
669 }
670
671 /**
672 * Creates the button with link to either forward or reverse
673 *
674 * @param string $type Type: "fwd" or "rwd
675 * @param int $pointer Pointer
676 * @param string $table Table name
677 * @return string
678 * @internal
679 */
680 public function fwd_rwd_HTML($type, $pointer, $table = '')
681 {
682 $content = '';
683 $tParam = $table ? '&table=' . rawurlencode($table) : '';
684 switch ($type) {
685 case 'fwd':
686 $href = $this->listURL() . '&pointer=' . ($pointer - $this->iLimit) . $tParam;
687 $content = '<a href="' . htmlspecialchars($href) . '">' . $this->iconFactory->getIcon(
688 'actions-move-up',
689 Icon::SIZE_SMALL
690 )->render() . ' <i>[' . (max(0, $pointer - $this->iLimit) + 1) . ' - ' . $pointer . ']</i></a>';
691 break;
692 case 'rwd':
693 $href = $this->listURL() . '&pointer=' . $pointer . $tParam;
694 $content = '<a href="' . htmlspecialchars($href) . '">' . $this->iconFactory->getIcon(
695 'actions-move-down',
696 Icon::SIZE_SMALL
697 )->render() . ' <i>[' . ($pointer + 1) . ' - ' . $this->totalItems . ']</i></a>';
698 break;
699 }
700 return $content;
701 }
702
703 /**
704 * Returning JavaScript for ClipBoard functionality.
705 *
706 * @return string
707 */
708 public function CBfunctions()
709 {
710 return '
711 // checkOffCB()
712 function checkOffCB(listOfCBnames, link) { //
713 var checkBoxes, flag, i;
714 var checkBoxes = listOfCBnames.split(",");
715 if (link.rel === "") {
716 link.rel = "allChecked";
717 flag = true;
718 } else {
719 link.rel = "";
720 flag = false;
721 }
722 for (i = 0; i < checkBoxes.length; i++) {
723 setcbValue(checkBoxes[i], flag);
724 }
725 }
726 // cbValue()
727 function cbValue(CBname) { //
728 var CBfullName = "CBC["+CBname+"]";
729 return (document.dblistForm[CBfullName] && document.dblistForm[CBfullName].checked ? 1 : 0);
730 }
731 // setcbValue()
732 function setcbValue(CBname,flag) { //
733 CBfullName = "CBC["+CBname+"]";
734 if(document.dblistForm[CBfullName]) {
735 document.dblistForm[CBfullName].checked = flag ? "on" : 0;
736 }
737 }
738
739 ';
740 }
741
742 /**
743 * Initializes page languages and icons
744 */
745 public function initializeLanguages()
746 {
747 // Look up page overlays:
748 $localizationParentField = $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'];
749 $languageField = $GLOBALS['TCA']['pages']['ctrl']['languageField'];
750 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
751 ->getQueryBuilderForTable('pages');
752 $queryBuilder->getRestrictions()
753 ->removeAll()
754 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
755 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
756 $result = $queryBuilder
757 ->select('*')
758 ->from('pages')
759 ->where(
760 $queryBuilder->expr()->andX(
761 $queryBuilder->expr()->eq(
762 $localizationParentField,
763 $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
764 ),
765 $queryBuilder->expr()->gt(
766 $languageField,
767 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
768 )
769 )
770 )
771 ->execute();
772
773 $this->pageOverlays = [];
774 while ($row = $result->fetch()) {
775 $this->pageOverlays[$row[$languageField]] = $row;
776 }
777
778 $this->languageIconTitles = $this->getTranslateTools()->getSystemLanguages($this->id);
779 }
780
781 /**
782 * Return the icon for the language
783 *
784 * @param int $sys_language_uid Sys language uid
785 * @param bool $addAsAdditionalText If set to true, only the flag is returned
786 * @return string Language icon
787 */
788 public function languageFlag($sys_language_uid, $addAsAdditionalText = true)
789 {
790 $out = '';
791 $title = htmlspecialchars($this->languageIconTitles[$sys_language_uid]['title']);
792 if ($this->languageIconTitles[$sys_language_uid]['flagIcon']) {
793 $out .= '<span title="' . $title . '">' . $this->iconFactory->getIcon(
794 $this->languageIconTitles[$sys_language_uid]['flagIcon'],
795 Icon::SIZE_SMALL
796 )->render() . '</span>';
797 if (!$addAsAdditionalText) {
798 return $out;
799 }
800 $out .= '&nbsp;';
801 }
802 $out .= $title;
803 return $out;
804 }
805
806 /**
807 * If there is a parent folder and user has access to it, return an icon
808 * which is linked to the filelist of the parent folder.
809 *
810 * @param Folder $currentFolder
811 * @return string
812 */
813 protected function getLinkToParentFolder(Folder $currentFolder)
814 {
815 $levelUp = '';
816 try {
817 $currentStorage = $currentFolder->getStorage();
818 $parentFolder = $currentFolder->getParentFolder();
819 if ($parentFolder->getIdentifier() !== $currentFolder->getIdentifier() && $currentStorage->isWithinFileMountBoundaries($parentFolder)) {
820 $levelUp = $this->linkWrapDir(
821 '<span title="' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.upOneLevel')) . '">'
822 . $this->iconFactory->getIcon('actions-view-go-up', Icon::SIZE_SMALL)->render()
823 . '</span>',
824 $parentFolder
825 );
826 }
827 } catch (\Exception $e) {
828 }
829 return $levelUp;
830 }
831
832 /**
833 * Gets the number of files and total size of a folder
834 *
835 * @return string
836 */
837 public function getFolderInfo()
838 {
839 if ($this->counter == 1) {
840 $fileLabel = htmlspecialchars($this->getLanguageService()->getLL('file'));
841 } else {
842 $fileLabel = htmlspecialchars($this->getLanguageService()->getLL('files'));
843 }
844 return $this->counter . ' ' . $fileLabel . ', ' . GeneralUtility::formatSize($this->totalbytes, htmlspecialchars($this->getLanguageService()->getLL('byteSizeUnits')));
845 }
846
847 /**
848 * This returns tablerows for the directories in the array $items['sorting'].
849 *
850 * @param Folder[] $folders Folders of \TYPO3\CMS\Core\Resource\Folder
851 * @return string HTML table rows.
852 */
853 public function formatDirList(array $folders)
854 {
855 $out = '';
856 foreach ($folders as $folderName => $folderObject) {
857 $role = $folderObject->getRole();
858 if ($role === FolderInterface::ROLE_PROCESSING) {
859 // don't show processing-folder
860 continue;
861 }
862 if ($role !== FolderInterface::ROLE_DEFAULT) {
863 $displayName = '<strong>' . htmlspecialchars($folderName) . '</strong>';
864 } else {
865 $displayName = htmlspecialchars($folderName);
866 }
867
868 $isLocked = $folderObject instanceof InaccessibleFolder;
869 $isWritable = $folderObject->checkActionPermission('write');
870
871 // Initialization
872 $this->counter++;
873
874 // The icon with link
875 $theIcon = '<span title="' . htmlspecialchars($folderName) . '">' . $this->iconFactory->getIconForResource($folderObject, Icon::SIZE_SMALL)->render() . '</span>';
876 if (!$isLocked) {
877 $theIcon = BackendUtility::wrapClickMenuOnIcon($theIcon, 'sys_file', $folderObject->getCombinedIdentifier());
878 }
879
880 // Preparing and getting the data-array
881 $theData = [];
882 if ($isLocked) {
883 foreach ($this->fieldArray as $field) {
884 $theData[$field] = '';
885 }
886 $theData['file'] = $displayName;
887 } else {
888 foreach ($this->fieldArray as $field) {
889 switch ($field) {
890 case 'size':
891 try {
892 $numFiles = $folderObject->getFileCount();
893 } catch (InsufficientFolderAccessPermissionsException $e) {
894 $numFiles = 0;
895 }
896 $theData[$field] = $numFiles . ' ' . htmlspecialchars($this->getLanguageService()->getLL(($numFiles === 1 ? 'file' : 'files')));
897 break;
898 case 'rw':
899 $theData[$field] = '<strong class="text-danger">' . htmlspecialchars($this->getLanguageService()->getLL('read')) . '</strong>' . (!$isWritable ? '' : '<strong class="text-danger">' . htmlspecialchars($this->getLanguageService()->getLL('write')) . '</strong>');
900 break;
901 case 'fileext':
902 $theData[$field] = htmlspecialchars($this->getLanguageService()->getLL('folder'));
903 break;
904 case 'tstamp':
905 $tstamp = $folderObject->getModificationTime();
906 $theData[$field] = $tstamp ? BackendUtility::date($tstamp) : '-';
907 break;
908 case 'file':
909 $theData[$field] = $this->linkWrapDir($displayName, $folderObject);
910 break;
911 case '_CONTROL_':
912 $theData[$field] = $this->makeEdit($folderObject);
913 break;
914 case '_CLIPBOARD_':
915 $theData[$field] = $this->makeClip($folderObject);
916 break;
917 case '_REF_':
918 $theData[$field] = $this->makeRef($folderObject);
919 break;
920 default:
921 $theData[$field] = GeneralUtility::fixed_lgd_cs($theData[$field] ?? '', $this->fixedL);
922 }
923 }
924 }
925 $out .= $this->addElement(1, $theIcon, $theData);
926 }
927 return $out;
928 }
929
930 /**
931 * Wraps the directory-titles
932 *
933 * @param string $title String to be wrapped in links
934 * @param Folder $folderObject Folder to work on
935 * @return string HTML
936 */
937 public function linkWrapDir($title, Folder $folderObject)
938 {
939 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
940 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
941 $href = (string)$uriBuilder->buildUriFromRoute('file_FilelistList', ['id' => $folderObject->getCombinedIdentifier()]);
942 $onclick = ' onclick="' . htmlspecialchars('top.document.getElementsByName("nav_frame")[0].contentWindow.Tree.highlightActiveItem("file","folder' . GeneralUtility::md5int($folderObject->getCombinedIdentifier()) . '_"+top.fsMod.currentBank)') . '"';
943 // Sometimes $code contains plain HTML tags. In such a case the string should not be modified!
944 if ((string)$title === strip_tags($title)) {
945 return '<a href="' . htmlspecialchars($href) . '"' . $onclick . ' title="' . htmlspecialchars($title) . '">' . $title . '</a>';
946 }
947 return '<a href="' . htmlspecialchars($href) . '"' . $onclick . '>' . $title . '</a>';
948 }
949
950 /**
951 * Wraps filenames in links which opens the metadata editor.
952 *
953 * @param string $code String to be wrapped in links
954 * @param File $fileObject File to be linked
955 * @return string HTML
956 */
957 public function linkWrapFile($code, File $fileObject)
958 {
959 try {
960 if ($fileObject instanceof File && $fileObject->isIndexed() && $fileObject->checkActionPermission('editMeta') && $this->getBackendUser()->check('tables_modify', 'sys_file_metadata')) {
961 $metaData = $fileObject->getMetaData()->get();
962 $urlParameters = [
963 'edit' => [
964 'sys_file_metadata' => [
965 $metaData['uid'] => 'edit'
966 ]
967 ],
968 'returnUrl' => $this->listURL()
969 ];
970 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
971 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
972 $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
973 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.editMetadata'));
974 $code = '<a class="responsive-title" href="' . htmlspecialchars($url) . '" title="' . $title . '">' . $code . '</a>';
975 }
976 } catch (\Exception $e) {
977 // intentional fall-through
978 }
979 return $code;
980 }
981
982 /**
983 * Returns list URL; This is the URL of the current script with id and imagemode parameters, that's all.
984 * The URL however is not relative, otherwise GeneralUtility::sanitizeLocalUrl() would say that
985 * the URL would be invalid
986 *
987 * @param string $altId
988 * @param string $table Table name to display. Enter "-1" for the current table.
989 * @param string $exclList Comma separated list of fields NOT to include ("sortField", "sortRev" or "firstElementNumber")
990 *
991 * @return string URL
992 */
993 public function listURL($altId = '', $table = '-1', $exclList = '')
994 {
995 return GeneralUtility::linkThisScript([
996 'target' => rawurlencode($this->folderObject->getCombinedIdentifier()),
997 'imagemode' => $this->thumbs
998 ]);
999 }
1000
1001 /**
1002 * This returns tablerows for the files in the array $items['sorting'].
1003 *
1004 * @param File[] $files File items
1005 * @return string HTML table rows.
1006 */
1007 public function formatFileList(array $files)
1008 {
1009 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
1010 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
1011 $out = '';
1012 // first two keys are "0" (default) and "-1" (multiple), after that comes the "other languages"
1013 $allSystemLanguages = GeneralUtility::makeInstance(TranslationConfigurationProvider::class)->getSystemLanguages();
1014 $systemLanguages = array_filter($allSystemLanguages, function ($languageRecord) {
1015 if ($languageRecord['uid'] === -1 || $languageRecord['uid'] === 0 || !$this->getBackendUser()->checkLanguageAccess($languageRecord['uid'])) {
1016 return false;
1017 }
1018 return true;
1019 });
1020
1021 foreach ($files as $fileObject) {
1022 // Initialization
1023 $this->counter++;
1024 $this->totalbytes += $fileObject->getSize();
1025 $ext = $fileObject->getExtension();
1026 $fileName = trim($fileObject->getName());
1027 // The icon with link
1028 $theIcon = '<span title="' . htmlspecialchars($fileName . ' [' . (int)$fileObject->getUid() . ']') . '">'
1029 . $this->iconFactory->getIconForResource($fileObject, Icon::SIZE_SMALL)->render() . '</span>';
1030 $theIcon = BackendUtility::wrapClickMenuOnIcon($theIcon, 'sys_file', $fileObject->getCombinedIdentifier());
1031 // Preparing and getting the data-array
1032 $theData = [];
1033 foreach ($this->fieldArray as $field) {
1034 switch ($field) {
1035 case 'size':
1036 $theData[$field] = GeneralUtility::formatSize($fileObject->getSize(), htmlspecialchars($this->getLanguageService()->getLL('byteSizeUnits')));
1037 break;
1038 case 'rw':
1039 $theData[$field] = '' . (!$fileObject->checkActionPermission('read') ? ' ' : '<strong class="text-danger">' . htmlspecialchars($this->getLanguageService()->getLL('read')) . '</strong>') . (!$fileObject->checkActionPermission('write') ? '' : '<strong class="text-danger">' . htmlspecialchars($this->getLanguageService()->getLL('write')) . '</strong>');
1040 break;
1041 case 'fileext':
1042 $theData[$field] = strtoupper($ext);
1043 break;
1044 case 'tstamp':
1045 $theData[$field] = BackendUtility::date($fileObject->getModificationTime());
1046 break;
1047 case '_CONTROL_':
1048 $theData[$field] = $this->makeEdit($fileObject);
1049 break;
1050 case '_CLIPBOARD_':
1051 $theData[$field] = $this->makeClip($fileObject);
1052 break;
1053 case '_LOCALIZATION_':
1054 if (!empty($systemLanguages) && $fileObject->isIndexed() && $fileObject->checkActionPermission('editMeta') && $this->getBackendUser()->check('tables_modify', 'sys_file_metadata')) {
1055 $metaDataRecord = $fileObject->getMetaData()->get();
1056 $translations = $this->getTranslationsForMetaData($metaDataRecord);
1057 $languageCode = '';
1058
1059 foreach ($systemLanguages as $language) {
1060 $languageId = $language['uid'];
1061 $flagIcon = $language['flagIcon'];
1062 if (array_key_exists($languageId, $translations)) {
1063 $title = htmlspecialchars(sprintf($this->getLanguageService()->getLL('editMetadataForLanguage'), $language['title']));
1064 // @todo the overlay for the flag needs to be added ($flagIcon . '-overlay')
1065 $urlParameters = [
1066 'edit' => [
1067 'sys_file_metadata' => [
1068 $translations[$languageId]['uid'] => 'edit'
1069 ]
1070 ],
1071 'returnUrl' => $this->listURL()
1072 ];
1073 $flagButtonIcon = $this->iconFactory->getIcon($flagIcon, Icon::SIZE_SMALL, 'overlay-edit')->render();
1074 $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
1075 $languageCode .= '<a href="' . htmlspecialchars($url) . '" class="btn btn-default" title="' . $title . '">'
1076 . $flagButtonIcon . '</a>';
1077 } else {
1078 $parameters = [
1079 'justLocalized' => 'sys_file_metadata:' . $metaDataRecord['uid'] . ':' . $languageId,
1080 'returnUrl' => $this->listURL()
1081 ];
1082 $returnUrl = (string)$uriBuilder->buildUriFromRoute('record_edit', $parameters);
1083 $href = BackendUtility::getLinkToDataHandlerAction(
1084 '&cmd[sys_file_metadata][' . $metaDataRecord['uid'] . '][localize]=' . $languageId,
1085 $returnUrl
1086 );
1087 $flagButtonIcon = '<span title="' . htmlspecialchars(sprintf($this->getLanguageService()->getLL('createMetadataForLanguage'), $language['title'])) . '">' . $this->iconFactory->getIcon($flagIcon, Icon::SIZE_SMALL, 'overlay-new')->render() . '</span>';
1088 $languageCode .= '<a href="' . htmlspecialchars($href) . '" class="btn btn-default">' . $flagButtonIcon . '</a> ';
1089 }
1090 }
1091
1092 // Hide flag button bar when not translated yet
1093 $theData[$field] = ' <div class="localisationData btn-group" data-fileid="' . $fileObject->getUid() . '"' .
1094 (empty($translations) ? ' style="display: none;"' : '') . '>' . $languageCode . '</div>';
1095 $theData[$field] .= '<a class="btn btn-default filelist-translationToggler" data-fileid="' . $fileObject->getUid() . '">' .
1096 '<span title="' . htmlspecialchars($this->getLanguageService()->getLL('translateMetadata')) . '">'
1097 . $this->iconFactory->getIcon('mimetypes-x-content-page-language-overlay', Icon::SIZE_SMALL)->render() . '</span>'
1098 . '</a>';
1099 }
1100 break;
1101 case '_REF_':
1102 $theData[$field] = $this->makeRef($fileObject);
1103 break;
1104 case 'file':
1105 // Edit metadata of file
1106 $theData[$field] = $this->linkWrapFile(htmlspecialchars($fileName), $fileObject);
1107
1108 if ($fileObject->isMissing()) {
1109 $theData[$field] .= '<span class="label label-danger label-space-left">'
1110 . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:warning.file_missing'))
1111 . '</span>';
1112 // Thumbnails?
1113 } elseif ($this->thumbs && ($this->isImage($ext) || $this->isMediaFile($ext))) {
1114 $imageUri = BackendUtility::getThumbnailUrl($fileObject->getUid(), [
1115 'width' => $this->thumbnailConfiguration->getWidth(),
1116 'height' => $this->thumbnailConfiguration->getHeight()
1117 ]);
1118 $theData[$field] .= '<br /><img src="' . htmlspecialchars($imageUri) . '" ' .
1119 'width="' . $this->thumbnailConfiguration->getWidth() . '" ' .
1120 'title="' . htmlspecialchars($fileName) . '" alt="" />';
1121 }
1122 break;
1123 default:
1124 $theData[$field] = '';
1125 if ($fileObject->hasProperty($field)) {
1126 $theData[$field] = htmlspecialchars(GeneralUtility::fixed_lgd_cs($fileObject->getProperty($field), $this->fixedL));
1127 }
1128 }
1129 }
1130 $out .= $this->addElement(1, $theIcon, $theData);
1131 }
1132 return $out;
1133 }
1134
1135 /**
1136 * Fetch the translations for a sys_file_metadata record
1137 *
1138 * @param $metaDataRecord
1139 * @return array keys are the sys_language uids, values are the $rows
1140 */
1141 protected function getTranslationsForMetaData($metaDataRecord)
1142 {
1143 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_metadata');
1144 $queryBuilder->getRestrictions()->removeAll();
1145 $translationRecords = $queryBuilder->select('*')
1146 ->from('sys_file_metadata')
1147 ->where(
1148 $queryBuilder->expr()->eq(
1149 $GLOBALS['TCA']['sys_file_metadata']['ctrl']['transOrigPointerField'],
1150 $queryBuilder->createNamedParameter($metaDataRecord['uid'], \PDO::PARAM_INT)
1151 ),
1152 $queryBuilder->expr()->gt(
1153 $GLOBALS['TCA']['sys_file_metadata']['ctrl']['languageField'],
1154 $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
1155 )
1156 )
1157 ->execute()
1158 ->fetchAll();
1159
1160 $translations = [];
1161 foreach ($translationRecords as $record) {
1162 $translations[$record[$GLOBALS['TCA']['sys_file_metadata']['ctrl']['languageField']]] = $record;
1163 }
1164 return $translations;
1165 }
1166
1167 /**
1168 * Returns TRUE if $ext is an image-extension according to $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
1169 *
1170 * @param string $ext File extension
1171 * @return bool
1172 */
1173 public function isImage($ext)
1174 {
1175 return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], strtolower($ext));
1176 }
1177
1178 /**
1179 * Returns TRUE if $ext is an media-extension according to $GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']
1180 *
1181 * @param string $ext File extension
1182 * @return bool
1183 */
1184 public function isMediaFile($ext)
1185 {
1186 return GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'], strtolower($ext));
1187 }
1188
1189 /**
1190 * Wraps the directory-titles ($code) in a link to filelist/Modules/Filelist/index.php (id=$path) and sorting commands...
1191 *
1192 * @param string $code String to be wrapped
1193 * @param string $folderIdentifier ID (path)
1194 * @param string $col Sorting column
1195 * @return string HTML
1196 */
1197 public function linkWrapSort($code, $folderIdentifier, $col)
1198 {
1199 $params = ['id' => $folderIdentifier, 'SET' => ['sort' => $col]];
1200
1201 if ($this->sort === $col) {
1202 // Check reverse sorting
1203 $params['SET']['reverse'] = ($this->sortRev ? '0' : '1');
1204 $sortArrow = $this->iconFactory->getIcon('status-status-sorting-' . ($this->sortRev ? 'desc' : 'asc'), Icon::SIZE_SMALL)->render();
1205 } else {
1206 $params['SET']['reverse'] = 0;
1207 $sortArrow = '';
1208 }
1209 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
1210 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
1211 $href = (string)$uriBuilder->buildUriFromRoute('file_FilelistList', $params);
1212 return '<a href="' . htmlspecialchars($href) . '">' . $code . ' ' . $sortArrow . '</a>';
1213 }
1214
1215 /**
1216 * Creates the clipboard control pad
1217 *
1218 * @param File|Folder $fileOrFolderObject Array with information about the file/directory for which to make the clipboard panel for the listing.
1219 * @return string HTML-table
1220 */
1221 public function makeClip($fileOrFolderObject)
1222 {
1223 if (!$fileOrFolderObject->checkActionPermission('read')) {
1224 return '';
1225 }
1226 $cells = [];
1227 $fullIdentifier = $fileOrFolderObject->getCombinedIdentifier();
1228 $fullName = $fileOrFolderObject->getName();
1229 $md5 = GeneralUtility::shortMD5($fullIdentifier);
1230 // For normal clipboard, add copy/cut buttons:
1231 if ($this->clipObj->current === 'normal') {
1232 $isSel = $this->clipObj->isSelected('_FILE', $md5);
1233 $copyTitle = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.copy'));
1234 $cutTitle = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.cut'));
1235 $copyIcon = $this->iconFactory->getIcon('actions-edit-copy', Icon::SIZE_SMALL)->render();
1236 $cutIcon = $this->iconFactory->getIcon('actions-edit-cut', Icon::SIZE_SMALL)->render();
1237
1238 if ($isSel === 'copy') {
1239 $copyIcon = $this->iconFactory->getIcon('actions-edit-copy-release', Icon::SIZE_SMALL)->render();
1240 } elseif ($isSel === 'cut') {
1241 $cutIcon = $this->iconFactory->getIcon('actions-edit-cut-release', Icon::SIZE_SMALL)->render();
1242 }
1243
1244 $cells[] = '<a class="btn btn-default" href="' . htmlspecialchars($this->clipObj->selUrlFile(
1245 $fullIdentifier,
1246 1,
1247 $isSel === 'copy'
1248 )) . '" title="' . $copyTitle . '">' . $copyIcon . '</a>';
1249 // we can only cut if file can be moved
1250 if ($fileOrFolderObject->checkActionPermission('move')) {
1251 $cells[] = '<a class="btn btn-default" href="' . htmlspecialchars($this->clipObj->selUrlFile(
1252 $fullIdentifier,
1253 0,
1254 $isSel === 'cut'
1255 )) . '" title="' . $cutTitle . '">' . $cutIcon . '</a>';
1256 } else {
1257 $cells[] = $this->spaceIcon;
1258 }
1259 } else {
1260 // For numeric pads, add select checkboxes:
1261 $n = '_FILE|' . $md5;
1262 $this->CBnames[] = $n;
1263 $checked = $this->clipObj->isSelected('_FILE', $md5) ? ' checked="checked"' : '';
1264 $cells[] = '<input type="hidden" name="CBH[' . $n . ']" value="0" /><label class="btn btn-default btn-checkbox"><input type="checkbox" name="CBC[' . $n . ']" value="' . htmlspecialchars($fullIdentifier) . '" ' . $checked . ' /><span class="t3-icon fa"></span></label>';
1265 }
1266 // Display PASTE button, if directory:
1267 $elFromTable = $this->clipObj->elFromTable('_FILE');
1268 if ($fileOrFolderObject instanceof Folder && !empty($elFromTable) && $fileOrFolderObject->checkActionPermission('write')) {
1269 $addPasteButton = true;
1270 $elToConfirm = [];
1271 foreach ($elFromTable as $key => $element) {
1272 $clipBoardElement = $this->resourceFactory->retrieveFileOrFolderObject($element);
1273 if ($clipBoardElement instanceof Folder && $clipBoardElement->getStorage()->isWithinFolder($clipBoardElement, $fileOrFolderObject)) {
1274 $addPasteButton = false;
1275 }
1276 $elToConfirm[$key] = $clipBoardElement->getName();
1277 }
1278 if ($addPasteButton) {
1279 $cells[] = '<a class="btn btn-default t3js-modal-trigger" '
1280 . ' href="' . htmlspecialchars($this->clipObj->pasteUrl('_FILE', $fullIdentifier)) . '"'
1281 . ' data-content="' . htmlspecialchars($this->clipObj->confirmMsgText('_FILE', $fullName, 'into', $elToConfirm)) . '"'
1282 . ' data-severity="warning"'
1283 . ' data-title="' . htmlspecialchars($this->getLanguageService()->getLL('clip_pasteInto')) . '"'
1284 . ' title="' . htmlspecialchars($this->getLanguageService()->getLL('clip_pasteInto')) . '"'
1285 . '>'
1286 . $this->iconFactory->getIcon('actions-document-paste-into', Icon::SIZE_SMALL)->render()
1287 . '</a>';
1288 }
1289 }
1290 // Compile items into a DIV-element:
1291 return ' <div class="btn-group" role="group">' . implode('', $cells) . '</div>';
1292 }
1293
1294 /**
1295 * Creates the edit control section
1296 *
1297 * @param File|Folder $fileOrFolderObject Array with information about the file/directory for which to make the edit control section for the listing.
1298 * @return string HTML-table
1299 */
1300 public function makeEdit($fileOrFolderObject)
1301 {
1302 $cells = [];
1303 $fullIdentifier = $fileOrFolderObject->getCombinedIdentifier();
1304 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
1305 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
1306
1307 // Edit file content (if editable)
1308 if ($fileOrFolderObject instanceof File && $fileOrFolderObject->checkActionPermission('write') && GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'], $fileOrFolderObject->getExtension())) {
1309 $url = (string)$uriBuilder->buildUriFromRoute('file_edit', ['target' => $fullIdentifier]);
1310 $editOnClick = 'top.list_frame.location.href=' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+encodeURIComponent(top.list_frame.document.location.pathname+top.list_frame.document.location.search);return false;';
1311 $cells['edit'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($editOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.editcontent') . '">'
1312 . $this->iconFactory->getIcon('actions-page-open', Icon::SIZE_SMALL)->render()
1313 . '</a>';
1314 } else {
1315 $cells['edit'] = $this->spaceIcon;
1316 }
1317
1318 // Edit metadata of file
1319 if ($fileOrFolderObject instanceof File && $fileOrFolderObject->checkActionPermission('editMeta') && $this->getBackendUser()->check('tables_modify', 'sys_file_metadata')) {
1320 $metaData = $fileOrFolderObject->getMetaData()->get();
1321 $urlParameters = [
1322 'edit' => [
1323 'sys_file_metadata' => [
1324 $metaData['uid'] => 'edit'
1325 ]
1326 ],
1327 'returnUrl' => $this->listURL()
1328 ];
1329 $url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
1330 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.editMetadata'));
1331 $cells['metadata'] = '<a class="btn btn-default" href="' . htmlspecialchars($url) . '" title="' . $title . '">' . $this->iconFactory->getIcon('actions-open', Icon::SIZE_SMALL)->render() . '</a>';
1332 }
1333
1334 // document view
1335 if ($fileOrFolderObject instanceof File) {
1336 $fileUrl = $fileOrFolderObject->getPublicUrl(true);
1337 if ($fileUrl) {
1338 $cells['view'] = '<a href="' . htmlspecialchars($fileUrl) . '" target="_blank" class="btn btn-default" title="' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.view') . '">' . $this->iconFactory->getIcon('actions-document-view', Icon::SIZE_SMALL)->render() . '</a>';
1339 } else {
1340 $cells['view'] = $this->spaceIcon;
1341 }
1342 } else {
1343 $cells['view'] = $this->spaceIcon;
1344 }
1345
1346 // replace file
1347 if ($fileOrFolderObject instanceof File && $fileOrFolderObject->checkActionPermission('replace')) {
1348 $url = (string)$uriBuilder->buildUriFromRoute('file_replace', ['target' => $fullIdentifier, 'uid' => $fileOrFolderObject->getUid()]);
1349 $replaceOnClick = 'top.list_frame.location.href = ' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+encodeURIComponent(top.list_frame.document.location.pathname+top.list_frame.document.location.search);return false;';
1350 $cells['replace'] = '<a href="#" class="btn btn-default" onclick="' . $replaceOnClick . '" title="' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.replace') . '">' . $this->iconFactory->getIcon('actions-edit-replace', Icon::SIZE_SMALL)->render() . '</a>';
1351 }
1352
1353 // rename the file
1354 if ($fileOrFolderObject->checkActionPermission('rename')) {
1355 $url = (string)$uriBuilder->buildUriFromRoute('file_rename', ['target' => $fullIdentifier]);
1356 $renameOnClick = 'top.list_frame.location.href = ' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+encodeURIComponent(top.list_frame.document.location.pathname+top.list_frame.document.location.search);return false;';
1357 $cells['rename'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($renameOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.rename') . '">' . $this->iconFactory->getIcon('actions-edit-rename', Icon::SIZE_SMALL)->render() . '</a>';
1358 } else {
1359 $cells['rename'] = $this->spaceIcon;
1360 }
1361
1362 // upload files
1363 if ($fileOrFolderObject->getStorage()->checkUserActionPermission('add', 'File') && $fileOrFolderObject->checkActionPermission('write')) {
1364 if ($fileOrFolderObject instanceof Folder) {
1365 $url = (string)$uriBuilder->buildUriFromRoute('file_upload', ['target' => $fullIdentifier]);
1366 $uploadOnClick = 'top.list_frame.location.href = ' . GeneralUtility::quoteJSvalue($url) . '+\'&returnUrl=\'+encodeURIComponent(top.list_frame.document.location.pathname+top.list_frame.document.location.search);return false;';
1367 $cells['upload'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($uploadOnClick) . '" title="' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.upload')) . '">' . $this->iconFactory->getIcon('actions-edit-upload', Icon::SIZE_SMALL)->render() . '</a>';
1368 }
1369 }
1370
1371 if ($fileOrFolderObject->checkActionPermission('read')) {
1372 $infoOnClick = '';
1373 if ($fileOrFolderObject instanceof Folder) {
1374 $infoOnClick = 'top.TYPO3.InfoWindow.showItem(\'_FOLDER\', ' . GeneralUtility::quoteJSvalue($fullIdentifier) . ');return false;';
1375 } elseif ($fileOrFolderObject instanceof File) {
1376 $infoOnClick = 'top.TYPO3.InfoWindow.showItem(\'_FILE\', ' . GeneralUtility::quoteJSvalue($fullIdentifier) . ');return false;';
1377 }
1378 $cells['info'] = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($infoOnClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.info') . '">' . $this->iconFactory->getIcon('actions-document-info', Icon::SIZE_SMALL)->render() . '</a>';
1379 } else {
1380 $cells['info'] = $this->spaceIcon;
1381 }
1382
1383 // delete the file
1384 if ($fileOrFolderObject->checkActionPermission('delete')) {
1385 $identifier = $fileOrFolderObject->getIdentifier();
1386 if ($fileOrFolderObject instanceof Folder) {
1387 $referenceCountText = BackendUtility::referenceCount('_FILE', $identifier, ' ' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.referencesToFolder'));
1388 $deleteType = 'delete_folder';
1389 } else {
1390 $referenceCountText = BackendUtility::referenceCount('sys_file', $fileOrFolderObject->getUid(), ' ' . $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.referencesToFile'));
1391 $deleteType = 'delete_file';
1392 }
1393
1394 if ($this->getBackendUser()->jsConfirmation(JsConfirmation::DELETE)) {
1395 $confirmationCheck = '1';
1396 } else {
1397 $confirmationCheck = '0';
1398 }
1399
1400 $deleteUrl = (string)$uriBuilder->buildUriFromRoute('tce_file');
1401 $confirmationMessage = sprintf($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:mess.delete'), $fileOrFolderObject->getName()) . $referenceCountText;
1402 $title = $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:cm.delete');
1403 $cells['delete'] = '<a href="#" class="btn btn-default t3js-filelist-delete" data-content="' . htmlspecialchars($confirmationMessage)
1404 . '" data-check="' . $confirmationCheck
1405 . '" data-delete-url="' . htmlspecialchars($deleteUrl)
1406 . '" data-title="' . htmlspecialchars($title)
1407 . '" data-identifier="' . htmlspecialchars($fileOrFolderObject->getCombinedIdentifier())
1408 . '" data-delete-type="' . $deleteType
1409 . '" title="' . htmlspecialchars($title) . '">'
1410 . $this->iconFactory->getIcon('actions-edit-delete', Icon::SIZE_SMALL)->render() . '</a>';
1411 } else {
1412 $cells['delete'] = $this->spaceIcon;
1413 }
1414
1415 // Hook for manipulating edit icons.
1416 $cells['__fileOrFolderObject'] = $fileOrFolderObject;
1417 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['fileList']['editIconsHook'] ?? [] as $className) {
1418 $hookObject = GeneralUtility::makeInstance($className);
1419 if (!$hookObject instanceof FileListEditIconHookInterface) {
1420 throw new \UnexpectedValueException(
1421 $className . ' must implement interface ' . FileListEditIconHookInterface::class,
1422 1235225797
1423 );
1424 }
1425 $hookObject->manipulateEditIcons($cells, $this);
1426 }
1427 unset($cells['__fileOrFolderObject']);
1428 // Compile items into a DIV-element:
1429 return '<div class="btn-group">' . implode('', $cells) . '</div>';
1430 }
1431
1432 /**
1433 * Make reference count
1434 *
1435 * @param File|Folder $fileOrFolderObject Array with information about the file/directory for which to make the clipboard panel for the listing.
1436 * @return string HTML
1437 */
1438 public function makeRef($fileOrFolderObject)
1439 {
1440 if ($fileOrFolderObject instanceof FolderInterface) {
1441 return '-';
1442 }
1443 // Look up the file in the sys_refindex.
1444 // Exclude sys_file_metadata records as these are no use references
1445 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
1446 $referenceCount = $queryBuilder->count('*')
1447 ->from('sys_refindex')
1448 ->where(
1449 $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
1450 $queryBuilder->expr()->eq(
1451 'ref_table',
1452 $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
1453 ),
1454 $queryBuilder->expr()->eq(
1455 'ref_uid',
1456 $queryBuilder->createNamedParameter($fileOrFolderObject->getUid(), \PDO::PARAM_INT)
1457 ),
1458 $queryBuilder->expr()->neq(
1459 'tablename',
1460 $queryBuilder->createNamedParameter('sys_file_metadata', \PDO::PARAM_STR)
1461 )
1462 )
1463 ->execute()
1464 ->fetchColumn();
1465
1466 return $this->generateReferenceToolTip($referenceCount, '\'_FILE\', ' . GeneralUtility::quoteJSvalue($fileOrFolderObject->getCombinedIdentifier()));
1467 }
1468
1469 /**
1470 * Returns an instance of LanguageService
1471 *
1472 * @return \TYPO3\CMS\Core\Localization\LanguageService
1473 */
1474 protected function getLanguageService()
1475 {
1476 return $GLOBALS['LANG'];
1477 }
1478
1479 /**
1480 * Returns the current BE user.
1481 *
1482 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
1483 */
1484 protected function getBackendUser()
1485 {
1486 return $GLOBALS['BE_USER'];
1487 }
1488
1489 /**
1490 * Sets the script url depending on being a module or script request
1491 */
1492 protected function determineScriptUrl()
1493 {
1494 if ($routePath = GeneralUtility::_GP('route')) {
1495 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
1496 $this->thisScript = (string)$uriBuilder->buildUriFromRoutePath($routePath);
1497 } else {
1498 $this->thisScript = GeneralUtility::getIndpEnv('SCRIPT_NAME');
1499 }
1500 }
1501
1502 /**
1503 * @return string
1504 */
1505 protected function getThisScript()
1506 {
1507 return strpos($this->thisScript, '?') === false ? $this->thisScript . '?' : $this->thisScript . '&';
1508 }
1509
1510 /**
1511 * Gets an instance of TranslationConfigurationProvider
1512 *
1513 * @return TranslationConfigurationProvider
1514 */
1515 protected function getTranslateTools()
1516 {
1517 if (!isset($this->translateTools)) {
1518 $this->translateTools = GeneralUtility::makeInstance(TranslationConfigurationProvider::class);
1519 }
1520 return $this->translateTools;
1521 }
1522
1523 /**
1524 * Generates HTML code for a Reference tooltip out of
1525 * sys_refindex records you hand over
1526 *
1527 * @param int $references number of records from sys_refindex table
1528 * @param string $launchViewParameter JavaScript String, which will be passed as parameters to top.TYPO3.InfoWindow.showItem
1529 * @return string
1530 */
1531 protected function generateReferenceToolTip($references, $launchViewParameter = '')
1532 {
1533 if (!$references) {
1534 $htmlCode = '-';
1535 } else {
1536 $htmlCode = '<a href="#"';
1537 if ($launchViewParameter !== '') {
1538 $htmlCode .= ' onclick="' . htmlspecialchars(
1539 'top.TYPO3.InfoWindow.showItem(' . $launchViewParameter . '); return false;'
1540 ) . '"';
1541 }
1542 $htmlCode .= ' title="' . htmlspecialchars(
1543 $this->getLanguageService()->sL(
1544 'LLL:EXT:backend/Resources/Private/Language/locallang.xlf:show_references'
1545 ) . ' (' . $references . ')'
1546 ) . '">';
1547 $htmlCode .= $references;
1548 $htmlCode .= '</a>';
1549 }
1550 return $htmlCode;
1551 }
1552 }