|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Resources\Users\Tables; |
| 4 | + |
| 5 | +use App\Jobs\BanUser; |
| 6 | +use App\Jobs\DeleteUserThreads; |
| 7 | +use App\Jobs\UnbanUser; |
| 8 | +use App\Jobs\UnVerifyAuthor; |
| 9 | +use App\Jobs\VerifyAuthor; |
| 10 | +use App\Models\User; |
| 11 | +use App\Policies\UserPolicy; |
| 12 | +use Filament\Actions\Action; |
| 13 | +use Filament\Actions\ActionGroup; |
| 14 | +use Filament\Actions\BulkActionGroup; |
| 15 | +use Filament\Actions\DeleteAction; |
| 16 | +use Filament\Actions\DeleteBulkAction; |
| 17 | +use Filament\Forms\Components\Checkbox; |
| 18 | +use Filament\Forms\Components\TextInput; |
| 19 | +use Filament\Notifications\Notification; |
| 20 | +use Filament\Tables\Columns\IconColumn; |
| 21 | +use Filament\Tables\Columns\ImageColumn; |
| 22 | +use Filament\Tables\Columns\TextColumn; |
| 23 | +use Filament\Tables\Filters\SelectFilter; |
| 24 | +use Filament\Tables\Filters\TernaryFilter; |
| 25 | +use Filament\Tables\Table; |
| 26 | + |
| 27 | +class UsersTable |
| 28 | +{ |
| 29 | + public static function configure(Table $table): Table |
| 30 | + { |
| 31 | + return $table |
| 32 | + ->defaultSort('created_at', 'desc') |
| 33 | + ->openRecordUrlInNewTab() |
| 34 | + ->columns([ |
| 35 | + ImageColumn::make('github_id') |
| 36 | + ->label('Name') |
| 37 | + ->circular() |
| 38 | + ->width('0%') |
| 39 | + ->defaultImageUrl(fn(?string $state): string => $state ? sprintf('https://avatars.githubusercontent.com/u/%s', $state) : asset('images/laravelio-icon-gray.svg')), |
| 40 | + |
| 41 | + TextColumn::make('username') |
| 42 | + ->label('') |
| 43 | + ->searchable() |
| 44 | + ->formatStateUsing(fn(User $user): ?string => $user->name) |
| 45 | + ->description(fn(User $user): ?string => $user->username), |
| 46 | + |
| 47 | + TextColumn::make('email') |
| 48 | + ->searchable() |
| 49 | + ->toggleable(isToggledHiddenByDefault: true), |
| 50 | + |
| 51 | + TextColumn::make('type') |
| 52 | + ->label('Role') |
| 53 | + ->badge() |
| 54 | + ->formatStateUsing(fn(string $state): string => match ($state) { |
| 55 | + '1' => 'User', |
| 56 | + '2' => 'Moderator', |
| 57 | + '3' => 'Admin', |
| 58 | + }), |
| 59 | + |
| 60 | + IconColumn::make('banned_at') |
| 61 | + ->label('Banned') |
| 62 | + ->boolean() |
| 63 | + ->default(false), |
| 64 | + |
| 65 | + TextColumn::make('created_at') |
| 66 | + ->label('Joined on') |
| 67 | + ->dateTime() |
| 68 | + ->sortable() |
| 69 | + ]) |
| 70 | + ->filters([ |
| 71 | + SelectFilter::make('type') |
| 72 | + ->options([ |
| 73 | + '1' => 'User', |
| 74 | + '2' => 'Moderator', |
| 75 | + '3' => 'Admin', |
| 76 | + ]), |
| 77 | + |
| 78 | + TernaryFilter::make('banned_at') |
| 79 | + ->label('Banned') |
| 80 | + ->nullable() |
| 81 | + ]) |
| 82 | + ->recordActions([ |
| 83 | + Action::make('view') |
| 84 | + ->url(fn(User $user): string => route('profile', $user->username)) |
| 85 | + ->openUrlInNewTab() |
| 86 | + ->icon('heroicon-s-eye'), |
| 87 | + |
| 88 | + ActionGroup::make([ |
| 89 | + Action::make('verify_author') |
| 90 | + ->action(function (User $user) { |
| 91 | + VerifyAuthor::dispatchSync($user); |
| 92 | + |
| 93 | + Notification::make() |
| 94 | + ->title($user->name . ' is now a verified author.') |
| 95 | + ->success() |
| 96 | + ->send(); |
| 97 | + }) |
| 98 | + ->openUrlInNewTab() |
| 99 | + ->color('primary') |
| 100 | + ->icon('heroicon-s-check-circle') |
| 101 | + ->requiresConfirmation() |
| 102 | + ->visible(fn(User $user): bool => auth()->user()->can(UserPolicy::ADMIN, $user) && ! $user->isVerifiedAuthor()), |
| 103 | + |
| 104 | + Action::make('unverify_author') |
| 105 | + ->action(function (User $user) { |
| 106 | + UnVerifyAuthor::dispatchSync($user); |
| 107 | + |
| 108 | + Notification::make() |
| 109 | + ->title($user->name . '\'s threads have been deleted.') |
| 110 | + ->success() |
| 111 | + ->send(); |
| 112 | + }) |
| 113 | + ->openUrlInNewTab() |
| 114 | + ->color('danger') |
| 115 | + ->icon('heroicon-s-x-circle') |
| 116 | + ->requiresConfirmation() |
| 117 | + ->visible(fn(User $user): bool => auth()->user()->can(UserPolicy::ADMIN, $user) && $user->isVerifiedAuthor()), |
| 118 | + |
| 119 | + Action::make('ban_author') |
| 120 | + ->schema([ |
| 121 | + TextInput::make('reason') |
| 122 | + ->label('Reason') |
| 123 | + ->required() |
| 124 | + ->placeholder('Provide a reason for banning this user...'), |
| 125 | + |
| 126 | + Checkbox::make('delete_threads') |
| 127 | + ->label('Delete all threads') |
| 128 | + ->default(false), |
| 129 | + ]) |
| 130 | + ->action(function (User $user, array $data) { |
| 131 | + BanUser::dispatchSync($user, $data['reason']); |
| 132 | + |
| 133 | + if ($data['delete_threads']) { |
| 134 | + DeleteUserThreads::dispatchSync($user); |
| 135 | + } |
| 136 | + |
| 137 | + Notification::make() |
| 138 | + ->title( |
| 139 | + $user->name . ' is now banned.' . ($data['delete_threads'] ? ' And all his threads are now deleted.' : '') |
| 140 | + ) |
| 141 | + ->success() |
| 142 | + ->send(); |
| 143 | + }) |
| 144 | + ->modalDescription('Are you sure you\'d like to ban this user? This will prevent him from logging in, posting threads and replying to threads.') |
| 145 | + ->openUrlInNewTab() |
| 146 | + ->color('danger') |
| 147 | + ->icon('heroicon-s-check-circle') |
| 148 | + ->requiresConfirmation() |
| 149 | + ->visible(fn(User $user): bool => auth()->user()->can(UserPolicy::BAN, $user) && ! $user->isBanned()), |
| 150 | + |
| 151 | + Action::make('unban_author') |
| 152 | + ->action(function (User $user) { |
| 153 | + |
| 154 | + UnbanUser::dispatchSync($user); |
| 155 | + |
| 156 | + Notification::make() |
| 157 | + ->title($user->name . ' is no longer a banned user.') |
| 158 | + ->success() |
| 159 | + ->send(); |
| 160 | + }) |
| 161 | + ->openUrlInNewTab() |
| 162 | + ->color('primary') |
| 163 | + ->icon('heroicon-s-x-circle') |
| 164 | + ->requiresConfirmation() |
| 165 | + ->visible(fn(User $user): bool => auth()->user()->can(UserPolicy::BAN, $user) && $user->isBanned()), |
| 166 | + |
| 167 | + Action::make('delete_threads') |
| 168 | + ->action(function (User $user) { |
| 169 | + DeleteUserThreads::dispatchSync($user); |
| 170 | + |
| 171 | + Notification::make() |
| 172 | + ->title($user->name . '\'s threads have been deleted.') |
| 173 | + ->success() |
| 174 | + ->send(); |
| 175 | + }) |
| 176 | + ->openUrlInNewTab() |
| 177 | + ->color('danger') |
| 178 | + ->icon('heroicon-s-archive-box-x-mark') |
| 179 | + ->requiresConfirmation() |
| 180 | + ->visible(fn(User $user): bool => auth()->user()->can(UserPolicy::DELETE, $user)), |
| 181 | + |
| 182 | + DeleteAction::make() |
| 183 | + ->visible(fn(User $user): bool => auth()->user()->can(UserPolicy::DELETE, $user)), |
| 184 | + ]), |
| 185 | + ]) |
| 186 | + ->toolbarActions([ |
| 187 | + BulkActionGroup::make([ |
| 188 | + DeleteBulkAction::make(), |
| 189 | + ]), |
| 190 | + ]); |
| 191 | + } |
| 192 | +} |
0 commit comments