query('SELECT 1 FROM roles LIMIT 1'); } catch (Exception $e) { ob_start(); require_once __DIR__ . '/db/migrations/migrate_roles.php'; ob_end_clean(); header('Location: ' . app_url('admin.php', ['page' => 'roles'])); exit; } require_permission('roles', 'view'); $pages = [ 'dashboard' => t('Dashboard', 'لوحة التحكم'), 'classes' => t('Classes', 'الصفوف'), 'subjects' => t('Subjects', 'المواد'), 'teachers' => t('Teachers', 'المعلمين'), 'courses' => t('Courses', 'الدورات'), 'assignments' => t('Assignments', 'الواجبات'), 'students' => t('Students', 'الطلاب'), 'plans' => t('Plans', 'الخطط'), 'users' => t('Platform Users', 'مستخدمي المنصة'), 'roles' => t('Role Groups', 'مجموعات الصلاحيات'), 'integrations' => t('Integrations', 'عمليات الربط'), 'landing' => t('Landing Page', 'الصفحة الرئيسية'), 'profile' => t('Platform Profile', 'ملف المنصة'), ]; $action = $_GET['action'] ?? 'list'; $id = (int)($_GET['id'] ?? 0); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $post_action = $_POST['action'] ?? $action; $post_id = (int)($_POST['id'] ?? $id); if ($post_action === 'delete' && $post_id > 0) { require_permission('roles', 'delete'); // check if system role $stmt = db()->prepare("SELECT is_system FROM roles WHERE id = ?"); $stmt->execute([$post_id]); $role = $stmt->fetch(PDO::FETCH_ASSOC); if ($role && !$role['is_system']) { $stmt = db()->prepare("DELETE FROM roles WHERE id = ?"); $stmt->execute([$post_id]); } header('Location: ' . app_url('admin.php', ['page' => 'roles'])); exit; } if ($post_action === 'edit' || $post_action === 'add') { require_permission('roles', $post_action === 'add' ? 'add' : 'edit'); $name = trim($_POST['name'] ?? ''); $description = trim($_POST['description'] ?? ''); $perms = $_POST['perms'] ?? []; // e.g. ['classes' => ['view'=>1, 'add'=>1]] if ($post_action === 'edit' && $post_id > 0) { $stmt = db()->prepare("UPDATE roles SET name=?, description=? WHERE id=?"); $stmt->execute([$name, $description, $post_id]); $role_id = $post_id; } else { $stmt = db()->prepare("INSERT INTO roles (name, description) VALUES (?, ?)"); $stmt->execute([$name, $description]); $role_id = db()->lastInsertId(); } // update permissions if ($role_id) { $stmt = db()->prepare("DELETE FROM role_permissions WHERE role_id = ?"); $stmt->execute([$role_id]); $stmt = db()->prepare("INSERT INTO role_permissions (role_id, page, can_view, can_add, can_edit, can_delete) VALUES (?, ?, ?, ?, ?, ?)"); foreach ($pages as $p => $pname) { if (isset($perms[$p])) { $v = !empty($perms[$p]['view']) ? 1 : 0; $a = !empty($perms[$p]['add']) ? 1 : 0; $e = !empty($perms[$p]['edit']) ? 1 : 0; $d = !empty($perms[$p]['delete']) ? 1 : 0; if ($v || $a || $e || $d) { $stmt->execute([$role_id, $p, $v, $a, $e, $d]); } } } } header('Location: ' . app_url('admin.php', ['page' => 'roles'])); exit; } } $roles = db()->query("SELECT * FROM roles ORDER BY id DESC")->fetchAll(PDO::FETCH_ASSOC); // Fetch permissions for all roles $stmt = db()->query("SELECT * FROM role_permissions"); $all_perms = $stmt->fetchAll(PDO::FETCH_ASSOC); $role_perms = []; foreach ($all_perms as $p) { $role_perms[$p['role_id']][$p['page']] = $p; } // Merge permissions into roles for JS foreach ($roles as &$r) { $r['permissions'] = $role_perms[$r['id']] ?? []; } unset($r); ?>
= h(t('Manage roles and their page permissions.', 'إدارة الأدوار وصلاحيات الصفحات الخاصة بها.')) ?>
| = h(t('Role Name', 'اسم الدور')) ?> | = h(t('Description', 'الوصف')) ?> | = h(t('Actions', 'الإجراءات')) ?> |
|---|---|---|
| = h($r['name']) ?> = h(t('System', 'نظام')) ?> | = h($r['description']) ?> |
|
| = h(t('No roles found.', 'لا توجد أدوار.')) ?> | ||