diff options
Diffstat (limited to 'includes/model/Thread.php')
-rw-r--r-- | includes/model/Thread.php | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/includes/model/Thread.php b/includes/model/Thread.php new file mode 100644 index 0000000..cfe10d6 --- /dev/null +++ b/includes/model/Thread.php @@ -0,0 +1,65 @@ +<?php + +include_once 'Category.php'; +include_once 'User.php'; +include_once 'Post.php'; + +class Thread +{ + public $id = 0; + public $subject = 'Unknown thread'; + public $date_created = 0; + public $date_lastpost = 0; + public $category; + public $author; + + function get_from_database($id): bool + { + $sql = "SELECT thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author FROM threads WHERE thread_id = ?;"; + $result = Database::get()->query($sql, "i", $id); + + if (empty($result)) { + return false; + } + + $this->id = $id; + $this->subject = $result[0]['thread_subject']; + $this->date_created = $result[0]['thread_date_created']; + $this->date_lastpost = $result[0]['thread_date_lastpost']; + + $this->category = new Category(); + $this->category->get_from_database($result[0]['thread_category']); + + $this->author = new User(); + $this->author->get_by_id($result[0]['thread_author']); + + return true; + } + + function get_posts(): array + { + $sql = "SELECT post_id FROM posts WHERE post_thread = ?"; + $result = Database::get()->query($sql, "i", $this->id); + + $posts = array(); + + foreach ($result as $row) { + $post = new Post(); + $post->get_from_database($row['post_id']); + array_push($posts, $post); + } + + return $posts; + } + + function get_latest_post(): Post + { + $sql = "SELECT post_id FROM posts WHERE post_thread = ? ORDER BY post_date_created DESC LIMIT 1"; + $result = Database::get()->query($sql, "i", $this->id); + + $post = new Post(); + $post->get_from_database($result[0]['post_id']); + + return $post; + } +} |