Initial commit
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
function isLoggedIn() {
|
||||
return isset($_SESSION['user_id']);
|
||||
}
|
||||
|
||||
function getCurrentUser() {
|
||||
if (!isLoggedIn()) {
|
||||
return null;
|
||||
}
|
||||
return [
|
||||
'id' => $_SESSION['user_id'],
|
||||
'username' => $_SESSION['username'],
|
||||
'role' => $_SESSION['role'],
|
||||
'ibadah_id' => $_SESSION['ibadah_id'] ? intval($_SESSION['ibadah_id']) : null
|
||||
];
|
||||
}
|
||||
|
||||
function requireLogin() {
|
||||
if (!isLoggedIn()) {
|
||||
header('HTTP/1.1 401 Unauthorized');
|
||||
echo json_encode(['status' => 'error', 'message' => 'Unauthorized: Silakan login terlebih dahulu.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function requireAdmin() {
|
||||
requireLogin();
|
||||
if ($_SESSION['role'] !== 'admin') {
|
||||
header('HTTP/1.1 403 Forbidden');
|
||||
echo json_encode(['status' => 'error', 'message' => 'Forbidden: Akses ditolak. Hanya Admin yang diizinkan.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function requireAdminOrPengelola() {
|
||||
requireLogin();
|
||||
if ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'pengelola') {
|
||||
header('HTTP/1.1 403 Forbidden');
|
||||
echo json_encode(['status' => 'error', 'message' => 'Forbidden: Akses ditolak.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function canManageIbadah($ibadah_id) {
|
||||
if (!isLoggedIn()) return false;
|
||||
if ($_SESSION['role'] === 'admin') return true;
|
||||
if ($_SESSION['role'] === 'pengelola') {
|
||||
return intval($_SESSION['ibadah_id']) === intval($ibadah_id);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
if (isset($_SESSION['user_id'])) {
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'isLoggedIn' => true,
|
||||
'data' => [
|
||||
'id' => intval($_SESSION['user_id']),
|
||||
'username' => $_SESSION['username'],
|
||||
'role' => $_SESSION['role'],
|
||||
'ibadah_id' => $_SESSION['ibadah_id'] ? intval($_SESSION['ibadah_id']) : null,
|
||||
'nama_ibadah' => $_SESSION['nama_ibadah'] ?? null
|
||||
]
|
||||
]);
|
||||
} else {
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'isLoggedIn' => false,
|
||||
'data' => null
|
||||
]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
require_once '../auth_helper.php';
|
||||
requireLogin();
|
||||
include_once '../db_connect.php';
|
||||
|
||||
$user = getCurrentUser();
|
||||
$isPengelola = $user['role'] === 'pengelola';
|
||||
$ibadahId = $isPengelola ? intval($user['ibadah_id']) : null;
|
||||
|
||||
// ── Helper: Haversine distance in meters ──────────────────────────────────
|
||||
function haversine($lat1, $lng1, $lat2, $lng2) {
|
||||
$R = 6371000;
|
||||
$dLat = deg2rad($lat2 - $lat1);
|
||||
$dLng = deg2rad($lng2 - $lng1);
|
||||
$a = sin($dLat/2)*sin($dLat/2)
|
||||
+ cos(deg2rad($lat1))*cos(deg2rad($lat2))*sin($dLng/2)*sin($dLng/2);
|
||||
return $R * 2 * atan2(sqrt($a), sqrt(1-$a));
|
||||
}
|
||||
|
||||
// ── 1. Rumah Ibadah ───────────────────────────────────────────────────────
|
||||
if ($isPengelola && $ibadahId) {
|
||||
$riRes = $conn->query("SELECT * FROM rumah_ibadah WHERE id=$ibadahId");
|
||||
} else {
|
||||
$riRes = $conn->query("SELECT * FROM rumah_ibadah");
|
||||
}
|
||||
$ibadahList = [];
|
||||
while ($row = $riRes->fetch_assoc()) $ibadahList[] = $row;
|
||||
$totalIbadah = count($ibadahList);
|
||||
|
||||
// Distribusi per jenis
|
||||
$jenisDist = [];
|
||||
foreach ($ibadahList as $ib) {
|
||||
$j = $ib['jenis'] ?? 'Lainnya';
|
||||
$jenisDist[$j] = ($jenisDist[$j] ?? 0) + 1;
|
||||
}
|
||||
|
||||
// ── 2. Penduduk Miskin ────────────────────────────────────────────────────
|
||||
$pmRes = $conn->query("SELECT * FROM penduduk_miskin");
|
||||
$allMiskin = [];
|
||||
while ($row = $pmRes->fetch_assoc()) $allMiskin[] = $row;
|
||||
|
||||
// Filter: pengelola hanya lihat yang dalam radius ibadahnya
|
||||
if ($isPengelola && $ibadahId && !empty($ibadahList)) {
|
||||
$ib = $ibadahList[0];
|
||||
$filteredMiskin = array_filter($allMiskin, function($m) use ($ib) {
|
||||
return haversine((float)$ib['lat'], (float)$ib['lng'], (float)$m['lat'], (float)$m['lng']) <= (float)$ib['radius'];
|
||||
});
|
||||
$filteredMiskin = array_values($filteredMiskin);
|
||||
} else {
|
||||
$filteredMiskin = $allMiskin;
|
||||
}
|
||||
|
||||
$totalMiskin = count($filteredMiskin);
|
||||
$totalJiwa = array_sum(array_column($filteredMiskin, 'jumlah_jiwa'));
|
||||
|
||||
// Kategori bantuan
|
||||
$kategoriDist = [];
|
||||
foreach ($filteredMiskin as $m) {
|
||||
$k = $m['kategori_bantuan'] ?? 'Lainnya';
|
||||
$kategoriDist[$k] = ($kategoriDist[$k] ?? 0) + 1;
|
||||
}
|
||||
|
||||
// Terurus vs Tidak (dalam radius salah satu ibadah)
|
||||
$terurusCount = 0;
|
||||
$tidakTerurusCount = 0;
|
||||
foreach ($filteredMiskin as $m) {
|
||||
$inAnyRadius = false;
|
||||
foreach ($ibadahList as $ib) {
|
||||
if (haversine((float)$ib['lat'], (float)$ib['lng'], (float)$m['lat'], (float)$m['lng']) <= (float)$ib['radius']) {
|
||||
$inAnyRadius = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($inAnyRadius) $terurusCount++;
|
||||
else $tidakTerurusCount++;
|
||||
}
|
||||
|
||||
// Jika admin: hitung seluruh miskin dalam radius seluruh ibadah
|
||||
if (!$isPengelola) {
|
||||
$allIbadahRes = $conn->query("SELECT * FROM rumah_ibadah");
|
||||
$allIbadahList = [];
|
||||
while ($row = $allIbadahRes->fetch_assoc()) $allIbadahList[] = $row;
|
||||
|
||||
$terurusCount = 0;
|
||||
$tidakTerurusCount = 0;
|
||||
foreach ($allMiskin as $m) {
|
||||
$inAnyRadius = false;
|
||||
foreach ($allIbadahList as $ib) {
|
||||
if (haversine((float)$ib['lat'], (float)$ib['lng'], (float)$m['lat'], (float)$m['lng']) <= (float)$ib['radius']) {
|
||||
$inAnyRadius = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($inAnyRadius) $terurusCount++;
|
||||
else $tidakTerurusCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// ── 3. Log Bantuan ────────────────────────────────────────────────────────
|
||||
if ($isPengelola && $ibadahId) {
|
||||
$logRes = $conn->query("
|
||||
SELECT lb.*, ri.nama as nama_ibadah, ri.jenis as jenis_ibadah, pm.nama as nama_kk
|
||||
FROM log_bantuan lb
|
||||
JOIN rumah_ibadah ri ON ri.id = lb.ibadah_id
|
||||
JOIN penduduk_miskin pm ON pm.id = lb.miskin_id
|
||||
WHERE lb.ibadah_id = $ibadahId
|
||||
ORDER BY lb.tanggal DESC, lb.id DESC
|
||||
LIMIT 10
|
||||
");
|
||||
$totalLogRes = $conn->query("SELECT COUNT(*) as cnt FROM log_bantuan WHERE ibadah_id=$ibadahId");
|
||||
} else {
|
||||
$logRes = $conn->query("
|
||||
SELECT lb.*, ri.nama as nama_ibadah, ri.jenis as jenis_ibadah, pm.nama as nama_kk
|
||||
FROM log_bantuan lb
|
||||
JOIN rumah_ibadah ri ON ri.id = lb.ibadah_id
|
||||
JOIN penduduk_miskin pm ON pm.id = lb.miskin_id
|
||||
ORDER BY lb.tanggal DESC, lb.id DESC
|
||||
LIMIT 10
|
||||
");
|
||||
$totalLogRes = $conn->query("SELECT COUNT(*) as cnt FROM log_bantuan");
|
||||
}
|
||||
|
||||
$recentLogs = [];
|
||||
while ($row = $logRes->fetch_assoc()) $recentLogs[] = $row;
|
||||
|
||||
$totalLog = 0;
|
||||
if ($totalLogRes && $trow = $totalLogRes->fetch_assoc()) $totalLog = (int)$trow['cnt'];
|
||||
|
||||
// Distribusi tipe bantuan dari log
|
||||
$tipeDist = [];
|
||||
if ($isPengelola && $ibadahId) {
|
||||
$tipeRes = $conn->query("SELECT tipe_bantuan, COUNT(*) as cnt FROM log_bantuan WHERE ibadah_id=$ibadahId GROUP BY tipe_bantuan");
|
||||
} else {
|
||||
$tipeRes = $conn->query("SELECT tipe_bantuan, COUNT(*) as cnt FROM log_bantuan GROUP BY tipe_bantuan");
|
||||
}
|
||||
while ($row = $tipeRes->fetch_assoc()) $tipeDist[$row['tipe_bantuan']] = (int)$row['cnt'];
|
||||
|
||||
// Bantuan per bulan (6 bulan terakhir)
|
||||
if ($isPengelola && $ibadahId) {
|
||||
$bulanRes = $conn->query("
|
||||
SELECT DATE_FORMAT(tanggal, '%Y-%m') as bulan, COUNT(*) as cnt
|
||||
FROM log_bantuan
|
||||
WHERE ibadah_id=$ibadahId AND tanggal >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
|
||||
GROUP BY bulan ORDER BY bulan ASC
|
||||
");
|
||||
} else {
|
||||
$bulanRes = $conn->query("
|
||||
SELECT DATE_FORMAT(tanggal, '%Y-%m') as bulan, COUNT(*) as cnt
|
||||
FROM log_bantuan
|
||||
WHERE tanggal >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
|
||||
GROUP BY bulan ORDER BY bulan ASC
|
||||
");
|
||||
}
|
||||
$bantuanPerBulan = [];
|
||||
while ($row = $bulanRes->fetch_assoc()) $bantuanPerBulan[] = $row;
|
||||
|
||||
// ── 4. Ibadah Tipe (admin only: performa per ibadah) ─────────────────────
|
||||
$ibadahPerforma = [];
|
||||
if (!$isPengelola) {
|
||||
$perfRes = $conn->query("
|
||||
SELECT ri.id, ri.nama, ri.jenis, ri.radius,
|
||||
COUNT(DISTINCT lb.id) as total_log,
|
||||
COUNT(DISTINCT lb.miskin_id) as total_penerima
|
||||
FROM rumah_ibadah ri
|
||||
LEFT JOIN log_bantuan lb ON lb.ibadah_id = ri.id
|
||||
GROUP BY ri.id
|
||||
ORDER BY total_log DESC
|
||||
");
|
||||
while ($row = $perfRes->fetch_assoc()) $ibadahPerforma[] = $row;
|
||||
}
|
||||
|
||||
// ── Output ────────────────────────────────────────────────────────────────
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'role' => $user['role'],
|
||||
'ibadah_id' => $ibadahId,
|
||||
'stats' => [
|
||||
'total_ibadah' => $totalIbadah,
|
||||
'total_miskin' => $totalMiskin,
|
||||
'total_jiwa' => $totalJiwa,
|
||||
'total_log' => $totalLog,
|
||||
'terurus' => $terurusCount,
|
||||
'tidak_terurus' => $tidakTerurusCount,
|
||||
'pct_terurus' => $totalMiskin > 0 ? round($terurusCount / $totalMiskin * 100, 1) : 0,
|
||||
],
|
||||
'distribusi' => [
|
||||
'jenis_ibadah' => $jenisDist,
|
||||
'kategori' => $kategoriDist,
|
||||
'tipe_bantuan' => $tipeDist,
|
||||
'per_bulan' => $bantuanPerBulan,
|
||||
],
|
||||
'ibadah_list' => $ibadahList,
|
||||
'ibadah_performa' => $ibadahPerforma,
|
||||
'recent_logs' => $recentLogs,
|
||||
]);
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
/**
|
||||
* stats_extended.php
|
||||
* Dashboard terintegrasi: webgis_poverty + jalan + spbu + penduduk choropleth
|
||||
*/
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
require_once '../auth_helper.php';
|
||||
requireLogin();
|
||||
|
||||
// ── Helper: buat koneksi terpisah ─────────────────────────────────────────
|
||||
function makeConn($dbName) {
|
||||
$c = new mysqli("localhost", "root", "", $dbName);
|
||||
if ($c->connect_error) return null;
|
||||
$c->set_charset("utf8mb4");
|
||||
return $c;
|
||||
}
|
||||
|
||||
// ── Koneksi ke masing-masing database ────────────────────────────────────
|
||||
$connPoverty = makeConn("webgis_poverty"); // atau "webgis" sesuai db.sql
|
||||
$connJalan = makeConn("jalan");
|
||||
$connSpbu = makeConn("spbu");
|
||||
|
||||
// ── 1. Data dari webgis_poverty (kemiskinan) ─────────────────────────────
|
||||
$poverty = [
|
||||
'total_miskin' => 0,
|
||||
'total_jiwa' => 0,
|
||||
'total_ibadah' => 0,
|
||||
'total_log' => 0,
|
||||
'terurus' => 0,
|
||||
'tidak_terurus' => 0,
|
||||
'pct_terurus' => 0,
|
||||
'distribusi' => ['kategori' => [], 'tipe_bantuan' => [], 'per_bulan' => []],
|
||||
];
|
||||
|
||||
if ($connPoverty) {
|
||||
// Total KK miskin & jiwa
|
||||
$r = $connPoverty->query("SELECT COUNT(*) as c, COALESCE(SUM(jumlah_jiwa),0) as j FROM penduduk_miskin");
|
||||
if ($r && $row = $r->fetch_assoc()) {
|
||||
$poverty['total_miskin'] = (int)$row['c'];
|
||||
$poverty['total_jiwa'] = (int)$row['j'];
|
||||
}
|
||||
|
||||
// Total rumah ibadah
|
||||
$r = $connPoverty->query("SELECT COUNT(*) as c FROM rumah_ibadah");
|
||||
if ($r && $row = $r->fetch_assoc()) $poverty['total_ibadah'] = (int)$row['c'];
|
||||
|
||||
// Total log bantuan
|
||||
$r = $connPoverty->query("SELECT COUNT(*) as c FROM log_bantuan");
|
||||
if ($r && $row = $r->fetch_assoc()) $poverty['total_log'] = (int)$row['c'];
|
||||
|
||||
// Kategori bantuan
|
||||
$r = $connPoverty->query("SELECT kategori_bantuan, COUNT(*) as c FROM penduduk_miskin GROUP BY kategori_bantuan");
|
||||
if ($r) while ($row = $r->fetch_assoc()) $poverty['distribusi']['kategori'][$row['kategori_bantuan']] = (int)$row['c'];
|
||||
|
||||
// Tipe bantuan dari log
|
||||
$r = $connPoverty->query("SELECT tipe_bantuan, COUNT(*) as c FROM log_bantuan GROUP BY tipe_bantuan");
|
||||
if ($r) while ($row = $r->fetch_assoc()) $poverty['distribusi']['tipe_bantuan'][$row['tipe_bantuan']] = (int)$row['c'];
|
||||
|
||||
// Bantuan per bulan (6 bulan terakhir)
|
||||
$r = $connPoverty->query("
|
||||
SELECT DATE_FORMAT(tanggal,'%Y-%m') as bulan, COUNT(*) as cnt
|
||||
FROM log_bantuan
|
||||
WHERE tanggal >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
|
||||
GROUP BY bulan ORDER BY bulan ASC
|
||||
");
|
||||
if ($r) while ($row = $r->fetch_assoc()) $poverty['distribusi']['per_bulan'][] = $row;
|
||||
|
||||
// Terurus vs tidak (radius sederhana: ada di radius salah satu ibadah)
|
||||
$ibadahRes = $connPoverty->query("SELECT lat, lng, radius FROM rumah_ibadah");
|
||||
$ibadahArr = [];
|
||||
if ($ibadahRes) while ($row = $ibadahRes->fetch_assoc()) $ibadahArr[] = $row;
|
||||
|
||||
$miskinRes = $connPoverty->query("SELECT lat, lng FROM penduduk_miskin");
|
||||
if ($miskinRes) {
|
||||
while ($m = $miskinRes->fetch_assoc()) {
|
||||
$inRadius = false;
|
||||
foreach ($ibadahArr as $ib) {
|
||||
$dlat = deg2rad($ib['lat'] - $m['lat']);
|
||||
$dlng = deg2rad($ib['lng'] - $m['lng']);
|
||||
$a = sin($dlat/2)**2 + cos(deg2rad($m['lat']))*cos(deg2rad($ib['lat']))*sin($dlng/2)**2;
|
||||
$dist = 6371000 * 2 * atan2(sqrt($a), sqrt(1-$a));
|
||||
if ($dist <= (float)$ib['radius']) { $inRadius = true; break; }
|
||||
}
|
||||
if ($inRadius) $poverty['terurus']++; else $poverty['tidak_terurus']++;
|
||||
}
|
||||
$tot = $poverty['total_miskin'];
|
||||
$poverty['pct_terurus'] = $tot > 0 ? round($poverty['terurus'] / $tot * 100, 1) : 0;
|
||||
}
|
||||
$connPoverty->close();
|
||||
}
|
||||
|
||||
// ── 2. Data Jalan ─────────────────────────────────────────────────────────
|
||||
$jalan = [
|
||||
'total_jalan' => 0,
|
||||
'total_panjang_m' => 0,
|
||||
'per_status' => [],
|
||||
'total_parsil' => 0,
|
||||
'total_luas_m2' => 0,
|
||||
'per_kepemilikan' => [],
|
||||
];
|
||||
|
||||
if ($connJalan) {
|
||||
$r = $connJalan->query("SELECT COUNT(*) as c, COALESCE(SUM(panjang_meter),0) as p FROM jalan");
|
||||
if ($r && $row = $r->fetch_assoc()) {
|
||||
$jalan['total_jalan'] = (int)$row['c'];
|
||||
$jalan['total_panjang_m'] = (float)$row['p'];
|
||||
}
|
||||
|
||||
$r = $connJalan->query("SELECT status_jalan, COUNT(*) as c FROM jalan GROUP BY status_jalan");
|
||||
if ($r) while ($row = $r->fetch_assoc()) $jalan['per_status'][$row['status_jalan']] = (int)$row['c'];
|
||||
|
||||
$r = $connJalan->query("SELECT COUNT(*) as c, COALESCE(SUM(luas_m2),0) as l FROM parsil_tanah");
|
||||
if ($r && $row = $r->fetch_assoc()) {
|
||||
$jalan['total_parsil'] = (int)$row['c'];
|
||||
$jalan['total_luas_m2'] = (float)$row['l'];
|
||||
}
|
||||
|
||||
$r = $connJalan->query("SELECT status_kepemilikan, COUNT(*) as c FROM parsil_tanah GROUP BY status_kepemilikan");
|
||||
if ($r) while ($row = $r->fetch_assoc()) $jalan['per_kepemilikan'][$row['status_kepemilikan']] = (int)$row['c'];
|
||||
|
||||
$connJalan->close();
|
||||
}
|
||||
|
||||
// ── 3. Data SPBU ──────────────────────────────────────────────────────────
|
||||
$spbu = [
|
||||
'total_spbu' => 0,
|
||||
'buka_24_jam' => 0,
|
||||
'tidak_24_jam' => 0,
|
||||
'list' => [],
|
||||
];
|
||||
|
||||
if ($connSpbu) {
|
||||
$r = $connSpbu->query("SELECT COUNT(*) as c FROM spbu");
|
||||
if ($r && $row = $r->fetch_assoc()) $spbu['total_spbu'] = (int)$row['c'];
|
||||
|
||||
$r = $connSpbu->query("SELECT SUM(buka_24_jam=1) as buka, SUM(buka_24_jam=0) as tidak FROM spbu");
|
||||
if ($r && $row = $r->fetch_assoc()) {
|
||||
$spbu['buka_24_jam'] = (int)$row['buka'];
|
||||
$spbu['tidak_24_jam'] = (int)$row['tidak'];
|
||||
}
|
||||
|
||||
$r = $connSpbu->query("SELECT nama_spbu, nomor_wa, buka_24_jam, latitude, longitude FROM spbu ORDER BY nama_spbu");
|
||||
if ($r) while ($row = $r->fetch_assoc()) $spbu['list'][] = $row;
|
||||
|
||||
$connSpbu->close();
|
||||
}
|
||||
|
||||
// ── 4. Data Penduduk (choropleth — static dari pontianak.json) ────────────
|
||||
// Data kepadatan per kecamatan sudah ada di file JS, ringkasan di sini
|
||||
$penduduk = [
|
||||
'kecamatan' => [
|
||||
['nama' => 'Pontianak Barat', 'density' => 9.331],
|
||||
['nama' => 'Pontianak Kota', 'density' => 8.102],
|
||||
['nama' => 'Pontianak Timur', 'density' => 9.196],
|
||||
['nama' => 'Pontianak Selatan', 'density' => 5.673],
|
||||
['nama' => 'Pontianak Utara', 'density' => 3.642],
|
||||
['nama' => 'Pontianak Tenggara', 'density' => 3.103],
|
||||
]
|
||||
];
|
||||
$penduduk['total_kecamatan'] = count($penduduk['kecamatan']);
|
||||
$penduduk['rata_density'] = round(array_sum(array_column($penduduk['kecamatan'],'density')) / $penduduk['total_kecamatan'], 3);
|
||||
$penduduk['terpadat'] = $penduduk['kecamatan'][array_search(max(array_column($penduduk['kecamatan'],'density')), array_column($penduduk['kecamatan'],'density'))]['nama'];
|
||||
|
||||
// ── Output ────────────────────────────────────────────────────────────────
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'poverty' => $poverty,
|
||||
'jalan' => $jalan,
|
||||
'spbu' => $spbu,
|
||||
'penduduk' => $penduduk,
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
$host = "localhost";
|
||||
$user = "root";
|
||||
$pass = "";
|
||||
$db = "webgis";
|
||||
|
||||
// Coba koneksi ke server dan pilih database
|
||||
$conn = new mysqli($host, $user, $pass, $db);
|
||||
|
||||
if ($conn->connect_error) {
|
||||
die("Koneksi gagal: " . $conn->connect_error);
|
||||
}
|
||||
// Set charset
|
||||
$conn->set_charset("utf8mb4");
|
||||
|
||||
// Jika di-include oleh file API, biarkan $conn tersedia
|
||||
?>
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
require_once '../auth_helper.php';
|
||||
requireAdminOrPengelola();
|
||||
require_once '../db_connect.php';
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$miskin_id = intval($data['miskin_id'] ?? 0);
|
||||
$ibadah_id = intval($data['ibadah_id'] ?? 0);
|
||||
$tipe_bantuan = trim($data['tipe_bantuan'] ?? '');
|
||||
$tanggal = trim($data['tanggal'] ?? '');
|
||||
$keterangan = trim($data['keterangan'] ?? '');
|
||||
|
||||
if ($_SESSION['role'] === 'pengelola') {
|
||||
if ($ibadah_id !== intval($_SESSION['ibadah_id'])) {
|
||||
header('HTTP/1.1 403 Forbidden');
|
||||
echo json_encode(['status' => 'error', 'message' => 'Forbidden: Anda tidak diizinkan membuat log untuk rumah ibadah lain.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$miskin_id || !$ibadah_id || !$tipe_bantuan || !$tanggal) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Data tidak lengkap']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql = "INSERT INTO log_bantuan (miskin_id, ibadah_id, tipe_bantuan, tanggal, keterangan) VALUES (?, ?, ?, ?, ?)";
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->bind_param('iisss', $miskin_id, $ibadah_id, $tipe_bantuan, $tanggal, $keterangan);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
echo json_encode(['status' => 'success', 'id' => $stmt->insert_id]);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => $conn->error]);
|
||||
}
|
||||
$conn->close();
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
require_once '../db_connect.php';
|
||||
|
||||
$miskin_id = isset($_GET['miskin_id']) ? intval($_GET['miskin_id']) : 0;
|
||||
|
||||
if (!$miskin_id) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'miskin_id diperlukan']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql = "
|
||||
SELECT
|
||||
lb.id,
|
||||
lb.tipe_bantuan,
|
||||
lb.tanggal,
|
||||
lb.keterangan,
|
||||
ri.nama AS nama_ibadah,
|
||||
ri.jenis AS jenis_ibadah
|
||||
FROM log_bantuan lb
|
||||
JOIN rumah_ibadah ri ON ri.id = lb.ibadah_id
|
||||
WHERE lb.miskin_id = ?
|
||||
ORDER BY lb.tanggal DESC, lb.id DESC
|
||||
";
|
||||
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->bind_param('i', $miskin_id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
$data = [];
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$data[] = $row;
|
||||
}
|
||||
|
||||
echo json_encode(['status' => 'success', 'data' => $data]);
|
||||
$conn->close();
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
require_once 'db_connect.php';
|
||||
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
$username = trim($data['username'] ?? '');
|
||||
$password = trim($data['password'] ?? '');
|
||||
|
||||
if (empty($username) || empty($password)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Username dan password wajib diisi']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Cari user di database
|
||||
$sql = "SELECT u.*, ri.nama as nama_ibadah
|
||||
FROM users u
|
||||
LEFT JOIN rumah_ibadah ri ON u.ibadah_id = ri.id
|
||||
WHERE u.username = ?";
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->bind_param('s', $username);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result->num_rows === 1) {
|
||||
$user = $result->fetch_assoc();
|
||||
// Verifikasi password
|
||||
if (password_verify($password, $user['password'])) {
|
||||
$_SESSION['user_id'] = $user['id'];
|
||||
$_SESSION['username'] = $user['username'];
|
||||
$_SESSION['role'] = $user['role'];
|
||||
$_SESSION['ibadah_id'] = $user['ibadah_id'];
|
||||
$_SESSION['nama_ibadah'] = $user['nama_ibadah'];
|
||||
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'message' => 'Login berhasil',
|
||||
'data' => [
|
||||
'id' => intval($user['id']),
|
||||
'username' => $user['username'],
|
||||
'role' => $user['role'],
|
||||
'ibadah_id' => $user['ibadah_id'] ? intval($user['ibadah_id']) : null,
|
||||
'nama_ibadah' => $user['nama_ibadah']
|
||||
]
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode(['status' => 'error', 'message' => 'Username atau password salah']);
|
||||
$conn->close();
|
||||
?>
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
$_SESSION = array();
|
||||
|
||||
if (ini_get("session.use_cookies")) {
|
||||
$params = session_get_cookie_params();
|
||||
setcookie(session_name(), '', time() - 42000,
|
||||
$params["path"], $params["domain"],
|
||||
$params["secure"], $params["httponly"]
|
||||
);
|
||||
}
|
||||
|
||||
session_destroy();
|
||||
|
||||
echo json_encode(['status' => 'success', 'message' => 'Logout berhasil']);
|
||||
?>
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
require_once '../auth_helper.php';
|
||||
requireAdminOrPengelola();
|
||||
include_once '../db_connect.php';
|
||||
|
||||
// Ambil info ibadah pengelola jika role pengelola
|
||||
$currentUser = getCurrentUser();
|
||||
$ibadahFilter = null; // null = admin (tidak dibatasi)
|
||||
if ($currentUser && $currentUser['role'] === 'pengelola') {
|
||||
$ibadah_id = $currentUser['ibadah_id'];
|
||||
if (!$ibadah_id) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Akun pengelola tidak terhubung ke rumah ibadah manapun.']);
|
||||
exit;
|
||||
}
|
||||
$res = $conn->query("SELECT lat, lng, radius FROM rumah_ibadah WHERE id=$ibadah_id");
|
||||
if (!$res || $res->num_rows === 0) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Rumah ibadah yang dikelola tidak ditemukan.']);
|
||||
exit;
|
||||
}
|
||||
$ibadahFilter = $res->fetch_assoc();
|
||||
}
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if (is_array($data)) {
|
||||
$conn->begin_transaction();
|
||||
$inserted = 0;
|
||||
$skipped = 0;
|
||||
$errors = [];
|
||||
foreach ($data as $item) {
|
||||
if (isset($item->lat) && isset($item->lng) && $item->lat !== '' && $item->lng !== '') {
|
||||
$nama = $conn->real_escape_string($item->nama ?? '');
|
||||
$kategori = $conn->real_escape_string($item->kategori_bantuan ?? 'Makan');
|
||||
$jumlah_jiwa = (int)($item->jumlah_jiwa ?? 1);
|
||||
$lat = (float)$item->lat;
|
||||
$lng = (float)$item->lng;
|
||||
|
||||
// Jika pengelola, cek apakah item berada dalam radius ibadahnya
|
||||
if ($ibadahFilter !== null) {
|
||||
$R = 6371000;
|
||||
$lat1 = deg2rad((float)$ibadahFilter['lat']);
|
||||
$lat2 = deg2rad($lat);
|
||||
$dLat = $lat2 - $lat1;
|
||||
$dLng = deg2rad($lng - (float)$ibadahFilter['lng']);
|
||||
$a = sin($dLat/2)*sin($dLat/2) + cos($lat1)*cos($lat2)*sin($dLng/2)*sin($dLng/2);
|
||||
$dist = $R * 2 * atan2(sqrt($a), sqrt(1-$a));
|
||||
if ($dist > (float)$ibadahFilter['radius']) {
|
||||
$skipped++;
|
||||
continue; // Lewati item di luar radius
|
||||
}
|
||||
}
|
||||
|
||||
$query = "INSERT INTO penduduk_miskin (nama, kategori_bantuan, jumlah_jiwa, lat, lng)
|
||||
VALUES ('$nama', '$kategori', $jumlah_jiwa, $lat, $lng)";
|
||||
|
||||
if (!$conn->query($query)) {
|
||||
$errors[] = $conn->error;
|
||||
} else {
|
||||
$inserted++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($errors) && $inserted > 0) {
|
||||
$conn->commit();
|
||||
$msg = "Berhasil mengimpor $inserted data penduduk miskin.";
|
||||
if ($skipped > 0) $msg .= " ($skipped data di luar radius diabaikan.)";
|
||||
echo json_encode(["status" => "success", "message" => $msg]);
|
||||
} else if ($inserted === 0) {
|
||||
$conn->rollback();
|
||||
$msg = $skipped > 0
|
||||
? "Semua $skipped data berada di luar radius rumah ibadah Anda, tidak ada yang diimpor."
|
||||
: "Tidak ada data valid yang diimpor.";
|
||||
echo json_encode(["status" => "error", "message" => $msg]);
|
||||
} else {
|
||||
$conn->rollback();
|
||||
echo json_encode(["status" => "error", "message" => "Gagal mengimpor beberapa data. Transaksi dibatalkan.", "errors" => $errors]);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "Format data salah."]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
require_once '../auth_helper.php';
|
||||
requireAdminOrPengelola();
|
||||
include_once '../db_connect.php';
|
||||
|
||||
$nama = '';
|
||||
$kategori = 'Makan';
|
||||
$jumlah_jiwa = 1;
|
||||
$lat = 0.0;
|
||||
$lng = 0.0;
|
||||
|
||||
if (!empty($_POST)) {
|
||||
$nama = $conn->real_escape_string($_POST['nama'] ?? '');
|
||||
$kategori = $conn->real_escape_string($_POST['kategori_bantuan'] ?? 'Makan');
|
||||
$jumlah_jiwa = (int)($_POST['jumlah_jiwa'] ?? 1);
|
||||
$lat = (float)($_POST['lat'] ?? 0);
|
||||
$lng = (float)($_POST['lng'] ?? 0);
|
||||
} else {
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
if ($data) {
|
||||
$nama = $conn->real_escape_string($data->nama ?? '');
|
||||
$kategori = $conn->real_escape_string($data->kategori_bantuan ?? 'Makan');
|
||||
$jumlah_jiwa = (int)($data->jumlah_jiwa ?? 1);
|
||||
$lat = (float)($data->lat ?? 0);
|
||||
$lng = (float)($data->lng ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Validasi pengelola: hanya boleh tambah jika lokasi berada dalam radius ibadah yang dikelolanya
|
||||
$currentUser = getCurrentUser();
|
||||
if ($currentUser && $currentUser['role'] === 'pengelola') {
|
||||
$ibadah_id = $currentUser['ibadah_id'];
|
||||
if (!$ibadah_id) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Akun pengelola tidak terhubung ke rumah ibadah manapun.']);
|
||||
exit;
|
||||
}
|
||||
// Ambil data ibadah (lat, lng, radius)
|
||||
$res = $conn->query("SELECT lat, lng, radius FROM rumah_ibadah WHERE id=$ibadah_id");
|
||||
if (!$res || $res->num_rows === 0) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Rumah ibadah yang dikelola tidak ditemukan.']);
|
||||
exit;
|
||||
}
|
||||
$ib = $res->fetch_assoc();
|
||||
// Hitung jarak menggunakan Haversine formula
|
||||
$R = 6371000; // meter
|
||||
$lat1 = deg2rad((float)$ib['lat']);
|
||||
$lat2 = deg2rad($lat);
|
||||
$dLat = $lat2 - $lat1;
|
||||
$dLng = deg2rad($lng - (float)$ib['lng']);
|
||||
$a = sin($dLat/2)*sin($dLat/2) + cos($lat1)*cos($lat2)*sin($dLng/2)*sin($dLng/2);
|
||||
$dist = $R * 2 * atan2(sqrt($a), sqrt(1-$a));
|
||||
if ($dist > (float)$ib['radius']) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Lokasi penduduk di luar radius rumah ibadah Anda. Hanya boleh menambah penduduk dalam radius yang dikelola.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
if ($lat != 0 && $lng != 0) {
|
||||
$upload_dir = "../../uploads/";
|
||||
if (!file_exists($upload_dir)) {
|
||||
mkdir($upload_dir, 0777, true);
|
||||
}
|
||||
|
||||
$foto_rumah = null;
|
||||
$foto_kk = null;
|
||||
|
||||
function uploadFile($fileKey, $upload_dir) {
|
||||
if (isset($_FILES[$fileKey]) && $_FILES[$fileKey]['error'] === UPLOAD_ERR_OK) {
|
||||
$fileTmpPath = $_FILES[$fileKey]['tmp_name'];
|
||||
$fileName = $_FILES[$fileKey]['name'];
|
||||
|
||||
$fileNameCmps = explode(".", $fileName);
|
||||
$fileExtension = strtolower(end($fileNameCmps));
|
||||
|
||||
$allowedfileExtensions = array('jpg', 'jpeg', 'png', 'gif');
|
||||
if (in_array($fileExtension, $allowedfileExtensions)) {
|
||||
$newFileName = uniqid() . '_' . preg_replace('/[^a-zA-Z0-9\._-]/', '_', $fileName);
|
||||
$dest_path = $upload_dir . $newFileName;
|
||||
|
||||
if (move_uploaded_file($fileTmpPath, $dest_path)) {
|
||||
return $newFileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
$foto_rumah = uploadFile('foto_rumah', $upload_dir);
|
||||
$foto_kk = uploadFile('foto_kk', $upload_dir);
|
||||
|
||||
$val_foto_rumah = $foto_rumah ? "'$foto_rumah'" : "NULL";
|
||||
$val_foto_kk = $foto_kk ? "'$foto_kk'" : "NULL";
|
||||
|
||||
$query = "INSERT INTO penduduk_miskin (nama, kategori_bantuan, jumlah_jiwa, lat, lng, foto_rumah, foto_kk)
|
||||
VALUES ('$nama', '$kategori', $jumlah_jiwa, $lat, $lng, $val_foto_rumah, $val_foto_kk)";
|
||||
|
||||
if ($conn->query($query)) {
|
||||
echo json_encode(["status" => "success", "id" => $conn->insert_id, "message" => "Berhasil ditambahkan."]);
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "Gagal: " . $conn->error]);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "Data tidak lengkap."]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
require_once '../auth_helper.php';
|
||||
requireAdminOrPengelola();
|
||||
include_once '../db_connect.php';
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if(!empty($data->id)) {
|
||||
$id = (int)$data->id;
|
||||
|
||||
// Validasi pengelola: hanya boleh hapus penduduk dalam radius ibadah yang dikelolanya
|
||||
$currentUser = getCurrentUser();
|
||||
if ($currentUser && $currentUser['role'] === 'pengelola') {
|
||||
$ibadah_id = $currentUser['ibadah_id'];
|
||||
if (!$ibadah_id) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Akun pengelola tidak terhubung ke rumah ibadah manapun.']);
|
||||
exit;
|
||||
}
|
||||
// Ambil koordinat penduduk
|
||||
$mRes = $conn->query("SELECT lat, lng FROM penduduk_miskin WHERE id=$id");
|
||||
if (!$mRes || $mRes->num_rows === 0) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Data penduduk tidak ditemukan.']);
|
||||
exit;
|
||||
}
|
||||
$mRow = $mRes->fetch_assoc();
|
||||
$pLat = (float)$mRow['lat'];
|
||||
$pLng = (float)$mRow['lng'];
|
||||
|
||||
// Ambil data ibadah
|
||||
$iRes = $conn->query("SELECT lat, lng, radius FROM rumah_ibadah WHERE id=$ibadah_id");
|
||||
if (!$iRes || $iRes->num_rows === 0) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Rumah ibadah yang dikelola tidak ditemukan.']);
|
||||
exit;
|
||||
}
|
||||
$ib = $iRes->fetch_assoc();
|
||||
|
||||
// Hitung jarak Haversine
|
||||
$R = 6371000;
|
||||
$lat1 = deg2rad((float)$ib['lat']);
|
||||
$lat2 = deg2rad($pLat);
|
||||
$dLat = $lat2 - $lat1;
|
||||
$dLng = deg2rad($pLng - (float)$ib['lng']);
|
||||
$a = sin($dLat/2)*sin($dLat/2) + cos($lat1)*cos($lat2)*sin($dLng/2)*sin($dLng/2);
|
||||
$dist = $R * 2 * atan2(sqrt($a), sqrt(1-$a));
|
||||
if ($dist > (float)$ib['radius']) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Penduduk ini berada di luar radius rumah ibadah Anda. Tidak diizinkan menghapus.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$query = "DELETE FROM penduduk_miskin WHERE id=$id";
|
||||
|
||||
if($conn->query($query)) {
|
||||
echo json_encode(["status" => "success", "message" => "Berhasil dihapus."]);
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "Gagal hapus: " . $conn->error]);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "ID tidak diberikan."]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
include_once '../db_connect.php';
|
||||
|
||||
$query = "SELECT * FROM penduduk_miskin";
|
||||
$result = $conn->query($query);
|
||||
|
||||
$arr = array();
|
||||
|
||||
if($result->num_rows > 0) {
|
||||
while($row = $result->fetch_assoc()) {
|
||||
$item = array(
|
||||
"id" => $row['id'],
|
||||
"nama" => $row['nama'],
|
||||
"kategori_bantuan" => $row['kategori_bantuan'],
|
||||
"jumlah_jiwa" => isset($row['jumlah_jiwa']) ? (int)$row['jumlah_jiwa'] : 1,
|
||||
"lat" => (float)$row['lat'],
|
||||
"lng" => (float)$row['lng'],
|
||||
"foto_rumah" => $row['foto_rumah'],
|
||||
"foto_kk" => $row['foto_kk']
|
||||
);
|
||||
array_push($arr, $item);
|
||||
}
|
||||
echo json_encode(["status" => "success", "data" => $arr]);
|
||||
} else {
|
||||
echo json_encode(["status" => "success", "data" => []]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
require_once '../auth_helper.php';
|
||||
requireAdminOrPengelola();
|
||||
include_once '../db_connect.php';
|
||||
|
||||
$id = 0;
|
||||
$nama = '';
|
||||
$kategori = 'Makan';
|
||||
$jumlah_jiwa = 1;
|
||||
$lat = 0.0;
|
||||
$lng = 0.0;
|
||||
|
||||
if (!empty($_POST)) {
|
||||
$id = (int)($_POST['id'] ?? 0);
|
||||
$nama = $conn->real_escape_string($_POST['nama'] ?? '');
|
||||
$kategori = $conn->real_escape_string($_POST['kategori_bantuan'] ?? 'Makan');
|
||||
$jumlah_jiwa = (int)($_POST['jumlah_jiwa'] ?? 1);
|
||||
$lat = (float)($_POST['lat'] ?? 0);
|
||||
$lng = (float)($_POST['lng'] ?? 0);
|
||||
} else {
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
if ($data) {
|
||||
$id = (int)($data->id ?? 0);
|
||||
$nama = $conn->real_escape_string($data->nama ?? '');
|
||||
$kategori = $conn->real_escape_string($data->kategori_bantuan ?? 'Makan');
|
||||
$jumlah_jiwa = (int)($data->jumlah_jiwa ?? 1);
|
||||
$lat = (float)($data->lat ?? 0);
|
||||
$lng = (float)($data->lng ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Validasi pengelola: hanya boleh edit penduduk yang lokasinya dalam radius ibadah yang dikelolanya
|
||||
$currentUser = getCurrentUser();
|
||||
if ($currentUser && $currentUser['role'] === 'pengelola') {
|
||||
$ibadah_id = $currentUser['ibadah_id'];
|
||||
if (!$ibadah_id) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Akun pengelola tidak terhubung ke rumah ibadah manapun.']);
|
||||
exit;
|
||||
}
|
||||
$res = $conn->query("SELECT lat, lng, radius FROM rumah_ibadah WHERE id=$ibadah_id");
|
||||
if (!$res || $res->num_rows === 0) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Rumah ibadah yang dikelola tidak ditemukan.']);
|
||||
exit;
|
||||
}
|
||||
$ib = $res->fetch_assoc();
|
||||
// Gunakan koordinat penduduk yang sedang diedit (dari DB jika lat/lng 0, atau dari input)
|
||||
$checkLat = $lat;
|
||||
$checkLng = $lng;
|
||||
if ($checkLat == 0 && $checkLng == 0 && $id > 0) {
|
||||
$mRes = $conn->query("SELECT lat, lng FROM penduduk_miskin WHERE id=$id");
|
||||
if ($mRes && $mRow = $mRes->fetch_assoc()) {
|
||||
$checkLat = (float)$mRow['lat'];
|
||||
$checkLng = (float)$mRow['lng'];
|
||||
}
|
||||
}
|
||||
$R = 6371000;
|
||||
$lat1 = deg2rad((float)$ib['lat']);
|
||||
$lat2 = deg2rad($checkLat);
|
||||
$dLat = $lat2 - $lat1;
|
||||
$dLng = deg2rad($checkLng - (float)$ib['lng']);
|
||||
$a = sin($dLat/2)*sin($dLat/2) + cos($lat1)*cos($lat2)*sin($dLng/2)*sin($dLng/2);
|
||||
$dist = $R * 2 * atan2(sqrt($a), sqrt(1-$a));
|
||||
if ($dist > (float)$ib['radius']) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Penduduk ini berada di luar radius rumah ibadah Anda. Tidak diizinkan mengedit.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ($id > 0) {
|
||||
// Get current files to delete them if updated
|
||||
$old_foto_rumah = null;
|
||||
$old_foto_kk = null;
|
||||
$res = $conn->query("SELECT foto_rumah, foto_kk FROM penduduk_miskin WHERE id=$id");
|
||||
if ($res && $row = $res->fetch_assoc()) {
|
||||
$old_foto_rumah = $row['foto_rumah'];
|
||||
$old_foto_kk = $row['foto_kk'];
|
||||
}
|
||||
|
||||
$upload_dir = "../../uploads/";
|
||||
if (!file_exists($upload_dir)) {
|
||||
mkdir($upload_dir, 0777, true);
|
||||
}
|
||||
|
||||
$foto_rumah = null;
|
||||
$foto_kk = null;
|
||||
|
||||
function uploadFile($fileKey, $upload_dir) {
|
||||
if (isset($_FILES[$fileKey]) && $_FILES[$fileKey]['error'] === UPLOAD_ERR_OK) {
|
||||
$fileTmpPath = $_FILES[$fileKey]['tmp_name'];
|
||||
$fileName = $_FILES[$fileKey]['name'];
|
||||
|
||||
$fileNameCmps = explode(".", $fileName);
|
||||
$fileExtension = strtolower(end($fileNameCmps));
|
||||
|
||||
$allowedfileExtensions = array('jpg', 'jpeg', 'png', 'gif');
|
||||
if (in_array($fileExtension, $allowedfileExtensions)) {
|
||||
$newFileName = uniqid() . '_' . preg_replace('/[^a-zA-Z0-9\._-]/', '_', $fileName);
|
||||
$dest_path = $upload_dir . $newFileName;
|
||||
|
||||
if (move_uploaded_file($fileTmpPath, $dest_path)) {
|
||||
return $newFileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
$foto_rumah = uploadFile('foto_rumah', $upload_dir);
|
||||
$foto_kk = uploadFile('foto_kk', $upload_dir);
|
||||
|
||||
// Build update fields
|
||||
$update_fields = [
|
||||
"nama='$nama'",
|
||||
"kategori_bantuan='$kategori'",
|
||||
"jumlah_jiwa=$jumlah_jiwa",
|
||||
"lat=$lat",
|
||||
"lng=$lng"
|
||||
];
|
||||
|
||||
if ($foto_rumah !== null) {
|
||||
$update_fields[] = "foto_rumah='$foto_rumah'";
|
||||
if ($old_foto_rumah && file_exists($upload_dir . $old_foto_rumah)) {
|
||||
unlink($upload_dir . $old_foto_rumah);
|
||||
}
|
||||
}
|
||||
if ($foto_kk !== null) {
|
||||
$update_fields[] = "foto_kk='$foto_kk'";
|
||||
if ($old_foto_kk && file_exists($upload_dir . $old_foto_kk)) {
|
||||
unlink($upload_dir . $old_foto_kk);
|
||||
}
|
||||
}
|
||||
|
||||
$query = "UPDATE penduduk_miskin SET " . implode(", ", $update_fields) . " WHERE id=$id";
|
||||
|
||||
if ($conn->query($query)) {
|
||||
echo json_encode(["status" => "success", "message" => "Update berhasil."]);
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "Gagal update: " . $conn->error]);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "ID tidak diberikan."]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
require_once '../auth_helper.php';
|
||||
requireAdmin();
|
||||
include_once '../db_connect.php';
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if (!empty($data->lat) && !empty($data->lng)) {
|
||||
$nama = $conn->real_escape_string($data->nama ?? '');
|
||||
$jenis = $conn->real_escape_string($data->jenis ?? 'Masjid');
|
||||
$alamat = $conn->real_escape_string($data->alamat ?? '');
|
||||
$radius = (float)($data->radius ?? 500);
|
||||
$lat = (float)$data->lat;
|
||||
$lng = (float)$data->lng;
|
||||
|
||||
$query = "INSERT INTO rumah_ibadah (nama, jenis, alamat, radius, lat, lng)
|
||||
VALUES ('$nama', '$jenis', '$alamat', $radius, $lat, $lng)";
|
||||
|
||||
if ($conn->query($query)) {
|
||||
echo json_encode(["status" => "success", "id" => $conn->insert_id, "message" => "Rumah Ibadah berhasil ditambahkan."]);
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "Gagal menambahkan: " . $conn->error]);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "Data tidak lengkap."]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
require_once '../auth_helper.php';
|
||||
requireAdmin();
|
||||
include_once '../db_connect.php';
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if(!empty($data->id)) {
|
||||
$id = (int)$data->id;
|
||||
|
||||
$query = "DELETE FROM rumah_ibadah WHERE id=$id";
|
||||
|
||||
if($conn->query($query)) {
|
||||
echo json_encode(["status" => "success", "message" => "Berhasil dihapus."]);
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "Gagal hapus: " . $conn->error]);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "ID tidak diberikan."]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
include_once '../db_connect.php';
|
||||
|
||||
$query = "SELECT * FROM rumah_ibadah";
|
||||
$result = $conn->query($query);
|
||||
|
||||
$arr = array();
|
||||
|
||||
if($result->num_rows > 0) {
|
||||
while($row = $result->fetch_assoc()) {
|
||||
$item = array(
|
||||
"id" => $row['id'],
|
||||
"nama" => $row['nama'],
|
||||
"jenis" => $row['jenis'],
|
||||
"alamat" => $row['alamat'],
|
||||
"radius" => (float)$row['radius'],
|
||||
"lat" => (float)$row['lat'],
|
||||
"lng" => (float)$row['lng']
|
||||
);
|
||||
array_push($arr, $item);
|
||||
}
|
||||
echo json_encode(["status" => "success", "data" => $arr]);
|
||||
} else {
|
||||
echo json_encode(["status" => "success", "data" => []]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
header("Content-Type: application/json; charset=UTF-8");
|
||||
require_once '../auth_helper.php';
|
||||
requireAdmin();
|
||||
include_once '../db_connect.php';
|
||||
|
||||
$data = json_decode(file_get_contents("php://input"));
|
||||
|
||||
if (!empty($data->id)) {
|
||||
$id = (int)$data->id;
|
||||
$nama = $conn->real_escape_string($data->nama ?? '');
|
||||
$jenis = $conn->real_escape_string($data->jenis ?? 'Masjid');
|
||||
$alamat = $conn->real_escape_string($data->alamat ?? '');
|
||||
$radius = (float)($data->radius ?? 500);
|
||||
$lat = (float)$data->lat;
|
||||
$lng = (float)$data->lng;
|
||||
|
||||
$query = "UPDATE rumah_ibadah
|
||||
SET nama='$nama', jenis='$jenis', alamat='$alamat', radius=$radius, lat=$lat, lng=$lng
|
||||
WHERE id=$id";
|
||||
|
||||
if ($conn->query($query)) {
|
||||
echo json_encode(["status" => "success", "message" => "Update berhasil."]);
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "Gagal update: " . $conn->error]);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "ID tidak diberikan."]);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
require_once '../auth_helper.php';
|
||||
requireAdmin();
|
||||
require_once '../db_connect.php';
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$username = trim($data['username'] ?? '');
|
||||
$password = trim($data['password'] ?? '');
|
||||
$role = trim($data['role'] ?? 'pengelola');
|
||||
$ibadah_id = !empty($data['ibadah_id']) ? intval($data['ibadah_id']) : null;
|
||||
|
||||
if (empty($username) || empty($password)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Username dan password wajib diisi']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($role !== 'admin' && $role !== 'pengelola') {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Role tidak valid']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Cek apakah username sudah ada
|
||||
$stmt = $conn->prepare("SELECT id FROM users WHERE username = ?");
|
||||
$stmt->bind_param('s', $username);
|
||||
$stmt->execute();
|
||||
if ($stmt->get_result()->num_rows > 0) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Username sudah digunakan']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Hash password
|
||||
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
|
||||
|
||||
// Insert user baru
|
||||
$stmt = $conn->prepare("INSERT INTO users (username, password, role, ibadah_id) VALUES (?, ?, ?, ?)");
|
||||
$stmt->bind_param('sssi', $username, $hashed_password, $role, $ibadah_id);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
echo json_encode(['status' => 'success', 'message' => 'User berhasil ditambahkan', 'id' => $stmt->insert_id]);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Gagal menambahkan user: ' . $conn->error]);
|
||||
}
|
||||
|
||||
$conn->close();
|
||||
?>
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
require_once '../auth_helper.php';
|
||||
requireAdmin();
|
||||
require_once '../db_connect.php';
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
$id = intval($data['id'] ?? 0);
|
||||
|
||||
if (!$id) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'ID tidak valid']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($id === intval($_SESSION['user_id'])) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Anda tidak bisa menghapus akun Anda sendiri']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = $conn->prepare("DELETE FROM users WHERE id = ?");
|
||||
$stmt->bind_param('i', $id);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
echo json_encode(['status' => 'success', 'message' => 'User berhasil dihapus']);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Gagal menghapus user: ' . $conn->error]);
|
||||
}
|
||||
|
||||
$conn->close();
|
||||
?>
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
require_once '../auth_helper.php';
|
||||
requireAdmin();
|
||||
require_once '../db_connect.php';
|
||||
|
||||
$sql = "SELECT u.id, u.username, u.role, u.ibadah_id, ri.nama as nama_ibadah
|
||||
FROM users u
|
||||
LEFT JOIN rumah_ibadah ri ON u.ibadah_id = ri.id
|
||||
ORDER BY u.id DESC";
|
||||
|
||||
$result = $conn->query($sql);
|
||||
$arr = array();
|
||||
|
||||
if ($result && $result->num_rows > 0) {
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$item = array(
|
||||
"id" => intval($row['id']),
|
||||
"username" => $row['username'],
|
||||
"role" => $row['role'],
|
||||
"ibadah_id" => $row['ibadah_id'] ? intval($row['ibadah_id']) : null,
|
||||
"nama_ibadah" => $row['nama_ibadah'] ?? '-'
|
||||
);
|
||||
array_push($arr, $item);
|
||||
}
|
||||
echo json_encode(["status" => "success", "data" => $arr]);
|
||||
} else {
|
||||
echo json_encode(["status" => "success", "data" => []]);
|
||||
}
|
||||
$conn->close();
|
||||
?>
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
require_once '../auth_helper.php';
|
||||
requireAdmin();
|
||||
require_once '../db_connect.php';
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$id = intval($data['id'] ?? 0);
|
||||
$username = trim($data['username'] ?? '');
|
||||
$password = trim($data['password'] ?? '');
|
||||
$role = trim($data['role'] ?? 'pengelola');
|
||||
$ibadah_id = !empty($data['ibadah_id']) ? intval($data['ibadah_id']) : null;
|
||||
|
||||
if (!$id || empty($username)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Data tidak lengkap']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($role !== 'admin' && $role !== 'pengelola') {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Role tidak valid']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Cek apakah username sudah ada di user lain
|
||||
$stmt = $conn->prepare("SELECT id FROM users WHERE username = ? AND id != ?");
|
||||
$stmt->bind_param('si', $username, $id);
|
||||
$stmt->execute();
|
||||
if ($stmt->get_result()->num_rows > 0) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Username sudah digunakan oleh user lain']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (!empty($password)) {
|
||||
// Hash new password
|
||||
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
|
||||
$stmt = $conn->prepare("UPDATE users SET username = ?, password = ?, role = ?, ibadah_id = ? WHERE id = ?");
|
||||
$stmt->bind_param('ssssi', $username, $hashed_password, $role, $ibadah_id, $id);
|
||||
} else {
|
||||
// Keep existing password
|
||||
$stmt = $conn->prepare("UPDATE users SET username = ?, role = ?, ibadah_id = ? WHERE id = ?");
|
||||
$stmt->bind_param('sssi', $username, $role, $ibadah_id, $id);
|
||||
}
|
||||
|
||||
if ($stmt->execute()) {
|
||||
echo json_encode(['status' => 'success', 'message' => 'User berhasil diperbarui']);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Gagal memperbarui user: ' . $conn->error]);
|
||||
}
|
||||
|
||||
$conn->close();
|
||||
?>
|
||||
Reference in New Issue
Block a user