Administrator Recipe: Managing Users
With all the user-oriented recipes completed, it's time to build a few pages geared toward the administration. Our first component combines two functions: a master list of registered users and the form for adding new users directly. The add user section is an enhanced version of the register
user
form we've already built. In addition to the record fields included (
UserFirstName
,
Password
,
UserEmail
, and so on), the administrator also has the capability to set access levels.
Step 1: Implement User Manager Design
The two
components
of the
user manager
page require two discrete sections. The top section will display a list of all the users in a Dreamweaver Repeat Region server behavior and will provide a link to a detail record page. The bottom section allows direct entry of new users.
-
Open
a new dynamic page, either constructing one by hand or deriving one from a template.
In the
UserLogin
folder, locate the folder for your server model and open the
user_manager
page found there.
-
Add a table to the content region of your page to contain the first interface element for the application, its title. This wireframe table should have a minimum of three rows.
From the Snippets panel, drag the
Recipes > UserLogin > Wireframes > User Manager - Wireframe
snippet into the Content editable region.
-
Within the table, insert the text placeholders for a recordset navigation status object and a recordset navigation bar in the top row. In the second row, add headers for
User
Name
and
Access
Group
. The third row of the table will eventually hold the dynamic data. It's often best to insert a nested table in a situation like this.
Place your cursor in the row below the words USER MANAGER and insert the
Recipes > UserLogin > Content Tables > View Users - Content Table
snippet [r1-17].
Figure r1-17.
-
In the third row of the wireframe table, add a form with elements for inserting a new user record. These elements include five text fields with their labels (
First Name
,
Last Name
,
User Name
,
Password
, and
Email Address
), a drop-down list for assigning group access, and a form button for adding a new record.
Place your cursor in the last row of the wireframe table and insert the
Recipes > UserLogin > Forms > Add New User - Form
snippet [r1-18].
Figure r1-18.
Step 2: Add Database Components
As you might expect with two visual components, this page uses two recordsets. However, what you might not suspect is that one of the recordsets combines two tables in what is known in SQL as an
inner join
. The first recordset gives us the list of access groups set up in the data source and is used to populate the drop-down list. The second recordset uses the inner join and, for the ease of our ASP and ColdFusion Recipes users, is available as an Access view. This recordset returns all the users and their corresponding access levels, which will be integrated into the initial section of the page. Because MySQL does not support views, we'll have to code the SQL directly for PHP.
Let's create the simple recordset first:
AccessGroups
.
-
From the Bindings panel, choose Add (+) and select
Recordset (Query)
.
-
In the Recordset dialog's simple view, enter the desired name, such as
AccessGroups
.
-
Select your data source connections.
Choose
Recipes
from the Connections list.
-
Select the table containing the user information.
Choose
AccessGroups
(
accessgroups
for PHP) from the Table list.
-
Make sure the Columns option is set to All.
-
Leave both Filter and Sort to None and click OK to close the dialog.
-
Save the page.
The second, more complex recordset is handled differently for the different server models.
For ASP and ColdFusion
As previously noted, this recordset requires an Access view that joins two tables,
Users
and
AccessGroups
. Here's the SQL behind the view, called
UserView
, for both version of ASP and ColdFusion:
To implement this SQL statement, we need only use Dreamweaver's simple Recordset dialog for ASP and ColdFusion.
-
From the Bindings panel, choose Add (+) and select
Recordset (Query)
.
-
In the Recordset dialog's simple view, enter an appropriate name.
Enter
Users
in the Recordset field.
-
Select your data source connections.
Choose
Recipes
from the Connections list.
-
Select the table or view containing the user information.
Select
UserView
from the Tables list.
-
Make sure the Columns option is set to All.
-
Leave both Filter and Sort to None and click OK to close the dialog.
-
Click OK to close the dialog and verify that the recordset is added to the Bindings panel.
-
Save the page.
For PHP
The actual SQL for the PHP version varies slightly from the version used in the other server models, but the major difference is that the SQL statement must be added directly in Dreamweaver because MySQL does not support views.
-
From the Bindings panel, choose Add (+) and select
Recordset (Query)
.
-
Switch to the advanced view of the Recordset dialog, if necessary.
-
Enter an appropriate name for the recordset.
Enter
Users
in the Recordset field.
-
Choose the data source.
Select
Recipes
from the Connections list.
-
Enter the following code in the SQL area:
[View full width]
SELECT * FROM users INNER JOIN accessgroups ON users.UserAccess = accessgroups.AccessGroupID ORDER
BY users.UserID DESC;
Note that the
ORDER
is set to
DESC
(descending) rather than
ASC
(
ascending
); this displays the last record entered first and makes it easier for administrators to track newly registered users.
-
Click OK to close the dialog and insert the recordset.
-
Save your page.
The Bindings panel should now have two recordsets and one session object.
Step 3: Data Binding Process
Initially, two data fields need to be bound to this page, both from the
Users
recordset. We'll also create a link to a detail page so that the administrator can easily navigate to a specific user's record for updating.
-
From the Bindings panel, expand the
Users
recordset.
-
Drag the
UserName
field onto the page and into the table
cell
below the User Name heading.
Dreamweaver displays this dynamic data as
{Users.UserName}
.
-
Drag the
AccessGroupName
field onto the page and into the table cell below the Access Group heading.
Now we're ready to link to our detail page.
-
Select the dynamic data
{Users.UserName}
.
-
From the Property inspector, select the
Link
folder icon.
-
In the Select File dialog, choose
Parameters
.
-
In the Parameters dialog, enter the variable name.
In the Name column, enter
ID
.
-
Enter the dynamic value.
Under the Value column, select the lightning bolt to open the Dynamic Data dialog and select
UserID
from the
Users
recordset.
Click OK once to close the Dynamic Data dialog and again to close the Parameters dialog.
-
Select the file you want to pass the parameter to.
Select Browse and choose the
edit_user
file for your server model.
-
Click OK to close the dialog.
You might notice that a different mechanism is being used for the first time in this application to connect with an associated record. Rather than looking at the session object,
UserID
, as we did previously, we're passing a URL parameter. This technique will allow us to see records other than those of the user, whose own
UserID
will be inspected to make sure someone with the proper access level is attempting to adjust a user's record.
One other element on the page needs to be bound to a data field: the Access Groups list. Although it is tempting to hard-code the list options,
especially
with a limited number of values, it is best not to. Should the data source back-end ever change—adding new access levels, for example—the front-end (this form) would have to be modified as well. By binding the data to the list, any alterations made to the data source are instantly reflected in the application page.
-
Select the
Access Group
list item in the form.
-
From the Property inspector, click the Dynamic button.
The Dynamic List/Menu dialog appears [r1-19].
Figure r1-19.
-
Verify that the
AccessGroup
list element is selected in the Menu list.
-
From the Options From Recordset list choose
AccessGroups
.
-
Set the Values list to
AccessGroupID
.
These are the values that get inserted into the record. Numbers (1 through 4) are used here rather than words (User, Administrator, and so on) to make comparisons easier.
-
Set the Labels (what the user sees) to
AccessGroupNames
.
-
In the Select Value Equal To field, enter
4
and click OK to close the dialog.
By setting the selected value to 4, the administrator's default choice is the most frequently accessed:
User
.
Step 4: Create a Repeat Region and Application Objects
If you preview the page now in Dreamweaver by choosing the Live Data View button on the toolbar, you'll see the name and access group for just one record, the last entered. To show multiple records, a Repeat Region server behavior is needed. A Repeat Region can show all the data or just a portion of it. Because we don't want to overwhelm the administrator with too much data, we'll only show a few records at a time. To make all of the records accessible in small groups, we'll also add some recordset navigation and status
reports
, all easily inserted with Dreamweaver.
Let's start with adding the Repeat Region to the recordset:
-
Place the cursor in either of the table
cells
containing the dynamic data.
-
From the Tag Selector, select the
<tr>
tag, just to the left of the current
<td>
tag [r1-20].
Figure r1-20.
-
From the Server Behaviors panel, choose Add (+) and select
Repeat Region
.
-
In the Repeat Region dialog, select the
Users
recordset.
-
Accepting the default of
Showing 10 Records at a Time
, click OK to close the dialog.
Now, if you go into Live Data view, you'll see the data from the last 10 users to register (r1-21). Of course, if you have fewer than 10 users registered, you'll see all the records. To see the
next
group of records, we'll now add some recordset navigation tools.
Figure r1-21.
First we'll put in the navigation elements
themselves
by using one of Dreamweaver's Application Objects, the Recordset Navigation Bar.
-
Place your cursor in the table where you'd like the navigation links to appear.
Be careful not to place your cursor within the Repeat Region.
Delete the text that says, "[insert recordset navigation bar object]" and leave your cursor in that cell.
-
Choose
Insert > Application Objects > Recordset Paging > Recordset Navigation Bar
.
Alternatively, you could select the Recordset Navigation Bar object from the Recordset Paging menu of the Insert bar's Application category.
-
In the Recordset Navigation Bar dialog, select the
Users
recordset.
-
Set the Display Using option to
Text
and click OK.
The Recordset Navigation Bar object is used to display four navigation options: First, Previous, Next, and Last. Each text object contains a hyperlink with functionality to move the cursor to the correct record in the result set.
Additionally, the Recordset Navigation Bar includes built-in server behaviors to show the links only when it is relevant to do so. For example, when the page is first previewed, no Previous link is displayed because there are no prior records to show. Similarly, when the last record is onscreen, the Next link is hidden.
Note
The server behaviors automatically added when inserting the Recordset Navigation Bar can be seen in the Server Behaviors panel.
As you are paging through a recordset, it's helpful to have some feedback
stating
where you are in the data. Dreamweaver includes another Application Object called Recordset Navigation Status, which provides just such feedback. When added to a page, this object helps the user keep track of the recordset by showing the current records displayed and the total number of records. The Recordset Navigation Status object shows the information like this:
Records 21 to 30 of 55
.
-
Place your cursor in the table where you'd like the navigation status to be displayed.
Again, be careful not to place your cursor within the Repeat Region.
Delete the text that says, "[insert recordset navigation status object]" and leave your cursor in that cell.
-
Choose
Insert > Application Objects > Display Recordset Count > Recordset Navigation Status
.
Alternatively, you could select the Recordset Navigation Status object from the Display Recordset Count menu of the Insert bar's Application category.
-
In the Recordset Navigation Status dialog, select the
Users
recordset and click OK.
To see the initial view, switch to Live Data view [r1-22]. To test the recordset navigation controls, you'll need to preview the page in a browser because Dreamweaver does not support clicking on links.
Figure r1-22.
Step 5: Insert New Users
The administrative version of the form to add new users is similar to the user version except for one difference. An additional form element has been added: the Access Group drop-down list, which lets the administrator
designate
a particular access group for any newly declared user.
For ASP
-
From the Server Behaviors panel, chose Add (+) and select
Insert Record
to display the dialog.
-
Select the connection to the data source.
Choose
Recipes
from the Connection list.
-
From the Insert Into Table list, choose the
Users
table.
-
Leave the After Inserting, Go To field blank.
By leaving this field blank, this same page will reload after the record is inserted. Thus, the administrator can enter record after record.
-
Set the Get Values From field to the form name,
AddNewUser
.
-
For the form elements shown in the list, set each one to its equivalent in the data source:
Set
FirstName
to
UserFirstName
as Text.
Set
Lastname
to
UserLastName
as Text.
Set
UserName
to
UserName
as Text.
Set
Password
to
UserPassword
as Text.
Set
EmailAddress
to
UserEmail
as Text.
Set
AccessGroup
to
UserAccess
at Numeric.
-
When you're done, click OK to close the dialog and insert the behavior.
-
Save the page as
user_manager
using the appropriate extension for your platform.
For ColdFusion and PHP
-
From the Server Behaviors panel, choose Add (+) and select
Insert Record
.
-
In the Insert Record dialog, choose the current form.
Select
AddNewUser
from the Submit Values From list.
-
Select your data source from the list.
Choose
Recipes
from the Data Source list.
-
ColdFusion users should enter their username and password, if needed.
-
Select the table in the data source to insert into from the list.
Choose
Users
(
users
for PHP) from the Insert Into Table list.
-
Set the data source fields to their corresponding form elements.
Make sure the
UserID
data column is set to be an unused Primary Key.
Set
UserAccess
to the
FORM.AccesGroup
and submit as Numeric type for ColdFusion and Integer type for PHP.
Set
UserFirstName
to the
FORM.FirstName
form element and submit as Text type.
Set
UserLastName
to the
FORM.LastName
form element and submit as Text type.
Set
UserName
to the
FORM.UserName
form element and submit as Text type.
Set
UserPassword
to the
FORM.UserPassword
form element and submit as Text type.
Set
UserEmail
to the
FORM.EmailAddress
form element and submit as Text type.
UserRegDate
should not get a value from the form.
-
In the After Inserting, Go To field, enter
report_projectsummary.cfm
and click OK to close the dialog.
-
Save the page.
|