summaryrefslogtreecommitdiff
path: root/model/Thread.php
blob: a9dc6902346d0f261e985c99bdc308e29ca182d1 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?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, $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));
		}
	
		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->author = new User();
				$this->author->get_by_id($row['thread_author'], $dbc);
			}
		}

		mysqli_free_result($result);
		return 1;
	}

	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);
		}
	
		$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);
			}
		}

		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;

		if (mysqli_num_rows($result) == 0) {
		} else {
			while ($row = mysqli_fetch_assoc($result)) {
				$post = new Post();
				$post->get_from_database($row['post_id'], $dbc);
			}
		}

		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;
}