Bib-Arts/Controller/EventController.php

161 lines
6.0 KiB
PHP

<?php
namespace Blog\Controller;
use Blog\Model\EventModel;
use Blog\Model\StandortModel;
class EventController {
private $model;
private $view;
public function __construct($view) {
$this->model = new EventModel();
$this->view = $view;
}
public function showEvents() {
$events = $this->model->getEvents();
$this->view->setVars([
'events' => $events
]);
}
public function createEvent() {
if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
header('Location: index.php?controller=Event&do=showEvents');
exit;
}
$data = [
'name' => $_POST['name'] ?? '',
'start_date' => $_POST['start_date'] ?? '',
'end_date' => $_POST['end_date'] ?? '',
'location_id' => $_POST['location_id'] ?? '',
'description' => $_POST['description'] ?? '',
'max_tickets' => $_POST['max_tickets'] ?? '',
'ticket_price' => $_POST['ticket_price'] ?? ''
];
$errors = [];
if (empty($data['name']) || empty($data['start_date']) || empty($data['end_date']) || empty($data['location_id']) || empty($data['description']) || empty($data['max_tickets']) || empty($data['ticket_price'])) {
$errors['event'] = 'Bitte alle Felder ausfüllen.';
}
if (!empty($errors)) {
$standortModel = new StandortModel();
$locations = $standortModel->getStandorte();
$this->view->setVars(['errors' => $errors, 'validData' => $data, 'locations' => $locations]);
$this->view->setDoMethodName('showCreateEvent');
return;
}
$this->model->createEvent($data);
$this->view->setDoMethodName('showCreateForwarding');
}
public function editEventForm() {
$id = $_GET['ausstellungid'];
$event = $this->model->getEvent($id);
$this->view->setVars(['event' => $event]);
}
public function updateEvent() {
if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
header('Location: index.php?controller=Event&do=showEvents');
exit;
}
$id = $_POST['id'] ?? null;
$data = [
'name' => $_POST['name'] ?? '',
'start_date' => $_POST['start_date'] ?? '',
'end_date' => $_POST['end_date'] ?? '',
'location_id' => $_POST['location_id'] ?? '',
'description' => $_POST['description'] ?? '',
'max_tickets' => $_POST['max_tickets'] ?? '',
'ticket_price' => $_POST['ticket_price'] ?? ''
];
$errors = [];
if (empty($data['name']) || empty($data['start_date']) || empty($data['end_date']) || empty($data['location_id']) || empty($data['description']) || empty($data['max_tickets']) || empty($data['ticket_price'])) {
$errors['event'] = 'Bitte alle Felder ausfüllen.';
}
if (!empty($errors)) {
$standortModel = new StandortModel();
$location = $standortModel->getStandort($data['location_id']);
$eventView = [
'id' => $id,
'name' => $data['name'],
'start_date' => $data['start_date'],
'end_date' => $data['end_date'],
'location_id' => $data['location_id'],
'location_name' => $location['city'] ?? '',
'description' => $data['description'],
'max_tickets' => $data['max_tickets'],
'ticket_price' => $data['ticket_price'],
];
$this->view->setVars(['event' => $eventView, 'errors' => $errors]);
$this->view->setDoMethodName('showUpdateEvent');
return;
}
$this->model->updateEvent($id, $data);
$this->view->setDoMethodName('showUpdateForwarding');
}
public function deleteEvent() {
if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
header('Location: index.php?controller=Event&do=showEvents');
exit;
}
$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() {
if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
header('Location: index.php?controller=Event&do=showEvents');
exit;
}
$id = $_GET['event_id'] ?? null;
if (!$id) {
$this->view->setVars(['error' => 'Keine Event-ID angegeben.']);
return;
}
$event = $this->model->getEvent($id);
if (!$event) {
$this->view->setVars(['error' => 'Event nicht gefunden.']);
return;
}
$standortModel = new StandortModel();
$location = $standortModel->getStandort($event['location_id']);
$eventView = [
'id' => $event['event_id'],
'name' => $event['name'],
'start_date' => $event['start_date'],
'end_date' => $event['end_date'],
'location_id' => $event['location_id'],
'location_name' => $location['city'] ?? '',
'description' => $event['description'],
'max_tickets' => $event['max_tickets'],
'ticket_price' => $event['ticket_price'],
];
$this->view->setVars(['event' => $eventView, 'errors' => []]);
}
public function showCreateEvent() {
if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
header('Location: index.php?controller=Event&do=showEvents');
exit;
}
$standortModel = new StandortModel();
$locations = $standortModel->getStandorte();
$this->view->setVars([
'locations' => $locations,
'errors' => [],
'validData' => []
]);
}
}