diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 5655544..fc20fbd 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -25,6 +25,7 @@ class DashboardController extends Controller public function index(){ $katDok = MasterKategori::where('statusenabled', true)->select('master_kategori_directory_id', 'nama_kategori_directory')->get(); $klasifikasiDok = MasterKlasifikasi::where('statusenabled', true)->select('master_klasifikasi_directory_id', 'nama_klasifikasi_directory')->get(); + $prefillFilter = session()->pull('dashboard_prefill'); $authMapping = auth()->user()?->dataUser?->mappingUnitKerjaPegawai[0]; $authUnitKerja = $authMapping->objectunitkerjapegawaifk; @@ -37,13 +38,37 @@ class DashboardController extends Controller 'klasifikasiDok' => $klasifikasiDok, 'authUnitKerja' => $authUnitKerja, 'authSubUnitKerja' => $authSubUnitKerja, - 'allAkses' => $allAkses ?? null + 'allAkses' => $allAkses ?? null, + 'prefillFilter' => $prefillFilter ]; return view('dashboard.index', $payload); } + public function setDashboardPrefill(Request $request) + { + $payload = [ + 'unitId' => (string) $request->input('unitId', ''), + 'subId' => (string) $request->input('subId', ''), + 'kategoriId' => (string) $request->input('kategoriId', ''), + 'unitName' => (string) $request->input('unitName', ''), + 'subName' => (string) $request->input('subName', ''), + 'kategoriName' => (string) $request->input('kategoriName', ''), + ]; + + session(['dashboard_prefill' => $payload]); + + return response()->json([ + 'status' => true, + 'message' => 'Prefill disimpan' + ]); + } + public function dataUnitKerja(){ $user = auth()->user()?->dataUser; + $entryPegawaiId = auth()->user()?->objectpegawaifk; + $authMapping = $user?->mappingUnitKerjaPegawai[0] ?? null; + $authUnit = $authMapping?->objectunitkerjapegawaifk; + $authSub = $authMapping?->objectsubunitkerjapegawaifk; $akses = AksesFile::where(['pegawai_id' => $user->id, 'statusenabled' => true])->first(); $aksesAll = $akses?->akses ?? $akses?->all_akses ?? false; $detailUnitIds = collect(); @@ -62,6 +87,11 @@ class DashboardController extends Controller $allowedUnitIds = collect([$akses->unit_akses]); } } + $limitPrivateToSubUnit = false; + if (!$aksesAll && !$allowedUnitIds && $authUnit) { + $allowedUnitIds = collect([$authUnit]); + $limitPrivateToSubUnit = true; + } $kategori = request('kategori'); $filterUnit = request('unitKerja'); @@ -70,28 +100,61 @@ class DashboardController extends Controller $subArray = $subUnit ? explode(',', $subUnit) : []; $katArray = $kategori ? explode(',', $kategori) : []; $katDok = MasterKategori::when($katArray, fn($q) => $q->whereIn('master_kategori_directory_id', $katArray))->where('statusenabled', true)->select('master_kategori_directory_id', 'nama_kategori_directory')->get(); + + $applyFileFilters = function ($q) use ($keyword, $katArray, $subArray, $entryPegawaiId) { + $q->where(function($subQuery) use ($entryPegawaiId){ + $subQuery->where('status_action', '!=', 'rejected') + ->orWhere(function ($pending) use ($entryPegawaiId) { + $pending->whereNull('status_action') + ->where('pegawai_id_entry', $entryPegawaiId); + }); + }) + ->when($subArray, fn($sq) => $sq->whereIn('id_sub_unit_kerja', $subArray)) + ->when($katArray, fn($sq) => $sq->whereIn('master_kategori_directory_id', $katArray)) + ->when($keyword, fn($sq) => + $sq->where(function ($query) use ($keyword) { + $query->where('file', 'ilike', "%{$keyword}%"); + }) + ); + }; + + $applyAccessFilter = function ($q) use ($aksesAll, $allowedUnitIds, $limitPrivateToSubUnit, $authSub) { + if ($aksesAll) { + return; + } + $hasPrivateScope = ($allowedUnitIds && $allowedUnitIds->isNotEmpty()) + || ($limitPrivateToSubUnit && $authSub); + $q->where(function ($query) use ($allowedUnitIds, $limitPrivateToSubUnit, $authSub, $hasPrivateScope) { + $query->where('permission_file', true); + if ($hasPrivateScope) { + $query->orWhere(function ($sub) use ($allowedUnitIds, $limitPrivateToSubUnit, $authSub) { + $sub->where('permission_file', false); + if ($allowedUnitIds && $allowedUnitIds->isNotEmpty()) { + $sub->whereIn('id_unit_kerja', $allowedUnitIds); + } + if ($limitPrivateToSubUnit && $authSub) { + $sub->where('id_sub_unit_kerja', $authSub); + } + }); + } + }); + }; + + $applyFileQuery = function ($q) use ($applyFileFilters, $applyAccessFilter) { + $applyFileFilters($q); + $applyAccessFilter($q); + }; + if ($katArray && $filterUnit && $subArray) { /* mode pencarian lengkap */ - if ($allowedUnitIds && !$allowedUnitIds->contains((int) $filterUnit)) { - $unitKerja = collect(); - } else { $unitKerja = UnitKerja::where('statusenabled', true) ->where('id', $filterUnit) ->with(['subUnitKerja' => fn($q) => $q->whereIn('id', $subArray) - ->with(['fileDirectory' => fn($q) => $q - ->where('id_unit_kerja', $filterUnit)->whereNotNull('status_action') - ->when($subArray, fn($q) => $q->whereIn('id_sub_unit_kerja', $subArray)) - ->when($katArray, fn($q) => $q->whereIn('master_kategori_directory_id', $katArray)) - ->when($keyword, fn($q) => - $q->where(function($query) use ($keyword) { - $query->where('file', 'ilike', "%{$keyword}%"); - }) - ) - ]) + ->whereHas('fileDirectory', fn($f) => $applyFileQuery($f)) + ->with(['fileDirectory' => fn($f) => $applyFileQuery($f)]) ]) ->select('id', 'name') ->get(); - } } elseif ($aksesAll) { /* all akses */ @@ -107,33 +170,36 @@ class DashboardController extends Controller } elseif ($allowedUnitIds) { $unitKerja = UnitKerja::where('statusenabled', true) - ->whereIn('id', $allowedUnitIds) + ->where(function ($q) use ($allowedUnitIds, $applyFileQuery) { + if ($allowedUnitIds && $allowedUnitIds->isNotEmpty()) { + $q->whereIn('id', $allowedUnitIds); + } + $q->orWhereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f)); + }) ->with([ - 'subUnitKerja' => fn($q) => $q->with([ - 'fileDirectory' => fn($f) => $f->whereNotNull('status_action')->when($keyword, fn($q) => - $q->where(function($query) use ($keyword) { - $query->where('file', 'ilike', "%{$keyword}%"); - }) - ) - ]) + 'subUnitKerja' => fn($q) => $q->whereHas('fileDirectory', fn($f) => $applyFileQuery($f)) + ->with(['fileDirectory' => fn($f) => $applyFileQuery($f)]) ]) ->select('id', 'name') ->get(); } else { - $authUnit = $user?->mappingUnitKerjaPegawai[0]?->objectunitkerjapegawaifk; - $authSub = $user?->mappingUnitKerjaPegawai[0]?->objectsubunitkerjapegawaifk; - $unitKerja = UnitKerja::where('statusenabled', true) - ->where('id', $authUnit) + ->where(function ($q) use ($authUnit, $applyFileQuery) { + if ($authUnit) { + $q->where('id', $authUnit); + } + $q->orWhereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f)); + }) ->with([ // 1. sub-unit milik user - 'subUnitKerja' => fn($q) => $q->where('id', $authSub) + 'subUnitKerja' => fn($q) => $q->where(function ($sq) use ($authSub, $applyFileQuery) { + if ($authSub) { + $sq->where('id', $authSub); + } + $sq->orWhereHas('fileDirectory', fn($f) => $applyFileQuery($f)); + }) ->with([ // 2. file-directory + filter keyword - 'fileDirectory' => fn($f) => $f->whereNotNull('status_action')->when($keyword, fn($q) => - $q->where(function($query) use ($keyword) { - $query->where('file', 'ilike', "%{$keyword}%"); - }) - ) + 'fileDirectory' => fn($f) => $applyFileQuery($f) ]) ]) ->select('id', 'name') @@ -295,7 +361,7 @@ class DashboardController extends Controller $paths = []; foreach ($rows as $r) { if(!empty($r['sub_unit_id'])){ - $files = FileDirectory::where('id_sub_unit_kerja', $r['sub_unit_id'])->where('statusenabled', true)->pluck('file'); + $files = FileDirectory::where('id_sub_unit_kerja', $r['sub_unit_id'])->where('statusenabled', true)->where('status_action', 'approved')->pluck('file'); $paths = array_merge($paths, $files->toArray()); } @@ -340,9 +406,9 @@ class DashboardController extends Controller $type = request('type'); if($type === "unit"){ - $data = FileDirectory::where('id_unit_kerja', $id)->where('statusenabled', true)->pluck('file'); + $data = FileDirectory::where('id_unit_kerja', $id)->where('statusenabled', true)->where('status_action', 'approved')->pluck('file'); }else{ - $data = FileDirectory::where('id_sub_unit_kerja', $id)->where('statusenabled', true)->pluck('file'); + $data = FileDirectory::where('id_sub_unit_kerja', $id)->where('statusenabled', true)->where('status_action', 'approved')->pluck('file'); } if (empty($data)) { return response()->json(['message' => 'File tidak ditemukan'], 404); @@ -393,11 +459,18 @@ class DashboardController extends Controller $perPage = (int) request('per_page', 10); $authUnitId = auth()->user()->dataUser?->mappingUnitKerjaPegawai[0]?->objectunitkerjapegawaifk; $user = auth()->user()?->dataUser; + $entryPegawaiId = auth()->user()?->objectpegawaifk; $akses = AksesFile::where(['pegawai_id' => $user->id, 'statusenabled' => true])->first(); $keyword = request('keyword'); - $query = FileDirectory::where('statusenabled', true)->whereNotNull('status_action') - ->when($keyword, function ($q) use ($keyword) { + $query = FileDirectory::where('statusenabled', true) + ->where(function($subQuery) use ($entryPegawaiId){ + $subQuery->where('status_action', '!=', 'rejected') + ->orWhere(function ($pending) use ($entryPegawaiId) { + $pending->whereNull('status_action') + ->where('pegawai_id_entry', $entryPegawaiId); + }); + })->when($keyword, function ($q) use ($keyword) { $q->where(function ($sub) use ($keyword) { $sub->where('file', 'ILIKE', "%{$keyword}%") ->orWhere('no_dokumen', 'ILIKE', "%{$keyword}%"); @@ -749,7 +822,10 @@ class DashboardController extends Controller $end = request('end_date'); $authUnit = auth()->user()->masterPersetujuan->details->pluck('unit_pegawai_id')->unique()->toArray(); - $query = FileDirectory::where('statusenabled', true)->whereNull('status_action')->whereIn('id_unit_kerja', $authUnit)->orderBy('entry_at','desc'); + $query = FileDirectory::where('statusenabled', true)->where(function($q){ + $q->where('status_action', '!=', 'approved') + ->orWhereNull('status_action'); + })->whereIn('id_unit_kerja', $authUnit)->orderBy('entry_at','desc'); if($keyword){ $query->where(function($q) use ($keyword){ $q->where('file', 'ILIKE', "%{$keyword}%") @@ -777,6 +853,7 @@ class DashboardController extends Controller 'entry_at' => $item->entry_at, 'tanggal_terbit' => $item->tanggal_terbit, 'permission_file' => $item->permission_file, + 'status_action' => $item->status_action ]; }); return response()->json([ diff --git a/public/js/dashboard/functions.js b/public/js/dashboard/functions.js index 8b23a57..21fbe0e 100644 --- a/public/js/dashboard/functions.js +++ b/public/js/dashboard/functions.js @@ -47,11 +47,16 @@ $(document).ready(function() { loadSubUnitKerja(initialUnit); } + let prefillSubIds = []; + let prefillSubMeta = {}; + // jalankan setiap kali unit_kerja berubah $('.unit_kerja').on('change', function(){ let idUnit = $(this).val(); if(idUnit){ - loadSubUnitKerja(idUnit); + loadSubUnitKerja(idUnit, prefillSubIds, prefillSubMeta); + prefillSubIds = []; + prefillSubMeta = {}; } }); @@ -62,9 +67,67 @@ $(document).ready(function() { }else{ selectOptionUnitKerjaV2(0); } + + if (window.__folderPrefillApplied) return; + const urlParams = new URLSearchParams(window.location.search); + const prefill = window.__prefillFromSession || null; + + const unitId = prefill?.unitId || urlParams.get('unitKerja'); + const subParam = prefill?.subId || urlParams.get('subUnit'); + const katParam = prefill?.kategoriId || urlParams.get('kategori'); + const unitName = prefill?.unitName || urlParams.get('unitName'); + const subName = prefill?.subName || urlParams.get('subName'); + const kategoriName = prefill?.kategoriName || urlParams.get('kategoriName'); + + let prefillSubIdsFinal = []; + let prefillKatIdsFinal = []; + + if (unitId) { + window.__folderPrefillApplied = true; + window.__prefillFromSession = null; + $('.unit_kerja').val(null).trigger('change'); + $('.sub_unit_kerja').val(null).trigger('change'); + $('.kategori_dok').val(null).trigger('change'); + + const unitLabel = unitName || unitId; + const unitOption = new Option(unitLabel, unitId, true, true); + const selectedSubIds = subParam ? Array.from(new Set(subParam.split(',').filter(Boolean))) : []; + const subNames = subName ? subName.split(',').map(s => s.trim()).filter(Boolean) : []; + + prefillSubIds = selectedSubIds; + prefillSubMeta = selectedSubIds.reduce((acc, id, idx) => { + let label = subNames[idx] || subNames[0] || id; + label = label.replace(/^\d+\s*\/\s*/, ''); + acc[id] = label; + return acc; + }, {}); + $('.unit_kerja').append(unitOption).trigger('change'); + + if (selectedSubIds.length) { + $('.sub_unit_kerja').val(selectedSubIds).trigger('change'); + } + + prefillSubIdsFinal = selectedSubIds; + } + + if (katParam) { + const katIds = Array.from(new Set(katParam.split(',').filter(Boolean))); + if (katIds.length) { + $('.kategori_dok').val(katIds).trigger('change'); + prefillKatIdsFinal = katIds; + } else if (kategoriName) { + const katOption = new Option(kategoriName, katParam, true, true); + $('.kategori_dok').append(katOption).trigger('change'); + prefillKatIdsFinal = katParam ? [katParam] : []; + } + } + + if ((unitId || subParam || katParam) && typeof index === 'function') { + index(prefillKatIdsFinal, unitId, prefillSubIdsFinal, ''); + } }); -function loadSubUnitKerja(unitId){ +function loadSubUnitKerja(unitId, selectedSubIds = [], selectedSubMeta = {}){ $('.sub_unit_kerja').empty().append(''); $.ajax({ @@ -73,10 +136,24 @@ function loadSubUnitKerja(unitId){ success: function(response) { if (response?.data) { response.data.forEach(unit => { - let selected = (authSubUnitKerja && unit.id === authSubUnitKerja.objectsubunitkerjapegawaifk); + let selected = false; + if (selectedSubIds.length) { + selected = selectedSubIds.includes(String(unit.id)); + } else if (authSubUnitKerja) { + selected = unit.id === authSubUnitKerja.objectsubunitkerjapegawaifk; + } const option = new Option(unit.name, unit.id, false, selected); $('.sub_unit_kerja').append(option); }); + if (selectedSubIds.length) { + selectedSubIds.forEach(id => { + if ($(`.sub_unit_kerja option[value="${id}"]`).length === 0) { + const label = selectedSubMeta[id] || id; + const opt = new Option(label, id, false, true); + $('.sub_unit_kerja').append(opt); + } + }); + } $('.sub_unit_kerja').trigger('change'); } } @@ -321,4 +398,3 @@ function selectOptionUnitKerjaV2(colCount) { } }); } - diff --git a/public/js/dashboard/index.js b/public/js/dashboard/index.js index 50ade92..27591c7 100644 --- a/public/js/dashboard/index.js +++ b/public/js/dashboard/index.js @@ -10,9 +10,18 @@ function renderTree(units, katDok, keyword) {
  • 📂 ${el.name} - + ${Array.isArray(el.sub_unit_kerja) && el.sub_unit_kerja.length > 0 ? `