From da9ea4f413e0c60fc2b18ccd4fe6c277edbae4a5 Mon Sep 17 00:00:00 2001 From: NOrtmann1 Date: Mon, 1 Jun 2026 23:33:20 +0200 Subject: [PATCH] Update createArticle-controller.php --- php/controller/createArticle-controller.php | 158 ++++++++++---------- 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/php/controller/createArticle-controller.php b/php/controller/createArticle-controller.php index f131a10..3f84c4b 100644 --- a/php/controller/createArticle-controller.php +++ b/php/controller/createArticle-controller.php @@ -13,6 +13,85 @@ if (!isset($_SESSION["user"])) { try { $user = $dao->findUser($_SESSION["user_email"] ?? ""); + + if ($_SERVER["REQUEST_METHOD"] === "POST") { + $_SESSION["old_title"] = $_POST["title"] ?? ''; + $_SESSION["old_content"] = $_POST["content"] ?? ''; + $_SESSION["old_category"] = $_POST["category"] ?? ''; + $_SESSION["old_tags"] = $_POST["tags"] ?? ''; + + if(!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){ + $_SESSION["message"] = "missing_parameters"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } else { + $title = $_POST["title"]; + $content = $_POST["content"]; + $author = $user; + $category = $_POST["category"]; + $tags = $_POST['tags'] ?? ''; + + // -------------------------------- Validierung der Daten: ------------------------- + if (!articleAuthorValidator($author)) { + $_SESSION["message"] = "author_not_valid"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + + if (!articleTitleValidator($title)) { + $_SESSION["message"] = "invalid_title"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + + if (!articleContentValidator($content)) { + $_SESSION["message"] = "invalid_content"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + + if (!articleCategoryValidator($category)) { + $_SESSION["message"] = "invalid_category"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + + if (!articleTagValidator($tags)) { + $_SESSION["message"] = "invalid_tags"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } else { + $cleanedTags = []; + $rawTags = explode(',', $tags); + foreach ($rawTags as $rawTag) { + // Leerzeichen am Anfang/Ende des einzelnen Tags entfernen: + $tag = trim($rawTag); + $cleanedTags[] = $tag; + } + // Duplikate entfernen: + $cleanedTags = array_unique($cleanedTags); + $cleanedTags = implode(',', $cleanedTags); + } + // ----------------- Übertragung der validierten Daten in ArticleManager: --------------------------- + try { + $articleManager = ArticleManager::getInstance(); + $articleManager->addArticle($title, $content, $author, $category, $cleanedTags); + + // Formulardaten nach erfolgreichem Erstellen aus der Session löschen + unset($_SESSION["old_title"], $_SESSION["old_content"], $_SESSION["old_category"], $_SESSION["old_tags"]); + + } catch (Exception $e){ + $_SESSION["message"] = "internal_error"; + header("location: ../../index.php?pfad=createArticle"); + exit(); + } + + $_SESSION["message"] = "new_article"; + // Weiterleitung zur Homepage + header("location: ../../index.php"); + exit(); + } + } } catch (Exception $e) { $_SESSION["message"] = "internal_error"; exit(); @@ -26,83 +105,4 @@ if (!$user) { exit(); } -if ($_SERVER["REQUEST_METHOD"] === "POST") { - $_SESSION["old_title"] = $_POST["title"] ?? ''; - $_SESSION["old_content"] = $_POST["content"] ?? ''; - $_SESSION["old_category"] = $_POST["category"] ?? ''; - $_SESSION["old_tags"] = $_POST["tags"] ?? ''; - - if(!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){ - $_SESSION["message"] = "missing_parameters"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } else { - $title = $_POST["title"]; - $content = $_POST["content"]; - $author = $user; - $category = $_POST["category"]; - $tags = $_POST['tags'] ?? ''; - - // -------------------------------- Validierung der Daten: ------------------------- - if (!articleAuthorValidator($author)) { - $_SESSION["message"] = "author_not_valid"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } - - if (!articleTitleValidator($title)) { - $_SESSION["message"] = "invalid_title"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } - - if (!articleContentValidator($content)) { - $_SESSION["message"] = "invalid_content"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } - - if (!articleCategoryValidator($category)) { - $_SESSION["message"] = "invalid_category"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } - - if (!articleTagValidator($tags)) { - $_SESSION["message"] = "invalid_tags"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } else { - $cleanedTags = []; - $rawTags = explode(',', $tags); - foreach ($rawTags as $rawTag) { - // Leerzeichen am Anfang/Ende des einzelnen Tags entfernen: - $tag = trim($rawTag); - $cleanedTags[] = $tag; - } - // Duplikate entfernen: - $cleanedTags = array_unique($cleanedTags); - $cleanedTags = implode(',', $cleanedTags); - } - // ----------------- Übertragung der validierten Daten in ArticleManager: --------------------------- - try { - $articleManager = ArticleManager::getInstance(); - $articleManager->addArticle($title, $content, $author, $category, $cleanedTags); - - // Formulardaten nach erfolgreichem Erstellen aus der Session löschen - unset($_SESSION["old_title"], $_SESSION["old_content"], $_SESSION["old_category"], $_SESSION["old_tags"]); - - } catch (Exception $e){ - $_SESSION["message"] = "internal_error"; - header("location: ../../index.php?pfad=createArticle"); - exit(); - } - - $_SESSION["message"] = "new_article"; - // Weiterleitung zur Homepage - header("location: ../../index.php"); - exit(); - } -} - ?> \ No newline at end of file