Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<com:TContent ID="Main">
2
 
3
<h1>Creating <tt>EditPost</tt> Page</h1>
4
 
5
<p>
6
The <tt>EditPost</tt> page is provided to authors and the administrator to edit existing blog posts. Like the <a href="?page=Day4.CreateNewPost">NewPost</a> page, it displays a form to collect the change to the title and content of a post.
7
</p>
8
 
9
<p>
10
We create two files <tt>protected/pages/posts/EditPost.page</tt> and <tt>protected/pages/posts/EditPost.php</tt> to save the page template and page class, respectively.
11
</p>
12
 
13
<h2>Creating Page Template</h2>
14
<p>
15
The <tt>EditPost</tt> page template is very similar to the <tt>NewPost</tt> template. Only the page title and the button caption are different.
16
</p>
17
 
18
<com:TTextHighlighter CssClass="source" Language="prado">
19
&lt;%@ Title="My Blog - Edit Post" %>
20
 
21
&lt;com:TContent ID="Main">
22
 
23
<h1>Edit Post</h1>
24
 
25
<span>Title:</span>
26
&lt;com:TRequiredFieldValidator
27
	ControlToValidate="TitleEdit"
28
	ErrorMessage="Please provide a title."
29
	Display="Dynamic" />
30
<br/>
31
&lt;com:TTextBox ID="TitleEdit" Columns="50" />
32
 
33
<br/>
34
<span>Content:</span>
35
&lt;com:TRequiredFieldValidator
36
	ControlToValidate="ContentEdit"
37
	ErrorMessage="Please provide content."
38
	Display="Dynamic" />
39
<br/>
40
&lt;com:THtmlArea ID="ContentEdit" />
41
 
42
<br/>
43
&lt;com:TButton Text="Save" OnClick="saveButtonClicked" />
44
 
45
&lt;/com:TContent>
46
</com:TTextHighlighter>
47
 
48
 
49
<h2>Creating Page Class</h2>
50
 
51
<p>
52
The <tt>EditPage</tt> page class is slightly complex than <tt>NewPage</tt> because it needs to load the specified post data first. It also needs to perform additional authorization check. In particular, it needs to ensure that a post can only be editted by the author or the administrator. Such authorization check is not provided by PRADO itself.
53
</p>
54
 
55
<com:TTextHighlighter CssClass="source" Language="php">
56
class EditPost extends TPage
57
{
58
	/**
59
	 * Initializes the inputs with existing post data.
60
	 * This method is invoked by the framework when the page is being initialized.
61
	 * @param mixed event parameter
62
	 */
63
	public function onInit($param)
64
	{
65
		parent::onInit($param);
66
		// Retrieves the existing user data. This is equivalent to:
67
		// $postRecord=$this->getPost();
68
		$postRecord=$this->Post;
69
		// Authorization check: only the author or the administrator can edit the post
70
		if($postRecord->author_id!==$this->User->Name && !$this->User->IsAdmin)
71
			throw new THttpException(500,'You are not allowed to edit this post.');
72
 
73
		if(!$this->IsPostBack)  // if the page is initially requested
74
		{
75
			// Populates the input controls with the existing post data
76
			$this->TitleEdit->Text=$postRecord->title;
77
			$this->ContentEdit->Text=$postRecord->content;
78
		}
79
	}
80
 
81
	/**
82
	 * Saves the post if all inputs are valid.
83
	 * This method responds to the OnClick event of the "Save" button.
84
	 * @param mixed event sender
85
	 * @param mixed event parameter
86
	 */
87
	public function saveButtonClicked($sender,$param)
88
	{
89
		if($this->IsValid)  // when all validations succeed
90
		{
91
			// Retrieves the existing user data. This is equivalent to:
92
			// $postRecord=$this->getPost();
93
			$postRecord=$this->Post;
94
 
95
			// Fetches the input data
96
			$postRecord->title=$this->TitleEdit->SafeText;
97
			$postRecord->content=$this->ContentEdit->SafeText;
98
 
99
			// saves to the database via Active Record mechanism
100
			$postRecord->save();
101
 
102
			// redirects the browser to the ReadPost page
103
			$url=$this->Service->constructUrl('posts.ReadPost',array('id'=>$postRecord->post_id));
104
			$this->Response->redirect($url);
105
		}
106
	}
107
 
108
	/**
109
	 * Returns the post data to be editted.
110
	 * @return PostRecord the post data to be editted.
111
	 * @throws THttpException if the post data is not found.
112
	 */
113
	protected function getPost()
114
	{
115
		// the ID of the post to be editted is passed via GET parameter 'id'
116
		$postID=(int)$this->Request['id'];
117
		// use Active Record to look for the specified post ID
118
		$postRecord=PostRecord::finder()->findByPk($postID);
119
		if($postRecord===null)
120
			throw new THttpException(500,'Post is not found.');
121
		return $postRecord;
122
	}
123
}
124
</com:TTextHighlighter>
125
 
126
<h2>Testing</h2>
127
<p>
128
To test the <tt>EditPost</tt> page, login first and visit the following URL: <tt>http://hostname/blog/index.php?page=EditPost&id=1</tt>. This URL can also be reached by clicking on the <tt>Edit</tt> link on a post detail page.
129
</p>
130
 
131
<img src="<%~ output4.gif %>" class="output" />
132
 
133
</com:TContent>