/upload/system/library/currency.php

https://bitbucket.org/mjalajel/opencart · PHP · 175 lines · 147 code · 28 blank · 0 comment · 45 complexity · 44d17e40cbed463a2f4fb75b54372603 MD5 · raw file

  1. <?php
  2. class Currency {
  3. private $code;
  4. private $currencies = array();
  5. public function __construct($registry) {
  6. $this->config = $registry->get('config');
  7. $this->db = $registry->get('db');
  8. $this->language = $registry->get('language');
  9. $this->request = $registry->get('request');
  10. $this->session = $registry->get('session');
  11. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "currency");
  12. foreach ($query->rows as $result) {
  13. $this->currencies[$result['code']] = array(
  14. 'currency_id' => $result['currency_id'],
  15. 'title' => $result['title'],
  16. 'symbol_left' => $result['symbol_left'],
  17. 'symbol_right' => $result['symbol_right'],
  18. 'decimal_place' => $result['decimal_place'],
  19. 'value' => $result['value']
  20. );
  21. }
  22. if (isset($this->request->get['currency']) && (array_key_exists($this->request->get['currency'], $this->currencies))) {
  23. $this->set($this->request->get['currency']);
  24. } elseif ((isset($this->session->data['currency'])) && (array_key_exists($this->session->data['currency'], $this->currencies))) {
  25. $this->set($this->session->data['currency']);
  26. } elseif ((isset($this->request->cookie['currency'])) && (array_key_exists($this->request->cookie['currency'], $this->currencies))) {
  27. $this->set($this->request->cookie['currency']);
  28. } else {
  29. $this->set($this->config->get('config_currency'));
  30. }
  31. }
  32. public function set($currency) {
  33. $this->code = $currency;
  34. if (!isset($this->session->data['currency']) || ($this->session->data['currency'] != $currency)) {
  35. $this->session->data['currency'] = $currency;
  36. }
  37. if (!isset($this->request->cookie['currency']) || ($this->request->cookie['currency'] != $currency)) {
  38. setcookie('currency', $currency, time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']);
  39. }
  40. }
  41. public function format($number, $currency = '', $value = '', $format = true) {
  42. if ($currency && $this->has($currency)) {
  43. $symbol_left = $this->currencies[$currency]['symbol_left'];
  44. $symbol_right = $this->currencies[$currency]['symbol_right'];
  45. $decimal_place = $this->currencies[$currency]['decimal_place'];
  46. } else {
  47. $symbol_left = $this->currencies[$this->code]['symbol_left'];
  48. $symbol_right = $this->currencies[$this->code]['symbol_right'];
  49. $decimal_place = $this->currencies[$this->code]['decimal_place'];
  50. $currency = $this->code;
  51. }
  52. if ($value) {
  53. $value = $value;
  54. } else {
  55. $value = $this->currencies[$currency]['value'];
  56. }
  57. if ($value) {
  58. $value = (float)$number * $value;
  59. } else {
  60. $value = $number;
  61. }
  62. $string = '';
  63. if (($symbol_left) && ($format)) {
  64. $string .= $symbol_left;
  65. }
  66. if ($format) {
  67. $decimal_point = $this->language->get('decimal_point');
  68. } else {
  69. $decimal_point = '.';
  70. }
  71. if ($format) {
  72. $thousand_point = $this->language->get('thousand_point');
  73. } else {
  74. $thousand_point = '';
  75. }
  76. $string .= number_format(round($value, (int)$decimal_place), (int)$decimal_place, $decimal_point, $thousand_point);
  77. if (($symbol_right) && ($format)) {
  78. $string .= $symbol_right;
  79. }
  80. return $string;
  81. }
  82. public function convert($value, $from, $to) {
  83. if (isset($this->currencies[$from])) {
  84. $from = $this->currencies[$from]['value'];
  85. } else {
  86. $from = 0;
  87. }
  88. if (isset($this->currencies[$to])) {
  89. $to = $this->currencies[$to]['value'];
  90. } else {
  91. $to = 0;
  92. }
  93. return $value * ($to / $from);
  94. }
  95. public function getId($currency = '') {
  96. if (!$currency) {
  97. return $this->currencies[$this->code]['currency_id'];
  98. } elseif ($currency && isset($this->currencies[$currency])) {
  99. return $this->currencies[$currency]['currency_id'];
  100. } else {
  101. return 0;
  102. }
  103. }
  104. public function getSymbolLeft($currency = '') {
  105. if (!$currency) {
  106. return $this->currencies[$this->code]['symbol_left'];
  107. } elseif ($currency && isset($this->currencies[$currency])) {
  108. return $this->currencies[$currency]['symbol_left'];
  109. } else {
  110. return '';
  111. }
  112. }
  113. public function getSymbolRight($currency = '') {
  114. if (!$currency) {
  115. return $this->currencies[$this->code]['symbol_right'];
  116. } elseif ($currency && isset($this->currencies[$currency])) {
  117. return $this->currencies[$currency]['symbol_right'];
  118. } else {
  119. return '';
  120. }
  121. }
  122. public function getDecimalPlace($currency = '') {
  123. if (!$currency) {
  124. return $this->currencies[$this->code]['decimal_place'];
  125. } elseif ($currency && isset($this->currencies[$currency])) {
  126. return $this->currencies[$currency]['decimal_place'];
  127. } else {
  128. return 0;
  129. }
  130. }
  131. public function getCode() {
  132. return $this->code;
  133. }
  134. public function getValue($currency = '') {
  135. if (!$currency) {
  136. return $this->currencies[$this->code]['value'];
  137. } elseif ($currency && isset($this->currencies[$currency])) {
  138. return $this->currencies[$currency]['value'];
  139. } else {
  140. return 0;
  141. }
  142. }
  143. public function has($currency) {
  144. return isset($this->currencies[$currency]);
  145. }
  146. }
  147. ?>