1: | <?php
2: | namespace Opencart\Admin\Controller\Extension\Opencart\Currency;
3: | /**
4: | * Class ECB
5: | *
6: | * @package Opencart\Admin\Controller\Extension\Opencart\Currency
7: | */
8: | class ECB extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->load->language('extension/opencart/currency/ecb');
16: |
17: | $this->document->setTitle($this->language->get('heading_title'));
18: |
19: | $data['breadcrumbs'] = [];
20: |
21: | $data['breadcrumbs'][] = [
22: | 'text' => $this->language->get('text_home'),
23: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
24: | ];
25: |
26: | $data['breadcrumbs'][] = [
27: | 'text' => $this->language->get('text_extension'),
28: | 'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=currency')
29: | ];
30: |
31: | $data['breadcrumbs'][] = [
32: | 'text' => $this->language->get('heading_title'),
33: | 'href' => $this->url->link('extension/opencart/currency/ecb', 'user_token=' . $this->session->data['user_token'])
34: | ];
35: |
36: | $data['save'] = $this->url->link('extension/opencart/currency/ecb.save', 'user_token=' . $this->session->data['user_token']);
37: | $data['back'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=currency');
38: |
39: | $data['currency_ecb_status'] = $this->config->get('currency_ecb_status');
40: |
41: | $data['header'] = $this->load->controller('common/header');
42: | $data['column_left'] = $this->load->controller('common/column_left');
43: | $data['footer'] = $this->load->controller('common/footer');
44: |
45: | $this->response->setOutput($this->load->view('extension/opencart/currency/ecb', $data));
46: | }
47: |
48: | /**
49: | * Save
50: | *
51: | * @return void
52: | */
53: | public function save(): void {
54: | $this->load->language('extension/opencart/currency/ecb');
55: |
56: | $json = [];
57: |
58: | if (!$this->user->hasPermission('modify', 'extension/opencart/currency/ecb')) {
59: | $json['error'] = $this->language->get('error_permission');
60: | }
61: |
62: | if (!$json) {
63: | $this->load->model('setting/setting');
64: |
65: | $this->model_setting_setting->editSetting('currency_ecb', $this->request->post);
66: |
67: | $json['success'] = $this->language->get('text_success');
68: | }
69: |
70: | $this->response->addHeader('Content-Type: application/json');
71: | $this->response->setOutput(json_encode($json));
72: | }
73: |
74: | /**
75: | * Currency
76: | *
77: | * @param string $default
78: | *
79: | * @return void
80: | */
81: | public function currency(string $default = ''): void {
82: | if ($this->config->get('currency_ecb_status')) {
83: | $curl = curl_init();
84: |
85: | curl_setopt($curl, CURLOPT_URL, 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
86: | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
87: | curl_setopt($curl, CURLOPT_HEADER, false);
88: | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
89: | curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
90: | curl_setopt($curl, CURLOPT_TIMEOUT, 30);
91: |
92: | $response = curl_exec($curl);
93: |
94: | $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
95: |
96: | curl_close($curl);
97: |
98: | if ($status == 200) {
99: | $dom = new \DOMDocument('1.0', 'UTF-8');
100: | $dom->loadXml($response);
101: |
102: | $cube = $dom->getElementsByTagName('Cube')->item(0);
103: |
104: | // Compile all the rates into an array
105: | $currencies = [];
106: |
107: | $currencies['EUR'] = 1.0000;
108: |
109: | foreach ($cube->getElementsByTagName('Cube') as $currency) {
110: | $currencies[$currency->getAttribute('currency')] = $currency->getAttribute('rate');
111: | }
112: |
113: | if (isset($currencies[$default])) {
114: | $value = $currencies[$default];
115: | } else {
116: | $value = $currencies['EUR'];
117: | }
118: |
119: | if (count($currencies) > 1) {
120: | $this->load->model('localisation/currency');
121: |
122: | $results = $this->model_localisation_currency->getCurrencies();
123: |
124: | foreach ($results as $result) {
125: | if (isset($currencies[$result['code']])) {
126: | $from = $currencies['EUR'];
127: | $to = $currencies[$result['code']];
128: |
129: | $this->model_localisation_currency->editValueByCode($result['code'], 1 / ($value * ($from / $to)));
130: | }
131: | }
132: |
133: | $this->model_localisation_currency->editValueByCode($default, 1.00000);
134: | }
135: | }
136: | }
137: |
138: | $this->cache->delete('currency');
139: | }
140: | }
141: |

OpenCart API API documentation generated by ApiGen dev-master