diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 462b603..1334370 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Models\AksesFile; use App\Models\FileDirectory; +use App\Models\LogActivity; use App\Models\MasterKategori; use App\Models\MasterKlasifikasi; use App\Models\SubUnitKerja; @@ -12,6 +13,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; +use setasign\Fpdi\Fpdi; use ZipArchive; use function PHPUnit\Framework\isEmpty; @@ -182,32 +184,7 @@ class DashboardController extends Controller public function OptionUnitKerja(){ $q = request()->get('q'); - $authPegawai = auth()->user()?->dataUser; - - $authUnitKerja = optional($authPegawai->mappingUnitKerjaPegawai[0] ?? null)->objectunitkerjapegawaifk; - $authSubUnitKerja = optional($authPegawai->mappingUnitKerjaPegawai[0] ?? null)->objectsubunitkerjapegawaifk; - - $aksesFile = AksesFile::where('pegawai_id', $authPegawai->id) - ->where('statusenabled', true) - ->first(); $query = UnitKerja::where('statusenabled', true); - if($aksesFile){ - if($aksesFile->all_akses){ - - }elseif($aksesFile->unit_akses){ - $query->where('id', $aksesFile->unit_akses); - } - }else{ - $data= $query->where('id', $authUnitKerja) - ->with(['subUnitKerja' => function($query) use($authSubUnitKerja){ - $query->where('id', $authSubUnitKerja); - }])->select('id', 'name')->get(); - - return response()->json([ - 'status' => true, - 'data' => $data - ], 200); - } $data = $query->when($q, function ($query, $q){ $query->where('name', 'ILIKE', '%' .$q . '%'); @@ -220,30 +197,60 @@ class DashboardController extends Controller } public function deleteFile(string $id){ - $data = FileDirectory::where('file_directory_id', $id)->first(); - if(!$data){ - return response()->json([ - 'success' => false, - 'message' => 'File tidak ditemukan' - ]); - } - $oldPath= public_path('file/' . $data->file); - $fileInfo = pathinfo($data->file); - $newFileName = $fileInfo['filename'] . '_deleted.' . $fileInfo['extension']; - $newPath = public_path('file/' . $fileInfo['dirname'] . '/' . $newFileName); - if (file_exists($oldPath)) { - // pastikan folder tujuan ada - if (!is_dir(dirname($newPath))) { - mkdir(dirname($newPath), 0777, true); - } - rename($oldPath, $newPath); + DB::connection('dbDirectory')->beginTransaction(); + try { + $data = FileDirectory::where('file_directory_id', $id)->first(); + if(!$data){ + return response()->json([ + 'success' => false, + 'message' => 'File tidak ditemukan' + ]); } + $oldPath= public_path('file/' . $data->file); + $fileInfo = pathinfo($data->file); + $newFileName = $fileInfo['filename'] . '_deleted.' . $fileInfo['extension']; + $newPath = public_path('file/' . $fileInfo['dirname'] . '/' . $newFileName); + if (file_exists($oldPath)) { + // pastikan folder tujuan ada + if (!is_dir(dirname($newPath))) { + mkdir(dirname($newPath), 0777, true); + } + rename($oldPath, $newPath); + } + + $data->update(['statusenabled' => false, 'file' => $fileInfo['dirname'].'/'. $newFileName]); + + $payloadLog = [ + 'file_directory_id' => $data->file_directory_id, + 'pegawai_id_entry' => $data->pegawai_id_entry, + 'pegawai_nama_entry' => $data->pegawai_nama_entry, + 'entry_at' => $data->entry_at, + 'action_type' => 'Hapus Dokumen', + 'statusenabled' => true, + 'mod_change' => $data->entry_at, + 'id_unit_kerja' => $data->id_unit_kerja, + 'id_sub_unit_kerja' => $data->id_sub_unit_kerja, + 'file' => $data->file, + 'tanggal_terbit' => $data->tanggal_terbit, + 'no_dokumen' => $data->no_dokumen, + 'permission_file' => $data->permission_file, + ]; + LogActivity::create($payloadLog); + + DB::connection('dbDirectory')->commit(); + return response()->json([ + 'success' => true, + 'message' => 'Berhasil menghapus data' + ]); + } catch (\Throwable $th) { + DB::connection('dbDirectory')->rollBack(); + return response()->json([ + 'success' => false, + 'message' => 'Gagal menghapus data' + ]); + //throw $th; + } - $data->update(['statusenabled' => false, 'file' => $fileInfo['dirname'].'/'. $newFileName]); - return response()->json([ - 'success' => true, - 'message' => 'Berhasil menghapus data' - ]); } public function optionSubUnitKerja(string $id){ @@ -349,8 +356,15 @@ class DashboardController extends Controller } public function dashboardVersion2(){ + $katDok = MasterKategori::where('statusenabled', true)->select('master_kategori_directory_id', 'nama_kategori_directory')->get(); + $authMapping = auth()->user()?->dataUser?->mappingUnitKerjaPegawai[0]; + $authUnitKerja = $authMapping->objectunitkerjapegawaifk; + $authSubUnitKerja = $authMapping->objectsubunitkerjapegawaifk; $data = [ - 'title' => 'Dashboard', + 'title' => 'Dashboard', + 'katDok' => $katDok, + 'authUnitKerja' => $authUnitKerja, + 'authSubUnitKerja' => $authSubUnitKerja, ]; return view('dashboardV2.index', $data); } @@ -372,4 +386,148 @@ class DashboardController extends Controller ]; return response()->json($payload); } + + public function storeVersion2(){ + DB::connection('dbDirectory')->beginTransaction(); + try { + $datas = request('data'); + foreach ($datas as $data) { + list($id_unit_kerja, $nama_unit_kerja) = explode('/', $data['id_unit_kerja'],2); + list($id_sub_unit_kerja, $nama_sub_unit_kerja) = explode('/', $data['id_sub_unit_kerja'],2); + list($master_kategori_directory_id, $nama_kategori) = explode('/', $data['master_kategori_directory_id'],2); + $payload = [ + 'id_unit_kerja' => $id_unit_kerja, + 'id_sub_unit_kerja' => $id_sub_unit_kerja, + 'master_kategori_directory_id' => $master_kategori_directory_id, + 'pegawai_id_entry' => auth()->user()->dataUser->id ?? 1, + 'pegawai_nama_entry' => auth()->user()->dataUser->namalengkap ?? null, + 'tanggal_terbit' => $data['date_active'] ?? null, + 'no_dokumen' => $data['no_dokumen'] ?? null, + 'permission_file' => $data['is_permission'] === "1" ? true : false, + ]; + if(!empty($data['file'])){ + $file = $data['file']; + $imageName = $file->getClientOriginalName(); + $path = "{$nama_unit_kerja}/{$nama_sub_unit_kerja}/{$nama_kategori}"; + $file->storeAs($path, $imageName, 'file_directory'); + $payload['file'] =$path .'/' .$imageName; + } + $fd = FileDirectory::create($payload); + $payloadLog = [ + 'file_directory_id' => $fd->file_directory_id, + 'pegawai_id_entry' => $fd->pegawai_id_entry, + 'pegawai_nama_entry' => $fd->pegawai_nama_entry, + 'entry_at' => $fd->entry_at, + 'action_type' => 'Upload Dokumen', + 'statusenabled' => true, + 'mod_change' => $fd->entry_at, + 'id_unit_kerja' => $fd->id_unit_kerja, + 'id_sub_unit_kerja' => $fd->id_sub_unit_kerja, + 'file' => $fd->file, + 'tanggal_terbit' => $fd->tanggal_terbit, + 'no_dokumen' => $fd->no_dokumen, + 'permission_file' => $fd->permission_file, + ]; + LogActivity::create($payloadLog); + } + DB::connection('dbDirectory')->commit(); + return response()->json([ + 'status' => true, + 'message' => 'Data berhasil disimpan' + ], 200); + } catch (\Throwable $th) { + DB::connection('dbDirectory')->rollback(); + return response()->json([ + 'status' => false, + 'message' => $th->getMessage() + ], 500); + } + } + + public function dataPdf($fileDirectoryId) + { + $data = FileDirectory::where('file_directory_id', $fileDirectoryId)->first(); + + $filePath = public_path('file/' . $data->file); + + if (!file_exists($filePath)) { + abort(404, 'PDF Tidak ditemukan'); + } + + $stampFile = public_path('assets/copy.png'); + if (!file_exists($stampFile)) { + // kalau watermark tidak ada, tampilkan file asli + return response()->file($filePath, [ + 'Content-Type' => 'application/pdf', + 'Content-Disposition' => 'inline; filename="preview.pdf"', + 'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0', + ]); + } + + $tempConverted = storage_path('app/temp/' . uniqid('conv_') . '.pdf'); + + try { + // coba watermark langsung + return $this->watermarkCenterAndStream($filePath, $stampFile); + + } catch (\Throwable $e) { + // kalau gagal (PDF modern) -> convert dulu + // dd($e); + $this->convertWithGhostscript($filePath, $tempConverted); + + $resp = $this->watermarkCenterAndStream($tempConverted, $stampFile); + + @unlink($tempConverted); + return $resp; + } + } + + private function watermarkCenterAndStream(string $pdfPath, string $stampFile) + { + $pdf = new Fpdi(); + $pageCount = $pdf->setSourceFile($pdfPath); + + for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) { + $tplId = $pdf->importPage($pageNo); + $size = $pdf->getTemplateSize($tplId); + + $pdf->AddPage($size['orientation'], [$size['width'], $size['height']]); + $pdf->useTemplate($tplId); + + // watermark tengah (skala adaptif) + $stampW = $size['width'] * 0.60; + $stampH = $stampW * 0.75; // sesuaikan rasio gambar kalau perlu + + $x = ($size['width'] - $stampW) / 2; + $y = ($size['height'] - $stampH) / 2; + + $pdf->Image($stampFile, $x, $y, $stampW, $stampH); + } + + $output = $pdf->Output('S'); + + return response($output, 200, [ + 'Content-Type' => 'application/pdf', + 'Content-Disposition' => 'inline; filename="preview.pdf"', + 'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0', + ]); + } + + private function convertWithGhostscript(string $inputPdf, string $outputPdf): void + { + $gs = config('services.ghostscript.path'); + + if (!$gs || !file_exists($gs)) { + throw new \RuntimeException('Ghostscript tidak ditemukan. Cek GHOSTSCRIPT_PATH di .env'); + } + + $cmd = "\"{$gs}\" -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress " + . "-dNOPAUSE -dBATCH -dQUIET -sOutputFile=\"{$outputPdf}\" \"{$inputPdf}\""; + + exec($cmd, $out, $code); + + if ($code !== 0 || !file_exists($outputPdf)) { + throw new \RuntimeException('Convert Ghostscript gagal (code=' . $code . ')'); + } + } } diff --git a/app/Models/FileDirectory.php b/app/Models/FileDirectory.php index f386aeb..a75dcbe 100644 --- a/app/Models/FileDirectory.php +++ b/app/Models/FileDirectory.php @@ -12,10 +12,4 @@ class FileDirectory extends Model protected $primaryKey = 'file_directory_id'; protected $guarded = ['file_directory_id']; - // protected $with = ['klasifikasi']; - - // // public function klasifikasi(){ - // // return $this->belongsTo(MasterKlasifikasi::class, 'master_klasifikasi_directory_id', 'master_klasifikasi_directory_id'); - // // } - } diff --git a/app/Models/LogActivity.php b/app/Models/LogActivity.php new file mode 100644 index 0000000..8e50b20 --- /dev/null +++ b/app/Models/LogActivity.php @@ -0,0 +1,14 @@ + env('SLACK_BOT_USER_DEFAULT_CHANNEL'), ], ], + 'ghostscript' => [ + 'path' => env('GHOSTSCRIPT_PATH'), + ], ]; diff --git a/public/assets/copy.png b/public/assets/copy.png new file mode 100644 index 0000000..d65b887 Binary files /dev/null and b/public/assets/copy.png differ diff --git a/public/js/dashboard/index.js b/public/js/dashboard/index.js index 07563fb..5ca4a28 100644 --- a/public/js/dashboard/index.js +++ b/public/js/dashboard/index.js @@ -40,33 +40,20 @@ function renderTree(units, katDok, keyword) {
Tidak bisa preview file ini. Silakan download:
- โฌ๏ธ Download - `; - } + previewBox.innerHTML = ``; + $("#previewModal").modal('show') // ๐ฅ Tampilkan modal $("#previewModal").modal("show"); @@ -261,21 +253,21 @@ } - $("#download-file").off('click').on('click', function(){ - if(currentFile){ - let link = document.createElement('a'); - link.href = 'file/' + currentFile; // alamat file - link.download = currentFile.split('/').pop(); // nama file otomatis - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); - }else{ - console.error('error'); + // $("#download-file").off('click').on('click', function(){ + // if(currentFile){ + // let link = document.createElement('a'); + // link.href = 'file/' + currentFile; // alamat file + // link.download = currentFile.split('/').pop(); // nama file otomatis + // document.body.appendChild(link); + // link.click(); + // document.body.removeChild(link); + // }else{ + // console.error('error'); - } - }) + // } + // }) }); - + diff --git a/resources/views/dashboard/modal/view.blade.php b/resources/views/dashboard/modal/view.blade.php index 88cfc68..bf83862 100644 --- a/resources/views/dashboard/modal/view.blade.php +++ b/resources/views/dashboard/modal/view.blade.php @@ -15,7 +15,6 @@