| 1: | <?php
|
| 2: | namespace Opencart\Admin\Controller\Customer;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class Address extends \Opencart\System\Engine\Controller {
|
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: |
|
| 14: | public function index(): void {
|
| 15: | $this->load->language('customer/customer');
|
| 16: |
|
| 17: | $this->response->setOutput($this->getAddress());
|
| 18: | }
|
| 19: |
|
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: |
|
| 25: | public function getAddress(): string {
|
| 26: | $this->load->language('customer/customer');
|
| 27: |
|
| 28: | if (isset($this->request->get['customer_id'])) {
|
| 29: | $customer_id = (int)$this->request->get['customer_id'];
|
| 30: | } else {
|
| 31: | $customer_id = 0;
|
| 32: | }
|
| 33: |
|
| 34: | $data['action'] = $this->url->link('customer/address', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $customer_id);
|
| 35: |
|
| 36: | $data['addresses'] = [];
|
| 37: |
|
| 38: | $this->load->model('customer/customer');
|
| 39: |
|
| 40: | $results = $this->model_customer_customer->getAddresses($customer_id);
|
| 41: |
|
| 42: | foreach ($results as $result) {
|
| 43: | $data['addresses'][] = [
|
| 44: | 'firstname' => $result['firstname'],
|
| 45: | 'lastname' => $result['lastname'],
|
| 46: | 'company' => $result['company'],
|
| 47: | 'address_1' => $result['address_1'],
|
| 48: | 'address_2' => $result['address_2'],
|
| 49: | 'postcode' => $result['postcode'],
|
| 50: | 'city' => $result['city'],
|
| 51: | 'zone' => $result['zone'],
|
| 52: | 'country' => $result['country'],
|
| 53: | 'default' => $result['default'],
|
| 54: | 'edit' => $this->url->link('customer/address.form', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $customer_id . '&address_id=' . $result['address_id']),
|
| 55: | 'delete' => $this->url->link('customer/address.delete', 'user_token=' . $this->session->data['user_token'] . '&address_id=' . $result['address_id'])
|
| 56: | ];
|
| 57: | }
|
| 58: |
|
| 59: | $data['address_add'] = $this->url->link('customer/address.form', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $customer_id);
|
| 60: |
|
| 61: | return $this->load->view('customer/address_list', $data);
|
| 62: | }
|
| 63: |
|
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: |
|
| 69: | public function form(): void {
|
| 70: | $this->load->language('customer/customer');
|
| 71: |
|
| 72: | if (isset($this->request->get['customer_id'])) {
|
| 73: | $customer_id = (int)$this->request->get['customer_id'];
|
| 74: | } else {
|
| 75: | $customer_id = 0;
|
| 76: | }
|
| 77: |
|
| 78: | if (!isset($this->request->get['address_id'])) {
|
| 79: | $data['heading_title'] = $this->language->get('text_address_add');
|
| 80: | } else {
|
| 81: | $data['heading_title'] = $this->language->get('text_address_edit');
|
| 82: | }
|
| 83: |
|
| 84: | $data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));
|
| 85: |
|
| 86: | $data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);
|
| 87: | $data['save'] = $this->url->link('customer/address.save', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $customer_id);
|
| 88: | $data['action'] = $this->url->link('customer/address', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . $customer_id);
|
| 89: | $data['upload'] = $this->url->link('tool/upload.upload', 'user_token=' . $this->session->data['user_token']);
|
| 90: |
|
| 91: | if (isset($this->request->get['address_id'])) {
|
| 92: | $this->load->model('customer/customer');
|
| 93: |
|
| 94: | $address_info = $this->model_customer_customer->getAddress($this->request->get['address_id']);
|
| 95: | }
|
| 96: |
|
| 97: | if (isset($this->request->get['address_id'])) {
|
| 98: | $data['address_id'] = (int)$this->request->get['address_id'];
|
| 99: | } else {
|
| 100: | $data['address_id'] = 0;
|
| 101: | }
|
| 102: |
|
| 103: | if (!empty($address_info)) {
|
| 104: | $data['firstname'] = $address_info['firstname'];
|
| 105: | } else {
|
| 106: | $data['firstname'] = '';
|
| 107: | }
|
| 108: |
|
| 109: | if (!empty($address_info)) {
|
| 110: | $data['lastname'] = $address_info['lastname'];
|
| 111: | } else {
|
| 112: | $data['lastname'] = '';
|
| 113: | }
|
| 114: |
|
| 115: | if (!empty($address_info)) {
|
| 116: | $data['company'] = $address_info['company'];
|
| 117: | } else {
|
| 118: | $data['company'] = '';
|
| 119: | }
|
| 120: |
|
| 121: | if (!empty($address_info)) {
|
| 122: | $data['address_1'] = $address_info['address_1'];
|
| 123: | } else {
|
| 124: | $data['address_1'] = '';
|
| 125: | }
|
| 126: |
|
| 127: | if (!empty($address_info)) {
|
| 128: | $data['address_2'] = $address_info['address_2'];
|
| 129: | } else {
|
| 130: | $data['address_2'] = '';
|
| 131: | }
|
| 132: |
|
| 133: | if (!empty($address_info)) {
|
| 134: | $data['postcode'] = $address_info['postcode'];
|
| 135: | } else {
|
| 136: | $data['postcode'] = '';
|
| 137: | }
|
| 138: |
|
| 139: | if (!empty($address_info)) {
|
| 140: | $data['city'] = $address_info['city'];
|
| 141: | } else {
|
| 142: | $data['city'] = '';
|
| 143: | }
|
| 144: |
|
| 145: | if (!empty($address_info)) {
|
| 146: | $data['country_id'] = $address_info['country_id'];
|
| 147: | } else {
|
| 148: | $data['country_id'] = $this->config->get('config_country_id');
|
| 149: | }
|
| 150: |
|
| 151: | if (!empty($address_info)) {
|
| 152: | $data['zone_id'] = $address_info['zone_id'];
|
| 153: | } else {
|
| 154: | $data['zone_id'] = '';
|
| 155: | }
|
| 156: |
|
| 157: | $this->load->model('localisation/country');
|
| 158: |
|
| 159: | $data['countries'] = $this->model_localisation_country->getCountries();
|
| 160: |
|
| 161: |
|
| 162: | $data['custom_fields'] = [];
|
| 163: |
|
| 164: | $filter_data = [
|
| 165: | 'filter_location' => 'address',
|
| 166: | 'sort' => 'cf.sort_order',
|
| 167: | 'order' => 'ASC'
|
| 168: | ];
|
| 169: |
|
| 170: | $this->load->model('customer/custom_field');
|
| 171: |
|
| 172: | $custom_fields = $this->model_customer_custom_field->getCustomFields($filter_data);
|
| 173: |
|
| 174: | foreach ($custom_fields as $custom_field) {
|
| 175: | $data['custom_fields'][] = [
|
| 176: | 'custom_field_id' => $custom_field['custom_field_id'],
|
| 177: | 'custom_field_value' => $this->model_customer_custom_field->getValues($custom_field['custom_field_id']),
|
| 178: | 'name' => $custom_field['name'],
|
| 179: | 'value' => $custom_field['value'],
|
| 180: | 'type' => $custom_field['type'],
|
| 181: | 'location' => $custom_field['location'],
|
| 182: | 'sort_order' => $custom_field['sort_order']
|
| 183: | ];
|
| 184: | }
|
| 185: |
|
| 186: | if (!empty($address_info)) {
|
| 187: | $data['address_custom_field'] = $address_info['custom_field'];
|
| 188: | } else {
|
| 189: | $data['address_custom_field'] = [];
|
| 190: | }
|
| 191: |
|
| 192: | if (isset($this->request->get['address_id'])) {
|
| 193: | $data['default'] = $address_info['default'];
|
| 194: | } else {
|
| 195: | $data['default'] = true;
|
| 196: | }
|
| 197: |
|
| 198: | $data['user_token'] = $this->session->data['user_token'];
|
| 199: |
|
| 200: | $this->response->setOutput($this->load->view('customer/address_form', $data));
|
| 201: | }
|
| 202: |
|
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: |
|
| 208: | public function save(): void {
|
| 209: | $this->load->language('customer/customer');
|
| 210: |
|
| 211: | $json = [];
|
| 212: |
|
| 213: | if (isset($this->request->get['customer_id'])) {
|
| 214: | $customer_id = (int)$this->request->get['customer_id'];
|
| 215: | } else {
|
| 216: | $customer_id = 0;
|
| 217: | }
|
| 218: |
|
| 219: | if (!$this->user->hasPermission('modify', 'customer/customer')) {
|
| 220: | $json['error']['warning'] = $this->language->get('error_permission');
|
| 221: | }
|
| 222: |
|
| 223: | $this->load->model('customer/customer');
|
| 224: |
|
| 225: | $customer_info = $this->model_customer_customer->getCustomer($customer_id);
|
| 226: |
|
| 227: | if (!$customer_info) {
|
| 228: | $json['error']['warning'] = $this->language->get('error_customer');
|
| 229: | }
|
| 230: |
|
| 231: | if (!$json) {
|
| 232: | if (!oc_validate_length($this->request->post['firstname'], 1, 32)) {
|
| 233: | $json['error']['address_firstname'] = $this->language->get('error_firstname');
|
| 234: | }
|
| 235: |
|
| 236: | if (!oc_validate_length($this->request->post['lastname'], 1, 32)) {
|
| 237: | $json['error']['address_lastname'] = $this->language->get('error_lastname');
|
| 238: | }
|
| 239: |
|
| 240: | if (!oc_validate_length($this->request->post['address_1'], 3, 128)) {
|
| 241: | $json['error']['address_address_1'] = $this->language->get('error_address_1');
|
| 242: | }
|
| 243: |
|
| 244: | if (!oc_validate_length($this->request->post['city'], 2, 128)) {
|
| 245: | $json['error']['address_city'] = $this->language->get('error_city');
|
| 246: | }
|
| 247: |
|
| 248: | $this->load->model('localisation/country');
|
| 249: |
|
| 250: | $country_info = $this->model_localisation_country->getCountry((int)$this->request->post['country_id']);
|
| 251: |
|
| 252: | if ($country_info && $country_info['postcode_required'] && (oc_strlen($this->request->post['postcode']) < 2 || oc_strlen($this->request->post['postcode']) > 10)) {
|
| 253: | $json['error']['address_postcode'] = $this->language->get('error_postcode');
|
| 254: | }
|
| 255: |
|
| 256: | if (!$country_info || $this->request->post['country_id'] == '') {
|
| 257: | $json['error']['address_country'] = $this->language->get('error_country');
|
| 258: | }
|
| 259: |
|
| 260: | if ($this->request->post['zone_id'] == '') {
|
| 261: | $json['error']['address_zone'] = $this->language->get('error_zone');
|
| 262: | }
|
| 263: |
|
| 264: | $filter_data = [
|
| 265: | 'filter_location' => 'address',
|
| 266: | 'filter_customer_group_id' => $customer_info['customer_group_id'],
|
| 267: | 'filter_status' => 1
|
| 268: | ];
|
| 269: |
|
| 270: | $this->load->model('customer/custom_field');
|
| 271: |
|
| 272: | $custom_fields = $this->model_customer_custom_field->getCustomFields($filter_data);
|
| 273: |
|
| 274: | foreach ($custom_fields as $custom_field) {
|
| 275: | if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
|
| 276: | $json['error']['address_custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
|
| 277: | } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
|
| 278: | $json['error']['address_custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
|
| 279: | }
|
| 280: | }
|
| 281: | }
|
| 282: |
|
| 283: | if (!$json) {
|
| 284: | $this->load->model('customer/customer');
|
| 285: |
|
| 286: | if (!$this->request->post['address_id']) {
|
| 287: | $this->model_customer_customer->addAddress($customer_id, $this->request->post);
|
| 288: | } else {
|
| 289: | $this->model_customer_customer->editAddress($customer_id, $this->request->post['address_id'], $this->request->post);
|
| 290: | }
|
| 291: |
|
| 292: | $json['success'] = $this->language->get('text_success');
|
| 293: | }
|
| 294: |
|
| 295: | $this->response->addHeader('Content-Type: application/json');
|
| 296: | $this->response->setOutput(json_encode($json));
|
| 297: | }
|
| 298: |
|
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: |
|
| 304: | public function delete(): void {
|
| 305: | $this->load->language('customer/customer');
|
| 306: |
|
| 307: | $json = [];
|
| 308: |
|
| 309: | if (isset($this->request->get['address_id'])) {
|
| 310: | $address_id = (int)$this->request->get['address_id'];
|
| 311: | } else {
|
| 312: | $address_id = 0;
|
| 313: | }
|
| 314: |
|
| 315: | if (!$this->user->hasPermission('modify', 'customer/customer')) {
|
| 316: | $json['error'] = $this->language->get('error_permission');
|
| 317: | }
|
| 318: |
|
| 319: | $this->load->model('customer/customer');
|
| 320: |
|
| 321: | $address_info = $this->model_customer_customer->getAddress($address_id);
|
| 322: |
|
| 323: | if (!$address_info) {
|
| 324: | $json['error'] = $this->language->get('error_address');
|
| 325: | }
|
| 326: |
|
| 327: | if (!$json) {
|
| 328: | $this->model_customer_customer->deleteAddress($address_info['customer_id'], $address_id);
|
| 329: |
|
| 330: | $json['success'] = $this->language->get('text_success');
|
| 331: | }
|
| 332: |
|
| 333: | $this->response->addHeader('Content-Type: application/json');
|
| 334: | $this->response->setOutput(json_encode($json));
|
| 335: | }
|
| 336: |
|
| 337: | |
| 338: | |
| 339: | |
| 340: | |
| 341: |
|
| 342: | public function address(): void {
|
| 343: | $this->load->language('customer/customer');
|
| 344: |
|
| 345: | $json = [];
|
| 346: |
|
| 347: | if (isset($this->request->get['address_id'])) {
|
| 348: | $address_id = (int)$this->request->get['address_id'];
|
| 349: | } else {
|
| 350: | $address_id = 0;
|
| 351: | }
|
| 352: |
|
| 353: | $this->load->model('customer/customer');
|
| 354: |
|
| 355: | $address_info = $this->model_customer_customer->getAddress($address_id);
|
| 356: |
|
| 357: | if (!$address_info) {
|
| 358: | $json['error'] = $this->language->get('error_address');
|
| 359: | }
|
| 360: |
|
| 361: | if (!$json) {
|
| 362: | $json = $address_info;
|
| 363: | }
|
| 364: |
|
| 365: | $this->response->addHeader('Content-Type: application/json');
|
| 366: | $this->response->setOutput(json_encode($json));
|
| 367: | }
|
| 368: | }
|
| 369: | |