Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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