first commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
// ============================================================
|
||||
// config.php — Koneksi ke MySQL
|
||||
// Ganti nilai di bawah sesuai pengaturan XAMPP / phpMyAdmin
|
||||
// ============================================================
|
||||
|
||||
define('DB_HOST', 'localhost'); // biasanya localhost
|
||||
define('DB_NAME', 'gis_ptk'); // nama database (buat dulu di phpMyAdmin)
|
||||
define('DB_USER', 'root'); // username MySQL (default XAMPP: root)
|
||||
define('DB_PASS', ''); // password MySQL (default XAMPP: kosong)
|
||||
define('DB_CHARSET', 'utf8mb4');
|
||||
|
||||
// ── Buat koneksi PDO ──────────────────────────────────────
|
||||
function getDB(): PDO {
|
||||
static $pdo = null;
|
||||
if ($pdo === null) {
|
||||
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET;
|
||||
$options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
];
|
||||
try {
|
||||
$pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Koneksi database gagal: ' . $e->getMessage()]);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
return $pdo;
|
||||
}
|
||||
|
||||
// ── Header CORS & JSON (izinkan akses dari browser) ──────
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
// Handle preflight OPTIONS request
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(204);
|
||||
exit;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
// api/kavlings.php — GET / POST / DELETE kavling (polygon)
|
||||
require_once __DIR__ . '/config.php';
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
|
||||
if ($method === 'GET') {
|
||||
$rows = getDB()->query("SELECT * FROM kavlings ORDER BY created_at DESC")->fetchAll();
|
||||
foreach ($rows as &$r) $r['area_m2'] = (int)$r['area_m2'];
|
||||
echo json_encode(['success'=>true,'data'=>$rows]); exit;
|
||||
}
|
||||
|
||||
if ($method === 'POST') {
|
||||
$b = json_decode(file_get_contents('php://input'),true);
|
||||
$name = trim($b['name'] ?? '');
|
||||
$status = trim($b['status'] ?? 'SHM');
|
||||
$area = (int)($b['area_m2'] ?? 0);
|
||||
$coords = $b['coords'] ?? '[]';
|
||||
$allowed = ['SHM','HGB','HGU','HP'];
|
||||
if (!$name){ http_response_code(400); echo json_encode(['error'=>'Nama wajib diisi']); exit; }
|
||||
if (!in_array($status,$allowed)) $status='SHM';
|
||||
$pdo = getDB();
|
||||
$stmt = $pdo->prepare("INSERT INTO kavlings (name,status,area_m2,coords) VALUES (:n,:s,:a,:c)");
|
||||
$stmt->execute([':n'=>$name,':s'=>$status,':a'=>$area,':c'=>$coords]);
|
||||
$id = $pdo->lastInsertId();
|
||||
$row = $pdo->query("SELECT * FROM kavlings WHERE id=$id")->fetch();
|
||||
$row['area_m2']=(int)$row['area_m2'];
|
||||
http_response_code(201); echo json_encode(['success'=>true,'data'=>$row]); exit;
|
||||
}
|
||||
|
||||
if ($method === 'DELETE') {
|
||||
$id=(int)($_GET['id']??0);
|
||||
if($id<=0){http_response_code(400);echo json_encode(['error'=>'ID tidak valid']);exit;}
|
||||
$stmt=getDB()->prepare("DELETE FROM kavlings WHERE id=:id");
|
||||
$stmt->execute([':id'=>$id]);
|
||||
if($stmt->rowCount()===0){http_response_code(404);echo json_encode(['error'=>'Tidak ditemukan']);exit;}
|
||||
echo json_encode(['success'=>true,'message'=>'Kavling dihapus']); exit;
|
||||
}
|
||||
|
||||
http_response_code(405); echo json_encode(['error'=>'Method tidak diizinkan']);
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
// ============================================================
|
||||
// api/locations.php
|
||||
// GET → ambil semua lokasi
|
||||
// POST → simpan lokasi baru
|
||||
// PATCH ?id=N → update koordinat (pindah pin)
|
||||
// DELETE ?id=N → hapus lokasi
|
||||
// ============================================================
|
||||
|
||||
require_once __DIR__ . '/config.php';
|
||||
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
|
||||
// ── GET ───────────────────────────────────────────────────
|
||||
if ($method === 'GET') {
|
||||
$pdo = getDB();
|
||||
$rows = $pdo->query("SELECT * FROM locations ORDER BY created_at DESC")->fetchAll();
|
||||
foreach ($rows as &$row) {
|
||||
$row['lat'] = (float) $row['lat'];
|
||||
$row['lng'] = (float) $row['lng'];
|
||||
$row['is24h'] = (int) $row['is24h'];
|
||||
}
|
||||
echo json_encode(['success' => true, 'data' => $rows]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// ── POST: simpan baru ─────────────────────────────────────
|
||||
if ($method === 'POST') {
|
||||
$body = json_decode(file_get_contents('php://input'), true);
|
||||
$name = trim($body['name'] ?? '');
|
||||
$phone = trim($body['phone'] ?? '');
|
||||
$is24h = isset($body['is24h']) ? (int)(bool)$body['is24h'] : 0;
|
||||
$lat = isset($body['lat']) ? (float)$body['lat'] : null;
|
||||
$lng = isset($body['lng']) ? (float)$body['lng'] : null;
|
||||
|
||||
if ($name === '') { http_response_code(400); echo json_encode(['error'=>'Nama wajib diisi']); exit; }
|
||||
if ($lat === null || $lng === null) { http_response_code(400); echo json_encode(['error'=>'Koordinat wajib diisi']); exit; }
|
||||
|
||||
$pdo = getDB();
|
||||
$stmt = $pdo->prepare(
|
||||
"INSERT INTO locations (name, phone, is24h, lat, lng) VALUES (:name,:phone,:is24h,:lat,:lng)"
|
||||
);
|
||||
$stmt->execute([':name'=>$name, ':phone'=>$phone, ':is24h'=>$is24h, ':lat'=>$lat, ':lng'=>$lng]);
|
||||
|
||||
$newId = $pdo->lastInsertId();
|
||||
$newRow = $pdo->query("SELECT * FROM locations WHERE id = $newId")->fetch();
|
||||
$newRow['lat'] = (float)$newRow['lat'];
|
||||
$newRow['lng'] = (float)$newRow['lng'];
|
||||
$newRow['is24h'] = (int) $newRow['is24h'];
|
||||
|
||||
http_response_code(201);
|
||||
echo json_encode(['success' => true, 'data' => $newRow]);
|
||||
exit;
|
||||
}
|
||||
|
||||
// ── PATCH: update koordinat setelah pin dipindah ──────────
|
||||
if ($method === 'PATCH') {
|
||||
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
||||
$body = json_decode(file_get_contents('php://input'), true);
|
||||
$lat = isset($body['lat']) ? (float)$body['lat'] : null;
|
||||
$lng = isset($body['lng']) ? (float)$body['lng'] : null;
|
||||
|
||||
if ($id <= 0 || $lat === null || $lng === null) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'ID atau koordinat tidak valid']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$pdo = getDB();
|
||||
$stmt = $pdo->prepare("UPDATE locations SET lat=:lat, lng=:lng WHERE id=:id");
|
||||
$stmt->execute([':lat'=>$lat, ':lng'=>$lng, ':id'=>$id]);
|
||||
|
||||
echo json_encode(['success' => true, 'message' => 'Koordinat diperbarui']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// ── DELETE ────────────────────────────────────────────────
|
||||
if ($method === 'DELETE') {
|
||||
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
||||
if ($id <= 0) { http_response_code(400); echo json_encode(['error'=>'ID tidak valid']); exit; }
|
||||
|
||||
$pdo = getDB();
|
||||
$stmt = $pdo->prepare("DELETE FROM locations WHERE id = :id");
|
||||
$stmt->execute([':id' => $id]);
|
||||
|
||||
if ($stmt->rowCount() === 0) { http_response_code(404); echo json_encode(['error'=>'Data tidak ditemukan']); exit; }
|
||||
|
||||
echo json_encode(['success' => true, 'message' => 'Data dihapus']);
|
||||
exit;
|
||||
}
|
||||
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method tidak diizinkan']);
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
// api/roads.php — GET / POST / DELETE jalan (polyline)
|
||||
require_once __DIR__ . '/config.php';
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
|
||||
if ($method === 'GET') {
|
||||
$rows = getDB()->query("SELECT * FROM roads ORDER BY created_at DESC")->fetchAll();
|
||||
foreach ($rows as &$r) $r['length_m'] = (int)$r['length_m'];
|
||||
echo json_encode(['success'=>true,'data'=>$rows]); exit;
|
||||
}
|
||||
|
||||
if ($method === 'POST') {
|
||||
$b = json_decode(file_get_contents('php://input'),true);
|
||||
$name = trim($b['name'] ?? '');
|
||||
$status = trim($b['status'] ?? 'kabupaten');
|
||||
$len = (int)($b['length_m'] ?? 0);
|
||||
$coords = $b['coords'] ?? '[]';
|
||||
$allowed = ['nasional','provinsi','kabupaten'];
|
||||
if (!$name){ http_response_code(400); echo json_encode(['error'=>'Nama wajib diisi']); exit; }
|
||||
if (!in_array($status,$allowed)) $status='kabupaten';
|
||||
$pdo = getDB();
|
||||
$stmt = $pdo->prepare("INSERT INTO roads (name,status,length_m,coords) VALUES (:n,:s,:l,:c)");
|
||||
$stmt->execute([':n'=>$name,':s'=>$status,':l'=>$len,':c'=>$coords]);
|
||||
$id = $pdo->lastInsertId();
|
||||
$row = $pdo->query("SELECT * FROM roads WHERE id=$id")->fetch();
|
||||
$row['length_m']=(int)$row['length_m'];
|
||||
http_response_code(201); echo json_encode(['success'=>true,'data'=>$row]); exit;
|
||||
}
|
||||
|
||||
if ($method === 'DELETE') {
|
||||
$id=(int)($_GET['id']??0);
|
||||
if($id<=0){http_response_code(400);echo json_encode(['error'=>'ID tidak valid']);exit;}
|
||||
$stmt=getDB()->prepare("DELETE FROM roads WHERE id=:id");
|
||||
$stmt->execute([':id'=>$id]);
|
||||
if($stmt->rowCount()===0){http_response_code(404);echo json_encode(['error'=>'Tidak ditemukan']);exit;}
|
||||
echo json_encode(['success'=>true,'message'=>'Jalan dihapus']); exit;
|
||||
}
|
||||
|
||||
http_response_code(405); echo json_encode(['error'=>'Method tidak diizinkan']);
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
// ============================================================
|
||||
// api/search.php
|
||||
// GET ?q=keyword → cari di locations, roads, kavlings
|
||||
// ============================================================
|
||||
require_once __DIR__ . '/config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['error' => 'Method tidak diizinkan']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$q = trim($_GET['q'] ?? '');
|
||||
if ($q === '') {
|
||||
echo json_encode(['success' => true, 'data' => ['points'=>[],'roads'=>[],'kavlings'=>[]]]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$pdo = getDB();
|
||||
$like = '%' . $q . '%';
|
||||
|
||||
// Points
|
||||
$stmt = $pdo->prepare("SELECT * FROM locations WHERE name LIKE :q OR phone LIKE :q2 ORDER BY name LIMIT 10");
|
||||
$stmt->execute([':q' => $like, ':q2' => $like]);
|
||||
$points = $stmt->fetchAll();
|
||||
foreach ($points as &$r) { $r['lat']=(float)$r['lat']; $r['lng']=(float)$r['lng']; $r['is24h']=(int)$r['is24h']; }
|
||||
|
||||
// Roads
|
||||
$stmt = $pdo->prepare("SELECT id,name,status,length_m,created_at FROM roads WHERE name LIKE :q OR status LIKE :q2 ORDER BY name LIMIT 8");
|
||||
$stmt->execute([':q' => $like, ':q2' => $like]);
|
||||
$roads = $stmt->fetchAll();
|
||||
foreach ($roads as &$r) { $r['length_m']=(int)$r['length_m']; }
|
||||
|
||||
// Kavlings
|
||||
$stmt = $pdo->prepare("SELECT id,name,status,area_m2,created_at FROM kavlings WHERE name LIKE :q OR status LIKE :q2 ORDER BY name LIMIT 8");
|
||||
$stmt->execute([':q' => $like, ':q2' => $like]);
|
||||
$kavlings = $stmt->fetchAll();
|
||||
foreach ($kavlings as &$r) { $r['area_m2']=(int)$r['area_m2']; }
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'data' => ['points' => $points, 'roads' => $roads, 'kavlings' => $kavlings]
|
||||
]);
|
||||
Reference in New Issue
Block a user