140 lines
5.1 KiB
PHP
140 lines
5.1 KiB
PHP
<?php
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
require 'db.php';
|
|
|
|
$action = $_GET['action'] ?? '';
|
|
|
|
// ==========================================
|
|
// DEPARTMENTS
|
|
// ==========================================
|
|
if ($action == 'get_departments') {
|
|
$stmt = $pdo->query("SELECT * FROM departments ORDER BY name ASC");
|
|
echo json_encode(['status' => 'success', 'data' => $stmt->fetchAll(PDO::FETCH_ASSOC)]);
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'save_department') {
|
|
$id = $_POST['id'] ?? '';
|
|
$name = trim($_POST['name'] ?? '');
|
|
|
|
if(!$name) {
|
|
echo json_encode(['status' => 'error', 'message' => 'กรุณากรอกชื่อหน่วยงาน']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
if($id) {
|
|
$stmt = $pdo->prepare("UPDATE departments SET name = ? WHERE id = ?");
|
|
$stmt->execute([$name, $id]);
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO departments (name) VALUES (?)");
|
|
$stmt->execute([$name]);
|
|
}
|
|
echo json_encode(['status' => 'success']);
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['status' => 'error', 'message' => 'ชื่อหน่วยงานอาจซ้ำกัน หรือเกิดข้อผิดพลาด']);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'delete_department') {
|
|
$id = $_POST['id'] ?? '';
|
|
|
|
// Check if used
|
|
$check = $pdo->prepare("SELECT COUNT(*) FROM employees WHERE department_id = ?");
|
|
$check->execute([$id]);
|
|
if($check->fetchColumn() > 0) {
|
|
echo json_encode(['status' => 'error', 'message' => 'ไม่สามารถลบได้ เนื่องจากมีบุคลากรในหน่วยงานนี้']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM departments WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
echo json_encode(['status' => 'success']);
|
|
exit;
|
|
}
|
|
|
|
// ==========================================
|
|
// EMPLOYEES
|
|
// ==========================================
|
|
if ($action == 'get_employees') {
|
|
$stmt = $pdo->query("
|
|
SELECT e.*, d.name as department_name
|
|
FROM employees e
|
|
LEFT JOIN departments d ON e.department_id = d.id
|
|
");
|
|
$employees = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Format for D3 Org Chart (id and parentId must be string, parentId can be empty)
|
|
$formatted = array_map(function($emp) {
|
|
return [
|
|
'id' => (string)$emp['id'],
|
|
'parentId' => $emp['parent_id'] ? (string)$emp['parent_id'] : '',
|
|
'name' => $emp['name'],
|
|
'position' => $emp['position'],
|
|
'department_id' => $emp['department_id'],
|
|
'department' => $emp['department_name'],
|
|
'imageUrl' => $emp['image_url']
|
|
];
|
|
}, $employees);
|
|
|
|
echo json_encode(['status' => 'success', 'data' => $formatted]);
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'save_employee') {
|
|
$id = $_POST['id'] ?? '';
|
|
$name = trim($_POST['name'] ?? '');
|
|
$position = trim($_POST['position'] ?? '');
|
|
$department_id = $_POST['department_id'] ?? '';
|
|
$parent_id = !empty($_POST['parent_id']) ? $_POST['parent_id'] : null;
|
|
$image_url = trim($_POST['image_url'] ?? '');
|
|
|
|
if(!$name || !$position || !$department_id) {
|
|
echo json_encode(['status' => 'error', 'message' => 'ข้อมูลไม่ครบถ้วน']);
|
|
exit;
|
|
}
|
|
|
|
if(!$image_url) {
|
|
$image_url = "https://ui-avatars.com/api/?name=" . urlencode($name) . "&background=f8fafc&color=1e40af&size=150";
|
|
}
|
|
|
|
try {
|
|
if($id) {
|
|
// Cannot be parent of oneself
|
|
if ($id == $parent_id) {
|
|
echo json_encode(['status' => 'error', 'message' => 'ไม่สามารถตั้งตัวเองเป็นหัวหน้าตัวเองได้']);
|
|
exit;
|
|
}
|
|
$stmt = $pdo->prepare("UPDATE employees SET name=?, position=?, department_id=?, parent_id=?, image_url=? WHERE id=?");
|
|
$stmt->execute([$name, $position, $department_id, $parent_id, $image_url, $id]);
|
|
} else {
|
|
$stmt = $pdo->prepare("INSERT INTO employees (name, position, department_id, parent_id, image_url) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([$name, $position, $department_id, $parent_id, $image_url]);
|
|
}
|
|
echo json_encode(['status' => 'success']);
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['status' => 'error', 'message' => 'เกิดข้อผิดพลาด: ' . $e->getMessage()]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
if ($action == 'delete_employee') {
|
|
$id = $_POST['id'] ?? '';
|
|
|
|
$check = $pdo->prepare("SELECT COUNT(*) FROM employees WHERE parent_id = ?");
|
|
$check->execute([$id]);
|
|
if($check->fetchColumn() > 0) {
|
|
echo json_encode(['status' => 'error', 'message' => 'ไม่สามารถลบได้ เนื่องจากมีผู้ใต้บังคับบัญชา']);
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM employees WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
echo json_encode(['status' => 'success']);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
|
|
?>
|