UAS SIG Proyek WebGis
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
// api/wilayah.php v2.0
|
||||
// ============================================================
|
||||
// API Data Wilayah (RT/RW/Kelurahan/Kecamatan)
|
||||
// ============================================================
|
||||
require_once __DIR__ . '/../includes/config.php';
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') exit;
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$db = getDB();
|
||||
|
||||
switch ($method) {
|
||||
|
||||
// ── GET semua wilayah atau per filter ─────────────────
|
||||
case 'GET':
|
||||
$kelurahan = $_GET['kelurahan'] ?? null;
|
||||
$kecamatan = $_GET['kecamatan'] ?? null;
|
||||
|
||||
// GET /api/wilayah.php?list=kelurahan → distinct kelurahan
|
||||
if (isset($_GET['list'])) {
|
||||
$type = $_GET['list'];
|
||||
if ($type === 'kelurahan') {
|
||||
$rows = $db->query("SELECT DISTINCT kelurahan, kecamatan FROM wilayah ORDER BY kelurahan")->fetchAll();
|
||||
jsonResponse(['success' => true, 'data' => $rows]);
|
||||
}
|
||||
if ($type === 'kecamatan') {
|
||||
$rows = $db->query("SELECT DISTINCT kecamatan FROM wilayah ORDER BY kecamatan")->fetchAll();
|
||||
jsonResponse(['success' => true, 'data' => array_column($rows, 'kecamatan')]);
|
||||
}
|
||||
}
|
||||
|
||||
$sql = "SELECT * FROM wilayah WHERE 1=1";
|
||||
$params = [];
|
||||
if ($kelurahan) { $sql .= " AND kelurahan = ?"; $params[] = $kelurahan; }
|
||||
if ($kecamatan) { $sql .= " AND kecamatan = ?"; $params[] = $kecamatan; }
|
||||
$sql .= " ORDER BY kelurahan, rt, rw";
|
||||
|
||||
$stmt = $db->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
jsonResponse(['success' => true, 'data' => $stmt->fetchAll()]);
|
||||
break;
|
||||
|
||||
// ── POST tambah wilayah ───────────────────────────────
|
||||
case 'POST':
|
||||
$in = getInput();
|
||||
if (empty($in['kelurahan']) || empty($in['kecamatan'])) {
|
||||
jsonResponse(['success' => false, 'error' => 'Kelurahan dan kecamatan wajib diisi'], 400);
|
||||
}
|
||||
$db->prepare("INSERT INTO wilayah (kelurahan, kecamatan, kota, rt, rw) VALUES (?,?,?,?,?)")
|
||||
->execute([
|
||||
trim($in['kelurahan']),
|
||||
trim($in['kecamatan']),
|
||||
trim($in['kota'] ?? 'Pontianak'),
|
||||
$in['rt'] ?? null,
|
||||
$in['rw'] ?? null,
|
||||
]);
|
||||
jsonResponse(['success' => true, 'id' => $db->lastInsertId(), 'message' => 'Wilayah ditambahkan']);
|
||||
break;
|
||||
|
||||
default:
|
||||
jsonResponse(['success' => false, 'error' => 'Method tidak diizinkan'], 405);
|
||||
}
|
||||
Reference in New Issue
Block a user