Add multi-announcement management system
- Update inc/announcements.php to support listing, fetching by ID, toggling, and deleting multiple announcements - Upgrade admin/index.php to an announcement management view with data table, status indicators, and create/edit sub-views - Add admin table and action styles to styles.css without using inline style blocks - Update index.php to render all currently active banner and hours announcements dynamically
This commit is contained in:
1 parent
e4cd4f6cfb
commit
bd22299247
4 files changed
+330
-120
No files matched your search
+207
-100
@@ -4,48 +4,58 @@ require_once __DIR__ . '/../inc/announcements.php';
|
||||
|
||||
require_auth();
|
||||
$user = get_logged_in_user();
|
||||
$pdo = get_db();
|
||||
|
||||
$successMsg = '';
|
||||
$errorMsg = '';
|
||||
|
||||
// Handle announcement form submission
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'save_announcement') {
|
||||
// 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 {
|
||||
$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);
|
||||
$action = $_POST['action'] ?? '';
|
||||
|
||||
$successMsg = 'Announcement updated successfully!';
|
||||
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.';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch current announcement (or default empty template)
|
||||
$announcement = get_active_announcement();
|
||||
if (!$announcement) {
|
||||
$stmt = $pdo->query("SELECT * FROM announcements ORDER BY id DESC LIMIT 1");
|
||||
$announcement = $stmt->fetch() ?: [
|
||||
'id' => 0,
|
||||
'is_active' => 0,
|
||||
'type' => 'holiday',
|
||||
'headline' => 'Holiday Closure',
|
||||
'message' => 'The Old Fitz will be closed for the holiday weekend.',
|
||||
'start_date' => '',
|
||||
'end_date' => '',
|
||||
'show_banner' => 1,
|
||||
'show_in_hours' => 1
|
||||
];
|
||||
// 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">
|
||||
@@ -88,87 +98,184 @@ if (!$announcement) {
|
||||
<div class="alert-error" role="alert"><?= htmlspecialchars($errorMsg) ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<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)($announcement['id'] ?? 0) ?>">
|
||||
<?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 & Visit" card
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-actions" style="display: flex; gap: 0.75rem; align-items: center;">
|
||||
<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>📢 Announcement Settings</span>
|
||||
<span>📢 All Announcements (<?= count($announcements) ?>)</span>
|
||||
</h2>
|
||||
<?php if (!empty($announcement['is_active'])): ?>
|
||||
<span class="status-badge active">● Live on site</span>
|
||||
<?php else: ?>
|
||||
<span class="status-badge inactive">○ Inactive / Off</span>
|
||||
<?php endif; ?>
|
||||
<a href="index.php?new=1" class="btn btn-primary btn-sm">+ Create Announcement</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($announcement['is_active']) ? 'checked' : '' ?>>
|
||||
<strong>Enable Announcement on Site</strong>
|
||||
</label>
|
||||
<?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>
|
||||
<?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>
|
||||
<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>
|
||||
<span class="eyebrow"><?= htmlspecialchars($ann['type']) ?></span>
|
||||
</td>
|
||||
<td class="font-mono" 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 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>
|
||||
<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>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="headline">Notice Headline</label>
|
||||
<input type="text" id="headline" name="headline" class="form-control"
|
||||
value="<?= htmlspecialchars($announcement['headline'] ?? '') ?>"
|
||||
placeholder="e.g. Labor Day Closure" required>
|
||||
<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>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="type">Style / Category</label>
|
||||
<select id="type" name="type" class="form-control">
|
||||
<option value="holiday" <?= ($announcement['type'] ?? '') === 'holiday' ? 'selected' : '' ?>>Holiday / Special Schedule (Warm Brass & Green)</option>
|
||||
<option value="special" <?= ($announcement['type'] ?? '') === 'special' ? 'selected' : '' ?>>Event / Live Music Announcement</option>
|
||||
<option value="closure" <?= ($announcement['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($announcement['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($announcement['start_date'] ?? '') ?>">
|
||||
<span class="muted field-hint">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($announcement['end_date'] ?? '') ?>">
|
||||
<span class="muted field-hint">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" <?= !empty($announcement['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" <?= !empty($announcement['show_in_hours']) ? 'checked' : '' ?>>
|
||||
Highlight inside the "Hours & Visit" card
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="btn btn-primary">Save & Update Site</button>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
|
||||
Reference in new issue
Block a user