Files
2026-09-16 23:20:08 +07:00

233 lines
11 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_meeting'])) {
$title = $_POST['title'];
$meeting_date = $_POST['meeting_date'];
$details = $_POST['details'];
$stmt = $pdo->prepare("INSERT INTO meeting_minutes (title, meeting_date, details) VALUES (?, ?, ?)");
$stmt->execute([$title, $meeting_date, $details]);
$meeting_id = $pdo->lastInsertId();
// 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 meeting_attachments (meeting_id, file_name, gdrive_file_id) VALUES (?, ?, ?)");
$stmt->execute([$meeting_id, $name, $gdrive_file_id]);
}
}
}
}
logActivity($pdo, 'CREATE_MEETING', "เพิ่มบันทึกการประชุม: $title");
header("Location: meetings.php");
exit;
}
if (isset($_POST['edit_meeting'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$meeting_date = $_POST['meeting_date'];
$details = $_POST['details'];
$stmt = $pdo->prepare("UPDATE meeting_minutes SET title = ?, meeting_date = ?, details = ? WHERE id = ?");
$stmt->execute([$title, $meeting_date, $details, $id]);
// 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 meeting_attachments (meeting_id, file_name, gdrive_file_id) VALUES (?, ?, ?)");
$stmt->execute([$id, $name, $gdrive_file_id]);
}
}
}
}
logActivity($pdo, 'UPDATE_MEETING', "แก้ไขบันทึกการประชุม: $title");
header("Location: meetings.php");
exit;
}
}
if ($action === 'delete_attachment' && isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = $pdo->prepare("SELECT * FROM meeting_attachments WHERE id = ?");
$stmt->execute([$id]);
$attachment = $stmt->fetch();
if ($attachment) {
deleteFileFromDrive($attachment['gdrive_file_id']);
$stmt = $pdo->prepare("DELETE FROM meeting_attachments WHERE id = ?");
$stmt->execute([$id]);
logActivity($pdo, 'DELETE_MEETING_FILE', "ลบไฟล์แนบการประชุม: " . $attachment['file_name']);
}
header("Location: meetings.php?action=edit&id=" . $attachment['meeting_id']);
exit;
}
if ($action === 'delete' && isset($_GET['id'])) {
$id = $_GET['id'];
// Get meeting info
$stmt = $pdo->prepare("SELECT title FROM meeting_minutes WHERE id = ?");
$stmt->execute([$id]);
$meeting = $stmt->fetch();
if ($meeting) {
// Delete all attachments from GDrive
$stmt = $pdo->prepare("SELECT * FROM meeting_attachments WHERE meeting_id = ?");
$stmt->execute([$id]);
$attachments = $stmt->fetchAll();
foreach ($attachments as $attachment) {
deleteFileFromDrive($attachment['gdrive_file_id']);
}
$stmt = $pdo->prepare("DELETE FROM meeting_minutes WHERE id = ?");
$stmt->execute([$id]);
logActivity($pdo, 'DELETE_MEETING', "ลบบันทึกการประชุม: " . $meeting['title']);
}
header("Location: meetings.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="meetings.php?action=add" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-md font-medium transition-colors">
+ เพิ่มบันทึกการประชุม
</a>
<?php else: ?>
<a href="meetings.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>
</tr>
</thead>
<tbody>
<?php
$stmt = $pdo->query("
SELECT m.*, COUNT(a.id) as file_count
FROM meeting_minutes m
LEFT JOIN meeting_attachments a ON m.id = a.meeting_id
GROUP BY m.id
ORDER BY m.meeting_date DESC
");
while ($row = $stmt->fetch()):
?>
<tr class="hover:bg-gray-50">
<td class="border-b py-3 px-4"><?= date('d/m/Y', strtotime($row['meeting_date'])) ?></td>
<td class="border-b py-3 px-4"><?= htmlspecialchars((string)$row['title']) ?></td>
<td class="border-b py-3 px-4"><?= $row['file_count'] ?> ไฟล์</td>
<td class="border-b py-3 px-4">
<a href="meetings.php?action=edit&id=<?= $row['id'] ?>" class="text-blue-600 hover:underline mr-3">แก้ไข</a>
<a href="meetings.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'):
$meeting = null;
$attachments = [];
if ($action === 'edit' && isset($_GET['id'])) {
$stmt = $pdo->prepare("SELECT * FROM meeting_minutes WHERE id = ?");
$stmt->execute([$_GET['id']]);
$meeting = $stmt->fetch();
$stmt = $pdo->prepare("SELECT * FROM meeting_attachments WHERE meeting_id = ?");
$stmt->execute([$_GET['id']]);
$attachments = $stmt->fetchAll();
}
?>
<div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
<form action="meetings.php" method="POST" enctype="multipart/form-data" id="meetingForm">
<?php if ($meeting): ?>
<input type="hidden" name="edit_meeting" value="1">
<input type="hidden" name="id" value="<?= $meeting['id'] ?>">
<?php else: ?>
<input type="hidden" name="add_meeting" value="1">
<?php endif; ?>
<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">วันที่ประชุม</label>
<input type="date" name="meeting_date" 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-blue-500" value="<?= $meeting ? $meeting['meeting_date'] : date('Y-m-d') ?>">
</div>
<div>
<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-blue-500" value="<?= $meeting ? htmlspecialchars((string)$meeting['title']) : '' ?>">
</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;"><?= $meeting ? $meeting['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-blue-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="meetings.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-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-6 rounded focus:outline-none transition-colors">
<?= $meeting ? 'บันทึกการแก้ไข' : 'เพิ่มข้อมูล' ?>
</button>
</div>
</form>
</div>
<script>
var quill = new Quill('#editor', {
theme: 'snow'
});
document.getElementById('meetingForm').onsubmit = function() {
document.getElementById('detailsInput').value = quill.root.innerHTML;
};
</script>
<?php endif; ?>
<?php require_once 'footer.php'; ?>