1: <?php
2: namespace Opencart\Admin\Model\Design;
3: /**
4: * Class Layout
5: *
6: * @package Opencart\Admin\Model\Design
7: */
8: class Layout extends \Opencart\System\Engine\Model {
9: /**
10: * Add Layout
11: *
12: * @param array<string, mixed> $data
13: *
14: * @return int
15: */
16: public function addLayout(array $data): int {
17: $this->db->query("INSERT INTO `" . DB_PREFIX . "layout` SET `name` = '" . $this->db->escape((string)$data['name']) . "'");
18:
19: $layout_id = $this->db->getLastId();
20:
21: if (isset($data['layout_route'])) {
22: foreach ($data['layout_route'] as $layout_route) {
23: $this->addRoute($layout_id, $layout_route);
24: }
25: }
26:
27: if (isset($data['layout_module'])) {
28: foreach ($data['layout_module'] as $layout_module) {
29: $this->addModule($layout_id, $layout_module);
30: }
31: }
32:
33: return $layout_id;
34: }
35:
36: /**
37: * Edit Layout
38: *
39: * @param int $layout_id
40: * @param array<string, mixed> $data
41: *
42: * @return void
43: */
44: public function editLayout(int $layout_id, array $data): void {
45: $this->db->query("UPDATE `" . DB_PREFIX . "layout` SET `name` = '" . $this->db->escape((string)$data['name']) . "' WHERE `layout_id` = '" . (int)$layout_id . "'");
46:
47: $this->deleteRoutes($layout_id);
48:
49: if (isset($data['layout_route'])) {
50: foreach ($data['layout_route'] as $layout_route) {
51: $this->addRoute($layout_id, $layout_route);
52: }
53: }
54:
55: $this->deleteModules($layout_id);
56:
57: if (isset($data['layout_module'])) {
58: foreach ($data['layout_module'] as $layout_module) {
59: $this->addModule($layout_id, $layout_module);
60: }
61: }
62: }
63:
64: /**
65: * Delete Layout
66: *
67: * @param int $layout_id
68: *
69: * @return void
70: */
71: public function deleteLayout(int $layout_id): void {
72: $this->db->query("DELETE FROM `" . DB_PREFIX . "layout` WHERE `layout_id` = '" . (int)$layout_id . "'");
73:
74: $this->deleteRoutes($layout_id);
75: $this->deleteModules($layout_id);
76:
77: $this->load->model('catalog/category');
78:
79: $this->model_catalog_category->deleteLayoutsByLayoutId($layout_id);
80:
81: $this->load->model('catalog/product');
82:
83: $this->model_catalog_product->deleteLayoutsByLayoutId($layout_id);
84:
85: $this->load->model('catalog/information');
86:
87: $this->model_catalog_information->deleteLayoutsByLayoutId($layout_id);
88:
89: $this->load->model('cms/article');
90:
91: $this->model_cms_article->deleteLayoutsByLayoutId($layout_id);
92:
93: $this->load->model('cms/topic');
94:
95: $this->model_cms_topic->deleteLayoutsByLayoutId($layout_id);
96: }
97:
98: /**
99: * Get Layout
100: *
101: * @param int $layout_id
102: *
103: * @return array<string, mixed>
104: */
105: public function getLayout(int $layout_id): array {
106: $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "layout` WHERE `layout_id` = '" . (int)$layout_id . "'");
107:
108: return $query->row;
109: }
110:
111: /**
112: * Get Layouts
113: *
114: * @param array<string, mixed> $data
115: *
116: * @return array<int, array<string, mixed>>
117: */
118: public function getLayouts(array $data = []): array {
119: $sql = "SELECT * FROM `" . DB_PREFIX . "layout`";
120:
121: $sort_data = ['name'];
122:
123: if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
124: $sql .= " ORDER BY " . $data['sort'];
125: } else {
126: $sql .= " ORDER BY `name`";
127: }
128:
129: if (isset($data['order']) && ($data['order'] == 'DESC')) {
130: $sql .= " DESC";
131: } else {
132: $sql .= " ASC";
133: }
134:
135: if (isset($data['start']) || isset($data['limit'])) {
136: if ($data['start'] < 0) {
137: $data['start'] = 0;
138: }
139:
140: if ($data['limit'] < 1) {
141: $data['limit'] = 20;
142: }
143:
144: $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
145: }
146:
147: $query = $this->db->query($sql);
148:
149: return $query->rows;
150: }
151:
152: /**
153: * Get Total Layouts
154: *
155: * @return int
156: */
157: public function getTotalLayouts(): int {
158: $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "layout`");
159:
160: return (int)$query->row['total'];
161: }
162:
163: /**
164: * Add Route
165: *
166: * @param int $layout_id
167: * @param array<string, mixed> $data
168: *
169: * @return void
170: */
171: public function addRoute(int $layout_id, array $data): void {
172: $this->db->query("INSERT INTO `" . DB_PREFIX . "layout_route` SET `layout_id` = '" . (int)$layout_id . "', `store_id` = '" . (int)$data['store_id'] . "', `route` = '" . $this->db->escape($data['route']) . "'");
173: }
174:
175: /**
176: * Delete Routes
177: *
178: * @param int $layout_id
179: *
180: * @return void
181: */
182: public function deleteRoutes(int $layout_id): void {
183: $this->db->query("DELETE FROM `" . DB_PREFIX . "layout_route` WHERE `layout_id` = '" . (int)$layout_id . "'");
184: }
185:
186: /**
187: * Delete Routes By Layout ID
188: *
189: * @param int $layout_id
190: *
191: * @return void
192: */
193: public function deleteRoutesByLayoutId(int $layout_id): void {
194: $this->db->query("DELETE FROM `" . DB_PREFIX . "layout_route` WHERE `layout_id` = '" . (int)$layout_id . "'");
195: }
196:
197: /**
198: * Delete Routes By Store ID
199: *
200: * @param int $store_id
201: *
202: * @return void
203: */
204: public function deleteRoutesByStoreId(int $store_id): void {
205: $this->db->query("DELETE FROM `" . DB_PREFIX . "layout_route` WHERE `store_id` = '" . (int)$store_id . "'");
206: }
207:
208: /**
209: * Get Routes
210: *
211: * @param int $layout_id
212: *
213: * @return array<int, array<string, mixed>>
214: */
215: public function getRoutes(int $layout_id): array {
216: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "layout_route` WHERE `layout_id` = '" . (int)$layout_id . "'");
217:
218: return $query->rows;
219: }
220:
221: /**
222: * Get Routes By Store ID
223: *
224: * @param int $store_id
225: *
226: * @return array<int, array<string, mixed>>
227: */
228: public function getRoutesByStoreId(int $store_id): array {
229: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "layout_route` WHERE `store_id` = '" . (int)$store_id . "'");
230:
231: return $query->rows;
232: }
233:
234: /**
235: * Add Module
236: *
237: * @param int $layout_id
238: * @param array<string, mixed> $data
239: *
240: * @return void
241: */
242: public function addModule(int $layout_id, array $data): void {
243: $this->db->query("INSERT INTO `" . DB_PREFIX . "layout_module` SET `layout_id` = '" . (int)$layout_id . "', `code` = '" . $this->db->escape($data['code']) . "', `position` = '" . $this->db->escape($data['position']) . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
244: }
245:
246: /**
247: * Delete Modules
248: *
249: * @param int $layout_id
250: *
251: * @return void
252: */
253: public function deleteModules(int $layout_id): void {
254: $this->db->query("DELETE FROM `" . DB_PREFIX . "layout_module` WHERE `layout_id` = '" . (int)$layout_id . "'");
255: }
256:
257: /**
258: * Delete Modules By Code
259: *
260: * @param string $code
261: *
262: * @return void
263: */
264: public function deleteModulesByCode(string $code): void {
265: $this->db->query("DELETE FROM `" . DB_PREFIX . "layout_module` WHERE `code` = '" . $this->db->escape($code) . "' OR `code` LIKE '" . $this->db->escape($code . '.%') . "'");
266: }
267:
268: /**
269: * Get Modules
270: *
271: * @param int $layout_id
272: *
273: * @return array<int, array<string, mixed>>
274: */
275: public function getModules(int $layout_id): array {
276: $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "layout_module` WHERE `layout_id` = '" . (int)$layout_id . "' ORDER BY `position` ASC, `sort_order` ASC");
277:
278: return $query->rows;
279: }
280: }
281: