106 lines
3.6 KiB
PHP
106 lines
3.6 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
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') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
$host = '127.0.0.1';
|
|
$dbname = 'sig_db';
|
|
$username = 'root';
|
|
$password = '';
|
|
|
|
try {
|
|
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['success' => false, 'message' => 'Database connection failed']);
|
|
exit();
|
|
}
|
|
|
|
$type = isset($_GET['type']) ? $_GET['type'] : '';
|
|
$action = isset($_GET['action']) ? $_GET['action'] : '';
|
|
$id = isset($_GET['id']) ? intval($_GET['id']) : null;
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
$validTypes = ['masjid', 'gereja', 'klenteng', 'vihara', 'pura'];
|
|
|
|
if (!in_array($type, $validTypes)) {
|
|
echo json_encode(['success' => false, 'message' => 'Invalid type']);
|
|
exit();
|
|
}
|
|
|
|
$table = 'tempat_ibadah';
|
|
|
|
switch ($action) {
|
|
case 'getAll':
|
|
$stmt = $pdo->prepare("SELECT * FROM $table WHERE jenis = ? ORDER BY nama ASC");
|
|
$stmt->execute([$type]);
|
|
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
echo json_encode(['success' => true, 'data' => $data]);
|
|
break;
|
|
|
|
case 'getOne':
|
|
$stmt = $pdo->prepare("SELECT * FROM $table WHERE id = ? AND jenis = ?");
|
|
$stmt->execute([$id, $type]);
|
|
$data = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
if ($data) {
|
|
echo json_encode(['success' => true, 'data' => $data]);
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Data tidak ditemukan']);
|
|
}
|
|
break;
|
|
|
|
case 'create':
|
|
if ($method === 'POST') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $pdo->prepare("INSERT INTO $table (jenis, nama, alamat, no_hp, latitude, longitude, radius, keterangan) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([
|
|
$type,
|
|
$input['nama'],
|
|
$input['alamat'] ?? '',
|
|
$input['no_hp'] ?? '',
|
|
$input['latitude'],
|
|
$input['longitude'],
|
|
$input['radius'] ?? 500,
|
|
$input['keterangan'] ?? ''
|
|
]);
|
|
echo json_encode(['success' => true, 'message' => 'Data berhasil disimpan', 'id' => $pdo->lastInsertId()]);
|
|
}
|
|
break;
|
|
|
|
case 'update':
|
|
if ($method === 'PUT') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$stmt = $pdo->prepare("UPDATE $table SET nama=?, alamat=?, no_hp=?, latitude=?, longitude=?, radius=?, keterangan=? WHERE id=? AND jenis=?");
|
|
$stmt->execute([
|
|
$input['nama'],
|
|
$input['alamat'] ?? '',
|
|
$input['no_hp'] ?? '',
|
|
$input['latitude'],
|
|
$input['longitude'],
|
|
$input['radius'] ?? 500,
|
|
$input['keterangan'] ?? '',
|
|
$id,
|
|
$type
|
|
]);
|
|
echo json_encode(['success' => true, 'message' => 'Data berhasil diupdate']);
|
|
}
|
|
break;
|
|
|
|
case 'delete':
|
|
if ($method === 'DELETE') {
|
|
$stmt = $pdo->prepare("DELETE FROM $table WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
echo json_encode(['success' => true, 'message' => 'Data berhasil dihapus']);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
echo json_encode(['success' => false, 'message' => 'Invalid action']);
|
|
}
|
|
?>
|