‪TYPO3CMS  ‪main
DashboardInitializationService.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 
18 namespace ‪TYPO3\CMS\Dashboard;
19 
20 use Psr\Http\Message\ServerRequestInterface;
29 
34 {
35  protected const ‪MODULE_DATA_CURRENT_DASHBOARD_IDENTIFIER = 'dashboard/current_dashboard/';
36 
39 
43  protected array ‪$javaScriptModuleInstructions = [];
44 
45  private array ‪$jsFiles = [];
46  private array ‪$cssFiles = [];
47 
48  public function ‪__construct(
49  private readonly ‪DashboardRepository $dashboardRepository,
50  private readonly ‪DashboardPresetRegistry $dashboardPresetRegistry
51  ) {}
52 
53  public function ‪initializeDashboards(ServerRequestInterface $request, ‪BackendUserAuthentication ‪$user): void
54  {
55  $this->user = ‪$user;
56  $this->currentDashboard = $this->‪defineCurrentDashboard();
57  $this->currentDashboard->initializeWidgets($request);
58  $this->‪defineResourcesOfWidgets($this->currentDashboard->getWidgets());
59  }
60 
62  {
64  }
65 
67  {
68  ‪$currentDashboard = $this->dashboardRepository->getDashboardByIdentifier($this->‪loadCurrentDashboard($this->user));
69  if (‪$currentDashboard === null) {
70  $dashboards = $this->‪getDashboardsForUser();
72  $currentDashboard = reset($dashboards);
74  }
75 
76  return ‪$currentDashboard;
77  }
78 
79  protected function ‪createDefaultDashboards(): array
80  {
81  $dashboardsForUser = [];
82 
83  $userConfig = $this->user->getTSConfig();
84  $dashboardsToCreate = ‪GeneralUtility::trimExplode(
85  ',',
86  $userConfig['options.']['dashboard.']['dashboardPresetsForNewUsers'] ?? 'default'
87  );
88 
89  foreach ($this->dashboardPresetRegistry->getDashboardPresets() as $dashboardPreset) {
90  if (in_array($dashboardPreset->getIdentifier(), $dashboardsToCreate, true)) {
91  $dashboard = $this->dashboardRepository->create(
92  $dashboardPreset,
93  (int)$this->user->user['uid']
94  );
95 
96  if ($dashboard === null) {
97  continue;
98  }
99  $dashboardsForUser[$dashboard->getIdentifier()] = $dashboard;
100  }
101  }
102 
103  return $dashboardsForUser;
104  }
105 
109  public function ‪getDashboardsForUser(): array
110  {
111  $dashboards = [];
112  foreach ($this->dashboardRepository->getDashboardsForUser((int)$this->user->user['uid']) as $dashboard) {
113  $dashboards[$dashboard->getIdentifier()] = $dashboard;
114  }
115 
116  if ($dashboards === []) {
117  $dashboards = $this->‪createDefaultDashboards();
118  }
119 
120  return $dashboards;
121  }
122 
126  protected function ‪defineResourcesOfWidgets(array $widgets): void
127  {
128  foreach ($widgets as $widget) {
129  $concreteInstance = GeneralUtility::makeInstance($widget->getServiceName());
130  if ($concreteInstance instanceof ‪JavaScriptInterface) {
131  $this->‪defineJavaScriptInstructions($concreteInstance);
132  }
133  if ($concreteInstance instanceof ‪AdditionalCssInterface) {
134  $this->‪defineCssFiles($concreteInstance);
135  }
136  if ($concreteInstance instanceof ‪AdditionalJavaScriptInterface) {
137  $this->‪defineJsFiles($concreteInstance);
138  }
139  }
140  }
141 
142  protected function ‪defineJavaScriptInstructions(‪JavaScriptInterface $widgetInstance): void
143  {
144  foreach ($widgetInstance->‪getJavaScriptModuleInstructions() as $instruction) {
145  $this->javaScriptModuleInstructions[] = $instruction;
146  }
147  }
148 
153  protected function ‪defineJsFiles(‪AdditionalJavaScriptInterface $widgetInstance): void
154  {
155  foreach ($widgetInstance->‪getJsFiles() as $jsFile) {
156  if (‪PathUtility::isExtensionPath($jsFile)) {
157  $jsFile = ‪PathUtility::getPublicResourceWebPath($jsFile);
158  }
159  $this->jsFiles[$jsFile] = $jsFile;
160  }
161  }
162 
167  protected function ‪defineCssFiles(‪AdditionalCssInterface $widgetInstance): void
168  {
169  foreach ($widgetInstance->‪getCssFiles() as $cssFile) {
170  if (‪PathUtility::isExtensionPath($cssFile)) {
171  $cssFile = ‪PathUtility::getPublicResourceWebPath($cssFile);
172  }
173  $this->cssFiles[$cssFile] = $cssFile;
174  }
175  }
176 
178  {
179  return ‪$user->‪getModuleData(self::MODULE_DATA_CURRENT_DASHBOARD_IDENTIFIER) ?? '';
180  }
181 
183  {
184  ‪$user->‪pushModuleData(self::MODULE_DATA_CURRENT_DASHBOARD_IDENTIFIER, ‪$identifier);
185  }
186 
190  public function ‪getJavaScriptModuleInstructions(): array
191  {
193  }
194 
195  public function ‪getJsFiles(): array
196  {
197  return ‪$this->jsFiles;
198  }
199 
200  public function ‪getCssFiles(): array
201  {
202  return ‪$this->cssFiles;
203  }
204 }
‪TYPO3\CMS\Core\Utility\PathUtility\isExtensionPath
‪static isExtensionPath(string $path)
Definition: PathUtility.php:117
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Dashboard\Dashboard\getIdentifier
‪getIdentifier()
Definition: Dashboard.php:49
‪TYPO3\CMS\Dashboard\Widgets\AdditionalCssInterface
Definition: AdditionalCssInterface.php:24
‪TYPO3\CMS\Dashboard\DashboardInitializationService\defineJavaScriptInstructions
‪defineJavaScriptInstructions(JavaScriptInterface $widgetInstance)
Definition: DashboardInitializationService.php:142
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$currentDashboard
‪Dashboard $currentDashboard
Definition: DashboardInitializationService.php:37
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\getModuleData
‪mixed getModuleData(string $module, string $type='')
Definition: AbstractUserAuthentication.php:980
‪TYPO3\CMS\Dashboard\DashboardInitializationService\getDashboardsForUser
‪Dashboard[] getDashboardsForUser()
Definition: DashboardInitializationService.php:109
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$javaScriptModuleInstructions
‪array $javaScriptModuleInstructions
Definition: DashboardInitializationService.php:43
‪TYPO3\CMS\Dashboard\Widgets\AdditionalJavaScriptInterface
Definition: AdditionalJavaScriptInterface.php:24
‪TYPO3\CMS\Dashboard\DashboardInitializationService\defineCssFiles
‪defineCssFiles(AdditionalCssInterface $widgetInstance)
Definition: DashboardInitializationService.php:167
‪TYPO3\CMS\Dashboard\DashboardInitializationService\createDefaultDashboards
‪createDefaultDashboards()
Definition: DashboardInitializationService.php:79
‪TYPO3\CMS\Dashboard\DashboardInitializationService
Definition: DashboardInitializationService.php:34
‪TYPO3\CMS\Dashboard\DashboardRepository
Definition: DashboardRepository.php:30
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\pushModuleData
‪pushModuleData(string $module, mixed $data, bool $dontPersistImmediately=false)
Definition: AbstractUserAuthentication.php:960
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Dashboard\Dashboard
Definition: Dashboard.php:29
‪TYPO3\CMS\Dashboard\DashboardInitializationService\MODULE_DATA_CURRENT_DASHBOARD_IDENTIFIER
‪const MODULE_DATA_CURRENT_DASHBOARD_IDENTIFIER
Definition: DashboardInitializationService.php:35
‪TYPO3\CMS\Dashboard\DashboardInitializationService\defineResourcesOfWidgets
‪defineResourcesOfWidgets(array $widgets)
Definition: DashboardInitializationService.php:126
‪TYPO3\CMS\Dashboard\DashboardInitializationService\getCurrentDashboard
‪getCurrentDashboard()
Definition: DashboardInitializationService.php:61
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$user
‪BackendUserAuthentication $user
Definition: DashboardInitializationService.php:38
‪TYPO3\CMS\Dashboard\DashboardInitializationService\__construct
‪__construct(private readonly DashboardRepository $dashboardRepository, private readonly DashboardPresetRegistry $dashboardPresetRegistry)
Definition: DashboardInitializationService.php:48
‪TYPO3\CMS\Dashboard\Widgets\AdditionalJavaScriptInterface\getJsFiles
‪getJsFiles()
‪TYPO3\CMS\Core\Utility\PathUtility\getPublicResourceWebPath
‪static getPublicResourceWebPath(string $resourcePath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:97
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$jsFiles
‪array $jsFiles
Definition: DashboardInitializationService.php:45
‪TYPO3\CMS\Dashboard\DashboardInitializationService\loadCurrentDashboard
‪loadCurrentDashboard(BackendUserAuthentication $user)
Definition: DashboardInitializationService.php:177
‪TYPO3\CMS\Dashboard\Widgets\AdditionalCssInterface\getCssFiles
‪getCssFiles()
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Dashboard\DashboardInitializationService\defineJsFiles
‪defineJsFiles(AdditionalJavaScriptInterface $widgetInstance)
Definition: DashboardInitializationService.php:153
‪TYPO3\CMS\Dashboard\DashboardInitializationService\getCssFiles
‪getCssFiles()
Definition: DashboardInitializationService.php:200
‪TYPO3\CMS\Dashboard\DashboardInitializationService\saveCurrentDashboard
‪saveCurrentDashboard(BackendUserAuthentication $user, string $identifier)
Definition: DashboardInitializationService.php:182
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$cssFiles
‪array $cssFiles
Definition: DashboardInitializationService.php:46
‪TYPO3\CMS\Dashboard\Widgets\JavaScriptInterface
Definition: JavaScriptInterface.php:26
‪TYPO3\CMS\Dashboard\Widgets\JavaScriptInterface\getJavaScriptModuleInstructions
‪list< JavaScriptModuleInstruction > getJavaScriptModuleInstructions()
‪TYPO3\CMS\Dashboard\DashboardInitializationService\defineCurrentDashboard
‪defineCurrentDashboard()
Definition: DashboardInitializationService.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Dashboard\DashboardInitializationService\getJsFiles
‪getJsFiles()
Definition: DashboardInitializationService.php:195
‪TYPO3\CMS\Dashboard\Widgets\WidgetConfigurationInterface
Definition: WidgetConfigurationInterface.php:26
‪TYPO3\CMS\Dashboard
‪TYPO3\CMS\Dashboard\DashboardPresetRegistry
Definition: DashboardPresetRegistry.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Dashboard\DashboardInitializationService\getJavaScriptModuleInstructions
‪list< JavaScriptModuleInstruction > getJavaScriptModuleInstructions()
Definition: DashboardInitializationService.php:190
‪TYPO3\CMS\Dashboard\DashboardInitializationService\initializeDashboards
‪initializeDashboards(ServerRequestInterface $request, BackendUserAuthentication $user)
Definition: DashboardInitializationService.php:53