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>AdminUser</tt> Page</h1>
4
 
5
<p>
6
The <tt>AdminUser</tt> page displays all user accounts in a list so that the administrator can perform some administrative work. For simplicity, the administrative work our blog system supports include editting a user account and deleting a user account.
7
</p>
8
 
9
<p>
10
We will display the user list in a table. Each row of the table represents a single user account, and the following columns are to be displayed:
11
</p>
12
<ul>
13
<li>Username - displays the usernames. In each cell a hyerplink is displayed which leads to the corresponding <a href="?page=Day3.CreateEditUser">EditUser</a> page.</li>
14
<li>Email - displays the emails.</li>
15
<li>Administrator - shows whether the user account is of the administrator role.</li>
16
<li>Command - displays a column of "Delete" buttons. Clicking on any of them will lead to deletion of the corresponding user account.</li>
17
</ul>
18
 
19
<p>
20
We create two files <tt>protected/pages/users/AdminUser.page</tt> and <tt>protected/pages/users/AdminUser.php</tt> to save the page template and page class, respectively.
21
</p>
22
 
23
<h2>Creating Page Template</h2>
24
<p>
25
We use <a href="http://www.pradosoft.com/demos/quickstart/?page=Controls.DataGrid">TDataGrid</a> to display the user accounts. Based on the above analysis, we configure the following four columns:
26
</p>
27
<ul>
28
<li><a href="http://www.pradosoft.com/docs/classdoc/THyperLinkColumn">THyperLinkColumn</a> displays the username column. The URL is constructed according to the PHP expression specified in the <tt>DataNavigateUrlFormatString</tt> property.</li>
29
<li><a href="http://www.pradosoft.com/docs/classdoc/TBoundColumn">TBoundColumn</a> displays the email column.</li>
30
<li><a href="http://www.pradosoft.com/docs/classdoc/TCheckBoxColumn">TCheckBoxColumn</a> uses checkboxes to indicate whether a user account is of role administrator.</li>
31
<li><a href="http://www.pradosoft.com/docs/classdoc/TButtonColumn">TButtonColumn</a> displays a column of "Delete" buttons.</li>
32
</ul>
33
 
34
<p>The complete page template is shown as follows:</p>
35
 
36
<com:TTextHighlighter CssClass="source" Language="prado">
37
&lt;%@ Title="My Blog - Manage User Accounts" %>
38
 
39
&lt;com:TContent ID="Main">
40
 
41
<h1>Manage User Accounts</h1>
42
 
43
<a href="&lt;%= $this->Service->constructUrl('users.NewUser')%>">Create New User</a>
44
<br/>
45
 
46
&lt;com:TDataGrid ID="UserGrid"
47
    DataKeyField="username"
48
    AutoGenerateColumns="false"
49
    OnDeleteCommand="deleteButtonClicked">
50
 
51
    &lt;com:THyperLinkColumn
52
        HeaderText="Username"
53
        DataTextField="username"
54
        DataNavigateUrlField="username">
55
        &lt;prop:DataNavigateUrlFormatString>#
56
          $this->Service->constructUrl('users.EditUser',array('username'=>{0}))
57
        &lt;/prop:DataNavigateUrlFormatString>
58
    &lt;/com:THyperLinkColumn>
59
 
60
    &lt;com:TBoundColumn
61
        HeaderText="Email"
62
        DataField="email" />
63
 
64
    &lt;com:TCheckBoxColumn
65
        HeaderText="Administrator"
66
        DataField="role" />
67
 
68
    &lt;com:TButtonColumn
69
        HeaderText="Command"
70
        Text="Delete"
71
        ButtonType="PushButton"
72
        CommandName="delete" />
73
 
74
&lt;/com:TDataGrid>
75
 
76
&lt;/com:TContent>
77
</com:TTextHighlighter>
78
 
79
 
80
<h2>Creating Page Class</h2>
81
 
82
<p>
83
In the above page template, the datagrid's <tt>OnDeleteCommand</tt> event is ttached with the method <tt>deleteButtonClicked()</tt> which we shall implement in the page class. In addition, the datagrid needs to be populated with user accounts data when the page is initialized. Therefore, we write the page class as follows:
84
</p>
85
 
86
<com:TTextHighlighter CssClass="source" Language="php">
87
class AdminUser extends TPage
88
{
89
    /**
90
     * Populates the datagrid with user lists.
91
     * This method is invoked by the framework when initializing the page
92
     * @param mixed event parameter
93
     */
94
    public function onInit($param)
95
    {
96
        parent::onInit($param);
97
        // fetches all data account information
98
        $this->UserGrid->DataSource=UserRecord::finder()->findAll();
99
        // binds the data to interface components
100
        $this->UserGrid->dataBind();
101
    }
102
 
103
    /**
104
     * Deletes a specified user record.
105
     * This method responds to the datagrid's OnDeleteCommand event.
106
     * @param TDataGrid the event sender
107
     * @param TDataGridCommandEventParameter the event parameter
108
     */
109
    public function deleteButtonClicked($sender,$param)
110
    {
111
        // obtains the datagrid item that contains the clicked delete button
112
        $item=$param->Item;
113
        // obtains the primary key corresponding to the datagrid item
114
        $username=$this->UserGrid->DataKeys[$item->ItemIndex];
115
        // deletes the user record with the specified username primary key
116
        UserRecord::finder()->deleteByPk($username);
117
    }
118
}
119
</com:TTextHighlighter>
120
 
121
<p>
122
In the above, the <tt>deleteButtonClicked()</tt> method is invoked whenever a "Delete" button is clicked. To determine which row of the buttons is clicked, we check the <tt>Item.ItemIndex</tt> property of the event parameter. To further identify which user account is to be deleted, we retrieve the primary key (username) value via the datagrid's <tt>DataKeys</tt> property.
123
</p>
124
 
125
<com:TipBox>
126
All <a href="http://www.pradosoft.com/docs/classdoc/TDataBoundControl">data-bound</a> controls have similar usage pattern. That is, set the <tt>DataSource</tt> property with the data and call <tt>dataBind()</tt> method to binds the data to the control's internal structure.
127
</com:TipBox>
128
 
129
 
130
<h2>Testing</h2>
131
<p>
132
To test the <tt>AdminUser</tt> page, visit the URL <tt>http://hostname/blog/index.php?page=users.AdminUser</tt>. You may be required to login as an administrator first if you have not done so. We shall expect to see the following result.
133
</p>
134
 
135
<img src="<%~ output3.gif %>" class="output" />
136
 
137
</com:TContent>