Skip to content

Commit

Permalink
guest entries and user editable redirect urls
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wesely committed May 10, 2024
1 parent e167cf5 commit 9eb6e03
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 21 deletions.
20 changes: 20 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -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();
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exports/FilamentFormUsersExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down
15 changes: 10 additions & 5 deletions src/Filament/Resources/FilamentFormResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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(),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
44 changes: 31 additions & 13 deletions src/Livewire/FilamentForm/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9eb6e03

Please sign in to comment.