DashboardController.php 2.5 KB

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