135 lines
4.7 KiB
PHP
135 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Setting;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\CmsFilePeraturan;
|
|
use App\Services\Setting\PeraturanService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controllers\HasMiddleware;
|
|
use Illuminate\Routing\Controllers\Middleware;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Yajra\DataTables\DataTables;
|
|
|
|
class PeraturanController implements HasMiddleware
|
|
{
|
|
protected $fileService;
|
|
|
|
public function __construct(PeraturanService $fileService)
|
|
{
|
|
$this->fileService = $fileService;
|
|
}
|
|
|
|
public static function middleware(): array
|
|
{
|
|
return [
|
|
//new Middleware('permission:/setting/peraturan'),
|
|
];
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$query = $this->fileService->getAll();
|
|
|
|
$result = datatables()->of($query)
|
|
->addColumn('file_upload', function ($row) {
|
|
if ($row->file_upload) {
|
|
$storagePath = 'peraturan';
|
|
return '<a href="' . Storage::url($storagePath . '/' . $row->file_upload) . '" class="btn btn-info">
|
|
<i class="ti ti-download"></i>
|
|
</a>';
|
|
} else {
|
|
return '<span class="badge badge-secondary">No File</span>';
|
|
}
|
|
})
|
|
->addColumn('action', function ($data) {
|
|
$actionBtn = '<a href="' . route('peraturan.edit', $data->id) . '" class="btn btn-primary">Edit</a>';
|
|
$actionBtn .= ' <form method="POST" action="' . route('peraturan.destroy', $data->id) . '" style="display: inline">';
|
|
$actionBtn .= csrf_field();
|
|
$actionBtn .= method_field('DELETE');
|
|
$actionBtn .= '<button type="submit" class="btn btn-danger" onclick="return confirm(\'Are you sure?\')">Delete</button>';
|
|
$actionBtn .= '</form>';
|
|
return $actionBtn;
|
|
})
|
|
->rawColumns(['file_upload', 'action'])
|
|
->make(true);
|
|
|
|
return $result;
|
|
}
|
|
|
|
return view('setting.file-peraturan.index');
|
|
}
|
|
|
|
|
|
public function create()
|
|
{
|
|
return view('setting.file-peraturan.create');
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'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('peraturan.index')->with('success', 'File Pengumuman / File Peraturan-Peraturan berhasil diupload.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'File Pengumuman / File Peraturan-Peraturan gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$data = $this->fileService->find($id);
|
|
if ($data) {
|
|
$data['storagePath'] = 'peraturan';
|
|
}
|
|
|
|
return view('setting.file-peraturan.edit', compact('data'));
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$request->validate([
|
|
'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;
|
|
|
|
$this->fileService->save($data);
|
|
|
|
return redirect()->route('peraturan.index')->with('success', 'File Pengumuman / File Peraturan-Peraturan berhasil diperbarui.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'File Pengumuman / File Peraturan-Peraturan gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$data = $this->fileService->find($id);
|
|
if ($data) {
|
|
$storagePath = 'peraturan';
|
|
}
|
|
|
|
$this->fileService->destroy($cms, $storagePath);
|
|
|
|
return redirect()->route('unit.index')->with('success', 'File Pengumuman / File Peraturan-Peraturan berhasil dihapus.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'File Pengumuman / File Peraturan-Peraturan gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
}
|