184 lines
7.8 KiB
PHP
184 lines
7.8 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
// Authentication check
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['role_name'] !== 'admin') {
|
|
header('Location: login.php');
|
|
exit();
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Create roles table if it doesn't exist
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS roles (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
name VARCHAR(255) NOT NULL UNIQUE
|
|
)");
|
|
|
|
// Function to log activity
|
|
function log_activity($user_id, $action) {
|
|
global $pdo;
|
|
$stmt = $pdo->prepare("INSERT INTO activities (user_id, action) VALUES (:user_id, :action)");
|
|
$stmt->execute(['user_id' => $user_id, 'action' => $action]);
|
|
}
|
|
|
|
// Handle Create and Update
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_POST['add_role'])) {
|
|
$name = trim($_POST['role_name']);
|
|
if (!empty($name)) {
|
|
$stmt = $pdo->prepare("INSERT INTO roles (name) VALUES (:name)");
|
|
$stmt->execute(['name' => $name]);
|
|
$new_role_id = $pdo->lastInsertId();
|
|
log_activity($_SESSION['user_id'], "Created role {$name} (ID: {$new_role_id})");
|
|
}
|
|
} elseif (isset($_POST['update_role'])) {
|
|
$id = $_POST['role_id'];
|
|
$name = trim($_POST['role_name']);
|
|
if (!empty($name) && !empty($id)) {
|
|
$stmt = $pdo->prepare("UPDATE roles SET name = :name WHERE id = :id");
|
|
$stmt->execute(['name' => $name, 'id' => $id]);
|
|
log_activity($_SESSION['user_id'], "Updated role {$name} (ID: {$id})");
|
|
}
|
|
}
|
|
header("Location: roles.php");
|
|
exit;
|
|
}
|
|
|
|
// Handle Delete
|
|
if (isset($_GET['delete_id'])) {
|
|
$id = $_GET['delete_id'];
|
|
// Get role name for logging
|
|
$stmt = $pdo->prepare("SELECT name FROM roles WHERE id = :id");
|
|
$stmt->execute(['id' => $id]);
|
|
$deleted_role = $stmt->fetch();
|
|
|
|
$stmt = $pdo->prepare("DELETE FROM roles WHERE id = :id");
|
|
$stmt->execute(['id' => $id]);
|
|
|
|
if ($deleted_role) {
|
|
log_activity($_SESSION['user_id'], "Deleted role {$deleted_role['name']} (ID: {$id})");
|
|
}
|
|
header("Location: roles.php");
|
|
exit;
|
|
}
|
|
|
|
// Fetch all roles
|
|
$roles = $pdo->query("SELECT * FROM roles ORDER BY id DESC")->fetchAll();
|
|
|
|
// Fetch role for editing
|
|
$editing_role = null;
|
|
if (isset($_GET['edit_id'])) {
|
|
$id = $_GET['edit_id'];
|
|
$stmt = $pdo->prepare("SELECT * FROM roles WHERE id = :id");
|
|
$stmt->execute(['id' => $id]);
|
|
$editing_role = $stmt->fetch();
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database error: " . $e->getMessage());
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="fa" dir="rtl">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>مدیریت نقشها</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
|
|
<link href="https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
<script src="https://unpkg.com/feather-icons"></script>
|
|
</head>
|
|
<body>
|
|
|
|
<header class="p-3 mb-3 border-bottom sticky-top bg-light">
|
|
<div class="container">
|
|
<div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start">
|
|
<a href="/" class="d-flex align-items-center mb-2 mb-lg-0 text-dark text-decoration-none">
|
|
<span class="fs-4">مدیریت مدرسه</span>
|
|
</a>
|
|
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
|
|
<li><a href="index.php" class="nav-link px-2 link-dark">خانه</a></li>
|
|
<li><a href="roles.php" class="nav-link px-2 link-secondary">مدیریت نقشها</a></li>
|
|
<li><a href="users.php" class="nav-link px-2 link-dark">مدیریت کاربران</a></li>
|
|
<li><a href="activities.php" class="nav-link px-2 link-dark">Activities</a></li>
|
|
<li><a href="exams.php" class="nav-link px-2 link-dark">Exams</a></li>
|
|
<li><a href="attendance.php" class="nav-link px-2 link-dark">Attendance</a></li>
|
|
</ul>
|
|
<div class="text-end">
|
|
<?php if (isset($_SESSION['user_id'])): ?>
|
|
<a href="logout.php" class="btn btn-outline-primary">خروج</a>
|
|
<?php else: ?>
|
|
<a href="login.php" class="btn btn-primary">ورود</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container py-5">
|
|
<div class="row">
|
|
<div class="col-md-8">
|
|
<h2>فهرست نقشها</h2>
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>#</th>
|
|
<th>نام نقش</th>
|
|
<th>عملیات</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($roles as $role): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($role['id']); ?></td>
|
|
<td><?php echo htmlspecialchars($role['name']); ?></td>
|
|
<td>
|
|
<a href="roles.php?edit_id=<?php echo $role['id']; ?>" class="btn btn-sm btn-outline-primary">ویرایش</a>
|
|
<a href="roles.php?delete_id=<?php echo $role['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('آیا مطمئن هستید؟')">حذف</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<h2><?php echo $editing_role ? 'ویرایش نقش' : 'افزودن نقش جدید'; ?></h2>
|
|
<form method="POST" action="roles.php">
|
|
<?php if ($editing_role): ?>
|
|
<input type="hidden" name="role_id" value="<?php echo $editing_role['id']; ?>">
|
|
<?php endif; ?>
|
|
<div class="mb-3">
|
|
<label for="role_name" class="form-label">نام نقش</label>
|
|
<input type="text" class="form-control" id="role_name" name="role_name" value="<?php echo htmlspecialchars($editing_role['name'] ?? ''); ?>" required>
|
|
</div>
|
|
<?php if ($editing_role): ?>
|
|
<button type="submit" name="update_role" class="btn btn-primary w-100">بهروزرسانی</button>
|
|
<a href="roles.php" class="btn btn-secondary w-100 mt-2">انصراف</a>
|
|
<?php else: ?>
|
|
<button type="submit" name="add_role" class="btn btn-success w-100">افزودن</button>
|
|
<?php endif; ?>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="footer mt-auto py-3 bg-light">
|
|
<div class="container text-center">
|
|
<span class="text-muted">© 2025 سیستم مدیریت مدرسه</span>
|
|
</div>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
feather.replace()
|
|
</script>
|
|
</body>
|
|
</html>
|