From 2458ca6964401c0bd2cd809d303dfbcaea3ead90 Mon Sep 17 00:00:00 2001 From: cflip <36554078+cflip@users.noreply.github.com> Date: Wed, 11 Aug 2021 15:14:18 -0600 Subject: Add getters for nested objects --- includes/model/Post.php | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'includes/model/Post.php') diff --git a/includes/model/Post.php b/includes/model/Post.php index eae3d3e..ab3602b 100755 --- a/includes/model/Post.php +++ b/includes/model/Post.php @@ -53,8 +53,8 @@ class Post public $id; public $content; public $date_created; - public $thread; - public $author; + public $thread_id; + public $author_id; private $has_value = false; @@ -70,10 +70,8 @@ class Post $this->id = $id; $this->content = $result[0]['post_content']; $this->date_created = $result[0]['post_date_created']; - $this->thread = new Thread($result[0]['post_thread']); - - $this->author = new User(); - $this->author->get_by_id($result[0]['post_author']); + $this->thread_id = $result[0]['post_thread']; + $this->author_id = $result[0]['post_author']; $this->has_value = true; } @@ -83,14 +81,27 @@ class Post return $this->has_value; } + public function get_parent_thread(): ?Thread + { + return new Thread($this->thread_id); + } + + public function get_author(): ?User + { + $result = new User(); + $result->get_by_id($this->author_id); + return $result; + } + /** * Get the post content from the database and return it as a string ready for HTML display */ function get_content(): string { // Build the header + $author = $this->get_author(); $result = '

#' . $this->id . ''; - $result .= ' Posted by ' . $this->author->name . ''; + $result .= ' Posted by ' . $author->name . ''; $result .= ' on ' . date('m/d/Y g:ia', strtotime($this->date_created)); if (Session::get()->is_signed_in() && Session::get()->get_current_user()->level == USER_LEVEL_MODERATOR) { $result .= '[Options]'; @@ -113,7 +124,7 @@ class Post // User must have permission to edit the post $current_user = Session::get()->get_current_user(); - if ($current_user->id != $this->author->id) { + if ($current_user->id != $this->author_id) { trigger_error("You don't have sufficient permissions to edit this post."); return; } @@ -141,7 +152,7 @@ class Post Database::get()->query("DELETE FROM posts WHERE post_id = ?", "i", $this->id); // Decrement the post count of the category - Database::get()->query("UPDATE categories SET `category_post_count` = `category_post_count` - '1' WHERE category_id = ?", "i", $this->thread->category->id); + Database::get()->query("UPDATE categories SET `category_post_count` = `category_post_count` - '1' WHERE category_id = ?", "i", $this->get_parent_thread()->category_id); } public static function create($post_content, $post_thread, $post_category) -- cgit v1.2.3