35149-vm/admin_institutions.php
Flatlogic Bot 7a3409404c v01
2025-10-23 20:44:05 +00:00

118 lines
4.1 KiB
PHP

<?php
include 'templates/header.php';
require_once 'db/config.php';
// Admin-only page
if (!isset($_SESSION['role_id']) || $_SESSION['role_id'] != 1) {
header('Location: dashboard.php');
exit;
}
$pdo = db();
$action = $_GET['action'] ?? 'list';
$id = $_GET['id'] ?? null;
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['add_institution'])) {
$name = trim($_POST['name']);
if (!empty($name)) {
$stmt = $pdo->prepare('INSERT INTO institutions (name) VALUES (?)');
$stmt->execute([$name]);
}
} elseif (isset($_POST['update_institution'])) {
$name = trim($_POST['name']);
if (!empty($name) && !empty($id)) {
$stmt = $pdo->prepare('UPDATE institutions SET name = ? WHERE id = ?');
$stmt->execute([$name, $id]);
}
header('Location: admin_institutions.php');
exit;
} elseif (isset($_POST['delete_institution'])) {
if (!empty($id)) {
$stmt = $pdo->prepare('DELETE FROM institutions WHERE id = ?');
$stmt->execute([$id]);
}
header('Location: admin_institutions.php');
exit;
}
}
?>
<h2>Assessment Planning: Institutions</h2>
<?php if ($action === 'edit' && $id): ?>
<?php
$stmt = $pdo->prepare('SELECT * FROM institutions WHERE id = ?');
$stmt->execute([$id]);
$institution = $stmt->fetch();
?>
<h3>Edit Institution</h3>
<form action="admin_institutions.php?action=edit&id=<?php echo $id; ?>" method="post">
<div class="mb-3">
<label for="name" class="form-label">Institution Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($institution['name']); ?>" required>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<button type="submit" name="update_institution" class="btn btn-primary">Update</button>
<a href="admin_institutions.php" class="btn btn-secondary">Cancel</a>
</form>
<?php elseif ($action === 'delete' && $id): ?>
<?php
$stmt = $pdo->prepare('SELECT * FROM institutions WHERE id = ?');
$stmt->execute([$id]);
$institution = $stmt->fetch();
?>
<h3>Delete Institution</h3>
<p>Are you sure you want to delete the institution "<?php echo htmlspecialchars($institution['name']); ?>"?</p>
<form action="admin_institutions.php?action=delete&id=<?php echo $id; ?>" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<button type="submit" name="delete_institution" class="btn btn-danger">Delete</button>
<a href="admin_institutions.php" class="btn btn-secondary">Cancel</a>
</form>
<?php else: ?>
<h3>Add New Institution</h3>
<form action="admin_institutions.php" method="post">
<div class="mb-3">
<label for="name" class="form-label">Institution Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<button type="submit" name="add_institution" class="btn btn-primary">Add Institution</button>
</form>
<hr>
<h3>Existing Institutions</h3>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $pdo->query('SELECT * FROM institutions ORDER BY id');
while ($row = $stmt->fetch()) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row['id']) . "</td>";
echo "<td>" . htmlspecialchars($row['name']) . "</td>";
echo '<td>
<a href="admin_institutions.php?action=edit&id=' . $row['id'] . '" class="btn btn-sm btn-outline-primary">Edit</a>
<a href="admin_institutions.php?action=delete&id=' . $row['id'] . '" class="btn btn-sm btn-outline-danger">Delete</a>
</td>';
echo "</tr>";
}
?>
</tbody>
</table>
<?php endif; ?>
<?php include 'templates/footer.php'; ?>