@@ -45,139 +45,139 @@ |
||
45 | 45 | |
46 | 46 | class DashboardController extends Controller { |
47 | 47 | |
48 | - /** @var IInitialStateService */ |
|
49 | - private $inititalStateService; |
|
50 | - /** @var IEventDispatcher */ |
|
51 | - private $eventDispatcher; |
|
52 | - /** @var IManager */ |
|
53 | - private $dashboardManager; |
|
54 | - /** @var IConfig */ |
|
55 | - private $config; |
|
56 | - /** @var string */ |
|
57 | - private $userId; |
|
58 | - /** |
|
59 | - * @var BackgroundService |
|
60 | - */ |
|
61 | - private $backgroundService; |
|
62 | - |
|
63 | - public function __construct( |
|
64 | - string $appName, |
|
65 | - IRequest $request, |
|
66 | - IInitialStateService $initialStateService, |
|
67 | - IEventDispatcher $eventDispatcher, |
|
68 | - IManager $dashboardManager, |
|
69 | - IConfig $config, |
|
70 | - BackgroundService $backgroundService, |
|
71 | - $userId |
|
72 | - ) { |
|
73 | - parent::__construct($appName, $request); |
|
74 | - |
|
75 | - $this->inititalStateService = $initialStateService; |
|
76 | - $this->eventDispatcher = $eventDispatcher; |
|
77 | - $this->dashboardManager = $dashboardManager; |
|
78 | - $this->config = $config; |
|
79 | - $this->backgroundService = $backgroundService; |
|
80 | - $this->userId = $userId; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @NoCSRFRequired |
|
85 | - * @NoAdminRequired |
|
86 | - * @return TemplateResponse |
|
87 | - */ |
|
88 | - public function index(): TemplateResponse { |
|
89 | - \OCP\Util::addStyle('dashboard', 'dashboard'); |
|
90 | - $this->eventDispatcher->dispatchTyped(new LoadSidebar()); |
|
91 | - if (class_exists(LoadViewer::class)) { |
|
92 | - $this->eventDispatcher->dispatchTyped(new LoadViewer()); |
|
93 | - } |
|
94 | - |
|
95 | - $this->eventDispatcher->dispatchTyped(new RegisterWidgetEvent($this->dashboardManager)); |
|
96 | - |
|
97 | - $userLayout = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', 'recommendations,spreed,mail,calendar')); |
|
98 | - $widgets = array_map(function (IWidget $widget) { |
|
99 | - return [ |
|
100 | - 'id' => $widget->getId(), |
|
101 | - 'title' => $widget->getTitle(), |
|
102 | - 'iconClass' => $widget->getIconClass(), |
|
103 | - 'url' => $widget->getUrl() |
|
104 | - ]; |
|
105 | - }, $this->dashboardManager->getWidgets()); |
|
106 | - $this->inititalStateService->provideInitialState('dashboard', 'panels', $widgets); |
|
107 | - $this->inititalStateService->provideInitialState('dashboard', 'layout', $userLayout); |
|
108 | - $this->inititalStateService->provideInitialState('dashboard', 'firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1'); |
|
109 | - $this->inititalStateService->provideInitialState('dashboard', 'shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS); |
|
110 | - $this->inititalStateService->provideInitialState('dashboard', 'background', $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default')); |
|
111 | - $this->inititalStateService->provideInitialState('dashboard', 'version', $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0)); |
|
112 | - $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0'); |
|
113 | - |
|
114 | - $response = new TemplateResponse('dashboard', 'index'); |
|
115 | - |
|
116 | - // For the weather widget we should allow the geolocation |
|
117 | - $featurePolicy = new Http\FeaturePolicy(); |
|
118 | - $featurePolicy->addAllowedGeoLocationDomain('\'self\''); |
|
119 | - $response->setFeaturePolicy($featurePolicy); |
|
120 | - |
|
121 | - return $response; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @NoAdminRequired |
|
126 | - * @param string $layout |
|
127 | - * @return JSONResponse |
|
128 | - */ |
|
129 | - public function updateLayout(string $layout): JSONResponse { |
|
130 | - $this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout); |
|
131 | - return new JSONResponse(['layout' => $layout]); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @NoAdminRequired |
|
136 | - */ |
|
137 | - public function setBackground(string $type, string $value): JSONResponse { |
|
138 | - $currentVersion = (int)$this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0); |
|
139 | - try { |
|
140 | - switch ($type) { |
|
141 | - case 'shipped': |
|
142 | - $this->backgroundService->setShippedBackground($value); |
|
143 | - break; |
|
144 | - case 'custom': |
|
145 | - $this->backgroundService->setFileBackground($value); |
|
146 | - break; |
|
147 | - case 'color': |
|
148 | - $this->backgroundService->setColorBackground($value); |
|
149 | - break; |
|
150 | - case 'default': |
|
151 | - $this->backgroundService->setDefaultBackground(); |
|
152 | - break; |
|
153 | - default: |
|
154 | - return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST); |
|
155 | - } |
|
156 | - } catch (\InvalidArgumentException $e) { |
|
157 | - return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
|
158 | - } catch (\Throwable $e) { |
|
159 | - return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
160 | - } |
|
161 | - $currentVersion++; |
|
162 | - $this->config->setUserValue($this->userId, 'dashboard', 'backgroundVersion', (string)$currentVersion); |
|
163 | - return new JSONResponse([ |
|
164 | - 'type' => $type, |
|
165 | - 'value' => $value, |
|
166 | - 'version' => $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion) |
|
167 | - ]); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @NoAdminRequired |
|
172 | - * @NoCSRFRequired |
|
173 | - */ |
|
174 | - public function getBackground() { |
|
175 | - $file = $this->backgroundService->getBackground(); |
|
176 | - if ($file !== null) { |
|
177 | - $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]); |
|
178 | - $response->cacheFor(24 * 60 * 60); |
|
179 | - return $response; |
|
180 | - } |
|
181 | - return new NotFoundResponse(); |
|
182 | - } |
|
48 | + /** @var IInitialStateService */ |
|
49 | + private $inititalStateService; |
|
50 | + /** @var IEventDispatcher */ |
|
51 | + private $eventDispatcher; |
|
52 | + /** @var IManager */ |
|
53 | + private $dashboardManager; |
|
54 | + /** @var IConfig */ |
|
55 | + private $config; |
|
56 | + /** @var string */ |
|
57 | + private $userId; |
|
58 | + /** |
|
59 | + * @var BackgroundService |
|
60 | + */ |
|
61 | + private $backgroundService; |
|
62 | + |
|
63 | + public function __construct( |
|
64 | + string $appName, |
|
65 | + IRequest $request, |
|
66 | + IInitialStateService $initialStateService, |
|
67 | + IEventDispatcher $eventDispatcher, |
|
68 | + IManager $dashboardManager, |
|
69 | + IConfig $config, |
|
70 | + BackgroundService $backgroundService, |
|
71 | + $userId |
|
72 | + ) { |
|
73 | + parent::__construct($appName, $request); |
|
74 | + |
|
75 | + $this->inititalStateService = $initialStateService; |
|
76 | + $this->eventDispatcher = $eventDispatcher; |
|
77 | + $this->dashboardManager = $dashboardManager; |
|
78 | + $this->config = $config; |
|
79 | + $this->backgroundService = $backgroundService; |
|
80 | + $this->userId = $userId; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @NoCSRFRequired |
|
85 | + * @NoAdminRequired |
|
86 | + * @return TemplateResponse |
|
87 | + */ |
|
88 | + public function index(): TemplateResponse { |
|
89 | + \OCP\Util::addStyle('dashboard', 'dashboard'); |
|
90 | + $this->eventDispatcher->dispatchTyped(new LoadSidebar()); |
|
91 | + if (class_exists(LoadViewer::class)) { |
|
92 | + $this->eventDispatcher->dispatchTyped(new LoadViewer()); |
|
93 | + } |
|
94 | + |
|
95 | + $this->eventDispatcher->dispatchTyped(new RegisterWidgetEvent($this->dashboardManager)); |
|
96 | + |
|
97 | + $userLayout = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', 'recommendations,spreed,mail,calendar')); |
|
98 | + $widgets = array_map(function (IWidget $widget) { |
|
99 | + return [ |
|
100 | + 'id' => $widget->getId(), |
|
101 | + 'title' => $widget->getTitle(), |
|
102 | + 'iconClass' => $widget->getIconClass(), |
|
103 | + 'url' => $widget->getUrl() |
|
104 | + ]; |
|
105 | + }, $this->dashboardManager->getWidgets()); |
|
106 | + $this->inititalStateService->provideInitialState('dashboard', 'panels', $widgets); |
|
107 | + $this->inititalStateService->provideInitialState('dashboard', 'layout', $userLayout); |
|
108 | + $this->inititalStateService->provideInitialState('dashboard', 'firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1'); |
|
109 | + $this->inititalStateService->provideInitialState('dashboard', 'shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS); |
|
110 | + $this->inititalStateService->provideInitialState('dashboard', 'background', $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default')); |
|
111 | + $this->inititalStateService->provideInitialState('dashboard', 'version', $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0)); |
|
112 | + $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0'); |
|
113 | + |
|
114 | + $response = new TemplateResponse('dashboard', 'index'); |
|
115 | + |
|
116 | + // For the weather widget we should allow the geolocation |
|
117 | + $featurePolicy = new Http\FeaturePolicy(); |
|
118 | + $featurePolicy->addAllowedGeoLocationDomain('\'self\''); |
|
119 | + $response->setFeaturePolicy($featurePolicy); |
|
120 | + |
|
121 | + return $response; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @NoAdminRequired |
|
126 | + * @param string $layout |
|
127 | + * @return JSONResponse |
|
128 | + */ |
|
129 | + public function updateLayout(string $layout): JSONResponse { |
|
130 | + $this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout); |
|
131 | + return new JSONResponse(['layout' => $layout]); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @NoAdminRequired |
|
136 | + */ |
|
137 | + public function setBackground(string $type, string $value): JSONResponse { |
|
138 | + $currentVersion = (int)$this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0); |
|
139 | + try { |
|
140 | + switch ($type) { |
|
141 | + case 'shipped': |
|
142 | + $this->backgroundService->setShippedBackground($value); |
|
143 | + break; |
|
144 | + case 'custom': |
|
145 | + $this->backgroundService->setFileBackground($value); |
|
146 | + break; |
|
147 | + case 'color': |
|
148 | + $this->backgroundService->setColorBackground($value); |
|
149 | + break; |
|
150 | + case 'default': |
|
151 | + $this->backgroundService->setDefaultBackground(); |
|
152 | + break; |
|
153 | + default: |
|
154 | + return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST); |
|
155 | + } |
|
156 | + } catch (\InvalidArgumentException $e) { |
|
157 | + return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
|
158 | + } catch (\Throwable $e) { |
|
159 | + return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
160 | + } |
|
161 | + $currentVersion++; |
|
162 | + $this->config->setUserValue($this->userId, 'dashboard', 'backgroundVersion', (string)$currentVersion); |
|
163 | + return new JSONResponse([ |
|
164 | + 'type' => $type, |
|
165 | + 'value' => $value, |
|
166 | + 'version' => $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion) |
|
167 | + ]); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @NoAdminRequired |
|
172 | + * @NoCSRFRequired |
|
173 | + */ |
|
174 | + public function getBackground() { |
|
175 | + $file = $this->backgroundService->getBackground(); |
|
176 | + if ($file !== null) { |
|
177 | + $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]); |
|
178 | + $response->cacheFor(24 * 60 * 60); |
|
179 | + return $response; |
|
180 | + } |
|
181 | + return new NotFoundResponse(); |
|
182 | + } |
|
183 | 183 | } |