82 lines
2.8 KiB
PHP
82 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Imports;
|
|
|
|
use App\Models\Student;
|
|
use App\Models\User;
|
|
use Filament\Actions\Imports\ImportColumn;
|
|
use Filament\Actions\Imports\Importer;
|
|
use Filament\Actions\Imports\Models\Import;
|
|
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
|
|
|
class StudentImporter extends Importer
|
|
{
|
|
protected static ?string $model = Student::class;
|
|
|
|
public static function getColumns(): array
|
|
{
|
|
return [
|
|
ImportColumn::make('full_name')
|
|
->requiredMapping()
|
|
->rules(['required', 'max:255']),
|
|
ImportColumn::make('nis')
|
|
->rules(['required','max:255']),
|
|
ImportColumn::make('nisn')
|
|
->rules(['required','max:255']),
|
|
ImportColumn::make('gender')
|
|
->requiredMapping()
|
|
->rules(['required', 'max:255']),
|
|
ImportColumn::make('birth_date')
|
|
->rules(['date']),
|
|
ImportColumn::make('birth_place')
|
|
->rules(['max:255']),
|
|
ImportColumn::make('address'),
|
|
ImportColumn::make('religion'),
|
|
ImportColumn::make('phone')
|
|
->rules(['required','max:255']),
|
|
ImportColumn::make('email')
|
|
->rules(['email', 'max:255']),
|
|
ImportColumn::make('parent_name')
|
|
->rules(['required','max:255']),
|
|
ImportColumn::make('parent_phone')
|
|
->rules(['required','max:255']),
|
|
];
|
|
}
|
|
|
|
public function resolveRecord(): ?Student
|
|
{
|
|
// Cek duplikat NIS
|
|
if (Student::where('nis', $this->data['nis'])->exists()) {
|
|
throw new RowImportFailedException('NIS sudah terdaftar');
|
|
}
|
|
|
|
// Cek duplikat NISN
|
|
if (Student::where('nisn', $this->data['nisn'])->exists()) {
|
|
throw new RowImportFailedException('NISN sudah terdaftar');
|
|
}
|
|
|
|
// Cek duplikat phone
|
|
if (Student::where('phone', $this->data['phone'])->exists()) {
|
|
throw new RowImportFailedException('Nomor telepon sudah terdaftar');
|
|
}
|
|
|
|
// Cek duplikat email di tabel users
|
|
if (User::where('email', $this->data['email'])->exists()) {
|
|
throw new RowImportFailedException('Email sudah digunakan');
|
|
}
|
|
|
|
return new Student();
|
|
}
|
|
|
|
public static function getCompletedNotificationBody(Import $import): string
|
|
{
|
|
$body = 'Your student import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';
|
|
|
|
if ($failedRowsCount = $import->getFailedRowsCount()) {
|
|
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.';
|
|
}
|
|
|
|
return $body;
|
|
}
|
|
}
|