| 1 |
lars |
1 |
<com:TContent ID="Main">
|
|
|
2 |
|
|
|
3 |
<h1>Creating <tt>ReadPost</tt> Page</h1>
|
|
|
4 |
|
|
|
5 |
<p>
|
|
|
6 |
The <tt>ReadPost</tt> page shows the detailed content of a blog post. For authorized users, it also shows link buttons that would allow them to edit or delete the post.
|
|
|
7 |
</p>
|
|
|
8 |
|
|
|
9 |
<p>
|
|
|
10 |
We create two files <tt>protected/pages/posts/ReadPost.page</tt> and <tt>protected/pages/posts/ReadPost.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>ReadPost</tt> page template is very similar to the <tt>PostRenderer</tt> template, both presenting the content of a post. The difference is that <tt>ReadPost</tt> needs to display two link buttons when the current user is authorized to edit or delete the post.
|
|
|
16 |
</p>
|
|
|
17 |
|
|
|
18 |
<com:TTextHighlighter CssClass="source" Language="prado">
|
|
|
19 |
<com:TContent ID="Main">
|
|
|
20 |
|
|
|
21 |
<h2>
|
|
|
22 |
<com:TLiteral Text="<%= $this->Post->title %>" />
|
|
|
23 |
</h2>
|
|
|
24 |
|
|
|
25 |
<com:TControl Visible="<%= $this->canEdit() %>">
|
|
|
26 |
<a href="<%= $this->Service->constructUrl('posts.EditPost',array('id'=>$this->Post->post_id))%>">Edit</a> |
|
|
|
27 |
<com:TLinkButton Text="Delete"
|
|
|
28 |
OnClick="deletePost"
|
|
|
29 |
Attributes.onclick="javascript:if(!confirm('Are you sure?')) return false;" />
|
|
|
30 |
</com:TControl>
|
|
|
31 |
|
|
|
32 |
<p>
|
|
|
33 |
Author:
|
|
|
34 |
<com:TLiteral Text="<%= $this->Post->author->username %>" /><br/>
|
|
|
35 |
Time:
|
|
|
36 |
<com:TLiteral Text="<%= date('m/d/Y h:m:sa', $this->Post->create_time) %>" />
|
|
|
37 |
</p>
|
|
|
38 |
|
|
|
39 |
<p>
|
|
|
40 |
<com:TLiteral Text="<%= $this->Post->content %>" />
|
|
|
41 |
</p>
|
|
|
42 |
|
|
|
43 |
</com:TContent>
|
|
|
44 |
</com:TTextHighlighter>
|
|
|
45 |
|
|
|
46 |
<p>
|
|
|
47 |
Many PHP expressions are used in the above template. The expression <tt>$this->Post</tt> refers to a property defined in the <tt>ReadPost</tt> page class. It represents the <tt>PostRecord</tt> object corresponding to the post currently being viewed.
|
|
|
48 |
</p>
|
|
|
49 |
|
|
|
50 |
<com:InfoBox>
|
|
|
51 |
Although we use expressions widely in templates, we do not overuse them. A major guideline in determining whether we should use an expression in a template is that <i>the expression should be a property or a simple presentational transformation of the property</i>. By following this guideline, we ensure content and presentation are well separated without losing sufficient flexibility.
|
|
|
52 |
</com:InfoBox>
|
|
|
53 |
|
|
|
54 |
<p>
|
|
|
55 |
We also notice in the above template that two link buttons are enclosed within a <tt>TControl</tt> whose visibility is determined by the expression <tt>$this->canEdit()</tt>. For the <tt>Delete</tt> link button, we use javascript confirmation dialog to obtain user's confirmation when he clicks to delete the post.
|
|
|
56 |
</p>
|
|
|
57 |
|
|
|
58 |
<com:InfoBox>
|
|
|
59 |
All PRADO controls have a very useful property named <tt>Attributes</tt> which can accept arbitrary name-value pairs. Most PRADO controls will render the name-value pairs in <tt>Attributes</tt> literally in the corresponding HTML tag. For example, in the <tt>Delete</tt> link button above, we define an <tt>onclick</tt> which is rendered as the <tt>onclick</tt> attribute in the resulting <tt><a></tt> tag.
|
|
|
60 |
</com:InfoBox>
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
<h2>Creating Page Class</h2>
|
|
|
64 |
|
|
|
65 |
<p>
|
|
|
66 |
From the above page template, we see that we need to write a page class that implements the event handler: <tt>deletePost()</tt> (attached to the <tt>Delete</tt> button's <tt>OnClick</tt> event). We also need to retrieve the post data specified by the post ID passed via the <tt>id</tt> GET parameter. </p>
|
|
|
67 |
|
|
|
68 |
<com:InfoBox>
|
|
|
69 |
We implement the post deletion feature in the <tt>ReadPost</tt> page because it is so natural to do so here. When the user clicks on the <tt>Delete</tt> button, a javascript confirmation dialog will pop up. If the user confirms it, the deletion will be carried in response to the <tt>OnClick</tt> event of the <tt>Delete</tt> button.
|
|
|
70 |
</com:InfoBox>
|
|
|
71 |
|
|
|
72 |
<com:TTextHighlighter CssClass="source" Language="php">
|
|
|
73 |
class ReadPost extends TPage
|
|
|
74 |
{
|
|
|
75 |
private $_post;
|
|
|
76 |
/**
|
|
|
77 |
* Fetches the post data.
|
|
|
78 |
* This method is invoked by the framework when initializing the page
|
|
|
79 |
* @param mixed event parameter
|
|
|
80 |
*/
|
|
|
81 |
public function onInit($param)
|
|
|
82 |
{
|
|
|
83 |
parent::onInit($param);
|
|
|
84 |
// post id is passed via the 'id' GET parameter
|
|
|
85 |
$postID=(int)$this->Request['id'];
|
|
|
86 |
// retrieves PostRecord with author information filled in
|
|
|
87 |
$this->_post=PostRecord::finder()->withAuthor()->findByPk($postID);
|
|
|
88 |
if($this->_post===null) // if post id is invalid
|
|
|
89 |
throw new THttpException(500,'Unable to find the specified post.');
|
|
|
90 |
// set the page title as the post title
|
|
|
91 |
$this->Title=$this->_post->title;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* @return PostRecord the PostRecord currently being viewed
|
|
|
96 |
*/
|
|
|
97 |
public function getPost()
|
|
|
98 |
{
|
|
|
99 |
return $this->_post;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Deletes the post currently being viewed
|
|
|
104 |
* This method is invoked when the user clicks on the "Delete" button
|
|
|
105 |
*/
|
|
|
106 |
public function deletePost($sender,$param)
|
|
|
107 |
{
|
|
|
108 |
// only the author or the administrator can delete a post
|
|
|
109 |
if(!$this->canEdit())
|
|
|
110 |
throw new THttpException('You are not allowed to perform this action.');
|
|
|
111 |
// delete it from DB
|
|
|
112 |
$this->_post->delete();
|
|
|
113 |
// redirect the browser to the homepage
|
|
|
114 |
$this->Response->redirect($this->Service->DefaultPageUrl);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* @return boolean whether the current user can edit/delete the post being viewed
|
|
|
119 |
*/
|
|
|
120 |
public function canEdit()
|
|
|
121 |
{
|
|
|
122 |
// only the author or the administrator can edit/delete a post
|
|
|
123 |
return $this->User->Name===$this->Post->author_id || $this->User->IsAdmin;
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
</com:TTextHighlighter>
|
|
|
127 |
|
|
|
128 |
<h2>Testing</h2>
|
|
|
129 |
<p>
|
|
|
130 |
To test the <tt>ReadPost</tt> page, visit the URL <tt>http://hostname/blog/index.php</tt> and click on the title of the only post. Our browser will display the following result with the URL <tt>http://hostname/blog/index.php?page=ReadPost&id=1</tt>. Note, if we do not login, the two link buttons will be invisible.
|
|
|
131 |
</p>
|
|
|
132 |
|
|
|
133 |
<img src="<%~ output2.gif %>" class="output" />
|
|
|
134 |
|
|
|
135 |
</com:TContent>
|