|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Resources; |
| 4 | + |
| 5 | +use App\Filament\Resources\WallOfLoveSubmissionResource\Pages; |
| 6 | +use App\Models\WallOfLoveSubmission; |
| 7 | +use Filament\Forms; |
| 8 | +use Filament\Forms\Form; |
| 9 | +use Filament\Resources\Resource; |
| 10 | +use Filament\Tables; |
| 11 | +use Filament\Tables\Table; |
| 12 | +use Illuminate\Database\Eloquent\Builder; |
| 13 | + |
| 14 | +class WallOfLoveSubmissionResource extends Resource |
| 15 | +{ |
| 16 | + protected static ?string $model = WallOfLoveSubmission::class; |
| 17 | + |
| 18 | + protected static ?string $navigationIcon = 'heroicon-o-heart'; |
| 19 | + |
| 20 | + protected static ?string $navigationLabel = 'Wall of Love'; |
| 21 | + |
| 22 | + protected static ?string $pluralModelLabel = 'Wall of Love Submissions'; |
| 23 | + |
| 24 | + public static function form(Form $form): Form |
| 25 | + { |
| 26 | + return $form |
| 27 | + ->schema([ |
| 28 | + Forms\Components\Section::make('Submission Details') |
| 29 | + ->schema([ |
| 30 | + Forms\Components\TextInput::make('name') |
| 31 | + ->required() |
| 32 | + ->maxLength(255), |
| 33 | + |
| 34 | + Forms\Components\TextInput::make('company') |
| 35 | + ->maxLength(255), |
| 36 | + |
| 37 | + Forms\Components\FileUpload::make('photo_path') |
| 38 | + ->label('Photo') |
| 39 | + ->image() |
| 40 | + ->disk('public') |
| 41 | + ->directory('wall-of-love-photos'), |
| 42 | + |
| 43 | + Forms\Components\TextInput::make('url') |
| 44 | + ->label('Website/Social URL') |
| 45 | + ->url() |
| 46 | + ->maxLength(255), |
| 47 | + |
| 48 | + Forms\Components\Textarea::make('testimonial') |
| 49 | + ->maxLength(1000) |
| 50 | + ->rows(4), |
| 51 | + ]), |
| 52 | + |
| 53 | + Forms\Components\Section::make('Review Information') |
| 54 | + ->schema([ |
| 55 | + Forms\Components\Select::make('user_id') |
| 56 | + ->relationship('user', 'name') |
| 57 | + ->required() |
| 58 | + ->disabled(), |
| 59 | + |
| 60 | + Forms\Components\DateTimePicker::make('approved_at') |
| 61 | + ->label('Approved At'), |
| 62 | + |
| 63 | + Forms\Components\Select::make('approved_by') |
| 64 | + ->relationship('approvedBy', 'name') |
| 65 | + ->label('Approved By'), |
| 66 | + |
| 67 | + Forms\Components\Placeholder::make('created_at') |
| 68 | + ->label('Submitted At') |
| 69 | + ->content(fn (WallOfLoveSubmission $record): ?string => $record->created_at?->diffForHumans()), |
| 70 | + ]), |
| 71 | + ]); |
| 72 | + } |
| 73 | + |
| 74 | + public static function table(Table $table): Table |
| 75 | + { |
| 76 | + return $table |
| 77 | + ->columns([ |
| 78 | + Tables\Columns\TextColumn::make('name') |
| 79 | + ->searchable() |
| 80 | + ->sortable(), |
| 81 | + |
| 82 | + Tables\Columns\TextColumn::make('company') |
| 83 | + ->searchable() |
| 84 | + ->toggleable(), |
| 85 | + |
| 86 | + Tables\Columns\TextColumn::make('user.name') |
| 87 | + ->label('Submitted By') |
| 88 | + ->searchable() |
| 89 | + ->sortable(), |
| 90 | + |
| 91 | + Tables\Columns\ImageColumn::make('photo_path') |
| 92 | + ->label('Photo') |
| 93 | + ->disk('public') |
| 94 | + ->height(40) |
| 95 | + ->toggleable(), |
| 96 | + |
| 97 | + Tables\Columns\IconColumn::make('approved_at') |
| 98 | + ->label('Status') |
| 99 | + ->boolean() |
| 100 | + ->trueIcon('heroicon-o-check-circle') |
| 101 | + ->falseIcon('heroicon-o-clock') |
| 102 | + ->trueColor('success') |
| 103 | + ->falseColor('warning') |
| 104 | + ->sortable(), |
| 105 | + |
| 106 | + Tables\Columns\TextColumn::make('approvedBy.name') |
| 107 | + ->label('Approved By') |
| 108 | + ->toggleable(), |
| 109 | + |
| 110 | + Tables\Columns\TextColumn::make('created_at') |
| 111 | + ->label('Submitted') |
| 112 | + ->dateTime() |
| 113 | + ->sortable() |
| 114 | + ->toggleable(), |
| 115 | + ]) |
| 116 | + ->filters([ |
| 117 | + Tables\Filters\TernaryFilter::make('approved_at') |
| 118 | + ->label('Status') |
| 119 | + ->placeholder('All submissions') |
| 120 | + ->trueLabel('Approved') |
| 121 | + ->falseLabel('Pending') |
| 122 | + ->queries( |
| 123 | + true: fn (Builder $query) => $query->whereNotNull('approved_at'), |
| 124 | + false: fn (Builder $query) => $query->whereNull('approved_at'), |
| 125 | + ), |
| 126 | + ]) |
| 127 | + ->actions([ |
| 128 | + Tables\Actions\Action::make('approve') |
| 129 | + ->icon('heroicon-o-check') |
| 130 | + ->color('success') |
| 131 | + ->visible(fn (WallOfLoveSubmission $record) => $record->isPending()) |
| 132 | + ->action(fn (WallOfLoveSubmission $record) => $record->update([ |
| 133 | + 'approved_at' => now(), |
| 134 | + 'approved_by' => auth()->id(), |
| 135 | + ])) |
| 136 | + ->requiresConfirmation() |
| 137 | + ->modalHeading('Approve Submission') |
| 138 | + ->modalDescription('Are you sure you want to approve this submission for the Wall of Love?'), |
| 139 | + |
| 140 | + Tables\Actions\Action::make('unapprove') |
| 141 | + ->icon('heroicon-o-x-mark') |
| 142 | + ->color('warning') |
| 143 | + ->visible(fn (WallOfLoveSubmission $record) => $record->isApproved()) |
| 144 | + ->action(fn (WallOfLoveSubmission $record) => $record->update([ |
| 145 | + 'approved_at' => null, |
| 146 | + 'approved_by' => null, |
| 147 | + ])) |
| 148 | + ->requiresConfirmation() |
| 149 | + ->modalHeading('Unapprove Submission') |
| 150 | + ->modalDescription('Are you sure you want to unapprove this submission?'), |
| 151 | + |
| 152 | + Tables\Actions\EditAction::make(), |
| 153 | + Tables\Actions\DeleteAction::make(), |
| 154 | + ]) |
| 155 | + ->bulkActions([ |
| 156 | + Tables\Actions\BulkActionGroup::make([ |
| 157 | + Tables\Actions\BulkAction::make('approve') |
| 158 | + ->icon('heroicon-o-check') |
| 159 | + ->color('success') |
| 160 | + ->action(function ($records) { |
| 161 | + $records->each(fn (WallOfLoveSubmission $record) => $record->update([ |
| 162 | + 'approved_at' => now(), |
| 163 | + 'approved_by' => auth()->id(), |
| 164 | + ])); |
| 165 | + }) |
| 166 | + ->requiresConfirmation() |
| 167 | + ->modalHeading('Approve Selected Submissions') |
| 168 | + ->modalDescription('Are you sure you want to approve all selected submissions?'), |
| 169 | + |
| 170 | + Tables\Actions\DeleteBulkAction::make(), |
| 171 | + ]), |
| 172 | + ]) |
| 173 | + ->defaultSort('created_at', 'desc'); |
| 174 | + } |
| 175 | + |
| 176 | + public static function getRelations(): array |
| 177 | + { |
| 178 | + return [ |
| 179 | + // |
| 180 | + ]; |
| 181 | + } |
| 182 | + |
| 183 | + public static function getPages(): array |
| 184 | + { |
| 185 | + return [ |
| 186 | + 'index' => Pages\ListWallOfLoveSubmissions::route('/'), |
| 187 | + //'create' => Pages\CreateWallOfLoveSubmission::route('/create'), |
| 188 | + 'edit' => Pages\EditWallOfLoveSubmission::route('/{record}/edit'), |
| 189 | + ]; |
| 190 | + } |
| 191 | +} |
0 commit comments