diff --git a/.htaccess b/.htaccess index 70cbdf64..908abdf0 100644 --- a/.htaccess +++ b/.htaccess @@ -118,7 +118,9 @@ RewriteRule ^logout$ src/processors/logout.php [L] RewriteRule ^process_trip$ src/processors/process_trip.php [L] RewriteRule ^process_event$ src/admin/process_event.php [L] RewriteRule ^toggle_trip_published$ src/processors/toggle_trip_published.php [L] +RewriteRule ^toggle_event_published$ src/admin/toggle_event_published.php [L] RewriteRule ^delete_trip$ src/processors/delete_trip.php [L] +RewriteRule ^delete_event$ src/admin/delete_event.php [L] diff --git a/src/admin/admin_events.php b/src/admin/admin_events.php index 0ac2c5d6..396fd145 100644 --- a/src/admin/admin_events.php +++ b/src/admin/admin_events.php @@ -3,6 +3,22 @@ $headerStyle = 'light'; $rootPath = dirname(dirname(__DIR__)); include_once($rootPath . '/header.php'); checkAdmin(); + +// Fetch all events +$events_query = " + SELECT + event_id, name, type, location, date, published + FROM events + ORDER BY date DESC +"; + +$result = $conn->query($events_query); +$events = []; +if ($result && $result->num_rows > 0) { + while ($row = $result->fetch_assoc()) { + $events[] = $row; + } +} ?> + - + 'index'], [$pageTitle => '']]; + require_once($rootPath . '/components/banner.php'); +?> + + + - + + Create New Event prepare("SELECT event_id, name, type, location, date, published FROM events ORDER BY date DESC"); - $stmt->execute(); - $result = $stmt->get_result(); - - if ($result->num_rows > 0) { - echo ''; - echo ' - - - Event Name - Type - Location - Date - Status - Actions - - - '; - - while ($event = $result->fetch_assoc()) { - $event_id = $event['event_id']; - $name = htmlspecialchars($event['name']); - $type = htmlspecialchars($event['type']); - $location = htmlspecialchars($event['location']); - $date = convertDate($event['date']); - $status = $event['published'] ? 'Published' : 'Draft'; - - echo " - {$name} - {$type} - {$location} - {$date} - {$status} - - - Edit - Delete + if (!empty($events)) { + echo ' + + + - - "; + + + + Event Name + Type + Location + Date + Status + Actions + + + '; + foreach ($events as $event) { + $publishButtonText = $event['published'] == 1 ? 'Unpublish' : 'Publish'; + $publishButtonClass = $event['published'] == 1 ? 'btn-warning' : 'btn-success'; + echo ' + ' . htmlspecialchars($event['name']) . ' + ' . htmlspecialchars($event['type']) . ' + ' . htmlspecialchars($event['location']) . ' + ' . convertDate($event['date']) . ' + ' . ($event['published'] == 1 ? 'Published' : 'Draft') . ' + + + + + + + + + + + + '; } - echo ''; + echo ''; + echo ''; } else { - echo 'No events found. Create the first event'; + echo 'No events found. Create one'; } ?> + diff --git a/src/admin/delete_event.php b/src/admin/delete_event.php new file mode 100644 index 00000000..152ae71f --- /dev/null +++ b/src/admin/delete_event.php @@ -0,0 +1,46 @@ + 'error', 'message' => 'Event ID is required']); + exit; +} + +// Get event details to delete associated files +$stmt = $conn->prepare("SELECT image, promo FROM events WHERE event_id = ?"); +$stmt->bind_param("i", $event_id); +$stmt->execute(); +$result = $stmt->get_result(); + +if ($result->num_rows > 0) { + $event = $result->fetch_assoc(); + + // Delete image files + if ($event['image'] && file_exists($rootPath . '/' . $event['image'])) { + unlink($rootPath . '/' . $event['image']); + } + if ($event['promo'] && file_exists($rootPath . '/' . $event['promo'])) { + unlink($rootPath . '/' . $event['promo']); + } + + // Delete from database + $delete_stmt = $conn->prepare("DELETE FROM events WHERE event_id = ?"); + $delete_stmt->bind_param("i", $event_id); + + if ($delete_stmt->execute()) { + echo json_encode(['status' => 'success', 'message' => 'Event deleted successfully']); + } else { + echo json_encode(['status' => 'error', 'message' => 'Failed to delete event']); + } + $delete_stmt->close(); +} else { + echo json_encode(['status' => 'error', 'message' => 'Event not found']); +} + +$stmt->close(); diff --git a/src/admin/manage_events.php b/src/admin/manage_events.php index 21157eeb..0d746b76 100644 --- a/src/admin/manage_events.php +++ b/src/admin/manage_events.php @@ -118,16 +118,6 @@ if ($event_id) { - - - - - > - Publish Event (visible on website) - - - - diff --git a/src/admin/process_event.php b/src/admin/process_event.php index 036b9e7f..50cdf4fe 100644 --- a/src/admin/process_event.php +++ b/src/admin/process_event.php @@ -62,7 +62,6 @@ $date = $_POST['date'] ?? null; $time = $_POST['time'] ?? null; $feature = $_POST['feature'] ?? null; $description = $_POST['description'] ?? null; -$published = isset($_POST['published']) ? 1 : 0; // Validate required fields if (!$name || !$type || !$location || !$date || !$time || !$feature || !$description) { @@ -135,7 +134,6 @@ try { 'time' => $time, 'feature' => $feature, 'description' => $description, - 'published' => $published, 'updated_at' => date('Y-m-d H:i:s') ]; @@ -174,13 +172,13 @@ try { $promo_path = $promo_path ?? 'assets/images/events/default-promo.jpg'; $stmt = $conn->prepare(" - INSERT INTO events (name, type, location, date, time, feature, description, image, promo, published, created_by) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO events (name, type, location, date, time, feature, description, image, promo, created_by) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "); $created_by = $_SESSION['user_id'] ?? 0; - $stmt->bind_param('ssssssssiii', $name, $type, $location, $date, $time, $feature, $description, $image_path, $promo_path, $published, $created_by); + $stmt->bind_param('sssssssssi', $name, $type, $location, $date, $time, $feature, $description, $image_path, $promo_path, $created_by); if ($stmt->execute()) { echo json_encode(['status' => 'success', 'message' => 'Event created successfully']); diff --git a/src/admin/toggle_event_published.php b/src/admin/toggle_event_published.php new file mode 100644 index 00000000..7525b64a --- /dev/null +++ b/src/admin/toggle_event_published.php @@ -0,0 +1,44 @@ + '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(); + +if ($result->num_rows === 0) { + echo json_encode(['status' => 'error', 'message' => 'Event not found']); + exit; +} + +$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();
No events found. Create one