summaryrefslogtreecommitdiff
path: root/demos/blog/protected/Pages/Posts/EditPost.php
blob: 8d754b309227e85e69287ae83284a4a5dd5bb708 (plain)
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
<?php
/**
 * EditPost class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2006 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 */

/**
 * EditPost class
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2006 PradoSoft
 * @license http://www.pradosoft.com/license/
 */
class EditPost extends BlogPage
{
	private $_postRecord=null;

	public function onInit($param)
	{
		parent::onInit($param);
		$id=TPropertyValue::ensureInteger($this->Request['id']);
		$this->_postRecord=$this->DataAccess->queryPostByID($id);
		if($this->_postRecord===null)
			throw new BlogException(500,'post_id_invalid',$id);
		// only the author and admin can edit the post
		if(!$this->User->IsAdmin && $this->User->ID!==$this->_postRecord->AuthorID)
			throw new BlogException(500,'post_edit_disallowed',$id);
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		if(!$this->IsPostBack)
		{
			$postRecord=$this->_postRecord;
			$this->Title->Text=$postRecord->Title;
			$this->Content->Text=$postRecord->Content;
			$this->DraftMode->Checked=$postRecord->Status===PostRecord::STATUS_DRAFT;
			$this->Categories->DataSource=$this->DataAccess->queryCategories();
			$this->Categories->dataBind();
			$cats=$this->DataAccess->queryCategoriesByPostID($postRecord->ID);
			$catIDs=array();
			foreach($cats as $cat)
				$catIDs[]=$cat->ID;
			$this->Categories->SelectedValues=$catIDs;
		}
	}

	public function saveButtonClicked($sender,$param)
	{
		if($this->IsValid)
		{
			$postRecord=$this->_postRecord;
			$postRecord->Title=$this->Title->SafeText;
			$postRecord->Content=$this->Content->SafeText;
			if($this->DraftMode->Checked)
				$postRecord->Status=PostRecord::STATUS_DRAFT;
			else if(!$this->User->IsAdmin && TPropertyValue::ensureBoolean($this->Application->Parameters['PostApproval']))
				$postRecord->Status=PostRecord::STATUS_PENDING;
			else
				$postRecord->Status=PostRecord::STATUS_PUBLISHED;
			$postRecord->ModifyTime=time();
			$cats=array();
			foreach($this->Categories->SelectedValues as $value)
				$cats[]=TPropertyValue::ensureInteger($value);
			$this->DataAccess->updatePost($postRecord,$cats);
			$this->gotoPage('Posts.ViewPost',array('id'=>$postRecord->ID));
		}
	}
}

?>