You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DashboardController.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Dashboard\Controller;
  25. use OCA\Files\Event\LoadSidebar;
  26. use OCA\Viewer\Event\LoadViewer;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\JSONResponse;
  29. use OCP\AppFramework\Http\TemplateResponse;
  30. use OCP\Dashboard\IManager;
  31. use OCP\Dashboard\IWidget;
  32. use OCP\Dashboard\RegisterWidgetEvent;
  33. use OCP\EventDispatcher\IEventDispatcher;
  34. use OCP\IConfig;
  35. use OCP\IInitialStateService;
  36. use OCP\IRequest;
  37. class DashboardController extends Controller {
  38. /** @var IInitialStateService */
  39. private $inititalStateService;
  40. /** @var IEventDispatcher */
  41. private $eventDispatcher;
  42. /** @var IManager */
  43. private $dashboardManager;
  44. /** @var IConfig */
  45. private $config;
  46. /** @var string */
  47. private $userId;
  48. public function __construct(
  49. string $appName,
  50. IRequest $request,
  51. IInitialStateService $initialStateService,
  52. IEventDispatcher $eventDispatcher,
  53. IManager $dashboardManager,
  54. IConfig $config,
  55. $userId
  56. ) {
  57. parent::__construct($appName, $request);
  58. $this->inititalStateService = $initialStateService;
  59. $this->eventDispatcher = $eventDispatcher;
  60. $this->dashboardManager = $dashboardManager;
  61. $this->config = $config;
  62. $this->userId = $userId;
  63. }
  64. /**
  65. * @NoCSRFRequired
  66. * @NoAdminRequired
  67. * @return TemplateResponse
  68. */
  69. public function index(): TemplateResponse {
  70. $this->eventDispatcher->dispatchTyped(new LoadSidebar());
  71. if (class_exists(LoadViewer::class)) {
  72. $this->eventDispatcher->dispatchTyped(new LoadViewer());
  73. }
  74. $this->eventDispatcher->dispatchTyped(new RegisterWidgetEvent($this->dashboardManager));
  75. $userLayout = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', 'recommendations,spreed,mail,calendar'));
  76. $widgets = array_map(function (IWidget $widget) {
  77. return [
  78. 'id' => $widget->getId(),
  79. 'title' => $widget->getTitle(),
  80. 'iconClass' => $widget->getIconClass(),
  81. 'url' => $widget->getUrl()
  82. ];
  83. }, $this->dashboardManager->getWidgets());
  84. $this->inititalStateService->provideInitialState('dashboard', 'panels', $widgets);
  85. $this->inititalStateService->provideInitialState('dashboard', 'layout', $userLayout);
  86. $this->inititalStateService->provideInitialState('dashboard', 'firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1');
  87. $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0');
  88. return new TemplateResponse('dashboard', 'index');
  89. }
  90. /**
  91. * @NoAdminRequired
  92. * @param string $layout
  93. * @return JSONResponse
  94. */
  95. public function updateLayout(string $layout): JSONResponse {
  96. $this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout);
  97. return new JSONResponse(['layout' => $layout]);
  98. }
  99. }