Completed
Push — fix_untranslated_strings ( 07522f...8348da )
by
unknown
17:53
created

SettingsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
/**
3
 * ownCloud - Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/g/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
use OCP\AppFramework\Controller;
25
use OCP\AppFramework\Http\JSONResponse;
26
use OCP\AppFramework\Http;
27
use OCP\IConfig;
28
use OCP\IRequest;
29
use OCP\IUserSession;
30
31
class SettingsController extends Controller {
32
33
	/**
34
	 * @var IConfig
35
	 */
36
	private $config;
37
38
	/**
39
	 * @var IUserSession
40
	 */
41
	private $userSession;
42
43
	/**
44
	 * @param string $appName
45
	 * @param IRequest $request an instance of the request
46
	 * @param IUserSession $userSession
47
	 * @param IConfig $config
48
	 */
49
	public function __construct($appName, IRequest $request, IUserSession $userSession,
50
								IConfig $config) {
51
		parent::__construct($appName, $request);
52
		$this->config = $config;
53
		$this->userSession = $userSession;
54
	}
55
56
57
	/**
58
	 * set a new view
59
	 *
60
	 * @param string $view
61
	 * @return JSONResponse
62
	 *
63
	 * @NoAdminRequired
64
	 */
65
	public function setView($view) {
66
		if (!$this->isViewAllowed($view)) {
67
			return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
68
		}
69
70
		$userId = $this->userSession->getUser()->getUID();
71
		$app = $this->appName;
72
73
		try {
74
			$this->config->setUserValue(
75
				$userId,
76
				$app,
77
				'currentView',
78
				$view
79
			);
80
		} catch(\Exception $e) {
81
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
82
		}
83
84
		return new JSONResponse();
85
	}
86
87
88
	/**
89
	 * get a config value
90
	 *
91
	 * @return JSONResponse
92
	 *
93
	 * @NoAdminRequired
94
	 */
95
	public function getView() {
96
		$userId = $this->userSession->getUser()->getUID();
97
		$app = $this->appName;
98
99
		try {
100
			$view = $this->config->getUserValue(
101
				$userId,
102
				$app,
103
				'currentView',
104
				'month'
105
			);
106
		} catch(\Exception $e) {
107
			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
108
		}
109
110
		return new JSONResponse([
111
			'value' => $view,
112
		]);
113
	}
114
115
	/**
116
	 * check if view is allowed
117
	 *
118
	 * @param $view
119
	 * @return bool
120
	 */
121
	private function isViewAllowed($view) {
122
		$allowedViews = [
123
			'agendaDay',
124
			'agendaWeek',
125
			'month',
126
		];
127
128
		return in_array($view, $allowedViews);
129
	}
130
}