Files
2026-06-13 11:24:58 +07:00

90 lines
3.8 KiB
PHP

<?php
/**
* api/wilayah.php — Wilayah (kelurahan) + statistik kemiskinan (KF-06) & CRUD.
*
* GET → FeatureCollection wilayah + persentase_miskin (publik).
* POST → tambah wilayah (operator). {nama, kode_wilayah, jenis, jumlah_penduduk, geometry}
* PUT ?id= → ubah atribut &/atau geometry (operator).
* DELETE ?id= → hapus (operator).
*/
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/../config/activity.php';
$pdo = Database::getConnection();
$method = $_SERVER['REQUEST_METHOD'];
if ($method !== 'GET') requireApiAnyRole(['operator']);
function nnw($v) { return ($v === '' || $v === null) ? null : $v; }
switch ($method) {
case 'GET':
$sql = "
SELECT w.id, w.nama, w.jenis, w.kode_wilayah, w.jumlah_penduduk,
COUNT(wm.id) AS jumlah_miskin, ST_AsGeoJSON(w.geom) AS geojson
FROM wilayah w
LEFT JOIN warga_miskin wm ON ST_Contains(w.geom, wm.geom)
GROUP BY w.id ORDER BY w.nama
";
$stmt = $pdo->query($sql);
$features = [];
while ($r = $stmt->fetch()) {
$penduduk = (int)$r['jumlah_penduduk'];
$miskin = (int)$r['jumlah_miskin'];
$persen = $penduduk > 0 ? round($miskin / $penduduk * 100, 2) : 0;
$features[] = [
'type' => 'Feature',
'geometry' => json_decode($r['geojson']),
'properties' => [
'id' => (int)$r['id'], 'nama' => $r['nama'], 'jenis' => $r['jenis'],
'kode_wilayah' => $r['kode_wilayah'], 'jumlah_penduduk' => $penduduk,
'jumlah_miskin' => $miskin, 'persentase_miskin' => $persen,
],
];
}
sendSuccess(['type' => 'FeatureCollection', 'features' => $features], 'Data Wilayah');
break;
case 'POST':
$d = getInput();
if (empty($d['nama']) || empty($d['geometry'])) sendError('Nama & geometri wajib diisi');
$pdo->prepare("INSERT INTO wilayah (nama, kode_wilayah, jenis, jumlah_penduduk, geom)
VALUES (?,?,?,?, ST_GeomFromGeoJSON(?))")
->execute([
$d['nama'], nnw($d['kode_wilayah'] ?? null), $d['jenis'] ?? 'kelurahan',
(int)($d['jumlah_penduduk'] ?? 0), json_encode($d['geometry']),
]);
$id = (int)$pdo->lastInsertId();
logActivity($pdo, 'create', 'wilayah', $id, "Tambah wilayah {$d['nama']}");
sendSuccess(['id' => $id], 'Wilayah disimpan', 201);
break;
case 'PUT':
$d = getInput();
$id = $_GET['id'] ?? null;
if (!$id) sendError('ID wajib');
if (!empty($d['geometry'])) {
$pdo->prepare("UPDATE wilayah SET nama=?, kode_wilayah=?, jenis=?, jumlah_penduduk=?, geom=ST_GeomFromGeoJSON(?) WHERE id=?")
->execute([$d['nama'], nnw($d['kode_wilayah'] ?? null), $d['jenis'] ?? 'kelurahan',
(int)($d['jumlah_penduduk'] ?? 0), json_encode($d['geometry']), $id]);
} else {
$pdo->prepare("UPDATE wilayah SET nama=?, kode_wilayah=?, jenis=?, jumlah_penduduk=? WHERE id=?")
->execute([$d['nama'], nnw($d['kode_wilayah'] ?? null), $d['jenis'] ?? 'kelurahan',
(int)($d['jumlah_penduduk'] ?? 0), $id]);
}
logActivity($pdo, 'update', 'wilayah', (int)$id, 'Ubah wilayah');
sendSuccess(null, 'Wilayah diperbarui');
break;
case 'DELETE':
$id = $_GET['id'] ?? null;
if (!$id) sendError('ID wajib');
$pdo->prepare("DELETE FROM wilayah WHERE id=?")->execute([$id]);
logActivity($pdo, 'delete', 'wilayah', (int)$id, 'Hapus wilayah');
sendSuccess(null, 'Wilayah dihapus');
break;
default:
sendError('Method not allowed', 405);
}