1: <?php
2: namespace Opencart\Admin\Controller\Localisation;
3: /**
4: * Class Zone
5: *
6: * @package Opencart\Admin\Controller\Localisation
7: */
8: class Zone extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('localisation/zone');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: if (isset($this->request->get['filter_name'])) {
20: $filter_name = (string)$this->request->get['filter_name'];
21: } else {
22: $filter_name = '';
23: }
24:
25: if (isset($this->request->get['filter_country'])) {
26: $filter_country = (string)$this->request->get['filter_country'];
27: } else {
28: $filter_country = '';
29: }
30:
31: if (isset($this->request->get['filter_code'])) {
32: $filter_code = (string)$this->request->get['filter_code'];
33: } else {
34: $filter_code = '';
35: }
36:
37: $url = '';
38:
39: if (isset($this->request->get['sort'])) {
40: $url .= '&sort=' . $this->request->get['sort'];
41: }
42:
43: if (isset($this->request->get['order'])) {
44: $url .= '&order=' . $this->request->get['order'];
45: }
46:
47: if (isset($this->request->get['page'])) {
48: $url .= '&page=' . $this->request->get['page'];
49: }
50:
51: $data['breadcrumbs'] = [];
52:
53: $data['breadcrumbs'][] = [
54: 'text' => $this->language->get('text_home'),
55: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
56: ];
57:
58: $data['breadcrumbs'][] = [
59: 'text' => $this->language->get('heading_title'),
60: 'href' => $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . $url)
61: ];
62:
63: $data['add'] = $this->url->link('localisation/zone.form', 'user_token=' . $this->session->data['user_token'] . $url);
64: $data['delete'] = $this->url->link('localisation/zone.delete', 'user_token=' . $this->session->data['user_token']);
65:
66: $data['list'] = $this->getList();
67:
68: $data['filter_name'] = $filter_name;
69: $data['filter_country'] = $filter_country;
70: $data['filter_code'] = $filter_code;
71:
72: $data['user_token'] = $this->session->data['user_token'];
73:
74: $data['header'] = $this->load->controller('common/header');
75: $data['column_left'] = $this->load->controller('common/column_left');
76: $data['footer'] = $this->load->controller('common/footer');
77:
78: $this->response->setOutput($this->load->view('localisation/zone', $data));
79: }
80:
81: /**
82: * List
83: *
84: * @return void
85: */
86: public function list(): void {
87: $this->load->language('localisation/zone');
88:
89: $this->response->setOutput($this->getList());
90: }
91:
92: /**
93: * Get List
94: *
95: * @return string
96: */
97: protected function getList(): string {
98: if (isset($this->request->get['filter_name'])) {
99: $filter_name = (string)$this->request->get['filter_name'];
100: } else {
101: $filter_name = '';
102: }
103:
104: if (isset($this->request->get['filter_country'])) {
105: $filter_country = (string)$this->request->get['filter_country'];
106: } else {
107: $filter_country = '';
108: }
109:
110: if (isset($this->request->get['filter_code'])) {
111: $filter_code = (string)$this->request->get['filter_code'];
112: } else {
113: $filter_code = '';
114: }
115:
116: if (isset($this->request->get['sort'])) {
117: $sort = (string)$this->request->get['sort'];
118: } else {
119: $sort = 'c.name';
120: }
121:
122: if (isset($this->request->get['order'])) {
123: $order = (string)$this->request->get['order'];
124: } else {
125: $order = 'ASC';
126: }
127:
128: if (isset($this->request->get['page'])) {
129: $page = (int)$this->request->get['page'];
130: } else {
131: $page = 1;
132: }
133:
134: $url = '';
135:
136: if (isset($this->request->get['filter_name'])) {
137: $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
138: }
139:
140: if (isset($this->request->get['filter_country'])) {
141: $url .= '&filter_country=' . urlencode(html_entity_decode($this->request->get['filter_country'], ENT_QUOTES, 'UTF-8'));
142: }
143:
144: if (isset($this->request->get['filter_code'])) {
145: $url .= '&filter_code=' . urlencode(html_entity_decode($this->request->get['filter_code'], ENT_QUOTES, 'UTF-8'));
146: }
147:
148: if (isset($this->request->get['sort'])) {
149: $url .= '&sort=' . $this->request->get['sort'];
150: }
151:
152: if (isset($this->request->get['order'])) {
153: $url .= '&order=' . $this->request->get['order'];
154: }
155:
156: if (isset($this->request->get['page'])) {
157: $url .= '&page=' . $this->request->get['page'];
158: }
159:
160: $data['action'] = $this->url->link('localisation/zone.list', 'user_token=' . $this->session->data['user_token'] . $url);
161:
162: $data['zones'] = [];
163:
164: $filter_data = [
165: 'filter_name' => $filter_name,
166: 'filter_country' => $filter_country,
167: 'filter_code' => $filter_code,
168: 'sort' => $sort,
169: 'order' => $order,
170: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
171: 'limit' => $this->config->get('config_pagination_admin')
172: ];
173:
174: $this->load->model('localisation/zone');
175:
176: $results = $this->model_localisation_zone->getZones($filter_data);
177:
178: foreach ($results as $result) {
179: $data['zones'][] = [
180: 'zone_id' => $result['zone_id'],
181: 'country' => $result['country'],
182: 'name' => $result['name'] . (($result['zone_id'] == $this->config->get('config_zone_id')) ? $this->language->get('text_default') : ''),
183: 'code' => $result['code'],
184: 'status' => $result['status'],
185: 'edit' => $this->url->link('localisation/zone.form', 'user_token=' . $this->session->data['user_token'] . '&zone_id=' . $result['zone_id'] . $url)
186: ];
187: }
188:
189: $url = '';
190:
191: if (isset($this->request->get['filter_name'])) {
192: $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
193: }
194:
195: if (isset($this->request->get['filter_country'])) {
196: $url .= '&filter_country=' . urlencode(html_entity_decode($this->request->get['filter_country'], ENT_QUOTES, 'UTF-8'));
197: }
198:
199: if (isset($this->request->get['filter_code'])) {
200: $url .= '&filter_code=' . urlencode(html_entity_decode($this->request->get['filter_code'], ENT_QUOTES, 'UTF-8'));
201: }
202:
203: if ($order == 'ASC') {
204: $url .= '&order=DESC';
205: } else {
206: $url .= '&order=ASC';
207: }
208:
209: $data['sort_country'] = $this->url->link('localisation/zone.list', 'user_token=' . $this->session->data['user_token'] . '&sort=c.name' . $url);
210: $data['sort_name'] = $this->url->link('localisation/zone.list', 'user_token=' . $this->session->data['user_token'] . '&sort=z.name' . $url);
211: $data['sort_code'] = $this->url->link('localisation/zone.list', 'user_token=' . $this->session->data['user_token'] . '&sort=z.code' . $url);
212:
213: $url = '';
214:
215: if (isset($this->request->get['filter_name'])) {
216: $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
217: }
218:
219: if (isset($this->request->get['filter_country'])) {
220: $url .= '&filter_country=' . urlencode(html_entity_decode($this->request->get['filter_country'], ENT_QUOTES, 'UTF-8'));
221: }
222:
223: if (isset($this->request->get['filter_code'])) {
224: $url .= '&filter_code=' . urlencode(html_entity_decode($this->request->get['filter_code'], ENT_QUOTES, 'UTF-8'));
225: }
226:
227: if (isset($this->request->get['sort'])) {
228: $url .= '&sort=' . $this->request->get['sort'];
229: }
230:
231: if (isset($this->request->get['order'])) {
232: $url .= '&order=' . $this->request->get['order'];
233: }
234:
235: $zone_total = $this->model_localisation_zone->getTotalZones($filter_data);
236:
237: $data['pagination'] = $this->load->controller('common/pagination', [
238: 'total' => $zone_total,
239: 'page' => $page,
240: 'limit' => $this->config->get('config_pagination_admin'),
241: 'url' => $this->url->link('localisation/zone.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
242: ]);
243:
244: $data['results'] = sprintf($this->language->get('text_pagination'), ($zone_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($zone_total - $this->config->get('config_pagination_admin'))) ? $zone_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $zone_total, ceil($zone_total / $this->config->get('config_pagination_admin')));
245:
246: $data['sort'] = $sort;
247: $data['order'] = $order;
248:
249: return $this->load->view('localisation/zone_list', $data);
250: }
251:
252: /**
253: * Form
254: *
255: * @return void
256: */
257: public function form(): void {
258: $this->load->language('localisation/zone');
259:
260: $this->document->setTitle($this->language->get('heading_title'));
261:
262: $data['text_form'] = !isset($this->request->get['zone_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
263:
264: $url = '';
265:
266: if (isset($this->request->get['filter_name'])) {
267: $url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
268: }
269:
270: if (isset($this->request->get['filter_country'])) {
271: $url .= '&filter_country=' . urlencode(html_entity_decode($this->request->get['filter_country'], ENT_QUOTES, 'UTF-8'));
272: }
273:
274: if (isset($this->request->get['filter_code'])) {
275: $url .= '&filter_code=' . urlencode(html_entity_decode($this->request->get['filter_code'], ENT_QUOTES, 'UTF-8'));
276: }
277:
278: if (isset($this->request->get['sort'])) {
279: $url .= '&sort=' . $this->request->get['sort'];
280: }
281:
282: if (isset($this->request->get['order'])) {
283: $url .= '&order=' . $this->request->get['order'];
284: }
285:
286: if (isset($this->request->get['page'])) {
287: $url .= '&page=' . $this->request->get['page'];
288: }
289:
290: $data['breadcrumbs'] = [];
291:
292: $data['breadcrumbs'][] = [
293: 'text' => $this->language->get('text_home'),
294: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
295: ];
296:
297: $data['breadcrumbs'][] = [
298: 'text' => $this->language->get('heading_title'),
299: 'href' => $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . $url)
300: ];
301:
302: $data['save'] = $this->url->link('localisation/zone.save', 'user_token=' . $this->session->data['user_token']);
303: $data['back'] = $this->url->link('localisation/zone', 'user_token=' . $this->session->data['user_token'] . $url);
304:
305: if (isset($this->request->get['zone_id'])) {
306: $this->load->model('localisation/zone');
307:
308: $zone_info = $this->model_localisation_zone->getZone($this->request->get['zone_id']);
309: }
310:
311: if (isset($this->request->get['zone_id'])) {
312: $data['zone_id'] = (int)$this->request->get['zone_id'];
313: } else {
314: $data['zone_id'] = 0;
315: }
316:
317: if (!empty($zone_info)) {
318: $data['status'] = $zone_info['status'];
319: } else {
320: $data['status'] = '1';
321: }
322:
323: if (!empty($zone_info)) {
324: $data['name'] = $zone_info['name'];
325: } else {
326: $data['name'] = '';
327: }
328:
329: if (!empty($zone_info)) {
330: $data['code'] = $zone_info['code'];
331: } else {
332: $data['code'] = '';
333: }
334:
335: $this->load->model('localisation/country');
336:
337: $data['countries'] = $this->model_localisation_country->getCountries();
338:
339: if (!empty($zone_info)) {
340: $data['country_id'] = $zone_info['country_id'];
341: } else {
342: $data['country_id'] = '';
343: }
344:
345: $data['header'] = $this->load->controller('common/header');
346: $data['column_left'] = $this->load->controller('common/column_left');
347: $data['footer'] = $this->load->controller('common/footer');
348:
349: $this->response->setOutput($this->load->view('localisation/zone_form', $data));
350: }
351:
352: /**
353: * Save
354: *
355: * @return void
356: */
357: public function save(): void {
358: $this->load->language('localisation/zone');
359:
360: $json = [];
361:
362: if (!$this->user->hasPermission('modify', 'localisation/zone')) {
363: $json['error']['warning'] = $this->language->get('error_permission');
364: }
365:
366: if (!oc_validate_length($this->request->post['name'], 1, 64)) {
367: $json['error']['name'] = $this->language->get('error_name');
368: }
369:
370: if (!$json) {
371: $this->load->model('localisation/zone');
372:
373: if (!$this->request->post['zone_id']) {
374: $json['zone_id'] = $this->model_localisation_zone->addZone($this->request->post);
375: } else {
376: $this->model_localisation_zone->editZone($this->request->post['zone_id'], $this->request->post);
377: }
378:
379: $json['success'] = $this->language->get('text_success');
380: }
381:
382: $this->response->addHeader('Content-Type: application/json');
383: $this->response->setOutput(json_encode($json));
384: }
385:
386: /**
387: * Delete
388: *
389: * @return void
390: */
391: public function delete(): void {
392: $this->load->language('localisation/zone');
393:
394: $json = [];
395:
396: if (isset($this->request->post['selected'])) {
397: $selected = $this->request->post['selected'];
398: } else {
399: $selected = [];
400: }
401:
402: if (!$this->user->hasPermission('modify', 'localisation/zone')) {
403: $json['error'] = $this->language->get('error_permission');
404: }
405:
406: $this->load->model('setting/store');
407: $this->load->model('customer/customer');
408: $this->load->model('localisation/geo_zone');
409:
410: foreach ($selected as $zone_id) {
411: if ($this->config->get('config_zone_id') == $zone_id) {
412: $json['error'] = $this->language->get('error_default');
413: }
414:
415: $store_total = $this->model_setting_store->getTotalStoresByZoneId($zone_id);
416:
417: if ($store_total) {
418: $json['error'] = sprintf($this->language->get('error_store'), $store_total);
419: }
420:
421: $address_total = $this->model_customer_customer->getTotalAddressesByZoneId($zone_id);
422:
423: if ($address_total) {
424: $json['error'] = sprintf($this->language->get('error_address'), $address_total);
425: }
426:
427: $zone_to_geo_zone_total = $this->model_localisation_geo_zone->getTotalZoneToGeoZoneByZoneId($zone_id);
428:
429: if ($zone_to_geo_zone_total) {
430: $json['error'] = sprintf($this->language->get('error_zone_to_geo_zone'), $zone_to_geo_zone_total);
431: }
432: }
433:
434: if (!$json) {
435: $this->load->model('localisation/zone');
436:
437: foreach ($selected as $zone_id) {
438: $this->model_localisation_zone->deleteZone($zone_id);
439: }
440:
441: $json['success'] = $this->language->get('text_success');
442: }
443:
444: $this->response->addHeader('Content-Type: application/json');
445: $this->response->setOutput(json_encode($json));
446: }
447: }
448: