| 1: | <?php
|
| 2: | namespace Opencart\Catalog\Model\Account;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class Address extends \Opencart\System\Engine\Model {
|
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: |
|
| 17: | public function addAddress(int $customer_id, array $data): int {
|
| 18: | $this->db->query("INSERT INTO `" . DB_PREFIX . "address` SET `customer_id` = '" . (int)$customer_id . "', `firstname` = '" . $this->db->escape($data['firstname']) . "', `lastname` = '" . $this->db->escape($data['lastname']) . "', `company` = '" . $this->db->escape($data['company']) . "', `address_1` = '" . $this->db->escape($data['address_1']) . "', `address_2` = '" . $this->db->escape($data['address_2']) . "', `postcode` = '" . $this->db->escape($data['postcode']) . "', `city` = '" . $this->db->escape($data['city']) . "', `zone_id` = '" . (int)$data['zone_id'] . "', `country_id` = '" . (int)$data['country_id'] . "', `custom_field` = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "', `default` = '" . (isset($data['default']) ? (int)$data['default'] : 0) . "'");
|
| 19: |
|
| 20: | $address_id = $this->db->getLastId();
|
| 21: |
|
| 22: | if (!empty($data['default'])) {
|
| 23: | $this->db->query("UPDATE `" . DB_PREFIX . "address` SET `default` = '0' WHERE `address_id` != '" . (int)$address_id . "' AND `customer_id` = '" . (int)$customer_id . "'");
|
| 24: | }
|
| 25: |
|
| 26: | return $address_id;
|
| 27: | }
|
| 28: |
|
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: |
|
| 38: | public function editAddress(int $customer_id, int $address_id, array $data): void {
|
| 39: | $this->db->query("UPDATE `" . DB_PREFIX . "address` SET `firstname` = '" . $this->db->escape($data['firstname']) . "', `lastname` = '" . $this->db->escape($data['lastname']) . "', `company` = '" . $this->db->escape($data['company']) . "', `address_1` = '" . $this->db->escape($data['address_1']) . "', `address_2` = '" . $this->db->escape($data['address_2']) . "', `postcode` = '" . $this->db->escape($data['postcode']) . "', `city` = '" . $this->db->escape($data['city']) . "', `zone_id` = '" . (int)$data['zone_id'] . "', `country_id` = '" . (int)$data['country_id'] . "', `custom_field` = '" . $this->db->escape(isset($data['custom_field']) ? json_encode($data['custom_field']) : '') . "', `default` = '" . (isset($data['default']) ? (int)$data['default'] : 0) . "' WHERE `address_id` = '" . (int)$address_id . "' AND `customer_id` = '" . (int)$customer_id . "'");
|
| 40: |
|
| 41: | if (!empty($data['default'])) {
|
| 42: | $this->db->query("UPDATE `" . DB_PREFIX . "address` SET `default` = '0' WHERE `address_id` != '" . (int)$address_id . "' AND `customer_id` = '" . (int)$customer_id . "'");
|
| 43: | }
|
| 44: | }
|
| 45: |
|
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: |
|
| 54: | public function deleteAddress(int $customer_id, int $address_id = 0): void {
|
| 55: | $sql = "DELETE FROM `" . DB_PREFIX . "address` WHERE `customer_id` = '" . (int)$customer_id . "'";
|
| 56: |
|
| 57: | if ($address_id) {
|
| 58: | $sql .= " AND `address_id` = '" . (int)$address_id . "'";
|
| 59: | }
|
| 60: |
|
| 61: | $this->db->query($sql);
|
| 62: | }
|
| 63: |
|
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: |
|
| 72: | public function getAddress(int $customer_id, int $address_id): array {
|
| 73: | $address_query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "address` WHERE `address_id` = '" . (int)$address_id . "' AND `customer_id` = '" . (int)$customer_id . "'");
|
| 74: |
|
| 75: | if ($address_query->num_rows) {
|
| 76: | $this->load->model('localisation/country');
|
| 77: |
|
| 78: | $country_info = $this->model_localisation_country->getCountry($address_query->row['country_id']);
|
| 79: |
|
| 80: | if ($country_info) {
|
| 81: | $country = $country_info['name'];
|
| 82: | $iso_code_2 = $country_info['iso_code_2'];
|
| 83: | $iso_code_3 = $country_info['iso_code_3'];
|
| 84: | $address_format = $country_info['address_format'];
|
| 85: | } else {
|
| 86: | $country = '';
|
| 87: | $iso_code_2 = '';
|
| 88: | $iso_code_3 = '';
|
| 89: | $address_format = '';
|
| 90: | }
|
| 91: |
|
| 92: | $this->load->model('localisation/zone');
|
| 93: |
|
| 94: | $zone_info = $this->model_localisation_zone->getZone($address_query->row['zone_id']);
|
| 95: |
|
| 96: | if ($zone_info) {
|
| 97: | $zone = $zone_info['name'];
|
| 98: | $zone_code = $zone_info['code'];
|
| 99: | } else {
|
| 100: | $zone = '';
|
| 101: | $zone_code = '';
|
| 102: | }
|
| 103: |
|
| 104: | return [
|
| 105: | 'address_id' => $address_query->row['address_id'],
|
| 106: | 'firstname' => $address_query->row['firstname'],
|
| 107: | 'lastname' => $address_query->row['lastname'],
|
| 108: | 'company' => $address_query->row['company'],
|
| 109: | 'address_1' => $address_query->row['address_1'],
|
| 110: | 'address_2' => $address_query->row['address_2'],
|
| 111: | 'city' => $address_query->row['city'],
|
| 112: | 'postcode' => $address_query->row['postcode'],
|
| 113: | 'zone_id' => $address_query->row['zone_id'],
|
| 114: | 'zone' => $zone,
|
| 115: | 'zone_code' => $zone_code,
|
| 116: | 'country_id' => $address_query->row['country_id'],
|
| 117: | 'country' => $country,
|
| 118: | 'iso_code_2' => $iso_code_2,
|
| 119: | 'iso_code_3' => $iso_code_3,
|
| 120: | 'address_format' => $address_format,
|
| 121: | 'custom_field' => json_decode($address_query->row['custom_field'], true),
|
| 122: | 'default' => $address_query->row['default']
|
| 123: | ];
|
| 124: | } else {
|
| 125: | return [];
|
| 126: | }
|
| 127: | }
|
| 128: |
|
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: |
|
| 136: | public function getAddresses(int $customer_id): array {
|
| 137: | $address_data = [];
|
| 138: |
|
| 139: | $query = $this->db->query("SELECT `address_id` FROM `" . DB_PREFIX . "address` WHERE `customer_id` = '" . (int)$customer_id . "'");
|
| 140: |
|
| 141: | foreach ($query->rows as $result) {
|
| 142: | $address_info = $this->getAddress($customer_id, $result['address_id']);
|
| 143: |
|
| 144: | if ($address_info) {
|
| 145: | $address_data[$result['address_id']] = $address_info;
|
| 146: | }
|
| 147: | }
|
| 148: |
|
| 149: | return $address_data;
|
| 150: | }
|
| 151: |
|
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: |
|
| 159: | public function getTotalAddresses(int $customer_id): int {
|
| 160: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "address` WHERE `customer_id` = '" . (int)$customer_id . "'");
|
| 161: |
|
| 162: | return (int)$query->row['total'];
|
| 163: | }
|
| 164: | }
|
| 165: | |