= :today2) ORDER BY id DESC "; $stmt = $pdo->prepare($sql); $stmt->execute([ ':today1' => $today, ':today2' => $today ]); return $stmt->fetchAll() ?: []; } 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() ?: []; } 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 { $pdo = get_db(); $isActive = !empty($data['is_active']) ? 1 : 0; $type = in_array($data['type'] ?? '', ['holiday', 'special', 'closure']) ? $data['type'] : 'holiday'; $headline = trim($data['headline'] ?? ''); $message = trim($data['message'] ?? ''); $startDate = !empty($data['start_date']) ? trim($data['start_date']) : null; $endDate = !empty($data['end_date']) ? trim($data['end_date']) : null; $showBanner = !empty($data['show_banner']) ? 1 : 0; $showInHours = !empty($data['show_in_hours']) ? 1 : 0; if ($id && $id > 0) { $stmt = $pdo->prepare(" UPDATE announcements SET is_active = ?, type = ?, headline = ?, message = ?, start_date = ?, end_date = ?, show_banner = ?, show_in_hours = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? "); $stmt->execute([ $isActive, $type, $headline, $message, $startDate, $endDate, $showBanner, $showInHours, $id ]); return $id; } else { $stmt = $pdo->prepare(" INSERT INTO announcements ( is_active, type, headline, message, start_date, end_date, show_banner, show_in_hours ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([ $isActive, $type, $headline, $message, $startDate, $endDate, $showBanner, $showInHours ]); return (int)$pdo->lastInsertId(); } }