DashboardController.php 2.8 KB

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