1: <?php
2: namespace Opencart\Admin\Model\Sale;
3: /**
4: * Class Voucher Theme
5: *
6: * @package Opencart\Admin\Model\Sale
7: */
8: class VoucherTheme extends \Opencart\System\Engine\Model {
9: /**
10: * Add Voucher Theme
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return int
15: */
16: public function addVoucherTheme(array $data): int {
17: $this->db->query("INSERT INTO `" . DB_PREFIX . "voucher_theme` SET `image` = '" . $this->db->escape((string)$data['image']) . "'");
18:
19: $voucher_theme_id = $this->db->getLastId();
20:
21: foreach ($data['voucher_theme_description'] as $language_id => $value) {
22: $this->addDescription($voucher_theme_id, $language_id, $value);
23: }
24:
25: $this->cache->delete('voucher_theme');
26:
27: return $voucher_theme_id;
28: }
29:
30: /**
31: * Edit Voucher Theme
32: *
33: * @param int $voucher_theme_id
34: * @param array<string, mixed> $data
35: *
36: * @return void
37: */
38: public function editVoucherTheme(int $voucher_theme_id, array $data): void {
39: $this->db->query("UPDATE `" . DB_PREFIX . "voucher_theme` SET `image` = '" . $this->db->escape((string)$data['image']) . "' WHERE `voucher_theme_id` = '" . (int)$voucher_theme_id . "'");
40:
41: $this->deleteDescriptions($voucher_theme_id);
42:
43: foreach ($data['voucher_theme_description'] as $language_id => $value) {
44: $this->addDescription($voucher_theme_id, $language_id, $value);
45: }
46:
47: $this->cache->delete('voucher_theme');
48: }
49:
50: /**
51: * Delete Voucher Theme
52: *
53: * @param int $voucher_theme_id
54: *
55: * @return void
56: */
57: public function deleteVoucherTheme(int $voucher_theme_id): void {
58: $this->db->query("DELETE FROM `" . DB_PREFIX . "voucher_theme` WHERE `voucher_theme_id` = '" . (int)$voucher_theme_id . "'");
59:
60: $this->deleteDescriptions($voucher_theme_id);
61:
62: $this->cache->delete('voucher_theme');
63: }
64:
65: /**
66: * Get Voucher Theme
67: *
68: * @param int $voucher_theme_id
69: *
70: * @return array<string, mixed>
71: */
72: public function getVoucherTheme(int $voucher_theme_id): array {
73: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "voucher_theme` `vt` LEFT JOIN `" . DB_PREFIX . "voucher_theme_description` `vtd` ON (`vt`.`voucher_theme_id` = `vtd`.`voucher_theme_id`) WHERE `vt`.`voucher_theme_id` = '" . (int)$voucher_theme_id . "' AND `vtd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
74:
75: return $query->row;
76: }
77:
78: /**
79: * Get Voucher Themes
80: *
81: * @param array<string, mixed> $data
82: *
83: * @return array<int, array<string, mixed>>
84: */
85: public function getVoucherThemes(array $data = []): array {
86: $sql = "SELECT * FROM `" . DB_PREFIX . "voucher_theme` `vt` LEFT JOIN `" . DB_PREFIX . "voucher_theme_description` `vtd` ON (`vt`.`voucher_theme_id` = `vtd`.`voucher_theme_id`) WHERE `vtd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "' ORDER BY `vtd`.`name`";
87:
88: if (isset($data['order']) && ($data['order'] == 'DESC')) {
89: $sql .= " DESC";
90: } else {
91: $sql .= " ASC";
92: }
93:
94: if (isset($data['start']) || isset($data['limit'])) {
95: if ($data['start'] < 0) {
96: $data['start'] = 0;
97: }
98:
99: if ($data['limit'] < 1) {
100: $data['limit'] = 20;
101: }
102:
103: $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
104: }
105:
106: $key = md5($sql);
107:
108: $voucher_theme_data = $this->cache->get('voucher_theme.' . $key);
109:
110: if (!$voucher_theme_data) {
111: $query = $this->db->query($sql);
112:
113: $voucher_theme_data = $query->rows;
114:
115: $this->cache->set('voucher_theme.' . $key, $voucher_theme_data);
116: }
117:
118: return $voucher_theme_data;
119: }
120:
121: /**
122: * Add Description
123: *
124: * @param int $voucher_theme_id
125: * @param int $language_id
126: * @param array<string, mixed> $data
127: *
128: * @return void
129: */
130: public function addDescription(int $voucher_theme_id, int $language_id, array $data): void {
131: $this->db->query("INSERT INTO `" . DB_PREFIX . "voucher_theme_description` SET `voucher_theme_id` = '" . (int)$voucher_theme_id . "', `language_id` = '" . (int)$language_id . "', `name` = '" . $this->db->escape($data['name']) . "'");
132: }
133:
134: /**
135: * Delete Descriptions
136: *
137: * @param int $voucher_theme_id
138: *
139: * @return void
140: */
141: public function deleteDescriptions(int $voucher_theme_id): void {
142: $this->db->query("DELETE FROM `" . DB_PREFIX . "voucher_theme_description` WHERE `voucher_theme_id` = '" . (int)$voucher_theme_id . "'");
143: }
144:
145: /**
146: * Delete Descriptions By Language ID
147: *
148: * @param int $language_id
149: *
150: * @return void
151: */
152: public function deleteDescriptionsByLanguageId(int $language_id): void {
153: $this->db->query("DELETE FROM `" . DB_PREFIX . "voucher_theme_description` WHERE `language_id` = '" . (int)$language_id . "'");
154: }
155:
156: /**
157: * Get Descriptions
158: *
159: * @param int $voucher_theme_id
160: *
161: * @return array<int, array<string, string>>
162: */
163: public function getDescriptions(int $voucher_theme_id): array {
164: $voucher_theme_data = [];
165:
166: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "voucher_theme_description` WHERE `voucher_theme_id` = '" . (int)$voucher_theme_id . "'");
167:
168: foreach ($query->rows as $result) {
169: $voucher_theme_data[$result['language_id']] = ['name' => $result['name']];
170: }
171:
172: return $voucher_theme_data;
173: }
174:
175: /**
176: * Get Descriptions By Language ID
177: *
178: * @param int $language_id
179: *
180: * @return array<int, array<string, string>>
181: */
182: public function getDescriptionsByLanguageId(int $language_id): array {
183: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "voucher_theme_description` WHERE `language_id` = '" . (int)$language_id . "'");
184:
185: return $query->rows;
186: }
187:
188: /**
189: * Get Total Voucher Themes
190: *
191: * @return int
192: */
193: public function getTotalVoucherThemes(): int {
194: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "voucher_theme`");
195:
196: return (int)$query->row['total'];
197: }
198: }
199: