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_Paypal_Block_Adminhtml_System_Config_Field_Country
35: extends Mage_Adminhtml_Block_System_Config_Form_Field
36: {
37: 38: 39: 40:
41: const REQUEST_PARAM_COUNTRY = 'country';
42: const REQUEST_PARAM_DEFAULT = 'default_country';
43:
44:
45: 46: 47: 48: 49:
50: protected $_defaultCountry;
51:
52: 53: 54: 55: 56: 57:
58: public function render(Varien_Data_Form_Element_Abstract $element)
59: {
60: $country = $this->getRequest()->getParam(self::REQUEST_PARAM_COUNTRY);
61: if ($country) {
62: $element->setValue($country);
63: }
64:
65: if ($element->getCanUseDefaultValue()) {
66: $defaultConfigNode = Mage::getConfig()->getNode(null, 'default');
67: if ($defaultConfigNode) {
68: $this->_defaultCountry = (string)$defaultConfigNode->descend('paypal/general/merchant_country');
69: }
70: if (!$this->_defaultCountry) {
71: $this->_defaultCountry = Mage::helper('core')->getDefaultCountry();
72: }
73: if ($country) {
74: $shouldInherit = $country == $this->_defaultCountry
75: && $this->getRequest()->getParam(self::REQUEST_PARAM_DEFAULT);
76: $element->setInherit($shouldInherit);
77: }
78: if ($element->getInherit()) {
79: $this->_defaultCountry = null;
80: }
81: }
82:
83: return parent::render($element);
84: }
85:
86: 87: 88: 89: 90: 91:
92: protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
93: {
94: $configDataModel = Mage::getSingleton('adminhtml/config_data');
95: $urlParams = array(
96: 'section' => $configDataModel->getSection(),
97: 'website' => $configDataModel->getWebsite(),
98: 'store' => $configDataModel->getStore(),
99: self::REQUEST_PARAM_COUNTRY => '__country__',
100: );
101: $urlString = $this->helper('core')
102: ->jsQuoteEscape(Mage::getModel('adminhtml/url')->getUrl('*/*/*', $urlParams));
103: $jsString = '
104: $("' . $element->getHtmlId() . '").observe("change", function () {
105: location.href = \'' . $urlString . '\'.replace("__country__", this.value);
106: });
107: ';
108:
109: if ($this->_defaultCountry) {
110: $urlParams[self::REQUEST_PARAM_DEFAULT] = '__default__';
111: $urlString = $this->helper('core')
112: ->jsQuoteEscape(Mage::getModel('adminhtml/url')->getUrl('*/*/*', $urlParams));
113: $jsParentCountry = $this->helper('core')->jsQuoteEscape($this->_defaultCountry);
114: $jsString .= '
115: $("' . $element->getHtmlId() . '_inherit").observe("click", function () {
116: if (this.checked) {
117: location.href = \'' . $urlString . '\'.replace("__country__", \'' . $jsParentCountry . '\')
118: .replace("__default__", "1");
119: }
120: });
121: ';
122: }
123:
124: return parent::_getElementHtml($element) . $this->helper('adminhtml/js')
125: ->getScript('document.observe("dom:loaded", function() {' . $jsString . '});');
126: }
127: }
128: