| 1: | <?php
|
| 2: | namespace Opencart\Admin\Model\Sale;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class VoucherTheme extends \Opencart\System\Engine\Model {
|
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 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: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 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: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 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: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 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: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 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: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 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: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 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: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 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: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 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: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 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: | |
| 190: | |
| 191: | |
| 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: | |