MIF_E31230887/app/Http/Controllers/AdminBukuController.php

160 lines
6.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Buku;
use App\Models\Kategori;
use Illuminate\Http\Request;
class AdminBukuController extends Controller
{
public function create()
{
$kategori = Kategori::all();
return view('admin.buku.create', compact('kategori'));
}
public function store(Request $request)
{
$validated = $request->validate([
'bibid' => 'required|string|max:30|unique:buku,bibid',
'judul' => 'required|string',
'pengarang' => 'required|string|max:100',
'penerbit' => 'required|string',
'tahun_terbit' => 'required|digits:4',
'edisi' => 'required|digits:4',
'deskripsi_fisik' => 'required|string|max:100',
'nomor_panggil' => 'required|string|max:50|unique:buku,nomor_panggil',
'eksemplar' => 'required|integer|min:1',
'id_kategori' => 'required|exists:kategori,id_kategori',
'cover' => 'image|mimes:jpeg,png,jpg,webp|max:2048',
]);
if ($request->hasFile('cover')) {
$validated['cover'] = $request->file('cover')->store('covers', 'public');
}
$koordinat = $this->tentukanKoordinatRak($validated['nomor_panggil']);
$validated['lokasi_x'] = $koordinat['x'];
$validated['lokasi_y'] = $koordinat['y'];
$validated['konten_digital'] = 0;
Buku::create($validated);
return back()->with('success', 'Aset buku baru berhasil ditambahkan.');
}
public function index(Request $request)
{
$search = $request->input('search');
$buku = Buku::with('kategori')
->when($search, function ($query, $search) {
return $query->where('judul', 'like', "%{$search}%")
->orWhere('bibid', 'like', "%{$search}%");
})
->paginate(15);
$kategori = Kategori::all();
return view('admin.buku.index', compact('buku', 'search', 'kategori'));
}
public function edit($id)
{
$buku = Buku::findOrFail($id);
$kategori = Kategori::all();
return view('admin.buku.edit', compact('buku', 'kategori'));
}
public function update(Request $request, $id)
{
$buku = Buku::findOrFail($id);
$validated = $request->validate([
'bibid' => 'required|string|max:30|unique:buku,bibid,' . $id . ',id_buku',
'judul' => 'required|string',
'pengarang' => 'required|string|max:100',
'penerbit' => 'required|string',
'tahun_terbit' => 'required|digits:4',
'edisi' => 'required|digits:4',
'deskripsi_fisik' => 'required|string|max:100',
'nomor_panggil' => 'required|string|max:50|unique:buku,nomor_panggil,' . $id . ',id_buku',
'eksemplar' => 'required|integer|min:1',
'id_kategori' => 'required|exists:kategori,id_kategori',
'cover' => 'image|mimes:jpeg,png,jpg,webp|max:2048'
]);
if ($request->hasFile('cover')) {
if ($buku->cover && \Illuminate\Support\Facades\Storage::disk('public')->exists($buku->cover)) {
\Illuminate\Support\Facades\Storage::disk('public')->delete($buku->cover);
}
$validated['cover'] = $request->file('cover')->store('covers', 'public');
}
$koordinat = $this->tentukanKoordinatRak($validated['nomor_panggil']);
$validated['lokasi_x'] = $koordinat['x'];
$validated['lokasi_y'] = $koordinat['y'];
$buku->update($validated);
return redirect()->route('admin.buku.index')->with('success', 'Aset buku berhasil diperbarui.');
}
public function destroy($id)
{
$buku = Buku::findOrFail($id);
$buku->delete();
return redirect()->route('admin.buku.index')->with('success', 'Aset buku berhasil dihapus dari sistem.');
}
private function tentukanKoordinatRak($nomor_panggil)
{
if (empty($nomor_panggil)) return ['x' => null, 'y' => null];
$kode_utama = (int) substr(trim(preg_replace('/[^0-9]/', '', $nomor_panggil)), 0, 3);
return match (true) {
$kode_utama >= 0 && $kode_utama <= 99 => match (true) {
$kode_utama <= 19 => ['x' => 12.00, 'y' => 13.00], // Rak 01
$kode_utama <= 50 => ['x' => 17.00, 'y' => 13.00], // Rak 02
default => ['x' => 22.00, 'y' => 13.00], // Rak 03-05
},
$kode_utama >= 100 && $kode_utama <= 199 => match (true) {
$kode_utama <= 150 => ['x' => 35.00, 'y' => 13.00], // Rak 06-10
default => ['x' => 35.00, 'y' => 17.00], // Rak 11-14
},
$kode_utama >= 200 && $kode_utama <= 299 => match (true) {
$kode_utama == 297 => ['x' => 14.00, 'y' => 38.00], // Rak 25-32 (Islam)
default => ['x' => 14.00, 'y' => 30.00], // Rak 15-24
},
$kode_utama >= 300 && $kode_utama <= 399 => match (true) {
$kode_utama <= 330 => ['x' => 38.00, 'y' => 23.00], // Rak 33-36
$kode_utama <= 360 => ['x' => 43.00, 'y' => 23.00], // Rak 37-40
default => ['x' => 48.00, 'y' => 23.00], // Rak 41-44
},
$kode_utama >= 400 && $kode_utama <= 499 => ['x' => 14.00, 'y' => 58.00], // Rak 45
$kode_utama >= 500 && $kode_utama <= 599 => ['x' => 38.00, 'y' => 72.00], // Rak 46-48
$kode_utama >= 600 && $kode_utama <= 699 => match (true) {
$kode_utama <= 610 => ['x' => 28.00, 'y' => 83.00], // Rak 49-53
$kode_utama <= 630 => ['x' => 36.00, 'y' => 83.00], // Rak 54-58
$kode_utama <= 650 => ['x' => 44.00, 'y' => 83.00], // Rak 59-63
default => ['x' => 52.00, 'y' => 83.00], // Rak 64-68
},
$kode_utama >= 700 && $kode_utama <= 799 => match (true) {
$kode_utama <= 739 => ['x' => 82.00, 'y' => 12.00], // Rak 71
$kode_utama <= 769 => ['x' => 86.00, 'y' => 12.00], // Rak 72
$kode_utama <= 789 => ['x' => 82.00, 'y' => 16.00], // Rak 73
default => ['x' => 86.00, 'y' => 16.00], // Rak 74
},
$kode_utama >= 800 && $kode_utama <= 899 => ['x' => 82.00, 'y' => 25.00], // Rak 77-79
$kode_utama >= 900 && $kode_utama <= 999 => match (true) {
$kode_utama <= 919 => ['x' => 82.00, 'y' => 30.00], // Rak 69-70
default => ['x' => 82.00, 'y' => 35.00], // Rak 80-84
},
default => ['x' => null, 'y' => null],
};
}
}