Skip to content

Commit

Permalink
More updates for currency refresh
Browse files Browse the repository at this point in the history
Move calls to ECB API to model files.
Make sure all calls to model/localisation/currency/refresh are now done to the selected currency engine's API, e.g. ECB.
  • Loading branch information
mhcwebdesign committed May 28, 2024
1 parent 468208c commit e7890ea
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 168 deletions.
60 changes: 2 additions & 58 deletions upload/admin/controller/extension/currency/ecb.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,64 +105,8 @@ public function uninstall() {


public function currency() {
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 ($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');
}
return true;
}
}

$this->load->model('extension/currency/ecb');
$this->model_extension_currency_ecb->refresh();
return null;
}
}
7 changes: 2 additions & 5 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() {
$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() {
if (isset($this->request->get['page'])) {
$url .= '&page=' . $this->request->get['page'];
}

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

$this->getList();
Expand Down Expand Up @@ -471,4 +468,4 @@ protected function validateRefresh() {

return !$this->error;
}
}
}
10 changes: 5 additions & 5 deletions upload/admin/controller/setting/setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public function index() {
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->model_setting_setting->editSetting('config', $this->request->post);

if ($this->config->get('config_currency_auto')) {
$this->load->model('localisation/currency');

$this->model_localisation_currency->refresh();
}
// if ($this->config->get('config_currency_auto')) {
// $this->load->model('localisation/currency');
//
// $this->model_localisation_currency->refresh();
// }

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

Expand Down
58 changes: 58 additions & 0 deletions upload/admin/model/extension/currency/ecb.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,62 @@ 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 ($currencies) {
$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');
}
return true;
}
}
return false;
}
}
54 changes: 6 additions & 48 deletions upload/admin/model/localisation/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public function addCurrency($data) {
$currency_id = $this->db->getLastId();

if ($this->config->get('config_currency_auto')) {
$this->refresh(true);
$this->refresh();
}

$this->cache->delete('currency');
Expand Down Expand Up @@ -99,54 +99,12 @@ public function getCurrencies($data = array()) {
}
}

public function refresh($force = false) {
$currency_data = array();

if ($force) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "'");
} else {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "' AND date_modified < '" . $this->db->escape(date('Y-m-d H:i:s', strtotime('-1 day'))) . "'");
}

foreach ($query->rows as $result) {
$currency_data[] = $this->config->get('config_currency') . $result['code'] . '=X';
$currency_data[] = $result['code'] . $this->config->get('config_currency') . '=X';
}

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=' . implode(',', $currency_data) . '&f=sl1&e=.json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);

$content = curl_exec($curl);

curl_close($curl);

$line = explode("\n", trim($content));

for ($i = 0; $i < count($line); $i = $i + 2) {
$currency = utf8_substr($line[$i], 4, 3);
$value = utf8_substr($line[$i], 11, 6);

if ((float)$value < 1 && isset($line[$i + 1])) {
if ((float)utf8_substr($line[$i + 1], 11, 6) > 0) {
$value = (1 / (float)utf8_substr($line[$i + 1], 11, 6));
} else {
$value = 0;
}
}

if ((float)$value) {
$this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '" . (float)$value . "', date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' WHERE code = '" . $this->db->escape($currency) . "'");
}
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();
}

$this->db->query("UPDATE " . DB_PREFIX . "currency SET value = '1.00000', date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' WHERE code = '" . $this->db->escape($this->config->get('config_currency')) . "'");

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

public function getTotalCurrencies() {
Expand Down
56 changes: 4 additions & 52 deletions upload/catalog/controller/extension/currency/ecb.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ public function refresh() {
}

$config_currency_engine = $this->config->get('config_currency_engine');

if (!$config_currency_engine) {
return false;
}

if ($config_currency_engine != 'ecb') {
return false;
}
Expand All @@ -22,58 +24,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
55 changes: 55 additions & 0 deletions upload/catalog/model/extension/currency/ecb.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,59 @@ 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() {
$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');
}
}
}

0 comments on commit e7890ea

Please sign in to comment.