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

198 lines
7.9 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;
// Fetch related items for dropdowns
$institutions = $pdo->query('SELECT id, name FROM institutions ORDER BY name')->fetchAll();
$programs = $pdo->query('SELECT id, name FROM programs ORDER BY name')->fetchAll();
$courses = $pdo->query('SELECT id, name FROM courses ORDER BY name')->fetchAll();
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$statement = trim($_POST['statement']);
$rel_type = $_POST['rel_type'];
$rel_id = $_POST['rel_id'];
if (isset($_POST['add_statement'])) {
if (!empty($statement) && !empty($rel_type) && !empty($rel_id)) {
$stmt = $pdo->prepare('INSERT INTO mission_statements (statement, rel_type, rel_id) VALUES (?, ?, ?)');
$stmt->execute([$statement, $rel_type, $rel_id]);
}
} elseif (isset($_POST['update_statement'])) {
if (!empty($statement) && !empty($rel_type) && !empty($rel_id) && !empty($id)) {
$stmt = $pdo->prepare('UPDATE mission_statements SET statement = ?, rel_type = ?, rel_id = ? WHERE id = ?');
$stmt->execute([$statement, $rel_type, $rel_id, $id]);
}
header('Location: admin_mission_statements.php');
exit;
} elseif (isset($_POST['delete_statement'])) {
if (!empty($id)) {
$stmt = $pdo->prepare('DELETE FROM mission_statements WHERE id = ?');
$stmt->execute([$id]);
}
header('Location: admin_mission_statements.php');
exit;
}
}
?>
<h2>Assessment Planning: Mission Statements</h2>
<p><a href="admin.php"> &larr; Back to Admin Dashboard</a></p>
<?php if ($action === 'edit' && $id): ?>
<?php
$stmt = $pdo->prepare('SELECT * FROM mission_statements WHERE id = ?');
$stmt->execute([$id]);
$statement = $stmt->fetch();
?>
<h3>Edit Mission Statement</h3>
<form action="admin_mission_statements.php?action=edit&id=<?php echo $id; ?>" method="post">
<div class="mb-3">
<label for="statement" class="form-label">Mission Statement</label>
<textarea class="form-control" id="statement" name="statement" rows="3" required><?php echo htmlspecialchars($statement['statement']); ?></textarea>
</div>
<div class="mb-3">
<label for="rel_type" class="form-label">Link to:</label>
<select class="form-select" name="rel_type" id="rel_type" required>
<option value="institution" <?php echo ($statement['rel_type'] == 'institution') ? 'selected' : ''; ?>>Institution</option>
<option value="program" <?php echo ($statement['rel_type'] == 'program') ? 'selected' : ''; ?>>Program</option>
<option value="course" <?php echo ($statement['rel_type'] == 'course') ? 'selected' : ''; ?>>Course</option>
</select>
</div>
<div class="mb-3">
<label for="rel_id" class="form-label">Specific Item</label>
<select class="form-select" name="rel_id" id="rel_id" required>
<!-- Options will be populated by JavaScript based on the selection above -->
</select>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<button type="submit" name="update_statement" class="btn btn-primary">Update</button>
<a href="admin_mission_statements.php" class="btn btn-secondary">Cancel</a>
</form>
<?php else: ?>
<h3>Add New Mission Statement</h3>
<form action="admin_mission_statements.php" method="post">
<div class="mb-3">
<label for="statement" class="form-label">Mission Statement</label>
<textarea class="form-control" id="statement" name="statement" rows="3" required></textarea>
</div>
<div class="mb-3">
<label for="rel_type" class="form-label">Link to:</label>
<select class="form-select" name="rel_type" id="rel_type_add" required>
<option value="">Select Type</option>
<option value="institution">Institution</option>
<option value="program">Program</option>
<option value="course">Course</option>
</select>
</div>
<div class="mb-3">
<label for="rel_id" class="form-label">Specific Item</label>
<select class="form-select" name="rel_id" id="rel_id_add" required>
<!-- Options will be populated by JavaScript -->
</select>
</div>
<button type="submit" name="add_statement" class="btn btn-primary">Add Mission Statement</button>
</form>
<hr>
<h3>Existing Mission Statements</h3>
<table class="table table-striped">
<thead>
<tr>
<th>Statement</th>
<th>Linked To</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $pdo->query('SELECT * FROM mission_statements ORDER BY id DESC');
while ($row = $stmt->fetch()) {
$rel_name = '';
if ($row['rel_type'] == 'institution') {
$rel_stmt = $pdo->prepare("SELECT name FROM institutions WHERE id = ?");
$rel_stmt->execute([$row['rel_id']]);
$rel_name = $rel_stmt->fetchColumn();
} elseif ($row['rel_type'] == 'program') {
$rel_stmt = $pdo->prepare("SELECT name FROM programs WHERE id = ?");
$rel_stmt->execute([$row['rel_id']]);
$rel_name = $rel_stmt->fetchColumn();
} elseif ($row['rel_type'] == 'course') {
$rel_stmt = $pdo->prepare("SELECT name FROM courses WHERE id = ?");
$rel_stmt->execute([$row['rel_id']]);
$rel_name = $rel_stmt->fetchColumn();
}
echo "<tr>";
echo "<td>" . htmlspecialchars($row['statement']) . "</td>";
echo "<td>" . ucfirst($row['rel_type']) . ': ' . htmlspecialchars($rel_name) . "</td>";
echo '<td>
<a href="admin_mission_statements.php?action=edit&id=' . $row['id'] . '" class="btn btn-sm btn-outline-primary">Edit</a>
<a href="admin_mission_statements.php?action=delete&id=' . $row['id'] . '" class="btn btn-sm btn-outline-danger">Delete</a>
</td>';
echo "</tr>";
}
?>
</tbody>
</table>
<?php endif; ?>
<script>
const institutions = <?php echo json_encode($institutions); ?>;
const programs = <?php echo json_encode($programs); ?>;
const courses = <?php echo json_encode($courses); ?>;
function populateSelect(typeSelectId, itemSelectId) {
const typeSelect = document.getElementById(typeSelectId);
const itemSelect = document.getElementById(itemSelectId);
typeSelect.addEventListener('change', function() {
itemSelect.innerHTML = '';
const selectedType = this.value;
let items = [];
if (selectedType === 'institution') {
items = institutions;
} else if (selectedType === 'program') {
items = programs;
} else if (selectedType === 'course') {
items = courses;
}
items.forEach(function(item) {
const option = document.createElement('option');
option.value = item.id;
option.textContent = item.name;
itemSelect.appendChild(option);
});
});
// Trigger change on page load for edit form
if (typeSelect.value) {
typeSelect.dispatchEvent(new Event('change'));
}
}
populateSelect('rel_type_add', 'rel_id_add');
populateSelect('rel_type', 'rel_id');
</script>
<?php include 'templates/footer.php'; ?>