/upload/extension/ocStore/admin/controller/currency/ecb.php

https://github.com/ocStore/ocStore · PHP · 132 lines · 94 code · 38 blank · 0 comment · 13 complexity · b8c918cc4acab9fec93e5395fc5da6f8 MD5 · raw file

  1. <?php
  2. class ControllerExtensionCurrencyEcb extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('extension/currency/ecb');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('setting/setting');
  8. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
  9. $this->model_setting_setting->editSetting('currency_ecb', $this->request->post);
  10. $this->session->data['success'] = $this->language->get('text_success');
  11. $this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=currency'));
  12. }
  13. $data['heading_title'] = $this->language->get('heading_title');
  14. $data['text_edit'] = $this->language->get('text_edit');
  15. $data['text_enabled'] = $this->language->get('text_enabled');
  16. $data['text_disabled'] = $this->language->get('text_disabled');
  17. $data['entry_status'] = $this->language->get('entry_status');
  18. $data['button_save'] = $this->language->get('button_save');
  19. $data['button_cancel'] = $this->language->get('button_cancel');
  20. if (isset($this->error['warning'])) {
  21. $data['error_warning'] = $this->error['warning'];
  22. } else {
  23. $data['error_warning'] = '';
  24. }
  25. $data['breadcrumbs'] = [];
  26. $data['breadcrumbs'][] = [
  27. 'text' => $this->language->get('text_home'),
  28. 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
  29. ];
  30. $data['breadcrumbs'][] = [
  31. 'text' => $this->language->get('text_extension'),
  32. 'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=currency')
  33. ];
  34. $data['breadcrumbs'][] = [
  35. 'text' => $this->language->get('heading_title'),
  36. 'href' => $this->url->link('extension/currency/ecb', 'user_token=' . $this->session->data['user_token'])
  37. ];
  38. $data['action'] = $this->url->link('extension/currency/ecb', 'user_token=' . $this->session->data['user_token']);
  39. $data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=currency');
  40. if (isset($this->request->post['currency_ecb_status'])) {
  41. $data['currency_ecb_status'] = $this->request->post['currency_ecb_status'];
  42. } else {
  43. $data['currency_ecb_status'] = $this->config->get('currency_ecb_status');
  44. }
  45. $data['header'] = $this->load->controller('common/header');
  46. $data['column_left'] = $this->load->controller('common/column_left');
  47. $data['footer'] = $this->load->controller('common/footer');
  48. $this->response->setOutput($this->load->view('extension/currency/ecb', $data));
  49. }
  50. protected function validate() {
  51. if (!$this->user->hasPermission('modify', 'extension/currency/ecb')) {
  52. $this->error['warning'] = $this->language->get('error_permission');
  53. }
  54. return !$this->error;
  55. }
  56. public function currency($default = '') {
  57. if ($this->config->get('currency_ecb_status')) {
  58. $curl = curl_init();
  59. curl_setopt($curl, CURLOPT_URL, 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
  60. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  61. curl_setopt($curl, CURLOPT_HEADER, false);
  62. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  63. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
  64. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  65. $response = curl_exec($curl);
  66. curl_close($curl);
  67. if ($response) {
  68. $dom = new \DOMDocument('1.0', 'UTF-8');
  69. $dom->loadXml($response);
  70. $cube = $dom->getElementsByTagName('Cube')->item(0);
  71. $currencies = [];
  72. $currencies['EUR'] = 1.0000;
  73. foreach ($cube->getElementsByTagName('Cube') as $currency) {
  74. if ($currency->getAttribute('currency')) {
  75. $currencies[$currency->getAttribute('currency')] = $currency->getAttribute('rate');
  76. }
  77. }
  78. if ($currencies) {
  79. $this->load->model('localisation/currency');
  80. $results = $this->model_localisation_currency->getCurrencies();
  81. foreach ($results as $result) {
  82. if (isset($currencies[$result['code']])) {
  83. $from = $currencies['EUR'];
  84. $to = $currencies[$result['code']];
  85. $this->model_localisation_currency->editValueByCode($result['code'], 1 / ($currencies[$default] * ($from / $to)));
  86. }
  87. }
  88. }
  89. $this->model_localisation_currency->editValueByCode($default, '1.00000');
  90. $this->cache->delete('currency');
  91. }
  92. }
  93. }
  94. }