Fixed error and Implemented BuyTickets views Model and Controller. Added Button to Events and Added Styles

This commit is contained in:
Viktor Sergeev 2025-07-11 22:43:29 +02:00
parent c306a59fec
commit a881b3933d
9 changed files with 342 additions and 33 deletions

View File

@ -630,3 +630,26 @@ td a:hover {
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;
}

View File

@ -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;
}
}
public function showUpdateEvent() {

View File

@ -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;
}
}

View File

@ -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.");
}
}

View File

@ -4,9 +4,12 @@ include dirname(__DIR__).'/header.phtml';
<div class="inhalt">
<div class="msg">
<p>Das Event mit der id"<?php echo $id?>" wurde erfolgreich gelöscht!</p>
<a href="?controller=Event&do=showEvents">Weiter</a>
<p>Das Event mit der ID "<?php echo htmlspecialchars($id); ?>" wurde erfolgreich gelöscht!</p>
<p>Sie werden in 3 Sekunden zur Event-Übersicht weitergeleitet...</p>
<a href="?controller=Event&do=showEvents">Jetzt zur Event-Übersicht</a>
</div>
</div>
<meta http-equiv="refresh" content="3;url=index.php?controller=Event&do=showEvents">
<?php include dirname(__DIR__).'/footer.phtml'; ?>

View File

@ -15,20 +15,30 @@
<th>Beschreibung</th>
<th>Von</th>
<th>Bis</th>
<th>Max. Tickets</th>
<?php if (isset($_SESSION['is_admin']) && $_SESSION['is_admin']): ?>
<th>Preis</th>
<th>Tickets</th>
<?php if (isset($_SESSION['user_id'])): ?>
<th>Aktionen</th>
<?php endif; ?>
<?php if (isset($_SESSION['is_admin']) && $_SESSION['is_admin']): ?>
<th>Admin</th>
<?php endif; ?>
</tr>
</thead>
<tbody>
<?php foreach ($events as $event): ?>
<tr>
<tr class="event-row" data-event-id="<?php echo $event['event_id']; ?>" style="cursor: pointer;">
<td><?php echo htmlspecialchars($event['name']); ?></td>
<td><?php echo nl2br(htmlspecialchars($event['description'])); ?></td>
<td><?php echo date('d.m.Y', strtotime($event['start_date'])); ?></td>
<td><?php echo date('d.m.Y', strtotime($event['end_date'])); ?></td>
<td><?php echo number_format($event['ticket_price'], 2, ',', '.'); ?></td>
<td><?php echo (int) $event['max_tickets']; ?></td>
<?php if (isset($_SESSION['user_id'])): ?>
<td>
<a href="?controller=Ticket&do=showBuyTicketForm&event_id=<?php echo $event['event_id']; ?>" class="admin-btn">Ticket kaufen</a>
</td>
<?php endif; ?>
<?php if (isset($_SESSION['is_admin']) && $_SESSION['is_admin']): ?>
<td>
<a href="?controller=Event&do=showUpdateEvent&event_id=<?php echo $event['event_id']; ?>" class="admin-btn">Bearbeiten</a>
@ -39,9 +49,29 @@
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const eventRows = document.querySelectorAll('.event-row');
eventRows.forEach(function(row) {
row.addEventListener('dblclick', function(e) {
// Don't trigger if clicking on a link or button
if (e.target.tagName === 'A' || e.target.tagName === 'BUTTON') {
return;
}
const eventId = this.getAttribute('data-event-id');
if (eventId) {
window.location.href = 'index.php?controller=Ticket&do=showBuyTicketForm&event_id=' + eventId;
}
});
});
});
</script>
<?php else: ?>
<p>Derzeit sind keine Ausstellungen verfügbar.</p>
<?php endif; ?>
</div>
</div>
</div>

View File

@ -2,11 +2,32 @@
include dirname(__DIR__).'/header.phtml';
?>
<div class="msg">
<p>Ihr Ticket für das Event "<?php echo $event['name']?>" wurde erfolgreich gekauft!</p>
<a href="?controller=Welcome&do=showWelcome">Weiter</a>
<div class="inhalt">
<div class="form-container">
<h1>Ticket erfolgreich gekauft!</h1>
<?php if (isset($event) && isset($ticket_id)): ?>
<div class="status-box">
<h2><?= htmlspecialchars($event['name']) ?></h2>
<p><strong>Ticket-ID:</strong> #<?= $ticket_id ?></p>
<p><strong>Kaufdatum:</strong> <?= date('d.m.Y', strtotime($purchase_date)) ?></p>
<p><strong>Gültig bis:</strong> <?= date('d.m.Y', strtotime($valid_until)) ?></p>
<p><strong>Preis:</strong> <?= number_format($event['ticket_price'], 2, ',', '.') ?></p>
<p><strong>Datum:</strong> <?= date('d.m.Y', strtotime($event['start_date'])) ?> - <?= date('d.m.Y', strtotime($event['end_date'])) ?></p>
</div>
<div style="text-align:center; margin-top: 1.5em;">
<a href="?controller=Ticket&do=showTickets" class="admin-btn">Meine Tickets anzeigen</a>
<br><br>
<a href="?controller=Event&do=showEvents">Zurück zu den Events</a>
</div>
<?php else: ?>
<div class="error-box">Fehler beim Anzeigen der Ticket-Details.</div>
<div style="text-align:center; margin-top: 1.5em;">
<a href="?controller=Event&do=showEvents">Zurück zu den Events</a>
</div>
<?php endif; ?>
</div>
</div>
<?php include dirname(__DIR__).'/footer.phtml'; ?>

View File

@ -0,0 +1,52 @@
<?php
include dirname(__DIR__).'/header.phtml';
?>
<div class="inhalt">
<div class="form-container">
<h1>Ticket kaufen</h1>
<?php if (isset($error)): ?>
<div class="error-box"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if (isset($event)): ?>
<div class="event-details">
<h2><?= htmlspecialchars($event['name']) ?></h2>
<p><strong>Beschreibung:</strong> <?= nl2br(htmlspecialchars($event['description'])) ?></p>
<p><strong>Datum:</strong> <?= date('d.m.Y', strtotime($event['start_date'])) ?> - <?= date('d.m.Y', strtotime($event['end_date'])) ?></p>
<p><strong>Preis:</strong> <?= number_format($event['ticket_price'], 2, ',', '.') ?></p>
<p><strong>Max. Tickets:</strong> <?= (int) $event['max_tickets'] ?></p>
</div>
<?php if ($hasTicket): ?>
<div class="status-box">
<p>Sie haben bereits ein Ticket für dieses Event gekauft.</p>
<a href="?controller=Ticket&do=showTickets" class="admin-btn">Meine Tickets anzeigen</a>
</div>
<?php else: ?>
<form class="form-horizontal" action="index.php" method="post">
<input type="hidden" name="controller" value="Ticket">
<input type="hidden" name="do" value="buyTicket">
<input type="hidden" name="event_id" value="<?= $event['event_id'] ?>">
<p>Möchten Sie ein Ticket für dieses Event kaufen?</p>
<p><strong>Preis:</strong> <?= number_format($event['ticket_price'], 2, ',', '.') ?></p>
<button class="button-login" type="submit">Ticket kaufen</button>
</form>
<div style="text-align:center; margin-top: 1.5em;">
<a href="?controller=Event&do=showEvents">Zurück zu den Events</a>
</div>
<?php endif; ?>
<?php else: ?>
<div class="error-box">Event nicht gefunden.</div>
<div style="text-align:center; margin-top: 1.5em;">
<a href="?controller=Event&do=showEvents">Zurück zu den Events</a>
</div>
<?php endif; ?>
</div>
</div>
<?php include dirname(__DIR__).'/footer.phtml'; ?>

View File

@ -1,8 +1,53 @@
<div class="inhalt">
<div class="tickets-container">
<h1>Tickets</h1>
<div class="tickets-container-inhalt">
<?php
include dirname(__DIR__).'/header.phtml';
?>
<div class="inhalt">
<div class="content-container">
<div class="event-header">
<h2>Meine Tickets</h2>
</div>
<?php if (!empty($tickets)): ?>
<div class="event-container-inhalt">
<table>
<thead>
<tr>
<th>Event</th>
<th>Datum</th>
<th>Standort</th>
<th>Preis</th>
<th>Kaufdatum</th>
<th>Gültig bis</th>
<th>Aktionen</th>
</tr>
</thead>
<tbody>
<?php foreach ($tickets as $ticket): ?>
<tr>
<td><?= htmlspecialchars($ticket['event_name']) ?></td>
<td><?= date('d.m.Y', strtotime($ticket['start_date'])) ?> - <?= date('d.m.Y', strtotime($ticket['end_date'])) ?></td>
<td><?= htmlspecialchars($ticket['location_street'] . ' ' . $ticket['location_house_number'] . ', ' . $ticket['location_city']) ?></td>
<td><?= number_format($ticket['ticket_price'], 2, ',', '.') ?></td>
<td><?= date('d.m.Y', strtotime($ticket['purchase_date'])) ?></td>
<td><?= date('d.m.Y', strtotime($ticket['valid_until'])) ?></td>
<td>
<a href="?controller=Ticket&do=deleteTicket&ticket_id=<?= $ticket['ticket_id'] ?>"
class="admin-btn"
onclick="return confirm('Ticket wirklich löschen?')">Löschen</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div class="status-box">
<p>Sie haben noch keine Tickets gekauft.</p>
<a href="?controller=Event&do=showEvents" class="admin-btn">Events anzeigen</a>
</div>
<?php endif; ?>
</div>
</div>
<?php include dirname(__DIR__).'/footer.phtml'; ?>