|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Divine CMS - Open source CMS for widespread use. |
|
4
|
|
|
Copyright (c) 2019 Mykola Burakov ([email protected]) |
|
5
|
|
|
|
|
6
|
|
|
See SOURCE.txt for other and additional information. |
|
7
|
|
|
|
|
8
|
|
|
This file is part of Divine CMS. |
|
9
|
|
|
|
|
10
|
|
|
This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
it under the terms of the GNU General Public License as published by |
|
12
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
13
|
|
|
(at your option) any later version. |
|
14
|
|
|
|
|
15
|
|
|
This program is distributed in the hope that it will be useful, |
|
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
GNU General Public License for more details. |
|
19
|
|
|
|
|
20
|
|
|
You should have received a copy of the GNU General Public License |
|
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
22
|
|
|
|
|
23
|
|
|
class ModelLocalisationCurrency extends \Divine\Engine\Core\Model |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
public function addCurrency($data) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$this->db->query(" |
|
28
|
|
|
INSERT INTO currency |
|
29
|
|
|
SET title = '" . $this->db->escape($data['title']) . "', |
|
30
|
|
|
code = '" . $this->db->escape($data['code']) . "', |
|
31
|
|
|
symbol_left = '" . $this->db->escape($data['symbol_left']) . "', |
|
32
|
|
|
symbol_right = '" . $this->db->escape($data['symbol_right']) . "', |
|
33
|
|
|
decimal_place = '" . $this->db->escape($data['decimal_place']) . "', |
|
34
|
|
|
value = '" . $this->db->escape($data['value']) . "', |
|
35
|
|
|
status = '" . (int)$data['status'] . "', |
|
36
|
|
|
date_modified = NOW() |
|
37
|
|
|
"); |
|
38
|
|
|
|
|
39
|
|
|
$currency_id = $this->db->getLastId(); |
|
40
|
|
|
|
|
41
|
|
|
if ($this->config->get('config_currency_auto')) { |
|
42
|
|
|
$this->refresh(true); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->cache->delete('currency'); |
|
46
|
|
|
|
|
47
|
|
|
return $currency_id; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function editCurrency($currency_id, $data) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->db->query(" |
|
53
|
|
|
UPDATE currency |
|
54
|
|
|
SET title = '" . $this->db->escape($data['title']) . "', |
|
55
|
|
|
code = '" . $this->db->escape($data['code']) . "', |
|
56
|
|
|
symbol_left = '" . $this->db->escape($data['symbol_left']) . "', |
|
57
|
|
|
symbol_right = '" . $this->db->escape($data['symbol_right']) . "', |
|
58
|
|
|
decimal_place = '" . $this->db->escape($data['decimal_place']) . "', |
|
59
|
|
|
value = '" . $this->db->escape($data['value']) . "', |
|
60
|
|
|
status = '" . (int)$data['status'] . "', |
|
61
|
|
|
date_modified = NOW() |
|
62
|
|
|
WHERE currency_id = '" . (int)$currency_id . "' |
|
63
|
|
|
"); |
|
64
|
|
|
|
|
65
|
|
|
$this->cache->delete('currency'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function deleteCurrency($currency_id) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->db->query(" |
|
71
|
|
|
DELETE |
|
72
|
|
|
FROM currency |
|
73
|
|
|
WHERE currency_id = '" . (int)$currency_id . "' |
|
74
|
|
|
"); |
|
75
|
|
|
|
|
76
|
|
|
$this->cache->delete('currency'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getCurrency($currency_id) |
|
80
|
|
|
{ |
|
81
|
|
|
$query = $this->db->query(" |
|
82
|
|
|
SELECT DISTINCT * |
|
83
|
|
|
FROM currency |
|
84
|
|
|
WHERE currency_id = '" . (int)$currency_id . "' |
|
85
|
|
|
"); |
|
86
|
|
|
|
|
87
|
|
|
return $query->row; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getCurrencyByCode($currency) |
|
91
|
|
|
{ |
|
92
|
|
|
$query = $this->db->query(" |
|
93
|
|
|
SELECT DISTINCT * |
|
94
|
|
|
FROM currency |
|
95
|
|
|
WHERE code = '" . $this->db->escape($currency) . "' |
|
96
|
|
|
"); |
|
97
|
|
|
|
|
98
|
|
|
return $query->row; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getCurrencies($data = array()) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($data) { |
|
104
|
|
|
$sql = " |
|
|
|
|
|
|
105
|
|
|
SELECT * |
|
106
|
|
|
FROM currency |
|
107
|
|
|
"; |
|
108
|
|
|
|
|
109
|
|
|
$sort_data = array( |
|
110
|
|
|
'title', |
|
111
|
|
|
'code', |
|
112
|
|
|
'value', |
|
113
|
|
|
'date_modified' |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { |
|
117
|
|
|
$sql .= " ORDER BY " . $data['sort']; |
|
|
|
|
|
|
118
|
|
|
} else { |
|
119
|
|
|
$sql .= " ORDER BY title"; |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if (isset($data['order']) && ($data['order'] == 'DESC')) { |
|
123
|
|
|
$sql .= " DESC"; |
|
|
|
|
|
|
124
|
|
|
} else { |
|
125
|
|
|
$sql .= " ASC"; |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if (isset($data['start']) || isset($data['limit'])) { |
|
129
|
|
|
if ($data['start'] < 0) { |
|
130
|
|
|
$data['start'] = 0; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if ($data['limit'] < 1) { |
|
134
|
|
|
$data['limit'] = 20; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$query = $this->db->query($sql); |
|
141
|
|
|
|
|
142
|
|
|
return $query->rows; |
|
143
|
|
|
} else { |
|
144
|
|
|
$currency_data = $this->cache->get('currency'); |
|
145
|
|
|
|
|
146
|
|
|
if (!$currency_data) { |
|
147
|
|
|
$currency_data = array(); |
|
148
|
|
|
|
|
149
|
|
|
$query = $this->db->query(" |
|
|
|
|
|
|
150
|
|
|
SELECT * |
|
151
|
|
|
FROM currency ORDER BY title ASC |
|
152
|
|
|
"); |
|
153
|
|
|
|
|
154
|
|
|
foreach ($query->rows as $result) { |
|
155
|
|
|
$currency_data[$result['code']] = array( |
|
156
|
|
|
'currency_id' => $result['currency_id'], |
|
157
|
|
|
'title' => $result['title'], |
|
158
|
|
|
'code' => $result['code'], |
|
159
|
|
|
'symbol_left' => $result['symbol_left'], |
|
160
|
|
|
'symbol_right' => $result['symbol_right'], |
|
161
|
|
|
'decimal_place' => $result['decimal_place'], |
|
162
|
|
|
'value' => $result['value'], |
|
163
|
|
|
'status' => $result['status'], |
|
164
|
|
|
'date_modified' => $result['date_modified'] |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$this->cache->set( |
|
169
|
|
|
'currency', |
|
170
|
|
|
$currency_data |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $currency_data; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function refresh($force = false) |
|
179
|
|
|
{ |
|
180
|
|
|
$data = array(); |
|
181
|
|
|
|
|
182
|
|
|
if ($force) { |
|
183
|
|
|
$query = $this->db->query(" |
|
184
|
|
|
SELECT * |
|
185
|
|
|
FROM currency |
|
186
|
|
|
WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "' |
|
187
|
|
|
"); |
|
188
|
|
|
} else { |
|
189
|
|
|
$query = $this->db->query(" |
|
190
|
|
|
SELECT * |
|
191
|
|
|
FROM currency |
|
192
|
|
|
WHERE code != '" . $this->db->escape($this->config->get('config_currency')) . "' |
|
193
|
|
|
AND date_modified < '" . $this->db->escape(date('Y-m-d H:i:s', strtotime('-1 day'))) . "' |
|
194
|
|
|
"); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
foreach ($query->rows as $result) { |
|
198
|
|
|
$data[] = $this->config->get('config_currency') . $result['code'] . '=X'; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$curl = curl_init(); |
|
202
|
|
|
|
|
203
|
|
|
curl_setopt($curl, CURLOPT_URL, 'http://download.finance.yahoo.com/d/quotes.csv?s=' . implode(',', $data) . '&f=sl1&e=.csv'); |
|
|
|
|
|
|
204
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
|
205
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false); |
|
206
|
|
|
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); |
|
207
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 30); |
|
208
|
|
|
|
|
209
|
|
|
$content = curl_exec($curl); |
|
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
curl_close($curl); |
|
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
$lines = explode("\n", trim($content)); |
|
214
|
|
|
|
|
215
|
|
|
foreach ($lines as $line) { |
|
216
|
|
|
$currency = \voku\helper\UTF8::substr($line, 4, 3); |
|
217
|
|
|
$value = \voku\helper\UTF8::substr($line, 11, 6); |
|
218
|
|
|
|
|
219
|
|
|
if ((float)$value) { |
|
220
|
|
|
$this->db->query(" |
|
221
|
|
|
UPDATE currency |
|
222
|
|
|
SET value = '" . (float)$value . "', |
|
223
|
|
|
date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' |
|
224
|
|
|
WHERE code = '" . $this->db->escape($currency) . "' |
|
225
|
|
|
"); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
$this->db->query(" |
|
230
|
|
|
UPDATE currency |
|
231
|
|
|
SET value = '1.00000', |
|
232
|
|
|
date_modified = '" . $this->db->escape(date('Y-m-d H:i:s')) . "' |
|
233
|
|
|
WHERE code = '" . $this->db->escape($this->config->get('config_currency')) . "' |
|
234
|
|
|
"); |
|
235
|
|
|
|
|
236
|
|
|
$this->cache->delete('currency'); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function getTotalCurrencies() |
|
240
|
|
|
{ |
|
241
|
|
|
$query = $this->db->query(" |
|
|
|
|
|
|
242
|
|
|
SELECT COUNT(*) AS total |
|
243
|
|
|
FROM currency |
|
244
|
|
|
"); |
|
245
|
|
|
|
|
246
|
|
|
return $query->row['total']; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.