· 4 months ago · May 14, 2025, 04:15 AM
1<?php
2if (session_status() == PHP_SESSION_NONE) {
3 session_start();
4}
5
6# Prevent access if user is not logged in
7if (!isset($_SESSION['user_id'])) {
8 header("Location: index.php");
9 exit();
10}
11
12$user_id = $_SESSION['user_id'];
13$role = $_SESSION['role'];
14
15require_once "../config/database.php";
16
17# Check if position name already exists
18function positionExists($db, $positions) {
19 try {
20 $stmt = $db->prepare("SELECT id FROM positions_tbl WHERE positions = :positions");
21 $stmt->bindParam(':positions', $positions, PDO::PARAM_STR);
22 $stmt->execute();
23 return $stmt->rowCount() > 0;
24 } catch (PDOException $e) {
25 error_log("Error: " . $e->getMessage());
26 return false;
27 }
28}
29
30# ===> Creating a new position
31if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['positions'])) {
32 $positions = filter_var(trim($_POST['positions']), FILTER_SANITIZE_STRING);
33
34 if (empty($positions)) {
35 $_SESSION['error'] = "Position name cannot be empty.";
36 } else if (positionExists($db, $positions)) {
37 $_SESSION['error'] = "Position name already exists!";
38 } else {
39 try {
40 $status = 'enabled';
41 $stmt = $db->prepare("INSERT INTO positions_tbl (positions, status) VALUES (:positions, :status)");
42 $stmt->bindParam(':positions', $positions, PDO::PARAM_STR);
43 $stmt->bindParam(':status', $status, PDO::PARAM_STR);
44 $stmt->execute();
45 $_SESSION['success'] = "Position created successfully";
46 } catch (PDOException $e) {
47 error_log("Error: " . $e->getMessage());
48 $_SESSION['error'] = "Error creating position, please try again.";
49 }
50 }
51}
52
53# ===> Toggling the status of a position
54if (isset($_POST['toggle_status'])) {
55 $position_id = $_POST['position_id'];
56
57 try {
58 $stmt = $db->prepare("SELECT status FROM positions_tbl WHERE id = :position_id");
59 $stmt->bindParam(':position_id', $position_id, PDO::PARAM_INT);
60 $stmt->execute();
61 $position = $stmt->fetch(PDO::FETCH_ASSOC);
62
63 if ($position) {
64 $currentStatus = $position['status'];
65 $newStatus = $currentStatus == 'enabled' ? 'disabled' : 'enabled';
66 $stmtUpdate = $db->prepare("UPDATE positions_tbl SET status = :newStatus WHERE id = :position_id");
67 $stmtUpdate->bindParam(':newStatus', $newStatus, PDO::PARAM_STR);
68 $stmtUpdate->bindParam(':position_id', $position_id, PDO::PARAM_INT);
69 $stmtUpdate->execute();
70 $_SESSION['success'] = "Position status updated successfully";
71 } else {
72 $_SESSION['error'] = "Position not found";
73 }
74 } catch (PDOException $e) {
75 $_SESSION['error'] = "Error updating position status: " . $e->getMessage();
76 }
77}
78
79# ===> Delete position
80if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['position_id']) && isset($_POST['delete_position_by_id'])) {
81 $position_id = $_POST['position_id'];
82
83 try {
84 $stmt = $db->prepare("DELETE FROM positions_tbl WHERE id = :position_id");
85 $stmt->bindParam(':position_id', $position_id, PDO::PARAM_INT);
86 $stmt->execute();
87 $_SESSION['success'] = "Position deleted successfully";
88 } catch (PDOException $e) {
89 $_SESSION['error'] = "Error deleting position: " . $e->getMessage();
90 }
91}
92
93try {
94 $stmt = $db->prepare("SELECT * FROM positions_tbl");
95 $stmt->execute();
96 $positions = $stmt->fetchAll(PDO::FETCH_ASSOC);
97} catch (PDOException $e) {
98 $_SESSION['error'] = "Error fetching positions: " . $e->getMessage();
99}
100?>
101
102<!DOCTYPE html>
103<html lang="en">
104<head>
105 <meta charset="UTF-8">
106 <meta name="viewport" content="width=device-width, initial-scale=1.0">
107 <title>Create Positions</title>
108 <link rel="stylesheet" href="../assets/css/bootstrap.min.css">
109 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
110</head>
111<body>
112
113<!-- Toast Notification -->
114<?php if (isset($_SESSION['success']) || isset($_SESSION['error'])): ?>
115<div aria-live="polite" aria-atomic="true" class="position-fixed top-0 end-0 p-3" style="z-index: 1050;">
116 <div id="toastNotif" class="toast align-items-center text-white <?php echo isset($_SESSION['success']) ? 'bg-success' : 'bg-danger'; ?> border-0 show" role="alert">
117 <div class="d-flex">
118 <div class="toast-body">
119 <?php
120 echo isset($_SESSION['success']) ? $_SESSION['success'] : $_SESSION['error'];
121 unset($_SESSION['success'], $_SESSION['error']);
122 ?>
123 </div>
124 <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
125 </div>
126 </div>
127</div>
128<?php endif; ?>
129
130<!-- Create Positions Modal -->
131<div class="modal fade" id="createPositionsModal" tabindex="-1" aria-labelledby="createPositionsModalLabel" aria-hidden="true">
132 <div class="modal-dialog modal-dialog-centered">
133 <div class="modal-content">
134 <div class="modal-header bg-dark text-white">
135 <h5 class="modal-title fw-bolder" id="createPositionsModalLabel">Create Positions</h5>
136 <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" style="background-color: #fff; display: flex; align-items: center; justify-content: center; text-align: center;">❌</button>
137 </div>
138 <div class="modal-body">
139 <form action="" method="POST">
140 <div class="mb-3">
141 <label class="form-label">Position Name</label>
142 <input type="text" name="positions" class="form-control form-control-sm" required />
143 </div>
144 <div class="mb-3 text-center">
145 <button type="submit" class="btn btn-dark btn-sm w-100">Create</button>
146 </div>
147 </form>
148 </div>
149 </div>
150 </div>
151</div>
152
153<div class="container mt-2">
154 <div class="card shadow border-0">
155 <div class="card-body">
156
157 <div class="d-flex justify-content-end align-items-end mt-1 mb-1">
158 <button class="btn btn-dark" data-bs-toggle="modal" data-bs-target="#createPositionsModal"><i class="fas fa-plus"></i> New</button>
159 </div>
160 <!-- Search Box -->
161 <div class="mb-3">
162 <input type="text" id="searchBox" class="form-control w-25" placeholder="Search positions...">
163 </div>
164 <div class="table-responsive">
165 <table class="table">
166 <thead>
167 <tr>
168 <th style="display: none;">ID</th>
169 <th>Positions</th>
170 <th>Status</th>
171 <th>Actions</th>
172 </tr>
173 </thead>
174 <tbody id="positionsTable">
175 <?php foreach ($positions as $position): ?>
176 <tr>
177 <td style="display: none;"><?php echo htmlspecialchars($position['id']); ?></td>
178 <td><?php echo htmlspecialchars($position['positions']); ?></td>
179 <td>
180 <?php if ($position['status'] === 'enabled'): ?>
181 <span class="badge rounded-pill badge bg-primary">Enabled</span>
182 <?php else: ?>
183 <span class="badge rounded-pill badge bg-danger">Disabled</span>
184 <?php endif; ?>
185 </td>
186 <td>
187 <div class="d-flex align-items-center justify-content-center gap-2">
188 <form method="POST" action="">
189 <input type="hidden" name="position_id" value="<?php echo htmlspecialchars($position['id']); ?>">
190 <?php if ($position['status'] === 'enabled'): ?>
191 <button type="submit" name="toggle_status" class="btn btn-danger btn-sm"><i class="fa fa-ban" aria-hidden="true"></i> Disable</button>
192 <?php else: ?>
193 <button type="submit" name="toggle_status" class="btn btn-primary btn-sm"><i class="fa fa-check" aria-hidden="true"></i> Enable</button>
194 <?php endif; ?>
195 </form>
196 <form method="POST">
197 <input type="hidden" name="position_id" value="<?php echo htmlspecialchars($position['id']); ?>">
198 <button type="submit" name="delete_position_by_id" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i> Delete</button>
199 </form>
200 </div>
201 </td>
202 </tr>
203 <?php endforeach; ?>
204 </tbody>
205 </table>
206 </div>
207 </div>
208 </div>
209</div>
210
211<!-- Search Script -->
212<script>
213document.getElementById("searchBox").addEventListener("keyup", function() {
214 let input = this.value.toLowerCase();
215 let rows = document.querySelectorAll("#positionsTable tr");
216
217 rows.forEach(row => {
218 let text = row.textContent.toLowerCase();
219 row.style.display = text.includes(input) ? "" : "none";
220 });
221});
222</script>
223<script src="../assets/js/bootstrap.bundle.min.js"></script>
224<script src="../assets/js/notif.js"></script>
225</body>
226</html>