X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/34e60986873ab47ef0a9e2447bddb7a5b82b0c13..refs/pull/3918/head:/app/Settings/SettingService.php diff --git a/app/Settings/SettingService.php b/app/Settings/SettingService.php index 310e0ccff..9f0a41ea2 100644 --- a/app/Settings/SettingService.php +++ b/app/Settings/SettingService.php @@ -1,4 +1,6 @@ -getValueFromStore($key) ?? $default; $formatted = $this->formatValue($value, $default); $this->localCache[$key] = $formatted; + return $formatted; } @@ -51,6 +54,7 @@ class SettingService protected function getFromSession(string $key, $default = false) { $value = session()->get($key, $default); + return $this->formatValue($value, $default); } @@ -66,6 +70,7 @@ class SettingService if ($user->isDefault()) { return $this->getFromSession($key, $default); } + return $this->get($this->userKey($user->id, $key), $default); } @@ -101,6 +106,7 @@ class SettingService } $this->cache->forever($cacheKey, $value); + return $value; } @@ -120,14 +126,14 @@ class SettingService } /** - * Format a settings value + * Format a settings value. */ protected function formatValue($value, $default) { // Change string booleans to actual booleans if ($value === 'true') { $value = true; - } else if ($value === 'false') { + } elseif ($value === 'false') { $value = false; } @@ -135,6 +141,7 @@ class SettingService if ($value === '') { $value = $default; } + return $value; } @@ -144,6 +151,7 @@ class SettingService public function has(string $key): bool { $setting = $this->getSettingObjectByKey($key); + return $setting !== null; } @@ -154,7 +162,7 @@ class SettingService public function put(string $key, $value): bool { $setting = $this->setting->newQuery()->firstOrNew([ - 'setting_key' => $key + 'setting_key' => $key, ]); $setting->type = 'string'; @@ -166,6 +174,7 @@ class SettingService $setting->value = $value; $setting->save(); $this->clearFromCache($key); + return true; } @@ -179,22 +188,36 @@ class SettingService $values = collect($value)->values()->filter(function (array $item) { return count(array_filter($item)) > 0; }); + return json_encode($values); } /** * Put a user-specific setting into the database. + * Can only take string value types since this may use + * the session which is less flexible to data types. */ public function putUser(User $user, string $key, string $value): bool { if ($user->isDefault()) { session()->put($key, $value); + return true; } return $this->put($this->userKey($user->id, $key), $value); } + /** + * Put a user-specific setting into the database for the current access user. + * Can only take string value types since this may use + * the session which is less flexible to data types. + */ + public function putForCurrentUser(string $key, string $value) + { + return $this->putUser(user(), $key, $value); + } + /** * Convert a setting key into a user-specific key. */