1: | <?php
2: | namespace Opencart\Admin\Model\Design;
3: | /**
4: | * Class Banner
5: | *
6: | * @package Opencart\Admin\Model\Design
7: | */
8: | class Banner extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Banner
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addBanner(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "banner` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `status` = '" . (bool)($data['status'] ?? 0) . "'");
18: |
19: | $banner_id = $this->db->getLastId();
20: |
21: | if (isset($data['banner_image'])) {
22: | foreach ($data['banner_image'] as $language_id => $value) {
23: | foreach ($value as $banner_image) {
24: | $this->addImage($banner_id, $language_id, $banner_image);
25: | }
26: | }
27: | }
28: |
29: | return $banner_id;
30: | }
31: |
32: | /**
33: | * Edit Banner
34: | *
35: | * @param int $banner_id
36: | * @param array<string, mixed> $data
37: | *
38: | * @return void
39: | */
40: | public function editBanner(int $banner_id, array $data): void {
41: | $this->db->query("UPDATE `" . DB_PREFIX . "banner` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `status` = '" . (bool)($data['status'] ?? 0) . "' WHERE `banner_id` = '" . (int)$banner_id . "'");
42: |
43: | $this->deleteImages($banner_id);
44: |
45: | if (isset($data['banner_image'])) {
46: | foreach ($data['banner_image'] as $language_id => $value) {
47: | foreach ($value as $banner_image) {
48: | $this->addImage($banner_id, $language_id, $banner_image);
49: | }
50: | }
51: | }
52: | }
53: |
54: | /**
55: | * Delete Banner
56: | *
57: | * @param int $banner_id
58: | *
59: | * @return void
60: | */
61: | public function deleteBanner(int $banner_id): void {
62: | $this->db->query("DELETE FROM `" . DB_PREFIX . "banner` WHERE `banner_id` = '" . (int)$banner_id . "'");
63: |
64: | $this->deleteImages($banner_id);
65: | }
66: |
67: | /**
68: | * Get Banner
69: | *
70: | * @param int $banner_id
71: | *
72: | * @return array<string, mixed>
73: | */
74: | public function getBanner(int $banner_id): array {
75: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "banner` WHERE `banner_id` = '" . (int)$banner_id . "'");
76: |
77: | return $query->row;
78: | }
79: |
80: | /**
81: | * Get Banners
82: | *
83: | * @param array<string, mixed> $data
84: | *
85: | * @return array<int, array<string, mixed>>
86: | */
87: | public function getBanners(array $data = []): array {
88: | $sql = "SELECT * FROM `" . DB_PREFIX . "banner`";
89: |
90: | $sort_data = [
91: | 'name',
92: | 'status'
93: | ];
94: |
95: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
96: | $sql .= " ORDER BY " . $data['sort'];
97: | } else {
98: | $sql .= " ORDER BY `name`";
99: | }
100: |
101: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
102: | $sql .= " DESC";
103: | } else {
104: | $sql .= " ASC";
105: | }
106: |
107: | if (isset($data['start']) || isset($data['limit'])) {
108: | if ($data['start'] < 0) {
109: | $data['start'] = 0;
110: | }
111: |
112: | if ($data['limit'] < 1) {
113: | $data['limit'] = 20;
114: | }
115: |
116: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
117: | }
118: |
119: | $query = $this->db->query($sql);
120: |
121: | return $query->rows;
122: | }
123: |
124: | /**
125: | * Get Total Banners
126: | *
127: | * @return int
128: | */
129: | public function getTotalBanners(): int {
130: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "banner`");
131: |
132: | return (int)$query->row['total'];
133: | }
134: |
135: | /**
136: | * Add Image
137: | *
138: | * @param int $banner_id
139: | * @param int $language_id
140: | * @param array<string, mixed> $data
141: | *
142: | * @return void
143: | */
144: | public function addImage(int $banner_id, int $language_id, array $data): void {
145: | $this->db->query("INSERT INTO `" . DB_PREFIX . "banner_image` SET `banner_id` = '" . (int)$banner_id . "', `language_id` = '" . (int)$language_id . "', `title` = '" . $this->db->escape($data['title']) . "', `link` = '" . $this->db->escape($data['link']) . "', `image` = '" . $this->db->escape($data['image']) . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
146: | }
147: |
148: | /**
149: | * Delete Images
150: | *
151: | * @param int $banner_id
152: | *
153: | * @return void
154: | */
155: | public function deleteImages(int $banner_id): void {
156: | $this->db->query("DELETE FROM `" . DB_PREFIX . "banner_image` WHERE `banner_id` = '" . (int)$banner_id . "'");
157: | }
158: |
159: | /**
160: | * Delete Images By Language ID
161: | *
162: | * @param int $language_id
163: | *
164: | * @return void
165: | */
166: | public function deleteImagesByLanguageId(int $language_id): void {
167: | $this->db->query("DELETE FROM `" . DB_PREFIX . "banner_image` WHERE `language_id` = '" . (int)$language_id . "'");
168: | }
169: |
170: | /**
171: | * Get Images
172: | *
173: | * @param int $banner_id
174: | *
175: | * @return array<int, array<int, array<string, mixed>>>
176: | */
177: | public function getImages(int $banner_id): array {
178: | $banner_image_data = [];
179: |
180: | $banner_image_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "banner_image` WHERE `banner_id` = '" . (int)$banner_id . "' ORDER BY `sort_order` ASC");
181: |
182: | foreach ($banner_image_query->rows as $banner_image) {
183: | $banner_image_data[$banner_image['language_id']][] = [
184: | 'title' => $banner_image['title'],
185: | 'link' => $banner_image['link'],
186: | 'image' => $banner_image['image'],
187: | 'sort_order' => $banner_image['sort_order']
188: | ];
189: | }
190: |
191: | return $banner_image_data;
192: | }
193: |
194: | /**
195: | * Get Images By Language ID
196: | *
197: | * @param int $language_id
198: | *
199: | * @return array<int, array<string, mixed>>
200: | */
201: | public function getImagesByLanguageId(int $language_id): array {
202: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "banner_image` WHERE `language_id` = '" . (int)$language_id . "'");
203: |
204: | return $query->rows;
205: | }
206: | }
207: |

OpenCart API API documentation generated by ApiGen dev-master