Skip to content

Commit

Permalink
Merge pull request #3 from TappNetwork/add_guest_entries_and_redirect…
Browse files Browse the repository at this point in the history
…_route

Add guest entries and redirect route
  • Loading branch information
johnwesely authored Jun 5, 2024
2 parents 0d56d4e + bbd1acf commit 12f6aa3
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
coverage: none

- name: Install composer dependencies
Expand Down
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();
});
```
2 changes: 2 additions & 0 deletions config/filament-form-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
return [
'filament-form-user-show-route' => 'filament-form-users.show',

'filament-form-show-route' => 'filament-form-builder.show',

'resources' => [
'FilamentFormResource' => FilamentFormResource::class,
],
Expand Down
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
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ parameters:
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false

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
42 changes: 34 additions & 8 deletions src/Filament/Resources/FilamentFormResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
namespace Tapp\FilamentFormBuilder\Filament\Resources;

use Filament\Forms;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\Pages\CreateFilamentForm;
use Tapp\FilamentFormBuilder\Filament\Resources\FilamentFormResource\Pages\EditFilamentForm;
Expand All @@ -20,6 +25,11 @@ class FilamentFormResource extends Resource

protected static ?int $navigationSort = 99;

public static function getBreadcrumb(): string
{
return config('filament-form-builder.admin-panel-resource-name-plural');
}

public static function getNavigationGroup(): ?string
{
return config('filament-form-builder.admin-panel-group-name');
Expand Down Expand Up @@ -47,6 +57,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 All @@ -56,28 +70,40 @@ public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('created_at')
TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('name')
TextColumn::make('name')
->sortable()
->searchable(),
TextColumn::make('form_link')
->copyable()
->copyMessage('Form link copied to clipboard')
->copyMessageDuration(1500),
IconColumn::make('permit_guest_entries')
->sortable()
->getStateUsing(function ($record) {
return (bool) $record->permit_guest_entries;
})
->boolean(),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
])
->emptyStateHeading('No '.config('filament-form-builder.admin-panel-resource-name-plural'));
}

public static function getRelations(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ class ListFilamentForms extends ListRecords

public function getTitle(): string
{
return config('filament-form-builder.admin-panel-resource-name');
return config('filament-form-builder.admin-panel-resource-name-plural');
}

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
Actions\CreateAction::make()
->label('Create '.config('filament-form-builder.admin-panel-resource-name')),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Actions\BulkAction;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Maatwebsite\Excel\Facades\Excel;
Expand Down Expand Up @@ -53,7 +55,10 @@ public function table(Table $table): Table
])
->recordUrl(fn ($record) => route(config('filament-form-builder.filament-form-user-show-route'), $record))
->filters([
//
Filter::make('guest_entries')
->query(fn (Builder $query): Builder => $query->whereNull('user_id')),
Filter::make('user_entries')
->query(fn (Builder $query): Builder => $query->whereNotNull('user_id')),
])
->headerActions([
])
Expand All @@ -66,7 +71,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
45 changes: 33 additions & 12 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,17 +96,34 @@ 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,
],
);
}

$this->emit('entrySaved', $entryModel);

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
5 changes: 5 additions & 0 deletions src/Models/FilamentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public function filamentFormUsers(): HasMany
{
return $this->hasMany(FilamentFormUser::class);
}

public function getFormLinkAttribute(): string
{
return route(config('filament-form-builder.filament-form-show-route'), $this->id);
}
}

0 comments on commit 12f6aa3

Please sign in to comment.