Administrator Recipe: New Employee

The goal of the final page in the Employee Lookup application is to add an employee to the data source. But isn't that just a basic insert record operation? Although our implementation does use Dreamweaver's standard Insert Record server behavior, we've also added a bit of intelligence that ties this page into the rest of the application. As we saw in the New Job page, you can't hire an employee unless there is at least one job opening. If you try to add an employee to the data source and no job opening is available, this page redirects you to the New Job page and displays an error message.

Step 1: Implement Design

Building a static page with all the necessary form elements is the first step.

  1. Create a basic dynamic page, either by hand or derived from a template.

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

  2. Add a table to the content region of your page to contain the interface elements for the application.

    graphics/book.gif From the Snippets panel, drag the Recipes > EmployeeLookup > Wireframes > New Employee - Wireframe snippet into the Content editable region.

  3. Within the table, insert the form and necessary form elements for the new employee. If you follow our example for this page, you'll need five text fields: first name, last name, email, direct line, cell phone, and cubicle number. You'll also need a list element for the job listings as well as a Submit button.

    graphics/book.gif Place your cursor in the row below the words NEW EMPLOYEE and insert the Recipes > EmployeeLookup > Forms > New Employee - Form snippet [r2-17].


    Figure r2-17.

    graphics/04fig17.gif


Step 2: Add Database Components

A single recordset is needed for this page to populate the Job Title list element. To develop the list of jobs, we'll use an Access view and nested SQL statement. The SQL statement reads as follows:

 
 SELECT * FROM JobsWithDepartments WHERE JobID NOT IN (SELECT EmployeeJob AS JobID FROM Employees) 
 

Essentially, with this SQL statement, the recordset displays all the JobID records (that is, all the jobs) that are not assigned to an employee.

graphics/book.gif

Before you begin this step, you'll need to copy the SQL code from the appropriate snippet by right-clicking on Recipes > EmployeeLookup > SQL > New Employee Jobs RS snippet for your server model and then, from the context menu, choose Copy Snippet.


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

  2. In the advanced Recordset view, enter a meaningful name for the query.

    graphics/book.gif Enter Jobs in the Name field.

  3. Select your data source connection.

    graphics/book.gif Choose Recipes from the Connections (or Data Source) list.

  4. ColdFusion users: Enter the username and password for the data source, if necessary.

  5. In the SQL area, enter the following code:

    graphics/book.gif Paste the copied snippet into the SQL field by pressing Ctrl-V (Command-V).

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

     SELECT * FROM JobsWithDepartments WHERE JobID NOT IN (SELECT EmployeeJob AS JobID FROM Employees) 

    graphics/php.gif

     SELECT jobswithdepartments.* FROM jobswithdepartments LEFT JOIN employees ON jobswithdepartments.JobID= employees.EmployeeJob WHERE employees.EmployeeJob IS NULL) 

  6. Verify your code and close the dialog to insert the recordset.

  7. Save your page.

Note

The JobsWithDepartment view was previously used on the Update Employee page and is discussed in that section.


Step 3: Data Binding Process

On this page, only one form element the list needs to be populated dynamically.

  1. Select the JobTitle list element.

  2. From the Property inspector, select Dynamic to open the Dynamic List/Menu dialog.

  3. In the Options From Recordset list, choose Jobs.

  4. In the Values field, select JobID.

  5. In the Labels field, choose JobAndDepartment.

  6. Leave the Select Value Equal To field blank and click OK to close the dialog.

By previewing the page in a browser, you can check to see what job titles are currently available [r2-18]. You won't be able to use Live Data Preview because list elements are not populated within Dreamweaver in Preview mode.


Figure r2-18.

graphics/04fig18.jpg


Step 4: Insert Record New Employee

Now it's time to apply the Insert Record server behavior.

Note

As noted before, the Insert Record dialog is different for ASP and ColdFusion server models and easiest understood when presented separately.


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

  2. In the Insert Record dialog, select your connection from the list.

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

  3. Select the table in the data source to modify from the list.

    graphics/book.gif Choose Employees from the Insert into Table list.

  4. Enter the path to the file you want the user to visit after the record has been updated in the After Inserting, Go To field.

    graphics/book.gif Choose Browse and locate the employee_results.asp file.

  5. Choose the form to use.

    graphics/book.gif Select NewEmployee from the Get Values From list.

  6. With the current form selected in the Get Values From list, set the form elements to their corresponding data source fields.

    graphics/book.gif Set the FirstName form element to the EmployeeFirst data column and submit as Text.

    graphics/book.gif Set the LastName form element to the EmployeeLast data column and submit as Text.

    Set the Email form element to the EmployeeEmail data column and submit as Text.

    Set the Phone form element to the EmployeePhone data column and submit as Text.

    Set the Phone2 form element to the EmployeeMobile data column and submit as Text.

    Set the JobTitle form element to the EmployeeJob data column and submit as Numeric.

    Set the CubeNumber form element to the EmployeeCube data column and submit as Numeric.

  7. Verify your choices and click OK to close the dialog.

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

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

    graphics/book.gif Select NewEmployee 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. Enter your username and password, if needed.

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

    graphics/book.gif Choose Employees (employees 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 EmployeeID data column is set to an Unused Primary Key.

    Set EmployeeFirst to the FORM.FirstName form element and submit as Text.

    Set EmployeeLast to the FORM.LastName form element and submit as Text.

    Set EmployeeEmail to the FORM.Email form element and submit as Text.

    Set EmployeePhone to the FORM.Phone form element and submit as Text.

    Set EmployeeMobile to the FORM.Phone2 form element and submit as Text.

    Set EmployeeJob to the FORM.JobTitle form element and submit as Numeric for ColdFusion and Integer for PHP.

    Set EmployeeCube to the FORM.CubeNumber form element and submit as Numeric for ColdFusion and Integer for PHP.

  7. Enter the path to the file you want the user to visit after the record has been inserted in the After Inserting, Go To field.

    graphics/book.gif Choose Browse and locate the employee_results file for your server model.

  8. Verify your choices and click OK to close the dialog.

Step 5: Hand Code Redirect When No Job Openings

Our final step on this page and the entire application is to insert some custom code to properly react when no jobs are available. Because this redirect relies on the recordset on the page which returns a list of available jobs you can place the redirect on the page itself, where you can be sure the recordset code has already been defined. As noted earlier, should no jobs be available, the code redirects the visitor to the New Job page and triggers the error message previously embedded.

  1. In Code view, place your cursor just above the opening <html> tag.

    Although you could place this code elsewhere for ASP and ColdFusion, PHP requires that the code appear directly after the recordset definition.

  2. Insert the following code:

    graphics/book.gif From the Snippets panel, open the Recipes > EmployeeLookup > Custom Code folder for your server model and insert the No Job Openings - Display Text snippet.

    graphics/vb.gif

     <% if (Jobs.EOF) then   Response.Redirect("new_job.asp?empty=true") end if %> 

    graphics/js.gif

     <% if (Jobs.EOF)   Response.Redirect("new_job.asp?empty=true"); %> 

    graphics/cf.gif

     <cfif Jobs.RecordCount EQ 0>   <cflocation url="new_job.cfm?empty=true"> </cfif> 

    graphics/php.gif

     <?php if ($totalRows_Jobs<=0) {      $url = "new_job.php?empty=true";      Header("Location: $url"); } ?> 

  3. Save your page.

The only way to test the redirection functionality is to preview the page in a browser when all the available jobs are filled. The recipe data source has a full slate of company employees for you to test your application with.



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