From 9eb6e034fad374fb76b81003fb52d27cc6f34225 Mon Sep 17 00:00:00 2001 From: John Wesely Date: Fri, 10 May 2024 12:50:11 -0600 Subject: [PATCH] guest entries and user editable redirect urls --- UPGRADE.md | 20 +++++++++ ...eate_dynamic_filament_form_tables.php.stub | 2 +- src/Exports/FilamentFormUsersExport.php | 2 +- .../Resources/FilamentFormResource.php | 15 ++++--- .../FilamentFormUsersRelationManager.php | 2 +- src/Livewire/FilamentForm/Show.php | 44 +++++++++++++------ 6 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 UPGRADE.md diff --git a/UPGRADE.md b/UPGRADE.md new file mode 100644 index 0000000..fa85bed --- /dev/null +++ b/UPGRADE.md @@ -0,0 +1,20 @@ +# Upgrade Guide +## Upgrading to to 1.1 from 1.0 +### Allow Guest Entries by Making User Nullable on FilamentFormUser +1.1 supports nullable user ids so that guest data can be collected by forms. If you are upgrading from 1.0 to 1.1, create a migration with the following methods to reflect this change. +``` + Schema::table('filament_form_user', function (Blueprint $table) { + $table->foreignId('user_id')->nullable()->change(); + }); + + Schema::table('filament_forms', function (Blueprint $table) { + $table->boolean('permit_guest_entries')->default(false); + }); +``` +### Support user configurable redirect URL +1.1 supports user configurable redirect URLs. When a redirect URL is present on the form model, the user will be redirected there instead of the redirect URL specified in the config. If you are upgrading from 1.0 to 1.1, create a migration with the following method to reflect this change. +``` + Schema::table('filament_forms', function (Blueprint $table) { + $table->text('redirect_url')->nullable(); + }); +``` diff --git a/database/migrations/create_dynamic_filament_form_tables.php.stub b/database/migrations/create_dynamic_filament_form_tables.php.stub index 21a4bdb..5e7b035 100644 --- a/database/migrations/create_dynamic_filament_form_tables.php.stub +++ b/database/migrations/create_dynamic_filament_form_tables.php.stub @@ -35,7 +35,7 @@ return new class extends Migration $table->id(); $table->timestamps(); $table->foreignId('filament_form_id')->constrained()->cascadeOnDelete(); - $table->foreignId('user_id')->constrained()->cascadeOnDelete(); + $table->foreignId('user_id')->nullable()->constrained()->cascadeOnDelete(); $table->json('entry'); }); } diff --git a/src/Exports/FilamentFormUsersExport.php b/src/Exports/FilamentFormUsersExport.php index a62bb2e..6b3557c 100644 --- a/src/Exports/FilamentFormUsersExport.php +++ b/src/Exports/FilamentFormUsersExport.php @@ -31,7 +31,7 @@ public function collection() public function map($entry): array { $mapping = [ - $entry->user->name, + $entry->user->name ?? 'Guest', $entry->created_at, $entry->updated_at, ]; diff --git a/src/Filament/Resources/FilamentFormResource.php b/src/Filament/Resources/FilamentFormResource.php index 0941a7f..854b509 100644 --- a/src/Filament/Resources/FilamentFormResource.php +++ b/src/Filament/Resources/FilamentFormResource.php @@ -3,16 +3,17 @@ namespace Tapp\FilamentFormBuilder\Filament\Resources; use Filament\Forms; -use Filament\Forms\Form; -use Filament\Resources\Resource; use Filament\Tables; +use Filament\Forms\Form; use Filament\Tables\Table; -use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\Pages\CreateFilamentForm; +use Filament\Resources\Resource; +use Filament\Forms\Components\Toggle; +use Tapp\FilamentFormBuilder\Models\FilamentForm; use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\Pages\EditFilamentForm; use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\Pages\ListFilamentForms; -use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\RelationManagers\FilamentFormFieldsRelationManager; +use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\Pages\CreateFilamentForm; use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\RelationManagers\FilamentFormUsersRelationManager; -use Tapp\FilamentFormBuilder\Models\FilamentForm; +use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\RelationManagers\FilamentFormFieldsRelationManager; class FilamentFormResource extends Resource { @@ -52,6 +53,10 @@ public static function form(Form $form): Form Forms\Components\TextInput::make('name') ->required() ->maxLength(255), + Toggle::make('permit_guest_entries') + ->hint('Permit non registered users to submit this form'), + Forms\Components\TextInput::make('redirect_url') + ->hint('(optional) complete this field to provide a custom redirect url on form completion. Use a fully qualified URL including "https://" to redirect to an external link, otherwise url will be relative to this sites domain'), Forms\Components\Textarea::make('description') ->columnSpanFull(), ]); diff --git a/src/Filament/Resources/FilamentFormResource/RelationManagers/FilamentFormUsersRelationManager.php b/src/Filament/Resources/FilamentFormResource/RelationManagers/FilamentFormUsersRelationManager.php index 5e575ed..18c273a 100644 --- a/src/Filament/Resources/FilamentFormResource/RelationManagers/FilamentFormUsersRelationManager.php +++ b/src/Filament/Resources/FilamentFormResource/RelationManagers/FilamentFormUsersRelationManager.php @@ -66,7 +66,7 @@ public function table(Table $table): Table BulkAction::make('Export Selected') ->action(fn (Collection $records) => Excel::download( new FilamentFormUsersExport($records), - $this->getOwnerRecord()->name.'_form_entry_export'.now()->format('Y-m-dhis').'.csv') + urlencode($this->getOwnerRecord()->name).'_form_entry_export'.now()->format('Y-m-dhis').'.csv') ) ->icon('heroicon-o-document-chart-bar') ->deselectRecordsAfterCompletion(), diff --git a/src/Livewire/FilamentForm/Show.php b/src/Livewire/FilamentForm/Show.php index 159fd64..bbf2795 100644 --- a/src/Livewire/FilamentForm/Show.php +++ b/src/Livewire/FilamentForm/Show.php @@ -19,9 +19,13 @@ class Show extends Component implements HasForms public ?array $data = []; - public function mount(FilamentForm $form): void + public function mount(FilamentForm $form) { $this->filamentForm = $form->load('filamentFormFields'); + + if (!$this->filamentForm->permit_guest_entries && !auth()->check()) { + return redirect('/', 401); + } } public function form(Form $form): Form @@ -92,18 +96,32 @@ public function create() ]); } - $entryModel = FilamentFormUser::updateOrCreate( - [ - 'user_id' => auth()->user()->id, - 'filament_form_id' => $this->filamentForm->id, - ], - [ - 'entry' => $entry, - ], - ); - - return redirect() - ->route(config('filament-form-builder.filament-form-user-show-route'), $entryModel); + if (auth()->check()) { + $entryModel = FilamentFormUser::updateOrCreate( + [ + 'user_id' => auth()->user()->id ?? null, + 'filament_form_id' => $this->filamentForm->id, + ], + [ + 'entry' => $entry, + ], + ); + } else { + $entryModel = FilamentFormUser::create( + [ + 'filament_form_id' => $this->filamentForm->id, + 'entry' => $entry, + ], + ); + } + + if ($this->filamentForm->redirect_url) { + return redirect($this->filamentForm->redirect_url); + } else { + return redirect() + ->route(config('filament-form-builder.filament-form-user-show-route'), $entryModel); + } + } public function parseValue(FilamentFormField $field, ?string $value): string