record = AppearanceModel::firstOrNew([ 'company_id' => auth()->user()->currentCompany->id, ]); abort_unless(static::canView($this->record), 404); $this->fillForm(); } public function fillForm(): void { $data = $this->record->attributesToArray(); $this->form->fill($data); } public function save(): void { try { $data = $this->form->getState(); $this->handleRecordUpdate($this->record, $data); } catch (Halt $exception) { return; } $this->getSavedNotification()->send(); } protected function getSavedNotification(): Notification { return Notification::make() ->success() ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title')); } public function form(Form $form): Form { return $form ->schema([ $this->getGeneralSection(), $this->getLayoutSection(), $this->getDataPresentationSection(), ]) ->model($this->record) ->statePath('data') ->operation('edit'); } protected function getGeneralSection(): Component { return Section::make('General') ->schema([ Select::make('primary_color') ->allowHtml() ->softRequired() ->localizeLabel() ->options( collect(PrimaryColor::cases()) ->mapWithKeys(static fn ($case) => [ $case->value => " " . $case->getLabel() . ' ', ]), ), Select::make('font') ->allowHtml() ->softRequired() ->localizeLabel() ->options( collect(Font::cases()) ->mapWithKeys(static fn ($case) => [ $case->value => "{$case->getLabel()}", ]), ), ])->columns(); } protected function getLayoutSection(): Component { return Section::make('Layout') ->schema([ Select::make('max_content_width') ->softRequired() ->localizeLabel() ->options(MaxContentWidth::class), Select::make('modal_width') ->softRequired() ->localizeLabel() ->options(ModalWidth::class), Select::make('has_top_navigation') ->localizeLabel('Navigation Layout') ->selectablePlaceholder(false) ->boolean(translate('Top Navigation'), translate('Side Navigation')), ToggleButton::make('is_table_striped') ->localizeLabel('Striped Tables') ->onLabel(translate('Enabled')) ->offLabel(translate('Disabled')), ])->columns(); } protected function getDataPresentationSection(): Component { return Section::make('Data Presentation') ->schema([ Select::make('table_sort_direction') ->softRequired() ->localizeLabel() ->options(TableSortDirection::class), Select::make('records_per_page') ->softRequired() ->localizeLabel() ->options(RecordsPerPage::class), ])->columns(); } protected function handleRecordUpdate(AppearanceModel $record, array $data): AppearanceModel { $record->fill($data); $keysToWatch = [ 'primary_color', 'max_content_width', 'has_top_navigation', 'font', ]; if ($record->isDirty($keysToWatch)) { $this->dispatch('appearanceUpdated'); } $record->save(); return $record; } /** * @return array */ protected function getFormActions(): array { return [ $this->getSaveFormAction(), ]; } protected function getSaveFormAction(): Action { return Action::make('save') ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label')) ->submit('save') ->keyBindings(['mod+s']); } public static function canView(Model $record): bool { try { return authorize('update', $record)->allowed(); } catch (AuthorizationException $exception) { return $exception->toResponse()->allowed(); } } }