| 1: | <?php
|
| 2: | namespace Opencart\Admin\Model\Localisation;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class WeightClass extends \Opencart\System\Engine\Model {
|
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: |
|
| 16: | public function addWeightClass(array $data): int {
|
| 17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "weight_class` SET `value` = '" . (float)$data['value'] . "'");
|
| 18: |
|
| 19: | $weight_class_id = $this->db->getLastId();
|
| 20: |
|
| 21: | foreach ($data['weight_class_description'] as $language_id => $value) {
|
| 22: | $this->addDescription($weight_class_id, $language_id, $value);
|
| 23: | }
|
| 24: |
|
| 25: | $this->cache->delete('weight_class');
|
| 26: |
|
| 27: | return $weight_class_id;
|
| 28: | }
|
| 29: |
|
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: |
|
| 38: | public function editWeightClass(int $weight_class_id, array $data): void {
|
| 39: | $this->db->query("UPDATE `" . DB_PREFIX . "weight_class` SET `value` = '" . (float)$data['value'] . "' WHERE `weight_class_id` = '" . (int)$weight_class_id . "'");
|
| 40: |
|
| 41: | $this->deleteDescriptions($weight_class_id);
|
| 42: |
|
| 43: | foreach ($data['weight_class_description'] as $language_id => $value) {
|
| 44: | $this->addDescription($weight_class_id, $language_id, $value);
|
| 45: | }
|
| 46: |
|
| 47: | $this->cache->delete('weight_class');
|
| 48: | }
|
| 49: |
|
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: |
|
| 57: | public function deleteWeightClass(int $weight_class_id): void {
|
| 58: | $this->db->query("DELETE FROM `" . DB_PREFIX . "weight_class` WHERE `weight_class_id` = '" . (int)$weight_class_id . "'");
|
| 59: |
|
| 60: | $this->deleteDescriptions($weight_class_id);
|
| 61: |
|
| 62: | $this->cache->delete('weight_class');
|
| 63: | }
|
| 64: |
|
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: |
|
| 72: | public function getWeightClasses(array $data = []): array {
|
| 73: | $sql = "SELECT * FROM `" . DB_PREFIX . "weight_class` `wc` LEFT JOIN `" . DB_PREFIX . "weight_class_description` `wcd` ON (`wc`.`weight_class_id` = `wcd`.`weight_class_id`) WHERE `wcd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
|
| 74: |
|
| 75: | $sort_data = [
|
| 76: | 'title',
|
| 77: | 'unit',
|
| 78: | 'value'
|
| 79: | ];
|
| 80: |
|
| 81: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
| 82: | $sql .= " ORDER BY " . $data['sort'];
|
| 83: | } else {
|
| 84: | $sql .= " ORDER BY `title`";
|
| 85: | }
|
| 86: |
|
| 87: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
| 88: | $sql .= " DESC";
|
| 89: | } else {
|
| 90: | $sql .= " ASC";
|
| 91: | }
|
| 92: |
|
| 93: | if (isset($data['start']) || isset($data['limit'])) {
|
| 94: | if ($data['start'] < 0) {
|
| 95: | $data['start'] = 0;
|
| 96: | }
|
| 97: |
|
| 98: | if ($data['limit'] < 1) {
|
| 99: | $data['limit'] = 20;
|
| 100: | }
|
| 101: |
|
| 102: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
| 103: | }
|
| 104: |
|
| 105: | $key = md5($sql);
|
| 106: |
|
| 107: | $weight_class_data = $this->cache->get('weight_class.' . $key);
|
| 108: |
|
| 109: | if (!$weight_class_data) {
|
| 110: | $query = $this->db->query($sql);
|
| 111: |
|
| 112: | $weight_class_data = $query->rows;
|
| 113: |
|
| 114: | $this->cache->set('weight_class.' . $key, $weight_class_data);
|
| 115: | }
|
| 116: |
|
| 117: | return $weight_class_data;
|
| 118: | }
|
| 119: |
|
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: |
|
| 127: | public function getWeightClass(int $weight_class_id): array {
|
| 128: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "weight_class` `wc` LEFT JOIN `" . DB_PREFIX . "weight_class_description` `wcd` ON (`wc`.`weight_class_id` = `wcd`.`weight_class_id`) WHERE `wc`.`weight_class_id` = '" . (int)$weight_class_id . "' AND `wcd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
|
| 129: |
|
| 130: | return $query->row;
|
| 131: | }
|
| 132: |
|
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: |
|
| 138: | public function getTotalWeightClasses(): int {
|
| 139: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "weight_class`");
|
| 140: |
|
| 141: | return (int)$query->row['total'];
|
| 142: | }
|
| 143: |
|
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: |
|
| 153: | public function addDescription(int $weight_class_id, int $language_id, array $data): void {
|
| 154: | $this->db->query("INSERT INTO `" . DB_PREFIX . "weight_class_description` SET `weight_class_id` = '" . (int)$weight_class_id . "', `language_id` = '" . (int)$language_id . "', `title` = '" . $this->db->escape($data['title']) . "', `unit` = '" . $this->db->escape($data['unit']) . "'");
|
| 155: | }
|
| 156: |
|
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: |
|
| 164: | public function deleteDescriptions(int $weight_class_id): void {
|
| 165: | $this->db->query("DELETE FROM `" . DB_PREFIX . "weight_class_description` WHERE `weight_class_id` = '" . (int)$weight_class_id . "'");
|
| 166: | }
|
| 167: |
|
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: |
|
| 175: | public function deleteDescriptionsByLanguageId(int $language_id): void {
|
| 176: | $this->db->query("DELETE FROM `" . DB_PREFIX . "weight_class_description` WHERE `language_id` = '" . (int)$language_id . "'");
|
| 177: | }
|
| 178: |
|
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: |
|
| 186: | public function getDescriptions(int $weight_class_id): array {
|
| 187: | $weight_class_data = [];
|
| 188: |
|
| 189: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "weight_class_description` WHERE `weight_class_id` = '" . (int)$weight_class_id . "'");
|
| 190: |
|
| 191: | foreach ($query->rows as $result) {
|
| 192: | $weight_class_data[$result['language_id']] = [
|
| 193: | 'title' => $result['title'],
|
| 194: | 'unit' => $result['unit']
|
| 195: | ];
|
| 196: | }
|
| 197: |
|
| 198: | return $weight_class_data;
|
| 199: | }
|
| 200: |
|
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: |
|
| 208: | public function getDescriptionsByLanguageId(int $language_id): array {
|
| 209: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "weight_class_description` WHERE `language_id` = '" . (int)$language_id . "'");
|
| 210: |
|
| 211: | return $query->rows;
|
| 212: | }
|
| 213: |
|
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: | |
| 220: |
|
| 221: | public function getDescriptionsByUnit(string $unit): array {
|
| 222: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "weight_class_description` WHERE `unit` = '" . $this->db->escape($unit) . "' AND `language_id` = '" . (int)$this->config->get('config_language_id') . "'");
|
| 223: |
|
| 224: | return $query->row;
|
| 225: | }
|
| 226: | }
|
| 227: | |