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: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: class Eabi_DpdEE_Block_Adminhtml_Config_Form_Field_Country extends Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract {
48:
49: public function __construct() {
50:
51: $this->addColumn('country_id', array(
52: 'label' => $this->_getDpdHelper()->__('Country'),
53: 'style' => 'width:120px',
54: 'type' => 'select',
55: 'source_model' => 'adminhtml/system_config_source_country',
56:
57: ));
58: $this->addColumn('base_price', array(
59: 'label' => $this->_getDpdHelper()->__('Base shipping price'),
60: 'style' => 'width:120px',
61: 'class' => 'validate-number',
62: ));
63: $this->addColumn('kg_price', array(
64: 'label' => $this->_getDpdHelper()->__('Price per additional 10kg over base 10kg'),
65: 'style' => 'width:120px',
66: 'class' => 'validate-number',
67: ));
68: $this->addColumn('free_shipping_from', array(
69: 'label' => $this->_getDpdHelper()->__('Free shipping from price'),
70: 'style' => 'width:120px',
71: ));
72: $this->addColumn('cod_fee', array(
73: 'label' => $this->_getDpdHelper()->__('Cash on delivery fee'),
74: 'style' => 'width:120px',
75: ));
76: $this->_addAfter = false;
77: $this->_addButtonLabel = $this->_getDpdHelper()->__('Add shipping country');
78: parent::__construct();
79:
80: }
81:
82:
83:
84: 85: 86: 87: 88: 89:
90: public function addColumn($name, $params) {
91: $this->_columns[$name] = array(
92: 'label' => empty($params['label']) ? 'Column' : $params['label'],
93: 'size' => empty($params['size']) ? false : $params['size'],
94: 'style' => empty($params['style']) ? null : $params['style'],
95: 'class' => empty($params['class']) ? null : $params['class'],
96: 'type' => empty($params['type']) ? null : $params['type'],
97: 'source_model' => empty($params['source_model']) ? null : $params['source_model'],
98: 'renderer' => false,
99: );
100: if ((!empty($params['renderer'])) && ($params['renderer'] instanceof Mage_Core_Block_Abstract)) {
101: $this->_columns[$name]['renderer'] = $params['renderer'];
102: }
103: }
104:
105:
106:
107: 108: 109: 110: 111: 112: 113:
114: protected function _renderCellTemplate($columnName) {
115: if (empty($this->_columns[$columnName])) {
116: throw new Exception('Wrong column name specified.');
117: }
118: $column = $this->_columns[$columnName];
119: $inputName = $this->getElement()->getName() . '[#{_id}][' . $columnName . ']';
120:
121: if ($column['renderer']) {
122: return $column['renderer']->setInputName($inputName)->setColumnName($columnName)->setColumn($column)
123: ->toHtml();
124: }
125:
126: if (isset($column['type']) && $column['type']) {
127: if ($column['type'] == 'select') {
128: $html = '<select name="' . $inputName . '" value="#{' . $columnName . '}" ' .
129: ($column['size'] ? 'size="' . $column['size'] . '"' : '') . ' class="' .
130: (isset($column['class']) ? $column['class'] : 'input-text') . '"' .
131: (isset($column['style']) ? ' style="' . $column['style'] . '"' : '') . '/>';
132:
133: $options = Mage::getModel($column['source_model'])->toOptionArray();
134: foreach ($options as $option) {
135: $html .= '<option value="' . htmlspecialchars($option['value']);
136: $html .= '" ';
137: $html .= '>';
138: $html .= htmlspecialchars($option['label'], ENT_QUOTES, 'UTF-8');
139: $html .= '</option>';
140: }
141: $html .= '</select>';
142:
143:
144: return $html;
145: }
146: }
147:
148: return '<input type="text" name="' . $inputName . '" value="#{' . $columnName . '}" ' .
149: ($column['size'] ? 'size="' . $column['size'] . '"' : '') . ' class="' .
150: (isset($column['class']) ? $column['class'] : 'input-text') . '"'.
151: (isset($column['style']) ? ' style="'.$column['style'] . '"' : '') . '/>';
152: }
153:
154:
155:
156: 157: 158: 159: 160: 161:
162: protected function _toHtml() {
163: $this->setHtmlId('_'. uniqid());
164: if (!$this->_isPreparedToRender) {
165: $this->_prepareToRender();
166: $this->_isPreparedToRender = true;
167: }
168: if (empty($this->_columns)) {
169: throw new Exception('At least one column must be defined.');
170: }
171: return parent::_toHtml().<<<JS
172: <script type="text/javascript">
173: // <![CDATA[
174: document.observe('dom:loaded', function() {
175: $$('#grid{$this->getHtmlId()} select').each(function(iterator) {
176: iterator.setValue(iterator.readAttribute('value'));
177: });
178: });
179: // ]]>
180: </script>
181: JS;
182: }
183:
184:
185:
186:
187: 188: 189: 190:
191: protected function _getDpdHelper() {
192: return Mage::helper('eabi_dpdee');
193: }
194:
195:
196: }
197: