160 lines
5.6 KiB
PHP
160 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\UserResource\Pages;
|
|
use App\Models\User;
|
|
use DB;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Form;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Columns\BadgeColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
|
|
class UserResource extends Resource
|
|
{
|
|
protected static ?string $model = User::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
TextInput::make('name')
|
|
->required()
|
|
->maxLength(255),
|
|
|
|
TextInput::make('email')
|
|
->email()
|
|
->required()
|
|
->maxLength(255),
|
|
|
|
TextInput::make('stripe_id')
|
|
->label('Stripe ID')
|
|
->disabled()
|
|
->helperText('Automatically managed by Stripe'),
|
|
|
|
TextInput::make('pm_type')
|
|
->label('Payment Method Type')
|
|
->disabled(),
|
|
|
|
TextInput::make('pm_last_four')
|
|
->label('Card Last 4 Digits')
|
|
->disabled(),
|
|
|
|
DatePicker::make('trial_ends_at')
|
|
->label('Trial Ends At')
|
|
->disabled()
|
|
->displayFormat('Y-m-d H:i:s'),
|
|
Select::make('level')
|
|
->label('User Level')
|
|
->options([
|
|
0 => 'Normal User',
|
|
1 => 'Banned',
|
|
9 => 'Super Admin',
|
|
])
|
|
->required(),
|
|
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
TextColumn::make('name')->sortable()->searchable(),
|
|
TextColumn::make('email')->sortable()->searchable(),
|
|
BadgeColumn::make('level')
|
|
->label('User Level')
|
|
->getStateUsing(function ($record) {
|
|
return match ($record->level) {
|
|
0 => 'Normal User',
|
|
1 => 'Banned',
|
|
9 => 'Super Admin',
|
|
default => 'Unknown', // In case some invalid level exists
|
|
};
|
|
})
|
|
->colors([
|
|
'success' => fn ($state) => $state === 'Normal User',
|
|
'danger' => fn ($state) => $state === 'Banned',
|
|
'warning' => fn ($state) => $state === 'Super Admin',
|
|
])
|
|
->sortable(),
|
|
TextColumn::make('stripe_id')->label('Stripe ID')->copyable(),
|
|
TextColumn::make('pm_last_four')->label('Card Last 4'),
|
|
TextColumn::make('trial_ends_at')->label('Trial Ends')->dateTime()->sortable(),
|
|
])
|
|
->defaultSort('created_at', 'desc')
|
|
->filters([
|
|
//
|
|
])
|
|
->actions([
|
|
Tables\Actions\EditAction::make(),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\BulkActionGroup::make([
|
|
Tables\Actions\DeleteBulkAction::make(),
|
|
Tables\Actions\BulkAction::make('updateLevel')
|
|
->label('Update User Level')
|
|
->action(function (Collection $records, array $data) {
|
|
|
|
$newLevel = $data['new_level'];
|
|
if ($newLevel === 9) {
|
|
throw new \Exception('User level cannot be 9 or higher.');
|
|
}
|
|
|
|
DB::table('users')
|
|
->whereIn('id', $records->pluck('id'))
|
|
->update(['level' => $newLevel]);
|
|
|
|
|
|
Notification::make()
|
|
->title('User Level Updated')
|
|
->body('The selected users\' levels have been updated successfully.')
|
|
->success()
|
|
->send();
|
|
})
|
|
->icon('heroicon-o-pencil')
|
|
->color('primary')
|
|
->modalHeading('Select User Level')
|
|
->modalSubheading('Please choose the user level to apply to the selected users.')
|
|
->form([
|
|
Select::make('new_level')
|
|
->label('Select User Level')
|
|
->options([
|
|
0 => 'Unban (Normal User)',
|
|
1 => 'Ban',
|
|
])
|
|
->required(),
|
|
]),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListUsers::route('/'),
|
|
'create' => Pages\CreateUser::route('/create'),
|
|
'edit' => Pages\EditUser::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|