summaryrefslogtreecommitdiff
path: root/includes/model/Thread.php
blob: cfe10d63668b55888183e1cc779f23f4a582ffe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
	}
}