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: class Eabi_Postoffice_IndexController extends Mage_Core_Controller_Front_Action {
39:
40:
41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51:
52: public function officeAction() {
53: try {
54: if ($this->getRequest()->isPost()) {
55: $this->getResponse()->setHeader('Content-type', 'application/json');
56: $jsonResponse = array(
57: 'html' => false,
58: 'comment' => false,
59: );
60:
61: $url = Mage::getUrl('eabi_postoffice/index/office', array('_secure' => true));
62:
63: $post = $this->getRequest()->getPost();
64: $addressId = $post['address_id'];
65: $carrierCode = $post['carrier_code'];
66: $carrierId = $post['carrier_id'];
67: $divId = $post['div_id'];
68: $groupId = isset($post['group_id']) ? ((int) $post['group_id']) : 0;
69: $placeId = isset($post['place_id']) ? ((int) $post['place_id']) : 0;
70: $shippingModel = Mage::getModel('shipping/shipping')->getCarrierByCode($carrierCode);
71: if (!$shippingModel->isAjaxInsertAllowed($addressId)) {
72: throw new Exception('Invalid Shipping method');
73: }
74: if (!($shippingModel instanceof Eabi_Postoffice_Model_Carrier_Abstract)) {
75: throw new Exception('Invalid Shipping model');
76: }
77:
78: if ($placeId > 0) {
79: $place = $shippingModel->getTerminal($placeId);
80: if ($place) {
81: $shippingModel->setOfficeToSession($addressId, $place);
82: $jsonResponse['html'] = true;
83: $terminalComment = $shippingModel->getTerminalComment($place);
84: $isError = $terminalComment->getIsError();
85: if ($isError) {
86: $jsonResponse['html'] = false;
87: }
88:
89: $jsonResponse['comment'] = $terminalComment->getComment();
90:
91: $this->getResponse()->setBody(json_encode($jsonResponse));
92: return;
93: } else {
94: $jsonResponse['html'] = false;
95: $this->getResponse()->setBody(json_encode($jsonResponse));
96: return;
97: }
98: }
99:
100: $groups = $shippingModel->getGroups($addressId);
101: $html = '';
102:
103: if ($groups) {
104: $groupSelectWidth = (int)$shippingModel->getConfigData('group_width');
105: $style = '';
106: if ($groupSelectWidth > 0) {
107: $style = ' style="width:'.$groupSelectWidth.'px"';
108: }
109: $html .= '<select onclick="return false;" '.$style.' name="' . $carrierCode . '_select_group" onchange="new Ajax.Request(\'' . $url . '\',{method:\'post\',parameters:{carrier_id:\'' . $carrierId . '\',carrier_code:\'' . $carrierCode . '\',div_id:\'' . $divId . '\',address_id:\'' . $addressId . '\',group_id: $(this).getValue()},onSuccess:function(a){$(\'' . $divId . '\').update(a.responseJSON.html)}});">';
110: $html .= '<option value="">';
111: $html .= htmlspecialchars(Mage::helper('eabi_postoffice')->__('-- select --'));
112: $html .= '</option>';
113:
114:
115: foreach ($groups as $group) {
116: $html .= '<option value="' . $group->getGroupId() . '"';
117: if ($groupId > 0 && $groupId == $group->getGroupId()) {
118: $html .= ' selected="selected"';
119: }
120: $html .= '>';
121: $html .= $shippingModel->getGroupTitle($group);
122: $html .= '</option>';
123: }
124: $html .= '</select>';
125: }
126:
127:
128: if ($groupId > 0 || $groups === false) {
129: $terminals = array();
130: if ($groups !== false) {
131: $terminals = $shippingModel->getTerminals($groupId, $addressId);
132: } else {
133: $terminals = $shippingModel->getTerminals(null, $addressId);
134: }
135: $officeSelectWidth = (int)$shippingModel->getConfigData('office_width');
136: $style = '';
137: if ($officeSelectWidth > 0) {
138: $style = ' style="width:'.$officeSelectWidth.'px"';
139: }
140:
141: $html .= '<select onclick="return false;" '.$style.' name="' . $carrierCode . '_select_office" onchange="var sel = $(this); new Ajax.Request(\'' . $url . '\',{method:\'post\',parameters:{carrier_id:\'' . $carrierId . '\',carrier_code:\'' . $carrierCode . '\',div_id:\'' . $divId . '\',address_id:\'' . $addressId . '\',place_id: sel.getValue()},onSuccess:function(a){ if (a.responseJSON.html === true) { if (typeof a.responseJSON.comment === \'string\') { sel.next().update(a.responseJSON.comment); sel.next().removeClassName(\'eabi_postoffice_comment_error\'); sel.next().show(); $(\'' . $divId. '\').addClassName(\'over\'); setTimeout(function() {$(\'' . $divId. '\').removeClassName(\'over\');}, 1500);} else { sel.next().hide(); } $(\'' . $carrierId . '\').writeAttribute(\'value\', \'' . $carrierCode . '_' . $carrierCode . '_\' + sel.getValue()); $(\'' . $carrierId . '\').click(); } else if (a.responseJSON.html === false) { if (typeof a.responseJSON.comment === \'string\') { sel.next().update(a.responseJSON.comment); sel.next().addClassName(\'eabi_postoffice_comment_error\'); sel.next().show(); $(\'' . $divId. '\').addClassName(\'over\'); setTimeout(function() {$(\'' . $divId. '\').removeClassName(\'over\');}, 1500); } else { sel.next().hide(); } } else { sel.next().hide(); }}});">';
142: $html .= '<option value="">';
143: $html .= htmlspecialchars(Mage::helper('eabi_postoffice')->__('-- select --'));
144: $html .= '</option>';
145:
146: $optionsHtml = '';
147: $previousGroup = false;
148: $optGroupHtml = '';
149: $groupCount = 0;
150:
151: foreach ($terminals as $terminal) {
152: if ($shippingModel->getGroupTitle($terminal) != $previousGroup && !$shippingModel->getConfigData('disable_group_titles')) {
153: if ($previousGroup != false) {
154: $optionsHtml .= '</optgroup>';
155: $optionsHtml .= '<optgroup label="'.$shippingModel->getGroupTitle($terminal).'">';
156: } else {
157: $optGroupHtml .= '<optgroup label="'.$shippingModel->getGroupTitle($terminal).'">';
158: }
159: $groupCount++;
160: }
161: $optionsHtml .= '<option value="' . $terminal->getRemotePlaceId() . '"';
162: if (false) {
163: $optionsHtml .= ' selected="selected"';
164: }
165: $optionsHtml .= '>';
166: $optionsHtml .= $shippingModel->getTerminalTitle($terminal);
167: $optionsHtml .= '</option>';
168:
169: $previousGroup = $shippingModel->getGroupTitle($terminal);
170: }
171: if ($groupCount > 1) {
172: $html .= $optGroupHtml . $optionsHtml . '</optgroup>';
173: } else {
174: $html .= $optionsHtml;
175: }
176:
177: $html .= '</select>';
178: }
179: $html .= '<span class="eabi_postoffice_comment" style="display:none;"></span>';
180:
181:
182: $jsonResponse['html'] = $html;
183: $this->getResponse()->setBody(json_encode($jsonResponse));
184: } else {
185: throw new Exception('Invalid request method');
186: }
187: } catch (Exception $e) {
188: $this->getResponse()->setHeader('HTTP/1.1', '500 Internal error');
189: $this->getResponse()->setHeader('Status', '500 Internal error');
190: throw $e;
191: }
192: return;
193: }
194:
195: }
196:
197: