105 lines
3.8 KiB
PHP
105 lines
3.8 KiB
PHP
<?php
|
|
// api/maps.php
|
|
require_once dirname(__DIR__) . '/config/config.php';
|
|
require_once dirname(__DIR__) . '/models/HospitalMap.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Unauthorized']);
|
|
exit;
|
|
}
|
|
|
|
$action = $_REQUEST['action'] ?? '';
|
|
$mapModel = new HospitalMap();
|
|
|
|
if ($action === 'list') {
|
|
$status = $_GET['status'] ?? null;
|
|
$maps = $mapModel->getAll($status);
|
|
echo json_encode(['status' => 'success', 'data' => $maps]);
|
|
|
|
} elseif ($action === 'upload') {
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid method']);
|
|
exit;
|
|
}
|
|
|
|
$name = $_POST['name'] ?? '';
|
|
$building = $_POST['building'] ?? '';
|
|
$floor = $_POST['floor'] ?? '';
|
|
$location = $_POST['transport_center_location'] ?? '';
|
|
$status = $_POST['status'] ?? 'active';
|
|
|
|
if (empty($name) || empty($building) || empty($floor)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'กรุณากรอกข้อมูลให้ครบถ้วน']);
|
|
exit;
|
|
}
|
|
|
|
if (!isset($_FILES['map_image']) || $_FILES['map_image']['error'] !== UPLOAD_ERR_OK) {
|
|
echo json_encode(['status' => 'error', 'message' => 'กรุณาอัปโหลดไฟล์รูปภาพแผนผัง']);
|
|
exit;
|
|
}
|
|
|
|
// Process file upload
|
|
$file = $_FILES['map_image'];
|
|
$allowed_types = ['image/jpeg', 'image/png', 'image/webp'];
|
|
|
|
if (!in_array($file['type'], $allowed_types)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'รองรับเฉพาะไฟล์รูปภาพ (JPG, PNG, WEBP) เท่านั้น']);
|
|
exit;
|
|
}
|
|
|
|
$upload_dir = dirname(__DIR__) . '/public/assets/img/maps/';
|
|
if (!is_dir($upload_dir)) {
|
|
mkdir($upload_dir, 0755, true);
|
|
}
|
|
|
|
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
|
|
$filename = 'map_' . time() . '_' . rand(1000, 9999) . '.' . $ext;
|
|
$target_path = $upload_dir . $filename;
|
|
|
|
if (move_uploaded_file($file['tmp_name'], $target_path)) {
|
|
$db_path = 'public/assets/img/maps/' . $filename;
|
|
|
|
$data = [
|
|
'name' => $name,
|
|
'building' => $building,
|
|
'floor' => $floor,
|
|
'image_path' => $db_path,
|
|
'transport_center_location' => $location,
|
|
'status' => $status
|
|
];
|
|
|
|
if ($mapModel->create($data)) {
|
|
echo json_encode(['status' => 'success', 'message' => 'อัปโหลดแผนผังเรียบร้อยแล้ว']);
|
|
} else {
|
|
// Delete file if DB insert fails
|
|
unlink($target_path);
|
|
echo json_encode(['status' => 'error', 'message' => 'เกิดข้อผิดพลาดในการบันทึกข้อมูล']);
|
|
}
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'message' => 'ไม่สามารถอัปโหลดไฟล์ได้']);
|
|
}
|
|
|
|
} elseif ($action === 'delete') {
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid method']);
|
|
exit;
|
|
}
|
|
|
|
$id = $_POST['id'] ?? '';
|
|
if (empty($id)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'ID is required']);
|
|
exit;
|
|
}
|
|
|
|
if ($mapModel->delete($id)) {
|
|
echo json_encode(['status' => 'success', 'message' => 'ลบแผนผังเรียบร้อยแล้ว']);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'message' => 'ไม่สามารถลบแผนผังได้']);
|
|
}
|
|
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
|
|
}
|