Kommentare bearbeiten
This commit is contained in:
@@ -193,6 +193,47 @@ if ($replyAuthor === null) {
|
||||
isset($_SESSION["user_email"])
|
||||
&& $_SESSION["user_email"] === $comment->getAuthor()
|
||||
): ?>
|
||||
<details class="edit-comment-details">
|
||||
<summary class="edit-comment-button">
|
||||
Kommentar bearbeiten
|
||||
</summary>
|
||||
|
||||
<form method="post"
|
||||
action="index.php?pfad=updateComment"
|
||||
class="edit-comment-form">
|
||||
|
||||
<input type="hidden"
|
||||
name="comment_id"
|
||||
value="<?php echo htmlspecialchars(
|
||||
(string) $comment->getId()
|
||||
); ?>">
|
||||
|
||||
<input type="hidden"
|
||||
name="article_id"
|
||||
value="<?php echo htmlspecialchars(
|
||||
(string) $comment->getArticleId()
|
||||
); ?>">
|
||||
|
||||
<label for="edit-comment-<?php
|
||||
echo htmlspecialchars((string) $comment->getId());
|
||||
?>">
|
||||
Kommentar bearbeiten
|
||||
</label>
|
||||
|
||||
<textarea
|
||||
id="edit-comment-<?php
|
||||
echo htmlspecialchars((string) $comment->getId());
|
||||
?>"
|
||||
name="content"
|
||||
required><?php echo htmlspecialchars(
|
||||
$comment->getContent()
|
||||
); ?></textarea>
|
||||
|
||||
<button type="submit" class="button">
|
||||
Änderungen speichern
|
||||
</button>
|
||||
</form>
|
||||
</details>
|
||||
|
||||
<form method="post"
|
||||
action="index.php?pfad=deleteComment"
|
||||
@@ -284,6 +325,48 @@ if ($replyAuthor === null) {
|
||||
&& $_SESSION["user_email"] === $reply->getAuthor()
|
||||
): ?>
|
||||
|
||||
<details class="edit-comment-details">
|
||||
<summary class="edit-comment-button">
|
||||
Antwort bearbeiten
|
||||
</summary>
|
||||
|
||||
<form method="post"
|
||||
action="index.php?pfad=updateComment"
|
||||
class="edit-comment-form">
|
||||
|
||||
<input type="hidden"
|
||||
name="comment_id"
|
||||
value="<?php echo htmlspecialchars(
|
||||
(string) $reply->getId()
|
||||
); ?>">
|
||||
|
||||
<input type="hidden"
|
||||
name="article_id"
|
||||
value="<?php echo htmlspecialchars(
|
||||
(string) $reply->getArticleId()
|
||||
); ?>">
|
||||
|
||||
<label for="edit-reply-<?php
|
||||
echo htmlspecialchars((string) $reply->getId());
|
||||
?>">
|
||||
Antwort bearbeiten
|
||||
</label>
|
||||
|
||||
<textarea
|
||||
id="edit-reply-<?php
|
||||
echo htmlspecialchars((string) $reply->getId());
|
||||
?>"
|
||||
name="content"
|
||||
required><?php echo htmlspecialchars(
|
||||
$reply->getContent()
|
||||
); ?></textarea>
|
||||
|
||||
<button type="submit" class="button">
|
||||
Änderungen speichern
|
||||
</button>
|
||||
</form>
|
||||
</details>
|
||||
|
||||
<form method="post"
|
||||
action="index.php?pfad=deleteComment"
|
||||
class="delete-comment-form">
|
||||
|
||||
@@ -39,8 +39,8 @@ if ($pfad === "deleteAccount") {
|
||||
include_once "php/controller/deleteAccount-controller.php";
|
||||
exit();
|
||||
}
|
||||
if ($pfad === "deleteComment") {
|
||||
include_once "php/controller/deleteComment-controller.php";
|
||||
if ($pfad === "updateComment") {
|
||||
include_once "php/controller/updateComment-controller.php";
|
||||
exit();
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
require_once "php/model/CommentManager.php";
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
|
||||
header("Location: index.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
if (!isset($_SESSION["user_email"])) {
|
||||
header("Location: index.php?pfad=login");
|
||||
exit();
|
||||
}
|
||||
|
||||
$commentId = filter_input(
|
||||
INPUT_POST,
|
||||
"comment_id",
|
||||
FILTER_VALIDATE_INT
|
||||
);
|
||||
|
||||
$articleId = filter_input(
|
||||
INPUT_POST,
|
||||
"article_id",
|
||||
FILTER_VALIDATE_INT
|
||||
);
|
||||
|
||||
$content = trim($_POST["content"] ?? "");
|
||||
|
||||
/*
|
||||
* Ohne gültige Beitrags-ID kann nicht sicher
|
||||
* zum ursprünglichen Beitrag zurückgeleitet werden.
|
||||
*/
|
||||
if (!$articleId) {
|
||||
header("Location: index.php");
|
||||
exit();
|
||||
}
|
||||
|
||||
/*
|
||||
* Kommentar-ID und Inhalt werden geprüft.
|
||||
*/
|
||||
if (!$commentId || $content === "") {
|
||||
$_SESSION["comment_message"] =
|
||||
"Der Kommentar darf nicht leer sein.";
|
||||
|
||||
$_SESSION["comment_message_type"] = "error";
|
||||
|
||||
header(
|
||||
"Location: index.php?pfad=showArticle&id="
|
||||
. urlencode((string) $articleId)
|
||||
. "#comments"
|
||||
);
|
||||
exit();
|
||||
}
|
||||
|
||||
try {
|
||||
$commentManager = CommentManager::getInstance();
|
||||
|
||||
$updated = $commentManager->updateComment(
|
||||
$commentId,
|
||||
$_SESSION["user_email"],
|
||||
$content
|
||||
);
|
||||
|
||||
if ($updated) {
|
||||
$_SESSION["comment_message"] =
|
||||
"Der Kommentar wurde erfolgreich bearbeitet.";
|
||||
|
||||
$_SESSION["comment_message_type"] = "success";
|
||||
} else {
|
||||
$_SESSION["comment_message"] =
|
||||
"Der Kommentar konnte nicht bearbeitet werden.";
|
||||
|
||||
$_SESSION["comment_message_type"] = "error";
|
||||
}
|
||||
|
||||
} catch (Throwable $e) {
|
||||
$_SESSION["comment_message"] =
|
||||
"Beim Bearbeiten des Kommentars ist ein Fehler aufgetreten.";
|
||||
|
||||
$_SESSION["comment_message_type"] = "error";
|
||||
}
|
||||
|
||||
header(
|
||||
"Location: index.php?pfad=showArticle&id="
|
||||
. urlencode((string) $articleId)
|
||||
. "#comments"
|
||||
);
|
||||
exit();
|
||||
@@ -72,4 +72,19 @@ interface CommentManagerDAO
|
||||
* @return void
|
||||
*/
|
||||
public function deleteCommentsByAuthor(string $author): void;
|
||||
/**
|
||||
* Bearbeitet einen Kommentar des angemeldeten Nutzers.
|
||||
*
|
||||
* Nur der Autor des Kommentars darf den Inhalt ändern.
|
||||
*
|
||||
* @param int $commentId ID des Kommentars
|
||||
* @param string $author E-Mail-Adresse des Autors
|
||||
* @param string $content Neuer Kommentarinhalt
|
||||
* @return bool true, wenn der Kommentar bearbeitet wurde
|
||||
*/
|
||||
public function updateComment(
|
||||
int $commentId,
|
||||
string $author,
|
||||
string $content
|
||||
): bool;
|
||||
}
|
||||
@@ -223,6 +223,44 @@ class DatabaseCommentManager implements CommentManagerDAO
|
||||
|
||||
return $comments;
|
||||
}
|
||||
/**
|
||||
* Bearbeitet einen eigenen Kommentar.
|
||||
*
|
||||
* Der Kommentar wird nur geändert, wenn er dem
|
||||
* angemeldeten Nutzer gehört.
|
||||
*
|
||||
* @param int $commentId ID des Kommentars
|
||||
* @param string $author E-Mail-Adresse des Autors
|
||||
* @param string $content Neuer Kommentarinhalt
|
||||
* @return bool true, wenn der Kommentar bearbeitet wurde
|
||||
*/
|
||||
public function updateComment(
|
||||
int $commentId,
|
||||
string $author,
|
||||
string $content
|
||||
): bool {
|
||||
try {
|
||||
$db = $this->getConnection();
|
||||
|
||||
$command = $db->prepare("
|
||||
UPDATE comments
|
||||
SET content = :content
|
||||
WHERE id = :commentId
|
||||
AND author = :author
|
||||
");
|
||||
|
||||
$command->execute([
|
||||
":content" => $content,
|
||||
":commentId" => $commentId,
|
||||
":author" => $author
|
||||
]);
|
||||
|
||||
return $command->rowCount() > 0;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
throw new RuntimeException("internal_error");
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Löscht einen eigenen Kommentar.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user