ค้นหากิจกรรม (ทั้งระบบ)
ค้นหากิจกรรม
ค้นหากิจกรรม
โปรดแก้ไขตามข้อเสนอแนะและส่งอนุมัติอีกครั้ง
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 } // Self-healing: คอลัมน์ homepage_activity_ids ใน system_settings try { $col_check2 = $pdo->query("SHOW COLUMNS FROM system_settings LIKE 'homepage_activity_ids'")->fetch(); if (!$col_check2) { $pdo->exec("ALTER TABLE system_settings ADD COLUMN homepage_activity_ids TEXT NULL"); } } catch (Exception $e) {} // อ่านรายการกิจกรรมเด่นที่แอดมินเลือกไว้ (CSV) function getHomepageActivityIds($pdo) { try { $row = $pdo->query("SELECT homepage_activity_ids FROM system_settings WHERE id=1")->fetch(); return array_values(array_filter(array_map('intval', explode(',', $row['homepage_activity_ids'] ?? '')))); } catch (Exception $e) { return []; } } // Toggle รายการเด่น — เฉพาะ user_type_id = 9 if ($action == 'toggle_featured' && isset($_GET['id']) && ($user['user_type_id'] ?? '') == '9') { $aid = (int)$_GET['id']; $chk = $pdo->prepare("SELECT status FROM featured_activities WHERE id = ?"); $chk->execute([$aid]); $r = $chk->fetch(); if ($r && $r['status'] === 'approved') { $ids = getHomepageActivityIds($pdo); if (in_array($aid, $ids, true)) { $ids = array_values(array_diff($ids, [$aid])); $message = "นำกิจกรรมออกจากหน้าหลักแล้ว"; } else { if (count($ids) >= 9) { $message = "เลือกได้สูงสุด 9 กิจกรรมเท่านั้น กรุณานำบางรายการออกก่อน"; } else { $ids[] = $aid; $message = "เพิ่มกิจกรรมในหน้าหลักแล้ว"; } } $csv = implode(',', $ids); $upd = $pdo->prepare("UPDATE system_settings SET homepage_activity_ids = ? WHERE id = 1"); $upd->execute([$csv]); } else { $message = "เลือกได้เฉพาะกิจกรรมที่อนุมัติแล้วเท่านั้น"; } $action = 'list'; } // ดึงรายการเด่นปัจจุบันสำหรับใช้แสดงผล $featuredIds = getHomepageActivityIds($pdo); // เฉพาะ 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(); } } elseif ($user['user_type_id'] == '9') { // ผู้ดูแลระบบ — เห็นสถานศึกษาทุกจังหวัด $schools = $pdo->query("SELECT username, firstname, title_name, province FROM users WHERE user_type_id = '2' ORDER BY province ASC, firstname ASC")->fetchAll(); } // === Filter ตามภาค/จังหวัด/อำเภอ/ตำบล (สำหรับ admin type 9) === $REGION_LABELS = [ 1 => 'ภาคเหนือ', 2 => 'ภาคตะวันออกเฉียงเหนือ', 3 => 'ภาคตะวันออก', 4 => 'ภาคกลาง', 5 => 'ภาคใต้', ]; $filter_region = isset($_GET['filter_region']) && $_GET['filter_region'] !== '' ? (int)$_GET['filter_region'] : ''; $filter_province = $_GET['filter_province'] ?? ''; $filter_district = $_GET['filter_district'] ?? ''; $filter_subdistrict = $_GET['filter_subdistrict'] ?? ''; // แมป จังหวัด → ภาค (จาก learning_resources) $province_geo_map = []; try { $pg_rows = $pdo->query("SELECT DISTINCT province, geo FROM learning_resources WHERE province IS NOT NULL AND province != '' AND geo IS NOT NULL")->fetchAll(); foreach ($pg_rows as $r) { if (!isset($province_geo_map[$r['province']])) { $province_geo_map[$r['province']] = (int)$r['geo']; } } } catch (Exception $e) {} // รายชื่อจังหวัด/อำเภอ/ตำบล ที่ใช้ใน dropdown (เฉพาะ type 9) — ดึงจาก featured_activities เพื่อให้สอดคล้องกับข้อมูลจริง $all_provinces = $all_districts = $all_subdistricts = []; if ($user['user_type_id'] == '9') { $all_provinces = $pdo->query("SELECT DISTINCT province FROM featured_activities WHERE province IS NOT NULL AND province != '' ORDER BY province ASC")->fetchAll(PDO::FETCH_COLUMN); // กรองจังหวัดตามภาคที่เลือก if ($filter_region !== '') { $all_provinces = array_values(array_filter($all_provinces, function($p) use ($province_geo_map, $filter_region) { return isset($province_geo_map[$p]) && $province_geo_map[$p] === $filter_region; })); } if ($filter_province !== '') { $stmt = $pdo->prepare("SELECT DISTINCT district FROM featured_activities WHERE province = ? AND district IS NOT NULL AND district != '' ORDER BY district ASC"); $stmt->execute([$filter_province]); $all_districts = $stmt->fetchAll(PDO::FETCH_COLUMN); } if ($filter_province !== '' && $filter_district !== '') { $stmt = $pdo->prepare("SELECT DISTINCT sub_district FROM featured_activities WHERE province = ? AND district = ? AND sub_district IS NOT NULL AND sub_district != '' ORDER BY sub_district ASC"); $stmt->execute([$filter_province, $filter_district]); $all_subdistricts = $stmt->fetchAll(PDO::FETCH_COLUMN); } // Cascade reset: ถ้าค่าระดับล่างไม่อยู่ในชุดที่ระดับบนอนุญาต ให้ล้างค่าทิ้ง if ($filter_province !== '' && !in_array($filter_province, $all_provinces, true)) { $filter_province = $filter_district = $filter_subdistrict = ''; $all_districts = $all_subdistricts = []; } if ($filter_district !== '' && !in_array($filter_district, $all_districts, true)) { $filter_district = $filter_subdistrict = ''; $all_subdistricts = []; } if ($filter_subdistrict !== '' && !in_array($filter_subdistrict, $all_subdistricts, true)) { $filter_subdistrict = ''; } } $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) { // ตารางยังไม่ถูกสร้าง } // === DB-level pagination + filtering === require_once '../includes/pagination.php'; ensureIndexes($pdo); // แปลง school_filter เป็นรายการ usernames $school_creators = null; if (in_array($user['user_type_id'], ['1', '9']) && !empty($selected_school)) { $stmt_sc = $pdo->prepare("SELECT username FROM users WHERE username = ? OR (leader = ? AND user_type_id = '4')"); $stmt_sc->execute([$selected_school, $selected_school]); $school_creators = $stmt_sc->fetchAll(PDO::FETCH_COLUMN); if (empty($school_creators)) $school_creators = ['__none__']; // บังคับ 0 ผลลัพธ์ } $filters = [ 'name' => $search_name, 'activity_type' => $search_activity_type, ]; if ($user['user_type_id'] == '2' && !empty($search_subdistrict)) { $filters['sub_district'] = $search_subdistrict; } if ($user['user_type_id'] == '9') { if ($filter_region !== '') $filters['region'] = $filter_region; if ($filter_province !== '') $filters['province'] = $filter_province; if ($filter_district !== '') $filters['district'] = $filter_district; if ($filter_subdistrict !== '') $filters['sub_district'] = $filter_subdistrict; } if ($school_creators !== null) $filters['school_creators'] = $school_creators; [$page, $per_page, ] = paginationParams(); $order_by = "COALESCE(start_date, activity_date) DESC, id DESC"; [$activities, $total_activities, $page, $per_page] = queryAccessiblePaginated($pdo, $user, 'featured_activities', [ 'filters' => $filters, 'order_by' => $order_by, 'featured_first' => ($user['user_type_id'] == '9') ? $featuredIds : null, 'page' => $page, 'per_page' => $per_page, ]); $pg = paginateSql($total_activities, $page, $per_page); $pg['items'] = $activities; // ดึงข้อมูลเดี่ยวสำหรับแก้ไข/ดู $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(); } } ?>