1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Customer_AddressController extends Mage_Core_Controller_Front_Action
36: {
37: 38: 39: 40: 41:
42: protected function _getSession()
43: {
44: return Mage::getSingleton('customer/session');
45: }
46:
47: public function preDispatch()
48: {
49: parent::preDispatch();
50:
51: if (!Mage::getSingleton('customer/session')->authenticate($this)) {
52: $this->setFlag('', 'no-dispatch', true);
53: }
54: }
55:
56: 57: 58:
59: public function indexAction()
60: {
61: if (count($this->_getSession()->getCustomer()->getAddresses())) {
62: $this->loadLayout();
63: $this->_initLayoutMessages('customer/session');
64: $this->_initLayoutMessages('catalog/session');
65:
66: $block = $this->getLayout()->getBlock('address_book');
67: if ($block) {
68: $block->setRefererUrl($this->_getRefererUrl());
69: }
70: $this->renderLayout();
71: } else {
72: $this->getResponse()->setRedirect(Mage::getUrl('*/*/new'));
73: }
74: }
75:
76: public function editAction()
77: {
78: $this->_forward('form');
79: }
80:
81: public function newAction()
82: {
83: $this->_forward('form');
84: }
85:
86: 87: 88:
89: public function formAction()
90: {
91: $this->loadLayout();
92: $this->_initLayoutMessages('customer/session');
93: $navigationBlock = $this->getLayout()->getBlock('customer_account_navigation');
94: if ($navigationBlock) {
95: $navigationBlock->setActive('customer/address');
96: }
97: $this->renderLayout();
98: }
99:
100: public function formPostAction()
101: {
102: if (!$this->_validateFormKey()) {
103: return $this->_redirect('*/*/');
104: }
105:
106: if ($this->getRequest()->isPost()) {
107: $customer = $this->_getSession()->getCustomer();
108:
109: $address = Mage::getModel('customer/address');
110: $addressId = $this->getRequest()->getParam('id');
111: if ($addressId) {
112: $existsAddress = $customer->getAddressById($addressId);
113: if ($existsAddress->getId() && $existsAddress->getCustomerId() == $customer->getId()) {
114: $address->setId($existsAddress->getId());
115: }
116: }
117:
118: $errors = array();
119:
120:
121: $addressForm = Mage::getModel('customer/form');
122: $addressForm->setFormCode('customer_address_edit')
123: ->setEntity($address);
124: $addressData = $addressForm->extractData($this->getRequest());
125: $addressErrors = $addressForm->validateData($addressData);
126: if ($addressErrors !== true) {
127: $errors = $addressErrors;
128: }
129:
130: try {
131: $addressForm->compactData($addressData);
132: $address->setCustomerId($customer->getId())
133: ->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
134: ->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false));
135:
136: $addressErrors = $address->validate();
137: if ($addressErrors !== true) {
138: $errors = array_merge($errors, $addressErrors);
139: }
140:
141: if (count($errors) === 0) {
142: $address->save();
143: $this->_getSession()->addSuccess($this->__('The address has been saved.'));
144: $this->_redirectSuccess(Mage::getUrl('*/*/index', array('_secure'=>true)));
145: return;
146: } else {
147: $this->_getSession()->setAddressFormData($this->getRequest()->getPost());
148: foreach ($errors as $errorMessage) {
149: $this->_getSession()->addError($errorMessage);
150: }
151: }
152: } catch (Mage_Core_Exception $e) {
153: $this->_getSession()->setAddressFormData($this->getRequest()->getPost())
154: ->addException($e, $e->getMessage());
155: } catch (Exception $e) {
156: $this->_getSession()->setAddressFormData($this->getRequest()->getPost())
157: ->addException($e, $this->__('Cannot save address.'));
158: }
159: }
160:
161: return $this->_redirectError(Mage::getUrl('*/*/edit', array('id' => $address->getId())));
162: }
163:
164: public function deleteAction()
165: {
166: $addressId = $this->getRequest()->getParam('id', false);
167:
168: if ($addressId) {
169: $address = Mage::getModel('customer/address')->load($addressId);
170:
171:
172: if ($address->getCustomerId() != $this->_getSession()->getCustomerId()) {
173: $this->_getSession()->addError($this->__('The address does not belong to this customer.'));
174: $this->getResponse()->setRedirect(Mage::getUrl('*/*/index'));
175: return;
176: }
177:
178: try {
179: $address->delete();
180: $this->_getSession()->addSuccess($this->__('The address has been deleted.'));
181: } catch (Exception $e){
182: $this->_getSession()->addException($e, $this->__('An error occurred while deleting the address.'));
183: }
184: }
185: $this->getResponse()->setRedirect(Mage::getUrl('*/*/index'));
186: }
187: }
188: