1: <?php
2: namespace Opencart\Catalog\Controller\Common;
3: /**
4: * Class Currency
5: *
6: * @package Opencart\Catalog\Controller\Common
7: */
8: class Currency extends \Opencart\System\Engine\Controller {
9: /**
10: * @return string
11: */
12: public function index(): string {
13: $this->load->language('common/currency');
14:
15: $data['action'] = $this->url->link('common/currency.save', 'language=' . $this->config->get('config_language'));
16:
17: $data['code'] = $this->session->data['currency'];
18:
19: $url_data = $this->request->get;
20:
21: if (isset($url_data['route'])) {
22: $route = $url_data['route'];
23: } else {
24: $route = $this->config->get('action_default');
25: }
26:
27: unset($url_data['route']);
28: unset($url_data['_route_']);
29:
30: $data['currencies'] = [];
31:
32: $this->load->model('localisation/currency');
33:
34: $results = $this->model_localisation_currency->getCurrencies();
35:
36: foreach ($results as $result) {
37: if ($result['status']) {
38: $data['currencies'][] = [
39: 'title' => $result['title'],
40: 'code' => $result['code'],
41: 'symbol_left' => $result['symbol_left'],
42: 'symbol_right' => $result['symbol_right']
43: ];
44: }
45: }
46:
47: $url = '';
48:
49: if ($url_data) {
50: $url .= '&' . urldecode(http_build_query($url_data, '', '&'));
51: }
52:
53: $data['redirect'] = $this->url->link($route, $url);
54:
55: return $this->load->view('common/currency', $data);
56: }
57:
58: /**
59: * Save
60: *
61: * @return void
62: */
63: public function save(): void {
64: if (isset($this->request->post['code'])) {
65: $this->session->data['currency'] = $this->request->post['code'];
66:
67: unset($this->session->data['shipping_method']);
68: unset($this->session->data['shipping_methods']);
69: }
70:
71: $option = [
72: 'expires' => time() + 60 * 60 * 24 * 30,
73: 'path' => '/',
74: 'SameSite' => 'Lax'
75: ];
76:
77: setcookie('currency', $this->session->data['currency'], $option);
78:
79: if (isset($this->request->post['redirect'])) {
80: $redirect = urldecode(html_entity_decode($this->request->post['redirect'], ENT_QUOTES, 'UTF-8'));
81: } else {
82: $redirect = '';
83: }
84:
85: if ($redirect && str_starts_with($redirect, $this->config->get('config_url'))) {
86: $this->response->redirect($redirect);
87: } else {
88: $this->response->redirect($this->url->link($this->config->get('action_default'), 'language=' . $this->config->get('config_language')));
89: }
90: }
91: }
92: