00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class Mage_GoogleCheckout_Block_Adminhtml_Shipping_Merchant
00028 extends Mage_Adminhtml_Block_System_Config_Form_Field
00029 {
00030 protected $_addRowButtonHtml = array();
00031 protected $_removeRowButtonHtml = array();
00032
00033 protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
00034 {
00035 $this->setElement($element);
00036
00037 $html = '<div id="merchant_allowed_methods_template" style="display:none">';
00038 $html .= $this->_getRowTemplateHtml();
00039 $html .= '</div>';
00040
00041 $html .= '<ul id="merchant_allowed_methods_container">';
00042 if ($this->_getValue('method')) {
00043 foreach ($this->_getValue('method') as $i=>$f) {
00044 if ($i) {
00045 $html .= $this->_getRowTemplateHtml($i);
00046 }
00047 }
00048 }
00049 $html .= '</ul>';
00050 $html .= $this->_getAddRowButtonHtml('merchant_allowed_methods_container',
00051 'merchant_allowed_methods_template', $this->__('Add Shipping Method'));
00052
00053 return $html;
00054 }
00055
00056 protected function _getRowTemplateHtml($i=0)
00057 {
00058 $html = '<li>';
00059 $html .= '<select name="'.$this->getElement()->getName().'[method][]" '.$this->_getDisabled().'>';
00060 $html .= '<option value="">'.$this->__('* Select shipping method').'</option>';
00061 foreach ($this->getShippingMethods() as $carrierCode=>$carrier) {
00062 $html .= '<optgroup label="'.$carrier['title'].'" style="border-top:solid 1px black; margin-top:3px;">';
00063 foreach ($carrier['methods'] as $methodCode=>$method) {
00064 $code = $carrierCode.'/'.$methodCode;
00065 $html .= '<option value="'.$code.'" '.$this->_getSelected('method/'.$i, $code).' style="background:white;">'.$method['title'].'</option>';
00066 }
00067 $html .= '</optgroup>';
00068 }
00069 $html .= '</select>';
00070
00071 $html .= '<div style="margin:5px 0 10px;">';
00072 $html .= '<label>'.$this->__('Default price:').'</label> ';
00073 $html .= '<input class="input-text" style="width:70px;" name="'.$this->getElement()->getName().'[price][]" value="'.$this->_getValue('price/'.$i).'" '.$this->_getDisabled().'/> ';
00074
00075 $html .= $this->_getRemoveRowButtonHtml();
00076 $html .= '</div>';
00077 $html .= '</li>';
00078
00079 return $html;
00080 }
00081
00082 protected function getShippingMethods()
00083 {
00084 if (!$this->hasData('shipping_methods')) {
00085 $website = $this->getRequest()->getParam('website');
00086 $store = $this->getRequest()->getParam('store');
00087
00088 $storeId = null;
00089 if (!is_null($website)) {
00090 $storeId = Mage::getModel('core/website')->load($website, 'code')->getDefaultGroup()->getDefaultStoreId();
00091 } elseif (!is_null($store)) {
00092 $storeId = Mage::getModel('core/store')->load($store, 'code')->getId();
00093 }
00094
00095 $methods = array();
00096 $carriers = Mage::getSingleton('shipping/config')->getActiveCarriers($storeId);
00097 foreach ($carriers as $carrierCode=>$carrierModel) {
00098 if (!$carrierModel->isActive()) {
00099 continue;
00100 }
00101 $carrierMethods = $carrierModel->getAllowedMethods();
00102 if (!$carrierMethods) {
00103 continue;
00104 }
00105 $carrierTitle = Mage::getStoreConfig('carriers/'.$carrierCode.'/title', $storeId);
00106 $methods[$carrierCode] = array(
00107 'title' => $carrierTitle,
00108 'methods' => array(),
00109 );
00110 foreach ($carrierMethods as $methodCode=>$methodTitle) {
00111 $methods[$carrierCode]['methods'][$methodCode] = array(
00112 'title' => '['.$carrierCode.'] '.$methodTitle,
00113 );
00114 }
00115 }
00116 $this->setData('shipping_methods', $methods);
00117 }
00118 return $this->getData('shipping_methods');
00119 }
00120
00121 protected function _getDisabled()
00122 {
00123 return $this->getElement()->getDisabled() ? ' disabled' : '';
00124 }
00125
00126 protected function _getValue($key)
00127 {
00128 return $this->getElement()->getData('value/'.$key);
00129 }
00130
00131 protected function _getSelected($key, $value)
00132 {
00133 return $this->getElement()->getData('value/'.$key)==$value ? 'selected="selected"' : '';
00134 }
00135
00136 protected function _getAddRowButtonHtml($container, $template, $title='Add')
00137 {
00138 if (!isset($this->_addRowButtonHtml[$container])) {
00139 $this->_addRowButtonHtml[$container] = $this->getLayout()->createBlock('adminhtml/widget_button')
00140 ->setType('button')
00141 ->setClass('add '.$this->_getDisabled())
00142 ->setLabel($this->__($title))
00143
00144 ->setOnClick("Element.insert($('".$container."'), {bottom: $('".$template."').innerHTML})")
00145 ->setDisabled($this->_getDisabled())
00146 ->toHtml();
00147 }
00148 return $this->_addRowButtonHtml[$container];
00149 }
00150
00151 protected function _getRemoveRowButtonHtml($selector='li', $title='Remove')
00152 {
00153 if (!$this->_removeRowButtonHtml) {
00154 $this->_removeRowButtonHtml = $this->getLayout()->createBlock('adminhtml/widget_button')
00155 ->setType('button')
00156 ->setClass('delete v-middle '.$this->_getDisabled())
00157 ->setLabel($this->__($title))
00158
00159 ->setOnClick("Element.remove($(this).up('".$selector."'))")
00160 ->setDisabled($this->_getDisabled())
00161 ->toHtml();
00162 }
00163 return $this->_removeRowButtonHtml;
00164 }
00165 }