1: <?php
2: namespace Opencart\Admin\Controller\Localisation;
3: /**
4: * Class Currency
5: *
6: * @package Opencart\Admin\Controller\Localisation
7: */
8: class Currency extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('localisation/currency');
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/currency', 'user_token=' . $this->session->data['user_token'] . $url)
43: ];
44:
45: $data['refresh'] = $this->url->link('localisation/currency.refresh', 'user_token=' . $this->session->data['user_token'] . $url);
46: $data['add'] = $this->url->link('localisation/currency.form', 'user_token=' . $this->session->data['user_token'] . $url);
47: $data['delete'] = $this->url->link('localisation/currency.delete', 'user_token=' . $this->session->data['user_token']);
48:
49: $data['list'] = $this->getList();
50:
51: $data['user_token'] = $this->session->data['user_token'];
52:
53: $data['header'] = $this->load->controller('common/header');
54: $data['column_left'] = $this->load->controller('common/column_left');
55: $data['footer'] = $this->load->controller('common/footer');
56:
57: $this->response->setOutput($this->load->view('localisation/currency', $data));
58: }
59:
60: /**
61: * List
62: *
63: * @return void
64: */
65: public function list(): void {
66: $this->load->language('localisation/currency');
67:
68: $this->response->setOutput($this->getList());
69: }
70:
71: /**
72: * Get List
73: *
74: * @return string
75: */
76: protected function getList(): string {
77: if (isset($this->request->get['sort'])) {
78: $sort = (string)$this->request->get['sort'];
79: } else {
80: $sort = 'title';
81: }
82:
83: if (isset($this->request->get['order'])) {
84: $order = (string)$this->request->get['order'];
85: } else {
86: $order = 'ASC';
87: }
88:
89: if (isset($this->request->get['page'])) {
90: $page = (int)$this->request->get['page'];
91: } else {
92: $page = 1;
93: }
94:
95: $url = '';
96:
97: if (isset($this->request->get['sort'])) {
98: $url .= '&sort=' . $this->request->get['sort'];
99: }
100:
101: if (isset($this->request->get['order'])) {
102: $url .= '&order=' . $this->request->get['order'];
103: }
104:
105: if (isset($this->request->get['page'])) {
106: $url .= '&page=' . $this->request->get['page'];
107: }
108:
109: $data['breadcrumbs'] = [];
110:
111: $data['breadcrumbs'][] = [
112: 'text' => $this->language->get('text_home'),
113: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
114: ];
115:
116: $data['breadcrumbs'][] = [
117: 'text' => $this->language->get('heading_title'),
118: 'href' => $this->url->link('localisation/currency', 'user_token=' . $this->session->data['user_token'] . $url)
119: ];
120:
121: $data['action'] = $this->url->link('localisation/currency.list', 'user_token=' . $this->session->data['user_token'] . $url);
122:
123: $data['currencies'] = [];
124:
125: $filter_data = [
126: 'sort' => $sort,
127: 'order' => $order,
128: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
129: 'limit' => $this->config->get('config_pagination_admin')
130: ];
131:
132: $this->load->model('localisation/currency');
133:
134: $results = $this->model_localisation_currency->getCurrencies($filter_data);
135:
136: foreach ($results as $result) {
137: $data['currencies'][] = [
138: 'currency_id' => $result['currency_id'],
139: 'title' => $result['title'] . (($result['code'] == $this->config->get('config_currency')) ? $this->language->get('text_default') : ''),
140: 'code' => $result['code'],
141: 'value' => $result['value'],
142: 'status' => $result['status'],
143: 'date_modified' => date($this->language->get('date_format_short'), strtotime($result['date_modified'])),
144: 'edit' => $this->url->link('localisation/currency.form', 'user_token=' . $this->session->data['user_token'] . '&currency_id=' . $result['currency_id'] . $url)
145: ];
146: }
147:
148: $url = '';
149:
150: if ($order == 'ASC') {
151: $url .= '&order=DESC';
152: } else {
153: $url .= '&order=ASC';
154: }
155:
156: $data['sort_title'] = $this->url->link('localisation/currency.list', 'user_token=' . $this->session->data['user_token'] . '&sort=title' . $url);
157: $data['sort_code'] = $this->url->link('localisation/currency.list', 'user_token=' . $this->session->data['user_token'] . '&sort=code' . $url);
158: $data['sort_value'] = $this->url->link('localisation/currency.list', 'user_token=' . $this->session->data['user_token'] . '&sort=value' . $url);
159: $data['sort_status'] = $this->url->link('localisation/currency.list', 'user_token=' . $this->session->data['user_token'] . '&sort=status' . $url);
160: $data['sort_date_modified'] = $this->url->link('localisation/currency.list', 'user_token=' . $this->session->data['user_token'] . '&sort=date_modified' . $url);
161:
162: $url = '';
163:
164: if (isset($this->request->get['sort'])) {
165: $url .= '&sort=' . $this->request->get['sort'];
166: }
167:
168: if (isset($this->request->get['order'])) {
169: $url .= '&order=' . $this->request->get['order'];
170: }
171:
172: $currency_total = $this->model_localisation_currency->getTotalCurrencies();
173:
174: $data['pagination'] = $this->load->controller('common/pagination', [
175: 'total' => $currency_total,
176: 'page' => $page,
177: 'limit' => $this->config->get('config_pagination_admin'),
178: 'url' => $this->url->link('localisation/currency.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
179: ]);
180:
181: $data['results'] = sprintf($this->language->get('text_pagination'), ($currency_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($currency_total - $this->config->get('config_pagination_admin'))) ? $currency_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $currency_total, ceil($currency_total / $this->config->get('config_pagination_admin')));
182:
183: $data['sort'] = $sort;
184: $data['order'] = $order;
185:
186: return $this->load->view('localisation/currency_list', $data);
187: }
188:
189: /**
190: * Form
191: *
192: * @return void
193: */
194: public function form(): void {
195: $this->load->language('localisation/currency');
196:
197: $this->document->setTitle($this->language->get('heading_title'));
198:
199: $data['text_form'] = !isset($this->request->get['currency_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
200:
201: $url = '';
202:
203: if (isset($this->request->get['sort'])) {
204: $url .= '&sort=' . $this->request->get['sort'];
205: }
206:
207: if (isset($this->request->get['order'])) {
208: $url .= '&order=' . $this->request->get['order'];
209: }
210:
211: if (isset($this->request->get['page'])) {
212: $url .= '&page=' . $this->request->get['page'];
213: }
214:
215: $data['breadcrumbs'] = [];
216:
217: $data['breadcrumbs'][] = [
218: 'text' => $this->language->get('text_home'),
219: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
220: ];
221:
222: $data['breadcrumbs'][] = [
223: 'text' => $this->language->get('heading_title'),
224: 'href' => $this->url->link('localisation/currency', 'user_token=' . $this->session->data['user_token'] . $url)
225: ];
226:
227: $data['save'] = $this->url->link('localisation/currency.save', 'user_token=' . $this->session->data['user_token']);
228: $data['back'] = $this->url->link('localisation/currency', 'user_token=' . $this->session->data['user_token'] . $url);
229:
230: if (isset($this->request->get['currency_id'])) {
231: $this->load->model('localisation/currency');
232:
233: $currency_info = $this->model_localisation_currency->getCurrency($this->request->get['currency_id']);
234: }
235:
236: if (isset($this->request->get['currency_id'])) {
237: $data['currency_id'] = (int)$this->request->get['currency_id'];
238: } else {
239: $data['currency_id'] = 0;
240: }
241:
242: if (!empty($currency_info)) {
243: $data['title'] = $currency_info['title'];
244: } else {
245: $data['title'] = '';
246: }
247:
248: if (!empty($currency_info)) {
249: $data['code'] = $currency_info['code'];
250: } else {
251: $data['code'] = '';
252: }
253:
254: if (!empty($currency_info)) {
255: $data['symbol_left'] = $currency_info['symbol_left'];
256: } else {
257: $data['symbol_left'] = '';
258: }
259:
260: if (!empty($currency_info)) {
261: $data['symbol_right'] = $currency_info['symbol_right'];
262: } else {
263: $data['symbol_right'] = '';
264: }
265:
266: if (!empty($currency_info)) {
267: $data['decimal_place'] = $currency_info['decimal_place'];
268: } else {
269: $data['decimal_place'] = '';
270: }
271:
272: if (!empty($currency_info)) {
273: $data['value'] = $currency_info['value'];
274: } else {
275: $data['value'] = '';
276: }
277:
278: if (!empty($currency_info)) {
279: $data['status'] = $currency_info['status'];
280: } else {
281: $data['status'] = '';
282: }
283:
284: $data['user_token'] = $this->session->data['user_token'];
285:
286: $data['header'] = $this->load->controller('common/header');
287: $data['column_left'] = $this->load->controller('common/column_left');
288: $data['footer'] = $this->load->controller('common/footer');
289:
290: $this->response->setOutput($this->load->view('localisation/currency_form', $data));
291: }
292:
293: /**
294: * Save
295: *
296: * @return void
297: */
298: public function save(): void {
299: $this->load->language('localisation/currency');
300:
301: $json = [];
302:
303: if (!$this->user->hasPermission('modify', 'localisation/currency')) {
304: $json['error']['warning'] = $this->language->get('error_permission');
305: }
306:
307: if ((oc_strlen($this->request->post['title']) < 3) || (oc_strlen($this->request->post['title']) > 32)) {
308: $json['error']['title'] = $this->language->get('error_title');
309: }
310:
311: if (oc_strlen($this->request->post['code']) != 3) {
312: $json['error']['code'] = $this->language->get('error_code');
313: }
314:
315: if (!$json) {
316: $this->load->model('localisation/currency');
317:
318: if (!$this->request->post['currency_id']) {
319: $json['currency_id'] = $this->model_localisation_currency->addCurrency($this->request->post);
320: } else {
321: $this->model_localisation_currency->editCurrency($this->request->post['currency_id'], $this->request->post);
322: }
323:
324: $json['success'] = $this->language->get('text_success');
325: }
326:
327: $this->response->addHeader('Content-Type: application/json');
328: $this->response->setOutput(json_encode($json));
329: }
330:
331: /**
332: * Refresh
333: *
334: * @return void
335: */
336: public function refresh(): void {
337: $this->load->language('localisation/currency');
338:
339: $json = [];
340:
341: if (!$this->user->hasPermission('modify', 'localisation/currency')) {
342: $json['error'] = $this->language->get('error_permission');
343: }
344:
345: $this->load->model('setting/extension');
346:
347: $extension_info = $this->model_setting_extension->getExtensionByCode('currency', $this->config->get('config_currency_engine'));
348:
349: if (!$extension_info) {
350: $json['error'] = $this->language->get('error_extension');
351: }
352:
353: if (!$json) {
354: $this->load->controller('extension/' . $extension_info['extension'] . '/currency/' . $extension_info['code'] . '.currency', $this->config->get('config_currency'));
355:
356: $json['success'] = $this->language->get('text_success');
357: }
358:
359: $this->response->addHeader('Content-Type: application/json');
360: $this->response->setOutput(json_encode($json));
361: }
362:
363: /**
364: * Delete
365: *
366: * @return void
367: */
368: public function delete(): void {
369: $this->load->language('localisation/currency');
370:
371: $json = [];
372:
373: if (isset($this->request->post['selected'])) {
374: $selected = $this->request->post['selected'];
375: } else {
376: $selected = [];
377: }
378:
379: if (!$this->user->hasPermission('modify', 'localisation/currency')) {
380: $json['error'] = $this->language->get('error_permission');
381: }
382:
383: $this->load->model('localisation/currency');
384: $this->load->model('setting/store');
385: $this->load->model('sale/order');
386:
387: foreach ($selected as $currency_id) {
388: $currency_info = $this->model_localisation_currency->getCurrency($currency_id);
389:
390: if ($currency_info) {
391: if ($this->config->get('config_currency') == $currency_info['code']) {
392: $json['error'] = $this->language->get('error_default');
393: }
394:
395: $store_total = $this->model_setting_store->getTotalStoresByCurrency($currency_info['code']);
396:
397: if ($store_total) {
398: $json['error'] = sprintf($this->language->get('error_store'), $store_total);
399: }
400: }
401:
402: $order_total = $this->model_sale_order->getTotalOrdersByCurrencyId($currency_id);
403:
404: if ($order_total) {
405: $json['error'] = sprintf($this->language->get('error_order'), $order_total);
406: }
407: }
408:
409: if (!$json) {
410: foreach ($selected as $currency_id) {
411: $this->model_localisation_currency->deleteCurrency($currency_id);
412: }
413:
414: $json['success'] = $this->language->get('text_success');
415: }
416:
417: $this->response->addHeader('Content-Type: application/json');
418: $this->response->setOutput(json_encode($json));
419: }
420: }
421: