|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backstage\UserManagement\Imports; |
| 4 | + |
| 5 | +use Filament\Actions\Exports\Exporter; |
| 6 | +use Filament\Actions\Exports\ExportColumn; |
| 7 | +use Filament\Actions\Exports\Models\Export; |
| 8 | +use Filament\Actions\Imports\ImportColumn; |
| 9 | +use Filament\Actions\Imports\Importer; |
| 10 | +use Filament\Actions\Imports\Models\Import; |
| 11 | +use App\Models\User; |
| 12 | + |
| 13 | + |
| 14 | +class UserImporter extends Importer |
| 15 | +{ |
| 16 | + public static function getModel(): string |
| 17 | + { |
| 18 | + return config('backstage.user-management.eloquent.users.model', \App\Models\User::class); |
| 19 | + } |
| 20 | + |
| 21 | + public static function getColumns(): array |
| 22 | + { |
| 23 | + return [ |
| 24 | + ImportColumn::make('name') |
| 25 | + ->label(__('Name')) |
| 26 | + ->requiredMapping() |
| 27 | + ->rules(['required', 'max:255']), |
| 28 | + |
| 29 | + ImportColumn::make('email') |
| 30 | + ->label(__('Email')) |
| 31 | + ->requiredMapping() |
| 32 | + ->rules(['required', 'email', 'max:255']), |
| 33 | + |
| 34 | + ImportColumn::make('email_verified_at') |
| 35 | + ->label(__('Email Verified At')) |
| 36 | + ->rules(['email', 'datetime']), |
| 37 | + |
| 38 | + ImportColumn::make('password') |
| 39 | + ->label(__('Password')) |
| 40 | + ->requiredMapping() |
| 41 | + ->rules(['required', 'max:255']), |
| 42 | + |
| 43 | + ImportColumn::make('created_at') |
| 44 | + ->label(__('Created At')) |
| 45 | + ->rules(['date']), |
| 46 | + ImportColumn::make('updated_at') |
| 47 | + ->label(__('Updated At')) |
| 48 | + ->rules(['date']), |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + public function resolveRecord(): ?User |
| 53 | + { |
| 54 | + // return User::firstOrNew([ |
| 55 | + // // Update existing records, matching them by `$this->data['column_name']` |
| 56 | + // 'email' => $this->data['email'], |
| 57 | + // ]); |
| 58 | + |
| 59 | + return new User(); |
| 60 | + } |
| 61 | + |
| 62 | + public static function getCompletedNotificationBody(Import $import): string |
| 63 | + { |
| 64 | + $body = 'Your user import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; |
| 65 | + |
| 66 | + if ($failedRowsCount = $import->getFailedRowsCount()) { |
| 67 | + $body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.'; |
| 68 | + } |
| 69 | + |
| 70 | + return $body; |
| 71 | + } |
| 72 | +} |
0 commit comments