|
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
|
7 |
|
public function __construct($appName, IRequest $request, IUserSession $userSession, |
|
50
|
|
|
IConfig $config) { |
|
51
|
7 |
|
parent::__construct($appName, $request); |
|
52
|
7 |
|
$this->config = $config; |
|
53
|
7 |
|
$this->userSession = $userSession; |
|
54
|
7 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* set a new view |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $view |
|
61
|
|
|
* @return JSONResponse |
|
62
|
|
|
* |
|
63
|
|
|
* @NoAdminRequired |
|
64
|
|
|
*/ |
|
65
|
5 |
View Code Duplication |
public function setView($view) { |
|
|
|
|
|
|
66
|
5 |
|
if (!$this->isViewAllowed($view)) { |
|
67
|
1 |
|
return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
4 |
|
$userId = $this->userSession->getUser()->getUID(); |
|
71
|
4 |
|
$app = $this->appName; |
|
72
|
|
|
|
|
73
|
|
|
try { |
|
74
|
4 |
|
$this->config->setUserValue( |
|
75
|
4 |
|
$userId, |
|
76
|
4 |
|
$app, |
|
77
|
4 |
|
'currentView', |
|
78
|
|
|
$view |
|
79
|
4 |
|
); |
|
80
|
4 |
|
} catch(\Exception $e) { |
|
81
|
1 |
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
return new JSONResponse(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* get a config value |
|
90
|
|
|
* |
|
91
|
|
|
* @return JSONResponse |
|
92
|
|
|
* |
|
93
|
|
|
* @NoAdminRequired |
|
94
|
|
|
*/ |
|
95
|
2 |
View Code Duplication |
public function getView() { |
|
|
|
|
|
|
96
|
2 |
|
$userId = $this->userSession->getUser()->getUID(); |
|
97
|
2 |
|
$app = $this->appName; |
|
98
|
|
|
|
|
99
|
|
|
try { |
|
100
|
2 |
|
$view = $this->config->getUserValue( |
|
101
|
2 |
|
$userId, |
|
102
|
2 |
|
$app, |
|
103
|
2 |
|
'currentView', |
|
104
|
|
|
'month' |
|
105
|
2 |
|
); |
|
106
|
2 |
|
} catch(\Exception $e) { |
|
107
|
1 |
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
return new JSONResponse([ |
|
111
|
1 |
|
'value' => $view, |
|
112
|
1 |
|
]); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* check if view is allowed |
|
117
|
|
|
* |
|
118
|
|
|
* @param $view |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
5 |
|
private function isViewAllowed($view) { |
|
122
|
|
|
$allowedViews = [ |
|
123
|
5 |
|
'agendaDay', |
|
124
|
5 |
|
'agendaWeek', |
|
125
|
5 |
|
'month', |
|
126
|
5 |
|
]; |
|
127
|
|
|
|
|
128
|
5 |
|
return in_array($view, $allowedViews); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* set config value for showing week numbers |
|
133
|
|
|
* |
|
134
|
|
|
* @param bool $showWeekNr |
|
135
|
|
|
* @return JSONResponse |
|
136
|
|
|
* |
|
137
|
|
|
* @NoAdminRequired |
|
138
|
|
|
*/ |
|
139
|
|
View Code Duplication |
public function setShowWeekNr($showWeekNr) { |
|
|
|
|
|
|
140
|
|
|
if (!is_bool($showWeekNr)) { |
|
141
|
|
|
return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$userId = $this->userSession->getUser()->getUID(); |
|
145
|
|
|
$app = $this->appName; |
|
146
|
|
|
|
|
147
|
|
|
try { |
|
148
|
|
|
$this->config->setUserValue( |
|
149
|
|
|
$userId, |
|
150
|
|
|
$app, |
|
151
|
|
|
'currentShowWeekNr', |
|
152
|
|
|
(string)$showWeekNr |
|
153
|
|
|
); |
|
154
|
|
|
} catch(\Exception $e) { |
|
155
|
|
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return new JSONResponse(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* get config value for showing week numbers |
|
163
|
|
|
* |
|
164
|
|
|
* @return JSONResponse |
|
165
|
|
|
* |
|
166
|
|
|
* @NoAdminRequired |
|
167
|
|
|
*/ |
|
168
|
|
View Code Duplication |
public function getShowWeekNr() { |
|
|
|
|
|
|
169
|
|
|
$userId = $this->userSession->getUser()->getUID(); |
|
170
|
|
|
$app = $this->appName; |
|
171
|
|
|
|
|
172
|
|
|
try { |
|
173
|
|
|
$showWeekNr = (bool) $this->config->getUserValue( |
|
174
|
|
|
$userId, |
|
175
|
|
|
$app, |
|
176
|
|
|
'currentShowWeekNr', |
|
177
|
|
|
'' |
|
178
|
|
|
); |
|
179
|
|
|
} catch(\Exception $e) { |
|
180
|
|
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return new JSONResponse([ |
|
184
|
|
|
'value' => $showWeekNr, |
|
185
|
|
|
]); |
|
186
|
|
|
} |
|
187
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.