1: <?php
2: namespace Opencart\Admin\Model\Localisation;
3: /**
4: * Class WeightClass
5: *
6: * @package Opencart\Admin\Model\Localisation
7: */
8: class WeightClass extends \Opencart\System\Engine\Model {
9: /**
10: * Add Weight Class
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return int
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: * Edit Weight Class
32: *
33: * @param int $weight_class_id
34: * @param array<string, mixed> $data
35: *
36: * @return void
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: * Delete Weight Class
52: *
53: * @param int $weight_class_id
54: *
55: * @return void
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: * Get Weight Classes
67: *
68: * @param array<string, mixed> $data
69: *
70: * @return array<int, array<string, mixed>>
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: * Get Weight Class
122: *
123: * @param int $weight_class_id
124: *
125: * @return array<string, mixed>
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: * Get Total Weight Classes
135: *
136: * @return int
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: * Add Description
146: *
147: * @param int $weight_class_id
148: * @param int $language_id
149: * @param array<string, mixed> $data
150: *
151: * @return void
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: * Delete Description
159: *
160: * @param int $weight_class_id
161: *
162: * @return void
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: * Delete Descriptions By Language ID
170: *
171: * @param int $language_id
172: *
173: * @return void
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: * Get Descriptions
181: *
182: * @param int $weight_class_id
183: *
184: * @return array<int, array<string, mixed>>
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: * Get Descriptions By Language ID
203: *
204: * @param int $language_id
205: *
206: * @return array<int, array<string, string>>
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: * Get Descriptions By Unit
216: *
217: * @param string $unit
218: *
219: * @return array<string, mixed>
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: