1: | <?php
|
2: | namespace Opencart\Catalog\Controller\Account;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Address extends \Opencart\System\Engine\Controller {
|
9: | |
10: | |
11: |
|
12: | public function index(): void {
|
13: | $this->load->language('account/address');
|
14: |
|
15: | if (!$this->customer->isLogged() || (!isset($this->request->get['customer_token']) || !isset($this->session->data['customer_token']) || ($this->request->get['customer_token'] != $this->session->data['customer_token']))) {
|
16: | $this->session->data['redirect'] = $this->url->link('account/address', 'language=' . $this->config->get('config_language'));
|
17: |
|
18: | $this->response->redirect($this->url->link('account/login', 'language=' . $this->config->get('config_language'), true));
|
19: | }
|
20: |
|
21: | $this->document->setTitle($this->language->get('heading_title'));
|
22: |
|
23: | $data['breadcrumbs'][] = [
|
24: | 'text' => $this->language->get('text_home'),
|
25: | 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
|
26: | ];
|
27: |
|
28: | $data['breadcrumbs'][] = [
|
29: | 'text' => $this->language->get('text_account'),
|
30: | 'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
|
31: | ];
|
32: |
|
33: | $data['breadcrumbs'][] = [
|
34: | 'text' => $this->language->get('heading_title'),
|
35: | 'href' => $this->url->link('account/address', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
|
36: | ];
|
37: |
|
38: | if (isset($this->session->data['success'])) {
|
39: | $data['success'] = $this->session->data['success'];
|
40: |
|
41: | unset($this->session->data['success']);
|
42: | } else {
|
43: | $data['success'] = '';
|
44: | }
|
45: |
|
46: | $data['add'] = $this->url->link('account/address.form', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']);
|
47: | $data['back'] = $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']);
|
48: |
|
49: | $data['list'] = $this->getList();
|
50: |
|
51: | $data['language'] = $this->config->get('config_language');
|
52: |
|
53: | $data['customer_token'] = $this->session->data['customer_token'];
|
54: |
|
55: | $data['column_left'] = $this->load->controller('common/column_left');
|
56: | $data['column_right'] = $this->load->controller('common/column_right');
|
57: | $data['content_top'] = $this->load->controller('common/content_top');
|
58: | $data['content_bottom'] = $this->load->controller('common/content_bottom');
|
59: | $data['footer'] = $this->load->controller('common/footer');
|
60: | $data['header'] = $this->load->controller('common/header');
|
61: |
|
62: | $this->response->setOutput($this->load->view('account/address', $data));
|
63: | }
|
64: |
|
65: | |
66: | |
67: | |
68: | |
69: |
|
70: | public function list(): void {
|
71: | $this->load->language('account/address');
|
72: |
|
73: | if (!$this->customer->isLogged() || (!isset($this->request->get['customer_token']) || !isset($this->session->data['customer_token']) || ($this->request->get['customer_token'] != $this->session->data['customer_token']))) {
|
74: | $this->session->data['redirect'] = $this->url->link('account/address', 'language=' . $this->config->get('config_language'));
|
75: |
|
76: | $this->response->redirect($this->url->link('account/login', 'language=' . $this->config->get('config_language'), true));
|
77: | }
|
78: |
|
79: | $this->response->setOutput($this->getList());
|
80: | }
|
81: |
|
82: | |
83: | |
84: | |
85: | |
86: |
|
87: | protected function getList(): string {
|
88: | $data['addresses'] = [];
|
89: |
|
90: | $this->load->model('account/address');
|
91: |
|
92: | $results = $this->model_account_address->getAddresses($this->customer->getId());
|
93: |
|
94: | foreach ($results as $result) {
|
95: | $find = [
|
96: | '{firstname}',
|
97: | '{lastname}',
|
98: | '{company}',
|
99: | '{address_1}',
|
100: | '{address_2}',
|
101: | '{city}',
|
102: | '{postcode}',
|
103: | '{zone}',
|
104: | '{zone_code}',
|
105: | '{country}'
|
106: | ];
|
107: |
|
108: | $replace = [
|
109: | 'firstname' => $result['firstname'],
|
110: | 'lastname' => $result['lastname'],
|
111: | 'company' => $result['company'],
|
112: | 'address_1' => $result['address_1'],
|
113: | 'address_2' => $result['address_2'],
|
114: | 'city' => $result['city'],
|
115: | 'postcode' => $result['postcode'],
|
116: | 'zone' => $result['zone'],
|
117: | 'zone_code' => $result['zone_code'],
|
118: | 'country' => $result['country']
|
119: | ];
|
120: |
|
121: | $data['addresses'][] = [
|
122: | 'address_id' => $result['address_id'],
|
123: | 'address' => str_replace(["\r\n", "\r", "\n"], '<br/>', preg_replace(["/\\s\\s+/", "/\r\r+/", "/\n\n+/"], '<br/>', trim(str_replace($find, $replace, $result['address_format'])))),
|
124: | 'edit' => $this->url->link('account/address.form', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . '&address_id=' . $result['address_id']),
|
125: | 'delete' => $this->url->link('account/address.delete', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . '&address_id=' . $result['address_id'])
|
126: | ];
|
127: | }
|
128: |
|
129: | return $this->load->view('account/address_list', $data);
|
130: | }
|
131: |
|
132: | |
133: | |
134: | |
135: | |
136: |
|
137: | public function form(): void {
|
138: | $this->load->language('account/address');
|
139: |
|
140: | if (!$this->customer->isLogged() || (!isset($this->request->get['customer_token']) || !isset($this->session->data['customer_token']) || ($this->request->get['customer_token'] != $this->session->data['customer_token']))) {
|
141: | $this->session->data['redirect'] = $this->url->link('account/address', 'language=' . $this->config->get('config_language'));
|
142: |
|
143: | $this->response->redirect($this->url->link('account/login', 'language=' . $this->config->get('config_language'), true));
|
144: | }
|
145: |
|
146: | $this->document->setTitle($this->language->get('heading_title'));
|
147: |
|
148: | $data['text_address'] = !isset($this->request->get['address_id']) ? $this->language->get('text_address_add') : $this->language->get('text_address_edit');
|
149: |
|
150: | $data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));
|
151: |
|
152: | $data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);
|
153: |
|
154: | $data['breadcrumbs'] = [];
|
155: |
|
156: | $data['breadcrumbs'][] = [
|
157: | 'text' => $this->language->get('text_home'),
|
158: | 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
|
159: | ];
|
160: |
|
161: | $data['breadcrumbs'][] = [
|
162: | 'text' => $this->language->get('text_account'),
|
163: | 'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
|
164: | ];
|
165: |
|
166: | $data['breadcrumbs'][] = [
|
167: | 'text' => $this->language->get('heading_title'),
|
168: | 'href' => $this->url->link('account/address', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
|
169: | ];
|
170: |
|
171: | if (!isset($this->request->get['address_id'])) {
|
172: | $data['breadcrumbs'][] = [
|
173: | 'text' => $this->language->get('text_address_add'),
|
174: | 'href' => $this->url->link('account/address.form', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
|
175: | ];
|
176: | } else {
|
177: | $data['breadcrumbs'][] = [
|
178: | 'text' => $this->language->get('text_address_edit'),
|
179: | 'href' => $this->url->link('account/address.form', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . '&address_id=' . $this->request->get['address_id'])
|
180: | ];
|
181: | }
|
182: |
|
183: | if (!isset($this->request->get['address_id'])) {
|
184: | $data['save'] = $this->url->link('account/address.save', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']);
|
185: | } else {
|
186: | $data['save'] = $this->url->link('account/address.save', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'] . '&address_id=' . $this->request->get['address_id']);
|
187: | }
|
188: |
|
189: | $this->session->data['upload_token'] = oc_token(32);
|
190: |
|
191: | $data['upload'] = $this->url->link('tool/upload', 'language=' . $this->config->get('config_language') . '&upload_token=' . $this->session->data['upload_token']);
|
192: |
|
193: | if (isset($this->request->get['address_id'])) {
|
194: | $this->load->model('account/address');
|
195: |
|
196: | $address_info = $this->model_account_address->getAddress($this->customer->getId(), $this->request->get['address_id']);
|
197: | }
|
198: |
|
199: | if (!empty($address_info)) {
|
200: | $data['firstname'] = $address_info['firstname'];
|
201: | } else {
|
202: | $data['firstname'] = '';
|
203: | }
|
204: |
|
205: | if (!empty($address_info)) {
|
206: | $data['lastname'] = $address_info['lastname'];
|
207: | } else {
|
208: | $data['lastname'] = '';
|
209: | }
|
210: |
|
211: | if (!empty($address_info)) {
|
212: | $data['company'] = $address_info['company'];
|
213: | } else {
|
214: | $data['company'] = '';
|
215: | }
|
216: |
|
217: | if (!empty($address_info)) {
|
218: | $data['address_1'] = $address_info['address_1'];
|
219: | } else {
|
220: | $data['address_1'] = '';
|
221: | }
|
222: |
|
223: | if (!empty($address_info)) {
|
224: | $data['address_2'] = $address_info['address_2'];
|
225: | } else {
|
226: | $data['address_2'] = '';
|
227: | }
|
228: |
|
229: | if (!empty($address_info)) {
|
230: | $data['postcode'] = $address_info['postcode'];
|
231: | } else {
|
232: | $data['postcode'] = '';
|
233: | }
|
234: |
|
235: | if (!empty($address_info)) {
|
236: | $data['city'] = $address_info['city'];
|
237: | } else {
|
238: | $data['city'] = '';
|
239: | }
|
240: |
|
241: | if (!empty($address_info)) {
|
242: | $data['country_id'] = $address_info['country_id'];
|
243: | } else {
|
244: | $data['country_id'] = $this->config->get('config_country_id');
|
245: | }
|
246: |
|
247: | if (!empty($address_info)) {
|
248: | $data['zone_id'] = $address_info['zone_id'];
|
249: | } else {
|
250: | $data['zone_id'] = '';
|
251: | }
|
252: |
|
253: | $this->load->model('localisation/country');
|
254: |
|
255: | $data['countries'] = $this->model_localisation_country->getCountries();
|
256: |
|
257: |
|
258: | $data['custom_fields'] = [];
|
259: |
|
260: | $this->load->model('account/custom_field');
|
261: |
|
262: | $custom_fields = $this->model_account_custom_field->getCustomFields($this->customer->getGroupId());
|
263: |
|
264: | foreach ($custom_fields as $custom_field) {
|
265: | if ($custom_field['location'] == 'address') {
|
266: | $data['custom_fields'][] = $custom_field;
|
267: | }
|
268: | }
|
269: |
|
270: | if (!empty($address_info)) {
|
271: | $data['address_custom_field'] = $address_info['custom_field'];
|
272: | } else {
|
273: | $data['address_custom_field'] = [];
|
274: | }
|
275: |
|
276: | if (isset($this->request->get['address_id'])) {
|
277: | $data['default'] = $address_info['default'];
|
278: | } else {
|
279: | $data['default'] = false;
|
280: | }
|
281: |
|
282: | $data['back'] = $this->url->link('account/address', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']);
|
283: |
|
284: | $data['language'] = $this->config->get('config_language');
|
285: |
|
286: | $data['column_left'] = $this->load->controller('common/column_left');
|
287: | $data['column_right'] = $this->load->controller('common/column_right');
|
288: | $data['content_top'] = $this->load->controller('common/content_top');
|
289: | $data['content_bottom'] = $this->load->controller('common/content_bottom');
|
290: | $data['footer'] = $this->load->controller('common/footer');
|
291: | $data['header'] = $this->load->controller('common/header');
|
292: |
|
293: | $this->response->setOutput($this->load->view('account/address_form', $data));
|
294: | }
|
295: |
|
296: | |
297: | |
298: | |
299: | |
300: |
|
301: | public function save(): void {
|
302: | $this->load->language('account/address');
|
303: |
|
304: | $json = [];
|
305: |
|
306: | if (!$this->customer->isLogged() || (!isset($this->request->get['customer_token']) || !isset($this->session->data['customer_token']) || ($this->request->get['customer_token'] != $this->session->data['customer_token']))) {
|
307: | $this->session->data['redirect'] = $this->url->link('account/address', 'language=' . $this->config->get('config_language'));
|
308: |
|
309: | $json['redirect'] = $this->url->link('account/login', 'language=' . $this->config->get('config_language'), true);
|
310: | }
|
311: |
|
312: | if (!$json) {
|
313: | $keys = [
|
314: | 'firstname',
|
315: | 'lastname',
|
316: | 'address_1',
|
317: | 'address_2',
|
318: | 'city',
|
319: | 'postcode',
|
320: | 'country_id',
|
321: | 'zone_id'
|
322: | ];
|
323: |
|
324: | foreach ($keys as $key) {
|
325: | if (!isset($this->request->post[$key])) {
|
326: | $this->request->post[$key] = '';
|
327: | }
|
328: | }
|
329: |
|
330: | if ((oc_strlen($this->request->post['firstname']) < 1) || (oc_strlen($this->request->post['firstname']) > 32)) {
|
331: | $json['error']['firstname'] = $this->language->get('error_firstname');
|
332: | }
|
333: |
|
334: | if ((oc_strlen($this->request->post['lastname']) < 1) || (oc_strlen($this->request->post['lastname']) > 32)) {
|
335: | $json['error']['lastname'] = $this->language->get('error_lastname');
|
336: | }
|
337: |
|
338: | if ((oc_strlen($this->request->post['address_1']) < 3) || (oc_strlen($this->request->post['address_1']) > 128)) {
|
339: | $json['error']['address_1'] = $this->language->get('error_address_1');
|
340: | }
|
341: |
|
342: | if ((oc_strlen($this->request->post['city']) < 2) || (oc_strlen($this->request->post['city']) > 128)) {
|
343: | $json['error']['city'] = $this->language->get('error_city');
|
344: | }
|
345: |
|
346: | $this->load->model('localisation/country');
|
347: |
|
348: | $country_info = $this->model_localisation_country->getCountry((int)$this->request->post['country_id']);
|
349: |
|
350: | if ($country_info && $country_info['postcode_required'] && (oc_strlen($this->request->post['postcode']) < 2 || oc_strlen($this->request->post['postcode']) > 10)) {
|
351: | $json['error']['postcode'] = $this->language->get('error_postcode');
|
352: | }
|
353: |
|
354: | if (!$country_info || $this->request->post['country_id'] == '') {
|
355: | $json['error']['country'] = $this->language->get('error_country');
|
356: | }
|
357: |
|
358: | if ($this->request->post['zone_id'] == '') {
|
359: | $json['error']['zone'] = $this->language->get('error_zone');
|
360: | }
|
361: |
|
362: |
|
363: | $this->load->model('account/custom_field');
|
364: |
|
365: | $custom_fields = $this->model_account_custom_field->getCustomFields($this->customer->getGroupId());
|
366: |
|
367: | foreach ($custom_fields as $custom_field) {
|
368: | if ($custom_field['location'] == 'address') {
|
369: | if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
|
370: | $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
|
371: | } 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']])) {
|
372: | $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
|
373: | }
|
374: | }
|
375: | }
|
376: |
|
377: | if (isset($this->request->get['address_id']) && ($this->customer->getAddressId() == $this->request->get['address_id']) && !$this->request->post['default']) {
|
378: | $json['error'] = $this->language->get('error_default');
|
379: | }
|
380: | }
|
381: |
|
382: | if (!$json) {
|
383: | $this->load->model('account/address');
|
384: |
|
385: |
|
386: | if (!isset($this->request->get['address_id'])) {
|
387: | $this->model_account_address->addAddress($this->customer->getId(), $this->request->post);
|
388: |
|
389: | $this->session->data['success'] = $this->language->get('text_add');
|
390: | }
|
391: |
|
392: |
|
393: | if (isset($this->request->get['address_id'])) {
|
394: | $this->model_account_address->editAddress($this->request->get['address_id'], $this->request->post);
|
395: |
|
396: |
|
397: | if (isset($this->session->data['shipping_address']) && ($this->session->data['shipping_address']['address_id'] == $this->request->get['address_id'])) {
|
398: | $this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getId(), $this->request->get['address_id']);
|
399: |
|
400: | unset($this->session->data['shipping_method']);
|
401: | unset($this->session->data['shipping_methods']);
|
402: | unset($this->session->data['payment_method']);
|
403: | unset($this->session->data['payment_methods']);
|
404: | }
|
405: |
|
406: |
|
407: | if (isset($this->session->data['payment_address']) && ($this->session->data['payment_address']['address_id'] == $this->request->get['address_id'])) {
|
408: | $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getId(), $this->request->get['address_id']);
|
409: |
|
410: | unset($this->session->data['shipping_method']);
|
411: | unset($this->session->data['shipping_methods']);
|
412: | unset($this->session->data['payment_method']);
|
413: | unset($this->session->data['payment_methods']);
|
414: | }
|
415: |
|
416: | $this->session->data['success'] = $this->language->get('text_edit');
|
417: | }
|
418: |
|
419: | $json['redirect'] = $this->url->link('account/address', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'], true);
|
420: | }
|
421: |
|
422: | $this->response->addHeader('Content-Type: application/json');
|
423: | $this->response->setOutput(json_encode($json));
|
424: | }
|
425: |
|
426: | |
427: | |
428: | |
429: | |
430: |
|
431: | public function delete(): void {
|
432: | $this->load->language('account/address');
|
433: |
|
434: | $json = [];
|
435: |
|
436: | if (isset($this->request->get['address_id'])) {
|
437: | $address_id = $this->request->get['address_id'];
|
438: | } else {
|
439: | $address_id = 0;
|
440: | }
|
441: |
|
442: | if (!$this->customer->isLogged() || (!isset($this->request->get['customer_token']) || !isset($this->session->data['customer_token']) || ($this->request->get['customer_token'] != $this->session->data['customer_token']))) {
|
443: | $this->session->data['redirect'] = $this->url->link('account/address', 'language=' . $this->config->get('config_language'));
|
444: |
|
445: | $json['redirect'] = $this->url->link('account/login', 'language=' . $this->config->get('config_language'), true);
|
446: | }
|
447: |
|
448: | if (!$json) {
|
449: | if ($this->customer->getAddressId() == $address_id) {
|
450: | $json['error'] = $this->language->get('error_default');
|
451: | }
|
452: |
|
453: | $this->load->model('account/address');
|
454: |
|
455: | if ($this->model_account_address->getTotalAddresses($this->customer->getId()) == 1) {
|
456: | $json['error'] = $this->language->get('error_delete');
|
457: | }
|
458: |
|
459: | $this->load->model('account/subscription');
|
460: |
|
461: | $subscription_total = $this->model_account_subscription->getTotalSubscriptionByShippingAddressId($address_id);
|
462: |
|
463: | if ($subscription_total) {
|
464: | $json['error'] = sprintf($this->language->get('error_subscription'), $subscription_total);
|
465: | }
|
466: |
|
467: | $subscription_total = $this->model_account_subscription->getTotalSubscriptionByPaymentAddressId($address_id);
|
468: |
|
469: | if ($subscription_total) {
|
470: | $json['error'] = sprintf($this->language->get('error_subscription'), $subscription_total);
|
471: | }
|
472: | }
|
473: |
|
474: | if (!$json) {
|
475: |
|
476: | $this->model_account_address->deleteAddress($this->customer->getId(), $address_id);
|
477: |
|
478: |
|
479: | if (isset($this->session->data['shipping_address']['address_id']) && ($this->session->data['shipping_address']['address_id'] == $address_id)) {
|
480: | unset($this->session->data['shipping_address']);
|
481: | unset($this->session->data['shipping_method']);
|
482: | unset($this->session->data['shipping_methods']);
|
483: | unset($this->session->data['payment_method']);
|
484: | unset($this->session->data['payment_methods']);
|
485: | }
|
486: |
|
487: |
|
488: | if (isset($this->session->data['payment_address']['address_id']) && ($this->session->data['payment_address']['address_id'] == $address_id)) {
|
489: | unset($this->session->data['payment_address']);
|
490: | unset($this->session->data['shipping_method']);
|
491: | unset($this->session->data['shipping_methods']);
|
492: | unset($this->session->data['payment_method']);
|
493: | unset($this->session->data['payment_methods']);
|
494: | }
|
495: |
|
496: | $json['success'] = $this->language->get('text_delete');
|
497: | }
|
498: |
|
499: | $this->response->addHeader('Content-Type: application/json');
|
500: | $this->response->setOutput(json_encode($json));
|
501: | }
|
502: | }
|
503: | |