1: <?php
2: namespace Opencart\Admin\Controller\Extension\Opencart\Currency;
3: /**
4: * Class Fixer
5: *
6: * @package Opencart\Admin\Controller\Extension\Opencart\Currency
7: */
8: class Fixer 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/fixer');
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/fixer', 'user_token=' . $this->session->data['user_token'])
34: ];
35:
36: $data['save'] = $this->url->link('extension/opencart/currency/fixer.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_fixer_api'] = $this->config->get('currency_fixer_api');
40: $data['currency_fixer_status'] = $this->config->get('currency_fixer_status');
41:
42: $data['header'] = $this->load->controller('common/header');
43: $data['column_left'] = $this->load->controller('common/column_left');
44: $data['footer'] = $this->load->controller('common/footer');
45:
46: $this->response->setOutput($this->load->view('extension/opencart/currency/fixer', $data));
47: }
48:
49: /**
50: * Save
51: *
52: * @return void
53: */
54: public function save(): void {
55: $this->load->language('extension/opencart/currency/fixer');
56:
57: $json = [];
58:
59: if (!$this->user->hasPermission('modify', 'extension/opencart/currency/fixer')) {
60: $json['error']['warning'] = $this->language->get('error_permission');
61: }
62:
63: if (!$this->request->post['currency_fixer_api']) {
64: $json['error']['api'] = $this->language->get('error_api');
65: }
66:
67: if (!$json) {
68: $this->load->model('setting/setting');
69:
70: $this->model_setting_setting->editSetting('currency_fixer', $this->request->post);
71:
72: $json['success'] = $this->language->get('text_success');
73: }
74:
75: $this->response->addHeader('Content-Type: application/json');
76: $this->response->setOutput(json_encode($json));
77: }
78:
79: /**
80: * Currency
81: *
82: * @param string $default
83: *
84: * @return void
85: */
86: public function currency(string $default = ''): void {
87: if ($this->config->get('currency_fixer_status')) {
88: $curl = curl_init();
89:
90: curl_setopt($curl, CURLOPT_URL, 'http://data.fixer.io/api/latest?access_key=' . $this->config->get('currency_fixer_api'));
91: curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
92: curl_setopt($curl, CURLOPT_HEADER, false);
93: curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
94: curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
95: curl_setopt($curl, CURLOPT_TIMEOUT, 30);
96:
97: $response = curl_exec($curl);
98:
99: curl_close($curl);
100:
101: $response_info = json_decode($response, true);
102:
103: if (is_array($response_info) && isset($response_info['rates'])) {
104: // Compile all the rates into an array
105: $currencies = [];
106:
107: $currencies['EUR'] = 1.0000;
108:
109: foreach ($response_info['rates'] as $key => $value) {
110: $currencies[$key] = $value;
111: }
112:
113: $this->load->model('localisation/currency');
114:
115: $results = $this->model_localisation_currency->getCurrencies();
116:
117: foreach ($results as $result) {
118: if (isset($currencies[$result['code']])) {
119: $from = $currencies['EUR'];
120:
121: $to = $currencies[$result['code']];
122:
123: $this->model_localisation_currency->editValueByCode($result['code'], 1 / ($currencies[$default] * ($from / $to)));
124: }
125: }
126:
127: $this->model_localisation_currency->editValueByCode($default, 1);
128:
129: $this->cache->delete('currency');
130: }
131: }
132: }
133: }
134: