66 lines
2.5 KiB
PHP
66 lines
2.5 KiB
PHP
<?php
|
|
header("Content-Type: application/json; charset=UTF-8");
|
|
require_once '../auth_helper.php';
|
|
requireAdminOrPengelola();
|
|
include_once '../db_connect.php';
|
|
|
|
$data = json_decode(file_get_contents("php://input"));
|
|
|
|
if(!empty($data->id)) {
|
|
$id = (int)$data->id;
|
|
|
|
// Validasi pengelola: hanya boleh hapus penduduk dalam radius ibadah yang dikelolanya
|
|
$currentUser = getCurrentUser();
|
|
if ($currentUser && $currentUser['role'] === 'pengelola') {
|
|
$ibadah_id = $currentUser['ibadah_id'];
|
|
if (!$ibadah_id) {
|
|
http_response_code(403);
|
|
echo json_encode(['status' => 'error', 'message' => 'Akun pengelola tidak terhubung ke rumah ibadah manapun.']);
|
|
exit;
|
|
}
|
|
// Ambil koordinat penduduk
|
|
$mRes = $conn->query("SELECT lat, lng FROM penduduk_miskin WHERE id=$id");
|
|
if (!$mRes || $mRes->num_rows === 0) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Data penduduk tidak ditemukan.']);
|
|
exit;
|
|
}
|
|
$mRow = $mRes->fetch_assoc();
|
|
$pLat = (float)$mRow['lat'];
|
|
$pLng = (float)$mRow['lng'];
|
|
|
|
// Ambil data ibadah
|
|
$iRes = $conn->query("SELECT lat, lng, radius FROM rumah_ibadah WHERE id=$ibadah_id");
|
|
if (!$iRes || $iRes->num_rows === 0) {
|
|
http_response_code(403);
|
|
echo json_encode(['status' => 'error', 'message' => 'Rumah ibadah yang dikelola tidak ditemukan.']);
|
|
exit;
|
|
}
|
|
$ib = $iRes->fetch_assoc();
|
|
|
|
// Hitung jarak Haversine
|
|
$R = 6371000;
|
|
$lat1 = deg2rad((float)$ib['lat']);
|
|
$lat2 = deg2rad($pLat);
|
|
$dLat = $lat2 - $lat1;
|
|
$dLng = deg2rad($pLng - (float)$ib['lng']);
|
|
$a = sin($dLat/2)*sin($dLat/2) + cos($lat1)*cos($lat2)*sin($dLng/2)*sin($dLng/2);
|
|
$dist = $R * 2 * atan2(sqrt($a), sqrt(1-$a));
|
|
if ($dist > (float)$ib['radius']) {
|
|
http_response_code(403);
|
|
echo json_encode(['status' => 'error', 'message' => 'Penduduk ini berada di luar radius rumah ibadah Anda. Tidak diizinkan menghapus.']);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$query = "DELETE FROM penduduk_miskin WHERE id=$id";
|
|
|
|
if($conn->query($query)) {
|
|
echo json_encode(["status" => "success", "message" => "Berhasil dihapus."]);
|
|
} else {
|
|
echo json_encode(["status" => "error", "message" => "Gagal hapus: " . $conn->error]);
|
|
}
|
|
} else {
|
|
echo json_encode(["status" => "error", "message" => "ID tidak diberikan."]);
|
|
}
|
|
?>
|