Add SQLite-backed announcement system and admin portal
- Add SQLite database initialization and management in inc/db.php - Implement session authentication and CSRF security helpers in inc/auth.php - Implement announcement querying and saving in inc/announcements.php - Add management dashboard, login, and logout in admin/ - Protect db/ directory with Apache access controls
This commit is contained in:
1 parent
b9d3262233
commit
9b7d9ceccb
7 files changed
+761
No files matched your search
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Announcements helper functions
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/db.php';
|
||||
|
||||
function get_active_announcement(): ?array {
|
||||
$pdo = get_db();
|
||||
$today = date('Y-m-d');
|
||||
|
||||
$sql = "
|
||||
SELECT * FROM announcements
|
||||
WHERE is_active = 1
|
||||
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);
|
||||
$stmt->execute([
|
||||
':today1' => $today,
|
||||
':today2' => $today
|
||||
]);
|
||||
$result = $stmt->fetch();
|
||||
|
||||
return $result ?: null;
|
||||
}
|
||||
|
||||
function get_all_announcements(): array {
|
||||
$pdo = get_db();
|
||||
$stmt = $pdo->query("SELECT * FROM announcements ORDER BY id DESC");
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
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) {
|
||||
$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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user