BlogTutorial:Edit Page

From Aspen Documentation

Now, we need the ability to edit one of our blog entries. In this step we'll create a basic form and code that processes the data for that.

Files we'll be editing in this step:

  • modules/Blog/Blog_Admin.php
  • modules/Blog/templates/index.tpl.php
  • modules/Blog/templates/edit.tpl.php

The add and edit functions are almost identical when designed using this method. In fact, the same template can be used for both add and edit functions. Even the processing code is almost exactly the same. The edit class code may be easily used to handle the add functionality as well.

Contents

Form Processing Code

Open for Editing: modules/Blog/Blog_Admin.php

We're going to be editing the edit function. There are many ways you can choose to handle forms in the framework, but we're going to cover the most common.

	public function edit($id = false){
 
		// tell our form handler that we'll be using
		// fields present in the "blog" table
		// and values found in record of $id
		$this->APP->form->loadRecord('blog', $id);
 
		// if form has been submitted
		if($this->APP->form->isSubmitted()){
 
			// insert a new record with available data
			if($this->APP->form->save($id)){
				// if successful insert, redirect to the list
				$this->APP->router->redirect('view');
			}
		}
 
		// make sure the template has access to all current values
		$data['values'] = $this->APP->form->getCurrentValues();
 
		$this->APP->template->addView($this->APP->template->getTemplateDir().DS . 'header.tpl.php');
		$this->APP->template->addView($this->APP->template->getModuleTemplateDir().DS . 'edit.tpl.php');
		$this->APP->template->addView($this->APP->template->getTemplateDir().DS . 'footer.tpl.php');
		$this->APP->template->display($data);
 
	}

Let's quickly look at each step so we understand what's happening.

$this->APP->form->loadRecord('blog', $id);

This first process tells the form processing class that it will be handling data that relates to fields in the blog table. The $id that we provide tells it that our current/default values can be found in that record.

if($this->APP->form->isSubmitted()){

isSubmitted returns true or false if there is POST value for an element named "submit". It's a good idea to name your form submit button "submit", as it will likely never need to be changed. If that value is present, the form has been submitted and we need to process it. This function loads in all POST values automatically.

Note: This method requires that you form field names match their related database table field names.

if($this->APP->form->save($id)){

This function instructs the form (and model) class to update the currently loaded values into the table we opened earlier. It will update the record identified by the $id argument. The $id must be the value to match the primary key field of your table.

$this->APP->router->redirect('view');

Upon success the redirect function instructs the system to redirect the user to the view page where we're already listing out the records.

Creating the Form

Create a new template file for our add form modules/Blog/templates/edit.tpl.php.

Next, we're going to build a standard html form.

<h3>Edit Blog Record</h3>
 
<p><?php print $this->createLink('Delete', 'delete', array('entry_id' => $values['entry_id'])); ?></p>
 
<form action="<?php print $this->createFormAction(); ?>" method="post">
	<p>
		<label for="entry_title">Entry Title:</label><br />
		<input type="text" name="entry_title" id="entry_title" value="<?php print $values['entry_title']; ?>" class="text" />
	</p>
	<p>
		<label for="entry_body">Entry Body:</label><br />
		<input type="text" name="entry_body" id="entry_body" value="<?php print $values['entry_body']; ?>" class="text" />
	</p>
	<p>
		<label for="date_posted">Date Posted:</label><br />
		<input type="text" name="date_posted" id="date_posted" value="<?php print $values['date_posted']; ?>" class="text" />
	</p>
	<p><input type="submit" name="submit" value="Save Changes" /></p>
</form>

That form is almost entirely standard html, and is structured in exactly the same way as the add form. One portion that is different is that we're passing additional information to the createFormAction function. Our first argument is false because that just tells the function to use the same page we're at now.

<?php print $this->createFormAction(); ?>

The second argument is an array that sets the id to the current unique id of this blog entry. Any parameters inside of this array will be passed as GET parameters in the URL when the form is submitted.

Next, we need to write some code which will display our values. The first time these are used they will display the values currently in the database. If the form has been submitted but returned for validation errors (not covered here) then they will display the latest values from POST.

<?php print $values['entry_body'] ?>

Create Edit Link

Finally, we need to create a link from the list page that allows you to edit an entry.

Open for Editing: modules/Blog/templates/index.tpl.php

I would like to turn our entry title into the edit link, so I would simply use that entry title value as the text of a link.

Replace:

<?php print $entry['entry_title'] ?>

With:

<?php print $this->createLink($entry['entry_title'], 'edit', array('id' => $entry['entry_id'])) ?>

Just as before, the first argument is the text of the link. The second argument is the page, and the third is an array of named values that will be passed in the url as GET parameters.

Save all of your files, and let's try it.

Try It

Go ahead and try out your form. Click the edit link from your post entry on the list page. Make some edits, and press "Post" and you should see your updated record in the list.

Next: Delete Entry