1: <?php
2: namespace Opencart\Admin\Controller\User;
3: /**
4: * Class Profile
5: *
6: * @package Opencart\Admin\Controller\User
7: */
8: class Profile extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('user/profile');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: $data['breadcrumbs'] = [];
20:
21: $data['breadcrumbs'][] = [
22: 'text' => $this->language->get('text_home'),
23: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
24: ];
25:
26: $data['breadcrumbs'][] = [
27: 'text' => $this->language->get('heading_title'),
28: 'href' => $this->url->link('user/profile', 'user_token=' . $this->session->data['user_token'])
29: ];
30:
31: $data['save'] = $this->url->link('user/profile.save', 'user_token=' . $this->session->data['user_token']);
32: $data['back'] = $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token']);
33:
34: $this->load->model('user/user');
35:
36: $user_info = $this->model_user_user->getUser($this->user->getId());
37:
38: if (!empty($user_info)) {
39: $data['username'] = $user_info['username'];
40: } else {
41: $data['username'] = '';
42: }
43:
44: if (!empty($user_info)) {
45: $data['firstname'] = $user_info['firstname'];
46: } else {
47: $data['firstname'] = '';
48: }
49:
50: if (!empty($user_info)) {
51: $data['lastname'] = $user_info['lastname'];
52: } else {
53: $data['lastname'] = '';
54: }
55:
56: if (!empty($user_info)) {
57: $data['email'] = $user_info['email'];
58: } else {
59: $data['email'] = '';
60: }
61:
62: if (!empty($user_info)) {
63: $data['image'] = $user_info['image'];
64: } else {
65: $data['image'] = '';
66: }
67:
68: $this->load->model('tool/image');
69:
70: $data['placeholder'] = $this->model_tool_image->resize('no_image.png', $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
71:
72: if ($data['image'] && is_file(DIR_IMAGE . html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8'))) {
73: $data['thumb'] = $this->model_tool_image->resize($data['image'], $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
74: } else {
75: $data['thumb'] = $data['placeholder'];
76: }
77:
78: $data['header'] = $this->load->controller('common/header');
79: $data['column_left'] = $this->load->controller('common/column_left');
80: $data['footer'] = $this->load->controller('common/footer');
81:
82: $this->response->setOutput($this->load->view('user/profile', $data));
83: }
84:
85: /**
86: * Save
87: *
88: * @return void
89: */
90: public function save(): void {
91: $this->load->language('user/profile');
92:
93: $json = [];
94:
95: if (!$this->user->hasPermission('modify', 'user/profile')) {
96: $json['error']['warning'] = $this->language->get('error_permission');
97: }
98:
99: if ((oc_strlen($this->request->post['username']) < 3) || (oc_strlen($this->request->post['username']) > 20)) {
100: $json['error']['username'] = $this->language->get('error_username');
101: }
102:
103: $this->load->model('user/user');
104:
105: $user_info = $this->model_user_user->getUserByUsername($this->request->post['username']);
106:
107: if ($user_info && ($this->user->getId() != $user_info['user_id'])) {
108: $json['error']['warning'] = $this->language->get('error_username_exists');
109: }
110:
111: if (!oc_validate_length($this->request->post['firstname'], 1, 32)) {
112: $json['error']['firstname'] = $this->language->get('error_firstname');
113: }
114:
115: if (!oc_validate_length($this->request->post['lastname'], 1, 32)) {
116: $json['error']['lastname'] = $this->language->get('error_lastname');
117: }
118:
119: if ((oc_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
120: $json['error']['email'] = $this->language->get('error_email');
121: }
122:
123: $user_info = $this->model_user_user->getUserByEmail($this->request->post['email']);
124:
125: if ($user_info && ($this->user->getId() != $user_info['user_id'])) {
126: $json['error']['warning'] = $this->language->get('error_email_exists');
127: }
128:
129: if ($this->request->post['password']) {
130: if ((oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) < 6) || (oc_strlen(html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) > 40)) {
131: $json['error']['password'] = $this->language->get('error_password');
132: }
133:
134: if ($this->request->post['password'] != $this->request->post['confirm']) {
135: $json['error']['confirm'] = $this->language->get('error_confirm');
136: }
137: }
138:
139: if (!$json) {
140: $user_data = array_merge($this->request->post, [
141: 'user_group_id' => $this->user->getGroupId(),
142: 'status' => 1,
143: ]);
144:
145: $this->model_user_user->editUser($this->user->getId(), $user_data);
146:
147: $json['success'] = $this->language->get('text_success');
148: }
149:
150: $this->response->addHeader('Content-Type: application/json');
151: $this->response->setOutput(json_encode($json));
152: }
153: }
154: