diff options
author | cflip <36554078+cflip@users.noreply.github.com> | 2021-06-05 11:18:10 -0600 |
---|---|---|
committer | cflip <36554078+cflip@users.noreply.github.com> | 2021-06-05 11:18:10 -0600 |
commit | 24efe49bc2b545e3a3e46d7d6f2bd1166163e52b (patch) | |
tree | c1852447d06c062052def6fc89be2e2dece17c78 /includes/model/Thread.php | |
parent | 45acfc48b3dd80b945a1501edea9ad4faa700c0f (diff) |
Move object related functions into their classes.
Some of the pages are still broken from this commit, but I plan
to either rewrite or ignore them.
Diffstat (limited to 'includes/model/Thread.php')
-rw-r--r-- | includes/model/Thread.php | 101 |
1 files changed, 82 insertions, 19 deletions
diff --git a/includes/model/Thread.php b/includes/model/Thread.php index cfe10d6..95bd3d8 100644 --- a/includes/model/Thread.php +++ b/includes/model/Thread.php @@ -1,42 +1,109 @@ <?php - +include_once './includes/Database.php'; +include_once './includes/Session.php'; include_once 'Category.php'; include_once 'User.php'; include_once 'Post.php'; class Thread { - public $id = 0; - public $subject = 'Unknown thread'; + public $id; + public $subject; public $date_created = 0; public $date_lastpost = 0; public $category; public $author; - function get_from_database($id): bool + private $has_value = false; + + function __construct($id) { $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; + return; } $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->category = new Category($result[0]['thread_category']); $this->author = new User(); $this->author->get_by_id($result[0]['thread_author']); - return true; + $this->has_value = true; } - function get_posts(): array + public static function create($subject, $category): int + { + if (!Session::get()->is_signed_in()) { + trigger_error('You must be signed in to create a thread'); + return 0; + } + + $user = Session::get()->get_current_user(); + + // Insert the new thread into the database + $sql = "INSERT INTO threads(thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);"; + Database::get()->query($sql, "sii", $subject, $category, $user->id); + + // Get the ID of the thread we just created + $thread_id = Database::get()->get_last_id(); + + // Increment the category's thread count + $sql = "UPDATE categories SET `cat_thread_count` = `cat_thread_count` + '1' WHERE cat_id = ?;"; + Database::get()->query($sql, "i", $category); + + return $thread_id; + } + + public static function delete($thread) + { + // User must be signed in + if (!Session::get()->is_signed_in()) { + trigger_error('You must be signed in to delete a thread.'); + return; + } + + // User must be a moderator to delete a thread + $current_user = Session::get()->get_current_user(); + if ($current_user->level != USER_LEVEL_MODERATOR) { + trigger_error("You must be a moderator to delete this post."); + return; + } + + // Delete the thread from the database + 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 `cat_thread_count` = `cat_thread_count` - '1' WHERE cat_id = ?", "i", $thread->category->id); + } + + public function has_value() + { + return $this->has_value; + } + + public static function get_all(): array + { + $sql = "SELECT thread_id FROM threads"; + $result = Database::get()->query($sql); + + $threads = array(); + + foreach ($result as $row) { + $thread = new Thread($row['thread_id']); + if ($thread->has_value()) + array_push($threads, $thread); + } + + return $threads; + } + + public function get_posts(): array { $sql = "SELECT post_id FROM posts WHERE post_thread = ?"; $result = Database::get()->query($sql, "i", $this->id); @@ -44,22 +111,18 @@ class Thread $posts = array(); foreach ($result as $row) { - $post = new Post(); - $post->get_from_database($row['post_id']); - array_push($posts, $post); + $post = new Post($row['post_id']); + if ($post->has_value()) + array_push($posts, $post); } return $posts; } - function get_latest_post(): Post + public 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; + return new Post($result[0]['post_id']); } } |