diff --git a/admin/index.php b/admin/index.php index a65117e..a1bec99 100644 --- a/admin/index.php +++ b/admin/index.php @@ -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'); ?> @@ -88,87 +98,184 @@ if (!$announcement) { -
- - - + + + + + + +
+ + +
+
+ +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + + Leave blank to show immediately while enabled. +
+ +
+ + + Auto-hides after 11:59 PM on this date. +
+ +
+ +
+ + +
+
+
+ +
+ + Cancel +
+
+
+ +

- πŸ“’ Announcement Settings + πŸ“’ All Announcements ()

- - ● Live on site - - β—‹ Inactive / Off - + + Create Announcement
-
-
- + +
+

No announcements created yet.

+ + Create Your First Announcement
+ +
+ + + + + + + + + + + + + + = $today); + ?> + + + + + + + + + + +
StatusHeadlineTypeSchedulePlacementsActions
+ + ● Live + + ⏳ Scheduled + + β—‹ Off + + + +
+ +
+
+ + + + β†’ + + Always (while on) + + + + +
+ Edit + +
+ + + + +
-
- - +
+ + + + +
+
+
- -
- - -
- -
- - -
- -
- - - Leave blank to show immediately while enabled. -
- -
- - - Auto-hides after 11:59 PM on this date. -
- -
- -
- - -
-
-
- -
- -
+
- +
diff --git a/inc/announcements.php b/inc/announcements.php index 99debe9..4276dcc 100644 --- a/inc/announcements.php +++ b/inc/announcements.php @@ -5,7 +5,7 @@ require_once __DIR__ . '/db.php'; -function get_active_announcement(): ?array { +function get_active_announcements(): array { $pdo = get_db(); $today = date('Y-m-d'); @@ -15,7 +15,6 @@ function get_active_announcement(): ?array { AND (start_date IS NULL OR start_date = '' OR start_date <= :today1) AND (end_date IS NULL OR end_date = '' OR end_date >= :today2) ORDER BY id DESC - LIMIT 1 "; $stmt = $pdo->prepare($sql); @@ -23,15 +22,38 @@ function get_active_announcement(): ?array { ':today1' => $today, ':today2' => $today ]); - $result = $stmt->fetch(); + return $stmt->fetchAll() ?: []; +} - return $result ?: null; +function get_active_announcement(): ?array { + $all = get_active_announcements(); + return !empty($all) ? $all[0] : null; } function get_all_announcements(): array { $pdo = get_db(); $stmt = $pdo->query("SELECT * FROM announcements ORDER BY id DESC"); - return $stmt->fetchAll(); + return $stmt->fetchAll() ?: []; +} + +function get_announcement_by_id(int $id): ?array { + $pdo = get_db(); + $stmt = $pdo->prepare("SELECT * FROM announcements WHERE id = ?"); + $stmt->execute([$id]); + $item = $stmt->fetch(); + return $item ?: null; +} + +function delete_announcement(int $id): bool { + $pdo = get_db(); + $stmt = $pdo->prepare("DELETE FROM announcements WHERE id = ?"); + return $stmt->execute([$id]); +} + +function toggle_announcement_active(int $id): bool { + $pdo = get_db(); + $stmt = $pdo->prepare("UPDATE announcements SET is_active = CASE WHEN is_active = 1 THEN 0 ELSE 1 END, updated_at = CURRENT_TIMESTAMP WHERE id = ?"); + return $stmt->execute([$id]); } function save_announcement(array $data, ?int $id = null): int { @@ -46,7 +68,7 @@ function save_announcement(array $data, ?int $id = null): int { $showBanner = !empty($data['show_banner']) ? 1 : 0; $showInHours = !empty($data['show_in_hours']) ? 1 : 0; - if ($id) { + if ($id && $id > 0) { $stmt = $pdo->prepare(" UPDATE announcements SET is_active = ?, diff --git a/index.php b/index.php index be0ebe5..138f65c 100644 --- a/index.php +++ b/index.php @@ -3,7 +3,7 @@ require __DIR__ . '/config.php'; require __DIR__ . '/inc/facebook-stats.php'; require __DIR__ . '/inc/announcements.php'; $fbStats = fritz_facebook_stats(); -$activeAnnouncement = get_active_announcement(); +$activeAnnouncements = get_active_announcements(); $siteUrl = rtrim(SITE_URL, '/'); ?> @@ -61,14 +61,21 @@ $siteUrl = rtrim(SITE_URL, '/'); - -