summaryrefslogtreecommitdiff
path: root/includes/model/Category.php
blob: ed53bdcd046b25dea7a1ab26f635383c3b45348a (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
<?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;
	}
}