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

OpenCart API API documentation generated by ApiGen dev-master