1: <?php
2: namespace Opencart\Admin\Controller\Localisation;
3: /**
4: * Class Country
5: *
6: * @package Opencart\Admin\Controller\Localisation
7: */
8: class Country extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('localisation/country');
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_iso_code_2'])) {
26: $filter_iso_code_2 = (string)$this->request->get['filter_iso_code_2'];
27: } else {
28: $filter_iso_code_2 = '';
29: }
30:
31: if (isset($this->request->get['filter_iso_code_3'])) {
32: $filter_iso_code_3 = (string)$this->request->get['filter_iso_code_3'];
33: } else {
34: $filter_iso_code_3 = '';
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/country', 'user_token=' . $this->session->data['user_token'] . $url)
61: ];
62:
63: $data['add'] = $this->url->link('localisation/country.form', 'user_token=' . $this->session->data['user_token'] . $url);
64: $data['delete'] = $this->url->link('localisation/country.delete', 'user_token=' . $this->session->data['user_token']);
65:
66: $data['list'] = $this->getList();
67:
68: $data['filter_name'] = $filter_name;
69: $data['filter_iso_code_2'] = $filter_iso_code_2;
70: $data['filter_iso_code_3'] = $filter_iso_code_3;
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/country', $data));
79: }
80:
81: /**
82: * List
83: *
84: * @return void
85: */
86: public function list(): void {
87: $this->load->language('localisation/country');
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_iso_code_2'])) {
105: $filter_iso_code_2 = (string)$this->request->get['filter_iso_code_2'];
106: } else {
107: $filter_iso_code_2 = '';
108: }
109:
110: if (isset($this->request->get['filter_iso_code_3'])) {
111: $filter_iso_code_3 = (string)$this->request->get['filter_iso_code_3'];
112: } else {
113: $filter_iso_code_3 = '';
114: }
115:
116: if (isset($this->request->get['sort'])) {
117: $sort = (string)$this->request->get['sort'];
118: } else {
119: $sort = '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_iso_code_2'])) {
141: $url .= '&filter_iso_code_2=' . urlencode(html_entity_decode($this->request->get['filter_iso_code_2'], ENT_QUOTES, 'UTF-8'));
142: }
143:
144: if (isset($this->request->get['filter_iso_code_3'])) {
145: $url .= '&filter_iso_code_3=' . urlencode(html_entity_decode($this->request->get['filter_iso_code_3'], 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/country.list', 'user_token=' . $this->session->data['user_token'] . $url);
161:
162: $data['countries'] = [];
163:
164: $filter_data = [
165: 'filter_name' => $filter_name,
166: 'filter_iso_code_2' => $filter_iso_code_2,
167: 'filter_iso_code_3' => $filter_iso_code_3,
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/country');
175:
176: $results = $this->model_localisation_country->getCountries($filter_data);
177:
178: foreach ($results as $result) {
179: $data['countries'][] = [
180: 'country_id' => $result['country_id'],
181: 'name' => $result['name'] . (($result['country_id'] == $this->config->get('config_country_id')) ? $this->language->get('text_default') : ''),
182: 'status' => $result['status'],
183: 'iso_code_2' => $result['iso_code_2'],
184: 'iso_code_3' => $result['iso_code_3'],
185: 'edit' => $this->url->link('localisation/country.form', 'user_token=' . $this->session->data['user_token'] . '&country_id=' . $result['country_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_iso_code_2'])) {
196: $url .= '&filter_iso_code_2=' . urlencode(html_entity_decode($this->request->get['filter_iso_code_2'], ENT_QUOTES, 'UTF-8'));
197: }
198:
199: if (isset($this->request->get['filter_iso_code_3'])) {
200: $url .= '&filter_iso_code_3=' . urlencode(html_entity_decode($this->request->get['filter_iso_code_3'], 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_name'] = $this->url->link('localisation/country.list', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url);
210: $data['sort_iso_code_2'] = $this->url->link('localisation/country.list', 'user_token=' . $this->session->data['user_token'] . '&sort=iso_code_2' . $url);
211: $data['sort_iso_code_3'] = $this->url->link('localisation/country.list', 'user_token=' . $this->session->data['user_token'] . '&sort=iso_code_3' . $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_iso_code_2'])) {
220: $url .= '&filter_iso_code_2=' . urlencode(html_entity_decode($this->request->get['filter_iso_code_2'], ENT_QUOTES, 'UTF-8'));
221: }
222:
223: if (isset($this->request->get['filter_iso_code_3'])) {
224: $url .= '&filter_iso_code_3=' . urlencode(html_entity_decode($this->request->get['filter_iso_code_3'], 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: $country_total = $this->model_localisation_country->getTotalCountries($filter_data);
236:
237: $data['pagination'] = $this->load->controller('common/pagination', [
238: 'total' => $country_total,
239: 'page' => $page,
240: 'limit' => $this->config->get('config_pagination_admin'),
241: 'url' => $this->url->link('localisation/country.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
242: ]);
243:
244: $data['results'] = sprintf($this->language->get('text_pagination'), ($country_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($country_total - $this->config->get('config_pagination_admin'))) ? $country_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $country_total, ceil($country_total / $this->config->get('config_pagination_admin')));
245:
246: $data['sort'] = $sort;
247: $data['order'] = $order;
248:
249: return $this->load->view('localisation/country_list', $data);
250: }
251:
252: /**
253: * Form
254: *
255: * @return void
256: */
257: public function form(): void {
258: $this->load->language('localisation/country');
259:
260: $this->document->setTitle($this->language->get('heading_title'));
261:
262: $data['text_form'] = !isset($this->request->get['country_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_iso_code_2'])) {
271: $url .= '&filter_iso_code_2=' . urlencode(html_entity_decode($this->request->get['filter_iso_code_2'], ENT_QUOTES, 'UTF-8'));
272: }
273:
274: if (isset($this->request->get['filter_iso_code_3'])) {
275: $url .= '&filter_iso_code_3=' . urlencode(html_entity_decode($this->request->get['filter_iso_code_3'], 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/country', 'user_token=' . $this->session->data['user_token'] . $url)
300: ];
301:
302: $data['save'] = $this->url->link('localisation/country.save', 'user_token=' . $this->session->data['user_token']);
303: $data['back'] = $this->url->link('localisation/country', 'user_token=' . $this->session->data['user_token'] . $url);
304:
305: if (isset($this->request->get['country_id'])) {
306: $this->load->model('localisation/country');
307:
308: $country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']);
309: }
310:
311: if (isset($this->request->get['country_id'])) {
312: $data['country_id'] = (int)$this->request->get['country_id'];
313: } else {
314: $data['country_id'] = 0;
315: }
316:
317: if (!empty($country_info)) {
318: $data['name'] = $country_info['name'];
319: } else {
320: $data['name'] = '';
321: }
322:
323: if (!empty($country_info)) {
324: $data['iso_code_2'] = $country_info['iso_code_2'];
325: } else {
326: $data['iso_code_2'] = '';
327: }
328:
329: if (!empty($country_info)) {
330: $data['iso_code_3'] = $country_info['iso_code_3'];
331: } else {
332: $data['iso_code_3'] = '';
333: }
334:
335: $this->load->model('localisation/address_format');
336:
337: $data['address_formats'] = $this->model_localisation_address_format->getAddressFormats();
338:
339: if (!empty($country_info)) {
340: $data['address_format_id'] = $country_info['address_format_id'];
341: } else {
342: $data['address_format_id'] = '';
343: }
344:
345: if (!empty($country_info)) {
346: $data['postcode_required'] = $country_info['postcode_required'];
347: } else {
348: $data['postcode_required'] = 0;
349: }
350:
351: if (!empty($country_info)) {
352: $data['status'] = $country_info['status'];
353: } else {
354: $data['status'] = '1';
355: }
356:
357: $data['header'] = $this->load->controller('common/header');
358: $data['column_left'] = $this->load->controller('common/column_left');
359: $data['footer'] = $this->load->controller('common/footer');
360:
361: $this->response->setOutput($this->load->view('localisation/country_form', $data));
362: }
363:
364: /**
365: * Save
366: *
367: * @return void
368: */
369: public function save(): void {
370: $this->load->language('localisation/country');
371:
372: $json = [];
373:
374: if (!$this->user->hasPermission('modify', 'localisation/country')) {
375: $json['error']['warning'] = $this->language->get('error_permission');
376: }
377:
378: if (!oc_validate_length($this->request->post['name'], 1, 128)) {
379: $json['error']['name'] = $this->language->get('error_name');
380: }
381:
382: if (!$json) {
383: $this->load->model('localisation/country');
384:
385: if (!$this->request->post['country_id']) {
386: $json['country_id'] = $this->model_localisation_country->addCountry($this->request->post);
387: } else {
388: $this->model_localisation_country->editCountry($this->request->post['country_id'], $this->request->post);
389: }
390:
391: $json['success'] = $this->language->get('text_success');
392: }
393:
394: $this->response->addHeader('Content-Type: application/json');
395: $this->response->setOutput(json_encode($json));
396: }
397:
398: /**
399: * Delete
400: *
401: * @return void
402: */
403: public function delete(): void {
404: $this->load->language('localisation/country');
405:
406: $json = [];
407:
408: if (isset($this->request->post['selected'])) {
409: $selected = $this->request->post['selected'];
410: } else {
411: $selected = [];
412: }
413:
414: if (!$this->user->hasPermission('modify', 'localisation/country')) {
415: $json['error'] = $this->language->get('error_permission');
416: }
417:
418: $this->load->model('setting/store');
419: $this->load->model('customer/customer');
420: $this->load->model('localisation/zone');
421: $this->load->model('localisation/geo_zone');
422:
423: foreach ($selected as $country_id) {
424: if ($this->config->get('config_country_id') == $country_id) {
425: $json['error'] = $this->language->get('error_default');
426: }
427:
428: $store_total = $this->model_setting_store->getTotalStoresByCountryId($country_id);
429:
430: if ($store_total) {
431: $json['error'] = sprintf($this->language->get('error_store'), $store_total);
432: }
433:
434: $address_total = $this->model_customer_customer->getTotalAddressesByCountryId($country_id);
435:
436: if ($address_total) {
437: $json['error'] = sprintf($this->language->get('error_address'), $address_total);
438: }
439:
440: $zone_total = $this->model_localisation_zone->getTotalZonesByCountryId($country_id);
441:
442: if ($zone_total) {
443: $json['error'] = sprintf($this->language->get('error_zone'), $zone_total);
444: }
445:
446: $zone_to_geo_zone_total = $this->model_localisation_geo_zone->getTotalZoneToGeoZoneByCountryId($country_id);
447:
448: if ($zone_to_geo_zone_total) {
449: $json['error'] = sprintf($this->language->get('error_zone_to_geo_zone'), $zone_to_geo_zone_total);
450: }
451: }
452:
453: if (!$json) {
454: $this->load->model('localisation/country');
455:
456: foreach ($selected as $country_id) {
457: $this->model_localisation_country->deleteCountry($country_id);
458: }
459:
460: $json['success'] = $this->language->get('text_success');
461: }
462:
463: $this->response->addHeader('Content-Type: application/json');
464: $this->response->setOutput(json_encode($json));
465: }
466:
467: /**
468: * Country
469: *
470: * @return void
471: */
472: public function country(): void {
473: $json = [];
474:
475: if (isset($this->request->get['country_id'])) {
476: $country_id = (int)$this->request->get['country_id'];
477: } else {
478: $country_id = 0;
479: }
480:
481: $this->load->model('localisation/country');
482:
483: $country_info = $this->model_localisation_country->getCountry($country_id);
484:
485: if ($country_info) {
486: $this->load->model('localisation/zone');
487:
488: $json = [
489: 'country_id' => $country_info['country_id'],
490: 'name' => $country_info['name'],
491: 'iso_code_2' => $country_info['iso_code_2'],
492: 'iso_code_3' => $country_info['iso_code_3'],
493: 'address_format_id' => $country_info['address_format_id'],
494: 'postcode_required' => $country_info['postcode_required'],
495: 'zone' => $this->model_localisation_zone->getZonesByCountryId($country_id),
496: 'status' => $country_info['status']
497: ];
498: }
499:
500: $this->response->addHeader('Content-Type: application/json');
501: $this->response->setOutput(json_encode($json));
502: }
503: }
504: