‪TYPO3CMS  10.4
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 
26 
31 {
32  protected const ‪MODULE_DATA_CURRENT_DASHBOARD_IDENTIFIER = 'dashboard/current_dashboard/';
33 
38 
43 
47  private ‪$currentDashboard;
48 
52  private ‪$user;
53 
57  private ‪$requireJsModules = [];
58  private ‪$jsFiles = [];
59  private ‪$cssFiles = [];
60 
61  public function ‪__construct(
64  ) {
65  $this->dashboardRepository = ‪$dashboardRepository;
66  $this->dashboardPresetRegistry = ‪$dashboardPresetRegistry;
67  }
68 
70  {
71  $this->user = ‪$user;
72  $this->currentDashboard = $this->‪defineCurrentDashboard();
73 
74  $this->currentDashboard->initializeWidgets();
75  $this->‪defineResourcesOfWidgets($this->currentDashboard->getWidgets());
76  }
77 
79  {
81  }
82 
83  protected function ‪defineCurrentDashboard(): ‪Dashboard
84  {
85  ‪$currentDashboard = $this->dashboardRepository->getDashboardByIdentifier($this->‪loadCurrentDashboard($this->user));
86  if (!‪$currentDashboard instanceof ‪Dashboard) {
87  $dashboards = $this->‪getDashboardsForUser();
88  ‪$currentDashboard = reset($dashboards);
90  }
91 
92  return ‪$currentDashboard;
93  }
94 
95  protected function ‪createDefaultDashboards(): array
96  {
97  $dashboardsForUser = [];
98 
99  $userConfig = $this->user->getTSConfig();
100  $dashboardsToCreate = ‪GeneralUtility::trimExplode(
101  ',',
102  $userConfig['options.']['dashboard.']['dashboardPresetsForNewUsers'] ?? 'default'
103  );
104 
106  foreach ($this->dashboardPresetRegistry->getDashboardPresets() as $dashboardPreset) {
107  if (in_array($dashboardPreset->getIdentifier(), $dashboardsToCreate, true)) {
108  $dashboard = $this->dashboardRepository->create(
109  $dashboardPreset,
110  (int)$this->user->user['uid']
111  );
112 
113  if ($dashboard instanceof Dashboard) {
114  $dashboardsForUser[$dashboard->getIdentifier()] = $dashboard;
115  }
116  }
117  }
118 
119  return $dashboardsForUser;
120  }
121 
125  public function ‪getDashboardsForUser(): array
126  {
127  $dashboards = [];
128  foreach ($this->dashboardRepository->getDashboardsForUser((int)$this->user->user['uid']) as $dashboard) {
129  $dashboards[$dashboard->getIdentifier()] = $dashboard;
130  }
131 
132  if ($dashboards === []) {
133  $dashboards = $this->‪createDefaultDashboards();
134  }
135 
136  return $dashboards;
137  }
138 
142  protected function ‪defineResourcesOfWidgets(array $widgets): void
143  {
144  foreach ($widgets as $widget) {
145  $concreteInstance = GeneralUtility::makeInstance($widget->getServiceName());
146  if ($concreteInstance instanceof ‪RequireJsModuleInterface) {
147  $this->‪defineRequireJsModules($concreteInstance);
148  }
149  if ($concreteInstance instanceof ‪AdditionalCssInterface) {
150  $this->‪defineCssFiles($concreteInstance);
151  }
152  if ($concreteInstance instanceof ‪AdditionalJavaScriptInterface) {
153  $this->‪defineJsFiles($concreteInstance);
154  }
155  }
156  }
157 
163  protected function ‪defineRequireJsModules(RequireJsModuleInterface $widgetInstance): void
164  {
165  foreach ($widgetInstance->getRequireJsModules() as $moduleNameOrIndex => $callbackOrModuleName) {
166  if (is_string($moduleNameOrIndex)) {
167  $this->requireJsModules[] = [$moduleNameOrIndex, $callbackOrModuleName];
168  } else {
169  $this->requireJsModules[] = $callbackOrModuleName;
170  }
171  }
172  }
173 
180  protected function ‪defineJsFiles(AdditionalJavaScriptInterface $widgetInstance): void
181  {
182  foreach ($widgetInstance->getJsFiles() as $jsFile) {
183  if (strpos($jsFile, 'EXT:') === 0) {
184  $jsFile = ‪PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($jsFile));
185  }
186  $this->jsFiles[$jsFile] = $jsFile;
187  }
188  }
189 
196  protected function ‪defineCssFiles(AdditionalCssInterface $widgetInstance): void
197  {
198  foreach ($widgetInstance->getCssFiles() as $cssFile) {
199  if (strpos($cssFile, 'EXT:') === 0) {
200  $cssFile = ‪PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($cssFile));
201  }
202  $this->cssFiles[$cssFile] = $cssFile;
203  }
204  }
205 
207  {
208  return ‪$user->‪getModuleData(self::MODULE_DATA_CURRENT_DASHBOARD_IDENTIFIER) ?? '';
209  }
210 
211  protected function ‪saveCurrentDashboard(‪BackendUserAuthentication ‪$user, string $identifier): void
212  {
213  ‪$user->‪pushModuleData(self::MODULE_DATA_CURRENT_DASHBOARD_IDENTIFIER, $identifier);
214  }
215 
219  public function ‪getRequireJsModules(): array
220  {
222  }
223 
227  public function ‪getJsFiles(): array
228  {
229  return ‪$this->jsFiles;
230  }
231 
235  public function ‪getCssFiles(): array
236  {
237  return ‪$this->cssFiles;
238  }
239 }
‪TYPO3\CMS\Dashboard\DashboardInitializationService\initializeDashboards
‪initializeDashboards(BackendUserAuthentication $user)
Definition: DashboardInitializationService.php:64
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Dashboard\Widgets\AdditionalCssInterface
Definition: AdditionalCssInterface.php:24
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$currentDashboard
‪Dashboard $currentDashboard
Definition: DashboardInitializationService.php:44
‪TYPO3\CMS\Dashboard\DashboardInitializationService\getCssFiles
‪array getCssFiles()
Definition: DashboardInitializationService.php:230
‪TYPO3\CMS\Dashboard\DashboardInitializationService\getDashboardsForUser
‪Dashboard[] getDashboardsForUser()
Definition: DashboardInitializationService.php:120
‪TYPO3\CMS\Dashboard\Widgets\AdditionalJavaScriptInterface
Definition: AdditionalJavaScriptInterface.php:24
‪TYPO3\CMS\Dashboard\DashboardInitializationService\defineCssFiles
‪defineCssFiles(AdditionalCssInterface $widgetInstance)
Definition: DashboardInitializationService.php:191
‪TYPO3\CMS\Dashboard\DashboardInitializationService\createDefaultDashboards
‪createDefaultDashboards()
Definition: DashboardInitializationService.php:90
‪TYPO3\CMS\Dashboard\DashboardInitializationService
Definition: DashboardInitializationService.php:31
‪TYPO3\CMS\Dashboard\DashboardRepository
Definition: DashboardRepository.php:31
‪TYPO3\CMS\Dashboard\DashboardInitializationService\getJsFiles
‪array getJsFiles()
Definition: DashboardInitializationService.php:222
‪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:32
‪TYPO3\CMS\Dashboard\DashboardInitializationService\defineResourcesOfWidgets
‪defineResourcesOfWidgets(array $widgets)
Definition: DashboardInitializationService.php:137
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$jsFiles
‪$jsFiles
Definition: DashboardInitializationService.php:53
‪TYPO3\CMS\Dashboard\Widgets\AdditionalCssInterface\getCssFiles
‪array getCssFiles()
‪TYPO3\CMS\Dashboard\DashboardInitializationService\getCurrentDashboard
‪getCurrentDashboard()
Definition: DashboardInitializationService.php:73
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$cssFiles
‪$cssFiles
Definition: DashboardInitializationService.php:54
‪TYPO3\CMS\Dashboard\Widgets\AdditionalJavaScriptInterface\getJsFiles
‪array getJsFiles()
‪TYPO3\CMS\Dashboard\Widgets\RequireJsModuleInterface\getRequireJsModules
‪array getRequireJsModules()
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$user
‪BackendUserAuthentication $user
Definition: DashboardInitializationService.php:48
‪TYPO3\CMS\Dashboard\Dashboard\getIdentifier
‪string getIdentifier()
Definition: Dashboard.php:71
‪TYPO3\CMS\Dashboard\DashboardInitializationService\loadCurrentDashboard
‪loadCurrentDashboard(BackendUserAuthentication $user)
Definition: DashboardInitializationService.php:201
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\getModuleData
‪mixed getModuleData($module, $type='')
Definition: AbstractUserAuthentication.php:1181
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\pushModuleData
‪pushModuleData($module, $data, $noSave=0)
Definition: AbstractUserAuthentication.php:1161
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$dashboardRepository
‪DashboardRepository $dashboardRepository
Definition: DashboardInitializationService.php:36
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Dashboard\DashboardInitializationService\defineJsFiles
‪defineJsFiles(AdditionalJavaScriptInterface $widgetInstance)
Definition: DashboardInitializationService.php:175
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$requireJsModules
‪array $requireJsModules
Definition: DashboardInitializationService.php:52
‪TYPO3\CMS\Dashboard\DashboardInitializationService\saveCurrentDashboard
‪saveCurrentDashboard(BackendUserAuthentication $user, string $identifier)
Definition: DashboardInitializationService.php:206
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Dashboard\DashboardInitializationService\getRequireJsModules
‪array getRequireJsModules()
Definition: DashboardInitializationService.php:214
‪TYPO3\CMS\Dashboard\DashboardInitializationService\$dashboardPresetRegistry
‪DashboardPresetRegistry $dashboardPresetRegistry
Definition: DashboardInitializationService.php:40
‪TYPO3\CMS\Dashboard\DashboardInitializationService\defineRequireJsModules
‪defineRequireJsModules(RequireJsModuleInterface $widgetInstance)
Definition: DashboardInitializationService.php:158
‪TYPO3\CMS\Dashboard\DashboardInitializationService\defineCurrentDashboard
‪defineCurrentDashboard()
Definition: DashboardInitializationService.php:78
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath($targetPath)
Definition: PathUtility.php:43
‪TYPO3\CMS\Dashboard
‪TYPO3\CMS\Dashboard\Widgets\RequireJsModuleInterface
Definition: RequireJsModuleInterface.php:24
‪TYPO3\CMS\Dashboard\DashboardPresetRegistry
Definition: DashboardPresetRegistry.php:26
‪TYPO3\CMS\Dashboard\DashboardInitializationService\__construct
‪__construct(DashboardRepository $dashboardRepository, DashboardPresetRegistry $dashboardPresetRegistry)
Definition: DashboardInitializationService.php:56