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.

  1. Open a new dynamic page, either constructing one by hand or deriving one from a template.

    graphics/book.gif In the UserLogin folder, locate the folder for your server model and open the user_manager page found there.

  2. 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.

    graphics/book.gif From the Snippets panel, drag the Recipes > UserLogin > Wireframes > User Manager - Wireframe snippet into the Content editable region.

  3. 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.

    graphics/book.gif 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.

    graphics/03fig17.jpg


  4. 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.

    graphics/book.gif 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.

    graphics/03fig18.jpg


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.

  1. From the Bindings panel, choose Add (+) and select Recordset (Query).

  2. In the Recordset dialog's simple view, enter the desired name, such as AccessGroups.

  3. Select your data source connections.

    graphics/book.gif Choose Recipes from the Connections list.

  4. Select the table containing the user information.

    graphics/book.gif Choose AccessGroups (accessgroups for PHP) from the Table list.

  5. Make sure the Columns option is set to All.

  6. Leave both Filter and Sort to None and click OK to close the dialog.

  7. 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:

graphics/vb.gifgraphics/js.gifgraphics/cf.gif

[View full width]

 SELECT *         FROM Users INNER JOIN AccessGroups ON Users graphics/ccc.gif.UserAccess = AccessGroups.AccessGroupID         ORDER BY Users.UserID DESC; 

To implement this SQL statement, we need only use Dreamweaver's simple Recordset dialog for ASP and ColdFusion.

  1. From the Bindings panel, choose Add (+) and select Recordset (Query).

  2. In the Recordset dialog's simple view, enter an appropriate name.

    graphics/book.gif Enter Users in the Recordset field.

  3. Select your data source connections.

    graphics/book.gif Choose Recipes from the Connections list.

  4. Select the table or view containing the user information.

    graphics/book.gif Select UserView from the Tables list.

  5. Make sure the Columns option is set to All.

  6. Leave both Filter and Sort to None and click OK to close the dialog.

  7. Click OK to close the dialog and verify that the recordset is added to the Bindings panel.

  8. 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.

  1. From the Bindings panel, choose Add (+) and select Recordset (Query).

  2. Switch to the advanced view of the Recordset dialog, if necessary.

  3. Enter an appropriate name for the recordset.

    graphics/book.gif Enter Users in the Recordset field.

  4. Choose the data source.

    graphics/book.gif Select Recipes from the Connections list.

  5. Enter the following code in the SQL area:

     

    [View full width]

    SELECT * FROM users INNER JOIN accessgroups ON users.UserAccess = accessgroups.AccessGroupID ORDER graphics/ccc.gif 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.

  6. Click OK to close the dialog and insert the recordset.

  7. 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.

  1. From the Bindings panel, expand the Users recordset.

  2. 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}.

  3. 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.

  4. Select the dynamic data {Users.UserName}.

  5. From the Property inspector, select the Link folder icon.

  6. In the Select File dialog, choose Parameters.

  7. In the Parameters dialog, enter the variable name.

    graphics/book.gif In the Name column, enter ID.

  8. Enter the dynamic value.

    graphics/book.gif Under the Value column, select the lightning bolt to open the Dynamic Data dialog and select UserID from the Users recordset.

    graphics/book.gif Click OK once to close the Dynamic Data dialog and again to close the Parameters dialog.

  9. Select the file you want to pass the parameter to.

    graphics/book.gif Select Browse and choose the edit_user file for your server model.

  10. 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.

  1. Select the Access Group list item in the form.

  2. From the Property inspector, click the Dynamic button.

    The Dynamic List/Menu dialog appears [r1-19].


    Figure r1-19.

    graphics/03fig19.gif


  3. Verify that the AccessGroup list element is selected in the Menu list.

  4. From the Options From Recordset list choose AccessGroups.

  5. 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.

  6. Set the Labels (what the user sees) to AccessGroupNames.

  7. 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:

  1. Place the cursor in either of the table cells containing the dynamic data.

  2. From the Tag Selector, select the <tr> tag, just to the left of the current <td> tag [r1-20].


    Figure r1-20.

    graphics/03fig20.gif


  3. From the Server Behaviors panel, choose Add (+) and select Repeat Region.

  4. In the Repeat Region dialog, select the Users recordset.

  5. 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.

graphics/03fig21.gif


First we'll put in the navigation elements themselves by using one of Dreamweaver's Application Objects, the Recordset Navigation Bar.

  1. 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.

    graphics/book.gif Delete the text that says, "[insert recordset navigation bar object]" and leave your cursor in that cell.

  2. 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.

  3. In the Recordset Navigation Bar dialog, select the Users recordset.

  4. 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.

  1. 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.

    graphics/book.gif Delete the text that says, "[insert recordset navigation status object]" and leave your cursor in that cell.

  2. 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.

  3. 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.

graphics/03fig22.gif


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
  1. From the Server Behaviors panel, chose Add (+) and select Insert Record to display the dialog.

  2. Select the connection to the data source.

    graphics/book.gif Choose Recipes from the Connection list.

  3. From the Insert Into Table list, choose the Users table.

  4. 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.

  5. Set the Get Values From field to the form name, AddNewUser.

  6. For the form elements shown in the list, set each one to its equivalent in the data source:

    graphics/book.gif 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.

  7. When you're done, click OK to close the dialog and insert the behavior.

  8. Save the page as user_manager using the appropriate extension for your platform.

For ColdFusion and PHP
  1. From the Server Behaviors panel, choose Add (+) and select Insert Record.

  2. In the Insert Record dialog, choose the current form.

    graphics/book.gif Select AddNewUser from the Submit Values From list.

  3. Select your data source from the list.

    graphics/book.gif Choose Recipes from the Data Source list.

  4. ColdFusion users should enter their username and password, if needed.

  5. Select the table in the data source to insert into from the list.

    graphics/book.gif Choose Users (users for PHP) from the Insert Into Table list.

  6. Set the data source fields to their corresponding form elements.

    graphics/book.gif 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.

  7. In the After Inserting, Go To field, enter report_projectsummary.cfm and click OK to close the dialog.

  8. Save the page.



Macromedia Dreamweaver MX 2004 Web Application Recipes
Macromedia Dreamweaver MX 2004 Web Application Recipes
ISBN: 0735713200
EAN: 2147483647
Year: 2003
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net