1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Customer
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Customer address edit block
29: *
30: * @category Mage
31: * @package Mage_Customer
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Customer_Block_Address_Edit extends Mage_Directory_Block_Data
35: {
36: protected $_address;
37: protected $_countryCollection;
38: protected $_regionCollection;
39:
40: protected function _prepareLayout()
41: {
42: parent::_prepareLayout();
43: $this->_address = Mage::getModel('customer/address');
44:
45: // Init address object
46: if ($id = $this->getRequest()->getParam('id')) {
47: $this->_address->load($id);
48: if ($this->_address->getCustomerId() != Mage::getSingleton('customer/session')->getCustomerId()) {
49: $this->_address->setData(array());
50: }
51: }
52:
53: if (!$this->_address->getId()) {
54: $this->_address->setPrefix($this->getCustomer()->getPrefix())
55: ->setFirstname($this->getCustomer()->getFirstname())
56: ->setMiddlename($this->getCustomer()->getMiddlename())
57: ->setLastname($this->getCustomer()->getLastname())
58: ->setSuffix($this->getCustomer()->getSuffix());
59: }
60:
61: if ($headBlock = $this->getLayout()->getBlock('head')) {
62: $headBlock->setTitle($this->getTitle());
63: }
64:
65: if ($postedData = Mage::getSingleton('customer/session')->getAddressFormData(true)) {
66: $this->_address->addData($postedData);
67: }
68: }
69:
70: /**
71: * Generate name block html
72: *
73: * @return string
74: */
75: public function getNameBlockHtml()
76: {
77: $nameBlock = $this->getLayout()
78: ->createBlock('customer/widget_name')
79: ->setObject($this->getAddress());
80:
81: return $nameBlock->toHtml();
82: }
83:
84: public function getTitle()
85: {
86: if ($title = $this->getData('title')) {
87: return $title;
88: }
89: if ($this->getAddress()->getId()) {
90: $title = Mage::helper('customer')->__('Edit Address');
91: }
92: else {
93: $title = Mage::helper('customer')->__('Add New Address');
94: }
95: return $title;
96: }
97:
98: public function getBackUrl()
99: {
100: if ($this->getData('back_url')) {
101: return $this->getData('back_url');
102: }
103:
104: if ($this->getCustomerAddressCount()) {
105: return $this->getUrl('customer/address');
106: } else {
107: return $this->getUrl('customer/account/');
108: }
109: }
110:
111: public function getSaveUrl()
112: {
113: return Mage::getUrl('customer/address/formPost', array('_secure'=>true, 'id'=>$this->getAddress()->getId()));
114: }
115:
116: public function getAddress()
117: {
118: return $this->_address;
119: }
120:
121: public function getCountryId()
122: {
123: if ($countryId = $this->getAddress()->getCountryId()) {
124: return $countryId;
125: }
126: return parent::getCountryId();
127: }
128:
129: public function getRegionId()
130: {
131: return $this->getAddress()->getRegionId();
132: }
133:
134: public function getCustomerAddressCount()
135: {
136: return count(Mage::getSingleton('customer/session')->getCustomer()->getAddresses());
137: }
138:
139: public function canSetAsDefaultBilling()
140: {
141: if (!$this->getAddress()->getId()) {
142: return $this->getCustomerAddressCount();
143: }
144: return !$this->isDefaultBilling();
145: }
146:
147: public function canSetAsDefaultShipping()
148: {
149: if (!$this->getAddress()->getId()) {
150: return $this->getCustomerAddressCount();
151: }
152: return !$this->isDefaultShipping();;
153: }
154:
155: public function isDefaultBilling()
156: {
157: $defaultBilling = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
158: return $this->getAddress()->getId() && $this->getAddress()->getId() == $defaultBilling;
159: }
160:
161: public function isDefaultShipping()
162: {
163: $defaultShipping = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
164: return $this->getAddress()->getId() && $this->getAddress()->getId() == $defaultShipping;
165: }
166:
167: public function getCustomer()
168: {
169: return Mage::getSingleton('customer/session')->getCustomer();
170: }
171:
172: public function getBackButtonUrl()
173: {
174: if ($this->getCustomerAddressCount()) {
175: return $this->getUrl('customer/address');
176: } else {
177: return $this->getUrl('customer/account/');
178: }
179: }
180: }
181: