1: <?php
2: namespace Opencart\Admin\Model\Cms;
3: /**
4: * Class Topic
5: *
6: * @package Opencart\Admin\Model\Cms
7: */
8: class Topic extends \Opencart\System\Engine\Model {
9: /**
10: * Add Topic
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return int $topic
15: */
16: public function addTopic(array $data): int {
17: $this->db->query("INSERT INTO `" . DB_PREFIX . "topic` SET `sort_order` = '" . (int)$data['sort_order'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "'");
18:
19: $topic_id = $this->db->getLastId();
20:
21: // Description
22: foreach ($data['topic_description'] as $language_id => $value) {
23: $this->model_cms_topic->addDescription($topic_id, $language_id, $value);
24: }
25:
26: // Store
27: if (isset($data['topic_store'])) {
28: foreach ($data['topic_store'] as $store_id) {
29: $this->model_cms_topic->addStore($topic_id, $store_id);
30: }
31: }
32:
33: // SEO URL
34: $this->load->model('design/seo_url');
35:
36: foreach ($data['topic_seo_url'] as $store_id => $language) {
37: foreach ($language as $language_id => $keyword) {
38: $this->model_design_seo_url->addSeoUrl('topic_id', $topic_id, $keyword, $store_id, $language_id);
39: }
40: }
41:
42: $this->cache->delete('topic');
43:
44: return $topic_id;
45: }
46:
47: /**
48: * Edit Topic
49: *
50: * @param int $topic_id
51: * @param array<string, mixed> $data
52: *
53: * @return void
54: */
55: public function editTopic(int $topic_id, array $data): void {
56: $this->db->query("UPDATE `" . DB_PREFIX . "topic` SET `sort_order` = '" . (int)$data['sort_order'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "' WHERE `topic_id` = '" . (int)$topic_id . "'");
57:
58: // Description
59: $this->model_cms_topic->deleteDescriptions($topic_id);
60:
61: foreach ($data['topic_description'] as $language_id => $value) {
62: $this->model_cms_topic->addDescription($topic_id, $language_id, $value);
63: }
64:
65: // Store
66: $this->model_cms_topic->deleteStores($topic_id);
67:
68: if (isset($data['topic_store'])) {
69: foreach ($data['topic_store'] as $store_id) {
70: $this->model_cms_topic->addStore($topic_id, $store_id);
71: }
72: }
73:
74: // SEO URL
75: $this->load->model('design/seo_url');
76:
77: $this->model_design_seo_url->deleteSeoUrlsByKeyValue('topic_id', $topic_id);
78:
79: foreach ($data['topic_seo_url'] as $store_id => $language) {
80: foreach ($language as $language_id => $keyword) {
81: $this->model_design_seo_url->addSeoUrl('topic_id', $topic_id, $keyword, $store_id, $language_id);
82: }
83: }
84:
85: $this->cache->delete('topic');
86: }
87:
88: /**
89: * Delete Topic
90: *
91: * @param int $topic_id
92: *
93: * @return void
94: */
95: public function deleteTopic(int $topic_id): void {
96: $this->db->query("DELETE FROM `" . DB_PREFIX . "topic` WHERE `topic_id` = '" . (int)$topic_id . "'");
97:
98: $this->model_cms_topic->deleteDescriptions($topic_id);
99: $this->model_cms_topic->deleteStores($topic_id);
100:
101: $this->load->model('design/seo_url');
102:
103: $this->model_design_seo_url->deleteSeoUrlsByKeyValue('topic_id', $topic_id);
104:
105: $this->cache->delete('topic');
106: }
107:
108: /**
109: * Get Topic
110: *
111: * @param int $topic_id
112: *
113: * @return array<string, mixed>
114: */
115: public function getTopic(int $topic_id): array {
116: $sql = "SELECT DISTINCT * FROM `" . DB_PREFIX . "topic` `t` LEFT JOIN `" . DB_PREFIX . "topic_description` `td` ON (`t`.`topic_id` = `td`.`topic_id`) WHERE `t`.`topic_id` = '" . (int)$topic_id . "' AND `td`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
117:
118: $topic_data = $this->cache->get('topic.' . md5($sql));
119:
120: if (!$topic_data) {
121: $query = $this->db->query($sql);
122:
123: $topic_data = $query->row;
124:
125: $this->cache->set('topic.' . md5($sql), $topic_data);
126: }
127:
128: return $topic_data;
129: }
130:
131: /**
132: * Get Topics
133: *
134: * @param array<string, mixed> $data
135: *
136: * @return array<int, array<string, mixed>>
137: */
138: public function getTopics(array $data = []): array {
139: $sql = "SELECT * FROM `" . DB_PREFIX . "topic` `t` LEFT JOIN `" . DB_PREFIX . "topic_description` `td` ON (`t`.`topic_id` = `td`.`topic_id`) WHERE `td`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
140:
141: $sort_data = [
142: 'td.name',
143: 't.sort_order'
144: ];
145:
146: if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
147: $sql .= " ORDER BY " . $data['sort'];
148: } else {
149: $sql .= " ORDER BY `t`.`sort_order`";
150: }
151:
152: if (isset($data['order']) && ($data['order'] == 'DESC')) {
153: $sql .= " DESC";
154: } else {
155: $sql .= " ASC";
156: }
157:
158: if (isset($data['start']) || isset($data['limit'])) {
159: if ($data['start'] < 0) {
160: $data['start'] = 0;
161: }
162:
163: if ($data['limit'] < 1) {
164: $data['limit'] = 20;
165: }
166:
167: $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
168: }
169:
170: $key = md5($sql);
171:
172: $topic_data = $this->cache->get('topic.' . $key);
173:
174: if (!$topic_data) {
175: $query = $this->db->query($sql);
176:
177: $topic_data = $query->rows;
178:
179: $this->cache->set('topic.' . $key, $topic_data);
180: }
181:
182: return $topic_data;
183: }
184:
185: /**
186: * Get Total Topics
187: *
188: * @return int
189: */
190: public function getTotalTopics(): int {
191: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "topic`");
192:
193: return (int)$query->row['total'];
194: }
195:
196: /**
197: * Add Description
198: *
199: * @param int $topic_id
200: * @param int $language_id
201: * @param array<string, mixed> $data
202: *
203: * @return void
204: */
205: public function addDescription(int $topic_id, int $language_id, array $data): void {
206: $this->db->query("INSERT INTO `" . DB_PREFIX . "topic_description` SET `topic_id` = '" . (int)$topic_id . "', `language_id` = '" . (int)$language_id . "', `image` = '" . $this->db->escape((string)$data['image']) . "', `name` = '" . $this->db->escape($data['name']) . "', `description` = '" . $this->db->escape($data['description']) . "', `meta_title` = '" . $this->db->escape($data['meta_title']) . "', `meta_description` = '" . $this->db->escape($data['meta_description']) . "', `meta_keyword` = '" . $this->db->escape($data['meta_keyword']) . "'");
207: }
208:
209: /**
210: * Delete Description
211: *
212: * @param int $topic_id primary key of the attribute record to be fetched
213: *
214: * @return void
215: */
216: public function deleteDescriptions(int $topic_id): void {
217: $this->db->query("DELETE FROM `" . DB_PREFIX . "topic_description` WHERE `topic_id` = '" . (int)$topic_id . "'");
218: }
219:
220: /**
221: * Delete Descriptions By Language ID
222: *
223: * @param int $language_id
224: *
225: * @return void
226: */
227: public function deleteDescriptionsByLanguageId(int $language_id): void {
228: $this->db->query("DELETE FROM `" . DB_PREFIX . "topic_description` WHERE `language_id` = '" . (int)$language_id . "'");
229: }
230:
231: /**
232: * Get Descriptions
233: *
234: * @param int $topic_id
235: *
236: * @return array<int, array<string, mixed>>
237: */
238: public function getDescriptions(int $topic_id): array {
239: $topic_description_data = [];
240:
241: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "topic_description` WHERE `topic_id` = '" . (int)$topic_id . "'");
242:
243: foreach ($query->rows as $result) {
244: $topic_description_data[$result['language_id']] = [
245: 'image' => $result['image'],
246: 'name' => $result['name'],
247: 'description' => $result['description'],
248: 'meta_title' => $result['meta_title'],
249: 'meta_description' => $result['meta_description'],
250: 'meta_keyword' => $result['meta_keyword']
251: ];
252: }
253:
254: return $topic_description_data;
255: }
256:
257: /**
258: * Get Descriptions By Language ID
259: *
260: * @param int $language_id
261: *
262: * @return array<int, array<string, string>>
263: */
264: public function getDescriptionsByLanguageId(int $language_id): array {
265: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "topic_description` WHERE `language_id` = '" . (int)$language_id . "'");
266:
267: return $query->rows;
268: }
269:
270: /**
271: * Add Store
272: *
273: * @param int $topic_id
274: * @param int $store_id
275: *
276: * @return void
277: */
278: public function addStore(int $topic_id, int $store_id): void {
279: $this->db->query("INSERT INTO `" . DB_PREFIX . "topic_to_store` SET `topic_id` = '" . (int)$topic_id . "', `store_id` = '" . (int)$store_id . "'");
280: }
281:
282: /**
283: * Delete Stores
284: *
285: * @param int $topic_id
286: *
287: * @return void
288: */
289: public function deleteStores(int $topic_id): void {
290: $this->db->query("DELETE FROM `" . DB_PREFIX . "topic_to_store` WHERE `topic_id` = '" . (int)$topic_id . "'");
291: }
292:
293: /**
294: * Get Stores
295: *
296: * @param int $topic_id
297: *
298: * @return array<int, int>
299: */
300: public function getStores(int $topic_id): array {
301: $topic_store_data = [];
302:
303: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "topic_to_store` WHERE `topic_id` = '" . (int)$topic_id . "'");
304:
305: foreach ($query->rows as $result) {
306: $topic_store_data[] = $result['store_id'];
307: }
308:
309: return $topic_store_data;
310: }
311:
312: /**
313: * Add Layout
314: *
315: * @param int $topic_id
316: * @param int $store_id
317: * @param int $layout_id
318: *
319: * @return void
320: */
321: public function addLayout(int $topic_id, int $store_id, int $layout_id): void {
322: $this->db->query("INSERT INTO `" . DB_PREFIX . "topic_to_layout` SET `article_id` = '" . (int)$topic_id . "', store_id = '" . (int)$store_id . "', `layout_id` = '" . (int)$layout_id . "'");
323: }
324:
325: /**
326: * Delete Layouts
327: *
328: * @param int $article_id
329: *
330: * @return void
331: */
332: public function deleteLayouts(int $article_id): void {
333: $this->db->query("DELETE FROM `" . DB_PREFIX . "topic_to_layout` WHERE `article_id` = '" . (int)$article_id . "'");
334: }
335:
336: /**
337: * Delete Layouts By Layout ID
338: *
339: * @param int $layout_id
340: *
341: * @return void
342: */
343: public function deleteLayoutsByLayoutId(int $layout_id): void {
344: $this->db->query("DELETE FROM `" . DB_PREFIX . "topic_to_layout` WHERE `layout_id` = '" . (int)$layout_id . "'");
345: }
346: }
347: