1: | <?php
|
2: | namespace Opencart\Admin\Model\Localisation;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Zone extends \Opencart\System\Engine\Model {
|
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: |
|
16: | public function addZone(array $data): int {
|
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "zone` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `code` = '" . $this->db->escape((string)$data['code']) . "', `country_id` = '" . (int)$data['country_id'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "'");
|
18: |
|
19: | $this->cache->delete('zone');
|
20: |
|
21: | return $this->db->getLastId();
|
22: | }
|
23: |
|
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: |
|
32: | public function editZone(int $zone_id, array $data): void {
|
33: | $this->db->query("UPDATE `" . DB_PREFIX . "zone` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `code` = '" . $this->db->escape((string)$data['code']) . "', `country_id` = '" . (int)$data['country_id'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "' WHERE `zone_id` = '" . (int)$zone_id . "'");
|
34: |
|
35: | $this->cache->delete('zone');
|
36: | }
|
37: |
|
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: |
|
45: | public function deleteZone(int $zone_id): void {
|
46: | $this->db->query("DELETE FROM `" . DB_PREFIX . "zone` WHERE `zone_id` = '" . (int)$zone_id . "'");
|
47: |
|
48: | $this->cache->delete('zone');
|
49: | }
|
50: |
|
51: | |
52: | |
53: | |
54: | |
55: | |
56: | |
57: |
|
58: | public function getZone(int $zone_id): array {
|
59: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "zone` WHERE `zone_id` = '" . (int)$zone_id . "'");
|
60: |
|
61: | return $query->row;
|
62: | }
|
63: |
|
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: |
|
71: | public function getZones(array $data = []): array {
|
72: | $sql = "SELECT *, `z`.`name`, `z`.`status`, `c`.`name` AS `country` FROM `" . DB_PREFIX . "zone` `z` LEFT JOIN `" . DB_PREFIX . "country` `c` ON (`z`.`country_id` = `c`.`country_id`)";
|
73: |
|
74: | $implode = [];
|
75: |
|
76: | if (!empty($data['filter_name'])) {
|
77: | $implode[] = "LCASE(`z`.`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'";
|
78: | }
|
79: |
|
80: | if (!empty($data['filter_country'])) {
|
81: | $implode[] = "LCASE(`c`.`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_country']) . '%') . "'";
|
82: | }
|
83: |
|
84: | if (!empty($data['filter_code'])) {
|
85: | $implode[] = "LCASE(`z`.`code`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_code']) . '%') . "'";
|
86: | }
|
87: |
|
88: | if ($implode) {
|
89: | $sql .= " WHERE " . implode(" AND ", $implode);
|
90: | }
|
91: |
|
92: | $sort_data = [
|
93: | 'c.name',
|
94: | 'z.name',
|
95: | 'z.code'
|
96: | ];
|
97: |
|
98: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
99: | $sql .= " ORDER BY " . $data['sort'];
|
100: | } else {
|
101: | $sql .= " ORDER BY `c`.`name`";
|
102: | }
|
103: |
|
104: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
105: | $sql .= " DESC";
|
106: | } else {
|
107: | $sql .= " ASC";
|
108: | }
|
109: |
|
110: | if (isset($data['start']) || isset($data['limit'])) {
|
111: | if ($data['start'] < 0) {
|
112: | $data['start'] = 0;
|
113: | }
|
114: |
|
115: | if ($data['limit'] < 1) {
|
116: | $data['limit'] = 20;
|
117: | }
|
118: |
|
119: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
120: | }
|
121: |
|
122: | $query = $this->db->query($sql);
|
123: |
|
124: | return $query->rows;
|
125: | }
|
126: |
|
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: |
|
134: | public function getZonesByCountryId(int $country_id): array {
|
135: | $sql = "SELECT * FROM `" . DB_PREFIX . "zone` WHERE `country_id` = '" . (int)$country_id . "' AND `status` = '1' ORDER BY `name`";
|
136: |
|
137: | $key = md5($sql);
|
138: |
|
139: | $zone_data = $this->cache->get('zone.' . $key);
|
140: |
|
141: | if (!$zone_data) {
|
142: | $query = $this->db->query($sql);
|
143: |
|
144: | $zone_data = $query->rows;
|
145: |
|
146: | $this->cache->set('zone.' . $key, $zone_data);
|
147: | }
|
148: |
|
149: | return $zone_data;
|
150: | }
|
151: |
|
152: | |
153: | |
154: | |
155: | |
156: | |
157: | |
158: |
|
159: | public function getTotalZones(array $data = []): int {
|
160: | $sql = "SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "zone` `z`";
|
161: |
|
162: | if (!empty($data['filter_country'])) {
|
163: | $sql .= " LEFT JOIN `" . DB_PREFIX . "country` `c` ON (`z`.`country_id` = `c`.`country_id`)";
|
164: | }
|
165: |
|
166: | $implode = [];
|
167: |
|
168: | if (!empty($data['filter_name'])) {
|
169: | $implode[] = "LCASE(`z`.`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'";
|
170: | }
|
171: |
|
172: | if (!empty($data['filter_country'])) {
|
173: | $implode[] = "LCASE(`c`.`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_country']) . '%') . "'";
|
174: | }
|
175: |
|
176: | if (!empty($data['filter_code'])) {
|
177: | $implode[] = "LCASE(`z`.`code`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_code']) . '%') . "'";
|
178: | }
|
179: |
|
180: | if ($implode) {
|
181: | $sql .= " WHERE " . implode(" AND ", $implode);
|
182: | }
|
183: |
|
184: | $query = $this->db->query($sql);
|
185: |
|
186: | return (int)$query->row['total'];
|
187: | }
|
188: |
|
189: | |
190: | |
191: | |
192: | |
193: | |
194: | |
195: |
|
196: | public function getTotalZonesByCountryId(int $country_id): int {
|
197: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "zone` WHERE `country_id` = '" . (int)$country_id . "'");
|
198: |
|
199: | return (int)$query->row['total'];
|
200: | }
|
201: | }
|
202: | |