false, 'error' => 'البيانات الأساسية مطلوبة']); exit; } if ($id && $can_edit) { $stmt = db()->prepare("UPDATE events SET title=?, description=?, event_date=?, start_time=?, end_time=?, location=? WHERE id=?"); $stmt->execute([$title, $description, $date, $start_time, $end_time, $location, $id]); } elseif (!$id && $can_add) { $stmt = db()->prepare("INSERT INTO events (title, description, event_date, start_time, end_time, location, created_by) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$title, $description, $date, $start_time, $end_time, $location, $_SESSION['user_id']]); } echo json_encode(['success' => true]); exit; } EOT; $replace = << false, 'error' => 'البيانات الأساسية مطلوبة']); exit; } try { if ($id && $can_edit) { $stmt = db()->prepare("UPDATE events SET title=?, description=?, event_date=?, start_time=?, end_time=?, location=? WHERE id=?"); $stmt->execute([$title, $description, $date, $start_time, $end_time, $location, $id]); } elseif (!$id && $can_add) { $stmt = db()->prepare("INSERT INTO events (title, description, event_date, start_time, end_time, location, created_by) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$title, $description, $date, $start_time, $end_time, $location, $_SESSION['user_id']]); } else { echo json_encode(['success' => false, 'error' => 'ليس لديك صلاحية لإضافة أو تعديل الأحداث. يرجى تفعيل هذه الصلاحيات من "إدارة المستخدمين" أولاً.']); exit; } echo json_encode(['success' => true]); } catch (Exception $e) { echo json_encode(['success' => false, 'error' => 'خطأ قاعدة البيانات: ' . $e->getMessage()]); } exit; } EOT; $content = str_replace($search, $replace, $content); $search2 = << r.json()) .then(res => { if (res.success) { eventModal.hide(); calendar.refetchEvents(); Swal.fire({icon: 'success', title: 'تم الحفظ', showConfirmButton: false, timer: 1500}); } else { Swal.fire({icon: 'error', title: 'خطأ', text: res.error || 'حدث خطأ أثناء الحفظ'}); } }); EOT; $replace2 = << { if (!r.ok) throw new Error("Network Error"); return r.json(); }) .then(res => { if (res.success) { eventModal.hide(); calendar.refetchEvents(); Swal.fire({icon: 'success', title: 'تم الحفظ', showConfirmButton: false, timer: 1500}); } else { Swal.fire({icon: 'error', title: 'خطأ', text: res.error || 'حدث خطأ أثناء الحفظ'}); } }) .catch(err => { console.error(err); Swal.fire({icon: 'error', title: 'حدث خطأ غير متوقع', text: 'ربما تكون جلسة تسجيل الدخول قد انتهت أو لا توجد صلاحيات كافية، يرجى تحديث الصفحة والمحاولة مجدداً.'}); }); EOT; $content = str_replace($search2, $replace2, $content); file_put_contents('events.php', $content); echo "Patched events.php successfully.\n";