DashboardController.php 4.9 KB

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