ticketModel = new TicketModel(); $this->eventModel = new EventModel(); $this->view = $view; } public function showTickets() { if (!isset($_SESSION['user_id'])) { $this->view->setVars(['redirect' => 'index.php?controller=Auth&do=showLoginForm']); return; } $tickets = $this->ticketModel->getUserTickets($_SESSION['user_id']); $this->view->setVars(['tickets' => $tickets]); } public function showBuyTicketForm() { if (!isset($_SESSION['user_id'])) { $this->view->setVars(['redirect' => 'index.php?controller=Auth&do=showLoginForm']); return; } $event_id = $_GET['event_id'] ?? null; if (!$event_id) { $this->view->setVars(['redirect' => 'index.php?controller=Event&do=showEvents']); return; } $event = $this->eventModel->getEvent($event_id); if (!$event) { $this->view->setVars(['redirect' => 'index.php?controller=Event&do=showEvents']); return; } // 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 = [ 'user_id' => $_SESSION['user_id'], 'event_id' => $event_id, 'purchase_date' => date('Y-m-d'), 'valid_until' => $valid_until ]; 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() { if (!isset($_SESSION['user_id'])) { $this->view->setVars(['redirect' => 'index.php?controller=Auth&do=showLoginForm']); return; } $ticket_id = $_GET['ticket_id'] ?? null; if ($ticket_id) { $this->ticketModel->deleteTicket($ticket_id); } // Redirect to tickets page using JavaScript $this->view->setVars(['redirect' => 'index.php?controller=Ticket&do=showTickets']); } }