Files
gravity/P4Q/admin/competitions.php
2026-09-16 23:20:08 +07:00

278 lines
14 KiB
PHP

<?php
require_once 'header.php';
require_once '../gdrive.php';
$action = $_GET['action'] ?? 'list';
$folder_id = getSetting($pdo, 'google_drive_folder_id');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['add_competition'])) {
$title = $_POST['title'];
$year_th = $_POST['year_th'];
$rank_result = $_POST['rank_result'];
$details = $_POST['details'];
$authors_str = $_POST['authors']; // comma separated
$stmt = $pdo->prepare("INSERT INTO competitions (title, details, rank_result, year_th) VALUES (?, ?, ?, ?)");
$stmt->execute([$title, $details, $rank_result, $year_th]);
$competition_id = $pdo->lastInsertId();
// Handle authors
$authors = array_filter(array_map('trim', explode(',', $authors_str)));
foreach ($authors as $author) {
$stmt = $pdo->prepare("INSERT INTO competition_authors (competition_id, author_name) VALUES (?, ?)");
$stmt->execute([$competition_id, $author]);
}
// Handle file uploads
if (!empty($_FILES['attachments']['name'][0])) {
foreach ($_FILES['attachments']['name'] as $key => $name) {
if ($_FILES['attachments']['error'][$key] == 0) {
$tmp_name = $_FILES['attachments']['tmp_name'][$key];
$gdrive_file_id = uploadFileToDrive($tmp_name, $name, $folder_id);
if ($gdrive_file_id) {
$stmt = $pdo->prepare("INSERT INTO competition_attachments (competition_id, file_name, gdrive_file_id) VALUES (?, ?, ?)");
$stmt->execute([$competition_id, $name, $gdrive_file_id]);
}
}
}
}
logActivity($pdo, 'CREATE_COMPETITION', "เพิ่มผลงาน: $title");
header("Location: competitions.php");
exit;
}
if (isset($_POST['edit_competition'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$year_th = $_POST['year_th'];
$rank_result = $_POST['rank_result'];
$details = $_POST['details'];
$authors_str = $_POST['authors'];
$stmt = $pdo->prepare("UPDATE competitions SET title = ?, details = ?, rank_result = ?, year_th = ? WHERE id = ?");
$stmt->execute([$title, $details, $rank_result, $year_th, $id]);
// Update authors (delete all and re-insert)
$stmt = $pdo->prepare("DELETE FROM competition_authors WHERE competition_id = ?");
$stmt->execute([$id]);
$authors = array_filter(array_map('trim', explode(',', $authors_str)));
foreach ($authors as $author) {
$stmt = $pdo->prepare("INSERT INTO competition_authors (competition_id, author_name) VALUES (?, ?)");
$stmt->execute([$id, $author]);
}
// Handle new file uploads
if (!empty($_FILES['attachments']['name'][0])) {
foreach ($_FILES['attachments']['name'] as $key => $name) {
if ($_FILES['attachments']['error'][$key] == 0) {
$tmp_name = $_FILES['attachments']['tmp_name'][$key];
$gdrive_file_id = uploadFileToDrive($tmp_name, $name, $folder_id);
if ($gdrive_file_id) {
$stmt = $pdo->prepare("INSERT INTO competition_attachments (competition_id, file_name, gdrive_file_id) VALUES (?, ?, ?)");
$stmt->execute([$id, $name, $gdrive_file_id]);
}
}
}
}
logActivity($pdo, 'UPDATE_COMPETITION', "แก้ไขผลงาน: $title");
header("Location: competitions.php");
exit;
}
}
if ($action === 'delete_attachment' && isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM competition_attachments WHERE id = ?");
$stmt->execute([$id]);
$attachment = $stmt->fetch();
if ($attachment) {
deleteFileFromDrive($attachment['gdrive_file_id']);
$stmt = $pdo->prepare("DELETE FROM competition_attachments WHERE id = ?");
$stmt->execute([$id]);
logActivity($pdo, 'DELETE_COMPETITION_FILE', "ลบไฟล์แนบผลงาน: " . $attachment['file_name']);
}
header("Location: competitions.php?action=edit&id=" . $attachment['competition_id']);
exit;
}
if ($action === 'delete' && isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT title FROM competitions WHERE id = ?");
$stmt->execute([$id]);
$comp = $stmt->fetch();
if ($comp) {
// Delete all attachments from GDrive
$stmt = $pdo->prepare("SELECT * FROM competition_attachments WHERE competition_id = ?");
$stmt->execute([$id]);
$attachments = $stmt->fetchAll();
foreach ($attachments as $attachment) {
deleteFileFromDrive($attachment['gdrive_file_id']);
}
$stmt = $pdo->prepare("DELETE FROM competitions WHERE id = ?");
$stmt->execute([$id]);
logActivity($pdo, 'DELETE_COMPETITION', "ลบผลงาน: " . $comp['title']);
}
header("Location: competitions.php");
exit;
}
?>
<div class="mb-8 flex justify-between items-center">
<h1 class="text-3xl font-bold text-gray-900">จัดการผลงานประกวด</h1>
<?php if ($action === 'list'): ?>
<a href="competitions.php?action=add" class="bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-md font-medium transition-colors">
+ เพิ่มผลงานใหม่
</a>
<?php else: ?>
<a href="competitions.php" class="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded-md font-medium transition-colors">
กลับหน้ารายการ
</a>
<?php endif; ?>
</div>
<?php if ($action === 'list'): ?>
<div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
<table class="datatable w-full text-left border-collapse">
<thead>
<tr>
<th class="border-b py-3 px-4 bg-gray-50">ปี (พ.ศ.)</th>
<th class="border-b py-3 px-4 bg-gray-50">ชื่อผลงาน</th>
<th class="border-b py-3 px-4 bg-gray-50">รางวัล</th>
<th class="border-b py-3 px-4 bg-gray-50">ผู้จัดทำ</th>
<th class="border-b py-3 px-4 bg-gray-50">ไฟล์แนบ</th>
<th class="border-b py-3 px-4 bg-gray-50">จัดการ</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $pdo->query("
SELECT c.*,
(SELECT GROUP_CONCAT(author_name SEPARATOR ', ') FROM competition_authors WHERE competition_id = c.id) as authors,
(SELECT COUNT(*) FROM competition_attachments WHERE competition_id = c.id) as file_count
FROM competitions c
ORDER BY c.year_th DESC, c.id DESC
");
while ($row = $stmt->fetch()):
?>
<tr class="hover:bg-gray-50">
<td class="border-b py-3 px-4 text-center"><?= $row['year_th'] ?></td>
<td class="border-b py-3 px-4"><?= htmlspecialchars((string)$row['title']) ?></td>
<td class="border-b py-3 px-4">
<span class="inline-block px-2 py-1 bg-yellow-100 text-yellow-800 text-xs rounded-md">
<?= htmlspecialchars((string)$row['rank_result']) ?>
</span>
</td>
<td class="border-b py-3 px-4 text-sm"><?= htmlspecialchars((string)$row['authors']) ?></td>
<td class="border-b py-3 px-4 text-center"><?= $row['file_count'] ?></td>
<td class="border-b py-3 px-4">
<a href="competitions.php?action=edit&id=<?= $row['id'] ?>" class="text-blue-600 hover:underline mr-3">แก้ไข</a>
<a href="competitions.php?action=delete&id=<?= $row['id'] ?>" class="text-red-600 hover:underline" onclick="return confirm('ยืนยันการลบข้อมูลนี้? การลบจะลบไฟล์ใน Google Drive ด้วย')">ลบ</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
<?php elseif ($action === 'add' || $action === 'edit'):
$comp = null;
$attachments = [];
$authors_str = '';
if ($action === 'edit' && isset($_GET['id'])) {
$stmt = $pdo->prepare("SELECT * FROM competitions WHERE id = ?");
$stmt->execute([$_GET['id']]);
$comp = $stmt->fetch();
$stmt = $pdo->prepare("SELECT * FROM competition_attachments WHERE competition_id = ?");
$stmt->execute([$_GET['id']]);
$attachments = $stmt->fetchAll();
$stmt = $pdo->prepare("SELECT GROUP_CONCAT(author_name SEPARATOR ', ') FROM competition_authors WHERE competition_id = ?");
$stmt->execute([$_GET['id']]);
$authors_str = $stmt->fetchColumn();
}
?>
<div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
<form action="competitions.php" method="POST" enctype="multipart/form-data" id="compForm">
<?php if ($comp): ?>
<input type="hidden" name="edit_competition" value="1">
<input type="hidden" name="id" value="<?= $comp['id'] ?>">
<?php else: ?>
<input type="hidden" name="add_competition" value="1">
<?php endif; ?>
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
<div class="md:col-span-2">
<label class="block text-sm font-medium text-gray-700 mb-2">ชื่อผลงาน</label>
<input type="text" name="title" required class="appearance-none border border-gray-300 rounded-md w-full py-2 px-3 text-gray-700 focus:outline-none focus:ring-2 focus:ring-green-500" value="<?= $comp ? htmlspecialchars((string)$comp['title']) : '' ?>">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">ปี (พ.ศ.)</label>
<input type="number" name="year_th" required class="appearance-none border border-gray-300 rounded-md w-full py-2 px-3 text-gray-700 focus:outline-none focus:ring-2 focus:ring-green-500" value="<?= $comp ? $comp['year_th'] : (date('Y') + 543) ?>">
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">รางวัลที่ได้รับ (เช่น รางวัลที่ 1, ชมเชย)</label>
<input type="text" name="rank_result" required class="appearance-none border border-gray-300 rounded-md w-full py-2 px-3 text-gray-700 focus:outline-none focus:ring-2 focus:ring-green-500" value="<?= $comp ? htmlspecialchars((string)$comp['rank_result']) : '' ?>">
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">รายชื่อผู้จัดทำ (คั่นด้วยเครื่องหมายจุลภาค , )</label>
<input type="text" name="authors" class="appearance-none border border-gray-300 rounded-md w-full py-2 px-3 text-gray-700 focus:outline-none focus:ring-2 focus:ring-green-500" value="<?= htmlspecialchars((string)$authors_str) ?>" placeholder="นายเอ, นายบี, นางสาวซี">
</div>
</div>
<div class="mb-6">
<label class="block text-sm font-medium text-gray-700 mb-2">รายละเอียด</label>
<input type="hidden" name="details" id="detailsInput">
<div id="editor" style="height: 200px;"><?= $comp ? $comp['details'] : '' ?></div>
</div>
<div class="mb-6">
<label class="block text-sm font-medium text-gray-700 mb-2">อัปโหลดไฟล์แนบ (PDF)</label>
<input type="file" name="attachments[]" multiple accept=".pdf" class="appearance-none border border-gray-300 rounded-md w-full py-2 px-3 text-gray-700 focus:outline-none focus:ring-2 focus:ring-green-500">
<?php if (!empty($attachments)): ?>
<div class="mt-4">
<h4 class="text-sm font-medium text-gray-700 mb-2">ไฟล์แนบปัจจุบัน</h4>
<ul class="space-y-2">
<?php foreach ($attachments as $file): ?>
<li class="flex items-center justify-between bg-gray-50 p-3 rounded border">
<span class="text-gray-700"><?= htmlspecialchars((string)$file['file_name']) ?></span>
<a href="competitions.php?action=delete_attachment&id=<?= $file['id'] ?>" class="text-red-500 hover:text-red-700 text-sm" onclick="return confirm('ยืนยันการลบไฟล์นี้จากระบบและ Google Drive?')">ลบไฟล์</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>
<div>
<button type="submit" class="bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-6 rounded focus:outline-none transition-colors">
<?= $comp ? 'บันทึกการแก้ไข' : 'เพิ่มผลงาน' ?>
</button>
</div>
</form>
</div>
<script>
var quill = new Quill('#editor', {
theme: 'snow'
});
document.getElementById('compForm').onsubmit = function() {
document.getElementById('detailsInput').value = quill.root.innerHTML;
};
</script>
<?php endif; ?>
<?php require_once 'footer.php'; ?>