264 lines
8.1 KiB
PHP
264 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\AksesFile;
|
|
use App\Models\AksesFileDetail;
|
|
use App\Models\DataUser;
|
|
use App\Models\FileDirectory;
|
|
use App\Models\UnitKerja;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AksesFileController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$data = [
|
|
'title' => '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'),
|
|
'all_akses' => $data['akses'] === "all" ? true : false,
|
|
];
|
|
$af = AksesFile::create($payload);
|
|
if($data['akses'] === "unit"){
|
|
$unitIds = collect($data['unit_akses'] ?? [])->filter()->unique()->values();
|
|
foreach ($unitIds as $unitId) {
|
|
$payloadDetail = [
|
|
'akses_file_id' => $af->akses_file_id,
|
|
'statusenabled' => true,
|
|
'id_unit_kerja' => $unitId,
|
|
];
|
|
AksesFileDetail::create($payloadDetail);
|
|
}
|
|
}
|
|
}
|
|
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)
|
|
{
|
|
$data = AksesFile::where('akses_file_id', $id)->first();
|
|
if (!$data) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Data tidak ditemukan'
|
|
], 404);
|
|
}
|
|
|
|
$aksesAll = $data->akses ?? null;
|
|
if (is_null($aksesAll)) {
|
|
$aksesAll = $data->all_akses ?? null;
|
|
}
|
|
$aksesType = $aksesAll ? 'all' : 'unit';
|
|
|
|
$units = [];
|
|
$detailUnits = AksesFileDetail::where('akses_file_id', $id)
|
|
->where('statusenabled', true)
|
|
->get();
|
|
if ($detailUnits->isNotEmpty()) {
|
|
$unitIds = $detailUnits->pluck('id_unit_kerja');
|
|
$units = UnitKerja::whereIn('id', $unitIds)
|
|
->select('id', 'name')
|
|
->get()
|
|
->map(function ($unit) {
|
|
return [
|
|
'id' => $unit->id,
|
|
'text' => $unit->name,
|
|
];
|
|
})
|
|
->values();
|
|
} elseif (!empty($data->unit_akses)) {
|
|
$unit = UnitKerja::where('id', $data->unit_akses)->select('id', 'name')->first();
|
|
if ($unit) {
|
|
$units[] = [
|
|
'id' => $unit->id,
|
|
'text' => $unit->name,
|
|
];
|
|
}
|
|
}
|
|
|
|
return response()->json([
|
|
'status' => true,
|
|
'data' => [
|
|
'akses_type' => $aksesType,
|
|
'pegawai_id' => $data->pegawai_id,
|
|
'pegawai_nama' => $data?->pegawai?->namalengkap,
|
|
'units' => $units,
|
|
]
|
|
], 200);
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
DB::connection('dbDirectory')->beginTransaction();
|
|
try {
|
|
$data = AksesFile::where('akses_file_id', $id)->first();
|
|
if (!$data) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Data tidak ditemukan'
|
|
], 404);
|
|
}
|
|
|
|
$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,
|
|
];
|
|
$data->update($payload);
|
|
|
|
AksesFileDetail::where('akses_file_id', $id)->delete();
|
|
|
|
if (request('akses') === 'unit') {
|
|
$unitIds = collect(request('unit_akses') ?? [])->filter()->unique()->values();
|
|
foreach ($unitIds as $unitId) {
|
|
$payloadDetail = [
|
|
'akses_file_id' => $data->akses_file_id,
|
|
'statusenabled' => true,
|
|
'id_unit_kerja' => $unitId,
|
|
];
|
|
AksesFileDetail::create($payloadDetail);
|
|
}
|
|
}
|
|
|
|
DB::connection('dbDirectory')->commit();
|
|
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)
|
|
{
|
|
try {
|
|
$data = AksesFile::where('akses_file_id', $id)
|
|
->where('statusenabled', true)
|
|
->first();
|
|
|
|
if (!$data) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Data tidak ditemukan'
|
|
], 404);
|
|
}
|
|
|
|
DB::transaction(function () use ($id, $data) {
|
|
AksesFileDetail::where('akses_file_id', $id)
|
|
->where('statusenabled', true)
|
|
->update(['statusenabled' => false]);
|
|
$data->update(['statusenabled' => false]);
|
|
});
|
|
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => 'Data berhasil dihapus'
|
|
], 200);
|
|
|
|
} catch (\Throwable $th) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Gagal menghapus data',
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
|
|
public function datatable(){
|
|
return AksesFile::with(['details.unit'])
|
|
->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);
|
|
}
|
|
}
|