From a881b3933df82b73ad81c4ba88d5d85898a93f55 Mon Sep 17 00:00:00 2001 From: pbbfa23cse Date: Fri, 11 Jul 2025 22:43:29 +0200 Subject: [PATCH] Fixed error and Implemented BuyTickets views Model and Controller. Added Button to Events and Added Styles --- CSS/style.css | 23 +++++++ Controller/EventController.php | 6 +- Controller/TicketController.php | 103 ++++++++++++++++++++++++++++--- Model/TicketModel.php | 54 +++++++++++++++- Views/Event/deleteEvent.phtml | 7 ++- Views/Event/showEvents.phtml | 44 ++++++++++--- Views/Ticket/buyTicket.phtml | 31 ++++++++-- Views/Ticket/buyTicketForm.phtml | 52 ++++++++++++++++ Views/Tickets/showTickets.phtml | 55 +++++++++++++++-- 9 files changed, 342 insertions(+), 33 deletions(-) create mode 100644 Views/Ticket/buyTicketForm.phtml diff --git a/CSS/style.css b/CSS/style.css index 03d8494..c737718 100644 --- a/CSS/style.css +++ b/CSS/style.css @@ -629,4 +629,27 @@ td a:hover { max-width: 1100px; width: 100%; margin: 0 auto; +} + +.event-details { + background: white; + padding: 20px; + border-radius: 8px; + margin-bottom: 20px; + box-shadow: 0 2px 8px rgba(0,0,0,0.1); +} + +.event-details h2 { + margin-top: 0; + color: #333; + font-size: 1.5em; +} + +.event-details p { + margin: 8px 0; + line-height: 1.4; +} + +.event-details strong { + color: #4d4d4d; } \ No newline at end of file diff --git a/Controller/EventController.php b/Controller/EventController.php index 5899876..3e88f86 100644 --- a/Controller/EventController.php +++ b/Controller/EventController.php @@ -106,8 +106,12 @@ class EventController { $id = $_GET['event_id'] ?? null; if ($id) { $this->model->deleteEvent($id); + $this->view->setVars(['id' => $id]); + $this->view->setDoMethodName('deleteEvent'); + } else { + header('Location: index.php?controller=Event&do=showEvents'); + exit; } - $this->view->setDoMethodName('deleteEvent'); } public function showUpdateEvent() { diff --git a/Controller/TicketController.php b/Controller/TicketController.php index 8ed3023..783bc7f 100644 --- a/Controller/TicketController.php +++ b/Controller/TicketController.php @@ -3,37 +3,120 @@ namespace Blog\Controller; use Blog\Model\TicketModel; +use Blog\Model\EventModel; +use Blog\Model\StandortModel; class TicketController { private $ticketModel; + private $eventModel; private $view; public function __construct($view) { $this->ticketModel = new TicketModel(); + $this->eventModel = new EventModel(); $this->view = $view; } public function showTickets() { - $tickets = $this->ticketModel->getTickets(); + if (!isset($_SESSION['user_id'])) { + header('Location: index.php?controller=Auth&do=showLoginForm'); + exit; + } + + $tickets = $this->ticketModel->getUserTickets($_SESSION['user_id']); $this->view->setVars(['tickets' => $tickets]); } + public function showBuyTicketForm() { + if (!isset($_SESSION['user_id'])) { + header('Location: index.php?controller=Auth&do=showLoginForm'); + exit; + } + + $event_id = $_GET['event_id'] ?? null; + if (!$event_id) { + header('Location: index.php?controller=Event&do=showEvents'); + exit; + } + + $event = $this->eventModel->getEvent($event_id); + if (!$event) { + header('Location: index.php?controller=Event&do=showEvents'); + exit; + } + + // Check if user already has a ticket for this event + $hasTicket = $this->ticketModel->hasTicket($_SESSION['user_id'], $event_id); + + $this->view->setVars([ + 'event' => $event, + 'hasTicket' => $hasTicket['count'] > 0 + ]); + } + public function buyTicket() { + if (!isset($_SESSION['user_id'])) { + header('Location: index.php?controller=Auth&do=showLoginForm'); + exit; + } + + $event_id = $_POST['event_id'] ?? null; + if (!$event_id) { + $this->view->setVars(['error' => 'Keine Event-ID angegeben.']); + return; + } + + $event = $this->eventModel->getEvent($event_id); + if (!$event) { + $this->view->setVars(['error' => 'Event nicht gefunden.']); + return; + } + + // Check if user already has a ticket for this event + $hasTicket = $this->ticketModel->hasTicket($_SESSION['user_id'], $event_id); + if ($hasTicket['count'] > 0) { + $this->view->setVars(['error' => 'Sie haben bereits ein Ticket für dieses Event.']); + return; + } + + // Calculate valid until date (event end date + 30 days) + $valid_until = date('Y-m-d', strtotime($event['end_date'] . ' +30 days')); + $data = [ - 'userid' => $_POST['userid'], - 'eventid' => $_POST['eventid'], - 'kaufdatum' => date('Y-m-d'), - 'gültigkeitsdatum' => $_POST['gültigkeitsdatum'] + 'user_id' => $_SESSION['user_id'], + 'event_id' => $event_id, + 'purchase_date' => date('Y-m-d'), + 'valid_until' => $valid_until ]; - $erg = $this->ticketModel->buyTicket($data); - $this->view->setVars(['ticket' => $erg]); + + try { + $ticket_id = $this->ticketModel->buyTicket($data); + $this->view->setVars([ + 'event' => $event, + 'ticket_id' => $ticket_id, + 'purchase_date' => $data['purchase_date'], + 'valid_until' => $valid_until + ]); + $this->view->setDoMethodName('buyTicket'); + } catch (Exception $e) { + $this->view->setVars(['error' => 'Fehler beim Kauf des Tickets: ' . $e->getMessage()]); + $this->view->setDoMethodName('showBuyTicketForm'); + } } public function deleteTicket() { - $ticketid = $_GET['ticketid'] ?? null; - if ($ticketid) { - $this->ticketModel->deleteTicket($ticketid); + if (!isset($_SESSION['user_id'])) { + header('Location: index.php?controller=Auth&do=showLoginForm'); + exit; } + + $ticket_id = $_GET['ticket_id'] ?? null; + if ($ticket_id) { + $this->ticketModel->deleteTicket($ticket_id); + } + + header('Location: index.php?controller=Ticket&do=showTickets'); + exit; } } \ No newline at end of file diff --git a/Model/TicketModel.php b/Model/TicketModel.php index 2a44b05..55b4a80 100644 --- a/Model/TicketModel.php +++ b/Model/TicketModel.php @@ -8,7 +8,12 @@ class TicketModel extends Database { public function getTickets() { $pdo = $this->linkDB(); - $sql = "SELECT * FROM ticket ORDER BY ticket_id ASC;"; + $sql = "SELECT t.*, e.name as event_name, e.start_date, e.end_date, e.ticket_price, + l.city as location_city, l.street as location_street, l.house_number as location_house_number + FROM ticket t + JOIN event e ON t.event_id = e.event_id + JOIN location l ON e.location_id = l.location_id + ORDER BY t.purchase_date DESC;"; try { $sth = $pdo->prepare($sql); $sth->execute(); @@ -19,8 +24,52 @@ class TicketModel extends Database { } } + public function getUserTickets($user_id) { + $pdo = $this->linkDB(); + $sql = "SELECT t.*, e.name as event_name, e.start_date, e.end_date, e.ticket_price, + l.city as location_city, l.street as location_street, l.house_number as location_house_number + FROM ticket t + JOIN event e ON t.event_id = e.event_id + JOIN location l ON e.location_id = l.location_id + WHERE t.user_id = :user_id + ORDER BY t.purchase_date DESC;"; + $params = [":user_id" => $user_id]; + try { + $sth = $pdo->prepare($sql); + $sth->execute($params); + return $sth->fetchAll(\PDO::FETCH_ASSOC); + } catch (PDOException $e) { + new \Blog\Library\ErrorMsg("Fehler beim Lesen der Benutzer-Tickets.", $e); + die; + } + } + public function buyTicket($data) { $pdo = $this->linkDB(); + + // First check if the event still has available tickets + $checkSql = "SELECT e.max_tickets, COUNT(t.ticket_id) as sold_tickets + FROM event e + LEFT JOIN ticket t ON e.event_id = t.event_id + WHERE e.event_id = :event_id + GROUP BY e.event_id, e.max_tickets"; + + try { + $checkStmt = $pdo->prepare($checkSql); + $checkStmt->execute([':event_id' => $data['event_id']]); + $eventInfo = $checkStmt->fetch(\PDO::FETCH_ASSOC); + + if (!$eventInfo) { + throw new \Exception("Event nicht gefunden."); + } + + if ($eventInfo['sold_tickets'] >= $eventInfo['max_tickets']) { + throw new \Exception("Alle Tickets für dieses Event sind bereits verkauft."); + } + } catch (PDOException $e) { + throw new \Exception("Fehler bei der Ticketverfügbarkeitsprüfung."); + } + $sql = "INSERT INTO ticket (user_id, event_id, purchase_date, valid_until) VALUES (:user_id, :event_id, :purchase_date, :valid_until);"; $params = [ @@ -34,8 +83,7 @@ class TicketModel extends Database { $sth->execute($params); return $pdo->lastInsertId(); } catch (PDOException $e) { - new \Blog\Library\ErrorMsg("Fehler beim Kauf des Tickets.", $e); - die; + throw new \Exception("Fehler beim Kauf des Tickets."); } } diff --git a/Views/Event/deleteEvent.phtml b/Views/Event/deleteEvent.phtml index 05e6e6e..c550e6b 100644 --- a/Views/Event/deleteEvent.phtml +++ b/Views/Event/deleteEvent.phtml @@ -4,9 +4,12 @@ include dirname(__DIR__).'/header.phtml';
-

Das Event mit der id"" wurde erfolgreich gelöscht!

- Weiter +

Das Event mit der ID "" wurde erfolgreich gelöscht!

+

Sie werden in 3 Sekunden zur Event-Übersicht weitergeleitet...

+ Jetzt zur Event-Übersicht
+ + \ No newline at end of file diff --git a/Views/Event/showEvents.phtml b/Views/Event/showEvents.phtml index 13cd584..0f99b3c 100644 --- a/Views/Event/showEvents.phtml +++ b/Views/Event/showEvents.phtml @@ -15,20 +15,30 @@ Beschreibung Von Bis - Max. Tickets - + Preis + Tickets + Aktionen + + Admin + - + + + + + Ticket kaufen + + Bearbeiten @@ -39,9 +49,29 @@ + + + + +

Derzeit sind keine Ausstellungen verfügbar.

- - - - \ No newline at end of file + \ No newline at end of file diff --git a/Views/Ticket/buyTicket.phtml b/Views/Ticket/buyTicket.phtml index 4cf930d..a158473 100644 --- a/Views/Ticket/buyTicket.phtml +++ b/Views/Ticket/buyTicket.phtml @@ -2,11 +2,32 @@ include dirname(__DIR__).'/header.phtml'; ?> -
-

Ihr Ticket für das Event "" wurde erfolgreich gekauft!

- Weiter +
+
+

Ticket erfolgreich gekauft!

+ + +
+

+

Ticket-ID: #

+

Kaufdatum:

+

Gültig bis:

+

Preis:

+

Datum: -

+
+ +
+ Meine Tickets anzeigen +

+ Zurück zu den Events +
+ +
Fehler beim Anzeigen der Ticket-Details.
+
+ Zurück zu den Events +
+
- - +
\ No newline at end of file diff --git a/Views/Ticket/buyTicketForm.phtml b/Views/Ticket/buyTicketForm.phtml new file mode 100644 index 0000000..6f8a5d0 --- /dev/null +++ b/Views/Ticket/buyTicketForm.phtml @@ -0,0 +1,52 @@ + + +
+
+

Ticket kaufen

+ + +
+ + + +
+

+

Beschreibung:

+

Datum: -

+

Preis:

+

Max. Tickets:

+
+ + +
+

Sie haben bereits ein Ticket für dieses Event gekauft.

+ Meine Tickets anzeigen +
+ +
+ + + + +

Möchten Sie ein Ticket für dieses Event kaufen?

+

Preis:

+ + +
+ +
+ Zurück zu den Events +
+ + +
Event nicht gefunden.
+
+ Zurück zu den Events +
+ +
+
+ + \ No newline at end of file diff --git a/Views/Tickets/showTickets.phtml b/Views/Tickets/showTickets.phtml index 0fd6a6e..fdf5c7f 100644 --- a/Views/Tickets/showTickets.phtml +++ b/Views/Tickets/showTickets.phtml @@ -1,8 +1,53 @@ + +
-
-

Tickets

-
- +
+
+

Meine Tickets

+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
EventDatumStandortPreisKaufdatumGültig bisAktionen
- + Löschen +
+
+ +
+

Sie haben noch keine Tickets gekauft.

+ Events anzeigen +
+
-
\ No newline at end of file +
+ + \ No newline at end of file