Passed
Push — master ( c538b1...0ed8d1 )
by John
10:37 queued 10s
created
apps/dashboard/lib/Controller/DashboardController.php 1 patch
Indentation   +152 added lines, -152 removed lines patch added patch discarded remove patch
@@ -45,156 +45,156 @@
 block discarded – undo
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
-		$configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', '');
107
-		$statuses = json_decode($configStatuses, true);
108
-		// We avoid getting an empty array as it will not produce an object in UI's JS
109
-		// It does not matter if some statuses are missing from the array, missing ones are considered enabled
110
-		$statuses = ($statuses && count($statuses) > 0) ? $statuses : ['weather' => true];
111
-
112
-		$this->inititalStateService->provideInitialState('dashboard', 'panels', $widgets);
113
-		$this->inititalStateService->provideInitialState('dashboard', 'statuses', $statuses);
114
-		$this->inititalStateService->provideInitialState('dashboard', 'layout', $userLayout);
115
-		$this->inititalStateService->provideInitialState('dashboard', 'firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1');
116
-		$this->inititalStateService->provideInitialState('dashboard', 'shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS);
117
-		$this->inititalStateService->provideInitialState('dashboard', 'background', $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default'));
118
-		$this->inititalStateService->provideInitialState('dashboard', 'version', $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0));
119
-		$this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0');
120
-
121
-		$response = new TemplateResponse('dashboard', 'index');
122
-
123
-		// For the weather widget we should allow the geolocation
124
-		$featurePolicy = new Http\FeaturePolicy();
125
-		$featurePolicy->addAllowedGeoLocationDomain('\'self\'');
126
-		$response->setFeaturePolicy($featurePolicy);
127
-
128
-		return $response;
129
-	}
130
-
131
-	/**
132
-	 * @NoAdminRequired
133
-	 * @param string $layout
134
-	 * @return JSONResponse
135
-	 */
136
-	public function updateLayout(string $layout): JSONResponse {
137
-		$this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout);
138
-		return new JSONResponse(['layout' => $layout]);
139
-	}
140
-
141
-	/**
142
-	 * @NoAdminRequired
143
-	 * @param string $statuses
144
-	 * @return JSONResponse
145
-	 */
146
-	public function updateStatuses(string $statuses): JSONResponse {
147
-		$this->config->setUserValue($this->userId, 'dashboard', 'statuses', $statuses);
148
-		return new JSONResponse(['statuses' => $statuses]);
149
-	}
150
-
151
-	/**
152
-	 * @NoAdminRequired
153
-	 */
154
-	public function setBackground(string $type = 'default', string $value = ''): JSONResponse {
155
-		$currentVersion = (int)$this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', '0');
156
-		try {
157
-			switch ($type) {
158
-				case 'shipped':
159
-					$this->backgroundService->setShippedBackground($value);
160
-					break;
161
-				case 'custom':
162
-					$this->backgroundService->setFileBackground($value);
163
-					break;
164
-				case 'color':
165
-					$this->backgroundService->setColorBackground($value);
166
-					break;
167
-				case 'default':
168
-					$this->backgroundService->setDefaultBackground();
169
-					break;
170
-				default:
171
-					return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST);
172
-			}
173
-		} catch (\InvalidArgumentException $e) {
174
-			return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
175
-		} catch (\Throwable $e) {
176
-			return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
177
-		}
178
-		$currentVersion++;
179
-		$this->config->setUserValue($this->userId, 'dashboard', 'backgroundVersion', (string)$currentVersion);
180
-		return new JSONResponse([
181
-			'type' => $type,
182
-			'value' => $value,
183
-			'version' => $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion)
184
-		]);
185
-	}
186
-
187
-	/**
188
-	 * @NoAdminRequired
189
-	 * @NoCSRFRequired
190
-	 */
191
-	public function getBackground() {
192
-		$file = $this->backgroundService->getBackground();
193
-		if ($file !== null) {
194
-			$response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]);
195
-			$response->cacheFor(24 * 60 * 60);
196
-			return $response;
197
-		}
198
-		return new NotFoundResponse();
199
-	}
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
+        $configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', '');
107
+        $statuses = json_decode($configStatuses, true);
108
+        // We avoid getting an empty array as it will not produce an object in UI's JS
109
+        // It does not matter if some statuses are missing from the array, missing ones are considered enabled
110
+        $statuses = ($statuses && count($statuses) > 0) ? $statuses : ['weather' => true];
111
+
112
+        $this->inititalStateService->provideInitialState('dashboard', 'panels', $widgets);
113
+        $this->inititalStateService->provideInitialState('dashboard', 'statuses', $statuses);
114
+        $this->inititalStateService->provideInitialState('dashboard', 'layout', $userLayout);
115
+        $this->inititalStateService->provideInitialState('dashboard', 'firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1');
116
+        $this->inititalStateService->provideInitialState('dashboard', 'shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS);
117
+        $this->inititalStateService->provideInitialState('dashboard', 'background', $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default'));
118
+        $this->inititalStateService->provideInitialState('dashboard', 'version', $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0));
119
+        $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0');
120
+
121
+        $response = new TemplateResponse('dashboard', 'index');
122
+
123
+        // For the weather widget we should allow the geolocation
124
+        $featurePolicy = new Http\FeaturePolicy();
125
+        $featurePolicy->addAllowedGeoLocationDomain('\'self\'');
126
+        $response->setFeaturePolicy($featurePolicy);
127
+
128
+        return $response;
129
+    }
130
+
131
+    /**
132
+     * @NoAdminRequired
133
+     * @param string $layout
134
+     * @return JSONResponse
135
+     */
136
+    public function updateLayout(string $layout): JSONResponse {
137
+        $this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout);
138
+        return new JSONResponse(['layout' => $layout]);
139
+    }
140
+
141
+    /**
142
+     * @NoAdminRequired
143
+     * @param string $statuses
144
+     * @return JSONResponse
145
+     */
146
+    public function updateStatuses(string $statuses): JSONResponse {
147
+        $this->config->setUserValue($this->userId, 'dashboard', 'statuses', $statuses);
148
+        return new JSONResponse(['statuses' => $statuses]);
149
+    }
150
+
151
+    /**
152
+     * @NoAdminRequired
153
+     */
154
+    public function setBackground(string $type = 'default', string $value = ''): JSONResponse {
155
+        $currentVersion = (int)$this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', '0');
156
+        try {
157
+            switch ($type) {
158
+                case 'shipped':
159
+                    $this->backgroundService->setShippedBackground($value);
160
+                    break;
161
+                case 'custom':
162
+                    $this->backgroundService->setFileBackground($value);
163
+                    break;
164
+                case 'color':
165
+                    $this->backgroundService->setColorBackground($value);
166
+                    break;
167
+                case 'default':
168
+                    $this->backgroundService->setDefaultBackground();
169
+                    break;
170
+                default:
171
+                    return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST);
172
+            }
173
+        } catch (\InvalidArgumentException $e) {
174
+            return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
175
+        } catch (\Throwable $e) {
176
+            return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
177
+        }
178
+        $currentVersion++;
179
+        $this->config->setUserValue($this->userId, 'dashboard', 'backgroundVersion', (string)$currentVersion);
180
+        return new JSONResponse([
181
+            'type' => $type,
182
+            'value' => $value,
183
+            'version' => $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion)
184
+        ]);
185
+    }
186
+
187
+    /**
188
+     * @NoAdminRequired
189
+     * @NoCSRFRequired
190
+     */
191
+    public function getBackground() {
192
+        $file = $this->backgroundService->getBackground();
193
+        if ($file !== null) {
194
+            $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]);
195
+            $response->cacheFor(24 * 60 * 60);
196
+            return $response;
197
+        }
198
+        return new NotFoundResponse();
199
+    }
200 200
 }
Please login to merge, or discard this patch.