First commit
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
<?php
|
||||
/**
|
||||
* WebGIS - API Manajemen Parsil Tanah / Kavling
|
||||
*
|
||||
* Endpoint : /api/parsil.php
|
||||
* Tabel : tb_parsil
|
||||
* Geometri : GeoJSON Polygon
|
||||
*/
|
||||
|
||||
// Matikan display_errors agar PHP tidak output HTML error (merusak JSON)
|
||||
ini_set('display_errors', 0);
|
||||
ini_set('display_startup_errors', 0);
|
||||
error_reporting(0);
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
/* Handle preflight request */
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit;
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/db.php';
|
||||
|
||||
$conn = getConnection();
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
$id = isset($_GET['id']) ? (int)$_GET['id'] : null;
|
||||
|
||||
try {
|
||||
|
||||
switch ($method) {
|
||||
|
||||
/* ============================================================
|
||||
READ — Ambil semua atau satu data parsil
|
||||
============================================================ */
|
||||
case 'GET':
|
||||
if ($id) {
|
||||
/* Satu data */
|
||||
$stmt = $conn->prepare(
|
||||
"SELECT id, nama_parsil, status_kepemilikan, luas_tanah, geojson, created_at
|
||||
FROM tb_parsil WHERE id = ?"
|
||||
);
|
||||
$stmt->bind_param('i', $id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
$row = $result->fetch_assoc();
|
||||
|
||||
if ($row) {
|
||||
$row['geojson'] = json_decode($row['geojson']);
|
||||
echo json_encode($row);
|
||||
} else {
|
||||
http_response_code(404);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'Data parsil tidak ditemukan']);
|
||||
}
|
||||
|
||||
} else {
|
||||
/* Semua data */
|
||||
$filter = isset($_GET['status']) ? $_GET['status'] : null;
|
||||
|
||||
if ($filter) {
|
||||
$stmt = $conn->prepare(
|
||||
"SELECT id, nama_parsil, status_kepemilikan, luas_tanah, geojson, created_at
|
||||
FROM tb_parsil WHERE status_kepemilikan = ? ORDER BY created_at DESC"
|
||||
);
|
||||
$stmt->bind_param('s', $filter);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
} else {
|
||||
$result = $conn->query(
|
||||
"SELECT id, nama_parsil, status_kepemilikan, luas_tanah, geojson, created_at
|
||||
FROM tb_parsil ORDER BY created_at DESC"
|
||||
);
|
||||
}
|
||||
|
||||
$data = [];
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$row['geojson'] = json_decode($row['geojson']);
|
||||
$data[] = $row;
|
||||
}
|
||||
echo json_encode($data);
|
||||
}
|
||||
break;
|
||||
|
||||
/* ============================================================
|
||||
CREATE — Simpan data parsil baru
|
||||
Body (JSON):
|
||||
{
|
||||
"nama_parsil" : "Kavling A-01",
|
||||
"status_kepemilikan" : "SHM", // SHM|HGB|HGU|HP
|
||||
"luas_tanah" : 1250.5000, // m², dihitung LeafletJS
|
||||
"geojson" : { // GeoJSON Polygon
|
||||
"type": "Polygon",
|
||||
"coordinates": [[[lng,lat], ...]]
|
||||
}
|
||||
}
|
||||
============================================================ */
|
||||
case 'POST':
|
||||
$body = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!$body) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'Body request tidak valid atau bukan JSON']);
|
||||
break;
|
||||
}
|
||||
|
||||
$nama_parsil = trim($body['nama_parsil'] ?? '');
|
||||
$status_kepemilikan = trim($body['status_kepemilikan'] ?? '');
|
||||
$luas_tanah = (float)($body['luas_tanah'] ?? 0);
|
||||
$geojson_raw = $body['geojson'] ?? null;
|
||||
|
||||
/* Validasi */
|
||||
$valid_status = ['SHM', 'HGB', 'HGU', 'HP'];
|
||||
if (!$nama_parsil) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'nama_parsil tidak boleh kosong']);
|
||||
break;
|
||||
}
|
||||
if (!in_array($status_kepemilikan, $valid_status)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'status_kepemilikan tidak valid. Pilih: SHM, HGB, HGU, atau HP']);
|
||||
break;
|
||||
}
|
||||
if (!$geojson_raw || ($geojson_raw['type'] ?? '') !== 'Polygon') {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'geojson harus bertipe Polygon']);
|
||||
break;
|
||||
}
|
||||
|
||||
$geojson_str = json_encode($geojson_raw);
|
||||
|
||||
$stmt = $conn->prepare(
|
||||
"INSERT INTO tb_parsil (nama_parsil, status_kepemilikan, luas_tanah, geojson)
|
||||
VALUES (?, ?, ?, ?)"
|
||||
);
|
||||
$stmt->bind_param('ssds', $nama_parsil, $status_kepemilikan, $luas_tanah, $geojson_str);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
echo json_encode([
|
||||
'status' => 'sukses',
|
||||
'id' => $conn->insert_id,
|
||||
'pesan' => 'Data parsil berhasil disimpan'
|
||||
]);
|
||||
} else {
|
||||
throw new Exception($stmt->error);
|
||||
}
|
||||
break;
|
||||
|
||||
/* ============================================================
|
||||
UPDATE — Perbarui atribut dan/atau koordinat parsil
|
||||
Mengirim field yang ingin diubah saja:
|
||||
- Edit atribut : { "nama_parsil": "...", "status_kepemilikan": "..." }
|
||||
- Geser vertex : { "geojson": {...}, "luas_tanah": 1234.5 }
|
||||
- Keduanya : { semua field }
|
||||
============================================================ */
|
||||
case 'PUT':
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'Parameter id diperlukan']);
|
||||
break;
|
||||
}
|
||||
|
||||
$body = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$body) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'Body tidak valid']);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Bangun query dinamis berdasarkan field yang dikirim */
|
||||
$setClauses = [];
|
||||
$paramVals = [];
|
||||
$paramTypes = '';
|
||||
|
||||
if (array_key_exists('nama_parsil', $body)) {
|
||||
$v = trim($body['nama_parsil']);
|
||||
if (!$v) { http_response_code(400); echo json_encode(['status'=>'gagal','pesan'=>'nama_parsil tidak boleh kosong']); break; }
|
||||
$setClauses[] = 'nama_parsil = ?'; $paramVals[] = $v; $paramTypes .= 's';
|
||||
}
|
||||
if (array_key_exists('status_kepemilikan', $body)) {
|
||||
$valid = ['SHM','HGB','HGU','HP'];
|
||||
if (!in_array($body['status_kepemilikan'], $valid)) {
|
||||
http_response_code(400); echo json_encode(['status'=>'gagal','pesan'=>'status_kepemilikan tidak valid']); break;
|
||||
}
|
||||
$setClauses[] = 'status_kepemilikan = ?'; $paramVals[] = $body['status_kepemilikan']; $paramTypes .= 's';
|
||||
}
|
||||
if (array_key_exists('luas_tanah', $body)) {
|
||||
$setClauses[] = 'luas_tanah = ?'; $paramVals[] = (float)$body['luas_tanah']; $paramTypes .= 'd';
|
||||
}
|
||||
if (array_key_exists('geojson', $body)) {
|
||||
$setClauses[] = 'geojson = ?'; $paramVals[] = json_encode($body['geojson']); $paramTypes .= 's';
|
||||
}
|
||||
|
||||
if (empty($setClauses)) {
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'Tidak ada field yang dikirim untuk diupdate']);
|
||||
break;
|
||||
}
|
||||
|
||||
$paramVals[] = $id;
|
||||
$paramTypes .= 'i';
|
||||
|
||||
$sql = 'UPDATE tb_parsil SET ' . implode(', ', $setClauses) . ' WHERE id = ?';
|
||||
$stmt = $conn->prepare($sql);
|
||||
$stmt->bind_param($paramTypes, ...$paramVals);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
echo json_encode(['status' => 'sukses', 'pesan' => 'Data parsil berhasil diperbarui']);
|
||||
} else {
|
||||
throw new Exception($stmt->error);
|
||||
}
|
||||
break;
|
||||
|
||||
/* ============================================================
|
||||
DELETE — Hapus data parsil
|
||||
============================================================ */
|
||||
case 'DELETE':
|
||||
if (!$id) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'Parameter id diperlukan']);
|
||||
break;
|
||||
}
|
||||
|
||||
$stmt = $conn->prepare("DELETE FROM tb_parsil WHERE id = ?");
|
||||
$stmt->bind_param('i', $id);
|
||||
|
||||
if ($stmt->execute() && $stmt->affected_rows > 0) {
|
||||
echo json_encode(['status' => 'sukses', 'pesan' => 'Data parsil berhasil dihapus']);
|
||||
} else {
|
||||
http_response_code(404);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'Data tidak ditemukan atau sudah dihapus']);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
http_response_code(405);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'Method tidak didukung']);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'gagal', 'pesan' => 'Server error: ' . $e->getMessage()]);
|
||||
} finally {
|
||||
if ($conn) $conn->close();
|
||||
}
|
||||
Reference in New Issue
Block a user