pluginhost: add profile_get/profile_set helpers

This commit is contained in:
Andrew Dolgov 2021-04-09 14:01:30 +03:00
parent a5af15cfe9
commit a61348e2b7
1 changed files with 43 additions and 0 deletions

View File

@ -469,6 +469,29 @@ class PluginHost {
} }
} }
// same as set(), but sets data to current preference profile
function profile_set(Plugin $sender, string $name, $value) {
$profile_id = $_SESSION["profile"] ?? null;
if ($profile_id) {
$idx = get_class($sender);
if (!isset($this->storage[$idx])) {
$this->storage[$idx] = [];
}
if (!isset($this->storage[$idx][$profile_id])) {
$this->storage[$idx][$profile_id] = [];
}
$this->storage[$idx][$profile_id][$name] = $value;
$this->save_data(get_class($sender));
} else {
return $this->set($sender, $name, $value);
}
}
function set(Plugin $sender, string $name, $value) { function set(Plugin $sender, string $name, $value) {
$idx = get_class($sender); $idx = get_class($sender);
@ -492,6 +515,26 @@ class PluginHost {
$this->save_data(get_class($sender)); $this->save_data(get_class($sender));
} }
// same as get(), but sets data to current preference profile
function profile_get(Plugin $sender, string $name, $default_value = false) {
$profile_id = $_SESSION["profile"] ?? null;
if ($profile_id) {
$idx = get_class($sender);
$this->load_data();
if (isset($this->storage[$idx][$profile_id][$name])) {
return $this->storage[$idx][$profile_id][$name];
} else {
return $default_value;
}
} else {
return $this->get($sender, $name, $default_value);
}
}
function get(Plugin $sender, string $name, $default_value = false) { function get(Plugin $sender, string $name, $default_value = false) {
$idx = get_class($sender); $idx = get_class($sender);