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

OpenCart API API documentation generated by ApiGen dev-master