Files
D1041231077-webgis/03/update_status.php
T
2026-06-10 00:49:56 +07:00

75 lines
2.3 KiB
PHP

<?php
/**
* update_status.php — Update Status Rumah
*
* POST params:
* house_id - ID rumah
* status - Status baru (green/red/yellow)
* center_id - ID pusat keagamaan terdekat
*/
require_once 'koneksi.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['success' => false, 'message' => 'Hanya menerima POST']);
exit;
}
$houseId = intval($_POST['house_id'] ?? 0);
$status = $conn->real_escape_string($_POST['status'] ?? 'green');
$centerId = intval($_POST['center_id'] ?? 0);
// Validasi status
$validStatuses = ['green', 'red', 'yellow'];
if (!in_array($status, $validStatuses)) {
echo json_encode(['success' => false, 'message' => 'Status tidak valid']);
exit;
}
// Validasi house_id
if ($houseId < 1) {
echo json_encode(['success' => false, 'message' => 'house_id tidak valid']);
exit;
}
// ========================
// Upsert status rumah
// ========================
// Cek apakah rumah sudah ada
$check = $conn->query("SELECT id FROM houses WHERE id = $houseId");
if ($check && $check->num_rows > 0) {
// Update
$sql = "UPDATE houses SET status='$status' WHERE id=$houseId";
} else {
// Insert (ambil posisi dari fixed positions yang sama seperti di JS)
// Posisi disimpan juga agar konsisten
$lat = floatval($_POST['lat'] ?? 0);
$lng = floatval($_POST['lng'] ?? 0);
$sql = "INSERT INTO houses (id, latitude, longitude, status) VALUES ($houseId, $lat, $lng, '$status')";
}
if ($conn->query($sql)) {
// ========================
// Catat ke aid_logs
// ========================
if ($status === 'yellow' && $centerId > 0) {
$logSql = "INSERT INTO aid_logs (house_id, religious_center_id, status, timestamp)
VALUES ($houseId, $centerId, 'helped', NOW())";
$conn->query($logSql);
} elseif ($status !== 'yellow') {
// Batalkan bantuan — catat sebagai reverted
$logSql = "INSERT INTO aid_logs (house_id, religious_center_id, status, timestamp)
VALUES ($houseId, $centerId, 'reverted', NOW())";
$conn->query($logSql);
}
echo json_encode([
'success' => true,
'house_id' => $houseId,
'status' => $status
]);
} else {
echo json_encode(['success' => false, 'message' => $conn->error]);
}
$conn->close();