88 lines
3.2 KiB
PHP
88 lines
3.2 KiB
PHP
<?php
|
|
// api/category_actions.php - Handle CRUD for categories
|
|
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_category') {
|
|
$id = $input['id'] ?? '';
|
|
$name = $input['name'] ?? '';
|
|
$agencyId = $input['agencyId'] ?? 'SUPER_ADMIN';
|
|
if(empty($id) || empty($name)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Missing category ID or name']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO categories (id, name, agency_id) VALUES (?, ?, ?)");
|
|
$stmt->execute([$id, $name, $agencyId]);
|
|
|
|
$actionUserId = $input['action_user_id'] ?? 'SYSTEM';
|
|
$actionUserName = $input['action_user_name'] ?? 'SYSTEM';
|
|
logActivity($pdo, $actionUserId, $actionUserName, 'ADD_CATEGORY', ['category_id' => $id, 'name' => $name]);
|
|
|
|
echo json_encode(['status' => 'success', 'message' => 'เพิ่มหมวดหมู่เรียบร้อยแล้ว']);
|
|
}
|
|
elseif ($action === 'edit_category') {
|
|
$id = $input['id'] ?? '';
|
|
$name = $input['name'] ?? '';
|
|
if(empty($id) || empty($name)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Missing category ID or name']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("UPDATE categories SET name = ? WHERE id = ?");
|
|
$stmt->execute([$name, $id]);
|
|
|
|
$actionUserId = $input['action_user_id'] ?? 'SYSTEM';
|
|
$actionUserName = $input['action_user_name'] ?? 'SYSTEM';
|
|
logActivity($pdo, $actionUserId, $actionUserName, 'EDIT_CATEGORY', ['category_id' => $id, 'name' => $name]);
|
|
|
|
echo json_encode(['status' => 'success', 'message' => 'แก้ไขหมวดหมู่เรียบร้อยแล้ว']);
|
|
}
|
|
elseif ($action === 'delete_category') {
|
|
$id = $input['id'] ?? '';
|
|
if(empty($id)) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Missing category ID']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM categories WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
|
|
$actionUserId = $input['action_user_id'] ?? 'SYSTEM';
|
|
$actionUserName = $input['action_user_name'] ?? 'SYSTEM';
|
|
logActivity($pdo, $actionUserId, $actionUserName, 'DELETE_CATEGORY', ['category_id' => $id]);
|
|
|
|
echo json_encode(['status' => 'success', 'message' => 'ลบหมวดหมู่เรียบร้อยแล้ว']);
|
|
}
|
|
else {
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
|
|
}
|
|
} catch (\Exception $e) {
|
|
echo json_encode(['status' => 'error', 'message' => 'Database Error: ' . $e->getMessage()]);
|
|
}
|
|
?>
|