TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
recordlist/Classes/LinkHandler/FileLinkHandler.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Recordlist\LinkHandler;
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 Psr\Http\Message\ServerRequestInterface;
31 
36 {
42  protected $linkParts = [];
43 
47  protected $expectedClass = File::class;
48 
52  protected $mode = 'file';
53 
57  protected $expandFolder;
58 
68  public function canHandleLink(array $linkParts)
69  {
70  if (!$linkParts['url']) {
71  return false;
72  }
73  if (isset($linkParts['url'][$this->mode]) && $linkParts['url'][$this->mode] instanceof $this->expectedClass) {
74  $this->linkParts = $linkParts;
75  return true;
76  }
77  return false;
78  }
79 
85  public function formatCurrentUrl()
86  {
87  return $this->linkParts['url'][$this->mode]->getName();
88  }
89 
97  public function render(ServerRequestInterface $request)
98  {
99  GeneralUtility::makeInstance(PageRenderer::class)->loadRequireJsModule('TYPO3/CMS/Recordlist/FileLinkHandler');
100 
102  $folderTree = GeneralUtility::makeInstance(ElementBrowserFolderTreeView::class);
103  $folderTree->setLinkParameterProvider($this);
104  $this->view->assign('tree', $folderTree->getBrowsableTree());
105 
106  $this->expandFolder = isset($request->getQueryParams()['expandFolder']) ? $request->getQueryParams()['expandFolder'] : null;
107  if (!empty($this->linkParts) && !isset($this->expandFolder)) {
108  $this->expandFolder = $this->linkParts['url'][$this->mode];
109  if ($this->expandFolder instanceof File) {
110  $this->expandFolder = $this->expandFolder->getParentFolder();
111  }
112  if ($this->expandFolder instanceof Folder) {
113  $this->expandFolder = $this->expandFolder->getCombinedIdentifier();
114  }
115  }
116 
117  // Create upload/create folder forms, if a path is given
118  $selectedFolder = $this->getSelectedFolder($this->expandFolder);
119 
120  // Build the file upload and folder creation form
121  if ($selectedFolder) {
122  $folderUtilityRenderer = GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this);
123  $uploadForm = $this->mode === 'file' ? $folderUtilityRenderer->uploadForm($selectedFolder, []) : '';
124  $createFolder = $folderUtilityRenderer->createFolder($selectedFolder);
125 
126  // Insert the upload form on top, if so configured
127  $positionOfUploadFieldsOnTop = $this->getBackendUser()->getTSConfigVal('options.uploadFieldsInTopOfEB');
128  $this->view->assign('positionOfUploadFields', $positionOfUploadFieldsOnTop ? 'top' : 'bottom');
129  $this->view->assign('uploadFileForm', $uploadForm);
130  $this->view->assign('createFolderForm', $createFolder);
131 
132  // Render the file or folderlist
133  if ($selectedFolder->checkActionPermission('read')) {
134  $this->view->assign('selectedFolder', $selectedFolder);
135  $parameters = $this->linkBrowser->getUrlParameters();
136  $allowedExtensions = isset($parameters['allowedExtensions']) ? $parameters['allowedExtensions'] : '';
137  $this->expandFolder($selectedFolder, $allowedExtensions);
138  }
139  }
140 
141  return $this->view->render(ucfirst($this->mode));
142  }
143 
151  public function expandFolder(Folder $folder, $extensionList = '')
152  {
153  // Create header element; The folder from which files are listed.
154  $folderIcon = $this->iconFactory->getIconForResource($folder, Icon::SIZE_SMALL)->render();
155  $this->view->assign('selectedFolderIcon', $folderIcon);
156  $this->view->assign('selectedFolderTitle', GeneralUtility::fixed_lgd_cs($folder->getIdentifier(), (int)$this->getBackendUser()->uc['titleLen']));
157  if ($this->mode === 'file') {
158  $this->view->assign('currentIdentifier', !empty($this->linkParts) ? $this->linkParts['url']['file']->getUid() : '');
159  } else {
160  $this->view->assign('currentIdentifier', !empty($this->linkParts) ? $this->linkParts['url']['folder']->getCombinedIdentifier() : '');
161  }
162 
163  // Get files from the folder:
164  $fileObjects = $this->getFolderContent($folder, $extensionList);
165  $itemsInSelectedFolder = [];
166  if (!empty($fileObjects)) {
167  foreach ($fileObjects as $fileOrFolderObject) {
168  $itemsInSelectedFolder[] = $this->renderItem($fileOrFolderObject);
169  }
170  }
171  $this->view->assign('itemsInSelectedFolder', $itemsInSelectedFolder);
172  }
173 
180  protected function getFolderContent(Folder $folder, $extensionList)
181  {
182  if ($extensionList !== '') {
184  $filter = GeneralUtility::makeInstance(FileExtensionFilter::class);
185  $filter->setAllowedFileExtensions($extensionList);
186  $folder->setFileAndFolderNameFilters([[$filter, 'filterFileList']]);
187  }
188  return $folder->getFiles();
189  }
190 
199  protected function renderItem(ResourceInterface $fileOrFolderObject)
200  {
201  if (!$fileOrFolderObject instanceof File) {
202  throw new \InvalidArgumentException('Expected File object, got "' . get_class($fileOrFolderObject) . '" object.', 1443651368);
203  }
204  // Get size and icon:
206  $fileOrFolderObject->getSize(),
207  $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_common.xlf:byteSizeUnits')
208  );
209 
210  return [
211  'icon' => $this->iconFactory->getIconForResource($fileOrFolderObject, Icon::SIZE_SMALL)->render(),
212  'uid' => $fileOrFolderObject->getUid(),
213  'size' => $size,
214  'name' => $fileOrFolderObject->getName(),
215  'url' => GeneralUtility::makeInstance(LinkService::class)->asString(['type' => LinkService::TYPE_FILE, 'file' => $fileOrFolderObject]),
216  'title' => GeneralUtility::fixed_lgd_cs($fileOrFolderObject->getName(), (int)$this->getBackendUser()->uc['titleLen'])
217  ];
218  }
219 
223  public function getBodyTagAttributes()
224  {
225  return [
226  'data-current-link' => GeneralUtility::makeInstance(LinkService::class)->asString(['type' => LinkService::TYPE_FILE, 'file' => $this->linkParts['url']['file']])
227  ];
228  }
229 
235  public function getUrlParameters(array $values)
236  {
237  $parameters = [
238  'expandFolder' => $values['identifier'] ?? $this->expandFolder
239  ];
240  return array_merge($this->linkBrowser->getUrlParameters($values), $parameters);
241  }
242 
248  public function isCurrentlySelectedItem(array $values)
249  {
250  return false;
251  }
252 
258  public function getScriptUrl()
259  {
260  return $this->linkBrowser->getScriptUrl();
261  }
262 
269  protected function getSelectedFolder($folderIdentifier = '')
270  {
271  $selectedFolder = false;
272  if ($folderIdentifier) {
273  try {
274  $fileOrFolderObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($folderIdentifier);
275  if ($fileOrFolderObject instanceof Folder) {
276  // It's a folder
277  $selectedFolder = $fileOrFolderObject;
278  } elseif ($fileOrFolderObject instanceof FileInterface) {
279  // It's a file
280  try {
281  $selectedFolder = $fileOrFolderObject->getParentFolder();
282  } catch (\Exception $e) {
283  // Accessing the parent folder failed for some reason. e.g. permissions
284  }
285  }
286  } catch (\Exception $e) {
287  // No path is selected
288  }
289  }
290 
291  // If no folder is selected, get the user's default upload folder
292  if (!$selectedFolder) {
293  try {
294  $selectedFolder = $this->getBackendUser()->getDefaultUploadFolder();
295  } catch (\Exception $e) {
296  // The configured default user folder does not exist
297  }
298  }
299  return $selectedFolder;
300  }
301 }
getFiles($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false, $sort= '', $sortRev=false)
static formatSize($sizeInBytes, $labels= '', $base=0)
static makeInstance($className,...$constructorArguments)