Quản Lý YouTube Links
| ID:
getPDO(); } catch(Exception $e) { error_log('DATABASE_CONNECTION_ERROR: ' . $e->getMessage()); die('❌ Lỗi kết nối hệ thống.'); } $current_user_id = getCurrentUserId(); $message = ''; $message_type = ''; // ===== TẠO BẢNG NẾU CHƯA TỒN TẠI ===== try { $pdo->exec(" CREATE TABLE IF NOT EXISTS youtube_links ( id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, title VARCHAR(255) NOT NULL, youtube_url TEXT NOT NULL, description TEXT, position INT NOT NULL, is_active TINYINT(1) DEFAULT 1, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX idx_user_id (user_id), INDEX idx_position (position) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci "); } catch(Exception $e) { error_log('CREATE_TABLE_ERROR: ' . $e->getMessage()); } // ===== XỬ LÝ THÊM/SỬA LINK ===== if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) { try { if ($_POST['action'] === 'save') { $id = $_POST['id'] ?? null; $title = trim($_POST['title']); $youtube_url = trim($_POST['youtube_url']); $description = trim($_POST['description']); $position = (int)$_POST['position']; if (empty($title) || empty($youtube_url)) { throw new Exception('Vui lòng điền đầy đủ tiêu đề và link YouTube!'); } // Validate YouTube URL if (!empty($youtube_url) && !preg_match('/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/.+$/', $youtube_url)) { throw new Exception('Link YouTube không hợp lệ!'); } if ($id) { // Cập nhật $stmt = $pdo->prepare(" UPDATE youtube_links SET title = ?, youtube_url = ?, description = ?, position = ?, updated_at = NOW() WHERE id = ? AND user_id = ? "); $stmt->execute([$title, $youtube_url, $description, $position, $id, $current_user_id]); $message = '✅ Cập nhật link thành công!'; } else { // Thêm mới $stmt = $pdo->prepare(" INSERT INTO youtube_links (user_id, title, youtube_url, description, position) VALUES (?, ?, ?, ?, ?) "); $stmt->execute([$current_user_id, $title, $youtube_url, $description, $position]); $message = '✅ Thêm link mới thành công!'; } $message_type = 'success'; } if ($_POST['action'] === 'delete') { $id = (int)$_POST['id']; $stmt = $pdo->prepare("DELETE FROM youtube_links WHERE id = ? AND user_id = ?"); $stmt->execute([$id, $current_user_id]); $message = '✅ Xóa link thành công!'; $message_type = 'success'; } if ($_POST['action'] === 'toggle') { $id = (int)$_POST['id']; $stmt = $pdo->prepare(" UPDATE youtube_links SET is_active = NOT is_active WHERE id = ? AND user_id = ? "); $stmt->execute([$id, $current_user_id]); $message = '✅ Cập nhật trạng thái thành công!'; $message_type = 'success'; } } catch(Exception $e) { $message = '❌ ' . $e->getMessage(); $message_type = 'danger'; } } // ===== LẤY DANH SÁCH LINK ===== try { $stmt = $pdo->prepare(" SELECT * FROM youtube_links WHERE user_id = ? ORDER BY position ASC, id ASC "); $stmt->execute([$current_user_id]); $links = $stmt->fetchAll(PDO::FETCH_ASSOC); // Nếu chưa có link nào, tạo 6 placeholder if (empty($links)) { for ($i = 1; $i <= 6; $i++) { $stmt = $pdo->prepare(" INSERT INTO youtube_links (user_id, title, youtube_url, description, position, is_active) VALUES (?, ?, ?, ?, ?, 0) "); $stmt->execute([ $current_user_id, "Video {$i} - Chưa cập nhật", "", "Mô tả cho video {$i}", $i ]); } // Lấy lại danh sách $stmt = $pdo->prepare(" SELECT * FROM youtube_links WHERE user_id = ? ORDER BY position ASC, id ASC "); $stmt->execute([$current_user_id]); $links = $stmt->fetchAll(PDO::FETCH_ASSOC); } } catch(Exception $e) { error_log('GET_LINKS_ERROR: ' . $e->getMessage()); $links = []; } // ===== FUNCTION: EXTRACT YOUTUBE VIDEO ID ===== function getYouTubeVideoId($url) { if (empty($url)) return null; preg_match('/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/', $url, $matches); return $matches[1] ?? null; } ?>