1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nextcloud - nextnote |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016, Sander Brand ([email protected]) |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\NextNote\Service; |
25
|
|
|
|
26
|
|
|
use OCP\IConfig; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class SettingsService { |
30
|
|
|
|
31
|
|
|
private $userId; |
32
|
|
|
private $config; |
33
|
|
|
private $appName; |
34
|
|
|
public $settings; |
35
|
|
|
public $userSettings; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
private $numeric_settings = array( |
39
|
|
|
'link_sharing_enabled', |
40
|
|
|
|
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
public function __construct($UserId, IConfig $config, $AppName) { |
44
|
|
|
$this->userId = $UserId; |
45
|
|
|
$this->config = $config; |
46
|
|
|
$this->appName = $AppName; |
47
|
|
|
$this->settings = array( |
48
|
|
|
'folder' => $this->config->getAppValue('nextnote', 'folder', ''), |
49
|
|
|
'sharemode' => $this->config->getAppValue('nextnote', 'sharemode', 'merge'), |
50
|
|
|
|
51
|
|
|
); |
52
|
|
|
$this->userSettings = array( |
53
|
|
|
'view_mode' => $this->config->getUserValue($this->userId, 'nextnote', 'view_mode', 'col'), // single|col |
54
|
|
|
'first_user' => $this->config->getUserValue($this->userId, 'nextnote', 'first_user', 1), |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get all settings |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getSettings() { |
64
|
|
|
$settings = [ |
65
|
|
|
'app' => $this->settings, |
66
|
|
|
'user' => $this->userSettings |
67
|
|
|
]; |
68
|
|
|
return $settings; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get all app settings |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getAppSettings() { |
78
|
|
|
return $this->settings; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get all user settings |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getUserSettings() { |
87
|
|
|
return $this->userSettings; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get user setting |
92
|
|
|
* |
93
|
|
|
* @param $key |
94
|
|
|
* @param string $default |
95
|
|
|
* @return string|int |
96
|
|
|
*/ |
97
|
|
|
public function getUserSetting($key, $default = '') { |
98
|
|
|
return $this->config->getUserValue($this->userId, 'nextnote', $key, $default); // single|col |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get a app setting |
103
|
|
|
* |
104
|
|
|
* @param $key string |
105
|
|
|
* @param null $default_value The default value if key does not exist |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
|
public function getAppSetting($key, $default_value = null) { |
109
|
|
|
$value = ($this->settings[$key]) ? $this->settings[$key] : $this->config->getAppValue('nextnote', $key, $default_value); |
110
|
|
|
if (in_array($key, $this->numeric_settings)) { |
111
|
|
|
$value = intval($value); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $value; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set a app setting |
119
|
|
|
* |
120
|
|
|
* @param $key string Setting name |
121
|
|
|
* @param $value mixed Value of the setting |
122
|
|
|
*/ |
123
|
|
|
public function setAppSetting($key, $value) { |
124
|
|
|
$this->settings[$key] = $value; |
125
|
|
|
$this->config->setAppValue('nextnote', $key, $value); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set a user setting |
130
|
|
|
* |
131
|
|
|
* @param $key string Setting name |
132
|
|
|
* @param $value mixed Value of the setting |
133
|
|
|
*/ |
134
|
|
|
|
135
|
|
|
public function setUserSetting($key, $value) { |
136
|
|
|
$this->userSettings[$key] = $value; |
137
|
|
|
return $this->config->setUserValue($this->userId, 'nextnote', $key, $value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Check if an setting is enabled (value of 1) |
142
|
|
|
* |
143
|
|
|
* @param $setting |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isEnabled($setting) { |
147
|
|
|
$value = intval($this->getAppSetting($setting, false)); |
148
|
|
|
return ($value === 1); |
149
|
|
|
} |
150
|
|
|
} |