diff options
Diffstat (limited to 'includes/model')
-rw-r--r-- | includes/model/Category.php | 56 | ||||
-rw-r--r-- | includes/model/Post.php | 36 | ||||
-rw-r--r-- | includes/model/Thread.php | 65 | ||||
-rw-r--r-- | includes/model/User.php | 43 |
4 files changed, 200 insertions, 0 deletions
diff --git a/includes/model/Category.php b/includes/model/Category.php new file mode 100644 index 0000000..ed53bdc --- /dev/null +++ b/includes/model/Category.php @@ -0,0 +1,56 @@ +<?php + +include_once 'Thread.php'; + +class Category +{ + public $id = 0; + public $name = 'Unknown'; + public $description = 'This category does not exist'; + public $thread_count = 0; + public $post_count = 0; + + function get_from_database($id): bool + { + $sql = "SELECT cat_name, cat_description, cat_thread_count, cat_post_count FROM categories WHERE cat_id = ?;"; + $result = Database::get()->query($sql, "i", $id); + + if (empty($result)) { + return false; + } + + $this->id = $id; + $this->name = $result[0]['cat_name']; + $this->description = $result[0]['cat_description']; + $this->thread_count = $result[0]['cat_thread_count']; + $this->post_count = $result[0]['cat_post_count']; + + return true; + } + + function get_threads(): array + { + $sql = "SELECT thread_id FROM threads WHERE thread_category = ? ORDER BY thread_date_lastpost DESC"; + $result = Database::get()->query($sql, "i", $this->id); + $threads = array(); + + foreach ($result as $row) { + $thread = new Thread(); + $thread->get_from_database($row['thread_id']); + array_push($threads, $thread); + } + + return $threads; + } + + function get_latest_thread(): Thread + { + $sql = "SELECT thread_id FROM threads WHERE thread_category = ? ORDER BY thread_date_lastpost DESC LIMIT 1"; + $result = Database::get()->query($sql, "i", $this->id); + + $thread = new Thread(); + $thread->get_from_database($result[0]['thread_id']); + + return $thread; + } +} diff --git a/includes/model/Post.php b/includes/model/Post.php new file mode 100644 index 0000000..67c7e4a --- /dev/null +++ b/includes/model/Post.php @@ -0,0 +1,36 @@ +<?php + +include_once 'Thread.php'; + +class Post +{ + public $id; + public $content; + public $date_created; + public $date_edited; + public $thread; + public $author; + + function get_from_database($id): bool + { + $sql = "SELECT post_content, post_date_created, post_date_edited, post_thread, post_author FROM posts WHERE post_id = ?;"; + $result = Database::get()->query($sql, "i", $id); + + if (empty($result)) { + return false; + } + + $this->id = $id; + $this->content = $result[0]['post_content']; + $this->date_created = $result[0]['post_date_created']; + $this->date_edited = $result[0]['post_date_edited']; + + $this->thread = new Thread(); + $this->thread->get_from_database($result[0]['post_thread']); + + $this->author = new User(); + $this->author->get_by_id($result[0]['post_author']); + + return true; + } +} 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; + } +} diff --git a/includes/model/User.php b/includes/model/User.php new file mode 100644 index 0000000..f2bd23d --- /dev/null +++ b/includes/model/User.php @@ -0,0 +1,43 @@ +<?php +include_once './includes/Database.php'; + +const USER_LEVEL_MODERATOR = 1; + +class User +{ + public $id; + public $name = 'Unknown'; + public $password; + public $date = 0; + public $level = 0; + + function get_by_name($name): bool + { + $sql = "SELECT user_id, user_date, user_level, user_pass FROM users WHERE user_name = ?"; + $result = Database::get()->query($sql, "s", $name); + + if (empty($result)) { + return false; + } + + $this->id = $result[0]['user_id']; + $this->name = $name; + $this->password = $result[0]['user_pass']; + $this->date = $result[0]['user_date']; + $this->level = $result[0]['user_level']; + + return true; + } + + function get_by_id($id) + { + $sql = "SELECT user_name, user_date, user_level, user_pass FROM users WHERE user_id = ?;"; + $result = Database::get()->query($sql, "i", $id); + + $this->id = $id; + $this->name = $result[0]['user_name']; + $this->password = $result[0]['user_pass']; + $this->date = $result[0]['user_date']; + $this->level = $result[0]['user_level']; + } +}
\ No newline at end of file |