Fixed error and Implemented BuyTickets views Model and Controller. Added Button to Events and Added Styles
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user