|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Resources; |
| 4 | + |
| 5 | +use App\Filament\Resources\PluginUserResource\Pages; |
| 6 | +use App\Filament\Resources\PluginUserResource\RelationManagers; |
| 7 | +use App\Models\PluginUser; |
| 8 | +use Filament\Forms; |
| 9 | +use Filament\Forms\Components\Actions\Action; |
| 10 | +use Filament\Resources\Form; |
| 11 | +use Filament\Resources\Resource; |
| 12 | +use Filament\Resources\Table; |
| 13 | +use Filament\Tables; |
| 14 | +use Illuminate\Database\Eloquent\Builder; |
| 15 | + |
| 16 | +class PluginUserResource extends Resource |
| 17 | +{ |
| 18 | + const STATUS_ARRAY = [ |
| 19 | + PluginUser::ACTIVE => 'Activate', |
| 20 | + PluginUser::INACTIVE => 'Deactivate', |
| 21 | + PluginUser::UNINSTALL => 'Uninstalled', |
| 22 | + ]; |
| 23 | + protected static ?string $model = PluginUser::class; |
| 24 | + |
| 25 | + protected static ?string $navigationIcon = 'heroicon-o-users'; |
| 26 | + |
| 27 | + public static function form(Form $form): Form |
| 28 | + { |
| 29 | + return $form |
| 30 | + ->schema([ |
| 31 | + Forms\Components\Card::make(array( |
| 32 | + Forms\Components\TextInput::make('name') |
| 33 | + ->maxLength(20) |
| 34 | + ->placeholder("Name of user's plugin"), |
| 35 | + Forms\Components\TextInput::make('version') |
| 36 | + ->required() |
| 37 | + ->mask(fn (Forms\Components\TextInput\Mask $mask) => $mask->pattern('0.0.0')) |
| 38 | + ->maxLength(10) |
| 39 | + ->placeholder('current version of plugin'), |
| 40 | + Forms\Components\TextInput::make('website') |
| 41 | + ->url() |
| 42 | + ->suffixAction(fn (?string $state): Action => |
| 43 | + Action::make('visit') |
| 44 | + ->icon('heroicon-s-external-link') |
| 45 | + ->url( |
| 46 | + filled($state) ? "https://{$state}" : null, |
| 47 | + shouldOpenInNewTab: true, |
| 48 | + ), |
| 49 | + ) |
| 50 | + ->maxLength(100) |
| 51 | + ->placeholder('User website'), |
| 52 | + ))->columns(3), |
| 53 | + Forms\Components\Card::make([ |
| 54 | + Forms\Components\KeyValue::make('plugins') |
| 55 | + ->keyLabel('#') |
| 56 | + ->disableEditingKeys(true) |
| 57 | + ->valueLabel('Name'), |
| 58 | + Forms\Components\KeyValue::make('server') |
| 59 | + ->keyLabel('#') |
| 60 | + ->disableEditingKeys(true) |
| 61 | + ->valueLabel('Software'), |
| 62 | + ])->columns(), |
| 63 | + |
| 64 | + Forms\Components\Card::make([ |
| 65 | + Forms\Components\DatePicker::make('activated_at') |
| 66 | + ->required() |
| 67 | + ->placeholder('month day, year'), |
| 68 | + Forms\Components\DatePicker::make('deactivated_at') |
| 69 | + ->placeholder('month day, year'), |
| 70 | + Forms\Components\DatePicker::make('uninstalled_at') |
| 71 | + ->placeholder('month day, year'), |
| 72 | + Forms\Components\Radio::make('status') |
| 73 | + ->options(self::STATUS_ARRAY), |
| 74 | + ])->columns(3), |
| 75 | + ]); |
| 76 | + } |
| 77 | + |
| 78 | + public static function table(Table $table): Table |
| 79 | + { |
| 80 | + return $table |
| 81 | + ->columns([ |
| 82 | + Tables\Columns\TextColumn::make('name')->searchable(), |
| 83 | + Tables\Columns\TextColumn::make('version')->searchable(), |
| 84 | + Tables\Columns\TextColumn::make('website') |
| 85 | + ->url(function (PluginUser $record): string { |
| 86 | + return $record->website; |
| 87 | + }, true) |
| 88 | + ->searchable(), |
| 89 | + Tables\Columns\BadgeColumn::make('status') |
| 90 | + ->colors([ |
| 91 | + 'warning' => 'deactivate', |
| 92 | + 'success' => 'activate', |
| 93 | + 'danger' => 'uninstalled', |
| 94 | + ]) |
| 95 | + ->icons([ |
| 96 | + 'heroicon-o-x' => 'deactivate', |
| 97 | + 'heroicon-o-check' => 'activate', |
| 98 | + 'heroicon-o-trash' => 'uninstalled', |
| 99 | + ]) |
| 100 | + ->sortable() |
| 101 | + ->searchable(), |
| 102 | + |
| 103 | + ]) |
| 104 | + ->filters([ |
| 105 | + Tables\Filters\SelectFilter::make('status') |
| 106 | + ->options(self::STATUS_ARRAY), |
| 107 | + Tables\Filters\Filter::make('activated_at') |
| 108 | + ->form([ |
| 109 | + Forms\Components\DatePicker::make('activated_from')->default(now()->startOfMonth()), |
| 110 | + Forms\Components\DatePicker::make('activated_until')->default(now()->endOfMonth()), |
| 111 | + ]) |
| 112 | + ->query(function (Builder $query, array $data): Builder { |
| 113 | + return $query |
| 114 | + ->when( |
| 115 | + $data['activated_from'], |
| 116 | + fn(Builder $query, $date): Builder => $query->whereDate('activated_at', '>=', $date), |
| 117 | + ) |
| 118 | + ->when( |
| 119 | + $data['activated_until'], |
| 120 | + fn(Builder $query, $date): Builder => $query->whereDate('activated_at', '<=', $date), |
| 121 | + ); |
| 122 | + }) |
| 123 | + ]) |
| 124 | + ->actions([ |
| 125 | + Tables\Actions\ViewAction::make(), |
| 126 | + Tables\Actions\EditAction::make() |
| 127 | + ]) |
| 128 | + ->bulkActions([ |
| 129 | + Tables\Actions\DeleteBulkAction::make(), |
| 130 | + ]); |
| 131 | + } |
| 132 | + |
| 133 | + public static function getRelations(): array |
| 134 | + { |
| 135 | + return [ |
| 136 | + // |
| 137 | + ]; |
| 138 | + } |
| 139 | + |
| 140 | + public static function getPages(): array |
| 141 | + { |
| 142 | + return [ |
| 143 | + 'index' => \App\Http\Resources\PluginUserResource\Pages\ListPluginUsers::route('/'), |
| 144 | + 'create' => \App\Http\Resources\PluginUserResource\Pages\CreatePluginUser::route('/create'), |
| 145 | + 'edit' => \App\Http\Resources\PluginUserResource\Pages\EditPluginUser::route('/{record}/edit'), |
| 146 | + ]; |
| 147 | + } |
| 148 | +} |
0 commit comments