1: <?php
2: namespace Opencart\Admin\Controller\Localisation;
3: /**
4: * Class Geo Zone
5: *
6: * @package Opencart\Admin\Controller\Localisation
7: */
8: class GeoZone extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('localisation/geo_zone');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: $url = '';
20:
21: if (isset($this->request->get['sort'])) {
22: $url .= '&sort=' . $this->request->get['sort'];
23: }
24:
25: if (isset($this->request->get['order'])) {
26: $url .= '&order=' . $this->request->get['order'];
27: }
28:
29: if (isset($this->request->get['page'])) {
30: $url .= '&page=' . $this->request->get['page'];
31: }
32:
33: $data['breadcrumbs'] = [];
34:
35: $data['breadcrumbs'][] = [
36: 'text' => $this->language->get('text_home'),
37: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
38: ];
39:
40: $data['breadcrumbs'][] = [
41: 'text' => $this->language->get('heading_title'),
42: 'href' => $this->url->link('localisation/geo_zone', 'user_token=' . $this->session->data['user_token'] . $url)
43: ];
44:
45: $data['add'] = $this->url->link('localisation/geo_zone.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: $data['delete'] = $this->url->link('localisation/geo_zone.delete', 'user_token=' . $this->session->data['user_token']);
47:
48: $data['list'] = $this->getList();
49:
50: $data['user_token'] = $this->session->data['user_token'];
51:
52: $data['header'] = $this->load->controller('common/header');
53: $data['column_left'] = $this->load->controller('common/column_left');
54: $data['footer'] = $this->load->controller('common/footer');
55:
56: $this->response->setOutput($this->load->view('localisation/geo_zone', $data));
57: }
58:
59: /**
60: * List
61: *
62: * @return void
63: */
64: public function list(): void {
65: $this->load->language('localisation/geo_zone');
66:
67: $this->response->setOutput($this->getList());
68: }
69:
70: /**
71: * Get List
72: *
73: * @return string
74: */
75: protected function getList(): string {
76: if (isset($this->request->get['sort'])) {
77: $sort = (string)$this->request->get['sort'];
78: } else {
79: $sort = 'name';
80: }
81:
82: if (isset($this->request->get['order'])) {
83: $order = (string)$this->request->get['order'];
84: } else {
85: $order = 'ASC';
86: }
87:
88: if (isset($this->request->get['page'])) {
89: $page = (int)$this->request->get['page'];
90: } else {
91: $page = 1;
92: }
93:
94: $url = '';
95:
96: if (isset($this->request->get['sort'])) {
97: $url .= '&sort=' . $this->request->get['sort'];
98: }
99:
100: if (isset($this->request->get['order'])) {
101: $url .= '&order=' . $this->request->get['order'];
102: }
103:
104: if (isset($this->request->get['page'])) {
105: $url .= '&page=' . $this->request->get['page'];
106: }
107:
108: $data['action'] = $this->url->link('localisation/geo_zone.list', 'user_token=' . $this->session->data['user_token'] . $url);
109:
110: $data['geo_zones'] = [];
111:
112: $filter_data = [
113: 'sort' => $sort,
114: 'order' => $order,
115: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
116: 'limit' => $this->config->get('config_pagination_admin')
117: ];
118:
119: $this->load->model('localisation/geo_zone');
120:
121: $results = $this->model_localisation_geo_zone->getGeoZones($filter_data);
122:
123: foreach ($results as $result) {
124: $data['geo_zones'][] = [
125: 'geo_zone_id' => $result['geo_zone_id'],
126: 'name' => $result['name'],
127: 'description' => $result['description'],
128: 'edit' => $this->url->link('localisation/geo_zone.form', 'user_token=' . $this->session->data['user_token'] . '&geo_zone_id=' . $result['geo_zone_id'] . $url)
129: ];
130: }
131:
132: $url = '';
133:
134: if ($order == 'ASC') {
135: $url .= '&order=DESC';
136: } else {
137: $url .= '&order=ASC';
138: }
139:
140: $data['sort_name'] = $this->url->link('localisation/geo_zone.list', 'user_token=' . $this->session->data['user_token'] . '&sort=name' . $url);
141: $data['sort_description'] = $this->url->link('localisation/geo_zone.list', 'user_token=' . $this->session->data['user_token'] . '&sort=description' . $url);
142:
143: $url = '';
144:
145: if (isset($this->request->get['sort'])) {
146: $url .= '&sort=' . $this->request->get['sort'];
147: }
148:
149: if (isset($this->request->get['order'])) {
150: $url .= '&order=' . $this->request->get['order'];
151: }
152:
153: $geo_zone_total = $this->model_localisation_geo_zone->getTotalGeoZones();
154:
155: $data['pagination'] = $this->load->controller('common/pagination', [
156: 'total' => $geo_zone_total,
157: 'page' => $page,
158: 'limit' => $this->config->get('config_pagination_admin'),
159: 'url' => $this->url->link('localisation/geo_zone.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
160: ]);
161:
162: $data['results'] = sprintf($this->language->get('text_pagination'), ($geo_zone_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($geo_zone_total - $this->config->get('config_pagination_admin'))) ? $geo_zone_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $geo_zone_total, ceil($geo_zone_total / $this->config->get('config_pagination_admin')));
163:
164: $data['sort'] = $sort;
165: $data['order'] = $order;
166:
167: return $this->load->view('localisation/geo_zone_list', $data);
168: }
169:
170: /**
171: * Form
172: *
173: * @return void
174: */
175: public function form(): void {
176: $this->load->language('localisation/geo_zone');
177:
178: $this->document->setTitle($this->language->get('heading_title'));
179:
180: $data['text_form'] = !isset($this->request->get['geo_zone_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
181:
182: $url = '';
183:
184: if (isset($this->request->get['sort'])) {
185: $url .= '&sort=' . $this->request->get['sort'];
186: }
187:
188: if (isset($this->request->get['order'])) {
189: $url .= '&order=' . $this->request->get['order'];
190: }
191:
192: if (isset($this->request->get['page'])) {
193: $url .= '&page=' . $this->request->get['page'];
194: }
195:
196: $data['breadcrumbs'] = [];
197:
198: $data['breadcrumbs'][] = [
199: 'text' => $this->language->get('text_home'),
200: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
201: ];
202:
203: $data['breadcrumbs'][] = [
204: 'text' => $this->language->get('heading_title'),
205: 'href' => $this->url->link('localisation/geo_zone', 'user_token=' . $this->session->data['user_token'] . $url)
206: ];
207:
208: $data['save'] = $this->url->link('localisation/geo_zone.save', 'user_token=' . $this->session->data['user_token']);
209: $data['back'] = $this->url->link('localisation/geo_zone', 'user_token=' . $this->session->data['user_token'] . $url);
210:
211: if (isset($this->request->get['geo_zone_id'])) {
212: $this->load->model('localisation/geo_zone');
213:
214: $geo_zone_info = $this->model_localisation_geo_zone->getGeoZone($this->request->get['geo_zone_id']);
215: }
216:
217: if (isset($this->request->get['geo_zone_id'])) {
218: $data['geo_zone_id'] = (int)$this->request->get['geo_zone_id'];
219: } else {
220: $data['geo_zone_id'] = 0;
221: }
222:
223: if (!empty($geo_zone_info)) {
224: $data['name'] = $geo_zone_info['name'];
225: } else {
226: $data['name'] = '';
227: }
228:
229: if (!empty($geo_zone_info)) {
230: $data['description'] = $geo_zone_info['description'];
231: } else {
232: $data['description'] = '';
233: }
234:
235: $this->load->model('localisation/country');
236:
237: $data['countries'] = $this->model_localisation_country->getCountries();
238:
239: if (!empty($geo_zone_info)) {
240: $data['zone_to_geo_zones'] = $this->model_localisation_geo_zone->getZones($this->request->get['geo_zone_id']);
241: } else {
242: $data['zone_to_geo_zones'] = [];
243: }
244:
245: $data['user_token'] = $this->session->data['user_token'];
246:
247: $data['header'] = $this->load->controller('common/header');
248: $data['column_left'] = $this->load->controller('common/column_left');
249: $data['footer'] = $this->load->controller('common/footer');
250:
251: $this->response->setOutput($this->load->view('localisation/geo_zone_form', $data));
252: }
253:
254: /**
255: * Save
256: *
257: * @return void
258: */
259: public function save(): void {
260: $this->load->language('localisation/geo_zone');
261:
262: $json = [];
263:
264: if (!$this->user->hasPermission('modify', 'localisation/geo_zone')) {
265: $json['error']['warning'] = $this->language->get('error_permission');
266: }
267:
268: if ((oc_strlen($this->request->post['name']) < 3) || (oc_strlen($this->request->post['name']) > 32)) {
269: $json['error']['name'] = $this->language->get('error_name');
270: }
271:
272: if ((oc_strlen($this->request->post['description']) < 3) || (oc_strlen($this->request->post['description']) > 255)) {
273: $json['error']['description'] = $this->language->get('error_description');
274: }
275:
276: if (!$json) {
277: $this->load->model('localisation/geo_zone');
278:
279: if (!$this->request->post['geo_zone_id']) {
280: $json['geo_zone_id'] = $this->model_localisation_geo_zone->addGeoZone($this->request->post);
281: } else {
282: $this->model_localisation_geo_zone->editGeoZone($this->request->post['geo_zone_id'], $this->request->post);
283: }
284:
285: $json['success'] = $this->language->get('text_success');
286: }
287:
288: $this->response->addHeader('Content-Type: application/json');
289: $this->response->setOutput(json_encode($json));
290: }
291:
292: /**
293: * Delete
294: *
295: * @return void
296: */
297: public function delete(): void {
298: $this->load->language('localisation/geo_zone');
299:
300: $json = [];
301:
302: if (isset($this->request->post['selected'])) {
303: $selected = $this->request->post['selected'];
304: } else {
305: $selected = [];
306: }
307:
308: if (!$this->user->hasPermission('modify', 'localisation/geo_zone')) {
309: $json['error'] = $this->language->get('error_permission');
310: }
311:
312: $this->load->model('localisation/tax_rate');
313:
314: foreach ($selected as $geo_zone_id) {
315: $tax_rate_total = $this->model_localisation_tax_rate->getTotalTaxRatesByGeoZoneId($geo_zone_id);
316:
317: if ($tax_rate_total) {
318: $json['error'] = sprintf($this->language->get('error_tax_rate'), $tax_rate_total);
319: }
320: }
321:
322: if (!$json) {
323: $this->load->model('localisation/geo_zone');
324:
325: foreach ($selected as $geo_zone_id) {
326: $this->model_localisation_geo_zone->deleteGeoZone($geo_zone_id);
327: }
328:
329: $json['success'] = $this->language->get('text_success');
330: }
331:
332: $this->response->addHeader('Content-Type: application/json');
333: $this->response->setOutput(json_encode($json));
334: }
335: }
336: