1
|
|
|
<?php |
2
|
|
|
/* Divine CMS - Open source CMS for widespread use. |
3
|
|
|
Copyright (c) 2019 Mykola Burakov ([email protected]) |
4
|
|
|
|
5
|
|
|
See SOURCE.txt for other and additional information. |
6
|
|
|
|
7
|
|
|
This file is part of Divine CMS. |
8
|
|
|
|
9
|
|
|
This program is free software: you can redistribute it and/or modify |
10
|
|
|
it under the terms of the GNU General Public License as published by |
11
|
|
|
the Free Software Foundation, either version 3 of the License, or |
12
|
|
|
(at your option) any later version. |
13
|
|
|
|
14
|
|
|
This program is distributed in the hope that it will be useful, |
15
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
GNU General Public License for more details. |
18
|
|
|
|
19
|
|
|
You should have received a copy of the GNU General Public License |
20
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
21
|
|
|
|
22
|
|
|
namespace Divine\Engine\Library; |
23
|
|
|
|
24
|
|
|
class Currency |
25
|
|
|
{ |
26
|
|
|
private $currencies = array(); |
27
|
|
|
|
28
|
|
|
public function __construct($registry) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$this->db = $registry->get('db'); |
|
|
|
|
31
|
|
|
$this->language = $registry->get('language'); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$query = $this->db->query(" |
|
|
|
|
34
|
|
|
SELECT * |
35
|
|
|
FROM currency |
36
|
|
|
"); |
37
|
|
|
|
38
|
|
|
foreach ($query->rows as $result) { |
39
|
|
|
$this->currencies[$result['code']] = array( |
40
|
|
|
'currency_id' => $result['currency_id'], |
41
|
|
|
'title' => $result['title'], |
42
|
|
|
'symbol_left' => $result['symbol_left'], |
43
|
|
|
'symbol_right' => $result['symbol_right'], |
44
|
|
|
'decimal_place' => $result['decimal_place'], |
45
|
|
|
'value' => $result['value'] |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function format($number, $currency, $value = '', $format = true) |
51
|
|
|
{ |
52
|
|
|
$symbol_left = $this->currencies[$currency]['symbol_left']; |
53
|
|
|
$symbol_right = $this->currencies[$currency]['symbol_right']; |
54
|
|
|
$decimal_place = $this->currencies[$currency]['decimal_place']; |
55
|
|
|
|
56
|
|
|
if (!$value) { |
57
|
|
|
$value = $this->currencies[$currency]['value']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$amount = $value ? (float) $number * $value : (float) $number; |
61
|
|
|
|
62
|
|
|
$amount = round($amount, (int) $decimal_place); |
63
|
|
|
|
64
|
|
|
if (!$format) { |
65
|
|
|
return $amount; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$string = ''; |
69
|
|
|
|
70
|
|
|
if ($symbol_left) { |
71
|
|
|
$string .= $symbol_left; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$string .= number_format($amount, (int) $decimal_place, $this->language->get('decimal_point'), $this->language->get('thousand_point')); |
75
|
|
|
|
76
|
|
|
if ($symbol_right) { |
77
|
|
|
$string .= $symbol_right; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $string; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function convert($value, $from, $to) |
84
|
|
|
{ |
85
|
|
|
if (isset($this->currencies[$from])) { |
86
|
|
|
$from = $this->currencies[$from]['value']; |
87
|
|
|
} else { |
88
|
|
|
$from = 1; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (isset($this->currencies[$to])) { |
92
|
|
|
$to = $this->currencies[$to]['value']; |
93
|
|
|
} else { |
94
|
|
|
$to = 1; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $value * ($to / $from); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getId($currency) |
101
|
|
|
{ |
102
|
|
|
if (isset($this->currencies[$currency])) { |
103
|
|
|
return $this->currencies[$currency]['currency_id']; |
104
|
|
|
} else { |
105
|
|
|
return 0; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getSymbolLeft($currency) |
110
|
|
|
{ |
111
|
|
|
if (isset($this->currencies[$currency])) { |
112
|
|
|
return $this->currencies[$currency]['symbol_left']; |
113
|
|
|
} else { |
114
|
|
|
return ''; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getSymbolRight($currency) |
119
|
|
|
{ |
120
|
|
|
if (isset($this->currencies[$currency])) { |
121
|
|
|
return $this->currencies[$currency]['symbol_right']; |
122
|
|
|
} else { |
123
|
|
|
return ''; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getDecimalPlace($currency) |
128
|
|
|
{ |
129
|
|
|
if (isset($this->currencies[$currency])) { |
130
|
|
|
return $this->currencies[$currency]['decimal_place']; |
131
|
|
|
} else { |
132
|
|
|
return 0; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getValue($currency) |
137
|
|
|
{ |
138
|
|
|
if (isset($this->currencies[$currency])) { |
139
|
|
|
return $this->currencies[$currency]['value']; |
140
|
|
|
} else { |
141
|
|
|
return 0; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function has($currency) |
146
|
|
|
{ |
147
|
|
|
return isset($this->currencies[$currency]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|