Files
awo-hamburg-intranet/module/knowledgecenter_update/Views/post_cardform_discussion.php

195 lines
10 KiB
PHP
Raw Normal View History

<div class="titlebar_small">
<h2><?=$translation->get("Diskussion")?></h2>
<span data-toggle-target="discussion_form" class="toggle post-action-btn-small green">+</span>
</div>
<hr>
<div data-toggle-target="discussion_form" class="toggle_container">
<form id="newCommentForm">
<input type="hidden" name="post_id" value="<?php echo $postId; ?>">
<input type="hidden" name="author_id" value="<?php echo $GLOBALS['main_contact']['id']; ?>">
<input type="text" class="input_round" name="text" placeholder="Text..." required>
<button class="send_comment" title="Senden" type="submit">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-send-fill"
viewBox="0 0 16 16">
<path
d="M15.964.686a.5.5 0 0 0-.65-.65L.767 5.855H.766l-.452.18a.5.5 0 0 0-.082.887l.41.26.001.002 4.995 3.178 3.178 4.995.002.002.26.41a.5.5 0 0 0 .886-.083zm-1.833 1.89L6.637 10.07l-.215-.338a.5.5 0 0 0-.154-.154l-.338-.215 7.494-7.494 1.178-.471z" />
</svg>
</button>
</form>
<hr>
</div>
<div id="commentsContainer">
</div>
<script>
var currentUserId = <?php echo json_encode($GLOBALS['main_contact']['id']); ?>;
$(document).ready(function () {
// Funktion zum Anzeigen des Antwortformulars
$(document).on("click", ".replyCommentBtn", function () {
var parentId = $(this).data("id");
var $comment = $(this).closest('.comment');
// Finde den entsprechenden Reply-Container
var $replyContainer = $comment.find("div.replies[data-parent-id='" + parentId + "']");
// Falls der Container nicht existiert, erstelle ihn
if ($replyContainer.length === 0) {
$comment.append("<div class='replies' data-parent-id='" + parentId + "'></div>");
$replyContainer = $comment.find("div.replies[data-parent-id='" + parentId + "']");
}
// Prüfen, ob bereits ein Reply-Formular vorhanden ist
if ($replyContainer.find(".replyCommentForm").length > 0) {
// Eventuell: Formular toggle ausblenden/anzeigen oder
// einfach nichts tun, um Mehrfach-Erzeugung zu vermeiden
return; // Verhindert, dass ein weiteres Formular hinzugefügt wird
}
// Erzeuge das Reply-Formular und füge es ein
var replyFormHtml = "<form class='replyCommentForm'>" +
"<input type='hidden' name='parent_id' value='" + parentId + "'>" +
"<input type='hidden' name='post_id' value='<?php echo $postId; ?>'>" +
"<input type='hidden' name='author_id' value='<?php echo $GLOBALS['main_contact']['id']; ?>'>" +
"<input type='hidden' name='action' value='insert_comment'>" +
"<input type='text' class='input_round' name='text' placeholder='<?=$translation->get("reply_placeholder")?>'>" +
"<button class='send_comment' type='submit'>↪</button>" +
"</form>";
$replyContainer.append(replyFormHtml);
});
// Absenden des Antwortformulars
$(document).on("submit", ".replyCommentForm", function (e) {
e.preventDefault();
var formData = $(this).serialize();
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", formData, function (response) {
if (response.success) {
// Kommentare neu laden alternativ kann das neue Reply direkt angezeigt werden
loadComments();
} else {
alert("Fehler: " + response.error);
}
}, "json");
});
// Die bestehende loadComments()-Funktion anpassen:
function loadComments() {
// $("#overlayLoader").show(); // Zeige den Loader an
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", {
action: 'get_post_comments',
post_id: <?php echo $postId; ?>
}, function (response) {
if (response.success) {
var html = '';
if (response.comments.length === 0) {
html = "<div class='no-comments'><?=$translation->get("no_comments")?></div>";
} else {
var commentMap = {};
response.comments.forEach(function (comment) {
commentMap[comment.id] = comment;
comment.children = []; // initialisiere ein Array für Antworten
});
var roots = [];
response.comments.forEach(function (comment) {
if (comment.parent_id && commentMap[comment.parent_id]) {
commentMap[comment.parent_id].children.push(comment);
} else {
roots.push(comment);
}
});
function renderComment(comment, isLast) {
var d = new Date(comment.created_at);
var formattedDate = d.toLocaleString("de-DE", {
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit"
});
// Füge die Zusatzklasse hinzu, wenn isLast true ist
var extraClass = isLast ? " last-comment" : "";
var commentHtml = "<div class='comment" + extraClass + "' data-id='" + comment.id + "'>";
commentHtml += " <div class='comment-content'>";
commentHtml += " <div class='comment-header'>";
commentHtml += " <strong>" + comment.author + "</strong> - <small>" + formattedDate + "</small>";
commentHtml += " <p>" + comment.text + "</p>";
commentHtml += " </div>";
commentHtml += " <div class='comment-toolbar'>";
commentHtml += " <span class='replyCommentBtn post-action-btn-small green' data-id='" + comment.id + "'>↪</span>";
if (comment.author_id == currentUserId) {
commentHtml += " <span class='deleteCommentBtn post-action-btn-small' data-id='" + comment.id + "' title='Kommentar löschen'>✕</span>";
}
commentHtml += " </div>";
commentHtml += " </div>";
// Wenn Kinder vorhanden sind, durchlaufe diese
if (comment.children && comment.children.length > 0) {
commentHtml += " <div class='replies' data-parent-id='" + comment.id + "'>";
comment.children.forEach(function (child, index, array) {
// Bestimme, ob es das letzte Kind im Array ist
var isLastChild = (index === array.length - 1);
commentHtml += renderComment(child, isLastChild);
});
commentHtml += " </div>";
}
commentHtml += "</div>";
return commentHtml;
}
// Beim Rendern der Wurzeln rufst du renderComment() auf, hier kannst du z.B. false übergeben, da es beim Wurzelelement nicht relevant ist:
roots.forEach(function (comment) {
// Wenn du weißt, dass das Wurzelelement möglicherweise auch das letzte im Container ist,
// kannst du den selben Check machen (z.B. isLast = index === roots.length-1)
html += renderComment(comment, false);
});
}
$("#commentsContainer").html(html);
}
}, "json").always(function () {
// $("#overlayLoader").hide(); // Blende den Loader aus, egal ob Erfolg oder Fehler
});
}
// Lade Kommentare beim Initialisieren
loadComments();
// Bestehender Code für das neue Kommentarformular bleibt erhalten...
$("#newCommentForm").on("submit", function (e) {
e.preventDefault();
var $btn = $(this).find("button[type='submit']");
$btn.prop("disabled", true);
var formData = $(this).serialize() + "&action=insert_comment";
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", formData, function (response) {
if (response.success) {
loadComments();
$("#newCommentForm input[name='text']").val('');
} else {
alert("Fehler: " + response.error);
}
$btn.prop("disabled", false); // Reaktivieren nach Abschluss
}, "json");
});
// Löschen eines Kommentars (bestehender Code) bleibt grundsätzlich gleich.
$(document).on("click", ".deleteCommentBtn", function () {
var commentId = $(this).data("id");
if (confirm("Kommentar wirklich löschen?")) {
$.post("/module/knowledgecenter_update/Ajax/Ajax.php", { action: 'delete_comment', comment_id: commentId, main_contact_id: <?php echo $GLOBALS["main_contact"]["id"]; ?> }, function (response) {
if (response.success) {
loadComments();
} else {
alert("Fehler: " + response.error);
}
}, "json");
}
});
});
</script>