Files
theoldfitz/admin/index.php
T
David DyerandClaude Sonnet 5 40f4c102b4 Add mobile nav toggle, responsive admin tables, and menu reordering
- Public site: replace the always-hidden mobile nav with a hamburger
  toggle (index.php, menu.php, main.js, styles.css)
- Admin: render the Menu/Announcements/Users tables as labeled cards
  below ~760px instead of squeezing descriptions into unreadable
  single-word-per-line columns
- Admin menu: replace the manual "Order" number (which allowed
  duplicates and never reflected in real time) with ↑/↓ buttons that
  reorder items and categories immediately and renumber sort_order
  sequentially on every move, self-healing any stale duplicates

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-31 22:12:24 -04:00

289 lines
13 KiB
PHP

<?php
require_once __DIR__ . '/../inc/auth.php';
require_once __DIR__ . '/../inc/announcements.php';
require_auth();
$user = get_logged_in_user();
$successMsg = '';
$errorMsg = '';
// Handle Actions (Save, Delete, Toggle)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!verify_csrf_token($_POST['csrf_token'] ?? '')) {
$errorMsg = 'Session expired or invalid security token. Please try again.';
} else {
$action = $_POST['action'] ?? '';
if ($action === 'save_announcement') {
$annId = !empty($_POST['announcement_id']) ? (int)$_POST['announcement_id'] : null;
save_announcement([
'is_active' => isset($_POST['is_active']) ? 1 : 0,
'type' => $_POST['type'] ?? 'holiday',
'headline' => $_POST['headline'] ?? '',
'message' => $_POST['message'] ?? '',
'start_date' => $_POST['start_date'] ?? null,
'end_date' => $_POST['end_date'] ?? null,
'show_banner' => isset($_POST['show_banner']) ? 1 : 0,
'show_in_hours' => isset($_POST['show_in_hours']) ? 1 : 0,
], $annId);
$successMsg = $annId ? 'Announcement updated successfully!' : 'New announcement created successfully!';
} elseif ($action === 'delete_announcement') {
$annId = (int)($_POST['announcement_id'] ?? 0);
if ($annId > 0) {
delete_announcement($annId);
$successMsg = 'Announcement deleted successfully.';
}
} elseif ($action === 'toggle_active') {
$annId = (int)($_POST['announcement_id'] ?? 0);
if ($annId > 0) {
toggle_announcement_active($annId);
$successMsg = 'Status updated.';
}
}
}
}
// Check if editing a specific announcement or creating new
$editingId = isset($_GET['edit']) ? (int)$_GET['edit'] : null;
$isCreatingNew = isset($_GET['new']);
$editItem = null;
if ($editingId) {
$editItem = get_announcement_by_id($editingId);
}
$announcements = get_all_announcements();
$today = date('Y-m-d');
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pub Management Dashboard | The Old Fitz</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@0,9..144,400;0,9..144,600;1,9..144,400&family=Karla:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../styles.css">
</head>
<body>
<nav class="admin-nav">
<div class="container admin-nav-inner">
<a href="index.php" class="admin-brand">
<img src="../assets/knot.png" alt="" class="knot">
<span>The Old Fitz Admin</span>
</a>
<div class="admin-nav-links">
<a href="index.php" class="active">Announcements</a>
<a href="menu.php">Menu</a>
<a href="users.php">Users</a>
</div>
<div class="admin-user">
<span>Logged in as <strong><?= htmlspecialchars($user['username']) ?></strong></span>
<a href="../" target="_blank" rel="noopener">View Public Site ↗</a>
<a href="logout.php">Sign Out</a>
</div>
</div>
</nav>
<main class="dashboard-layout">
<div class="container">
<div class="dash-header">
<p class="eyebrow">Content & Operations</p>
<h1>Site Announcements & Alerts</h1>
</div>
<?php if (!empty($successMsg)): ?>
<div class="alert-success" role="status"><?= htmlspecialchars($successMsg) ?></div>
<?php endif; ?>
<?php if (!empty($errorMsg)): ?>
<div class="alert-error" role="alert"><?= htmlspecialchars($errorMsg) ?></div>
<?php endif; ?>
<?php if ($isCreatingNew || $editItem): ?>
<!-- Announcement Editor Form (Create / Edit) -->
<form method="POST" action="index.php">
<input type="hidden" name="action" value="save_announcement">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars(get_csrf_token()) ?>">
<input type="hidden" name="announcement_id" value="<?= (int)($editItem['id'] ?? 0) ?>">
<div class="panel">
<div class="panel-head">
<h2>
<span><?= $editItem ? '✏️ Edit Announcement' : '✨ New Announcement' ?></span>
</h2>
<a href="index.php" class="btn btn-outline btn-sm">← Back to List</a>
</div>
<div class="form-grid">
<div class="form-group full-width">
<label class="checkbox-label checkbox-label-lg">
<input type="checkbox" name="is_active" value="1" <?= (!empty($editItem['is_active']) || $isCreatingNew) ? 'checked' : '' ?>>
<strong>Enable Announcement on Site</strong>
</label>
</div>
<div class="form-group">
<label for="headline">Notice Headline</label>
<input type="text" id="headline" name="headline" class="form-control"
value="<?= htmlspecialchars($editItem['headline'] ?? '') ?>"
placeholder="e.g. Labor Day Closure" required>
</div>
<div class="form-group">
<label for="type">Style / Category</label>
<select id="type" name="type" class="form-control">
<option value="holiday" <?= ($editItem['type'] ?? '') === 'holiday' ? 'selected' : '' ?>>Holiday / Special Schedule (Warm Brass & Green)</option>
<option value="special" <?= ($editItem['type'] ?? '') === 'special' ? 'selected' : '' ?>>Event / Live Music Announcement</option>
<option value="closure" <?= ($editItem['type'] ?? '') === 'closure' ? 'selected' : '' ?>>Urgent Closure / Weather Alert</option>
</select>
</div>
<div class="form-group full-width">
<label for="message">Notice Message</label>
<textarea id="message" name="message" class="form-control" required
placeholder="Write details of closure, dates, and when regular hours resume..."><?= htmlspecialchars($editItem['message'] ?? '') ?></textarea>
</div>
<div class="form-group">
<label for="start_date">Start Date (Optional Auto-Schedule)</label>
<input type="date" id="start_date" name="start_date" class="form-control"
value="<?= htmlspecialchars($editItem['start_date'] ?? '') ?>">
<span class="muted font-mono" style="font-size: 0.78rem;">Leave blank to show immediately while enabled.</span>
</div>
<div class="form-group">
<label for="end_date">End Date (Optional Expiration)</label>
<input type="date" id="end_date" name="end_date" class="form-control"
value="<?= htmlspecialchars($editItem['end_date'] ?? '') ?>">
<span class="muted font-mono" style="font-size: 0.78rem;">Auto-hides after 11:59 PM on this date.</span>
</div>
<div class="form-group full-width">
<label>Display Placements</label>
<div class="checkbox-group">
<label class="checkbox-label">
<input type="checkbox" name="show_banner" value="1" <?= (!isset($editItem) || !empty($editItem['show_banner'])) ? 'checked' : '' ?>>
Show as top alert banner across the site
</label>
<label class="checkbox-label">
<input type="checkbox" name="show_in_hours" value="1" <?= (!isset($editItem) || !empty($editItem['show_in_hours'])) ? 'checked' : '' ?>>
Highlight inside the "Hours &amp; Visit" card
</label>
</div>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save Announcement</button>
<a href="index.php" class="btn btn-outline">Cancel</a>
</div>
</div>
</form>
<?php else: ?>
<!-- Announcements List View -->
<div class="panel">
<div class="panel-head">
<h2>
<span>📢 All Announcements (<?= count($announcements) ?>)</span>
</h2>
<a href="index.php?new=1" class="btn btn-primary btn-sm">+ Create Announcement</a>
</div>
<?php if (empty($announcements)): ?>
<div class="empty-state">
<p>No announcements created yet.</p>
<a href="index.php?new=1" class="btn btn-primary btn-sm">+ Create Your First Announcement</a>
</div>
<?php else: ?>
<div class="admin-table-wrap">
<table class="admin-table">
<thead>
<tr>
<th>Status</th>
<th>Headline</th>
<th>Type</th>
<th>Schedule</th>
<th>Placements</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($announcements as $ann): ?>
<?php
$isCurrentlyActive = $ann['is_active'] &&
(empty($ann['start_date']) || $ann['start_date'] <= $today) &&
(empty($ann['end_date']) || $ann['end_date'] >= $today);
?>
<tr>
<td data-label="Status">
<?php if ($isCurrentlyActive): ?>
<span class="status-badge active">● Live</span>
<?php elseif ($ann['is_active']): ?>
<span class="status-badge inactive">⏳ Scheduled</span>
<?php else: ?>
<span class="status-badge inactive">○ Off</span>
<?php endif; ?>
</td>
<td data-label="Headline">
<strong><?= htmlspecialchars($ann['headline']) ?></strong>
<div class="muted" style="font-size: 0.82rem; margin-top: 0.2rem; max-width: 32ch; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
<?= htmlspecialchars($ann['message']) ?>
</div>
</td>
<td data-label="Type">
<span class="eyebrow"><?= htmlspecialchars($ann['type']) ?></span>
</td>
<td class="font-mono" data-label="Schedule" style="font-size: 0.8rem; color: var(--ink-muted);">
<?php if (!empty($ann['start_date']) || !empty($ann['end_date'])): ?>
<?= htmlspecialchars($ann['start_date'] ?: 'Now') ?> → <?= htmlspecialchars($ann['end_date'] ?: 'Ongoing') ?>
<?php else: ?>
Always (while on)
<?php endif; ?>
</td>
<td data-label="Placements" style="font-size: 0.82rem; color: var(--ink-muted);">
<?php
$locs = [];
if (!empty($ann['show_banner'])) $locs[] = 'Top Banner';
if (!empty($ann['show_in_hours'])) $locs[] = 'Hours Card';
echo implode(', ', $locs) ?: 'None';
?>
</td>
<td data-label="Actions">
<div class="admin-table-actions">
<a href="index.php?edit=<?= (int)$ann['id'] ?>" class="btn btn-outline btn-sm">Edit</a>
<form method="POST" action="index.php" style="display: inline;">
<input type="hidden" name="action" value="toggle_active">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars(get_csrf_token()) ?>">
<input type="hidden" name="announcement_id" value="<?= (int)$ann['id'] ?>">
<button type="submit" class="btn btn-outline btn-sm">
<?= $ann['is_active'] ? 'Turn Off' : 'Turn On' ?>
</button>
</form>
<form method="POST" action="index.php" style="display: inline;" onsubmit="return confirm('Are you sure you want to delete this announcement?');">
<input type="hidden" name="action" value="delete_announcement">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars(get_csrf_token()) ?>">
<input type="hidden" name="announcement_id" value="<?= (int)$ann['id'] ?>">
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</main>
</body>
</html>