127 lines
4.1 KiB
PHP
127 lines
4.1 KiB
PHP
<?php
|
|
|
|
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() {
|
|
if (!isset($_SESSION['user_id'])) {
|
|
$this->view->setVars(['error' => 'Bitte melden Sie sich an, um Ihre Tickets zu sehen.']);
|
|
$this->view->setDoMethodName('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(['error' => 'Bitte melden Sie sich an, um Tickets zu kaufen.']);
|
|
$this->view->setDoMethodName('showLoginForm');
|
|
return;
|
|
}
|
|
|
|
$event_id = $_GET['event_id'] ?? null;
|
|
if (!$event_id) {
|
|
$this->view->setVars(['error' => 'Keine Event-ID angegeben.']);
|
|
$this->view->setDoMethodName('showEvents');
|
|
return;
|
|
}
|
|
|
|
$event = $this->eventModel->getEvent($event_id);
|
|
if (!$event) {
|
|
$this->view->setVars(['error' => 'Event nicht gefunden.']);
|
|
$this->view->setDoMethodName('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(['error' => 'Bitte melden Sie sich an.']);
|
|
$this->view->setDoMethodName('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']);
|
|
}
|
|
} |