‪TYPO3CMS  10.4
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;
38 
43 {
47  private ‪$moduleTemplate;
48 
52  protected ‪$uriBuilder;
53 
57  protected ‪$view;
58 
63 
68 
72  protected ‪$dashboardRepository;
73 
78 
83 
84  public function ‪__construct(
91  ) {
92  $this->moduleTemplate = ‪$moduleTemplate;
93  $this->uriBuilder = ‪$uriBuilder;
94  $this->dashboardPresetRepository = ‪$dashboardPresetRepository;
95  $this->dashboardRepository = ‪$dashboardRepository;
96  $this->dashboardInitializationService = ‪$dashboardInitializationService;
97 
98  $this->dashboardInitializationService->‪initializeDashboards($this->‪getBackendUser());
99  $this->currentDashboard = $this->dashboardInitializationService->getCurrentDashboard();
100  $this->widgetGroupInitializationService = ‪$widgetGroupInitializationService;
101  }
102 
109  public function ‪mainAction(): void
110  {
111  $this->view->assignMultiple([
112  'availableDashboards' => $this->dashboardInitializationService->getDashboardsForUser(),
113  'dashboardPresets' => $this->dashboardPresetRepository->getDashboardPresets(),
114  'widgetGroups' => $this->widgetGroupInitializationService->buildWidgetGroupsConfiguration(),
115  'currentDashboard' => $this->currentDashboard,
116  'addWidgetUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'addWidget']),
117  'addDashboardUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'addDashboard']),
118  'deleteDashboardUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'deleteDashboard']),
119  'configureDashboardUri' => (string)$this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'configureDashboard']),
120  ]);
121  }
122 
129  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
130  {
131  $pageRenderer = $this->moduleTemplate->getPageRenderer();
132  $this->‪preparePageRenderer($pageRenderer);
133 
134  $action = $request->getQueryParams()['action'] ?? $request->getParsedBody()['action'] ?? 'main';
135  $this->‪initializeView('Dashboard/' . ucfirst($action));
136  $result = $this->{$action . 'Action'}($request);
137  if ($result instanceof ResponseInterface) {
138  return $result;
139  }
140  $this->‪addFrontendResources($pageRenderer);
141  $this->moduleTemplate->setContent($this->view->render());
142  return new ‪HtmlResponse($this->moduleTemplate->renderContent());
143  }
144 
150  public function ‪configureDashboardAction(ServerRequestInterface $request): ResponseInterface
151  {
152  $parameters = $request->getParsedBody();
153  ‪$currentDashboard = $parameters['currentDashboard'] ?? '';
154  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main'], ‪UriBuilder::ABSOLUTE_URL);
155 
156  if (‪$currentDashboard !== '' && isset($parameters['dashboard'])) {
157  $this->dashboardRepository->updateDashboardSettings(‪$currentDashboard, $parameters['dashboard']);
158  }
159 
160  return new ‪RedirectResponse($route);
161  }
162 
168  public function ‪setActiveDashboardAction(ServerRequestInterface $request): ResponseInterface
169  {
170  $this->‪saveCurrentDashboard($request->getQueryParams()['currentDashboard']);
171  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']);
172  return new ‪RedirectResponse($route);
173  }
174 
180  public function ‪addDashboardAction(ServerRequestInterface $request): ResponseInterface
181  {
182  $parameters = $request->getParsedBody();
183  $dashboardIdentifier = $parameters['dashboard'] ?? '';
184 
185  if ($dashboardIdentifier !== '') {
186  $dashboard = $this->dashboardRepository->create($this->dashboardPresetRepository->getDashboardPresets()[$dashboardIdentifier], (int)$this->getBackendUser()->user['uid'], $parameters['dashboard-title']);
187 
188  if ($dashboard instanceof ‪Dashboard) {
189  $this->‪saveCurrentDashboard($dashboard->getIdentifier());
190  }
191  }
192 
193  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']));
194  }
195 
200  public function ‪deleteDashboardAction(): ResponseInterface
201  {
202  $this->dashboardRepository->delete($this->currentDashboard);
203  return new ‪RedirectResponse($this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']));
204  }
205 
211  public function ‪addWidgetAction(ServerRequestInterface $request): ResponseInterface
212  {
213  $parameters = $request->getQueryParams();
214  $widgetKey = $parameters['widget'];
215 
216  if ($widgetKey) {
217  $widgets = $this->currentDashboard->getWidgetConfig();
218  $hash = sha1($widgetKey . '-' . time());
219  $widgets[$hash] = ['identifier' => $widgetKey];
220  $this->dashboardRepository->updateWidgetConfig($this->currentDashboard, $widgets);
221  }
222 
223  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']);
224  return new ‪RedirectResponse($route);
225  }
226 
232  public function ‪removeWidgetAction(ServerRequestInterface $request): ResponseInterface
233  {
234  $parameters = $request->getQueryParams();
235  $widgetHash = $parameters['widgetHash'];
236  $widgets = $this->currentDashboard->getWidgetConfig();
237 
238  if (array_key_exists($widgetHash, $widgets)) {
239  unset($widgets[$widgetHash]);
240  $this->dashboardRepository->updateWidgetConfig($this->currentDashboard, $widgets);
241  }
242  $route = $this->uriBuilder->buildUriFromRoute('dashboard', ['action' => 'main']);
243  return new ‪RedirectResponse($route);
244  }
245 
251  protected function ‪initializeView(string $templateName): void
252  {
253  $this->view = GeneralUtility::makeInstance(StandaloneView::class);
254  $this->view->setTemplate($templateName);
255 
256  $this->view->getRenderingContext()->getTemplatePaths()->fillDefaultsByPackageName('dashboard');
257  $this->moduleTemplate->getDocHeaderComponent()->disable();
258  }
259 
265  protected function ‪addFrontendResources(‪PageRenderer $pageRenderer): void
266  {
267  foreach ($this->dashboardInitializationService->getRequireJsModules() as $requireJsModule) {
268  if (is_array($requireJsModule)) {
269  $pageRenderer->‪loadRequireJsModule($requireJsModule[0], $requireJsModule[1]);
270  } else {
271  $pageRenderer->‪loadRequireJsModule($requireJsModule);
272  }
273  }
274  foreach ($this->dashboardInitializationService->getCssFiles() as $cssFile) {
275  $pageRenderer->‪addCssFile($cssFile);
276  }
277  foreach ($this->dashboardInitializationService->getJsFiles() as $jsFile) {
278  $pageRenderer->‪addJsFile($jsFile);
279  }
280  }
281 
287  protected function ‪preparePageRenderer(PageRenderer $pageRenderer): void
288  {
289  $publicResourcesPath =
291 
292  $pageRenderer->addRequireJsConfiguration(
293  [
294  'paths' => [
295  'muuri' => $publicResourcesPath . 'JavaScript/Contrib/muuri',
296  'web-animate' => $publicResourcesPath . 'JavaScript/Contrib/web-animate',
297  ],
298  ]
299  );
300 
301  $pageRenderer->loadRequireJsModule('muuri');
302  $pageRenderer->loadRequireJsModule('web-animate');
303  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/Grid');
304  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/WidgetContentCollector');
305  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/WidgetSelector');
306  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/WidgetRemover');
307  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/DashboardModal');
308  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Dashboard/DashboardDelete');
309  $pageRenderer->addCssFile($publicResourcesPath . 'Css/dashboard.css');
310  }
311 }
‪TYPO3\CMS\Dashboard\DashboardInitializationService\initializeDashboards
‪initializeDashboards(BackendUserAuthentication $user)
Definition: DashboardInitializationService.php:64
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Dashboard\Controller\DashboardController\handleRequest
‪ResponseInterface handleRequest(ServerRequestInterface $request)
Definition: DashboardController.php:121
‪TYPO3\CMS\Core\Page\PageRenderer\addRequireJsConfiguration
‪addRequireJsConfiguration(array $configuration)
Definition: PageRenderer.php:1404
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$moduleTemplate
‪ModuleTemplate $moduleTemplate
Definition: DashboardController.php:46
‪TYPO3\CMS\Dashboard\Controller\DashboardController\mainAction
‪mainAction()
Definition: DashboardController.php:101
‪TYPO3\CMS\Dashboard\DashboardInitializationService
Definition: DashboardInitializationService.php:31
‪TYPO3\CMS\Dashboard\DashboardRepository
Definition: DashboardRepository.php:31
‪TYPO3\CMS\Dashboard\Controller\DashboardController\__construct
‪__construct(ModuleTemplate $moduleTemplate, UriBuilder $uriBuilder, DashboardPresetRegistry $dashboardPresetRepository, DashboardRepository $dashboardRepository, DashboardInitializationService $dashboardInitializationService, WidgetGroupInitializationService $widgetGroupInitializationService)
Definition: DashboardController.php:76
‪TYPO3\CMS\Dashboard\Controller
Definition: AbstractController.php:18
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$dashboardRepository
‪DashboardRepository $dashboardRepository
Definition: DashboardController.php:66
‪TYPO3\CMS\Dashboard\Dashboard
Definition: Dashboard.php:29
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$widgetGroupInitializationService
‪WidgetGroupInitializationService $widgetGroupInitializationService
Definition: DashboardController.php:74
‪TYPO3\CMS\Dashboard\Controller\DashboardController\preparePageRenderer
‪preparePageRenderer(PageRenderer $pageRenderer)
Definition: DashboardController.php:279
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:43
‪TYPO3\CMS\Core\Page\PageRenderer\loadRequireJsModule
‪loadRequireJsModule($mainModuleName, $callBackFunction=null)
Definition: PageRenderer.php:1493
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:43
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:42
‪TYPO3\CMS\Dashboard\Controller\DashboardController\setActiveDashboardAction
‪ResponseInterface setActiveDashboardAction(ServerRequestInterface $request)
Definition: DashboardController.php:160
‪TYPO3\CMS\Core\Page\PageRenderer\addCssFile
‪addCssFile($file, $rel='stylesheet', $media='all', $title='', $compress=true, $forceOnTop=false, $allWrap='', $excludeFromConcatenation=false, $splitChar='|', $inline=false)
Definition: PageRenderer.php:1201
‪TYPO3\CMS\Dashboard\Controller\DashboardController\addWidgetAction
‪ResponseInterface addWidgetAction(ServerRequestInterface $request)
Definition: DashboardController.php:203
‪TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
Definition: RouteNotFoundException.php:22
‪TYPO3\CMS\Dashboard\Controller\DashboardController\addDashboardAction
‪ResponseInterface addDashboardAction(ServerRequestInterface $request)
Definition: DashboardController.php:172
‪TYPO3\CMS\Dashboard\Controller\DashboardController
Definition: DashboardController.php:43
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$uriBuilder
‪UriBuilder $uriBuilder
Definition: DashboardController.php:50
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$dashboardInitializationService
‪DashboardInitializationService $dashboardInitializationService
Definition: DashboardController.php:70
‪TYPO3\CMS\Dashboard\Controller\DashboardController\removeWidgetAction
‪ResponseInterface removeWidgetAction(ServerRequestInterface $request)
Definition: DashboardController.php:224
‪TYPO3\CMS\Dashboard\Controller\AbstractController\saveCurrentDashboard
‪saveCurrentDashboard(string $identifier)
Definition: AbstractController.php:45
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface
Definition: ViewInterface.php:24
‪TYPO3\CMS\Dashboard\Controller\DashboardController\deleteDashboardAction
‪ResponseInterface deleteDashboardAction()
Definition: DashboardController.php:192
‪TYPO3\CMS\Core\Http\RedirectResponse
Definition: RedirectResponse.php:28
‪TYPO3\CMS\Dashboard\Controller\DashboardController\configureDashboardAction
‪ResponseInterface configureDashboardAction(ServerRequestInterface $request)
Definition: DashboardController.php:142
‪TYPO3\CMS\Dashboard\WidgetGroupInitializationService
Definition: WidgetGroupInitializationService.php:26
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$dashboardPresetRepository
‪DashboardPresetRegistry $dashboardPresetRepository
Definition: DashboardController.php:62
‪TYPO3\CMS\Dashboard\Controller\DashboardController\initializeView
‪initializeView(string $templateName)
Definition: DashboardController.php:243
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:34
‪TYPO3\CMS\Dashboard\Controller\AbstractController
Definition: AbstractController.php:27
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$view
‪ViewInterface $view
Definition: DashboardController.php:54
‪TYPO3\CMS\Dashboard\Controller\DashboardController\addFrontendResources
‪addFrontendResources(PageRenderer $pageRenderer)
Definition: DashboardController.php:257
‪TYPO3\CMS\Core\Page\PageRenderer\addJsFile
‪addJsFile($file, $type='text/javascript', $compress=true, $forceOnTop=false, $allWrap='', $excludeFromConcatenation=false, $splitChar='|', $async=false, $integrity='', $defer=false, $crossorigin='', $nomodule=false)
Definition: PageRenderer.php:1089
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\extPath
‪static string extPath($key, $script='')
Definition: ExtensionManagementUtility.php:127
‪TYPO3\CMS\Dashboard\Controller\DashboardController\$currentDashboard
‪Dashboard $currentDashboard
Definition: DashboardController.php:58
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Routing\UriBuilder\ABSOLUTE_URL
‪const ABSOLUTE_URL
Definition: UriBuilder.php:42
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:43
‪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