1: <?php
2: namespace Opencart\Catalog\Controller\Account;
3: /**
4: * Class Edit
5: *
6: * @package Opencart\Catalog\Controller\Account
7: */
8: class Edit extends \Opencart\System\Engine\Controller {
9: /**
10: * @return void
11: */
12: public function index(): void {
13: $this->load->language('account/edit');
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/edit', '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:
25: $data['breadcrumbs'][] = [
26: 'text' => $this->language->get('text_home'),
27: 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
28: ];
29:
30: $data['breadcrumbs'][] = [
31: 'text' => $this->language->get('text_account'),
32: 'href' => $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
33: ];
34:
35: $data['breadcrumbs'][] = [
36: 'text' => $this->language->get('text_edit'),
37: 'href' => $this->url->link('account/edit', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'])
38: ];
39:
40: $data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));
41:
42: $data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);
43: $data['config_telephone_display'] = $this->config->get('config_telephone_display');
44: $data['config_telephone_required'] = $this->config->get('config_telephone_required');
45:
46: $data['save'] = $this->url->link('account/edit.save', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']);
47:
48: $this->session->data['upload_token'] = oc_token(32);
49:
50: $data['upload'] = $this->url->link('tool/upload', 'language=' . $this->config->get('config_language') . '&upload_token=' . $this->session->data['upload_token']);
51:
52: $this->load->model('account/customer');
53:
54: $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
55:
56: $data['firstname'] = $customer_info['firstname'];
57: $data['lastname'] = $customer_info['lastname'];
58: $data['email'] = $customer_info['email'];
59: $data['telephone'] = $customer_info['telephone'];
60:
61: // Custom Fields
62: $data['custom_fields'] = [];
63:
64: $this->load->model('account/custom_field');
65:
66: $custom_fields = $this->model_account_custom_field->getCustomFields($this->customer->getGroupId());
67:
68: foreach ($custom_fields as $custom_field) {
69: if ($custom_field['location'] == 'account') {
70: $data['custom_fields'][] = $custom_field;
71: }
72: }
73:
74: $data['account_custom_field'] = $customer_info['custom_field'];
75:
76: $data['back'] = $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token']);
77:
78: $data['language'] = $this->config->get('config_language');
79:
80: $data['column_left'] = $this->load->controller('common/column_left');
81: $data['column_right'] = $this->load->controller('common/column_right');
82: $data['content_top'] = $this->load->controller('common/content_top');
83: $data['content_bottom'] = $this->load->controller('common/content_bottom');
84: $data['footer'] = $this->load->controller('common/footer');
85: $data['header'] = $this->load->controller('common/header');
86:
87: $this->response->setOutput($this->load->view('account/edit', $data));
88: }
89:
90: /**
91: * Save
92: *
93: * @return void
94: */
95: public function save(): void {
96: $this->load->language('account/edit');
97:
98: $json = [];
99:
100: 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']))) {
101: $this->session->data['redirect'] = $this->url->link('account/edit', 'language=' . $this->config->get('config_language'));
102:
103: $json['redirect'] = $this->url->link('account/login', 'language=' . $this->config->get('config_language'), true);
104: }
105:
106: if (!$json) {
107: $keys = [
108: 'firstname',
109: 'lastname',
110: 'email',
111: 'telephone'
112: ];
113:
114: foreach ($keys as $key) {
115: if (!isset($this->request->post[$key])) {
116: $this->request->post[$key] = '';
117: }
118: }
119:
120: if ((oc_strlen($this->request->post['firstname']) < 1) || (oc_strlen($this->request->post['firstname']) > 32)) {
121: $json['error']['firstname'] = $this->language->get('error_firstname');
122: }
123:
124: if ((oc_strlen($this->request->post['lastname']) < 1) || (oc_strlen($this->request->post['lastname']) > 32)) {
125: $json['error']['lastname'] = $this->language->get('error_lastname');
126: }
127:
128: if ((oc_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
129: $json['error']['email'] = $this->language->get('error_email');
130: }
131:
132: $this->load->model('account/customer');
133:
134: if (($this->customer->getEmail() != $this->request->post['email']) && $this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
135: $json['error']['warning'] = $this->language->get('error_exists');
136: }
137:
138: if ($this->config->get('config_telephone_required') && (oc_strlen($this->request->post['telephone']) < 3) || (oc_strlen($this->request->post['telephone']) > 32)) {
139: $json['error']['telephone'] = $this->language->get('error_telephone');
140: }
141:
142: // Custom field validation
143: $this->load->model('account/custom_field');
144:
145: $custom_fields = $this->model_account_custom_field->getCustomFields($this->customer->getGroupId());
146:
147: foreach ($custom_fields as $custom_field) {
148: if ($custom_field['location'] == 'account') {
149: if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
150: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
151: } 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']])) {
152: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
153: }
154: }
155: }
156: }
157:
158: if (!$json) {
159: // Update customer in db
160: $this->model_account_customer->editCustomer($this->customer->getId(), $this->request->post);
161:
162: $this->session->data['success'] = $this->language->get('text_success');
163:
164: // Update customer session details
165: $this->session->data['customer'] = [
166: 'customer_id' => $this->customer->getId(),
167: 'customer_group_id' => $this->customer->getGroupId(),
168: 'firstname' => $this->request->post['firstname'],
169: 'lastname' => $this->request->post['lastname'],
170: 'email' => $this->request->post['email'],
171: 'telephone' => $this->request->post['telephone'],
172: 'custom_field' => $this->request->post['custom_field'] ?? []
173: ];
174:
175: unset($this->session->data['shipping_method']);
176: unset($this->session->data['shipping_methods']);
177: unset($this->session->data['payment_method']);
178: unset($this->session->data['payment_methods']);
179:
180: $json['redirect'] = $this->url->link('account/account', 'language=' . $this->config->get('config_language') . '&customer_token=' . $this->session->data['customer_token'], true);
181: }
182:
183: $this->response->addHeader('Content-Type: application/json');
184: $this->response->setOutput(json_encode($json));
185: }
186: }
187: