-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.Topic.php
103 lines (86 loc) · 2.18 KB
/
class.Topic.php
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
<?php
/* A class representing a single topic. Each message in a topic is a post.
Each topic has a topicId, a forumId, a userId, a dateTime, a total number of
posts, a boolean variable indicating whether the topic is locked or not and a
boolean variable indicating whether it is a "sticky" topic or not.
The noOfPosts is cached here for performance reasons instead of recounting the
number of posts each time it is required.
The object is constructed from a string in the following format:
forumId
topicId
userId
dateTime
noOfPosts
topicName
isLocked
isSticky
Each topic has a directory consisting of the topic string:
db/Topics/<topicId>/topic.dat
and the posts:
db/Topics/<topicId>/posts.dat
*/
class Topic
{
private $topicId;
private $forumId;
private $userId;
private $dateTime;
private $posts;
private $topicName;
private $locked;
private $sticky;
public function __construct($str)
{
$arr = explode("\n",$str);
$this->forumId = trim($arr[0]);
$this->topicId = trim($arr[1]);
$this->userId = trim($arr[2]);
$this->dateTime = trim($arr[3]);
$this->posts = trim($arr[4]);
$this->topicName = trim($arr[5]);
$this->locked = trim($arr[6]);
$this->sticky = trim($arr[7]);
}
public function getForumId()
{
return $this->forumId;
}
public function getTopicId()
{
return $this->topicId;
}
/* Loads a User object of the user who created this Topic */
public function getUser()
{
return new User(file_get_contents("db/Users/".$this->userId.".dat"));
}
public function getDateTime()
{
return $this->dateTime;
}
public function getTopicName()
{
return $this->topicName;
}
public function getTotalPosts()
{
return $this->posts;
}
/* Load a Post object of the most recent post in the Topic.
This is used to get the dateTime of the most recent post in this topic
for display in various places. */
public function getLatestPost()
{
$fileC = file("db/Topics/".$this->topicId."/posts.dat");
return new Post(array_pop($fileC));
}
public function isLocked()
{
return $this->locked;
}
public function isSticky()
{
return $this->sticky;
}
}
?>