diff --git a/content/updateArticle.php b/content/updateArticle.php new file mode 100644 index 0000000..600f2a5 --- /dev/null +++ b/content/updateArticle.php @@ -0,0 +1,86 @@ + + +
diff --git a/php/controller/updateArticle-controller.php b/php/controller/updateArticle-controller.php new file mode 100644 index 0000000..60c1180 --- /dev/null +++ b/php/controller/updateArticle-controller.php @@ -0,0 +1,56 @@ +getArticle($_GET["id"]); + if($article != null){ + $title = $article->getTitle(); + $content = $article->getContent(); + $category = $article->getCategory(); + $author = $article->getAuthor(); + $tags = $article->getTags(); + }else{ + $_SESSION["message"] = "article_not_found"; + echo "article_not_found"; + } + } catch (Exception $e){ + $_SESSION["message"] = "internal_error"; + echo "Fehler aufgetreten: " . $e->getMessage(); + } +}else{ + $_SESSION["message"] = "article_not_found"; +} + +// Wenn "speichern" gedrückt wurde: +if ($_SERVER["REQUEST_METHOD"] === "POST") { + if(!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){ + $_SESSION["message"] = "missing_parameters"; + header("location: ../../index.php?pfad=updateArticle"); + } else { + + $title = $_POST["title"]; + $content = $_POST["content"]; + $category = $_POST["category"]; + $author = "max.mustermann"; // TODO: später aus Session den angemeldeten Nutzer beziehen. + $tags = $_POST["tags"]; + + try { + $articleManager = ArticleManager::getInstance(); + $articleManager->updateArticle($title, $content, $author, $category, $tags); + } catch (Exception $e){ + $_SESSION["message"] = "internal_error"; + } + $_SESSION["message"] = "new_article"; + // Weiterleitung zur Homepage + header("location: ../../index.php"); + exit(); + + } +} + +?> \ No newline at end of file diff --git a/php/model/ArticleManagerDAO.php b/php/model/ArticleManagerDAO.php index 99b93f8..ac5dc42 100644 --- a/php/model/ArticleManagerDAO.php +++ b/php/model/ArticleManagerDAO.php @@ -21,26 +21,23 @@ interface ArticleManagerDAO public function addArticle($title, $content, $author, $category, $tags); /** - * Ein angemeldeter Nutzer bearbeitet einen Beitrag. - * $id ID des Beitrags - * $title Titel des Beitrags - * $content Der Inhalt des Beitrags - * $author dem Author des des Beitrags (NID oder email) + * Ändert den gespeicherten Beitrag eines übergebenen Beitrags und eines Autors. + * Es wird geprüft, ob der zu änderne Beitrag existiert und ob der übergebene Autor der Autor des originalen + * Beitrages ist. + * @param $article + * @param $author + * @return void * - * Mögliche Exceptions: - * TODO Fehlerbeschreibung hinzufügen + * TODO: Fehlerbeschreibung hinzufügen */ - public function updateArticle($id, $title, $content, $author); + public function updateArticle($article, $author); - /* - * Ein angemeldeter Nutzer löscht einen seiner Beiträge. - * $id ID des Beitrags - * $title Titel des Beitrags - * $content Der Inhalt des Beitrags - * $author dem Author des des Beitrags (NID oder email) + /** + * Löscht einen Beitrag aus übergebener ID. + * @param $id + * @return void * - * Mögliche Exceptions: - * TODO Fehlerbeschreibung hinzufügen + * TODO: Fehlerbeschreibung hinzufügen */ public function deleteArticle($id); diff --git a/php/model/LocalArticleManager.php b/php/model/LocalArticleManager.php index f686127..4e72b0a 100644 --- a/php/model/LocalArticleManager.php +++ b/php/model/LocalArticleManager.php @@ -62,14 +62,61 @@ class LocalArticleManager implements ArticleManagerDAO { $this->saveArticle($articles); } - public function updateArticle($id, $title, $content, $author) + public function updateArticle($article, $author) { - // TODO: Implement updateArticle() method. + if (empty($article)) { + return; + } + + // Berechtigungsprüfung: + if ($article->getAuthor() !== $author) { + // TODO: Implement Exception (z.B. throw new Exception("Nicht autorisiert")); + return; + } + + $articles = $this->getAllArticles(); + $updated = false; + + // Beitrag aktualisieren: + foreach ($articles as $index => $storedArticle) { + if (isset($storedArticle['id']) && $storedArticle['id'] == $article->getId()) { + $articles[$index] = [ + "id" => $article->getId(), + "title" => $article->getTitle(), + "content" => $article->getContent(), + "author" => $article->getAuthor(), + "category" => $article->getCategory(), + "tags" => $article->getTags(), + "creationDate" => $article->getCreationDate() // Behält das originale Erstelldatum bei + ]; + $updated = true; + break; + } + } + + // Nur speichern, wenn Beitrag geändert wurde: + if ($updated) { + $this->saveArticle($articles); + } } public function deleteArticle($id) { - // TODO: Implement deleteArticle() method. + $articles = $this->getAllArticles(); + $articleFound = false; + + foreach ($articles as $index => $article) { + if (isset($article['id']) && $article['id'] == $id) { + unset($articles[$index]); + $articleFound = true; + break; // Schleife abbrechen, da die ID eindeutig ist + } + } + + if ($articleFound) { + // array_values stellt sicher, dass die Array-Keys wieder fortlaufend bei 0 beginnen + $this->saveArticle(array_values($articles)); + } } public function getArticle($id)