summaryrefslogtreecommitdiff
path: root/includes/model/Thread.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/model/Thread.php')
-rw-r--r--includes/model/Thread.php114
1 files changed, 34 insertions, 80 deletions
diff --git a/includes/model/Thread.php b/includes/model/Thread.php
index a9dc690..cfe10d6 100644
--- a/includes/model/Thread.php
+++ b/includes/model/Thread.php
@@ -4,7 +4,8 @@ include_once 'Category.php';
include_once 'User.php';
include_once 'Post.php';
-class Thread {
+class Thread
+{
public $id = 0;
public $subject = 'Unknown thread';
public $date_created = 0;
@@ -12,100 +13,53 @@ class Thread {
public $category;
public $author;
- function get_from_database($id, $dbc) {
- $sql = "SELECT thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author FROM threads WHERE thread_id = " . mysqli_real_escape_string($dbc, $id);
- $result = mysqli_query($dbc, $sql);
-
- if (!$result) {
- die('Error trying to display thread page: ' . mysqli_error($dbc));
+ 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;
}
-
- if (mysqli_num_rows($result) == 0) {
- return 0;
- } else {
- while ($row = mysqli_fetch_assoc($result)) {
- $this->id = $id;
- $this->subject = $row['thread_subject'];
- $this->date_created = $row['thread_date_created'];
- $this->date_lastpost = $row['thread_date_lastpost'];
- $this->category = new Category();
- $this->category->get_from_database($row['thread_category'], $dbc);
+ $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->author = new User();
- $this->author->get_by_id($row['thread_author'], $dbc);
- }
- }
+ $this->category = new Category();
+ $this->category->get_from_database($result[0]['thread_category']);
- mysqli_free_result($result);
- return 1;
+ $this->author = new User();
+ $this->author->get_by_id($result[0]['thread_author']);
+
+ return true;
}
- function get_posts($dbc) {
- $sql = "SELECT post_id FROM posts WHERE post_thread = " . $this->id;
- $result = mysqli_query($dbc, $sql);
-
- if (!$result) {
- echo 'Could not get posts from thread: ' . mysqli_error($dbc);
- }
-
+ function get_posts(): array
+ {
+ $sql = "SELECT post_id FROM posts WHERE post_thread = ?";
+ $result = Database::get()->query($sql, "i", $this->id);
+
$posts = array();
- if (mysqli_num_rows($result) == 0) {
- } else {
- while ($row = mysqli_fetch_assoc($result)) {
- $post = new Post();
- $post->get_from_database($row['post_id'], $dbc);
- array_push($posts, $post);
- }
+ foreach ($result as $row) {
+ $post = new Post();
+ $post->get_from_database($row['post_id']);
+ array_push($posts, $post);
}
- mysqli_free_result($result);
return $posts;
}
- function get_latest_post($dbc) {
- $sql = "SELECT post_id FROM posts WHERE post_thread = " . $this->id . " ORDER BY post_date_created DESC LIMIT 1";
- $result = mysqli_query($dbc, $sql);
-
- if (!$result) {
- echo 'Could not get post from category: ' . mysqli_error($dbc);
- }
-
- $post = null;
+ 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);
- if (mysqli_num_rows($result) == 0) {
- } else {
- while ($row = mysqli_fetch_assoc($result)) {
- $post = new Post();
- $post->get_from_database($row['post_id'], $dbc);
- }
- }
+ $post = new Post();
+ $post->get_from_database($result[0]['post_id']);
- mysqli_free_result($result);
return $post;
}
}
-
-function get_all_threads($dbc) {
- $sql = "SELECT thread_id FROM threads";
- $result = mysqli_query($dbc, $sql);
-
- if (!$result) {
- echo 'Failed to get threads: ' . mysqli_error($dbc);
- }
-
- $threads = array();
-
- if (mysqli_num_rows($result) == 0) {
- } else {
- while ($row = mysqli_fetch_assoc($result)) {
- $thread = new Thread();
- $thread->get_from_database($row['thread_id'], $dbc);
- array_push($threads, $thread);
- }
- }
-
- mysqli_free_result($result);
- return $threads;
-}