23.02.2015 Views

www.it-ebooks.info

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4<br />

Let's continue by creating ed<strong>it</strong>PostAction; this action is very similar to the<br />

newPostAction. The main difference is that in the case of ed<strong>it</strong>PostAction,<br />

we are working w<strong>it</strong>h an already existing registry record, so we will need to<br />

add some validation before setting the data.<br />

The file location is app/code/local/Mdg/Giftregistry/controllers/<br />

IndexController.php. Let's take a closer look at the following action code:<br />

public function ed<strong>it</strong>PostAction()<br />

{<br />

try {<br />

$data = $this->getRequest()->getParams();<br />

$registry = Mage::getModel('mdg_giftregistry/ent<strong>it</strong>y');<br />

$customer = Mage::getSingleton('customer/session')-<br />

>getCustomer();<br />

if($this->getRequest()->getPosts() && !empty($data) )<br />

{<br />

$registry->load($data['registry_id']);<br />

if($registry){<br />

$registry->updateRegistryData($customer, $data);<br />

$registry->save();<br />

$successMessage = Mage::helper('mdg_<br />

giftregistry')->__('Registry Successfully Saved');<br />

Mage::getSingleton('core/session')-<br />

>addSuccess($successMessage);<br />

}else {<br />

throw new Exception("Invalid Registry Specified");<br />

}<br />

}else {<br />

throw new Exception("Insufficient Data provided");<br />

}<br />

} catch (Mage_Core_Exception $e) {<br />

Mage::getSingleton('core/session')->addError($e-<br />

>getMessage());<br />

$this->_redirect('*/*/');<br />

}<br />

$this->_redirect('*/*/');<br />

}<br />

As we can see, this code is pretty much the same as our newPostAction() controller<br />

w<strong>it</strong>h the cr<strong>it</strong>ical distinction that <strong>it</strong> tries to load an existing registry before updating<br />

the data.<br />

[ 113 ]<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!