nextcloud-notes/lib/Service/SettingsService.php

96 lines
2.2 KiB
PHP
Raw Normal View History

2020-07-13 08:27:52 +02:00
<?php
declare(strict_types=1);
2018-07-03 21:53:46 +02:00
namespace OCA\Notes\Service;
2019-05-25 08:15:05 +02:00
2018-07-03 21:53:46 +02:00
use OCP\IConfig;
2020-06-28 19:04:59 +02:00
use OCP\IL10N;
use OCP\Files\IRootFolder;
2018-07-03 21:53:46 +02:00
2019-05-25 08:15:05 +02:00
class SettingsService {
2018-07-03 21:53:46 +02:00
private $config;
2020-06-28 19:04:59 +02:00
private $l10n;
2018-07-03 21:53:46 +02:00
private $root;
/* Default values */
2020-06-28 19:04:59 +02:00
private $defaults;
2018-07-03 21:53:46 +02:00
public function __construct(
2020-06-28 19:04:59 +02:00
IConfig $config,
IL10N $l10n,
IRootFolder $root
2018-07-03 21:53:46 +02:00
) {
$this->config = $config;
2020-06-28 19:04:59 +02:00
$this->l10n = $l10n;
$this->root = $root;
$this->defaults = [
'fileSuffix' => '.txt',
'notesPath' => function (string $uid) {
return $this->getDefaultNotesPath($uid);
},
];
}
private function getDefaultNotesPath(string $uid) : string {
$defaultFolder = 'Notes';
$defaultExists = $this->root->getUserFolder($uid)->nodeExists($defaultFolder);
if ($defaultExists) {
return $defaultFolder;
} else {
return $this->l10n->t($defaultFolder);
}
2018-07-03 21:53:46 +02:00
}
/**
* @throws \OCP\PreConditionNotMetException
*/
2020-04-12 19:26:57 +02:00
public function set(string $uid, array $settings) : void {
2018-07-24 21:34:05 +02:00
// remove illegal, empty and default settings
2019-05-25 08:15:05 +02:00
foreach ($settings as $name => $value) {
if (!array_key_exists($name, $this->defaults)
2018-07-24 21:34:05 +02:00
|| empty($value)
|| $value === $this->defaults[$name]
) {
unset($settings[$name]);
}
2018-07-03 21:53:46 +02:00
}
$this->config->setUserValue($uid, 'notes', 'settings', json_encode($settings));
2018-07-03 21:53:46 +02:00
}
2020-04-12 19:26:57 +02:00
public function getAll(string $uid) : \stdClass {
$settings = json_decode($this->config->getUserValue($uid, 'notes', 'settings'));
2020-06-28 19:04:59 +02:00
if (!is_object($settings)) {
$settings = new \stdClass();
}
// use default for empty settings
$toBeSaved = false;
foreach ($this->defaults as $name => $defaultValue) {
if (!property_exists($settings, $name) || empty($settings->{$name})) {
if (is_callable($defaultValue)) {
$settings->{$name} = $defaultValue($uid);
$toBeSaved = true;
} else {
2018-07-24 21:34:05 +02:00
$settings->{$name} = $defaultValue;
}
}
2020-06-28 19:04:59 +02:00
}
if ($toBeSaved) {
$this->set($uid, (array) $settings);
2018-07-24 21:34:05 +02:00
}
return $settings;
}
/**
* @throws \OCP\PreConditionNotMetException
*/
2020-04-12 19:26:57 +02:00
public function get(string $uid, string $name) : string {
$settings = $this->getAll($uid);
2019-05-25 08:15:05 +02:00
if (property_exists($settings, $name)) {
2018-07-24 21:34:05 +02:00
return $settings->{$name};
} else {
throw new \OCP\PreConditionNotMetException('Setting '.$name.' not found for user '.$uid.'.');
2018-07-24 21:34:05 +02:00
}
2018-07-03 21:53:46 +02:00
}
}