edit files
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
// api/controllers/PetaController.php
|
||||
// Returns GeoJSON-ready data for Leaflet
|
||||
class PetaController {
|
||||
public function index(): void {
|
||||
$db = getDB();
|
||||
$layer = $_GET['layer'] ?? 'penduduk';
|
||||
|
||||
if ($layer === 'rumah_ibadah') {
|
||||
$rows = $db->query("SELECT id,nama,jenis,lat,lng,radius_meter,alamat FROM rumah_ibadah WHERE aktif=1 AND lat IS NOT NULL")->fetchAll();
|
||||
$features = [];
|
||||
foreach ($rows as $r) {
|
||||
$features[] = [
|
||||
'type' => 'Feature',
|
||||
'geometry' => ['type'=>'Point','coordinates'=>[(float)$r['lng'],(float)$r['lat']]],
|
||||
'properties' => $r,
|
||||
];
|
||||
}
|
||||
jsonSuccess(['type'=>'FeatureCollection','features'=>$features]);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($layer === 'laporan') {
|
||||
$rows = $db->query(
|
||||
"SELECT id,nama_pelapor,deskripsi,lat,lng,urgensi,status,alamat_laporan FROM laporan
|
||||
WHERE lat IS NOT NULL ORDER BY FIELD(urgensi,'darurat','tinggi','sedang','rendah')"
|
||||
)->fetchAll();
|
||||
$features = [];
|
||||
foreach ($rows as $r) {
|
||||
$features[] = [
|
||||
'type' => 'Feature',
|
||||
'geometry' => ['type'=>'Point','coordinates'=>[(float)$r['lng'],(float)$r['lat']]],
|
||||
'properties' => $r,
|
||||
];
|
||||
}
|
||||
jsonSuccess(['type'=>'FeatureCollection','features'=>$features]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Default: penduduk (keluarga sebagai titik)
|
||||
$sql = "SELECT k.id, k.no_kk, k.nama_kepala, k.alamat, k.kecamatan,
|
||||
k.lat, k.lng, k.status_ekonomi,
|
||||
ri.nama AS nama_ri,
|
||||
COUNT(p.nik) AS jumlah_anggota,
|
||||
COUNT(DISTINCT hb.id) AS total_bantuan
|
||||
FROM keluarga k
|
||||
LEFT JOIN rumah_ibadah ri ON k.id_rumah_ibadah=ri.id
|
||||
LEFT JOIN penduduk p ON p.id_keluarga=k.id AND p.status_hidup='hidup'
|
||||
LEFT JOIN histori_bantuan hb ON hb.nik=p.nik
|
||||
WHERE k.lat IS NOT NULL";
|
||||
$params = [];
|
||||
if (!empty($_GET['status_ekonomi'])) {
|
||||
$sql .= " AND k.status_ekonomi=?"; $params[] = $_GET['status_ekonomi'];
|
||||
}
|
||||
$sql .= " GROUP BY k.id";
|
||||
$stmt = $db->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
$rows = $stmt->fetchAll();
|
||||
|
||||
$features = [];
|
||||
foreach ($rows as $r) {
|
||||
$color = match($r['status_ekonomi']) {
|
||||
'miskin' => '#ef4444',
|
||||
'rentan' => '#f59e0b',
|
||||
default => '#22c55e',
|
||||
};
|
||||
$r['color'] = $color;
|
||||
$features[] = [
|
||||
'type' => 'Feature',
|
||||
'geometry' => ['type'=>'Point','coordinates'=>[(float)$r['lng'],(float)$r['lat']]],
|
||||
'properties' => $r,
|
||||
];
|
||||
}
|
||||
jsonSuccess(['type'=>'FeatureCollection','features'=>$features]);
|
||||
}
|
||||
public function show(string $id): void { jsonError('Not implemented', 501); }
|
||||
public function store(): void { jsonError('Not implemented', 501); }
|
||||
public function update(string $id): void { jsonError('Not implemented', 501); }
|
||||
public function destroy(string $id): void { jsonError('Not implemented', 501); }
|
||||
}
|
||||
Reference in New Issue
Block a user