1
0

DashboardController.php 4.8 KB

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