ArticleManager fehlende Implementationen
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
session_start();
|
||||
?>
|
||||
<!--
|
||||
Seite: Beitrag erstellen
|
||||
Inhalt: Formular für die Erstellung eines neuen Beitrags
|
||||
-->
|
||||
<form method="post" action="php/controller/updateArticle-controller.php" id="editor-form" class="article-editor-scope.editor-container article-editor-scope editor-container">
|
||||
|
||||
<main class="editor-main">
|
||||
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "internal_error"): ?>
|
||||
<p class="alert-message is-error">
|
||||
Es ist ein Fehler beim Speichern aufgetreten. Bitte versuche es erneut.
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
<?php if (isset($_SESSION["message"]) && $_SESSION["message"] == "missing_parameters"): ?>
|
||||
<p class="alert-message is-error">
|
||||
Jeder Beitrag muss einen Titel, Kategorie und Inhalt besitzen.
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
<input type="text" id="title" name="title" value="<?php if (isset($title) && !empty($title)){echo htmlspecialchars($title);} ?>" placeholder="Titel hier eingeben" required>
|
||||
<textarea id="content" name="content" placeholder="Schreibe deinen Beitrag...">
|
||||
<?php if (isset($content) && !empty($content)){echo htmlspecialchars($content);} ?>
|
||||
</textarea>
|
||||
</main>
|
||||
|
||||
<!-- Seitenleiste -->
|
||||
<aside class="editor-sidebar">
|
||||
|
||||
<div class="sidebar-block">
|
||||
<button type="submit" class="btn-publish">Änderungen speichern</button>
|
||||
</div>
|
||||
|
||||
<div class="sidebar-block">
|
||||
<label for="category">Kategorie <span class="required">*</span></label>
|
||||
<select id="category" name="category" required>
|
||||
<option value="<?php if (isset($category) && !empty($category)){echo htmlspecialchars($category);} ?>" disabled selected>Kategorie wählen...</option>
|
||||
|
||||
<optgroup label="Sprachen">
|
||||
<option value="deutsch">Deutsch</option>
|
||||
<option value="englisch">Englisch</option>
|
||||
<option value="franzoesisch">Französisch</option>
|
||||
<option value="latein">Latein</option>
|
||||
<option value="literatur">Literatur</option>
|
||||
</optgroup>
|
||||
|
||||
<optgroup label="MINT">
|
||||
<option value="mathe">Mathematik</option>
|
||||
<option value="biologie">Biologie</option>
|
||||
<option value="chemie">Chemie</option>
|
||||
<option value="physik">Physik</option>
|
||||
<option value="informatik">Informatik</option>
|
||||
<option value="astronomie">Astronomie</option>
|
||||
</optgroup>
|
||||
|
||||
<optgroup label="Gesellschaft & Werte">
|
||||
<option value="geschichte">Geschichte</option>
|
||||
<option value="erdkunde">Erdkunde</option>
|
||||
<option value="sozialkunde">Sozialkunde</option>
|
||||
<option value="wirtschaft">Wirtschaftskunde</option>
|
||||
<option value="religion">Religion</option>
|
||||
<option value="ethik">Ethikunterricht</option>
|
||||
<option value="philosophie">Philosophie</option>
|
||||
<option value="psychologie">Psychologie</option>
|
||||
<option value="kunst">Kunst</option>
|
||||
<option value="musik">Musik</option>
|
||||
<option value="theater">Theater</option>
|
||||
</optgroup>
|
||||
|
||||
<optgroup label="Technik & Praxis">
|
||||
<option value="technik">Technik</option>
|
||||
<option value="werken">Werken</option>
|
||||
<option value="hauswirtschaft">Hauswirtschaft</option>
|
||||
<option value="sport">Sport</option>
|
||||
</optgroup>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="sidebar-block">
|
||||
<label for="tags">Schlagwörter</label>
|
||||
<input type="text" id="tags" name="tags" value="<?php if (isset($tags) && !empty($tags)){echo htmlspecialchars($tags);} ?>" placeholder="z.B. Technik, IT (mit Komma trennen)">
|
||||
</div>
|
||||
|
||||
</aside>
|
||||
|
||||
</form>
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once '../model/LocalArticleManager.php';
|
||||
require_once '../model/ArticleManager.php';
|
||||
|
||||
// Angaben aus übergebener ID setzen:
|
||||
if (isset($_GET["id"])){ // TODO: Später aus Session den Nutzer auslesen und Autorenrechte prüfen!
|
||||
try {
|
||||
$articleManager = ArticleManager::getInstance();
|
||||
$article = $articleManager->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();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user