80 lines
3.5 KiB
PHP
80 lines
3.5 KiB
PHP
<?php
|
|
// ── UPDATE SPBU ───────────────────────────────────────────────────────────────
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: POST, PUT, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
if ($method !== 'POST' && $method !== 'PUT') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'message' => 'Method not allowed']);
|
|
exit();
|
|
}
|
|
|
|
// ── BACA JSON BODY ─────────────────────────────────────────────────────────────
|
|
$body = file_get_contents('php://input');
|
|
$data = json_decode($body, true);
|
|
|
|
if (!$data) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'message' => 'Data tidak valid (JSON parse error)']);
|
|
exit();
|
|
}
|
|
|
|
// ── VALIDASI ──────────────────────────────────────────────────────────────────
|
|
$id = intval($data['id'] ?? 0);
|
|
$nama_spbu = trim($data['nama_spbu'] ?? '');
|
|
$nomor_spbu = trim($data['nomor_spbu'] ?? '');
|
|
$status = trim($data['status'] ?? '');
|
|
$latitude = floatval($data['latitude'] ?? 0);
|
|
$longitude = floatval($data['longitude'] ?? 0);
|
|
|
|
$valid_status = ['Buka 24 Jam', 'Tidak Buka 24 Jam'];
|
|
|
|
if ($id <= 0) { echo json_encode(['success' => false, 'message' => 'ID tidak valid']); exit(); }
|
|
if (empty($nama_spbu)) { echo json_encode(['success' => false, 'message' => 'Nama SPBU tidak boleh kosong']); exit(); }
|
|
if (empty($nomor_spbu)) { echo json_encode(['success' => false, 'message' => 'Nomor SPBU tidak boleh kosong']); exit(); }
|
|
if (!in_array($status, $valid_status)) { echo json_encode(['success' => false, 'message' => 'Status tidak valid']); exit(); }
|
|
if ($latitude === 0.0 && $longitude === 0.0) { echo json_encode(['success' => false, 'message' => 'Koordinat tidak valid']); exit(); }
|
|
|
|
// ── UPDATE DATA ───────────────────────────────────────────────────────────────
|
|
try {
|
|
$stmt = $pdo->prepare("
|
|
UPDATE spbu
|
|
SET nama_spbu = :nama_spbu,
|
|
nomor_spbu = :nomor_spbu,
|
|
status = :status,
|
|
latitude = :latitude,
|
|
longitude = :longitude
|
|
WHERE id = :id
|
|
");
|
|
|
|
$stmt->execute([
|
|
':id' => $id,
|
|
':nama_spbu' => $nama_spbu,
|
|
':nomor_spbu' => $nomor_spbu,
|
|
':status' => $status,
|
|
':latitude' => $latitude,
|
|
':longitude' => $longitude,
|
|
]);
|
|
|
|
if ($stmt->rowCount() === 0) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'message' => 'Data tidak ditemukan atau tidak ada perubahan']);
|
|
} else {
|
|
echo json_encode(['success' => true, 'message' => 'Data SPBU berhasil diperbarui']);
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'message' => 'Gagal memperbarui data: ' . $e->getMessage()]);
|
|
}
|