/admin/model/localisation/currency.php

https://gitlab.com/reclamare/mao · PHP · 152 lines · 114 code · 38 blank · 0 comment · 21 complexity · 125172f17ed94f8711ba279e03f7da7f MD5 · raw file

  1. <?php
  2. class ModelLocalisationCurrency extends Model {
  3. public function addCurrency($data) {
  4. $this->db->query("INSERT INTO " . DB_PREFIX . "currency SET title = '" . $this->db->escape($data['title']) . "', code = '" . $this->db->escape($data['code']) . "', symbol_left = '" . $this->db->escape($data['symbol_left']) . "', symbol_right = '" . $this->db->escape($data['symbol_right']) . "', decimal_place = '" . $this->db->escape($data['decimal_place']) . "', value = '" . $this->db->escape($data['value']) . "', status = '" . (int)$data['status'] . "', date_modified = NOW()");
  5. if ($this->config->get('config_currency_auto')) {
  6. $this->refresh(true);
  7. }
  8. $this->cache->delete('currency');
  9. }
  10. public function editCurrency($currency_id, $data) {
  11. $this->db->query("UPDATE " . DB_PREFIX . "currency SET title = '" . $this->db->escape($data['title']) . "', code = '" . $this->db->escape($data['code']) . "', symbol_left = '" . $this->db->escape($data['symbol_left']) . "', symbol_right = '" . $this->db->escape($data['symbol_right']) . "', decimal_place = '" . $this->db->escape($data['decimal_place']) . "', value = '" . $this->db->escape($data['value']) . "', status = '" . (int)$data['status'] . "', date_modified = NOW() WHERE currency_id = '" . (int)$currency_id . "'");
  12. $this->cache->delete('currency');
  13. }
  14. public function deleteCurrency($currency_id) {
  15. $this->db->query("DELETE FROM " . DB_PREFIX . "currency WHERE currency_id = '" . (int)$currency_id . "'");
  16. $this->cache->delete('currency');
  17. }
  18. public function getCurrency($currency_id) {
  19. $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "currency WHERE currency_id = '" . (int)$currency_id . "'");
  20. return $query->row;
  21. }
  22. public function getCurrencyByCode($currency) {
  23. $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "currency WHERE code = '" . $this->db->escape($currency) . "'");
  24. return $query->row;
  25. }
  26. public function getCurrencies($data = array()) {
  27. if ($data) {
  28. $sql = "SELECT * FROM " . DB_PREFIX . "currency";
  29. $sort_data = array(
  30. 'title',
  31. 'code',
  32. 'value',
  33. 'date_modified'
  34. );
  35. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  36. $sql .= " ORDER BY " . $data['sort'];
  37. } else {
  38. $sql .= " ORDER BY title";
  39. }
  40. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  41. $sql .= " DESC";
  42. } else {
  43. $sql .= " ASC";
  44. }
  45. if (isset($data['start']) || isset($data['limit'])) {
  46. if ($data['start'] < 0) {
  47. $data['start'] = 0;
  48. }
  49. if ($data['limit'] < 1) {
  50. $data['limit'] = 20;
  51. }
  52. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  53. }
  54. $query = $this->db->query($sql);
  55. return $query->rows;
  56. } else {
  57. $currency_data = $this->cache->get('currency');
  58. if (!$currency_data) {
  59. $currency_data = array();
  60. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency ORDER BY title ASC");
  61. foreach ($query->rows as $result) {
  62. $currency_data[$result['code']] = array(
  63. 'currency_id' => $result['currency_id'],
  64. 'title' => $result['title'],
  65. 'code' => $result['code'],
  66. 'symbol_left' => $result['symbol_left'],
  67. 'symbol_right' => $result['symbol_right'],
  68. 'decimal_place' => $result['decimal_place'],
  69. 'value' => $result['value'],
  70. 'status' => $result['status'],
  71. 'date_modified' => $result['date_modified']
  72. );
  73. }
  74. $this->cache->set('currency', $currency_data);
  75. }
  76. return $currency_data;
  77. }
  78. }
  79. public function refresh($force = false) {
  80. if (extension_loaded('curl')) {
  81. $data = array();
  82. if ($force) {
  83. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "'");
  84. } else {
  85. $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'))) . "'");
  86. }
  87. foreach ($query->rows as $result) {
  88. $data[] = $this->config->get('config_currency') . $result['code'] . '=X';
  89. }
  90. $curl = curl_init();
  91. curl_setopt($curl, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=' . implode(',', $data) . '&f=sl1&e=.csv');
  92. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  93. curl_setopt($curl, CURLOPT_HEADER, false);
  94. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
  95. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  96. $content = curl_exec($curl);
  97. curl_close($curl);
  98. $lines = explode("\n", trim($content));
  99. foreach ($lines as $line) {
  100. $currency = utf8_substr($line, 4, 3);
  101. $value = utf8_substr($line, 11, 6);
  102. if ((float)$value) {
  103. $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) . "'");
  104. }
  105. }
  106. $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')) . "'");
  107. $this->cache->delete('currency');
  108. }
  109. }
  110. public function getTotalCurrencies() {
  111. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "currency");
  112. return $query->row['total'];
  113. }
  114. }