ليس لديك صلاحية لعرض هذه الصفحة."; require_once 'includes/footer.php'; exit; } $can_add = canAdd('events'); $can_edit = canEdit('events'); $can_delete = canDelete('events'); // Auto-create table if missing (useful for deployed environments that haven't run migrations) try { db()->query("SELECT 1 FROM events LIMIT 1"); } catch (Exception $e) { try { $sql = file_get_contents(__DIR__ . '/db/migrations/037_add_events_module.sql'); if ($sql) { db()->exec($sql); } } catch (Exception $e2) { // Silently ignore, let the AJAX or save functions report the error } } // Handle AJAX requests if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['ajax'])) { ob_clean(); header('Content-Type: application/json'); $action = $_POST['action'] ?? ''; if ($action === 'fetch') { $stmt = db()->query("SELECT id, title, description, event_date, start_time, end_time, location FROM events"); $events = $stmt->fetchAll(); $fc_events = []; foreach($events as $e) { $start = $e['event_date']; if ($e['start_time']) $start .= 'T' . $e['start_time']; $end = $e['event_date']; if ($e['end_time']) $end .= 'T' . $e['end_time']; $fc_events[] = [ 'id' => $e['id'], 'title' => $e['title'], 'start' => $start, 'end' => $end, 'extendedProps' => [ 'description' => $e['description'] ?? '', 'location' => $e['location'] ?? '', 'start_time' => $e['start_time'] ? substr($e['start_time'], 0, 5) : '', 'end_time' => $e['end_time'] ? substr($e['end_time'], 0, 5) : '' ] ]; } echo json_encode($fc_events); exit; } if ($action === 'save') { $id = $_POST['id'] ?? 0; $title = $_POST['title'] ?? ''; $date = $_POST['event_date'] ?? ''; $start_time = !empty($_POST['start_time']) ? $_POST['start_time'] : null; $end_time = !empty($_POST['end_time']) ? $_POST['end_time'] : null; $location = $_POST['location'] ?? ''; $description = $_POST['description'] ?? ''; if (!$title || !$date) { echo json_encode(['success' => 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; } if ($action === 'delete') { if (!$can_delete) { echo json_encode(['success' => false, 'error' => 'لا تملك صلاحية الحذف.']); exit; } try { $id = $_POST['id'] ?? 0; db()->prepare("DELETE FROM events WHERE id=?")->execute([$id]); echo json_encode(['success' => true]); } catch (Exception $e) { echo json_encode(['success' => false, 'error' => 'خطأ قاعدة البيانات: ' . $e->getMessage()]); } exit; } } ?>