exec("CREATE TABLE IF NOT EXISTS hospital_ads ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NULL, video_path VARCHAR(255) NOT NULL, is_active TINYINT(1) DEFAULT 1, sort_order INT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); $pdo->exec("CREATE TABLE IF NOT EXISTS hospital_news ( id INT AUTO_INCREMENT PRIMARY KEY, phrase VARCHAR(1000) NOT NULL, is_active TINYINT(1) DEFAULT 1, sort_order INT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); // Handle Form Submissions $message = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['action']) && $_POST['action'] === 'add_video' && !empty($_FILES['video']['name'])) { $uploadDir = __DIR__ . '/assets/videos/uploads/'; if (!is_dir($uploadDir)) { mkdir($uploadDir, 0777, true); } $ext = strtolower(pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION)); $allowed = ['mp4', 'webm', 'ogg']; if (!in_array($ext, $allowed)) { $message = qh_t("Invalid video format (.$ext). Allowed formats: MP4, WebM, OGG.", "تنسيق فيديو غير صالح (.$ext). الصيغ المسموحة: MP4, WebM, OGG."); } elseif ($_FILES['video']['error'] !== UPLOAD_ERR_OK) { $errorMap = [ UPLOAD_ERR_INI_SIZE => 'File exceeds max size in php.ini.', UPLOAD_ERR_FORM_SIZE => 'File exceeds max size in HTML form.', UPLOAD_ERR_PARTIAL => 'File was partially uploaded.', UPLOAD_ERR_NO_FILE => 'No file was uploaded.', UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.', UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.', UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the upload.' ]; $errMsg = $errorMap[$_FILES['video']['error']] ?? 'Unknown error'; $message = qh_t('Upload error: ' . $errMsg, 'خطأ في الرفع: ' . $errMsg); } else { $filename = uniqid('vid_') . '.' . $ext; $dest = $uploadDir . $filename; if (move_uploaded_file($_FILES['video']['tmp_name'], $dest)) { $title = $_POST['title'] ?? ''; $path = 'assets/videos/uploads/' . $filename; $stmt = $pdo->prepare("INSERT INTO hospital_ads (title, video_path, is_active) VALUES (?, ?, 1)"); $stmt->execute([$title, $path]); $message = qh_t('Video uploaded successfully.', 'تم رفع الفيديو بنجاح.'); } else { $message = qh_t('Failed to save uploaded file.', 'فشل في حفظ الملف المرفوع.'); } } } elseif (isset($_POST['action']) && $_POST['action'] === 'delete_video' && !empty($_POST['video_id'])) { $id = (int) $_POST['video_id']; $stmt = $pdo->prepare("SELECT video_path FROM hospital_ads WHERE id = ?"); $stmt->execute([$id]); $video = $stmt->fetch(); if ($video) { $fullPath = __DIR__ . '/' . $video['video_path']; if (file_exists($fullPath)) { unlink($fullPath); } $pdo->prepare("DELETE FROM hospital_ads WHERE id = ?")->execute([$id]); $message = qh_t('Video deleted successfully.', 'تم حذف الفيديو بنجاح.'); } } elseif (isset($_POST['action']) && $_POST['action'] === 'toggle_status' && !empty($_POST['video_id'])) { $id = (int) $_POST['video_id']; $stmt = $pdo->prepare("UPDATE hospital_ads SET is_active = NOT is_active WHERE id = ?"); $stmt->execute([$id]); $message = qh_t('Video status updated.', 'تم تحديث حالة الفيديو.'); } elseif (isset($_POST['action']) && $_POST['action'] === 'add_news' && !empty($_POST['phrase'])) { $phrase = trim($_POST['phrase']); $stmt = $pdo->prepare("INSERT INTO hospital_news (phrase, is_active) VALUES (?, 1)"); $stmt->execute([$phrase]); $message = qh_t('Phrase added successfully.', 'تمت إضافة العبارة بنجاح.'); } elseif (isset($_POST['action']) && $_POST['action'] === 'delete_news' && !empty($_POST['news_id'])) { $id = (int) $_POST['news_id']; $pdo->prepare("DELETE FROM hospital_news WHERE id = ?")->execute([$id]); $message = qh_t('Phrase deleted successfully.', 'تم حذف العبارة بنجاح.'); } elseif (isset($_POST['action']) && $_POST['action'] === 'toggle_news_status' && !empty($_POST['news_id'])) { $id = (int) $_POST['news_id']; $pdo->prepare("UPDATE hospital_news SET is_active = NOT is_active WHERE id = ?")->execute([$id]); $message = qh_t('Phrase status updated.', 'تم تحديث حالة العبارة.'); } } // Fetch existing ads $stmt = $pdo->query("SELECT * FROM hospital_ads ORDER BY sort_order ASC, id DESC"); $ads = $stmt->fetchAll(); $stmt2 = $pdo->query("SELECT * FROM hospital_news ORDER BY sort_order ASC, id DESC"); $newsPhrases = $stmt2->fetchAll(); qh_page_start( 'admin_ads', qh_t('Manage Advertisements', 'إدارة الإعلانات'), qh_t('Upload and manage videos to display on the queue screen.', 'رفع وإدارة الفيديوهات لعرضها على شاشة الطابور.') ); ?>