154 lines
5.6 KiB
PHP
154 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Auth; // <-- WAJIB TAMBAH INI
|
|
use App\Models\Prediksi;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
|
|
class KlasifikasiController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('welcome');
|
|
}
|
|
|
|
public function prediksi(Request $request)
|
|
{
|
|
$request->validate([
|
|
'gambar' => 'required|image'
|
|
]);
|
|
|
|
$gambar = $request->file('gambar');
|
|
$uploadPath = public_path('uploads');
|
|
|
|
if (!is_dir($uploadPath)) {
|
|
mkdir($uploadPath, 0755, true);
|
|
}
|
|
|
|
$namaFile = time() . '.' . $gambar->getClientOriginalExtension();
|
|
$gambar->move($uploadPath, $namaFile);
|
|
$pathGambar = $uploadPath . DIRECTORY_SEPARATOR . $namaFile;
|
|
|
|
try {
|
|
$response = Http::timeout(60)
|
|
->attach('gambar', file_get_contents($pathGambar), $namaFile)
|
|
->post('http://127.0.0.1:5000/prediksi');
|
|
|
|
if (!$response->successful()) {
|
|
Log::error('Server prediksi gagal', ['body' => $response->body()]);
|
|
return back()->with('error', 'Tidak dapat memproses prediksi.');
|
|
}
|
|
|
|
$hasil = $response->json();
|
|
|
|
// Logika deskripsi tetap sama
|
|
$deskripsi = ''; $solusi = '';
|
|
if ($hasil['penyakit'] == 'Blast') {
|
|
$deskripsi = 'Blast adalah penyakit daun padi akibat jamur Pyricularia oryzae.';
|
|
$solusi = 'Gunakan fungisida dan varietas tahan.';
|
|
} elseif ($hasil['penyakit'] == 'Blight') {
|
|
$deskripsi = 'Blight merupakan hawar daun bakteri.';
|
|
$solusi = 'Gunakan benih sehat dan sanitasi lahan.';
|
|
} elseif ($hasil['penyakit'] == 'Tungro') {
|
|
$deskripsi = 'Tungro adalah penyakit virus.';
|
|
$solusi = 'Kendalikan wereng hijau.';
|
|
} elseif ($hasil['penyakit'] == 'Healthy') {
|
|
$deskripsi = 'Kondisi tanaman sehat.';
|
|
$solusi = 'Pertahankan perawatan yang baik.';
|
|
}
|
|
|
|
// SIMPAN DATA DENGAN USER_ID
|
|
Prediksi::create([
|
|
'gambar' => $namaFile,
|
|
'penyakit' => $hasil['penyakit'],
|
|
|
|
// Kita buang persen (%) nya dan ubah menjadi tipe Float murni
|
|
'confidence' => floatval(str_replace('%', '', $hasil['confidence'])),
|
|
|
|
'user_id' => Auth::id(),
|
|
]);
|
|
|
|
return back()->with([
|
|
'success' => 'Prediksi berhasil',
|
|
'gambar' => $namaFile,
|
|
'hasil' => $hasil['penyakit'] ?? 'Tidak ada hasil',
|
|
'confidence' => $hasil['confidence'] ?? 0,
|
|
'deskripsi' => $deskripsi,
|
|
'solusi' => $solusi,
|
|
'allPredictions' => $hasil['all_predictions'] ?? [],
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error('Prediksi request error: ' . $e->getMessage());
|
|
return back()->with('error', 'Terjadi kesalahan server.');
|
|
}
|
|
}
|
|
|
|
public function riwayat(Request $request)
|
|
{
|
|
// FILTER BERDASARKAN USER_ID
|
|
$query = Prediksi::where('user_id', Auth::id());
|
|
|
|
if ($request->filled('search')) {
|
|
$query->where('penyakit', 'like', '%' . $request->search . '%');
|
|
}
|
|
|
|
if ($request->filled('filter')) {
|
|
$query->where('penyakit', $request->filter);
|
|
}
|
|
|
|
$data = $query->latest()->get();
|
|
return view('riwayat', compact('data'));
|
|
}
|
|
|
|
public function dashboard()
|
|
{
|
|
// FILTER BERDASARKAN USER_ID
|
|
$userId = Auth::id();
|
|
|
|
$totalPrediksi = Prediksi::where('user_id', $userId)->count();
|
|
$blast = Prediksi::where('user_id', $userId)->where('penyakit', 'Blast')->count();
|
|
$blight = Prediksi::where('user_id', $userId)->where('penyakit', 'Blight')->count();
|
|
$healthy = Prediksi::where('user_id', $userId)->where('penyakit', 'Healthy')->count();
|
|
$tungro = Prediksi::where('user_id', $userId)->where('penyakit', 'Tungro')->count();
|
|
$terbaru = Prediksi::where('user_id', $userId)->latest()->take(3)->get();
|
|
|
|
return view('dashboard', compact('totalPrediksi', 'blast', 'blight', 'healthy', 'tungro', 'terbaru'));
|
|
}
|
|
|
|
public function exportPdf()
|
|
{
|
|
$data = Prediksi::where('user_id', Auth::id())->latest()->get();
|
|
$pdf = Pdf::loadView('pdf.laporan', compact('data'));
|
|
return $pdf->download('laporan-prediksi.pdf');
|
|
}
|
|
|
|
public function hapus($id)
|
|
{
|
|
// Tambahkan cek user_id agar user tidak bisa hapus data orang lain via URL
|
|
$data = Prediksi::where('id', $id)->where('user_id', Auth::id())->firstOrFail();
|
|
|
|
$path = public_path('uploads/' . $data->gambar);
|
|
if (file_exists($path)) {
|
|
unlink($path);
|
|
}
|
|
|
|
$data->delete();
|
|
return redirect()->back()->with('success', 'Data berhasil dihapus');
|
|
}
|
|
|
|
public function statistik()
|
|
{
|
|
$userId = Auth::id();
|
|
return response()->json([
|
|
'blast' => Prediksi::where('user_id', $userId)->where('penyakit', 'Blast')->count(),
|
|
'blight' => Prediksi::where('user_id', $userId)->where('penyakit', 'Blight')->count(),
|
|
'healthy' => Prediksi::where('user_id', $userId)->where('penyakit', 'Healthy')->count(),
|
|
'tungro' => Prediksi::where('user_id', $userId)->where('penyakit', 'Tungro')->count(),
|
|
]);
|
|
}
|
|
} |