‪TYPO3CMS  ‪main
WidgetAjaxController.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;
29 
33 #[AsController]
35 {
36  public function ‪__construct(
37  protected readonly ‪DashboardRepository $dashboardRepository,
38  protected readonly ‪WidgetRegistry $widgetRegistry,
39  ) {}
40 
41  public function ‪getContent(ServerRequestInterface $request): ResponseInterface
42  {
43  $queryParams = $request->getQueryParams();
44  $widget = (string)($queryParams['widget'] ?? '');
45  try {
46  $widgetObject = $this->widgetRegistry->getAvailableWidget($request, $widget);
47  } catch (\InvalidArgumentException $e) {
48  return new ‪JsonResponse(['error' => 'Widget is not available!']);
49  }
50  if (!$widgetObject instanceof ‪WidgetInterface) {
51  return new ‪JsonResponse(['error' => 'Widget doesn\'t have a valid widget class']);
52  }
53 
54  $data = [
55  'widget' => $widget,
56  'content' => $widgetObject->renderWidgetContent(),
57  'eventdata' => ($widgetObject instanceof ‪EventDataInterface) ? $widgetObject->getEventData() : [],
58  ];
59  return new ‪JsonResponse($data);
60  }
61 
65  public function ‪savePositions(ServerRequestInterface $request): ResponseInterface
66  {
67  $backendUser = $this->‪getBackendUser();
68  $body = $request->getParsedBody();
69  $widgets = [];
70  foreach ($body['widgets'] ?? [] as $widget) {
71  if (!is_string($widget[0] ?? null) || !is_string($widget[1] ?? null)) {
72  continue;
73  }
74  $widgets[$widget[1]] = ['identifier' => $widget[0]];
75  }
76  $currentDashboard = $this->dashboardRepository->getDashboardByIdentifier($backendUser->getModuleData('dashboard/current_dashboard/') ?? '');
77  if ($currentDashboard !== null) {
78  $this->dashboardRepository->updateWidgetConfig($currentDashboard, $widgets);
79  }
80  return new ‪JsonResponse(['status' => 'saved']);
81  }
82 
84  {
85  return ‪$GLOBALS['BE_USER'];
86  }
87 }
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController\getContent
‪getContent(ServerRequestInterface $request)
Definition: WidgetAjaxController.php:41
‪TYPO3\CMS\Dashboard\DashboardRepository
Definition: DashboardRepository.php:30
‪TYPO3\CMS\Dashboard\Controller
Definition: DashboardController.php:18
‪TYPO3\CMS\Dashboard\Widgets\WidgetInterface
Definition: WidgetInterface.php:26
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController\savePositions
‪savePositions(ServerRequestInterface $request)
Definition: WidgetAjaxController.php:65
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController\getBackendUser
‪getBackendUser()
Definition: WidgetAjaxController.php:83
‪TYPO3\CMS\Dashboard\WidgetRegistry
Definition: WidgetRegistry.php:33
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController\__construct
‪__construct(protected readonly DashboardRepository $dashboardRepository, protected readonly WidgetRegistry $widgetRegistry,)
Definition: WidgetAjaxController.php:36
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController
Definition: WidgetAjaxController.php:35
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Dashboard\Widgets\EventDataInterface
Definition: EventDataInterface.php:24