77 lines
2.9 KiB
PHP
77 lines
2.9 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
include __DIR__ . '/../../db_config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$jenisTarget = trim($_POST['jenis_target'] ?? 'penduduk_miskin');
|
|
$targetNama = trim($_POST['target_nama'] ?? '');
|
|
$petugasNama = trim($_POST['petugas_nama'] ?? '');
|
|
$statusVerifikasi = trim($_POST['status_verifikasi'] ?? 'menunggu');
|
|
$hasilTemuan = trim($_POST['hasil_temuan'] ?? '');
|
|
$tindakLanjut = trim($_POST['tindak_lanjut'] ?? '');
|
|
$latitude = $_POST['latitude'] ?? '';
|
|
$longitude = $_POST['longitude'] ?? '';
|
|
|
|
if ($petugasNama === '' || empty($latitude) || empty($longitude)) {
|
|
http_response_code(400);
|
|
echo json_encode(['status' => 'error', 'message' => 'Nama petugas 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;
|
|
}
|
|
|
|
// Folder is in root
|
|
if (!is_dir(__DIR__ . '/../../uploads')) {
|
|
mkdir(__DIR__ . '/../../uploads', 0755, true);
|
|
}
|
|
|
|
$foto = 'uploads/' . time() . '_' . basename($filename);
|
|
if (!move_uploaded_file($_FILES['foto']['tmp_name'], __DIR__ . '/../../' . $foto)) {
|
|
http_response_code(500);
|
|
echo json_encode(['status' => 'error', 'message' => 'Gagal upload foto']);
|
|
$conn->close();
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$stmt = $conn->prepare('INSERT INTO verifikasi_lapangan (jenis_target, target_nama, petugas_nama, status_verifikasi, hasil_temuan, 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', $jenisTarget, $targetNama, $petugasNama, $statusVerifikasi, $hasilTemuan, $tindakLanjut, $foto, $latitude, $longitude);
|
|
|
|
if ($stmt->execute()) {
|
|
echo json_encode(['status' => 'success', 'message' => 'Verifikasi lapangan 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();
|
|
?>
|