1<?php
2
3declare(strict_types=1);
4/**
5 * Calendar App
6 *
7 * @author Georg Ehrke
8 * @copyright 2019 Georg Ehrke <oc.list@georgehrke.com>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12 * License as published by the Free Software Foundation; either
13 * version 3 of the License, or any later version.
14 *
15 * This library 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
21 * License along with this library.  If not, see <http://www.gnu.org/g/>.
22 *
23 */
24namespace OCA\Calendar\Controller;
25
26use OCP\AppFramework\Controller;
27use OCP\AppFramework\Http\JSONResponse;
28use OCP\AppFramework\Http;
29use OCP\IConfig;
30use OCP\IRequest;
31
32/**
33 * Class SettingsController
34 *
35 * @package OCA\Calendar\Controller
36 */
37class SettingsController extends Controller {
38
39	/** @var IConfig */
40	private $config;
41
42	/** @var string */
43	private $userId;
44
45	/**
46	 * SettingsController constructor.
47	 *
48	 * @param string $appName
49	 * @param IRequest $request
50	 * @param IConfig $config
51	 * @param string $userId
52	 */
53	public function __construct(string $appName,
54								IRequest $request,
55								IConfig $config,
56								string $userId) {
57		parent::__construct($appName, $request);
58		$this->config = $config;
59		$this->userId = $userId;
60	}
61
62	/**
63	 * set a configuration item
64	 *
65	 * @NoAdminRequired
66	 *
67	 * @param string $key The config key to set
68	 * @param mixed $value The value to set for given config key
69	 * @return JSONResponse
70	 */
71	public function setConfig(string $key,
72							  string $value):JSONResponse {
73		switch ($key) {
74			case 'view':
75				return $this->setView($value);
76			case 'skipPopover':
77				return $this->setSkipPopover($value);
78			case 'showWeekends':
79				return $this->showWeekends($value);
80			case 'showWeekNr':
81				return $this->setShowWeekNr($value);
82			case 'firstRun':
83				return $this->setFirstRun();
84			case 'timezone':
85				return $this->setTimezone($value);
86			case 'eventLimit':
87				return $this->setEventLimit($value);
88			case 'slotDuration':
89				return $this->setSlotDuration($value);
90			case 'defaultReminder':
91				return $this->setDefaultReminder($value);
92			case 'showTasks':
93				return $this->setShowTasks($value);
94			default:
95				return new JSONResponse([], Http::STATUS_BAD_REQUEST);
96		}
97	}
98
99
100	/**
101	 * set a new view
102	 *
103	 * @param string $view Selected view by user
104	 * @return JSONResponse
105	 */
106	private function setView(string $view):JSONResponse {
107		if (!\in_array($view, ['timeGridDay', 'timeGridWeek', 'dayGridMonth', 'listMonth'])) {
108			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
109		}
110
111		try {
112			$this->config->setUserValue(
113				$this->userId,
114				$this->appName,
115				'currentView',
116				$view
117			);
118		} catch (\Exception $e) {
119			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
120		}
121
122		return new JSONResponse();
123	}
124
125	/**
126	 * set if popover shall be skipped
127	 *
128	 * @param $value User-selected option whether or not to show simple event editor
129	 * @return JSONResponse
130	 */
131	private function setSkipPopover(string $value):JSONResponse {
132		if (!\in_array($value, ['yes', 'no'])) {
133			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
134		}
135
136		try {
137			$this->config->setUserValue(
138				$this->userId,
139				$this->appName,
140				'skipPopover',
141				$value
142			);
143		} catch (\Exception $e) {
144			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
145		}
146
147		return new JSONResponse();
148	}
149
150	/**
151	 * set config value for showing tasks
152	 *
153	 * @param $value User-selected option whether or not to show tasks
154	 * @return JSONResponse
155	 */
156	private function setShowTasks(string $value):JSONResponse {
157		if (!\in_array($value, ['yes', 'no'])) {
158			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
159		}
160
161		try {
162			$this->config->setUserValue(
163				$this->userId,
164				$this->appName,
165				'showTasks',
166				$value
167			);
168		} catch (\Exception $e) {
169			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
170		}
171
172		return new JSONResponse();
173	}
174
175	/**
176	 * set config value for showing week numbers
177	 *
178	 * @param $value User-selected option whether or not to show weekends
179	 * @return JSONResponse
180	 */
181	private function showWeekends(string $value):JSONResponse {
182		if (!\in_array($value, ['yes', 'no'])) {
183			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
184		}
185
186		try {
187			$this->config->setUserValue(
188				$this->userId,
189				$this->appName,
190				'showWeekends',
191				$value
192			);
193		} catch (\Exception $e) {
194			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
195		}
196
197		return new JSONResponse();
198	}
199
200	/**
201	 * set config value for showing week numbers
202	 *
203	 * @param $value User-selected option whether or not to show week numbers
204	 * @return JSONResponse
205	 */
206	private function setShowWeekNr(string $value):JSONResponse {
207		if (!\in_array($value, ['yes', 'no'])) {
208			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
209		}
210
211		try {
212			$this->config->setUserValue(
213				$this->userId,
214				$this->appName,
215				'showWeekNr',
216				$value
217			);
218		} catch (\Exception $e) {
219			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
220		}
221
222		return new JSONResponse();
223	}
224
225	/**
226	 * remember that first run routines executed
227	 *
228	 * @return JSONResponse
229	 */
230	private function setFirstRun():JSONResponse {
231		try {
232			$this->config->setUserValue(
233				$this->userId,
234				$this->appName,
235				'firstRun',
236				'no'
237			);
238		} catch (\Exception $e) {
239			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
240		}
241
242		return new JSONResponse();
243	}
244
245	/**
246	 * sets display timezone for user
247	 *
248	 * @param string $value User-selected option for timezone to display events in
249	 * @return JSONResponse
250	 */
251	private function setTimezone(string $value):JSONResponse {
252		try {
253			$this->config->setUserValue(
254				$this->userId,
255				$this->appName,
256				'timezone',
257				$value
258			);
259		} catch (\Exception $e) {
260			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
261		}
262
263		return new JSONResponse();
264	}
265
266	/**
267	 * sets eventLimit for user
268	 *
269	 * @param string $value User-selected option whether or not to have an event limit
270	 * @return JSONResponse
271	 */
272	private function setEventLimit(string $value):JSONResponse {
273		if (!\in_array($value, ['yes', 'no'])) {
274			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
275		}
276
277		try {
278			$this->config->setUserValue(
279				$this->userId,
280				$this->appName,
281				'eventLimit',
282				$value
283			);
284		} catch (\Exception $e) {
285			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
286		}
287
288		return new JSONResponse();
289	}
290
291	/**
292	 * sets slotDuration for user
293	 *
294	 * @param string $value User-selected option for slot-duration in agenda view
295	 * @return JSONResponse
296	 */
297	private function setSlotDuration(string $value):JSONResponse {
298		if (!\in_array($value, ['00:05:00', '00:10:00', '00:15:00', '00:20:00', '00:30:00', '01:00:00'])) {
299			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
300		}
301
302		try {
303			$this->config->setUserValue(
304				$this->userId,
305				$this->appName,
306				'slotDuration',
307				$value
308			);
309		} catch (\Exception $e) {
310			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
311		}
312
313		return new JSONResponse();
314	}
315
316	/**
317	 * sets defaultReminder for user
318	 *
319	 * @param string $value User-selected option for default_reminder in agenda view
320	 * @return JSONResponse
321	 */
322	private function setDefaultReminder(string $value):JSONResponse {
323		if ($value !== 'none' &&
324			filter_var($value, FILTER_VALIDATE_INT,
325				['options' => ['max_range' => 0]]) === false) {
326			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
327		}
328
329		try {
330			$this->config->setUserValue(
331				$this->userId,
332				$this->appName,
333				'defaultReminder',
334				$value
335			);
336		} catch (\Exception $e) {
337			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
338		}
339
340		return new JSONResponse();
341	}
342}
343