209 lines
7.4 KiB
PHP
209 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Setting;
|
|
|
|
use App\Enums\LingkupAksesData;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\CmsFileDataAktivitas;
|
|
use App\Models\ReferenceSubSector;
|
|
use App\Services\Setting\DataAktivitasService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controllers\HasMiddleware;
|
|
use Illuminate\Routing\Controllers\Middleware;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Yajra\DataTables\DataTables;
|
|
|
|
class DataAktivitasController implements HasMiddleware
|
|
{
|
|
protected $title = 'Upload Data Aktivitas';
|
|
protected $template = 'modules.setting.file-data-aktivitas';
|
|
protected $route = 'modules.pengaturan.upload-aktifitas';
|
|
protected $fileService;
|
|
|
|
public function __construct(DataAktivitasService $fileService)
|
|
{
|
|
$this->fileService = $fileService;
|
|
}
|
|
|
|
public static function middleware(): array
|
|
{
|
|
return [
|
|
//new Middleware('permission:/setting/data_aktivitas'),
|
|
];
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$data['sectors'] = $this->fileService->getSectors();
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
return view($this->template.'.index', $data);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$data['sectors'] = $this->fileService->getSectors();
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
|
|
return view($this->template.'.create', $data);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'sector' => 'required|string|max:255',
|
|
'sub_sector' => 'nullable|string|max:255',
|
|
'inventory_year' => 'required|integer',
|
|
'name' => 'required|string|max:255',
|
|
'file_document' => 'required|file|max:2048|mimes:pdf,doc,docx,xlsx,xls',
|
|
], [
|
|
'file_document.max' => 'Ukuran file tidak boleh melebihi 2MB.',
|
|
]);
|
|
|
|
try {
|
|
$this->fileService->save($request->all());
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'File Data Aktivitas berhasil diupload.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'File Data Aktivitas gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$data = $this->fileService->find($id);
|
|
if ($data) {
|
|
$data['storagePath'] = 'data_aktivitas/' . $data->sector . '/' . $data->inventory_year;
|
|
}
|
|
|
|
$data['sectors'] = $this->fileService->getSectors();
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
|
|
return view($this->template.'.edit', $data);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'sector' => 'required|string|max:255',
|
|
'sub_sector' => 'nullable|string|max:255',
|
|
'inventory_year' => 'required|integer',
|
|
'name' => 'required|string|max:255',
|
|
'file_document' => 'nullable|file|max:2048|mimes:pdf,doc,docx,xlsx,xls',
|
|
], [
|
|
'file_document.max' => 'Ukuran file tidak boleh melebihi 2MB.',
|
|
]);
|
|
|
|
try {
|
|
$data = $request->all();
|
|
$data['id'] = $id; // Set the ID of the record to update
|
|
|
|
$this->fileService->save($data);
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'File Data Aktivitas berhasil diperbarui.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'File Data Aktivitas gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$data = $this->fileService->find($id);
|
|
if ($data) {
|
|
$storagePath = 'data_aktivitas/' . $data->sector . '/' . $data->inventory_year;
|
|
}
|
|
|
|
$this->fileService->destroy($cms, $storagePath);
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'File Data Aktivitas berhasil dihapus.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'File Data Aktivitas gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function fetchSubSectors(Request $request)
|
|
{
|
|
$request->validate([
|
|
'sector_code' => 'required|string'
|
|
]);
|
|
|
|
$subSectors = ReferenceSubSector::rowActive()->where('sector_code', $request->sector_code)->get();
|
|
|
|
return response()->json($subSectors);
|
|
}
|
|
|
|
public function grid(Request $request)
|
|
{
|
|
|
|
$_data = [];
|
|
$user = Auth::user();
|
|
$scope = $user->getScope();
|
|
$data = CmsFileDataAktivitas::rowActive();
|
|
|
|
if ($scope === LingkupAksesData::INTERNAL->value) {
|
|
$data->whereHas('creator', function ($q) use ($user) {
|
|
$q->where('agency_id', $user->agency_id);
|
|
});
|
|
} else if ($scope === '') {
|
|
$data->where('created_by', null);
|
|
}
|
|
|
|
// Apply category filter if a category is selected
|
|
if ($request->has('sectorFilter') && $request->sectorFilter != '' && $request->sectorFilter != 'SEMUA SEKTOR') {
|
|
$data = $data->where('sector', $request->sectorFilter);
|
|
}
|
|
|
|
if ($request->has('subSectorFilter') && $request->subSectorFilter != '' && $request->subSectorFilter != 'SEMUA SUB SEKTOR') {
|
|
$data = $data->where('sub_sector', $request->subSectorFilter);
|
|
}
|
|
|
|
// Apply inventory year filter if a year is selected
|
|
if ($request->has('inventoryYearFilter') && $request->inventoryYearFilter != '') {
|
|
$data = $data->where('inventory_year', $request->inventoryYearFilter);
|
|
}
|
|
|
|
foreach ($data->get() as $key => $row) {
|
|
|
|
$btn = '<a href="' . route($this->route.'.edit', $row->id) . '" class="btn btn-sm btn-primary">Edit</a>';
|
|
$btn .= ' <form method="POST" action="' . route($this->route.'.destroy', $row->id) . '" style="display: inline">';
|
|
$btn .= csrf_field();
|
|
$btn .= method_field('DELETE');
|
|
$btn .= '<button type="submit" class="btn btn-sm btn-danger" onclick="return confirm(\'Are you sure?\')">Delete</button>';
|
|
$btn .= '</form>';
|
|
|
|
$sector = $row->sectorData->name;
|
|
if ($row->sub_sector) {
|
|
$sector = $sector . ': ' . $row->subSector->name;
|
|
}
|
|
|
|
if ($row->file_upload) {
|
|
$storagePath = 'data_aktivitas/' . $row->sector . '/' . $row->inventory_year;
|
|
$file = '<a href="' . Storage::url($storagePath . '/' . $row->file_upload) . '" class="btn btn-info">
|
|
<i class="ti ti-download"></i>
|
|
</a>';
|
|
} else {
|
|
$file = '<span class="badge badge-secondary">No File</span>';
|
|
}
|
|
|
|
|
|
$_data[] = [
|
|
'no' => $key+1,
|
|
'sector' => $sector,
|
|
'file' => $file,
|
|
'inventory_year' => $row->inventory_year,
|
|
'name' => $row->name,
|
|
'agency' => $row->agency,
|
|
'created_at' => $row->created_at->format('d-m-Y H:i:s'),
|
|
'action' => @$btn,
|
|
];
|
|
|
|
}
|
|
|
|
return response()->json($_data);
|
|
}
|
|
}
|