Files
WebGis_UAS_Project/02/api_parsil.php
T
2026-06-10 19:23:50 +07:00

155 lines
5.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// ============================================================
// api_parsil.php REST API CRUD Data Parsil Tanah (Polygon)
// GET api_parsil.php → semua data parsil
// GET api_parsil.php?id=N → satu data parsil
// POST api_parsil.php → tambah parsil
// PUT api_parsil.php?id=N → update parsil
// DELETE api_parsil.php?id=N → hapus parsil
// ============================================================
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');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { exit(0); }
require_once 'config.php';
$method = $_SERVER['REQUEST_METHOD'];
$id = isset($_GET['id']) ? (int) $_GET['id'] : null;
// ── GET ──────────────────────────────────────────────────────
if ($method === 'GET') {
$conn = getConnection();
if ($id) {
$stmt = $conn->prepare("SELECT * FROM parsil_tanah WHERE id = ?");
$stmt->bind_param('i', $id);
$stmt->execute();
$row = $stmt->get_result()->fetch_assoc();
if ($row) {
$row['id'] = (int) $row['id'];
$row['luas_meter2'] = (float) $row['luas_meter2'];
$row['geojson'] = json_decode($row['geojson']);
echo json_encode(['status' => 'success', 'data' => $row]);
} else {
http_response_code(404);
echo json_encode(['status' => 'error', 'message' => 'Data tidak ditemukan']);
}
$stmt->close();
} else {
$res = $conn->query("SELECT * FROM parsil_tanah ORDER BY created_at DESC");
$data = [];
while ($row = $res->fetch_assoc()) {
$row['id'] = (int) $row['id'];
$row['luas_meter2'] = (float) $row['luas_meter2'];
$row['geojson'] = json_decode($row['geojson']);
$data[] = $row;
}
echo json_encode(['status' => 'success', 'data' => $data]);
}
$conn->close();
exit;
}
// ── POST (Tambah) ─────────────────────────────────────────────
if ($method === 'POST') {
$body = json_decode(file_get_contents('php://input'), true);
$nama = trim($body['nama_pemilik'] ?? '');
$no_sert = trim($body['no_sertifikat'] ?? '');
$status = $body['status_sertifikat'] ?? '';
$luas = (float) ($body['luas_meter2'] ?? 0);
$geojson = isset($body['geojson'])
? json_encode($body['geojson'])
: null;
$valid_status = ['SHM', 'HGB', 'HGU', 'HP'];
if (!$nama || !$no_sert || !in_array($status, $valid_status) || !$geojson || $luas <= 0) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Data tidak lengkap atau tidak valid']);
exit;
}
$conn = getConnection();
$stmt = $conn->prepare(
"INSERT INTO parsil_tanah (nama_pemilik, no_sertifikat, status_sertifikat, luas_meter2, geojson)
VALUES (?, ?, ?, ?, ?)"
);
$stmt->bind_param('sssds', $nama, $no_sert, $status, $luas, $geojson);
if ($stmt->execute()) {
echo json_encode([
'status' => 'success',
'message' => 'Data parsil berhasil ditambahkan',
'id' => $stmt->insert_id
]);
} else {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $stmt->error]);
}
$stmt->close();
$conn->close();
exit;
}
// ── PUT (Update) ──────────────────────────────────────────────
if ($method === 'PUT' && $id) {
$body = json_decode(file_get_contents('php://input'), true);
$nama = trim($body['nama_pemilik'] ?? '');
$no_sert = trim($body['no_sertifikat'] ?? '');
$status = $body['status_sertifikat'] ?? '';
$luas = (float) ($body['luas_meter2'] ?? 0);
$geojson = isset($body['geojson'])
? json_encode($body['geojson'])
: null;
$valid_status = ['SHM', 'HGB', 'HGU', 'HP'];
if (!$nama || !$no_sert || !in_array($status, $valid_status) || !$geojson || $luas <= 0) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Data tidak lengkap atau tidak valid']);
exit;
}
$conn = getConnection();
$stmt = $conn->prepare(
"UPDATE parsil_tanah
SET nama_pemilik=?, no_sertifikat=?, status_sertifikat=?, luas_meter2=?, geojson=?
WHERE id=?"
);
$stmt->bind_param('sssdsi', $nama, $no_sert, $status, $luas, $geojson, $id);
if ($stmt->execute()) {
echo json_encode(['status' => 'success', 'message' => 'Data parsil berhasil diperbarui']);
} else {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $stmt->error]);
}
$stmt->close();
$conn->close();
exit;
}
// ── DELETE ────────────────────────────────────────────────────
if ($method === 'DELETE' && $id) {
$conn = getConnection();
$stmt = $conn->prepare("DELETE FROM parsil_tanah WHERE id=?");
$stmt->bind_param('i', $id);
if ($stmt->execute()) {
echo json_encode(['status' => 'success', 'message' => 'Data parsil berhasil dihapus']);
} else {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => $stmt->error]);
}
$stmt->close();
$conn->close();
exit;
}
http_response_code(405);
echo json_encode(['status' => 'error', 'message' => 'Method tidak diizinkan']);