148 lines
4.7 KiB
PHP
148 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Template;
|
|
use App\Models\Kategori;
|
|
use App\Models\Fitur;
|
|
use App\Models\KategoriFitur; // <-- tambah ini
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class TemplateController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$templates = Template::with(['kategori', 'fiturs'])->paginate(5);
|
|
$kategoris = Kategori::all();
|
|
|
|
// semua fitur dengan relasi kategori
|
|
$fiturs = Fitur::with('kategoriFitur')->get();
|
|
|
|
// kategori fitur beserta fiturnya
|
|
$kategoriFiturs = KategoriFitur::with('fiturs')->get();
|
|
|
|
// fitur yang tidak punya kategori
|
|
$fitursTanpaKategori = Fitur::whereNull('kategori_fitur_id')->get();
|
|
|
|
return view('admin.templates.index', compact(
|
|
'templates',
|
|
'kategoris',
|
|
'fiturs',
|
|
'kategoriFiturs',
|
|
'fitursTanpaKategori'
|
|
));
|
|
}
|
|
|
|
|
|
public function show($id)
|
|
{
|
|
return Template::with('kategori')->findOrFail($id);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'nama_template' => 'required|string|max:255',
|
|
'kategori_id' => 'required|exists:kategoris,id',
|
|
'fitur_id' => 'required|array',
|
|
'fitur_id.*' => 'exists:fiturs,id',
|
|
'foto' => 'nullable|image|mimes:jpg,jpeg,png,gif|max:5120',
|
|
'harga' => 'required|string', // wajib string karena ada titik
|
|
]);
|
|
|
|
// hitung total harga dari fitur yang dipilih
|
|
$totalHarga = Fitur::whereIn('id', $data['fitur_id'])->sum('harga');
|
|
|
|
if ($request->hasFile('foto')) {
|
|
$data['foto'] = $request->file('foto')->store('templates', 'public');
|
|
}
|
|
|
|
// Bersihkan titik ribuan sebelum simpan
|
|
$hargaBersih = (int) str_replace('.', '', $request->harga);
|
|
|
|
// store
|
|
$template = Template::create([
|
|
'nama_template' => $data['nama_template'],
|
|
'kategori_id' => $data['kategori_id'],
|
|
'foto' => $data['foto'] ?? null,
|
|
'harga' => $hargaBersih,
|
|
]);
|
|
|
|
$template->fiturs()->sync($data['fitur_id']);
|
|
|
|
return redirect()->route('templates.index')->with('success', 'Template berhasil ditambahkan!');
|
|
}
|
|
|
|
public function update(Request $request, Template $template)
|
|
{
|
|
$data = $request->validate([
|
|
'nama_template' => 'required|string|max:255',
|
|
'kategori_id' => 'required|exists:kategoris,id',
|
|
'fitur_id' => 'required|array',
|
|
'fitur_id.*' => 'exists:fiturs,id',
|
|
'foto' => 'nullable|image|mimes:jpg,jpeg,png,gif|max:5120',
|
|
'harga' => 'required|string',
|
|
]);
|
|
|
|
// hitung ulang harga fitur
|
|
$totalHarga = Fitur::whereIn('id', $data['fitur_id'])->sum('harga');
|
|
|
|
if ($request->hasFile('foto')) {
|
|
if ($template->foto && Storage::disk('public')->exists($template->foto)) {
|
|
Storage::disk('public')->delete($template->foto);
|
|
}
|
|
$data['foto'] = $request->file('foto')->store('templates', 'public');
|
|
}
|
|
|
|
// Bersihkan titik ribuan
|
|
$hargaBersih = (int) str_replace('.', '', $request->harga);
|
|
|
|
// update
|
|
$template->update([
|
|
'nama_template' => $data['nama_template'],
|
|
'kategori_id' => $data['kategori_id'],
|
|
'foto' => $data['foto'] ?? $template->foto,
|
|
'harga' => $hargaBersih,
|
|
]);
|
|
|
|
$template->fiturs()->sync($data['fitur_id']);
|
|
|
|
return redirect()->route('templates.index')->with('success', 'Template berhasil diperbarui!');
|
|
}
|
|
|
|
public function destroy(Template $template)
|
|
{
|
|
if ($template->foto && Storage::disk('public')->exists($template->foto)) {
|
|
Storage::disk('public')->delete($template->foto);
|
|
}
|
|
|
|
$template->fiturs()->detach();
|
|
$template->delete();
|
|
|
|
return redirect()->route('templates.index')->with('success', 'Template berhasil dihapus!');
|
|
}
|
|
|
|
public function byKategori($id)
|
|
{
|
|
$kategori = Kategori::findOrFail($id);
|
|
$templates = Template::with(['kategori', 'fiturs'])
|
|
->where('kategori_id', $id)
|
|
->paginate(5);
|
|
$kategoris = Kategori::all();
|
|
$fiturs = Fitur::with('kategoriFitur')->get();
|
|
$kategoriFiturs = KategoriFitur::with('fiturs')->get();
|
|
$fitursTanpaKategori = Fitur::whereNull('kategori_fitur_id')->get(); // <-- tambahkan ini
|
|
|
|
return view('admin.templates.index', compact(
|
|
'templates',
|
|
'kategoris',
|
|
'fiturs',
|
|
'kategori',
|
|
'kategoriFiturs',
|
|
'fitursTanpaKategori' // <-- kirim ke view
|
|
));
|
|
}
|
|
|
|
}
|