|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Filament\Resources; |
| 6 | + |
| 7 | +use App\Enums\ContactStatus; |
| 8 | +use App\Filament\Resources\ContactResource\Pages\CreateContact; |
| 9 | +use App\Filament\Resources\ContactResource\Pages\EditContact; |
| 10 | +use App\Filament\Resources\ContactResource\Pages\ListContacts; |
| 11 | +use App\Models\Contact; |
| 12 | +use Filament\Forms; |
| 13 | +use Filament\Resources\Resource; |
| 14 | +use Filament\Schemas\Components\Grid; |
| 15 | +use Filament\Schemas\Components\Section; |
| 16 | +use Filament\Schemas\Schema; |
| 17 | +use Filament\Tables; |
| 18 | +use Filament\Tables\Filters\SelectFilter; |
| 19 | +use Filament\Tables\Table; |
| 20 | +use Illuminate\Support\Str; |
| 21 | + |
| 22 | +class ContactResource extends Resource |
| 23 | +{ |
| 24 | + protected static ?string $model = Contact::class; |
| 25 | + |
| 26 | + protected static string|\UnitEnum|null $navigationGroup = 'Audience'; |
| 27 | + |
| 28 | + protected static string|\BackedEnum|null $navigationIcon = 'heroicon-m-users'; |
| 29 | + |
| 30 | + public static function form(Schema $schema): Schema |
| 31 | + { |
| 32 | + return $schema->components([ |
| 33 | + Section::make('Details') |
| 34 | + ->schema([ |
| 35 | + Grid::make(2)->schema([ |
| 36 | + Forms\Components\TextInput::make('email') |
| 37 | + ->label('Email') |
| 38 | + ->required() |
| 39 | + ->email() |
| 40 | + ->maxLength(255), |
| 41 | + Forms\Components\Select::make('status') |
| 42 | + ->label('Status') |
| 43 | + ->options(self::statusOptions()) |
| 44 | + ->default(ContactStatus::Active->value) |
| 45 | + ->required(), |
| 46 | + Forms\Components\TextInput::make('first_name') |
| 47 | + ->label('First name') |
| 48 | + ->maxLength(255), |
| 49 | + Forms\Components\TextInput::make('last_name') |
| 50 | + ->label('Last name') |
| 51 | + ->maxLength(255), |
| 52 | + ]), |
| 53 | + ]), |
| 54 | + Section::make('Lists') |
| 55 | + ->schema([ |
| 56 | + Forms\Components\Select::make('lists') |
| 57 | + ->label('Lists') |
| 58 | + ->relationship('lists', 'name') |
| 59 | + ->multiple() |
| 60 | + ->preload() |
| 61 | + ->searchable() |
| 62 | + ->helperText('Assign the contact to one or more lists.'), |
| 63 | + ]) |
| 64 | + ->collapsible() |
| 65 | + ->collapsed(), |
| 66 | + Section::make('Attributes') |
| 67 | + ->schema([ |
| 68 | + Forms\Components\KeyValue::make('attributes') |
| 69 | + ->label('Attributes') |
| 70 | + ->helperText('Store additional metadata as key-value pairs.') |
| 71 | + ->nullable() |
| 72 | + ->columnSpanFull(), |
| 73 | + ]) |
| 74 | + ->collapsible() |
| 75 | + ->collapsed(), |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + public static function table(Table $table): Table |
| 80 | + { |
| 81 | + return $table |
| 82 | + ->columns([ |
| 83 | + Tables\Columns\TextColumn::make('email') |
| 84 | + ->label('Email') |
| 85 | + ->sortable() |
| 86 | + ->searchable(), |
| 87 | + Tables\Columns\TextColumn::make('first_name') |
| 88 | + ->label('First name') |
| 89 | + ->sortable() |
| 90 | + ->toggleable(), |
| 91 | + Tables\Columns\TextColumn::make('last_name') |
| 92 | + ->label('Last name') |
| 93 | + ->sortable() |
| 94 | + ->toggleable(), |
| 95 | + Tables\Columns\TextColumn::make('status') |
| 96 | + ->label('Status') |
| 97 | + ->badge() |
| 98 | + ->formatStateUsing(static function (?ContactStatus $status): string { |
| 99 | + if ($status === null) { |
| 100 | + return ''; |
| 101 | + } |
| 102 | + |
| 103 | + return Str::title($status->value); |
| 104 | + }), |
| 105 | + Tables\Columns\TextColumn::make('lists_count') |
| 106 | + ->label('Lists') |
| 107 | + ->counts('lists') |
| 108 | + ->badge(), |
| 109 | + Tables\Columns\TextColumn::make('created_at') |
| 110 | + ->label('Created') |
| 111 | + ->dateTime() |
| 112 | + ->sortable() |
| 113 | + ->toggleable(isToggledHiddenByDefault: true), |
| 114 | + Tables\Columns\TextColumn::make('updated_at') |
| 115 | + ->label('Updated') |
| 116 | + ->dateTime() |
| 117 | + ->sortable() |
| 118 | + ->toggleable(isToggledHiddenByDefault: true), |
| 119 | + ]) |
| 120 | + ->filters([ |
| 121 | + SelectFilter::make('status') |
| 122 | + ->label('Status') |
| 123 | + ->options(self::statusOptions()), |
| 124 | + ]); |
| 125 | + } |
| 126 | + |
| 127 | + public static function getPages(): array |
| 128 | + { |
| 129 | + return [ |
| 130 | + 'index' => ListContacts::route('/contacts'), |
| 131 | + 'create' => CreateContact::route('/contacts/create'), |
| 132 | + 'edit' => EditContact::route('/contacts/{record}/edit'), |
| 133 | + ]; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @return array<string, string> |
| 138 | + */ |
| 139 | + private static function statusOptions(): array |
| 140 | + { |
| 141 | + $options = []; |
| 142 | + |
| 143 | + foreach (ContactStatus::cases() as $status) { |
| 144 | + $options[$status->value] = Str::title($status->value); |
| 145 | + } |
| 146 | + |
| 147 | + return $options; |
| 148 | + } |
| 149 | +} |
0 commit comments