153 lines
5.1 KiB
PHP
153 lines
5.1 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 $title = 'Data Pengumuman';
|
|
protected $template = 'modules.setting.file-peraturan';
|
|
protected $route = 'modules.pengaturan.pengumuman';
|
|
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)
|
|
{
|
|
$datas['route'] = $this->route;
|
|
$datas['title'] = $this->title;
|
|
|
|
return view($this->template.'.index',$datas);
|
|
}
|
|
|
|
|
|
public function create()
|
|
{
|
|
$datas['route'] = $this->route;
|
|
$datas['title'] = $this->title;
|
|
return view($this->template.'.create',$datas);
|
|
}
|
|
|
|
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($this->route.'.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)
|
|
{
|
|
$datas['data'] = $this->fileService->find($id);
|
|
if ($datas['data']) {
|
|
$datas['storagePath'] = 'peraturan';
|
|
}
|
|
$datas['route'] = $this->route;
|
|
$datas['title'] = $this->title;
|
|
return view($this->template.'.edit', $datas);
|
|
}
|
|
|
|
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($this->route.'.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($data, $storagePath);
|
|
|
|
return redirect()->route($this->route.'.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()]);
|
|
}
|
|
}
|
|
|
|
public function grid(Request $request)
|
|
{
|
|
// dd($request->all());
|
|
$_data = [];
|
|
$data = $this->fileService->getAll();
|
|
|
|
foreach ($data 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>';
|
|
|
|
if ($row->file_upload) {
|
|
$storagePath = 'storage/peraturan';
|
|
$file = '<a href="' . asset($storagePath . '/' . $row->file_upload) . '" class="btn btn-sm w-100 btn-info">
|
|
<i class="bx bx-download"></i>
|
|
</a>';
|
|
} else {
|
|
$file = '<span class="badge badge-secondary">No File</span>';
|
|
}
|
|
|
|
|
|
$_data[] = [
|
|
'no' => $key+1,
|
|
'file' => $file,
|
|
'name' => $row->name,
|
|
'description' => $row->description,
|
|
'action' => @$btn,
|
|
];
|
|
|
|
}
|
|
|
|
return response()->json($_data);
|
|
}
|
|
}
|