From 60d2a676203040f4a668802f67c040ed661713ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zden=C4=9Bk=20Borovec?= Date: Mon, 6 May 2024 20:20:57 +0200 Subject: [PATCH] focus comment after posting --- docs/www/blog/article.php | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/docs/www/blog/article.php b/docs/www/blog/article.php index c5a2ad0..5f16bad 100644 --- a/docs/www/blog/article.php +++ b/docs/www/blog/article.php @@ -19,7 +19,7 @@ class BlogpostComment */ public function display_comment() { printf(" -
+
By: %s On: %s @@ -45,7 +45,9 @@ class BlogpostComment
", - $this->poster_name, date("Y-m-d H:i", strtotime($this->timestamp)), + $this->comment_id, + $this->poster_name, + date("Y-m-d H:i", strtotime($this->timestamp)), $this->comment_id, $this->comment_id, $this->content, @@ -232,15 +234,27 @@ class Blogpost * If the poster is not signed in, send "NULL" (as a string) as the $posterID * The same goes for $parentId (that is the parent comment, * if this one is a response) - * Returns: error message string or null on success. + * Returns: GUID PK of the newly added comment. */ function send_comment($conn, $blogId, $posterId, $content, $parentId) { + // If content is empty, do not post + if(empty($content)) { + return ""; + } + + // Get a uuid for the comment + $stmt = $conn->prepare("SELECT UUID()"); + $stmt->execute(); + $result = $stmt->fetch(PDO::FETCH_ASSOC); + $uuid = $result["UUID()"]; + // Prepare the statemtnt $stmt = $conn->prepare("INSERT INTO blogpost_comments - ( parent_id, blogpost_id, poster_id, content) VALUES - (:parent_id, :blogpost_id, :poster_id, :content);"); + ( comment_id, parent_id, blogpost_id, poster_id, content) VALUES + (:comment_id, :parent_id, :blogpost_id, :poster_id, :content);"); // Bind all the parameters + $stmt->bindValue(":comment_id", $uuid, PDO::PARAM_STR); $stmt->bindValue(":parent_id", $parentId == "NULL" ? NULL : $parentId, PDO::PARAM_STR); $stmt->bindValue(":blogpost_id", $blogId, PDO::PARAM_STR); @@ -251,7 +265,7 @@ function send_comment($conn, $blogId, $posterId, $content, $parentId) { // Execute the statement $stmt->execute(); - return null; + return $uuid; } /** @@ -378,10 +392,12 @@ if(isset($_POST["submit"])) { $posterId = isset($_SESSION["current_user"]) ? $_SESSION["current_user"]->user_id : "NULL"; // Try to send the comment - send_comment($conn, $blogId, $posterId, $commentContent, $parentId); + $commentId = send_comment($conn, $blogId, $posterId, + $commentContent, $parentId); // Redirect to this page with GET - header("Location: http://www.zdenekborovec-dev.cz/blog/article?guid=".$blogId); + header("Location: http://www.zdenekborovec-dev.cz/blog/article?guid=". + $blogId."#comment-".$commentId); } // Get the blog id.