23.02.2015 Views

www.it-ebooks.info

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[ 111 ]<br />

Chapter 4<br />

Now, if we try to load http://localhost.com/giftregistry/index/index, we<br />

will be redirected to the login page unless we are logged in to the frontend.<br />

Our next step will be to add all the logic to each of the controller actions so the<br />

controller can properly handle creation, update, and deletion.<br />

The index, new, and ed<strong>it</strong> actions are mostly used to load and render the<br />

layout, so there is not much logic involved in controller, newPostAction(),<br />

ed<strong>it</strong>PostAction(), and deleteAction(); on the other hand, they handle a<br />

heavier and more complicated logic.<br />

Let's get started w<strong>it</strong>h newPostAction(). This action is used to handle the data<br />

received from the newAction() form. To do so, follow these steps:<br />

1. Open IndexController.php.<br />

2. The first thing we will add to the action is an if statement to check if the<br />

request is a post-request, which we can retrieve by using the following code:<br />

$this->getRequest()->isPost()<br />

3. In add<strong>it</strong>ion to that, we also want to check that the request has actual data; for<br />

that, we can use the following code:<br />

$this->getRequest()->getParams()<br />

Once we have validated that, the request is a proper request, and while we are<br />

receiving data, we need to actually create gift registry. To do so, we will add a new<br />

function inside our registry model by following these steps:<br />

1. Open the registry ent<strong>it</strong>y model.<br />

2. Create a new function called updateRegistryData() and make sure the<br />

function takes two parameters: $customer and $data.<br />

3. The file location is app/code/local/Mdg/Giftregistry/Model/Ent<strong>it</strong>y.<br />

php. Add the following code inside this function:<br />

public function updateRegistryData(Mage_Customer_Model_Customer<br />

$customer, $data)<br />

{<br />

try{<br />

if(!empty($data))<br />

{<br />

$this->setCustomerId($customer->getId());<br />

$this->setWebs<strong>it</strong>eId($customer->getWebs<strong>it</strong>eId());<br />

$this->setTypeId($data['type_id']);<br />

$this->setEventName($data['event_name']);<br />

$this->setEventDate($data['event_date']);<br />

$this->setEventCountry($data['event_country']);<br />

<strong>www</strong>.<strong>it</strong>-<strong>ebooks</strong>.<strong>info</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!