DashboardController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Dashboard\Controller;
  8. use OCA\Dashboard\Service\DashboardService;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  12. use OCP\AppFramework\Http\Attribute\OpenAPI;
  13. use OCP\AppFramework\Http\TemplateResponse;
  14. use OCP\AppFramework\Services\IInitialState;
  15. use OCP\Dashboard\IIconWidget;
  16. use OCP\Dashboard\IManager;
  17. use OCP\Dashboard\IWidget;
  18. use OCP\EventDispatcher\IEventDispatcher;
  19. use OCP\IConfig;
  20. use OCP\IL10N;
  21. use OCP\IRequest;
  22. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  23. class DashboardController extends Controller {
  24. public function __construct(
  25. string $appName,
  26. IRequest $request,
  27. private IInitialState $initialState,
  28. private IEventDispatcher $eventDispatcher,
  29. private IManager $dashboardManager,
  30. private IConfig $config,
  31. private IL10N $l10n,
  32. private ?string $userId,
  33. private DashboardService $service,
  34. ) {
  35. parent::__construct($appName, $request);
  36. }
  37. /**
  38. * @NoCSRFRequired
  39. * @NoAdminRequired
  40. * @return TemplateResponse
  41. */
  42. #[FrontpageRoute(verb: 'GET', url: '/')]
  43. public function index(): TemplateResponse {
  44. \OCP\Util::addStyle('dashboard', 'dashboard');
  45. \OCP\Util::addScript('dashboard', 'main', 'theming');
  46. $widgets = array_map(function (IWidget $widget) {
  47. return [
  48. 'id' => $widget->getId(),
  49. 'title' => $widget->getTitle(),
  50. 'iconClass' => $widget->getIconClass(),
  51. 'iconUrl' => $widget instanceof IIconWidget ? $widget->getIconUrl() : '',
  52. 'url' => $widget->getUrl()
  53. ];
  54. }, $this->dashboardManager->getWidgets());
  55. $this->initialState->provideInitialState('panels', $widgets);
  56. $this->initialState->provideInitialState('statuses', $this->service->getStatuses());
  57. $this->initialState->provideInitialState('layout', $this->service->getLayout());
  58. $this->initialState->provideInitialState('appStoreEnabled', $this->config->getSystemValueBool('appstoreenabled', true));
  59. $this->initialState->provideInitialState('firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1');
  60. $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0');
  61. $response = new TemplateResponse('dashboard', 'index', [
  62. 'id-app-content' => '#app-dashboard',
  63. 'id-app-navigation' => null,
  64. 'pageTitle' => $this->l10n->t('Dashboard'),
  65. ]);
  66. // For the weather widget we should allow the geolocation
  67. $featurePolicy = new Http\FeaturePolicy();
  68. $featurePolicy->addAllowedGeoLocationDomain('\'self\'');
  69. $response->setFeaturePolicy($featurePolicy);
  70. return $response;
  71. }
  72. }