1: | <?php
|
2: | namespace Opencart\Admin\Model\Catalog;
|
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: |
|
10: | class Attribute extends \Opencart\System\Engine\Model {
|
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: |
|
20: | public function addAttribute(array $data): int {
|
21: | $this->db->query("INSERT INTO `" . DB_PREFIX . "attribute` SET `attribute_group_id` = '" . (int)$data['attribute_group_id'] . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
|
22: |
|
23: | $attribute_id = $this->db->getLastId();
|
24: |
|
25: | foreach ($data['attribute_description'] as $language_id => $attribute_description) {
|
26: | $this->model_catalog_attribute->addDescription($attribute_id, $language_id, $attribute_description);
|
27: | }
|
28: |
|
29: | return $attribute_id;
|
30: | }
|
31: |
|
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: |
|
42: | public function editAttribute(int $attribute_id, array $data): void {
|
43: | $this->db->query("UPDATE `" . DB_PREFIX . "attribute` SET `attribute_group_id` = '" . (int)$data['attribute_group_id'] . "', `sort_order` = '" . (int)$data['sort_order'] . "' WHERE `attribute_id` = '" . (int)$attribute_id . "'");
|
44: |
|
45: | $this->model_catalog_attribute->deleteDescriptions($attribute_id);
|
46: |
|
47: | foreach ($data['attribute_description'] as $language_id => $attribute_description) {
|
48: | $this->model_catalog_attribute->addDescription($attribute_id, $language_id, $attribute_description);
|
49: | }
|
50: | }
|
51: |
|
52: | |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
60: |
|
61: | public function deleteAttribute(int $attribute_id): void {
|
62: | $this->db->query("DELETE FROM `" . DB_PREFIX . "attribute` WHERE `attribute_id` = '" . (int)$attribute_id . "'");
|
63: |
|
64: | $this->model_catalog_attribute->deleteDescriptions($attribute_id);
|
65: | }
|
66: |
|
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: |
|
76: | public function getAttribute(int $attribute_id): array {
|
77: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "attribute` `a` LEFT JOIN `" . DB_PREFIX . "attribute_description` `ad` ON (`a`.`attribute_id` = `ad`.`attribute_id`) WHERE `a`.`attribute_id` = '" . (int)$attribute_id . "' AND `ad`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
|
78: |
|
79: | return $query->row;
|
80: | }
|
81: |
|
82: | |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | |
90: |
|
91: | public function getAttributes(array $data = []): array {
|
92: | $sql = "SELECT *, (SELECT `agd`.`name` FROM `" . DB_PREFIX . "attribute_group_description` `agd` WHERE `agd`.`attribute_group_id` = `a`.`attribute_group_id` AND `agd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "') AS `attribute_group` FROM `" . DB_PREFIX . "attribute` `a` LEFT JOIN `" . DB_PREFIX . "attribute_description` `ad` ON (`a`.`attribute_id` = `ad`.`attribute_id`) WHERE `ad`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
|
93: |
|
94: | if (!empty($data['filter_name'])) {
|
95: | $sql .= " AND LCASE(`ad`.`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'";
|
96: | }
|
97: |
|
98: | if (!empty($data['filter_attribute_group_id'])) {
|
99: | $sql .= " AND `a`.`attribute_group_id` = '" . (int)$data['filter_attribute_group_id'] . "'";
|
100: | }
|
101: |
|
102: | $sort_data = [
|
103: | 'ad.name',
|
104: | 'attribute_group',
|
105: | 'a.sort_order'
|
106: | ];
|
107: |
|
108: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
109: | $sql .= " ORDER BY " . $data['sort'];
|
110: | } else {
|
111: | $sql .= " ORDER BY `attribute_group`, `ad`.`name`";
|
112: | }
|
113: |
|
114: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
115: | $sql .= " DESC";
|
116: | } else {
|
117: | $sql .= " ASC";
|
118: | }
|
119: |
|
120: | if (isset($data['start']) || isset($data['limit'])) {
|
121: | if ($data['start'] < 0) {
|
122: | $data['start'] = 0;
|
123: | }
|
124: |
|
125: | if ($data['limit'] < 1) {
|
126: | $data['limit'] = 20;
|
127: | }
|
128: |
|
129: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
130: | }
|
131: |
|
132: | $query = $this->db->query($sql);
|
133: |
|
134: | return $query->rows;
|
135: | }
|
136: |
|
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: |
|
144: | public function getTotalAttributes(): int {
|
145: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "attribute`");
|
146: |
|
147: | if ($query->num_rows) {
|
148: | return (int)$query->row['total'];
|
149: | } else {
|
150: | return 0;
|
151: | }
|
152: | }
|
153: |
|
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | |
162: |
|
163: | public function getTotalAttributesByAttributeGroupId(int $attribute_group_id): int {
|
164: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "attribute` WHERE `attribute_group_id` = '" . (int)$attribute_group_id . "'");
|
165: |
|
166: | return (int)$query->row['total'];
|
167: | }
|
168: |
|
169: | |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: |
|
178: | public function addDescription(int $attribute_id, int $language_id, array $data): void {
|
179: | $this->db->query("INSERT INTO `" . DB_PREFIX . "attribute_description` SET `attribute_id` = '" . (int)$attribute_id . "', `language_id` = '" . (int)$language_id . "', `name` = '" . $this->db->escape($data['name']) . "'");
|
180: | }
|
181: |
|
182: | |
183: | |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | |
190: |
|
191: | public function deleteDescriptions(int $attribute_id): void {
|
192: | $this->db->query("DELETE FROM `" . DB_PREFIX . "attribute_description` WHERE `attribute_id` = '" . (int)$attribute_id . "'");
|
193: | }
|
194: |
|
195: | |
196: | |
197: | |
198: | |
199: | |
200: | |
201: | |
202: | |
203: |
|
204: | public function deleteDescriptionsByLanguageId(int $language_id): void {
|
205: | $this->db->query("DELETE FROM `" . DB_PREFIX . "attribute_description` WHERE `language_id` = '" . (int)$language_id . "'");
|
206: | }
|
207: |
|
208: | |
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: |
|
218: | public function getDescription(int $attribute_id, int $language_id): array {
|
219: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "attribute_description` WHERE `attribute_id` = '" . (int)$attribute_id . "' AND language_id = '" . (int)$language_id . "'");
|
220: |
|
221: | return $query->row;
|
222: | }
|
223: |
|
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | |
231: | |
232: |
|
233: | public function getDescriptions(int $attribute_id): array {
|
234: | $attribute_data = [];
|
235: |
|
236: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "attribute_description` WHERE `attribute_id` = '" . (int)$attribute_id . "'");
|
237: |
|
238: | foreach ($query->rows as $result) {
|
239: | $attribute_data[$result['language_id']] = ['name' => $result['name']];
|
240: | }
|
241: |
|
242: | return $attribute_data;
|
243: | }
|
244: |
|
245: | |
246: | |
247: | |
248: | |
249: | |
250: | |
251: | |
252: | |
253: |
|
254: | public function getDescriptionsByLanguageId(int $language_id): array {
|
255: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "attribute_description` WHERE `language_id` = '" . (int)$language_id . "'");
|
256: |
|
257: | return $query->rows;
|
258: | }
|
259: | }
|
260: | |