1: | <?php
2: | namespace Opencart\Admin\Model\Localisation;
3: | /**
4: | * Class GeoZone
5: | *
6: | * @package Opencart\Admin\Model\Localisation
7: | */
8: | class GeoZone extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Geo Zone
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addGeoZone(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "geo_zone` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `description` = '" . $this->db->escape((string)$data['description']) . "'");
18: |
19: | $geo_zone_id = $this->db->getLastId();
20: |
21: | if (isset($data['zone_to_geo_zone'])) {
22: | foreach ($data['zone_to_geo_zone'] as $zone_to_geo_zone) {
23: | $this->addZone($geo_zone_id, $zone_to_geo_zone);
24: | }
25: | }
26: |
27: | $this->cache->delete('geo_zone');
28: |
29: | return $geo_zone_id;
30: | }
31: |
32: | /**
33: | * Edit Geo Zone
34: | *
35: | * @param int $geo_zone_id
36: | * @param array<string, mixed> $data
37: | *
38: | * @return void
39: | */
40: | public function editGeoZone(int $geo_zone_id, array $data): void {
41: | $this->db->query("UPDATE `" . DB_PREFIX . "geo_zone` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `description` = '" . $this->db->escape((string)$data['description']) . "' WHERE `geo_zone_id` = '" . (int)$geo_zone_id . "'");
42: |
43: | $this->deleteZones($geo_zone_id);
44: |
45: | if (isset($data['zone_to_geo_zone'])) {
46: | foreach ($data['zone_to_geo_zone'] as $zone_to_geo_zone) {
47: | $this->addZone($geo_zone_id, $zone_to_geo_zone);
48: | }
49: | }
50: |
51: | $this->cache->delete('geo_zone');
52: | }
53: |
54: | /**
55: | * Delete Geo Zone
56: | *
57: | * @param int $geo_zone_id
58: | *
59: | * @return void
60: | */
61: | public function deleteGeoZone(int $geo_zone_id): void {
62: | $this->db->query("DELETE FROM `" . DB_PREFIX . "geo_zone` WHERE `geo_zone_id` = '" . (int)$geo_zone_id . "'");
63: |
64: | $this->deleteZones($geo_zone_id);
65: |
66: | $this->cache->delete('geo_zone');
67: | }
68: |
69: | /**
70: | * Get Geo Zone
71: | *
72: | * @param int $geo_zone_id
73: | *
74: | * @return array<string, mixed>
75: | */
76: | public function getGeoZone(int $geo_zone_id): array {
77: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "geo_zone` WHERE `geo_zone_id` = '" . (int)$geo_zone_id . "'");
78: |
79: | return $query->row;
80: | }
81: |
82: | /**
83: | * Get Geo Zones
84: | *
85: | * @param array<string, mixed> $data
86: | *
87: | * @return array<int, array<string, mixed>>
88: | */
89: | public function getGeoZones(array $data = []): array {
90: | $sql = "SELECT * FROM `" . DB_PREFIX . "geo_zone`";
91: |
92: | $sort_data = [
93: | 'name',
94: | 'description'
95: | ];
96: |
97: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
98: | $sql .= " ORDER BY " . $data['sort'];
99: | } else {
100: | $sql .= " ORDER BY `name`";
101: | }
102: |
103: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
104: | $sql .= " DESC";
105: | } else {
106: | $sql .= " ASC";
107: | }
108: |
109: | if (isset($data['start']) || isset($data['limit'])) {
110: | if ($data['start'] < 0) {
111: | $data['start'] = 0;
112: | }
113: |
114: | if ($data['limit'] < 1) {
115: | $data['limit'] = 20;
116: | }
117: |
118: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
119: | }
120: |
121: | $key = md5($sql);
122: |
123: | $geo_zone_data = $this->cache->get('geo_zone.' . $key);
124: |
125: | if (!$geo_zone_data) {
126: | $query = $this->db->query($sql);
127: |
128: | $geo_zone_data = $query->rows;
129: |
130: | $this->cache->set('geo_zone.' . $key, $geo_zone_data);
131: | }
132: |
133: | return $geo_zone_data;
134: | }
135: |
136: | /**
137: | * Get Total Geo Zones
138: | *
139: | * @return int
140: | */
141: | public function getTotalGeoZones(): int {
142: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "geo_zone`");
143: |
144: | return (int)$query->row['total'];
145: | }
146: |
147: | /**
148: | * Add Zone
149: | *
150: | * @param int $geo_zone_id
151: | * @param array<string, mixed> $data
152: | *
153: | * @return void
154: | */
155: | public function addZone(int $geo_zone_id, array $data): void {
156: | $this->db->query("INSERT INTO `" . DB_PREFIX . "zone_to_geo_zone` SET `geo_zone_id` = '" . (int)$geo_zone_id . "', `country_id` = '" . (int)$data['country_id'] . "', `zone_id` = '" . (int)$data['zone_id'] . "'");
157: | }
158: |
159: | /**
160: | * Delete Zones
161: | *
162: | * @param int $geo_zone_id
163: | *
164: | * @return void
165: | */
166: | public function deleteZones(int $geo_zone_id): void {
167: | $this->db->query("DELETE FROM `" . DB_PREFIX . "zone_to_geo_zone` WHERE `geo_zone_id` = '" . (int)$geo_zone_id . "'");
168: | }
169: |
170: | /**
171: | * Get Zones
172: | *
173: | * @param int $geo_zone_id
174: | *
175: | * @return array<int, array<string, mixed>>
176: | */
177: | public function getZones(int $geo_zone_id): array {
178: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone_to_geo_zone` WHERE `geo_zone_id` = '" . (int)$geo_zone_id . "'");
179: |
180: | return $query->rows;
181: | }
182: |
183: | /**
184: | * Get Total Zones
185: | *
186: | * @param int $geo_zone_id
187: | *
188: | * @return int
189: | */
190: | public function getTotalZones(int $geo_zone_id): int {
191: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "zone_to_geo_zone` WHERE `geo_zone_id` = '" . (int)$geo_zone_id . "'");
192: |
193: | return (int)$query->row['total'];
194: | }
195: |
196: | /**
197: | * Get Total Zones By Country ID
198: | *
199: | * @param int $country_id
200: | *
201: | * @return int
202: | */
203: | public function getTotalZonesByCountryId(int $country_id): int {
204: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "zone_to_geo_zone` WHERE `country_id` = '" . (int)$country_id . "'");
205: |
206: | return (int)$query->row['total'];
207: | }
208: |
209: | /**
210: | * Get Total Zones By Zone ID
211: | *
212: | * @param int $zone_id
213: | *
214: | * @return int
215: | */
216: | public function getTotalZonesByZoneId(int $zone_id): int {
217: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "zone_to_geo_zone` WHERE `zone_id` = '" . (int)$zone_id . "'");
218: |
219: | return (int)$query->row['total'];
220: | }
221: | }
222: |

OpenCart API API documentation generated by ApiGen dev-master