1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* A representation of a forum thread. A forum thread is 1 topic on the forum |
5
|
|
|
* which has multiple posts underneath it. |
6
|
|
|
* |
7
|
|
|
* @package forum |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class ForumThread extends DataObject |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private static $db = array( |
|
|
|
|
14
|
|
|
"Title" => "Varchar(255)", |
15
|
|
|
"NumViews" => "Int", |
16
|
|
|
"IsSticky" => "Boolean", |
17
|
|
|
"IsReadOnly" => "Boolean", |
18
|
|
|
"IsGlobalSticky" => "Boolean" |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
private static $has_one = array( |
|
|
|
|
22
|
|
|
'Forum' => 'Forum' |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
private static $has_many = array( |
|
|
|
|
26
|
|
|
'Posts' => 'Post' |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
private static $defaults = array( |
|
|
|
|
30
|
|
|
'NumViews' => 0, |
31
|
|
|
'IsSticky' => false, |
32
|
|
|
'IsReadOnly' => false, |
33
|
|
|
'IsGlobalSticky' => false |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
private static $indexes = array( |
|
|
|
|
37
|
|
|
'IsSticky' => true, |
38
|
|
|
'IsGlobalSticky' => true |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var null|boolean Per-request cache, whether we should display signatures on a post. |
43
|
|
|
*/ |
44
|
|
|
private static $_cache_displaysignatures = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check if the user can create new threads and add responses |
48
|
|
|
*/ |
49
|
|
|
public function canPost($member = null) |
50
|
|
|
{ |
51
|
|
|
if (!$member) { |
52
|
|
|
$member = Member::currentUser(); |
53
|
|
|
} |
54
|
|
|
return ($this->Forum()->canPost($member) && !$this->IsReadOnly); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check if user can moderate this thread |
59
|
|
|
*/ |
60
|
|
|
public function canModerate($member = null) |
61
|
|
|
{ |
62
|
|
|
if (!$member) { |
63
|
|
|
$member = Member::currentUser(); |
64
|
|
|
} |
65
|
|
|
return $this->Forum()->canModerate($member); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Check if user can view the thread |
70
|
|
|
*/ |
71
|
|
|
public function canView($member = null) |
72
|
|
|
{ |
73
|
|
|
if (!$member) { |
74
|
|
|
$member = Member::currentUser(); |
75
|
|
|
} |
76
|
|
|
return $this->Forum()->canView($member); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Hook up into moderation. |
81
|
|
|
*/ |
82
|
|
|
public function canEdit($member = null) |
83
|
|
|
{ |
84
|
|
|
if (!$member) { |
85
|
|
|
$member = Member::currentUser(); |
86
|
|
|
} |
87
|
|
|
return $this->canModerate($member); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Hook up into moderation - users cannot delete their own posts/threads because |
92
|
|
|
* we will loose history this way. |
93
|
|
|
*/ |
94
|
|
|
public function canDelete($member = null) |
95
|
|
|
{ |
96
|
|
|
if (!$member) { |
97
|
|
|
$member = Member::currentUser(); |
98
|
|
|
} |
99
|
|
|
return $this->canModerate($member); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Hook up into canPost check |
104
|
|
|
*/ |
105
|
|
|
public function canCreate($member = null) |
106
|
|
|
{ |
107
|
|
|
if (!$member) { |
108
|
|
|
$member = Member::currentUser(); |
109
|
|
|
} |
110
|
|
|
return $this->canPost($member); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Are Forum Signatures on Member profiles allowed. |
115
|
|
|
* This only needs to be checked once, so we cache the initial value once per-request. |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function getDisplaySignatures() |
120
|
|
|
{ |
121
|
|
|
if (isset(self::$_cache_displaysignatures) && self::$_cache_displaysignatures !== null) { |
122
|
|
|
return self::$_cache_displaysignatures; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$result = $this->Forum()->Parent()->DisplaySignatures; |
|
|
|
|
126
|
|
|
self::$_cache_displaysignatures = $result; |
127
|
|
|
return $result; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the latest post from this thread. Nicer way then using an control |
132
|
|
|
* from the template |
133
|
|
|
* |
134
|
|
|
* @return Post |
135
|
|
|
*/ |
136
|
|
|
public function getLatestPost() |
137
|
|
|
{ |
138
|
|
|
return DataObject::get_one('Post', "\"ThreadID\" = '$this->ID'", true, '"ID" DESC'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return the first post from the thread. Useful to working out the original author |
143
|
|
|
* |
144
|
|
|
* @return Post |
145
|
|
|
*/ |
146
|
|
|
public function getFirstPost() |
147
|
|
|
{ |
148
|
|
|
return DataObject::get_one('Post', "\"ThreadID\" = '$this->ID'", true, '"ID" ASC'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return the number of posts in this thread. We could use count on |
153
|
|
|
* the dataobject set but that is slower and causes a performance overhead |
154
|
|
|
* |
155
|
|
|
* @return int |
156
|
|
|
*/ |
157
|
|
|
public function getNumPosts() |
158
|
|
|
{ |
159
|
|
|
$sqlQuery = new SQLQuery(); |
|
|
|
|
160
|
|
|
$sqlQuery->setFrom('"Post"'); |
161
|
|
|
$sqlQuery->setSelect('COUNT("Post"."ID")'); |
162
|
|
|
$sqlQuery->addInnerJoin('Member', '"Post"."AuthorID" = "Member"."ID"'); |
163
|
|
|
$sqlQuery->addWhere('"Member"."ForumStatus" = \'Normal\''); |
164
|
|
|
$sqlQuery->addWhere('"ThreadID" = ' . $this->ID); |
165
|
|
|
return $sqlQuery->execute()->value(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Check if they have visited this thread before. If they haven't increment |
170
|
|
|
* the NumViews value by 1 and set visited to true. |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public function incNumViews() |
175
|
|
|
{ |
176
|
|
|
if (Session::get('ForumViewed-' . $this->ID)) { |
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
Session::set('ForumViewed-' . $this->ID, 'true'); |
181
|
|
|
|
182
|
|
|
$this->NumViews++; |
|
|
|
|
183
|
|
|
$SQL_numViews = Convert::raw2sql($this->NumViews); |
|
|
|
|
184
|
|
|
|
185
|
|
|
DB::query("UPDATE \"ForumThread\" SET \"NumViews\" = '$SQL_numViews' WHERE \"ID\" = $this->ID"); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Link to this forum thread |
190
|
|
|
* |
191
|
|
|
* @return String |
192
|
|
|
*/ |
193
|
|
|
public function Link($action = "show", $showID = true) |
194
|
|
|
{ |
195
|
|
|
$forum = DataObject::get_by_id("Forum", $this->ForumID); |
|
|
|
|
196
|
|
|
if ($forum) { |
197
|
|
|
$baseLink = $forum->Link(); |
198
|
|
|
$extra = ($showID) ? '/'.$this->ID : ''; |
199
|
|
|
return ($action) ? $baseLink . $action . $extra : $baseLink; |
200
|
|
|
} else { |
201
|
|
|
user_error("Bad ForumID '$this->ForumID'", E_USER_WARNING); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Check to see if the user has subscribed to this thread |
207
|
|
|
* |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
|
|
public function getHasSubscribed() |
211
|
|
|
{ |
212
|
|
|
$member = Member::currentUser(); |
213
|
|
|
|
214
|
|
|
return ($member) ? ForumThread_Subscription::already_subscribed($this->ID, $member->ID) : false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Before deleting the thread remove all the posts |
219
|
|
|
*/ |
220
|
|
|
public function onBeforeDelete() |
221
|
|
|
{ |
222
|
|
|
parent::onBeforeDelete(); |
223
|
|
|
|
224
|
|
|
if ($posts = $this->Posts()) { |
|
|
|
|
225
|
|
|
foreach ($posts as $post) { |
226
|
|
|
// attachment deletion is handled by the {@link Post::onBeforeDelete} |
227
|
|
|
$post->delete(); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function onAfterWrite() |
233
|
|
|
{ |
234
|
|
|
if ($this->isChanged('ForumID', 2)) { |
235
|
|
|
$posts = $this->Posts(); |
|
|
|
|
236
|
|
|
if ($posts && $posts->count()) { |
237
|
|
|
foreach ($posts as $post) { |
238
|
|
|
$post->ForumID=$this->ForumID; |
|
|
|
|
239
|
|
|
$post->write(); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
parent::onAfterWrite(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return Text |
248
|
|
|
*/ |
249
|
|
|
public function getEscapedTitle() |
250
|
|
|
{ |
251
|
|
|
//return DBField::create('Text', $this->dbObject('Title')->XML()); |
252
|
|
|
return DBField::create_field('Text', $this->dbObject('Title')->XML()); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Forum Thread Subscription: Allows members to subscribe to this thread |
259
|
|
|
* and receive email notifications when these topics are replied to. |
260
|
|
|
* |
261
|
|
|
* @package forum |
262
|
|
|
*/ |
263
|
|
|
class ForumThread_Subscription extends DataObject |
264
|
|
|
{ |
265
|
|
|
|
266
|
|
|
private static $db = array( |
|
|
|
|
267
|
|
|
"LastSent" => "SS_Datetime" |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
private static $has_one = array( |
|
|
|
|
271
|
|
|
"Thread" => "ForumThread", |
272
|
|
|
"Member" => "Member" |
273
|
|
|
); |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Checks to see if a Member is already subscribed to this thread |
277
|
|
|
* |
278
|
|
|
* @param int $threadID The ID of the thread to check |
279
|
|
|
* @param int $memberID The ID of the currently logged in member (Defaults to Member::currentUserID()) |
280
|
|
|
* |
281
|
|
|
* @return bool true if they are subscribed, false if they're not |
282
|
|
|
*/ |
283
|
|
|
static function already_subscribed($threadID, $memberID = null) |
|
|
|
|
284
|
|
|
{ |
285
|
|
|
if (!$memberID) { |
|
|
|
|
286
|
|
|
$memberID = Member::currentUserID(); |
287
|
|
|
} |
288
|
|
|
$SQL_threadID = Convert::raw2sql($threadID); |
289
|
|
|
$SQL_memberID = Convert::raw2sql($memberID); |
290
|
|
|
|
291
|
|
|
if ($SQL_threadID=='' || $SQL_memberID=='') { |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return (DB::query(" |
296
|
|
|
SELECT COUNT(\"ID\") |
297
|
|
|
FROM \"ForumThread_Subscription\" |
298
|
|
|
WHERE \"ThreadID\" = '$SQL_threadID' AND \"MemberID\" = $SQL_memberID")->value() > 0) ? true : false; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Notifies everybody that has subscribed to this topic that a new post has been added. |
303
|
|
|
* To get emailed, people subscribed to this topic must have visited the forum |
304
|
|
|
* since the last time they received an email |
305
|
|
|
* |
306
|
|
|
* @param Post $post The post that has just been added |
307
|
|
|
*/ |
308
|
|
|
static function notify(Post $post) |
|
|
|
|
309
|
|
|
{ |
310
|
|
|
$list = DataObject::get( |
311
|
|
|
"ForumThread_Subscription", |
312
|
|
|
"\"ThreadID\" = '". $post->ThreadID ."' AND \"MemberID\" != '$post->AuthorID'" |
|
|
|
|
313
|
|
|
); |
314
|
|
|
|
315
|
|
|
if ($list) { |
316
|
|
|
foreach ($list as $obj) { |
317
|
|
|
$SQL_id = Convert::raw2sql((int)$obj->MemberID); |
318
|
|
|
|
319
|
|
|
// Get the members details |
320
|
|
|
$member = DataObject::get_one("Member", "\"Member\".\"ID\" = '$SQL_id'"); |
321
|
|
|
$adminEmail = Config::inst()->get('Email', 'admin_email'); |
322
|
|
|
|
323
|
|
|
if ($member) { |
324
|
|
|
$email = new Email(); |
325
|
|
|
$email->setFrom($adminEmail); |
326
|
|
|
$email->setTo($member->Email); |
327
|
|
|
$email->setSubject(_t('Post.NEWREPLY', 'New reply for {title}', array('title' => $post->Title))); |
|
|
|
|
328
|
|
|
$email->setTemplate('ForumMember_TopicNotification'); |
329
|
|
|
$email->populateTemplate($member); |
330
|
|
|
$email->populateTemplate($post); |
331
|
|
|
$email->populateTemplate(array( |
332
|
|
|
'UnsubscribeLink' => Director::absoluteBaseURL() . $post->Thread()->Forum()->Link() . '/unsubscribe/' . $post->ID |
|
|
|
|
333
|
|
|
)); |
334
|
|
|
$email->send(); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|