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

OpenCart API API documentation generated by ApiGen dev-master