1: <?php
2: /*
3:
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * or OpenGPL v3 license (GNU Public License V3.0)
9: * that is bundled with this package in the file LICENSE.txt.
10: * It is also available through the world-wide-web at this URL:
11: * http://opensource.org/licenses/osl-3.0.php
12: * or
13: * http://www.gnu.org/licenses/gpl-3.0.txt
14: * If you did not receive a copy of the license and are unable to
15: * obtain it through the world-wide-web, please send an email
16: * to info@e-abi.ee so we can send you a copy immediately.
17: *
18: * DISCLAIMER
19: *
20: * Do not edit or add to this file if you wish to upgrade this module to newer
21: * versions in the future.
22: *
23: * @category Eabi
24: * @package Eabi_Dpd
25: * @copyright Copyright (c) 2014 Aktsiamaailm LLC (http://en.e-abi.ee/)
26: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
27: * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU Public License V3.0
28: * @author Matis Halmann
29: *
30:
31: */
32:
33: /**
34: * <p>Renders the button under the Magento Configuration panel.</p>
35: * <p>Purpose of this button is to invoke the procedure, which rebuilds the list of Postoffices/Parcel terminals
36: * which are directly related to this carrier.</p>
37: * <p>This button is only intended to use in the Magento -> System -> Configuration -> Shipping methods section
38: * and the carrier, that is using this button in the configuration panel should extend Eabi_Postoffice_Model_Carrier_Abstract class.</p>
39: *
40: * <p>In order to use this button in the system.xml configuration use the following example:</p>
41: * <p><pre><code>
42: * <rebuild_all translate="label">
43: <label>Rebuild Postoffice List</label>
44: <frontend_type>label</frontend_type>
45: <sort_order>999</sort_order>
46: <frontend_model>eabi_postoffice/config_rebuildbutton</frontend_model>
47: <show_in_default>1</show_in_default>
48: <show_in_website>1</show_in_website>
49: <show_in_store>1</show_in_store>
50: <comment>If post offices are not displayed correctly, then rebuilding the list may help</comment>
51: </rebuild_all>
52:
53: * </code>
54: * </pre></p>
55: * <p>Most important parts of this example are <b>frontend_model</b> declaration, which refers to this block. Also <b>frontend_type</b> should be label.</p>
56: *
57: * @author matishalmann
58: */
59: class Eabi_Postoffice_Block_Config_Rebuildbutton extends Mage_Adminhtml_Block_System_Config_Form_Field {
60:
61: /**
62: * Pre-render the element HTML.
63: * @param Varien_Data_Form_Element_Abstract $element
64: * @return string
65: * @throws Exception
66: */
67: protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
68: $html = '';
69: $rebuildTextSuccess = Mage::helper('eabi_postoffice')->__('Rebuild successful');
70:
71: //get the carrier code
72: $transformedNames = explode('/', str_replace(array('[', ']'), array('/', ''), $element->getName()));
73: $carrierName = trim($transformedNames[1]);
74: if ($carrierName == '') {
75: throw new Exception('Invalid carrier name');
76: }
77: $url = Mage::helper('adminhtml')->getUrl('eabi_postoffice/adminhtml_postoffice/rebuild', array('carrier_code' => $carrierName));
78:
79: $rebuildText = Mage::helper('eabi_postoffice')->__('Rebuild postoffices for this carrier');
80: $rebuildTextConfirm = addslashes(sprintf(Mage::helper('eabi_postoffice')->__('Rebuilding postoffices for the carrier %s takes a little while... Continue?'), $carrierName));
81:
82: $divId = $element->getId();
83:
84: $html .= <<<EOT
85: <button class="scalable" type="button" onclick="{$divId}_eabi_office_rebuild(); return false;">
86: <span>{$rebuildText}</span>
87: </button>
88: <script type="text/javascript">
89: //<![CDATA[
90: function {$divId}_eabi_office_rebuild() {
91: var confirmR = confirm("{$rebuildTextConfirm}");
92: if (confirmR) {
93: new Ajax.Request('{$url}', {
94: method: 'get',
95: onSuccess: function(transport) {
96: json = transport.responseText.evalJSON(true);
97: if (json) {
98: if (json['error']) {
99: alert(json['error']);
100: } else if (json['success']) {
101: alert('{$rebuildTextSuccess}');
102: }
103: } else {
104: alert('Rebuild failed!');
105: }
106: }
107: });
108: }
109: }
110:
111:
112: //]]>
113: </script>
114:
115: EOT;
116:
117: return $html;
118: }
119: }
120:
121:
122: