‪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;
19 use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
33 
37 #[Autoconfigure(public: true, shared: false)]
39 {
40  protected ‪LinkType ‪$type = LinkType::FILE;
41 
42  public function ‪initializeVariables(ServerRequestInterface $request): void
43  {
44  parent::initializeVariables($request);
45  $this->pageRenderer->loadJavaScriptModule('@typo3/filelist/linkbrowser-file-handler.js');
46 
47  $this->resourceDisplayMatcher = GeneralUtility::makeInstance(Matcher::class);
48  $this->resourceDisplayMatcher->addMatcher(GeneralUtility::makeInstance(ResourceFolderTypeMatcher::class));
49 
50  // @todo Deprecate "allowedExtensions", see LinkPopup for further information
51  $allowedExtensions = ‪GeneralUtility::trimExplode(',', (string)($this->linkBrowser->getParameters()['params']['allowedExtensions'] ?? ''), true);
52  $allowedFileExtensions = ‪GeneralUtility::trimExplode(',', (string)($this->linkBrowser->getParameters()['params']['allowedFileExtensions'] ?? ''), true);
53  $allowedFileExtensions = array_unique(array_merge($allowedExtensions, $allowedFileExtensions));
54  if (count($allowedFileExtensions) >= 1) {
55  $fileExtensionMatcher = GeneralUtility::makeInstance(ResourceFileExtensionMatcher::class);
56  $fileExtensionMatcher->setExtensions($allowedFileExtensions);
57  } else {
58  $fileExtensionMatcher = GeneralUtility::makeInstance(ResourceFileTypeMatcher::class);
59  }
60  $this->resourceDisplayMatcher->addMatcher($fileExtensionMatcher);
61  }
62 
63  public function ‪render(ServerRequestInterface $request): string
64  {
65  $contentHtml = '';
66  if ($this->selectedFolder !== null) {
67  // Create the filelist
68  $this->filelist->start(
69  $this->selectedFolder,
70  ‪MathUtility::forceIntegerInRange($this->currentPage, 1, 100000),
71  $request->getQueryParams()['sort'] ?? '',
72  ($request->getQueryParams()['reverse'] ?? '') === '1',
73  Mode::BROWSE
74  );
75  $this->filelist->setResourceDisplayMatcher($this->resourceDisplayMatcher);
76  $this->filelist->setResourceSelectableMatcher($this->resourceSelectableMatcher);
77 
78  $searchWord = trim((string)($request->getParsedBody()['searchTerm'] ?? $request->getQueryParams()['searchTerm'] ?? ''));
79  $searchDemand = $searchWord !== '' ? ‪FileSearchDemand::createForSearchTerm($searchWord)->withFolder($this->selectedFolder)->withRecursive() : null;
80 
81  $resource = $this->linkParts['url']['file'] ?? null;
82  if ($resource instanceof ‪ResourceInterface) {
83  $resourceSelectedMatcher = GeneralUtility::makeInstance(Matcher::class);
84  $resourceMatcher = GeneralUtility::makeInstance(ResourceMatcher::class);
85  $resourceMatcher->addResource($resource);
86  $resourceSelectedMatcher->addMatcher($resourceMatcher);
87  $this->filelist->setResourceSelectedMatcher($resourceSelectedMatcher);
88  }
89 
90  $markup = [];
91 
92  // Render the filelist search box
93  $markup[] = '<div class="mb-4">';
94  $markup[] = GeneralUtility::makeInstance(RecordSearchBoxComponent::class)
95  ->setSearchWord($searchWord)
96  ->render($request, $this->filelist->createModuleUri($this->getUrlParameters([])));
97  $markup[] = '</div>';
98 
99  // Render the filelist header bar
100  $markup[] = '<div class="row justify-content-between mb-2">';
101  $markup[] = ' <div class="col-auto"></div>';
102  $markup[] = ' <div class="col-auto">';
103  $markup[] = ' ' . $this->‪getViewModeButton($request);
104  $markup[] = ' </div>';
105  $markup[] = '</div>';
106 
107  // Render the filelist
108  $markup[] = $this->filelist->render($searchDemand, $this->view);
109 
110  // Render the file upload and folder creation form
111  $folderUtilityRenderer = GeneralUtility::makeInstance(FolderUtilityRenderer::class, $this);
112  $markup[] = $folderUtilityRenderer->uploadForm($request, $this->selectedFolder);
113  $markup[] = $folderUtilityRenderer->createFolder($request, $this->selectedFolder);
114 
115  $contentHtml = implode(PHP_EOL, $markup);
116  }
117 
118  $this->view->assign('selectedFolder', $this->selectedFolder);
119  $this->view->assign('content', $contentHtml);
120  $this->view->assign('contentOnly', (bool)($request->getQueryParams()['contentOnly'] ?? false));
121  $this->view->assign('treeActions', ($this->type === LinkType::FOLDER) ? ['link'] : []);
122  $this->view->assign('currentIdentifier', !empty($this->linkParts) ? $this->linkParts['url']['file']->getUid() : '');
123 
124  return $this->view->render('LinkHandler/File');
125  }
126 }
‪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:30
‪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