Deploy WebGIS 01-05 dengan Docker
This commit is contained in:
+163
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
/**
|
||||
* tambah.php
|
||||
* Menangani tambah data SPBU, polyline, dan polygon.
|
||||
* Menerima POST dengan parameter 'tipe' untuk membedakan jenis data.
|
||||
*/
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Hanya terima method POST
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Method tidak diizinkan.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
require_once 'koneksi.php';
|
||||
|
||||
$tipe = isset($_POST['tipe']) ? trim($_POST['tipe']) : '';
|
||||
|
||||
// ─── TAMBAH SPBU ────────────────────────────────────────────────────────────
|
||||
if ($tipe === 'spbu') {
|
||||
$nama_spbu = isset($_POST['nama_spbu']) ? trim($_POST['nama_spbu']) : '';
|
||||
$status_ramai = isset($_POST['status_ramai']) ? trim($_POST['status_ramai']) : '';
|
||||
$buka_24_jam = isset($_POST['buka_24_jam']) ? trim($_POST['buka_24_jam']) : '';
|
||||
$status_operasional = isset($_POST['status_operasional']) ? trim($_POST['status_operasional']) : '';
|
||||
$nomor_hp = isset($_POST['nomor_hp']) ? trim($_POST['nomor_hp']) : '';
|
||||
$latitude = isset($_POST['latitude']) ? floatval($_POST['latitude']) : null;
|
||||
$longitude = isset($_POST['longitude']) ? floatval($_POST['longitude']) : null;
|
||||
|
||||
// Validasi server-side
|
||||
if (empty($nama_spbu) || empty($status_ramai) || empty($buka_24_jam) || empty($status_operasional) || $latitude === null || $longitude === null) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Data tidak lengkap.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$valid_ramai = ['ramai', 'tidak_ramai'];
|
||||
$valid_24 = ['ya', 'tidak'];
|
||||
$valid_ops = ['buka', 'tutup'];
|
||||
|
||||
if (!in_array($status_ramai, $valid_ramai) || !in_array($buka_24_jam, $valid_24) || !in_array($status_operasional, $valid_ops)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Nilai enum tidak valid.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = $koneksi->prepare(
|
||||
"INSERT INTO spbu (nama_spbu, status_ramai, buka_24_jam, status_operasional, nomor_hp, latitude, longitude)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||
);
|
||||
$stmt->bind_param('sssssdd', $nama_spbu, $status_ramai, $buka_24_jam, $status_operasional, $nomor_hp, $latitude, $longitude);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
$id = $koneksi->insert_id;
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'message' => 'SPBU berhasil ditambahkan.',
|
||||
'id' => $id,
|
||||
'data' => [
|
||||
'id' => $id,
|
||||
'nama_spbu' => $nama_spbu,
|
||||
'status_ramai' => $status_ramai,
|
||||
'buka_24_jam' => $buka_24_jam,
|
||||
'status_operasional' => $status_operasional,
|
||||
'nomor_hp' => $nomor_hp,
|
||||
'latitude' => $latitude,
|
||||
'longitude' => $longitude,
|
||||
]
|
||||
]);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Gagal menyimpan SPBU: ' . $stmt->error]);
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
// ─── TAMBAH POLYLINE ─────────────────────────────────────────────────────────
|
||||
} elseif ($tipe === 'polyline') {
|
||||
$nama_line = isset($_POST['nama_line']) ? trim($_POST['nama_line']) : '';
|
||||
$keterangan = isset($_POST['keterangan']) ? trim($_POST['keterangan']) : '';
|
||||
$warna = isset($_POST['warna']) ? trim($_POST['warna']) : '#3b82f6';
|
||||
$koordinat = isset($_POST['koordinat']) ? trim($_POST['koordinat']) : '';
|
||||
|
||||
if (empty($nama_line) || empty($koordinat)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Nama line dan koordinat wajib diisi.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Validasi JSON koordinat
|
||||
$decoded = json_decode($koordinat, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE || !is_array($decoded)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Format koordinat tidak valid.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = $koneksi->prepare(
|
||||
"INSERT INTO polyline (nama_line, keterangan, warna, koordinat) VALUES (?, ?, ?, ?)"
|
||||
);
|
||||
$stmt->bind_param('ssss', $nama_line, $keterangan, $warna, $koordinat);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
$id = $koneksi->insert_id;
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'message' => 'Polyline berhasil ditambahkan.',
|
||||
'id' => $id,
|
||||
'data' => [
|
||||
'id' => $id,
|
||||
'nama_line' => $nama_line,
|
||||
'keterangan' => $keterangan,
|
||||
'warna' => $warna,
|
||||
'koordinat' => $koordinat,
|
||||
]
|
||||
]);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Gagal menyimpan polyline: ' . $stmt->error]);
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
// ─── TAMBAH POLYGON ──────────────────────────────────────────────────────────
|
||||
} elseif ($tipe === 'polygon') {
|
||||
$nama_polygon = isset($_POST['nama_polygon']) ? trim($_POST['nama_polygon']) : '';
|
||||
$keterangan = isset($_POST['keterangan']) ? trim($_POST['keterangan']) : '';
|
||||
$warna = isset($_POST['warna']) ? trim($_POST['warna']) : '#22c55e';
|
||||
$koordinat = isset($_POST['koordinat']) ? trim($_POST['koordinat']) : '';
|
||||
|
||||
if (empty($nama_polygon) || empty($koordinat)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Nama polygon dan koordinat wajib diisi.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$decoded = json_decode($koordinat, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE || !is_array($decoded)) {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Format koordinat tidak valid.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = $koneksi->prepare(
|
||||
"INSERT INTO polygon (nama_polygon, keterangan, warna, koordinat) VALUES (?, ?, ?, ?)"
|
||||
);
|
||||
$stmt->bind_param('ssss', $nama_polygon, $keterangan, $warna, $koordinat);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
$id = $koneksi->insert_id;
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'message' => 'Polygon berhasil ditambahkan.',
|
||||
'id' => $id,
|
||||
'data' => [
|
||||
'id' => $id,
|
||||
'nama_polygon' => $nama_polygon,
|
||||
'keterangan' => $keterangan,
|
||||
'warna' => $warna,
|
||||
'koordinat' => $koordinat,
|
||||
]
|
||||
]);
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Gagal menyimpan polygon: ' . $stmt->error]);
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
} else {
|
||||
echo json_encode(['status' => 'error', 'message' => 'Tipe tidak dikenali.']);
|
||||
}
|
||||
|
||||
$koneksi->close();
|
||||
Reference in New Issue
Block a user