1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* 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
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $userId; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $appName |
50
|
|
|
* @param IRequest $request an instance of the request |
51
|
|
|
* @param IUserSession $userSession |
52
|
|
|
* @param IConfig $config |
53
|
|
|
*/ |
54
|
19 |
|
public function __construct($appName, IRequest $request, IUserSession $userSession, |
55
|
|
|
IConfig $config) { |
56
|
19 |
|
parent::__construct($appName, $request); |
57
|
19 |
|
$this->config = $config; |
58
|
19 |
|
$this->userSession = $userSession; |
59
|
19 |
|
$this->userId = $userSession->getUser()->getUID(); |
60
|
19 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* get a configuration item |
64
|
|
|
* |
65
|
|
|
* @NoAdminRequired |
66
|
|
|
* |
67
|
|
|
* @param string $key |
68
|
|
|
* @return JSONResponse |
69
|
|
|
*/ |
70
|
7 |
|
public function getConfig($key) { |
71
|
|
|
switch ($key) { |
72
|
7 |
|
case 'view': |
73
|
2 |
|
return $this->getView(); |
74
|
5 |
|
case 'skipPopover': |
75
|
2 |
|
return $this->getSkipPopover(); |
76
|
3 |
|
case 'showWeekNr': |
77
|
|
|
return $this->getShowWeekNr(); |
78
|
3 |
|
case 'firstRun': |
79
|
2 |
|
return $this->getFirstRun(); |
80
|
|
|
default: |
81
|
1 |
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* set a configuration item |
87
|
|
|
* |
88
|
|
|
* @NoAdminRequired |
89
|
|
|
* |
90
|
|
|
* @param string $key |
91
|
|
|
* @param mixed $value |
92
|
|
|
* @return JSONResponse |
93
|
|
|
*/ |
94
|
12 |
|
public function setConfig($key, $value) { |
95
|
|
|
switch ($key) { |
96
|
12 |
|
case 'view': |
97
|
5 |
|
return $this->setView($value); |
98
|
7 |
|
case 'skipPopover': |
99
|
4 |
|
return $this->setSkipPopover($value); |
100
|
3 |
|
case 'showWeekNr': |
101
|
|
|
return $this->setShowWeekNr($value); |
102
|
3 |
|
case 'firstRun': |
103
|
2 |
|
return $this->setFirstRun(); |
104
|
|
|
default: |
105
|
1 |
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* set a new view |
112
|
|
|
* |
113
|
|
|
* @param string $view |
114
|
|
|
* @return JSONResponse |
115
|
|
|
*/ |
116
|
5 |
View Code Duplication |
private function setView($view) { |
|
|
|
|
117
|
5 |
|
if (!$this->isViewAllowed($view)) { |
118
|
1 |
|
return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
try { |
122
|
4 |
|
$this->config->setUserValue( |
123
|
4 |
|
$this->userId, |
124
|
4 |
|
$this->appName, |
125
|
4 |
|
'currentView', |
126
|
4 |
|
$view |
127
|
|
|
); |
128
|
1 |
|
} catch(\Exception $e) { |
129
|
1 |
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
130
|
|
|
} |
131
|
|
|
|
132
|
3 |
|
return new JSONResponse(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* get a config value |
138
|
|
|
* |
139
|
|
|
* @return JSONResponse |
140
|
|
|
*/ |
141
|
2 |
View Code Duplication |
private function getView() { |
|
|
|
|
142
|
|
|
try { |
143
|
2 |
|
$view = $this->config->getUserValue( |
144
|
2 |
|
$this->userId, |
145
|
2 |
|
$this->appName, |
146
|
2 |
|
'currentView', |
147
|
2 |
|
'month' |
148
|
|
|
); |
149
|
1 |
|
} catch(\Exception $e) { |
150
|
1 |
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
return new JSONResponse([ |
154
|
1 |
|
'value' => $view, |
155
|
|
|
]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* check if view is allowed |
160
|
|
|
* |
161
|
|
|
* @param $view |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
5 |
|
private function isViewAllowed($view) { |
165
|
|
|
$allowedViews = [ |
166
|
5 |
|
'agendaDay', |
167
|
|
|
'agendaWeek', |
168
|
|
|
'month', |
169
|
|
|
]; |
170
|
|
|
|
171
|
5 |
|
return in_array($view, $allowedViews); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* set if popover shall be skipped |
176
|
|
|
* |
177
|
|
|
* @param $value |
178
|
|
|
* @return JSONResponse |
179
|
|
|
*/ |
180
|
4 |
View Code Duplication |
private function setSkipPopover($value) { |
|
|
|
|
181
|
4 |
|
if (!$this->isSkipPopoverValueAllowed($value)) { |
182
|
1 |
|
return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
try { |
186
|
3 |
|
$this->config->setUserValue( |
187
|
3 |
|
$this->userId, |
188
|
3 |
|
$this->appName, |
189
|
3 |
|
'skipPopover', |
190
|
3 |
|
$value |
191
|
|
|
); |
192
|
1 |
|
} catch(\Exception $e) { |
193
|
1 |
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
194
|
|
|
} |
195
|
|
|
|
196
|
2 |
|
return new JSONResponse(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* get if popover shall be skipped |
201
|
|
|
* |
202
|
|
|
* @return JSONResponse |
203
|
|
|
*/ |
204
|
2 |
View Code Duplication |
private function getSkipPopover() { |
|
|
|
|
205
|
|
|
try { |
206
|
2 |
|
$view = $this->config->getUserValue( |
207
|
2 |
|
$this->userId, |
208
|
2 |
|
$this->appName, |
209
|
2 |
|
'skipPopover', |
210
|
2 |
|
'no' |
211
|
|
|
); |
212
|
1 |
|
} catch(\Exception $e) { |
213
|
1 |
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
return new JSONResponse([ |
217
|
1 |
|
'value' => $view, |
218
|
|
|
]); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* check if value for skipPopover is allowed |
223
|
|
|
* |
224
|
|
|
* @param $value |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
4 |
|
private function isSkipPopoverValueAllowed($value) { |
228
|
|
|
$allowedValues = [ |
229
|
4 |
|
'yes', |
230
|
|
|
'no' |
231
|
|
|
]; |
232
|
|
|
|
233
|
4 |
|
return in_array($value, $allowedValues); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* set config value for showing week numbers |
238
|
|
|
* |
239
|
|
|
* @param $value |
240
|
|
|
* @return JSONResponse |
241
|
|
|
*/ |
242
|
|
View Code Duplication |
private function setShowWeekNr($value) { |
|
|
|
|
243
|
|
|
if (!$this->isShowWeekNrValueAllowed($value)) { |
244
|
|
|
return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
try { |
248
|
|
|
$this->config->setUserValue( |
249
|
|
|
$this->userId, |
250
|
|
|
$this->appName, |
251
|
|
|
'showWeekNr', |
252
|
|
|
$value |
253
|
|
|
); |
254
|
|
|
} catch(\Exception $e) { |
255
|
|
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return new JSONResponse(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* get config value for showing week numbers |
263
|
|
|
* |
264
|
|
|
* @return JSONResponse |
265
|
|
|
*/ |
266
|
|
View Code Duplication |
private function getShowWeekNr() { |
|
|
|
|
267
|
|
|
try { |
268
|
|
|
$value = $this->config->getUserValue( |
269
|
|
|
$this->userId, |
270
|
|
|
$this->appName, |
271
|
|
|
'showWeekNr', |
272
|
|
|
'no' |
273
|
|
|
); |
274
|
|
|
} catch(\Exception $e) { |
275
|
|
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return new JSONResponse([ |
279
|
|
|
'value' => $value, |
280
|
|
|
]); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* check if value for showWeekNr is allowed |
285
|
|
|
* |
286
|
|
|
* @param $value |
287
|
|
|
* @return bool |
288
|
|
|
*/ |
289
|
|
|
private function isShowWeekNrValueAllowed($value) { |
290
|
|
|
$allowedValues = [ |
291
|
|
|
'yes', |
292
|
|
|
'no' |
293
|
|
|
]; |
294
|
|
|
|
295
|
|
|
return in_array($value, $allowedValues); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* remember that first run routines executed |
300
|
|
|
* |
301
|
|
|
* @return JSONResponse |
302
|
|
|
*/ |
303
|
2 |
View Code Duplication |
private function setFirstRun() { |
|
|
|
|
304
|
|
|
try { |
305
|
2 |
|
$this->config->setUserValue( |
306
|
2 |
|
$this->userId, |
307
|
2 |
|
$this->appName, |
308
|
2 |
|
'firstRun', |
309
|
2 |
|
'no' |
310
|
|
|
); |
311
|
1 |
|
} catch(\Exception $e) { |
312
|
1 |
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
313
|
|
|
} |
314
|
|
|
|
315
|
1 |
|
return new JSONResponse(); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* get stored value for first run |
320
|
|
|
* |
321
|
|
|
* @return JSONResponse |
322
|
|
|
*/ |
323
|
2 |
View Code Duplication |
private function getFirstRun() { |
|
|
|
|
324
|
|
|
try { |
325
|
2 |
|
$value = $this->config->getUserValue( |
326
|
2 |
|
$this->userId, |
327
|
2 |
|
$this->appName, |
328
|
2 |
|
'firstRun', |
329
|
2 |
|
'yes' |
330
|
|
|
); |
331
|
1 |
|
} catch(\Exception $e) { |
332
|
1 |
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
333
|
|
|
} |
334
|
|
|
|
335
|
1 |
|
return new JSONResponse([ |
336
|
1 |
|
'value' => $value, |
337
|
|
|
]); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
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.