1: <?php
2: namespace Opencart\Catalog\Controller\Api\Sale;
3: /**
4: * Class Customer
5: *
6: * @package Opencart\Catalog\Controller\Api\Sale
7: */
8: class Customer extends \Opencart\System\Engine\Controller {
9: /**
10: * @return void
11: */
12: public function index(): void {
13: $this->load->language('api/sale/customer');
14:
15: $json = [];
16:
17: $keys = [
18: 'customer_id',
19: 'customer_group_id',
20: 'firstname',
21: 'lastname',
22: 'email',
23: 'telephone',
24: 'account_custom_field'
25: ];
26:
27: foreach ($keys as $key) {
28: if (!isset($this->request->post[$key])) {
29: $this->request->post[$key] = '';
30: }
31: }
32:
33: $this->load->model('account/customer');
34:
35: if ($this->request->post['customer_id']) {
36: $customer_info = $this->model_account_customer->getCustomer($this->request->post['customer_id']);
37:
38: if (!$customer_info) {
39: $json['error']['warning'] = $this->language->get('error_customer');
40: }
41: }
42:
43: // Customer Group
44: if ($this->request->post['customer_group_id']) {
45: $customer_group_id = (int)$this->request->post['customer_group_id'];
46: } else {
47: $customer_group_id = (int)$this->config->get('config_customer_group_id');
48: }
49:
50: $this->load->model('account/customer_group');
51:
52: $customer_group_info = $this->model_account_customer_group->getCustomerGroup($customer_group_id);
53:
54: if (!$customer_group_info) {
55: $json['error']['warning'] = $this->language->get('error_customer_group');
56: }
57:
58: if ((oc_strlen($this->request->post['firstname']) < 1) || (oc_strlen($this->request->post['firstname']) > 32)) {
59: $json['error']['firstname'] = $this->language->get('error_firstname');
60: }
61:
62: if ((oc_strlen($this->request->post['lastname']) < 1) || (oc_strlen($this->request->post['lastname']) > 32)) {
63: $json['error']['lastname'] = $this->language->get('error_lastname');
64: }
65:
66: if ((oc_strlen($this->request->post['email']) > 96) || !filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL)) {
67: $json['error']['email'] = $this->language->get('error_email');
68: }
69:
70: if ($this->config->get('config_telephone_required') && (oc_strlen($this->request->post['telephone']) < 3) || (oc_strlen($this->request->post['telephone']) > 32)) {
71: $json['error']['telephone'] = $this->language->get('error_telephone');
72: }
73:
74: // Custom field validation
75: $this->load->model('account/custom_field');
76:
77: $custom_fields = $this->model_account_custom_field->getCustomFields((int)$customer_group_id);
78:
79: foreach ($custom_fields as $custom_field) {
80: if ($custom_field['location'] == 'account') {
81: if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
82: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
83: } 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']])) {
84: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
85: }
86: }
87: }
88:
89: if (!$json) {
90: $this->session->data['customer'] = [
91: 'customer_id' => $this->request->post['customer_id'],
92: 'customer_group_id' => $this->request->post['customer_group_id'],
93: 'firstname' => $this->request->post['firstname'],
94: 'lastname' => $this->request->post['lastname'],
95: 'email' => $this->request->post['email'],
96: 'telephone' => $this->request->post['telephone'],
97: 'custom_field' => !empty($this->request->post['custom_field']) && is_array($this->request->post['custom_field']) ? $this->request->post['custom_field'] : []
98: ];
99:
100: $json['success'] = $this->language->get('text_success');
101:
102: unset($this->session->data['reward']);
103: }
104:
105: $this->response->addHeader('Content-Type: application/json');
106: $this->response->setOutput(json_encode($json));
107: }
108: }
109: