123 lines
4.1 KiB
PHP
123 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\KepalaSekolah;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Http\Request;
|
|
|
|
class KepalaSekolahController extends Controller
|
|
{
|
|
protected function storagePath()
|
|
{
|
|
return env('STORAGE_PATH', public_path('penyimpanan'));
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$data = KepalaSekolah::orderBy('id_kepala_sekolah', 'desc')->paginate(15);
|
|
return view('admin.kepala-sekolah', compact('data'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('admin.form-kepala-sekolah');
|
|
}
|
|
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'nama' => 'required',
|
|
'nip_nuptk' => 'required|numeric|digits_between:0,18|unique:kepala_sekolah,nip_nuptk',
|
|
'foto' => 'nullable|image|mimes:jpg,jpeg,png,gif,bmp,webp',
|
|
'tanggal_lahir' => 'required|date',
|
|
'jenis_kelamin' => 'required|in:Laki-laki,Perempuan',
|
|
'sambutan' => 'nullable|string',
|
|
]);
|
|
|
|
$data = $request->all();
|
|
$data['id_user'] = Auth::id();
|
|
|
|
$storagePath = $this->storagePath();
|
|
|
|
// Handle upload foto
|
|
if ($request->hasFile('foto')) {
|
|
if (!file_exists($storagePath . '/foto_kepala_sekolah')) {
|
|
mkdir($storagePath . '/foto_kepala_sekolah', 0755, true);
|
|
}
|
|
$filename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '_', $request->file('foto')->getClientOriginalName());
|
|
$request->file('foto')->move($storagePath . '/foto_kepala_sekolah', $filename);
|
|
$data['foto'] = 'foto_kepala_sekolah/' . $filename;
|
|
}
|
|
|
|
KepalaSekolah::create($data);
|
|
|
|
return redirect()->route('kepala-sekolah.index')->with('success', 'Data berhasil ditambahkan');
|
|
}
|
|
|
|
public function edit($id_kepala_sekolah)
|
|
{
|
|
$kepala = KepalaSekolah::findOrFail($id_kepala_sekolah);
|
|
return view('admin.edit-kepala-sekolah', compact('kepala'));
|
|
}
|
|
|
|
public function update(Request $request, $id_kepala_sekolah)
|
|
{
|
|
$kepala = KepalaSekolah::findOrFail($id_kepala_sekolah);
|
|
|
|
$request->validate([
|
|
'nama' => 'required',
|
|
'nip_nuptk' => 'required|numeric|digits_between:0,18|unique:kepala_sekolah,nip_nuptk,' . $id_kepala_sekolah . ',id_kepala_sekolah',
|
|
'foto' => 'nullable|image|mimes:jpg,jpeg,png,gif,bmp,webp',
|
|
'tanggal_lahir' => 'required|date',
|
|
'jenis_kelamin' => 'required|in:Laki-laki,Perempuan',
|
|
'sambutan' => 'nullable|string',
|
|
]);
|
|
|
|
$data = $request->all();
|
|
$storagePath = $this->storagePath();
|
|
|
|
// Handle upload foto baru
|
|
if ($request->hasFile('foto')) {
|
|
// Hapus foto lama jika ada
|
|
if ($kepala->foto && file_exists($storagePath . '/' . $kepala->foto)) {
|
|
unlink($storagePath . '/' . $kepala->foto);
|
|
}
|
|
|
|
if (!file_exists($storagePath . '/foto_kepala_sekolah')) {
|
|
mkdir($storagePath . '/foto_kepala_sekolah', 0755, true);
|
|
}
|
|
$filename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '_', $request->file('foto')->getClientOriginalName());
|
|
$request->file('foto')->move($storagePath . '/foto_kepala_sekolah', $filename);
|
|
$data['foto'] = 'foto_kepala_sekolah/' . $filename;
|
|
} else {
|
|
unset($data['foto']);
|
|
}
|
|
|
|
$kepala->update($data);
|
|
|
|
return redirect()->route('kepala-sekolah.index')->with('success', 'Data berhasil diupdate');
|
|
}
|
|
|
|
public function destroy($id_kepala_sekolah)
|
|
{
|
|
$kepala = KepalaSekolah::findOrFail($id_kepala_sekolah);
|
|
|
|
$storagePath = $this->storagePath();
|
|
|
|
// Hapus foto dari storage jika ada
|
|
if ($kepala->foto && file_exists($storagePath . '/' . $kepala->foto)) {
|
|
unlink($storagePath . '/' . $kepala->foto);
|
|
}
|
|
|
|
$kepala->delete();
|
|
return redirect()->route('kepala-sekolah.index')->with('success', 'Data berhasil dihapus');
|
|
}
|
|
//
|
|
|
|
|
|
}
|