D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
skyconb
/
newsyacine
/
Filename :
manage_ads.php
back
Copy
<?php // =============================================== // 1. الأمان والاتصال // =============================================== session_start(); if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) { header("Location: login.php"); exit(); } include 'db_connect.php'; $message = null; // =============================================== // 2. معالجة الإرسال (إضافة/تعديل/تفعيل/تعطيل) // =============================================== if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['add_ad']) || isset($_POST['edit_ad'])) { $name = $_POST['name']; $position = $_POST['position']; $ad_code = $_POST['ad_code']; $is_active = isset($_POST['is_active']) ? 1 : 0; if (isset($_POST['add_ad'])) { $sql = "INSERT INTO ads (name, position, ad_code, is_active) VALUES (?, ?, ?, ?)"; $stmt = $conn->prepare($sql); $stmt->bind_param("sssi", $name, $position, $ad_code, $is_active); if ($stmt->execute()) { $message = "تم إضافة الإعلان بنجاح!"; } else { $message = "خطأ في الإضافة: " . $stmt->error; } } elseif (isset($_POST['edit_ad'])) { $id = $_POST['id']; $sql = "UPDATE ads SET name = ?, position = ?, ad_code = ?, is_active = ? WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("sssi", $name, $position, $ad_code, $is_active, $id); if ($stmt->execute()) { $message = "تم تحديث الإعلان بنجاح!"; } else { $message = "خطأ في التحديث: " . $stmt->error; } } $stmt->close(); } elseif (isset($_POST['toggle_status'])) { $id = $_POST['id']; $current_status = $_POST['current_status']; $new_status = $current_status == 1 ? 0 : 1; $action_name = $new_status == 1 ? "تفعيل" : "تعطيل"; $sql = "UPDATE ads SET is_active = ? WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("ii", $new_status, $id); if ($stmt->execute()) { $message = "تم " . $action_name . " الإعلان بنجاح!"; } else { $message = "خطأ في تغيير الحالة: " . $stmt->error; } $stmt->close(); } } // =============================================== // 3. جلب الإعلانات للعرض // =============================================== $ads_sql = "SELECT * FROM ads ORDER BY id DESC"; $ads_result = $conn->query($ads_sql); ?> <!DOCTYPE html> <html lang="ar" dir="rtl"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>إدارة الإعلانات</title> <style> /* CSS مماثل لـ admin.php */ body { font-family: Tahoma, sans-serif; margin: 0; background-color: #f4f4f4; } .dashboard-container { display: flex; min-height: 100vh; } .sidebar { width: 250px; background-color: #2c3e50; color: white; padding: 20px; box-shadow: 2px 0 5px rgba(0,0,0,0.1); } .sidebar h2 { text-align: center; border-bottom: 2px solid #34495e; padding-bottom: 10px; margin-bottom: 30px; } .sidebar ul { list-style: none; padding: 0; margin: 0; } .sidebar ul li a { display: block; padding: 10px 15px; margin-bottom: 10px; text-decoration: none; color: white; border-radius: 4px; transition: background-color 0.3s; } .sidebar ul li a:hover { background-color: #34495e; } .main-content { flex-grow: 1; padding: 20px; } .main-content h1 { border-bottom: 1px solid #ccc; padding-bottom: 15px; margin-top: 0; } .logout-btn { display: block; margin-top: 30px; text-align: center; padding-top: 10px; border-top: 1px solid #34495e; } .logout-btn a { color: #f39c12; text-decoration: none; } /* تنسيقات الإعلانات */ .add-form, .edit-form { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 30px; } label { display: block; margin-top: 15px; font-weight: bold; } input[type="text"], textarea, select { width: 100%; padding: 10px; margin-top: 5px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } button { background-color: #3498db; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; margin-top: 20px; transition: background-color 0.3s; } button:hover { background-color: #2980b9; } .ad-list table { width: 100%; border-collapse: collapse; margin-top: 20px; background: white; } .ad-list th, .ad-list td { border: 1px solid #ddd; padding: 12px; text-align: right; } .ad-list th { background-color: #2c3e50; color: white; } .ad-list tr:nth-child(even) { background-color: #f2f2f2; } .status-active { color: white; background-color: #2ecc71; padding: 4px 8px; border-radius: 3px; } .status-inactive { color: white; background-color: #e74c3c; padding: 4px 8px; border-radius: 3px; } .toggle-btn { background-color: #e67e22; margin-top: 0; padding: 8px 15px; } .toggle-btn:hover { background-color: #d35400; } .action-btn-group { display: flex; gap: 5px; } .edit-ad-form textarea { height: 200px; } .status-message { padding: 10px; margin-bottom: 15px; border-radius: 4px; text-align: center; font-weight: bold; background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; } </style> </head> <body> <div class="dashboard-container"> <div class="sidebar"> <h2>لوحة الإدارة</h2> <ul> <li><a href="admin.php">قائمة الفيديوهات</a></li> <li><a href="add_video.php">إضافة فيديو جديد</a></li> <li><a href="manage_ads.php" style="background-color: #3498db;">إدارة الإعلانات</a></li> </ul> <div class="logout-btn"> <a href="logout.php">تسجيل الخروج</a> </div> </div> <div class="main-content"> <h1>إدارة الإعلانات</h1> <?php if ($message): ?><div class="status-message"><?php echo $message; ?></div><?php endif; ?> <div class="add-form"> <h2>إضافة إعلان جديد</h2> <form method="POST"> <label for="name">اسم الإعلان (للتذكير فقط):</label> <input type="text" id="name" name="name" required> <label for="position">موقع الإعلان (مفتاح الاستدعاء):</label> <select id="position" name="position" required> <option value="">-- اختر موقعاً --</option> <option value="index_top">القائمة الرئيسية (الأعلى)</option> <option value="index_middle">القائمة الرئيسية (بين الفيديوهات)</option> <option value="watch_top">صفحة المشاهدة (فوق المشغل)</option> <option value="watch_bottom">صفحة المشاهدة (أسفل الوصف)</option> </select> <label for="ad_code">كود الإعلان (Script/HTML):</label> <textarea id="ad_code" name="ad_code" rows="8" required></textarea> <label> <input type="checkbox" name="is_active" value="1" checked> تفعيل الإعلان الآن </label> <button type="submit" name="add_ad">إضافة الإعلان</button> </form> </div> <h2>قائمة الإعلانات الحالية</h2> <div class="ad-list"> <?php if ($ads_result->num_rows > 0): ?> <table> <thead> <tr> <th>ID</th> <th>الاسم</th> <th>الموقع</th> <th>الحالة</th> <th>الإجراءات</th> </tr> </thead> <tbody> <?php while($ad = $ads_result->fetch_assoc()): ?> <tr> <td><?php echo $ad['id']; ?></td> <td><?php echo htmlspecialchars($ad['name']); ?></td> <td><?php echo htmlspecialchars($ad['position']); ?></td> <td> <span class="<?php echo $ad['is_active'] ? 'status-active' : 'status-inactive'; ?>"> <?php echo $ad['is_active'] ? 'مفعّل' : 'معطّل'; ?> </span> </td> <td class="action-btn-group"> <form method="POST" style="display: inline-block;"> <input type="hidden" name="id" value="<?php echo $ad['id']; ?>"> <input type="hidden" name="current_status" value="<?php echo $ad['is_active']; ?>"> <button type="submit" name="toggle_status" class="toggle-btn" onclick="return confirm('هل أنت متأكد من تغيير حالة هذا الإعلان؟');"> <?php echo $ad['is_active'] ? 'تعطيل' : 'تفعيل'; ?> </button> </form> <button class="embed-button" style="margin: 0;" onclick="openEditModal(<?php echo $ad['id']; ?>, '<?php echo htmlspecialchars($ad['name']); ?>', '<?php echo htmlspecialchars($ad['position']); ?>', `<?php echo htmlspecialchars($ad['ad_code']); ?>`, <?php echo $ad['is_active']; ?>)"> تعديل </button> </td> </tr> <?php endwhile; ?> </tbody> </table> <?php else: ?> <p style="text-align: center; padding: 20px;">لا توجد إعلانات مضافة حالياً.</p> <?php endif; ?> </div> </div> </div> <div id="editModal" class="modal" style="display: none;"> <div class="modal-content"> <h2>تعديل الإعلان</h2> <form method="POST" class="edit-ad-form"> <input type="hidden" id="edit-id" name="id"> <label for="edit-name">اسم الإعلان:</label> <input type="text" id="edit-name" name="name" required> <label for="edit-position">موقع الإعلان:</label> <select id="edit-position" name="position" required> <option value="index_top">القائمة الرئيسية (الأعلى)</option> <option value="index_middle">القائمة الرئيسية (بين الفيديوهات)</option> <option value="watch_top">صفحة المشاهدة (فوق المشغل)</option> <option value="watch_bottom">صفحة المشاهدة (أسفل الوصف)</option> </select> <label for="edit-ad_code">كود الإعلان (Script/HTML):</label> <textarea id="edit-ad_code" name="ad_code" rows="8" required></textarea> <label> <input type="checkbox" id="edit-is_active" name="is_active" value="1"> تفعيل الإعلان </label> <button type="submit" name="edit_ad">حفظ التعديلات</button> <button type="button" onclick="document.getElementById('editModal').style.display='none';" style="background-color: #e74c3c;">إلغاء</button> </form> </div> </div> <script> function openEditModal(id, name, position, adCode, isActive) { document.getElementById('edit-id').value = id; document.getElementById('edit-name').value = name; document.getElementById('edit-position').value = position; document.getElementById('edit-ad_code').value = adCode.replace(/</g, '<').replace(/>/g, '>'); // فك تشفير HTML document.getElementById('edit-is_active').checked = isActive == 1; document.getElementById('editModal').style.display = 'block'; } </script> </body> </html> <?php $conn->close(); ?>