diff --git a/content/showArticle.php b/content/showArticle.php index 0038341..5c9e5dc 100644 --- a/content/showArticle.php +++ b/content/showArticle.php @@ -14,28 +14,17 @@ if (isset($_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) { $_SESSION["message"] = "internal_error"; } } ?> - -

@@ -43,28 +32,9 @@ if (isset($_GET["id"])) {

- -

- Es ist ein Fehler aufgetreten. Die ID konnte nicht ausgelesen werden. Bitte versuche es erneut. -

- - - -

- Jeder Beitrag muss einen Titel, Kategorie und Inhalt besitzen. -

- - - -

- Dein Beitrag wurde erfolgreich bearbeitet! -

- -
- @@ -80,7 +50,6 @@ if (isset($_GET["id"])) {
-
@@ -107,8 +76,7 @@ if (isset($_GET["id"])) { -
@@ -129,12 +97,14 @@ if (isset($_GET["id"])) {

getContent())); ?>

- + + +
getId()])): ?> @@ -182,8 +152,10 @@ if (isset($_GET["id"])) { -

Du musst angemeldet sein, um einen Kommentar zu schreiben.

+ -
\ No newline at end of file diff --git a/css/showArticle.css b/css/showArticle.css index e55855b..8d5c353 100644 --- a/css/showArticle.css +++ b/css/showArticle.css @@ -196,4 +196,16 @@ margin: 0.5rem 0; color: #475569; font-weight: 600; +} +.comment-login-hint { + margin-top: 2rem; + padding: 1.5rem; + background-color: #f8fafc; + border: 1px solid #e2e8f0; + border-radius: 10px; + text-align: center; +} + +.comment-login-hint p { + margin-bottom: 1rem; } \ No newline at end of file diff --git a/js/comments.js b/js/comments.js index 46992b0..92663c3 100644 --- a/js/comments.js +++ b/js/comments.js @@ -1,13 +1,10 @@ /** * Initialisiert die Kommentarfunktion. * - * Ermöglicht: - * - Erstellen neuer Kommentare - * - Antworten auf bestehende Kommentare - * - AJAX-Kommunikation ohne Seitenneuladen + * Kommentare werden per AJAX gespeichert, + * ohne dass die Seite neu geladen werden muss. */ document.addEventListener("DOMContentLoaded", function () { - const form = document.getElementById("comment-form"); const commentsList = document.getElementById("comments-list"); const commentContent = document.getElementById("comment-content"); @@ -19,65 +16,44 @@ document.addEventListener("DOMContentLoaded", function () { } /** - * Registriert die Antwort-Buttons. - * - * Beim Klick wird die ID des Eltern-Kommentars gespeichert, - * damit die neue Nachricht als Antwort angelegt werden kann. + * Registriert alle Antwort-Buttons. */ document.querySelectorAll(".reply-button").forEach(function (button) { - button.addEventListener("click", function () { - parentCommentInput.value = button.dataset.commentId; - - replyInfo.textContent = - "Antwort auf " + button.dataset.author; - + replyInfo.textContent = "Antwort auf " + button.dataset.author; replyInfo.style.display = "block"; - commentContent.focus(); }); }); /** - * Verarbeitet das Absenden eines Kommentars. - * - * Die Daten werden per AJAX an den Server gesendet, - * sodass die Seite nicht neu geladen werden muss. + * Sendet Kommentare per AJAX an den Server. */ form.addEventListener("submit", function (event) { - event.preventDefault(); const formData = new FormData(form); const parentCommentId = parentCommentInput.value; - /* - * Sendet den Kommentar an den Server - * und speichert ihn in der Datenbank. - */ fetch("php/ajax/add-comment.php", { method: "POST", body: formData }) .then(response => response.json()) .then(data => { - if (!data.success) { alert(data.message); return; } - const emptyMessage = - commentsList.querySelector(".no-comments-message"); + const emptyMessage = commentsList.querySelector(".no-comments-message"); if (emptyMessage) { emptyMessage.remove(); } - const commentElement = - document.createElement("div"); - + const commentElement = document.createElement("div"); commentElement.classList.add("comment-item"); if (parentCommentId) { @@ -92,60 +68,37 @@ document.addEventListener("DOMContentLoaded", function () {

${escapeHtml(data.content).replace(/\n/g, "
")}

`; - /* - * Antworten werden unter dem - * zugehörigen Kommentar angezeigt. - */ if (parentCommentId) { - - const parentComment = document.querySelector( + const parentReplies = document.querySelector( `.comment-item[data-comment-id="${parentCommentId}"] .comment-replies` ); - if (parentComment) { - parentComment.appendChild(commentElement); + if (parentReplies) { + parentReplies.appendChild(commentElement); } - } else { - - /* - * Normale Kommentare werden - * oben in die Liste eingefügt. - */ - commentElement.dataset.commentId = ""; - commentsList.prepend(commentElement); } - /* - * Formular zurücksetzen. - */ commentContent.value = ""; parentCommentInput.value = ""; replyInfo.textContent = ""; replyInfo.style.display = "none"; }) .catch(() => { - - alert( - "Kommentar konnte nicht gesendet werden." - ); + alert("Kommentar konnte nicht gesendet werden."); }); }); /** - * Escaped HTML-Sonderzeichen zur Vermeidung - * von XSS-Angriffen. + * Entfernt HTML-Sonderzeichen aus Nutzereingaben. * * @param {string} text Zu bereinigender Text - * @returns {string} HTML-sicherer Text + * @returns {string} Sicherer Text */ function escapeHtml(text) { - const div = document.createElement("div"); - div.textContent = text; - return div.innerHTML; } }); \ No newline at end of file diff --git a/php/ajax/add-comment.php b/php/ajax/add-comment.php index d40b460..0b9ef29 100644 --- a/php/ajax/add-comment.php +++ b/php/ajax/add-comment.php @@ -8,7 +8,7 @@ require_once "../model/CommentManager.php"; if (!isset($_SESSION["user_email"])) { echo json_encode([ "success" => false, - "message" => "Du musst angemeldet sein." + "message" => "Du musst angemeldet sein, um zu kommentieren." ]); exit(); } @@ -17,7 +17,7 @@ $articleId = $_POST["article_id"] ?? null; $content = trim($_POST["content"] ?? ""); $parentCommentId = $_POST["parent_comment_id"] ?? null; -if ($parentCommentId === "") { +if ($parentCommentId === "" || $parentCommentId === "0") { $parentCommentId = null; }