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: if (!defined('_PS_VERSION_')) {
34: exit;
35: }
36: 37: 38: 39: 40:
41: class Eabi_PostofficePostofficeModuleFrontController extends ModuleFrontController {
42: public $ssl = true;
43:
44:
45: 46: 47: 48: 49: 50:
51: public function initContent() {
52: try {
53: if (isset($_POST) && count($_POST)) {
54:
55: $post = $_POST;
56: $url = str_replace('/'.Language::getIsoById(Context::getContext()->language->id).'/', '/', $this->context->link->getModuleLink('eabi_postoffice', 'postoffice', array(), true));
57: $addressId = $post['address_id'];
58: $carrierCode = $post['carrier_code'];
59: $carrierId = $post['carrier_id'];
60: $divId = $post['div_id'];
61: $groupId = isset($post['group_id']) ? ((int) $post['group_id']) : 0;
62: $placeId = isset($post['place_id']) ? ((int) $post['place_id']) : 0;
63: $shippingModel = Module::getInstanceByName($carrierCode);
64: if (!Validate::isLoadedObject($shippingModel)) {
65: throw new Exception(sprintf('Shipping model not found for code %s', $carrierCode));
66: }
67:
68:
69: if (!$shippingModel->active) {
70: throw new Exception('Invalid Shipping method');
71: }
72:
73: $handlerModel = Module::getInstanceByName('eabi_postoffice');
74:
75: if (!Validate::isLoadedObject($handlerModel)) {
76: throw new Exception(sprintf('Handler model not found for code %s', 'eabi_postoffice'));
77: }
78:
79:
80: if (!$handlerModel->idFromCode($carrierCode)) {
81: throw new Exception('Invalid Shipping method');
82: }
83: $handlerModel = clone $handlerModel;
84: $handlerModel->setShippingModel($shippingModel);
85:
86: if ($placeId > 0) {
87: $places = $handlerModel->getPostOffices($carrierCode, null, $placeId, null);
88: $place = isset($places[0])?$places[0]:false;
89: if ($place) {
90: $handlerModel->setOfficeToSession($carrierCode, $addressId, $place['remote_place_id'], $groupId);
91: echo 'true';
92: exit;
93: } else {
94: echo 'false';
95: exit;
96: }
97: }
98: $selectedOffice = $handlerModel->getOfficeFromSession($addressId);
99:
100:
101: $groups = $handlerModel->getPostOfficeGroups($carrierCode, $addressId);
102: $html = '';
103: if ($shippingModel->getConfigData('DIS_FIRST') == 'yes') {
104: $groups = false;
105: }
106: if (isset($shippingModel->text) && $shippingModel->text) {
107: $html .= '<p class="eabi_shipping_method_title">' . htmlspecialchars($shippingModel->text) . '</p>';
108: } else {
109: $title = $shippingModel->getConfigData('TITLE');
110: $finalTitle = $title;
111: if ($this->_isSerialized($title)) {
112: $title = @unserialize($title);
113: if (is_array($title)) {
114: $finalTitle = isset($title[$this->context->language->id]) ? $title[$this->context->language->id] : $title[0];
115: }
116: }
117:
118: $html .= '<p class="eabi_shipping_method_title">' . htmlspecialchars($finalTitle) . '</p>';
119: }
120:
121: if ($groups) {
122: $groupSelectWidth = (int)$shippingModel->getConfigData('GR_WIDTH');
123: $style = '';
124: if ($groupSelectWidth > 0) {
125: $style = ' style="width:'.$groupSelectWidth.'px"';
126: }
127: $html .= '<select onclick="return false;" '.$style.' name="' . $carrierCode . '_select_group" onchange="$.ajax(\'' . $url . '\',{\'type\':\'POST\',data:{carrier_id:\'' . $carrierId . '\',carrier_code:\'' . $carrierCode . '\',div_id:\'' . $divId . '\',address_id:\'' . $addressId . '\',group_id: $(this).val()},success:function(a){$(\'#' . $divId . '\').html(a)}});">';
128: $html .= '<option value="">';
129: $html .= htmlspecialchars($handlerModel->l('-- select --'));
130: $html .= '</option>';
131:
132: foreach ($groups as $group) {
133: $html .= '<option value="' . $group['group_id'] . '"';
134: if ($groupId > 0 && $groupId == $group['group_id']) {
135: $html .= ' selected="selected"';
136: }
137: if ($groupId <= 0 && $selectedOffice['code'] == $carrierCode
138: && $selectedOffice['group_id'] && $group['group_id'] == $selectedOffice['group_id']) {
139: $html .= ' selected="selected"';
140: $groupId = $selectedOffice['group_id'];
141: }
142: $html .= '>';
143: $html .= $shippingModel->getGroupTitle($group);
144: $html .= '</option>';
145: }
146: $html .= '</select>';
147: }
148:
149:
150: if ($groupId > 0 || $groups === false) {
151: $terminals = array();
152: if ($groups !== false) {
153: $terminals = $handlerModel->getPostOffices($carrierCode, $groupId, null, $addressId);
154: } else {
155: $terminals = $handlerModel->getPostOffices($carrierCode, null, null, $addressId);
156: }
157: $officeSelectWidth = (int)$shippingModel->getConfigData('OF_WIDTH');
158: $style = '';
159: if ($officeSelectWidth > 0) {
160: $style = ' style="width:'.$officeSelectWidth.'px"';
161: }
162: $carrierIde = 'id_carrier_'.$carrierCode;
163: $prestaShopCarrierCode = $handlerModel->getCarrierFromCode($carrierCode);
164: $terminalsCount = count($terminals);
165:
166:
167:
168: if ($terminalsCount === 1 && $groups === false) {
169: $terminal = array_shift($terminals);
170: $html .= '<span>';
171:
172: $html .= '</span>';
173:
174: $html .= <<<HTML
175: <script type="text/javascript">
176: // <![CDATA[
177: jQuery('input[id=\'{$carrierIde}\']').val('{$prestaShopCarrierCode->id},');
178: jQuery('input[id={$carrierIde}]').removeAttr('disabled').removeAttr('title');
179: // ]]>
180: </script>
181: HTML;
182: } else {
183: $html .= '<select onclick="return false;" ' . $style . ' name="' . $carrierCode . '_select_office" onchange="var sel = $(this); $.ajax(\'' . $url . '\',{\'type\':\'POST\',data:{carrier_id:\'' . $carrierId . '\',carrier_code:\'' . $carrierCode . '\',div_id:\'' . $divId . '\',address_id:\'' . $addressId . '\',place_id: sel.val()},success:function(a){ if (a == \'true\') { $(\'input[id=\\\'' . $carrierIde . '\\\']\').val(\'' . $prestaShopCarrierCode->id . ',\'); $(\'input[id=' . $carrierIde . ']\').attr(\'checked\',\'checked\').removeAttr(\'disabled\').change(); }}});">';
184: $html .= '<option value="">';
185: $html .= htmlspecialchars($handlerModel->l('-- select --'));
186: $html .= '</option>';
187:
188: $optionsHtml = '';
189: $previousGroup = false;
190: $optGroupHtml = '';
191: $groupCount = 0;
192:
193: foreach ($terminals as $terminal) {
194: if ($shippingModel->getGroupTitle($terminal) != $previousGroup && $shippingModel->getConfigData('DIS_GR_TITLE') != 'yes') {
195: if ($previousGroup != false) {
196: $optionsHtml .= '</optgroup>';
197: $optionsHtml .= '<optgroup label="' . $shippingModel->getGroupTitle($terminal) . '">';
198: } else {
199: $optGroupHtml .= '<optgroup label="' . $shippingModel->getGroupTitle($terminal) . '">';
200: }
201: $groupCount++;
202: }
203: $optionsHtml .= '<option value="' . $terminal['remote_place_id'] . '"';
204: if ($selectedOffice['code'] == $carrierCode && $selectedOffice['place_id'] == $terminal['remote_place_id']) {
205: $optionsHtml .= ' selected="selected"';
206: }
207: $optionsHtml .= '>';
208: $optionsHtml .= $shippingModel->getTerminalTitle($terminal);
209: $optionsHtml .= '</option>';
210:
211: $previousGroup = $shippingModel->getGroupTitle($terminal);
212: }
213: if ($groupCount > 1) {
214: $html .= $optGroupHtml . $optionsHtml . '</optgroup>';
215: } else {
216: $html .= $optionsHtml;
217: }
218:
219: $html .= '</select>';
220: }
221: }
222:
223:
224: echo $html;
225: } else {
226: throw new Exception('Invalid request method');
227: }
228: } catch (Exception $e) {
229: header('HTTP/1.1 500 Internal error');
230: header('Status: 500 Internal error');
231:
232: throw $e;
233: }
234: exit;
235: }
236:
237: protected function _isSerialized($input) {
238: return preg_match('/^([adObis]):/', $input);
239: }
240:
241:
242:
243: }
244:
245: