‪TYPO3CMS  ‪main
AbstractResourceLinkHandler.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 TYPO3\CMS\Backend\Controller\AbstractLinkBrowserController;
48 
53 {
54  protected ?string ‪$expandFolder = null;
55  protected int ‪$currentPage = 1;
56  protected string ‪$moduleStorageIdentifier = 'file_list';
57 
58  protected ?‪FileList ‪$filelist = null;
59  protected ?string ‪$viewMode = null;
60  protected bool ‪$displayThumbs = true;
61 
62  protected ?‪Folder ‪$selectedFolder = null;
65 
66  protected ‪LinkType ‪$type;
67  protected array ‪$linkParts = [];
68 
71  protected AbstractLinkBrowserController ‪$linkBrowser;
72 
73  public function ‪__construct(
74  protected readonly ‪IconFactory $iconFactory,
75  protected readonly ‪ResourceFactory $resourceFactory,
76  protected readonly ‪PageRenderer $pageRenderer,
77  protected readonly ‪UriBuilder $uriBuilder,
78  protected readonly ‪LanguageServiceFactory $languageServiceFactory
79  ) {
80  $this->languageService = $this->languageServiceFactory->createFromUserPreferences($this->‪getBackendUser());
81  }
82 
83  public function ‪canHandleLink(array ‪$linkParts): bool
84  {
85  if (!‪$linkParts['url']) {
86  return false;
87  }
88  if (isset(‪$linkParts['url'][$this->type->value]) && ‪$linkParts['url'][$this->type->value] instanceof ($this->type->getResourceType())) {
89  $this->linkParts = ‪$linkParts;
90  return true;
91  }
92  return false;
93  }
94 
95  public function ‪formatCurrentUrl(): string
96  {
97  $resource = $this->linkParts['url'][$this->type->value];
98  if (!$resource->checkActionPermission('read')) {
99  return '';
100  }
101  if ($resource->getStorage()->isFallbackStorage()) {
102  return '';
103  }
104  return $this->linkParts['url'][$this->type->value]->getName();
105  }
106 
107  public function ‪createView(‪BackendViewFactory $backendViewFactory, ServerRequestInterface $request): ‪ViewInterface
108  {
109  return $backendViewFactory->‪create($request, ['typo3/cms-filelist']);
110  }
111 
112  public function ‪setView(‪ViewInterface ‪$view): self
113  {
114  $this->view = ‪$view;
115  return $this;
116  }
117 
118  public function ‪getView(): ‪ViewInterface
119  {
120  return ‪$this->view;
121  }
122 
123  public function ‪getLinkAttributes(): array
124  {
125  return ['target', 'title', 'class', 'params', 'rel'];
126  }
127 
128  public function ‪initialize(AbstractLinkBrowserController ‪$linkBrowser, ‪$identifier, array $configuration)
129  {
130  $this->linkBrowser = ‪$linkBrowser;
131  }
132 
133  public function ‪initializeVariables(ServerRequestInterface $request): void
134  {
135  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/viewport/resizable-navigation.js');
136  $this->pageRenderer->loadJavaScriptModule('@typo3/backend/tree/file-storage-browser.js');
137  $this->pageRenderer->loadJavaScriptModule('@typo3/filelist/file-list-actions.js');
138 
139  $this->currentPage = (int)($request->getParsedBody()['currentPage'] ?? $request->getQueryParams()['currentPage'] ?? 1);
140 
141  $this->viewMode = $request->getParsedBody()['viewMode'] ?? $request->getQueryParams()['viewMode'] ?? null;
142  if ($this->viewMode !== null) {
143  $this->‪getBackendUser()->pushModuleData(
144  $this->moduleStorageIdentifier,
145  array_merge($this->‪getBackendUser()->getModuleData($this->moduleStorageIdentifier) ?? [], ['viewMode' => $this->viewMode])
146  );
147  } else {
148  $this->viewMode = $this->‪getBackendUser()->getModuleData($this->moduleStorageIdentifier)['viewMode'] ?? ‪ViewMode::TILES->value;
149  }
150 
151  ‪$displayThumbs = $request->getParsedBody()['displayThumbs'] ?? $request->getQueryParams()['displayThumbs'] ?? null;
152  if (‪$displayThumbs !== null) {
153  $this->displayThumbs = (bool)‪$displayThumbs;
154  $this->‪getBackendUser()->pushModuleData(
155  $this->moduleStorageIdentifier,
156  array_merge($this->‪getBackendUser()->getModuleData($this->moduleStorageIdentifier) ?? [], ['displayThumbs' => $this->displayThumbs])
157  );
158  } else {
159  $this->displayThumbs = (bool)($this->‪getBackendUser()->getModuleData($this->moduleStorageIdentifier)['displayThumbs'] ?? true);
160  }
161 
162  // Selected Folder folder
163  $this->expandFolder = $request->getParsedBody()['expandFolder'] ?? $request->getQueryParams()['expandFolder'] ?? null;
164  if ($this->expandFolder === null) {
165  if (!empty($this->linkParts)) {
166  $resource = $this->linkParts['url'][$this->type->value];
167  if ($resource instanceof ‪File) {
168  $resource = $resource->getParentFolder();
169  }
170  if ($resource instanceof ‪Folder) {
171  $this->expandFolder = $resource->getCombinedIdentifier();
172  if ($this->type === LinkType::FOLDER) {
173  // Select the parent folder of selected folder as entry point.
174  $parentFolder = $resource->getParentFolder();
175  if ($parentFolder instanceof ‪Folder) {
176  $this->expandFolder = $parentFolder->getCombinedIdentifier();
177  }
178  }
179  }
180  }
181  }
182  if ($this->expandFolder) {
183  try {
184  $this->selectedFolder = $this->resourceFactory->getFolderObjectFromCombinedIdentifier($this->expandFolder);
185  } catch (‪FolderDoesNotExistException $e) {
186  }
187  }
188  if ($this->selectedFolder?->checkActionPermission('read') === false) {
189  $this->selectedFolder = null;
190  }
191  if ($this->selectedFolder?->getStorage()?->isFallbackStorage()) {
192  $this->selectedFolder = null;
193  }
194  if (!$this->selectedFolder) {
195  $this->selectedFolder = $this->resourceFactory->getDefaultStorage()?->getRootLevelFolder() ?? null;
196  }
197 
198  $this->filelist = GeneralUtility::makeInstance(FileList::class, $request);
199  $this->filelist->viewMode = ViewMode::tryFrom($this->viewMode) ?? ‪ViewMode::TILES;
200  $this->filelist->thumbs = (‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['thumbnails'] ?? false) && $this->displayThumbs;
201  }
202 
203  public function ‪modifyLinkAttributes(array $fieldDefinitions): array
204  {
205  return $fieldDefinitions;
206  }
207 
208  public function ‪isUpdateSupported(): bool
209  {
210  $resource = $this->linkParts['url'][$this->type->value];
211  if (!$resource->checkActionPermission('read')) {
212  return false;
213  }
214  if ($resource->getStorage()->isFallbackStorage()) {
215  return false;
216  }
217  return true;
218  }
219 
223  public function ‪getBodyTagAttributes(): array
224  {
225  $resource = $this->linkParts['url'][$this->type->value] ?? null;
226  if (!$resource instanceof ($this->type->getResourceType())) {
227  return [];
228  }
229  if (!$resource->checkActionPermission('read')) {
230  return [];
231  }
232  if ($resource->getStorage()->isFallbackStorage()) {
233  return [];
234  }
235  return [
236  'data-linkbrowser-current-link' => GeneralUtility::makeInstance(LinkService::class)->asString([
237  'type' => $this->type->getLinkServiceType(),
238  $this->type->value => $resource,
239  ]),
240  ];
241  }
242 
243  protected function ‪createUri(ServerRequestInterface $request, array $parameters = []): string
244  {
245  return (string)$this->uriBuilder->buildUriFromRequest($request, $this->‪getUrlParameters($parameters));
246  }
247 
248  protected function ‪getViewModeButton(ServerRequestInterface $request): ‪ButtonInterface
249  {
250  $viewModeItems = [];
251  $viewModeItems[] = GeneralUtility::makeInstance(DropDownRadio::class)
252  ->setActive($this->viewMode === ‪ViewMode::TILES->value)
253  ->setHref($this->‪createUri($request, ['viewMode' => ‪ViewMode::TILES->value]))
254  ->setLabel($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.view.tiles'))
255  ->setIcon($this->iconFactory->getIcon('actions-viewmode-tiles'));
256  $viewModeItems[] = GeneralUtility::makeInstance(DropDownRadio::class)
257  ->setActive($this->viewMode === ‪ViewMode::LIST->value)
258  ->setHref($this->‪createUri($request, ['viewMode' => ‪ViewMode::LIST->value]))
259  ->setLabel($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.view.list'))
260  ->setIcon($this->iconFactory->getIcon('actions-viewmode-list'));
261  if (!($this->‪getBackendUser()->getTSConfig()['options.']['noThumbsInEB'] ?? false)) {
262  $viewModeItems[] = GeneralUtility::makeInstance(DropdownDivider::class);
263  $viewModeItems[] = GeneralUtility::makeInstance(DropDownToggle::class)
264  ->setActive($this->displayThumbs)
265  ->setHref($this->‪createUri($request, ['displayThumbs' => $this->displayThumbs ? 0 : 1]))
266  ->setLabel($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.view.showThumbnails'))
267  ->setIcon($this->iconFactory->getIcon('actions-image'));
268  }
269 
270  $viewModeButton = GeneralUtility::makeInstance(DropDownButton::class)
271  ->setLabel($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.view'));
272  foreach ($viewModeItems as $viewModeItem) {
274  $viewModeButton->addItem($viewModeItem);
275  }
276 
277  return $viewModeButton;
278  }
279 
280  public function ‪getUrlParameters(array $values): array
281  {
282  $values = array_replace_recursive([
283  'expandFolder' => $values['identifier'] ?? $this->expandFolder,
284  ], $values);
285 
286  return array_merge($this->linkBrowser->getUrlParameters($values), $values);
287  }
288 
290  {
292  }
293 
295  {
296  return ‪$GLOBALS['BE_USER'];
297  }
298 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Scheduler\LIST
‪@ LIST
Definition: SchedulerManagementAction.php:28
‪TYPO3\CMS\Core\View\ViewInterface
Definition: ViewInterface.php:24
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton
Definition: DropDownButton.php:48
‪TYPO3\CMS\Backend\View\BackendViewFactory
Definition: BackendViewFactory.php:35
‪TYPO3\CMS\Filelist\Matcher\Matcher
Definition: Matcher.php:24
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownToggle
Definition: DropDownToggle.php:39
‪TYPO3\CMS\Filelist\Type\LinkType
‪LinkType
Definition: LinkType.php:28
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownDivider
Definition: DropDownDivider.php:27
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:44
‪TYPO3\CMS\Filelist\FileList
Definition: FileList.php:75
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownRadio
Definition: DropDownRadio.php:54
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownItemInterface
Definition: DropDownItemInterface.php:19
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException
Definition: FolderDoesNotExistException.php:21
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Backend\View\BackendViewFactory\create
‪create(ServerRequestInterface $request, array $packageNames=[])
Definition: BackendViewFactory.php:45
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Filelist\Type\TILES
‪@ TILES
Definition: ViewMode.php:26
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Filelist\Type\ViewMode
‪ViewMode
Definition: ViewMode.php:24
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface
Definition: ButtonInterface.php:22