|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OCA\Notes\Service; |
|
4
|
|
|
|
|
5
|
|
|
use OCP\IConfig; |
|
6
|
|
|
|
|
7
|
|
|
class SettingsService { |
|
8
|
|
|
|
|
9
|
|
|
private $config; |
|
10
|
|
|
private $root; |
|
11
|
|
|
|
|
12
|
|
|
/* Default values */ |
|
13
|
|
|
private $defaults = [ |
|
14
|
|
|
'notesPath' => 'Notes', |
|
15
|
|
|
'fileSuffix' => '.txt', |
|
16
|
|
|
]; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct( |
|
19
|
|
|
IConfig $config |
|
20
|
|
|
) { |
|
21
|
|
|
$this->config = $config; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @throws \OCP\PreConditionNotMetException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function set($uid, $settings) { |
|
28
|
|
|
// remove illegal, empty and default settings |
|
29
|
|
|
foreach ($settings as $name => $value) { |
|
30
|
|
|
if (!array_key_exists($name, $this->defaults) |
|
31
|
|
|
|| empty($value) |
|
32
|
|
|
|| $value === $this->defaults[$name] |
|
33
|
|
|
) { |
|
34
|
|
|
unset($settings[$name]); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
$this->config->setUserValue($uid, 'notes', 'settings', json_encode($settings)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getAll($uid) { |
|
41
|
|
|
$settings = json_decode($this->config->getUserValue($uid, 'notes', 'settings')); |
|
42
|
|
|
if (is_object($settings)) { |
|
43
|
|
|
// use default for empty settings |
|
44
|
|
|
foreach ($this->defaults as $name => $defaultValue) { |
|
45
|
|
|
if (!property_exists($settings, $name) || empty($settings->{$name})) { |
|
46
|
|
|
$settings->{$name} = $defaultValue; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} else { |
|
50
|
|
|
$settings = (object)$this->defaults; |
|
51
|
|
|
} |
|
52
|
|
|
return $settings; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @throws \OCP\PreConditionNotMetException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function get($uid, $name) { |
|
59
|
|
|
$settings = $this->getAll($uid); |
|
60
|
|
|
if (property_exists($settings, $name)) { |
|
61
|
|
|
return $settings->{$name}; |
|
62
|
|
|
} else { |
|
63
|
|
|
throw new \OCP\PreConditionNotMetException('Setting '.$name.' not found for user '.$uid.'.'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|