CrestRoad/website/admin/sessions.php

164 lines
7.3 KiB
PHP

<?php
require_once __DIR__ . '/../includes/auth.php';
require_login();
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = trim($_POST['title'] ?? '');
$description = trim($_POST['description'] ?? '');
$tag = trim($_POST['tag'] ?? '');
$cta_label = trim($_POST['cta_label'] ?? 'Register');
$start_date = $_POST['start_date'] ?? null;
$editId = isset($_POST['id']) ? (int) $_POST['id'] : null;
if ($title === '' || $description === '') {
$error = 'Title and description are required.';
} else {
if ($editId) {
update_session($editId, compact('title', 'description', 'tag', 'cta_label', 'start_date'));
header('Location: sessions.php?updated=1');
} else {
add_session(compact('title', 'description', 'tag', 'cta_label', 'start_date'));
header('Location: sessions.php?success=1');
}
exit;
}
}
if (isset($_GET['delete'])) {
$id = (int) $_GET['delete'];
delete_session($id);
header('Location: sessions.php?deleted=1');
exit;
}
$sessions = get_sessions();
$editing = null;
if (isset($_GET['edit'])) {
$editId = (int) $_GET['edit'];
foreach ($sessions as $row) {
if ((int) $row['id'] === $editId) {
$editing = $row;
break;
}
}
}
$assetPathPrefix = '../';
$pageTitle = 'Manage Sessions';
$currentPage = 'news';
$extraStyles = <<<CSS
<style>
body { background: #f6faf7; }
.admin-shell { padding: 40px 0; }
.table { width: 100%; border-collapse: collapse; margin-top: 20px; }
.table th, .table td { text-align: left; padding: 12px 10px; border-bottom: 1px solid #e1ede7; }
.table th { color: #0f4333; }
.badge { display: inline-block; padding: 6px 10px; border-radius: 10px; background: #e8f5ef; color: #0f4333; font-weight: 700; font-size: 13px; }
.inline-btn { color: #b9342f; font-weight: 700; }
form textarea { min-height: 120px; resize: vertical; }
form input, form textarea { width: 100%; padding: 12px 14px; border-radius: 10px; border: 1px solid #d8e7e0; font-size: 15px; margin-top: 8px; }
form label { font-weight: 700; color: #0f4333; display: block; margin-top: 12px; }
.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
</style>
CSS;
require __DIR__ . '/../includes/admin-header.php';
?>
<main class="admin-shell">
<div class="container">
<div class="topbar">
<div>
<p class="eyebrow">Upcoming Sessions</p>
<h2>Manage events and workshops</h2>
<p class="body">Add session details to show on the public site.</p>
</div>
<button class="btn primary" onclick="document.getElementById('session-form').scrollIntoView({behavior:'smooth'})">Add new</button>
</div>
<div class="admin-nav">
<a href="/admin/tutorials.php">Tutorials</a>
<a href="/admin/sessions.php">Upcoming Sessions</a>
<a href="/admin/contacts.php">Contact Entries</a>
<a href="/admin/newsletter.php">Newsletter</a>
<a href="/admin/admins.php">Admins</a>
<a href="/admin/logout.php">Logout (<?php echo htmlspecialchars(current_admin_email() ?? ''); ?>)</a>
</div>
<?php if (isset($_GET['success'])): ?>
<div class="alert good">Session added successfully.</div>
<?php endif; ?>
<?php if (isset($_GET['deleted'])): ?>
<div class="alert good">Session deleted.</div>
<?php endif; ?>
<?php if (isset($_GET['updated'])): ?>
<div class="alert good">Session updated.</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert bad"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<?php if (empty($sessions)): ?>
<p class="body muted">No sessions yet. Add your first event below.</p>
<?php else: ?>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Tag</th>
<th>Start Date</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($sessions as $session): ?>
<tr>
<td><?php echo htmlspecialchars($session['title']); ?></td>
<td><?php echo htmlspecialchars($session['description']); ?></td>
<td><?php echo htmlspecialchars($session['tag']); ?></td>
<td><?php echo htmlspecialchars($session['start_date'] ?? ''); ?></td>
<td>
<a class="inline-btn" href="?edit=<?php echo (int)$session['id']; ?>">Edit</a> |
<a class="inline-btn" href="?delete=<?php echo (int)$session['id']; ?>" onclick="return confirm('Delete this session?')">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<section id="session-form" style="margin-top:32px;">
<div class="card">
<h3><?php echo $editing ? 'Edit Session' : 'Add Session'; ?></h3>
<form method="post">
<?php if ($editing): ?>
<input type="hidden" name="id" value="<?php echo (int)$editing['id']; ?>">
<?php endif; ?>
<label for="title">Title</label>
<input type="text" id="title" name="title" required placeholder="Session title" value="<?php echo htmlspecialchars($editing['title'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
<label for="description">Description</label>
<textarea id="description" name="description" required placeholder="Brief description"><?php echo htmlspecialchars($editing['description'] ?? '', ENT_QUOTES, 'UTF-8'); ?></textarea>
<label for="tag">Tag</label>
<input type="text" id="tag" name="tag" placeholder="e.g., New, Upcoming" value="<?php echo htmlspecialchars($editing['tag'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
<label for="cta_label">CTA Label</label>
<input type="text" id="cta_label" name="cta_label" placeholder="Register" value="<?php echo htmlspecialchars($editing['cta_label'] ?? 'Register', ENT_QUOTES, 'UTF-8'); ?>">
<label for="start_date">Start Date</label>
<input type="date" id="start_date" name="start_date" value="<?php echo htmlspecialchars($editing['start_date'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
<div style="margin-top:16px;">
<button type="submit" class="btn primary"><?php echo $editing ? 'Update session' : 'Save session'; ?></button>
<a class="text-link" href="/news-events.php" style="margin-left:10px;">View live</a>
</div>
</form>
</div>
</section>
</div>
</main>
<?php require __DIR__ . '/../includes/admin-footer.php'; ?>