73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
include 'db_config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$latitude = $_POST['latitude'] ?? '';
|
|
$longitude = $_POST['longitude'] ?? '';
|
|
$jenis_kerusakan = $_POST['jenis_kerusakan'] ?? '';
|
|
$deskripsi = $_POST['deskripsi'] ?? '';
|
|
$severity = $_POST['severity'] ?? 'sedang';
|
|
|
|
if (empty($latitude) || empty($longitude) || empty($jenis_kerusakan)) {
|
|
http_response_code(400);
|
|
echo json_encode(['status' => 'error', 'message' => 'Latitude, longitude, dan jenis kerusakan wajib diisi']);
|
|
exit;
|
|
}
|
|
|
|
$gambar = null;
|
|
|
|
// Handle file upload
|
|
if (isset($_FILES['gambar']) && $_FILES['gambar']['error'] === UPLOAD_ERR_OK) {
|
|
$allowed = ['jpg', 'jpeg', 'png', 'gif'];
|
|
$filename = $_FILES['gambar']['name'];
|
|
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
|
|
|
|
if (!in_array($ext, $allowed)) {
|
|
http_response_code(400);
|
|
echo json_encode(['status' => 'error', 'message' => 'Format gambar hanya JPG, PNG, GIF']);
|
|
exit;
|
|
}
|
|
|
|
// Buat folder uploads jika belum ada
|
|
if (!is_dir('uploads')) {
|
|
mkdir('uploads', 0755, true);
|
|
}
|
|
|
|
// Generate nama file unik
|
|
$gambar = 'uploads/' . time() . '_' . basename($filename);
|
|
|
|
if (!move_uploaded_file($_FILES['gambar']['tmp_name'], $gambar)) {
|
|
http_response_code(500);
|
|
echo json_encode(['status' => 'error', 'message' => 'Gagal upload gambar']);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$stmt = $conn->prepare("INSERT INTO jalan_rusak (latitude, longitude, jenis_kerusakan, deskripsi, severity, gambar) VALUES (?, ?, ?, ?, ?, ?)");
|
|
|
|
if (!$stmt) {
|
|
http_response_code(500);
|
|
echo json_encode(['status' => 'error', 'message' => $conn->error]);
|
|
exit;
|
|
}
|
|
|
|
$stmt->bind_param("ddssss", $latitude, $longitude, $jenis_kerusakan, $deskripsi, $severity, $gambar);
|
|
|
|
if ($stmt->execute()) {
|
|
echo json_encode(['status' => 'success', 'message' => 'Laporan jalan rusak berhasil disimpan', 'id' => $stmt->insert_id]);
|
|
} else {
|
|
http_response_code(500);
|
|
echo json_encode(['status' => 'error', 'message' => $stmt->error]);
|
|
}
|
|
|
|
$stmt->close();
|
|
$conn->close();
|
|
exit;
|
|
}
|
|
|
|
http_response_code(405);
|
|
echo json_encode(['status' => 'error', 'message' => 'Method tidak diizinkan']);
|
|
$conn->close();
|
|
?>
|