|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Resources\UserResource\RelationManagers; |
| 4 | + |
| 5 | +use App\Enums\PluginType; |
| 6 | +use Filament\Forms; |
| 7 | +use Filament\Forms\Form; |
| 8 | +use Filament\Resources\RelationManagers\RelationManager; |
| 9 | +use Filament\Tables; |
| 10 | +use Filament\Tables\Table; |
| 11 | + |
| 12 | +class PluginLicensesRelationManager extends RelationManager |
| 13 | +{ |
| 14 | + protected static string $relationship = 'pluginLicenses'; |
| 15 | + |
| 16 | + protected static ?string $title = 'Plugins'; |
| 17 | + |
| 18 | + public function form(Form $form): Form |
| 19 | + { |
| 20 | + return $form |
| 21 | + ->schema([ |
| 22 | + Forms\Components\Select::make('plugin_id') |
| 23 | + ->relationship('plugin', 'name') |
| 24 | + ->searchable() |
| 25 | + ->preload() |
| 26 | + ->required(), |
| 27 | + Forms\Components\Toggle::make('is_grandfathered') |
| 28 | + ->label('Comped') |
| 29 | + ->default(true), |
| 30 | + Forms\Components\DateTimePicker::make('purchased_at') |
| 31 | + ->default(now()), |
| 32 | + Forms\Components\DateTimePicker::make('expires_at'), |
| 33 | + ]); |
| 34 | + } |
| 35 | + |
| 36 | + public function table(Table $table): Table |
| 37 | + { |
| 38 | + return $table |
| 39 | + ->columns([ |
| 40 | + Tables\Columns\TextColumn::make('plugin.name') |
| 41 | + ->label('Plugin') |
| 42 | + ->searchable() |
| 43 | + ->sortable() |
| 44 | + ->fontFamily('mono'), |
| 45 | + Tables\Columns\TextColumn::make('plugin.type') |
| 46 | + ->label('Type') |
| 47 | + ->badge() |
| 48 | + ->color(fn (PluginType $state): string => match ($state) { |
| 49 | + PluginType::Free => 'gray', |
| 50 | + PluginType::Paid => 'success', |
| 51 | + }), |
| 52 | + Tables\Columns\TextColumn::make('price_paid') |
| 53 | + ->label('Price Paid') |
| 54 | + ->money('usd', divideBy: 100) |
| 55 | + ->sortable(), |
| 56 | + Tables\Columns\IconColumn::make('is_grandfathered') |
| 57 | + ->label('Comped') |
| 58 | + ->boolean(), |
| 59 | + Tables\Columns\TextColumn::make('purchased_at') |
| 60 | + ->dateTime() |
| 61 | + ->sortable(), |
| 62 | + Tables\Columns\TextColumn::make('expires_at') |
| 63 | + ->dateTime() |
| 64 | + ->sortable() |
| 65 | + ->placeholder('Never'), |
| 66 | + ]) |
| 67 | + ->defaultSort('purchased_at', 'desc') |
| 68 | + ->filters([ |
| 69 | + Tables\Filters\TernaryFilter::make('is_grandfathered') |
| 70 | + ->label('Comped'), |
| 71 | + ]) |
| 72 | + ->headerActions([ |
| 73 | + Tables\Actions\CreateAction::make() |
| 74 | + ->mutateFormDataUsing(function (array $data): array { |
| 75 | + $data['price_paid'] = 0; |
| 76 | + $data['currency'] = 'USD'; |
| 77 | + |
| 78 | + return $data; |
| 79 | + }), |
| 80 | + ]) |
| 81 | + ->actions([ |
| 82 | + Tables\Actions\DeleteAction::make(), |
| 83 | + ]); |
| 84 | + } |
| 85 | +} |
0 commit comments