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 ++++++++++++++++++++--------- includes/model/Thread.php | 24 +++++++++++++++++------- includes/model/User.php | 6 +++--- index.php | 6 +++--- viewcategory.php | 4 ++-- viewthread.php | 7 +++---- viewuser.php | 6 +++--- 7 files changed, 51 insertions(+), 31 deletions(-) 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) diff --git a/includes/model/Thread.php b/includes/model/Thread.php index c939738..2a74a0e 100644 --- a/includes/model/Thread.php +++ b/includes/model/Thread.php @@ -11,8 +11,8 @@ class Thread public $subject; public $date_created = 0; public $last_post_date = 0; - public $category; - public $author; + public $category_id; + public $author_id; private $has_value = false; @@ -29,10 +29,8 @@ class Thread $this->subject = $result[0]['thread_subject']; $this->date_created = $result[0]['thread_date_created']; $this->last_post_date = $result[0]['thread_last_post_date']; - $this->category = new Category($result[0]['thread_category']); - - $this->author = new User(); - $this->author->get_by_id($result[0]['thread_author']); + $this->category_id = $result[0]['thread_category']; + $this->author_id = $result[0]['thread_author']; $this->has_value = true; } @@ -79,7 +77,7 @@ class Thread Database::get()->query("DELETE FROM threads WHERE thread_id = ?", "i", $thread->id); // Decrement the thread count of the category - Database::get()->query("UPDATE categories SET `category_thread_count` = `category_thread_count` - '1' WHERE category_id = ?", "i", $thread->category->id); + Database::get()->query("UPDATE categories SET `category_thread_count` = `category_thread_count` - '1' WHERE category_id = ?", "i", $thread->category_id); } public function has_value(): bool @@ -87,6 +85,18 @@ class Thread return $this->has_value; } + public function get_parent_category(): ?Category + { + return new Category($this->category_id); + } + + public function get_author(): ?User + { + $result = new User(); + $result->get_by_id($this->author_id); + return $result; + } + public static function get_all(): array { $sql = "SELECT thread_id FROM threads"; diff --git a/includes/model/User.php b/includes/model/User.php index 17d40cb..e489edf 100755 --- a/includes/model/User.php +++ b/includes/model/User.php @@ -8,7 +8,7 @@ class User public $id; public $name; public $password; - public $date; + public $date_registered; public $level = 'user'; private $has_value = false; @@ -27,7 +27,7 @@ class User $this->id = $id; $this->name = $result[0]['user_name']; $this->password = $result[0]['user_password']; - $this->date = $result[0]['user_date_registered']; + $this->date_registered = $result[0]['user_date_registered']; $this->level = $result[0]['user_level']; $this->has_value = true; @@ -45,7 +45,7 @@ class User $this->id = $result[0]['user_id']; $this->name = $name; $this->password = $result[0]['user_password']; - $this->date = $result[0]['user_date_registered']; + $this->date_registered = $result[0]['user_date_registered']; $this->level = $result[0]['user_level']; $this->has_value = true; diff --git a/index.php b/index.php index 0c8409c..9545e43 100755 --- a/index.php +++ b/index.php @@ -23,7 +23,7 @@ subject ?>
- by author->name ?>, date_created ?> + by get_author()->name ?>, date_created ?> No threads yet! @@ -44,10 +44,10 @@ subject ?> on date_created)); ?> - category->name ?> + get_parent_category()->name ?> get_latest_post(); if ($latest_post->has_value()): ?> - by author->name ?> + by get_author()->name ?> on date_created ?> diff --git a/viewcategory.php b/viewcategory.php index acca740..486dde7 100755 --- a/viewcategory.php +++ b/viewcategory.php @@ -40,10 +40,10 @@ if (!$current->has_value()) { subject ?> on date_created)); ?> - author->name ?> + get_author()->name ?> get_latest_post(); if ($latest_post->has_value()): ?> - by author->name ?> + by get_author()->name ?> on date_created ?> diff --git a/viewthread.php b/viewthread.php index 06debe8..d0a387e 100755 --- a/viewthread.php +++ b/viewthread.php @@ -27,13 +27,12 @@ if (!$current->has_value()) { -

subject; ?>

- created by author->name; ?> - in category->name; ?>, date_created)); ?> + created by get_author()->name; ?> + in get_parent_category()->name; ?>, date_created)); ?> is_signed_in() and Session::get()->get_current_user()->level == USER_LEVEL_MODERATOR): ?> Moderator Options @@ -59,7 +58,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (empty($post_content) or !$post_content) { trigger_error('Reply cannot be empty'); } else { - Post::create($post_content, $current->id, $current->category->id); + Post::create($post_content, $current->id, $current->category_id); header('Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $current->id); } } diff --git a/viewuser.php b/viewuser.php index 20dc55f..8d9c9c1 100755 --- a/viewuser.php +++ b/viewuser.php @@ -25,7 +25,7 @@ if (!$current->has_value()) {

name; ?>

- member since date)); ?> + member since date_registered)); ?>

name; ?>'s Threads

@@ -39,10 +39,10 @@ if (!$current->has_value()) { subject ?>on date_created)); ?> - + get_latest_post(); if ($latest_post->has_value()): ?> -- cgit v1.2.3
category->name ?>get_parent_category()->name ?> - by author->name ?> + by get_author()->name ?> on date_created ?>