| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class ReadPost extends TPage
|
|
|
4 |
{
|
|
|
5 |
private $_post;
|
|
|
6 |
/**
|
|
|
7 |
* Fetches the post data.
|
|
|
8 |
* This method is invoked by the framework when initializing the page
|
|
|
9 |
* @param mixed event parameter
|
|
|
10 |
*/
|
|
|
11 |
public function onInit($param)
|
|
|
12 |
{
|
|
|
13 |
parent::onInit($param);
|
|
|
14 |
// post id is passed via the 'id' GET parameter
|
|
|
15 |
$postID=(int)$this->Request['id'];
|
|
|
16 |
// retrieves PostRecord with author information filled in
|
|
|
17 |
$this->_post=PostRecord::finder()->withAuthor()->findByPk($postID);
|
|
|
18 |
if($this->_post===null) // if post id is invalid
|
|
|
19 |
throw new THttpException(500,'Unable to find the specified post.');
|
|
|
20 |
// set the page title as the post title
|
|
|
21 |
$this->Title=$this->_post->title;
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* @return PostRecord the PostRecord currently being viewed
|
|
|
26 |
*/
|
|
|
27 |
public function getPost()
|
|
|
28 |
{
|
|
|
29 |
return $this->_post;
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Deletes the post currently being viewed
|
|
|
34 |
* This method is invoked when the user clicks on the "Delete" button
|
|
|
35 |
*/
|
|
|
36 |
public function deletePost($sender,$param)
|
|
|
37 |
{
|
|
|
38 |
// only the author or the administrator can delete a post
|
|
|
39 |
if(!$this->canEdit())
|
|
|
40 |
throw new THttpException('You are not allowed to perform this action.');
|
|
|
41 |
// delete it from DB
|
|
|
42 |
$this->_post->delete();
|
|
|
43 |
// redirect the browser to the homepage
|
|
|
44 |
$this->Response->redirect($this->Service->DefaultPageUrl);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* @return boolean whether the current user can edit/delete the post being viewed
|
|
|
49 |
*/
|
|
|
50 |
public function canEdit()
|
|
|
51 |
{
|
|
|
52 |
// only the author or the administrator can edit/delete a post
|
|
|
53 |
return $this->User->Name===$this->Post->author_id || $this->User->IsAdmin;
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
?>
|