|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Resources; |
| 4 | + |
| 5 | +use App\Domain\CatchEmAll\Models\SpecialCode; |
| 6 | +use App\Filament\Resources\SpecialCodeResource\Pages; |
| 7 | +use App\Models\Fursuit\Fursuit; |
| 8 | +use Filament\Forms; |
| 9 | +use Filament\Forms\Form; |
| 10 | +use Filament\Resources\Resource; |
| 11 | +use Filament\Tables; |
| 12 | +use Filament\Tables\Table; |
| 13 | + |
| 14 | +class SpecialCodeResource extends Resource |
| 15 | +{ |
| 16 | + protected static ?string $model = SpecialCode::class; |
| 17 | + |
| 18 | + protected static ?string $navigationIcon = 'heroicon-o-qr-code'; |
| 19 | + |
| 20 | + protected static ?string $navigationGroup = 'Events & Registration'; |
| 21 | + |
| 22 | + protected static ?int $navigationSort = 3; |
| 23 | + |
| 24 | + public static function form(Form $form): Form |
| 25 | + { |
| 26 | + return $form |
| 27 | + ->schema([ |
| 28 | + Forms\Components\Select::make('event_id') |
| 29 | + ->label('Event') |
| 30 | + ->helperText('Event in which the code can be used') |
| 31 | + ->options( |
| 32 | + \App\Models\Event::all()->pluck('name', 'id') |
| 33 | + ) |
| 34 | + ->required() |
| 35 | + ->columnSpanFull(), |
| 36 | + Forms\Components\Select::make('class_name') |
| 37 | + ->label('Class') |
| 38 | + ->helperText('PHP class used for code handling') |
| 39 | + ->options([ |
| 40 | + 'App\\Domain\\CatchEmAll\\SpecialActions\\BugBountyAction' => 'Bug Hunter Bounty', |
| 41 | + ]) |
| 42 | + ->columnSpanFull(), |
| 43 | + Forms\Components\Textarea::make('constructor_data') |
| 44 | + ->label('Constructor Data') |
| 45 | + ->helperText('Data to be passed to the constructor of the action class') |
| 46 | + ->rows(3) |
| 47 | + ->columnSpanFull() |
| 48 | + ->disabled(fn($get) => match ($get('class_name')) { |
| 49 | + 'EXAMPLE' => false, |
| 50 | + default => true |
| 51 | + }) |
| 52 | + ->placeholder(fn($get) => match ($get('class_name')) { |
| 53 | + 'EXAMPLE' => '{"amount": 100, "reason": "An Example"}', |
| 54 | + default => '', |
| 55 | + }) |
| 56 | + ->rules(['nullable', 'json']), |
| 57 | + |
| 58 | + Forms\Components\TextInput::make('code') |
| 59 | + ->label('Code') |
| 60 | + ->helperText('E.g. ABC45') |
| 61 | + ->maxLength(5) |
| 62 | + ->minLength(5) |
| 63 | + ->required() |
| 64 | + ->unique(ignoreRecord: true, table: 'special_codes', column: 'code') |
| 65 | + ->rule(fn() => function ($attribute, $value, $fail) { |
| 66 | + if (Fursuit::where('catch_code', $value)->exists()) { |
| 67 | + $fail('This code is already used in Fursuits.'); |
| 68 | + } |
| 69 | + }), |
| 70 | + ]); |
| 71 | + } |
| 72 | + |
| 73 | + public static function table(Table $table): Table |
| 74 | + { |
| 75 | + return $table |
| 76 | + ->columns([ |
| 77 | + Tables\Columns\TextColumn::make('code') |
| 78 | + ->label('Code') |
| 79 | + ->sortable(), |
| 80 | + Tables\Columns\TextColumn::make('class_name') |
| 81 | + ->label('Class') |
| 82 | + ->formatStateUsing(fn(string $state): string => match ($state) { |
| 83 | + 'App\\Domain\\CatchEmAll\\SpecialActions\\BugBountyAction' => 'Bug Hunter Bounty', |
| 84 | + default => $state |
| 85 | + }) |
| 86 | + ->sortable(), |
| 87 | + Tables\Columns\TextColumn::make('constructor_data') |
| 88 | + ->label('Data') |
| 89 | + ->sortable(), |
| 90 | + Tables\Columns\TextColumn::make('event_id') |
| 91 | + ->label('Event') |
| 92 | + ->formatStateUsing(fn(string $state): string => \App\Models\Event::where('id', $state)->pluck('name')->first()) |
| 93 | + ->sortable(), |
| 94 | + ]) |
| 95 | + ->filters([ |
| 96 | + // |
| 97 | + ]) |
| 98 | + ->actions([ |
| 99 | + Tables\Actions\EditAction::make(), |
| 100 | + Tables\Actions\DeleteAction::make(), |
| 101 | + ]) |
| 102 | + ->bulkActions([ |
| 103 | + Tables\Actions\BulkActionGroup::make([ |
| 104 | + Tables\Actions\DeleteBulkAction::make(), |
| 105 | + ]), |
| 106 | + ]) |
| 107 | + ->defaultSort('created_at', 'desc'); |
| 108 | + } |
| 109 | + |
| 110 | + public static function getPages(): array |
| 111 | + { |
| 112 | + return [ |
| 113 | + 'index' => Pages\ManageSpecialCodes::route('/'), |
| 114 | + ]; |
| 115 | + } |
| 116 | +} |
0 commit comments