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: class Mage_Adminhtml_Block_Customer_Edit_Tab_Addresses extends Mage_Adminhtml_Block_Widget_Form
35: {
36: public function __construct()
37: {
38: parent::__construct();
39: $this->setTemplate('customer/tab/addresses.phtml');
40: }
41:
42: public function getRegionsUrl()
43: {
44: return $this->getUrl('*/json/countryRegion');
45: }
46:
47: protected function _prepareLayout()
48: {
49: $this->setChild('delete_button',
50: $this->getLayout()->createBlock('adminhtml/widget_button')
51: ->setData(array(
52: 'label' => Mage::helper('customer')->__('Delete Address'),
53: 'name' => 'delete_address',
54: 'element_name' => 'delete_address',
55: 'disabled' => $this->isReadonly(),
56: 'class' => 'delete' . ($this->isReadonly() ? ' disabled' : '')
57: ))
58: );
59: $this->setChild('add_address_button',
60: $this->getLayout()->createBlock('adminhtml/widget_button')
61: ->setData(array(
62: 'label' => Mage::helper('customer')->__('Add New Address'),
63: 'id' => 'add_address_button',
64: 'name' => 'add_address_button',
65: 'element_name' => 'add_address_button',
66: 'disabled' => $this->isReadonly(),
67: 'class' => 'add' . ($this->isReadonly() ? ' disabled' : ''),
68: 'onclick'=> 'customerAddresses.addNewAddress()'
69: ))
70: );
71: $this->setChild('cancel_button',
72: $this->getLayout()->createBlock('adminhtml/widget_button')
73: ->setData(array(
74: 'label' => Mage::helper('customer')->__('Cancel'),
75: 'id' => 'cancel_add_address'.$this->getTemplatePrefix(),
76: 'name' => 'cancel_address',
77: 'element_name' => 'cancel_address',
78: 'class' => 'cancel delete-address' . ($this->isReadonly() ? ' disabled' : ''),
79: 'disabled' => $this->isReadonly(),
80: 'onclick'=> 'customerAddresses.cancelAdd(this)',
81: ))
82: );
83: return parent::_prepareLayout();
84: }
85:
86: 87: 88: 89: 90:
91: public function isReadonly()
92: {
93: $customer = Mage::registry('current_customer');
94: return $customer->isReadonly();
95: }
96:
97: public function getDeleteButtonHtml()
98: {
99: return $this->getChildHtml('delete_button');
100: }
101:
102: 103: 104: 105: 106:
107: public function initForm()
108: {
109:
110: $customer = Mage::registry('current_customer');
111:
112: $form = new Varien_Data_Form();
113: $fieldset = $form->addFieldset('address_fieldset', array(
114: 'legend' => Mage::helper('customer')->__("Edit Customer's Address"))
115: );
116:
117: $addressModel = Mage::getModel('customer/address');
118: $addressModel->setCountryId(Mage::helper('core')->getDefaultCountry($customer->getStore()));
119:
120: $addressForm = Mage::getModel('customer/form');
121: $addressForm->setFormCode('adminhtml_customer_address')
122: ->setEntity($addressModel)
123: ->initDefaultValues();
124:
125: $attributes = $addressForm->getAttributes();
126: if(isset($attributes['street'])) {
127: Mage::helper('adminhtml/addresses')
128: ->processStreetAttribute($attributes['street']);
129: }
130: foreach ($attributes as $attribute) {
131:
132: $attribute->setFrontendLabel(Mage::helper('customer')->__($attribute->getFrontend()->getLabel()));
133: $attribute->unsIsVisible();
134: }
135: $this->_setFieldset($attributes, $fieldset);
136:
137: $regionElement = $form->getElement('region');
138: $regionElement->setRequired(true);
139: if ($regionElement) {
140: $regionElement->setRenderer(Mage::getModel('adminhtml/customer_renderer_region'));
141: }
142:
143: $regionElement = $form->getElement('region_id');
144: if ($regionElement) {
145: $regionElement->setNoDisplay(true);
146: }
147:
148: $country = $form->getElement('country_id');
149: if ($country) {
150: $country->addClass('countries');
151: }
152:
153: if ($this->isReadonly()) {
154: foreach ($addressModel->getAttributes() as $attribute) {
155: $element = $form->getElement($attribute->getAttributeCode());
156: if ($element) {
157: $element->setReadonly(true, true);
158: }
159: }
160: }
161:
162: $customerStoreId = null;
163: if ($customer->getId()) {
164: $customerStoreId = Mage::app()->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
165: }
166:
167: $prefixElement = $form->getElement('prefix');
168: if ($prefixElement) {
169: $prefixOptions = $this->helper('customer')->getNamePrefixOptions($customerStoreId);
170: if (!empty($prefixOptions)) {
171: $fieldset->removeField($prefixElement->getId());
172: $prefixField = $fieldset->addField($prefixElement->getId(),
173: 'select',
174: $prefixElement->getData(),
175: '^'
176: );
177: $prefixField->setValues($prefixOptions);
178: }
179: }
180:
181: $suffixElement = $form->getElement('suffix');
182: if ($suffixElement) {
183: $suffixOptions = $this->helper('customer')->getNameSuffixOptions($customerStoreId);
184: if (!empty($suffixOptions)) {
185: $fieldset->removeField($suffixElement->getId());
186: $suffixField = $fieldset->addField($suffixElement->getId(),
187: 'select',
188: $suffixElement->getData(),
189: $form->getElement('lastname')->getId()
190: );
191: $suffixField->setValues($suffixOptions);
192: }
193: }
194:
195: $addressCollection = $customer->getAddresses();
196: $this->assign('customer', $customer);
197: $this->assign('addressCollection', $addressCollection);
198: $form->setValues($addressModel->getData());
199: $this->setForm($form);
200:
201: return $this;
202: }
203:
204: public function getCancelButtonHtml()
205: {
206: return $this->getChildHtml('cancel_button');
207: }
208:
209: public function getAddNewButtonHtml()
210: {
211: return $this->getChildHtml('add_address_button');
212: }
213:
214: public function getTemplatePrefix()
215: {
216: return '_template_';
217: }
218:
219: 220: 221: 222: 223:
224: protected function _getAdditionalElementTypes()
225: {
226: return array(
227: 'file' => Mage::getConfig()->getBlockClassName('adminhtml/customer_form_element_file'),
228: 'image' => Mage::getConfig()->getBlockClassName('adminhtml/customer_form_element_image'),
229: 'boolean' => Mage::getConfig()->getBlockClassName('adminhtml/customer_form_element_boolean'),
230: );
231: }
232:
233: 234: 235: 236: 237:
238: public function getDefaultCountriesJson() {
239: $websites = Mage::getSingleton('adminhtml/system_store')->getWebsiteValuesForForm(false, true);
240: $result = array();
241: foreach ($websites as $website) {
242: $result[$website['value']] = Mage::app()->getWebsite($website['value'])->getConfig(
243: Mage_Core_Helper_Data::XML_PATH_DEFAULT_COUNTRY
244: );
245: }
246:
247: return Mage::helper('core')->jsonEncode($result);
248: }
249:
250: 251: 252: 253: 254: 255:
256: public function addValuesToNamePrefixElement($values)
257: {
258: if ($this->getForm() && $this->getForm()->getElement('prefix')) {
259: $this->getForm()->getElement('prefix')->addElementValues($values);
260: }
261: return $this;
262: }
263:
264: 265: 266: 267: 268: 269:
270: public function addValuesToNameSuffixElement($values)
271: {
272: if ($this->getForm() && $this->getForm()->getElement('suffix')) {
273: $this->getForm()->getElement('suffix')->addElementValues($values);
274: }
275: return $this;
276: }
277: }
278: