300 lines
9.9 KiB
PHP
300 lines
9.9 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
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();
|
|
}
|
|
|
|
// Database configuration
|
|
$host = 'sql208.infinityfree.com';
|
|
$dbname = 'if0_42170546_sig_db';
|
|
$username = 'if0_42170546';
|
|
$password = 'SABTU01012005';
|
|
|
|
try {
|
|
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['success' => false, 'message' => 'Database connection failed: ' . $e->getMessage()]);
|
|
exit();
|
|
}
|
|
|
|
// Get request parameters
|
|
$type = isset($_GET['type']) ? $_GET['type'] : '';
|
|
$action = isset($_GET['action']) ? $_GET['action'] : '';
|
|
$id = isset($_GET['id']) ? intval($_GET['id']) : null;
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
// Map type to table name
|
|
$tables = [
|
|
'spbu' => 'spbu',
|
|
'jalan' => 'jalan',
|
|
'parsil' => 'parsil',
|
|
'rusak' => 'jalan_rusak'
|
|
];
|
|
|
|
if (!isset($tables[$type])) {
|
|
echo json_encode(['success' => false, 'message' => 'Invalid type']);
|
|
exit();
|
|
}
|
|
|
|
$table = $tables[$type];
|
|
|
|
// Handle different actions
|
|
switch ($action) {
|
|
case 'getAll':
|
|
getAll($pdo, $table);
|
|
break;
|
|
case 'getOne':
|
|
getOne($pdo, $table, $id);
|
|
break;
|
|
case 'create':
|
|
if ($method === 'POST') {
|
|
create($pdo, $table, $type);
|
|
}
|
|
break;
|
|
case 'update':
|
|
if ($method === 'PUT') {
|
|
update($pdo, $table, $type, $id);
|
|
}
|
|
break;
|
|
case 'delete':
|
|
if ($method === 'DELETE') {
|
|
delete($pdo, $table, $id);
|
|
}
|
|
break;
|
|
case 'add': // Alias for create (SPBU)
|
|
if ($method === 'POST') {
|
|
create($pdo, $table, $type);
|
|
}
|
|
break;
|
|
default:
|
|
echo json_encode(['success' => false, 'message' => 'Invalid action']);
|
|
}
|
|
|
|
// ==================== FUNCTIONS ====================
|
|
|
|
function getAll($pdo, $table) {
|
|
try {
|
|
if ($table === 'jalan' || $table === 'parsil') {
|
|
$stmt = $pdo->query("SELECT * FROM $table ORDER BY id DESC");
|
|
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Decode JSON coordinates
|
|
foreach ($data as &$row) {
|
|
if (isset($row['coordinates'])) {
|
|
$row['coordinates'] = json_decode($row['coordinates'], true);
|
|
}
|
|
}
|
|
} else {
|
|
$stmt = $pdo->query("SELECT * FROM $table ORDER BY id DESC");
|
|
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'data' => $data]);
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
function getOne($pdo, $table, $id) {
|
|
try {
|
|
$stmt = $pdo->prepare("SELECT * FROM $table WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$data = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($data) {
|
|
if (isset($data['coordinates'])) {
|
|
$data['coordinates'] = json_decode($data['coordinates'], true);
|
|
}
|
|
echo json_encode(['success' => true, 'data' => $data]);
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Data not found']);
|
|
}
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
function create($pdo, $table, $type) {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input) {
|
|
// Check if it's form-data (for file upload)
|
|
if ($type === 'rusak' && isset($_POST['nama'])) {
|
|
createRusak($pdo);
|
|
return;
|
|
}
|
|
echo json_encode(['success' => false, 'message' => 'No input data']);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
switch ($type) {
|
|
case 'spbu':
|
|
$stmt = $pdo->prepare("INSERT INTO spbu (nama, no_spbu, tipe, latitude, longitude) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([
|
|
$input['nama'],
|
|
$input['no_spbu'],
|
|
$input['tipe'],
|
|
$input['latitude'],
|
|
$input['longitude']
|
|
]);
|
|
break;
|
|
|
|
case 'jalan':
|
|
$coordinates = json_encode($input['coordinates']);
|
|
$stmt = $pdo->prepare("INSERT INTO jalan (nama, status, panjang, coordinates) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([
|
|
$input['nama'],
|
|
$input['status'],
|
|
$input['panjang'],
|
|
$coordinates
|
|
]);
|
|
break;
|
|
|
|
case 'parsil':
|
|
$coordinates = json_encode($input['coordinates']);
|
|
$stmt = $pdo->prepare("INSERT INTO parsil (nama, status, luas, coordinates) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([
|
|
$input['nama'],
|
|
$input['status'],
|
|
$input['luas'],
|
|
$coordinates
|
|
]);
|
|
break;
|
|
}
|
|
|
|
$newId = $pdo->lastInsertId();
|
|
echo json_encode(['success' => true, 'message' => 'Data created successfully', 'id' => $newId]);
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
function createRusak($pdo) {
|
|
try {
|
|
$nama = $_POST['nama'];
|
|
$latitude = $_POST['latitude'];
|
|
$longitude = $_POST['longitude'];
|
|
$fotoPath = '';
|
|
|
|
// Handle file upload
|
|
if (isset($_FILES['foto']) && $_FILES['foto']['error'] === UPLOAD_ERR_OK) {
|
|
$uploadDir = 'uploads/rusak/';
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0777, true);
|
|
}
|
|
|
|
$fileExtension = pathinfo($_FILES['foto']['name'], PATHINFO_EXTENSION);
|
|
$fileName = 'rusak_' . time() . '_' . uniqid() . '.' . $fileExtension;
|
|
$uploadPath = $uploadDir . $fileName;
|
|
|
|
if (move_uploaded_file($_FILES['foto']['tmp_name'], $uploadPath)) {
|
|
$fotoPath = $uploadPath;
|
|
}
|
|
}
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO jalan_rusak (nama, latitude, longitude, foto) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$nama, $latitude, $longitude, $fotoPath]);
|
|
|
|
$newId = $pdo->lastInsertId();
|
|
echo json_encode(['success' => true, 'message' => 'Data created successfully', 'id' => $newId]);
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
function update($pdo, $table, $type, $id) {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input) {
|
|
echo json_encode(['success' => false, 'message' => 'No input data']);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
switch ($type) {
|
|
case 'spbu':
|
|
$stmt = $pdo->prepare("UPDATE spbu SET nama=?, no_spbu=?, tipe=?, latitude=?, longitude=? WHERE id=?");
|
|
$stmt->execute([
|
|
$input['nama'],
|
|
$input['no_spbu'],
|
|
$input['tipe'],
|
|
$input['latitude'],
|
|
$input['longitude'],
|
|
$id
|
|
]);
|
|
break;
|
|
|
|
case 'jalan':
|
|
$coordinates = json_encode($input['coordinates']);
|
|
$stmt = $pdo->prepare("UPDATE jalan SET nama=?, status=?, panjang=?, coordinates=? WHERE id=?");
|
|
$stmt->execute([
|
|
$input['nama'],
|
|
$input['status'],
|
|
$input['panjang'],
|
|
$coordinates,
|
|
$id
|
|
]);
|
|
break;
|
|
|
|
case 'parsil':
|
|
$coordinates = json_encode($input['coordinates']);
|
|
$stmt = $pdo->prepare("UPDATE parsil SET nama=?, status=?, luas=?, coordinates=? WHERE id=?");
|
|
$stmt->execute([
|
|
$input['nama'],
|
|
$input['status'],
|
|
$input['luas'],
|
|
$coordinates,
|
|
$id
|
|
]);
|
|
break;
|
|
|
|
case 'rusak':
|
|
$stmt = $pdo->prepare("UPDATE jalan_rusak SET nama=?, latitude=?, longitude=? WHERE id=?");
|
|
$stmt->execute([
|
|
$input['nama'],
|
|
$input['latitude'],
|
|
$input['longitude'],
|
|
$id
|
|
]);
|
|
break;
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'message' => 'Data updated successfully']);
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
function delete($pdo, $table, $id) {
|
|
try {
|
|
// Delete associated file for rusak
|
|
if ($table === 'jalan_rusak') {
|
|
$stmt = $pdo->prepare("SELECT foto FROM jalan_rusak WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$data = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
if ($data && $data['foto'] && file_exists($data['foto'])) {
|
|
unlink($data['foto']);
|
|
}
|
|
}
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM $table WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
|
|
if ($stmt->rowCount() > 0) {
|
|
echo json_encode(['success' => true, 'message' => 'Data deleted successfully']);
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Data not found']);
|
|
}
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
|
}
|
|
}
|
|
?>
|