‪TYPO3CMS  10.4
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;
28 
33 {
37  protected ‪$currentDashboard;
38 
42  protected ‪$dashboardRepository;
43 
47  protected ‪$widgetRegistry;
48 
50  {
51  $this->dashboardRepository = ‪$dashboardRepository;
52  $this->widgetRegistry = ‪$widgetRegistry;
53 
54  $this->currentDashboard = $this->dashboardRepository->getDashboardByIdentifier($this->‪loadCurrentDashboard());
55  }
56 
62  public function ‪getContent(ServerRequestInterface $request): ResponseInterface
63  {
64  $queryParams = $request->getQueryParams();
65 
66  try {
67  $widgetObject = $this->widgetRegistry->getAvailableWidget((string)$queryParams['widget']);
68  } catch (\InvalidArgumentException $e) {
69  return new ‪JsonResponse(['error' => 'Widget is not available!']);
70  }
71 
72  if (!$widgetObject instanceof ‪WidgetInterface) {
73  return new ‪JsonResponse(['error' => 'Widget doesn\'t have a valid widget class']);
74  }
75 
76  $data = [
77  'widget' => $queryParams['widget'],
78  'content' => $widgetObject->renderWidgetContent(),
79  'eventdata' => $widgetObject instanceof ‪EventDataInterface ? $widgetObject->‪getEventData() : [],
80  ];
81 
82  return new ‪JsonResponse($data);
83  }
84 
91  public function ‪savePositions(ServerRequestInterface $request): ResponseInterface
92  {
93  $body = $request->getParsedBody();
94  $widgets = [];
95  foreach ($body['widgets'] as $widget) {
96  $widgets[$widget[1]] = ['identifier' => $widget[0]];
97  }
98 
99  if ($this->currentDashboard !== null) {
100  $this->dashboardRepository->updateWidgetConfig($this->currentDashboard, $widgets);
101  }
102  return new ‪JsonResponse(['status' => 'saved']);
103  }
104 }
‪TYPO3\CMS\Dashboard\Controller\AbstractController\loadCurrentDashboard
‪loadCurrentDashboard()
Definition: AbstractController.php:40
‪TYPO3\CMS\Dashboard\DashboardRepository
Definition: DashboardRepository.php:31
‪TYPO3\CMS\Dashboard\Widgets\EventDataInterface\getEventData
‪array getEventData()
‪TYPO3\CMS\Dashboard\Controller
Definition: AbstractController.php:18
‪TYPO3\CMS\Dashboard\Widgets\WidgetInterface
Definition: WidgetInterface.php:26
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController\__construct
‪__construct(DashboardRepository $dashboardRepository, WidgetRegistry $widgetRegistry)
Definition: WidgetAjaxController.php:46
‪TYPO3\CMS\Dashboard\Dashboard
Definition: Dashboard.php:29
‪TYPO3\CMS\Dashboard\WidgetRegistry
Definition: WidgetRegistry.php:31
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController\$widgetRegistry
‪WidgetRegistry $widgetRegistry
Definition: WidgetAjaxController.php:44
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController\savePositions
‪ResponseInterface savePositions(ServerRequestInterface $request)
Definition: WidgetAjaxController.php:88
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController\$currentDashboard
‪Dashboard $currentDashboard
Definition: WidgetAjaxController.php:36
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController\getContent
‪ResponseInterface getContent(ServerRequestInterface $request)
Definition: WidgetAjaxController.php:59
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController\$dashboardRepository
‪DashboardRepository $dashboardRepository
Definition: WidgetAjaxController.php:40
‪TYPO3\CMS\Dashboard\Controller\AbstractController
Definition: AbstractController.php:27
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:26
‪TYPO3\CMS\Dashboard\Controller\WidgetAjaxController
Definition: WidgetAjaxController.php:33
‪TYPO3\CMS\Dashboard\Widgets\EventDataInterface
Definition: EventDataInterface.php:24