‪TYPO3CMS  ‪main
FileLinkHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Http\Message\ServerRequestInterface;
32 
37 {
38  protected ‪LinkType ‪$type = LinkType::FILE;
39 
40  public function ‪initializeVariables(ServerRequestInterface $request): void
41  {
42  parent::initializeVariables($request);
43  $this->pageRenderer->loadJavaScriptModule('@typo3/filelist/linkbrowser-file-handler.js');
44 
45  $this->resourceDisplayMatcher = GeneralUtility::makeInstance(Matcher::class);
46  $this->resourceDisplayMatcher->addMatcher(GeneralUtility::makeInstance(ResourceFolderTypeMatcher::class));
47 
48  // @todo Deprecate "allowedExtensions", see LinkPopup for further information
49  $allowedExtensions = ‪GeneralUtility::trimExplode(',', (string)($this->linkBrowser->getParameters()['params']['allowedExtensions'] ?? ''), true);
50  $allowedFileExtensions = ‪GeneralUtility::trimExplode(',', (string)($this->linkBrowser->getParameters()['params']['allowedFileExtensions'] ?? ''), true);
51  $allowedFileExtensions = array_unique(array_merge($allowedExtensions, $allowedFileExtensions));
52  if (count($allowedFileExtensions) >= 1) {
53  $fileExtensionMatcher = GeneralUtility::makeInstance(ResourceFileExtensionMatcher::class);
54  $fileExtensionMatcher->setExtensions($allowedFileExtensions);
55  } else {
56  $fileExtensionMatcher = GeneralUtility::makeInstance(ResourceFileTypeMatcher::class);
57  }
58  $this->resourceDisplayMatcher->addMatcher($fileExtensionMatcher);
59  }
60 
61  public function ‪render(ServerRequestInterface $request): string
62  {
63  $contentHtml = '';
64  if ($this->selectedFolder !== null) {
65  // Create the filelist
66  $this->filelist->start(
67  $this->selectedFolder,
68  ‪MathUtility::forceIntegerInRange($this->currentPage, 1, 100000),
69  $request->getQueryParams()['sort'] ?? '',
70  ($request->getQueryParams()['reverse'] ?? '') === '1',
71  Mode::BROWSE
72  );
73  $this->filelist->setResourceDisplayMatcher($this->resourceDisplayMatcher);
74  $this->filelist->setResourceSelectableMatcher($this->resourceSelectableMatcher);
75 
76  $searchWord = trim((string)($request->getParsedBody()['searchTerm'] ?? $request->getQueryParams()['searchTerm'] ?? ''));
77  $searchDemand = $searchWord !== '' ? ‪FileSearchDemand::createForSearchTerm($searchWord)->withFolder($this->selectedFolder)->withRecursive() : null;
78 
79  $resource = $this->linkParts['url']['file'] ?? null;
80  if ($resource instanceof ‪ResourceInterface) {
81  $resourceSelectedMatcher = GeneralUtility::makeInstance(Matcher::class);
82  $resourceMatcher = GeneralUtility::makeInstance(ResourceMatcher::class);
83  $resourceMatcher->addResource($resource);
84  $resourceSelectedMatcher->addMatcher($resourceMatcher);
85  $this->filelist->setResourceSelectedMatcher($resourceSelectedMatcher);
86  }
87 
88  $markup = [];
89 
90  // Render the filelist search box
91  $markup[] = '<div class="mb-4">';
92  $markup[] = GeneralUtility::makeInstance(RecordSearchBoxComponent::class)
93  ->setSearchWord($searchWord)
94  ->render($request, $this->filelist->createModuleUri($this->getUrlParameters([])));
95  $markup[] = '</div>';
96 
97  // Render the filelist header bar
98  $markup[] = '<div class="row justify-content-between mb-2">';
99  $markup[] = ' <div class="col-auto"></div>';
100  $markup[] = ' <div class="col-auto">';
101  $markup[] = ' ' . $this->‪getViewModeButton($request);
102  $markup[] = ' </div>';
103  $markup[] = '</div>';
104 
105  // Render the filelist
106  $markup[] = $this->filelist->render($searchDemand, $this->view);
107 
108  // Render the file upload and folder creation form
109  $folderUtilityRenderer = GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this);
110  $markup[] = $folderUtilityRenderer->uploadForm($request, $this->selectedFolder);
111  $markup[] = $folderUtilityRenderer->createFolder($request, $this->selectedFolder);
112 
113  $contentHtml = implode(PHP_EOL, $markup);
114  }
115 
116  $this->view->assign('selectedFolder', $this->selectedFolder);
117  $this->view->assign('content', $contentHtml);
118  $this->view->assign('contentOnly', (bool)($request->getQueryParams()['contentOnly'] ?? false));
119  $this->view->assign('treeActions', ($this->type === LinkType::FOLDER) ? ['link'] : []);
120  $this->view->assign('currentIdentifier', !empty($this->linkParts) ? $this->linkParts['url']['file']->getUid() : '');
121 
122  return $this->view->render('LinkHandler/File');
123  }
124 }
‪TYPO3\CMS\Filelist\Matcher\ResourceMatcher
Definition: ResourceMatcher.php:26
‪TYPO3\CMS\Filelist\Matcher\ResourceFolderTypeMatcher
Definition: ResourceFolderTypeMatcher.php:27
‪TYPO3\CMS\Filelist\Matcher\ResourceFileExtensionMatcher
Definition: ResourceFileExtensionMatcher.php:26
‪TYPO3\CMS\Filelist\Matcher\Matcher
Definition: Matcher.php:24
‪TYPO3\CMS\Filelist\Type\LinkType
‪LinkType
Definition: LinkType.php:28
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand
Definition: FileSearchDemand.php:26
‪TYPO3\CMS\Filelist\Type\Mode
‪Mode
Definition: Mode.php:24
‪TYPO3\CMS\Backend\View\RecordSearchBoxComponent
Definition: RecordSearchBoxComponent.php:28
‪TYPO3\CMS\Backend\View\FolderUtilityRenderer
Definition: FolderUtilityRenderer.php:36
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Resource\ResourceInterface
Definition: ResourceInterface.php:21
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand\createForSearchTerm
‪static createForSearchTerm(string $searchTerm)
Definition: FileSearchDemand.php:70
‪TYPO3\CMS\Filelist\Matcher\ResourceFileTypeMatcher
Definition: ResourceFileTypeMatcher.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822