model = new NewsModel(); $this->view = $view; } public function showNews() { $news = $this->model->getNews(); $this->view->setVars(['news' => $news]); } public function createNews() { if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) { header('Location: index.php?controller=News&do=showNews'); exit; } $data = [ 'name' => $_POST['name'] ?? '', 'description' => $_POST['description'] ?? '', 'date' => $_POST['date'] ?? date('Y-m-d'), ]; $errors = []; if (empty($data['name']) || empty($data['description']) || empty($data['date'])) { $errors['news'] = 'Bitte alle Felder ausfüllen.'; } if (!empty($errors)) { $this->view->setVars(['errors' => $errors, 'validData' => $data]); $this->view->setDoMethodName('createNewsForm'); return; } $this->model->createNews($data); $this->view->setDoMethodName('showCreateSuccess'); } public function createNewsForm() { if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) { header('Location: index.php?controller=News&do=showNews'); exit; } // Leere Felder für das Formular $this->view->setVars([ 'errors' => [], 'validData' => [] ]); } public function editNewsForm() { $id = $_GET['newsid']; $news = $this->model->getNewsById($id); $this->view->setVars(['news' => $news]); } public function updateNews() { $id = $_POST['newsid'] ?? null; $data = [ 'name' => $_POST['name'], 'beschreibung' => $_POST['beschreibung'], 'datum' => $_POST['datum'], ]; $this->model->updateNews($id, $data); } public function deleteNews() { if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) { header('Location: index.php?controller=News&do=showNews'); exit; } $id = $_GET['id'] ?? null; if ($id) { $this->model->deleteNews($id); } $this->view->setDoMethodName('showDeleteSuccess'); } }