1: | <?php
|
2: | namespace Opencart\Admin\Model\Cms;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Topic extends \Opencart\System\Engine\Model {
|
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
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: |
|
22: | foreach ($data['topic_description'] as $language_id => $value) {
|
23: | $this->model_cms_topic->addDescription($topic_id, $language_id, $value);
|
24: | }
|
25: |
|
26: |
|
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: |
|
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: | |
49: | |
50: | |
51: | |
52: | |
53: | |
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: |
|
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: |
|
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: |
|
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: | |
90: | |
91: | |
92: | |
93: | |
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: | |
110: | |
111: | |
112: | |
113: | |
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: | |
133: | |
134: | |
135: | |
136: | |
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: | |
187: | |
188: | |
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: | |
198: | |
199: | |
200: | |
201: | |
202: | |
203: | |
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: | |
211: | |
212: | |
213: | |
214: | |
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: | |
222: | |
223: | |
224: | |
225: | |
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: | |
233: | |
234: | |
235: | |
236: | |
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: | |
259: | |
260: | |
261: | |
262: | |
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: | |
272: | |
273: | |
274: | |
275: | |
276: | |
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: | |
284: | |
285: | |
286: | |
287: | |
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: | |
295: | |
296: | |
297: | |
298: | |
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: | |
314: | |
315: | |
316: | |
317: | |
318: | |
319: | |
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: | |
327: | |
328: | |
329: | |
330: | |
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: | |
338: | |
339: | |
340: | |
341: | |
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: | |