Difference between revisions of "DesignPatternStep6"

From mi-linux
Jump to navigationJump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Main Page]] >> [[6CC001|Advanced Web Technologies]] >> [[6CC001 - Workbook|Workbook]] >> [[6CC001 Workshop - week 04|Week 04]] >> Step 6 - Adding messages
 
[[Main Page]] >> [[6CC001|Advanced Web Technologies]] >> [[6CC001 - Workbook|Workbook]] >> [[6CC001 Workshop - week 04|Week 04]] >> Step 6 - Adding messages
 +
 +
== Step 6 - Adding messages ==
 +
 +
Next, let’s add a command to add blog messages. You might have noticed above that our “add messages” link (see previous section) points to “index.php?cmd=BlogAddForm”
 +
 +
So first, you need a new command called “Command_BlogAddForm”. It doesn’t do much, it simply displays the view containing the form. Here is the code:
 +
 +
<pre>
 +
// Command_BlogAddForm.php
 +
 +
//############################################################################
 +
// SearchVenueCommand class
 +
//############################################################################
 +
class Command_BlogAddForm extends Command
 +
{
 +
  //############################################################################
 +
  // doExecute
 +
  //############################################################################
 +
  function doExecute(Request $request)
 +
  {
 +
    // Include view
 +
    include("views/v_add_form.php");
 +
  }
 +
}
 +
</pre>
 +
 +
And here is the view containing the form:
 +
 +
<pre>
 +
<!-- views/v_add_form.php -->
 +
 +
<?require_once("ViewHelper.php");?>
 +
 +
<?ViewHelper::DisplayHeader("Add message");?>
 +
 +
<h1>Blog add form</h1>
 +
 +
<p>Please fill in the fields</p>
 +
 +
<form action="index.php?cmd=BlogAdd" method="post">
 +
  Title:<br><input name="title">
 +
  <br>
 +
  Message:<br><textarea name="message"></textarea>
 +
  <br>
 +
  <input type="submit" value="Add">
 +
</form>
 +
   
 +
<?ViewHelper::DisplayFooter();?>
 +
</pre>
 +
 +
Again note that the "action" attribute of the <FORM> above points to "index.php?cmd=BlogAdd". So we need another action responsible for reading the user data from the form, and creating a new record in the database... here it is!
 +
 +
<pre>
 +
// Command_BlogAdd.php
 +
 +
//############################################################################
 +
// Command_BlogAdd class
 +
//############################################################################
 +
class Command_BlogAdd extends Command
 +
{
 +
  //############################################################################
 +
  // doExecute
 +
  //############################################################################
 +
  function doExecute(Request $request)
 +
  {
 +
    // Get data from request
 +
    $title = $request->getProperty('title');
 +
    $message = $request->getProperty('message');
 +
   
 +
    // Create manager object
 +
    $manager = new ManagerMessage();   
 +
   
 +
    // Add to database
 +
    $manager->addMessage($title, $message);
 +
   
 +
    // Redirect to index
 +
    header("location:index.php");
 +
  }
 +
}
 +
</pre>
 +
 +
Note: This action doesn't have a view. It simply redirects the user to the index of the forum.
 +
 +
=== Checkpoint ===
 +
 +
You should now be able to add new messages... try it!
 +
 +
>> Onto [[DesignPatternStep7|Step 7]]

Latest revision as of 14:55, 22 September 2011

Main Page >> Advanced Web Technologies >> Workbook >> Week 04 >> Step 6 - Adding messages

Step 6 - Adding messages

Next, let’s add a command to add blog messages. You might have noticed above that our “add messages” link (see previous section) points to “index.php?cmd=BlogAddForm”

So first, you need a new command called “Command_BlogAddForm”. It doesn’t do much, it simply displays the view containing the form. Here is the code:

// Command_BlogAddForm.php

//############################################################################
// SearchVenueCommand class
//############################################################################
class Command_BlogAddForm extends Command
{
  //############################################################################
  // doExecute
  //############################################################################ 
  function doExecute(Request $request)
  {
    // Include view
    include("views/v_add_form.php");
  }
}

And here is the view containing the form:

<!-- views/v_add_form.php -->

<?require_once("ViewHelper.php");?>

<?ViewHelper::DisplayHeader("Add message");?>

<h1>Blog add form</h1>

<p>Please fill in the fields</p>

<form action="index.php?cmd=BlogAdd" method="post">
  Title:<br><input name="title">
  <br>
  Message:<br><textarea name="message"></textarea>
  <br>
  <input type="submit" value="Add">
</form>
    
<?ViewHelper::DisplayFooter();?>

Again note that the "action" attribute of the <FORM> above points to "index.php?cmd=BlogAdd". So we need another action responsible for reading the user data from the form, and creating a new record in the database... here it is!

// Command_BlogAdd.php

//############################################################################
// Command_BlogAdd class
//############################################################################
class Command_BlogAdd extends Command
{
  //############################################################################
  // doExecute
  //############################################################################ 
  function doExecute(Request $request)
  {
    // Get data from request
    $title = $request->getProperty('title');
    $message = $request->getProperty('message');
    
    // Create manager object
    $manager = new ManagerMessage();    
    
    // Add to database
    $manager->addMessage($title, $message);
    
    // Redirect to index
    header("location:index.php");
  }
}

Note: This action doesn't have a view. It simply redirects the user to the index of the forum.

Checkpoint

You should now be able to add new messages... try it!

>> Onto Step 7