‪TYPO3CMS  ‪main
DashboardController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
36 
40 #[AsController]
42 {
44 
45  public function ‪__construct(
46  protected readonly ‪PageRenderer $pageRenderer,
47  protected readonly ‪UriBuilder $uriBuilder,
48  protected readonly ‪DashboardPresetRegistry $dashboardPresetRepository,
49  protected readonly ‪DashboardRepository $dashboardRepository,
50  protected readonly ‪DashboardInitializationService $dashboardInitializationService,
51  protected readonly ‪WidgetGroupInitializationService $widgetGroupInitializationService,
52  protected readonly ‪ModuleTemplateFactory $moduleTemplateFactory,
53  ) {}
54 
58  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
59  {
60  $this->dashboardInitializationService->initializeDashboards($request, $this->‪getBackendUser());
61  $this->currentDashboard = $this->dashboardInitializationService->getCurrentDashboard();
62  $action = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'] ?? 'main';
63  return $this->{$action . 'Action'}($request);
64  }
65 
69  protected function ‪mainAction(ServerRequestInterface $request): ResponseInterface
70  {
71  $view = $this->moduleTemplateFactory->create($request);
72  $this->‪preparePageRenderer();
73  $this->‪addFrontendResources();
74  $view->setTitle(
75  $this->‪getLanguageService()->sL('LLL:EXT:dashboard/Resources/Private/Language/locallang_mod.xlf:mlang_tabs_tab'),
76  $this->currentDashboard->getTitle()
77  );
78  $view->assignMultiple([
79  'availableDashboards' => $this->dashboardInitializationService->getDashboardsForUser(),
80  'dashboardPresets' => $this->dashboardPresetRepository->getDashboardPresets(),
81  'widgetGroups' => $this->widgetGroupInitializationService->buildWidgetGroupsConfiguration(),
82  'currentDashboard' => $this->currentDashboard,
83  'addWidgetUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'addWidget']),
84  'addDashboardUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'addDashboard']),
85  'deleteDashboardUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'deleteDashboard']),
86  'configureDashboardUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'configureDashboard']),
87  ]);
88  return $view->renderResponse('Dashboard/Main');
89  }
90 
91  protected function ‪configureDashboardAction(ServerRequestInterface $request): ResponseInterface
92  {
93  $parameters = $request->getParsedBody();
94  ‪$currentDashboard = $parameters['currentDashboard'] ?? '';
95  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main'], ‪UriBuilder::ABSOLUTE_URL);
96  if (‪$currentDashboard !== '' && isset($parameters['dashboard'])) {
97  $this->dashboardRepository->updateDashboardSettings(‪$currentDashboard, $parameters['dashboard']);
98  }
99  return new ‪RedirectResponse($route);
100  }
101 
102  protected function ‪setActiveDashboardAction(ServerRequestInterface $request): ResponseInterface
103  {
104  $this->‪saveCurrentDashboard((string)($request->getQueryParams()['currentDashboard'] ?? ''));
105  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']);
106  return new ‪RedirectResponse($route);
107  }
108 
109  protected function ‪addDashboardAction(ServerRequestInterface $request): ResponseInterface
110  {
111  $parameters = $request->getParsedBody();
112  $dashboardIdentifier = (string)($parameters['dashboard'] ?? '');
113  $dashboardPreset = $this->dashboardPresetRepository->getDashboardPresets()[$dashboardIdentifier] ?? null;
114  if ($dashboardPreset instanceof ‪DashboardPreset) {
115  $dashboard = $this->dashboardRepository->create(
116  $dashboardPreset,
117  (int)$this->‪getBackendUser()->user['uid'],
118  $parameters['dashboard-title'] ?? ''
119  );
120  if ($dashboard !== null) {
121  $this->‪saveCurrentDashboard($dashboard->getIdentifier());
122  }
123  }
124  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']));
125  }
126 
127  protected function ‪deleteDashboardAction(): ResponseInterface
128  {
129  $this->dashboardRepository->delete($this->currentDashboard);
130  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']));
131  }
132 
133  protected function ‪addWidgetAction(ServerRequestInterface $request): ResponseInterface
134  {
135  $widgetKey = (string)($request->getQueryParams()['widget'] ?? '');
136  if ($widgetKey === '') {
137  throw new ‪RequiredArgumentMissingException('Argument "widget" not set.', 1624436360);
138  }
139  $widgets = $this->currentDashboard->getWidgetConfig();
140  $hash = sha1($widgetKey . '-' . time());
141  $widgets[$hash] = ['identifier' => $widgetKey];
142  $this->dashboardRepository->updateWidgetConfig($this->currentDashboard, $widgets);
143  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']);
144  return new ‪RedirectResponse($route);
145  }
146 
147  protected function ‪removeWidgetAction(ServerRequestInterface $request): ResponseInterface
148  {
149  $parameters = $request->getQueryParams();
150  $widgetHash = $parameters['widgetHash'] ?? '';
151  $widgets = $this->currentDashboard->getWidgetConfig();
152  if ($widgetHash !== '' && array_key_exists($widgetHash, $widgets)) {
153  unset($widgets[$widgetHash]);
154  $this->dashboardRepository->updateWidgetConfig($this->currentDashboard, $widgets);
155  }
156  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']);
157  return new ‪RedirectResponse($route);
158  }
159 
163  protected function ‪addFrontendResources(): void
164  {
165  $javaScriptRenderer = $this->pageRenderer->getJavaScriptRenderer();
166  foreach ($this->dashboardInitializationService->getJavaScriptModuleInstructions() as $instruction) {
167  $javaScriptRenderer->addJavaScriptModuleInstruction($instruction);
168  }
169  foreach ($this->dashboardInitializationService->getCssFiles() as $cssFile) {
170  $this->pageRenderer->addCssFile($cssFile);
171  }
172  foreach ($this->dashboardInitializationService->getJsFiles() as $jsFile) {
173  $this->pageRenderer->addJsFile($jsFile);
174  }
175  }
176 
180  protected function ‪preparePageRenderer(): void
181  {
182  $this->pageRenderer->loadJavaScriptModule('muuri');
183  $this->pageRenderer->loadJavaScriptModule('@typo3/dashboard/grid.js');
184  $this->pageRenderer->loadJavaScriptModule('@typo3/dashboard/widget-content-collector.js');
185  $this->pageRenderer->loadJavaScriptModule('@typo3/dashboard/widget-selector.js');
186  $this->pageRenderer->loadJavaScriptModule('@typo3/dashboard/widget-refresh.js');
187  $this->pageRenderer->loadJavaScriptModule('@typo3/dashboard/widget-remover.js');
188  $this->pageRenderer->loadJavaScriptModule('@typo3/dashboard/dashboard-modal.js');
189  $this->pageRenderer->loadJavaScriptModule('@typo3/dashboard/dashboard-delete.js');
190  $this->pageRenderer->addCssFile('EXT:dashboard/Resources/Public/Css/dashboard.css');
191  }
192 
193  protected function ‪saveCurrentDashboard(string ‪$identifier): void
194  {
195  $this->‪getBackendUser()->pushModuleData('dashboard/current_dashboard/', $identifier);
196  }
197 
199  {
200  return ‪$GLOBALS['BE_USER'];
201  }
202 
204  {
205  return ‪$GLOBALS['LANG'];
206  }
207 }
‪TYPO3\CMS\Dashboard\DashboardPreset
Definition: DashboardPreset.php:26
‪TYPO3\CMS\Dashboard\Controller\DashboardController\__construct
‪__construct(protected readonly PageRenderer $pageRenderer, protected readonly UriBuilder $uriBuilder, protected readonly DashboardPresetRegistry $dashboardPresetRepository, protected readonly DashboardRepository $dashboardRepository, protected readonly DashboardInitializationService $dashboardInitializationService, protected readonly WidgetGroupInitializationService $widgetGroupInitializationService, protected readonly ModuleTemplateFactory $moduleTemplateFactory,)
Definition: DashboardController.php:45
‪TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException
Definition: RequiredArgumentMissingException.php:25
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:33
‪TYPO3\CMS\Dashboard\DashboardInitializationService
Definition: DashboardInitializationService.php:34
‪TYPO3\CMS\Dashboard\DashboardRepository
Definition: DashboardRepository.php:30
‪TYPO3\CMS\Dashboard\Controller
Definition: DashboardController.php:18
‪TYPO3\CMS\Dashboard\Controller\DashboardController\saveCurrentDashboard
‪saveCurrentDashboard(string $identifier)
Definition: DashboardController.php:193
‪TYPO3\CMS\Dashboard\Controller\DashboardController\deleteDashboardAction
‪deleteDashboardAction()
Definition: DashboardController.php:127
‪TYPO3\CMS\Dashboard\Dashboard
Definition: Dashboard.php:29
‪TYPO3\CMS\Dashboard\Controller\DashboardController\addFrontendResources
‪addFrontendResources()
Definition: DashboardController.php:163
‪TYPO3\CMS\Dashboard\Controller\DashboardController\preparePageRenderer
‪preparePageRenderer()
Definition: DashboardController.php:180
‪TYPO3\CMS\Dashboard\Controller\DashboardController\getLanguageService
‪getLanguageService()
Definition: DashboardController.php:203
‪TYPO3\CMS\Dashboard\Controller\DashboardController\setActiveDashboardAction
‪setActiveDashboardAction(ServerRequestInterface $request)
Definition: DashboardController.php:102
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:44
‪TYPO3\CMS\Dashboard\Controller\DashboardController\mainAction
‪mainAction(ServerRequestInterface $request)
Definition: DashboardController.php:69
‪TYPO3\CMS\Dashboard\Controller\DashboardController\addDashboardAction
‪addDashboardAction(ServerRequestInterface $request)
Definition: DashboardController.php:109
‪TYPO3\CMS\Dashboard\Controller\DashboardController\addWidgetAction
‪addWidgetAction(ServerRequestInterface $request)
Definition: DashboardController.php:133
‪TYPO3\CMS\Dashboard\Controller\DashboardController
Definition: DashboardController.php:42
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:44
‪TYPO3\CMS\Dashboard\Controller\DashboardController\getBackendUser
‪getBackendUser()
Definition: DashboardController.php:198
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:30
‪TYPO3\CMS\Dashboard\WidgetGroupInitializationService
Definition: WidgetGroupInitializationService.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Dashboard\Controller\DashboardController\handleRequest
‪handleRequest(ServerRequestInterface $request)
Definition: DashboardController.php:58
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$currentDashboard
‪Dashboard $currentDashboard
Definition: DashboardController.php:43
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Dashboard\Controller\DashboardController\configureDashboardAction
‪configureDashboardAction(ServerRequestInterface $request)
Definition: DashboardController.php:91
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: UriBuilder.php:48
‪TYPO3\CMS\Dashboard\DashboardPresetRegistry
Definition: DashboardPresetRegistry.php:26
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Dashboard\Controller\DashboardController\removeWidgetAction
‪removeWidgetAction(ServerRequestInterface $request)
Definition: DashboardController.php:147