Completed
Push — master ( 0d9acd...172550 )
by Sander
9s
created

SettingsService::gettUserSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Nextcloud - nextnote
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
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
		);
55
	}
56
57
	/**
58
	 * Get all settings
59
	 *
60
	 * @return array
61
	 */
62
	public function getSettings() {
63
		$settings = [
64
			'app' => $this->settings,
65
			'user' => $this->userSettings
66
		];
67
		return $settings;
68
	}
69
70
71
	/**
72
	 * Get all app settings
73
	 *
74
	 * @return array
75
	 */
76
	public function getAppSettings() {
77
		return $this->settings;
78
	}
79
80
	/**
81
	 * Get all user settings
82
	 *
83
	 * @return array
84
	 */
85
	public function gettUserSettings() {
86
		return $this->userSettings;
87
	}
88
89
	/**
90
	 * Get a app setting
91
	 *
92
	 * @param $key string
93
	 * @param null $default_value The default value if key does not exist
94
	 * @return mixed
95
	 */
96
	public function getAppSetting($key, $default_value = null) {
97
		$value = ($this->settings[$key]) ? $this->settings[$key] : $this->config->getAppValue('nextnote', $key, $default_value);
98
		if (in_array($key, $this->numeric_settings)) {
99
			$value = intval($value);
100
		}
101
102
		return $value;
103
	}
104
105
	/**
106
	 * Set a app setting
107
	 *
108
	 * @param $key string Setting name
109
	 * @param $value mixed Value of the setting
110
	 */
111
	public function setAppSetting($key, $value) {
112
		$this->settings[$key] = $value;
113
		$this->config->setAppValue('nextnote', $key, $value);
114
	}
115
116
	/**
117
	 * Set a user setting
118
	 *
119
	 * @param $key string Setting name
120
	 * @param $value mixed Value of the setting
121
	 */
122
123
	public function setUserSetting($key, $value) {
124
		return $this->config->setUserValue($this->userId, 'nextnote', $key, $value);
125
	}
126
127
	/**
128
	 * Check if an setting is enabled (value of 1)
129
	 *
130
	 * @param $setting
131
	 * @return bool
132
	 */
133
	public function isEnabled($setting) {
134
		$value = intval($this->getAppSetting($setting, false));
135
		return ($value === 1);
136
	}
137
}