‪TYPO3CMS  11.5
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;
22 use ‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException as RouteNotFoundExceptionAlias;
29 use TYPO3\CMS\Core\Page\PageRenderer;
33 use TYPO3\CMS\Dashboard\DashboardInitializationService;
40 
45 {
46  protected PageRenderer ‪$pageRenderer;
51  protected DashboardInitializationService ‪$dashboardInitializationService;
54 
57 
58  public function ‪__construct(
59  PageRenderer ‪$pageRenderer,
63  DashboardInitializationService ‪$dashboardInitializationService,
66  ) {
67  $this->pageRenderer = ‪$pageRenderer;
68  $this->uriBuilder = ‪$uriBuilder;
69  $this->dashboardPresetRepository = ‪$dashboardPresetRepository;
70  $this->dashboardRepository = ‪$dashboardRepository;
71  $this->dashboardInitializationService = ‪$dashboardInitializationService;
72 
73  $this->dashboardInitializationService->initializeDashboards($this->‪getBackendUser());
74  $this->currentDashboard = $this->dashboardInitializationService->getCurrentDashboard();
75  $this->widgetGroupInitializationService = ‪$widgetGroupInitializationService;
76 
77  $this->moduleTemplateFactory = ‪$moduleTemplateFactory;
78 
79  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
80  }
81 
88  protected function ‪mainAction(): void
89  {
90  $this->moduleTemplate->setTitle(
91  $this->‪getLanguageService()->sL('LLL:EXT:dashboard/Resources/Private/Language/locallang_mod.xlf:mlang_tabs_tab'),
92  $this->currentDashboard->getTitle()
93  );
94 
95  $this->view->assignMultiple([
96  'availableDashboards' => $this->dashboardInitializationService->getDashboardsForUser(),
97  'dashboardPresets' => $this->dashboardPresetRepository->getDashboardPresets(),
98  'widgetGroups' => $this->widgetGroupInitializationService->buildWidgetGroupsConfiguration(),
99  'currentDashboard' => $this->currentDashboard,
100  'addWidgetUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'addWidget']),
101  'addDashboardUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'addDashboard']),
102  'deleteDashboardUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'deleteDashboard']),
103  'configureDashboardUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'configureDashboard']),
104  ]);
105  }
106 
113  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
114  {
115  $this->moduleTemplate = $this->moduleTemplateFactory->create($request);
116  $this->‪preparePageRenderer();
117 
118  $action = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'] ?? 'main';
119  $this->‪initializeView('Dashboard/' . ucfirst($action));
120  $result = $this->{$action . 'Action'}($request);
121  if ($result instanceof ResponseInterface) {
122  return $result;
123  }
124  $this->‪addFrontendResources();
125  $this->moduleTemplate->setContent($this->view->render());
126  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
127  }
128 
134  protected function ‪configureDashboardAction(ServerRequestInterface $request): ResponseInterface
135  {
136  $parameters = $request->getParsedBody();
137  ‪$currentDashboard = $parameters['currentDashboard'] ?? '';
138  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main'], ‪UriBuilder::ABSOLUTE_URL);
139 
140  if (‪$currentDashboard !== '' && isset($parameters['dashboard'])) {
141  $this->dashboardRepository->updateDashboardSettings(‪$currentDashboard, $parameters['dashboard']);
142  }
143 
144  return new ‪RedirectResponse($route);
145  }
146 
152  protected function ‪setActiveDashboardAction(ServerRequestInterface $request): ResponseInterface
153  {
154  $this->‪saveCurrentDashboard((string)($request->getQueryParams()['currentDashboard'] ?? ''));
155  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']);
156  return new ‪RedirectResponse($route);
157  }
158 
164  protected function ‪addDashboardAction(ServerRequestInterface $request): ResponseInterface
165  {
166  $parameters = $request->getParsedBody();
167  $dashboardIdentifier = (string)($parameters['dashboard'] ?? '');
168 
169  $dashboardPreset = $this->dashboardPresetRepository->getDashboardPresets()[$dashboardIdentifier] ?? null;
170  if ($dashboardPreset instanceof ‪DashboardPreset) {
171  $dashboard = $this->dashboardRepository->create(
172  $dashboardPreset,
173  (int)$this->‪getBackendUser()->user['uid'],
174  $parameters['dashboard-title'] ?? ''
175  );
176  if ($dashboard instanceof ‪Dashboard) {
177  $this->‪saveCurrentDashboard($dashboard->getIdentifier());
178  }
179  }
180 
181  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']));
182  }
183 
188  protected function ‪deleteDashboardAction(): ResponseInterface
189  {
190  $this->dashboardRepository->delete($this->currentDashboard);
191  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']));
192  }
193 
200  protected function ‪addWidgetAction(ServerRequestInterface $request): ResponseInterface
201  {
202  $widgetKey = (string)($request->getQueryParams()['widget'] ?? '');
203 
204  if ($widgetKey === '') {
205  throw new ‪RequiredArgumentMissingException('Argument "widget" not set.', 1624436360);
206  }
207 
208  $widgets = $this->currentDashboard->getWidgetConfig();
209  $hash = sha1($widgetKey . '-' . time());
210  $widgets[$hash] = ['identifier' => $widgetKey];
211  $this->dashboardRepository->updateWidgetConfig($this->currentDashboard, $widgets);
212 
213  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']);
214  return new ‪RedirectResponse($route);
215  }
216 
222  protected function ‪removeWidgetAction(ServerRequestInterface $request): ResponseInterface
223  {
224  $parameters = $request->getQueryParams();
225  $widgetHash = $parameters['widgetHash'] ?? '';
226  $widgets = $this->currentDashboard->getWidgetConfig();
227 
228  if ($widgetHash !== '' && array_key_exists($widgetHash, $widgets)) {
229  unset($widgets[$widgetHash]);
230  $this->dashboardRepository->updateWidgetConfig($this->currentDashboard, $widgets);
231  }
232  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']);
233  return new ‪RedirectResponse($route);
234  }
235 
241  protected function ‪initializeView(string $templateName): void
242  {
243  $this->view->setTemplate($templateName);
244  $this->view->getRenderingContext()->getTemplatePaths()->fillDefaultsByPackageName('dashboard');
245  $this->moduleTemplate->getDocHeaderComponent()->disable();
246  }
247 
251  protected function ‪addFrontendResources(): void
252  {
253  $javaScriptRenderer = $this->pageRenderer->getJavaScriptRenderer();
254  foreach ($this->dashboardInitializationService->getJavaScriptModuleInstructions() as $instruction) {
255  $javaScriptRenderer->addJavaScriptModuleInstruction($instruction);
256  }
257  // @todo Deprecate `RequireJsModuleInterface` in TYPO3 v12.0
258  foreach ($this->dashboardInitializationService->getRequireJsModules() as $requireJsModule) {
259  if (is_array($requireJsModule)) {
260  $this->pageRenderer->loadRequireJsModule($requireJsModule[0], $requireJsModule[1]);
261  } else {
262  $javaScriptRenderer->addJavaScriptModuleInstruction(
264  );
265  }
266  }
267  foreach ($this->dashboardInitializationService->getCssFiles() as $cssFile) {
268  $this->pageRenderer->addCssFile($cssFile);
269  }
270  foreach ($this->dashboardInitializationService->getJsFiles() as $jsFile) {
271  $this->pageRenderer->addJsFile($jsFile);
272  }
273  }
274 
278  protected function ‪preparePageRenderer(): void
279  {
280  $publicResourcesPath = ‪PathUtility::getPublicResourceWebPath('EXT:dashboard/Resources/Public/');
281  $this->pageRenderer->addRequireJsConfiguration(
282  [
283  'paths' => [
284  'muuri' => $publicResourcesPath . 'JavaScript/Contrib/muuri',
285  'web-animate' => $publicResourcesPath . 'JavaScript/Contrib/web-animate',
286  ],
287  ]
288  );
289 
290  $this->pageRenderer->loadRequireJsModule('muuri');
291  $this->pageRenderer->loadRequireJsModule('web-animate');
292  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/Grid');
293  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/WidgetContentCollector');
294  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/WidgetSelector');
295  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/WidgetRefresh');
296  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/WidgetRemover');
297  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/DashboardModal');
298  $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/DashboardDelete');
299  $this->pageRenderer->addCssFile('EXT:dashboard/Resources/Public/Css/dashboard.css');
300  }
301 }
‪TYPO3\CMS\Dashboard\DashboardPreset
Definition: DashboardPreset.php:26
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$moduleTemplateFactory
‪ModuleTemplateFactory $moduleTemplateFactory
Definition: DashboardController.php:53
‪TYPO3\CMS\Dashboard\Controller\DashboardController\handleRequest
‪ResponseInterface handleRequest(ServerRequestInterface $request)
Definition: DashboardController.php:113
‪TYPO3\CMS\Extbase\Mvc\Controller\Exception\RequiredArgumentMissingException
Definition: RequiredArgumentMissingException.php:25
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: DashboardController.php:55
‪TYPO3\CMS\Backend\Template\ModuleTemplateFactory
Definition: ModuleTemplateFactory.php:29
‪TYPO3\CMS\Dashboard\Controller\DashboardController\__construct
‪__construct(PageRenderer $pageRenderer, UriBuilder $uriBuilder, DashboardPresetRegistry $dashboardPresetRepository, DashboardRepository $dashboardRepository, DashboardInitializationService $dashboardInitializationService, WidgetGroupInitializationService $widgetGroupInitializationService, ModuleTemplateFactory $moduleTemplateFactory)
Definition: DashboardController.php:58
‪TYPO3\CMS\Dashboard\Controller\DashboardController\mainAction
‪mainAction()
Definition: DashboardController.php:88
‪TYPO3\CMS\Dashboard\DashboardRepository
Definition: DashboardRepository.php:30
‪TYPO3\CMS\Dashboard\Controller
Definition: AbstractController.php:18
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static string getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:98
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$dashboardRepository
‪DashboardRepository $dashboardRepository
Definition: DashboardController.php:50
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Dashboard\Dashboard
Definition: Dashboard.php:28
‪TYPO3\CMS\Dashboard\Controller\DashboardController\addFrontendResources
‪addFrontendResources()
Definition: DashboardController.php:251
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$widgetGroupInitializationService
‪WidgetGroupInitializationService $widgetGroupInitializationService
Definition: DashboardController.php:52
‪TYPO3\CMS\Dashboard\Controller\DashboardController\preparePageRenderer
‪preparePageRenderer()
Definition: DashboardController.php:278
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\forRequireJS
‪static self forRequireJS(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:49
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$view
‪StandaloneView $view
Definition: DashboardController.php:56
‪TYPO3\CMS\Dashboard\Controller\DashboardController\setActiveDashboardAction
‪ResponseInterface setActiveDashboardAction(ServerRequestInterface $request)
Definition: DashboardController.php:152
‪TYPO3\CMS\Dashboard\Controller\DashboardController\addWidgetAction
‪ResponseInterface addWidgetAction(ServerRequestInterface $request)
Definition: DashboardController.php:200
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:21
‪TYPO3\CMS\Dashboard\Controller\DashboardController\addDashboardAction
‪ResponseInterface addDashboardAction(ServerRequestInterface $request)
Definition: DashboardController.php:164
‪TYPO3\CMS\Dashboard\Controller\DashboardController
Definition: DashboardController.php:45
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$uriBuilder
‪UriBuilder $uriBuilder
Definition: DashboardController.php:47
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$dashboardInitializationService
‪DashboardInitializationService $dashboardInitializationService
Definition: DashboardController.php:51
‪TYPO3\CMS\Dashboard\Controller\DashboardController\removeWidgetAction
‪ResponseInterface removeWidgetAction(ServerRequestInterface $request)
Definition: DashboardController.php:222
‪TYPO3\CMS\Dashboard\Controller\AbstractController\saveCurrentDashboard
‪saveCurrentDashboard(string $identifier)
Definition: AbstractController.php:45
‪TYPO3\CMS\Dashboard\Controller\DashboardController\deleteDashboardAction
‪ResponseInterface deleteDashboardAction()
Definition: DashboardController.php:188
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪TYPO3\CMS\Dashboard\Controller\DashboardController\configureDashboardAction
‪ResponseInterface configureDashboardAction(ServerRequestInterface $request)
Definition: DashboardController.php:134
‪TYPO3\CMS\Dashboard\WidgetGroupInitializationService
Definition: WidgetGroupInitializationService.php:26
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$dashboardPresetRepository
‪DashboardPresetRegistry $dashboardPresetRepository
Definition: DashboardController.php:49
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$pageRenderer
‪PageRenderer $pageRenderer
Definition: DashboardController.php:46
‪TYPO3\CMS\Dashboard\Controller\DashboardController\initializeView
‪initializeView(string $templateName)
Definition: DashboardController.php:241
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪TYPO3\CMS\Dashboard\Controller\AbstractController
Definition: AbstractController.php:27
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$currentDashboard
‪Dashboard $currentDashboard
Definition: DashboardController.php:48
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: UriBuilder.php:44
‪TYPO3\CMS\Dashboard\Controller\AbstractController\getLanguageService
‪getLanguageService()
Definition: AbstractController.php:35
‪TYPO3\CMS\Dashboard\DashboardPresetRegistry
Definition: DashboardPresetRegistry.php:26
‪TYPO3\CMS\Core\Http\HtmlResponse
Definition: HtmlResponse.php:26
‪TYPO3\CMS\Dashboard\Controller\AbstractController\getBackendUser
‪getBackendUser()
Definition: AbstractController.php:30