main
Ilham Wara Nugroho 2025-10-02 13:25:21 +07:00
parent aca6cf830b
commit f41401387e
2 changed files with 153 additions and 154 deletions

View File

@ -31,48 +31,53 @@ public function model(array $row)
{
// cek kalau row kosong jangan insert
if (
$row[0] === null &&
$row[1] === null &&
$row[2] === null &&
$row[3] === null &&
$row[4] === null &&
$row[5] === null &&
$row[6] === null &&
$row[7] === null &&
$row[8] === null &&
$row[9] === null &&
$row[10] === null &&
$row[11] === null &&
$row[12] === null &&
$row[13] === null &&
$row[14] === null &&
$row[15] === null &&
$row[16] === null &&
$row[17] === null
) {
return null; // baris kosong dilewati
try {
if (
$row[0] === null &&
$row[1] === null &&
$row[2] === null &&
$row[3] === null &&
$row[4] === null &&
$row[5] === null &&
$row[6] === null &&
$row[7] === null &&
$row[8] === null &&
$row[9] === null &&
$row[10] === null &&
$row[11] === null &&
$row[12] === null &&
$row[13] === null &&
$row[14] === null &&
$row[15] === null &&
$row[16] === null &&
$row[17] === null
) {
return null; // baris kosong dilewati
}
return new DatasetTable32([
'dataset_id' => $this->datasetId,
'created_by' => $this->userId,
'lokasi' => $row[1],
'tidak_sekolah_l' => $row[2],
'tidak_sekolah_p' => $row[3],
'sd_l' => $row[4],
'sd_p' => $row[5],
'sltp_l' => $row[6],
'sltp_p' => $row[7],
'slta_smk_l' => $row[8],
'slta_smk_p' => $row[9],
'pt_l' => $row[10],
'pt_p' => $row[11],
's1_l' => $row[12],
's1_p' => $row[13],
's2_l' => $row[14],
's2_p' => $row[15],
's3_l' => $row[16],
's3_p' => $row[17],
]);
} catch (\Throwable $th) {
dd($th->getMessage());
}
return new DatasetTable32([
'dataset_id' => $this->datasetId,
'created_by' => $this->userId,
'lokasi' => $row[1],
'tidak_sekolah_l' => $row[2],
'tidak_sekolah_p' => $row[3],
'sd_l' => $row[4],
'sd_p' => $row[5],
'sltp_l' => $row[6],
'sltp_p' => $row[7],
'slta_smk_l' => $row[8],
'slta_smk_p' => $row[9],
'pt_l' => $row[10],
'pt_p' => $row[11],
's1_l' => $row[12],
's1_p' => $row[13],
's2_l' => $row[14],
's2_p' => $row[15],
's3_l' => $row[16],
's3_p' => $row[17],
]);
}
}

View File

@ -52,132 +52,126 @@ class DatasetRepository implements DatasetRepositoryInterface
throw new \Exception("Import {$importClass} tidak ditemukan");
}
try {
// dd($data);
return DB::transaction(function () use ($modelClass, $importClass, $file, $data) {
// dd($data);
return DB::transaction(function () use ($modelClass, $importClass, $file, $data) {
try {
$keyId = decode_id($data['secure_id']);
$json = [];
$filePath = null;
if(@$file){
$file = $file;
$path = $file->getRealPath();
$spreadsheet = IOFactory::load($path);
$sheet = $spreadsheet->getActiveSheet();
$rows = $sheet->toArray();
$header = $rows[4]; // Baris pertama sebagai header
if (@$file) {
$file = $data['file'];
$destinationPath = public_path('uploads/dataset');
$current = Carbon::now()->format('Y/m/d');
$path = $destinationPath . '/' . $current;
$fileName = $file->getClientOriginalName();
$fileMime = $file->getClientMimeType();
$fileExtension = $file->getClientOriginalExtension();
$fileSize = $file->getSize();
if(($fileExtension != 'xls') && ($fileExtension != 'xlsx')){
return redirect()->back()->with([
'message' => 'Maaf File Harus Berupa xls,xlsx!',
'type' => "error"
]);
}
$newFilename = session('id').'_'.uniqid('file_') . '.' . $fileExtension;
try {
$keyId = decode_id($data['secure_id']);
$json = [];
$filePath = null;
if(@$file){
$file = $file;
$path = $file->getRealPath();
$spreadsheet = IOFactory::load($path);
$sheet = $spreadsheet->getActiveSheet();
$rows = $sheet->toArray();
$header = $rows[4]; // Baris pertama sebagai header
if (@$file) {
$file = $data['file'];
$destinationPath = public_path('uploads/dataset');
$current = Carbon::now()->format('Y/m/d');
$path = $destinationPath . '/' . $current;
$fileName = $file->getClientOriginalName();
$fileMime = $file->getClientMimeType();
$fileExtension = $file->getClientOriginalExtension();
$fileSize = $file->getSize();
if(($fileExtension != 'xls') && ($fileExtension != 'xlsx')){
return redirect()->back()->with([
'message' => 'Maaf File Harus Berupa xls,xlsx!',
'type' => "error"
]);
}
$newFilename = session('id').'_'.uniqid('file_') . '.' . $fileExtension;
if (!File::exists($path)) {
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
}
if (!File::exists($path)) {
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
}
$filePath = 'dataset/' . $current . '/' . $newFilename;
$uploaded = $file->move($path, $newFilename);
$filePath = 'dataset/' . $current . '/' . $newFilename;
$uploaded = $file->move($path, $newFilename);
for ($i = 5; $i < count($rows); $i++) {
$row = $rows[$i];
for ($i = 5; $i < count($rows); $i++) {
$row = $rows[$i];
// Skip baris kosong
if (collect($row)->filter()->isEmpty()) continue;
// Skip baris kosong
if (collect($row)->filter()->isEmpty()) continue;
$assoc = [];
foreach ($header as $j => $columnName) {
if($columnName != null){
$key = strtolower(str_replace(' ', '_', $columnName));
$assoc[$key] = $row[$j] ?? null;
}
$assoc = [];
foreach ($header as $j => $columnName) {
if($columnName != null){
$key = strtolower(str_replace(' ', '_', $columnName));
$assoc[$key] = $row[$j] ?? null;
}
$json[] = $assoc;
}
$json[] = $assoc;
}
}
if(@$keyId){
$masterModel = Dataset::find($keyId);
$masterModel->instansi_id = decode_id($data['instansi_id']);
$masterModel->template_id = decode_id($data['template_id']);
$masterModel->name = $data['name'];
$masterModel->publik = $data['publik'];
// $masterModel->tags = json_encode($data['tags']);
if(@$file){
$masterModel->data = json_encode($json);
$masterModel->file = $filePath;
}
$masterModel->deskripsi = $data['deskripsi'];
$masterModel->save();
if(@$file){
$modelClass::where('dataset_id',$masterModel->DatasetId)->delete();
}
}else{
$masterModel = Dataset::updateOrCreate([
'instansi_id' => decode_id($data['instansi_id']),
'template_id' => decode_id($data['template_id']),
'tahun' => $data['tahun'],
],[
'instansi_id' => decode_id($data['instansi_id']),
'template_id' => decode_id($data['template_id']),
'tahun' => $data['tahun'],
'name' => $data['name'],
'publik' => $data['publik'],
// 'tags' => json_encode($data['tags']),
'data' => json_encode($json),
'file' => $filePath,
'deskripsi' => $data['deskripsi'],
'created_by' => auth()->user()->id,
]);
}
if(@$file){
Excel::import(new $importClass($masterModel->DatasetId,auth()->user()->id),$path.'/'.$newFilename);
}
return $masterModel;
}catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
$failures = $e->failures();
foreach ($failures as $failure) {
// Kolom & baris error
dump('Row: '.$failure->row());
dump('Attribute: '.$failure->attribute());
dump('Errors: ', $failure->errors());
dump('Values: ', $failure->values());
}
return $failures;
} catch (Exception $e) {
dd($e->getMessage());
}
});
} catch (Exception $e) {
dd($e->getMessage());
}
if(@$keyId){
$masterModel = Dataset::find($keyId);
$masterModel->instansi_id = decode_id($data['instansi_id']);
$masterModel->template_id = decode_id($data['template_id']);
$masterModel->name = $data['name'];
$masterModel->publik = $data['publik'];
// $masterModel->tags = json_encode($data['tags']);
if(@$file){
$masterModel->data = json_encode($json);
$masterModel->file = $filePath;
}
$masterModel->deskripsi = $data['deskripsi'];
$masterModel->save();
if(@$file){
$modelClass::where('dataset_id',$masterModel->DatasetId)->delete();
}
}else{
$masterModel = Dataset::updateOrCreate([
'instansi_id' => decode_id($data['instansi_id']),
'template_id' => decode_id($data['template_id']),
'tahun' => $data['tahun'],
],[
'instansi_id' => decode_id($data['instansi_id']),
'template_id' => decode_id($data['template_id']),
'tahun' => $data['tahun'],
'name' => $data['name'],
'publik' => $data['publik'],
// 'tags' => json_encode($data['tags']),
'data' => json_encode($json),
'file' => $filePath,
'deskripsi' => $data['deskripsi'],
'created_by' => auth()->user()->id,
]);
}
if(@$file){
Excel::import(new $importClass($masterModel->DatasetId,auth()->user()->id),$path.'/'.$newFilename);
}
return $masterModel;
}catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
$failures = $e->failures();
foreach ($failures as $failure) {
// Kolom & baris error
dump('Row: '.$failure->row());
dump('Attribute: '.$failure->attribute());
dump('Errors: ', $failure->errors());
dump('Values: ', $failure->values());
}
return $failures;
} catch (Exception $e) {
dd($e->getMessage());
}
});
}
public function getChartData($modelClass,$datasetId,$templateId)