00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Customer_AddressController extends Mage_Core_Controller_Front_Action
00036 {
00037
00038
00039
00040
00041
00042 protected function _getSession()
00043 {
00044 return Mage::getSingleton('customer/session');
00045 }
00046
00047 public function preDispatch()
00048 {
00049 parent::preDispatch();
00050
00051 if (!Mage::getSingleton('customer/session')->authenticate($this)) {
00052 $this->setFlag('', 'no-dispatch', true);
00053 }
00054 }
00055
00056
00057
00058
00059 public function indexAction()
00060 {
00061 if (count($this->_getSession()->getCustomer()->getAddresses())) {
00062 $this->loadLayout();
00063 $this->_initLayoutMessages('customer/session');
00064 $this->_initLayoutMessages('catalog/session');
00065
00066 if ($block = $this->getLayout()->getBlock('address_book')) {
00067 $block->setRefererUrl($this->_getRefererUrl());
00068 }
00069 $this->renderLayout();
00070 }
00071 else {
00072 $this->getResponse()->setRedirect(Mage::getUrl('*/*/new'));
00073 }
00074 }
00075
00076 public function editAction()
00077 {
00078 $this->_forward('form');
00079 }
00080
00081 public function newAction()
00082 {
00083 $this->_forward('form');
00084 }
00085
00086
00087
00088
00089 public function formAction()
00090 {
00091 $this->loadLayout();
00092 $this->_initLayoutMessages('customer/session');
00093 if ($navigationBlock = $this->getLayout()->getBlock('customer_account_navigation')) {
00094 $navigationBlock->setActive('customer/address');
00095 }
00096 $this->renderLayout();
00097 }
00098
00099 public function formPostAction()
00100 {
00101 if (!$this->_validateFormKey()) {
00102 return $this->_redirect('*/*/');
00103 }
00104
00105 if ($this->getRequest()->isPost()) {
00106 $address = Mage::getModel('customer/address')
00107 ->setData($this->getRequest()->getPost())
00108 ->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
00109 ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
00110 ->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
00111 $addressId = $this->getRequest()->getParam('id');
00112 if ($addressId) {
00113 $customerAddress = $this->_getSession()->getCustomer()->getAddressById($addressId);
00114 if ($customerAddress->getId() && $customerAddress->getCustomerId() == $this->_getSession()->getCustomerId()) {
00115 $address->setId($addressId);
00116 }
00117 else {
00118 $address->setId(null);
00119 }
00120 }
00121 else {
00122 $address->setId(null);
00123 }
00124 try {
00125 $accressValidation = $address->validate();
00126 if (true === $accressValidation) {
00127 $address->save();
00128 $this->_getSession()->addSuccess($this->__('The address was successfully saved'));
00129 $this->_redirectSuccess(Mage::getUrl('*/*/index', array('_secure'=>true)));
00130 return;
00131 } else {
00132 $this->_getSession()->setAddressFormData($this->getRequest()->getPost());
00133 if (is_array($accressValidation)) {
00134 foreach ($accressValidation as $errorMessage) {
00135 $this->_getSession()->addError($errorMessage);
00136 }
00137 } else {
00138 $this->_getSession()->addError($this->__('Can\'t save address'));
00139 }
00140 }
00141 }
00142 catch (Mage_Core_Exception $e) {
00143 $this->_getSession()->setAddressFormData($this->getRequest()->getPost())
00144 ->addException($e, $e->getMessage());
00145 }
00146 catch (Exception $e) {
00147 $this->_getSession()->setAddressFormData($this->getRequest()->getPost())
00148 ->addException($e, $this->__('Can\'t save address'));
00149 }
00150 }
00151 $this->_redirectError(Mage::getUrl('*/*/edit', array('id'=>$address->getId())));
00152 }
00153
00154 public function deleteAction()
00155 {
00156 $addressId = $this->getRequest()->getParam('id', false);
00157
00158 if ($addressId) {
00159 $address = Mage::getModel('customer/address')->load($addressId);
00160
00161
00162 if ($address->getCustomerId() != $this->_getSession()->getCustomerId()) {
00163 $this->_getSession()->addError($this->__('The address does not belong to this customer'));
00164 $this->getResponse()->setRedirect(Mage::getUrl('*/*/index'));
00165 return;
00166 }
00167
00168 try {
00169 $address->delete();
00170 $this->_getSession()->addSuccess($this->__('The address was successfully deleted'));
00171 }
00172 catch (Exception $e){
00173 $this->_getSession()->addError($this->__('There was an error while deleting the address'));
00174 }
00175 }
00176 $this->getResponse()->setRedirect(Mage::getUrl('*/*/index'));
00177 }
00178 }