1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Forum Post Object. Contains a single post by the user. A thread is generated |
5
|
|
|
* with multiple posts. |
6
|
|
|
* |
7
|
|
|
* @package forum |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class Post extends DataObject { |
11
|
|
|
|
12
|
|
|
private static $db = array( |
|
|
|
|
13
|
|
|
"Content" => "Text", |
14
|
|
|
"Status" => "Enum('Awaiting, Moderated, Rejected, Archived', 'Moderated')", |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
private static $casting = array( |
|
|
|
|
18
|
|
|
"Updated" => "SS_Datetime", |
19
|
|
|
"RSSContent" => "HTMLText", |
20
|
|
|
"RSSAuthor" => "Varchar", |
21
|
|
|
"Content" => "HTMLText" |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
private static $has_one = array( |
|
|
|
|
25
|
|
|
"Author" => "Member", |
26
|
|
|
"Thread" => "ForumThread", |
27
|
|
|
"Forum" => "Forum" // denormalized data but used for read speed |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
private static $has_many = array( |
|
|
|
|
31
|
|
|
"Attachments" => "Post_Attachment" |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
private static $summary_fields = array( |
|
|
|
|
35
|
|
|
"Content.LimitWordCount" => "Summary", |
36
|
|
|
"Created" => "Created", |
37
|
|
|
"Status" => "Status", |
38
|
|
|
"Thread.Title" => "Thread", |
39
|
|
|
"Forum.Title" => "Forum" |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Update all the posts to have a forum ID of their thread ID. |
44
|
|
|
*/ |
45
|
|
|
function requireDefaultRecords() { |
|
|
|
|
46
|
|
|
$posts = Post::get()->filter(array('ForumID' => 0, 'ThreadID:GreaterThan' => 0)); |
47
|
|
|
|
48
|
|
|
if($posts->exists()) { |
49
|
|
|
foreach($posts as $post) { |
50
|
|
|
if($post->ThreadID) { |
51
|
|
|
$post->ForumID = $post->Thread()->ForumID; |
52
|
|
|
$post->write(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
DB::alteration_message(_t('Forum.POSTSFORUMIDUPDATED', 'Forum posts forum ID added'), 'created'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Before deleting a post make sure all attachments are also deleted |
62
|
|
|
*/ |
63
|
|
|
function onBeforeDelete() { |
|
|
|
|
64
|
|
|
parent::onBeforeDelete(); |
65
|
|
|
|
66
|
|
|
if($attachments = $this->Attachments()) { |
|
|
|
|
67
|
|
|
foreach($attachments as $file) { |
68
|
|
|
$file->delete(); |
69
|
|
|
$file->destroy(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if user can see the post |
76
|
|
|
*/ |
77
|
|
|
function canView($member = null) { |
|
|
|
|
78
|
|
|
if(!$member) $member = Member::currentUser(); |
79
|
|
|
|
80
|
|
|
if($this->Author()->ForumStatus != 'Normal') { |
|
|
|
|
81
|
|
|
if($this->AuthorID != $member->ID || $member->ForumStatus != 'Ghost') { |
|
|
|
|
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->Thread()->canView($member); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check if user can edit the post (only if it's his own, or he's an admin user) |
91
|
|
|
*/ |
92
|
|
|
function canEdit($member = null) { |
|
|
|
|
93
|
|
|
if(!$member) $member = Member::currentUser(); |
94
|
|
|
|
95
|
|
|
if($member) { |
96
|
|
|
// Admins can always edit, regardless of thread/post ownership |
97
|
|
|
if(Permission::checkMember($member, 'ADMIN')) return true; |
98
|
|
|
|
99
|
|
|
// Otherwise check for thread permissions and ownership |
100
|
|
|
if($this->Thread()->canPost($member) && $member->ID == $this->AuthorID) return true; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Follow edit permissions for this, but additionally allow moderation even |
108
|
|
|
* if the thread is marked as readonly. |
109
|
|
|
*/ |
110
|
|
|
function canDelete($member = null) { |
|
|
|
|
111
|
|
|
if(!$member) $member = Member::currentUser(); |
112
|
|
|
if($this->canEdit($member)) { |
113
|
|
|
return true; |
114
|
|
|
} else { |
115
|
|
|
return $this->Thread()->canModerate($member); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Check if user can add new posts - hook up into canPost. |
121
|
|
|
*/ |
122
|
|
|
function canCreate($member = null) { |
|
|
|
|
123
|
|
|
if(!$member) $member = Member::currentUser(); |
124
|
|
|
return $this->Thread()->canPost($member); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the absolute url rather then relative. Used in Post RSS Feed |
129
|
|
|
* |
130
|
|
|
* @return String |
131
|
|
|
*/ |
132
|
|
|
function AbsoluteLink() { |
|
|
|
|
133
|
|
|
return Director::absoluteURL($this->Link()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return the title of the post. Because we don't have to have the title |
138
|
|
|
* on individual posts check with the topic |
139
|
|
|
* |
140
|
|
|
* @return String |
141
|
|
|
*/ |
142
|
|
|
function getTitle() { |
|
|
|
|
143
|
|
|
return ($this->isFirstPost()) ? $this->Thread()->Title : sprintf(_t('Post.RESPONSE',"Re: %s",'Post Subject Prefix'),$this->Thread()->Title); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Return the last edited date, if it's different from created |
148
|
|
|
*/ |
149
|
|
|
function getUpdated() { |
|
|
|
|
150
|
|
|
if($this->LastEdited != $this->Created) return $this->LastEdited; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Is this post the first post in the thread. Check if their is a post with an ID less |
155
|
|
|
* than the one of this post in the same thread |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function isFirstPost() { |
160
|
|
|
if(empty($this->ThreadID) || empty($this->ID)) return false; |
|
|
|
|
161
|
|
|
$earlierPosts = DB::query(sprintf( |
162
|
|
|
'SELECT COUNT("ID") FROM "Post" WHERE "ThreadID" = \'%d\' and "ID" < \'%d\'', |
163
|
|
|
$this->ThreadID, |
|
|
|
|
164
|
|
|
$this->ID |
165
|
|
|
))->value(); |
166
|
|
|
return empty($earlierPosts); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Return a link to edit this post. |
171
|
|
|
* |
172
|
|
|
* @return String |
173
|
|
|
*/ |
174
|
|
|
function EditLink() { |
|
|
|
|
175
|
|
|
if ($this->canEdit()) { |
176
|
|
|
$url = Controller::join_links($this->Link('editpost'), $this->ID); |
177
|
|
|
return '<a href="' . $url . '" class="editPostLink">' . _t('Post.EDIT', 'Edit') . '</a>'; |
178
|
|
|
} |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Return a link to delete this post. |
184
|
|
|
* |
185
|
|
|
* If the member is an admin of this forum, (ADMIN permissions |
186
|
|
|
* or a moderator) then they can delete the post. |
187
|
|
|
* |
188
|
|
|
* @return String |
189
|
|
|
*/ |
190
|
|
|
function DeleteLink() { |
|
|
|
|
191
|
|
|
if($this->canDelete()) { |
192
|
|
|
$url = Controller::join_links($this->Link('deletepost'), $this->ID); |
193
|
|
|
$token = SecurityToken::inst(); |
194
|
|
|
$url = $token->addToUrl($url); |
195
|
|
|
|
196
|
|
|
$firstPost = ($this->isFirstPost()) ? ' firstPost' : ''; |
197
|
|
|
|
198
|
|
|
return '<a class="deleteLink' . $firstPost . '" href="' . $url . '">' . _t('Post.DELETE','Delete') . '</a>'; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Return a link to the reply form. Permission checking is handled on the actual URL |
206
|
|
|
* and not on this function |
207
|
|
|
* |
208
|
|
|
* @return String |
209
|
|
|
*/ |
210
|
|
|
function ReplyLink() { |
|
|
|
|
211
|
|
|
$url = $this->Link('reply'); |
212
|
|
|
|
213
|
|
|
return '<a href="' . $url . '" class="replyLink">' . _t('Post.REPLYLINK','Post Reply') . '</a>'; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Return a link to the post view. |
218
|
|
|
* |
219
|
|
|
* @return String |
220
|
|
|
*/ |
221
|
|
|
function ShowLink() { |
|
|
|
|
222
|
|
|
$url = $this->Link('show'); |
223
|
|
|
|
224
|
|
|
return '<a href="' . $url . '" class="showLink">' . _t('Post.SHOWLINK','Show Thread') . "</a>"; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
View Code Duplication |
public function BanLink() { |
|
|
|
|
228
|
|
|
$thread = $this->Thread(); |
|
|
|
|
229
|
|
|
if($thread->canModerate()) { |
230
|
|
|
$link = $thread->Forum()->Link('ban') .'/'. $this->AuthorID; |
|
|
|
|
231
|
|
|
return "<a class='banLink' href=\"$link\" rel=\"$this->AuthorID\">". _t('Post.BANUSER', 'Ban User') ."</a>"; |
|
|
|
|
232
|
|
|
} |
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
View Code Duplication |
public function GhostLink() { |
|
|
|
|
237
|
|
|
$thread = $this->Thread(); |
|
|
|
|
238
|
|
|
if($thread->canModerate()) { |
239
|
|
|
$link = $thread->Forum()->Link('ghost') .'/'. $this->AuthorID; |
|
|
|
|
240
|
|
|
return "<a class='ghostLink' href=\"$link\" rel=\"$this->AuthorID\">". _t('Post.GHOSTUSER', 'Ghost User') ."</a>"; |
|
|
|
|
241
|
|
|
} |
242
|
|
|
return false; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Return the parsed content and the information for the |
247
|
|
|
* RSS feed |
248
|
|
|
*/ |
249
|
|
|
function getRSSContent() { |
|
|
|
|
250
|
|
|
return $this->renderWith('Includes/Post_rss'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
function getRSSAuthor() { |
|
|
|
|
255
|
|
|
$author = $this->Author(); |
|
|
|
|
256
|
|
|
|
257
|
|
|
return $author->Nickname; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Return a link to show this post |
262
|
|
|
* |
263
|
|
|
* @return String |
264
|
|
|
*/ |
265
|
|
|
function Link($action = "show") { |
|
|
|
|
266
|
|
|
// only include the forum thread ID in the URL if we're showing the thread either |
267
|
|
|
// by showing the posts or replying therwise we only need to pass a single ID. |
268
|
|
|
$includeThreadID = ($action == "show" || $action == "reply") ? true : false; |
269
|
|
|
$link = $this->Thread()->Link($action, $includeThreadID); |
|
|
|
|
270
|
|
|
|
271
|
|
|
// calculate what page results the post is on |
272
|
|
|
// the count is the position of the post in the thread |
273
|
|
|
$count = DB::query(" |
274
|
|
|
SELECT COUNT(\"ID\") |
275
|
|
|
FROM \"Post\" |
276
|
|
|
WHERE \"ThreadID\" = '$this->ThreadID' AND \"Status\" = 'Moderated' AND \"ID\" < $this->ID |
|
|
|
|
277
|
|
|
")->value(); |
278
|
|
|
|
279
|
|
|
$start = ($count >= Forum::$posts_per_page) ? floor($count / Forum::$posts_per_page) * Forum::$posts_per_page : 0; |
|
|
|
|
280
|
|
|
$pos = ($start == 0 ? '' : "?start=$start") . ($count == 0 ? '' : "#post{$this->ID}"); |
281
|
|
|
|
282
|
|
|
return ($action == "show") ? $link . $pos : $link; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Attachments for posts (one post can have many attachments) |
288
|
|
|
* |
289
|
|
|
* @package forum |
290
|
|
|
*/ |
291
|
|
|
class Post_Attachment extends File { |
292
|
|
|
|
293
|
|
|
private static $has_one = array( |
|
|
|
|
294
|
|
|
"Post" => "Post" |
295
|
|
|
); |
296
|
|
|
|
297
|
|
|
private static $defaults = array( |
|
|
|
|
298
|
|
|
'ShowInSearch' => 0 |
299
|
|
|
); |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Can a user delete this attachment |
303
|
|
|
* |
304
|
|
|
* @return bool |
305
|
|
|
*/ |
306
|
|
|
function canDelete($member = null) { |
|
|
|
|
307
|
|
|
if(!$member) $member = Member::currentUser(); |
308
|
|
|
return ($this->Post()) ? $this->Post()->canDelete($member) : true; |
|
|
|
|
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Can a user edit this attachement |
313
|
|
|
* |
314
|
|
|
* @return bool |
315
|
|
|
*/ |
316
|
|
|
function canEdit($member = null) { |
|
|
|
|
317
|
|
|
if(!$member) $member = Member::currentUser(); |
318
|
|
|
return ($this->Post()) ? $this->Post()->canEdit($member) : true; |
|
|
|
|
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Allows the user to download a file without right-clicking |
323
|
|
|
*/ |
324
|
|
|
function download() { |
|
|
|
|
325
|
|
|
if(isset($this->urlParams['ID'])) { |
|
|
|
|
326
|
|
|
$SQL_ID = Convert::raw2sql($this->urlParams['ID']); |
|
|
|
|
327
|
|
|
|
328
|
|
|
if(is_numeric($SQL_ID)) { |
329
|
|
|
$file = DataObject::get_by_id("Post_Attachment", $SQL_ID); |
330
|
|
|
$response = SS_HTTPRequest::send_file(file_get_contents($file->getFullPath()), $file->Name); |
331
|
|
|
$response->output(); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return $this->redirectBack(); |
|
|
|
|
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|