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>NewPost</tt> Page</h1>
4
 
5
<p>
6
The <tt>NewPost</tt> page is provided to authenticated users for creating new blog posts. It needs to display a form that collects the information about the new post, including the post title and the post body content.
7
</p>
8
 
9
<p>
10
Because <tt>NewPost</tt> can only be accessed by authenticated users, we add a page configuration file <tt>config.xml</tt> under the directory <tt>protected/pages/posts</tt>. The configuration specifies that authenticated users can access <tt>NewPost</tt> and <tt>EditPost</tt> which is to be introduced in the next section. All other users only have access to <tt>ListPost</tt> and <tt>ReadPost</tt>.
11
</p>
12
 
13
<com:TTextHighlighter CssClass="source" Language="xml">
14
<?xml version="1.0" encoding="utf-8"?>
15
<configuration>
16
  <authorization>
17
    <allow pages="NewPost,EditPost" users="@" />
18
    <allow pages="ListPost,ReadPost" />
19
    <deny users="*" />
20
  </authorization>
21
</configuration>
22
</com:TTextHighlighter>
23
 
24
<com:TipBox>
25
It's always a good idea to start with a <tt>deny="*"</tt> catch all rule at the bottom and then step by step grant access to pages with additional rules.
26
</com:TipBox>
27
 
28
<p>
29
As the number of our pages expands, we would like to modify <tt>MainLayout</tt> so that in the footer of our blog pages there are links to various pages, including the homepage, the <a href="?page=Day3.CreateNewUser">NewUser</a> page (visible to the administrator only), and the upcoming <tt>NewPost</tt> page (visible to authenticated users only).
30
</p>
31
 
32
<com:TTextHighlighter CssClass="source" Language="prado">
33
<div id="footer">
34
&lt;com:THyperLink Text="Home"
35
	NavigateUrl="&lt;%= $this->Service->DefaultPageUrl %>" />
36
 
37
&lt;com:THyperLink Text="New Post"
38
	NavigateUrl="&lt;%= $this->Service->constructUrl('posts.NewPost') %>"
39
	Visible="&lt;%= !$this->User->IsGuest %>" />
40
 
41
&lt;com:THyperLink Text="New User"
42
	NavigateUrl="&lt;%= $this->Service->constructUrl('users.NewUser') %>"
43
	Visible="&lt;%= $this->User->IsAdmin %>" />
44
...other links...
45
</div>
46
</com:TTextHighlighter>
47
 
48
<p>
49
We now create two files <tt>protected/pages/posts/NewPost.page</tt> and <tt>protected/pages/posts/NewPost.php</tt> to save the page template and page class, respectively.
50
</p>
51
 
52
<h2>Creating Page Template</h2>
53
<p>
54
The <tt>NewPost</tt> page template contains a <a href="http://www.pradosoft.com/demos/quickstart/?page=Controls.TextBox">TTextBox</a> to collect the post title and a <a href="http://www.pradosoft.com/demos/quickstart/?page=Controls.HtmlArea">THtmlArea</a> to collect the post content. The latter is a WYSIWYG HTML editor. To ensure the user input is valid, we associate validators with these input controls.
55
</p>
56
 
57
<com:TTextHighlighter CssClass="source" Language="prado">
58
&lt;%@ Title="My Blog - New Post" %>
59
 
60
&lt;com:TContent ID="Main">
61
 
62
<h1>Create New Post</h1>
63
 
64
<span>Title:</span>
65
&lt;com:TRequiredFieldValidator
66
	ControlToValidate="TitleEdit"
67
	ErrorMessage="Please provide a title."
68
	Display="Dynamic" />
69
<br/>
70
&lt;com:TTextBox ID="TitleEdit" Columns="50" />
71
 
72
<br/>
73
<span>Content:</span>
74
&lt;com:TRequiredFieldValidator
75
	ControlToValidate="ContentEdit"
76
	ErrorMessage="Please provide content."
77
	Display="Dynamic" />
78
<br/>
79
&lt;com:THtmlArea ID="ContentEdit" />
80
 
81
<br/>
82
&lt;com:TButton Text="Create" OnClick="createButtonClicked" />
83
 
84
&lt;/com:TContent>
85
</com:TTextHighlighter>
86
 
87
 
88
<h2>Creating Page Class</h2>
89
 
90
<p>
91
From the above page template, we see that we mainly need to write a page class that implements the event handler: <tt>createButtonClicked()</tt> (attached to the <tt>Create</tt> button's <tt>OnClick</tt> event).
92
</p>
93
 
94
<com:TTextHighlighter CssClass="source" Language="php">
95
class NewPost extends TPage
96
{
97
	/**
98
	 * Creates a new post if all inputs are valid.
99
	 * This method responds to the OnClick event of the "create" button.
100
	 * @param mixed event sender
101
	 * @param mixed event parameter
102
	 */
103
	public function createButtonClicked($sender,$param)
104
	{
105
		if($this->IsValid)  // when all validations succeed
106
		{
107
			// populates a PostRecord object with user inputs
108
			$postRecord=new PostRecord;
109
			// using SafeText instead of Text avoids Cross Site Scripting attack
110
			$postRecord->title=$this->TitleEdit->SafeText;
111
			$postRecord->content=$this->ContentEdit->SafeText;
112
			$postRecord->author_id=$this->User->Name;
113
			$postRecord->create_time=time();
114
			$postRecord->status=0;
115
 
116
			// saves to the database via Active Record mechanism
117
			$postRecord->save();
118
 
119
			// redirects the browser to the newly created post page
120
			$url=$this->Service->constructUrl('posts.ReadPost',array('id'=>$postRecord->post_id));
121
			$this->Response->redirect($url);
122
		}
123
	}
124
}
125
</com:TTextHighlighter>
126
 
127
<h2>Testing</h2>
128
<p>
129
To test the <tt>NewPost</tt> page, login first and click on the <tt>New Post</tt> link button in the footer of the homepage. Our browser will display the following result with the URL <tt>http://hostname/blog/index.php?page=NewPost</tt>.
130
</p>
131
 
132
<com:InfoBox>
133
When you visit the <tt>NewPost</tt> page for the first time, you may notice that it takes several seconds for the page to be displayed. This is because PRADO needs to unpack and publish the javascript code and images for the <tt>THtmlArea</tt> control used in the page. This is done once and for all.
134
</com:InfoBox>
135
 
136
<com:TipBox>
137
To test the pagination feature that we developed for the <a href="?page=Day4.CreateListPost">ListPost</a> page, we can create five or more posts and see what happens to the homepage. The pager in <tt>ListPost</tt> displays five posts each page.
138
</com:TipBox>
139
 
140
<img src="<%~ output3.gif %>" class="output" />
141
 
142
</com:TContent>