From b778b7dee4e4d5aa30c027bc545404a22f352364 Mon Sep 17 00:00:00 2001 From: JokoPrasetio Date: Tue, 9 Sep 2025 14:42:27 +0700 Subject: [PATCH] akses done --- app/Http/Controllers/AksesFileController.php | 170 +++++++++++ app/Http/Controllers/DashboardController.php | 39 ++- app/Models/AksesFile.php | 24 ++ app/Models/FileDirectory.php | 6 + public/js/akses/_init.js | 7 + public/js/akses/action.js | 265 ++++++++++++++++++ public/js/akses/dt.js | 57 ++++ public/js/akses/functions.js | 130 +++++++++ public/js/dashboard/index.js | 8 +- resources/views/akses/index.blade.php | 30 ++ resources/views/akses/modal/add.blade.php | 53 ++++ resources/views/akses/modal/edit.blade.php | 50 ++++ resources/views/dashboard/index.blade.php | 25 +- .../views/layout/partials/sidenav.blade.php | 12 +- routes/web.php | 5 + 15 files changed, 850 insertions(+), 31 deletions(-) create mode 100644 app/Http/Controllers/AksesFileController.php create mode 100644 app/Models/AksesFile.php create mode 100644 public/js/akses/_init.js create mode 100644 public/js/akses/action.js create mode 100644 public/js/akses/dt.js create mode 100644 public/js/akses/functions.js create mode 100644 resources/views/akses/index.blade.php create mode 100644 resources/views/akses/modal/add.blade.php create mode 100644 resources/views/akses/modal/edit.blade.php diff --git a/app/Http/Controllers/AksesFileController.php b/app/Http/Controllers/AksesFileController.php new file mode 100644 index 0000000..239b59c --- /dev/null +++ b/app/Http/Controllers/AksesFileController.php @@ -0,0 +1,170 @@ + 'Akses' + ]; + return view('akses.index', $data); + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + DB::connection('dbDirectory')->beginTransaction(); + try { + $datas = request('data'); + foreach ($datas as $data) { + $payload = [ + 'pegawai_id' => $data['pegawai_id'], + 'pegawai_id_entry' => auth()->user()?->dataUser?->id, + 'pegawai_nama_entry' => auth()->user()?->dataUser?->namalengkap, + 'entry_at' => Carbon::now()->format('Y-m-d H:i:s.u'), + ]; + if($data['akses'] === "all"){ + $payload['all_akses'] = true; + }else{ + $payload['all_akses'] = false; + $payload['unit_akses'] = $data['unit_akses']; + + } + AksesFile::create($payload); + } + 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); + } + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + // + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + try { + $data = AksesFile::where('akses_file_id', $id)->first(); + $payload = [ + 'pegawai_id' => request('pegawai_id'), + 'pegawai_id_modified' => auth()->user()?->dataUser?->id, + 'pegawai_nama_modified' => auth()->user()?->dataUser?->namalengkap, + 'modified_at' => Carbon::now()->format('Y-m-d H:i:s.u'), + 'all_akses' => request('akses') === "all" ? true : false, + 'unit_akses' => request('unit_akses') + ]; + $data->update($payload); + + return response()->json([ + 'status' => true, + 'message' => 'Data berhasil diperbarui', + ], 200); + } catch (\Throwable $th) { + DB::connection('dbDirectory')->rollBack(); + return response()->json([ + 'status' => false, + 'message' => $th->getMessage() + ], 500); + } + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + $data = AksesFile::where('akses_file_id', $id)->first(); + if(!$data){ + return response()->json([ + 'status' => false, + 'message' => 'Data tidak ditemukan' + ], 404); + } + $payload =[ + 'statusenabled' => false + ]; + $data->update($payload); + return response()->json([ + 'status' => true, + 'message' => 'Data berhasil dihapus' + ], 200); + } + + + public function datatable(){ + return AksesFile::where('statusenabled', true)->get(); + } + + public function optionPegawai(){ + $q = request()->get('q'); + $query = DataUser::where('statusenabled', true); + $data = $query->when($q, function($query, $q){ + $query->where('nama', 'ILIKE', '%' . $q . '%'); + })->select('id','nama')->get(); + + return response()->json([ + 'status' => true, + 'data' => $data + ], 200); + } + + public function optionUnitKerja(){ + $q = request()->get('q'); + $query = UnitKerja::where('statusenabled', true); + $data = $query->when($q, function($query, $q){ + $query->where('name', 'ILIKE', '%' . $q . '%'); + })->select('id','name')->get(); + + return response()->json([ + 'status' => true, + 'data' => $data + ], 200); + } +} diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 3a51650..22364de 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\AksesFile; use App\Models\FileDirectory; use App\Models\MasterKategori; use App\Models\MasterKlasifikasi; @@ -41,11 +42,11 @@ class DashboardController extends Controller $klasifikasi = request('klasifikasi'); - - $unitKerja = UnitKerja::where('statusenabled', true) - ->where('id', $authUnitKerja) - ->with(['subUnitKerja' => function ($query) use ($authSubUnitKerja, $kategori, $filterUnit, $subUnit, $klasifikasi) { - $query->where('id', $authSubUnitKerja) + if($kategori && $filterUnit && $subUnit && $klasifikasi){ + $unitKerja = UnitKerja::where('statusenabled', true) + ->where('id', $filterUnit) + ->with(['subUnitKerja' => function ($query) use ($kategori, $filterUnit, $subUnit, $klasifikasi) { + $query->where('id', $subUnit) ->with(['fileDirectory' => function ($q) use ($kategori, $filterUnit, $subUnit, $klasifikasi) { $q->when($filterUnit, function ($subQ) use ($filterUnit) { $subQ->where('id_unit_kerja', $filterUnit); @@ -60,10 +61,11 @@ class DashboardController extends Controller }]) ->select('id', 'name') ->get(); + } $katDok = MasterKategori::where('statusenabled', true)->select('master_kategori_directory_id', 'nama_kategori_directory')->get(); $data = [ - 'unitKerja' => $unitKerja, + 'unitKerja' => $unitKerja ?? null, 'katDok' => $katDok ]; return response()->json([ @@ -109,18 +111,29 @@ class DashboardController extends Controller public function OptionUnitKerja(){ $q = request()->get('q'); - $authMapping = auth()->user()?->dataUser?->mappingUnitKerjaPegawai[0]; - $authUnitKerja = $authMapping?->objectunitkerjapegawaifk; - $authSubUnitKerja = $authMapping?->objectsubunitkerjapegawaifk; - $akses = false; + $authPegawai = auth()->user()?->dataUser; - $query = UnitKerja::query(); - if(!$akses){ - $query->where(['statusenabled' => true, 'id' => $authUnitKerja]) + $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{ + $query->where('id', $authUnitKerja) ->with(['subUnitKerja' => function($query) use($authSubUnitKerja){ $query->where('id', $authSubUnitKerja); }]); } + $data = $query->when($q, function ($query, $q){ $query->where('name', 'ILIKE', '%' .$q . '%'); }) diff --git a/app/Models/AksesFile.php b/app/Models/AksesFile.php new file mode 100644 index 0000000..065fbe7 --- /dev/null +++ b/app/Models/AksesFile.php @@ -0,0 +1,24 @@ +belongsTo(DataUser::class, 'pegawai_id', 'id')->select('id', 'namalengkap'); + } + + public function akses(){ + return $this->belongsTo(UnitKerja::class, 'unit_akses', 'id')->select('id', 'name'); + } +} diff --git a/app/Models/FileDirectory.php b/app/Models/FileDirectory.php index fae2585..6f5cb36 100644 --- a/app/Models/FileDirectory.php +++ b/app/Models/FileDirectory.php @@ -11,4 +11,10 @@ class FileDirectory extends Model public $timestamps = false; 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/public/js/akses/_init.js b/public/js/akses/_init.js new file mode 100644 index 0000000..23c8ae4 --- /dev/null +++ b/public/js/akses/_init.js @@ -0,0 +1,7 @@ +const datatableAkses = $("#table_akses") + +const formCreateAkses = $("#formAkses") +const modalCreate = document.getElementById('modalAkses') + +const modalEdit = document.getElementById('modalEditAkses') +const formEditAkses = $("#formEditAkses") diff --git a/public/js/akses/action.js b/public/js/akses/action.js new file mode 100644 index 0000000..c51e375 --- /dev/null +++ b/public/js/akses/action.js @@ -0,0 +1,265 @@ +formCreateAkses.on('submit', function(e){ + e.preventDefault(); + + const form = this; + const formData = new FormData(form); + + fetch(`/akses`, { + method: 'POST', + headers: { + 'X-CSRF-TOKEN': document.querySelector('input[name="_token"]').value, + }, + body: formData + }).then(async(res) => { + const responseData = await res.json(); + if (responseData.status) { + const handler = function () { + Swal.fire({ + icon: 'success', + title: 'Berhasil', + text: responseData.message || 'Berhasil melakukan aksi!', + timer: 2000, + showConfirmButton: false, + backdrop: true, + }); + $("#col_add_akses").html(''); + colCount = 1; // reset counter + formCreateAkses.find('select').val(null).trigger('change'); + datatableAkses.bootstrapTable('refresh'); + modalCreate.removeEventListener('hidden.bs.modal', handler); + }; + modalCreate.addEventListener('hidden.bs.modal', handler); + bootstrap.Modal.getInstance(modalCreate).hide(); + } else { + throw new Error(responseData.message || 'Terjadi kesalahan saat menyimpan data.'); + } + + }).catch(err => { + if (err.message) { + Swal.fire({ + icon: 'error', + title: 'Gagal', + text: err.message + }); + } + }); +}); + + +function deleteAkses(e){ + let id =$(e).data('akses_file_id') + Swal.fire({ + title: "Apakah kamu yakin ingin menghapus akses?", + text : $(e).data('name') + ' sebagai ' + $(e).data('akses'), + icon: "warning", + showCancelButton: true, + backdrop: true, + }).then((result) => { + if(result.isConfirmed){ + fetch(`/akses/${id}`, { + method:'DELETE', + headers: { + "X-CSRF-TOKEN": document.querySelector('input[name="_token"]').value, + "Content-Type": "application/json" + } + }).then((response) => { + if(!response.ok){ + throw new Error("Network response was not ok"); + } + return response.json(); + }) + .then((data) => { + if(data.status){ + Swal.fire({ + title: "Success", + text: "Data berhasil dihapus", + icon:"success", + showConfirmButton: false, + timer: 1000 + }).then(() => { + datatableAkses.bootstrapTable("refresh") + }) + }else{ + Swal.fire({ + title: "Error!", + text: data.message || "Failed to delete Item.", + icon: "error" + }); + } + }) + .catch(error => { + Swal.fire({ + title: "Error!", + text: "Something went wrong. Please try again later.", + icon: "error" + }); + }); + } + }) +} + + +function editAkses(e){ + const data = $(e).data(); + new bootstrap.Modal(modalEdit).show(); + formEditAkses.attr('action', `/akses/${data.akses_file_id}`) + $("#akses_id_edit").val(data.akses === 1 ? 'all' : 'unit') + selectAksesEdit() + + selectOptionPegawaiEdit() + selectOptionUnitEdit() + if (data.pegawai_id) { + setOldSelect2Value('#pegawai_id_edit', data.pegawai_id, data.pegawai_nama); + } + + if (data.unit_id) { + setOldSelect2Value('#unit_akses_edit', data.unit_id, data.unit_nama); + } +} + + +document.addEventListener('change', function(e){ + e.preventDefault() + if(e.target){ + selectAksesEdit() + } +}) + +function selectAksesEdit(){ + let aksesId = $(`#akses_id_edit`); + let colUnit = $(`#col_select_unit_edit`) + if(aksesId.val() === "all"){ + colUnit.addClass('d-none') + }else{ + colUnit.removeClass('d-none') + } +} + + +function selectOptionPegawaiEdit() { + let selectPegawai = $(`#pegawai_id_edit`); + // inisialisasi select2 untuk Unit Kerja + selectPegawai.select2({ + placeholder: '-- Pilih Pegawai --', + allowClear:true, + width: '100%', + dropdownParent: selectPegawai.parent(), + ajax:{ + url : '/select-pegawai', + dataType: 'json', + delay: 250, + data: function(params){ + return { q: params.term } + }, + processResults: function(data){ + return { + results : data?.data.map(item => ({ + id: item.id, + text: item.nama, + })) + } + }, + cache: true, + }, + minimumInputLength: 1, + }); +} + +function selectOptionUnitEdit(oldId = null) { + let selectUnit = $(`#unit_akses_edit`); + // inisialisasi select2 untuk Unit Kerja + selectUnit.select2({ + placeholder: '-- Pilih Unit --', + allowClear:true, + width: '100%', + dropdownParent: selectUnit.parent(), + ajax:{ + url : '/select-unit-kerja-option', + dataType: 'json', + delay: 250, + data: function(params){ + return { q: params.term } + }, + processResults: function(data){ + return { + results : data?.data.map(item => ({ + id: item.id, + text: item.name, + })) + } + }, + cache: true, + }, + minimumInputLength: 1, + }); + + if (oldId) { + $.ajax({ + url: '/select-unit-kerja', + data: { id: oldId }, + dataType: 'json' + }).then(function (data) { + if (data?.data?.length) { + let item = data.data[0]; + let option = new Option(item.name, item.id, true, true); + selectUnit.append(option).trigger('change'); + } + }); + } +} + + +function setOldSelect2Value(selector, id, text) { + if (!id || !text) return; + let option = new Option(text, id, true, true); + $(selector).append(option).trigger('change') +} + + +formEditAkses.on('submit', function(e){ + e.preventDefault(); + + const form = this; + const actionUrl = formEditAkses.attr('action'); + const formData = new FormData(form); + formData.append('_method', 'PUT') + fetch(actionUrl, { + method: 'POST', + headers: { + 'X-CSRF-TOKEN': document.querySelector('input[name="_token"]').value, + }, + body: formData + }).then(async(res) => { + const responseData = await res.json(); + + if (responseData.status) { + const handler = function () { + Swal.fire({ + icon: 'success', + title: 'Berhasil', + text: responseData.message || 'Berhasil melakukan aksi!', + timer: 2000, + showConfirmButton: false, + backdrop: true, + }); + formCreateAkses.find('select').val(null).trigger('change'); + datatableAkses.bootstrapTable('refresh'); + datatableAkses.bootstrapTable('refresh'); + modalEdit.removeEventListener('hidden.bs.modal', handler); + }; + modalEdit.addEventListener('hidden.bs.modal', handler); + bootstrap.Modal.getInstance(modalEdit).hide(); + } else { + throw new Error(responseData.message || 'Terjadi kesalahan saat menyimpan data.'); + } + + }).catch(err => { + if (err.message) { + Swal.fire({ + icon: 'error', + title: 'Gagal', + text: err.message + }); + } + }); +}); diff --git a/public/js/akses/dt.js b/public/js/akses/dt.js new file mode 100644 index 0000000..60b9987 --- /dev/null +++ b/public/js/akses/dt.js @@ -0,0 +1,57 @@ + datatableAkses.bootstrapTable({ + url: "/datatable/akses", + showRefresh: true, + sortable: true, + search: true, + searchOnEnterKey: false, + searchHighlight: true, + pagination: true, + serverSide:true, + pageSize: 10, + pageList: [10, 20, 30], + cookie: true, + cookieIdTable: "table_master_kategori", + icons: { + refresh: "fas fa-sync-alt", + }, + columns: [ + { + title:"Action", + formatter: function(value, row){ + let buttons = ''; + buttons += ` + + ` + + buttons += ` + ` + return ` +
+ ${buttons} +
+ `; + } + }, + { + title:"Nama", + field:'pegawai.namalengkap' + }, + { + title:"Akses", + formatter:function(value, row){ + return row?.all_akses ? 'Semua Akses' : row?.akses?.name; + } + } + ], + }); diff --git a/public/js/akses/functions.js b/public/js/akses/functions.js new file mode 100644 index 0000000..6682f18 --- /dev/null +++ b/public/js/akses/functions.js @@ -0,0 +1,130 @@ +$(document).ready(function() { + selectOptionPegawai(0) + selectOptionUnit(0) +}); + +function selectOptionPegawai(colCount) { + let selectPegawai = $(`#pegawai_id_${colCount}`); + // inisialisasi select2 untuk Unit Kerja + selectPegawai.select2({ + placeholder: '-- Pilih Pegawai --', + allowClear:true, + width: '100%', + dropdownParent: selectPegawai.parent(), + ajax:{ + url : '/select-pegawai', + dataType: 'json', + delay: 250, + data: function(params){ + return { q: params.term } + }, + processResults: function(data){ + return { + results : data?.data.map(item => ({ + id: item.id, + text: item.nama, + })) + } + }, + cache: true, + }, + minimumInputLength: 1, + }); +} + +function selectOptionUnit(colCount) { + let selectUnit = $(`#unit_akses_${colCount}`); + // inisialisasi select2 untuk Unit Kerja + selectUnit.select2({ + placeholder: '-- Pilih Unit --', + allowClear:true, + width: '100%', + dropdownParent: selectUnit.parent(), + ajax:{ + url : '/select-unit-kerja-option', + dataType: 'json', + delay: 250, + data: function(params){ + return { q: params.term } + }, + processResults: function(data){ + return { + results : data?.data.map(item => ({ + id: item.id, + text: item.name, + })) + } + }, + cache: true, + }, + minimumInputLength: 1, + }); +} + +function selectAkses(colCount){ + let aksesId = $(`#akses_id_${colCount}`); + let colUnit = $(`#col_select_unit_${colCount}`) + if(aksesId.val() === "all"){ + colUnit.addClass('d-none') + }else{ + colUnit.removeClass('d-none') + } + +} + + +document.addEventListener('change', function(e){ + e.preventDefault() + if(e.target && e.target.id.startsWith("akses_id")){ + let colCount = e.target.id.split("_")[2]; + selectAkses(colCount) + } +}) + + +let colCount = 1; +function addForm(){ + let col = $("#col_add_akses") + + let html = ''; + + html += ` +
+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ +
+ ` + col.append(html) + selectOptionPegawai(colCount) + selectOptionUnit(colCount) + colCount++; + +} + + +function removeCol(count){ + $(`#col-${count}`).remove() +} + diff --git a/public/js/dashboard/index.js b/public/js/dashboard/index.js index e9eaccb..0432aa1 100644 --- a/public/js/dashboard/index.js +++ b/public/js/dashboard/index.js @@ -2,6 +2,7 @@ function renderTree(units, katDok) { if (!Array.isArray(units)) return ''; return ` +