118 lines
5.0 KiB
PHP
118 lines
5.0 KiB
PHP
<?php
|
|
// api/agency_actions.php - Handle CRUD for agencies
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
require_once __DIR__ . '/../config/db.php';
|
|
require_once __DIR__ . '/logger.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid request method']);
|
|
exit;
|
|
}
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if (!$input) {
|
|
$input = $_POST;
|
|
}
|
|
|
|
$action = $input['action'] ?? '';
|
|
|
|
if (empty($action)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Missing action']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$dbInfo = getDbConnection();
|
|
$pdo = $dbInfo['pdo'];
|
|
|
|
if ($action === 'add_agency') {
|
|
$id = strtoupper(trim($input['id'] ?? ''));
|
|
$name = trim($input['name'] ?? '');
|
|
$code = strtoupper(trim($input['code'] ?? ''));
|
|
$password = trim($input['password'] ?? '1234');
|
|
$gdriveFolderId = trim($input['gdriveFolderId'] ?? '');
|
|
|
|
if(empty($id) || empty($name) || empty($code)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Missing required fields']);
|
|
exit;
|
|
}
|
|
|
|
// Check if ID exists
|
|
$checkStmt = $pdo->prepare("SELECT id FROM agencies WHERE id = ?");
|
|
$checkStmt->execute([$id]);
|
|
if ($checkStmt->fetch()) {
|
|
echo json_encode(['status' => 'error', 'message' => 'รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO agencies (id, name, code, admin_password, gdrive_folder_id) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([$id, $name, $code, $password, $gdriveFolderId ?: null]);
|
|
|
|
$actionUserId = $input['action_user_id'] ?? 'SUPER_ADMIN';
|
|
$actionUserName = $input['action_user_name'] ?? 'ผู้ดูแลระบบ';
|
|
logActivity($pdo, $actionUserId, $actionUserName, 'ADD_AGENCY', ['agency_id' => $id, 'name' => $name, 'code' => $code]);
|
|
|
|
echo json_encode(['status' => 'success', 'message' => 'เพิ่มหน่วยงานเรียบร้อยแล้ว']);
|
|
}
|
|
elseif ($action === 'edit_agency') {
|
|
$id = strtoupper(trim($input['id'] ?? ''));
|
|
$name = trim($input['name'] ?? '');
|
|
$code = strtoupper(trim($input['code'] ?? ''));
|
|
$password = trim($input['password'] ?? '');
|
|
$gdriveFolderId = trim($input['gdriveFolderId'] ?? '');
|
|
|
|
if(empty($id) || empty($name) || empty($code)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Missing required fields']);
|
|
exit;
|
|
}
|
|
|
|
if (empty($password)) {
|
|
$stmt = $pdo->prepare("UPDATE agencies SET name = ?, code = ?, gdrive_folder_id = ? WHERE id = ?");
|
|
$stmt->execute([$name, $code, $gdriveFolderId ?: null, $id]);
|
|
} else {
|
|
$stmt = $pdo->prepare("UPDATE agencies SET name = ?, code = ?, admin_password = ?, gdrive_folder_id = ? WHERE id = ?");
|
|
$stmt->execute([$name, $code, $password, $gdriveFolderId ?: null, $id]);
|
|
}
|
|
|
|
$actionUserId = $input['action_user_id'] ?? 'SUPER_ADMIN';
|
|
$actionUserName = $input['action_user_name'] ?? 'ผู้ดูแลระบบ';
|
|
logActivity($pdo, $actionUserId, $actionUserName, 'EDIT_AGENCY', ['agency_id' => $id, 'name' => $name, 'code' => $code]);
|
|
|
|
echo json_encode(['status' => 'success', 'message' => 'แก้ไขข้อมูลหน่วยงานเรียบร้อยแล้ว']);
|
|
}
|
|
elseif ($action === 'delete_agency') {
|
|
$id = strtoupper(trim($input['id'] ?? ''));
|
|
if(empty($id)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Missing agency ID']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM agencies WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
|
|
$actionUserId = $input['action_user_id'] ?? 'SUPER_ADMIN';
|
|
$actionUserName = $input['action_user_name'] ?? 'ผู้ดูแลระบบ';
|
|
logActivity($pdo, $actionUserId, $actionUserName, 'DELETE_AGENCY', ['agency_id' => $id]);
|
|
|
|
echo json_encode(['status' => 'success', 'message' => 'ลบหน่วยงานเรียบร้อยแล้ว']);
|
|
}
|
|
elseif ($action === 'reset_password') {
|
|
$id = strtoupper(trim($input['id'] ?? ''));
|
|
if(empty($id)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Missing agency ID']);
|
|
exit;
|
|
}
|
|
$stmt = $pdo->prepare("UPDATE agencies SET admin_password = '1234' WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
echo json_encode(['status' => 'success', 'message' => 'รีเซ็ตรหัสผ่านเป็น 1234 เรียบร้อยแล้ว']);
|
|
}
|
|
else {
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
|
|
}
|
|
} catch (\Exception $e) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Database Error: ' . $e->getMessage()]);
|
|
}
|
|
?>
|