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