sementara
This commit is contained in:
166
app/Imports/ReferensiLinkImport.php
Normal file
166
app/Imports/ReferensiLinkImport.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Imports;
|
||||
|
||||
use Modules\Lpj\app\Models\ReferensiLink;
|
||||
use Maatwebsite\Excel\Concerns\ToModel;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
use Maatwebsite\Excel\Concerns\Importable;
|
||||
use Maatwebsite\Excel\Concerns\SkipsFailures;
|
||||
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
|
||||
use Maatwebsite\Excel\Concerns\WithBatchInserts;
|
||||
use Maatwebsite\Excel\Concerns\WithChunkReading;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ReferensiLinkImport implements ToModel, WithHeadingRow, WithValidation, SkipsOnFailure, WithBatchInserts, WithChunkReading
|
||||
{
|
||||
use Importable, SkipsFailures;
|
||||
|
||||
protected $importedCount = 0;
|
||||
protected $failedCount = 0;
|
||||
protected $errors = [];
|
||||
|
||||
/**
|
||||
* Convert row to model
|
||||
*/
|
||||
public function model(array $row)
|
||||
{
|
||||
try {
|
||||
$this->importedCount++;
|
||||
|
||||
return new ReferensiLink([
|
||||
'name' => $row['nama'] ?? $row['name'],
|
||||
'link' => $this->formatLink($row['link'] ?? $row['url'] ?? ''),
|
||||
'kategori' => $row['kategori'] ?? $row['category'] ?? 'lainnya',
|
||||
'deskripsi' => $row['deskripsi'] ?? $row['description'] ?? null,
|
||||
'is_active' => $this->parseStatus($row['status_aktif'] ?? $row['is_active'] ?? 'aktif'),
|
||||
'urutan' => $this->parseUrutan($row['urutan'] ?? $row['order'] ?? 0),
|
||||
'created_by' => Auth::id(),
|
||||
'updated_by' => Auth::id(),
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->failedCount++;
|
||||
$this->errors[] = "Baris {$row['row_number']}: " . $e->getMessage();
|
||||
Log::error('Import ReferensiLink Error: ' . $e->getMessage(), ['row' => $row]);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validation rules
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'nama' => 'required|string|max:255',
|
||||
'name' => 'required|string|max:255',
|
||||
'link' => 'required|string|max:500',
|
||||
'url' => 'required|string|max:500',
|
||||
'kategori' => 'nullable|string|max:100',
|
||||
'category' => 'nullable|string|max:100',
|
||||
'deskripsi' => 'nullable|string|max:2000',
|
||||
'description' => 'nullable|string|max:2000',
|
||||
'status_aktif' => 'nullable|string|in:aktif,tidak aktif,1,0,true,false',
|
||||
'is_active' => 'nullable|string|in:aktif,tidak aktif,1,0,true,false',
|
||||
'urutan' => 'nullable|integer|min:0',
|
||||
'order' => 'nullable|integer|min:0',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom validation messages
|
||||
*/
|
||||
public function customValidationMessages()
|
||||
{
|
||||
return [
|
||||
'nama.required' => 'Nama wajib diisi',
|
||||
'name.required' => 'Nama wajib diisi',
|
||||
'link.required' => 'Link wajib diisi',
|
||||
'url.required' => 'Link wajib diisi',
|
||||
'link.url' => 'Link harus berupa URL yang valid',
|
||||
'url.url' => 'Link harus berupa URL yang valid',
|
||||
'kategori.max' => 'Kategori maksimal 100 karakter',
|
||||
'category.max' => 'Kategori maksimal 100 karakter',
|
||||
'deskripsi.max' => 'Deskripsi maksimal 2000 karakter',
|
||||
'description.max' => 'Deskripsi maksimal 2000 karakter',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch size for inserts
|
||||
*/
|
||||
public function batchSize(): int
|
||||
{
|
||||
return 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* Chunk size for reading
|
||||
*/
|
||||
public function chunkSize(): int
|
||||
{
|
||||
return 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format link to ensure it has protocol
|
||||
*/
|
||||
private function formatLink($link)
|
||||
{
|
||||
if (empty($link)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Remove any whitespace
|
||||
$link = trim($link);
|
||||
|
||||
// Add protocol if not present
|
||||
if (!preg_match('/^(https?:\/\/)/i', $link)) {
|
||||
$link = 'https://' . $link;
|
||||
}
|
||||
|
||||
return $link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse status value
|
||||
*/
|
||||
private function parseStatus($status)
|
||||
{
|
||||
if (empty($status)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$status = strtolower(trim($status));
|
||||
|
||||
return in_array($status, ['aktif', '1', 'true', 'yes', 'ya']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse order value
|
||||
*/
|
||||
private function parseUrutan($urutan)
|
||||
{
|
||||
if (empty($urutan)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) $urutan;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get import statistics
|
||||
*/
|
||||
public function getImportStats(): array
|
||||
{
|
||||
return [
|
||||
'imported' => $this->importedCount,
|
||||
'failed' => $this->failedCount,
|
||||
'errors' => $this->errors,
|
||||
'success' => $this->importedCount - $this->failedCount,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user