edit files
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
// api/controllers/PeristiwaController.php
|
||||
class PeristiwaController {
|
||||
public function index(): void {
|
||||
requireAuth([ROLE_ADMIN, ROLE_PENGURUS, ROLE_PIMPINAN]);
|
||||
$db = getDB();
|
||||
$rows = $db->query(
|
||||
"SELECT pk.*, p.nama_lengkap FROM peristiwa_kependudukan pk
|
||||
JOIN penduduk p ON pk.nik=p.nik
|
||||
ORDER BY pk.tanggal DESC LIMIT 100"
|
||||
)->fetchAll();
|
||||
jsonSuccess($rows);
|
||||
}
|
||||
public function show(string $id): void { jsonError('N/A',501); }
|
||||
public function store(): void {
|
||||
requireAuth([ROLE_ADMIN, ROLE_PENGURUS]);
|
||||
$user = currentUser();
|
||||
$body = getBody();
|
||||
$errs = required($body, ['nik','jenis','tanggal']);
|
||||
if ($errs) jsonError(implode('; ',$errs));
|
||||
$db = getDB();
|
||||
|
||||
$db->prepare(
|
||||
"INSERT INTO peristiwa_kependudukan (nik,jenis,tanggal,keterangan,id_user)
|
||||
VALUES (?,?,?,?,?)"
|
||||
)->execute([
|
||||
$body['nik'], $body['jenis'], $body['tanggal'],
|
||||
sanitize($body['keterangan'] ?? ''), $user['id'],
|
||||
]);
|
||||
|
||||
// Otomatis update status penduduk
|
||||
switch ($body['jenis']) {
|
||||
case 'kematian':
|
||||
$db->prepare("UPDATE penduduk SET status_hidup='meninggal' WHERE nik=?")->execute([$body['nik']]);
|
||||
break;
|
||||
case 'pernikahan':
|
||||
$db->prepare("UPDATE penduduk SET status_perkawinan='kawin' WHERE nik=?")->execute([$body['nik']]);
|
||||
break;
|
||||
case 'perceraian':
|
||||
$db->prepare("UPDATE penduduk SET status_perkawinan='cerai_hidup' WHERE nik=?")->execute([$body['nik']]);
|
||||
break;
|
||||
}
|
||||
|
||||
logActivity('peristiwa_'.$body['jenis'], 'penduduk', $body['nik'], [], $body);
|
||||
jsonSuccess(['id' => $db->lastInsertId()], 'Peristiwa dicatat', 201);
|
||||
}
|
||||
public function update(string $id): void { jsonError('N/A',405); }
|
||||
public function destroy(string $id): void {
|
||||
requireAuth([ROLE_ADMIN]);
|
||||
getDB()->prepare("DELETE FROM peristiwa_kependudukan WHERE id=?")->execute([$id]);
|
||||
jsonSuccess([],'Peristiwa dihapus');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user