ค้นหากิจกรรม
ค้นหากิจกรรม
โปรดแก้ไขตามข้อเสนอแนะและส่งอนุมัติอีกครั้ง
query("SHOW COLUMNS FROM featured_activities LIKE 'activity_type_other'")->fetch(); if (!$col_check) { $pdo->exec("ALTER TABLE featured_activities ADD COLUMN activity_type_other VARCHAR(255) NULL AFTER activity_type"); } } catch (Exception $e) { // ignore } // เฉพาะ user_type_id = 4 (ตำบล) เท่านั้นที่สามารถเพิ่มกิจกรรม if ($action == 'add' && $user['user_type_id'] != '4') { header("Location: activities.php"); exit(); } // ลบกิจกรรม if ($action == 'delete' && isset($_GET['id'])) { $stmt = $pdo->prepare("SELECT * FROM featured_activities WHERE id = ?"); $stmt->execute([$_GET['id']]); $record = $stmt->fetch(); if ($record && canDeleteRecord($user, $record)) { $upload_dir = 'upload/'; // ลบรูปภาพโปสเตอร์ (ถ้าเป็นไฟล์ใน upload/) if (!empty($record['poster_url']) && strpos($record['poster_url'], 'http') !== 0) { $poster_path = $upload_dir . $record['poster_url']; if (file_exists($poster_path)) { unlink($poster_path); } } $stmt = $pdo->prepare("DELETE FROM featured_activities WHERE id = ?"); $stmt->execute([$_GET['id']]); $message = "ลบกิจกรรมเรียบร้อยแล้ว"; } else { $message = "ไม่มีสิทธิ์ลบกิจกรรม"; } $action = 'list'; } // อนุมัติกิจกรรม if ($action == 'approve' && isset($_GET['id'])) { $stmt = $pdo->prepare("SELECT * FROM featured_activities WHERE id = ?"); $stmt->execute([$_GET['id']]); $record = $stmt->fetch(); if ($record && canApproveRecord($pdo, $user, $record)) { $stmt = $pdo->prepare("UPDATE featured_activities SET status = 'approved', approved_by = ?, approved_date = NOW() WHERE id = ?"); $stmt->execute([$user['username'], $_GET['id']]); $message = "อนุมัติกิจกรรมเรียบร้อยแล้ว"; } else { $message = "ไม่มีสิทธิ์อนุมัติกิจกรรม"; } $action = 'list'; } // ยกเลิกการอนุมัติ if ($action == 'unapprove' && isset($_GET['id'])) { $stmt = $pdo->prepare("SELECT * FROM featured_activities WHERE id = ?"); $stmt->execute([$_GET['id']]); $record = $stmt->fetch(); if ($record && $record['status'] === 'approved' && $record['approved_by'] === $user['username']) { $stmt = $pdo->prepare("UPDATE featured_activities SET status = 'pending', approved_by = NULL, approved_date = NULL WHERE id = ?"); $stmt->execute([$_GET['id']]); $message = "ยกเลิกการอนุมัติเรียบร้อยแล้ว"; } else { $message = "ไม่มีสิทธิ์ยกเลิกการอนุมัติ"; } $action = 'list'; } // ปฏิเสธกิจกรรม if ($action == 'reject' && isset($_GET['id'])) { $stmt = $pdo->prepare("SELECT * FROM featured_activities WHERE id = ?"); $stmt->execute([$_GET['id']]); $record = $stmt->fetch(); if ($record && canApproveRecord($pdo, $user, $record)) { $stmt = $pdo->prepare("UPDATE featured_activities SET status = 'rejected' WHERE id = ?"); $stmt->execute([$_GET['id']]); $message = "ปฏิเสธกิจกรรมเรียบร้อยแล้ว"; } else { $message = "ไม่มีสิทธิ์ปฏิเสธกิจกรรม"; } $action = 'list'; } // ฟังก์ชันย่อขนาดรูปภาพ function compressAndSaveImage($sourcePath, $destinationPath, $maxFileSize = 512000) { list($width, $height, $type) = getimagesize($sourcePath); switch ($type) { case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($sourcePath); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($sourcePath); break; case IMAGETYPE_WEBP: $image = imagecreatefromwebp($sourcePath); break; default: return false; } if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_WEBP) { imagealphablending($image, true); imagesavealpha($image, true); } $maxDim = 1200; if ($width > $maxDim || $height > $maxDim) { $ratio = $width / $height; if ($ratio > 1) { $new_width = $maxDim; $new_height = $maxDim / $ratio; } else { $new_height = $maxDim; $new_width = $maxDim * $ratio; } $dst = imagecreatetruecolor($new_width, $new_height); if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_WEBP) { imagealphablending($dst, false); imagesavealpha($dst, true); } imagecopyresampled($dst, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagedestroy($image); $image = $dst; } $quality = 85; do { ob_start(); imagejpeg($image, null, $quality); $imageData = ob_get_clean(); $currentSize = strlen($imageData); $quality -= 5; } while ($currentSize > $maxFileSize && $quality > 10); $result = file_put_contents($destinationPath, $imageData); imagedestroy($image); return $result ? true : false; } // บันทึก (Add/Edit) if ($_SERVER['REQUEST_METHOD'] == 'POST') { $title = $_POST['title'] ?? ''; $activity_type = $_POST['activity_type'] ?? ''; // ถ้าเลือก "อื่นๆ" ให้เก็บทั้ง "อื่นๆ" และข้อความที่ระบุ $activity_type_other = ''; if ($activity_type === 'อื่นๆ') { $activity_type_other = trim($_POST['activity_type_other'] ?? ''); } $location = $_POST['location'] ?? ''; $start_date = $_POST['start_date'] ?: null; $end_date = $_POST['end_date'] ?: null; $start_time = $_POST['start_time'] ?: null; $end_time = $_POST['end_time'] ?: null; $target_group = $_POST['target_group'] ?? ''; $target_count = (int)($_POST['target_count'] ?? 0); $participant_count = (int)($_POST['participant_count'] ?? 0); $description = $_POST['description'] ?? ''; $link_url = $_POST['link_url'] ?? ''; $province = $_POST['province'] ?? ($_SESSION['user_data']['province'] ?? ''); $district = $_POST['district'] ?? ($_SESSION['user_data']['amphur'] ?? ''); $sub_district = $_POST['sub_district'] ?? ($_SESSION['user_data']['tumbon'] ?? ''); // ใช้ activity_date เป็นวันที่หลัก (= start_date) $activity_date = $start_date; // ดึงโปสเตอร์เดิม (ถ้าแก้ไข) $poster_url = ''; if (isset($_POST['id']) && !empty($_POST['id'])) { $stmt_old = $pdo->prepare("SELECT poster_url FROM featured_activities WHERE id = ?"); $stmt_old->execute([$_POST['id']]); $old_data = $stmt_old->fetch(); if ($old_data) $poster_url = $old_data['poster_url']; } // อัปโหลดโปสเตอร์ใหม่ (ถ้ามี) if (isset($_FILES['poster_file']) && $_FILES['poster_file']['error'] == 0) { $upload_dir = 'upload/'; if (!is_dir($upload_dir)) mkdir($upload_dir, 0755, true); $tmp_name = $_FILES['poster_file']['tmp_name']; $new_filename = 'act_' . uniqid() . '_' . time() . '.jpg'; $destination = $upload_dir . $new_filename; if (compressAndSaveImage($tmp_name, $destination)) { // ลบรูปเดิม if (!empty($poster_url) && strpos($poster_url, 'http') !== 0) { $old_path = $upload_dir . $poster_url; if (file_exists($old_path)) unlink($old_path); } $poster_url = $new_filename; } } $username_owner = $_SESSION['user_data']['username'] ?? ''; if (isset($_POST['id']) && !empty($_POST['id'])) { // ดึงสถานะปัจจุบัน $stmt_check = $pdo->prepare("SELECT status FROM featured_activities WHERE id = ?"); $stmt_check->execute([$_POST['id']]); $current = $stmt_check->fetch(); $current_status = $current['status'] ?? 'pending'; $new_status = ($current_status === 'rejected') ? 'pending' : $current_status; $stmt = $pdo->prepare("UPDATE featured_activities SET title=?, description=?, activity_date=?, poster_url=?, activity_type=?, activity_type_other=?, location=?, start_date=?, end_date=?, start_time=?, end_time=?, target_group=?, target_count=?, participant_count=?, link_url=?, province=?, district=?, sub_district=?, status=? WHERE id=? AND created_by=?"); $stmt->execute([ $title, $description, $activity_date, $poster_url, $activity_type, $activity_type_other, $location, $start_date, $end_date, $start_time, $end_time, $target_group, $target_count, $participant_count, $link_url, $province, $district, $sub_district, $new_status, $_POST['id'], $username_owner ]); $message = "แก้ไขกิจกรรมเรียบร้อยแล้ว" . ($current_status === 'rejected' ? " - ส่งอนุมัติอีกครั้ง" : ""); } else { $stmt = $pdo->prepare("INSERT INTO featured_activities (created_by, title, description, activity_date, poster_url, activity_type, activity_type_other, location, start_date, end_date, start_time, end_time, target_group, target_count, participant_count, link_url, province, district, sub_district, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending')"); $stmt->execute([ $username_owner, $title, $description, $activity_date, $poster_url, $activity_type, $activity_type_other, $location, $start_date, $end_date, $start_time, $end_time, $target_group, $target_count, $participant_count, $link_url, $province, $district, $sub_district ]); $message = "เพิ่มกิจกรรมเรียบร้อยแล้ว - รอการอนุมัติ"; } $action = 'list'; } // ดึงข้อมูลค้นหา/กรอง $schools = []; $selected_school = $_GET['school_filter'] ?? ''; if ($user['user_type_id'] == '1') { $province = $user['province'] ?? ''; if (!empty($province)) { $stmt = $pdo->prepare("SELECT username, firstname, title_name FROM users WHERE user_type_id = '2' AND province = ? ORDER BY firstname ASC"); $stmt->execute([$province]); $schools = $stmt->fetchAll(); } } $subdistricts = []; if ($user['user_type_id'] == '2') { $username = $user['username'] ?? ''; if (!empty($username)) { $stmt = $pdo->prepare("SELECT DISTINCT tumbon FROM users WHERE user_type_id = '4' AND leader = ? ORDER BY tumbon ASC"); $stmt->execute([$username]); $subdistricts = $stmt->fetchAll(PDO::FETCH_COLUMN); } } $search_name = $_GET['search_name'] ?? ''; $search_subdistrict = $_GET['search_subdistrict'] ?? ''; $search_activity_type = $_GET['search_activity_type'] ?? ''; // ดึงประเภทกิจกรรม (เรียงตามลำดับที่เพิ่ม) - ใช้ในฟอร์มค้นหาและฟอร์มเพิ่ม/แก้ไข $activity_types_list = []; try { $activity_types_list = $pdo->query("SELECT * FROM activity_types ORDER BY id ASC")->fetchAll(); } catch (Exception $e) { // ตารางยังไม่ถูกสร้าง } // ดึงกิจกรรมตามสิทธิ์ $activities = getAccessibleActivities($pdo, $user); // Filter โดยสถานศึกษา if ($user['user_type_id'] == '1' && !empty($selected_school)) { $stmt_filter = $pdo->prepare("SELECT username FROM users WHERE username = ? OR (leader = ? AND user_type_id = '4')"); $stmt_filter->execute([$selected_school, $selected_school]); $allowed_creators = $stmt_filter->fetchAll(PDO::FETCH_COLUMN); if (!empty($allowed_creators)) { $activities = array_filter($activities, function($r) use ($allowed_creators) { return in_array($r['created_by'], $allowed_creators); }); } else { $activities = []; } } if (!empty($search_name)) { $activities = array_filter($activities, function($r) use ($search_name) { return stripos($r['title'], $search_name) !== false; }); } if ($user['user_type_id'] == '2' && !empty($search_subdistrict)) { $activities = array_filter($activities, function($r) use ($search_subdistrict) { return ($r['sub_district'] ?? '') === $search_subdistrict; }); } // Filter โดยประเภทกิจกรรม if (!empty($search_activity_type)) { $known_type_names = array_column($activity_types_list, 'name'); if ($search_activity_type === '__OTHER__') { // "อื่นๆ" = ประเภทเป็น "อื่นๆ" หรือไม่อยู่ในรายการตัวเลือก (ข้อมูลเก่า) $activities = array_filter($activities, function($r) use ($known_type_names) { $t = $r['activity_type'] ?? ''; if ($t === 'อื่นๆ') return true; return $t !== '' && !in_array($t, $known_type_names); }); } else { $activities = array_filter($activities, function($r) use ($search_activity_type) { return ($r['activity_type'] ?? '') === $search_activity_type; }); } } // ดึงข้อมูลเดี่ยวสำหรับแก้ไข/ดู $editData = null; $editStatus = 'pending'; $isEditMode = false; $canViewOnly = false; if ($action == 'edit' && isset($_GET['id'])) { $stmt = $pdo->prepare("SELECT * FROM featured_activities WHERE id = ?"); $stmt->execute([$_GET['id']]); $editData = $stmt->fetch(); if (!$editData) { header("Location: activities.php"); exit(); } $editStatus = $editData['status'] ?? 'pending'; if (canEditRecord($user, $editData)) { $isEditMode = true; } elseif (canAccessRecord($user, $editData)) { $canViewOnly = true; } else { header("Location: activities.php"); exit(); } } ?>