<commit_before><?php
require_once('common.php');
?>
<?php
$topicId = $_GET['topicId'];
if (!empty ($_POST['comment'])) {
    $comm = $_POST['comment'];
    $userId2 = $_SESSION['userId'];
    $mysqli->query("INSERT INTO Comment (UserId, TopicId, Comment) VALUES ('$userId2', '$topicId', '$comm')");
}
?>
<?php
$sqlString = "SELECT *  FROM Comment
                  JOIN Topic
                  ON Comment.TopicId = Topic.TopicId
                  JOIN User
                  ON Comment.UserId = User.UserId
                  JOIN Role
                  ON User.RoleId=Role.RoleId
                  WHERE Topic.TopicId = '$topicId' AND Comment.CommentStatus = 'Active'
                  ORDER BY Comment.CommentParentId ASC ";
$searchTopics = $mysqli->query($sqlString);

$arrayComments = array();
foreach ($searchTopics as $value) {
    if (!empty($value["Comment"])) {
        if ($value["CommentParentId"] == 0) {
            $arrayComments["id" . $value["CommentId"]][] = $value;
        } else {
            $arrayComments["id" . $value["CommentParentId"]][] = $value;
        }
    }
}
?>
<body class="mainbody">
<!--<script src="ajax.js"></script>-->
<div class="container">
    <?php
    require('header.php');
    ?>
    <div class="regform">
        <?php
        $i = 0;
        $commentId = null;
        foreach ($arrayComments as $key => $row) {
            foreach ($row as $valueCommentParrent) {
                $i++;
                $commentId = $valueCommentParrent["CommentId"];
//                var_dump($commentId);
            if ($valueCommentParrent["CommentParentId"] == 0) {
                ?>

                <div class="lefttabel">
                    <h3> <?php echo $valueCommentParrent['TopicName']; ?></h3>
                    <br>

                    <p id="comment"><?php echo $valueCommentParrent['Comment']; ?></p>
                    <br>

                    <p id="postedBy">Posted by <?php echo $valueCommentParrent['UserName']; ?>,
                        <?php echo $valueCommentParrent['Date']; ?></p>
                    <hr>
                    <div id="hiddenForm<?php echo $i; ?>">
                        <textarea id="commentInput<?php echo $i; ?>" title="textarea"></textarea>
                        <button class="button1" id="submitHidden<?php echo $i; ?>">Submit</button>
                    </div>
                    <?php if (isset($_SESSION['userId']) || $_SESSION['userId'] != null) { ?>
                    <button id="replyButton<?php echo $i; ?>" type="button" value="hide/show">Reply</button>
                    <?php } ?>
                </div>
                <div class="righttabel">
                    <p><cite><?php echo $valueCommentParrent['UserName']; ?></cite></p>

                    <p><?php echo $valueCommentParrent["RoleName"]; ?></p>
                    <?php
                    $userId = $valueCommentParrent['UserId'];
                    $sqlStringP = "SELECT * FROM Comment
                        WHERE Comment.UserId = '$userId' ";
                    $countResult = $mysqli->query($sqlStringP);
                    $count = $countResult->num_rows;
                    ?>
                    <p> Posts: <?php echo "$count"; ?></p>
                    <?php
                    if ($_SESSION["userId"] == 1 || $_SESSION["userId"] == 2 || $_SESSION["userId"] == $userId) {
                        echo "<a href=\"editComment.php?commentId=$commentId&topicId=$topicId\">Edit          |</a>";
                        echo "<button class=\"deleteButton\" onclick=\"deleteFunction(' . $commentId . ')\">Delete</button>";
//                            <a href=\"deleteComment.php?commentId=$commentId&topicId=$topicId\">Delete</a>";
                    }
                    ?>
                </div>
                <div class="clearFix"></div>
                <?php
            } else {
                echo "<div>";
                echo "<div id=\"reply\">";
                echo '<p>';
                echo $valueCommentParrent["Comment"];
                ?>
                <p id="repliedBy">Posted by <?php echo $valueCommentParrent['UserName']; ?>,
                    <?php echo $valueCommentParrent['Date']; ?></p>
            <?php
            echo '</p>';
            if ($_SESSION["userId"] == 1 || $_SESSION["userId"] == 2 || $_SESSION["userId"] == $valueCommentParrent) {
                echo "</div>";
                echo "<div id=\"userOption\">";
                echo "<a href=\"editComment.php?commentId=$commentId&topicId=$topicId\">Edit          |</a>";
                echo "<button class=\"deleteButton\" onclick=\"deleteFunction(' . $commentId . ')\">Delete</button>";
                echo "</div>";
                echo "<div class=\"clearFix\"></div>";
                echo "</div>";
            } else {
                echo "</div>";
                echo "</div>";
            }
            }
            ?>
                <script>
                    $(document).ready(function () {
                        $('#hiddenForm<?php echo $i; ?>').hide();
                        $('#replyButton<?php echo $i; ?>').on('click', function () {
                            $('#hiddenForm<?php echo $i; ?>').show();
                        });
                        $('#submitHidden<?php echo $i; ?>').on('click', function () {
                            $('#hiddenForm<?php echo $i; ?>').hide();
                        })
                    });
                </script>

                <script>
                    $(document).ready(function () {
                        var topicId = <?php echo $topicId; ?>;
                        var commentId = <?php echo $commentId; ?>;
                        var userId = <?php echo $_SESSION["userId"];?>;
                        $("#submitHidden<?php echo $i; ?>").click(function () {
                            var replyComment = document.getElementById("commentInput<?php echo $i; ?>").value;
                            $.ajax({
                                method: "POST",<commit_msg>hide reply button after it is pushed and added some style<commit_after><?php
require_once('common.php');
?>
<?php
$topicId = $_GET['topicId'];
if (!empty ($_POST['comment'])) {
    $comm = $_POST['comment'];
    $userId2 = $_SESSION['userId'];
    $mysqli->query("INSERT INTO Comment (UserId, TopicId, Comment) VALUES ('$userId2', '$topicId', '$comm')");
}
?>
<?php
$sqlString = "SELECT *  FROM Comment
                  JOIN Topic
                  ON Comment.TopicId = Topic.TopicId
                  JOIN User
                  ON Comment.UserId = User.UserId
                  JOIN Role
                  ON User.RoleId=Role.RoleId
                  WHERE Topic.TopicId = '$topicId' AND Comment.CommentStatus = 'Active'
                  ORDER BY Comment.CommentParentId ASC ";
$searchTopics = $mysqli->query($sqlString);

$arrayComments = array();
foreach ($searchTopics as $value) {
    if (!empty($value["Comment"])) {
        if ($value["CommentParentId"] == 0) {
            $arrayComments["id" . $value["CommentId"]][] = $value;
        } else {
            $arrayComments["id" . $value["CommentParentId"]][] = $value;
        }
    }
}
?>
<body class="mainbody">

<div class="container">
    <?php
    require('header.php');
    ?>
    <div class="regform">
        <?php
        $i = 0;
        $commentId = null;
        foreach ($arrayComments as $key => $row) {
            foreach ($row as $valueCommentParrent) {
                $i++;
                $commentId = $valueCommentParrent["CommentId"];

            if ($valueCommentParrent["CommentParentId"] == 0) {
                ?>

                <div class="lefttabel">
                    <h3> <?php echo $valueCommentParrent['TopicName']; ?></h3>
                    <br>

                    <p id="comment"><?php echo $valueCommentParrent['Comment']; ?></p>
                    <br>

                    <p id="postedBy">Posted by <?php echo $valueCommentParrent['UserName']; ?>,
                        <?php echo $valueCommentParrent['Date']; ?></p>
                    <hr>
                    <div id="hiddenForm<?php echo $i; ?>">
                        <textarea id="commentInput<?php echo $i; ?>" title="textarea"></textarea>
                        <br><br>
                        <button class="button1" id="submitHidden<?php echo $i; ?>">Submit</button>
                    </div>
                    <?php if (isset($_SESSION['userId']) || $_SESSION['userId'] != null) { ?>
                    <button id="replyButton<?php echo $i; ?>" type="button" value="hide/show">Reply</button>
                    <?php } ?>
                </div>
                <div class="righttabel">
                    <p><cite><?php echo $valueCommentParrent['UserName']; ?></cite></p>

                    <p><?php echo $valueCommentParrent["RoleName"]; ?></p>
                    <?php
                    $userId = $valueCommentParrent['UserId'];
                    $sqlStringP = "SELECT * FROM Comment
                        WHERE Comment.UserId = '$userId' ";
                    $countResult = $mysqli->query($sqlStringP);
                    $count = $countResult->num_rows;
                    ?>
                    <p> Posts: <?php echo "$count"; ?></p>
                    <?php
                    if ($_SESSION["userId"] == 1 || $_SESSION["userId"] == 2 || $_SESSION["userId"] == $userId) {
                        echo "<a href=\"editComment.php?commentId=$commentId&topicId=$topicId\">Edit          |</a>";
                        echo "<button class=\"deleteButton\" onclick=\"deleteFunction(' . $commentId . ')\">Delete</button>";
//                            <a href=\"deleteComment.php?commentId=$commentId&topicId=$topicId\">Delete</a>";
                    }
                    ?>
                </div>
                <div class="clearFix"></div>
                <?php
            } else {
                echo "<div>";
                echo "<div id=\"reply\">";
                echo '<p>';
                echo $valueCommentParrent["Comment"];
                ?>
                <p id="repliedBy">Posted by <?php echo $valueCommentParrent['UserName']; ?>,
                    <?php echo $valueCommentParrent['Date']; ?></p>
            <?php
            echo '</p>';
            if ($_SESSION["userId"] == 1 || $_SESSION["userId"] == 2 || $_SESSION["userId"] == $valueCommentParrent) {
                echo "</div>";
                echo "<div id=\"userOption\">";
                echo "<a href=\"editComment.php?commentId=$commentId&topicId=$topicId\">Edit          |</a>";
                echo "<button class=\"deleteButton\" onclick=\"deleteFunction(' . $commentId . ')\">Delete</button>";
                echo "</div>";
                echo "<div class=\"clearFix\"></div>";
                echo "</div>";
            } else {
                echo "</div>";
                echo "</div>";
            }
            }
            ?>
                <script>
                    $(document).ready(function () {
                        $('#hiddenForm<?php echo $i; ?>').hide();
                        $('#replyButton<?php echo $i; ?>').on('click', function () {
                            $('#hiddenForm<?php echo $i; ?>').show();
                            $("#replyButton<?php echo $i; ?>").hide();
                        });
                        $('#submitHidden<?php echo $i; ?>').on('click', function () {
                            $('#hiddenForm<?php echo $i; ?>').hide();
                        })
                    });
                </script>

                <script>
                    $(document).ready(function () {
                        var topicId = <?php echo $topicId; ?>;
                        var commentId = <?php echo $commentId; ?>;
                        var userId = <?php echo $_SESSION["userId"];?>;
                        $("#submitHidden<?php echo $i; ?>").click(function () {
                            var replyComment = document.getElementById("commentInput<?php echo $i; ?>").value;
                            $.ajax({
                                method: "POST",
{'fraction_non_alphanumeric': 0.17117487461974842, 'fraction_numerical': 0.0019731974019567542, 'mean_word_length': 1.4455166867712104, 'pattern_counts': {'":': 0, '<': 199, '<?xml version=': 0, '>': 209, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7074455', 'n_tokens_mistral': 3187, 'n_tokens_neox': 3060, 'n_words': 655}