prepare("SELECT * FROM learning_resources WHERE id = ?"); $stmt->execute([$_GET['id']]); $record = $stmt->fetch(); if ($record && canDeleteRecord($user, $record)) { $stmt = $pdo->prepare("DELETE FROM learning_resources 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 = []; if (isset($_POST['id']) && !empty($_POST['id'])) { $stmt_old = $pdo->prepare("SELECT image_url 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 (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; } } } } // แปลงอาเรย์รายชื่อไฟล์รูปภาพให้เป็น JSON String เพื่อนำไปลงฐานข้อมูล $image_url_json = json_encode($uploaded_images, JSON_UNESCAPED_UNICODE); if (isset($_POST['id']) && !empty($_POST['id'])) { // 1. แก้ไขฝั่ง Update: เพิ่ม latitude=?, longitude=? เข้าไปก่อนเงื่อนไข WHERE $username_owner = $_SESSION['user_data']['username'] ?? ''; $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=?, 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, $latitude, $longitude, $_POST['id'], $username_owner]); $message = "แก้ไขข้อมูลเรียบร้อยแล้ว"; } else { // 2. แก้ไขฝั่ง Insert: เพิ่มคอลัมน์ latitude, longitude ในวงเล็บแรก และเพิ่มเครื่องหมาย ? อีก 2 ตัวในวงเล็บ VALUES $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, 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, $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(); } } // 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 = []; } } // Fetch single resource for edit $editData = null; if ($action == 'edit' && isset($_GET['id'])) { $stmt = $pdo->prepare("SELECT * FROM learning_resources WHERE id = ?"); $stmt->execute([$_GET['id']]); $editData = $stmt->fetch(); // ตรวจสอบสิทธิ์ - ต้องเป็นเจ้าของเท่านั้น if (!$editData || !canEditRecord($user, $editData)) { header("Location: resources.php"); exit(); } } ?>