1: | <?php
2: | namespace Opencart\Admin\Model\Catalog;
3: | /**
4: | * Class Attribute Group
5: | *
6: | * @package Opencart\Admin\Model\Catalog
7: | */
8: | class AttributeGroup extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Attribute Group
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addAttributeGroup(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "attribute_group` SET `sort_order` = '" . (int)$data['sort_order'] . "'");
18: |
19: | $attribute_group_id = $this->db->getLastId();
20: |
21: | foreach ($data['attribute_group_description'] as $language_id => $attribute_group_description) {
22: | $this->model_catalog_attribute_group->addDescription($attribute_group_id, $language_id, $attribute_group_description);
23: | }
24: |
25: | return $attribute_group_id;
26: | }
27: |
28: | /**
29: | * Edit Attribute Group
30: | *
31: | * @param int $attribute_group_id
32: | * @param array<string, mixed> $data
33: | *
34: | * @return void
35: | */
36: | public function editAttributeGroup(int $attribute_group_id, array $data): void {
37: | $this->db->query("UPDATE `" . DB_PREFIX . "attribute_group` SET `sort_order` = '" . (int)$data['sort_order'] . "' WHERE `attribute_group_id` = '" . (int)$attribute_group_id . "'");
38: |
39: | $this->deleteDescriptions($attribute_group_id);
40: |
41: | foreach ($data['attribute_group_description'] as $language_id => $attribute_group_description) {
42: | $this->addDescription($attribute_group_id, $language_id, $attribute_group_description);
43: | }
44: | }
45: |
46: | /**
47: | * Delete Attribute Group
48: | *
49: | * @param int $attribute_group_id
50: | *
51: | * @return void
52: | */
53: | public function deleteAttributeGroup(int $attribute_group_id): void {
54: | $this->db->query("DELETE FROM `" . DB_PREFIX . "attribute_group` WHERE `attribute_group_id` = '" . (int)$attribute_group_id . "'");
55: |
56: | $this->model_catalog_attribute_group->deleteDescriptions($attribute_group_id);
57: | }
58: |
59: | /**
60: | * Get Attribute Group
61: | *
62: | * @param int $attribute_group_id
63: | *
64: | * @return array<string, mixed>
65: | */
66: | public function getAttributeGroup(int $attribute_group_id): array {
67: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "attribute_group` WHERE `attribute_group_id` = '" . (int)$attribute_group_id . "'");
68: |
69: | return $query->row;
70: | }
71: |
72: | /**
73: | * Get Attribute Groups
74: | *
75: | * @param array<string, mixed> $data
76: | *
77: | * @return array<int, array<string, mixed>>
78: | */
79: | public function getAttributeGroups(array $data = []): array {
80: | $sql = "SELECT * FROM `" . DB_PREFIX . "attribute_group` `ag` LEFT JOIN `" . DB_PREFIX . "attribute_group_description` `agd` ON (`ag`.`attribute_group_id` = agd.`attribute_group_id`) WHERE `agd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
81: |
82: | $sort_data = [
83: | 'agd.name',
84: | 'ag.sort_order'
85: | ];
86: |
87: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
88: | $sql .= " ORDER BY " . $data['sort'];
89: | } else {
90: | $sql .= " ORDER BY `agd`.`name`";
91: | }
92: |
93: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
94: | $sql .= " DESC";
95: | } else {
96: | $sql .= " ASC";
97: | }
98: |
99: | if (isset($data['start']) || isset($data['limit'])) {
100: | if ($data['start'] < 0) {
101: | $data['start'] = 0;
102: | }
103: |
104: | if ($data['limit'] < 1) {
105: | $data['limit'] = 20;
106: | }
107: |
108: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
109: | }
110: |
111: | $query = $this->db->query($sql);
112: |
113: | return $query->rows;
114: | }
115: |
116: | /**
117: | * Get Total Attribute Groups
118: | *
119: | * @return int
120: | */
121: | public function getTotalAttributeGroups(): int {
122: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "attribute_group`");
123: |
124: | return (int)$query->row['total'];
125: | }
126: |
127: | /**
128: | * Add Description
129: | *
130: | * @param int $attribute_group_id
131: | * @param int $language_id
132: | * @param array<string, mixed> $data
133: | *
134: | * @return void
135: | */
136: | public function addDescription(int $attribute_group_id, int $language_id, array $data): void {
137: | $this->db->query("INSERT INTO `" . DB_PREFIX . "attribute_group_description` SET `attribute_group_id` = '" . (int)$attribute_group_id . "', `language_id` = '" . (int)$language_id . "', `name` = '" . $this->db->escape($data['name']) . "'");
138: | }
139: |
140: | /**
141: | * Delete Descriptions
142: | *
143: | * @param int $attribute_group_id primary key of the attribute record to be fetched
144: | *
145: | * @return void
146: | */
147: | public function deleteDescriptions(int $attribute_group_id): void {
148: | $this->db->query("DELETE FROM `" . DB_PREFIX . "attribute_group_description` WHERE `attribute_group_id` = '" . (int)$attribute_group_id . "'");
149: | }
150: |
151: | /**
152: | * Delete Descriptions By Language ID
153: | *
154: | * @param int $language_id
155: | *
156: | * @return void
157: | */
158: | public function deleteDescriptionsByLanguageId(int $language_id): void {
159: | $this->db->query("DELETE FROM `" . DB_PREFIX . "attribute_group_description` WHERE `language_id` = '" . (int)$language_id . "'");
160: | }
161: |
162: | /**
163: | * Get Descriptions
164: | *
165: | * @param int $attribute_group_id
166: | *
167: | * @return array<int, array<string, string>>
168: | */
169: | public function getDescriptions(int $attribute_group_id): array {
170: | $attribute_group_data = [];
171: |
172: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "attribute_group_description` WHERE `attribute_group_id` = '" . (int)$attribute_group_id . "'");
173: |
174: | foreach ($query->rows as $result) {
175: | $attribute_group_data[$result['language_id']] = ['name' => $result['name']];
176: | }
177: |
178: | return $attribute_group_data;
179: | }
180: |
181: | /**
182: | * Get Descriptions By Language ID
183: | *
184: | * @param int $language_id
185: | *
186: | * @return array<int, array<string, string>>
187: | */
188: | public function getDescriptionsByLanguageId(int $language_id): array {
189: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "attribute_group_description` WHERE `language_id` = '" . (int)$language_id . "'");
190: |
191: | return $query->rows;
192: | }
193: | }
194: |

OpenCart API API documentation generated by ApiGen dev-master