149 lines
4.6 KiB
PHP
149 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Bkk;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
class BkkController extends Controller
|
|
{
|
|
protected function storagePath()
|
|
{
|
|
return env('STORAGE_PATH', public_path('penyimpanan'));
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
// Menggunakan paginate agar tampilan lebih rapi jika data banyak
|
|
$data = Bkk::orderBy('id_bkk', 'desc')->paginate(10);
|
|
// Mengarahkan ke view admin yang benar
|
|
return view('admin.bkk', compact('data'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
// Anda perlu membuat view 'admin.bkk_create'
|
|
return view('admin.form-bkk');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'judul_lowongan' => 'required|string|max:255',
|
|
'nama_perusahaan' => 'required|string|max:255',
|
|
'persyaratan' => 'required|string',
|
|
'tanggal_dibuka' => 'required|date',
|
|
'tanggal_ditutup' => 'required|date|after_or_equal:tanggal_dibuka',
|
|
'foto' => 'required|image|mimes:jpg,jpeg,png,gif,bmp,webp',
|
|
]);
|
|
|
|
$data = $request->all();
|
|
$data['id_user'] = Auth::id();
|
|
|
|
$storagePath = $this->storagePath();
|
|
|
|
// Handle upload foto - SIMPAN KE folder penyimpanan eksternal
|
|
if ($request->hasFile('foto')) {
|
|
if (!file_exists($storagePath . '/foto_bkk')) {
|
|
mkdir($storagePath . '/foto_bkk', 0755, true);
|
|
}
|
|
$filename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '_', $request->file('foto')->getClientOriginalName());
|
|
$request->file('foto')->move($storagePath . '/foto_bkk', $filename);
|
|
$data['foto'] = 'foto_bkk/' . $filename;
|
|
}
|
|
|
|
Bkk::create($data);
|
|
|
|
return redirect()->route('bkk.index')->with('success', 'Data BKK berhasil ditambahkan.');
|
|
}
|
|
/**
|
|
* Display the specified resource for public view.
|
|
*/
|
|
public function show(Bkk $bkk)
|
|
{
|
|
// Anda perlu membuat view 'user.detail-bkk'
|
|
return view('user.detail-bkk', compact('bkk'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Bkk $bkk)
|
|
{
|
|
// Anda perlu membuat view 'admin.bkk_edit'
|
|
return view('admin.edit-bkk', compact('bkk'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, $id_bkk)
|
|
{
|
|
$bkk = Bkk::findOrFail($id_bkk);
|
|
|
|
$request->validate([
|
|
'judul_lowongan' => 'required|string|max:255',
|
|
'nama_perusahaan' => 'required|string|max:255',
|
|
'persyaratan' => 'required|string',
|
|
'tanggal_dibuka' => 'required|date',
|
|
'tanggal_ditutup' => 'required|date|after_or_equal:tanggal_dibuka',
|
|
'foto' => 'nullable|image|mimes:jpg,jpeg,png,gif,bmp,webp',
|
|
]);
|
|
|
|
$data = $request->all();
|
|
|
|
$storagePath = $this->storagePath();
|
|
|
|
// Handle upload foto baru
|
|
if ($request->hasFile('foto')) {
|
|
// Hapus foto lama jika ada
|
|
if ($bkk->foto && file_exists($storagePath . '/' . $bkk->foto)) {
|
|
unlink($storagePath . '/' . $bkk->foto);
|
|
}
|
|
|
|
if (!file_exists($storagePath . '/foto_bkk')) {
|
|
mkdir($storagePath . '/foto_bkk', 0755, true);
|
|
}
|
|
$filename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '_', $request->file('foto')->getClientOriginalName());
|
|
$request->file('foto')->move($storagePath . '/foto_bkk', $filename);
|
|
$data['foto'] = 'foto_bkk/' . $filename;
|
|
} else {
|
|
unset($data['foto']);
|
|
}
|
|
|
|
$bkk->update($data);
|
|
return redirect()->route('bkk.index')->with('success', 'Data BKK berhasil diperbarui.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy($id_bkk)
|
|
{
|
|
$bkk = Bkk::findOrFail($id_bkk);
|
|
|
|
// Hapus foto dari storage jika ada
|
|
$storagePath = $this->storagePath();
|
|
if ($bkk->foto && file_exists($storagePath . '/' . $bkk->foto)) {
|
|
unlink($storagePath . '/' . $bkk->foto);
|
|
}
|
|
|
|
$bkk->delete();
|
|
return redirect()->route('bkk.index')->with('success', 'Data berhasil dihapus');
|
|
}
|
|
}
|