1: <?php
2: namespace Opencart\Admin\Controller\Localisation;
3: /**
4: * Class Address Format
5: *
6: * @package Opencart\Admin\Controller\Localisation
7: */
8: class AddressFormat extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('localisation/address_format');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: $url = '';
20:
21: if (isset($this->request->get['page'])) {
22: $url .= '&page=' . $this->request->get['page'];
23: }
24:
25: $data['breadcrumbs'] = [];
26:
27: $data['breadcrumbs'][] = [
28: 'text' => $this->language->get('text_home'),
29: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
30: ];
31:
32: $data['breadcrumbs'][] = [
33: 'text' => $this->language->get('heading_title'),
34: 'href' => $this->url->link('localisation/address_format', 'user_token=' . $this->session->data['user_token'] . $url)
35: ];
36:
37: $data['add'] = $this->url->link('localisation/address_format.form', 'user_token=' . $this->session->data['user_token'] . $url);
38: $data['delete'] = $this->url->link('localisation/address_format.delete', 'user_token=' . $this->session->data['user_token']);
39:
40: $data['list'] = $this->getList();
41:
42: $data['user_token'] = $this->session->data['user_token'];
43:
44: $data['header'] = $this->load->controller('common/header');
45: $data['column_left'] = $this->load->controller('common/column_left');
46: $data['footer'] = $this->load->controller('common/footer');
47:
48: $this->response->setOutput($this->load->view('localisation/address_format', $data));
49: }
50:
51: /**
52: * List
53: *
54: * @return void
55: */
56: public function list(): void {
57: $this->load->language('localisation/address_format');
58:
59: $this->response->setOutput($this->getList());
60: }
61:
62: /**
63: * Get List
64: *
65: * @return string
66: */
67: protected function getList(): string {
68: if (isset($this->request->get['page'])) {
69: $page = (int)$this->request->get['page'];
70: } else {
71: $page = 1;
72: }
73:
74: $url = '';
75:
76: if (isset($this->request->get['page'])) {
77: $url .= '&page=' . $this->request->get['page'];
78: }
79:
80: $data['action'] = $this->url->link('localisation/address_format.list', 'user_token=' . $this->session->data['user_token'] . $url);
81:
82: $data['address_formats'] = [];
83:
84: $filter_data = [
85: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
86: 'limit' => $this->config->get('config_pagination_admin')
87: ];
88:
89: $this->load->model('localisation/address_format');
90:
91: $results = $this->model_localisation_address_format->getAddressFormats($filter_data);
92:
93: foreach ($results as $result) {
94: $data['address_formats'][] = [
95: 'address_format_id' => $result['address_format_id'],
96: 'name' => $result['name'] . (($result['address_format_id'] == $this->config->get('config_address_format_id')) ? $this->language->get('text_default') : ''),
97: 'address_format' => nl2br($result['address_format']),
98: 'edit' => $this->url->link('localisation/address_format.form', 'user_token=' . $this->session->data['user_token'] . '&address_format_id=' . $result['address_format_id'] . $url)
99: ];
100: }
101:
102: $address_format_total = $this->model_localisation_address_format->getTotalAddressFormats($filter_data);
103:
104: $data['pagination'] = $this->load->controller('common/pagination', [
105: 'total' => $address_format_total,
106: 'page' => $page,
107: 'limit' => $this->config->get('config_pagination_admin'),
108: 'url' => $this->url->link('localisation/address_format.list', 'user_token=' . $this->session->data['user_token'] . '&page={page}')
109: ]);
110:
111: $data['results'] = sprintf($this->language->get('text_pagination'), ($address_format_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($address_format_total - $this->config->get('config_pagination_admin'))) ? $address_format_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $address_format_total, ceil($address_format_total / $this->config->get('config_pagination_admin')));
112:
113: return $this->load->view('localisation/address_format_list', $data);
114: }
115:
116: /**
117: * Form
118: *
119: * @return void
120: */
121: public function form(): void {
122: $this->load->language('localisation/address_format');
123:
124: $this->document->setTitle($this->language->get('heading_title'));
125:
126: $data['text_form'] = !isset($this->request->get['address_format_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
127:
128: $url = '';
129:
130: if (isset($this->request->get['page'])) {
131: $url .= '&page=' . $this->request->get['page'];
132: }
133:
134: $data['breadcrumbs'] = [];
135:
136: $data['breadcrumbs'][] = [
137: 'text' => $this->language->get('text_home'),
138: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
139: ];
140:
141: $data['breadcrumbs'][] = [
142: 'text' => $this->language->get('heading_title'),
143: 'href' => $this->url->link('localisation/address_format', 'user_token=' . $this->session->data['user_token'] . $url)
144: ];
145:
146: $data['save'] = $this->url->link('localisation/address_format.save', 'user_token=' . $this->session->data['user_token']);
147: $data['back'] = $this->url->link('localisation/address_format', 'user_token=' . $this->session->data['user_token'] . $url);
148:
149: if (isset($this->request->get['address_format_id'])) {
150: $this->load->model('localisation/address_format');
151:
152: $address_format_info = $this->model_localisation_address_format->getAddressFormat($this->request->get['address_format_id']);
153: }
154:
155: if (isset($this->request->get['address_format_id'])) {
156: $data['address_format_id'] = (int)$this->request->get['address_format_id'];
157: } else {
158: $data['address_format_id'] = 0;
159: }
160:
161: if (!empty($address_format_info)) {
162: $data['name'] = $address_format_info['name'];
163: } else {
164: $data['name'] = '';
165: }
166:
167: if (!empty($address_format_info)) {
168: $data['address_format'] = $address_format_info['address_format'];
169: } else {
170: $data['address_format'] = '';
171: }
172:
173: $data['header'] = $this->load->controller('common/header');
174: $data['column_left'] = $this->load->controller('common/column_left');
175: $data['footer'] = $this->load->controller('common/footer');
176:
177: $this->response->setOutput($this->load->view('localisation/address_format_form', $data));
178: }
179:
180: /**
181: * Save
182: *
183: * @return void
184: */
185: public function save(): void {
186: $this->load->language('localisation/address_format');
187:
188: $json = [];
189:
190: if (!$this->user->hasPermission('modify', 'localisation/address_format')) {
191: $json['error']['warning'] = $this->language->get('error_permission');
192: }
193:
194: if (!oc_validate_length($this->request->post['name'], 1, 128)) {
195: $json['error']['name'] = $this->language->get('error_name');
196: }
197:
198: if (!$json) {
199: $this->load->model('localisation/address_format');
200:
201: if (!$this->request->post['address_format_id']) {
202: $json['address_format_id'] = $this->model_localisation_address_format->addAddressFormat($this->request->post);
203: } else {
204: $this->model_localisation_address_format->editAddressFormat($this->request->post['address_format_id'], $this->request->post);
205: }
206:
207: $json['success'] = $this->language->get('text_success');
208: }
209:
210: $this->response->addHeader('Content-Type: application/json');
211: $this->response->setOutput(json_encode($json));
212: }
213:
214: /**
215: * Delete
216: *
217: * @return void
218: */
219: public function delete(): void {
220: $this->load->language('localisation/address_format');
221:
222: $json = [];
223:
224: if (isset($this->request->post['selected'])) {
225: $selected = $this->request->post['selected'];
226: } else {
227: $selected = [];
228: }
229:
230: if (!$this->user->hasPermission('modify', 'localisation/address_format')) {
231: $json['error'] = $this->language->get('error_permission');
232: }
233:
234: $this->load->model('localisation/country');
235:
236: foreach ($selected as $address_format_id) {
237: if ($this->config->get('config_address_format_id') == $address_format_id) {
238: $json['error'] = $this->language->get('error_default');
239: }
240:
241: $country_total = $this->model_localisation_country->getTotalCountriesByAddressFormatId($address_format_id);
242:
243: if ($country_total) {
244: $json['error'] = sprintf($this->language->get('error_country'), $country_total);
245: }
246: }
247:
248: if (!$json) {
249: $this->load->model('localisation/address_format');
250:
251: foreach ($selected as $address_format_id) {
252: $this->model_localisation_address_format->deleteAddressFormat($address_format_id);
253: }
254:
255: $json['success'] = $this->language->get('text_success');
256: }
257:
258: $this->response->addHeader('Content-Type: application/json');
259: $this->response->setOutput(json_encode($json));
260: }
261: }
262: