DashboardController.php 2.8 KB

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