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