From 32651ed4335e2a20d3e323114a618e863517f24c Mon Sep 17 00:00:00 2001 From: twotalesanimation <80506065+twotalesanimation@users.noreply.github.com> Date: Thu, 4 Dec 2025 21:56:57 +0200 Subject: [PATCH] fix: publish toggle error alert and event visibility - Add proper error handling to toggle_event_published.php with HTTP status codes - Add try-catch block for database operations in toggle endpoint - Update events.php query to only show published events (added published = 1 filter) - Add updated_at timestamp update when toggling publish status - Improve error messages for better debugging --- src/admin/toggle_event_published.php | 71 +++++++++++++++++----------- src/pages/events/events.php | 4 +- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/admin/toggle_event_published.php b/src/admin/toggle_event_published.php index 7525b64a..d0bf1f3a 100644 --- a/src/admin/toggle_event_published.php +++ b/src/admin/toggle_event_published.php @@ -8,37 +8,54 @@ header('Content-Type: application/json'); $event_id = $_POST['event_id'] ?? null; if (!$event_id) { + http_response_code(400); echo json_encode(['status' => 'error', 'message' => 'Event ID is required']); exit; } -// Get current published status -$stmt = $conn->prepare("SELECT published FROM events WHERE event_id = ?"); -$stmt->bind_param("i", $event_id); -$stmt->execute(); -$result = $stmt->get_result(); +try { + // Get current published status + $stmt = $conn->prepare("SELECT published FROM events WHERE event_id = ?"); + if (!$stmt) { + throw new Exception("Prepare failed: " . $conn->error); + } + + $stmt->bind_param("i", $event_id); + $stmt->execute(); + $result = $stmt->get_result(); -if ($result->num_rows === 0) { - echo json_encode(['status' => 'error', 'message' => 'Event not found']); - exit; + if ($result->num_rows === 0) { + http_response_code(404); + echo json_encode(['status' => 'error', 'message' => 'Event not found']); + $stmt->close(); + exit; + } + + $event = $result->fetch_assoc(); + $new_status = $event['published'] == 1 ? 0 : 1; + $stmt->close(); + + // Update published status + $update_stmt = $conn->prepare("UPDATE events SET published = ?, updated_at = NOW() WHERE event_id = ?"); + if (!$update_stmt) { + throw new Exception("Prepare failed: " . $conn->error); + } + + $update_stmt->bind_param("ii", $new_status, $event_id); + + if ($update_stmt->execute()) { + http_response_code(200); + echo json_encode([ + 'status' => 'success', + 'message' => $new_status == 1 ? 'Event published' : 'Event unpublished', + 'published' => $new_status + ]); + } else { + throw new Exception("Update failed: " . $update_stmt->error); + } + $update_stmt->close(); +} catch (Exception $e) { + http_response_code(500); + echo json_encode(['status' => 'error', 'message' => 'Database error: ' . $e->getMessage()]); } -$event = $result->fetch_assoc(); -$new_status = $event['published'] == 1 ? 0 : 1; - -// Update published status -$update_stmt = $conn->prepare("UPDATE events SET published = ? WHERE event_id = ?"); -$update_stmt->bind_param("ii", $new_status, $event_id); - -if ($update_stmt->execute()) { - echo json_encode([ - 'status' => 'success', - 'message' => $new_status == 1 ? 'Event published' : 'Event unpublished', - 'published' => $new_status - ]); -} else { - echo json_encode(['status' => 'error', 'message' => 'Failed to update event status']); -} - -$stmt->close(); -$update_stmt->close(); diff --git a/src/pages/events/events.php b/src/pages/events/events.php index 950c85eb..02c7fae0 100644 --- a/src/pages/events/events.php +++ b/src/pages/events/events.php @@ -90,8 +90,8 @@ include_once($rootPath . '/header.php'); prepare("SELECT event_id, date, time, name, image, description, feature, location, type, promo FROM events WHERE date > CURDATE() ORDER BY date ASC"); + // Query to retrieve upcoming published events only + $stmt = $conn->prepare("SELECT event_id, date, time, name, image, description, feature, location, type, promo FROM events WHERE date > CURDATE() AND published = 1 ORDER BY date ASC"); $stmt->execute(); $result = $stmt->get_result();