175 lines
5.2 KiB
PHP
175 lines
5.2 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
include 'koneksi.php';
|
|
|
|
// Handle OPTIONS preflight
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
$input = json_decode(file_get_contents("php://input"), true);
|
|
$action = $_GET['action'] ?? ($input['action'] ?? '');
|
|
|
|
function escape($data) {
|
|
global $conn;
|
|
return mysqli_real_escape_string($conn, $data);
|
|
}
|
|
|
|
// ==================== INSERT DATA ====================
|
|
if ($action == 'add_spbu') {
|
|
$nama = escape($input['nama'] ?? '');
|
|
$hp = escape($input['hp'] ?? '');
|
|
$buka = (int)($input['buka'] ?? 0);
|
|
$lat = (float)($input['lat'] ?? 0);
|
|
$lng = (float)($input['lng'] ?? 0);
|
|
|
|
$sql = "INSERT INTO spbu (nama, hp, buka, lat, lng)
|
|
VALUES ('$nama', '$hp', $buka, $lat, $lng)";
|
|
|
|
if ($conn->query($sql)) {
|
|
echo json_encode(['status' => 'ok', 'id' => $conn->insert_id]);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'msg' => $conn->error]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'add_jalan') {
|
|
$status = escape($input['status'] ?? '');
|
|
$panjang = (float)($input['panjang'] ?? 0);
|
|
$koordinat = escape($input['koordinat'] ?? '');
|
|
|
|
$sql = "INSERT INTO jalan (status, panjang, koordinat)
|
|
VALUES ('$status', $panjang, '$koordinat')";
|
|
|
|
if ($conn->query($sql)) {
|
|
echo json_encode(['status' => 'ok', 'id' => $conn->insert_id]);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'msg' => $conn->error]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'add_tanah') {
|
|
$status = escape($input['status'] ?? '');
|
|
$luas = (float)($input['luas'] ?? 0);
|
|
$koordinat = escape($input['koordinat'] ?? '');
|
|
|
|
$sql = "INSERT INTO tanah (status, luas, koordinat)
|
|
VALUES ('$status', $luas, '$koordinat')";
|
|
|
|
if ($conn->query($sql)) {
|
|
echo json_encode(['status' => 'ok', 'id' => $conn->insert_id]);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'msg' => $conn->error]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// ==================== DELETE DATA ====================
|
|
if ($action == 'delete_spbu') {
|
|
$id = (int)($input['id'] ?? $_GET['id'] ?? 0);
|
|
$sql = "DELETE FROM spbu WHERE id = $id";
|
|
if ($conn->query($sql)) {
|
|
echo json_encode(['status' => 'ok']);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'msg' => $conn->error]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'delete_jalan') {
|
|
$id = (int)($input['id'] ?? $_GET['id'] ?? 0);
|
|
$sql = "DELETE FROM jalan WHERE id = $id";
|
|
if ($conn->query($sql)) {
|
|
echo json_encode(['status' => 'ok']);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'msg' => $conn->error]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'delete_tanah') {
|
|
$id = (int)($input['id'] ?? $_GET['id'] ?? 0);
|
|
$sql = "DELETE FROM tanah WHERE id = $id";
|
|
if ($conn->query($sql)) {
|
|
echo json_encode(['status' => 'ok']);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'msg' => $conn->error]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// ==================== SELECT DATA ====================
|
|
if ($action == 'get_spbu') {
|
|
$result = $conn->query("SELECT * FROM spbu ORDER BY id DESC");
|
|
$data = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$data[] = $row;
|
|
}
|
|
echo json_encode($data);
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'get_jalan') {
|
|
$result = $conn->query("SELECT * FROM jalan ORDER BY id DESC");
|
|
$data = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$data[] = $row;
|
|
}
|
|
echo json_encode($data);
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'get_tanah') {
|
|
$result = $conn->query("SELECT * FROM tanah ORDER BY id DESC");
|
|
$data = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$data[] = $row;
|
|
}
|
|
echo json_encode($data);
|
|
exit;
|
|
}
|
|
|
|
// ==================== POPULASI ====================
|
|
if ($action == 'get_populasi') {
|
|
$result = $conn->query("SELECT * FROM populasi_kecamatan");
|
|
if ($result) {
|
|
$data = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$data[] = $row;
|
|
}
|
|
echo json_encode(['status' => 'ok', 'data' => $data]);
|
|
} else {
|
|
echo json_encode(['status' => 'ok', 'data' => []]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'update_populasi') {
|
|
$kecamatan = escape($input['kecamatan'] ?? '');
|
|
$populasi = (int)($input['populasi'] ?? 0);
|
|
|
|
$check = $conn->query("SELECT id FROM populasi_kecamatan WHERE kecamatan = '$kecamatan'");
|
|
if ($check && $check->num_rows > 0) {
|
|
$sql = "UPDATE populasi_kecamatan SET populasi = $populasi WHERE kecamatan = '$kecamatan'";
|
|
} else {
|
|
$sql = "INSERT INTO populasi_kecamatan (kecamatan, populasi) VALUES ('$kecamatan', $populasi)";
|
|
}
|
|
|
|
if ($conn->query($sql)) {
|
|
echo json_encode(['status' => 'ok']);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'msg' => $conn->error]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
// Jika action tidak dikenal
|
|
echo json_encode(['status' => 'error', 'msg' => 'Action tidak dikenal: ' . $action]);
|
|
?>
|