1: <?php
2: namespace Opencart\Admin\Model\Localisation;
3: /**
4: * Class Currency
5: *
6: * @package Opencart\Admin\Model\Localisation
7: */
8: class Currency extends \Opencart\System\Engine\Model {
9: /**
10: * Add Currency
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return int
15: */
16: public function addCurrency(array $data): int {
17: $this->db->query("INSERT INTO `" . DB_PREFIX . "currency` SET `title` = '" . $this->db->escape((string)$data['title']) . "', `code` = '" . $this->db->escape((string)$data['code']) . "', `symbol_left` = '" . $this->db->escape((string)$data['symbol_left']) . "', `symbol_right` = '" . $this->db->escape((string)$data['symbol_right']) . "', `decimal_place` = '" . (int)$data['decimal_place'] . "', `value` = '" . (float)$data['value'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "', `date_modified` = NOW()");
18:
19: $this->cache->delete('currency');
20:
21: return $this->db->getLastId();
22: }
23:
24: /**
25: * Edit Currency
26: *
27: * @param int $currency_id
28: * @param array<string, mixed> $data
29: *
30: * @return void
31: */
32: public function editCurrency(int $currency_id, array $data): void {
33: $this->db->query("UPDATE `" . DB_PREFIX . "currency` SET `title` = '" . $this->db->escape((string)$data['title']) . "', `code` = '" . $this->db->escape((string)$data['code']) . "', `symbol_left` = '" . $this->db->escape((string)$data['symbol_left']) . "', `symbol_right` = '" . $this->db->escape((string)$data['symbol_right']) . "', `decimal_place` = '" . (int)$data['decimal_place'] . "', `value` = '" . (float)$data['value'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "', `date_modified` = NOW() WHERE `currency_id` = '" . (int)$currency_id . "'");
34:
35: $this->cache->delete('currency');
36: }
37:
38: /**
39: * Edit Value By Code
40: *
41: * @param string $code
42: * @param float $value
43: *
44: * @return void
45: */
46: public function editValueByCode(string $code, float $value): void {
47: $this->db->query("UPDATE `" . DB_PREFIX . "currency` SET `value` = '" . (float)$value . "', `date_modified` = NOW() WHERE `code` = '" . $this->db->escape($code) . "'");
48:
49: $this->cache->delete('currency');
50: }
51:
52: /**
53: * Delete Currency
54: *
55: * @param int $currency_id
56: *
57: * @return void
58: */
59: public function deleteCurrency(int $currency_id): void {
60: $this->db->query("DELETE FROM `" . DB_PREFIX . "currency` WHERE `currency_id` = '" . (int)$currency_id . "'");
61:
62: $this->cache->delete('currency');
63: }
64:
65: /**
66: * Get Currency
67: *
68: * @param int $currency_id
69: *
70: * @return array<string, mixed>
71: */
72: public function getCurrency(int $currency_id): array {
73: $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "currency` WHERE `currency_id` = '" . (int)$currency_id . "'");
74:
75: return $query->row;
76: }
77:
78: /**
79: * Get Currency By Code
80: *
81: * @param string $currency
82: *
83: * @return array<string, mixed>
84: */
85: public function getCurrencyByCode(string $currency): array {
86: $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "currency` WHERE `code` = '" . $this->db->escape($currency) . "'");
87:
88: return $query->row;
89: }
90:
91: /**
92: * Get Currencies
93: *
94: * @param array<string, mixed> $data
95: *
96: * @return array<string, array<string, mixed>>
97: */
98: public function getCurrencies(array $data = []): array {
99: $sql = "SELECT * FROM `" . DB_PREFIX . "currency`";
100:
101: $sort_data = [
102: 'title',
103: 'code',
104: 'value',
105: 'date_modified'
106: ];
107:
108: if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
109: $sql .= " ORDER BY " . $data['sort'];
110: } else {
111: $sql .= " ORDER BY `title`";
112: }
113:
114: if (isset($data['order']) && ($data['order'] == 'DESC')) {
115: $sql .= " DESC";
116: } else {
117: $sql .= " ASC";
118: }
119:
120: if (isset($data['start']) || isset($data['limit'])) {
121: if ($data['start'] < 0) {
122: $data['start'] = 0;
123: }
124:
125: if ($data['limit'] < 1) {
126: $data['limit'] = 20;
127: }
128:
129: $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
130: }
131:
132: $results = $this->cache->get('currency.' . md5($sql));
133:
134: if (!$results) {
135: $query = $this->db->query($sql);
136:
137: $results = $query->rows;
138:
139: $this->cache->set('currency.' . md5($sql), $results);
140: }
141:
142: $currency_data = [];
143:
144: foreach ($results as $result) {
145: $currency_data[$result['code']] = [
146: 'currency_id' => $result['currency_id'],
147: 'title' => $result['title'],
148: 'code' => $result['code'],
149: 'symbol_left' => $result['symbol_left'],
150: 'symbol_right' => $result['symbol_right'],
151: 'decimal_place' => $result['decimal_place'],
152: 'value' => $result['value'],
153: 'status' => $result['status'],
154: 'date_modified' => $result['date_modified']
155: ];
156: }
157:
158: return $currency_data;
159: }
160:
161: /**
162: * Get Total Currencies
163: *
164: * @return int
165: */
166: public function getTotalCurrencies(): int {
167: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "currency`");
168:
169: return (int)$query->row['total'];
170: }
171: }
172: