D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
skyconb
/
newsyacine
/
Filename :
edit_article.php
back
Copy
<?php // =============================================== // edit_article.php (تعديل مقال موجود) // =============================================== session_start(); include 'db_connect.php'; // 1. التحقق من الصلاحية $is_admin = ($_SESSION['admin_logged_in'] ?? false) === true && ($_SESSION['admin'] ?? 'user') === 'admin'; if (!$is_admin) { $_SESSION['error'] = "ليس لديك الصلاحية لتعديل المقالات."; header("Location: admin.php?section=articles"); exit(); } $error = ''; $message = ''; $article_id = $_GET['id'] ?? 0; // =============================================== // 2. جلب بيانات المقال الحالي // =============================================== if ($article_id > 0) { $sql = "SELECT id, title, content, slug, category, thumbnail_url FROM articles WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $article_id); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows === 1) { $article = $result->fetch_assoc(); // تعيين المتغيرات لعرضها في النموذج $title = $article['title']; $content = $article['content']; $slug = $article['slug']; $category = $article['category']; $current_thumbnail_url = $article['thumbnail_url']; } else { $_SESSION['error'] = "المقال المطلوب تعديله غير موجود."; header("Location: admin.php?section=articles"); exit(); } $stmt->close(); } else { $_SESSION['error'] = "معرّف المقال غير صالح."; header("Location: admin.php?section=articles"); exit(); } // =============================================== // 3. معالجة تحديث البيانات عند إرسال النموذج // =============================================== if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_article'])) { // جلب البيانات المُرسلة وتجهيزها $title_new = trim($_POST['title']); $content_new = $_POST['content']; // يتم إنشاء slug جديد من العنوان إذا كان حقل slug فارغاً، وإلا يتم استخدام المدخل $slug_new = strtolower(trim(str_replace(' ', '-', $_POST['slug'] ?: $title_new))); $category_new = $_POST['category']; $thumbnail_path = $current_thumbnail_url; // احتفاظ بالمسار القديم كقيمة افتراضية if (empty($title_new) || empty($content_new) || empty($slug_new)) { $error = "الرجاء ملء جميع الحقول المطلوبة (العنوان والمحتوى والعنوان المختصر)."; } // 3.1. منطق تحميل الصورة (Handling File Upload) if (isset($_FILES['thumbnail_file']) && $_FILES['thumbnail_file']['error'] === UPLOAD_ERR_OK) { $file_tmp_name = $_FILES['thumbnail_file']['tmp_name']; $file_name = $_FILES['thumbnail_file']['name']; $file_extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); $unique_name = uniqid() . '.' . $file_extension; $upload_dir = 'uploads/'; $destination = $upload_dir . $unique_name; $allowed_types = ['jpg', 'jpeg', 'png', 'gif']; if (!in_array($file_extension, $allowed_types)) { $error = "الرجاء رفع ملف صورة صالح (JPG, PNG, GIF)."; } elseif (!is_dir($upload_dir) && !mkdir($upload_dir, 0777, true)) { $error = "فشل إنشاء مجلد الرفع: $upload_dir"; } else { if (move_uploaded_file($file_tmp_name, $destination)) { $thumbnail_path = $destination; // المسار الجديد // حذف الصورة القديمة إذا كانت موجودة وتختلف عن الصورة الجديدة if (!empty($current_thumbnail_url) && file_exists($current_thumbnail_url) && $current_thumbnail_url !== $thumbnail_path) { unlink($current_thumbnail_url); } } else { $error = "حدث خطأ أثناء تحميل الملف."; } } } // 3.2. التحقق من تكرار Slug (تجاهل Slug المقال الحالي) if (!$error) { $check_sql = "SELECT id FROM articles WHERE slug = ? AND id != ?"; $check_stmt = $conn->prepare($check_sql); $check_stmt->bind_param("si", $slug_new, $article_id); $check_stmt->execute(); if ($check_stmt->get_result()->num_rows > 0) { $error = "العنوان المختصر (Slug) مستخدم بالفعل بواسطة مقال آخر. اختر اسماً فريداً."; } $check_stmt->close(); } // 3.3. تنفيذ تحديث البيانات if (!$error) { $update_sql = "UPDATE articles SET title = ?, content = ?, slug = ?, category = ?, thumbnail_url = ? WHERE id = ?"; $update_stmt = $conn->prepare($update_sql); if ($update_stmt) { $update_stmt->bind_param("sssssi", $title_new, $content_new, $slug_new, $category_new, $thumbnail_path, $article_id); if ($update_stmt->execute()) { $_SESSION['message'] = "تم تحديث المقال بنجاح!"; header("Location: admin.php?section=articles"); exit(); } else { $error = "خطأ في التحديث: " . $update_stmt->error; } $update_stmt->close(); } } // في حالة وجود خطأ، نقوم بتحديث المتغيرات المعروضة بالقيم الجديدة غير المحفوظة $title = $title_new; $content = $content_new; $slug = $slug_new; $category = $category_new; $current_thumbnail_url = $thumbnail_path; } $conn->close(); ?> <!DOCTYPE html> <html lang="ar" dir="rtl"> <head> <meta charset="UTF-8"> <title>تعديل المقال: <?php echo htmlspecialchars($article['title'] ?? 'N/A'); ?></title> <style> /* CSS: استخدم نفس تصميم add_article.php */ body { font-family: Tahoma, sans-serif; margin: 0; background-color: #f0f2f5; display: flex; justify-content: center; align-items: center; min-height: 100vh; } .form-container { background: white; padding: 40px; border-radius: 8px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); width: 90%; max-width: 800px; } h2 { color: #2c3e50; border-bottom: 2px solid #eee; padding-bottom: 10px; margin-bottom: 25px; text-align: center; } .form-group { margin-bottom: 20px; } label { display: block; margin-bottom: 8px; font-weight: bold; color: #333; } input[type="text"], input[type="file"], textarea { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; font-size: 14px; } textarea { resize: vertical; } .status-message { padding: 10px; margin-bottom: 15px; border-radius: 4px; text-align: center; font-weight: bold; } .error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; } .submit-btn { width: 100%; padding: 12px; background-color: #3498db; /* لون مختلف للتعديل */ color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; font-weight: bold; transition: background-color 0.3s; } .submit-btn:hover { background-color: #2980b9; } .back-link { display: block; margin-top: 25px; text-align: center; color: #3498db; text-decoration: none; } .current-thumbnail { margin-top: 10px; padding: 10px; border: 1px solid #ddd; background: #f9f9f9; } .current-thumbnail img { max-width: 100px; max-height: 100px; display: block; margin-top: 5px; } </style> </head> <body> <div class="form-container"> <h2>تعديل المقال: <?php echo htmlspecialchars($article['title'] ?? 'N/A'); ?></h2> <?php if ($error): ?><div class="status-message error"><?php echo $error; ?></div><?php endif; ?> <form method="POST" enctype="multipart/form-data"> <div class="form-group"> <label for="title">عنوان المقال:</label> <input type="text" name="title" required value="<?php echo htmlspecialchars($title); ?>"> </div> <div class="form-group"> <label for="slug">العنوان المختصر (Slug):</label> <input type="text" name="slug" placeholder="اتركه فارغاً ليتم إنشاؤه تلقائياً من العنوان" value="<?php echo htmlspecialchars($slug); ?>"> </div> <div class="form-group"> <label for="thumbnail_file">تغيير الصورة المصغرة (JPG, PNG, GIF):</label> <input type="file" name="thumbnail_file" id="thumbnail_file" accept="image/*"> <?php if (!empty($current_thumbnail_url)): ?> <div class="current-thumbnail"> <span>الصورة الحالية:</span> <img src="<?php echo htmlspecialchars($current_thumbnail_url); ?>" alt="صورة مصغرة"> </div> <?php endif; ?> </div> <div class="form-group"> <label for="category">التصنيف:</label> <input type="text" name="category" value="<?php echo htmlspecialchars($category); ?>"> </div> <div class="form-group"> <label for="content">محتوى المقال:</label> <textarea name="content" rows="15" required><?php echo htmlspecialchars($content); ?></textarea> </div> <button type="submit" name="update_article" class="submit-btn">حفظ التعديلات</button> </form> <a href="admin.php?section=articles" class="back-link">العودة لإدارة المقالات</a> </div> </body> </html>