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: class Mage_GoogleCheckout_Block_Adminhtml_Shipping_Merchant
28: extends Mage_Adminhtml_Block_System_Config_Form_Field
29: {
30: protected $_addRowButtonHtml = array();
31: protected $_removeRowButtonHtml = array();
32:
33: protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
34: {
35: $this->setElement($element);
36:
37: $html = '<div id="merchant_allowed_methods_template" style="display:none">';
38: $html .= $this->_getRowTemplateHtml();
39: $html .= '</div>';
40:
41: $html .= '<ul id="merchant_allowed_methods_container">';
42: if ($this->_getValue('method')) {
43: foreach ($this->_getValue('method') as $i => $f) {
44: if ($i) {
45: $html .= $this->_getRowTemplateHtml($i);
46: }
47: }
48: }
49: $html .= '</ul>';
50: $html .= $this->_getAddRowButtonHtml('merchant_allowed_methods_container',
51: 'merchant_allowed_methods_template', $this->__('Add Shipping Method'));
52:
53: return $html;
54: }
55:
56: 57: 58: 59: 60: 61:
62: protected function _getRowTemplateHtml($rowIndex = 0)
63: {
64: $html = '<li>';
65: $html .= '<select name="' . $this->getElement()->getName() . '[method][]" ' . $this->_getDisabled() . '>';
66: $html .= '<option value="">' . $this->__('* Select shipping method') . '</option>';
67:
68: foreach ($this->getShippingMethods() as $carrierCode => $carrier) {
69: $html .= '<optgroup label="' . $this->escapeHtml($carrier['title'])
70: . '" style="border-top:solid 1px black; margin-top:3px;">';
71:
72: foreach ($carrier['methods'] as $methodCode => $method) {
73: $code = $carrierCode . '/' . $methodCode;
74: $html .= '<option value="' . $this->escapeHtml($code) . '" '
75: . $this->_getSelected('method/' . $rowIndex, $code)
76: . ' style="background:white;">' . $this->escapeHtml($method['title']) . '</option>';
77: }
78: $html .= '</optgroup>';
79: }
80: $html .= '</select>';
81:
82: $html .= '<div style="margin:5px 0 10px;">';
83: $html .= '<label>' . $this->__('Default price:') . '</label> ';
84: $html .= '<input class="input-text" style="width:70px;" name="'
85: . $this->getElement()->getName() . '[price][]" value="'
86: . $this->_getValue('price/' . $rowIndex) . '" ' . $this->_getDisabled() . '/> ';
87:
88: $html .= $this->_getRemoveRowButtonHtml();
89: $html .= '</div>';
90: $html .= '</li>';
91:
92: return $html;
93: }
94:
95: protected function getShippingMethods()
96: {
97: if (!$this->hasData('shipping_methods')) {
98: $website = $this->getRequest()->getParam('website');
99: $store = $this->getRequest()->getParam('store');
100:
101: $storeId = null;
102: if (!is_null($website)) {
103: $storeId = Mage::getModel('core/website')
104: ->load($website, 'code')
105: ->getDefaultGroup()
106: ->getDefaultStoreId();
107: } elseif (!is_null($store)) {
108: $storeId = Mage::getModel('core/store')
109: ->load($store, 'code')
110: ->getId();
111: }
112:
113: $methods = array();
114: $carriers = Mage::getSingleton('shipping/config')->getActiveCarriers($storeId);
115: foreach ($carriers as $carrierCode=>$carrierModel) {
116: if (!$carrierModel->isActive()) {
117: continue;
118: }
119: $carrierMethods = $carrierModel->getAllowedMethods();
120: if (!$carrierMethods) {
121: continue;
122: }
123: $carrierTitle = Mage::getStoreConfig('carriers/' . $carrierCode . '/title', $storeId);
124: $methods[$carrierCode] = array(
125: 'title' => $carrierTitle,
126: 'methods' => array(),
127: );
128: foreach ($carrierMethods as $methodCode=>$methodTitle) {
129: $methods[$carrierCode]['methods'][$methodCode] = array(
130: 'title' => '[' . $carrierCode . '] ' . $methodTitle,
131: );
132: }
133: }
134: $this->setData('shipping_methods', $methods);
135: }
136: return $this->getData('shipping_methods');
137: }
138:
139: protected function _getDisabled()
140: {
141: return $this->getElement()->getDisabled() ? ' disabled' : '';
142: }
143:
144: protected function _getValue($key)
145: {
146: return $this->getElement()->getData('value/' . $key);
147: }
148:
149: protected function _getSelected($key, $value)
150: {
151: return $this->getElement()->getData('value/' . $key) == $value ? 'selected="selected"' : '';
152: }
153:
154: protected function _getAddRowButtonHtml($container, $template, $title='Add')
155: {
156: if (!isset($this->_addRowButtonHtml[$container])) {
157: $this->_addRowButtonHtml[$container] = $this->getLayout()->createBlock('adminhtml/widget_button')
158: ->setType('button')
159: ->setClass('add ' . $this->_getDisabled())
160: ->setLabel($this->__($title))
161: ->setOnClick("Element.insert($('" . $container . "'), {bottom: $('" . $template . "').innerHTML})")
162: ->setDisabled($this->_getDisabled())
163: ->toHtml();
164: }
165: return $this->_addRowButtonHtml[$container];
166: }
167:
168: protected function _getRemoveRowButtonHtml($selector = 'li', $title = 'Remove')
169: {
170: if (!$this->_removeRowButtonHtml) {
171: $this->_removeRowButtonHtml = $this->getLayout()->createBlock('adminhtml/widget_button')
172: ->setType('button')
173: ->setClass('delete v-middle ' . $this->_getDisabled())
174: ->setLabel($this->__($title))
175: ->setOnClick("Element.remove($(this).up('" . $selector . "'))")
176: ->setDisabled($this->_getDisabled())
177: ->toHtml();
178: }
179: return $this->_removeRowButtonHtml;
180: }
181: }
182: