Skip to content

Commit

Permalink
Added currency changes from 3.0.x.x
Browse files Browse the repository at this point in the history
  • Loading branch information
condor2 committed May 29, 2024
1 parent bf5964b commit 0cb84a3
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 124 deletions.
9 changes: 9 additions & 0 deletions upload/admin/controller/common/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public function index(): void {
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');

// Run currency update
if ($this->config->get('config_currency_auto')) {
$config_currency_engine = $this->config->get('config_currency_engine');

if ($config_currency_engine) {
$this->load->controller('extension/currency/' . $config_currency_engine . '/currency');
}
}

$this->response->setOutput($this->load->view('common/dashboard', $data));
}
}
67 changes: 7 additions & 60 deletions upload/admin/controller/extension/currency/ecb.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public function index(): void {

if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->model_setting_setting->editSetting('ecb', $this->request->post);

$this->session->data['success'] = $this->language->get('text_success');

$this->response->redirect($this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=currency', true));
}

Expand Down Expand Up @@ -97,11 +99,14 @@ protected function validate() {
} else {
if (!empty($this->request->post['ecb_status'])) {
$this->load->model('localisation/currency');

$euro_currency = $this->model_localisation_currency->getCurrencyByCode('EUR');

if (empty($euro_currency)) {
$this->error['warning'] = $this->language->get('error_euro');
}
}

if (!empty($this->request->post['ecb_ip'])) {
if (!filter_var($this->request->post['ecb_ip'], FILTER_VALIDATE_IP)) {
$this->error['ip'] = $this->language->get('error_ip');
Expand All @@ -113,66 +118,8 @@ protected function validate() {
}

public function currency() {
if ($this->config->get('ecb_status')) {
if ($this->config->get('config_currency_engine') == 'ecb') {
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);

$response = curl_exec($curl);

curl_close($curl);

if ($response) {
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->loadXml($response);

$cube = $dom->getElementsByTagName('Cube')->item(0);

$currencies = [];

$currencies['EUR'] = 1.0000;

foreach ($cube->getElementsByTagName('Cube') as $currency) {
if ($currency->getAttribute('currency')) {
$currencies[$currency->getAttribute('currency')] = $currency->getAttribute('rate');
}
}

if (count($currencies) > 1) {
$this->load->model('localisation/currency');
$this->load->model('extension/currency/ecb');

$default = $this->config->get('config_currency');

$results = $this->model_localisation_currency->getCurrencies();

foreach ($results as $result) {
if (isset($currencies[$result['code']])) {
$from = $currencies['EUR'];

$to = $currencies[$result['code']];

$this->model_extension_currency_ecb->editValueByCode($result['code'], 1 / ($currencies[$default] * ($from / $to)));
}
}
}

$default = $this->config->get('config_currency');

$this->model_extension_currency_ecb->editValueByCode($default, '1.00000');

$this->cache->delete('currency');
}

return true;
}
}
$this->load->model('extension/currency/ecb');
$this->model_extension_currency_ecb->refresh();

return null;
}
Expand Down
5 changes: 1 addition & 4 deletions upload/admin/controller/localisation/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ public function refresh(): void {
$this->load->model('localisation/currency');

if ($this->validateRefresh()) {
$config_currency_engine = $this->config->get('config_currency_engine');
$this->load->controller('extension/currency/' . $config_currency_engine . '/currency');
$this->model_localisation_currency->refresh();

$this->session->data['success'] = $this->language->get('text_success');

Expand All @@ -136,8 +135,6 @@ public function refresh(): void {
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}

$this->response->redirect($this->url->link('localisation/currency', 'token=' . $this->session->data['token'] . $url, true));
}

$this->getList();
Expand Down
59 changes: 59 additions & 0 deletions upload/admin/model/extension/currency/ecb.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,65 @@
class ModelExtensionCurrencyEcb extends Model {
public function editValueByCode($code, $value) {
$this->db->query("UPDATE `" . DB_PREFIX . "currency` SET `value` = '" . (float)$value . "', `date_modified` = NOW() WHERE `code` = '" . $this->db->escape((string)$code) . "'");

$this->cache->delete('currency');
}

public function refresh() {
if ($this->config->get('currency_ecb_status')) {
if ($this->config->get('config_currency_engine') == 'ecb') {
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);

$response = curl_exec($curl);

curl_close($curl);

if ($response) {
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->loadXml($response);

$cube = $dom->getElementsByTagName('Cube')->item(0);

$currencies = [];

$currencies['EUR'] = 1.0000;

foreach ($cube->getElementsByTagName('Cube') as $currency) {
if ($currency->getAttribute('currency')) {
$currencies[$currency->getAttribute('currency')] = $currency->getAttribute('rate');
}
}

if (count($currencies) > 1) {
$this->load->model('localisation/currency');

$default = $this->config->get('config_currency');

$results = $this->model_localisation_currency->getCurrencies();

foreach ($results as $result) {
if (isset($currencies[$result['code']])) {
$from = $currencies['EUR'];

$to = $currencies[$result['code']];

$this->editValueByCode($result['code'], 1 / ($currencies[$default] * ($from / $to)));
}
}
}

$this->editValueByCode($default, '1.00000');

Check failure on line 59 in upload/admin/model/extension/currency/ecb.php

View workflow job for this annotation

GitHub Actions / tests (7.4)

Variable $default might not be defined.

Check failure on line 59 in upload/admin/model/extension/currency/ecb.php

View workflow job for this annotation

GitHub Actions / tests (8.0)

Variable $default might not be defined.

Check failure on line 59 in upload/admin/model/extension/currency/ecb.php

View workflow job for this annotation

GitHub Actions / tests (8.1)

Variable $default might not be defined.

Check failure on line 59 in upload/admin/model/extension/currency/ecb.php

View workflow job for this annotation

GitHub Actions / tests (8.3)

Variable $default might not be defined.
}
return true;
}
}
return false;
}
}
26 changes: 18 additions & 8 deletions upload/admin/model/localisation/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ class ModelLocalisationCurrency extends Model {
public function addCurrency($data) {
$this->db->query("INSERT INTO " . DB_PREFIX . "currency SET title = '" . $this->db->escape((string)$data['title']) . "', code = '" . $this->db->escape((string)$data['code']) . "', symbol_left = '" . $this->db->escape((string)$data['symbol_left']) . "', symbol_right = '" . $this->db->escape((string)$data['symbol_right']) . "', decimal_place = '" . (int)$data['decimal_place'] . "', value = '" . (float)$data['value'] . "', status = '" . (bool)($data['status'] ?? 0) . "', date_modified = NOW()");

$this->cache->delete('currency');

return $this->db->getLastId();
}
$currency_id = $this->db->getLastId();

public function editCurrency($currency_id, $data) {
$this->db->query("UPDATE " . DB_PREFIX . "currency SET title = '" . $this->db->escape((string)$data['title']) . "', code = '" . $this->db->escape((string)$data['code']) . "', symbol_left = '" . $this->db->escape((string)$data['symbol_left']) . "', symbol_right = '" . $this->db->escape((string)$data['symbol_right']) . "', decimal_place = '" . (int)$data['decimal_place'] . "', value = '" . (float)$data['value'] . "', status = '" . (bool)($data['status'] ?? 0) . "', date_modified = NOW() WHERE currency_id = '" . (int)$currency_id . "'");
if ($this->config->get('config_currency_auto')) {
$this->refresh();
}

$this->cache->delete('currency');

return $currency_id;
}

public function editValueByCode($code, $value) {
$this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$value . "', date_modified = NOW() WHERE code = '" . $this->db->escape($code) . "'");
public function editCurrency($currency_id, $data) {
$this->db->query("UPDATE " . DB_PREFIX . "currency SET title = '" . $this->db->escape((string)$data['title']) . "', code = '" . $this->db->escape((string)$data['code']) . "', symbol_left = '" . $this->db->escape((string)$data['symbol_left']) . "', symbol_right = '" . $this->db->escape((string)$data['symbol_right']) . "', decimal_place = '" . (int)$data['decimal_place'] . "', value = '" . (float)$data['value'] . "', status = '" . (bool)($data['status'] ?? 0) . "', date_modified = NOW() WHERE currency_id = '" . (int)$currency_id . "'");

$this->cache->delete('currency');
}
Expand Down Expand Up @@ -101,6 +101,16 @@ public function getCurrencies(array $data = []) {
return $currency_data;
}

public function refresh() {
$config_currency_engine = $this->config->get('config_currency_engine');

if ($config_currency_engine) {
$this->load->model('extension/currency/'.$config_currency_engine);

$this->{'model_extension_currency_' . $config_currency_engine}->refresh();
}
}

public function getTotalCurrencies() {
$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "currency");

Expand Down
55 changes: 3 additions & 52 deletions upload/catalog/controller/extension/currency/ecb.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function refresh() {
if (!$config_currency_engine) {
return false;
}

if ($config_currency_engine != 'ecb') {
return false;
}
Expand All @@ -21,58 +22,8 @@ public function refresh() {
}
}

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);

$response = curl_exec($curl);

curl_close($curl);

if ($response) {
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->loadXml($response);

$cube = $dom->getElementsByTagName('Cube')->item(0);

$currencies = [];

$currencies['EUR'] = 1.0000;

foreach ($cube->getElementsByTagName('Cube') as $currency) {
if ($currency->getAttribute('currency')) {
$currencies[$currency->getAttribute('currency')] = $currency->getAttribute('rate');
}
}

if ($currencies) {
$this->load->model('localisation/currency');
$this->load->model('extension/currency/ecb');

$default = $this->config->get('config_currency');

$results = $this->model_localisation_currency->getCurrencies();

foreach ($results as $result) {
if (isset($currencies[$result['code']])) {
$from = $currencies['EUR'];

$to = $currencies[$result['code']];

$this->model_extension_currency_ecb->editValueByCode($result['code'], 1 / ($currencies[$default] * ($from / $to)));
}
}
}

$this->model_extension_currency_ecb->editValueByCode($default, '1.00000');

$this->cache->delete('currency');
}
$this->load->model('extension/currency/ecb');
$this->model_extension_currency_ecb->refresh();

return true;
}
Expand Down
56 changes: 56 additions & 0 deletions upload/catalog/model/extension/currency/ecb.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,62 @@
class ModelExtensionCurrencyEcb extends Model {
public function editValueByCode(string $code, float $value): void {
$this->db->query("UPDATE `" . DB_PREFIX . "currency` SET `value` = '" . (float)$value . "', `date_modified` = NOW() WHERE `code` = '" . $this->db->escape((string)$code) . "'");

$this->cache->delete('currency');
}

public function refresh() {
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);

$response = curl_exec($curl);

curl_close($curl);

if ($response) {
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->loadXml($response);

$cube = $dom->getElementsByTagName('Cube')->item(0);

$currencies = [];

$currencies['EUR'] = 1.0000;

foreach ($cube->getElementsByTagName('Cube') as $currency) {
if ($currency->getAttribute('currency')) {
$currencies[$currency->getAttribute('currency')] = $currency->getAttribute('rate');
}
}

if (count($currencies) > 1) {
$this->load->model('localisation/currency');
$this->load->model('extension/currency/ecb');

$default = $this->config->get('config_currency');

$results = $this->model_localisation_currency->getCurrencies();

foreach ($results as $result) {
if (isset($currencies[$result['code']])) {
$from = $currencies['EUR'];

$to = $currencies[$result['code']];

$this->editValueByCode($result['code'], 1 / ($currencies[$default] * ($from / $to)));
}
}
}

$this->editValueByCode($default, '1.00000');

Check failure on line 58 in upload/catalog/model/extension/currency/ecb.php

View workflow job for this annotation

GitHub Actions / tests (7.4)

Variable $default might not be defined.

Check failure on line 58 in upload/catalog/model/extension/currency/ecb.php

View workflow job for this annotation

GitHub Actions / tests (8.0)

Variable $default might not be defined.

Check failure on line 58 in upload/catalog/model/extension/currency/ecb.php

View workflow job for this annotation

GitHub Actions / tests (8.1)

Variable $default might not be defined.

Check failure on line 58 in upload/catalog/model/extension/currency/ecb.php

View workflow job for this annotation

GitHub Actions / tests (8.3)

Variable $default might not be defined.

$this->cache->delete('currency');
}
}
}

0 comments on commit 0cb84a3

Please sign in to comment.