From 024bd2112378046cdd5d3be70cc52d8bc5315670 Mon Sep 17 00:00:00 2001 From: Bobby Iliev Date: Wed, 13 Nov 2024 17:36:05 +0200 Subject: [PATCH] Fix global settings function --- .../SettingResource/Pages/CreateSetting.php | 6 ++++++ .../SettingResource/Pages/EditSetting.php | 6 ++++++ .../SettingResource/Pages/ListSettings.php | 6 ++++++ .../themes/anchor/partials/footer.blade.php | 2 +- routes/web.php | 2 +- wave/src/Helpers/globals.php | 15 ++++++++++--- wave/src/Setting.php | 21 +++++++++++++++++++ 7 files changed, 53 insertions(+), 5 deletions(-) diff --git a/app/Filament/Resources/SettingResource/Pages/CreateSetting.php b/app/Filament/Resources/SettingResource/Pages/CreateSetting.php index 8395ca527..19051060a 100644 --- a/app/Filament/Resources/SettingResource/Pages/CreateSetting.php +++ b/app/Filament/Resources/SettingResource/Pages/CreateSetting.php @@ -2,6 +2,7 @@ namespace App\Filament\Resources\SettingResource\Pages; +use Illuminate\Support\Facades\Cache; use App\Filament\Resources\SettingResource; use Filament\Actions; use Filament\Resources\Pages\CreateRecord; @@ -9,4 +10,9 @@ class CreateSetting extends CreateRecord { protected static string $resource = SettingResource::class; + + protected function afterCreate(): void + { + Cache::forget('wave_settings'); + } } diff --git a/app/Filament/Resources/SettingResource/Pages/EditSetting.php b/app/Filament/Resources/SettingResource/Pages/EditSetting.php index 3b8ba465e..c3472aeb8 100644 --- a/app/Filament/Resources/SettingResource/Pages/EditSetting.php +++ b/app/Filament/Resources/SettingResource/Pages/EditSetting.php @@ -2,6 +2,7 @@ namespace App\Filament\Resources\SettingResource\Pages; +use Illuminate\Support\Facades\Cache; use App\Filament\Resources\SettingResource; use Filament\Actions; use Filament\Resources\Pages\EditRecord; @@ -16,4 +17,9 @@ protected function getHeaderActions(): array Actions\DeleteAction::make(), ]; } + + protected function afterSave(): void + { + Cache::forget('wave_settings'); + } } diff --git a/app/Filament/Resources/SettingResource/Pages/ListSettings.php b/app/Filament/Resources/SettingResource/Pages/ListSettings.php index aa0afa784..16171e646 100644 --- a/app/Filament/Resources/SettingResource/Pages/ListSettings.php +++ b/app/Filament/Resources/SettingResource/Pages/ListSettings.php @@ -2,6 +2,7 @@ namespace App\Filament\Resources\SettingResource\Pages; +use Illuminate\Support\Facades\Cache; use App\Filament\Resources\SettingResource; use Filament\Actions; use Filament\Resources\Pages\ListRecords; @@ -16,4 +17,9 @@ protected function getHeaderActions(): array Actions\CreateAction::make(), ]; } + + protected function afterDelete(): void + { + Cache::forget('wave_settings'); + } } diff --git a/resources/themes/anchor/partials/footer.blade.php b/resources/themes/anchor/partials/footer.blade.php index a52db96e3..74fb5a49f 100644 --- a/resources/themes/anchor/partials/footer.blade.php +++ b/resources/themes/anchor/partials/footer.blade.php @@ -199,4 +199,4 @@ - \ No newline at end of file + diff --git a/routes/web.php b/routes/web.php index e8da1bb43..902569f18 100644 --- a/routes/web.php +++ b/routes/web.php @@ -19,4 +19,4 @@ Route::get('role', function(){ dd(\App\Models\User::find(2)->roles); -}); \ No newline at end of file +}); diff --git a/wave/src/Helpers/globals.php b/wave/src/Helpers/globals.php index 2e419ee5c..b888a09a1 100644 --- a/wave/src/Helpers/globals.php +++ b/wave/src/Helpers/globals.php @@ -1,13 +1,22 @@ toArray(); + }); + } + + // Return the requested setting or default value if not found + return $settingsCache[$key] ?? $default; } } @@ -83,4 +92,4 @@ function get_default_billing_cycle(){ // Return null or a default value if neither is present return 'Monthly'; // or any default value you prefer } -} \ No newline at end of file +} diff --git a/wave/src/Setting.php b/wave/src/Setting.php index a6fbdd9e5..d467d9bd2 100644 --- a/wave/src/Setting.php +++ b/wave/src/Setting.php @@ -2,6 +2,7 @@ namespace Wave; +use Illuminate\Support\Facades\Cache; use Illuminate\Database\Eloquent\Model; class Setting extends Model @@ -12,4 +13,24 @@ class Setting extends Model public $timestamps = false; + protected static function booted() + { + static::saved(function () { + Cache::forget('wave_settings'); + }); + + static::deleted(function () { + Cache::forget('wave_settings'); + }); + } + + public static function get($key, $default = null) + { + $settings = Cache::rememberForever('wave_settings', function () { + return self::pluck('value', 'key')->toArray(); + }); + + return $settings[$key] ?? $default; + } + }