From 5c0314a75d2722bb99918ef44473f4204462835a Mon Sep 17 00:00:00 2001 From: cflip <36554078+cflip@users.noreply.github.com> Date: Wed, 14 Apr 2021 17:23:56 -0600 Subject: Add 'date edited' attribute to posts --- includes/functions_insert.php | 4 ++-- model/Post.php | 15 +++++++++++---- model/Thread.php | 4 ++-- setup.sql | 3 ++- viewcategory.php | 2 +- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/includes/functions_insert.php b/includes/functions_insert.php index e13b80e..4f60701 100644 --- a/includes/functions_insert.php +++ b/includes/functions_insert.php @@ -16,7 +16,7 @@ function insert_thread($dbc, $thread_subject, $thread_cat, $thread_author) { } function insert_post($dbc, $post_content, $post_thread, $post_author, $post_category) { - $sql = "INSERT INTO posts(post_content, post_date, post_thread, post_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);"; + $sql = "INSERT INTO posts(post_content, post_date_created, post_thread, post_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);"; $stmt = mysqli_stmt_init($dbc); if (!mysqli_stmt_prepare($stmt, $sql)) { @@ -32,4 +32,4 @@ function insert_post($dbc, $post_content, $post_thread, $post_author, $post_cate $sql = "UPDATE threads SET thread_date_lastpost = CONVERT_TZ(NOW(), 'SYSTEM', '+00:00') WHERE thread_id = " . $post_thread . ";"; mysqli_query($dbc, $sql); -} \ No newline at end of file +} diff --git a/model/Post.php b/model/Post.php index 5d85f20..d7aba72 100644 --- a/model/Post.php +++ b/model/Post.php @@ -25,13 +25,14 @@ function add_quote($dbc, $thread_id, $matches) { class Post { public $id; public $content; - public $date; + public $date_created; + public $date_edited; public $thread; public $author; function get_from_database($id, $dbc) { // TODO: Potential SQL injection risk? - $sql = "SELECT post_content, post_date, post_thread, post_author FROM posts WHERE post_id = " . mysqli_real_escape_string($dbc, $id); + $sql = "SELECT post_content, post_date_created, post_date_edited, post_thread, post_author FROM posts WHERE post_id = " . mysqli_real_escape_string($dbc, $id); $result = mysqli_query($dbc, $sql); if (!$result) { @@ -39,11 +40,13 @@ class Post { } if (mysqli_num_rows($result) == 0) { + return 0; } else { while ($row = mysqli_fetch_assoc($result)) { $this->id = $id; $this->content = $row['post_content']; - $this->date = $row['post_date']; + $this->date_created = $row['post_date_created']; + $this->date_edited = $row['post_date_edited']; $this->thread = new Thread(); $this->thread->get_from_database($row['post_thread'], $dbc); @@ -54,12 +57,16 @@ class Post { } mysqli_free_result($result); + return 1; } function display_content($dbc) { echo '
#' . $this->id . ''; echo ' Posted by ' . $this->author->name . ''; - echo ' on ' . date('m/d/Y g:ia', strtotime($this->date)); + echo ' on ' . date('m/d/Y g:ia', strtotime($this->date_created)); + if (!is_null($this->date_edited)) { + echo ' edited ' . date('m/d/Y g:ia', strtotime($this->date_edited)) . ''; + } if (isset($_SESSION['signed_in']) && $_SESSION['user_id'] == $this->author->id) { echo ''; echo '[Edit] '; diff --git a/model/Thread.php b/model/Thread.php index aa48cfd..a9dc690 100644 --- a/model/Thread.php +++ b/model/Thread.php @@ -65,7 +65,7 @@ class Thread { } function get_latest_post($dbc) { - $sql = "SELECT post_id FROM posts WHERE post_thread = " . $this->id . " ORDER BY post_date DESC LIMIT 1"; + $sql = "SELECT post_id FROM posts WHERE post_thread = " . $this->id . " ORDER BY post_date_created DESC LIMIT 1"; $result = mysqli_query($dbc, $sql); if (!$result) { @@ -108,4 +108,4 @@ function get_all_threads($dbc) { mysqli_free_result($result); return $threads; -} \ No newline at end of file +} diff --git a/setup.sql b/setup.sql index b362798..6a96b8a 100644 --- a/setup.sql +++ b/setup.sql @@ -31,7 +31,8 @@ CREATE TABLE threads ( CREATE TABLE posts ( post_id INT(8) NOT NULL AUTO_INCREMENT, post_content TEXT NOT NULL, - post_date DATETIME NOT NULL, + post_date_created DATETIME NOT NULL, + post_date_edited DATETIME, post_thread INT(8) NOT NULL, post_author INT(8) NOT NULL, PRIMARY KEY (post_id) diff --git a/viewcategory.php b/viewcategory.php index 0d69ed8..a10afce 100644 --- a/viewcategory.php +++ b/viewcategory.php @@ -59,7 +59,7 @@ if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) { echo ' by ' . $thread->author->name . ' on ' . date('M d, Y', strtotime($thread->date_created)) . ''; if (!is_null($latest_post)) { - echo 'by ' . $latest_post->author->name . ' on ' . $latest_post->date . ''; + echo 'by ' . $latest_post->author->name . ' on ' . $latest_post->date_created . ''; } else { echo 'No posts yet!'; } -- cgit v1.2.3