DashboardController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julien Veyssier <eneiluj@posteo.net>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Kate Döen <kate.doeen@nextcloud.com>
  11. * @author Eduardo Morales <eduardo.morales@nextcloud.com>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\Dashboard\Controller;
  30. use OCA\Files\Event\LoadSidebar;
  31. use OCA\Viewer\Event\LoadViewer;
  32. use OCP\AppFramework\Controller;
  33. use OCP\AppFramework\Http;
  34. use OCP\AppFramework\Http\Attribute\OpenAPI;
  35. use OCP\AppFramework\Http\JSONResponse;
  36. use OCP\AppFramework\Http\TemplateResponse;
  37. use OCP\AppFramework\Services\IInitialState;
  38. use OCP\Dashboard\IManager;
  39. use OCP\Dashboard\IWidget;
  40. use OCP\Dashboard\RegisterWidgetEvent;
  41. use OCP\EventDispatcher\IEventDispatcher;
  42. use OCP\IConfig;
  43. use OCP\IL10N;
  44. use OCP\IRequest;
  45. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  46. class DashboardController extends Controller {
  47. /** @var IInitialState */
  48. private $initialState;
  49. /** @var IEventDispatcher */
  50. private $eventDispatcher;
  51. /** @var IManager */
  52. private $dashboardManager;
  53. /** @var IConfig */
  54. private $config;
  55. /** @var IL10N */
  56. private $l10n;
  57. /** @var string */
  58. private $userId;
  59. public function __construct(
  60. string $appName,
  61. IRequest $request,
  62. IInitialState $initialState,
  63. IEventDispatcher $eventDispatcher,
  64. IManager $dashboardManager,
  65. IConfig $config,
  66. IL10N $l10n,
  67. $userId
  68. ) {
  69. parent::__construct($appName, $request);
  70. $this->initialState = $initialState;
  71. $this->eventDispatcher = $eventDispatcher;
  72. $this->dashboardManager = $dashboardManager;
  73. $this->config = $config;
  74. $this->l10n = $l10n;
  75. $this->userId = $userId;
  76. }
  77. /**
  78. * @NoCSRFRequired
  79. * @NoAdminRequired
  80. * @return TemplateResponse
  81. */
  82. public function index(): TemplateResponse {
  83. \OCP\Util::addStyle('dashboard', 'dashboard');
  84. \OCP\Util::addScript('dashboard', 'main', 'theming');
  85. $this->eventDispatcher->dispatchTyped(new LoadSidebar());
  86. if (class_exists(LoadViewer::class)) {
  87. $this->eventDispatcher->dispatchTyped(new LoadViewer());
  88. }
  89. $this->eventDispatcher->dispatchTyped(new RegisterWidgetEvent($this->dashboardManager));
  90. $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
  91. $userLayout = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault));
  92. $widgets = array_map(function (IWidget $widget) {
  93. return [
  94. 'id' => $widget->getId(),
  95. 'title' => $widget->getTitle(),
  96. 'iconClass' => $widget->getIconClass(),
  97. 'url' => $widget->getUrl()
  98. ];
  99. }, $this->dashboardManager->getWidgets());
  100. $configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', '');
  101. $statuses = json_decode($configStatuses, true);
  102. // We avoid getting an empty array as it will not produce an object in UI's JS
  103. // It does not matter if some statuses are missing from the array, missing ones are considered enabled
  104. $statuses = ($statuses && count($statuses) > 0) ? $statuses : ['weather' => true];
  105. $this->initialState->provideInitialState('panels', $widgets);
  106. $this->initialState->provideInitialState('statuses', $statuses);
  107. $this->initialState->provideInitialState('layout', $userLayout);
  108. $this->initialState->provideInitialState('firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1');
  109. $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0');
  110. $response = new TemplateResponse('dashboard', 'index', [
  111. 'id-app-content' => '#app-dashboard',
  112. 'id-app-navigation' => null,
  113. 'pageTitle' => $this->l10n->t('Dashboard'),
  114. ]);
  115. // For the weather widget we should allow the geolocation
  116. $featurePolicy = new Http\FeaturePolicy();
  117. $featurePolicy->addAllowedGeoLocationDomain('\'self\'');
  118. $response->setFeaturePolicy($featurePolicy);
  119. return $response;
  120. }
  121. /**
  122. * @NoAdminRequired
  123. * @param string $layout
  124. * @return JSONResponse
  125. */
  126. public function updateLayout(string $layout): JSONResponse {
  127. $this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout);
  128. return new JSONResponse(['layout' => $layout]);
  129. }
  130. /**
  131. * @NoAdminRequired
  132. * @param string $statuses
  133. * @return JSONResponse
  134. */
  135. public function updateStatuses(string $statuses): JSONResponse {
  136. $this->config->setUserValue($this->userId, 'dashboard', 'statuses', $statuses);
  137. return new JSONResponse(['statuses' => $statuses]);
  138. }
  139. }