Files
WEB-GIS/save_aduan_warga.php
2026-05-12 10:16:16 +07:00

75 lines
2.8 KiB
PHP

<?php
header('Content-Type: application/json');
include 'db_config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$kategori = trim($_POST['kategori'] ?? '');
$pelaporNama = trim($_POST['pelapor_nama'] ?? '');
$kontakPelapor = trim($_POST['kontak_pelapor'] ?? '');
$deskripsi = trim($_POST['deskripsi'] ?? '');
$statusTindakLanjut = trim($_POST['status_tindak_lanjut'] ?? 'baru');
$tindakLanjut = trim($_POST['tindak_lanjut'] ?? '');
$latitude = $_POST['latitude'] ?? '';
$longitude = $_POST['longitude'] ?? '';
if ($kategori === '' || $pelaporNama === '' || $deskripsi === '' || empty($latitude) || empty($longitude)) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Kategori, nama pelapor, deskripsi, dan koordinat wajib diisi']);
$conn->close();
exit;
}
$foto = null;
if (isset($_FILES['foto']) && $_FILES['foto']['error'] === UPLOAD_ERR_OK) {
$allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
$filename = $_FILES['foto']['name'];
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (!in_array($ext, $allowed, true)) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Format foto hanya JPG, PNG, GIF, WEBP']);
$conn->close();
exit;
}
if (!is_dir('uploads')) {
mkdir('uploads', 0755, true);
}
$foto = 'uploads/' . time() . '_' . basename($filename);
if (!move_uploaded_file($_FILES['foto']['tmp_name'], $foto)) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => 'Gagal upload foto']);
$conn->close();
exit;
}
}
$stmt = $conn->prepare('INSERT INTO aduan_warga (kategori, pelapor_nama, kontak_pelapor, deskripsi, status_tindak_lanjut, tindak_lanjut, foto, latitude, longitude) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
if (!$stmt) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $conn->error]);
$conn->close();
exit;
}
$latitude = (float)$latitude;
$longitude = (float)$longitude;
$stmt->bind_param('sssssssdd', $kategori, $pelaporNama, $kontakPelapor, $deskripsi, $statusTindakLanjut, $tindakLanjut, $foto, $latitude, $longitude);
if ($stmt->execute()) {
echo json_encode(['status' => 'success', 'message' => 'Aduan warga 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();
?>