summaryrefslogtreecommitdiff
path: root/includes/model/Thread.php
diff options
context:
space:
mode:
authorcflip <36554078+cflip@users.noreply.github.com>2021-05-08 17:30:08 -0600
committerGitHub <noreply@github.com>2021-05-08 17:30:08 -0600
commit87b1dfd1f77b08915ee5e905da45e316ba2c0e7d (patch)
treef6c0f8d09454b6e887df0f66ca37c1ce9efb30d0 /includes/model/Thread.php
parent0b045d57b2164b5ce003955d79627ae506a153eb (diff)
parenta09d9f377f5c055e42e5f21b5cdea64c2e2ca896 (diff)
Merge pull request #14 from cflip/refactor
Huge refactor
Diffstat (limited to 'includes/model/Thread.php')
-rw-r--r--includes/model/Thread.php65
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;
+ }
+}