diff --git a/app/Filament/Pages/Settings.php b/app/Filament/Pages/Settings.php new file mode 100644 index 000000000..7b0efa79a --- /dev/null +++ b/app/Filament/Pages/Settings.php @@ -0,0 +1,203 @@ +fillForm(); + } + + protected function fillForm(): void + { + $this->callHook('beforeFill'); + + $settings = app(SettingRepository::class)->where('type', '!=', 'hidden')->orderBy('order')->get(); + + $data = $this->mutateFormDataBeforeFill($settings->toArray()); + + $this->form->fill($data); + + $this->callHook('afterFill'); + } + + protected function mutateFormDataBeforeFill(array $data): array + { + $newData = []; + + foreach ($data as $setting) { + $newData[$setting['key']] = $setting['value']; + } + + return Arr::undot($newData); + } + + public function save(): void + { + try { + $this->callHook('beforeValidate'); + + $data = $this->form->getState(); + + $this->callHook('afterValidate'); + + $data = $this->mutateFormDataBeforeSave($data); + + $this->callHook('beforeSave'); + + foreach ($data as $key => $value) { + app(SettingRepository::class)->store($key, $value); + + $cache = config('cache.keys.SETTINGS'); + Cache::forget($cache['key'].$key); + } + + app(FinanceService::class)->changeJournalCurrencies(); + + $this->callHook('afterSave'); + + $this->getSavedNotification()?->send(); + + if ($redirectUrl = $this->getRedirectUrl()) { + $this->redirect($redirectUrl); + } + } catch (Halt $exception) { + return; + } + } + + protected function mutateFormDataBeforeSave(array $data): array + { + return Arr::dot($data); + } + + public function getSavedNotification(): ?Notification + { + return Notification::make()->success()->title('Settings saved successfully'); + } + + public function getFormActions() + { + return [ + Action::make('save')->label('Save')->submit('save')->keyBindings(['mod+s']) + ]; + } + + public function form(Form $form): Form + { + return $form; + } + + protected function getForms(): array + { + return [ + 'form' => $this->form( + $this->makeForm() + ->schema($this->getFormSchema()) + ->statePath('data') + ->columns(2) + ->inlineLabel($this->hasInlineLabels()), + ), + ]; + } + + + protected function getFormSchema(): array + { + $schema = []; + + $grouped_settings = app(SettingRepository::class)->where('type', '!=', 'hidden')->orderBy('order')->get(); + foreach ($grouped_settings->groupBy('group') as $group => $settings) { + $schema[] = Section::make(Str::ucfirst($group))->schema( + $settings->map(function ($setting) { + if ($setting->type === 'date') { + return DatePicker::make($setting->key)->label($setting->name)->helperText($setting->description)->format('Y-m-d'); + } else if ($setting->type === 'boolean' || $setting->type === 'bool') { + return Toggle::make($setting->key)->label($setting->name)->helperText($setting->description)->offIcon('heroicon-m-x-circle')->offColor('danger')->onIcon('heroicon-m-check-circle')->onColor('success'); + } else if ($setting->type === 'int') { + return TextInput::make($setting->key)->label($setting->name)->helperText($setting->description)->integer(); + } else if ($setting->type === 'number') { + return TextInput::make($setting->key)->label($setting->name)->helperText($setting->description)->numeric()->step(0.01); + } else if ($setting->type === 'select') { + if ($setting->id === 'general_theme') { + return Select::make($setting->key)->label($setting->name)->helperText($setting->description)->options(list_to_assoc($this->getThemes())); + } else if ($setting->id === 'units_currency') { + return Select::make($setting->key)->label($setting->name)->helperText($setting->description)->options(list_to_assoc($this->getCurrencyList())); + } else { + return Select::make($setting->key)->label($setting->name)->helperText($setting->description)->options(list_to_assoc(explode(',', $setting->options))); + } + } else { + return TextInput::make($setting->key)->label($setting->name)->helperText($setting->description)->string(); + } + })->toArray() + ); + } + + return $schema; + } + + public function getRedirectUrl(): ?string + { + return null; + } + + private function getThemes(): array + { + Theme::rebuildCache(); + $themes = Theme::all(); + $theme_list = []; + foreach ($themes as $t) { + if (!$t || !$t->name || $t->name === 'false') { + continue; + } + $theme_list[] = $t->name; + } + + return $theme_list; + } + + private function getCurrencyList(): array + { + $curr = []; + foreach (config('money') as $currency => $attrs) { + $name = $attrs['name'].' ('.$attrs['symbol'].'/'.$currency.')'; + $curr[$currency] = $name; + } + + return $curr; + } + +} diff --git a/resources/views/filament/pages/settings.blade.php b/resources/views/filament/pages/settings.blade.php new file mode 100644 index 000000000..e2116b5a5 --- /dev/null +++ b/resources/views/filament/pages/settings.blade.php @@ -0,0 +1,10 @@ + + + {{ $this->form }} + + + +