1: | <?php
|
2: | namespace Opencart\Admin\Model\Localisation;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class GeoZone extends \Opencart\System\Engine\Model {
|
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
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: | |
34: | |
35: | |
36: | |
37: | |
38: | |
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: | |
56: | |
57: | |
58: | |
59: | |
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: | |
71: | |
72: | |
73: | |
74: | |
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: | |
84: | |
85: | |
86: | |
87: | |
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: | |
138: | |
139: | |
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: | |
149: | |
150: | |
151: | |
152: | |
153: | |
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: | |
161: | |
162: | |
163: | |
164: | |
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: | |
172: | |
173: | |
174: | |
175: | |
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: | |
185: | |
186: | |
187: | |
188: | |
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: | |
198: | |
199: | |
200: | |
201: | |
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: | |
211: | |
212: | |
213: | |
214: | |
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: | |