245 lines
13 KiB
PHP
245 lines
13 KiB
PHP
<?php
|
|
require_once 'config.php';
|
|
requireLogin();
|
|
requireAdmin();
|
|
|
|
$action = $_GET['action'] ?? 'list';
|
|
$error = '';
|
|
$success = '';
|
|
|
|
// Handle form submissions
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = $_POST['username'] ?? '';
|
|
$name = $_POST['name'] ?? '';
|
|
$role = $_POST['role'] ?? 'student';
|
|
$line_token = $_POST['line_token'] ?? null;
|
|
$telegram_chat_id = $_POST['telegram_chat_id'] ?? null;
|
|
|
|
if (isset($_POST['add'])) {
|
|
$password = $_POST['password'] ?? '';
|
|
if (empty($username) || empty($password) || empty($name)) {
|
|
$error = 'กรุณากรอกข้อมูลที่จำเป็นให้ครบถ้วน';
|
|
} else {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
try {
|
|
$stmt = $pdo->prepare("INSERT INTO users (username, password, role, name, line_token, telegram_chat_id) VALUES (?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([$username, $hashed_password, $role, $name, $line_token, $telegram_chat_id]);
|
|
$new_id = $pdo->lastInsertId();
|
|
logActivity($pdo, 'CREATE_USER', "Created user ID: $new_id, Username: $username, Role: $role");
|
|
$success = 'เพิ่มผู้ใช้สำเร็จ';
|
|
$action = 'list';
|
|
} catch (PDOException $e) {
|
|
if ($e->getCode() == 23000) {
|
|
$error = 'ชื่อผู้ใช้นี้มีอยู่ในระบบแล้ว';
|
|
} else {
|
|
$error = 'เกิดข้อผิดพลาด: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
} elseif (isset($_POST['edit'])) {
|
|
$id = $_POST['id'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
try {
|
|
if (!empty($password)) {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = $pdo->prepare("UPDATE users SET name=?, role=?, line_token=?, telegram_chat_id=?, password=? WHERE id=?");
|
|
$stmt->execute([$name, $role, $line_token, $telegram_chat_id, $hashed_password, $id]);
|
|
logActivity($pdo, 'UPDATE_USER', "Updated user ID: $id (including password change)");
|
|
} else {
|
|
$stmt = $pdo->prepare("UPDATE users SET name=?, role=?, line_token=?, telegram_chat_id=? WHERE id=?");
|
|
$stmt->execute([$name, $role, $line_token, $telegram_chat_id, $id]);
|
|
logActivity($pdo, 'UPDATE_USER', "Updated user ID: $id (without password change)");
|
|
}
|
|
$success = 'แก้ไขข้อมูลผู้ใช้สำเร็จ';
|
|
$action = 'list';
|
|
} catch (PDOException $e) {
|
|
$error = 'เกิดข้อผิดพลาด: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle delete
|
|
if (isset($_GET['delete'])) {
|
|
$id = $_GET['delete'];
|
|
|
|
// Prevent deleting self
|
|
if ($id == $_SESSION['user_id']) {
|
|
$error = 'ไม่สามารถลบบัญชีของตัวเองได้';
|
|
} else {
|
|
$stmt_info = $pdo->prepare("SELECT username FROM users WHERE id=?");
|
|
$stmt_info->execute([$id]);
|
|
$user_info = $stmt_info->fetch();
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM users WHERE id=?");
|
|
if ($stmt->execute([$id])) {
|
|
$username = $user_info ? $user_info['username'] : 'Unknown';
|
|
logActivity($pdo, 'DELETE_USER', "Deleted user ID: $id, Username: $username");
|
|
$success = 'ลบผู้ใช้สำเร็จ';
|
|
}
|
|
}
|
|
}
|
|
|
|
include 'includes/header.php';
|
|
include 'includes/sidebar.php';
|
|
?>
|
|
|
|
<div class="mb-6 flex justify-between items-center">
|
|
<div>
|
|
<h2 class="text-2xl font-bold text-gray-800">จัดการผู้ใช้</h2>
|
|
<p class="text-gray-600 mt-1">เพิ่ม แก้ไข ลบข้อมูลนักเรียนและตั้งค่า Token</p>
|
|
</div>
|
|
<?php if ($action === 'list'): ?>
|
|
<a href="?action=add" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded shadow">
|
|
<i class="fas fa-plus mr-2"></i> เพิ่มผู้ใช้ใหม่
|
|
</a>
|
|
<?php else: ?>
|
|
<a href="users.php" class="bg-gray-500 hover:bg-gray-600 text-white font-bold py-2 px-4 rounded shadow">
|
|
<i class="fas fa-arrow-left mr-2"></i> กลับ
|
|
</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-6 rounded shadow-sm">
|
|
<p><i class="fas fa-exclamation-circle mr-2"></i> <?= htmlspecialchars($error) ?></p>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success): ?>
|
|
<script>
|
|
Swal.fire({
|
|
icon: 'success',
|
|
title: 'สำเร็จ',
|
|
text: '<?= addslashes($success) ?>',
|
|
timer: 2000,
|
|
showConfirmButton: false
|
|
});
|
|
</script>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($action === 'list'): ?>
|
|
<div class="bg-white rounded-lg shadow-md p-6">
|
|
<table class="dataTable display w-full text-sm text-left text-gray-500">
|
|
<thead class="text-xs text-gray-700 uppercase bg-gray-50">
|
|
<tr>
|
|
<th class="text-center w-16">ลำดับ</th>
|
|
<th class="text-center">ชื่อ-สกุล</th>
|
|
<th class="text-center">ชื่อผู้ใช้</th>
|
|
<th class="text-center">สิทธิ์</th>
|
|
<th class="text-center">จัดการ</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
$stmt = $pdo->query("SELECT * FROM users ORDER BY name ASC");
|
|
$index = 1;
|
|
while ($row = $stmt->fetch()):
|
|
?>
|
|
<tr class="bg-white border-b hover:bg-gray-50">
|
|
<td class="text-center"><?= $index++ ?></td>
|
|
<td class="font-medium text-gray-900">
|
|
<div class="flex items-center">
|
|
<img class="w-8 h-8 rounded-full mr-2" src="https://ui-avatars.com/api/?name=<?= urlencode($row['name']) ?>&background=random" alt="Avatar">
|
|
<?= htmlspecialchars($row['name']) ?>
|
|
</div>
|
|
</td>
|
|
<td><?= htmlspecialchars($row['username']) ?></td>
|
|
<td class="text-center">
|
|
<?php if ($row['role'] === 'admin'): ?>
|
|
<span class="bg-purple-100 text-purple-800 text-xs font-medium px-2.5 py-0.5 rounded border border-purple-400">Admin</span>
|
|
<?php else: ?>
|
|
<span class="bg-blue-100 text-blue-800 text-xs font-medium px-2.5 py-0.5 rounded border border-blue-400">Student</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="text-center">
|
|
<a href="?action=edit&id=<?= $row['id'] ?>" class="text-white bg-yellow-400 hover:bg-yellow-500 font-medium rounded-lg text-sm px-3 py-1 mr-2"><i class="fas fa-edit"></i></a>
|
|
<?php if ($row['id'] != $_SESSION['user_id']): ?>
|
|
<button onclick="confirmDelete(<?= $row['id'] ?>)" class="text-white bg-red-600 hover:bg-red-700 font-medium rounded-lg text-sm px-3 py-1"><i class="fas fa-trash"></i></button>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endwhile; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<script>
|
|
function confirmDelete(id) {
|
|
Swal.fire({
|
|
title: 'ยืนยันการลบ?',
|
|
text: "งานที่เกี่ยวข้องกับผู้ใช้นี้จะถูกลบไปด้วย!",
|
|
icon: 'warning',
|
|
showCancelButton: true,
|
|
confirmButtonColor: '#d33',
|
|
cancelButtonColor: '#3085d6',
|
|
confirmButtonText: 'ใช่, ลบเลย!',
|
|
cancelButtonText: 'ยกเลิก'
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
window.location.href = `users.php?delete=${id}`;
|
|
}
|
|
})
|
|
}
|
|
</script>
|
|
|
|
<?php elseif ($action === 'add' || $action === 'edit'):
|
|
$editMode = ($action === 'edit' && isset($_GET['id']));
|
|
$item = null;
|
|
if ($editMode) {
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
|
|
$stmt->execute([$_GET['id']]);
|
|
$item = $stmt->fetch();
|
|
if (!$item) {
|
|
echo "<script>window.location.href='users.php';</script>";
|
|
exit;
|
|
}
|
|
}
|
|
?>
|
|
<div class="bg-white rounded-lg shadow-md p-6 max-w-2xl mx-auto">
|
|
<h3 class="text-xl font-bold mb-4 border-b pb-2"><?= $editMode ? 'แก้ไขข้อมูลผู้ใช้' : 'เพิ่มผู้ใช้ใหม่' ?></h3>
|
|
|
|
<form method="POST" action="users.php">
|
|
<?php if ($editMode): ?>
|
|
<input type="hidden" name="id" value="<?= $item['id'] ?>">
|
|
<?php endif; ?>
|
|
|
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-4">
|
|
<div>
|
|
<label class="block text-gray-700 text-sm font-bold mb-2" for="username">ชื่อผู้ใช้ (Username) <span class="text-red-500">*</span></label>
|
|
<input type="text" name="username" id="username" value="<?= $editMode ? htmlspecialchars($item['username']) : '' ?>" <?= $editMode ? 'readonly class="bg-gray-100 shadow appearance-none border rounded w-full py-2 px-3 text-gray-500 leading-tight focus:outline-none"' : 'class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:ring-2 focus:ring-blue-500" required' ?>>
|
|
</div>
|
|
<div>
|
|
<label class="block text-gray-700 text-sm font-bold mb-2" for="password"><?= $editMode ? 'รหัสผ่าน (เว้นว่างถ้าไม่ต้องการเปลี่ยน)' : 'รหัสผ่าน <span class="text-red-500">*</span>' ?></label>
|
|
<input type="password" name="password" id="password" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:ring-2 focus:ring-blue-500" <?= $editMode ? '' : 'required' ?>>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label class="block text-gray-700 text-sm font-bold mb-2" for="name">ชื่อ-นามสกุล <span class="text-red-500">*</span></label>
|
|
<input type="text" name="name" id="name" value="<?= $editMode ? htmlspecialchars($item['name']) : '' ?>" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:ring-2 focus:ring-blue-500" required>
|
|
</div>
|
|
|
|
<div class="mb-6">
|
|
<label class="block text-gray-700 text-sm font-bold mb-2" for="role">สิทธิ์การใช้งาน <span class="text-red-500">*</span></label>
|
|
<select name="role" id="role" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:ring-2 focus:ring-blue-500" required>
|
|
<option value="student" <?= ($editMode && $item['role'] === 'student') ? 'selected' : '' ?>>Student (นักเรียน)</option>
|
|
<option value="admin" <?= ($editMode && $item['role'] === 'admin') ? 'selected' : '' ?>>Admin (ผู้ดูแลระบบ)</option>
|
|
</select>
|
|
</div>
|
|
|
|
<h4 class="font-bold text-gray-700 mb-2 mt-4 border-b pb-1"><i class="fas fa-bell text-yellow-500 mr-2"></i> ตั้งค่าการแจ้งเตือน</h4>
|
|
<p class="text-sm text-gray-500 mb-4">* ระบบจะทำการแจ้งเตือนไปยังกลุ่ม Telegram ที่ตั้งค่าไว้ในหน้าตั้งค่าระบบโดยอัตโนมัติ</p>
|
|
|
|
<div class="flex items-center justify-end border-t pt-4 mt-6">
|
|
<a href="users.php" class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded mr-2">ยกเลิก</a>
|
|
<button type="submit" name="<?= $editMode ? 'edit' : 'add' ?>" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
|
|
<?= $editMode ? 'บันทึกการแก้ไข' : 'เพิ่มผู้ใช้' ?>
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|