Antwortmöglichkeit

This commit is contained in:
2026-06-15 22:56:00 +02:00
parent dafc149bba
commit 7694396454
+24 -8
View File
@@ -3,11 +3,28 @@ include_once 'php/controller/showArticle-controller.php';
require_once 'php/model/CommentManager.php'; require_once 'php/model/CommentManager.php';
$comments = []; $comments = [];
$mainComments = [];
$repliesByParent = [];
if (isset($_GET["id"])) { if (isset($_GET["id"])) {
try { try {
$commentManager = CommentManager::getInstance(); $commentManager = CommentManager::getInstance();
$comments = $commentManager->getCommentsByArticle($_GET["id"]); $comments = $commentManager->getCommentsByArticle($_GET["id"]);
foreach ($comments as $comment) {
if ($comment->isReply()) {
$parentId = $comment->getParentCommentId();
if (!isset($repliesByParent[$parentId])) {
$repliesByParent[$parentId] = [];
}
$repliesByParent[$parentId][] = $comment;
} else {
$mainComments[] = $comment;
}
}
} catch (Exception $e) { } catch (Exception $e) {
$_SESSION["message"] = "internal_error"; $_SESSION["message"] = "internal_error";
} }
@@ -102,9 +119,8 @@ if (isset($_GET["id"])) {
<h2>Kommentare</h2> <h2>Kommentare</h2>
<div id="comments-list"> <div id="comments-list">
<?php if (!empty($comments)): ?> <?php if (!empty($mainComments)): ?>
<?php foreach ($comments as $comment): ?> <?php foreach ($mainComments as $comment): ?>
<?php if (!$comment->isReply()): ?>
<div class="comment-item" data-comment-id="<?php echo htmlspecialchars($comment->getId()); ?>"> <div class="comment-item" data-comment-id="<?php echo htmlspecialchars($comment->getId()); ?>">
<p> <p>
<strong><?php echo htmlspecialchars($comment->getAuthor()); ?></strong> <strong><?php echo htmlspecialchars($comment->getAuthor()); ?></strong>
@@ -121,8 +137,8 @@ if (isset($_GET["id"])) {
</button> </button>
<div class="comment-replies"> <div class="comment-replies">
<?php foreach ($comments as $reply): ?> <?php if (isset($repliesByParent[$comment->getId()])): ?>
<?php if ($reply->getParentCommentId() === $comment->getId()): ?> <?php foreach ($repliesByParent[$comment->getId()] as $reply): ?>
<div class="comment-item comment-reply"> <div class="comment-item comment-reply">
<p> <p>
<strong><?php echo htmlspecialchars($reply->getAuthor()); ?></strong> <strong><?php echo htmlspecialchars($reply->getAuthor()); ?></strong>
@@ -131,11 +147,10 @@ if (isset($_GET["id"])) {
<p><?php echo nl2br(htmlspecialchars($reply->getContent())); ?></p> <p><?php echo nl2br(htmlspecialchars($reply->getContent())); ?></p>
</div> </div>
<?php endif; ?>
<?php endforeach; ?> <?php endforeach; ?>
</div>
</div>
<?php endif; ?> <?php endif; ?>
</div>
</div>
<?php endforeach; ?> <?php endforeach; ?>
<?php else: ?> <?php else: ?>
<p class="no-comments-message"> <p class="no-comments-message">
@@ -149,6 +164,7 @@ if (isset($_GET["id"])) {
<input type="hidden" <input type="hidden"
name="article_id" name="article_id"
value="<?php echo htmlspecialchars($_GET["id"] ?? ""); ?>"> value="<?php echo htmlspecialchars($_GET["id"] ?? ""); ?>">
<input type="hidden" <input type="hidden"
name="parent_comment_id" name="parent_comment_id"
id="parent-comment-id" id="parent-comment-id"