ค้นหาแหล่งเรียนรู้
ค้นหาแหล่งเรียนรู้
โปรดแก้ไขตามข้อเสนอแนะและส่งอนุมัติอีกครั้ง
prepare("SELECT * FROM learning_resources WHERE id = ?"); $stmt->execute([$_GET['id']]); $record = $stmt->fetch(); if ($record && canDeleteRecord($user, $record)) { $upload_dir = 'upload/'; // ลบรูปภาพปก if (!empty($record['cover_image'])) { $cover_path = $upload_dir . $record['cover_image']; if (file_exists($cover_path)) { unlink($cover_path); } } // ลบรูปภาพหลัก (JSON array) if (!empty($record['image_url'])) { $images = json_decode($record['image_url'], true) ?: []; foreach ($images as $img) { $img_path = $upload_dir . $img; if (file_exists($img_path)) { unlink($img_path); } } } // ลบข้อมูลจากฐานข้อมูล $stmt = $pdo->prepare("DELETE FROM learning_resources WHERE id = ?"); $stmt->execute([$_GET['id']]); $message = "ลบข้อมูลและไฟล์ที่เกี่ยวข้องเรียบร้อยแล้ว"; } else { $message = "ไม่มีสิทธิ์ลบข้อมูล"; } $action = 'list'; } // ตรวจสอบการอนุมัติ if ($action == 'approve' && isset($_GET['id'])) { $stmt = $pdo->prepare("SELECT * FROM learning_resources WHERE id = ?"); $stmt->execute([$_GET['id']]); $record = $stmt->fetch(); if ($record && canApproveRecord($pdo, $user, $record)) { $stmt = $pdo->prepare("UPDATE learning_resources 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 learning_resources WHERE id = ?"); $stmt->execute([$_GET['id']]); $record = $stmt->fetch(); if ($record && $record['status'] === 'approved' && $record['approved_by'] === $user['username']) { $stmt = $pdo->prepare("UPDATE learning_resources 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 learning_resources WHERE id = ?"); $stmt->execute([$_GET['id']]); $record = $stmt->fetch(); if ($record && canApproveRecord($pdo, $user, $record)) { $stmt = $pdo->prepare("UPDATE learning_resources SET status = 'rejected' WHERE id = ?"); $stmt->execute([$_GET['id']]); $message = "ปฏิเสธแหล่งเรียนรู้เรียบร้อยแล้ว"; } else { $message = "ไม่มีสิทธิ์ปฏิเสธแหล่งเรียนรู้"; } $action = 'list'; } // ฟังก์ชันสำหรับย่อขนาดรูปภาพและรักษาระดับขนาดไฟล์ไม่ให้เกิน 500KB 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; // ไม่รองรับประเภทอื่น } // จัดการค่าโปร่งใสสำหรับไฟล์ PNG/WEBP if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_WEBP) { imagealphablending($image, true); imagesavealpha($image, true); } // ถ้ารูปใหญ่เกินไป ให้ปรับสัดส่วนความกว้างให้เหมาะสมก่อน (เช่น ไม่เกิน 1200px) เพื่อลดขนาดไฟล์เชิงลึก $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; } // ทำการเซฟไฟล์เป็น JPEG และสุ่มปรับ Quality จนกว่าขนาดไฟล์ต่ำกว่า 500KB $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; } // Handle Add/Edit if ($_SERVER['REQUEST_METHOD'] == 'POST') { $name = $_POST['name']; $detail = $_POST['knowledge_detail']; $geo = $_POST['geo']; $province = $_POST['province']; $district = $_POST['district']; $sub_district = $_POST['sub_district']; $zip_code = $_POST['zip_code']; $phone = $_POST['phone']; $subject = $_POST['subject']; $type = $_POST['type']; $wisdom_branch = $_POST['wisdom_branch']; $video_url = $_POST['video_url']; $latitude = $_POST['latitude'] ?? ''; $longitude = $_POST['longitude'] ?? ''; $uploaded_images = []; $cover_image = ''; if (isset($_POST['id']) && !empty($_POST['id'])) { $stmt_old = $pdo->prepare("SELECT image_url, cover_image FROM learning_resources WHERE id = ?"); $stmt_old->execute([$_POST['id']]); $old_data = $stmt_old->fetch(); if ($old_data && !empty($old_data['image_url'])) { $uploaded_images = json_decode($old_data['image_url'], true) ?: []; } if ($old_data && !empty($old_data['cover_image'])) { $cover_image = $old_data['cover_image']; } } // ตรวจสอบการอัปโหลดไฟล์ใหม่ (รูปภาพหลัก - จำกัดสูงสุด 4 รูป) if (isset($_FILES['images']) && !empty($_FILES['images']['name'][0])) { $upload_dir = 'upload/'; // วนลูปเช็คไฟล์ (จำกัดสูงสุด 4 รูป) foreach ($_FILES['images']['name'] as $key => $val) { if ($key >= 4) break; // ตัดถ้าเกิน 4 รูป if ($_FILES['images']['error'][$key] == 0) { $tmp_name = $_FILES['images']['tmp_name'][$key]; $extension = pathinfo($_FILES['images']['name'][$key], PATHINFO_EXTENSION); // ตั้งชื่อไฟล์ใหม่ป้องกันชื่อซ้ำ (บันทึกเป็นสกุล .jpg ทั้งหมดเพราะถูกแปลงผ่านฟังก์ชันย่อภาพ) $new_filename = 'img_' . uniqid() . '_' . time() . '.jpg'; $destination = $upload_dir . $new_filename; // เรียกใช้ฟังก์ชันย่อไฟล์และบันทึก if (compressAndSaveImage($tmp_name, $destination)) { $uploaded_images[] = $new_filename; } } } } // ตรวจสอบการอัปโหลดรูปภาพปก (ภาพเดี่ยว) if (isset($_FILES['cover_image']) && $_FILES['cover_image']['error'] == 0) { $upload_dir = 'upload/'; $tmp_name = $_FILES['cover_image']['tmp_name']; $cover_filename = 'cover_' . uniqid() . '_' . time() . '.jpg'; $destination = $upload_dir . $cover_filename; if (compressAndSaveImage($tmp_name, $destination)) { $cover_image = $cover_filename; } } // แปลงอาเรย์รายชื่อไฟล์รูปภาพให้เป็น JSON String เพื่อนำไปลงฐานข้อมูล $image_url_json = json_encode($uploaded_images, JSON_UNESCAPED_UNICODE); if (isset($_POST['id']) && !empty($_POST['id'])) { // 1. แก้ไขฝั่ง Update: ตรวจสอบสถานะ ถ้า rejected ให้เปลี่ยนเป็น pending $username_owner = $_SESSION['user_data']['username'] ?? ''; // ดึงสถานะปัจจุบัน $stmt_check = $pdo->prepare("SELECT status FROM learning_resources WHERE id = ?"); $stmt_check->execute([$_POST['id']]); $current = $stmt_check->fetch(); $current_status = $current['status'] ?? 'pending'; // ถ้าสถานะเป็น rejected ให้เปลี่ยนเป็น pending สำหรับการอนุมัติอีกครั้ง $new_status = ($current_status === 'rejected') ? 'pending' : $current_status; $stmt = $pdo->prepare("UPDATE learning_resources SET name=?, knowledge_detail=?, geo=?, province=?, district=?, sub_district=?, zip_code=?, phone=?, subject=?, type=?, wisdom_branch=?, video_url=?, image_url=?, cover_image=?, status=?, latitude=?, longitude=? WHERE id=? AND created_by=?"); $stmt->execute([$name, $detail, $geo, $province, $district, $sub_district, $zip_code, $phone, $subject, $type, $wisdom_branch, $video_url, $image_url_json, $cover_image, $new_status, $latitude, $longitude, $_POST['id'], $username_owner]); $message = "แก้ไขข้อมูลเรียบร้อยแล้ว" . ($current_status === 'rejected' ? " - ส่งอนุมัติอีกครั้ง" : ""); } else { // 2. แก้ไขฝั่ง Insert: เพิ่มคอลัมน์ cover_image $username_owner = $_SESSION['user_data']['username'] ?? ''; $stmt = $pdo->prepare("INSERT INTO learning_resources (created_by, name, knowledge_detail, geo, province, district, sub_district, zip_code, phone, subject, type, wisdom_branch, video_url, image_url, cover_image, latitude, longitude) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$username_owner, $name, $detail, $geo, $province, $district, $sub_district, $zip_code, $phone, $subject, $type, $wisdom_branch, $video_url, $image_url_json, $cover_image, $latitude, $longitude]); $message = "เพิ่มข้อมูลเรียบร้อยแล้ว"; } $action = 'list'; } // ดึงข้อมูล สถานศึกษา (schools) สำหรับ user_type_id = 1 (จังหวัด) $schools = []; $selected_school = $_GET['school_filter'] ?? ''; if ($user['user_type_id'] == '1') { // จังหวัด user สามารถค้นหา/กรองตามสถานศึกษาในจังหวัด $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) สำหรับ user_type_id = 2 (สถานศึกษา) $subdistricts = []; if ($user['user_type_id'] == '2') { // สถานศึกษา user สามารถค้นหาตามตำบลที่อยู่ใต้สังกัด $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 parameters $search_name = $_GET['search_name'] ?? ''; $search_subdistrict = $_GET['search_subdistrict'] ?? ''; $search_type = $_GET['search_type'] ?? ''; // ดึงประเภทแหล่งเรียนรู้ทั้งหมด $resource_types = []; try { $resource_types = $pdo->query("SELECT * FROM resource_types ORDER BY name ASC")->fetchAll(); } catch (Exception $e) { // ตารางยังไม่ถูกสร้าง } // Fetch resources for list ตามสิทธิ์ $resources = getAccessibleResources($pdo, $user); // Filter โดยสถานศึกษา (ถ้า user_type_id = 1 เลือก school) if ($user['user_type_id'] == '1' && !empty($selected_school)) { // ดึง usernames ของผู้ใช้ที่มี leader = selected_school หรือเป็น 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); // Filter resources โดยตรวจสอบ created_by if (!empty($allowed_creators)) { $resources = array_filter($resources, function($r) use ($allowed_creators) { return in_array($r['created_by'], $allowed_creators); }); } else { $resources = []; } } // Filter โดยชื่อแหล่งเรียนรู้ if (!empty($search_name)) { $resources = array_filter($resources, function($r) use ($search_name) { return stripos($r['name'], $search_name) !== false; }); } // Filter โดยประเภทแหล่งเรียนรู้ if (!empty($search_type)) { $resources = array_filter($resources, function($r) use ($search_type) { return ($r['type'] ?? '') === $search_type; }); } // Filter โดยตำบล (ถ้า user_type_id = 2 เลือก subdistrict) if ($user['user_type_id'] == '2' && !empty($search_subdistrict)) { $resources = array_filter($resources, function($r) use ($search_subdistrict) { return $r['sub_district'] === $search_subdistrict; }); } // Fetch single resource for edit/view $editData = null; $editStatus = 'pending'; $isEditMode = false; $canViewOnly = false; if ($action == 'edit' && isset($_GET['id'])) { $stmt = $pdo->prepare("SELECT * FROM learning_resources WHERE id = ?"); $stmt->execute([$_GET['id']]); $editData = $stmt->fetch(); if (!$editData) { header("Location: resources.php"); exit(); } $editStatus = $editData['status'] ?? 'pending'; // ตรวจสอบสิทธิ์ในการแก้ไข if (canEditRecord($user, $editData)) { $isEditMode = true; } elseif (canAccessRecord($user, $editData)) { $canViewOnly = true; } else { header("Location: resources.php"); exit(); } } ?>