1: <?php
2: namespace Opencart\Admin\Model\Catalog;
3: /**
4: * Class Information
5: *
6: * @package Opencart\Admin\Model\Catalog
7: */
8: class Information extends \Opencart\System\Engine\Model {
9: /**
10: * Add Information
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return int
15: */
16: public function addInformation(array $data): int {
17: $this->db->query("INSERT INTO `" . DB_PREFIX . "information` SET `sort_order` = '" . (int)$data['sort_order'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "'");
18:
19: $information_id = $this->db->getLastId();
20:
21: foreach ($data['information_description'] as $language_id => $information_description) {
22: $this->model_catalog_information->addDescription($information_id, $language_id, $information_description);
23: }
24:
25: if (isset($data['information_store'])) {
26: foreach ($data['information_store'] as $store_id) {
27: $this->model_catalog_information->addStore($information_id, $store_id);
28: }
29: }
30:
31: // SEO URL
32: $this->load->model('design/seo_url');
33:
34: foreach ($data['information_seo_url'] as $store_id => $language) {
35: foreach ($language as $language_id => $keyword) {
36: $this->model_design_seo_url->addSeoUrl('information_id', $information_id, $keyword, $store_id, $language_id);
37: }
38: }
39:
40: if (isset($data['information_layout'])) {
41: foreach ($data['information_layout'] as $store_id => $layout_id) {
42: if ($layout_id) {
43: $this->model_catalog_information->addLayout($information_id, $store_id, $layout_id);
44: }
45: }
46: }
47:
48: $this->cache->delete('information');
49:
50: return $information_id;
51: }
52:
53: /**
54: * Edit Information
55: *
56: * @param int $information_id
57: * @param array<string, mixed> $data
58: *
59: * @return void
60: */
61: public function editInformation(int $information_id, array $data): void {
62: $this->db->query("UPDATE `" . DB_PREFIX . "information` SET `sort_order` = '" . (int)$data['sort_order'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "' WHERE `information_id` = '" . (int)$information_id . "'");
63:
64: $this->model_catalog_information->deleteDescriptions($information_id);
65:
66: foreach ($data['information_description'] as $language_id => $information_description) {
67: $this->model_catalog_information->addDescription($information_id, $language_id, $information_description);
68: }
69:
70: $this->model_catalog_information->deleteStores($information_id);
71:
72: if (isset($data['information_store'])) {
73: foreach ($data['information_store'] as $store_id) {
74: $this->model_catalog_information->addStore($information_id, $store_id);
75: }
76: }
77:
78: $this->load->model('design/seo_url');
79:
80: $this->model_design_seo_url->deleteSeoUrlsByKeyValue('information_id', $information_id);
81:
82: foreach ($data['information_seo_url'] as $store_id => $language) {
83: foreach ($language as $language_id => $keyword) {
84: $this->model_design_seo_url->addSeoUrl('information_id', $information_id, $keyword, $store_id, $language_id);
85: }
86: }
87:
88: $this->model_catalog_information->deleteLayouts($information_id);
89:
90: if (isset($data['information_layout'])) {
91: foreach ($data['information_layout'] as $store_id => $layout_id) {
92: if ($layout_id) {
93: $this->model_catalog_information->addLayout($information_id, $store_id, $layout_id);
94: }
95: }
96: }
97:
98: $this->cache->delete('information');
99: }
100:
101: /**
102: * Delete Information
103: *
104: * @param int $information_id
105: *
106: * @return void
107: */
108: public function deleteInformation(int $information_id): void {
109: $this->db->query("DELETE FROM `" . DB_PREFIX . "information` WHERE `information_id` = '" . (int)$information_id . "'");
110:
111: $this->model_catalog_information->deleteDescriptions($information_id);
112: $this->model_catalog_information->deleteStores($information_id);
113: $this->model_catalog_information->deleteLayouts($information_id);
114:
115: $this->load->model('design/seo_url');
116:
117: $this->model_design_seo_url->deleteSeoUrlsByKeyValue('information_id', $information_id);
118:
119: $this->cache->delete('information');
120: }
121:
122: /**
123: * Get Information
124: *
125: * @param int $information_id
126: *
127: * @return array<string, mixed>
128: */
129: public function getInformation(int $information_id): array {
130: $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "information` WHERE `information_id` = '" . (int)$information_id . "'");
131:
132: return $query->row;
133: }
134:
135: /**
136: * Get Information(s)
137: *
138: * @param array<string, mixed> $data
139: *
140: * @return array<int, array<string, mixed>>
141: */
142: public function getInformations(array $data = []): array {
143: $sql = "SELECT * FROM `" . DB_PREFIX . "information` `i` LEFT JOIN `" . DB_PREFIX . "information_description` `id` ON (`i`.`information_id` = `id`.`information_id`) WHERE `id`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
144:
145: $sort_data = [
146: 'id.title',
147: 'i.sort_order'
148: ];
149:
150: if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
151: $sql .= " ORDER BY " . $data['sort'];
152: } else {
153: $sql .= " ORDER BY `id`.`title`";
154: }
155:
156: if (isset($data['order']) && ($data['order'] == 'DESC')) {
157: $sql .= " DESC";
158: } else {
159: $sql .= " ASC";
160: }
161:
162: if (isset($data['start']) || isset($data['limit'])) {
163: if ($data['start'] < 0) {
164: $data['start'] = 0;
165: }
166:
167: if ($data['limit'] < 1) {
168: $data['limit'] = 20;
169: }
170:
171: $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
172: }
173:
174: $key = md5($sql);
175:
176: $information_data = $this->cache->get('information.' . $key);
177:
178: if (!$information_data) {
179: $query = $this->db->query($sql);
180:
181: $information_data = $query->rows;
182:
183: $this->cache->set('information.' . $key, $information_data);
184: }
185:
186: return $information_data;
187: }
188:
189: /**
190: * Get Total Information(s)
191: *
192: * @return int
193: */
194: public function getTotalInformations(): int {
195: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "information`");
196:
197: return (int)$query->row['total'];
198: }
199:
200: /**
201: * Add Description
202: *
203: * @param int $information_id
204: * @param int $language_id
205: * @param array<string, mixed> $data
206: *
207: * @return void
208: */
209: public function addDescription(int $information_id, int $language_id, array $data): void {
210: $this->db->query("INSERT INTO `" . DB_PREFIX . "information_description` SET `information_id` = '" . (int)$information_id . "', `language_id` = '" . (int)$language_id . "', `title` = '" . $this->db->escape($data['title']) . "', `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']) . "'");
211: }
212:
213: /**
214: * Delete Description
215: *
216: * @param int $information_id
217: *
218: * @return void
219: */
220: public function deleteDescriptions(int $information_id): void {
221: $this->db->query("DELETE FROM `" . DB_PREFIX . "information_description` WHERE `information_id` = '" . (int)$information_id . "'");
222: }
223:
224: /**
225: * Delete Descriptions By Language ID
226: *
227: * @param int $language_id
228: *
229: * @return void
230: */
231: public function deleteDescriptionsByLanguageId(int $language_id): void {
232: $this->db->query("DELETE FROM `" . DB_PREFIX . "information_description` WHERE `language_id` = '" . (int)$language_id . "'");
233: }
234:
235: /**
236: * Get Descriptions
237: *
238: * @param int $information_id
239: *
240: * @return array<int, array<string, string>>
241: */
242: public function getDescriptions(int $information_id): array {
243: $information_description_data = [];
244:
245: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "information_description` WHERE `information_id` = '" . (int)$information_id . "'");
246:
247: foreach ($query->rows as $result) {
248: $information_description_data[$result['language_id']] = [
249: 'title' => $result['title'],
250: 'description' => $result['description'],
251: 'meta_title' => $result['meta_title'],
252: 'meta_description' => $result['meta_description'],
253: 'meta_keyword' => $result['meta_keyword']
254: ];
255: }
256:
257: return $information_description_data;
258: }
259:
260: /**
261: * Get Descriptions By Language ID
262: *
263: * @param int $language_id
264: *
265: * @return array<int, array<string, string>>
266: */
267: public function getDescriptionsByLanguageId(int $language_id): array {
268: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "information_description` WHERE `language_id` = '" . (int)$language_id . "'");
269:
270: return $query->rows;
271: }
272:
273: /**
274: * Add Store
275: *
276: * @param int $information_id
277: * @param int $store_id
278: *
279: * @return void
280: */
281: public function addStore(int $information_id, int $store_id): void {
282: $this->db->query("INSERT INTO `" . DB_PREFIX . "information_to_store` SET `information_id` = '" . (int)$information_id . "', `store_id` = '" . (int)$store_id . "'");
283: }
284:
285: /**
286: * Delete Stores
287: *
288: * @param int $information_id
289: *
290: * @return void
291: */
292: public function deleteStores(int $information_id): void {
293: $this->db->query("DELETE FROM `" . DB_PREFIX . "information_to_store` WHERE `information_id` = '" . (int)$information_id . "'");
294: }
295:
296: /**
297: * Delete Stores By Store ID
298: *
299: * @param int $store_id
300: *
301: * @return void
302: */
303: public function deleteStoresByStoreId(int $store_id): void {
304: $this->db->query("DELETE FROM `" . DB_PREFIX . "information_to_store` WHERE `store_id` = '" . (int)$store_id . "'");
305: }
306:
307: /**
308: * Get Stores
309: *
310: * @param int $information_id
311: *
312: * @return array<int, int>
313: */
314: public function getStores(int $information_id): array {
315: $information_store_data = [];
316:
317: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "information_to_store` WHERE `information_id` = '" . (int)$information_id . "'");
318:
319: foreach ($query->rows as $result) {
320: $information_store_data[] = $result['store_id'];
321: }
322:
323: return $information_store_data;
324: }
325:
326: /**
327: * Add Layout
328: *
329: * @param int $information_id
330: * @param int $store_id
331: * @param int $layout_id
332: *
333: * @return void
334: */
335: public function addLayout(int $information_id, int $store_id, int $layout_id): void {
336: $this->db->query("INSERT INTO `" . DB_PREFIX . "information_to_layout` SET `information_id` = '" . (int)$information_id . "', store_id = '" . (int)$store_id . "', `layout_id` = '" . (int)$layout_id . "'");
337: }
338:
339: /**
340: * Delete Layouts
341: *
342: * @param int $information_id
343: *
344: * @return void
345: */
346: public function deleteLayouts(int $information_id): void {
347: $this->db->query("DELETE FROM `" . DB_PREFIX . "information_to_layout` WHERE `information_id` = '" . (int)$information_id . "'");
348: }
349:
350: /**
351: * Delete Layouts By Layout ID
352: *
353: * @param int $layout_id
354: *
355: * @return void
356: */
357: public function deleteLayoutsByLayoutId(int $layout_id): void {
358: $this->db->query("DELETE FROM `" . DB_PREFIX . "information_to_layout` WHERE `layout_id` = '" . (int)$layout_id . "'");
359: }
360:
361: /**
362: * Delete Layouts By Store ID
363: *
364: * @param int $store_id
365: *
366: * @return void
367: */
368: public function deleteLayoutsByStoreId(int $store_id): void {
369: $this->db->query("DELETE FROM `" . DB_PREFIX . "information_to_layout` WHERE `store_id` = '" . (int)$store_id . "'");
370: }
371:
372: /**
373: * Get Layouts
374: *
375: * @param int $information_id
376: *
377: * @return array<int, int>
378: */
379: public function getLayouts(int $information_id): array {
380: $information_layout_data = [];
381:
382: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "information_to_layout` WHERE `information_id` = '" . (int)$information_id . "'");
383:
384: foreach ($query->rows as $result) {
385: $information_layout_data[$result['store_id']] = $result['layout_id'];
386: }
387:
388: return $information_layout_data;
389: }
390:
391: /**
392: * Get Total Layouts By Layout ID
393: *
394: * @param int $layout_id
395: *
396: * @return int
397: */
398: public function getTotalLayoutsByLayoutId(int $layout_id): int {
399: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "information_to_layout` WHERE `layout_id` = '" . (int)$layout_id . "'");
400:
401: return (int)$query->row['total'];
402: }
403: }
404: